Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 5 additions & 3 deletions Sources/AuralKit/SpeechSession+Audio.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,14 @@ extension SpeechSession {
}

do {
try await reset()
try resetAudioCapture()
} catch {
if Self.shouldLog(.error) {
Self.logger.error(
"Failed to reset audio engine after route change: \(error.localizedDescription, privacy: .public)"
)
}
await endTranscription(error: error, generation: sessionGeneration)
}

publishCurrentAudioInputInfo()
Expand Down Expand Up @@ -279,14 +280,15 @@ extension SpeechSession {
#elseif os(macOS)
func handleEngineConfigurationChange() async {
do {
try await reset()
try resetAudioCapture()
} catch {
if Self.shouldLog(.error) {
let desc = error.localizedDescription
Self.logger.error(
"Failed to reset audio engine after configuration change: \(desc, privacy: .public)"
)
}
await endTranscription(error: error, generation: sessionGeneration)
}

publishCurrentAudioInputInfo()
Expand Down Expand Up @@ -348,7 +350,7 @@ extension SpeechSession {
}
#endif

func reset() async throws {
private func resetAudioCapture() throws {
if Self.shouldLog(.debug) {
Self.logger.debug("Resetting audio engine")
}
Expand Down
28 changes: 28 additions & 0 deletions Tests/AuralKitTests/SessionLifecycleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,34 @@ struct SpeechSessionLifecycleTests {
#expect(session.recognizerTask == nil)
}

#if os(macOS)
@Test("A microphone restart failure closes the result stream")
@MainActor
func microphoneRestartFailureClosesResults() async {
let session = SpeechSession(inputProviderPreference: .legacy)
let (results, resultBuilder) = AsyncThrowingStream<SpeechTranscriber.Result, Error>.makeStream()
session.continuation = .speech(resultBuilder)
session.streamingMode = .liveMicrophone
session.isAudioStreaming = true
session.setStatus(.transcribing)

await session.handleEngineConfigurationChange()

do {
try await awaitResult(timeout: 3) {
for try await _ in results { Issue.record("Expected no results") }
}
Issue.record("Expected the microphone restart error")
} catch SpeechSessionError.invalidAudioDataType {
} catch {
Issue.record(error)
}
#expect(session.status == .idle)
#expect(session.continuation == nil)
await session.cancelTranscribing()
}
#endif

/// Pausing is only meaningful for live capture; during file transcription it must be
/// refused (previously, resuming a "paused" file session started the microphone).
@Test("Pause is refused during file transcription")
Expand Down