知了 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(windows): the talk key no longer sends "Hmm." for a press nobody spoke in, or the other side's own words back while the meeting plays
wqugurudbd92d845b5d
diff --git a/windows/src/Cicada.Core/ListenEngine.cs b/windows/src/Cicada.Core/ListenEngine.csindex 24404ef..298fa43 100644--- a/windows/src/Cicada.Core/ListenEngine.cs+++ b/windows/src/Cicada.Core/ListenEngine.cs@@ -108,6 +108,8 @@ public sealed class ListenEngine/// 电脑和会议室: the main side holds the gate on loud audio and lists what it heard; the room/// side checks its lines against that before recording them.public EchoFilter? Echo { get; set; }+ /// When the computer's audio was loud (the talk key's echo check), fed while listening to it.+ public LoudnessLog? Loudness { get; set; }/// Seconds from the meeting's start to this side's first sample (the room side joins later).double offset;Channel<(Segment seg, Heard heard, double at, double queued)>? held;@@ -171,6 +173,7 @@ public sealed class ListenEngineLevel = Rms(buf);// The computer is playing speech: the room mic would hear it from the speakers.if (!RoomSide && Echo is { } echo && Level >= Segmenter.Threshold) echo.Gate.Hold();+ if (!RoomSide) Loudness?.Add(Level >= Segmenter.Threshold);segmenter.Feed(buf);}segmenter.Flush();diff --git a/windows/src/Cicada.Core/Speak.cs b/windows/src/Cicada.Core/Speak.csindex c1ee89b..f92614b 100644--- a/windows/src/Cicada.Core/Speak.cs+++ b/windows/src/Cicada.Core/Speak.cs@@ -123,10 +123,16 @@ public sealed class SpeakEngine}DateTimeOffset? pressedAt;+ double? pressedMono;+ /// When the computer's audio was loud, while listening (the echo check); null when not listening.+ public LoudnessLog? Echo { get; set; }+ /// Share of the press the computer may play through before it counts as echo (Linux's 0.5).+ public const double EchoShare = 0.5;public void BeginRecording(){pressedAt = DateTimeOffset.Now;+ pressedMono = Echo?.Now;State = Phase.Recording; Said = Heard = Problem = null;Changed?.Invoke();}@@ -143,6 +149,21 @@ public sealed class SpeakEnginetry{var released = DateTime.UtcNow;+ // A press nobody spoke in: Ming hears 嗯 in the room's hush and the meeting would hear "Hmm."+ if (!PressSpeech.Spoken(wav))+ {+ Log.Debug("speak: nothing said in the press, nothing sent");+ State = Phase.Idle; pressedAt = null;+ Changed?.Invoke();+ return null;+ }+ // The meeting playing through most of the press: what the mic heard is its echo as much+ // as me, and Ming would send the other side's words back to them (measured on the Mac).+ if (Echo is { } echo && echo.Fraction(pressedMono ?? echo.Now - wav.Length / 32000.0, echo.Now) > EchoShare)+ {+ Log.Debug("speak: the computer was playing through the press (echo), nothing sent");+ return Fail(Strings.L("对方在说话时说的,没有说出去"));+ }var to = Target;State = Phase.Translating; Said = Heard = Problem = null;Changed?.Invoke();@@ -150,6 +171,13 @@ public sealed class SpeakEnginetry { heard = await translator.Hear(wav, to, MyLanguage, Priority.Speak); }catch (LingException e) { Log.Debug($"speak: hear failed {e.Message}"); return Fail(e.Failure is LingException.Kind.Network or LingException.Kind.Timeout ? (ling.Config.IsLing ? Strings.L("连不上 Ling,没有发出去") : Strings.LF("连不上 %@,没有发出去", ling.Config.Name)) : Strings.L("这句没翻译成 · 没有说出去")); }if (heard is null) return Fail(Strings.L("没听清,没有发出去"));+ if (PressSpeech.Filler(heard.Original))+ {+ Log.Debug("speak: only a filler heard, nothing sent");+ State = Phase.Idle; pressedAt = null;+ Changed?.Invoke();+ return null;+ }Said = heard.Original;var text = heard.Translation;// Ming didn't translate it: the text translator gets one try; failing that, silence.@@ -237,3 +265,69 @@ public static class VirtualCablepublic static bool Verify(byte[] package) => Convert.ToHexString(SHA256.HashData(package)).Equals(PackageSha256, StringComparison.OrdinalIgnoreCase);}++/// Whether anyone spoke during a press of the talk key (PressSpeech in SpeakEngine.swift, same+/// numbers): 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 came back from Ming as "嗯。" and went out as "Hmm.";+/// a result that is only fillers isn't sent either.+public static class PressSpeech+{+ const int Frame = 480;+ const double Needed = 0.3;++ /// `wav`: 16 kHz mono 16-bit, with or without its 44-byte header.+ public static bool Spoken(byte[] wav)+ {+ var start = wav.Length >= 44 && wav[0] == 'R' && wav[1] == 'I' ? 44 : 0;+ var n = (wav.Length - start) / 2;+ if (n < Frame) return false;+ var levels = new List<double>();+ for (var i = 0; i + Frame <= n; i += Frame)+ {+ double sum = 0;+ for (var k = i; k < i + Frame; k++) { double v = BitConverter.ToInt16(wav, start + k * 2) / 32768.0; sum += v * v; }+ levels.Add(Math.Sqrt(sum / Frame));+ }+ var floor = levels.Order().ElementAt(levels.Count / 10);+ var line = Math.Min(0.016, Math.Max(0.0063, floor * 3));+ return levels.Count(l => l >= line) * Frame / 16000.0 >= Needed;+ }++ static readonly HashSet<string> Fillers = ["嗯", "啊", "呃", "哦", "唔", "额", "噢", "哎", "um", "uh", "hmm", "hm", "mm", "mhm", "ah", "oh", "er", "えー", "あの", "음", "어"];++ /// Nothing but fillers (嗯, 啊, um, hmm…), whatever the punctuation.+ public static bool Filler(string text)+ {+ var words = System.Text.RegularExpressions.Regex.Split(text.ToLowerInvariant(), @"[^\p{L}]+").Where(w => w.Length > 0).ToList();+ return words.Count == 0 || words.All(w => Fillers.Contains(w) || (w.Length > 1 && w.Distinct().Count() == 1 && Fillers.Contains(w[0].ToString())));+ }+}++/// When the computer's audio was loud: (time, loud) per buffer of the listening side, the last+/// minute (Linux's LoudnessLog). Read by the talk key's echo check.+public sealed class LoudnessLog(Func<double>? clock = null)+{+ readonly Func<double> clock = clock ?? (() => Environment.TickCount64 / 1000.0);+ readonly Queue<(double t, bool loud)> log = new();+ public double Now => clock();++ public void Add(bool loud)+ {+ var now = Now;+ lock (log)+ {+ log.Enqueue((now, loud));+ while (log.Count > 0 && log.Peek().t < now - 60) log.Dequeue();+ }+ }++ /// Share of the buffers between `start` and `end` that were loud (0 with none).+ public double Fraction(double start, double end)+ {+ lock (log)+ {+ var span = log.Where(b => b.t >= start && b.t <= end).ToList();+ return span.Count == 0 ? 0 : span.Count(b => b.loud) / (double)span.Count;+ }+ }+}diff --git a/windows/src/Cicada/App.cs b/windows/src/Cicada/App.csindex 6ff6621..565d48c 100644--- a/windows/src/Cicada/App.cs+++ b/windows/src/Cicada/App.cs@@ -123,6 +123,10 @@ sealed class ControllerSpeak.Changed += () => Dispatch.BeginInvoke(() => { speakBar ??= new SpeakBar(this); speakBar.Refresh(); });Listen.Voiceprint = new WinVoiceprint();// No voices on the room side: its numbers would collide with the PC's.+ // The talk key's echo check: the meeting playing through a press is sent back otherwise.+ var loudness = new LoudnessLog();+ Listen.Loudness = loudness;+ Speak.Echo = loudness;RoomListen = new ListenEngine(Ling, Translator, Store, Notes, Settings, roomSide: true);RoomListen.Changed += Changed;Listen.Changed += Changed;diff --git a/windows/src/Cicada/Strings/en.lproj/Windows.strings b/windows/src/Cicada/Strings/en.lproj/Windows.stringsindex e90199d..d575265 100644--- a/windows/src/Cicada/Strings/en.lproj/Windows.strings+++ b/windows/src/Cicada/Strings/en.lproj/Windows.strings@@ -317,3 +317,5 @@"本机和局域网服务可不填" = "Optional on this PC or your local network";"例如 kokoro" = "e.g. kokoro";"例如 af_heart" = "e.g. af_heart";+/* The talk key: the computer was playing through the press, so what the mic heard may be its echo. */+"对方在说话时说的,没有说出去" = "Said while they were talking, so not sent";diff --git a/windows/src/Cicada/Strings/ja.lproj/Windows.strings b/windows/src/Cicada/Strings/ja.lproj/Windows.stringsindex b657123..7f2200b 100644--- a/windows/src/Cicada/Strings/ja.lproj/Windows.strings+++ b/windows/src/Cicada/Strings/ja.lproj/Windows.strings@@ -317,3 +317,5 @@"本机和局域网服务可不填" = "この PC やローカルネットワークでは省略可";"例如 kokoro" = "例: kokoro";"例如 af_heart" = "例: af_heart";+/* The talk key: the computer was playing through the press, so what the mic heard may be its echo. */+"对方在说话时说的,没有说出去" = "相手が話している間の発言なので、送っていません";diff --git a/windows/src/Cicada/Strings/ko.lproj/Windows.strings b/windows/src/Cicada/Strings/ko.lproj/Windows.stringsindex 96f07a2..e2b851a 100644--- a/windows/src/Cicada/Strings/ko.lproj/Windows.strings+++ b/windows/src/Cicada/Strings/ko.lproj/Windows.strings@@ -317,3 +317,5 @@"本机和局域网服务可不填" = "이 PC나 로컬 네트워크에서는 생략 가능";"例如 kokoro" = "예: kokoro";"例如 af_heart" = "예: af_heart";+/* The talk key: the computer was playing through the press, so what the mic heard may be its echo. */+"对方在说话时说的,没有说出去" = "상대방이 말하는 중이라 보내지 않았어요";diff --git a/windows/tests/Cicada.Tests/Program.cs b/windows/tests/Cicada.Tests/Program.csindex 112fba6..aad3cf2 100644--- a/windows/tests/Cicada.Tests/Program.cs+++ b/windows/tests/Cicada.Tests/Program.cs@@ -1322,6 +1322,38 @@ static class Testsfinally { LingClient.Spacing = old; }}+ public static async Task SilencesFillersAndEchoAreNotSent()+ {+ var old = LingClient.Spacing;+ LingClient.Spacing = 0.01;+ try+ {+ var asked = 0;+ var reply = "LANG: zh-CN\nORIGINAL: 嗯。\nTRANSLATION: Hmm.";+ var fake = new Fake(body =>+ {+ if (body.Contains(LingClient.AudioModel)) { Interlocked.Increment(ref asked); return Reply(reply); }+ return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("data:{\"type\":\"speech.audio.done\"}\n\n") };+ });+ var engine = Speaker(Client(fake), new NoVoice(), new AppSettings { SpeakFrom = "zh-CN" });+ // A press nobody spoke in: not even sent to the service.+ Check(await engine.Speak(Wav.Bytes(new short[16000 * 3]), new Sink()) is null && asked == 0, "a silent press sends nothing");+ Check(engine.State == SpeakEngine.Phase.Idle && engine.Problem is null, "and says nothing about it");+ // Ming heard only 嗯: nothing goes out.+ Check(await engine.Speak(Wav.Bytes(Tone(1)), new Sink()) is null && asked == 1, "a filler alone isn't sent");+ // The computer played through the press: its echo, not me.+ double now = 0;+ var log = new LoudnessLog(() => now);+ engine.Echo = log;+ engine.BeginRecording();+ for (var i = 0; i < 30; i++) { log.Add(true); now += 0.1; }+ Check(await engine.Speak(Wav.Bytes(Tone(1)), new Sink()) is null && asked == 1, "pressed while the meeting played: not sent");+ Equal(Strings.L("对方在说话时说的,没有说出去"), engine.Problem, "and the bar says why");+ }+ finally { LingClient.Spacing = old; }+ Check(PressSpeech.Filler("嗯嗯。") && PressSpeech.Filler("Um, uh...") && !PressSpeech.Filler("嗯,好的。") && !PressSpeech.Filler("OK."), "fillers");+ }+public static async Task WhatISayGoesAheadOfTheMeeting(){var old = LingClient.Spacing;