知了 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): a quiet short phrase on its own is kept however its first moments line up with the audio, and a faded-in hiss no longer passes as a word
wquguru1d193be72249
diff --git a/windows/src/Cicada.Core/Segmenter.cs b/windows/src/Cicada.Core/Segmenter.csindex c217b6e..fae2dc6 100644--- a/windows/src/Cicada.Core/Segmenter.cs+++ b/windows/src/Cicada.Core/Segmenter.cs@@ -24,7 +24,8 @@ public sealed class Segmenterpublic static double Dips = 2.5; // …unless the median is this far over the 10th, quiet ones held at gate / 8public static double Short = 2.0; // a sound up to this long, with real quiet (gate / 4) on both sides…public static double Swell = Math.Pow(10, 10 / 20.0); // …that swells and fades by 10 dB is a word: see IsWord- public static double Fade = Math.Pow(10, 30 / 20.0); // its rise and fade reach this far under its loudest frame (or gate / 4 if lower)+ public static double Fade = Math.Pow(10, 30 / 20.0); // its rise and fade reach this far under its loudest step (or gate / 4 if lower)+ public static double Voiced = 0.15; // a word's full frames crossing zero under this often (per sample) have voice in them: see IsVoicedpublic static double Above = 0.25; // under this share of its loud frames' energy over 100 Hz: rumble, not speechconst double PreRoll = 0.15, Tail = 0.15, CutWindow = 2.0;// A first-order high-pass at 100 Hz, for how much of a sound is above it (IsRumble).@@ -188,31 +189,58 @@ public sealed class Segmenter}/// A short word on its own: a sound up to `Short` that rises out of real quiet and falls back- /// into it (under gate / 4, about -52 dBFS, in the 0.6 s before it and in the pause after), and- /// that swells and fades by 10 dB or more (90th / 10th percentile of its frames from where it- /// rises to where it fades: over gate / 4, or 30 dB under its loudest frame if that's lower).- /// Such a word ("Thanks.", "Good morning everyone,") is one smooth syllable or a run of voiced- /// ones, so the steady test, which looks for dips inside, dropped it even at -26 dBFS; and a- /// quiet one ("Thanks." at -36 dBFS) has under 0.25 s over the gate, so it needs `MinWord` over- /// gate / 4 instead (a knock with a 150 ms tail has 0.15 s). A fan, hum or hiss is there on- /// both sides; a short burst of one in silence stays within about 9 dB (brown noise), 5 dB (the- /// rest), and Ming makes up a sentence for a hum or fan burst. Not for a forced cut, whose end- /// isn't a pause.- /// Measured over gate / 4 alone, a quiet word lost its rise and fade (an "s" or breath at -55- /// dBFS) and only its flat top was left: Samantha's "Thanks." was dropped at -33 dBFS (loudest- /// 0.1 s) and a meeting's quiet opening "Good morning everyone." at -34; now both are kept to- /// about -38 to -40 dBFS, where the gate itself stops them. A hiss faded in and out over 0.3 s- /// passes from -40 dBFS (was -36); brown noise still never does.+ /// into it (under gate / 4, about -52 dBFS, in the 0.6 s before it and in the pause after),+ /// and that swells and fades by 10 dB or more: at least 5% of its 10 ms steps (and 3 of them),+ /// from where it rises to where it fades, are that far under its 90th percentile. Such a word+ /// ("Thanks.", "Good morning everyone,") is one smooth syllable or a run of voiced ones, so the+ /// steady test, which looks for dips inside, dropped it even at -26 dBFS; and a quiet one+ /// ("Thanks." at -36 dBFS) has under 0.25 s over the gate, so it needs `MinWord` over gate / 4+ /// instead (a knock with a 150 ms tail has 0.15 s). A fan, hum or hiss is there on both sides;+ /// a short burst of one in silence has at most one low step at each edge (its 5th percentile+ /// stays within 2 dB, 9 dB with 50 ms fades), and Ming makes up a sentence for a hum or fan+ /// burst. Not for a forced cut, whose end isn't a pause.+ /// Where it rises and fades: over gate / 4, or 30 dB under its loudest step if that's lower;+ /// over gate / 4 alone, a quiet word lost its rise and fade (an "s" or breath at -55 dBFS) and+ /// only its flat top was left. 30 ms frames and their 10th percentile put Samantha's "Good+ /// morning everyone." at 7.5 to 11.4 dB depending on where the frames fell, so it came and+ /// went with a few ms of lead-in; 10 ms steps and their 5th percentile give it 14.6 to 16.5.internal bool IsWord(List<double> frameLevels, double gate){int first = frameLevels.FindIndex(l => l >= gate), last = frameLevels.FindLastIndex(l => l >= gate);if (first < 0 || (last - first + 1) * Frame > Short * Rate) return false;double quiet = gate / 4;if (lead is not { } q || q > quiet || last + 1 >= frameLevels.Count || frameLevels.Skip(last + 1).Min() > quiet) return false;- double rise = Math.Min(quiet, frameLevels.Max() / Fade);- int from = frameLevels.FindIndex(l => l >= rise), to = frameLevels.FindLastIndex(l => l >= rise);- var body = frameLevels.GetRange(from, to - from + 1).Select(l => Math.Max(l, gate / 8)).Order().ToList();- return body[(int)(0.9 * (body.Count - 1))] >= Swell * body[(int)(0.1 * (body.Count - 1))];+ var steps = new List<double>();+ for (int i = 0; i + Step10 <= current.Count; i += Step10)+ {+ double sum = 0;+ for (int k = i; k < i + Step10; k++) sum += (double)current[k] * current[k];+ steps.Add(Math.Sqrt(sum / Step10) / 32768);+ }+ double rise = Math.Min(quiet, steps.Max() / Fade);+ int from = steps.FindIndex(l => l >= rise), to = steps.FindLastIndex(l => l >= rise);+ var body = steps.GetRange(from, to - from + 1).Select(l => Math.Max(l, gate / 8)).Order().ToList();+ int low = Math.Max(2, (int)(0.05 * (body.Count - 1)));+ return low < body.Count && body[(int)(0.9 * (body.Count - 1))] >= Swell * body[low] && IsVoiced(frameLevels);+ }++ /// Whether a word has voice in it: 2 or more of its full frames (within 3 dB of its 90th+ /// percentile) cross zero under `Voiced` times a sample. Voiced speech crosses slowly (every+ /// voice tested had full frames at 0.01 to 0.06); a hiss crosses about every other sample (0.46+ /// and up) and, faded in and out over 0.3 s, swells like a word. Edge frames half silence+ /// cross slowly whatever they hold, hence full frames only and more than one.+ bool IsVoiced(List<double> frameLevels)+ {+ double full = frameLevels.Order().ElementAt((int)(0.9 * (frameLevels.Count - 1))) / Math.Sqrt(2);+ int slow = 0;+ for (int i = 0; i < frameLevels.Count; i++)+ {+ if (frameLevels[i] < full) continue;+ int start = i * Frame, end = Math.Min(current.Count, start + Frame), crossings = 0;+ for (int k = start + 1; k < end; k++) if (current[k] >= 0 != current[k - 1] >= 0) crossings++;+ if (crossings < Voiced * (end - start - 1)) slow++;+ }+ return slow >= 2;}/// The frame's level over 100 Hz (RMS after the high-pass), the filter running on across frames.diff --git a/windows/tests/Cicada.Tests/Program.cs b/windows/tests/Cicada.Tests/Program.csindex 73165b8..70b5fca 100644--- a/windows/tests/Cicada.Tests/Program.cs+++ b/windows/tests/Cicada.Tests/Program.cs@@ -488,6 +488,13 @@ static class Testsreturn Enumerable.Range(0, n).Select(i => (short)(Math.Sin(2 * Math.PI * 300 * i / 16000) * Math.Pow(Math.Sin(Math.PI * i / n), 2) * amp * 32767)).ToArray();}+ /// A 7 kHz tone faded in and out over 0.3 s: crosses zero like a hiss, swells like a word.+ static short[] Hiss(double seconds, double amp)+ {+ int n = (int)(seconds * 16000);+ return Enumerable.Range(0, n).Select(i => (short)(Math.Sin(2 * Math.PI * 7000 * i / 16000) * Math.Min(1, Math.Min(i / 4800.0, (n - i) / 4800.0)) * amp * 32767)).ToArray();+ }+static List<Segment> Cut(params short[][] parts){var seg = new Segmenter();@@ -538,6 +545,17 @@ static class TestsEqual(1, Cut(Silence(1), Word(1.2, 0.022), Silence(1)).Count, "one smooth run of voice on its own, no dips inside");Equal(1, Cut(Silence(1), Tone(0.5, 0.022, syllables: false), Tone(0.15, 0.0025, syllables: false), Silence(1)).Count,"a quiet word whose fade (an \"s\", a breath) falls under gate / 4 still swells: a meeting's quiet opening line");+ // Wherever the 30 ms frames fall (a few ms more lead-in), a quiet phrase with brief dips between+ // its words stays a word, and a hum or a faded hiss in silence stays out.+ short[] Flat(double s, double a) => Tone(s, a, syllables: false);+ short[] phrase = [.. Flat(0.35, 0.022), .. Flat(0.04, 0.0055), .. Flat(0.4, 0.022), .. Flat(0.04, 0.0055), .. Flat(0.35, 0.022), .. Flat(0.06, 0.0025)];+ for (int ms = 0; ms < 30; ms += 2)+ {+ var lead = Silence(1.0 + ms / 1000.0);+ Equal(1, Cut(lead, phrase, Silence(1)).Count, $"a quiet phrase with dips, {ms} ms more lead-in");+ Equal(0, Cut(lead, Flat(1, 0.03), Silence(1)).Count, $"a short hum, {ms} ms more lead-in");+ Equal(0, Cut(lead, Hiss(1, 0.03), Silence(1)).Count, $"a faded hiss, {ms} ms more lead-in");+ }Equal(0, Cut(Silence(1), Tone(1, 0.03, syllables: false), Silence(1)).Count, "a short hum in silence doesn't swell and fade");Equal(0, Cut(Silence(1), Word(0.15, 0.3), Silence(1)).Count, "nor is a knock a word");Equal(0, Cut(Silence(1), Hz(12, 3, 0.1), Silence(1)).Count, "rumble far under 100 Hz is no voice however it swells");