mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 02:47:34 +00:00
fix(speech): skip a canceled stream's remaining chunks inside one feed or finish
This commit is contained in:
@@ -29,4 +29,6 @@ export type SttRecognizer = {
|
||||
finish(streamId: string): Promise<void>
|
||||
/** Drop the stream's audio undecoded. */
|
||||
discard(streamId: string): void
|
||||
/** Called on a cancel's arrival, ahead of the queue: skip the stream's remaining chunks; the running decode finishes. */
|
||||
cancel(streamId: string): void
|
||||
}
|
||||
|
||||
@@ -174,9 +174,15 @@ export async function createTranscribeCppRecognizer(
|
||||
})
|
||||
}
|
||||
|
||||
// Canceled streams, until their queued discard runs; also covers a finish that already closed its stream.
|
||||
const canceled = new Set<string>()
|
||||
|
||||
const decode = async (streamId: string, stream: StreamCoverage, chunks: Float32Array[]) => {
|
||||
let undecoded = chunks.reduce((total, chunk) => total + chunk.length, 0)
|
||||
for (const chunk of chunks) {
|
||||
if (canceled.has(streamId)) {
|
||||
return
|
||||
}
|
||||
const startedAt = performance.now()
|
||||
const text = await transcribeChunk(chunk, stream.options)
|
||||
const decodeMs = performance.now() - startedAt
|
||||
@@ -254,7 +260,11 @@ export async function createTranscribeCppRecognizer(
|
||||
}
|
||||
},
|
||||
discard(streamId) {
|
||||
canceled.delete(streamId)
|
||||
closeStream(streamId)?.vad?.dispose()
|
||||
},
|
||||
cancel(streamId) {
|
||||
canceled.add(streamId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,6 +114,7 @@ async function handleStop(streamId: string): Promise<void> {
|
||||
// A cancel answers at once, even during Finish, and the stream's queued audio is skipped rather than decoded.
|
||||
function cancelStream(streamId: string): void {
|
||||
discarded.add(streamId)
|
||||
recognizer?.cancel(streamId)
|
||||
const input = audioPorts.get(streamId)
|
||||
audioPorts.delete(streamId)
|
||||
input?.port.close()
|
||||
|
||||
Reference in New Issue
Block a user