fix(speech): keep words spoken while the model loads

Stopping a CPU dictation closed the renderer's audio port while frames still
waited behind a decode in the speech worker; the close ended the stream at once
and the queued frames were dropped. A close after 'end' now leaves them to
decode. A model slower than 30 s to load also lost the start of the dictation,
since the renderer kept only 30 s of startup audio; it now keeps as much as the
host waits for the load.
This commit is contained in:
Kelvin Amoaba
2026-11-09 19:08:24 +00:00
parent d291670a01
commit 146bad42e9
6 changed files with 18 additions and 7 deletions
+1 -1
View File
@@ -16,7 +16,7 @@ import {
teardownIdleSttEngine
} from './stt-session-stop'
export { START_DICTATION_TIMEOUT_MS } from './stt-session-timeouts'
export { START_DICTATION_TIMEOUT_MS } from '../../shared/dictation-finish-deadlines'
export type SttEvent =
| { type: 'ready' }
+1 -1
View File
@@ -26,7 +26,7 @@ import {
initializeSttWorker,
waitForSttWorkerReady
} from './stt-worker-startup'
import { START_DICTATION_TIMEOUT_MS } from './stt-session-timeouts'
import { START_DICTATION_TIMEOUT_MS } from '../../shared/dictation-finish-deadlines'
import { STT_WORKER_EXITED_ERROR } from './stt-worker-process'
export async function startSttDictation(
-1
View File
@@ -1 +0,0 @@
export const START_DICTATION_TIMEOUT_MS = 60_000
+11 -3
View File
@@ -9,6 +9,8 @@ import type { SttWorkerRequest } from './stt-worker-process'
type AudioPortInput = {
port: MessagePortMain
onEnded: (() => void) | null
/** 'end' arrived; its frames may still wait behind a decode. */
ending: boolean
}
const parentPort = connectSttWorkerParentPort()
@@ -46,8 +48,7 @@ function attachAudioPort(streamId: string, port: MessagePortMain | undefined): v
if (!port) {
return
}
const input: AudioPortInput = { port, onEnded: null }
// The renderer closes its end after 'end'; a close alone means it went away.
const input: AudioPortInput = { port, onEnded: null, ending: false }
const end = (): void => {
if (audioPorts.get(streamId) !== input) {
return
@@ -59,6 +60,7 @@ function attachAudioPort(streamId: string, port: MessagePortMain | undefined): v
port.on('message', ({ data }) => {
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: only the desktop preload holds the other end, and it sends SpeechAudioPortMessage.
const msg = data as SpeechAudioPortMessage
input.ending ||= msg.type === 'end'
enqueue(() => {
// A discard or end ran first; a late frame must not reopen the stream.
if (audioPorts.get(streamId) !== input) {
@@ -69,7 +71,13 @@ function attachAudioPort(streamId: string, port: MessagePortMain | undefined): v
: handleFeed({ type: 'feed', streamId, samples: msg.samples, sampleRate: msg.sampleRate })
})
})
port.on('close', end)
// The renderer closes its end right after 'end', so that close must leave the queued frames to decode.
// A close alone means it canceled or went away.
port.on('close', () => {
if (!input.ending) {
end()
}
})
audioPorts.set(streamId, input)
port.start()
}
+3 -1
View File
@@ -8,6 +8,7 @@ import {
} from '@/components/dictation/dictation-capture-worklet-protocol'
import { DICTATION_SAMPLE_RATE } from '../../../shared/rpc-contract/speech-params'
import { DICTATION_METER_WINDOW_MS } from '../../../shared/dictation-audio-meter'
import { START_DICTATION_TIMEOUT_MS } from '../../../shared/dictation-finish-deadlines'
import { useAudioCaptureMeter, type DictationMeterPublisher } from './use-audio-capture-meter'
import { useAudioCaptureStopTail, type StopCaptureOptions } from './use-audio-capture-stop-tail'
@@ -35,7 +36,8 @@ export type StartAudioCaptureResult = {
fellBackToDefaultMicrophone: boolean
}
const MAX_BUFFERED_AUDIO_SECONDS = 30
// Holds everything said while the model loads, for as long as the host waits for it to load.
const MAX_BUFFERED_AUDIO_SECONDS = START_DICTATION_TIMEOUT_MS / 1000
const MAX_BUFFERED_AUDIO_BYTES = 8 * 1024 * 1024
const CAPTURE_WORKLET_OPTIONS: DictationCaptureWorkletOptions = {
outputSampleRate: DICTATION_SAMPLE_RATE,
+2
View File
@@ -7,6 +7,8 @@ const CLOUD_RESPONSE_ALLOWANCE_MS = 30_000
const UPLOAD_BYTES_PER_MS = 125
export const LOCAL_DICTATION_STOP_TIMEOUT_MS = 60_000
// How long an on-device model may take to load before its dictation fails.
export const START_DICTATION_TIMEOUT_MS = 60_000
// The host's answer still has to cross the network after its own deadline.
const PAIRED_CLIENT_REPLY_MARGIN_MS = 15_000