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: recording a voice sample no longer crashes when the system input is a virtual mic with a different format

wquguru257c9cea72d3
diff --git a/Sources/Cicada/Voices.swift b/Sources/Cicada/Voices.swift
index 00cca06..edd68dc 100644
--- a/Sources/Cicada/Voices.swift
+++ b/Sources/Cicada/Voices.swift
@@ -283,8 +283,9 @@ final class VoiceRecorder: ObservableObject {
try? FileManager.default.removeItem(at: url)
let input = engine.inputNode
AudioDevices.pinInput(engine, to: device)
- let inFormat = input.outputFormat(forBus: 0)
- guard inFormat.sampleRate > 0,
+ // The hardware format, as in `RoomMic.start`: the client one can still be the system mic's.
+ let inFormat = input.inputFormat(forBus: 0)
+ guard inFormat.sampleRate > 0, inFormat.channelCount > 0,
let f = try? AVAudioFile(forWriting: url, settings: format.settings, commonFormat: .pcmFormatInt16, interleaved: true),
let conv = AVAudioConverter(from: inFormat, to: format) else {
phase = .failed(L("麦克风没收到声音,请检查麦克风权限"))
diff --git a/Tests/CicadaTests/SpeakMicTests.swift b/Tests/CicadaTests/SpeakMicTests.swift
index 7738962..1543a4a 100644
--- a/Tests/CicadaTests/SpeakMicTests.swift
+++ b/Tests/CicadaTests/SpeakMicTests.swift
@@ -66,4 +66,19 @@ struct SpeakMicTests {
#expect(frames.count > 0)
#expect(AudioDevices.defaultInput?.uid == original.uid)
}
+
+ @Test func voiceSampleRecordsFromTheRealMic() async throws {
+ let (original, device) = try devices()
+ let recorder = VoiceRecorder()
+ for _ in 0..<2 {
+ recorder.start(locale: "zh-CN", device: device)
+ #expect(recorder.phase == .recording)
+ try await Task.sleep(for: .milliseconds(500))
+ let size = (try? FileManager.default.attributesOfItem(atPath: recorder.url.path)[.size] as? Int) ?? 0
+ #expect(size > 8_000) // a quarter second of 16 kHz 16-bit audio
+ // Cancel: the sample is deleted, never transcribed or uploaded.
+ recorder.cancel()
+ }
+ #expect(AudioDevices.defaultInput?.uid == original.uid)
+ }
}
diff --git a/tools/README.md b/tools/README.md
index eacc649..acc9a00 100644
--- a/tools/README.md
+++ b/tools/README.md
@@ -36,7 +36,7 @@
## 测试技巧
-- 麦克风切换回归:Mac 内置麦克风与 BlackHole 采样率不同、系统输入已选 BlackHole 时,运行 `CICADA_TEST_MIC=1 scripts/test.sh --filter SpeakMicTests`。测试只打开 Mac 内置麦克风,连续采集并取消三次;仅统计帧数,不识别、保存或上传音频,不改变系统输入。需要运行测试的程序已有麦克风权限,普通单元测试和 CI 默认跳过。
+- 麦克风切换回归:系统输入选 BlackHole,Mac 内置麦克风改成与它不同的采样率(音频 MIDI 设置),运行 `CICADA_TEST_MIC=1 swift test --filter SpeakMicTests`。按住说话、会议室麦克风、录声音样本各开内置麦克风几次,只统计帧数或在取消时删掉样本,不识别、上传或播放,不改系统输入。运行测试的终端要有麦克风权限;普通单元测试和 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。模拟鼠标和按键需要终端有“辅助功能”权限。