知了 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
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(.enabled(if: ProcessInfo.processInfo.environment["CICADA_TEST_MIC"] == "1"))
struct SpeakMicTests {
private func devices() throws -> (original: OutputDevice, mic: OutputDevice) {
let original = try #require(AudioDevices.defaultInput)
try #require(original.isVirtualMic, "Select BlackHole as the system input before this hardware test")
let mic = try #require(AudioDevices.inputs().first {
AudioDevices.transport($0.id) == kAudioDeviceTransportTypeBuiltIn
})
return (original, mic)
}
@Test func repeatedPressesCaptureFromTheRealMic() async throws {
let (original, device) = try devices()
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))
speak.pressCancelled()
#expect(frames.count > before)
#expect(speak.phase == .idle)
}
#expect(AudioDevices.defaultInput?.uid == original.uid)
}
@Test func roomMicCapturesFromTheRealMic() async throws {
let (original, device) = try devices()
let room = RoomMic(device: device)
let frames = MicFrames()
room.onAudio = { frames.append($0) }
try room.start()
try await Task.sleep(for: .milliseconds(500))
room.stop()
#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)
recorder.cancel()
}
#expect(AudioDevices.defaultInput?.uid == original.uid)
}
}