知了 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: record the virtual mic test with a Core Audio IO proc, check microphone access first
wquguru26bf2f9a103d
diff --git a/Sources/LiveSubs/SettingsWindow.swift b/Sources/LiveSubs/SettingsWindow.swiftindex 55e1d38..04a4bcf 100644--- a/Sources/LiveSubs/SettingsWindow.swift+++ b/Sources/LiveSubs/SettingsWindow.swift@@ -208,18 +208,18 @@ struct TestResultView: View {let result: VirtualMicInstaller.TestResultvar body: some View {HStack(alignment: .center, spacing: 12) {- HStack(alignment: .center, spacing: 2) {+ if result.heard { HStack(alignment: .center, spacing: 2) {ForEach(Array(result.levels.enumerated()), id: \.offset) { _, l inCapsule().fill(result.heard ? Palette.accent : Color(white: 0.8)).frame(width: 3, height: max(3, CGFloat(min(1, l * 2.5)) * 24))}}- .frame(height: 24)+ .frame(height: 24) }if result.heard {(Text("对方会听到:") + Text(result.text.isEmpty ? "一段声音" : "“\(result.text)”").bold()).font(.system(size: 12)).foregroundStyle(Palette.ink)} else {- Text("虚拟麦克风里没收到声音,再试一次").font(.system(size: 12)).foregroundStyle(Palette.accentInk)+ Text(result.problem ?? "虚拟麦克风里没收到声音,再试一次").font(.system(size: 12)).foregroundStyle(Palette.accentInk)}Spacer(minLength: 0)}diff --git a/Sources/LiveSubs/SpeakEngine.swift b/Sources/LiveSubs/SpeakEngine.swiftindex 97c8a1a..f596166 100644--- a/Sources/LiveSubs/SpeakEngine.swift+++ b/Sources/LiveSubs/SpeakEngine.swift@@ -417,7 +417,11 @@ final class SpeakEngine {}player.play()}- debugLog("spoke \(buffers.count) buffers to \(device?.name ?? "default output")")+ var cur = AudioObjectID(0); var sz = UInt32(MemoryLayout<AudioObjectID>.size)+ if let unit = out.outputNode.audioUnit {+ AudioUnitGetProperty(unit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &cur, &sz)+ }+ debugLog("spoke \(buffers.count) buffers to \(device?.name ?? "default output") id=\(device?.id ?? 0) readback=\(cur)")}/// Core Audio restarted (virtual mic install): device IDs changed, rebuild the output next time.diff --git a/Sources/LiveSubs/VirtualMic.swift b/Sources/LiveSubs/VirtualMic.swiftindex e13d96b..32d41b6 100644--- a/Sources/LiveSubs/VirtualMic.swift+++ b/Sources/LiveSubs/VirtualMic.swift@@ -30,6 +30,8 @@ final class VirtualMicInstaller: ObservableObject {var heard: Boolvar text: Stringvar levels: [Float]+ /// Why nothing was heard, shown instead of the generic line.+ var problem: String? = nil}@Published private(set) var state: State = .idle@@ -92,10 +94,19 @@ final class VirtualMicInstaller: ObservableObject {guard !busy, let device else { return }Task {state = .testing+ // Reading the virtual mic's input side is microphone access like any other.+ if AVCaptureDevice.authorizationStatus(for: .audio) == .notDetermined {+ _ = await AVCaptureDevice.requestAccess(for: .audio)+ }+ guard AVCaptureDevice.authorizationStatus(for: .audio) == .authorized else {+ test = .init(heard: false, text: "", levels: [], problem: "试音要用麦克风权限,请在系统设置里允许 Live Subs")+ state = .idle+ return+ }test = await LoopbackTest.run(device: device, phrase: Self.testPhrase) { [weak self] inawait self?.speakThrough?(Self.testPhrase, "en", device)}- debugLog("virtual mic test: heard=\(test?.heard ?? false) text=\(test?.text ?? "")")+ debugLog("virtual mic test: mic=\(AVCaptureDevice.authorizationStatus(for: .audio).rawValue) heard=\(test?.heard ?? false) text=\(test?.text ?? "")")state = .idle}}@@ -179,17 +190,19 @@ enum LoopbackTest {static func run(device: OutputDevice, phrase: String, play: @escaping () async -> Void) async -> VirtualMicInstaller.TestResult {let recorder = LoopbackRecorder()guard recorder.start(deviceID: device.id) else {- return .init(heard: false, text: "", levels: [])+ return .init(heard: false, text: "", levels: [], problem: "打不开虚拟麦克风,点“让系统认出它”后再试")}try? await Task.sleep(nanoseconds: 150_000_000)await play()try? await Task.sleep(nanoseconds: 400_000_000)let buffers = recorder.stop()+ debugLog("loopback: \(buffers.first?.frameLength ?? 0) frames")let levels = LoopbackRecorder.levels(buffers, bars: 12)let loud = (levels.max() ?? 0) > 0.02let text = loud ? await transcribe(buffers, locale: "en-US") : ""// Any real signal counts; the transcript is shown so people can judge it themselves.- return .init(heard: loud, text: text, levels: levels)+ return .init(heard: loud, text: text, levels: levels,+ problem: loud ? nil : buffers.isEmpty ? "虚拟麦克风没有送来声音,再试一次" : nil)}private static func transcribe(_ buffers: [AVAudioPCMBuffer], locale: String) async -> String {@@ -216,41 +229,58 @@ enum LoopbackTest {}}-/// An input-only engine pointed at one device (the virtual mic), collecting buffers.+/// Records one device's input side with a plain Core Audio IO proc. (An AVAudioEngine input+/// node switched to another device keeps the default mic's format and can stop itself on the+/// format change, which made the test flaky.)final class LoopbackRecorder: @unchecked Sendable {- private let engine = AVAudioEngine()private let lock = NSLock()- private var buffers: [AVAudioPCMBuffer] = []+ private var samples: [Float] = []+ private var device = AudioObjectID(0)+ private var proc: AudioDeviceIOProcID?+ private(set) var sampleRate: Double = 48_000+ private let queue = DispatchQueue(label: "com.wquguru.cicada.loopback")func start(deviceID: AudioObjectID) -> Bool {- let input = engine.inputNode- guard let unit = input.audioUnit else { return false }- var id = deviceID- let status = AudioUnitSetProperty(unit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0,- &id, UInt32(MemoryLayout<AudioObjectID>.size))- let format = input.outputFormat(forBus: 0)- guard status == noErr, format.sampleRate > 0 else {- debugLog("loopback: cannot open device \(deviceID) status=\(status)")- return false- }- input.installTap(onBus: 0, bufferSize: 4096, format: format) { [weak self] buf, _ in- guard let self, let copy = buf.copy() as? AVAudioPCMBuffer else { return }- self.lock.lock(); self.buffers.append(copy); self.lock.unlock()+ device = deviceID+ var rate = Float64(0)+ var size = UInt32(MemoryLayout<Float64>.size)+ var addr = AudioObjectPropertyAddress(mSelector: kAudioDevicePropertyNominalSampleRate,+ mScope: kAudioObjectPropertyScopeGlobal,+ mElement: kAudioObjectPropertyElementMain)+ if AudioObjectGetPropertyData(deviceID, &addr, 0, nil, &size, &rate) == noErr, rate > 0 { sampleRate = rate }+ let status = AudioDeviceCreateIOProcIDWithBlock(&proc, deviceID, queue) { [weak self] _, input, _, _, _ in+ guard let self else { return }+ let list = UnsafeMutableAudioBufferListPointer(UnsafeMutablePointer(mutating: input))+ guard let first = list.first, let data = first.mData else { return }+ let channels = max(1, Int(first.mNumberChannels))+ let frames = Int(first.mDataByteSize) / MemoryLayout<Float>.size / channels+ let p = data.assumingMemoryBound(to: Float.self)+ self.lock.lock()+ for i in 0..<frames { self.samples.append(p[i * channels]) } // first channel is enough+ self.lock.unlock()}- do {- try engine.start()- return true- } catch {- debugLog("loopback: start failed \(error)")+ guard status == noErr, let proc else {+ debugLog("loopback: io proc failed \(status)")return false}+ let started = AudioDeviceStart(deviceID, proc)+ debugLog("loopback: recording device \(deviceID) at \(sampleRate) Hz, start=\(started)")+ return started == noErr}func stop() -> [AVAudioPCMBuffer] {- engine.inputNode.removeTap(onBus: 0)- engine.stop()- lock.lock(); defer { lock.unlock() }- return buffers+ if let proc {+ AudioDeviceStop(device, proc)+ AudioDeviceDestroyIOProcID(device, proc)+ }+ proc = nil+ lock.lock(); let all = samples; lock.unlock()+ guard !all.isEmpty,+ let format = AVAudioFormat(standardFormatWithSampleRate: sampleRate, channels: 1),+ let buf = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: AVAudioFrameCount(all.count)) else { return [] }+ buf.frameLength = AVAudioFrameCount(all.count)+ all.withUnsafeBufferPointer { buf.floatChannelData![0].update(from: $0.baseAddress!, count: all.count) }+ return [buf]}/// Peak level per slice of the recording, 0…1, for the little bar graph.