知了 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(linux): someone typing during a pause no longer joins the sentences either side into one
wquguruf1fbf83ed598
diff --git a/linux/cicada/core/segmenter.py b/linux/cicada/core/segmenter.pyindex a6f2b32..56088cb 100644--- a/linux/cicada/core/segmenter.py+++ b/linux/cicada/core/segmenter.py@@ -11,6 +11,8 @@ from collections import dequefrom .audio import RATE, WavFRAME = 480 # 30 ms+STEP = 160 # 10 ms: a key click is judged in these, as the Mac's VoiceActivity does+ONSET = 3 # steps of sound before it counts as speechPRE_ROLL = 0.15TAIL = 0.15CUT_WINDOW = 2.0@@ -51,6 +53,9 @@ class Segmenter:self.current = array("h")self.levels: list = []self.highs: list = [] # per frame of `current`: its level over 100 Hz+ self.speech: list = [] # per frame of `current`: whether it held speech (_speech)+ self.sounding = 0 # 10 ms steps of sound in a row so far+ self.carried = False # the sound that made the last frame speech began in the frame beforeself._hx = 0.0 # the high-pass's last input and output, carried from frame to frameself._hy = 0.0self.before: deque = deque()@@ -75,19 +80,39 @@ class Segmenter:if len(self.current):self._close(len(self.current) - self.quiet * FRAME + int(TAIL * RATE))+ def _speech(self, frame) -> bool:+ """Whether the frame holds speech: sound that has lasted 30 ms, judged in 10 ms steps against the+ line, so a lone key click (a few ms) neither opens a segment nor breaks the pause that ends+ one (VoiceActivity.swift's onset). The pre-roll then reaches back to where that sound began."""+ said = False+ carried = self.sounding > 0+ for i in range(0, len(frame), STEP):+ if Wav.rms(frame[i:i + STEP]) >= self.threshold:+ self.sounding += 1+ if self.sounding >= ONSET and not said:+ said = True+ self.carried = carried+ else:+ self.sounding = 0+ carried = False+ return said+def _step(self, frame: array) -> None:level = Wav.rms(frame)high = self._high(frame)- loud = level >= self.threshold+ loud = self._speech(frame)now = self.consumed / RATEself.consumed += len(frame)if not len(self.current):if not loud:self.idle.append(level)self.before.append((frame, level, high))- while len(self.before) > PRE_ROLL * RATE / FRAME:+ while len(self.before) > PRE_ROLL * RATE / FRAME + 1:self.before.popleft()return+ # One frame more when the sound began in the last quiet one.+ while len(self.before) > PRE_ROLL * RATE / FRAME + self.carried:+ self.before.popleft()self.current_start = now - len(self.before) * FRAME / RATEself.lead = min(self.idle) if self.idle else Noneself.idle.clear()@@ -95,11 +120,13 @@ class Segmenter:self.current.extend(f)self.levels.append(l)self.highs.append(h)+ self.speech.append(False)self.before.clear()self.quiet = self.loud = 0self.current.extend(frame)self.levels.append(level)self.highs.append(high)+ self.speech.append(loud)if loud:self.quiet = 0self.loud += 1@@ -194,6 +221,7 @@ class Segmenter:rest = self.current[end:]rest_levels = self.levels[min(len(self.levels), end // FRAME):]rest_highs = self.highs[min(len(self.highs), end // FRAME):]+ rest_speech = self.speech[min(len(self.speech), end // FRAME):]start = self.current_startsent = self.levels[:end // FRAME]if self._isolated(self.levels):@@ -205,6 +233,7 @@ class Segmenter:self.current = array("h")self.levels = []self.highs = []+ self.speech = []self.quiet = self.loud = 0if speech and self.on_segment:self.index += 1@@ -212,16 +241,17 @@ class Segmenter:elif speech:self.index += 1# A forced cut carries on with the remainder; after a pause the remainder is silence.- if any(l >= self.threshold for l in rest_levels):+ if any(rest_speech):self.current_start = start + end / RATEself.lead = Noneself.current.extend(rest)self.levels = list(rest_levels)self.highs = list(rest_highs)+ self.speech = list(rest_speech)trailing = 0- for l in reversed(self.levels):- if l >= self.threshold:+ for v in reversed(self.speech):+ if v:breaktrailing += 1self.quiet = trailing- self.loud = sum(1 for l in self.levels if l >= self.threshold)+ self.loud = sum(self.speech)diff --git a/linux/tests/test_core.py b/linux/tests/test_core.pyindex 5fd1c6b..d25bcd4 100644--- a/linux/tests/test_core.py+++ b/linux/tests/test_core.py@@ -128,6 +128,15 @@ def silence(seconds):return array("h", bytes(int(seconds * 16000) * 2))+def keys(seconds):+ """Typing: a key click every 0.2 s, 6 ms of decaying crackle peaking near -12 dBFS (scripts/vad-clips.py)."""+ out = silence(seconds)+ for at in range(1600, len(out) - 96, 3200):+ for k in range(96):+ out[at + k] = int(0.25 * math.exp(-k / 24) * (1 if k % 2 else -1) * 32767)+ return out++def concat(*parts):out = array("h")for p in parts:@@ -450,6 +459,14 @@ class Segmenting(Base):self.assertTrue(all(s.seconds <= 8.0 for s in got))self.assertAlmostEqual(got[1].start, got[0].start + got[0].seconds, delta=0.001)+ def test_key_clicks_are_not_speech(self):+ # Clicks in a pause don't hold it open: the sentences either side stay apart.+ got = cut(tone(2.0), keys(1.5), tone(1.5), silence(1.0))+ self.assertEqual(2, len(got))+ self.assertAlmostEqual(3.35, got[1].start, delta=0.06)+ # Nor does typing alone make a segment.+ self.assertEqual(0, len(cut(silence(0.5), keys(3.0), silence(1.0))))+def test_ignores_clicks(self):self.assertEqual(0, len(cut(silence(1.0), tone(0.1), silence(1.0))))self.assertEqual(0, len(cut(tone(3.0, 0.002))))