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(linux): hold music, ringback tones and typing no longer show up as lines of filler words

wquguru3a6e5e5e094f
diff --git a/linux/cicada/core/translator.py b/linux/cicada/core/translator.py
index 323214b..31420de 100644
--- a/linux/cicada/core/translator.py
+++ b/linux/cicada/core/translator.py
@@ -3,6 +3,7 @@ of audio to Ming's audio model, which returns the language, the original and the
call. Text translation is Ling first, then Google. Calls block."""
from __future__ import annotations
+import re
import time
import urllib.parse
from dataclasses import dataclass
@@ -33,9 +34,11 @@ class Translator:
"""One stretch of speech (16 kHz mono WAV) heard and interpreted. `hint` is the language the
meeting has been in, which keeps Ming from flipping on a short or accented stretch. None when
there was no speech or the reply wasn't in the three-line form; never a guess."""
+ # 16 kHz mono 16-bit after the 44-byte header: the length caps the reply and catches a runaway one.
+ seconds = max(0, len(wav) - 44) / 32000.0
reply = self.ling.chat(hear_prompt(target, hint, self.glossary()), hear_instruction(target), TRANSLATE,
- max_tokens=800, timeout=12.0, model=AUDIO_MODEL, audio=wav)
- return parse_heard(reply, target, hint)
+ max_tokens=hear_tokens(seconds), timeout=12.0, model=AUDIO_MODEL, audio=wav)
+ return parse_heard(reply, target, hint, seconds)
def interpret(self, wav: bytes, source: str, target: str):
"""What I said (16 kHz mono WAV) and its translation, or None. Never a guess: an untranslated
@@ -145,7 +148,7 @@ def _take(line: str, label: str) -> str | None:
return line[len(label):].strip() if line.lower().startswith(label.lower()) else None
-def parse_heard(reply: str, target: str, hint: str | None) -> Heard | None:
+def parse_heard(reply: str, target: str, hint: str | None, seconds: float = 0.0) -> Heard | None:
"""`LANG: … / ORIGINAL: … / TRANSLATION: …`. Drops replies without an original, "NONE" (music,
silence), and translations not in the target's script; a dropped translation leaves the original
for the text translator to try."""
@@ -168,6 +171,9 @@ def parse_heard(reply: str, target: str, hint: str | None) -> Heard | None:
return None
if not any(c.isalnum() for c in o):
return None
+ if is_noise(o, seconds):
+ Log.d("hear: noise dropped")
+ return None
code = normalize(lang, hint or "en")
t = translation
if code == target:
@@ -179,6 +185,76 @@ def parse_heard(reply: str, target: str, hint: str | None) -> Heard | None:
return Heard(code, o, t)
+def hear_tokens(seconds: float) -> int:
+ """Room for a reply to `seconds` of audio: a fast talker's original and translation fit; a reply
+ that runs on repeating itself ("滴答" ×800 from typing) stops early. 0: unknown."""
+ return 800 if seconds <= 0 else min(800, int(60 + 40 * seconds))
+
+
+# Ming describing the audio instead of transcribing it: "(无语音内容)", "[Music]", "这段音频是一段旋律,
+# 没有说话声。". A bracketed note beside real words ("(笑)好的") keeps the words.
+_ANNOTATION = re.compile(r"[((\[【][^))\]】]*[))\]】]|♪")
+_NO_SPEECH = (
+ "没有说话", "无语音", "没有语音", "没有人声", "无人声", "没有人说话", "无人说话",
+ "no speech", "no spoken", "no one is speaking", "nobody is speaking", "no audible speech", "no dialogue",
+ "音声がありません", "発話がありません", "話し声はありません", "음성이 없습니다", "말소리가 없습니다",
+)
+
+_FILLERS = (
+ "嗯", "啊", "呃", "额", "哦", "噢", "喔", "唔", "哈", "诶", "欸", "哎", "喂", "嘿", "哼",
+ "uh", "uhm", "um", "umm", "hmm", "hm", "mm", "mhm", "ah", "oh", "eh", "er", "erm", "huh",
+ "え", "えー", "あ", "あー", "う", "うー", "うん", "うーん", "ん", "えっと", "ええと", "あの",
+ "음", "어", "아", "응", "흠", "으",
+)
+
+
+def is_noise(original: str, seconds: float = 0.0) -> bool:
+ """Ming writes something for every stretch it is sent, speech or not (it can't be asked for NONE,
+ see hear_instruction): hold music, a ringback tone, a fan or typing came back as "嗯。", "嗯,嗯,嗯…"
+ or "滴答" ×800. That is noise: fillers only, one short sound six or more times over, or more than
+ anyone says in `seconds` (0: unknown). A short real answer ("对", "好的", "喂,能听到吗") stays.
+ Same rules and lists as the Android and Windows ports."""
+ text = _ANNOTATION.sub(" ", original.lower())
+ if any(r in text for r in _NO_SPEECH):
+ return True
+ words, word = [], ""
+ for c in text:
+ if c.isalnum():
+ word += c
+ elif word:
+ words.append(word)
+ word = ""
+ if word:
+ words.append(word)
+ if not words or all(_filler_word(w) for w in words):
+ return True
+ # Each CJK character is a unit, as a word is elsewhere.
+ units = [u for w in words for u in (list(w) if any(_cjk(c) for c in w) else [w])]
+ n = len(units)
+ for p in range(1, 5):
+ if n >= 6 * p and all(units[i] == units[i - p] for i in range(p, n)):
+ return True
+ return seconds > 0 and n > max(12.0, 12 * seconds)
+
+
+def _filler_word(raw: str) -> bool:
+ """Made of fillers only, a held sound shortened first: "嗯嗯嗯", "hmmmm"; "えーと" is not."""
+ w = ""
+ for c in raw:
+ if len(w) < 2 or w[-1] != c or w[-2] != c:
+ w += c
+ ok = [True] + [False] * len(w)
+ for i in range(1, len(w) + 1):
+ ok[i] = any(len(f) <= i and ok[i - len(f)] and w[i - len(f):i] == f for f in _FILLERS)
+ return ok[len(w)]
+
+
+def _cjk(c: str) -> bool:
+ o = ord(c)
+ return (0x3040 <= o <= 0x30FF or 0x3400 <= o <= 0x4DBF or 0x4E00 <= o <= 0x9FFF
+ or 0xF900 <= o <= 0xFAFF or 0xAC00 <= o <= 0xD7AF)
+
+
_OPENER = ("here is", "here's", "the subtitle", "this subtitle", "the text", "this text", "the original",
"the user", "note", "(note", "i'm sorry", "i am sorry", "sorry", "it seems", "it appears", "this appears")
_ABOUT = ("translat", "subtitle", "garbled", "transcri", "asr", "speech recognition", "best-effort")
diff --git a/linux/tests/test_core.py b/linux/tests/test_core.py
index a069563..b2235f2 100644
--- a/linux/tests/test_core.py
+++ b/linux/tests/test_core.py
@@ -31,7 +31,7 @@ from cicada.core import session_store # noqa: E402
from cicada.core.session_store import SessionStore # noqa: E402
from cicada.core.settings import AppSettings # noqa: E402
from cicada.core.support import Paths # noqa: E402
-from cicada.core.translator import Heard, Translator, clean_translation, parse_heard, parse_interpretation # noqa: E402
+from cicada.core.translator import Heard, Translator, clean_translation, hear_tokens, is_noise, parse_heard, parse_interpretation # noqa: E402
from cicada.core.ling import StreamResponse # noqa: E402
from cicada.core.speak import SpeakEngine # noqa: E402
from cicada.core import speakers # noqa: E402
@@ -289,6 +289,21 @@ class Hearing(Base):
self.assertIsNone(parse_heard("LANG: en\nORIGINAL: Hi\nTRANSLATION: Hi again", "zh-CN", None).translation)
self.assertEqual("ja", parse_heard("lang: ja-JP\noriginal: はい\ntranslation: 是", "zh-CN", None).lang)
+ def test_drops_noise(self):
+ # Same cases as the Android and Windows ports' noise tests.
+ for o in ("嗯。", "Mm.", "uh, um", "呃呃呃呃呃", "嗯,嗯,嗯,嗯,嗯,嗯,嗯,嗯,嗯,嗯,嗯,嗯。", "滴答" * 50,
+ "hmmmm", "うーん", "음…", "喂?", "No no no no no no",
+ "(无语音内容)", "[Music]", "♪♪", "这段音频是一段旋律,没有说话声。", "There is no speech in this audio."):
+ self.assertTrue(is_noise(o), o)
+ for o in ("嗯,好的。", "OK.", "Oh no, it crashed.", "对", "对对对", "谢谢谢谢", "喂喂,能听到吗?", "No, no, no.",
+ "えーと、来週の件ですが", "あの人です", "Okay, let's move on to the quarterly numbers.", "(笑)好的"):
+ self.assertFalse(is_noise(o), o)
+ spoken = "我们今天讨论一下这个季度的数字和下个季度的计划安排"
+ self.assertTrue(is_noise(spoken, 1.0))
+ self.assertFalse(is_noise(spoken, 4.0))
+ self.assertIsNone(parse_heard("LANG: zh-CN\nORIGINAL: 嗯,嗯,嗯,嗯,嗯,嗯。\nTRANSLATION: 嗯,嗯,嗯,嗯,嗯,嗯。", "zh-CN", None))
+ self.assertEqual((800, 380, 800), (hear_tokens(0), hear_tokens(8.0), hear_tokens(30.0)))
+
def test_follow_system_as_on_the_mac(self):
# FollowSystemTests, with Linux locale names.
for prefs, mine in ((["zh_CN.UTF-8"], "zh-CN"), (["en_US.UTF-8"], "en"), (["en_CA", "zh_CN"], "zh-CN"),