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 fan, hum or hiss no longer turns into a made-up English sentence

wquguruca9d86a77750
diff --git a/windows/src/Cicada.Core/Segmenter.cs b/windows/src/Cicada.Core/Segmenter.cs
index 1d50686..b245e20 100644
--- a/windows/src/Cicada.Core/Segmenter.cs
+++ b/windows/src/Cicada.Core/Segmenter.cs
@@ -17,6 +17,7 @@ public sealed class Segmenter
const int Frame = 480; // 30 ms
public static double Threshold = 0.01; // the Mac AudioRouter's gate (about -40 dBFS)
public static double Pause = 0.6, MaxLength = 8.0, MinSpeech = 0.25;
+ public static double Steady = 2.0; // loud frames' 90th / 10th percentile below this: not speech
const double PreRoll = 0.15, Tail = 0.15, CutWindow = 2.0;
readonly List<short> pending = []; // samples not yet a whole frame
@@ -93,6 +94,17 @@ public sealed class Segmenter
}
}
+ /// Steady sound just over the gate (a fan, hum, hiss) isn't speech: its loud frames barely vary
+ /// (90th / 10th percentile about 1.1; speech, even over such a floor, 3 and up). Given one, Ming
+ /// writes a stock sentence in the hint's language ("I'm going to show you a couple of things").
+ /// Speech buried under steady noise goes too; system audio rarely carries it.
+ internal static bool IsSteady(IEnumerable<double> frameLevels)
+ {
+ var loud = frameLevels.Where(l => l >= Threshold).Order().ToList();
+ if (loud.Count < 2) return false;
+ return loud[(int)(0.9 * (loud.Count - 1))] < Steady * loud[(int)(0.1 * (loud.Count - 1))];
+ }
+
/// Emits `current[..end]` and keeps what follows as the start of the next segment.
void Close(int end)
{
@@ -101,7 +113,7 @@ public sealed class Segmenter
var rest = current.GetRange(end, current.Count - end);
var restLevels = levels.GetRange(end / Frame, levels.Count - end / Frame);
double start = currentStart;
- bool speech = loudFrames * Frame >= MinSpeech * Rate;
+ bool speech = loudFrames * Frame >= MinSpeech * Rate && !IsSteady(levels.GetRange(0, Math.Min(levels.Count, end / Frame)));
current.Clear(); levels.Clear();
quietFrames = 0; loudFrames = 0;
if (speech) Emitted?.Invoke(new Segment(++index, Math.Round(start, 3), samples));
diff --git a/windows/tests/Cicada.Tests/Program.cs b/windows/tests/Cicada.Tests/Program.cs
index e3a76e7..3dea4f9 100644
--- a/windows/tests/Cicada.Tests/Program.cs
+++ b/windows/tests/Cicada.Tests/Program.cs
@@ -401,8 +401,10 @@ static class Tests
// MARK: Segmenter
- static short[] Tone(double seconds, double amp = 0.3) =>
- Enumerable.Range(0, (int)(seconds * 16000)).Select(i => (short)(Math.Sin(2 * Math.PI * 300 * i / 16000) * amp * 32767)).ToArray();
+ /// A 300 Hz tone that swells and fades four times a second like syllables; steady when not.
+ static short[] Tone(double seconds, double amp = 0.3, bool syllables = true) =>
+ Enumerable.Range(0, (int)(seconds * 16000)).Select(i => (short)(Math.Sin(2 * Math.PI * 300 * i / 16000)
+ * (syllables ? 0.55 + 0.45 * Math.Sin(2 * Math.PI * 4 * i / 16000) : 1) * amp * 32767)).ToArray();
static short[] Silence(double seconds) => new short[(int)(seconds * 16000)];
static List<Segment> Cut(params short[][] parts)
@@ -438,6 +440,8 @@ static class Tests
{
Equal(0, Cut(Silence(1), Tone(0.1), Silence(1)).Count, "under 0.25 s of sound is not speech");
Equal(0, Cut(Tone(3, 0.002)).Count, "below the gate is silence");
+ Equal(0, Cut(Tone(3, 0.02, syllables: false), Silence(1)).Count, "a steady hum over the gate is not speech");
+ Equal(1, Cut(Tone(3), Silence(1)).Count, "speech, which rises and falls, is");
}
public static void WavRoundTrip()