Skip to content

A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.

TypeScript

fix(voice): keep recording through AVAudioEngine's own reconfiguration

Alan Buscaglia049adf2d5de3
diff --git a/apps/desktop/src-tauri/src/voice/macos.rs b/apps/desktop/src-tauri/src/voice/macos.rs
index 6ae9521..9855179 100644
--- a/apps/desktop/src-tauri/src/voice/macos.rs
+++ b/apps/desktop/src-tauri/src/voice/macos.rs
@@ -123,9 +123,24 @@ impl Microphone for MacMicrophone {
return Err(VoiceError::Failed(format!("The microphone could not start: {}", describe(&error))));
}
- // A device change (headset unplugged, input switched) stops the engine.
- let interrupted = RcBlock::new(move |_note: NonNull<NSNotification>| {
- sink.failed(&VoiceError::Interrupted.to_string());
+ // A device change (headset unplugged, input switched) stops the engine; the engine also
+ // posts this for its own reconfiguration while still running, which is not an
+ // interruption (`configuration_change`, #10). The engine is read from the notification
+ // rather than captured: Apple warns against releasing it inside this handler. Only a
+ // real Mac exercises this path; a route change that stops the engine still ends the
+ // recording (no transparent restart).
+ let interrupted = RcBlock::new(move |note: NonNull<NSNotification>| {
+ // SAFETY: the notification is valid for the call; its object is the observed engine.
+ let note = unsafe { note.as_ref() };
+ let engine = note.object();
+ // SAFETY: reading a flag off the engine, on the thread that posted the notification.
+ let running = engine
+ .as_deref()
+ .and_then(|object| object.downcast_ref::<AVAudioEngine>())
+ .is_some_and(|engine| unsafe { engine.isRunning() });
+ if let Some(error) = super::configuration_change(running) {
+ sink.failed(&error.to_string());
+ }
});
let engine_object: &AnyObject = &engine;
// SAFETY: a framework constant name, observed for this engine only; the block posts
diff --git a/apps/desktop/src-tauri/src/voice/mod.rs b/apps/desktop/src-tauri/src/voice/mod.rs
index 44a05ed..db930f3 100644
--- a/apps/desktop/src-tauri/src/voice/mod.rs
+++ b/apps/desktop/src-tauri/src/voice/mod.rs
@@ -166,6 +166,19 @@ impl From<VoiceError> for String {
}
}
+/// What an `AVAudioEngineConfigurationChangeNotification` means for the recording (macOS),
+/// given whether the engine that posted it is still running.
+///
+/// AVAudioEngine stops itself and posts the notification when the input hardware's sample rate
+/// or channel count changes (a headset unplugged, the input switched), so a stopped engine has
+/// lost the capture. It also posts it for its own reconfiguration a few hundred milliseconds
+/// after starting, still running and with the tap delivering audio in an unchanged format
+/// (#10); treating that as an interruption ended every recording right after it began.
+#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
+pub(crate) fn configuration_change(engine_running: bool) -> Option<VoiceError> {
+ (!engine_running).then_some(VoiceError::Interrupted)
+}
+
/// The capture in the device's own format (on macOS, the input node's `AVAudioPCMBuffer`),
/// valid for one call. Opaque to the machine: only the recognition that asks for it downcasts
/// it, so it must come from the microphone that recognition was made for.
@@ -252,6 +265,16 @@ mod tests {
);
}
+ #[test]
+ fn a_configuration_change_with_the_engine_running_is_not_an_interruption() {
+ assert_eq!(configuration_change(true), None);
+ }
+
+ #[test]
+ fn a_configuration_change_that_stopped_the_engine_is_an_interruption() {
+ assert_eq!(configuration_change(false), Some(VoiceError::Interrupted));
+ }
+
#[test]
fn the_stop_reply_is_the_text() {
let reply = Transcript { text: "hello".into() };