Repository navigation
[FIX] Recover the microphone when the audio route changes - #127
Merged
Merged
Conversation
A Bluetooth headset switches to its 16 kHz hands-free profile the moment something opens the input. AVAudioEngine answers a hardware change by stopping itself and posting AVAudioEngineConfigurationChange. Nothing observed it, so a session that began on that switch listened to an engine that was no longer running: no levels, the dead-microphone amber after 600ms, no words, and no error. Reproduced against the device with the old setup, which delivered zero buffers, and confirmed the recovery delivers audio at the new rate. Three things the reproduction settled. Restarting the same engine fails with -10868, so the engine is replaced. Installing a tap with a format captured moments earlier throws an Objective-C exception once the hardware has moved on, which Swift cannot catch, so the tap now takes nil and uses the bus's live format. And the converter is rebuilt when the buffers change rate, since the session no longer starts and ends in one format. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012Abfbei4sXuCngAWEH8Z2k
Measured against the headset: 670ms for the first engine.start, 270ms until the route change, then the rebuild, and the first buffer at about 1.4 seconds. The watchdog calls a microphone dead after 600ms of no levels, so every Bluetooth session went amber and looked broken for the second before it started working. That budget is for a microphone that stopped mid-sentence. A session that has never had a buffer is a different case and now gets 2.5s, after which amber still means what it always did. Also drops the settle delay before the rebuild, worth 250ms of the 1.4. Measurement showed a fresh engine starts cleanly with no delay at all: the -10868 that looked like it needed one came from restarting the old engine, not from being early. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012Abfbei4sXuCngAWEH8Z2k
tornikegomareli
added a commit
that referenced
this pull request
Sep 8, 2026
An empty file that a wide git add swept into #127. It is not part of the project. Ignored so the next wide add cannot catch it again. Claude-Session: https://claude.ai/code/session_012Abfbei4sXuCngAWEH8Z2k Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This was referenced Sep 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Dictating with Bluetooth earbuds starts a session that hears nothing: the HUD comes up, goes to the dead-microphone amber after 600ms, and no words arrive. A headset switches to its 16 kHz hands-free profile the moment something opens the input, and AVAudioEngine answers a hardware change by stopping itself and posting AVAudioEngineConfigurationChange. Nothing in the app observed that notification, so the session sat in front of an engine that was no longer running, with no error to show for it.
I reproduced it outside the app first, using the same setup MicrophoneInput had: the engine started, the notification fired, isRunning went false, and zero buffers arrived. The same probe with the recovery below delivers audio at the new rate after one rebuild.
The reproduction settled three things that a first guess would have got wrong. Restarting the same engine fails with -10868 from AUGraphParser, so the engine is replaced rather than restarted. Installing a tap with a format read moments earlier throws an Objective-C exception once the hardware has moved on, and Swift cannot catch that, so the app would crash instead of recovering: the tap now passes nil and uses the bus's live format. And the converter into the analyzer's format is rebuilt when the incoming rate changes, because a session no longer begins and ends in the same one.
A second change arriving mid-rebuild is queued rather than dropped, since a headset settling its profile can post more than one. If the rebuild fails the session ends with a reason instead of holding the shape open in front of a dead microphone.
Tests cover the converter following a format change and holding while the format does not. The recovery itself needs real hardware, so it is verified by the reproduction rather than by the suite.