知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.
Swift androidlinuxlive-captionsmacosmeeting-notesmenubar-appsimultaneous-interpretationspeech-recognitionsubtitlesswiftswiftuitranslationwindows
import Accelerate
import AVFoundation
import CoreML
import Combine
import Foundation
// audio becomes a voiceprint (WeSpeaker ResNet34-LM, VoxCeleb, CC BY 4.0, bundled as Core ML),
enum Fbank {
static let bins = 80
static let frameLength = 400
static let shift = 160
private static let fftSize = 512
private static let window: [Float] = (0..<frameLength).map {
pow(0.5 - 0.5 * cos(2 * Float.pi * Float($0) / Float(frameLength - 1)), 0.85)
}
private static let melBanks: [Float] = {
func mel(_ f: Double) -> Double { 1127 * log(1 + f / 700) }
let cols = fftSize / 2 + 1
let lo = mel(20), hi = mel(8000), delta = (hi - lo) / Double(bins + 1)
var w = [Float](repeating: 0, count: bins * cols)
for m in 0..<bins {
let left = lo + Double(m) * delta, center = left + delta, right = center + delta
for k in 0..<(fftSize / 2) {
let x = mel(Double(k) * 16000 / Double(fftSize))
let v = min((x - left) / (center - left), (right - x) / (right - center))
if v > 0 { w[m * cols + k] = Float(v) }
}
}
return w
}()
private static let setup = vDSP_create_fftsetup(9, FFTRadix(kFFTRadix2))!
static func compute(_ samples: [Float]) -> [Float] {
guard samples.count >= frameLength else { return [] }
let frames = 1 + (samples.count - frameLength) / shift
let cols = fftSize / 2 + 1
var out = [Float](repeating: 0, count: frames * bins)
var frame = [Float](repeating: 0, count: fftSize)
var re = [Float](repeating: 0, count: fftSize / 2), im = re
var power = [Float](repeating: 0, count: cols)
var mels = [Float](repeating: 0, count: bins)
for f in 0..<frames {
let start = f * shift
for i in 0..<frameLength { frame[i] = samples[start + i] * 32768 }
for i in frameLength..<fftSize { frame[i] = 0 }
var mean: Float = 0
vDSP_meanv(frame, 1, &mean, vDSP_Length(frameLength))
for i in 0..<frameLength { frame[i] -= mean }
for i in stride(from: frameLength - 1, to: 0, by: -1) { frame[i] -= 0.97 * frame[i - 1] }
frame[0] -= 0.97 * frame[0]
vDSP_vmul(frame, 1, window, 1, &frame, 1, vDSP_Length(frameLength))
re.withUnsafeMutableBufferPointer { rp in
im.withUnsafeMutableBufferPointer { ip in
var split = DSPSplitComplex(realp: rp.baseAddress!, imagp: ip.baseAddress!)
frame.withUnsafeBytes { raw in
vDSP_ctoz(raw.bindMemory(to: DSPComplex.self).baseAddress!, 2, &split, 1, vDSP_Length(fftSize / 2))
}
vDSP_fft_zrip(setup, &split, 1, 9, FFTDirection(FFT_FORWARD))
}
}
power[0] = re[0] * re[0] / 4
power[cols - 1] = im[0] * im[0] / 4
for k in 1..<(fftSize / 2) { power[k] = (re[k] * re[k] + im[k] * im[k]) / 4 }
vDSP_mmul(melBanks, 1, power, 1, &mels, 1, vDSP_Length(bins), 1, vDSP_Length(cols))
for b in 0..<bins { out[f * bins + b] = log(max(mels[b], .ulpOfOne)) }
}
return out
}
}
final class Voiceprints: @unchecked Sendable {
static let minSeconds = 0.5
static let maxSeconds = 20.0
private let model: MLModel
private init(model: MLModel) { self.model = model }
static func load(packageURL: URL? = nil) throws -> Voiceprints {
guard let pkg = packageURL ?? Bundle.main.url(forResource: "SpeakerEmbedding", withExtension: "mlpackage") else {
throw CocoaError(.fileNoSuchFile)
}
let size = (try? FileManager.default.attributesOfItem(atPath: pkg.appendingPathComponent("Data/com.apple.CoreML/weights/weight.bin").path)[.size] as? Int) ?? 0
let cache = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]
.appendingPathComponent(DevBuild.supportFolder).appendingPathComponent("SpeakerEmbedding-\(size).mlmodelc")
if !FileManager.default.fileExists(atPath: cache.path) {
try? FileManager.default.createDirectory(at: cache.deletingLastPathComponent(), withIntermediateDirectories: true)
let compiled = try MLModel.compileModel(at: pkg)
try? FileManager.default.removeItem(at: cache)
try FileManager.default.moveItem(at: compiled, to: cache)
}
let config = MLModelConfiguration()
config.computeUnits = .cpuAndGPU
return Voiceprints(model: try MLModel(contentsOf: cache, configuration: config))
}
static func samples(of url: URL) -> [Float]? {
guard let file = try? AVAudioFile(forReading: url),
let format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 16000, channels: 1, interleaved: false),
let input = AVAudioPCMBuffer(pcmFormat: file.processingFormat, frameCapacity: AVAudioFrameCount(file.length)),
(try? file.read(into: input)) != nil,
let out = PCMConverter().convert(input, to: format), let d = out.floatChannelData else { return nil }
return Array(UnsafeBufferPointer(start: d[0], count: Int(out.frameLength)))
}
func embed(_ samples: [Float]) -> [Float]? {
guard Double(samples.count) >= Self.minSeconds * 16000 else { return nil }
let clip = samples.count > Int(Self.maxSeconds * 16000) ? Array(samples.prefix(Int(Self.maxSeconds * 16000))) : samples
var feats = Fbank.compute(clip)
let frames = feats.count / Fbank.bins
guard frames >= 50 else { return nil }
for b in 0..<Fbank.bins {
var sum: Float = 0
for f in 0..<frames { sum += feats[f * Fbank.bins + b] }
let mean = sum / Float(frames)
for f in 0..<frames { feats[f * Fbank.bins + b] -= mean }
}
guard let input = try? MLMultiArray(shape: [1, NSNumber(value: frames), NSNumber(value: Fbank.bins)], dataType: .float32) else { return nil }
feats.withUnsafeBufferPointer { src in
input.withUnsafeMutableBytes { dst, _ in
dst.copyMemory(from: UnsafeRawBufferPointer(src))
}
}
guard let out = try? model.prediction(from: MLDictionaryFeatureProvider(dictionary: ["feats": input])),
let embs = out.featureValue(for: "embs")?.multiArrayValue else { return nil }
var v = (0..<embs.count).map { Float(truncating: embs[$0]) }
var norm: Float = 0
vDSP_svesq(v, 1, &norm, vDSP_Length(v.count))
guard norm > 0 else { return nil }
vDSP_vsdiv(v, 1, [norm.squareRoot()], &v, 1, vDSP_Length(v.count))
return v
}
}
struct VoiceClusterer {
static let newSeconds = 2.0
static let join: Float = 0.5
static let startNew: Float = 0.5
private(set) var sums: [[Float]] = []
func centroid(_ voice: Int) -> [Float]? {
guard sums.indices.contains(voice) else { return nil }
return SpeakerEmbedding.normalized(sums[voice])
}
func closest(to embedding: [Float], minimum: Float) -> Int? {
guard let probe = SpeakerEmbedding.normalized(embedding) else { return nil }
var best: Int?
var bestScore = minimum
for i in sums.indices {
guard let c = centroid(i), let score = SpeakerEmbedding.cosine(probe, c) else { continue }
if score >= bestScore { best = i; bestScore = score }
}
return best
}
mutating func seed(_ embedding: [Float]) -> Int? {
guard let v = SpeakerEmbedding.normalized(embedding) else { return nil }
sums.append(v)
return sums.count - 1
}
mutating func adopt(_ embedding: [Float], into voice: Int) {
guard sums.indices.contains(voice), let v = SpeakerEmbedding.normalized(embedding) else { return }
vDSP_vadd(sums[voice], 1, v, 1, &sums[voice], 1, vDSP_Length(sums[voice].count))
}
mutating func merge(_ from: Int, into: Int) {
guard sums.indices.contains(from), sums.indices.contains(into), from != into else { return }
vDSP_vadd(sums[into], 1, sums[from], 1, &sums[into], 1, vDSP_Length(sums[into].count))
sums[from] = [Float](repeating: 0, count: sums[from].count)
}
mutating func label(_ v: [Float], seconds: Double) -> Int? {
var best = -1, bestSim: Float = -1
for (i, s) in sums.enumerated() {
var dot: Float = 0, sq: Float = 0
vDSP_dotpr(v, 1, s, 1, &dot, vDSP_Length(v.count))
vDSP_svesq(s, 1, &sq, vDSP_Length(s.count))
let sim = dot / max(sq.squareRoot(), .ulpOfOne)
if sim > bestSim { bestSim = sim; best = i }
}
if best >= 0, bestSim >= Self.join {
vDSP_vadd(sums[best], 1, v, 1, &sums[best], 1, vDSP_Length(v.count))
return best
}
if seconds >= Self.newSeconds, sums.isEmpty || bestSim < Self.startNew {
sums.append(v)
return sums.count - 1
}
return nil
}
}
enum SpeakerLabels {
static let shownAfter = 5
static func substantive(_ text: String) -> Bool {
let words = text.split(whereSeparator: { $0.isWhitespace }).filter { $0.contains(where: \.isLetter) }
if words.count >= 3 { return true }
return text.unicodeScalars.filter { $0.properties.isIdeographic || (0x3040...0x30FF).contains($0.value) || (0xAC00...0xD7A3).contains($0.value) }.count >= 4
}
static func voiceNumbers(_ lines: [TranscriptLine], known: Set<Int> = []) -> [Int: Int] {
var count: [Int: Int] = [:], reached: [Int: Int] = [:]
for (i, l) in lines.enumerated() where l.speaker == "them" {
guard let v = l.voice, substantive(l.src) else { continue }
count[v, default: 0] += 1
if reached[v] == nil {
if known.contains(v) {
reached[v] = i
} else if count[v] == shownAfter {
reached[v] = i
}
}
}
let shown = reached.keys.sorted { reached[$0]! < reached[$1]! }
guard shown.count >= 2 || !known.isEmpty else { return [:] }
return Dictionary(uniqueKeysWithValues: shown.enumerated().map { ($1, $0 + 1) })
}
static func knownVoices(_ s: Session) -> Set<Int> {
var known = Set((s.voiceProfiles ?? [:]).keys.compactMap(Int.init))
if let only = singleVoice(s.lines), let name = s.voiceNames?[String(only)], !name.isEmpty { known.insert(only) }
return known
}
static func voiceNumbers(_ s: Session) -> [Int: Int] {
voiceNumbers(s.lines, known: knownVoices(s))
}
static func singleVoice(_ lines: [TranscriptLine]) -> Int? {
let voices = Set(lines.filter { $0.speaker == "them" }.compactMap(\.voice))
if voices.count == 1 { return voices.first }
let substantiveVoices = Set(lines.filter { $0.speaker == "them" && substantive($0.src) }
.compactMap(\.voice))
return substantiveVoices.count == 1 ? substantiveVoices.first : nil
}
static func numbers(_ lines: [TranscriptLine], known: Set<Int> = []) -> [String: Int] {
let number = voiceNumbers(lines, known: known)
var out: [String: Int] = [:]
for l in lines where l.speaker == "them" {
if let v = l.voice, let n = number[v] { out[l.id] = n }
}
return out
}
static func numbers(_ s: Session) -> [String: Int] {
numbers(s.lines, known: knownVoices(s))
}
static func names(_ s: Session) -> [Int: String] {
let known = knownVoices(s)
guard s.voiceNames != nil || s.voiceGuesses != nil || !known.isEmpty else { return [:] }
var out: [Int: String] = [:]
for (v, n) in voiceNumbers(s.lines, known: known) {
if let mine = s.voiceNames?[String(v)] {
if !mine.isEmpty { out[n] = mine }
} else if let p = s.voiceProfiles?[String(v)], !p.name.isEmpty {
out[n] = p.name
} else if let g = s.voiceGuesses?[String(v)] {
out[n] = g.name
}
}
return out
}
static func guessed(_ s: Session) -> [Int: VoiceGuess] {
var out: [Int: VoiceGuess] = [:]
for (v, n) in voiceNumbers(s.lines, known: knownVoices(s)) where s.voiceNames?[String(v)] == nil {
if let g = s.voiceGuesses?[String(v)] { out[n] = g }
}
return out
}
static func runStarts(_ lines: [TranscriptLine], _ numbers: [String: Int]) -> Set<String> {
var out: Set<String> = [], last: String?
for l in lines {
let key = l.speaker == "me" ? "me" : numbers[l.id].map { "n\($0)" } ?? "?"
if key != last { out.insert(l.id) }
last = key
}
return out
}
static func label(_ number: Int, _ names: [Int: String]) -> String { names[number] ?? L("说话人 \(number)") }
static func named(_ notes: MeetingNotes, _ names: [Int: String]) -> MeetingNotes {
guard !names.isEmpty else { return notes }
func swap(_ text: String) -> String {
guard text.contains("说话人") || text.contains("Speaker") else { return text }
var out = text
for (n, name) in names.sorted(by: { $0.key > $1.key }) {
for form in ["说话人 \(n)", "说话人\(n)", "Speaker \(n)"] { out = out.replacingOccurrences(of: form, with: name) }
}
return out
}
var n = notes
n.summary = swap(n.summary)
n.topics = n.topics.map { t in var t = t; t.title = swap(t.title); t.summary = swap(t.summary); t.points = t.points.map(swap); return t }
n.conclusions = n.conclusions.map { var i = $0; i.text = swap(i.text); i.owner = i.owner.map(swap); return i }
n.todos = n.todos.map { var i = $0; i.text = swap(i.text); i.owner = i.owner.map(swap); return i }
return n
}
static func shownCount(_ lines: [TranscriptLine]) -> Int { Set(numbers(lines).values).count }
}
struct SpeakerAssignment: Equatable {
var voice: Int?
var match: SpeakerProfileMatch?
}
@MainActor
final class SpeakerTagger {
private let queue = DispatchQueue(label: "com.wquguru.cicada.voiceprint", qos: .utility)
private nonisolated(unsafe) var model: Voiceprints?
private nonisolated(unsafe) var loadFailed = false
private nonisolated(unsafe) var clusterer = VoiceClusterer()
private nonisolated(unsafe) var voiceProfiles: [Int: UUID] = [:]
private nonisolated(unsafe) var suggestedVoices: Set<Int> = []
var profiles: SpeakerProfileStore?
var policy = SpeakerMatchPolicy()
var onVoice: ((_ lineID: String, _ voice: Int) -> Void)?
var onProfile: ((_ lineID: String, _ voice: Int, _ match: SpeakerProfileMatch) -> Void)?
var onSuggestion: ((_ lineID: String, _ voice: Int, _ match: SpeakerProfileMatch) -> Void)?
func reset() {
queue.async { [self] in
clusterer = VoiceClusterer()
voiceProfiles = [:]
suggestedVoices = []
}
}
func merge(_ from: Int, into: Int) { queue.async { [self] in clusterer.merge(from, into: into) } }
func bind(voice: Int, profile: SpeakerProfile) {
queue.async { [self] in
voiceProfiles[voice] = profile.id
suggestedVoices.insert(voice)
clusterer.adopt(profile.embedding, into: voice)
}
}
func centroid(voice: Int, _ done: @escaping @MainActor ([Float]?) -> Void) {
queue.async { [self] in
let v = clusterer.centroid(voice)
DispatchQueue.main.async { MainActor.assumeIsolated { done(v) } }
}
}
func tag(lineID: String, samples: [Float], done: @escaping @MainActor () -> Void = {}) {
voice(of: samples) { [weak self] assignment in
if let voice = assignment.voice { self?.onVoice?(lineID, voice) }
if let voice = assignment.voice, let match = assignment.match {
if match.kind == .auto { self?.onProfile?(lineID, voice, match) }
else { self?.onSuggestion?(lineID, voice, match) }
}
done()
}
}
func voice(of samples: [Float], _ done: @escaping @MainActor (SpeakerAssignment) -> Void) {
let seconds = Double(samples.count) / 16000
let profiles = profiles
let policy = policy
queue.async { [self] in
if model == nil, !loadFailed {
do { model = try Voiceprints.load() } catch {
loadFailed = true
debugLog("voiceprint model failed to load: \(error)")
}
}
guard let embedding = model.flatMap({ $0.embed(samples) }) else {
DispatchQueue.main.async { MainActor.assumeIsolated { done(SpeakerAssignment()) } }
return
}
var voice: Int?
var matched = profiles?.match(embedding, policy: policy)
if let match = matched, match.kind == .auto, seconds < VoiceClusterer.newSeconds {
matched = SpeakerProfileMatch(profile: match.profile, similarity: match.similarity, kind: .suggested)
}
if let match = matched, match.kind == .auto {
if let existing = voiceProfiles.first(where: { $0.value == match.profile.id })?.key {
voice = existing
clusterer.adopt(embedding, into: existing)
matched = nil
} else if let nearby = clusterer.closest(to: match.profile.embedding, minimum: VoiceClusterer.join),
let bound = voiceProfiles[nearby] {
voice = clusterer.label(embedding, seconds: seconds)
matched = bound == match.profile.id ? nil : matched.flatMap {
SpeakerProfileMatch(profile: $0.profile, similarity: $0.similarity, kind: .suggested)
}
} else {
let nearby = clusterer.closest(to: match.profile.embedding, minimum: VoiceClusterer.join)
voice = nearby ?? clusterer.seed(match.profile.embedding)
if let voice {
voiceProfiles[voice] = match.profile.id
clusterer.adopt(embedding, into: voice)
}
}
} else {
voice = clusterer.label(embedding, seconds: seconds)
if let v = voice, matched != nil, voiceProfiles[v] != nil { matched = nil }
}
if let match = matched, let v = voice, match.kind == .suggested, suggestedVoices.contains(v) {
matched = nil
}
if let match = matched, match.kind == .suggested, let v = voice { suggestedVoices.insert(v) }
let assignment = SpeakerAssignment(voice: voice, match: matched)
DispatchQueue.main.async { MainActor.assumeIsolated { done(assignment) } }
}
}
}
// MARK: - Names from the conversation
enum SpeakerNameGuess {
static func content(_ s: Session) -> String? {
let numbers = SpeakerLabels.numbers(s)
guard !numbers.isEmpty else { return nil }
let lines = s.lines.suffix(400)
var out = "Meeting transcript. Speakers: 你 = the user; 说话人N = one person on the other side, told apart by voice; 对方 = someone on the other side, not identified.\n"
for (i, l) in lines.enumerated() {
let who = l.speaker == "me" ? "你" : numbers[l.id].map { "说话人\($0)" } ?? "对方"
out += "[L\(i + 1) \(TranscriptExport.clock(l.t)) \(who)] \(l.shown)\n"
}
return out
}
static let instruction = """
Who are the people labelled 说话人N? Name one only when the transcript makes it clear: they \
introduce themselves on a line labelled with their own 说话人N; someone addresses them by name just \
before or after they speak ("Thanks, George" right after 说话人1's answer: 说话人1 is George); \
or others name them as they speak. A name on a 对方 line, or far from any line of 说话人N (an advert, \
a clip, people introducing themselves on another show), is not theirs. Speech recognition writes \
names by ear and often misspells them: for a public figure (an official, an executive, a presenter) \
recognisable from the name and what is said, write the name as the news spells it ("Jensen Hwang" \
→ "Jensen Huang"); otherwise as written. Leave out anyone \
you are unsure of. Reply as JSON: {"speakers": [{"speaker": 2, "name": "George", "evidence": "L12"}]}, \
the evidence being the line with the name; an empty list if none. Check each name's spelling before replying.
"""
static func parse(_ reply: String, _ s: Session) -> [Int: VoiceGuess] { read(reply, s).found }
static let evidenceReach = 2
static func read(_ reply: String, _ s: Session) -> (found: [Int: VoiceGuess], refuted: [Int: String]) {
var text = reply
if let a = text.firstIndex(of: "{"), let b = text.lastIndex(of: "}"), a < b { text = String(text[a...b]) }
guard let data = text.data(using: .utf8),
let obj = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
let list = obj["speakers"] as? [[String: Any]] else { return ([:], [:]) }
let voiceOf = Dictionary(uniqueKeysWithValues: SpeakerLabels.voiceNumbers(s).map { ($1, $0) })
let lines = Array(s.lines.suffix(400))
func count(_ v: Int) -> Int { s.lines.filter { $0.voice == v }.count }
var out: [Int: VoiceGuess] = [:], refuted: [Int: String] = [:]
for item in list {
guard let n = (item["speaker"] as? Int) ?? Int("\(item["speaker"] ?? "")"), let v = voiceOf[n],
let name = (item["name"] as? String)?.trimmingCharacters(in: .whitespacesAndNewlines),
!name.isEmpty, name.count <= 40 else { continue }
let e = (item["evidence"] as? String) ?? "\(item["evidence"] ?? "")"
guard let i = Int(String(e.drop(while: { !$0.isNumber }).prefix(while: \.isNumber))).map({ $0 - 1 }),
lines.indices.contains(i),
lines[max(0, i - evidenceReach)...min(lines.count - 1, i + evidenceReach)]
.contains(where: { $0.speaker == "them" && $0.voice == v }) else {
refuted[v] = name
continue
}
if let other = out.first(where: { $0.value.name == name })?.key {
if count(other) >= count(v) { continue }
out[other] = nil
}
let l = lines[i]
let evidence = "\(TranscriptExport.clock(l.t)) “\(l.shown.count > 60 ? String(l.shown.prefix(60)) + "…" : l.shown)”"
out[v] = VoiceGuess(name: name, evidence: evidence)
}
return (out, refuted)
}
static func holds(_ g: VoiceGuess, voice v: Int, in s: Session) -> Bool {
guard let open = g.evidence.firstIndex(of: "“") else { return true }
let clock = g.evidence[..<open].trimmingCharacters(in: .whitespaces)
let quote = String(g.evidence[g.evidence.index(after: open)...].dropLast().prefix(while: { $0 != "…" }))
guard let i = s.lines.firstIndex(where: { TranscriptExport.clock($0.t) == clock && $0.shown.hasPrefix(quote) }) else { return true }
return s.lines[max(0, i - evidenceReach)...min(s.lines.count - 1, i + evidenceReach)]
.contains { $0.speaker == "them" && $0.voice == v }
}
}
struct SpeakerUndo: Identifiable {
let id = UUID()
let session: String
let snapshot: SessionStore.VoiceSnapshot
var text: String
}
@MainActor
final class SpeakerNamer: ObservableObject {
@Published private(set) var busy: Set<String> = []
private var askedAt: [String: Int] = [:]
private let ling: LingClient
private let store: SessionStore
init(ling: LingClient, store: SessionStore) {
self.ling = ling
self.store = store
}
var available: Bool { ling.hasKey }
func guess(_ id: String, force: Bool = false) {
guard ling.hasKey, !busy.contains(id), let s = store.session(id) else { return }
guard force || s.lines.count >= (askedAt[id] ?? -1000) + 20, let content = SpeakerNameGuess.content(s) else { return }
let numbers = SpeakerLabels.voiceNumbers(s)
if numbers.keys.allSatisfy({ s.voiceNames?[String($0)] != nil }) { return }
busy.insert(id)
askedAt[id] = s.lines.count
Task {
defer { busy.remove(id) }
do {
let reply = try await ling.chat(content: content, instruction: SpeakerNameGuess.instruction,
priority: .notes, json: true, maxTokens: 300, role: "speakers")
let (found, read) = SpeakerNameGuess.read(reply, s)
var refuted = read
for (key, g) in s.voiceGuesses ?? [:] {
if let v = Int(key), found[v] == nil, !SpeakerNameGuess.holds(g, voice: v, in: s) { refuted[v] = g.name }
}
store.setVoiceGuesses(id, found, refuted: refuted)
debugLog("speaker names: \(found.count) found, \(refuted.count) without evidence for \(id)")
contentLog("speaker names reply: \(reply)")
} catch {
debugLog("speaker names: ling failed \(error)")
}
}
}
}