hex/src/linux/dictation/silence.rs
Dax 689e5b35e9
labs: keep the microphone open after dictating (#1701)
* labs: keep the microphone open after dictating

With Release microphone while idle on, a capture that ends now leaves the
microphone open for a Labs window (`labs.keep_microphone_open` +
`labs.keep_microphone_open_seconds`: 30 s, 1, 3, 5, or 30 minutes, default
1 minute), so the next dictation takes the warm path with its pre-roll
instead of the cold open's asynchronous start. Off, every listener closes
exactly as before, and with the release setting off the lab does nothing.

One `audio::linger::MicrophoneLinger` decides it for macOS, Windows, and
Linux: the first idle pass after a capture arms the window, a capture clears
it, and only a later idle pass past the window closes, once nothing records,
no client dictates, no release tail runs, and no capture copies the
microphone. Windows and Linux move their repeated `release_while_idle &&
!wants_audio()` closes into that one check; macOS reconciles through it. The
switch takes the Labs state word's last free bit; the window has no room
there, so it lives in its own atomic, both written by `labs::apply`.

Also moves Linux's Live typing glue into `linux/dictation/typing.rs`, since
`linux/dictation.rs` was at the source cap, and regenerates the Labs goldens
for the new card.

* labs: release the lingering microphone where the old sites did

Review fixes for keep-microphone-open:

* macOS reconcile_input keeps origin/dev's branches: with a capture sink
  open and an inactive input, recovery is still requested, and the lab's
  window is cleared while a sink holds the microphone.
* A digital-silence finish closes a lingering microphone instead of
  skipping recovery, so the next press opens a fresh stream. macOS does it
  in reopen_silent_stream; Linux's moved to linux/dictation/silence.rs
  (which also keeps linux/dictation.rs under the source cap). Windows has
  no digital-silence path.
* Windows and Linux run the release check at the old event sites (a
  submitted finish, a cancel, a discard before the open, a client's end)
  through one small macro, so a press handled in the same pass still sees
  a closed microphone when the lab is off. That check also releases a
  microphone an interrupted recording left open, which the old
  latch-based sites never did.
* Docs: drop the "exactly as before" claims, describe the extra close, the
  Annotate capture-sink and local-API windows, macOS ending the window on
  a stream interruption, and the Linux poll rather than an idle wait.
* clear() is real code now (the silence close); state()/apply() say why
  the switch bit and the window atomic are written separately.

* labs: keep the lab's windows and spots exact

Follow-ups from review:

* Windows releases the microphone when a pending client dictation's start
  gives up at the first chunk, which the old site closed too.
* macOS leaves a lingering microphone alone when a digital-silence finish
  lands while a capture sink copies it, so Annotate gets no gap.
* The Linux silence test's released case opens the input first, so it really
  covers "lab off, release while idle: no reopen, stream stays open".
* Docs say where a digital-silence finish ends the window (macOS and Linux,
  not with a capture sink on macOS) and that Windows can linger on a
  zero-only stream.
2026-11-11 18:12:08 -05:00

105 lines
3.8 KiB
Rust
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.

//! A digital-silence finish on the Linux listener: the microphone runs but
//! delivered only zeros, and must be reopened (or, while lingering, closed so
//! the next press opens a fresh stream). Split from [`super`] to keep it under
//! the source cap.
use std::time::Duration;
use crate::audio::RecoveringAudioInput;
use crate::audio::linger::MicrophoneLinger;
/// A warm microphone whose finished dictation was exactly zero: the stream
/// runs but its device was handed to another app, and stays silent until
/// reopened (#1122). Reopen it as after a stream failure. A microphone
/// released while idle already opens fresh for the next press, unless Labs ›
/// Keep mic open after dictating keeps it lingering (`window`): then it closes
/// now, so the next press opens a fresh stream instead of the dead one.
/// Returns whether the stream went away, recovering or closed; the caller
/// must treat it as unavailable until it reopens or opens again.
pub(super) fn reopen_silent_stream(
input: &mut RecoveringAudioInput,
release_while_idle: bool,
linger: &mut MicrophoneLinger,
window: Option<Duration>,
) -> bool {
if !input.is_open() || input.is_recovering() {
return false;
}
if release_while_idle {
if window.is_none() {
return false;
}
tracing::warn!(
device = %input.device_name(),
"dictation audio was digital silence; closing the lingering microphone"
);
linger.clear();
input.close();
return true;
}
tracing::warn!(
device = %input.device_name(),
"dictation audio was digital silence; reopening the microphone"
);
input.request_recovery();
true
}
#[cfg(test)]
mod tests {
use super::*;
use crate::audio::{RecoveringAudioInput, RecoveringAudioInputEvent};
/// An input whose replacement open already answered, so it is open and
/// receiving.
fn open_input() -> RecoveringAudioInput {
let (mut input, opened) = RecoveringAudioInput::pending_for_test();
let (replacement, _samples) = crate::audio::AudioInput::channel_for_test();
opened
.send((0, Ok(crate::audio::Opened::Input(Box::new(replacement)))))
.unwrap();
assert!(matches!(
input.recv_timeout(Duration::ZERO, true),
RecoveringAudioInputEvent::Reopened
));
input
}
#[test]
fn a_warm_silent_stream_reopens_once_unless_released() {
let mut input = open_input();
let mut linger = MicrophoneLinger::default();
assert!(reopen_silent_stream(&mut input, false, &mut linger, None));
assert!(input.is_recovering());
// A second finish cannot queue another replacement.
assert!(!reopen_silent_stream(&mut input, false, &mut linger, None));
// Release while idle with the lab off keeps the open stream: no
// reopen, and it stays open for the next press.
let mut released = open_input();
assert!(!reopen_silent_stream(
&mut released,
true,
&mut linger,
None
));
assert!(released.is_open(), "the stream stays open");
assert!(!released.is_recovering(), "and is not reopened");
}
#[test]
fn a_lingering_silent_stream_closes_instead_of_reopening() {
let mut input = open_input();
assert!(input.is_open());
let mut linger = MicrophoneLinger::default();
assert!(reopen_silent_stream(
&mut input,
true,
&mut linger,
Some(Duration::from_secs(60)),
));
assert!(!input.is_open(), "the lingering microphone closes");
assert!(!input.is_recovering(), "nothing is reopened");
assert!(!linger.armed(), "the window ends with the stream");
}
}