Fastest and only macOS Dictation app with on-device STT and custom trained AI enhancement model. Windows, iOS testflight! Android, Linux waitlist open! A local Wispr Flow alternative. Follow https://x.com/fluidvoiceapp for more
fix cache microphone liveness off main
altic-dev03a0cfd3ebe3
diff --git a/Sources/Fluid/ContentView.swift b/Sources/Fluid/ContentView.swiftindex 9bba5c98..b2383181 100644--- a/Sources/Fluid/ContentView.swift+++ b/Sources/Fluid/ContentView.swift@@ -1519,7 +1519,7 @@ struct ContentView: View {// Query CoreAudio off the main thread — during device topology changes, synchronous// CoreAudio calls on main can deadlock while the HAL is still settling.DispatchQueue.global(qos: .userInitiated).async {- let inputs = AudioDevice.listInputDevices()+ let inputs = AudioDevice.listInputDevicesRefreshingLiveness()let outputs = AudioDevice.listOutputDevices()let defaultInputUID = AudioDevice.getDefaultInputDevice()?.uidDispatchQueue.main.async {diff --git a/Sources/Fluid/Services/ASRService.swift b/Sources/Fluid/Services/ASRService.swiftindex cb49889b..d4838b8a 100644--- a/Sources/Fluid/Services/ASRService.swift+++ b/Sources/Fluid/Services/ASRService.swift@@ -1370,7 +1370,7 @@ final class ASRService: ObservableObject {let initialInputSnapshot = await withCheckedContinuation { continuation inDispatchQueue.global(qos: .userInitiated).async {- let devices = AudioDevice.listInputDevices()+ let devices = AudioDevice.listInputDevicesRefreshingLiveness()let defaultInputUID = AudioDevice.getDefaultInputDevice()?.uidcontinuation.resume(returning: (devices, defaultInputUID))}@@ -2998,7 +2998,7 @@ final class ASRService: ObservableObject {) async -> Bool {let snapshot = await withCheckedContinuation { continuation inDispatchQueue.global(qos: .userInitiated).async {- let devices = AudioDevice.listInputDevices()+ let devices = AudioDevice.listInputDevicesRefreshingLiveness()let defaultInputUID = AudioDevice.getDefaultInputDevice()?.uidcontinuation.resume(returning: (devices, defaultInputUID))}@@ -3504,30 +3504,43 @@ final class ASRService: ObservableObject {// Perform CoreAudio queries off the main thread — during a device topology change// the HAL may still be settling, and synchronous queries on main can deadlock.DispatchQueue.global(qos: .userInitiated).async { [weak self] in- let currentDevices = AudioDevice.listInputDevices()+ let currentDevices = AudioDevice.listInputDevicesRefreshingLiveness()let defaultInputUID = AudioDevice.getDefaultInputDevice()?.uidDispatchQueue.main.async { [weak self] inguard let self else { return }let cachedUIDs = self.cachedDeviceUIDslet cachedDeviceIDsByUID = self.cachedInputDeviceIDsByUID+ let cachedLivenessByUID = self.cachedInputLivenessByUIDlet currentUIDs = Set(currentDevices.map(\.uid))let currentDeviceIDsByUID = Dictionary(currentDevices.map { ($0.uid, $0.id) },uniquingKeysWith: { current, _ in current })+ let currentLivenessByUID = Dictionary(+ currentDevices.map { ($0.uid, $0.isAlive) },+ uniquingKeysWith: { current, _ in current }+ )DebugLogger.shared.debug("Current input devices: \(currentDevices.map { $0.name }.joined(separator: ", "))", source: "ASRService")- if currentUIDs != cachedUIDs || currentDeviceIDsByUID != cachedDeviceIDsByUID {+ if currentUIDs != cachedUIDs ||+ currentDeviceIDsByUID != cachedDeviceIDsByUID ||+ currentLivenessByUID != cachedLivenessByUID+ {let microphonePreferenceCoordinator =AppServices.shared.microphonePreferenceCoordinatorlet migrationPending = microphonePreferenceCoordinator.needsMicrophonePriorityMigration- microphonePreferenceCoordinator.reconcileMicrophoneSelection(+ let resolvedInput = microphonePreferenceCoordinator.reconcileMicrophoneSelection(availableInputs: currentDevices,defaultInputUID: defaultInputUID)let priorityUIDs = SettingsStore.shared.microphonePriority.map(\.uid)+ let livenessRequiresRecovery =+ (self.isRunning &&+ resolvedInput?.uid != microphonePreferenceCoordinator.confirmedActiveInputUID) ||+ (self.hasPreparedAudioCapture &&+ resolvedInput?.id != self.directAudioLifecycleController.snapshot.deviceID)let shouldReconcileInputSelection = AudioCaptureIdlePolicy.shouldReconcileInputSelection(priorityInputUIDs: priorityUIDs,migrationPending: migrationPending,@@ -3537,7 +3550,7 @@ final class ASRService: ObservableObject {priorityInputUIDs: priorityUIDs,previousInputDeviceIDsByUID: cachedDeviceIDsByUID,currentInputDeviceIDsByUID: currentDeviceIDsByUID- )+ ) || livenessRequiresRecoveryif shouldReconcileInputSelection {self.scheduleAudioRouteRecovery(reason: "app microphone availability changed",@@ -3555,21 +3568,20 @@ final class ASRService: ObservableObject {/// Handles device availability changes (device disconnected or reconnected)private func handleDeviceAvailabilityChanged(deviceID: AudioObjectID) {DebugLogger.shared.info("⚠️ Device availability changed for ID: \(deviceID)", source: "ASRService")+ DispatchQueue.global(qos: .userInitiated).async { [weak self] in+ let isAlive = AudioDevice.refreshInputDeviceLiveness(deviceID: deviceID)+ DispatchQueue.main.async { [weak self] in+ self?.applyDeviceAvailability(isAlive: isAlive, deviceID: deviceID)+ }+ }+ }- // Check if device is still alive- var address = AudioObjectPropertyAddress(- mSelector: kAudioDevicePropertyDeviceIsAlive,- mScope: kAudioObjectPropertyScopeGlobal,- mElement: kAudioObjectPropertyElementMain+ private func applyDeviceAvailability(isAlive: Bool, deviceID: AudioObjectID) {+ DebugLogger.shared.debug(+ "Device \(deviceID) cached alive status: \(isAlive)",+ source: "ASRService")-- var isAlive: UInt32 = 0- var size = UInt32(MemoryLayout<UInt32>.size)- let status = AudioObjectGetPropertyData(deviceID, &address, 0, nil, &size, &isAlive)-- DebugLogger.shared.debug("Device \(deviceID) alive status query: status=\(status), isAlive=\(isAlive)", source: "ASRService")-- if status == noErr, isAlive == 0 {+ if isAlive == false {// Device disconnectedDebugLogger.shared.warning("❌ Monitored device (ID: \(deviceID)) DISCONNECTED", source: "ASRService")self.stopMonitoringDevice()@@ -3586,7 +3598,7 @@ final class ASRService: ObservableObject {} else {DebugLogger.shared.info("Not recording - device disconnect handled gracefully", source: "ASRService")}- } else if status == noErr, isAlive != 0 {+ } else {DebugLogger.shared.info("✅ Device (ID: \(deviceID)) is still alive", source: "ASRService")}}@@ -3629,6 +3641,7 @@ final class ASRService: ObservableObject {/// Device caching for change detectionprivate var cachedDeviceUIDs: Set<String> = []private var cachedInputDeviceIDsByUID: [String: AudioObjectID] = [:]+ private var cachedInputLivenessByUID: [String: Bool] = [:]private func cacheCurrentDeviceList(_ devices: [AudioDevice.Device]) {self.cachedDeviceUIDs = Set(devices.map { $0.uid })@@ -3636,6 +3649,10 @@ final class ASRService: ObservableObject {devices.map { ($0.uid, $0.id) },uniquingKeysWith: { current, _ in current })+ self.cachedInputLivenessByUID = Dictionary(+ devices.map { ($0.uid, $0.isAlive) },+ uniquingKeysWith: { current, _ in current }+ )}// Audio tap processing is handled by AudioCapturePipeline (thread-safe).diff --git a/Sources/Fluid/Services/AudioDeviceService.swift b/Sources/Fluid/Services/AudioDeviceService.swiftindex 66b63bf5..b2d50d5e 100644--- a/Sources/Fluid/Services/AudioDeviceService.swift+++ b/Sources/Fluid/Services/AudioDeviceService.swift@@ -21,6 +21,7 @@ nonisolated enum AudioDevice {let hasInput: Boollet hasOutput: Boollet transportType: UInt32+ let isAlive: Boolinit(id: AudioObjectID,@@ -28,7 +29,8 @@ nonisolated enum AudioDevice {name: String,hasInput: Bool,hasOutput: Bool,- transportType: UInt32 = kAudioDeviceTransportTypeUnknown+ transportType: UInt32 = kAudioDeviceTransportTypeUnknown,+ isAlive: Bool = true) {self.id = idself.uid = uid@@ -36,6 +38,7 @@ nonisolated enum AudioDevice {self.hasInput = hasInputself.hasOutput = hasOutputself.transportType = transportType+ self.isAlive = isAlive}var isBluetooth: Bool {@@ -48,6 +51,39 @@ nonisolated enum AudioDevice {}}+ private struct InputLivenessKey: Hashable {+ let id: AudioObjectID+ let uid: String+ }++ private final class InputLivenessCache: @unchecked Sendable {+ private let lock = NSLock()+ private var values: [InputLivenessKey: Bool] = [:]++ func snapshot() -> [InputLivenessKey: Bool] {+ self.lock.lock()+ defer { self.lock.unlock() }+ return self.values+ }++ func replace(with values: [InputLivenessKey: Bool]) {+ self.lock.lock()+ self.values = values+ self.lock.unlock()+ }++ func update(deviceID: AudioObjectID, isAlive: Bool) {+ self.lock.lock()+ let matchingKeys = self.values.keys.filter { $0.id == deviceID }+ for key in matchingKeys {+ self.values[key] = isAlive+ }+ self.lock.unlock()+ }+ }++ private static let inputLivenessCache = InputLivenessCache()+static func listAllDevices() -> [Device] {var address = AudioObjectPropertyAddress(mSelector: kAudioHardwarePropertyDevices,@@ -84,6 +120,7 @@ nonisolated enum AudioDevice {return []}+ let cachedInputLiveness = self.inputLivenessCache.snapshot()var devices: [Device] = []devices.reserveCapacity(deviceIDs.count)@@ -104,7 +141,10 @@ nonisolated enum AudioDevice {name: name,hasInput: hasIn,hasOutput: hasOut,- transportType: transportType+ transportType: transportType,+ isAlive: cachedInputLiveness[+ InputLivenessKey(id: devId, uid: uid)+ ] ?? true))}@@ -116,6 +156,40 @@ nonisolated enum AudioDevice {return self.listAllDevices().filter { $0.hasInput }}+ /// Refreshes the HAL liveness snapshot. Call only from an existing background+ /// hardware-refresh path; UI and capture selection consume the cached value.+ static func listInputDevicesRefreshingLiveness() -> [Device] {+ let devices = self.listInputDevices()+ let liveness = Dictionary(+ uniqueKeysWithValues: devices.map { device in+ (+ InputLivenessKey(id: device.id, uid: device.uid),+ self.queryInputDeviceLiveness(device.id)+ )+ }+ )+ self.inputLivenessCache.replace(with: liveness)+ return devices.map { device in+ Device(+ id: device.id,+ uid: device.uid,+ name: device.name,+ hasInput: device.hasInput,+ hasOutput: device.hasOutput,+ transportType: device.transportType,+ isAlive: liveness[InputLivenessKey(id: device.id, uid: device.uid)] ?? true+ )+ }+ }++ /// Refreshes one monitored device without blocking the listener's main queue.+ /// Callers must invoke this from a background hardware-refresh path.+ static func refreshInputDeviceLiveness(deviceID: AudioObjectID) -> Bool {+ let isAlive = self.queryInputDeviceLiveness(deviceID)+ self.inputLivenessCache.update(deviceID: deviceID, isAlive: isAlive)+ return isAlive+ }+static func listOutputDevices() -> [Device] {return self.listAllDevices().filter { $0.hasOutput }}@@ -147,10 +221,14 @@ nonisolated enum AudioDevice {return self.listInputDevices().first { $0.uid == uid }}- /// Core Audio may continue enumerating an input after it has stopped being usable,- /// notably the internal microphone during some clamshell transitions.- /// Fail open when the property cannot be queried so unusual/virtual devices still work.+ /// Reads the liveness value captured by the latest background hardware refresh.static func isInputDeviceAlive(_ device: Device) -> Bool {+ device.isAlive+ }++ /// Core Audio may continue enumerating an input after it has stopped being usable.+ /// Fail open when HAL cannot answer so unusual and virtual devices still work.+ private static func queryInputDeviceLiveness(_ deviceID: AudioObjectID) -> Bool {var address = AudioObjectPropertyAddress(mSelector: kAudioDevicePropertyDeviceIsAlive,mScope: kAudioObjectPropertyScopeGlobal,@@ -159,7 +237,7 @@ nonisolated enum AudioDevice {var value: UInt32 = 1var dataSize = UInt32(MemoryLayout<UInt32>.size)let status = AudioObjectGetPropertyData(- device.id,+ deviceID,&address,0,nil,@@ -487,7 +565,7 @@ final class AudioHardwareObserver: ObservableObject {self.inputAvailabilityRefreshGeneration &+= 1let generation = self.inputAvailabilityRefreshGenerationDispatchQueue.global(qos: .utility).async { [weak self] in- let devices = AudioDevice.listInputDevices()+ let devices = AudioDevice.listInputDevicesRefreshingLiveness()DispatchQueue.main.async { [weak self] inguard let self,self.inputAvailabilityRefreshGeneration == generation@@ -511,15 +589,18 @@ final class AudioHardwareObserver: ObservableObject {var address = Self.inputAvailabilityAddresslet token: AudioObjectPropertyListenerBlock = { [weak self] _, _ in// Never query Core Audio synchronously from its property callback.- // Hop to the next main-queue turn before observers resolve priority.- DispatchQueue.main.async { [weak self] in- guard let self else { return }- self.changeTick &+= 1- NotificationCenter.default.post(- name: .inputDeviceAvailabilityDidChange,- object: self,- userInfo: ["deviceID": deviceID]- )+ // Refresh the cached snapshot off-main before observers resolve priority.+ DispatchQueue.global(qos: .userInitiated).async { [weak self] in+ _ = AudioDevice.listInputDevicesRefreshingLiveness()+ DispatchQueue.main.async { [weak self] in+ guard let self else { return }+ self.changeTick &+= 1+ NotificationCenter.default.post(+ name: .inputDeviceAvailabilityDidChange,+ object: self,+ userInfo: ["deviceID": deviceID]+ )+ }}}let status = AudioObjectAddPropertyListenerBlock(diff --git a/Sources/Fluid/Services/MenuBarManager.swift b/Sources/Fluid/Services/MenuBarManager.swiftindex 8c006098..4d331d2c 100644--- a/Sources/Fluid/Services/MenuBarManager.swift+++ b/Sources/Fluid/Services/MenuBarManager.swift@@ -606,8 +606,8 @@ final class MenuBarManager: NSObject, ObservableObject, NSMenuDelegate {loadingItem.isEnabled = falsesubmenu.addItem(loadingItem)- DispatchQueue.global(qos: .userInitiated).async {- let inputDevices = AudioDevice.listInputDevices()+ DispatchQueue.global(qos: .userInitiated).async { [weak self] in+ let inputDevices = AudioDevice.listInputDevicesRefreshingLiveness()let defaultInputUID = AudioDevice.getDefaultInputDevice()?.uidDispatchQueue.main.async { [weak self] indiff --git a/Tests/FluidDictationIntegrationTests/HotkeyShortcutTests.swift b/Tests/FluidDictationIntegrationTests/HotkeyShortcutTests.swiftindex 1bdeae59..b23ab78c 100644--- a/Tests/FluidDictationIntegrationTests/HotkeyShortcutTests.swift+++ b/Tests/FluidDictationIntegrationTests/HotkeyShortcutTests.swift@@ -980,6 +980,19 @@ final class HotkeyShortcutTests: XCTestCase {}}+ func testInputDeviceLivenessUsesSnapshotWithoutQueryingHAL() {+ let unavailable = AudioDevice.Device(+ id: 42,+ uid: "unavailable",+ name: "Unavailable Microphone",+ hasInput: true,+ hasOutput: false,+ isAlive: false+ )++ XCTAssertFalse(AudioDevice.isInputDeviceAlive(unavailable))+ }+@MainActorfunc testClamshellSkipsEnumeratedUnusableBuiltInMicrophone() throws {try self.withRestoredDefaults(keys: [