fix: refresh microphone list on CoreAudio device-list changes#178
Open
shumengliu wants to merge 1 commit into
Open
fix: refresh microphone list on CoreAudio device-list changes#178shumengliu wants to merge 1 commit into
shumengliu wants to merge 1 commit into
Conversation
AVCaptureDeviceWasConnected/Disconnected notifications are not delivered reliably for Bluetooth devices, so a mic that reconnected (e.g. AirPods auto-switching back from an iPhone) stayed missing from the microphone menu until app relaunch, and recording silently fell back to another device — on a lid-closed MacBook the built-in mic records pure silence, which whisper hallucinates into text like "Thank you.". Observe kAudioHardwarePropertyDevices on the system object instead; it fires for every HAL device change regardless of transport. Also keep the saved selection across a transient empty device list so it is restored when the device returns, and publish the list only when it actually changed so no-op HAL events don't rebuild the status bar menu. Fixes Starmel#176 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
4 tasks
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.
Summary
Fixes #179 — the Microphone menu goes stale when a Bluetooth mic (e.g. AirPods Pro) reconnects: the device shows up in System Settings → Sound but never reappears in the app's menu until relaunch, and recording silently falls back to another device. On a lid-closed MacBook that fallback is the built-in mic, which records pure digital silence that whisper hallucinates into text like "Thank you." (likely the same failure mode as #84).
Root cause
MicrophoneServicerefreshesavailableMicrophonesonly ininitand onAVCaptureDeviceWasConnected/Disconnectednotifications. Those AVFoundation notifications are not delivered reliably for Bluetooth audio devices re-registering with CoreAudio. I verified the sameAVCaptureDevice.DiscoverySessionquery returns the missing device when run fresh while the app's list is stale — the refresh triggers are the problem, not discovery.Fix
kAudioHardwarePropertyDeviceson the system object viaAudioObjectAddPropertyListenerBlock(main queue) and refresh the list + current mic when it fires. This is the same signal System Settings reacts to, and it fires for every HAL device change regardless of transport. The status bar menu and the in-app picker already rebuild reactively from$availableMicrophones, so no UI changes are needed.selectedMicrophonewhen the device list is momentarily empty (e.g. mid Bluetooth handover), so the user's saved choice is restored automatically when the device returns instead of being dropped until relaunch.availableMicrophonesonly when the list actually changed —kAudioHardwarePropertyDevicescan fire several times per connect, and every publish rebuilds the status bar menu.The existing AVFoundation notification observers are left in place; the CoreAudio listener is additive and the change-detection makes duplicate refreshes free.
Test plan
DiscoverySessionquery sees the reconnected device while the app's list is stale, and thatkAudioHardwarePropertyDevicesis the reliable change signal.swiftc -parseclean.Manual verification steps for anyone with the setup:
🤖 Generated with Claude Code