mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
* dictation: keep the stream inside the clip, and only call a lost opening one Two regressions in 3.4.0 made most hotkey dictations give up their live stream and re-transcribe the whole clip, so paste latency grew with the clip's length. The release tail (3.4.0) waits for the stop tone plus speech after it before it decides the tail, keeping capture running up to release + 428 + 160 ms with the default Classic sound. Every chunk ingested meanwhile was fed to the live stream, while `Recording::finish` trims the clip to release + its tail: the stream heard 388 ms of audio the clip dropped, past the engine's 250 ms overfeed tolerance, so it refused the stream. A finish now caps live feeding at the release plus `POST_RELEASE_TAIL` (`DictationCapture::limit_stream_to`), the base tail that is always kept; a grown tail, and audio captured while the tail decides, arrive through `finish_stream`'s remainder of the finished clip. macOS, Linux, and Windows all cap at the release they finish on. `MAX_OVERFED_SAMPLES` is unchanged. The opening-coverage check (3.4.0) summed every speech frame anywhere before the first token, so Hex's own start tone and the double tap's click at the clip's head added up past 300 ms and forced a whole-clip run on a stream that had lost nothing. `PauseCap` now counts only the contiguous run of speech that runs into the text's start (syllable gaps of up to 40 ms bridged, ending within the 400 ms slack the first token may be emitted late by) and names the run's span in the reason. A sound that sits on its own, seconds earlier, is never a lost opening. Checks: `a_long_release_tail_never_feeds_the_stream_past_the_clip` (fails by 4_480 samples without the cap), `an_isolated_burst_before_a_late_first_token_does_not_discard_the_stream`, `a_split_burst_that_reaches_the_threshold_is_not_a_lost_opening`, and `speech_running_into_the_texts_start_is_still_a_lost_opening`. * dictation: ignore the capture's own sounds, not a whole run, before the text The opening-coverage check counted every speech frame anywhere before the first token, so Hex's own audio at the clip's head (the pre-roll through the press, which can hold a retracted first tone and an earlier dictation's stop tone, then the start tone) added up past 300 ms and forced a whole-clip run on a stream that had lost nothing. Counting only one contiguous run was worse: a lost opening followed by a pause longer than the check's 400 ms slack (2 s of speech, a 1 s pause, text at 3.5 s) was accepted, which is the case the check exists for, and requiring 300 ms of one run is unproven on real speech. So the check sums every counted frame again, and skips the capture's own sounds instead: the clip's press offset plus the current set's start sound (`feedback::start_tone_length`, zero with sounds off) and the output and input latency it takes to reach the microphone. `DictationCapture::begin_stream` passes that count with the stream (`Command::Begin`), so `PauseCap` still reads its own samples and the 400 ms slack is untouched. The reason keeps the span of the counted speech. Checks: `hexs_own_sounds_at_the_captures_head_are_not_a_lost_opening` (the same clip errs without the exclusion), `a_lost_opening_after_a_long_pause_still_falls_back_to_the_whole_clip`. `ReleaseTail` and `ReleaseWait` move to `src/dictation/release_tail.rs` to keep `src/dictation.rs` under its size cap, and the docs say plainly that a release handled late can still leave the stream past the clip's trimmed end. * build: gate ReleaseWait and the zero-head begin to the platforms that use them macOS has no ReleaseWait (Linux and Windows poll it), so the re-export and its imports are gated there. The Windows bench is the one non-test caller of a head-less stream, so it asks for no ignored head explicitly and Transcript- ionEngine::begin stays test-only. * docs: word the press window's margin and its blind spots accurately The 60 ms margin after the start tone covers the 40 ms chord grace before a provisional tone sounds plus a little latency, not the output and input latency alone. With sounds off only the pre-roll is excluded, and with release-while-idle the tone sounds once the capture is intentional or ready, so the window may not cover it. Say plainly that a short lost opening inside the window, pre-roll speech included, goes unseen, that the earlier version likely rejected streams rather than certainly doing so, and that only the part of the tail audio the clip keeps is fed from the finished clip.
94 lines
3.1 KiB
Rust
94 lines
3.1 KiB
Rust
//! Where a shortcut finish's tail stands, and the poll a capture loop that
|
|
//! reads microphone chunks does while it waits for the tail's audio.
|
|
|
|
use std::time::Duration;
|
|
#[cfg(not(target_os = "macos"))]
|
|
use std::time::Instant;
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
use crate::audio::CaptureInstant;
|
|
#[cfg(not(target_os = "macos"))]
|
|
use crate::dictation::DictationCapture;
|
|
use crate::dictation::{MAX_RELEASE_TAIL, POST_RELEASE_TAIL};
|
|
|
|
/// Where a shortcut finish's tail stands, from the audio recorded so far.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum ReleaseTail {
|
|
/// More audio is needed. `growing` once speech has carried past
|
|
/// [`POST_RELEASE_TAIL`], so the wait is bounded by
|
|
/// [`MAX_RELEASE_TAIL`] rather than the base tail.
|
|
Waiting { growing: bool },
|
|
/// Finish keeping this much audio after the release.
|
|
Through(Duration),
|
|
}
|
|
|
|
impl ReleaseTail {
|
|
/// The longest tail a wait in this state may still end with.
|
|
pub fn bound(self) -> Duration {
|
|
match self {
|
|
Self::Waiting { growing: true } => MAX_RELEASE_TAIL,
|
|
Self::Waiting { growing: false } => POST_RELEASE_TAIL,
|
|
Self::Through(tail) => tail,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A shortcut release waiting for its tail, for capture loops that poll
|
|
/// between microphone chunks (Linux and Windows; the macOS audio owner
|
|
/// drains its tail directly).
|
|
#[cfg(not(target_os = "macos"))]
|
|
pub struct ReleaseWait {
|
|
ended_at: CaptureInstant,
|
|
started: Instant,
|
|
tone: Duration,
|
|
waits: bool,
|
|
}
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
impl ReleaseWait {
|
|
/// How long past a tail itself the loop waits for its audio.
|
|
const GRACE: Duration = Duration::from_millis(100);
|
|
|
|
/// A release at `ended_at`. Only one that will transcribe waits; its
|
|
/// stop tone (sounding from now) is never taken for speech.
|
|
pub fn new(capture: &DictationCapture, ended_at: CaptureInstant) -> Self {
|
|
let waits = capture.would_transcribe(ended_at);
|
|
Self {
|
|
ended_at,
|
|
started: Instant::now(),
|
|
tone: if waits {
|
|
crate::feedback::stop_tone_length()
|
|
} else {
|
|
Duration::ZERO
|
|
},
|
|
waits,
|
|
}
|
|
}
|
|
|
|
pub fn ended_at(&self) -> CaptureInstant {
|
|
self.ended_at
|
|
}
|
|
|
|
pub fn waits(&self) -> bool {
|
|
self.waits
|
|
}
|
|
|
|
/// Finishes with the audio already captured, as after a lost microphone.
|
|
pub fn stop_waiting(&mut self) {
|
|
self.waits = false;
|
|
}
|
|
|
|
/// The tail to finish with once it is decided, or once the wait for it
|
|
/// is over (the base tail plus the grace, or the longest tail plus the
|
|
/// grace while speech grows it); `None` while it is still coming.
|
|
pub fn tail(&self, capture: &DictationCapture, now: Instant) -> Option<Duration> {
|
|
if !self.waits {
|
|
return Some(POST_RELEASE_TAIL);
|
|
}
|
|
match capture.release_tail(self.ended_at, self.tone) {
|
|
ReleaseTail::Through(tail) => Some(tail),
|
|
waiting if now >= self.started + waiting.bound() + Self::GRACE => Some(waiting.bound()),
|
|
ReleaseTail::Waiting { .. } => None,
|
|
}
|
|
}
|
|
}
|