FluidVoice icon indicating copy to clipboard operation
FluidVoice copied to clipboard

Add support for choosing custom audio input

Open grohith327 opened this issue 4 months ago โ€ข 1 comments

Description

Add support for choosing custom audio input separate from MacOS default. The following is the behavior

  • Toggle on: input dropdown stays enabled.
  • Toggle on + user changes input: FluidVoice immediately syncs that selection to the Mac default input.
  • Toggle off + user changes input: FluidVoice only saves the manual preference and does not immediately change the Mac input.
  • Toggle on transition still restores/syncs back to the captured system input when available.

Also, add a menu bar setting to toggle micrphone

Type of Change

  • [ ] ๐Ÿž Bug fix
  • [x] โœจ New feature
  • [ ] ๐Ÿ’ฅ Breaking change
  • [ ] ๐Ÿงน Chore
  • [ ] ๐Ÿ“ Documentation update

Related Issue or Discussion

Closes #136

Testing

  • [ ] Tested on Intel Mac
  • [x] Tested on Apple Silicon Mac
  • [x] Tested on macOS version: 27
  • [x] Ran linter locally: swiftlint --strict --config .swiftlint.yml Sources
  • [x] Ran tests locally

Screenshots / Video

https://github.com/user-attachments/assets/8506c208-6379-4a73-8b91-b57879c977f7

grohith327 avatar Jul 05 '26 17:07 grohith327

@altic-dev Good to review, tested with the new audio optimized pipeline

grohith327 avatar Jul 08 '26 02:07 grohith327

@greptileai review

altic-dev avatar Jul 10 '26 04:07 altic-dev

Greptile Summary

This PR introduces a MicrophoneSelectionMode enum (.system / .manual) to replace the previously inert syncAudioDevicesWithSystem stub, adds a new MicrophonePreferenceCoordinator that centralises preferred-device enforcement with retry logic, and surfaces both a Settings-panel toggle and a menu-bar item for switching between modes.

  • MicrophonePreferenceCoordinator โ€” new testable coordinator with AudioDeviceManaging protocol injection; handles enforcement, fallback, and hardware-change stabilisation.
  • SettingsStore.setMicrophoneSelectionMode โ€” atomically transitions modes, saves the pre-manual system UID for restoration, and returns the UID to restore on .system re-entry.
  • One confirmed desync โ€” ContentView.microphoneSelectionMode is a @State initialised once from SettingsStore; changes made via the menu bar are not reflected in the binding passed to SettingsView, causing onChange(of: inputDevices) to branch on the wrong mode after a menu-bar toggle.

Confidence Score: 4/5

Safe to merge after addressing the mode-binding desync between the menu bar and SettingsView.

The menu bar can change SettingsStore.microphoneSelectionMode independently of the @State copy held by ContentView. After such a toggle, SettingsView.onChange(of: inputDevices) branches on the stale local value, which can silently reset selectedInputUID to the system default even when the user just enabled manual mode. The coordinator design, SettingsStore transitions, recording-path device resolution, and test coverage are all solid.

Sources/Fluid/ContentView.swift โ€” the microphoneSelectionMode @State needs to be kept in sync with SettingsStore when the mode is changed via the menu bar.

Reviews (6): Last reviewed commit: "fix for preferred device being unavailab..." | Re-trigger Greptile

greptile-apps avatar Jul 10 '26 04:07 greptile-apps

Greptile Summary

This PR introduces a MicrophoneSelectionMode enum (.system / .manual) to replace the always-true syncAudioDevicesWithSystem flag, giving users explicit control over whether FluidVoice follows the macOS default input or locks to a preferred microphone. A new MicrophonePreferenceCoordinator service centralizes enforcement of the preferred device โ€” re-asserting it after hardware topology changes โ€” and is injected via a lazy property on AppServices.

  • New coordinator: MicrophonePreferenceCoordinator provides enforcePreferredInput, inputDeviceForCapture, and a debounced stabilizePreferredInputAfterHardwareChange with a progressive retry schedule; a AudioDeviceManaging protocol makes it fully unit-testable via FakeAudioDeviceManager.
  • Settings & menu bar: a toggle is added to both the Settings panel and the menu bar microphone submenu; SettingsStore.setMicrophoneSelectionMode snapshots the system UID before entering manual mode so it can be restored on toggle-off.
  • One defect identified: the hardware-change handler in ContentView sets selectedInputUID to the stored preferred UID without verifying the device is still in the refreshed device list, leaving the picker blank when the preferred device disconnects.

Confidence Score: 3/5

The core recording path correctly delegates device resolution to MicrophonePreferenceCoordinator, which handles device unavailability safely. However, the ContentView hardware-change handler leaves the picker UI in a broken blank state when a preferred device disconnects in manual mode.

The device-availability check dropped from the ContentView hardware-change .manual branch is a real UI regression: after the preferred device is unplugged, the Picker renders no matching item while the stored UID persists, and neither the ContentView handler nor the SettingsView .onChange(of: inputDevices) recovers it. The actual recording path falls back correctly, but the UI state is wrong and will mislead users.

Sources/Fluid/ContentView.swift โ€” hardware-change handler for .manual mode needs an availability check with a system-default fallback.

Fix All in Codex

Prompt To Fix All With AI
Fix the following 3 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 3
Sources/Fluid/ContentView.swift:365-368
**Preferred-device unavailability not handled after hardware change**

In `.manual` mode the hardware-change handler now sets `selectedInputUID` to the stored `preferredInputDeviceUID` unconditionally, even when the device was just disconnected. Because `refreshDevices()` runs first, `inputDevices` is already up-to-date, so the picker immediately shows a UID that is not in the list โ€” the selection renders blank/missing. The old implementation guarded against this with an availability check and fell back to the system default, which needs to be restored here. Note: `MicrophonePreferenceCoordinator.inputDeviceForCapture()` does fall back correctly for the actual recording path, but the UI state is left stale.

```suggestion
                case .manual:
                    if let prefIn = SettingsStore.shared.preferredInputDeviceUID,
                       self.inputDevices.contains(where: { $0.uid == prefIn })
                    {
                        self.selectedInputUID = prefIn
                    } else if let sysIn = AudioDevice.getDefaultInputDevice()?.uid {
                        self.selectedInputUID = sysIn
                    }
```

### Issue 2 of 3
Sources/Fluid/Services/AppServices.swift:71-79
**Manual lazy-init pattern is not thread-safe; prefer `lazy var`**

The same manual double-check pattern is used for `_asr`, so it's consistent with the existing style, but `MicrophonePreferenceCoordinator` is `@MainActor`-isolated and is accessed from multiple call sites. Swift's `lazy var` is automatically initialized on first access and is thread-safe for value-type initialization, making intent clearer and removing the possibility of a race if the surrounding class ever stops being exclusively main-actor-accessed.

```suggestion
    lazy var microphonePreferenceCoordinator: MicrophonePreferenceCoordinator = MicrophonePreferenceCoordinator()
```

### Issue 3 of 3
Sources/Fluid/Services/MenuBarManager.swift:621-647
**Duplicated mode-toggle logic diverges from SettingsView**

`MenuBarManager.toggleMicrophoneSelectionMode` and `SettingsView.updateMicrophoneSelectionMode` implement the same "switch mode and seed the manual preference" flow independently. The key difference: the menu-bar path checks `SettingsStore.shared.preferredInputDeviceUID` to decide whether to seed a default, while the settings-view path checks the local UI binding `selectedInputUID`. This means flipping the toggle from the menu bar when `preferredInputDeviceUID` already holds a value will skip seeding (correct), but if the state somehow diverges the two call-sites will behave differently. Extracting the shared transition logic into `SettingsStore.setMicrophoneSelectionMode` (or a dedicated coordinator method) would remove this maintenance risk.


Reviews (2): Last reviewed commit: "centralize microhpone input irrespective..." | Re-trigger Greptile

greptile-apps avatar Jul 10 '26 04:07 greptile-apps

@greptileai review latest changes

grohith327 avatar Jul 16 '26 02:07 grohith327