use std::ops::Add; #[cfg(target_os = "macos")] use std::sync::OnceLock; use std::sync::mpsc::{self, Receiver, RecvTimeoutError, Sender, SyncSender, TryRecvError}; use std::time::{Duration, Instant}; use color_eyre::eyre::{Result, WrapErr, eyre}; use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; use cpal::{Device, SampleFormat, Stream, StreamConfig}; #[cfg(target_os = "macos")] const AUTOMATIC_INPUT_DEVICE_PREFERENCES: &[&str] = crate::config::INPUT_DEVICES; #[cfg(target_os = "linux")] const AUTOMATIC_INPUT_DEVICE_PREFERENCES: &[&str] = &[]; #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] pub struct CaptureInstant(u64); impl CaptureInstant { pub const ZERO: Self = Self(0); pub const fn from_nanos(nanos: u64) -> Self { Self(nanos) } pub const fn as_nanos(self) -> u64 { self.0 } #[cfg(target_os = "macos")] pub fn now() -> Self { unsafe extern "C" { fn mach_absolute_time() -> u64; } Self::from_mach_ticks(unsafe { mach_absolute_time() }) } #[cfg(target_os = "macos")] pub fn from_mach_ticks(ticks: u64) -> Self { let Some((numerator, denominator)) = mach_timebase() else { return Self::ZERO; }; Self(scale_mach_ticks(ticks, numerator, denominator)) } pub fn from_stream(instant: cpal::StreamInstant) -> Self { Self(u64::try_from(instant.as_nanos()).unwrap_or(u64::MAX)) } pub fn checked_add(self, duration: Duration) -> Option { let nanos = u64::try_from(duration.as_nanos()).ok()?; self.0.checked_add(nanos).map(Self) } pub fn checked_sub(self, duration: Duration) -> Option { let nanos = u64::try_from(duration.as_nanos()).ok()?; self.0.checked_sub(nanos).map(Self) } pub fn checked_duration_since(self, earlier: Self) -> Option { self.0.checked_sub(earlier.0).map(Duration::from_nanos) } pub fn saturating_duration_since(self, earlier: Self) -> Duration { Duration::from_nanos(self.0.saturating_sub(earlier.0)) } pub fn duration_since(self, earlier: Self) -> Duration { self.saturating_duration_since(earlier) } } #[cfg(target_os = "macos")] fn scale_mach_ticks(ticks: u64, numerator: u32, denominator: u32) -> u64 { let nanos = u128::from(ticks) * u128::from(numerator) / u128::from(denominator); u64::try_from(nanos).unwrap_or(u64::MAX) } #[cfg(target_os = "macos")] fn mach_timebase() -> Option<(u32, u32)> { #[repr(C)] struct MachTimebaseInfo { numer: u32, denom: u32, } unsafe extern "C" { fn mach_timebase_info(info: *mut MachTimebaseInfo) -> i32; } static TIMEBASE: OnceLock> = OnceLock::new(); *TIMEBASE.get_or_init(|| { let mut info = MachTimebaseInfo { numer: 0, denom: 0 }; let status = unsafe { mach_timebase_info(&mut info) }; (status == 0 && info.denom != 0).then_some((info.numer, info.denom)) }) } impl Add for CaptureInstant { type Output = Self; fn add(self, duration: Duration) -> Self::Output { self.checked_add(duration) .expect("capture instant overflowed") } } pub struct AudioInput { // Tests can deliver PCM through channels without opening a native microphone. _stream: Option, chunks: Receiver<(Vec, CaptureInstant)>, pub sample_rate: u32, pub device_name: String, stream_errors: Receiver, } pub enum AudioInputEvent { Chunk { samples: Vec, captured_through: CaptureInstant, }, Timeout, StreamFailed(String), } type InputOpenResult = (u64, Result); pub struct RecoveringAudioInput { input: Option, device_override: Option, selected_device: Option, active_revision: u64, recovery: MicrophoneRecovery, replacement: Option>, } pub enum RecoveringAudioInputEvent { Chunk { samples: Vec, captured_through: CaptureInstant, }, Timeout, Interrupted, Reopened, OpenFailed(String), } const MICROPHONE_RETRY_INITIAL: Duration = Duration::from_millis(250); const MICROPHONE_RETRY_MAX: Duration = Duration::from_secs(5); #[derive(Clone, Copy, Eq, PartialEq)] enum MicrophoneRecoveryReason { SelectionChanged, StreamFailed, } struct MicrophoneRecovery { next_attempt: Option, delay: Duration, reason: Option, target_revision: Option, } impl Default for MicrophoneRecovery { fn default() -> Self { Self { next_attempt: None, delay: MICROPHONE_RETRY_INITIAL, reason: None, target_revision: None, } } } impl MicrophoneRecovery { fn request_selection_change(&mut self, revision: u64, now: Instant) { if self.target_revision != Some(revision) { self.next_attempt = Some(now); self.delay = MICROPHONE_RETRY_INITIAL; self.target_revision = Some(revision); if self.reason != Some(MicrophoneRecoveryReason::StreamFailed) { self.reason = Some(MicrophoneRecoveryReason::SelectionChanged); } } } fn request_stream_recovery(&mut self, now: Instant) { if self.reason != Some(MicrophoneRecoveryReason::StreamFailed) { self.next_attempt = Some(now); self.delay = MICROPHONE_RETRY_INITIAL; self.reason = Some(MicrophoneRecoveryReason::StreamFailed); } } fn blocks_audio(&self) -> bool { self.reason == Some(MicrophoneRecoveryReason::StreamFailed) } fn should_attempt(&self, now: Instant) -> bool { self.next_attempt.is_some_and(|next| now >= next) } fn failed(&mut self, now: Instant) { self.next_attempt = Some(now + self.delay); self.delay = self.delay.saturating_mul(2).min(MICROPHONE_RETRY_MAX); } fn recovered(&mut self) { self.next_attempt = None; self.delay = MICROPHONE_RETRY_INITIAL; self.reason = None; self.target_revision = None; } } impl AudioInput { #[cfg(test)] pub fn channel_for_test() -> (Self, Sender<(Vec, CaptureInstant)>) { let (sender, chunks) = mpsc::channel(); let (_, stream_errors) = mpsc::channel(); ( Self { _stream: None, chunks, stream_errors, sample_rate: 48_000, device_name: "Test microphone".into(), }, sender, ) } pub fn open(device_queries: &[&str]) -> Result { let host = cpal::default_host(); let device = find_device(&host, device_queries)?; Self::open_device(device) } pub fn open_named(name: &str) -> Result { let host = cpal::default_host(); let device = host .input_devices() .wrap_err("could not enumerate input devices")? .find(|device| device.to_string() == name) .ok_or_else(|| eyre!("microphone is unavailable: {name}"))?; Self::open_device(device) } #[cfg(target_os = "linux")] pub fn open_matching(query: &str) -> Result { let host = cpal::default_host(); let device = host .input_devices() .wrap_err("could not enumerate input devices")? .find(|device| { device .to_string() .to_lowercase() .contains(&query.to_lowercase()) }) .ok_or_else(|| eyre!("microphone is unavailable: {query}"))?; Self::open_device(device) } fn open_device(device: Device) -> Result { let device_name = device.to_string(); let supported = device .default_input_config() .wrap_err("could not read the input device configuration")?; let sample_format = supported.sample_format(); let config: StreamConfig = supported.into(); let sample_rate = config.sample_rate; let channels = usize::from(config.channels); let (sender, chunks) = mpsc::channel(); let (error_sender, stream_errors) = mpsc::sync_channel(1); let stream = match sample_format { SampleFormat::F32 => { build_f32_stream(&device, &config, channels, sender, error_sender)? } SampleFormat::I16 => { build_i16_stream(&device, &config, channels, sender, error_sender)? } SampleFormat::U16 => { build_u16_stream(&device, &config, channels, sender, error_sender)? } format => return Err(eyre!("unsupported microphone sample format: {format:?}")), }; stream .play() .wrap_err("could not start microphone capture")?; Ok(Self { _stream: Some(stream), chunks, sample_rate, device_name, stream_errors, }) } pub fn recv_timeout(&self, timeout: Duration) -> AudioInputEvent { if let Ok(error) = self.stream_errors.try_recv() { return AudioInputEvent::StreamFailed(error); } match self.chunks.recv_timeout(timeout) { Ok((samples, captured_through)) => match self.stream_errors.try_recv() { Ok(error) => AudioInputEvent::StreamFailed(error), Err(_) => AudioInputEvent::Chunk { samples, captured_through, }, }, Err(RecvTimeoutError::Timeout) => match self.stream_errors.try_recv() { Ok(error) => AudioInputEvent::StreamFailed(error), Err(_) => AudioInputEvent::Timeout, }, Err(RecvTimeoutError::Disconnected) => AudioInputEvent::StreamFailed( self.stream_errors .try_recv() .unwrap_or_else(|_| "microphone audio channel disconnected".into()), ), } } } impl RecoveringAudioInput { pub fn open( device_override: Option<&str>, selection_revision: u64, selected_device: Option<&str>, ) -> Self { let mut input = Self::closed(device_override, selection_revision, selected_device); let opened = open_configured_input(device_override, selected_device, true); input.finish_initial_open(opened, Instant::now()); input } fn finish_initial_open(&mut self, opened: Result, now: Instant) { match opened { Ok(opened) => self.input = Some(opened), Err(error) => { tracing::warn!(%error, "could not open dictation microphone at startup; retrying"); self.recovery.request_stream_recovery(now); self.recovery.failed(now); } } } pub fn closed( device_override: Option<&str>, selection_revision: u64, selected_device: Option<&str>, ) -> Self { Self { input: None, device_override: device_override.map(str::to_owned), selected_device: selected_device.map(str::to_owned), active_revision: selection_revision, recovery: MicrophoneRecovery::default(), replacement: None, } } #[cfg(test)] pub fn pending_for_test() -> (Self, Sender) { let mut input = Self::closed( Some("HEX nonexistent microphone for pending open test"), 0, None, ); let (sender, receiver) = mpsc::channel(); input.replacement = Some(receiver); (input, sender) } pub fn request_open(&mut self) { if self.input.is_none() && self.replacement.is_none() && !self.is_recovering() { self.start_replacement(); } } pub fn request_recovery(&mut self) { self.recovery.request_stream_recovery(Instant::now()); } pub fn is_recovering(&self) -> bool { self.recovery.blocks_audio() } pub fn close(&mut self) { self.input = None; self.replacement = None; self.recovery.recovered(); } pub fn is_open(&self) -> bool { self.input.is_some() } pub fn is_opening(&self) -> bool { self.replacement.is_some() } pub fn cancel_open(&mut self) { // Cancelling a capture must not cancel the warm microphone's recovery. if self.is_recovering() { return; } self.replacement = None; if self.input.is_none() { self.recovery.recovered(); } } pub fn request_selection(&mut self, revision: u64, selected_device: Option<&str>) { if self.device_override.is_some() || revision == self.active_revision { return; } self.selected_device = selected_device.map(str::to_owned); if self.input.is_none() && !self.is_recovering() { let was_opening = self.replacement.take().is_some(); self.active_revision = revision; self.recovery.recovered(); if was_opening { self.start_replacement(); } return; } self.recovery .request_selection_change(revision, Instant::now()); } pub fn recv_timeout( &mut self, timeout: Duration, capture_idle: bool, ) -> RecoveringAudioInputEvent { let now = Instant::now(); if let Some((opened_revision, result)) = self.poll_replacement() { let target_revision = self .recovery .target_revision .unwrap_or(self.active_revision); if opened_revision != target_revision { self.recovery.next_attempt = Some(now); return RecoveringAudioInputEvent::Timeout; } match result { Ok(replacement) if capture_idle || self.input.is_none() => { self.input = Some(replacement); self.active_revision = opened_revision; self.recovery.recovered(); tracing::info!(device = %self.device_name(), "dictation microphone reopened"); return RecoveringAudioInputEvent::Reopened; } Ok(_) => { self.recovery.failed(now); } Err(error) => { if self.input.is_none() && !self.is_recovering() { self.recovery.recovered(); return RecoveringAudioInputEvent::OpenFailed(error); } self.recovery.failed(now); tracing::warn!( %error, "could not reopen dictation microphone; retrying" ); } } } if (capture_idle || self.input.is_none()) && self.recovery.should_attempt(now) && self.replacement.is_none() { self.start_replacement(); } if self.recovery.blocks_audio() { std::thread::sleep(timeout); return RecoveringAudioInputEvent::Timeout; } let Some(input) = self.input.as_ref() else { std::thread::sleep(timeout); return RecoveringAudioInputEvent::Timeout; }; match input.recv_timeout(timeout) { AudioInputEvent::Chunk { samples, captured_through, } => RecoveringAudioInputEvent::Chunk { samples, captured_through, }, AudioInputEvent::Timeout => RecoveringAudioInputEvent::Timeout, AudioInputEvent::StreamFailed(error) => { tracing::warn!(%error, device = %input.device_name, "microphone stream stopped; reopening it"); self.recovery.request_stream_recovery(Instant::now()); RecoveringAudioInputEvent::Interrupted } } } pub fn sample_rate(&self) -> u32 { self.input .as_ref() .expect("microphone sample rate requested while closed") .sample_rate } pub fn device_name(&self) -> &str { &self .input .as_ref() .expect("microphone name requested while closed") .device_name } fn start_replacement(&mut self) { let revision = self .recovery .target_revision .unwrap_or(self.active_revision); let device_override = self.device_override.clone(); let selected_device = self.selected_device.clone(); let cold_open = self.input.is_none(); let (sender, receiver) = mpsc::sync_channel(1); std::thread::spawn(move || { let result = open_configured_input( device_override.as_deref(), selected_device.as_deref(), cold_open, ) .map_err(|error| error.to_string()); let _ = sender.send((revision, result)); }); self.replacement = Some(receiver); } fn poll_replacement(&mut self) -> Option { let result = match self.replacement.as_ref()?.try_recv() { Ok(result) => result, Err(TryRecvError::Empty) => return None, Err(TryRecvError::Disconnected) => ( self.active_revision, Err("microphone replacement worker stopped".into()), ), }; self.replacement = None; Some(result) } } fn open_configured_input( device_override: Option<&str>, selected_device: Option<&str>, fallback_from_selected: bool, ) -> Result { if let Some(device) = device_override { return AudioInput::open_named(device); } if let Some(device) = selected_device { return AudioInput::open_named(device).or_else(|error| { if !fallback_from_selected { return Err(error); } tracing::warn!(%error, device, "selected microphone is unavailable; using automatic selection"); AudioInput::open(AUTOMATIC_INPUT_DEVICE_PREFERENCES) }); } AudioInput::open(AUTOMATIC_INPUT_DEVICE_PREFERENCES) } pub fn input_device_names() -> Result> { let host = cpal::default_host(); let mut names = host .input_devices() .wrap_err("could not enumerate input devices")? .map(|device| device.to_string()) .collect::>(); names.sort_by_key(|name| name.to_lowercase()); names.dedup(); Ok(names) } fn find_device(host: &cpal::Host, queries: &[&str]) -> Result { let devices: Vec<_> = host .input_devices() .wrap_err("could not enumerate input devices")? .collect(); for query in queries { if let Some(device) = devices.iter().find(|device| { device .to_string() .to_lowercase() .contains(&query.to_lowercase()) }) { return Ok(device.clone()); } } host.default_input_device().ok_or_else(|| { eyre!( "no preferred or default input device is available (preferred: {})", queries.join(", ") ) }) } fn mono(samples: &[T], channels: usize, convert: impl Fn(&T) -> f32) -> Vec { samples .chunks(channels) .map(|frame| frame.iter().map(&convert).sum::() / channels as f32) .collect() } fn send( sender: &Sender<(Vec, CaptureInstant)>, chunk: Vec, captured_through: CaptureInstant, ) { let _ = sender.send((chunk, captured_through)); } fn stream_error(sender: &SyncSender, error: cpal::Error) { report_stream_error(sender, error.to_string()); } fn report_stream_error(sender: &SyncSender, error: String) { tracing::error!(%error, "microphone stream failed"); let _ = sender.try_send(error); } fn build_f32_stream( device: &Device, config: &StreamConfig, channels: usize, sender: Sender<(Vec, CaptureInstant)>, error_sender: SyncSender, ) -> Result { let sample_rate = config.sample_rate; device .build_input_stream( *config, move |data: &[f32], info| { let captured_through = captured_through(info, data.len() / channels, sample_rate); send( &sender, mono(data, channels, |sample| *sample), captured_through, ) }, move |error| stream_error(&error_sender, error), None, ) .wrap_err("could not open f32 microphone stream") } fn build_i16_stream( device: &Device, config: &StreamConfig, channels: usize, sender: Sender<(Vec, CaptureInstant)>, error_sender: SyncSender, ) -> Result { let sample_rate = config.sample_rate; device .build_input_stream( *config, move |data: &[i16], info| { let captured_through = captured_through(info, data.len() / channels, sample_rate); send( &sender, mono(data, channels, |sample| *sample as f32 / i16::MAX as f32), captured_through, ) }, move |error| stream_error(&error_sender, error), None, ) .wrap_err("could not open i16 microphone stream") } fn build_u16_stream( device: &Device, config: &StreamConfig, channels: usize, sender: Sender<(Vec, CaptureInstant)>, error_sender: SyncSender, ) -> Result { let sample_rate = config.sample_rate; device .build_input_stream( *config, move |data: &[u16], info| { let captured_through = captured_through(info, data.len() / channels, sample_rate); send( &sender, mono(data, channels, |sample| *sample as f32 / 32768.0 - 1.0), captured_through, ) }, move |error| stream_error(&error_sender, error), None, ) .wrap_err("could not open u16 microphone stream") } fn captured_through( info: &cpal::InputCallbackInfo, frames: usize, sample_rate: u32, ) -> CaptureInstant { let captured_at = CaptureInstant::from_stream(info.timestamp().capture); let nanos = (frames as u128 * 1_000_000_000 / u128::from(sample_rate)) as u64; let duration = Duration::from_nanos(nanos); captured_at.checked_add(duration).unwrap_or(captured_at) } #[cfg(test)] mod tests { use super::*; #[cfg(target_os = "macos")] #[test] fn mach_event_ticks_are_converted_to_nanoseconds() { assert_eq!(scale_mach_ticks(72_000_000, 125, 3), 3_000_000_000); assert_eq!(scale_mach_ticks(3_000_000_000, 1, 1), 3_000_000_000); } #[test] fn stream_failures_are_visible_to_the_audio_owner() { let (sender, errors) = mpsc::sync_channel(1); report_stream_error(&sender, "Device sample rate changed".into()); assert_eq!(errors.try_recv().unwrap(), "Device sample rate changed"); } #[test] fn closed_input_applies_selection_without_opening() { let mut input = RecoveringAudioInput::closed(None, 3, Some("Old microphone")); input.request_selection(4, Some("Next microphone")); assert!(!input.is_open()); assert_eq!(input.active_revision, 4); assert_eq!(input.selected_device.as_deref(), Some("Next microphone")); assert!(input.replacement.is_none()); } #[test] fn failed_startup_retry_keeps_backoff_without_an_open_stream() { let mut input = RecoveringAudioInput::closed(None, 3, Some("Missing microphone")); let started = Instant::now(); input.finish_initial_open(Err(eyre!("device unavailable")), started); assert!(input.is_recovering()); assert!( !input .recovery .should_attempt(started + Duration::from_millis(249)) ); assert!( input .recovery .should_attempt(started + Duration::from_millis(250)) ); let (sender, receiver) = mpsc::channel(); input.replacement = Some(receiver); sender.send((3, Err("device unavailable".into()))).unwrap(); let before = Instant::now(); assert!(matches!( input.recv_timeout(Duration::ZERO, true), RecoveringAudioInputEvent::Timeout )); let after = Instant::now(); assert!(!input.is_open()); assert!(!input.is_opening()); assert!(input.is_recovering()); let next_attempt = input.recovery.next_attempt.unwrap(); assert!(next_attempt >= before + Duration::from_millis(500)); assert!(next_attempt <= after + Duration::from_millis(500)); input.cancel_open(); input.request_open(); assert!(input.is_recovering()); assert!(!input.is_opening()); assert_eq!(input.recovery.next_attempt, Some(next_attempt)); input.request_selection(4, Some("Next microphone")); assert!(input.is_recovering()); assert_eq!(input.recovery.target_revision, Some(4)); assert_eq!(input.active_revision, 3); assert_eq!(input.selected_device.as_deref(), Some("Next microphone")); assert!(input.recovery.should_attempt(Instant::now())); } #[test] fn startup_retry_recovers_metadata_and_audio_without_restarting() { let mut input = RecoveringAudioInput::closed(None, 3, Some("Test microphone")); input.finish_initial_open(Err(eyre!("device unavailable")), Instant::now()); let (sender, receiver) = mpsc::channel(); input.replacement = Some(receiver); let (opened, samples) = AudioInput::channel_for_test(); sender.send((3, Ok(opened))).unwrap(); assert!(matches!( input.recv_timeout(Duration::ZERO, true), RecoveringAudioInputEvent::Reopened )); assert!(!input.is_recovering()); assert!(!input.is_opening()); assert!(input.recovery.next_attempt.is_none()); assert_eq!(input.sample_rate(), 48_000); assert_eq!(input.device_name(), "Test microphone"); let at = CaptureInstant::from_nanos(60_000_000_000); samples.send((vec![0.25; 480], at)).unwrap(); assert!(matches!( input.recv_timeout(Duration::ZERO, true), RecoveringAudioInputEvent::Chunk { samples, captured_through } if samples == vec![0.25; 480] && captured_through == at )); } #[test] fn startup_recovery_opens_even_when_a_capture_is_pending() { let mut input = RecoveringAudioInput::closed( Some("HEX nonexistent microphone for pending recovery test"), 3, None, ); input.request_recovery(); input.request_selection(4, Some("Ignored selection under CLI override")); assert_eq!(input.active_revision, 3); assert!(input.selected_device.is_none()); assert!(matches!( input.recv_timeout(Duration::ZERO, false), RecoveringAudioInputEvent::Timeout )); assert!(input.is_opening()); input.cancel_open(); assert!(input.is_opening()); input.close(); assert!(!input.is_opening()); assert!(!input.is_recovering()); assert!(input.recovery.next_attempt.is_none()); } #[test] fn failed_on_demand_open_does_not_retry_while_idle() { let mut input = RecoveringAudioInput::closed(None, 3, None); let (sender, receiver) = mpsc::channel(); input.replacement = Some(receiver); sender.send((3, Err("device unavailable".into()))).unwrap(); assert!(matches!( input.recv_timeout(Duration::ZERO, true), RecoveringAudioInputEvent::OpenFailed(error) if error == "device unavailable" )); assert!(!input.is_recovering()); assert!(!input.is_opening()); assert!(input.recovery.next_attempt.is_none()); } #[test] fn microphone_recovery_retries_with_bounded_backoff_and_resets() { let started = Instant::now(); let mut recovery = MicrophoneRecovery::default(); recovery.request_selection_change(1, started); assert!(!recovery.blocks_audio()); assert!(recovery.should_attempt(started)); recovery.failed(started); recovery.request_selection_change(1, started + Duration::from_millis(100)); assert!(!recovery.should_attempt(started + Duration::from_millis(249))); assert!(recovery.should_attempt(started + Duration::from_millis(250))); let mut now = started + Duration::from_millis(250); for expected_delay in [500, 1_000, 2_000, 4_000, 5_000, 5_000] { recovery.failed(now); assert!(!recovery.should_attempt(now + Duration::from_millis(expected_delay - 1))); now += Duration::from_millis(expected_delay); assert!(recovery.should_attempt(now)); } recovery.recovered(); assert!(!recovery.blocks_audio()); assert!(!recovery.should_attempt(now)); recovery.request_stream_recovery(now); assert!(recovery.blocks_audio()); recovery.failed(now); recovery.request_stream_recovery(now + Duration::from_millis(100)); recovery.request_selection_change(2, now + Duration::from_millis(100)); assert!(recovery.blocks_audio()); assert!(recovery.should_attempt(now + Duration::from_millis(100))); } }