Skip to content

[FIX] Recover the microphone when the audio route changes - #127

Merged
tornikegomareli merged 2 commits into
mainfrom
fix/bluetooth-route-change
Sep 8, 2026
Merged

tornikegomareli merged 2 commits into
mainfrom
fix/bluetooth-route-change

Conversation

@tornikegomareli

Copy link
Copy Markdown
Owner

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.

tornikegomareli and others added 2 commits September 8, 2026 14:26
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
tornikegomareli merged commit 047c214 into main Sep 8, 2026
1 check passed
@tornikegomareli
tornikegomareli deleted the fix/bluetooth-route-change branch September 8, 2026 10:38
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant