hex/ios/HEXShared/MicrophoneChoice.swift
Ryan Vogel d23acdf8ca
iOS: dictation picks the car's mic in CarPlay, and a warm session never holds call audio (#2179)
Settings › Dictation › Microphone (Automatic, iPhone, Bluetooth) replaces the
Always use iPhone microphone switch. In a car the old default recorded from
the iPhone, often in a cupholder hearing the speakers and the road, while the
car's microphone sat unused; Hex's own recording mixes, so the car went on
playing over it. Automatic (the default) now records from a wired or USB
headset's microphone, from the car's in CarPlay (the driver's microphone Siri
uses), else from the iPhone's, so AirPods and other Bluetooth headphones keep
playing at full quality and nothing waits for call audio. iPhone is the old
default; Bluetooth also takes Bluetooth hands-free microphones (AirPods,
Bluetooth car kits, which iOS can't tell from headphones). The stored switch
migrates: left on reads as Automatic, turned off as Bluetooth.

Changed behaviour:
- The input is always set explicitly (MicrophoneRoute). Before, Bluetooth
  mode set no preferred input, so an iPhone preference set earlier in the same
  session stuck, and Automatic-style CarPlay/wired choices didn't exist.
- The warm keepalive stays on the iPhone's microphone without HFP whatever the
  choice. Before, with the switch off, it took the headset's or car's
  hands-free microphone for the whole warm session (up to an hour of call
  audio). A dictation moves to its input as it starts; a new route device
  re-pins the keepalive in every mode, not only with Bluetooth allowed.
- In-keyboard dictation asks WebKit for the same input by its label
  (getUserMedia deviceId), read from the keyboard's own session. Before, with
  the switch off it asked for nothing, and WebKit takes the input of the route
  already playing, the iPhone's while a car or headphones play over A2DP, so
  the switch had no effect there. The probe log now lists the inputs offered
  and the one chosen.

Kept: mixWithOthers, no preferred sample rate, RetiredRecorders, keepalive
backoff, and the keyboard's resume request after WebKit's capture.

Verified: xcodebuild test on an iPhone 18 Pro simulator (iOS 27.2) on the Mac
Studio, 519 tests, 0 failures (1 opt-in skip), including new cases for each
choice on each route (CarPlay, AirPods, Bluetooth car kit, wired headset,
nothing), the keepalive's input, HFP only for Bluetooth, the migration and the
controller passing its choice to the capture. Settings row rendered in English
and Simplified Chinese. The simulator has no Bluetooth or CarPlay: the route
behaviour itself is unverified on an iPhone.
2026-11-21 07:21:46 -05:00

88 lines
3.9 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import AVFAudio
/// Settings › Dictation › Microphone: what dictation records from while a car, headset or
/// headphones are connected. The app's recorder and the keyboard's capture page both follow it.
nonisolated enum MicrophoneChoice: String, CaseIterable, Sendable {
/// A wired or USB headset's microphone, the car's in CarPlay, otherwise the iPhone's:
/// Bluetooth headphones keep playing at full quality, and nothing waits for call audio.
case automatic
/// Always the iPhone's own microphone.
case iPhone
/// Bluetooth hands-free microphones too: AirPods, headsets and Bluetooth car kits, which
/// switch to call audio (8–16 kHz, other audio in call quality) while dictating.
case bluetooth
var label: String {
switch self {
case .automatic: String(localized: "Automatic")
case .iPhone: String(localized: "iPhone")
case .bluetooth: String(localized: "Bluetooth")
}
}
var detail: String {
switch self {
case .automatic: String(localized: "Car mic in CarPlay, else iPhone mic")
case .iPhone: String(localized: "Keeps music playing and starts faster")
case .bluetooth: String(localized: "AirPods and car mics, in call quality")
}
}
/// Whether a capture may record from a Bluetooth hands-free microphone, which needs
/// `.allowBluetoothHFP` in the session's category.
var allowsBluetooth: Bool { self == .bluetooth }
}
/// An input iOS offers (`AVAudioSessionPortDescription`), as a value tests can make.
nonisolated struct Microphone: Equatable, Sendable {
let uid: String
let name: String
let port: AVAudioSession.Port
init(uid: String, name: String, port: AVAudioSession.Port) {
self.uid = uid
self.name = name
self.port = port
}
init(_ description: AVAudioSessionPortDescription) {
self.init(uid: description.uid, name: description.portName, port: description.portType)
}
}
/// Which of the inputs iOS offers a dictation and the warm session record from.
nonisolated enum MicrophoneRoute {
/// The input a dictation records from; nil when none of the choice's kinds is offered.
///
/// Automatic takes a wired or USB headset's microphone (close to the mouth, no call audio)
/// and, in CarPlay, the car's: the one the car has for its driver, which Siri and calls
/// use, where the iPhone's would hear the car's speakers and the road. A Bluetooth car kit
/// can't be told from headphones, so it takes `bluetooth`.
static func capture(_ choice: MicrophoneChoice, from inputs: [Microphone]) -> Microphone? {
let order: [AVAudioSession.Port] = switch choice {
case .iPhone: [.builtInMic]
case .automatic: [.headsetMic, .usbAudio, .carAudio, .builtInMic]
case .bluetooth: [.headsetMic, .usbAudio, .carAudio, .bluetoothHFP, .bluetoothLE, .builtInMic]
}
for port in order {
if let input = inputs.first(where: { $0.port == port }) { return input }
}
return nil
}
/// The warm session's keepalive, which holds the microphone between dictations: always the
/// iPhone's, so a warm session never keeps a car or headset in call audio for its minutes.
/// Each dictation moves to its own input as it starts.
static func keepalive(from inputs: [Microphone]) -> Microphone? {
inputs.first { $0.port == .builtInMic }
}
/// The category options for recording: `.mixWithOthers` keeps other apps' video and music
/// playing, and A2DP keeps headphones in stereo while another microphone records.
/// Bluetooth hands-free inputs are offered only when allowed.
static func categoryOptions(allowingBluetooth: Bool) -> AVAudioSession.CategoryOptions {
allowingBluetooth
? [.mixWithOthers, .allowBluetoothHFP, .allowBluetoothA2DP, .defaultToSpeaker]
: [.mixWithOthers, .allowBluetoothA2DP, .defaultToSpeaker]
}
}