Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions Sources/WhisperKit/Core/Audio/AudioStreamTranscriber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
private let transcribeTask: TranscribeTask
private let audioProcessor: any AudioProcessing
private let decodingOptions: DecodingOptions
/// Optional input device to capture from. nil = system default input.
/// Lets callers stream from a specific device (e.g. a virtual device) instead
/// of always using the system default. Passed through to startRecordingLive.
private let inputDeviceID: DeviceID?

public init(
audioEncoder: any AudioEncoding,
Expand All @@ -52,6 +56,7 @@
silenceThreshold: Float = 0.3,
compressionCheckWindow: Int = 60,
useVAD: Bool = true,
inputDeviceID: DeviceID? = nil,
stateChangeCallback: AudioStreamTranscriberCallback?
) {
self.transcribeTask = TranscribeTask(
Expand All @@ -70,6 +75,7 @@
self.silenceThreshold = silenceThreshold
self.compressionCheckWindow = compressionCheckWindow
self.useVAD = useVAD
self.inputDeviceID = inputDeviceID
self.stateChangeCallback = stateChangeCallback
}

Expand All @@ -80,10 +86,17 @@
return
}
state.isRecording = true
try audioProcessor.startRecordingLive { [weak self] _ in
Task { [weak self] in
await self?.onAudioBufferCallback()
do {
try audioProcessor.startRecordingLive(inputDeviceID: inputDeviceID) { [weak self] _ in
Task { [weak self] in
await self?.onAudioBufferCallback()
}
}
Comment thread
DTM-7-RX marked this conversation as resolved.
} catch {
// Reset the flag so a failed start (e.g. an unavailable inputDeviceID)
// does not leave the actor stuck in a recording state that blocks retries.
state.isRecording = false
throw error
}
await realtimeLoop()
Logging.info("Realtime transcription has started")
Expand Down Expand Up @@ -197,7 +210,7 @@
options.clipTimestamps = [state.lastConfirmedSegmentEndSeconds]
let checkWindow = compressionCheckWindow
return try await transcribeTask
.run(audioArray: samples, decodeOptions: options) { [options] progress in

Check warning on line 213 in Sources/WhisperKit/Core/Audio/AudioStreamTranscriber.swift

View workflow job for this annotation

GitHub Actions / Pre-merge Tests (macos-15, 18.5, iPhone 16, 11.5, 2.5, 16.4) / iOS on macos-15

sending 'self.transcribeTask' risks causing data races; this is an error in the Swift 6 language mode

Check warning on line 213 in Sources/WhisperKit/Core/Audio/AudioStreamTranscriber.swift

View workflow job for this annotation

GitHub Actions / Pre-merge Tests (macos-14, 17.2, iPhone 15, 10.2, 1.0, 16.1) / iOS on macos-14

sending 'self.transcribeTask' risks causing data races; this is an error in the Swift 6 language mode
Task { [weak self] in
await self?.onProgressCallback(progress)
}
Expand Down
Loading