hex/src/dictation/opening_head.rs
Dax 1bbfc8dfa1
dictation: don't count audio before output suppression as missed opening speech (#2338) (#2514)
With Lower audio, Mute, or Pause media, a hotkey capture keeps no pre-roll
and asks for output suppression only once it passes the 300 ms hold; Lower
audio then ramps over 120 ms on its own thread. Audio still coming from the
speakers in that window was counted as speech the stream missed, so nearly
every dictation with something playing fell back to the whole clip.

The recording environment now reports the moment suppression actually
changed the output, and only when it did: Lower audio sends when the ramp
first writes its full target, from the ramp's own thread, so a late Bluetooth
readback no longer delays it; Mute when the write lands over a non-zero
volume; Pause media the latest of the player pause command being issued and
the browser pause being sent, without the browser's confirmation windows. The
report is asynchronous, so stopping or another session is never held up
behind a ramp. Nothing is reported when the effect changed nothing, was
skipped on headphones under Labs › Only on speakers, or was stopped before
the target was written.

The audio owner maps the reported moment onto the recorded timeline and adds
a 40 ms speaker-to-microphone margin; up to one resampler input buffer may
still be unprocessed, so the position can sit slightly early, which only ever
ignores less speech. The live stream's head is raised with
Command::IgnoreHead, applied before it finalizes if it arrives before Finish;
one after the finish is ignored.

Checks: a_reported_effect_raises_the_head_from_the_recorded_timeline,
media_speech_before_output_suppression_took_effect_is_not_a_lost_opening,
speech_after_the_raised_head_is_still_a_lost_opening,
a_head_raised_before_the_finish_keeps_the_stream,
a_head_raised_after_the_finish_cannot_save_the_stream,
a_guard_reports_when_the_target_is_written_not_when_the_readback_settles,
a_guard_that_changes_nothing_reports_no_moment, and
muting_an_already_silent_output_changes_nothing. Linux and Windows keep their
current head (ROADMAP). Not measured on a microphone or a device.
2026-11-23 07:09:11 -05:00

85 lines
3.3 KiB
Rust

//! The audio at a capture's head a live stream must not take for speech it
//! missed: Hex's own sounds, and the window before "While dictating" output
//! suppression reaches the microphone (#2338).
use std::time::Duration;
use super::{PARAKEET_SAMPLE_RATE, STOP_TONE_MARGIN, samples_for};
use crate::audio::CaptureInstant;
/// How far a report may run past the newest audio the capture has. The
/// audio owner has usually ingested it already; beyond this the microphone
/// is stalled and the clip will end long before it.
const MAX_AHEAD: Duration = Duration::from_millis(500);
/// Clip samples at `at` on the recorded timeline: ahead of the newest audio
/// it projects forward (bounded); into audio already recorded it maps back,
/// so a report handled after more audio arrived still lands where it
/// happened.
pub(super) fn clip_position(
recorded: usize,
recorded_through: Option<CaptureInstant>,
at: CaptureInstant,
) -> usize {
let Some(through) = recorded_through else {
return recorded;
};
match at.checked_duration_since(through) {
Some(ahead) => recorded + samples_for(ahead.min(MAX_AHEAD), PARAKEET_SAMPLE_RATE),
None => recorded.saturating_sub(samples_for(
through.duration_since(at),
PARAKEET_SAMPLE_RATE,
)),
}
}
/// How long after an effect takes effect the speakers may still reach the
/// microphone: the output-and-input path, once the ramp or write is done.
const SPEAKER_TO_MIC_MARGIN: Duration = Duration::from_millis(40);
/// Clip samples to ignore for suppression that took effect at `at`.
pub(super) fn effective_through(
recorded: usize,
recorded_through: Option<CaptureInstant>,
at: CaptureInstant,
) -> usize {
clip_position(recorded, recorded_through, at)
+ samples_for(SPEAKER_TO_MIC_MARGIN, PARAKEET_SAMPLE_RATE)
}
/// Clip samples of Hex's own audio at the capture's head: the pre-roll (a
/// retracted double-tap first tone, an earlier dictation's stop tone) through
/// the press, then the start tone and the margin after it (the grace before a
/// provisional tone sounds, plus a little latency), and everything through
/// the moment output suppression took effect. A stream that hears it has not
/// lost any speech.
pub(super) fn ignored_head(pre_press: Duration, suppressed_through: usize) -> usize {
let tone = crate::feedback::start_tone_length();
let after_press = if tone.is_zero() {
Duration::ZERO
} else {
tone + STOP_TONE_MARGIN
};
let own_sounds = samples_for(pre_press, PARAKEET_SAMPLE_RATE)
+ samples_for(after_press, PARAKEET_SAMPLE_RATE);
own_sounds.max(suppressed_through)
}
#[cfg(test)]
mod tests {
use super::*;
const THROUGH: CaptureInstant = CaptureInstant::from_nanos(60_000_000_000);
#[test]
fn a_late_report_maps_back_into_the_recorded_audio() {
let at = THROUGH.checked_sub(Duration::from_millis(200)).unwrap();
assert_eq!(clip_position(10_400, Some(THROUGH), at), 7_200);
let ahead = THROUGH + Duration::from_millis(100);
assert_eq!(clip_position(10_400, Some(THROUGH), ahead), 12_000);
// The speaker-to-microphone margin follows the effect's moment.
assert_eq!(
effective_through(10_400, Some(THROUGH), ahead),
12_000 + samples_for(SPEAKER_TO_MIC_MARGIN, PARAKEET_SAMPLE_RATE)
);
}
}