---
title: "Comment by Sina Rezaei on \"Offline speech-to-text for Flutter, with no server and no API key\" | Hashnode"
description: "One edge case I’d be curious about is what happens when the audio route changes mid-session. For example, a user starts dictating through a Bluetooth headset, u"
url: "https://hashnode.com/posts/offline-speech-to-text-for-flutter/6ad65d66923dd340f7a3db90/comment/6ad74a7b4009bea95d294016"
---

[Discussion](https://hashnode.com/posts/offline-speech-to-text-for-flutter/6ad65d66923dd340f7a3db90)

Comment [Offline speech-to-text for Flutter, with no server and no API key](https://hashnode.com/posts/offline-speech-to-text-for-flutter/6ad65d66923dd340f7a3db90)

[Sina Rezaei](https://hashnode.com/@sinarezaei)

[Oct 20](https://hashnode.com/posts/offline-speech-to-text-for-flutter/6ad65d66923dd340f7a3db90/comment/6ad74a7b4009bea95d294016)

One edge case I’d be curious about is what happens when the audio route changes mid-session. For example, a user starts dictating through a Bluetooth headset, unplugs it halfway through a sentence, and the phone switches back to its built-in mic with a different sample rate.

On iOS, a route change can affect the sample rate and channel configuration, so it’s not just a microphone restart problem. How does `transcribe_cpp` handle that transition? Does it rebuild the capture pipeline while preserving the current utterance, or does it finalize the sentence and start a fresh one? That distinction matters quite a bit for hands-free transcription, especially during long sessions.

[Ishanga Vidusha](https://hashnode.com/@ishanga)

[Oct 20](https://hashnode.com/posts/offline-speech-to-text-for-flutter/6ad65d66923dd340f7a3db90/comment/6ad74a7b4009bea95d294016#reply-6ad7529aef919b190b2a4934)

Great question, and you've found a real gap. On a route change (headset connected or removed, earbuds plugged in), iOS capture tears down the tap and the resampler and rebuilds them against the new input format, because AVAudioEngine won't pick up the new rate by itself. Capture keeps writing to the same ring buffer, so nothing downstream has to reconnect, and you can see the change as a short "recovering" state.

On the Dart side, audio on either side of the switch isn't continuous, so voice detection and the endpointer are reset rather than stitching two moments into one. Today that reset throws away the sentence that was in progress instead of finishing it. Whatever the user said before the switch is lost, and a new sentence starts on the new mic. Finishing it properly is the right behaviour: decode what was heard before the switch as a final result, then start fresh. That's the next fix.

[Sina Rezaei](https://hashnode.com/@sinarezaei)

[Oct 20](https://hashnode.com/posts/offline-speech-to-text-for-flutter/6ad65d66923dd340f7a3db90/comment/6ad74a7b4009bea95d294016#reply-6ad75c8a2836b63b62f22998)

One thing I’d be careful about here is the timing of the final decode. Say the Bluetooth input is running at 16 kHz mono, and the route switches to the built-in mic at 44.1 kHz while the decoder still has unprocessed frames from the previous utterance. If the VAD and endpointer reset immediately, how do you make sure those buffered frames are actually decoded before the old state is discarded?

I’d probably treat the route change as a boundary between two audio segments: stop accepting frames from the old capture session, drain its pending audio through the existing resampling and decode path, emit a final result if there’s enough speech to recover, and only then reset the endpointing state for the new input. Otherwise, even if the ring buffer survives the switch, the sentence can still disappear because the decoder never gets a chance to finish it.

[Sina Rezaei](https://hashnode.com/@sinarezaei)

[Oct 20](https://hashnode.com/posts/offline-speech-to-text-for-flutter/6ad65d66923dd340f7a3db90/comment/6ad74a7b4009bea95d294016#reply-6ad75cb2bcf46b8a8af0c5b0)

There’s another edge case worth thinking about: what happens if a route change lands while the capture callback is still delivering buffers in the old format? For example, a Bluetooth headset disappears, the input switches to the built-in mic, and a delayed callback from the old tap arrives after the new resampler has been configured.

If both streams write into the same ring buffer, I’d want some explicit guarantee that frames from different input configurations can’t be interpreted using the wrong sample rate or channel layout. One approach would be to tag buffers with a capture-generation ID and format metadata, then reject stale callbacks while keeping the old segment available for final decoding. That would also make rapid headset connect/disconnect cycles easier to handle without accidentally mixing audio across route changes. Does the current capture layer already guard against that race, or is that something you’re planning to address alongside utterance preservation?

[Ishanga Vidusha](https://hashnode.com/@ishanga)

[Oct 21](https://hashnode.com/posts/offline-speech-to-text-for-flutter/6ad65d66923dd340f7a3db90/comment/6ad74a7b4009bea95d294016#reply-6ad8a647db658af902b4315c)

Fixed in 0.1.3, now on pub.dev. Testing it showed two problems, not one. On iOS, re-activating the audio session during the change was refused, so the mic could stop entirely; a route change now rebuilds only the engine. And the sentence in progress is now finished instead of dropped.

The order you describe is how it works now. The ring only ever holds 16 kHz mono, because each tap callback converts before it writes. So on a route change the audio already buffered from the old route is drained into the endpointer first, the open sentence is emitted as a final result and queued for the decoder, and only then is the endpointer reset for the new input. The decoder runs separately, so the reset can't cancel a decode that's already queued.

On stale callbacks: the old tap is removed and the engine stopped before the new converter is built, so a buffer in the old format shouldn't reach it. There's no explicit check, though, and checking each buffer's format against the converter's input is cheap, so I'll add one.
