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 bluetooth microphone startup churn
altic-dev9417cfca1437
diff --git a/Sources/Fluid/Services/ASRService.swift b/Sources/Fluid/Services/ASRService.swiftindex 845e58c6..3e40d0e2 100644--- a/Sources/Fluid/Services/ASRService.swift+++ b/Sources/Fluid/Services/ASRService.swift@@ -710,6 +710,7 @@ final class ASRService: ObservableObject {private var activeAudioCaptureBackend: AudioCaptureBackend = .noneprivate var audioStartAttemptInputUID: String?private var audioStartAttemptInputName: String?+ private var audioStartAttemptIsBluetooth = falseprivate var hasPreparedAudioCapture: Bool {self.directAudioLifecycleController.snapshot.isPrepared || self.hasWarmAudioEngine@@ -919,19 +920,21 @@ final class ASRService: ObservableObject {}private func startConfiguredAudioCapture(- excluding excludedInputUIDs: Set<String> = []+ excluding excludedInputUIDs: Set<String> = [],+ forcingInputUID: String? = nil) async throws {self.audioStartAttemptInputUID = nilself.audioStartAttemptInputName = nil+ self.audioStartAttemptIsBluetooth = falseif SettingsStore.shared.experimentalDirectAudioCaptureEnabled {// A non-route path may have scheduled a fire-and-forget retirement.// Do not let direct capture startup overlap a queued AVAudioEngine// release.await self.audioEngineRetirementDrain.waitForScheduledReleases()do {- guard let selection = self.directCoreAudioDeviceSelection(- excluding: excludedInputUIDs- ) else {+ let selection = forcingInputUID.map(DirectCoreAudioDeviceSelection.preferredUID) ??+ self.directCoreAudioDeviceSelection(excluding: excludedInputUIDs)+ guard let selection else {throw NSError(domain: "ASRService",code: -4,@@ -947,6 +950,7 @@ final class ASRService: ObservableObject {)self.audioStartAttemptInputUID = device.uidself.audioStartAttemptInputName = device.name+ self.audioStartAttemptIsBluetooth = device.isBluetoothAppServices.shared.microphonePreferenceCoordinator.reportResolvedSelection(uid: device.uid,name: device.name@@ -1716,23 +1720,54 @@ final class ASRService: ObservableObject {? max(AudioDevice.listInputDevices().count, 1) + 1: 1var startAttempt = 1+ var fallbackAttempt = 1var failedInputUIDs = Set<String>()var immediatelyRetriedInputUID: String?+ var bluetoothStabilization = AudioCaptureIdlePolicy.BluetoothInputStabilization()+ var forcedInputUID: String?self.audioStartAttemptInputUID = nilwhile true {let routeGenerationAtStart = self.audioRouteRecoveryGenerationdo {- try await self.startConfiguredAudioCapture(excluding: failedInputUIDs)+ try await self.startConfiguredAudioCapture(+ excluding: failedInputUIDs,+ forcingInputUID: forcedInputUID+ )} catch {guard let failedUID = self.audioStartAttemptInputUID else { throw error }- let retrySameInput = immediatelyRetriedInputUID == nil- if retrySameInput {+ let now = ProcessInfo.processInfo.systemUptime+ let retryBluetoothInput = bluetoothStabilization.shouldRetry(+ inputUID: failedUID,+ isBluetoothInput: self.audioStartAttemptIsBluetooth,+ now: now+ )+ let retrySameInput = retryBluetoothInput || immediatelyRetriedInputUID == nil+ if retryBluetoothInput {+ forcedInputUID = failedUIDimmediatelyRetriedInputUID = failedUID+ self.logBluetoothStartupRetry(+ uid: failedUID,+ attempt: startAttempt + 1,+ elapsed: bluetoothStabilization.elapsed(at: now),+ reason: error.localizedDescription+ )+ } else {+ forcedInputUID = nil+ if bluetoothStabilization.inputUID == failedUID {+ self.logBluetoothStartupStabilizationEnded(+ uid: failedUID,+ elapsed: bluetoothStabilization.elapsed(at: now),+ outcome: "budget_exhausted"+ )+ }+ if immediatelyRetriedInputUID == nil {+ immediatelyRetriedInputUID = failedUID+ } else {+ failedInputUIDs.insert(failedUID)+ }+ fallbackAttempt += 1}- if retrySameInput == false {- failedInputUIDs.insert(failedUID)- }- guard startAttempt < maximumStartAttempts,+ guard retryBluetoothInput || fallbackAttempt <= maximumStartAttempts,startGeneration == self.audioCaptureStartGeneration,self.isTerminating == falseelse {@@ -1743,7 +1778,7 @@ final class ASRService: ObservableObject {startGeneration: startGeneration,completedAttempt: startAttempt,reason: "backend_start_error:\(error.localizedDescription)",- waitForTopologyQuiet: retrySameInput == false+ waitForTopologyQuiet: retryBluetoothInput || retrySameInput == false)startAttempt += 1continue@@ -1770,6 +1805,15 @@ final class ASRService: ObservableObject {self.pendingAudioRouteRecovery == nil &&self.isRecoveringAudioRoute == falseif readiness == .ready, routeStayedStable {+ if let stabilizedUID = bluetoothStabilization.inputUID {+ self.logBluetoothStartupStabilizationEnded(+ uid: stabilizedUID,+ elapsed: bluetoothStabilization.elapsed(+ at: ProcessInfo.processInfo.systemUptime+ ),+ outcome: "first_pcm"+ )+ }AppServices.shared.microphonePreferenceCoordinator.confirmActiveSelection(uid: self.audioStartAttemptInputUID,name: self.audioStartAttemptInputName@@ -1791,17 +1835,44 @@ final class ASRService: ObservableObject {throw CancellationError()}var retrySameInput = false+ var retryBluetoothInput = falseif let failedUID = self.audioStartAttemptInputUID {- retrySameInput = readiness == .formatInvalidated &&- immediatelyRetriedInputUID == nil- if retrySameInput {+ let now = ProcessInfo.processInfo.systemUptime+ retryBluetoothInput = bluetoothStabilization.shouldRetry(+ inputUID: failedUID,+ isBluetoothInput: self.audioStartAttemptIsBluetooth,+ now: now+ )+ retrySameInput = retryBluetoothInput || (+ readiness == .formatInvalidated && immediatelyRetriedInputUID == nil+ )+ if retryBluetoothInput {+ forcedInputUID = failedUIDimmediatelyRetriedInputUID = failedUID- }- if retrySameInput == false {- failedInputUIDs.insert(failedUID)+ self.logBluetoothStartupRetry(+ uid: failedUID,+ attempt: startAttempt + 1,+ elapsed: bluetoothStabilization.elapsed(at: now),+ reason: "readiness_\(readiness)"+ )+ } else {+ forcedInputUID = nil+ if bluetoothStabilization.inputUID == failedUID {+ self.logBluetoothStartupStabilizationEnded(+ uid: failedUID,+ elapsed: bluetoothStabilization.elapsed(at: now),+ outcome: "budget_exhausted"+ )+ }+ if retrySameInput {+ immediatelyRetriedInputUID = failedUID+ } else {+ failedInputUIDs.insert(failedUID)+ fallbackAttempt += 1+ }}}- guard startAttempt < maximumStartAttempts else {+ guard retryBluetoothInput || fallbackAttempt <= maximumStartAttempts else {let message: Stringswitch readiness {case .timedOut:@@ -1827,7 +1898,7 @@ final class ASRService: ObservableObject {startGeneration: startGeneration,completedAttempt: startAttempt,reason: "readiness_\(readiness)_routeStable_\(routeStayedStable)",- waitForTopologyQuiet: retrySameInput == false+ waitForTopologyQuiet: retryBluetoothInput || retrySameInput == false)startAttempt += 1}@@ -2012,6 +2083,33 @@ final class ASRService: ObservableObject {return attemptID}+ private func logBluetoothStartupRetry(+ uid: String,+ attempt: Int,+ elapsed: TimeInterval,+ reason: String+ ) {+ let elapsedMilliseconds = Int((elapsed * 1000).rounded())+ let maximumMilliseconds = Int(+ (AudioCaptureIdlePolicy.BluetoothInputStabilization.maximumDuration * 1000).rounded()+ )+ self.benchmarkLog(+ "bluetooth_start_retry uid=\(uid) attempt=\(attempt) " ++ "elapsedMs=\(elapsedMilliseconds) budgetMs=\(maximumMilliseconds) reason=\(reason)"+ )+ }++ private func logBluetoothStartupStabilizationEnded(+ uid: String,+ elapsed: TimeInterval,+ outcome: String+ ) {+ self.benchmarkLog(+ "bluetooth_start_stabilization_end uid=\(uid) outcome=\(outcome) " ++ "elapsedMs=\(Int((elapsed * 1000).rounded()))"+ )+ }+func cancelPendingAudioCaptureStart(reason: String) async {guard self.isStarting, self.isRunning == false else { return }self.audioCaptureStartGeneration &+= 1@@ -2978,6 +3076,19 @@ final class ASRService: ObservableObject {self.benchmarkLog("route_recovery_ignored reason=app_terminating event=\(reason)")return}+ if AudioCaptureIdlePolicy.shouldDeferRouteRecoveryToBluetoothStart(+ directCaptureEnabled: SettingsStore.shared.experimentalDirectAudioCaptureEnabled,+ isStarting: self.isStarting,+ isRunning: self.isRunning,+ attemptedInputIsBluetooth: self.audioStartAttemptIsBluetooth+ ) {+ // AirPods and other Bluetooth inputs can replace their streams more+ // than once while entering microphone mode. The active start owns+ // its bounded retry loop; a second recovery owner would exclude the+ // same healthy device before the Bluetooth route settles.+ self.benchmarkLog("bluetooth_start_route_change_deferred event=\(reason)")+ return+ }self.audioRouteRecoveryGeneration &+= 1let requiresPrewarmAfterRecovery =requiresIdlePrewarm || self.pendingAudioRouteRecovery?.requiresIdlePrewarm == truediff --git a/Sources/Fluid/Services/AudioCaptureIdlePolicy.swift b/Sources/Fluid/Services/AudioCaptureIdlePolicy.swiftindex 37f2d6c1..c4aac2bb 100644--- a/Sources/Fluid/Services/AudioCaptureIdlePolicy.swift+++ b/Sources/Fluid/Services/AudioCaptureIdlePolicy.swift@@ -1,6 +1,31 @@import CoreAudioenum AudioCaptureIdlePolicy {+ struct BluetoothInputStabilization {+ static let maximumDuration: TimeInterval = 5++ private(set) var inputUID: String?+ private(set) var startedAt: TimeInterval?++ mutating func shouldRetry(+ inputUID: String,+ isBluetoothInput: Bool,+ now: TimeInterval+ ) -> Bool {+ guard isBluetoothInput || self.inputUID == inputUID else { return false }+ if self.inputUID != inputUID || self.startedAt == nil {+ self.inputUID = inputUID+ self.startedAt = now+ }+ return self.elapsed(at: now) < Self.maximumDuration+ }++ func elapsed(at now: TimeInterval) -> TimeInterval {+ guard let startedAt else { return 0 }+ return max(now - startedAt, 0)+ }+ }+static func shouldPrewarmCapture(experimentalDirectAudioCaptureEnabled: Bool) -> Bool {experimentalDirectAudioCaptureEnabled}@@ -53,4 +78,13 @@ enum AudioCaptureIdlePolicy {) -> Bool {isRunning || isStarting}++ static func shouldDeferRouteRecoveryToBluetoothStart(+ directCaptureEnabled: Bool,+ isStarting: Bool,+ isRunning: Bool,+ attemptedInputIsBluetooth: Bool+ ) -> Bool {+ directCaptureEnabled && isStarting && isRunning == false && attemptedInputIsBluetooth+ }}diff --git a/Tests/FluidDictationIntegrationTests/HotkeyShortcutTests.swift b/Tests/FluidDictationIntegrationTests/HotkeyShortcutTests.swiftindex 0320ce47..8c6d8b1b 100644--- a/Tests/FluidDictationIntegrationTests/HotkeyShortcutTests.swift+++ b/Tests/FluidDictationIntegrationTests/HotkeyShortcutTests.swift@@ -655,6 +655,54 @@ final class HotkeyShortcutTests: XCTestCase {XCTAssertFalse(builtInDevice.isBluetooth)}+ func testBluetoothStartupRetriesSameInputWithinBoundedWindow() {+ var stabilization = AudioCaptureIdlePolicy.BluetoothInputStabilization()++ XCTAssertTrue(stabilization.shouldRetry(+ inputUID: "airpods",+ isBluetoothInput: true,+ now: 10+ ))+ XCTAssertTrue(stabilization.shouldRetry(+ inputUID: "airpods",+ isBluetoothInput: false,+ now: 14.999+ ))+ XCTAssertFalse(stabilization.shouldRetry(+ inputUID: "airpods",+ isBluetoothInput: false,+ now: 15+ ))+ }++ func testBluetoothStartupPolicyDoesNotAffectOtherInputsOrActiveRecovery() {+ var stabilization = AudioCaptureIdlePolicy.BluetoothInputStabilization()++ XCTAssertFalse(stabilization.shouldRetry(+ inputUID: "usb",+ isBluetoothInput: false,+ now: 10+ ))+ XCTAssertTrue(AudioCaptureIdlePolicy.shouldDeferRouteRecoveryToBluetoothStart(+ directCaptureEnabled: true,+ isStarting: true,+ isRunning: false,+ attemptedInputIsBluetooth: true+ ))+ XCTAssertFalse(AudioCaptureIdlePolicy.shouldDeferRouteRecoveryToBluetoothStart(+ directCaptureEnabled: true,+ isStarting: true,+ isRunning: true,+ attemptedInputIsBluetooth: true+ ))+ XCTAssertFalse(AudioCaptureIdlePolicy.shouldDeferRouteRecoveryToBluetoothStart(+ directCaptureEnabled: true,+ isStarting: true,+ isRunning: false,+ attemptedInputIsBluetooth: false+ ))+ }+@MainActorfunc testLegacySystemModeSeedsPriorityFromCurrentDefault() throws {try self.withRestoredDefaults(keys: [