Skip to content

知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.

fix(windows): a sentence is no longer split in two when the PC or VM delivers its audio a moment late

wquguruecdc376a8454
diff --git a/windows/src/Cicada.Core/Audio.cs b/windows/src/Cicada.Core/Audio.cs
index 191a84a..d0a7e28 100644
--- a/windows/src/Cicada.Core/Audio.cs
+++ b/windows/src/Cicada.Core/Audio.cs
@@ -46,6 +46,24 @@ public sealed class FileAudioSource(string path, double speed = 1) : IAudioSourc
public void Stop() => cts?.Cancel();
}
+/// Loopback capture sends nothing while nothing plays, so SystemAudioTap feeds silence itself to end
+/// segments and keep the clock going. But a packet that is merely late (a busy PC, a VM) arrives
+/// after that silence: a gap that wasn't there goes into the middle of the sentence, and with the
+/// pause between two words it reaches the segmenter's 0.6 s and the line splits in two ("…keep
+/// the twenty." / "First is the release date…"). Waiting 300 ms, as the tap did, let any stall from
+/// 300 ms up do that; it now waits a whole second, so only a stall over a second can. Silence that
+/// is real costs nothing until the pause would end a segment anyway, then 0.4 s at most, once,
+/// when the playback stops altogether (a meeting app or player keeps sending silent packets).
+public static class LoopbackGap
+{
+ public const double Late = 1.0;
+
+ /// Samples of silence to feed now: none until `sinceLastPacket` reaches Late, then all of
+ /// `sinceFed` (the time since audio, or silence, was last handed on).
+ public static int Silence(double sinceLastPacket, double sinceFed) =>
+ sinceLastPacket >= Late ? (int)(Math.Max(0, sinceFed) * Segmenter.Rate) : 0;
+}
+
public static class Wav
{
public static byte[] Bytes(short[] pcm, int rate = Segmenter.Rate)
diff --git a/windows/src/Cicada/SystemAudioTap.cs b/windows/src/Cicada/SystemAudioTap.cs
index 910e09f..02557ca 100644
--- a/windows/src/Cicada/SystemAudioTap.cs
+++ b/windows/src/Cicada/SystemAudioTap.cs
@@ -74,10 +74,8 @@ sealed class SystemAudioTap : IAudioSource
Marshal.ThrowExceptionForHR(client.Start());
try
{
- // Loopback sends nothing while nothing plays, so we feed silence ourselves to end segments
- // and keep the clock going. Only after 300 ms without a packet, though: a packet that is
- // merely late (a busy PC, a VM) must not get silence put in front of it, or a sentence
- // splits in two.
+ // Loopback sends nothing while nothing plays, so we feed silence ourselves (LoopbackGap:
+ // only after a second without a packet, or a late packet splits its sentence in two).
var lastPacket = DateTime.UtcNow; var fedUntil = lastPacket;
int gaps = 0;
while (running)
@@ -85,13 +83,17 @@ sealed class SystemAudioTap : IAudioSource
if (!ready.WaitOne(100))
{
var now = DateTime.UtcNow;
- if ((now - lastPacket).TotalMilliseconds >= 300)
+ var silence = LoopbackGap.Silence((now - lastPacket).TotalSeconds, (now - fedUntil).TotalSeconds);
+ if (silence > 0)
{
- Samples?.Invoke(new short[(int)((now - fedUntil).TotalSeconds * 16000)]);
+ Samples?.Invoke(new short[silence]);
fedUntil = now;
}
continue;
}
+ // How long packets stall here (a VM): a stall over LoopbackGap.Late puts a gap in a sentence.
+ var waited = (DateTime.UtcNow - lastPacket).TotalMilliseconds;
+ if (waited >= 300 && fedUntil == lastPacket) Log.Debug($"tap: packets late by {waited:F0} ms");
while (capture.GetNextPacketSize(out var packet) == 0 && packet > 0)
{
Marshal.ThrowExceptionForHR(capture.GetBuffer(out var data, out var frames, out var flags, out _, out _));
diff --git a/windows/tests/Cicada.Tests/Program.cs b/windows/tests/Cicada.Tests/Program.cs
index 7475f7a..728dce4 100644
--- a/windows/tests/Cicada.Tests/Program.cs
+++ b/windows/tests/Cicada.Tests/Program.cs
@@ -519,6 +519,23 @@ static class Tests
Equal(0, Cut(Silence(1), Word(0.15, 0.3), Silence(1)).Count, "nor is a knock a word");
}
+ public static void LateLoopbackPacketsDontSplitASentence()
+ {
+ // A VM held packets back for 0.3-0.7 s: silence went in front of them and lines split
+ // mid-phrase. The tap now waits a second before it calls a stall silence.
+ Equal(0, LoopbackGap.Silence(0.3, 0.3), "a 300 ms stall is no silence");
+ Equal(0, LoopbackGap.Silence(0.9, 0.9), "nor is a 900 ms one");
+ Equal(16000, LoopbackGap.Silence(1.0, 1.0), "a second of nothing is: all of it goes in");
+ Equal(1600, LoopbackGap.Silence(1.1, 0.1), "then what has passed since");
+ // The segmenter fed like the tap: speech, a 0.3 s break between words, a 0.5 s stall.
+ var whole = Cut(Tone(2), Silence(0.3), Tone(2), Silence(1));
+ Equal(1, whole.Count, "one sentence");
+ var stalled = Cut(Tone(2), Silence(0.3), new short[LoopbackGap.Silence(0.5, 0.5)], Tone(2), Silence(1));
+ Equal(1, stalled.Count, "still one after a late packet");
+ var old = Cut(Tone(2), Silence(0.3), Silence(0.5), Tone(2), Silence(1));
+ Equal(2, old.Count, "with 300 ms it was split in two");
+ }
+
public static void WavRoundTrip()
{
var pcm = Tone(0.5);