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: prevent crashes when starting push-to-talk with a different microphone format

Philoveritas1bdce0a2a0ff
diff --git a/README.en.md b/README.en.md
index c937693..f791f06 100644
--- a/README.en.md
+++ b/README.en.md
@@ -64,7 +64,7 @@ In such a meeting each line is marked **In room** or **Online**. While you liste
### Speak back in their language
-Hold the right `⌥` key and speak so the room hears your translated voice through BlackHole 2ch, a virtual microphone you can install from the app with one click and that Cicada switches to automatically while interpreting is on. With an AI service key the other side hears natural voices (Gemini through OpenRouter or ZenMux, Ming through Ling), and Ling can also clone your own voice from an 8 to 30 second sample; without a key it uses macOS system voices.
+Hold the right `⌥` key and speak so the room hears your translated voice through BlackHole 2ch, a virtual microphone you can install from the app with one click and that Cicada switches to automatically while interpreting is on. The real and virtual microphones can use different sample rates; no manual alignment is needed. With an AI service key the other side hears natural voices (Gemini through OpenRouter or ZenMux, Ming through Ling), and Ling can also clone your own voice from an 8 to 30 second sample; without a key it uses macOS system voices.
<p align="center">
<img src="docs/interpreting.png" width="640" alt="Interpreting settings showing the right Option talk key, target language, Ming voice previews, and macOS voices">
diff --git a/README.ja.md b/README.ja.md
index 17b5f7d..184a4de 100644
--- a/README.ja.md
+++ b/README.ja.md
@@ -66,7 +66,7 @@ Cicada は、話されている言語を数秒以内にデバイス上で検出
### 相手の言語で話し返す
-右の `⌥` キーを押したまま話すと、BlackHole 2ch を通じて、翻訳された声が相手に届きます。BlackHole 2ch はアプリからワンクリックでインストールできる仮想マイクで、通訳がオンの間は Cicada が自動的に切り替えます。AI サービスのキーがあれば、相手には自然な音声(OpenRouter と ZenMux では Gemini、Ling では Ming)が聞こえ、Ling では 8〜30 秒のサンプルからあなた自身の声のクローンを作ることもできます。キーがない場合は、macOS のシステム音声を使います。
+右の `⌥` キーを押したまま話すと、BlackHole 2ch を通じて、翻訳された声が相手に届きます。BlackHole 2ch はアプリからワンクリックでインストールできる仮想マイクで、通訳がオンの間は Cicada が自動的に切り替えます。実際のマイクと仮想マイクのサンプルレートが異なっていても、手動でそろえる必要はありません。AI サービスのキーがあれば、相手には自然な音声(OpenRouter と ZenMux では Gemini、Ling では Ming)が聞こえ、Ling では 8〜30 秒のサンプルからあなた自身の声のクローンを作ることもできます。キーがない場合は、macOS のシステム音声を使います。
<p align="center">
<img src="docs/interpreting.png" width="640" alt="右 Option の話すキー、翻訳先の言語、Ming の音声のプレビュー、macOS の音声を表示した通訳の設定">
diff --git a/README.ko.md b/README.ko.md
index 8296bb6..13c8955 100644
--- a/README.ko.md
+++ b/README.ko.md
@@ -64,7 +64,7 @@ Zoom, Teams, Feishu 회의부터 X Spaces, Discord 스테이지, YouTube 라이
### 상대방 언어로 말하기
-오른쪽 `⌥` 키를 누른 채 말하면 번역된 음성이 가상 마이크인 BlackHole 2ch를 통해 회의 앱으로 전달됩니다. BlackHole 2ch는 앱에서 클릭 한 번으로 설치할 수 있으며, 동시통역을 켜면 Cicada가 자동으로 마이크를 전환합니다. AI 서비스 키를 설정하면 자연스러운 음성(OpenRouter나 ZenMux의 Gemini, Ling의 Ming)을 쓸 수 있고, Ling에서는 8~30초 분량의 녹음으로 내 목소리를 복제할 수도 있습니다. 키가 없어도 macOS 기본 음성을 사용할 수 있습니다.
+오른쪽 `⌥` 키를 누른 채 말하면 번역된 음성이 가상 마이크인 BlackHole 2ch를 통해 회의 앱으로 전달됩니다. BlackHole 2ch는 앱에서 클릭 한 번으로 설치할 수 있으며, 동시통역을 켜면 Cicada가 자동으로 마이크를 전환합니다. 실제 마이크와 가상 마이크의 샘플레이트가 달라도 수동으로 맞출 필요가 없습니다. AI 서비스 키를 설정하면 자연스러운 음성(OpenRouter나 ZenMux의 Gemini, Ling의 Ming)을 쓸 수 있고, Ling에서는 8~30초 분량의 녹음으로 내 목소리를 복제할 수도 있습니다. 키가 없어도 macOS 기본 음성을 사용할 수 있습니다.
<p align="center">
<img src="docs/interpreting.png" width="640" alt="오른쪽 Option 말하기 키, 대상 언어, Ming 음성 미리 듣기, macOS 음성이 표시된 동시통역 설정 화면">
diff --git a/README.md b/README.md
index cc34b01..65191a2 100644
--- a/README.md
+++ b/README.md
@@ -62,7 +62,7 @@
### 同声传译
-按住键盘右边的 `⌥` 键说话,对方在会议软件里就能直接听到翻译后的语音,打开同声传译时知了会自动把系统麦克风切到一键安装的虚拟驱动 BlackHole 2ch。填了 Ling API key 后可以用自然人声或用 8 到 30 秒录音克隆你自己的声音,不填也能用 Mac 自带语音。
+按住键盘右边的 `⌥` 键说话,对方在会议软件里就能直接听到翻译后的语音,打开同声传译时知了会自动把系统麦克风切到一键安装的虚拟驱动 BlackHole 2ch。真实麦克风与虚拟麦克风的采样率可以不同,无需手动统一。填了 Ling API key 后可以用自然人声或用 8 到 30 秒录音克隆你自己的声音,不填也能用 Mac 自带语音。
<p align="center">
<img src="docs/interpreting.png" width="640" alt="同声传译设置页,包含按住右 Option 键说话、目标语言、Ming 声音试听与本机声音选择">
diff --git a/Sources/Cicada/SpeakEngine.swift b/Sources/Cicada/SpeakEngine.swift
index d1768a7..28b859f 100644
--- a/Sources/Cicada/SpeakEngine.swift
+++ b/Sources/Cicada/SpeakEngine.swift
@@ -366,8 +366,10 @@ final class SpeakEngine {
if !voiceMic.inUse { voiceMic.release() }
let inputNode = mic.inputNode
AudioDevices.pinInput(mic, to: inputDevice?())
- let inFormat = inputNode.outputFormat(forBus: 0)
- guard inFormat.sampleRate > 0 else {
+ // Pinning can leave the client format at BlackHole's rate/channels. A tap must use
+ // the real hardware format or AVAudioEngine raises an Objective-C exception.
+ let inFormat = inputNode.inputFormat(forBus: 0)
+ guard inFormat.sampleRate > 0, inFormat.channelCount > 0 else {
onStatus?(L("没有可用的麦克风"))
phase = .idle
return
diff --git a/Tests/CicadaTests/SpeakMicTests.swift b/Tests/CicadaTests/SpeakMicTests.swift
new file mode 100644
index 0000000..8d6a62f
--- /dev/null
+++ b/Tests/CicadaTests/SpeakMicTests.swift
@@ -0,0 +1,51 @@
+import AVFoundation
+import CoreAudio
+import Foundation
+import Testing
+@testable import Cicada
+
+private final class MicFrames: RecognitionStream, @unchecked Sendable {
+ let format = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: 16_000, channels: 1, interleaved: false)!
+ var results: AsyncThrowingStream<Recognition.Piece, Error> { AsyncThrowingStream { $0.finish() } }
+ private let lock = NSLock()
+ private var frames = 0
+ var count: Int { lock.withLock { frames } }
+ func append(_ buffer: AVAudioPCMBuffer) { lock.withLock { frames += Int(buffer.frameLength) } }
+ func finalize(through seconds: Double) async throws {}
+ func finish() async {}
+ func cancel() async {}
+}
+
+@MainActor @Suite struct SpeakMicTests {
+ // Requires microphone permission and a virtual system input with a different format from
+ // the real mic. Opt in locally: CI has neither these devices nor microphone permission.
+ @Test(.enabled(if: ProcessInfo.processInfo.environment["CICADA_TEST_MIC"] == "1"))
+ func repeatedPressesCaptureFromTheRealMic() async throws {
+ let original = try #require(AudioDevices.defaultInput)
+ try #require(original.isVirtualMic, "Select BlackHole as the system input before this hardware test")
+ // A continuity microphone may be listed first: hardware tests must only open this Mac's mic.
+ let device = try #require(AudioDevices.inputs().first {
+ AudioDevices.transport($0.id) == kAudioDeviceTransportTypeBuiltIn
+ })
+ let frames = MicFrames()
+ let previousHost = Recognition.host
+ Recognition.host = { _, _ in frames }
+ defer { Recognition.host = previousHost }
+ let speak = SpeakEngine(settings: AppSettings(), voiceMic: VoiceMic())
+ speak.inputDevice = { device }
+ speak.probe = SpeakProbe()
+ defer { speak.pressCancelled() }
+ await speak.prepare()
+ for _ in 0..<3 {
+ let before = frames.count
+ speak.pressBegan()
+ try #require(speak.phase == .listening)
+ try await Task.sleep(for: .milliseconds(500))
+ // Cancel rather than release: this regression test must never translate or play audio.
+ speak.pressCancelled()
+ #expect(frames.count > before)
+ #expect(speak.phase == .idle)
+ }
+ #expect(AudioDevices.defaultInput?.uid == original.uid)
+ }
+}
diff --git a/site/changelog.json b/site/changelog.json
index 5887d14..e580078 100644
--- a/site/changelog.json
+++ b/site/changelog.json
@@ -6,6 +6,14 @@
"summary": "刚开完会就换 AI 服务时,设置里不再把连不上的新服务显示成已连接。",
"image": null,
"items": [
+ {
+ "kind": "fixed",
+ "platforms": [
+ "mac"
+ ],
+ "title": "真实麦克风与虚拟麦克风采样率不同时,按住说话键不再闪退。",
+ "text": "录音使用所选麦克风的实际格式,无需手动统一采样率。"
+ },
{
"kind": "fixed",
"platforms": [
diff --git a/tools/README.md b/tools/README.md
index 55860aa..b37a510 100644
--- a/tools/README.md
+++ b/tools/README.md
@@ -36,6 +36,7 @@
## 测试技巧
+- 麦克风切换回归:Mac 内置麦克风与 BlackHole 采样率不同、系统输入已选 BlackHole 时,运行 `CICADA_TEST_MIC=1 scripts/test.sh --filter SpeakMicTests`。测试只打开 Mac 内置麦克风,连续采集并取消三次;仅统计帧数,不识别、保存或上传音频,不改变系统输入。需要运行测试的程序已有麦克风权限,普通单元测试和 CI 默认跳过。
- 英文界面:`defaults write com.wquguru.cicada.dev AppleLanguages -array en` 后重启,测完 `defaults delete com.wquguru.cicada.dev AppleLanguages`;或启动时加 `--args -AppleLanguages "(en)"`。
- 纪要窗口不出现在截屏里:先发 `notesCapturable` 再 `screencapture -l`,或直接 `notesRender:`。
- 合成的按键会发给所有 App:别的 App 的启动器可能抢走 ⌥ Space。模拟鼠标和按键需要终端有“辅助功能”权限。