知了 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
fix: holding the talk key without saying anything no longer sends "Hmm." into the meeting
wquguru88ac6489acbe
diff --git a/Sources/Cicada/SpeakEngine.swift b/Sources/Cicada/SpeakEngine.swiftindex d980b0a..bfdafd6 100644--- a/Sources/Cicada/SpeakEngine.swift+++ b/Sources/Cicada/SpeakEngine.swift@@ -408,13 +408,15 @@ final class SpeakEngine {pressSamples.append(contentsOf: UnsafeBufferPointer(start: p[0], count: Int(c.frameLength)))}- /// The press as a WAV file in memory (44-byte RIFF header + little-endian samples).- private func takePressWAV() -> Data? {+ /// The press as a WAV file in memory (44-byte RIFF header + little-endian samples), and whether+ /// anyone spoke in it (`PressSpeech`).+ private func takePressWAV() -> (wav: Data?, spoken: Bool) {let samples = micQueue.sync { () -> [Int16] indefer { pressSamples = [] }return pressSamples}- guard samples.count >= 16_000 / 4 else { return nil }+ let spoken = PressSpeech.spoken(samples)+ guard samples.count >= 16_000 / 4 else { return (nil, spoken) }var d = Data()func u32(_ v: UInt32) { withUnsafeBytes(of: v.littleEndian) { d.append(contentsOf: $0) } }func u16(_ v: UInt16) { withUnsafeBytes(of: v.littleEndian) { d.append(contentsOf: $0) } }@@ -425,7 +427,7 @@ final class SpeakEngine {samples.withUnsafeBufferPointer { buf infor s in buf { withUnsafeBytes(of: s.littleEndian) { d.append(contentsOf: $0) } }}- return d+ return (d, spoken)}private nonisolated func recordMic(_ buffer: AVAudioPCMBuffer) {@@ -454,8 +456,18 @@ final class SpeakEngine {Task { await u.input.finish() }let released = releasedAt ?? Date()releasedAt = nil- let wav = takePressWAV()+ let (wav, spoken) = takePressWAV()probe?.released = released+ // A press nobody spoke in: Ming hears 嗯 in the room's hush and the meeting would hear "Hmm."+ if wav != nil, !spoken, probe == nil {+ debugLog("speak: nothing said in the press, nothing sent")+ Task { _ = await u.text.value }+ finals = []+ volatile = ""+ phase = .idle+ onLive?(.idle, "", "")+ return+ }probe?.pressWAV = wavlet from = settings.speakFromLang.codelet to = targetCode@@ -480,7 +492,9 @@ final class SpeakEngine {let textAt = Date()probe?.text = textprobe?.textAt = textAt- guard !text.isEmpty else {+ // A lone 嗯 or "um" is the room's hush or an echo's tail, not a sentence to interpret.+ guard !text.isEmpty, !PressSpeech.filler(text) else {+ if !text.isEmpty { debugLog("speak: only a filler heard, nothing sent") }phase = .idleonLive?(.idle, "", "")probe?.finish()@@ -765,3 +779,38 @@ final class SpeechWriter: NSObject, AVSpeechSynthesizerDelegate, @unchecked Sendfunc speechSynthesizer(_ s: AVSpeechSynthesizer, didFinish u: AVSpeechUtterance) { end() }func speechSynthesizer(_ s: AVSpeechSynthesizer, didCancel u: AVSpeechUtterance) { end() }}++/// Whether anyone spoke during a press of the talk key, from its 16 kHz samples: 0.3 s of 30 ms+/// frames over three times the press's own quiet (its quietest tenth), between -44 and -36 dBFS. A silent+/// press in a quiet room came back from Ming as "嗯。" and went out as "Hmm.".+enum PressSpeech {+ static let frame = 480+ static let needed = 0.3++ /// Nothing but fillers (嗯, 啊, um, hmm…), whatever the punctuation.+ static func filler(_ text: String) -> Bool {+ let words = text.lowercased().split { !$0.isLetter }.map(String.init)+ guard !words.isEmpty else { return true }+ return words.allSatisfy { w in fillers.contains(w) || (w.count > 1 && Set(w).count == 1 && fillers.contains(String(w.first!))) }+ }+ static let fillers: Set<String> = ["嗯", "啊", "呃", "哦", "唔", "额", "噢", "哎", "um", "uh", "hmm", "hm", "mm", "mhm", "ah", "oh", "er", "えー", "あの", "음", "어"]++ static func spoken(_ samples: [Int16]) -> Bool {+ guard samples.count >= frame else { return false }+ var levels: [Float] = []+ var i = 0+ while i + frame <= samples.count {+ var sum: Float = 0+ for k in i..<(i + frame) { let v = Float(samples[k]) / 32768; sum += v * v }+ levels.append((sum / Float(frame)).squareRoot())+ i += frame+ }+ // A press can be speech from end to end: its quietest tenth is the room only when it has+ // pauses, so the line stops at -36 dBFS, which a voice into the mic clears anyway.+ let floor = levels.sorted()[levels.count / 10]+ let line = min(0.016, max(0.0063, floor * 3))+ let voiced = Double(levels.filter { $0 >= line }.count) * Double(frame) / 16_000+ return voiced >= needed+ }+}+diff --git a/Tests/CicadaTests/PressSpeechTests.swift b/Tests/CicadaTests/PressSpeechTests.swiftnew file mode 100644index 0000000..75153f9--- /dev/null+++ b/Tests/CicadaTests/PressSpeechTests.swift@@ -0,0 +1,41 @@+import Foundation+import Testing+@testable import Cicada++@Suite struct PressSpeechTests {+ /// `seconds` of a 200 Hz tone at `level` (0–1) after `quiet` seconds of room noise at `noise`.+ private func press(quiet: Double, noise: Float, seconds: Double, level: Float) -> [Int16] {+ var out: [Int16] = []+ var seed: UInt32 = 1+ func rnd() -> Float { seed = seed &* 1_664_525 &+ 1_013_904_223; return Float(seed >> 8) / Float(1 << 24) * 2 - 1 }+ for _ in 0..<Int(quiet * 16_000) { out.append(Int16(rnd() * noise * 1.7 * 32767)) }+ for i in 0..<Int(seconds * 16_000) {+ out.append(Int16((sin(Float(i) * 2 * .pi * 200 / 16_000) * level * 1.41 + rnd() * noise) * 32767))+ }+ return out+ }++ @Test func aSilentPressIsNothing() {+ #expect(!PressSpeech.spoken(press(quiet: 3, noise: 0.003, seconds: 0, level: 0)))+ // A noisier room, still nobody talking.+ #expect(!PressSpeech.spoken(press(quiet: 3, noise: 0.01, seconds: 0, level: 0)))+ }++ @Test func aSentenceIsSpeech() {+ #expect(PressSpeech.spoken(press(quiet: 0.4, noise: 0.003, seconds: 2, level: 0.03)))+ // A quiet voice, quieter than the line on its own room: still over three times its noise.+ #expect(PressSpeech.spoken(press(quiet: 0.4, noise: 0.002, seconds: 1, level: 0.008)))+ }++ @Test func aClickIsNotASentence() {+ #expect(!PressSpeech.spoken(press(quiet: 2, noise: 0.003, seconds: 0.1, level: 0.2)))+ }++ @Test func fillersAloneAreNotSent() {+ #expect(PressSpeech.filler("嗯。"))+ #expect(PressSpeech.filler("嗯嗯"))+ #expect(PressSpeech.filler("Um, uh..."))+ #expect(!PressSpeech.filler("嗯,好的。"))+ #expect(!PressSpeech.filler("OK."))+ }+}