feat(ios): audio presets + advanced settings disclosure
Replaces the flat list of audio settings with a preset picker that shows context-appropriate options based on whether a Bluetooth device is connected. Presets: - Default (Phone Speaker): built-in mic + speaker, standard, mono - Bluetooth Headset (HFP): BT mic + BT output, standard, mono — only shown when a BT device is connected - BT Headphones + Phone Mic: A2DP stereo output + built-in mic, standard, mono — only shown when BT connected - BT Headphones + Stereo Mic: A2DP stereo output + built-in mic stereo (front+back capsules), standard, stereo — only shown when BT connected - Custom: shown when advanced settings don't match any preset When no Bluetooth device is connected, only 'Default' and 'Custom' appear, with a hint to connect Bluetooth headphones for more options. All granular controls (input port, orientation, polar pattern, mic mode, channels, bluetooth mode, output route, AirPlay) are now under an 'Advanced Audio' disclosure group, collapsed by default. IOSAudioRouter gains: - AudioPreset enum with bluetoothMode/captureChannels/micMode/usesBuiltInMic - hasBluetoothDevice detection (checks currentRoute + availableInputs for bluetoothA2DP/bluetoothHFP port types) - availablePresets (filtered by BT connection state) - activePreset (computed from current settings) - applyPreset() (sets all individual settings + finds built-in mic port UID)
This commit is contained in:
@@ -49,6 +49,57 @@ final class IOSAudioRouter: ObservableObject {
|
||||
@Published var selectedPolarPattern: String?
|
||||
@Published var showsRawModeSpeakerWarning: Bool = false
|
||||
@Published var showsA2dpNoAecWarning: Bool = false
|
||||
@Published var hasBluetoothDevice: Bool = false
|
||||
|
||||
enum AudioPreset: String, CaseIterable, Identifiable {
|
||||
/// Built-in mic + phone speaker. No Bluetooth. Standard processing, mono.
|
||||
case `default` = "Default (Phone Speaker)"
|
||||
/// Bluetooth HFP: BT mic + BT output. Standard processing, mono. Voice-quality.
|
||||
case bluetoothHeadset = "Bluetooth Headset (HFP)"
|
||||
/// A2DP stereo output + built-in mic. Standard processing, mono.
|
||||
case btHeadphonesMic = "BT Headphones + Phone Mic"
|
||||
/// A2DP stereo output + built-in mic stereo (front+back capsules). Standard, stereo.
|
||||
case btHeadphonesStereoMic = "BT Headphones + Stereo Mic"
|
||||
/// Settings don't match any preset — user has tweaked advanced controls.
|
||||
case custom = "Custom"
|
||||
|
||||
var id: String { rawValue }
|
||||
|
||||
var requiresBluetooth: Bool {
|
||||
switch self {
|
||||
case .default, .custom: return false
|
||||
default: return true
|
||||
}
|
||||
}
|
||||
|
||||
var bluetoothMode: BluetoothMode {
|
||||
switch self {
|
||||
case .default: return .builtInMicSpeaker
|
||||
case .bluetoothHeadset: return .btHfpVoice
|
||||
case .btHeadphonesMic, .btHeadphonesStereoMic: return .builtInMicBtA2dp
|
||||
case .custom: return .builtInMicSpeaker // placeholder
|
||||
}
|
||||
}
|
||||
|
||||
var captureChannels: CaptureChannels {
|
||||
switch self {
|
||||
case .btHeadphonesStereoMic: return .stereo
|
||||
default: return .mono
|
||||
}
|
||||
}
|
||||
|
||||
var micMode: MicMode {
|
||||
.standard // all presets use standard processing
|
||||
}
|
||||
|
||||
/// Whether this preset selects the built-in mic explicitly (vs. system default).
|
||||
var usesBuiltInMic: Bool {
|
||||
switch self {
|
||||
case .btHeadphonesMic, .btHeadphonesStereoMic: return true
|
||||
default: return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum BluetoothMode: String, CaseIterable, Identifiable {
|
||||
case btHfpVoice = "BT HFP Voice"
|
||||
@@ -78,6 +129,7 @@ final class IOSAudioRouter: ObservableObject {
|
||||
private let kInputPortId = "cat.voice.audio.inputPortId"
|
||||
private let kDataSourceId = "cat.voice.audio.dataSourceId"
|
||||
private let kPolarPattern = "cat.voice.audio.polarPattern"
|
||||
private let kPreset = "cat.voice.audio.preset"
|
||||
|
||||
/// Re-entrancy guard: setCategory/setPreferredInput/etc. trigger route-change
|
||||
/// notifications synchronously on the same thread. Without this guard,
|
||||
@@ -136,6 +188,45 @@ final class IOSAudioRouter: ObservableObject {
|
||||
}
|
||||
|
||||
updateWarnings()
|
||||
detectBluetooth()
|
||||
}
|
||||
|
||||
/// Detect whether a Bluetooth audio device is currently connected (A2DP or HFP).
|
||||
/// Drives which presets are shown — BT presets are hidden when no BT device is
|
||||
/// connected to avoid confusing the user with irrelevant options.
|
||||
private func detectBluetooth() {
|
||||
let session = AVAudioSession.sharedInstance()
|
||||
let route = session.currentRoute
|
||||
let hasBTOutput = route.outputs.contains {
|
||||
$0.portType == .bluetoothA2DP || $0.portType == .bluetoothHFP
|
||||
}
|
||||
let hasBTInput = route.inputs.contains { $0.portType == .bluetoothHFP }
|
||||
let hasBTAvailable = (session.availableInputs ?? []).contains {
|
||||
$0.portType == .bluetoothHFP || $0.portType == .bluetoothA2DP
|
||||
}
|
||||
let wasConnected = hasBluetoothDevice
|
||||
hasBluetoothDevice = hasBTOutput || hasBTInput || hasBTAvailable
|
||||
if hasBluetoothDevice != wasConnected {
|
||||
logger.info("bluetooth device \(self.hasBluetoothDevice ? "connected" : "disconnected")")
|
||||
}
|
||||
}
|
||||
|
||||
/// The presets available given the current Bluetooth connection state.
|
||||
/// Always includes .default and .custom; BT presets only when a BT device is connected.
|
||||
var availablePresets: [AudioPreset] {
|
||||
AudioPreset.allCases.filter { !$0.requiresBluetooth || hasBluetoothDevice }
|
||||
}
|
||||
|
||||
/// Which preset matches the current settings, or .custom if nothing matches.
|
||||
var activePreset: AudioPreset {
|
||||
for preset in AudioPreset.allCases where preset != .custom {
|
||||
if bluetoothMode == preset.bluetoothMode
|
||||
&& captureChannels == preset.captureChannels
|
||||
&& micMode == preset.micMode {
|
||||
return preset
|
||||
}
|
||||
}
|
||||
return .custom
|
||||
}
|
||||
|
||||
// MARK: - Apply configuration
|
||||
@@ -278,7 +369,6 @@ final class IOSAudioRouter: ObservableObject {
|
||||
selectedDataSourceId = UserDefaults.standard.string(forKey: kDataSourceId)
|
||||
selectedPolarPattern = UserDefaults.standard.string(forKey: kPolarPattern)
|
||||
}
|
||||
|
||||
/// Persist current selections to UserDefaults.
|
||||
func savePreferences() {
|
||||
UserDefaults.standard.set(bluetoothMode.rawValue, forKey: kBluetoothMode)
|
||||
@@ -335,6 +425,44 @@ final class IOSAudioRouter: ObservableObject {
|
||||
applyConfiguration()
|
||||
}
|
||||
|
||||
// MARK: - Presets
|
||||
|
||||
/// Apply a preset — sets all individual audio settings to the preset's values, then
|
||||
/// applies the configuration. For presets that use the built-in mic (A2DP presets),
|
||||
/// finds the built-in mic port UID from availableInputs.
|
||||
func applyPreset(_ preset: AudioPreset) {
|
||||
guard preset != .custom else { return } // can't "apply" custom — it's a display state
|
||||
|
||||
bluetoothMode = preset.bluetoothMode
|
||||
micMode = preset.micMode
|
||||
captureChannels = preset.captureChannels
|
||||
|
||||
if preset.usesBuiltInMic {
|
||||
// Find the built-in mic port from available inputs and select it.
|
||||
let session = AVAudioSession.sharedInstance()
|
||||
if let builtInMic = (session.availableInputs ?? []).first(where: {
|
||||
$0.portType == .builtInMic
|
||||
}) {
|
||||
selectedInputPortId = builtInMic.uid
|
||||
}
|
||||
// Don't set a specific data source — in stereo mode, iOS uses multiple mic
|
||||
// capsules automatically. In mono, the default orientation is fine.
|
||||
selectedDataSourceId = nil
|
||||
selectedPolarPattern = nil
|
||||
} else {
|
||||
// For Default and Bluetooth Headset presets, let the system pick the input.
|
||||
selectedInputPortId = nil
|
||||
selectedDataSourceId = nil
|
||||
selectedPolarPattern = nil
|
||||
}
|
||||
|
||||
UserDefaults.standard.set(preset.rawValue, forKey: kPreset)
|
||||
savePreferences()
|
||||
applyConfiguration()
|
||||
refreshRoutes()
|
||||
logger.info("applyPreset — \(preset.rawValue)")
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
/// Update warning indicators for the Settings UI.
|
||||
|
||||
@@ -6,12 +6,34 @@ struct SettingsView: View {
|
||||
@Environment(AppState.self) private var appState
|
||||
@Bindable var session: SessionState
|
||||
@StateObject private var router = IOSAudioRouter.shared
|
||||
@State private var showAdvanced = false
|
||||
|
||||
var body: some View {
|
||||
NavigationStack {
|
||||
Form {
|
||||
// MARK: - Audio Input
|
||||
Section("Audio Input") {
|
||||
// MARK: - Audio Preset
|
||||
Section("Audio") {
|
||||
Picker("Preset", selection: Binding(
|
||||
get: { router.activePreset },
|
||||
set: { preset in router.applyPreset(preset) }
|
||||
)) {
|
||||
ForEach(router.availablePresets) { preset in
|
||||
Text(preset.rawValue).tag(preset)
|
||||
}
|
||||
}
|
||||
.accessibilityLabel("Audio preset")
|
||||
|
||||
if !router.hasBluetoothDevice {
|
||||
Text("Connect Bluetooth headphones to see Bluetooth presets.")
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
.accessibilityLabel("No Bluetooth device connected")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Advanced Audio
|
||||
Section {
|
||||
DisclosureGroup("Advanced Audio", isExpanded: $showAdvanced) {
|
||||
// Input port picker (AVAudioSession.availableInputs)
|
||||
Picker("Input Port", selection: Binding(
|
||||
get: { router.selectedInputPortId ?? "" },
|
||||
@@ -43,7 +65,7 @@ struct SettingsView: View {
|
||||
}
|
||||
.accessibilityLabel("Microphone orientation")
|
||||
|
||||
// Polar pattern sub-picker (only if the data source supports patterns)
|
||||
// Polar pattern sub-picker
|
||||
if let selectedDs = dataSources.first(where: { $0.id == router.selectedDataSourceId }),
|
||||
let patterns = selectedDs.polarPatterns,
|
||||
!patterns.isEmpty {
|
||||
@@ -103,10 +125,7 @@ struct SettingsView: View {
|
||||
}
|
||||
}
|
||||
.accessibilityLabel("Capture channel count")
|
||||
}
|
||||
|
||||
// MARK: - Audio Output
|
||||
Section("Audio Output") {
|
||||
// Bluetooth mode
|
||||
Picker("Bluetooth Mode", selection: Binding(
|
||||
get: { router.bluetoothMode },
|
||||
@@ -143,6 +162,7 @@ struct SettingsView: View {
|
||||
}
|
||||
.accessibilityLabel("AirPlay output selector")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Voice
|
||||
Section("Voice") {
|
||||
|
||||
Reference in New Issue
Block a user