知了 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): what you say with the talk key while the other side is talking is sent instead of dropped
wquguru1a1a5eb079e4
diff --git a/linux/README.md b/linux/README.mdindex 85c8c77..848ff70 100644--- a/linux/README.md+++ b/linux/README.md@@ -6,7 +6,7 @@ Cicada 的 Linux 版:把电脑播放的声音变成双语字幕和实时纪要- 识别:分段发给 AI 服务的“听的模型”,一次请求返回语言、原文和译文。默认 Ling 的 Ming(`Ming-3.0-audio-flash`);用不了 Ling 时可在 设置 › AI 服务 换成 OpenRouter、ZenMux 或兼容 OpenAI 的自建服务(`cicada/core/providers.py`,推荐的听写模型是 Qwen 3.8 Omni Flash,比 Ming 慢 1–2 秒,测试见 `tools/audio-probe`),纪要、翻译和同传声音跟着换,只有“用你自己的声音”只能用 Ling。都需要一个 key,声音都会发到所选服务。和 Windows、Android 同一套做法。- 纪要窗:浮在最上面、点它也不抢焦点(app 走 XWayland,窗口类型是 notification;GNOME 的 Wayland 不给普通窗口这两个能力)。**Linux 没法让窗口避开屏幕共享**,所以纪要窗一直带“可能被共享”。- 纪要、记录格式(逐字节和 Mac 一致)、提示词、文案都和 Mac 版一致;英日韩翻译直接用 `Resources/*.lproj/Localizable.strings`,Linux 独有的在 `strings/`。-- 同声传译:声音服务器里建一个虚拟麦克风“知了 麦克风”(`module-null-sink` + `module-remap-source`,PipeWire 和 PulseAudio 都行,不用装驱动),打开时设成默认输入,关掉或退出时把原来的麦克风还回去(被 `kill -9` 后下次启动也会还)。点纪要窗的麦克风、托盘的“开始说”或快捷键(Super + Shift + S,`cicada --speak`)开始 / 停止;Ming 一次请求识别并翻译,Ming 的声音流式播进知了 麦克风,下一句的声音在上一句播完前就已经在取。对方的声音从扬声器漏进麦克风的那句会丢掉,不说出去。+- 同声传译:声音服务器里建一个虚拟麦克风“知了 麦克风”(`module-null-sink` + `module-remap-source`,PipeWire 和 PulseAudio 都行,不用装驱动),打开时设成默认输入,关掉或退出时把原来的麦克风还回去(被 `kill -9` 后下次启动也会还)。点纪要窗的麦克风、托盘的“开始说”或快捷键(Super + Shift + S,`cicada --speak`)开始 / 停止;Ming 一次请求识别并翻译,Ming 的声音流式播进知了 麦克风,下一句的声音在上一句播完前就已经在取。不戴耳机时,同声传译打开期间还会加载 `module-echo-cancel`(WebRTC 回声消除):它的输出“知了 · 防回声”接在原来的默认输出前面并设为默认(声音仍从原来的扬声器出来,关掉、退出、被杀后下次启动都会把默认输出还回去),按下说话时从它的麦克风录音,对方从扬声器传进来的声音被去掉,你压着对方说的话照常说出去。声音服务器加载不了它、或默认输出被换走时,这一次用原来的麦克风,对方说话期间说的那句会丢掉,不说出去。- 说话人识别(默认打开,和 Mac 一样):每句话的声音算一个声纹,按 Mac 的阈值分组,标出说话人 1、2…,Ling 从对话里认出名字;会议记录里可以改名、合并、把一句改给别人,都能撤销。声纹模型和 Mac 是同一个(WeSpeaker ResNet34-LM,VoxCeleb,CC BY 4.0),由 `scripts/convert-voiceprint.py` 转成 `models/voiceprint.bin`;Ubuntu 桌面没有矩阵库、Python 也跑不动 ResNet,所以推理在我们自己的 C 代码里(`native/voiceprint.c`,不用第三方库),每个架构编一个 `libcicada-voice-<arch>.so` 随包附带。和 Mac 的 Core ML 结果余弦相似度 1.0000;在 4 核 arm64 虚拟机里 5 秒的一句约 0.3 秒。没有这个库时功能显示不可用,听照常。- 支持 Ubuntu 22.04 / 24.04 桌面版。diff --git a/linux/cicada/app/debug.py b/linux/cicada/app/debug.pyindex 9bb3e9c..07ac323 100644--- a/linux/cicada/app/debug.py+++ b/linux/cicada/app/debug.py@@ -310,11 +310,13 @@ def command(app, line: str) -> dict:return {"ok": True, "on": app.settings.speakerVoices}return {"ok": True, **_speakers(app, app.store.get(arg))}if cmd == "speak-enable":- return {"ok": app.set_speak_enabled(arg == "on"), "enabled": app.settings.speakEnabled, "realMic": app.room.real_mic}+ return {"ok": app.set_speak_enabled(arg == "on"), "enabled": app.settings.speakEnabled, "realMic": app.room.real_mic,+ "talkEcho": app.talk_echo.on}if cmd == "speak":if (arg == "on") != app.speak.active:app.toggle_speaking()- return {"ok": True, "speaking": app.speak.active}+ # echoCancelled: this press reads the talk key's echo canceller (else the plain mic, echo drop on)+ return {"ok": True, "speaking": app.speak.active, "echoCancelled": app.speak.echo_cancelled}if cmd == "codecs":from . import mediareturn {"ok": True, **media.codecs(), "recordAudio": app.settings.recordAudio, "recording": listen.recording}diff --git a/linux/cicada/app/main.py b/linux/cicada/app/main.pyindex e1ba56d..da8059e 100644--- a/linux/cicada/app/main.py+++ b/linux/cicada/app/main.py@@ -90,8 +90,12 @@ class Controller:self.translator.on_fixes = self.fixes.received# 同声传译: my microphone → the AI service → 知了 麦克风. The listen side's loudness feeds its echo check.from ..core.speak import SpeakEngine- from .room import EchoCanceller, Player, VirtualMic+ from .room import TALK_MIC, TALK_SINK, EchoCanceller, Player, VirtualMicself.room = VirtualMic()+ # The talk key's microphone with the meeting's voices taken out, made with 知了 麦克风 (as the+ # Mac makes its voice unit ahead): what I say while they talk goes out instead of being dropped.+ self.talk_echo = EchoCanceller(TALK_SINK, TALK_MIC, "talk-sink", agc_off=True)+ self._talk_echo_lock = threading.Lock()# 电脑和会议室: the online voices taken out of the room mic (module-echo-cancel).self.echo = EchoCanceller()# Debug (roomEcho on): the room mic through the canceller. Off by default, as on the Mac: there@@ -213,8 +217,13 @@ class Controller:passself.tray = tray.make(self, icon)def prepare():- (self.room.create if self.settings.speakEnabled else self.room.leftovers)()+ self.talk_echo.remove() # left by a run that crashed: the speakers are the default output againself.echo.remove() # left by a run that crashed during 电脑和会议室+ if self.settings.speakEnabled:+ if self.room.create():+ self._make_talk_echo()+ else:+ self.room.leftovers()if speak: # `cicada --speak` before Cicada was running: speak once 知了 麦克风 is thereGLib.idle_add(lambda: self.toggle_speaking() and False)threading.Thread(target=prepare, daemon=True).start()@@ -885,16 +894,32 @@ class Controller:def set_speak_enabled(self, on: bool) -> bool:"""Turning it on makes 知了 麦克风 (and the default input); off removes it. False if the soundserver wouldn't make the device."""- if on and not self.room.on and not self.room.create():- return False+ if on and not self.room.on:+ if not self.room.create():+ return False+ self._make_talk_echo()if not on:self.speak.cancel()+ self.talk_echo.remove()self.room.remove()self.settings.speakEnabled = onself.settings.save()self.changed()return True+ def _make_talk_echo(self) -> None:+ """The talk key's echo canceller, in front of the output that is the default now. Not while+ 电脑和会议室's debug canceller is (two in a row fight over the default output)."""+ real = self.room.real_mic+ if not real or self.echo.on:+ return+ with self._talk_echo_lock: # a press can ask again while the last one is still being made+ if self.talk_echo.ready(real):+ return+ self.talk_echo.remove()+ made = self.talk_echo.create(real) is not None+ Log.d("speak: echo canceller %s" % ("ready" if made else "unavailable, the plain mic"))+def toggle_speaking(self) -> None:if self.speak.active:self.speak.stop()@@ -912,8 +937,19 @@ class Controller:self.speak.problem = L("没能建好知了麦克风")self.changed()return- # Me, from the microphone that was the default before 知了 麦克风 took its place.- mic = SystemAudio(self.room.real_mic or "@DEFAULT_SOURCE@")+ # Me, from the microphone that was the default before 知了 麦克风 took its place: through the+ # echo canceller made for it while the meeting still plays through that, else the plain mic+ # (said over the meeting is dropped then) and the canceller is made again for the next time.+ real = self.room.real_mic+ cancelled = bool(real) and self.talk_echo.ready(real)+ why = ""+ if not cancelled:+ why = ("no real microphone known" if not real else "no canceller" if not self.talk_echo.on+ else "the canceller was made for another microphone" if self.talk_echo.mic != real+ else "the canceller isn't the default output")+ if real:+ threading.Thread(target=self._make_talk_echo, daemon=True).start()+ mic = SystemAudio(self.talk_echo.source if cancelled else real or "@DEFAULT_SOURCE@")def ended(reason):if reason:@@ -921,7 +957,7 @@ class Controller:self.speak.problem = L("没有可用的麦克风")GLib.idle_add(lambda: self.speak.stop() or False)mic.on_ended = ended- self.speak.start(mic)+ self.speak.start(mic, cancelled, why)self.show_notes()def stop_listening(self, ended_at=None, auto: bool = False) -> None:@@ -1037,6 +1073,7 @@ class Controller:def run():self.speak.cancel()+ self.talk_echo.remove() # the speakers are the default output againif self.room.on:self.room.remove() # the real microphone is the default againif self.echo.on:diff --git a/linux/cicada/app/room.py b/linux/cicada/app/room.pyindex 3b7567f..7f9230b 100644--- a/linux/cicada/app/room.py+++ b/linux/cicada/app/room.py@@ -240,39 +240,52 @@ class VirtualMic:AEC_SINK = "cicada_aec_out"AEC_MIC = "cicada_aec_mic"---def _real_sink_path() -> str:- return os.path.join(os.path.dirname(Paths.log()), "real-sink")+# 同声传译's own canceller: the talk key reads the microphone through it.+TALK_SINK = "cicada_talk_out"+TALK_MIC = "cicada_talk_mic"+# PulseAudio's WebRTC canceller turns on gain control unless told not to, which moves the+# microphone's own volume; off, as on the Mac. pipewire-pulse ignores these keys (its default is off).+NO_AGC = 'aec_args="analog_gain_control=0 digital_gain_control=0"'class EchoCanceller:- """电脑和会议室: the online voices taken out of the room mic, so a remote sentence isn't heard- twice and the room can talk over it (the Mac's voice-processing unit). One sound-server module,- module-echo-cancel (WebRTC's canceller, in PipeWire and PulseAudio):-- cicada_aec_out an output in front of the speakers, made the default while it runs: what it- plays is what the canceller takes out (the meeting app's voices)- cicada_aec_mic the room microphone with them taken out: the room side listens to it-- Removed when 电脑和会议室 stops, on 退出, SIGTERM, and after a crash at the next launch; the- output that was the default is the default again each time."""-- def __init__(self):+ """The meeting's voices taken out of a microphone (the Mac's voice-processing unit). One+ sound-server module, module-echo-cancel (WebRTC's canceller, in PipeWire and PulseAudio):++ <sink> an output in front of the speakers, made the default while it runs: what it plays+ goes on to the speakers and is what the canceller takes out (the meeting app's+ voices). The canceller only hears what is played into it, so it has to be the+ default; the speakers stay where the sound comes out.+ <source> the microphone with them taken out++ Two of them: 电脑和会议室's room side (cicada_aec_*, debug only, the room stays unprocessed by+ default) and 同声传译's talk key (cicada_talk_*, made with 知了 麦克风 so it has settled before a+ press). Removed when they stop, on 退出, SIGTERM, and after a crash at the next launch; the output+ that was the default is the default again each time."""++ def __init__(self, sink: str = AEC_SINK, source: str = AEC_MIC, state: str = "real-sink",+ agc_off: bool = False):+ self.sink = sink+ self.source = source+ self.state = state+ self.agc_off = agc_offself.on = False+ self.mic = None # the microphone it was made ontry:- with open(_real_sink_path()) as f:+ with open(self._state_path()) as f:self.real_sink = f.read().strip() or Noneexcept OSError:self.real_sink = None- @staticmethod- def _ours(modules) -> list:- return [i for i, name, arg in modules if name == "module-echo-cancel" and ("sink_name=" + AEC_SINK) in arg]+ def _state_path(self) -> str:+ return os.path.join(os.path.dirname(Paths.log()), self.state)++ def _ours(self, modules) -> list:+ return [i for i, name, arg in modules if name == "module-echo-cancel" and ("sink_name=" + self.sink) in arg]def create(self, mic: str) -> str | None:"""Puts the canceller between `mic` and the speakers; the source to listen to, or None when the- sound server won't (the room is then gated instead)."""+ sound server won't (the caller then uses the plain mic)."""try:s = _Session()except OSError:@@ -282,31 +295,52 @@ class EchoCanceller:return Noneself._give_back(s)sink = s.server().get("sink")- if not sink or sink == AEC_SINK:+ if not sink or sink.startswith("cicada_"):+ # Another canceller of ours is the default already: two in a row fight over it.+ Log.d("echo cancel %s: the default output is %s, not made" % (self.sink, sink))return Noneself.real_sink = sinktry:- with open(_real_sink_path(), "w") as f:+ with open(self._state_path(), "w") as f:f.write(sink)except OSError:pass- out_desc = L("知了 · 防回声").replace("'", "")- index = s.load("module-echo-cancel",- "aec_method=webrtc source_master=%s sink_master=%s source_name=%s sink_name=%s "- "source_properties=\"device.description='%s'\" sink_properties=\"device.description='%s'\""- % (mic, sink, AEC_MIC, AEC_SINK, out_desc, out_desc))+ desc = L("知了 · 防回声").replace("'", "")+ args = ("aec_method=webrtc source_master=%s sink_master=%s source_name=%s sink_name=%s "+ "source_properties=\"device.description='%s'\" sink_properties=\"device.description='%s'\""+ % (mic, sink, self.source, self.sink, desc, desc))+ index = s.load("module-echo-cancel", args + " " + NO_AGC) if self.agc_off else None+ if index is None:+ if self.agc_off:+ Log.d("echo cancel %s: not loaded with gain control off, again with the defaults" % self.sink)+ index = s.load("module-echo-cancel", args)if index is None:- Log.d("echo cancel: module not loaded")+ Log.d("echo cancel %s: module not loaded" % self.sink)return None- s.set_default_sink(AEC_SINK)+ s.set_default_sink(self.sink)self.on = True- Log.d("echo cancel: on, in front of the default output")- return AEC_MIC+ self.mic = mic+ Log.d("echo cancel %s: on, in front of the default output" % self.sink)+ return self.source+ finally:+ s.close()++ def ready(self, mic: str) -> bool:+ """Whether it is there for `mic` and still the default output: the meeting plays through it.+ False after the output was changed (a headset, the sound settings) or the module went away."""+ if not self.on or mic != self.mic:+ return False+ try:+ s = _Session()+ except OSError:+ return False+ try:+ return s.ok and s.server().get("sink") == self.sink and bool(self._ours(s.modules()))finally:s.close()def _give_back(self, s) -> None:- if s.server().get("sink") == AEC_SINK and self.real_sink:+ if s.server().get("sink") == self.sink and self.real_sink:s.set_default_sink(self.real_sink)for index in self._ours(s.modules()):s.unload(index)@@ -322,8 +356,9 @@ class EchoCanceller:had = bool(self._ours(s.modules()))self._give_back(s)if had:- Log.d("echo cancel: removed")+ Log.d("echo cancel %s: removed" % self.sink)self.on = False+ self.mic = Nonefinally:s.close()diff --git a/linux/cicada/core/speak.py b/linux/cicada/core/speak.pyindex 4d0d4c3..63c8e46 100644--- a/linux/cicada/core/speak.py+++ b/linux/cicada/core/speak.py@@ -7,9 +7,11 @@ into 知了 麦克风, the virtual microphone the meeting app uses. Sentences plorder. A sentence that couldn't be interpreted is never spoken (AGENTS.md: never speak untranslatedtext into the room).-Without headphones my microphone also hears the other side through the speakers; a sentence heard-while the system audio was loud for most of it is dropped, so the room doesn't get its own words-back. UI-free like ListenEngine: on_change is called on worker threads."""+Without headphones my microphone also hears the other side through the speakers. The app reads it+through the sound server's echo canceller when it can (app/room.py), which takes their voices out, so+what I say over them goes out. From the plain microphone, a sentence heard while the system audio was+loud for most of it is dropped instead, so the room doesn't get its own words back. UI-free like+ListenEngine: on_change is called on worker threads."""from __future__ import annotationsimport queue@@ -49,8 +51,19 @@ class LoudnessLog:return sum(span) / len(span) if span else 0.0+ECHO = 0.5 # from the plain mic, drop a sentence if the system audio was loud for more than half of it+++def echo_dropped(listening: bool, headphones: bool, loud: float, echo_cancelled: bool) -> bool:+ """Whether a sentence may be the other side's voice from the speakers, not mine. Only from the+ plain microphone (the Mac's rule): through the echo canceller their voices are taken out, and+ dropping what I say over them is what the canceller is there to stop. Unknown (not listening, so+ nothing measured the system audio) or 戴着耳机: kept."""+ return listening and not headphones and not echo_cancelled and loud > ECHO++class SpeakEngine:- ECHO = 0.5 # drop a sentence if the system audio was loud for more than half of it+ ECHO = ECHOdef __init__(self, ling, translator, settings, store=None, listen=None, player=None):self.ling = ling@@ -74,6 +87,7 @@ class SpeakEngine:self.problem = None # the last thing worth saying (a sentence not spoken, a missing voice)self.level = 0.0self.spoken = 0+ self.echo_cancelled = False # this press reads the echo-cancelled microphoneself._me = 0def on_change(self, listener) -> None:@@ -108,9 +122,12 @@ class SpeakEngine:# Running- def start(self, mic) -> None:+ def start(self, mic, echo_cancelled: bool = False, why: str = "") -> None:+ """`echo_cancelled`: `mic` is the echo canceller's source (the meeting's voices taken out), so+ nothing said over them is dropped; `why` says why not, for the log."""if self.active:return+ self.echo_cancelled = echo_cancelledself.active = Trueself.stage = "hearing"self.problem = None@@ -123,7 +140,7 @@ class SpeakEngine:def emitted(seg):# Interpreted at once, while the sentence before may still be playing; spoken in order.ended = time.monotonic()- self._work.put((gen, seg, self._pool.submit(self._interpret, seg, ended - seg.seconds, ended)))+ self._work.put((gen, seg, self._pool.submit(self._interpret, seg, ended - seg.seconds, ended, echo_cancelled)))segmenter.on_segment = emitteddef feed():@@ -142,7 +159,8 @@ class SpeakEngine:self._source = micmic.on_samples = audio.putmic.start()- Log.d("speak: started %s -> %s" % (self.source, self.target))+ Log.d("speak: started %s -> %s, %s" % (self.source, self.target, "through the echo canceller" if echo_cancelled else+ "from the plain mic (%s), said over the meeting is dropped" % (why or "no canceller")))self._changed()def stop(self) -> None:@@ -177,10 +195,14 @@ class SpeakEngine:self.stage = "hearing" if self.active else "idle"self._changed()- def _interpret(self, seg, start: float, end: float):+ def _interpret(self, seg, start: float, end: float, echo_cancelled: bool = False):"""(original, translation, source, target, ended, voice chunks) or None, on the pool."""listening = self.listen is not None and self.listen.listening- if listening and not self.settings.speakHeadphones and self.loudness.fraction(start, end) > self.ECHO:+ loud = self.loudness.fraction(start, end) if listening else 0.0+ if listening:+ Log.d("speak: segment %d %.1fs, system audio loud %d%% of it, %s" % (+ seg.index, seg.seconds, round(loud * 100), "echo cancelled" if echo_cancelled else "plain mic"))+ if echo_dropped(listening, self.settings.speakHeadphones, loud, echo_cancelled):Log.d("speak: segment %d dropped, the system audio was playing (echo)" % seg.index)# Said, so the user knows: talking over them (or no headphones) is why it didn't go out.self.problem = strings.L("对方在说话时说的,没有说出去")diff --git a/linux/tests/test_core.py b/linux/tests/test_core.pyindex 28be4a1..bd3f631 100644--- a/linux/tests/test_core.py+++ b/linux/tests/test_core.py@@ -1211,11 +1211,11 @@ class Speaking(Base):eng = SpeakEngine(ling, Translator(ling), settings, listen=listen, player=lambda: player)return eng, fake, player- def _say(self, eng, pcm):+ def _say(self, eng, pcm, echo_cancelled=False):src = PcmAudioSource(lambda: pcm, speed=0)done = threading.Event()src.on_ended = lambda why: done.set()- eng.start(src)+ eng.start(src, echo_cancelled)done.wait(5)time.sleep(0.6)eng.stop()@@ -1304,6 +1304,35 @@ class Speaking(Base):self.assertEqual([], fake.calls, "not even sent to Ming")self.assertIsNotNone(eng.problem, "and the user is told")+ def test_through_the_echo_canceller_talking_over_them_goes_out(self):+ # The talk key read the microphone through module-echo-cancel: their voices are taken out,+ # so what I say over them is mine and is sent (the Mac's rule since 96205f8).+ class Listening:+ listening = True+ spoken = "en"+ session_id = None+ eng, fake, player = self._engine("ORIGINAL: 你好\nTRANSLATION: Hello", listen=Listening())+ stop = threading.Event()++ def loud():+ while not stop.is_set():+ eng.loudness.add(True)+ time.sleep(0.02)+ threading.Thread(target=loud, daemon=True).start()+ self._say(eng, concat(tone(1.2), silence(1.0)), echo_cancelled=True)+ stop.set()+ self.assertEqual(1, eng.spoken)+ self.assertEqual(1, len(fake.calls), "sent to Ming")+ self.assertIsNone(eng.problem)++ def test_echo_drop_is_only_for_the_plain_mic(self):+ from cicada.core.speak import echo_dropped+ self.assertTrue(echo_dropped(True, False, 0.8, False), "plain mic, loud meeting: dropped")+ self.assertFalse(echo_dropped(True, False, 0.8, True), "echo cancelled: sent")+ self.assertFalse(echo_dropped(True, True, 0.8, False), "headphones: sent")+ self.assertFalse(echo_dropped(True, False, 0.4, False), "loud for less than half: sent")+ self.assertFalse(echo_dropped(False, False, 1.0, False), "not listening, nothing measured: sent")+def test_with_headphones_talking_over_them_still_goes_out(self):class Listening:listening = Truediff --git a/site/changelog.json b/site/changelog.jsonindex b9a5dfc..594a11b 100644--- a/site/changelog.json+++ b/site/changelog.json@@ -53,6 +53,14 @@],"title": "打开麦克风开混合会议时,对方正在说话也能按住右 ⌥ 同声传译了。","text": "以前打开麦克风后,说话键用的是没有回声消除的麦克风,对方的声音从扬声器传进来,只要对方在说,按下去的话一律不说出去。现在会议室麦克风改走回声消除的通道、平时不做处理,按住说话键时才打开回声消除:对方说话时说的也能完整翻译播出,会议室那边听到的不变。"+ },+ {+ "kind": "fixed",+ "platforms": [+ "linux"+ ],+ "title": "Linux 上对方正在说话时,同声传译也能把你的话说出去。",+ "text": "以前不戴耳机时,对方说话期间你说的那句会整句丢掉,怕把扬声器里对方的声音当成你的。现在打开同声传译时,知了用系统的回声消除听你的麦克风,对方的声音被去掉,你的话照常翻译、说出去。系统不支持回声消除时还是原来的做法。"}]},