Files
ten31-transcripts/Ten31Transcripts/Backend/VoiceprintStore.swift
T
Grant Gilliam 53d7fcdac0 Client: dual-channel label-merge (mic_file + system_file)
The backend shipped dual-channel mode; wire the client to it. We already capture
mic (you) and system (others) separately, so send them as two files instead of the
mono mix — fixing the misattribution at the source.

- SparkControlClient: labelMergeDual(mic_file, system_file, self_name, self_vad);
  multipart generalized to N files; shared POST/retry/decode extracted.
- SessionPackager.rebasedSelfVadData: chunk-local [{start,end}] for self_vad;
  sliceAudio reused for both tracks.
- TranscriptPipeline.process: dual-channel chunking (slice mic+system, rebase
  timeline + self_vad per chunk) when system audio is healthy; mono mixed-file
  fallback (self folded into the timeline) otherwise.
- VisualCapture.finish: write the full visual_timeline.json (remote + self merged)
  but return REMOTE (vision) segments only — self travels via the mic channel.
- TranscriptAssembler: rank mic_channel highest (the user's own track wins).
- VoiceprintStore: store the clean mic_channel self voiceprint.
- SessionController: pass mic/system URLs + remote timeline + channel self-spans +
  self_name + systemHealthy; self_vad.json now reflects the channel-verified spans.

Validated END-TO-END against the live backend on the real misattributing session:
'Go Bitcoin' (remote) is now attributed to Unknown_0, NOT the user; the user's own
lines come back source=mic_channel; per-channel ASR recovered fuller remote text.
36/36 XCTest (4 new: self_vad rebase, mic_channel ranking + voiceprint storage).
2026-06-06 13:15:29 -05:00

106 lines
3.7 KiB
Swift

import Foundation
/// Local persistence of named voiceprints the compounding-identity layer.
///
/// File `~/Ten31Transcripts/voiceprints.json`:
/// `{ "<name>": { "vector": [192 floats], "updated": <iso>, "calls": <int> } }`
///
/// On send `knownVoiceprints()` feeds `label-merge`. On response `update(with:)`
/// stores/refreshes vectors for speakers resolved by **visual** (overlap ~0.8)
/// or **voiceprint** match. Never stores `Unknown_N` / `Speaker_unknown`.
///
/// Thread-safe (lock-guarded); the sequential pipeline is the only writer.
final class VoiceprintStore {
struct Entry: Codable, Equatable {
var vector: [Float]
var updated: String
var calls: Int
}
private let url: URL
private let minOverlapToStore: Double
private let lock = NSLock()
private var entriesStore: [String: Entry] = [:]
init(fileURL: URL, minOverlapToStore: Double = 0.8) {
self.url = fileURL
self.minOverlapToStore = minOverlapToStore
load()
}
var entries: [String: Entry] {
lock.lock(); defer { lock.unlock() }
return entriesStore
}
/// Vectors keyed by name, for the `known_voiceprints` field.
func knownVoiceprints() -> [String: [Float]] {
lock.lock(); defer { lock.unlock() }
return entriesStore.mapValues { $0.vector }
}
/// Persist fingerprints from a `label-merge` response for confidently-named
/// speakers only.
func update(with response: LabelMergeResponse) {
lock.lock(); defer { lock.unlock() }
let now = ISO8601DateFormatter().string(from: Date())
for sp in response.speakers {
guard !Self.isUnknown(sp.name) else { continue }
let acceptable: Bool
switch sp.source {
case "mic_channel": acceptable = true // the user's own clean mic voiceprint
case "visual": acceptable = (sp.overlapConfidence ?? 0) >= minOverlapToStore
case "voiceprint": acceptable = true // already matched a known print
default: acceptable = false // unmatched
}
guard acceptable, let vector = sp.fingerprint ?? response.fingerprints[sp.name],
!vector.isEmpty else { continue }
var entry = entriesStore[sp.name] ?? Entry(vector: vector, updated: now, calls: 0)
entry.vector = vector
entry.updated = now
entry.calls += 1
entriesStore[sp.name] = entry
}
save()
}
func rename(_ old: String, to new: String) {
lock.lock(); defer { lock.unlock() }
guard let e = entriesStore.removeValue(forKey: old) else { return }
entriesStore[new] = e
save()
}
func remove(_ name: String) {
lock.lock(); defer { lock.unlock() }
entriesStore.removeValue(forKey: name)
save()
}
func reset() {
lock.lock(); defer { lock.unlock() }
entriesStore = [:]
save()
}
// MARK: - Persistence (call with lock held)
private func load() {
guard let data = try? Data(contentsOf: url),
let decoded = try? JSONDecoder().decode([String: Entry].self, from: data) else { return }
entriesStore = decoded
}
private func save() {
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
try? FileManager.default.createDirectory(at: url.deletingLastPathComponent(),
withIntermediateDirectories: true)
if let data = try? encoder.encode(entriesStore) { try? data.write(to: url) }
}
private static func isUnknown(_ name: String) -> Bool {
LabelMergeResponse.isUnknownName(name)
}
}