Skip to content

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(audio): retry settling preferred input

altic-dev7d780f400654
diff --git a/Sources/Fluid/Services/ASRService.swift b/Sources/Fluid/Services/ASRService.swift
index 004a4193..8924e693 100644
--- a/Sources/Fluid/Services/ASRService.swift
+++ b/Sources/Fluid/Services/ASRService.swift
@@ -865,12 +865,13 @@ final class ASRService: ObservableObject {
}
private func resolvedInputDeviceForCapture(
+ availableInputs: [AudioDevice.Device] = AudioDevice.listInputDevices(),
+ defaultInputUID: String? = AudioDevice.getDefaultInputDevice()?.uid,
excluding excludedUIDs: Set<String> = []
) -> AudioDevice.Device? {
- let inputs = AudioDevice.listInputDevices()
return AppServices.shared.microphonePreferenceCoordinator.inputDeviceForCapture(
- availableInputs: inputs,
- defaultInputUID: AudioDevice.getDefaultInputDevice()?.uid,
+ availableInputs: availableInputs,
+ defaultInputUID: defaultInputUID,
excluding: excludedUIDs
)
}
@@ -945,11 +946,29 @@ final class ASRService: ObservableObject {
// release.
await self.audioEngineRetirementDrain.waitForScheduledReleases()
do {
+ let deviceSnapshot = await Task.detached(priority: .userInitiated) {
+ let allDevices = AudioDevice.listAllDevices()
+ return (
+ allDevices: allDevices,
+ defaultInputUID: AudioDevice.getDefaultInputDevice(from: allDevices)?.uid
+ )
+ }.value
+ let allDevices = deviceSnapshot.allDevices
+ let availableInputs = allDevices.filter(\.hasInput)
let selectedInput: AudioDevice.Device?
if let forcingInputUID {
- selectedInput = AudioDevice.listInputDevices().first { $0.uid == forcingInputUID }
+ selectedInput = availableInputs.first { $0.uid == forcingInputUID }
} else {
- selectedInput = self.resolvedInputDeviceForCapture(excluding: excludedInputUIDs)
+ selectedInput = AudioCaptureIdlePolicy.bluetoothInputAwaitingAvailability(
+ priorityInputUIDs: SettingsStore.shared.microphonePriority.map(\.uid),
+ preferredInputUID: SettingsStore.shared.preferredInputDeviceUID,
+ allDevices: allDevices,
+ excluding: excludedInputUIDs
+ ) ?? self.resolvedInputDeviceForCapture(
+ availableInputs: availableInputs,
+ defaultInputUID: deviceSnapshot.defaultInputUID,
+ excluding: excludedInputUIDs
+ )
}
guard let attemptIdentity = AudioCaptureIdlePolicy.CaptureAttemptIdentity.resolve(
selectedInput: selectedInput,
diff --git a/Sources/Fluid/Services/AudioCaptureIdlePolicy.swift b/Sources/Fluid/Services/AudioCaptureIdlePolicy.swift
index 62b6d91b..4f52dd6f 100644
--- a/Sources/Fluid/Services/AudioCaptureIdlePolicy.swift
+++ b/Sources/Fluid/Services/AudioCaptureIdlePolicy.swift
@@ -126,6 +126,25 @@ enum AudioCaptureIdlePolicy {
}
}
+ static func bluetoothInputAwaitingAvailability(
+ priorityInputUIDs: [String],
+ preferredInputUID: String?,
+ allDevices: [AudioDevice.Device],
+ excluding excludedUIDs: Set<String>
+ ) -> AudioDevice.Device? {
+ let priorityUID = priorityInputUIDs.first { excludedUIDs.contains($0) == false }
+ let candidateUID = priorityUID ?? preferredInputUID.flatMap { uid in
+ excludedUIDs.contains(uid) ? nil : uid
+ }
+ guard let candidateUID,
+ let candidate = allDevices.first(where: { $0.uid == candidateUID }),
+ candidate.isBluetooth,
+ candidate.hasInput == false,
+ candidate.isAlive
+ else { return nil }
+ return candidate
+ }
+
static func shouldPrewarmCapture(experimentalDirectAudioCaptureEnabled: Bool) -> Bool {
experimentalDirectAudioCaptureEnabled
}
diff --git a/Sources/Fluid/Services/AudioDeviceService.swift b/Sources/Fluid/Services/AudioDeviceService.swift
index c9669fa1..206247b6 100644
--- a/Sources/Fluid/Services/AudioDeviceService.swift
+++ b/Sources/Fluid/Services/AudioDeviceService.swift
@@ -14,7 +14,7 @@ import IOKit.pwr_mgt
// MARK: - Audio Device Manager
nonisolated enum AudioDevice {
- struct Device: Identifiable, Hashable {
+ struct Device: Identifiable, Hashable, Sendable {
static let externalMicrophoneDataSourceID: UInt32 = 0x656d6963 // 'emic'
let id: AudioObjectID
@@ -213,8 +213,14 @@ nonisolated enum AudioDevice {
}
static func getDefaultInputDevice() -> Device? {
- guard let devId: AudioObjectID = getDefaultDeviceId(selector: kAudioHardwarePropertyDefaultInputDevice) else { return nil }
- return self.listAllDevices().first { $0.id == devId }
+ self.getDefaultInputDevice(from: self.listAllDevices())
+ }
+
+ static func getDefaultInputDevice(from devices: [Device]) -> Device? {
+ guard let deviceID: AudioObjectID = getDefaultDeviceId(
+ selector: kAudioHardwarePropertyDefaultInputDevice
+ ) else { return nil }
+ return devices.first { $0.id == deviceID }
}
static func getDefaultOutputDevice() -> Device? {
diff --git a/Tests/FluidDictationIntegrationTests/HotkeyShortcutTests.swift b/Tests/FluidDictationIntegrationTests/HotkeyShortcutTests.swift
index 63f5cfdc..af3756c6 100644
--- a/Tests/FluidDictationIntegrationTests/HotkeyShortcutTests.swift
+++ b/Tests/FluidDictationIntegrationTests/HotkeyShortcutTests.swift
@@ -724,6 +724,52 @@ final class HotkeyShortcutTests: XCTestCase {
XCTAssertFalse(replacement?.isBluetooth == true)
}
+ func testCaptureAttemptSeedsPreferredBluetoothIdentityBeforeInputAppears() {
+ let airPodsOutputProfile = AudioDevice.Device(
+ id: 42,
+ uid: "airpods",
+ name: "AirPods",
+ hasInput: false,
+ hasOutput: true,
+ transportType: kAudioDeviceTransportTypeBluetooth
+ )
+
+ let candidate = AudioCaptureIdlePolicy.bluetoothInputAwaitingAvailability(
+ priorityInputUIDs: ["airpods", "built-in"],
+ preferredInputUID: "airpods",
+ allDevices: [airPodsOutputProfile],
+ excluding: []
+ )
+
+ XCTAssertEqual(candidate?.uid, "airpods")
+ XCTAssertTrue(candidate?.isBluetooth == true)
+ }
+
+ func testCaptureAttemptDoesNotWaitForLowerPriorityBluetoothInput() {
+ let builtIn = Self.device(
+ uid: "built-in",
+ name: "MacBook Pro Microphone",
+ transportType: kAudioDeviceTransportTypeBuiltIn
+ )
+ let airPodsOutputProfile = AudioDevice.Device(
+ id: 42,
+ uid: "airpods",
+ name: "AirPods",
+ hasInput: false,
+ hasOutput: true,
+ transportType: kAudioDeviceTransportTypeBluetooth
+ )
+
+ let candidate = AudioCaptureIdlePolicy.bluetoothInputAwaitingAvailability(
+ priorityInputUIDs: ["built-in", "airpods"],
+ preferredInputUID: "built-in",
+ allDevices: [builtIn, airPodsOutputProfile],
+ excluding: []
+ )
+
+ XCTAssertNil(candidate)
+ }
+
func testBluetoothStartupPolicyDoesNotAffectOtherInputsOrActiveRecovery() {
var stabilization = AudioCaptureIdlePolicy.BluetoothInputStabilization()