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
CoreAudio fixes (similar to prev commit but more references that were missed is included in this commit
altic-dev5933d49e1c2c
diff --git a/Sources/Fluid/ContentView.swift b/Sources/Fluid/ContentView.swiftindex c8c55354..1e13e6c2 100644--- a/Sources/Fluid/ContentView.swift+++ b/Sources/Fluid/ContentView.swift@@ -187,6 +187,47 @@ struct ContentView: View {// Configure menu bar manager with ASR servicemenuBarManager.configure(asrService: asr)+ // CRITICAL FIX: Defer all audio subsystem initialization by 1.5 seconds.+ // There is a known race condition between CoreAudio's HALSystem initialization (triggered by+ // AudioObjectAddPropertyListenerBlock) and SwiftUI's AttributeGraph metadata processing during app launch.+ // This race causes an EXC_BAD_ACCESS (SIGSEGV) at 0x0.+ // By waiting for the main runloop to settle and SwiftUI to finish its initial layout passes,+ // we ensure the audio system initializes safely.+ DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {+ DebugLogger.shared.info("🔊 Starting delayed audio initialization...", source: "ContentView")++ audioObserver.startObserving()+ asr.initialize()++ // Load available devices+ refreshDevices()++ // Set default selection if empty+ if selectedInputUID.isEmpty, let defIn = AudioDevice.getDefaultInputDevice()?.uid { selectedInputUID = defIn }+ if selectedOutputUID.isEmpty, let defOut = AudioDevice.getDefaultOutputDevice()?.uid { selectedOutputUID = defOut }++ // Apply saved preferences if present and available+ if let prefIn = SettingsStore.shared.preferredInputDeviceUID,+ prefIn.isEmpty == false,+ inputDevices.first(where: { $0.uid == prefIn }) != nil,+ prefIn != AudioDevice.getDefaultInputDevice()?.uid+ {+ _ = AudioDevice.setDefaultInputDevice(uid: prefIn)+ selectedInputUID = prefIn+ }++ if let prefOut = SettingsStore.shared.preferredOutputDeviceUID,+ prefOut.isEmpty == false,+ outputDevices.first(where: { $0.uid == prefOut }) != nil,+ prefOut != AudioDevice.getDefaultOutputDevice()?.uid+ {+ _ = AudioDevice.setDefaultOutputDevice(uid: prefOut)+ selectedOutputUID = prefOut+ }++ DebugLogger.shared.info("✅ Audio subsystems initialized", source: "ContentView")+ }+// Set up notch click callback for expanding command conversationNotchOverlayManager.shared.onNotchClicked = { [weak commandModeService] in// When notch is clicked in command mode, show expanded conversation@@ -221,27 +262,8 @@ struct ContentView: View {// Note: Overlay is now managed by MenuBarManager (persists even when window closed)- // Load devices and defaults- refreshDevices()- if selectedInputUID.isEmpty, let defIn = AudioDevice.getDefaultInputDevice()?.uid { selectedInputUID = defIn }- if selectedOutputUID.isEmpty, let defOut = AudioDevice.getDefaultOutputDevice()?.uid { selectedOutputUID = defOut }- // Apply saved preferences if present and available- if let prefIn = SettingsStore.shared.preferredInputDeviceUID,- prefIn.isEmpty == false,- inputDevices.first(where: { $0.uid == prefIn }) != nil,- prefIn != AudioDevice.getDefaultInputDevice()?.uid- {- _ = AudioDevice.setDefaultInputDevice(uid: prefIn)- selectedInputUID = prefIn- }- if let prefOut = SettingsStore.shared.preferredOutputDeviceUID,- prefOut.isEmpty == false,- outputDevices.first(where: { $0.uid == prefOut }) != nil,- prefOut != AudioDevice.getDefaultOutputDevice()?.uid- {- _ = AudioDevice.setDefaultOutputDevice(uid: prefOut)- selectedOutputUID = prefOut- }+ // Devices loaded in delayed audio initialization block+ // Device defaults and preferences handled in delayed block// Preload ASR model on app startup (with small delay to let app initialize)Task {diff --git a/Sources/Fluid/Services/ASRService.swift b/Sources/Fluid/Services/ASRService.swiftindex a69794d1..29afac60 100644--- a/Sources/Fluid/Services/ASRService.swift+++ b/Sources/Fluid/Services/ASRService.swift@@ -78,7 +78,11 @@ final class ASRService: ObservableObject}- private let engine = AVAudioEngine()+ // CRITICAL FIX: Use lazy initialization to prevent AVAudioEngine() from being called during+ // @StateObject init. AVAudioEngine's constructor triggers CoreAudio's HALSystem::InitializeShell()+ // which races with SwiftUI's AttributeGraph metadata processing and causes EXC_BAD_ACCESS crashes.+ // By making this lazy, the engine is only created when first accessed (after app launch is complete).+ private lazy var engine: AVAudioEngine = AVAudioEngine()private var inputFormat: AVAudioFormat?private var micPermissionGranted = false@@ -109,10 +113,25 @@ final class ASRService: ObservableObjectprivate let noiseGateThreshold: CGFloat = 0.06init(){+ // CRITICAL FIX: Do NOT call any audio-related APIs here!+ // This includes AVCaptureDevice.authorizationStatus, which can trigger AVFCapture/CoreAudio+ // initialization. All audio API calls are deferred to initialize() which runs after+ // SwiftUI's view graph is stable.+ //+ // Default values are set in the property declarations:+ // - micStatus = .notDetermined+ // - micPermissionGranted = false+ checkIfModelsExist()+ }++ /// Call this AFTER the app has finished launching to complete ASR initialization.+ /// This must be called from onAppear or later, never during init.+ func initialize() {+ // Check microphone permission (deferred from init to avoid AVFCapture race condition)self.micStatus = AVCaptureDevice.authorizationStatus(for: .audio)self.micPermissionGranted = (self.micStatus == .authorized)+registerDefaultDeviceChangeListener()- checkIfModelsExist()// Auto-load models if they exist on disk to avoid "Downloaded but not loaded" stateif modelsExistOnDisk {diff --git a/Sources/Fluid/Services/AudioDeviceService.swift b/Sources/Fluid/Services/AudioDeviceService.swiftindex 56051ef4..175fef71 100644--- a/Sources/Fluid/Services/AudioDeviceService.swift+++ b/Sources/Fluid/Services/AudioDeviceService.swift@@ -173,6 +173,15 @@ final class AudioHardwareObserver: ObservableObject {private var installed: Bool = falseinit() {+ // IMPORTANT: Do NOT call register() here!+ // Calling AudioObjectAddPropertyListenerBlock during @StateObject init causes a race condition+ // with SwiftUI's AttributeGraph metadata processing, leading to EXC_BAD_ACCESS crashes.+ // Registration is deferred until startObserving() is called after app finishes launching.+ }++ /// Call this AFTER the app has finished launching to start observing audio hardware changes.+ /// This must be called from onAppear or later, never during init.+ func startObserving() {register()}diff --git a/Sources/Fluid/UI/SettingsView.swift b/Sources/Fluid/UI/SettingsView.swiftindex 12a626be..cd673b5a 100644--- a/Sources/Fluid/UI/SettingsView.swift+++ b/Sources/Fluid/UI/SettingsView.swift@@ -30,6 +30,12 @@ struct SettingsView: View {@Binding var enableStreamingPreview: Bool@Binding var copyToClipboard: Bool+ // CRITICAL FIX: Cache default device names to avoid CoreAudio calls during view body evaluation.+ // Querying AudioDevice.getDefaultInputDevice() in the view body triggers HALSystem::InitializeShell()+ // which races with SwiftUI's AttributeGraph metadata processing and causes EXC_BAD_ACCESS crashes.+ @State private var cachedDefaultInputName: String = ""+ @State private var cachedDefaultOutputName: String = ""+let hotkeyManager: GlobalHotkeyManager?let menuBarManager: MenuBarManagerlet startRecording: () -> Void@@ -482,6 +488,9 @@ struct SettingsView: View {HStack(spacing: 10) {Button {refreshDevices()+ // Update cached default device names on refresh+ cachedDefaultInputName = AudioDevice.getDefaultInputDevice()?.name ?? ""+ cachedDefaultOutputName = AudioDevice.getDefaultOutputDevice()?.name ?? ""} label: {Label("Refresh", systemImage: "arrow.clockwise")}@@ -490,9 +499,10 @@ struct SettingsView: View {Spacer()- if let defIn = AudioDevice.getDefaultInputDevice()?.name,- let defOut = AudioDevice.getDefaultOutputDevice()?.name {- Text("Default: \(defIn) / \(defOut)")+ // CRITICAL FIX: Use cached values instead of querying CoreAudio in view body.+ // Querying AudioDevice here triggers HALSystem::InitializeShell() race condition.+ if !cachedDefaultInputName.isEmpty && !cachedDefaultOutputName.isEmpty {+ Text("Default: \(cachedDefaultInputName) / \(cachedDefaultOutputName)").font(.caption).foregroundStyle(.tertiary).lineLimit(1)@@ -587,6 +597,10 @@ struct SettingsView: View {}.onAppear {refreshDevices()+ // CRITICAL FIX: Populate cached default device names after onAppear, not during view body evaluation.+ // This avoids the CoreAudio/SwiftUI AttributeGraph race condition that causes EXC_BAD_ACCESS.+ cachedDefaultInputName = AudioDevice.getDefaultInputDevice()?.name ?? ""+ cachedDefaultOutputName = AudioDevice.getDefaultOutputDevice()?.name ?? ""}.onChange(of: visualizerNoiseThreshold) { newValue inSettingsStore.shared.visualizerNoiseThreshold = newValue