A free, open source, and extensible speech-to-text application that works completely offline.
long form chunking with vad (#2282)
CJ Pais9e57a0d7929f
diff --git a/src-tauri/src/actions.rs b/src-tauri/src/actions.rsindex b2db42d3..a159841e 100644--- a/src-tauri/src/actions.rs+++ b/src-tauri/src/actions.rs@@ -5,6 +5,7 @@ use crate::audio_toolkit::VadPolicy;use crate::managers::audio::{recording_error_type, AudioRecordingManager};use crate::managers::history::HistoryManager;use crate::managers::model::ModelManager;+use crate::managers::transcription::policy::{live_decode, LiveDecode, ModelTraits};use crate::managers::transcription::StreamWorkKind;use crate::managers::transcription::TranscriptionManager;use crate::settings::{get_settings, AppSettings, OverlayStyle, APPLE_INTELLIGENCE_PROVIDER_ID};@@ -445,7 +446,14 @@ impl ShortcutAction for TranscribeAction {} else {VadPolicy::Offline};- if model_supports_streaming {+ // Before the model loads, whether it keeps its accuracy on long audio+ // is unknown, so assume not: the worker decides again with the loaded+ // model, and a wrong guess costs only an idle worker.+ let expected_model = ModelTraits {+ streaming: model_supports_streaming,+ long_form: false,+ };+ if live_decode(expected_model, settings.long_form_chunking) != LiveDecode::None {tm.start_stream();}let plan_elapsed = plan_started.elapsed();@@ -637,20 +645,12 @@ impl ShortcutAction for TranscribeAction {crate::audio_toolkit::save_wav_file(&wav_path, &samples_for_wav)});- // Transcribe concurrently with WAV save. If a live stream was- // running, finalize it and use its text (all audio was already- // fed to the stream); otherwise batch-transcribe the samples.+ // Transcribe concurrently with WAV save: the live decode's+ // result when it has one (all audio was already fed to it),+ // otherwise a batch run of the samples. A cancel is surfaced+ // as an error, so it never starts a batch run.let transcription_time = Instant::now();- let transcription_result = match tm.finalize_stream() {- // A finalized stream with usable text wins. An empty result- // (no active stream, produced nothing, or the stream failed- // or its worker crashed) falls back to a full batch- // transcription of the same audio. A cancelled finalize is- // surfaced instead, so a cancel never starts a batch run.- Ok(Some(text)) if !text.trim().is_empty() => Ok(text),- Ok(_) => tm.transcribe(samples),- Err(err) => Err(err),- };+ let transcription_result = tm.transcribe_recording(samples);// Await WAV save and verifylet wav_saved = match wav_handle.await {diff --git a/src-tauri/src/audio_toolkit/chunking.rs b/src-tauri/src/audio_toolkit/chunking.rsnew file mode 100644index 00000000..1f7206d9--- /dev/null+++ b/src-tauri/src/audio_toolkit/chunking.rs@@ -0,0 +1,616 @@+//! Long-form chunking: long audio is decoded in chunks cut at pauses, because+//! most models' accuracy degrades past 30-40 s of audio in one run.+//!+//! Cuts come from per-frame voice activity probabilities. Every duration here+//! is in samples of 16 kHz audio, never in frames, so any detector's frame size+//! works with the same rules. The thresholds are Silero's.+//!+//! Ported from pi-voice's `chunking.ts`. The cut plan is the same; the Rust+//! version returns chunks for the caller to decode rather than decoding them.++use std::ops::Range;++use anyhow::Result;+use log::{debug, warn};+use once_cell::sync::Lazy;+use regex::Regex;++use super::constants;+use super::vad::SpeechScorer;++const RATE: usize = constants::WHISPER_SAMPLE_RATE as usize;++const TARGET_SECONDS: f64 = 30.0;+/// Audio up to this many samples is decoded in one piece; longer audio is cut.+const MAX_CHUNK_SAMPLES: usize = 40 * RATE;+const MIN_CHUNK_SAMPLES: usize = 5 * RATE;+/// Live, a cut is only final once this much audio past its window is scored:+/// then no cut it allows can leave a short rest, so live cuts match the plan+/// for the whole clip.+const LOOKAHEAD_SAMPLES: usize = MIN_CHUNK_SAMPLES;++// Silero's `get_speech_timestamps` defaults, as `VadSession::run` applies them.+const THRESHOLD: f32 = 0.5;+const NEG_THRESHOLD: f32 = THRESHOLD - 0.15;+const MIN_SPEECH_SAMPLES: usize = 250 * RATE / 1000;+const MIN_SILENCE_SAMPLES: usize = 100 * RATE / 1000;+const SPEECH_PAD_SAMPLES: usize = 30 * RATE / 1000;++/// Speech segments, in samples, from per-frame speech probabilities: Silero's+/// `get_speech_timestamps` without a maximum speech length. `samples` is the+/// length of the audio the probabilities score.+fn speech_segments(probs: &[f32], frame_samples: usize, samples: usize) -> Vec<Range<usize>> {+ let mut segments: Vec<Range<usize>> = Vec::new();+ let mut triggered = false;+ let mut start = 0;+ let mut temp_end: Option<usize> = None;+ for (i, &prob) in probs.iter().enumerate() {+ let at = i * frame_samples;+ if prob >= THRESHOLD {+ temp_end = None;+ if !triggered {+ triggered = true;+ start = at;+ }+ continue;+ }+ if prob < NEG_THRESHOLD && triggered {+ let end = *temp_end.get_or_insert(at);+ if at - end < MIN_SILENCE_SAMPLES {+ continue;+ }+ if end - start > MIN_SPEECH_SAMPLES {+ segments.push(start..end);+ }+ triggered = false;+ temp_end = None;+ }+ }+ if triggered && samples.saturating_sub(start) > MIN_SPEECH_SAMPLES {+ segments.push(start..samples);+ }++ for i in 0..segments.len() {+ if i == 0 {+ segments[0].start = segments[0].start.saturating_sub(SPEECH_PAD_SAMPLES);+ }+ if i + 1 == segments.len() {+ segments[i].end = (segments[i].end + SPEECH_PAD_SAMPLES).min(samples);+ continue;+ }+ let silence = segments[i + 1].start - segments[i].end;+ let pad = if silence < 2 * SPEECH_PAD_SAMPLES {+ silence / 2+ } else {+ SPEECH_PAD_SAMPLES+ };+ segments[i].end = (segments[i].end + pad).min(samples);+ segments[i + 1].start = segments[i + 1].start.saturating_sub(pad);+ }+ segments+}++/// A place a chunk may end: the middle of a pause.+#[derive(Debug, Clone, Copy, PartialEq)]+struct CutCandidate {+ cut: usize,+ gap_seconds: f64,+}++/// One candidate in the middle of each pause, lead-in and tail included.+fn cut_candidates(segments: &[Range<usize>], samples: usize) -> Vec<CutCandidate> {+ let mut candidates = Vec::new();+ let mut silence_start = 0;+ for segment in segments+ .iter()+ .cloned()+ .chain(std::iter::once(samples..samples))+ {+ if segment.start > silence_start {+ candidates.push(CutCandidate {+ cut: (silence_start + segment.start) / 2,+ gap_seconds: (segment.start - silence_start) as f64 / RATE as f64,+ });+ }+ silence_start = silence_start.max(segment.end);+ }+ candidates+}++/// Greedy, left to right from `from`: each cut is the best-scoring pause+/// 5-40 s after the previous one, preferring long pauses near 30 s and ones+/// that leave at least 5 s after them. With no pause, it is a hard cut at 40 s,+/// or halfway when that would leave under 5 s. Audio of 40 s or less is never+/// cut. `candidates` must be in order.+fn plan_cuts(candidates: &[CutCandidate], samples: usize, from: usize) -> Vec<usize> {+ let mut cuts = Vec::new();+ let mut start = from;+ // Each window starts after the last, so earlier candidates never return.+ let mut first = 0;+ while samples - start > MAX_CHUNK_SAMPLES {+ while first < candidates.len() && candidates[first].cut < start + MIN_CHUNK_SAMPLES {+ first += 1;+ }+ // A pause that leaves under 5 s is only used when no other pause will do.+ let mut best: Option<(usize, f64)> = None;+ let mut fallback: Option<(usize, f64)> = None;+ for candidate in candidates[first..]+ .iter()+ .take_while(|candidate| candidate.cut <= start + MAX_CHUNK_SAMPLES)+ {+ let position = (candidate.cut - start) as f64 / RATE as f64;+ let off_target = if position > TARGET_SECONDS {+ 0.1 * (position - TARGET_SECONDS)+ } else {+ 0.05 * (TARGET_SECONDS - position)+ };+ let score = candidate.gap_seconds.min(1.0) - off_target;+ if samples - candidate.cut >= MIN_CHUNK_SAMPLES+ && best.is_none_or(|(_, best)| score > best)+ {+ best = Some((candidate.cut, score));+ }+ if fallback.is_none_or(|(_, fallback)| score > fallback) {+ fallback = Some((candidate.cut, score));+ }+ }+ let hard_cut = if samples - start < MAX_CHUNK_SAMPLES + MIN_CHUNK_SAMPLES {+ start + (samples - start) / 2+ } else {+ start + MAX_CHUNK_SAMPLES+ };+ start = best.or(fallback).map_or(hard_cut, |(cut, _)| cut);+ cuts.push(start);+ }+ cuts+}++/// Makes a fresh scorer for one clip.+pub type MakeScorer = Box<dyn Fn() -> Result<Box<dyn SpeechScorer>> + Send>;++/// Cuts audio into chunks at pauses as it arrives, and hands each back once+/// its cut is certain, so it can be decoded while the user is still+/// speaking. Pushed all at once, a clip is cut exactly as it is live: the+/// same scores and the same plan.+///+/// If the audio can't be scored (the VAD fails to load or to run), nothing+/// more is cut and the rest is one chunk: chunking only places cuts, so the+/// audio is still decoded, as it would be without chunking.+pub struct Chunker {+ make_scorer: MakeScorer,+ /// Made the first time audio has to be scored, so a clip short enough to+ /// be one chunk never loads a VAD.+ scorer: Option<Box<dyn SpeechScorer>>,+ /// The scorer's frame size, once it is made.+ frame_samples: usize,+ /// Set when scoring fails: no more cuts.+ uncut: bool,+ /// Audio from `start` on; everything before it is already in a chunk.+ pending: Vec<f32>,+ /// One speech probability per whole frame from the beginning. Audio is+ /// only scored once a cut has to be planned, so a clip short enough to+ /// be one chunk is never scored.+ probs: Vec<f32>,+ start: usize,+ len: usize,+}++impl Chunker {+ /// `make_scorer` must make a fresh scorer: scores carry on from what it+ /// saw before.+ pub fn new(make_scorer: MakeScorer) -> Self {+ Self {+ make_scorer,+ scorer: None,+ frame_samples: 0,+ uncut: false,+ pending: Vec::new(),+ probs: Vec::new(),+ start: 0,+ len: 0,+ }+ }++ /// Samples pushed so far, chunked or not. A caller checks this against+ /// the whole recording before trusting the chunks.+ pub fn samples(&self) -> usize {+ self.len+ }++ /// Adds audio. Returns the chunks whose cuts this made certain, in order.+ /// A whole clip pushed at once is moved in, not copied.+ pub fn push(&mut self, pcm: impl Into<Vec<f32>>) -> Vec<Vec<f32>> {+ let pcm = pcm.into();+ self.len += pcm.len();+ if self.pending.is_empty() {+ self.pending = pcm;+ } else {+ self.pending.extend_from_slice(&pcm);+ }+ if self.uncut || self.len - self.start <= MAX_CHUNK_SAMPLES + LOOKAHEAD_SAMPLES {+ return Vec::new();+ }+ // Plan over whole frames only; a partial one waits for the rest.+ let scored = match self.score(false) {+ Ok(scored) => scored,+ Err(e) => {+ self.stop_cutting(e);+ return Vec::new();+ }+ };+ let segments = speech_segments(&self.probs, self.frame_samples, scored);+ // A pause still open at the end moves its midpoint as it grows: wait+ // for speech.+ if segments.last().map(|segment| segment.end) != Some(scored) {+ return Vec::new();+ }+ // One plan, committed while each cut's whole window plus the lookahead+ // is scored.+ let cuts = plan_cuts(&cut_candidates(&segments, scored), scored, self.start);+ let mut chunks = Vec::new();+ for cut in cuts {+ if scored - self.start <= MAX_CHUNK_SAMPLES + LOOKAHEAD_SAMPLES {+ break;+ }+ chunks.push(self.take_until(cut));+ }+ chunks+ }++ /// Cuts what is left and returns the remaining chunks, in order. Nothing+ /// more is pushed after.+ pub fn finish(&mut self) -> Vec<Vec<f32>> {+ let mut chunks = Vec::new();+ if !self.uncut && self.len - self.start > MAX_CHUNK_SAMPLES {+ match self.score(true) {+ Ok(_) => {+ let segments = speech_segments(&self.probs, self.frame_samples, self.len);+ let cuts =+ plan_cuts(&cut_candidates(&segments, self.len), self.len, self.start);+ for cut in cuts {+ chunks.push(self.take_until(cut));+ }+ }+ Err(e) => self.stop_cutting(e),+ }+ }+ if self.len > self.start {+ chunks.push(self.take_until(self.len));+ }+ chunks+ }++ /// Scores every frame not yet scored, making the scorer first if this is+ /// the first time: whole frames only, or with `pad_last` the partial last+ /// frame too, zero padded. Returns how many samples are scored.+ fn score(&mut self, pad_last: bool) -> Result<usize> {+ if self.scorer.is_none() {+ // Nothing is cut before the first score, so a fresh scorer starts+ // at the first sample, as it must.+ debug_assert!(self.start == 0 && self.probs.is_empty());+ let scorer = (self.make_scorer)()?;+ self.frame_samples = scorer.frame_samples();+ assert!(self.frame_samples > 0, "VAD frame size must be non-zero");+ self.scorer = Some(scorer);+ }+ let scorer = self.scorer.as_mut().expect("the scorer was made above");+ let frame = self.frame_samples;+ let scored = self.probs.len() * frame;+ // Cuts only fall in scored audio, so its unscored rest is pending.+ let from = scored - self.start;+ let whole = (self.len - scored) / frame * frame;+ if whole > 0 {+ self.probs+ .extend(scorer.score(&self.pending[from..from + whole])?);+ }+ if pad_last && self.len > scored + whole {+ let mut tail = self.pending[from + whole..].to_vec();+ tail.resize(frame, 0.0);+ self.probs.extend(scorer.score(&tail)?);+ return Ok(self.len);+ }+ Ok(scored + whole)+ }++ /// Scoring failed: nothing more is cut, so the rest is one chunk.+ fn stop_cutting(&mut self, e: anyhow::Error) {+ warn!(+ "Voice activity detection failed at {:.1}s; decoding the rest in one piece: {:#}",+ seconds(self.len),+ e+ );+ self.uncut = true;+ }++ fn take_until(&mut self, cut: usize) -> Vec<f32> {+ debug!(+ "Long-form chunk: {:.1}-{:.1}s",+ seconds(self.start),+ seconds(cut)+ );+ let chunk = self.pending.drain(..cut - self.start).collect();+ self.start = cut;+ chunk+ }+}++/// Scripts written without spaces between words, and CJK punctuation.+static UNSPACED: Lazy<Regex> = Lazy::new(|| {+ Regex::new(+ r"[\p{Han}\p{Hiragana}\p{Katakana}\p{Bopomofo}\p{Thai}\p{Lao}\p{Khmer}\p{Myanmar}\x{3000}-\x{303f}\x{ff00}-\x{ffef}]",+ )+ .unwrap()+});++fn is_unspaced(c: char) -> bool {+ UNSPACED.is_match(c.encode_utf8(&mut [0; 4]))+}++/// Chunk texts in order: a space between two chunks unless either side is+/// unspaced script.+pub fn join_transcripts<S: AsRef<str>>(texts: &[S]) -> String {+ let mut joined = String::new();+ let mut last: Option<char> = None;+ for text in texts {+ let text = text.as_ref().trim();+ let Some(first) = text.chars().next() else {+ continue;+ };+ if last.is_some_and(|last| !is_unspaced(last) && !is_unspaced(first)) {+ joined.push(' ');+ }+ joined.push_str(text);+ last = text.chars().last();+ }+ joined+}++fn seconds(samples: usize) -> f64 {+ samples as f64 / RATE as f64+}++#[cfg(test)]+mod tests {+ use super::*;+ use crate::audio_toolkit::vad::{VadFrame, VoiceActivityDetector};+ use std::sync::atomic::{AtomicUsize, Ordering};+ use std::sync::Arc;++ const FRAME: usize = 512;++ fn s(seconds: f64) -> usize {+ (seconds * RATE as f64).round() as usize+ }++ /// Frame probabilities: speech everywhere except the given pauses, in seconds.+ fn probs_with_pauses(seconds: f64, pauses: &[(f64, f64)]) -> Vec<f32> {+ (0..s(seconds) / FRAME)+ .map(|i| {+ let at = (i * FRAME) as f64 / RATE as f64;+ if pauses.iter().any(|&(start, end)| at >= start && at < end) {+ 0.0+ } else {+ 1.0+ }+ })+ .collect()+ }++ /// Cut positions, in tenths of a second.+ fn plan(seconds: f64, pauses: &[(f64, f64)]) -> Vec<f64> {+ let samples = s(seconds);+ let segments = speech_segments(&probs_with_pauses(seconds, pauses), FRAME, samples);+ plan_cuts(&cut_candidates(&segments, samples), samples, 0)+ .into_iter()+ .map(|cut| (cut as f64 / RATE as f64 * 10.0).round() / 10.0)+ .collect()+ }++ fn lengths(chunks: &[Vec<f32>]) -> Vec<usize> {+ chunks.iter().map(Vec::len).collect()+ }++ /// Where chunks end, except the last, in tenths of a second.+ fn cuts(chunks: &[Vec<f32>]) -> Vec<f64> {+ let mut end = 0;+ chunks[..chunks.len().saturating_sub(1)]+ .iter()+ .map(|chunk| {+ end += chunk.len();+ (end as f64 / RATE as f64 * 10.0).round() / 10.0+ })+ .collect()+ }++ #[test]+ fn cuts_prefer_long_pauses_near_30s() {+ assert!(+ plan(40.0, &[(20.0, 21.0)]).is_empty(),+ "40 s or less is never cut"+ );+ // A 1 s pause at 28-29 s beats a short one right at 30 s; 41.5 s+ // remain, so cut again.+ assert_eq!(+ plan(70.0, &[(28.0, 29.0), (30.0, 30.2), (55.0, 56.0)]),+ vec![28.5, 55.5]+ );+ // Without any pause, cuts are hard 40 s ones.+ assert_eq!(plan(100.0, &[]), vec![40.0, 80.0]);+ // A pause under 5 s in is never used.+ assert_eq!(plan(60.0, &[(2.0, 3.0)]), vec![40.0]);+ }++ #[test]+ fn cuts_avoid_leaving_a_short_last_chunk() {+ // The pause at 38 s would leave 4 s, so the weaker one at 10 s wins.+ assert_eq!(plan(42.0, &[(9.8, 10.2), (37.5, 38.5)]), vec![10.0]);+ // Without a pause, a short rest is split evenly instead of cut at 40 s.+ assert_eq!(plan(84.0, &[]), vec![40.0, 62.0]);+ // A pause that leaves a short rest still beats cutting mid-speech.+ assert_eq!(plan(40.5, &[(38.8, 39.2)]), vec![39.0]);+ }++ #[test]+ fn joins_put_spaces_only_between_spaced_scripts() {+ assert_eq!(+ join_transcripts(&[" Hello there. ", "", "General Kenobi."]),+ "Hello there. General Kenobi."+ );+ assert_eq!(+ join_transcripts(&["今天天气很好。", "我们去公园吧。"]),+ "今天天气很好。我们去公园吧。"+ );+ assert_eq!(join_transcripts(&["I said", "你好"]), "I said你好");+ assert_eq!(+ join_transcripts(&["안녕하세요.", "반갑습니다."]),+ "안녕하세요. 반갑습니다."+ );+ // CJK Extension B, outside the Basic Multilingual Plane.+ assert_eq!(join_transcripts(&["你好𠀀", "我们"]), "你好𠀀我们");+ }++ /// Scores each frame with its first sample, so a test clip carries its+ /// own probabilities. Counts the frames it scores, and fails on the+ /// `fail_on`th call (from 0).+ struct ScriptedScorer {+ scored: Arc<AtomicUsize>,+ calls: usize,+ fail_on: Option<usize>,+ }++ impl VoiceActivityDetector for ScriptedScorer {+ fn push_frame<'a>(&'a mut self, _frame: &'a [f32]) -> Result<VadFrame<'a>> {+ unreachable!("chunking only scores")+ }++ fn frame_samples(&self) -> usize {+ FRAME+ }+ }++ impl SpeechScorer for ScriptedScorer {+ fn score(&mut self, frames: &[f32]) -> Result<Vec<f32>> {+ assert!(frames.len().is_multiple_of(FRAME), "whole frames only");+ self.calls += 1;+ if self.fail_on == Some(self.calls - 1) {+ anyhow::bail!("scripted VAD failure");+ }+ self.scored+ .fetch_add(frames.len() / FRAME, Ordering::Relaxed);+ Ok(frames.chunks(FRAME).map(|frame| frame[0]).collect())+ }+ }++ /// How many scorers a chunker made, and how many frames they scored.+ #[derive(Clone, Default)]+ struct Counts {+ made: Arc<AtomicUsize>,+ scored: Arc<AtomicUsize>,+ }++ fn scripted(fail_on: Option<usize>) -> (Chunker, Counts) {+ let counts = Counts::default();+ let make = {+ let counts = counts.clone();+ move || {+ counts.made.fetch_add(1, Ordering::Relaxed);+ let scorer = ScriptedScorer {+ scored: Arc::clone(&counts.scored),+ calls: 0,+ fail_on,+ };+ Ok(Box::new(scorer) as Box<dyn SpeechScorer>)+ }+ };+ (Chunker::new(Box::new(make)), counts)+ }++ fn chunker() -> (Chunker, Counts) {+ scripted(None)+ }++ /// Audio whose every frame holds its own probability.+ fn clip(probs: &[f32]) -> Vec<f32> {+ probs.iter().flat_map(|&p| [p; FRAME]).collect()+ }++ /// 100 s of audio, with pauses at 29-30 s and 58-59 s.+ fn long_clip() -> Vec<f32> {+ clip(&probs_with_pauses(100.0, &[(29.0, 30.0), (58.0, 59.0)]))+ }++ #[test]+ fn live_chunks_arrive_before_the_end_at_the_same_cuts() {+ // A partial last frame, scored padded.+ let mut pcm = long_clip();+ pcm.extend([1.0; 100]);+ let (mut once, _) = chunker();+ let mut offline = once.push(&pcm[..]);+ offline.extend(once.finish());+ // The pauses' midpoints, then the 41.5 s left split evenly.+ assert_eq!(cuts(&offline), vec![29.5, 58.5, 79.3]);++ // Pieces that split frames, as the recorder's do.+ let (mut chunker, counts) = chunker();+ let mut live = Vec::new();+ for piece in pcm.chunks(FRAME * 3 + 7) {+ live.extend(chunker.push(piece));+ }+ // A cut is made whenever more than 45 s is waiting: 2 of the 4 chunks+ // before the end.+ assert_eq!(live.len(), 2, "chunks are ready while audio still arrives");+ live.extend(chunker.finish());+ assert_eq!(lengths(&live), lengths(&offline));+ assert_eq!(live.concat(), pcm, "every sample once, in order");++ // One stateful scorer for the clip, scoring each frame once.+ assert_eq!(counts.made.load(Ordering::Relaxed), 1);+ assert_eq!(+ counts.scored.load(Ordering::Relaxed),+ pcm.len().div_ceil(FRAME)+ );+ }++ #[test]+ fn live_cuts_wait_for_a_pause_to_end() {+ // At 45 s the pause so far is 34-45 s, a cut at 39.5 s; the whole+ // pause's midpoint, 42 s, is out of reach, so the plan cuts at 40 s.+ let pcm = clip(&probs_with_pauses(90.0, &[(34.0, 50.0)]));+ let planned = vec![s(40.0), s(40.0), pcm.len() - s(80.0)];++ let (mut chunker, _) = chunker();+ let mut live = Vec::new();+ for piece in pcm.chunks(16 * FRAME) {+ live.extend(chunker.push(piece));+ }+ live.extend(chunker.finish());+ assert_eq!(lengths(&live), planned);+ }++ #[test]+ fn audio_that_is_one_chunk_never_loads_a_vad() {+ let probs = probs_with_pauses(40.0, &[(20.0, 21.0)]);+ let pcm = clip(&probs);+ let (mut chunker, counts) = chunker();+ for frame in pcm.chunks(FRAME) {+ assert!(chunker.push(frame).is_empty());+ }+ assert_eq!(lengths(&chunker.finish()), vec![pcm.len()]);+ assert_eq!(counts.made.load(Ordering::Relaxed), 0);+ }++ #[test]+ fn a_vad_failure_leaves_the_rest_in_one_piece() {+ // The VAD fails on its second score, after the first chunk is out.+ let pcm = long_clip();+ let (mut chunker, _) = scripted(Some(1));+ let mut chunks = Vec::new();+ for frame in pcm.chunks(FRAME) {+ chunks.extend(chunker.push(frame));+ }+ assert_eq!(chunks.len(), 1);+ chunks.extend(chunker.finish());+ assert_eq!(cuts(&chunks), vec![29.5]);+ assert_eq!(chunks.concat(), pcm, "no audio lost");+ }+}diff --git a/src-tauri/src/audio_toolkit/mod.rs b/src-tauri/src/audio_toolkit/mod.rsindex e35f0a71f..9b75c0fa 100644--- a/src-tauri/src/audio_toolkit/mod.rs+++ b/src-tauri/src/audio_toolkit/mod.rs@@ -1,4 +1,5 @@pub mod audio;+pub mod chunking;pub mod constants;pub mod lang_id;pub mod text;@@ -14,4 +15,4 @@ pub use text::{apply_custom_words, normalize_transcription_output, remove_filler_words, OutputLanguageEvidence,};pub use utils::get_cpal_host;-pub use vad::{EarshotVad, SileroVad, VoiceActivityDetector};+pub use vad::{EarshotVad, SileroVad, SpeechScorer, VoiceActivityDetector};diff --git a/src-tauri/src/audio_toolkit/vad/earshot.rs b/src-tauri/src/audio_toolkit/vad/earshot.rsindex bcffce91..b0930d74 100644--- a/src-tauri/src/audio_toolkit/vad/earshot.rs+++ b/src-tauri/src/audio_toolkit/vad/earshot.rs@@ -1,6 +1,6 @@use anyhow::Result;-use super::{VadFrame, VoiceActivityDetector};+use super::{check_whole_frames, SpeechScorer, VadFrame, VoiceActivityDetector};pub const EARSHOT_FRAME_SAMPLES: usize = 256;@@ -30,8 +30,9 @@ impl EarshotVad {}}-impl VoiceActivityDetector for EarshotVad {- fn push_frame<'a>(&'a mut self, frame: &'a [f32]) -> Result<VadFrame<'a>> {+impl EarshotVad {+ /// The speech score of one frame.+ fn score_frame(&mut self, frame: &[f32]) -> Result<f32> {if frame.len() != EARSHOT_FRAME_SAMPLES {anyhow::bail!("expected {EARSHOT_FRAME_SAMPLES} samples, got {}",@@ -46,16 +47,22 @@ impl VoiceActivityDetector for EarshotVad {// signal can ring slightly outside [-1, 1]. Earshot documents that// range as a precondition and asserts it in debug builds, so clamp only// the prediction input while preserving the original audio on output.- let score = if frame.iter().all(|sample| (-1.0..=1.0).contains(sample)) {- self.engine.predict_f32(frame)- } else {- for (clamped, sample) in self.clamped_frame.iter_mut().zip(frame) {- *clamped = sample.clamp(-1.0, 1.0);- }- self.engine.predict_f32(&self.clamped_frame)- };+ Ok(+ if frame.iter().all(|sample| (-1.0..=1.0).contains(sample)) {+ self.engine.predict_f32(frame)+ } else {+ for (clamped, sample) in self.clamped_frame.iter_mut().zip(frame) {+ *clamped = sample.clamp(-1.0, 1.0);+ }+ self.engine.predict_f32(&self.clamped_frame)+ },+ )+ }+}- if score >= self.threshold {+impl VoiceActivityDetector for EarshotVad {+ fn push_frame<'a>(&'a mut self, frame: &'a [f32]) -> Result<VadFrame<'a>> {+ if self.score_frame(frame)? >= self.threshold {Ok(VadFrame::Speech(frame))} else {Ok(VadFrame::Noise)@@ -71,6 +78,14 @@ impl VoiceActivityDetector for EarshotVad {}}+impl SpeechScorer for EarshotVad {+ fn score(&mut self, frames: &[f32]) -> Result<Vec<f32>> {+ check_whole_frames(frames, EARSHOT_FRAME_SAMPLES)?;+ let (frames, _) = frames.as_chunks::<EARSHOT_FRAME_SAMPLES>();+ frames.iter().map(|frame| self.score_frame(frame)).collect()+ }+}+#[cfg(test)]mod tests {use super::*;diff --git a/src-tauri/src/audio_toolkit/vad/mod.rs b/src-tauri/src/audio_toolkit/vad/mod.rsindex f1bdbd1b..f676896a 100644--- a/src-tauri/src/audio_toolkit/vad/mod.rs+++ b/src-tauri/src/audio_toolkit/vad/mod.rs@@ -55,6 +55,25 @@ pub trait VoiceActivityDetector: Send + Sync {fn reset(&mut self) {}}+/// A detector's own speech probabilities, before any threshold or smoothing:+/// what long-form chunking cuts at.+pub trait SpeechScorer: VoiceActivityDetector {+ /// One probability per frame of `frames`, which must be whole frames.+ /// Scoring carries on from the frames scored before, so a clip scored in+ /// pieces gets the same probabilities as scored in one go.+ fn score(&mut self, frames: &[f32]) -> Result<Vec<f32>>;+}++fn check_whole_frames(frames: &[f32], frame_samples: usize) -> Result<()> {+ if !frames.len().is_multiple_of(frame_samples) {+ anyhow::bail!(+ "expected whole {frame_samples}-sample frames, got {} samples",+ frames.len()+ );+ }+ Ok(())+}+/// End-of-recording snapshot of a smoothing detector's state. Voiced frames/// in the withheld tail suggest — but don't prove — a final word cut off at/// the stop; a clean report doesn't rule VAD loss out either (soft trailingdiff --git a/src-tauri/src/audio_toolkit/vad/silero.rs b/src-tauri/src/audio_toolkit/vad/silero.rsindex 1785e5f0..e349726f 100644--- a/src-tauri/src/audio_toolkit/vad/silero.rs+++ b/src-tauri/src/audio_toolkit/vad/silero.rs@@ -4,7 +4,7 @@ use std::sync::{Mutex, OnceLock, PoisonError};use transcribe_cpp::{Backend, BackendMask, Model, ModelOptions, VadSession, VadSessionOptions};-use super::{VadFrame, VoiceActivityDetector};+use super::{check_whole_frames, SpeechScorer, VadFrame, VoiceActivityDetector};use crate::audio_toolkit::constants;pub struct SileroVad {@@ -79,24 +79,7 @@ impl VoiceActivityDetector for SileroVad {frame.len());}-- let result = self- .session- .get_mut()- .unwrap_or_else(PoisonError::into_inner)- .stream_feed(frame)- .map_err(|e| anyhow::anyhow!("Silero VAD error: {e}"))?;-- // A whole frame in must score exactly one frame; anything else means- // the stream is out of step with the recorder's framing.- let [prob] = result.probs[..] else {- anyhow::bail!(- "Silero VAD returned {} probabilities for one frame",- result.probs.len()- );- };-- if prob > self.threshold {+ if self.score(frame)?[0] > self.threshold {Ok(VadFrame::Speech(frame))} else {Ok(VadFrame::Noise)@@ -116,3 +99,62 @@ impl VoiceActivityDetector for SileroVad {.stream_reset();}}++impl SpeechScorer for SileroVad {+ fn score(&mut self, frames: &[f32]) -> Result<Vec<f32>> {+ check_whole_frames(frames, self.frame_samples)?;+ let result = self+ .session+ .get_mut()+ .unwrap_or_else(PoisonError::into_inner)+ .stream_feed(frames)+ .map_err(|e| anyhow::anyhow!("Silero VAD error: {e}"))?;++ // Whole frames in must score exactly that many frames; anything else+ // means the stream is out of step with the caller's framing.+ let expected = frames.len() / self.frame_samples;+ if result.probs.len() != expected {+ anyhow::bail!(+ "Silero VAD returned {} probabilities for {expected} frames",+ result.probs.len()+ );+ }+ Ok(result.probs)+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ /// Needs the bundled model (see AGENTS.md); run with `--ignored`.+ #[test]+ #[ignore]+ fn scores_the_same_in_pieces_and_frame_by_frame() {+ let path = concat!(+ env!("CARGO_MANIFEST_DIR"),+ "/resources/models/silero-vad-v6.2-F32.gguf"+ );+ let mut vad = SileroVad::new(path, 0.3).unwrap();+ let frame = vad.frame_samples();+ let pcm: Vec<f32> = (0..frame * 600)+ .map(|i| (i as f32 * 0.013).sin() * (i as f32 * 0.0007).cos() * 0.2)+ .collect();+ let whole = vad.score(&pcm).unwrap();+ assert_eq!(whole.len(), 600);++ let mut pieces = SileroVad::new(path, 0.3).unwrap();+ let mut scored = pieces.score(&pcm[..frame * 250]).unwrap();+ scored.extend(pieces.score(&pcm[frame * 250..]).unwrap());+ assert_eq!(scored, whole);++ let mut by_frame = SileroVad::new(path, 0.3).unwrap();+ let fed: Vec<f32> = pcm+ .chunks(frame)+ .flat_map(|chunk| by_frame.score(chunk).unwrap())+ .collect();+ assert_eq!(fed, whole);++ assert!(vad.score(&pcm[..frame + 1]).is_err());+ }+}diff --git a/src-tauri/src/cli.rs b/src-tauri/src/cli.rsindex 0423bced..4d40398b 100644--- a/src-tauri/src/cli.rs+++ b/src-tauri/src/cli.rs@@ -29,8 +29,9 @@ pub struct CliArgs {pub debug: bool,/// Transcribe this WAV (16 kHz mono) headlessly and exit. Runs the same- /// batch transcription path as the app — no mic, no VAD, no download- /// (the model must already be installed).+ /// batch transcription path as the app — no mic, no silence removal, no+ /// download (the model must already be installed). Long audio is still+ /// chunked at pauses, as in the app.#[arg(short = 'f', long, value_name = "WAV")]pub transcribe_file: Option<PathBuf>,diff --git a/src-tauri/src/engine_supervisor/mod.rs b/src-tauri/src/engine_supervisor/mod.rsindex a88634a4..0d733e8a 100644--- a/src-tauri/src/engine_supervisor/mod.rs+++ b/src-tauri/src/engine_supervisor/mod.rs@@ -29,7 +29,8 @@ mod worker;pub use protocol::{DeviceInfo, DeviceSelector, LoadedInfo};pub use supervisor::{- EngineError, EngineSupervisor, Finalized, LoadSpec, StreamHandle, StreamProgress, Unloading,+ CancelMark, EngineError, EngineSupervisor, Finalized, LoadSpec, StreamHandle, StreamProgress,+ Unloading,};/// Hidden first argument that turns the executable into a worker.diff --git a/src-tauri/src/engine_supervisor/protocol.rs b/src-tauri/src/engine_supervisor/protocol.rsindex 4e2726a6..cb8fb8f5 100644--- a/src-tauri/src/engine_supervisor/protocol.rs+++ b/src-tauri/src/engine_supervisor/protocol.rs@@ -153,6 +153,9 @@ pub struct LoadedInfo {pub on_gpu: bool,pub capabilities: Capabilities,pub supports_initial_prompt: bool,+ /// Whether the model handles audio longer than its native window in one+ /// run (whisper today); other models' long audio is chunked by Handy.+ pub supports_long_form: bool,pub supports_vocabulary: bool,}diff --git a/src-tauri/src/engine_supervisor/supervisor.rs b/src-tauri/src/engine_supervisor/supervisor.rsindex 8fae4f0b..713b9a57 100644--- a/src-tauri/src/engine_supervisor/supervisor.rs+++ b/src-tauri/src/engine_supervisor/supervisor.rs@@ -154,6 +154,10 @@ impl fmt::Display for EngineError {impl std::error::Error for EngineError {}+/// See [`EngineSupervisor::cancel_mark`].+#[derive(Debug, Clone, Copy)]+pub struct CancelMark(u64);+/// The result of one stream feed, handed to the stream's callback.pub struct StreamProgress {pub update: StreamUpdate,@@ -401,7 +405,24 @@ impl EngineSupervisor {/// Transcribe 16 kHz mono PCM with the loaded model. A worker crash or/// hang is retried once in a fresh worker (on CPU if it was on the GPU).pub fn transcribe(&self, pcm: Vec<f32>, run: RunOptions) -> Result<Transcript, EngineError> {- let epoch = self.shared.epoch();+ self.transcribe_since(self.cancel_mark(), pcm, run)+ }++ /// A point to cancel from: every run given it is cancelled by any+ /// [`cancel`](Self::cancel) after it was taken, so the runs that make up+ /// one transcription (its chunks) stop together, even between runs.+ pub fn cancel_mark(&self) -> CancelMark {+ CancelMark(self.shared.epoch())+ }++ /// [`transcribe`](Self::transcribe), cancelled by any cancel since `mark`.+ pub fn transcribe_since(+ &self,+ mark: CancelMark,+ pcm: Vec<f32>,+ run: RunOptions,+ ) -> Result<Transcript, EngineError> {+ let epoch = mark.0;self.ask(|reply| Command::Transcribe {pcm,run,@@ -1585,6 +1606,28 @@ mod tests {assert_eq!(worker_count(), 0);}+ /// A cancel between two runs from one mark (two chunks of one+ /// transcription) stops the second; a fresh mark runs again.+ #[test]+ #[ignore]+ fn cancel_between_runs_stops_the_rest_of_the_mark() {+ set_fault(None, false);+ let engine = EngineSupervisor::new(true);+ engine.load(spec()).unwrap();+ let mark = engine.cancel_mark();+ engine+ .transcribe_since(mark, test_pcm(), RunOptions::default())+ .unwrap();+ engine.cancel();+ assert!(matches!(+ engine.transcribe_since(mark, test_pcm(), RunOptions::default()),+ Err(EngineError::Cancelled)+ ));+ engine+ .transcribe(test_pcm(), RunOptions::default())+ .unwrap();+ }+/// A worker crash mid-stream loses the stream; the batch fallback then/// runs in a fresh worker.#[test]diff --git a/src-tauri/src/engine_supervisor/worker.rs b/src-tauri/src/engine_supervisor/worker.rsindex 3277268d..6a9a15a1 100644--- a/src-tauri/src/engine_supervisor/worker.rs+++ b/src-tauri/src/engine_supervisor/worker.rs@@ -313,6 +313,7 @@ fn load(.capabilities().map_err(|e| format!("Failed to read model capabilities: {e}"))?,supports_initial_prompt: model.supports(Feature::InitialPrompt),+ supports_long_form: model.supports(Feature::LongForm),supports_vocabulary: model.supports(Feature::Vocabulary),};Ok((session, info))diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rsindex 3f3370e4..5d512386 100644--- a/src-tauri/src/lib.rs+++ b/src-tauri/src/lib.rs@@ -694,6 +694,7 @@ pub fn run(cli_args: CliArgs) {shortcut::change_append_trailing_space_setting,shortcut::change_lazy_stream_close_setting,shortcut::change_vad_enabled_setting,+ shortcut::change_long_form_chunking_setting,shortcut::change_vad_backend_setting,shortcut::change_filler_word_removal_enabled_setting,shortcut::change_chinese_script_setting,diff --git a/src-tauri/src/managers/audio.rs b/src-tauri/src/managers/audio.rsindex c2e8db65..077be9a8 100644--- a/src-tauri/src/managers/audio.rs+++ b/src-tauri/src/managers/audio.rs@@ -4,7 +4,7 @@ use crate::audio_toolkit::{frames_for_duration_ms, EarshotVad, SmoothedVad, VAD_OFFLINE_HANGOVER_MS, VAD_ONSET_MS,VAD_PREFILL_MS, VAD_STREAMING_HANGOVER_MS,},- CaptureSink, CaptureVad, SileroVad, VadPolicy, VoiceActivityDetector,+ CaptureSink, CaptureVad, SileroVad, SpeechScorer, VadPolicy, VoiceActivityDetector,};use crate::helpers::clamshell;use crate::managers::transcription::StreamRouter;@@ -321,11 +321,14 @@ pub fn recording_error_type(error: &anyhow::Error) -> &'static str {/* ──────────────────────────────────────────────────────────────── */-fn create_capture_vad(+/// A fresh detector for `backend`, without Handy's smoothing: what a+/// recording's VAD wraps, and what scores audio for long-form chunking, so+/// every path uses the selected backend.+pub fn create_detector(backend: VadBackend,app_handle: &tauri::AppHandle,-) -> Result<CaptureVad, anyhow::Error> {- let detector: Box<dyn VoiceActivityDetector> = match backend {+) -> Result<Box<dyn SpeechScorer>, anyhow::Error> {+ Ok(match backend {VadBackend::Silero => {let vad_path = app_handle.path()@@ -343,7 +346,14 @@ fn create_capture_vad(EarshotVad::new(EARSHOT_VAD_THRESHOLD).map_err(|e| anyhow::anyhow!("Failed to create EarshotVad: {e}"))?,),- };+ })+}++fn create_capture_vad(+ backend: VadBackend,+ app_handle: &tauri::AppHandle,+) -> Result<CaptureVad, anyhow::Error> {+ let detector: Box<dyn VoiceActivityDetector> = create_detector(backend, app_handle)?;// Earshot uses 16 ms frames while Silero uses 32 ms. Convert the existing// time-based capture profile to each detector's frame size so selecting adiff --git a/src-tauri/src/managers/transcription.rs b/src-tauri/src/managers/transcription.rsindex 3a4a6b4c..ce666b53 100644--- a/src-tauri/src/managers/transcription.rs+++ b/src-tauri/src/managers/transcription.rs@@ -1,11 +1,12 @@+use crate::audio_toolkit::chunking;use crate::audio_toolkit::{apply_custom_words, detect_output_language, normalize_transcription_output,remove_filler_words, OutputLanguageEvidence,};use crate::chinese_script::{convert_chinese_script, ChineseVariety};use crate::engine_supervisor::{- DeviceInfo, DeviceSelector, EngineError, EngineSupervisor, LoadSpec, LoadedInfo,- StreamProgress, Unloading,+ CancelMark, DeviceInfo, DeviceSelector, EngineError, EngineSupervisor, LoadSpec, LoadedInfo,+ Unloading,};use crate::managers::audio::AudioRecordingManager;use crate::managers::model::ModelManager;@@ -13,7 +14,10 @@ use crate::settings::{get_settings, AppSettings, ChineseScript, ModelUnloadTimeout, TranscribeAcceleratorSetting,};use anyhow::Result;+use chunked::ChunkedDecode;+use live::LiveSession;use log::{debug, error, info, warn};+use policy::{batch_decode, live_decode, BatchDecode, ModelTraits};use serde::{Deserialize, Serialize};use specta::Type;use std::panic::{catch_unwind, AssertUnwindSafe};@@ -23,7 +27,11 @@ use std::thread;use std::time::{Duration, Instant, SystemTime};use tauri::{AppHandle, Emitter, Manager};use tauri_specta::Event;-use transcribe_cpp::{Backend, RunOptions, StreamOptions, Task};+use transcribe_cpp::{Backend, RunOptions, Task, Transcript};++mod chunked;+mod live;+pub mod policy;const STREAM_PERF_LOG_INTERVAL: Duration = Duration::from_secs(5);@@ -88,23 +96,19 @@ pub struct StreamPhaseEvent {/// is processed before finalize runs.enum StreamCmd {Feed(Vec<f32>),- /// Flush the stream and reply with the final text, or `None` if no- /// usable stream exists (caller should fall back to batch transcription).- /// `Err(Cancelled)` if the finalize itself was cancelled.- Finalize(mpsc::Sender<StreamFinalizeReply>),+ /// Flush the live decode and reply with what it decoded, or `None` if+ /// there is no usable live result (caller should fall back to batch+ /// transcription). `Err(Cancelled)` if the finalize itself was cancelled.+ /// `samples` is the whole recording's length, for a chunked decode that+ /// checks it saw it all.+ Finalize {+ reply: mpsc::Sender<StreamFinalizeReply>,+ samples: usize,+ },Cancel,}-type StreamFinalizeReply = Result<Option<FinalizedStreamText>, EngineError>;--struct FinalizedStreamText {- text: String,- output_language: OutputLanguageEvidence,- /// The streaming model's supported languages, for text-based detection.- supported_languages: Vec<String>,- /// Custom words went to the model as vocabulary; skip fuzzy correction.- custom_words_prompted: bool,-}+type StreamFinalizeReply = Result<Option<RunOutcome>, EngineError>;/// Routes real-time audio frames to the active streaming worker. Shared between/// the [`TranscriptionManager`] (opens/closes the route) and the audio recorder's@@ -112,44 +116,56 @@ struct FinalizedStreamText {/// directly, so a frame with no stream pending costs a single relaxed atomic/// load — no Tauri state lookup, no mutex lock.pub struct StreamRouter {- /// Command channel to the active streaming worker, present from- /// `start_stream` until `finalize_stream`/`cancel_stream`.- tx: Mutex<Option<mpsc::Sender<StreamCmd>>>,+ /// The active streaming worker's route, present from `start_stream`+ /// until `finalize_live`/`cancel_stream`.+ route: Mutex<Option<Route>>,/// True while a stream is pending or active (channel is open). The audio/// callback checks this first to avoid the mutex lock when no stream runs.open: Arc<AtomicBool>,}+/// One streaming worker's command channel.+struct Route {+ tx: mpsc::Sender<StreamCmd>,+ /// Set by `cancel_stream`, so the worker drops audio still queued ahead+ /// of the `Cancel` instead of decoding it.+ cancelled: Arc<AtomicBool>,+}+impl StreamRouter {fn new() -> Self {Self {- tx: Mutex::new(None),+ route: Mutex::new(None),open: Arc::new(AtomicBool::new(false)),}}/// Open a fresh command channel for a new streaming session, returning the- /// receiver the worker should drain. Caller must ensure no prior channel is- /// still open.- fn open(&self) -> mpsc::Receiver<StreamCmd> {+ /// receiver the worker should drain and its cancel flag. Caller must+ /// ensure no prior channel is still open.+ fn open(&self) -> (mpsc::Receiver<StreamCmd>, Arc<AtomicBool>) {let (tx, rx) = mpsc::channel::<StreamCmd>();- *self.tx.lock().unwrap() = Some(tx);+ let cancelled = Arc::new(AtomicBool::new(false));+ *self.route.lock().unwrap() = Some(Route {+ tx,+ cancelled: Arc::clone(&cancelled),+ });self.open.store(true, Ordering::Relaxed);- rx+ (rx, cancelled)}- /// Take the sender out (closing the channel to new feeds). Returns the- /// sender so the caller can send the final `Finalize`/`Cancel` command.- fn take(&self) -> Option<mpsc::Sender<StreamCmd>> {+ /// Take the route out (closing the channel to new feeds), so the caller+ /// can send the final `Finalize`/`Cancel` command.+ fn take(&self) -> Option<Route> {self.open.store(false, Ordering::Relaxed);- self.tx.lock().unwrap().take()+ self.route.lock().unwrap().take()}/// Drop the channel and mark closed without sending a final command (used/// when the worker exits without a finalize/cancel handshake).fn clear(&self) {self.open.store(false, Ordering::Relaxed);- *self.tx.lock().unwrap() = None;+ *self.route.lock().unwrap() = None;}/// Forward a 16 kHz frame to the active streaming worker. Cheap no-op (a@@ -158,8 +174,8 @@ impl StreamRouter {if !self.open.load(Ordering::Relaxed) {return;}- if let Some(tx) = self.tx.lock().unwrap().as_ref() {- let _ = tx.send(StreamCmd::Feed(frame.to_vec()));+ if let Some(route) = self.route.lock().unwrap().as_ref() {+ let _ = route.tx.send(StreamCmd::Feed(frame.to_vec()));}}@@ -699,16 +715,17 @@ impl TranscriptionManager {Arc::clone(&self.router)}- /// Begin a live streaming transcription on the held engine's session.- /// Audio frames pushed via [`StreamRouter::feed`] (captured directly by the- /// audio recorder) are decoded incrementally and emitted to the overlay as- /// [`StreamTextEvent`].+ /// Begin a recording's live decode on the held engine's session. Audio+ /// frames pushed via [`StreamRouter::feed`] (captured directly by the+ /// audio recorder) are streamed, with preview text emitted to the overlay+ /// as [`StreamTextEvent`], or cut into long-form chunks decoded as the+ /// recording goes; [`policy::live_decode`] picks which.////// Non-blocking: spawns a worker that waits for any in-progress model load,- /// verifies the model supports streaming, then begins the stream. If the- /// model can't stream, the worker idles until finalize/cancel and reports- /// `None` so the caller falls back to batch transcription. Frames sent- /// before the stream begins queue on the channel and are not lost.+ /// then begins the live decode the loaded model calls for. With none, the+ /// worker idles until finalize/cancel and reports `None` so the caller+ /// falls back to batch transcription. Frames sent before the decode begins+ /// queue on the channel and are not lost.pub fn start_stream(&self) {if self.router.is_open() || self.active_stream_worker.load(Ordering::Acquire) != 0 {warn!("start_stream called while a stream worker is already active");@@ -723,14 +740,19 @@ impl TranscriptionManager {warn!("start_stream lost a race with another stream worker");return;}- let rx = self.router.open();+ let (rx, cancelled) = self.router.open();self.stream_active.store(false, Ordering::Release);let manager = self.clone();- thread::spawn(move || manager.run_stream_worker(rx, worker_id));+ thread::spawn(move || manager.run_stream_worker(rx, cancelled, worker_id));}- fn run_stream_worker(&self, rx: mpsc::Receiver<StreamCmd>, worker_id: u64) {+ fn run_stream_worker(+ &self,+ rx: mpsc::Receiver<StreamCmd>,+ cancelled: Arc<AtomicBool>,+ worker_id: u64,+ ) {let _worker = StreamWorkerGuard {worker_id,active_stream_worker: Arc::clone(&self.active_stream_worker),@@ -748,9 +770,8 @@ impl TranscriptionManager {let model_id = self.get_current_model().unwrap_or_default();- // Models without streaming fall back to batch. The loaded model (not- // the ModelManager copy) is the source of truth for run-path- // capabilities.+ // The loaded model (not the ModelManager copy) is the source of truth+ // for run-path capabilities.let Some(info) = self.engine.loaded() else {info!("Live preview: model '{}' is not a loaded transcribe-cpp model; \@@ -772,193 +793,75 @@ impl TranscriptionManager {caps.supports_translate,caps.languages,);- if !caps.supports_streaming {+ let mode = live_decode(+ ModelTraits::of(&info),+ get_settings(&self.app_handle).long_form_chunking,+ );+ let Some(mut session) = LiveSession::begin(self, mode, &info, &model_id) else {+ // Nothing to decode live: the recording is transcribed in batch.self.router.clear();drain_until_finalize(rx);return;- }- let languages = caps.languages.clone();-- // Build run options mirroring the offline transcribe-cpp path: task +- // language gated against what the model actually advertises.- let settings = get_settings(&self.app_handle);- let effective_language =- effective_language_for_model(&settings, self.model_manager.as_ref(), &model_id);- let run_plan = transcribe_cpp_run_plan(- settings.translate_to_english,- &effective_language,- &languages,- caps.supports_translate,- );- let output_language = resolve_output_language_evidence(- &settings,- run_plan.language.as_deref(),- &languages,- run_plan.target_language.as_deref() == Some("en"),- );- let vocabulary = vocabulary_for_model(info.supports_vocabulary, &settings.custom_words);- let custom_words_prompted = !vocabulary.is_empty();- let run_options = RunOptions {- task: run_plan.task,- language: run_plan.language,- target_language: run_plan.target_language,- vocabulary,- ..Default::default()};-- // Feed results arrive on the engine's thread; this callback only- // converts and emits preview text.- let perf = Arc::new(Mutex::new(StreamPerf::new()));- let on_progress = {- let perf = Arc::clone(&perf);- let app_handle = self.app_handle.clone();- let languages = languages.clone();- let mut preview_script = PreviewScript::new(settings.chinese_script, &output_language);- move |progress: StreamProgress| {- let mut perf = lock_perf(&perf);- perf.record_compute(progress.elapsed);- perf.record_update(&progress.update);- if let Some(text) = progress.text {- perf.record_emit();- let (committed, tentative) =- preview_script.convert(&text.committed, &text.tentative, &languages);- emit_stream_text(&app_handle, &committed, &tentative);- }- perf.maybe_log();- }- };-- // Run the stream in the engine's worker process. Feeds are queued- // without waiting; a crashed or hung worker makes finalize report no- // result, and the caller falls back to batch transcription. StreamOptions::default()- // uses CommitPolicy::Auto and lets the family pick its own streaming- // strategy (no family-specific ext).- let stream =- match self- .engine- .start_stream(run_options, StreamOptions::default(), on_progress)- {- Ok(stream) => stream,- Err(e) => {- error!("Failed to begin stream: {}", e);- self.forget_model_if_engine_dropped(&model_id, &e.to_string());- drain_until_finalize(rx);- return;- }- };-- self.stream_active.store(true, Ordering::Release);self.touch_activity();- info!(- "Live streaming transcription started (model '{}', backend '{}')",- model_id, info.backend- );while let Ok(cmd) = rx.recv() {match cmd {StreamCmd::Feed(pcm) => {+ // Audio queued ahead of a cancel is dropped, not decoded.+ if cancelled.load(Ordering::Acquire) {+ continue;+ }self.touch_activity();- lock_perf(&perf).record_feed(pcm.len());- stream.feed(pcm);+ session.feed(pcm);}- StreamCmd::Finalize(reply) => {- // In auto mode the model's own LID is the best remaining- // evidence; the snapshot is only materialized when it can- // change the outcome.- let want_language = matches!(output_language, OutputLanguageEvidence::Unknown);- let result = match stream.finalize(want_language) {- // After finalize the committed prefix holds the full- // text; display() = committed + tentative is the safe read.- Ok(Some(finalized)) => {- let mut perf = lock_perf(&perf);- perf.record_compute(finalized.elapsed);- perf.record_update(&finalized.update);- let output_language = match &output_language {- OutputLanguageEvidence::Unknown => with_model_detected_language(- OutputLanguageEvidence::Unknown,- finalized.language,- ),- resolved => resolved.clone(),- };- Ok(Some(FinalizedStreamText {- text: finalized.text.full,- output_language,- supported_languages: languages.clone(),- custom_words_prompted,- }))- }- Ok(None) => {- error!(- "Live transcription could not be finalized; \- falling back to batch transcription"- );- self.forget_model_if_engine_dropped(- &model_id,- "live transcription failed and the model could not be restarted",- );- Ok(None)- }- Err(e) => {- info!("Live transcription finalize stopped: {}", e);- Err(e)- }- };- let chars = match &result {- Ok(Some(finalized)) => finalized.text.len(),- _ => 0,- };- lock_perf(&perf).log_finalized(chars);- let _ = reply.send(result);+ StreamCmd::Finalize { reply, samples } => {+ let _ = reply.send(session.finish(samples));return;}- // Dropping the stream handle resets the worker's stream.+ // Dropping the session resets the worker's stream.StreamCmd::Cancel => return,}}- // The channel closed without finalize/cancel; dropping the stream- // handle resets the worker's stream. `_worker` drops after, clearing- // this worker's flags.+ // The channel closed without finalize/cancel; dropping the session+ // resets the worker's stream. `_worker` drops after, clearing this+ // worker's flags.}- /// Flush the active stream and return its final, post-filtered text.+ /// Flush the live decode (a stream, or chunks decoded while recording)+ /// and return what it produced. `recorded_samples` is the length of the+ /// whole recording: a chunked decode that saw less reports no result.///- /// `Ok(None)` means no usable stream result exists and the caller should+ /// `Ok(None)` means no usable live result exists and the caller should/// fall back to batch transcription. `Err` means the transcription was/// cancelled. There is no timeout here: waiting behind a model load is a/// queue position, not a failure (#1841), and the engine bounds the/// finalize itself by the audio it still has to decode.- pub fn finalize_stream(&self) -> Result<Option<String>> {- let Some(tx) = self.router.take() else {+ fn finalize_live(&self, recorded_samples: usize) -> Result<Option<RunOutcome>> {+ let Some(route) = self.router.take() else {return Ok(None);};let (reply_tx, reply_rx) = mpsc::channel();- if tx.send(StreamCmd::Finalize(reply_tx)).is_err() {+ let finalize = StreamCmd::Finalize {+ reply: reply_tx,+ samples: recorded_samples,+ };+ if route.tx.send(finalize).is_err() {return Ok(None);}- let finalized = match reply_rx.recv() {- Ok(Ok(Some(finalized))) => finalized,- Ok(Ok(None)) | Err(_) => return Ok(None),- Ok(Err(e)) => return Err(e.into()),- };-- let settings = get_settings(&self.app_handle);- // Skip fuzzy correction if the model already got the custom words.- let filtered = post_process_transcription_text(- finalized.text,- &settings,- finalized.custom_words_prompted,- &finalized.output_language,- &finalized.supported_languages,- );-- self.maybe_unload_immediately("streaming transcription");- Ok(Some(filtered))+ match reply_rx.recv() {+ Ok(Ok(outcome)) => Ok(outcome),+ Ok(Err(e)) => Err(e.into()),+ Err(_) => Ok(None),+ }}- /// Abandon any active stream without producing text (e.g. on cancel).+ /// Abandon any live decode without producing text (e.g. on cancel). A+ /// chunk already decoding finishes; audio queued behind it is dropped.pub fn cancel_stream(&self) {- if let Some(tx) = self.router.take() {- let _ = tx.send(StreamCmd::Cancel);+ if let Some(route) = self.router.take() {+ route.cancelled.store(true, Ordering::Release);+ let _ = route.tx.send(StreamCmd::Cancel);}self.stream_active.store(false, Ordering::Release);}@@ -980,7 +883,28 @@ impl TranscriptionManager {.emit(&self.app_handle);}+ /// Transcribe a whole clip in batch: a re-transcribed history entry, or+ /// a file from the CLI.pub fn transcribe(&self, audio: Vec<f32>) -> Result<String> {+ self.complete(Instant::now(), audio, None)+ }++ /// Transcribe a finished recording: what its live decode produced, or a+ /// batch run of the whole recording when there is no live result.+ pub fn transcribe_recording(&self, samples: Vec<f32>) -> Result<String> {+ let started = Instant::now();+ let live = self.finalize_live(samples.len())?;+ self.complete(started, samples, live)+ }++ /// The final text of one transcription, from its live result or else a+ /// batch run of `audio`. Every transcription finishes here.+ fn complete(+ &self,+ started: Instant,+ audio: Vec<f32>,+ live: Option<RunOutcome>,+ ) -> Result<String> {#[cfg(debug_assertions)]if std::env::var("HANDY_FORCE_TRANSCRIPTION_FAILURE").is_ok() {return Err(anyhow::anyhow!(@@ -988,20 +912,61 @@ impl TranscriptionManager {));}- // Update last activity timestamp- self.touch_activity();-- let st = std::time::Instant::now();let audio_len = audio.len();-debug!("Audio vector length: {}", audio_len);-if audio.is_empty() {debug!("Empty audio vector");self.maybe_unload_immediately("empty audio");return Ok(String::new());}+ let outcome = match live {+ Some(outcome) => outcome,+ None => self.decode_batch(audio)?,+ };+ let settings = get_settings(&self.app_handle);+ let final_result = finish_text(outcome, &settings);++ let translation_note = if settings.translate_to_english {+ " (translated)"+ } else {+ ""+ };+ // Real-time factor. Input PCM is 16 kHz mono, so audio length in seconds+ // is samples / 16000. `speedup` is audio_secs / elapsed_secs — e.g. 4.00x+ // means transcribed 4x faster than real time+ let elapsed_secs = started.elapsed().as_secs_f64();+ let audio_secs = audio_len as f64 / 16_000.0;+ let speedup = real_time_factor(audio_secs, elapsed_secs);+ info!(+ "Transcription completed in {:.2}s for {:.2}s of audio ({:.2}x real-time){}",+ elapsed_secs, audio_secs, speedup, translation_note+ );++ if final_result.is_empty() {+ info!("Transcription result is empty");+ } else {+ info!(+ "Transcription result: {}",+ crate::utils::redact_text(&final_result)+ );+ }++ self.maybe_unload_immediately("transcription");++ Ok(final_result)+ }++ /// Decode a whole clip with the loaded model, loading it first if a load+ /// is under way.+ fn decode_batch(&self, audio: Vec<f32>) -> Result<RunOutcome> {+ // A cancel from here on (during the model-load wait or chunk planning+ // included) stops every run of this transcription.+ let mark = self.engine.cancel_mark();++ // Update last activity timestamp+ self.touch_activity();+// Check if model is loaded, if not try to load it{// If the model is loading, wait for it to complete.@@ -1045,150 +1010,212 @@ impl TranscriptionManager {"Model failed to load after auto-load attempt. Please check your model settings."));};- let run =- self.transcribe_cpp(info, audio, &settings, &validated_language, &active_model)?;-- let output_language = with_model_detected_language(- resolve_output_language_evidence(- &settings,- run.applied_language_hint.as_deref(),- &run.languages,- run.output_was_translated,- ),- run.model_detected_language,- );- debug!("Output language evidence: {:?}", output_language);-- // Skip fuzzy correction if the model already got the custom words.- let filtered_result = post_process_transcription_text(- run.text,+ self.transcribe_cpp(+ mark,+ info,+ audio,&settings,- run.custom_words_prompted,- &output_language,- &run.languages,- );-- let et = std::time::Instant::now();- let translation_note = if settings.translate_to_english {- " (translated)"- } else {- ""- };- // Real-time factor. Input PCM is 16 kHz mono, so audio length in seconds- // is samples / 16000. `speedup` is audio_secs / elapsed_secs — e.g. 4.00x- // means transcribed 4x faster than real time- let elapsed_secs = (et - st).as_secs_f64();- let audio_secs = audio_len as f64 / 16_000.0;- let speedup = real_time_factor(audio_secs, elapsed_secs);- info!(- "Transcription completed in {:.2}s for {:.2}s of audio ({:.2}x real-time){}",- elapsed_secs, audio_secs, speedup, translation_note- );-- let final_result = filtered_result;-- if final_result.is_empty() {- info!("Transcription result is empty");- } else {- info!(- "Transcription result: {}",- crate::utils::redact_text(&final_result)- );- }-- self.maybe_unload_immediately("transcription");-- Ok(final_result)+ &validated_language,+ &active_model,+ )}/// transcribe-cpp: the model runs in the engine's worker process, which- /// recovers from crashes and hangs itself. The loaded model's live- /// capabilities (cheap GGUF-metadata reads) are the source of truth, not- /// the ModelManager copy; translate is gated on what the model advertises.+ /// recovers from crashes and hangs itself. Long audio is decoded in+ /// chunks cut at pauses, unless the model handles it in one run.fn transcribe_cpp(&self,+ mark: CancelMark,info: LoadedInfo,audio: Vec<f32>,settings: &AppSettings,validated_language: &str,active_model: &str,) -> Result<RunOutcome> {- let model_supports_translate = info.capabilities.supports_translate;- let languages = info.capabilities.languages;-- let vocabulary = vocabulary_for_model(info.supports_vocabulary, &settings.custom_words);- let custom_words_prompted = !vocabulary.is_empty();-- debug!(- "transcribe-cpp model '{}' on '{}': initial_prompt={}, vocabulary={}, translate={}, languages={:?}",- settings.selected_model,- info.backend,- info.supports_initial_prompt,- info.supports_vocabulary,- model_supports_translate,- languages- );-- let run_plan = transcribe_cpp_run_plan(- settings.translate_to_english,- validated_language,- &languages,- model_supports_translate,- );- let output_was_translated = run_plan.target_language.as_deref() == Some("en");- let applied_language_hint = run_plan.language.clone();-- let run_options = RunOptions {- task: run_plan.task,- language: run_plan.language,- target_language: run_plan.target_language,- vocabulary,- ..Default::default()+ let setup = run_setup(&info, settings, validated_language);+ let whole = |pcm: Vec<f32>| {+ self.run_batch(mark, pcm, &setup.run_options, active_model)+ .map_err(engine_error)};-- debug!(- "transcribe-cpp run: task={:?}, language={:?}, vocabulary_terms={}",- run_options.task,- run_options.language,- run_options.vocabulary.len()- );-- let transcript = match self.engine.transcribe(audio, run_options) {- Ok(transcript) => transcript,- Err(EngineError::Cancelled) => return Err(EngineError::Cancelled.into()),- Err(e) => {- self.forget_model_if_engine_dropped(active_model, &e.to_string());- return Err(anyhow::anyhow!(- "transcribe-cpp transcription failed: {}",- e- ));- }+ // `mark` covers every chunk, so a cancel between two stops the rest.+ // A failed chunk fails the whole transcription.+ let transcripts = match batch_decode(ModelTraits::of(&info), settings.long_form_chunking) {+ BatchDecode::Chunked => ChunkedDecode::decode_clip(+ self,+ mark,+ setup.run_options.clone(),+ audio,+ active_model,+ settings.vad_backend,+ )+ .map_err(engine_error)?,+ BatchDecode::Whole => vec![whole(audio)?],};+ let (text, model_detected_language) = join_chunks(transcripts);Ok(RunOutcome {- text: transcript.text,- languages,- applied_language_hint,- output_was_translated,- // Whisper's audio-based LID (auto mode only; `None` when a- // language hint was passed).- model_detected_language: transcript.language,- custom_words_prompted,+ setup,+ text,+ model_detected_language,})}++ /// One batch run of a transcription that began at `mark`, so a cancel+ /// since then stops it. Keeps the idle watcher from unloading the model+ /// between the runs of a chunked transcription.+ fn run_batch(+ &self,+ mark: CancelMark,+ pcm: Vec<f32>,+ run_options: &RunOptions,+ active_model: &str,+ ) -> Result<Transcript, EngineError> {+ self.touch_activity();+ self.engine+ .transcribe_since(mark, pcm, run_options.clone())+ .inspect_err(|e| {+ if !matches!(e, EngineError::Cancelled) {+ self.forget_model_if_engine_dropped(active_model, &e.to_string());+ }+ })+ }}-/// What a batch transcription produced, plus what post-processing needs to-/// know about how it was produced.+/// What one transcription decoded (live or in batch), with how its runs were+/// set up, which post-processing needs to know.struct RunOutcome {+ setup: RunSetup,text: String,- /// The model's supported languages.+ /// The language the model itself detected, if it reported one.+ model_detected_language: Option<String>,+}++/// One transcription's final text: its output language resolved, then+/// post-processed.+fn finish_text(outcome: RunOutcome, settings: &AppSettings) -> String {+ let RunOutcome {+ setup,+ text,+ model_detected_language,+ } = outcome;+ let output_language = setup.output_language(settings, model_detected_language);+ debug!("Output language evidence: {:?}", output_language);+ // Skip fuzzy correction if the model already got the custom words.+ post_process_transcription_text(+ text,+ settings,+ setup.custom_words_prompted(),+ &output_language,+ &setup.languages,+ )+}++/// How every run of one transcription (each chunk, or a whole clip) is+/// configured on the loaded model, and what post-processing needs to know.+struct RunSetup {+ run_options: RunOptions,languages: Vec<String>,applied_language_hint: Option<String>,output_was_translated: bool,- /// The language the model itself detected, if it reported one.- model_detected_language: Option<String>,+}++impl RunSetup {/// Custom words went to the model as vocabulary; skip fuzzy correction.- custom_words_prompted: bool,+ fn custom_words_prompted(&self) -> bool {+ !self.run_options.vocabulary.is_empty()+ }++ /// What the output language is known to be, given the model's own+ /// detection when it reported one.+ fn output_language(+ &self,+ settings: &AppSettings,+ model_detected_language: Option<String>,+ ) -> OutputLanguageEvidence {+ with_model_detected_language(+ resolve_output_language_evidence(+ settings,+ self.applied_language_hint.as_deref(),+ &self.languages,+ self.output_was_translated,+ ),+ model_detected_language,+ )+ }+}++/// The loaded model's live capabilities (cheap GGUF-metadata reads) are the+/// source of truth, not the ModelManager copy; translate is gated on what the+/// model advertises.+fn run_setup(info: &LoadedInfo, settings: &AppSettings, validated_language: &str) -> RunSetup {+ let model_supports_translate = info.capabilities.supports_translate;+ let languages = info.capabilities.languages.clone();+ debug!(+ "transcribe-cpp model '{}' on '{}': initial_prompt={}, vocabulary={}, translate={}, languages={:?}",+ settings.selected_model,+ info.backend,+ info.supports_initial_prompt,+ info.supports_vocabulary,+ model_supports_translate,+ languages+ );++ let run_plan = transcribe_cpp_run_plan(+ settings.translate_to_english,+ validated_language,+ &languages,+ model_supports_translate,+ );+ let output_was_translated = run_plan.target_language.as_deref() == Some("en");+ let applied_language_hint = run_plan.language.clone();++ let run_options = RunOptions {+ task: run_plan.task,+ language: run_plan.language,+ target_language: run_plan.target_language,+ vocabulary: vocabulary_for_model(info.supports_vocabulary, &settings.custom_words),+ ..Default::default()+ };++ debug!(+ "transcribe-cpp run: task={:?}, language={:?}, vocabulary_terms={}",+ run_options.task,+ run_options.language,+ run_options.vocabulary.len()+ );++ RunSetup {+ run_options,+ languages,+ applied_language_hint,+ output_was_translated,+ }+}++/// A batch run's error: a cancel stays recognisable as one.+fn engine_error(e: EngineError) -> anyhow::Error {+ match e {+ EngineError::Cancelled => EngineError::Cancelled.into(),+ e => anyhow::anyhow!("transcribe-cpp transcription failed: {}", e),+ }+}++/// One transcription's text from its chunks, in order, and the model's+/// detected language (Whisper's audio-based LID, auto mode only; `None` when a+/// language hint was passed). Chunks that disagree leave it unknown. A single+/// transcript passes through untouched.+fn join_chunks(transcripts: Vec<Transcript>) -> (String, Option<String>) {+ let mut detected = transcripts.iter().map(|t| t.language.clone());+ let language = detected+ .next()+ .flatten()+ .filter(|first| detected.all(|language| language.as_ref() == Some(first)));+ let text = match <[Transcript; 1]>::try_from(transcripts) {+ Ok([one]) => one.text,+ Err(many) => {+ chunking::join_transcripts(&many.iter().map(|t| t.text.as_str()).collect::<Vec<_>>())+ }+ };+ (text, language)}fn emit_stream_text(app_handle: &AppHandle, committed: &str, tentative: &str) {@@ -1608,7 +1635,7 @@ fn drain_until_finalize(rx: mpsc::Receiver<StreamCmd>) {while let Ok(cmd) = rx.recv() {match cmd {StreamCmd::Feed(_) => {}- StreamCmd::Finalize(reply) => {+ StreamCmd::Finalize { reply, .. } => {let _ = reply.send(Ok(None));break;}diff --git a/src-tauri/src/managers/transcription/chunked.rs b/src-tauri/src/managers/transcription/chunked.rsnew file mode 100644index 00000000..3e226657--- /dev/null+++ b/src-tauri/src/managers/transcription/chunked.rs@@ -0,0 +1,142 @@+//! Long-form chunked decoding, shared by a live recording and a batch clip:+//! both cut the same audio at the same pauses and decode it the same way.++use std::time::Instant;++use log::info;+use transcribe_cpp::{RunOptions, Transcript};++use super::TranscriptionManager;+use crate::audio_toolkit::chunking::Chunker;+use crate::engine_supervisor::{CancelMark, EngineError};+use crate::managers::audio::create_detector;+use crate::settings::VadBackend;++/// Cuts audio at pauses as it arrives and decodes each chunk once its cut is+/// certain, so a recording has only its last chunk left when it stops.+pub(super) struct ChunkedDecode<'a> {+ tm: &'a TranscriptionManager,+ /// Every chunk's run stops with one cancel.+ mark: CancelMark,+ run_options: RunOptions,+ model_id: String,+ chunker: Chunker,+ transcripts: Vec<Transcript>,+ /// The first run that failed or was cancelled; nothing more is decoded+ /// after it.+ failure: Option<EngineError>,+}++impl<'a> ChunkedDecode<'a> {+ pub(super) fn new(+ tm: &'a TranscriptionManager,+ mark: CancelMark,+ run_options: RunOptions,+ model_id: &str,+ backend: VadBackend,+ ) -> Self {+ // The VAD is only loaded if the audio turns out long enough to cut.+ let app_handle = tm.app_handle.clone();+ Self {+ tm,+ mark,+ run_options,+ model_id: model_id.to_string(),+ chunker: Chunker::new(Box::new(move || create_detector(backend, &app_handle))),+ transcripts: Vec::new(),+ failure: None,+ }+ }++ /// A whole clip's chunks, decoded in order.+ pub(super) fn decode_clip(+ tm: &'a TranscriptionManager,+ mark: CancelMark,+ run_options: RunOptions,+ audio: Vec<f32>,+ model_id: &str,+ backend: VadBackend,+ ) -> Result<Vec<Transcript>, EngineError> {+ let mut decode = Self::new(tm, mark, run_options, model_id, backend);+ decode.push(audio);+ decode.finish()+ }++ /// Samples pushed so far.+ pub(super) fn samples(&self) -> usize {+ self.chunker.samples()+ }++ /// Chunks decoded so far.+ pub(super) fn decoded(&self) -> usize {+ self.transcripts.len()+ }++ /// Whether a failure already ended the decode; [`finish`](Self::finish)+ /// reports it.+ pub(super) fn failed(&self) -> bool {+ self.failure.is_some()+ }++ /// Adds audio, decoding each chunk it makes certain.+ pub(super) fn push(&mut self, pcm: Vec<f32>) {+ if self.failure.is_some() {+ return;+ }+ let chunks = self.chunker.push(pcm);+ self.decode(chunks);+ }++ /// Decodes what is left. Every chunk's transcript in order, or the run+ /// that failed or was cancelled.+ pub(super) fn finish(mut self) -> Result<Vec<Transcript>, EngineError> {+ if self.failure.is_none() {+ let chunks = self.chunker.finish();+ self.decode(chunks);+ }+ if let Some(failure) = self.failure {+ return Err(failure);+ }+ if self.transcripts.len() > 1 {+ info!(+ "Decoded {:.1}s of audio in {} chunks",+ self.samples() as f64 / 16_000.0,+ self.transcripts.len()+ );+ }+ Ok(self.transcripts)+ }++ fn decode(&mut self, chunks: Vec<Vec<f32>>) {+ for chunk in chunks {+ if self.failure.is_some() {+ return;+ }+ let audio_secs = chunk.len() as f64 / 16_000.0;+ let started = Instant::now();+ match self+ .tm+ .run_batch(self.mark, chunk, &self.run_options, &self.model_id)+ {+ Ok(transcript) => {+ self.transcripts.push(transcript);+ // Real-time factor: audio seconds decoded per second of+ // wall clock. Floored so a near-instant decode reports a+ // large factor instead of infinity.+ let elapsed_secs = started.elapsed().as_secs_f64().max(1e-6);+ info!(+ "Decoded long-form chunk {} ({:.1}s of audio) in {:.2}s ({:.0}x real time)",+ self.transcripts.len(),+ audio_secs,+ elapsed_secs,+ audio_secs / elapsed_secs+ );+ }+ Err(e) => {+ info!("Chunk decoding stopped: {}", e);+ self.failure = Some(e);+ }+ }+ }+ }+}diff --git a/src-tauri/src/managers/transcription/live.rs b/src-tauri/src/managers/transcription/live.rsnew file mode 100644index 00000000..ae1e4050--- /dev/null+++ b/src-tauri/src/managers/transcription/live.rs@@ -0,0 +1,248 @@+//! A recording's live decode: a stream the model decodes as audio arrives,+//! or long-form chunks decoded as the recording goes.++use std::sync::atomic::Ordering;+use std::sync::{Arc, Mutex};++use log::{debug, error, info};+use transcribe_cpp::StreamOptions;++use super::chunked::ChunkedDecode;+use super::policy::LiveDecode;+use super::{+ effective_language_for_model, emit_stream_text, join_chunks, lock_perf, run_setup,+ PreviewScript, RunOutcome, RunSetup, StreamFinalizeReply, StreamPerf, TranscriptionManager,+};+use crate::audio_toolkit::OutputLanguageEvidence;+use crate::engine_supervisor::{EngineError, LoadedInfo, StreamHandle, StreamProgress};+use crate::settings::{get_settings, AppSettings};++/// One recording's live decode, with the run setup its result finishes with.+pub(super) struct LiveSession<'a> {+ setup: RunSetup,+ decoder: LiveDecoder<'a>,+}++enum LiveDecoder<'a> {+ Stream(StreamSession<'a>),+ Chunked(Box<ChunkedDecode<'a>>),+}++/// A decoded text and the language the model detected in it, if any.+type Decoded = (String, Option<String>);++impl<'a> LiveSession<'a> {+ /// Begins the live decode `mode` picks on the loaded model. `None` when+ /// there is none or it couldn't begin: the recording is then transcribed+ /// in batch when it stops.+ pub(super) fn begin(+ tm: &'a TranscriptionManager,+ mode: LiveDecode,+ info: &LoadedInfo,+ model_id: &str,+ ) -> Option<Self> {+ if mode == LiveDecode::None {+ return None;+ }+ // The same run setup as the batch path: task + language gated+ // against what the model actually advertises.+ let settings = get_settings(&tm.app_handle);+ let effective_language =+ effective_language_for_model(&settings, tm.model_manager.as_ref(), model_id);+ let setup = run_setup(info, &settings, &effective_language);+ let decoder = match mode {+ LiveDecode::None => unreachable!("returned above"),+ LiveDecode::Stream => {+ LiveDecoder::Stream(StreamSession::begin(tm, info, &settings, &setup, model_id)?)+ }+ LiveDecode::Chunked => {+ info!("Live long-form chunking started (model '{}')", model_id);+ LiveDecoder::Chunked(Box::new(ChunkedDecode::new(+ tm,+ tm.engine.cancel_mark(),+ setup.run_options.clone(),+ model_id,+ settings.vad_backend,+ )))+ }+ };+ Some(Self { setup, decoder })+ }++ pub(super) fn feed(&mut self, pcm: Vec<f32>) {+ match &mut self.decoder {+ LiveDecoder::Stream(stream) => stream.feed(pcm),+ LiveDecoder::Chunked(chunks) => chunks.push(pcm),+ }+ }++ /// The live result; `None` to transcribe the recording in batch instead.+ /// `recorded_samples` is the whole recording's length.+ pub(super) fn finish(self, recorded_samples: usize) -> StreamFinalizeReply {+ let decoded = match self.decoder {+ LiveDecoder::Stream(stream) => stream.finish()?,+ LiveDecoder::Chunked(chunks) => finish_chunks(*chunks, recorded_samples)?,+ };+ Ok(decoded.map(|(text, model_detected_language)| RunOutcome {+ setup: self.setup,+ text,+ model_detected_language,+ }))+ }+}++fn finish_chunks(+ chunks: ChunkedDecode<'_>,+ recorded_samples: usize,+) -> Result<Option<Decoded>, EngineError> {+ // A failure stops the decode short of the recording; report the failure.+ if chunks.samples() != recorded_samples && !chunks.failed() {+ debug!(+ "Live chunks saw {} of the recording's {} samples; transcribing it in batch",+ chunks.samples(),+ recorded_samples+ );+ return Ok(None);+ }+ let while_recording = chunks.decoded();+ match chunks.finish() {+ Ok(transcripts) => {+ if while_recording > 0 {+ info!(+ "{} of {} chunks were decoded while recording",+ while_recording,+ transcripts.len()+ );+ }+ Ok(Some(join_chunks(transcripts)))+ }+ Err(EngineError::Cancelled) => Err(EngineError::Cancelled),+ Err(e) => {+ info!("Live chunks failed ({}); transcribing in batch", e);+ Ok(None)+ }+ }+}++/// A stream the model decodes as audio arrives, with preview text emitted+/// to the overlay.+struct StreamSession<'a> {+ tm: &'a TranscriptionManager,+ model_id: String,+ stream: StreamHandle,+ perf: Arc<Mutex<StreamPerf>>,+ /// In auto mode the model's own LID is the best remaining evidence; the+ /// snapshot is only materialized when it can change the outcome.+ want_language: bool,+}++impl<'a> StreamSession<'a> {+ fn begin(+ tm: &'a TranscriptionManager,+ info: &LoadedInfo,+ settings: &AppSettings,+ setup: &RunSetup,+ model_id: &str,+ ) -> Option<Self> {+ let output_language = setup.output_language(settings, None);++ // Feed results arrive on the engine's thread; this callback only+ // converts and emits preview text.+ let perf = Arc::new(Mutex::new(StreamPerf::new()));+ let on_progress = {+ let perf = Arc::clone(&perf);+ let app_handle = tm.app_handle.clone();+ let languages = setup.languages.clone();+ let mut preview_script = PreviewScript::new(settings.chinese_script, &output_language);+ move |progress: StreamProgress| {+ let mut perf = lock_perf(&perf);+ perf.record_compute(progress.elapsed);+ perf.record_update(&progress.update);+ if let Some(text) = progress.text {+ perf.record_emit();+ let (committed, tentative) =+ preview_script.convert(&text.committed, &text.tentative, &languages);+ emit_stream_text(&app_handle, &committed, &tentative);+ }+ perf.maybe_log();+ }+ };++ // Run the stream in the engine's worker process. Feeds are queued+ // without waiting; a crashed or hung worker makes finalize report no+ // result, and the caller falls back to batch transcription.+ // StreamOptions::default() uses CommitPolicy::Auto and lets the+ // family pick its own streaming strategy (no family-specific ext).+ let stream = match tm.engine.start_stream(+ setup.run_options.clone(),+ StreamOptions::default(),+ on_progress,+ ) {+ Ok(stream) => stream,+ Err(e) => {+ error!("Failed to begin stream: {}", e);+ tm.forget_model_if_engine_dropped(model_id, &e.to_string());+ return None;+ }+ };++ tm.stream_active.store(true, Ordering::Release);+ info!(+ "Live streaming transcription started (model '{}', backend '{}')",+ model_id, info.backend+ );+ Some(Self {+ tm,+ model_id: model_id.to_string(),+ stream,+ perf,+ want_language: matches!(output_language, OutputLanguageEvidence::Unknown),+ })+ }++ fn feed(&mut self, pcm: Vec<f32>) {+ lock_perf(&self.perf).record_feed(pcm.len());+ self.stream.feed(pcm);+ }++ fn finish(self) -> Result<Option<Decoded>, EngineError> {+ let result = match self.stream.finalize(self.want_language) {+ // After finalize the committed prefix holds the full text;+ // display() = committed + tentative is the safe read.+ Ok(Some(finalized)) => {+ let mut perf = lock_perf(&self.perf);+ perf.record_compute(finalized.elapsed);+ perf.record_update(&finalized.update);+ if finalized.text.full.trim().is_empty() {+ // A stream can miss what a run over the whole recording+ // finds, so empty text retries in batch.+ info!("Live transcription produced no text; retrying in batch");+ Ok(None)+ } else {+ Ok(Some((finalized.text.full, finalized.language)))+ }+ }+ Ok(None) => {+ error!(+ "Live transcription could not be finalized; \+ falling back to batch transcription"+ );+ self.tm.forget_model_if_engine_dropped(+ &self.model_id,+ "live transcription failed and the model could not be restarted",+ );+ Ok(None)+ }+ Err(e) => {+ info!("Live transcription finalize stopped: {}", e);+ Err(e)+ }+ };+ let chars = match &result {+ Ok(Some((text, _))) => text.len(),+ _ => 0,+ };+ lock_perf(&self.perf).log_finalized(chars);+ result+ }+}diff --git a/src-tauri/src/managers/transcription/policy.rs b/src-tauri/src/managers/transcription/policy.rsnew file mode 100644index 00000000..82bd83ee--- /dev/null+++ b/src-tauri/src/managers/transcription/policy.rs@@ -0,0 +1,69 @@+//! How audio is decoded: the one place the model's traits and the settings+//! choose between streaming, long-form chunking and a single run.++use crate::engine_supervisor::LoadedInfo;++/// What a model can do that decides how its audio is decoded.+#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub struct ModelTraits {+ /// Decodes audio incrementally as it is recorded.+ pub streaming: bool,+ /// Keeps its accuracy on audio longer than its native window in one run+ /// (whisper today).+ pub long_form: bool,+}++impl ModelTraits {+ /// The loaded model's own traits: the authority once a model is loaded.+ pub fn of(info: &LoadedInfo) -> Self {+ Self {+ streaming: info.capabilities.supports_streaming,+ long_form: info.supports_long_form,+ }+ }+}++/// How a recording is decoded while it is still going.+#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub enum LiveDecode {+ /// The model decodes the audio as it arrives.+ Stream,+ /// Long audio is cut at pauses and each chunk decoded once its cut is+ /// certain.+ Chunked,+ /// Nothing until the recording stops; then it is decoded in batch.+ None,+}++/// How a whole clip is decoded: a recording without a live result, a+/// re-transcribed history entry, or a file from the CLI.+#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub enum BatchDecode {+ /// Cut at pauses into chunks of about 30 s, each decoded in turn; a clip+ /// of 40 s or less is one chunk.+ Chunked,+ /// One run.+ Whole,+}++/// Streaming wins: a streaming model handles long audio itself. Otherwise+/// long audio is chunked unless the model keeps its accuracy on it.+pub fn live_decode(model: ModelTraits, long_form_chunking: bool) -> LiveDecode {+ if model.streaming {+ LiveDecode::Stream+ } else if long_form_chunking && !model.long_form {+ LiveDecode::Chunked+ } else {+ LiveDecode::None+ }+}++/// As live, except that a streaming model takes a whole clip in one run, as+/// it did before long-form chunking existed.+pub fn batch_decode(model: ModelTraits, long_form_chunking: bool) -> BatchDecode {+ if long_form_chunking && !model.long_form && !model.streaming {+ BatchDecode::Chunked+ } else {+ BatchDecode::Whole+ }+}diff --git a/src-tauri/src/settings.rs b/src-tauri/src/settings.rsindex 8a478407..eff41ea7 100644--- a/src-tauri/src/settings.rs+++ b/src-tauri/src/settings.rs@@ -525,6 +525,11 @@ pub struct AppSettings {/// Experimental detector implementation. Silero remains the stable default.#[serde(default)]pub vad_backend: VadBackend,+ /// Decode long audio in chunks cut at pauses, for models whose accuracy+ /// degrades on long audio. Independent of `vad_enabled`, which only+ /// decides whether silence is dropped from recordings.+ #[serde(default = "default_long_form_chunking")]+ pub long_form_chunking: bool,/// Which recording overlay to show: None / Minimal / Live. Streaming mode is/// not gated on this — that follows model capability. Migrated from the old/// `overlay_position` (position `none` → style `None`).@@ -597,6 +602,10 @@ fn default_vad_enabled() -> bool {true}+fn default_long_form_chunking() -> bool {+ true+}+fn default_filler_word_removal_enabled() -> bool {true}@@ -1011,6 +1020,7 @@ pub fn get_default_settings() -> AppSettings {extra_recording_buffer_ms: 0,vad_enabled: default_vad_enabled(),vad_backend: VadBackend::default(),+ long_form_chunking: default_long_form_chunking(),overlay_style: default_overlay_style(),}}@@ -1455,6 +1465,10 @@ mod tests {assert_eq!(settings.sound_theme, SoundTheme::Pop);assert!(settings.filler_word_removal_enabled);assert_eq!(settings.vad_backend, VadBackend::Silero);+ assert!(+ settings.long_form_chunking,+ "chunking is on for settings saved before it existed"+ );// The 0.1 integer device index is cleared once for transcribe.cpp 0.2.// Without an exact device, the retired generic GPU choice becomes Auto.diff --git a/src-tauri/src/shortcut/mod.rs b/src-tauri/src/shortcut/mod.rsindex c4626399..d4a8e00d 100644--- a/src-tauri/src/shortcut/mod.rs+++ b/src-tauri/src/shortcut/mod.rs@@ -1359,6 +1359,15 @@ pub fn change_vad_enabled_setting(app: AppHandle, enabled: bool) -> Result<(), SOk(())}+#[tauri::command]+#[specta::specta]+pub fn change_long_form_chunking_setting(app: AppHandle, enabled: bool) -> Result<(), String> {+ let mut settings = settings::get_settings(&app);+ settings.long_form_chunking = enabled;+ settings::write_settings(&app, settings);+ Ok(())+}+#[tauri::command]#[specta::specta]pub async fn change_vad_backend_setting(app: AppHandle, backend: VadBackend) -> Result<(), String> {diff --git a/src/bindings.ts b/src/bindings.tsindex 7c2f32b6..708c0b63 100644--- a/src/bindings.ts+++ b/src/bindings.ts@@ -375,6 +375,14 @@ async changeVadEnabledSetting(enabled: boolean) : Promise<Result<null, string>>else return { status: "error", error: e as any };}},+async changeLongFormChunkingSetting(enabled: boolean) : Promise<Result<null, string>> {+ try {+ return { status: "ok", data: await TAURI_INVOKE("change_long_form_chunking_setting", { enabled }) };+} catch (e) {+ if(e instanceof Error) throw e;+ else return { status: "error", error: e as any };+}+},async changeVadBackendSetting(backend: VadBackend) : Promise<Result<null, string>> {try {return { status: "ok", data: await TAURI_INVOKE("change_vad_backend_setting", { backend }) };@@ -1023,6 +1031,12 @@ transcribe_gpu_device?: string | null; extra_recording_buffer_ms?: number; vad_e* Experimental detector implementation. Silero remains the stable default.*/vad_backend?: VadBackend;+/**+ * Decode long audio in chunks cut at pauses, for models whose accuracy+ * degrades on long audio. Independent of `vad_enabled`, which only+ * decides whether silence is dropped from recordings.+ */+long_form_chunking?: boolean;/*** Which recording overlay to show: None / Minimal / Live. Streaming mode is* not gated on this — that follows model capability. Migrated from the olddiff --git a/src/components/settings/LongFormChunking.tsx b/src/components/settings/LongFormChunking.tsxnew file mode 100644index 00000000..76af5c0c--- /dev/null+++ b/src/components/settings/LongFormChunking.tsx@@ -0,0 +1,30 @@+import React from "react";+import { useTranslation } from "react-i18next";+import { ToggleSwitch } from "../ui/ToggleSwitch";+import { useSettings } from "../../hooks/useSettings";++interface LongFormChunkingProps {+ descriptionMode?: "tooltip" | "inline";+ grouped?: boolean;+}++export const LongFormChunking: React.FC<LongFormChunkingProps> = ({+ descriptionMode = "tooltip",+ grouped = false,+}) => {+ const { t } = useTranslation();+ const { getSetting, updateSetting, isUpdating } = useSettings();+ const enabled = getSetting("long_form_chunking") ?? true;++ return (+ <ToggleSwitch+ checked={enabled}+ onChange={(enabled) => updateSetting("long_form_chunking", enabled)}+ isUpdating={isUpdating("long_form_chunking")}+ label={t("settings.advanced.longFormChunking.title")}+ description={t("settings.advanced.longFormChunking.description")}+ descriptionMode={descriptionMode}+ grouped={grouped}+ />+ );+};diff --git a/src/components/settings/advanced/AdvancedSettings.tsx b/src/components/settings/advanced/AdvancedSettings.tsxindex 69ec7148..95aa9ef0 100644--- a/src/components/settings/advanced/AdvancedSettings.tsx+++ b/src/components/settings/advanced/AdvancedSettings.tsx@@ -19,6 +19,7 @@ import { ExperimentalToggle } from "../ExperimentalToggle";import { useSettings } from "../../../hooks/useSettings";import { KeyboardImplementationSelector } from "../debug/KeyboardImplementationSelector";import { VoiceActivityDetection } from "../VoiceActivityDetection";+import { LongFormChunking } from "../LongFormChunking";import { AccelerationSelector } from "../AccelerationSelector";import { LazyStreamClose } from "../LazyStreamClose";import { FillerWordRemoval } from "../FillerWordRemoval";@@ -50,6 +51,7 @@ export const AdvancedSettings: React.FC = () => {<SettingsGroup title={t("settings.advanced.groups.transcription")}><VoiceActivityDetection descriptionMode="tooltip" grouped={true} />+ <LongFormChunking descriptionMode="tooltip" grouped={true} /><FillerWordRemoval descriptionMode="tooltip" grouped={true} /><ChineseScriptSetting descriptionMode="tooltip" grouped={true} /><CustomWords descriptionMode="tooltip" grouped />diff --git a/src/i18n/locales/ar/translation.json b/src/i18n/locales/ar/translation.jsonindex c46253d8..a3fa8d0a 100644--- a/src/i18n/locales/ar/translation.json+++ b/src/i18n/locales/ar/translation.json@@ -325,6 +325,10 @@"title": "كشف النشاط الصوتي","description": "تصفية الصمت من التسجيلات. النماذج التي تدعم البث تستخدم فترة VAD أطول؛ وتعطيل VAD يسجّل الصوت الخام."},+ "longFormChunking": {+ "title": "تقسيم الصوت الطويل",+ "description": "يقسّم التسجيلات الطويلة عند فترات التوقف قبل نسخها، مما يحسّن أداء النسخ عادةً."+ },"vadBackend": {"title": "محرك كشف النشاط الصوتي","description": "اختر الكاشف المستخدم لتصفية الصمت. Earshot تجريبي؛ ارجع إلى Silero إذا تم اقتطاع الكلام أو الاحتفاظ بالضوضاء.",diff --git a/src/i18n/locales/bg/translation.json b/src/i18n/locales/bg/translation.jsonindex 86cdf969..ec5a987a 100644--- a/src/i18n/locales/bg/translation.json+++ b/src/i18n/locales/bg/translation.json@@ -355,6 +355,10 @@"title": "Откриване на гласова активност","description": "Филтрира тишината от записите. Моделите с поддръжка на стрийминг използват по-дълга опашка на VAD; изключването на VAD записва необработен звук."},+ "longFormChunking": {+ "title": "Разделяне на дълго аудио",+ "description": "Разделя дългите записи на паузите преди транскрибиране, което обикновено подобрява качеството на транскрипцията."+ },"vadBackend": {"title": "Бекенд за откриване на глас","description": "Изберете детектора, използван за филтриране на тишината. Earshot е експериментален; върнете се към Silero, ако речта се отрязва или шумът се запазва.",diff --git a/src/i18n/locales/ca/translation.json b/src/i18n/locales/ca/translation.jsonindex 9e963a7e..bcd5b369 100644--- a/src/i18n/locales/ca/translation.json+++ b/src/i18n/locales/ca/translation.json@@ -355,6 +355,10 @@"title": "Detecció d'activitat de veu","description": "Filtra els silencis dels enregistraments. Els models compatibles amb la transcripció en temps real utilitzen un marge final de detecció de veu més llarg; si desactiveu la detecció, s'enregistra l'àudio sense filtrar."},+ "longFormChunking": {+ "title": "Divisió d'àudio llarg",+ "description": "Divideix els enregistraments llargs a les pauses abans de transcriure'ls, cosa que normalment millora el rendiment de la transcripció."+ },"vadBackend": {"title": "Motor de detecció d'activitat de veu","description": "Trieu el detector utilitzat per filtrar els silencis. L'Earshot és experimental; torneu al Silero si es corta la parla o es conserva el soroll.",diff --git a/src/i18n/locales/cs/translation.json b/src/i18n/locales/cs/translation.jsonindex d6c15e04..066502a6 100644--- a/src/i18n/locales/cs/translation.json+++ b/src/i18n/locales/cs/translation.json@@ -355,6 +355,10 @@"title": "Detekce hlasové aktivity","description": "Odfiltruje ticho z nahrávek. Modely podporující streaming používají delší doběh VAD; vypnutí VAD nahrává surový zvuk."},+ "longFormChunking": {+ "title": "Dělení dlouhého zvuku",+ "description": "Před přepisem rozdělí dlouhé nahrávky v pauzách, což obvykle zlepšuje kvalitu přepisu."+ },"vadBackend": {"title": "Backend detekce hlasu","description": "Vyberte detektor používaný k filtrování ticha. Earshot je experimentální; pokud je řeč ořezávána nebo zůstává šum, přepněte zpět na Silero.",diff --git a/src/i18n/locales/da/translation.json b/src/i18n/locales/da/translation.jsonindex a5b135f3..f95e825e 100644--- a/src/i18n/locales/da/translation.json+++ b/src/i18n/locales/da/translation.json@@ -355,6 +355,10 @@"title": "Stemmeaktivitetsdetektion","description": "Filtrer stilhed fra optagelser. Streaming-modeller bruger en længere VAD-hale; deaktivering af VAD optager rå lyd."},+ "longFormChunking": {+ "title": "Opdeling af lang lyd",+ "description": "Deler lange optagelser ved pauser før transskribering, hvilket typisk forbedrer transskriptionen."+ },"vadBackend": {"title": "Stemmedetektions-backend","description": "Vælg den detektor, der bruges til at filtrere stilhed. Earshot er eksperimentel; skift tilbage til Silero, hvis tale klippes eller støj bevares.",diff --git a/src/i18n/locales/de/translation.json b/src/i18n/locales/de/translation.jsonindex 95285787..d8a00182 100644--- a/src/i18n/locales/de/translation.json+++ b/src/i18n/locales/de/translation.json@@ -355,6 +355,10 @@"title": "Sprachaktivitätserkennung (VAD)","description": "Filtert Stille aus Aufnahmen. Streamingfähige Modelle verwenden ein längeres VAD-Ende; das Deaktivieren von VAD nimmt Rohaudio auf."},+ "longFormChunking": {+ "title": "Lange Aufnahmen aufteilen",+ "description": "Teilt lange Aufnahmen vor der Transkription an Sprechpausen auf, was die Transkription in der Regel verbessert."+ },"vadBackend": {"title": "VAD-Backend","description": "Wählt den Detektor zum Herausfiltern von Stille. Earshot ist experimentell; wechseln Sie zurück zu Silero, wenn Sprache abgeschnitten oder Rauschen beibehalten wird.",diff --git a/src/i18n/locales/en/translation.json b/src/i18n/locales/en/translation.jsonindex d1f5cfb8..357dc25a 100644--- a/src/i18n/locales/en/translation.json+++ b/src/i18n/locales/en/translation.json@@ -355,6 +355,10 @@"title": "Voice Activity Detection","description": "Filter silence from recordings. Streaming-capable models use a longer VAD tail; disabling VAD records raw audio."},+ "longFormChunking": {+ "title": "Long Audio Chunking",+ "description": "Splits long recordings at pauses before transcribing, which typically improves transcription performance."+ },"vadBackend": {"title": "Voice Activity Backend","description": "Choose the detector used to filter silence. Earshot is experimental; switch back to Silero if speech is clipped or noise is retained.",diff --git a/src/i18n/locales/es/translation.json b/src/i18n/locales/es/translation.jsonindex 4086da6e..c696ed25 100644--- a/src/i18n/locales/es/translation.json+++ b/src/i18n/locales/es/translation.json@@ -355,6 +355,10 @@"title": "Detección de actividad de voz","description": "Filtra el silencio de las grabaciones. Los modelos compatibles con streaming usan una cola de VAD más larga; desactivar el VAD graba audio sin procesar."},+ "longFormChunking": {+ "title": "División de audio largo",+ "description": "Divide las grabaciones largas en las pausas antes de transcribirlas, lo que normalmente mejora el rendimiento de la transcripción."+ },"vadBackend": {"title": "Backend de detección de voz","description": "Elige el detector usado para filtrar el silencio. Earshot es experimental; vuelve a Silero si la voz se recorta o se conserva el ruido.",diff --git a/src/i18n/locales/fr/translation.json b/src/i18n/locales/fr/translation.jsonindex de8ed36b..2d73bb5c 100644--- a/src/i18n/locales/fr/translation.json+++ b/src/i18n/locales/fr/translation.json@@ -355,6 +355,10 @@"title": "Détection d'activité vocale (VAD)","description": "Filtre le silence des enregistrements. Les modèles compatibles avec le streaming utilisent une marge de détection VAD plus longue ; désactiver le VAD enregistre l'audio brut."},+ "longFormChunking": {+ "title": "Découpage des longs enregistrements",+ "description": "Découpe les longs enregistrements aux pauses avant la transcription, ce qui améliore généralement la qualité de la transcription."+ },"vadBackend": {"title": "Backend de détection vocale","description": "Choisissez le détecteur utilisé pour filtrer le silence. Earshot est expérimental ; revenez à Silero si la parole est coupée ou si le bruit est conservé.",diff --git a/src/i18n/locales/he/translation.json b/src/i18n/locales/he/translation.jsonindex f50ca803..261a8949 100644--- a/src/i18n/locales/he/translation.json+++ b/src/i18n/locales/he/translation.json@@ -355,6 +355,10 @@"title": "זיהוי פעילות קולית","description": "סינון שקט מההקלטות. מודלים התומכים בסטרימינג משתמשים בזנב VAD ארוך יותר; ביטול VAD מקליט אודיו גולמי."},+ "longFormChunking": {+ "title": "פיצול שמע ארוך",+ "description": "מפצל הקלטות ארוכות בהפסקות לפני התמלול, מה שבדרך כלל משפר את איכות התמלול."+ },"vadBackend": {"title": "מנוע זיהוי פעילות קולית","description": "בחרו את הגלאי המשמש לסינון שקט. Earshot ניסיוני; חזרו ל-Silero אם הדיבור נחתך או שרעש נשמר.",diff --git a/src/i18n/locales/hi/translation.json b/src/i18n/locales/hi/translation.jsonindex 5c25d398..09251fc4 100644--- a/src/i18n/locales/hi/translation.json+++ b/src/i18n/locales/hi/translation.json@@ -355,6 +355,10 @@"title": "वॉइस एक्टिविटी डिटेक्शन","description": "रिकॉर्डिंग से बिना आवाज़ वाले हिस्से हटाएं. स्ट्रीमिंग वाले मॉडल लंबी VAD टेल इस्तेमाल करते हैं; VAD बंद करने पर कच्चा ऑडियो रिकॉर्ड होता है."},+ "longFormChunking": {+ "title": "लंबे ऑडियो का विभाजन",+ "description": "ट्रांसक्राइब करने से पहले लंबी रिकॉर्डिंग को विरामों पर विभाजित करता है, जिससे आमतौर पर ट्रांसक्रिप्शन बेहतर होता है।"+ },"vadBackend": {"title": "वॉइस एक्टिविटी बैकएंड","description": "मौन को फ़िल्टर करने के लिए उपयोग किया जाने वाला डिटेक्टर चुनें। Earshot प्रायोगिक है; यदि बोली कट रही हो या शोर बना रहे तो Silero पर वापस जाएँ।",diff --git a/src/i18n/locales/id/translation.json b/src/i18n/locales/id/translation.jsonindex b7d9d757..147e9100 100644--- a/src/i18n/locales/id/translation.json+++ b/src/i18n/locales/id/translation.json@@ -355,6 +355,10 @@"title": "Deteksi Aktivitas Suara","description": "Saring keheningan dari rekaman. Model yang mendukung streaming menggunakan ekor VAD yang lebih panjang; menonaktifkan VAD akan merekam audio mentah."},+ "longFormChunking": {+ "title": "Pemisahan Audio Panjang",+ "description": "Memisahkan rekaman panjang pada jeda sebelum ditranskripsi, yang biasanya meningkatkan kinerja transkripsi."+ },"vadBackend": {"title": "Backend Deteksi Aktivitas Suara","description": "Pilih detektor yang digunakan untuk menyaring keheningan. Earshot bersifat eksperimental; kembali ke Silero jika ucapan terpotong atau kebisingan tetap terekam.",diff --git a/src/i18n/locales/it/translation.json b/src/i18n/locales/it/translation.jsonindex c8a8ae6c..05b4effc 100644--- a/src/i18n/locales/it/translation.json+++ b/src/i18n/locales/it/translation.json@@ -355,6 +355,10 @@"title": "Rilevamento dell'attività vocale","description": "Filtra il silenzio dalle registrazioni. I modelli che supportano lo streaming utilizzano una coda VAD più lunga; disabilitando la VAD si registra l'audio grezzo."},+ "longFormChunking": {+ "title": "Suddivisione dell'audio lungo",+ "description": "Suddivide le registrazioni lunghe in corrispondenza delle pause prima della trascrizione, il che di solito migliora le prestazioni della trascrizione."+ },"vadBackend": {"title": "Backend di rilevamento vocale","description": "Scegli il rilevatore usato per filtrare il silenzio. Earshot è sperimentale; torna a Silero se il parlato viene tagliato o il rumore viene mantenuto.",diff --git a/src/i18n/locales/ja/translation.json b/src/i18n/locales/ja/translation.jsonindex cd293dce..ff3111d1 100644--- a/src/i18n/locales/ja/translation.json+++ b/src/i18n/locales/ja/translation.json@@ -355,6 +355,10 @@"title": "音声区間検出 (VAD)","description": "録音から無音部分を除去します。ストリーミング対応モデルはより長い音声検出区間を使用します。VAD を無効にすると音声をそのまま録音します。"},+ "longFormChunking": {+ "title": "長い音声の分割",+ "description": "文字起こしの前に長い録音を無音部分で分割します。通常、文字起こしの精度が向上します。"+ },"vadBackend": {"title": "音声区間検出バックエンド","description": "無音のフィルタリングに使用する検出器を選択します。Earshot は実験的機能です。音声が途切れたりノイズが残る場合は Silero に戻してください。",diff --git a/src/i18n/locales/ko/translation.json b/src/i18n/locales/ko/translation.jsonindex ef1b72c5..af13fe25 100644--- a/src/i18n/locales/ko/translation.json+++ b/src/i18n/locales/ko/translation.json@@ -355,6 +355,10 @@"title": "음성 활동 감지","description": "녹음에서 무음 구간을 걸러냅니다. 스트리밍 지원 모델은 더 긴 VAD 테일을 사용하며, VAD를 끄면 원본 오디오가 그대로 녹음됩니다."},+ "longFormChunking": {+ "title": "긴 오디오 분할",+ "description": "전사하기 전에 긴 녹음을 쉬는 구간에서 나눕니다. 일반적으로 전사 성능이 향상됩니다."+ },"vadBackend": {"title": "음성 활동 감지 백엔드","description": "무음을 필터링하는 데 사용할 감지기를 선택합니다. Earshot은 실험적 기능입니다. 음성이 잘리거나 소음이 남는 경우 Silero로 되돌리세요.",diff --git a/src/i18n/locales/ne/translation.json b/src/i18n/locales/ne/translation.jsonindex 6d78fa63..b7534ceb 100644--- a/src/i18n/locales/ne/translation.json+++ b/src/i18n/locales/ne/translation.json@@ -355,6 +355,10 @@"title": "भ्वाइस एक्टिभिटी डिटेक्सन","description": "रेकर्डिङबाट मौनता हटाउँछ। स्ट्रिमिङ-सक्षम मोडेलहरूले लामो VAD टेल प्रयोग गर्छन्; VAD अफ गर्दा कच्चा अडियो रेकर्ड हुन्छ।"},+ "longFormChunking": {+ "title": "लामो अडियो विभाजन",+ "description": "ट्रान्सक्राइब गर्नुअघि लामो रेकर्डिङलाई विरामहरूमा विभाजन गर्छ, जसले सामान्यतया ट्रान्सक्रिप्सन सुधार गर्छ।"+ },"vadBackend": {"title": "भ्वाइस एक्टिभिटी ब्याकएन्ड","description": "मौनता फिल्टर गर्न प्रयोग हुने डिटेक्टर छान्नुहोस्। Earshot प्रयोगात्मक हो; बोली काटिएमा वा आवाज रहिरहेमा Silero मा फर्कनुहोस्।",diff --git a/src/i18n/locales/nl/translation.json b/src/i18n/locales/nl/translation.jsonindex 7f7e00fb..e3a6e592 100644--- a/src/i18n/locales/nl/translation.json+++ b/src/i18n/locales/nl/translation.json@@ -355,6 +355,10 @@"title": "Spraakactiviteitsdetectie (VAD)","description": "Filter stilte uit opnames. Streaming-modellen gebruiken een langere VAD-uitloop; het uitschakelen van VAD neemt de ruwe audio op."},+ "longFormChunking": {+ "title": "Lange audio opsplitsen",+ "description": "Splitst lange opnames bij pauzes op voordat ze worden getranscribeerd, wat de transcriptie doorgaans verbetert."+ },"vadBackend": {"title": "VAD-backend","description": "Kies de detector die stilte filtert. Earshot is experimenteel; schakel terug naar Silero als spraak wordt afgekapt of ruis behouden blijft.",diff --git a/src/i18n/locales/pl/translation.json b/src/i18n/locales/pl/translation.jsonindex 1a5a8ba9..61513e54 100644--- a/src/i18n/locales/pl/translation.json+++ b/src/i18n/locales/pl/translation.json@@ -355,6 +355,10 @@"title": "Wykrywanie aktywności głosowej","description": "Odfiltrowuje ciszę z nagrań. Modele obsługujące streaming używają dłuższego marginesu VAD; wyłączenie VAD nagrywa surowy dźwięk."},+ "longFormChunking": {+ "title": "Dzielenie długich nagrań",+ "description": "Dzieli długie nagrania w miejscach pauz przed transkrypcją, co zwykle poprawia jakość transkrypcji."+ },"vadBackend": {"title": "Backend wykrywania głosu","description": "Wybierz detektor używany do filtrowania ciszy. Earshot jest eksperymentalny; wróć do Silero, jeśli mowa jest ucinana lub szum jest zachowywany.",diff --git a/src/i18n/locales/pt/translation.json b/src/i18n/locales/pt/translation.jsonindex 5b14cee5..ce3a1c3a 100644--- a/src/i18n/locales/pt/translation.json+++ b/src/i18n/locales/pt/translation.json@@ -355,6 +355,10 @@"title": "Detecção de atividade de voz","description": "Filtra o silêncio das gravações. Modelos com suporte a streaming usam uma cauda de VAD mais longa; desativar o VAD grava o áudio bruto."},+ "longFormChunking": {+ "title": "Divisão de áudio longo",+ "description": "Divide gravações longas nas pausas antes da transcrição, o que normalmente melhora o desempenho da transcrição."+ },"vadBackend": {"title": "Backend de detecção de voz","description": "Escolha o detector usado para filtrar o silêncio. O Earshot é experimental; volte para o Silero se a fala for cortada ou o ruído for mantido.",diff --git a/src/i18n/locales/ru/translation.json b/src/i18n/locales/ru/translation.jsonindex 49dc72de..daba401a 100644--- a/src/i18n/locales/ru/translation.json+++ b/src/i18n/locales/ru/translation.json@@ -355,6 +355,10 @@"title": "Определение голосовой активности","description": "Отфильтровывает тишину из записей. Модели с поддержкой потоковой обработки используют более длинный «хвост» VAD; отключение VAD записывает необработанный звук."},+ "longFormChunking": {+ "title": "Разделение длинного аудио",+ "description": "Разделяет длинные записи по паузам перед распознаванием, что обычно улучшает качество распознавания."+ },"vadBackend": {"title": "Движок определения голоса","description": "Выберите детектор для фильтрации тишины. Earshot — экспериментальный; вернитесь к Silero, если речь обрезается или шум сохраняется.",diff --git a/src/i18n/locales/sv/translation.json b/src/i18n/locales/sv/translation.jsonindex 5513cc92..160db42f 100644--- a/src/i18n/locales/sv/translation.json+++ b/src/i18n/locales/sv/translation.json@@ -355,6 +355,10 @@"title": "Röstaktivitetsdetektering","description": "Filtrerar bort tystnad från inspelningar. Modeller med streamingstöd använder en längre VAD-svans; om VAD inaktiveras spelas rått ljud in."},+ "longFormChunking": {+ "title": "Dela upp långt ljud",+ "description": "Delar upp långa inspelningar vid pauser före transkribering, vilket oftast förbättrar transkriberingen."+ },"vadBackend": {"title": "Backend för röstdetektering","description": "Välj detektorn som används för att filtrera bort tystnad. Earshot är experimentell; byt tillbaka till Silero om tal klipps eller brus behålls.",diff --git a/src/i18n/locales/tr/translation.json b/src/i18n/locales/tr/translation.jsonindex 584655ab..ade7d2cb 100644--- a/src/i18n/locales/tr/translation.json+++ b/src/i18n/locales/tr/translation.json@@ -355,6 +355,10 @@"title": "Ses Etkinliği Algılama","description": "Kayıtlardaki sessizliği filtreler. Akış destekli modeller daha uzun bir VAD kuyruğu kullanır; VAD devre dışı bırakıldığında ham ses kaydedilir."},+ "longFormChunking": {+ "title": "Uzun Sesi Bölme",+ "description": "Uzun kayıtları yazıya dökmeden önce duraklamalardan böler; bu genellikle yazıya dökme performansını artırır."+ },"vadBackend": {"title": "Ses Etkinliği Arka Ucu","description": "Sessizliği filtrelemek için kullanılan algılayıcıyı seçin. Earshot deneyseldir; konuşma kesiliyorsa veya gürültü korunuyorsa Silero'ya geri dönün.",diff --git a/src/i18n/locales/uk/translation.json b/src/i18n/locales/uk/translation.jsonindex d406199f..890e9f19 100644--- a/src/i18n/locales/uk/translation.json+++ b/src/i18n/locales/uk/translation.json@@ -355,6 +355,10 @@"title": "Виявлення голосової активності","description": "Відфільтровує тишу із записів. Моделі з підтримкою потокової обробки використовують довший «хвіст» VAD; вимкнення VAD записує необроблений звук."},+ "longFormChunking": {+ "title": "Поділ довгого аудіо",+ "description": "Розділяє довгі записи за паузами перед розпізнаванням, що зазвичай покращує якість розпізнавання."+ },"vadBackend": {"title": "Рушій виявлення голосу","description": "Виберіть детектор для фільтрації тиші. Earshot — експериментальний; поверніться до Silero, якщо мовлення обрізається або шум зберігається.",diff --git a/src/i18n/locales/vi/translation.json b/src/i18n/locales/vi/translation.jsonindex 72538b11..bc1d65fc 100644--- a/src/i18n/locales/vi/translation.json+++ b/src/i18n/locales/vi/translation.json@@ -355,6 +355,10 @@"title": "Phát hiện hoạt động giọng nói","description": "Lọc bỏ khoảng lặng khỏi bản ghi. Các mô hình hỗ trợ truyền trực tiếp dùng đuôi VAD dài hơn; tắt VAD sẽ ghi âm thanh thô."},+ "longFormChunking": {+ "title": "Chia nhỏ âm thanh dài",+ "description": "Chia các bản ghi dài tại các khoảng ngắt trước khi chuyển thành văn bản, điều này thường cải thiện hiệu quả chuyển văn bản."+ },"vadBackend": {"title": "Bộ phát hiện giọng nói","description": "Chọn bộ phát hiện dùng để lọc khoảng lặng. Earshot đang thử nghiệm; hãy chuyển về Silero nếu giọng nói bị cắt hoặc tiếng ồn vẫn còn.",diff --git a/src/i18n/locales/zh-TW/translation.json b/src/i18n/locales/zh-TW/translation.jsonindex 54503e36..86aaa68b 100644--- a/src/i18n/locales/zh-TW/translation.json+++ b/src/i18n/locales/zh-TW/translation.json@@ -355,6 +355,10 @@"title": "語音活動偵測","description": "過濾錄音中的靜音。支援串流的模型會使用較長的 VAD 尾段;停用 VAD 則會錄製原始音訊"},+ "longFormChunking": {+ "title": "長音訊分段",+ "description": "在轉錄前依停頓處將長錄音分段,通常可以提升轉錄效果。"+ },"vadBackend": {"title": "語音活動偵測後端","description": "選擇用於過濾靜音的偵測器。Earshot 為實驗性功能;若語音被截斷或雜訊被保留,請切換回 Silero。",diff --git a/src/i18n/locales/zh/translation.json b/src/i18n/locales/zh/translation.jsonindex 4982f4fe..98a15630 100644--- a/src/i18n/locales/zh/translation.json+++ b/src/i18n/locales/zh/translation.json@@ -355,6 +355,10 @@"title": "语音活动检测","description": "过滤录音中的静音。支持流式的模型会使用更长的 VAD 尾段;停用 VAD 则会录制原始音频。"},+ "longFormChunking": {+ "title": "长音频分段",+ "description": "在转录前按停顿处将长录音分段,通常可以提升转录效果。"+ },"vadBackend": {"title": "语音活动检测后端","description": "选择用于过滤静音的检测器。Earshot 为实验性功能;如果语音被截断或噪音被保留,请切换回 Silero。",diff --git a/src/stores/settingsStore.ts b/src/stores/settingsStore.tsindex ece9bdf1..3bc9e779 100644--- a/src/stores/settingsStore.ts+++ b/src/stores/settingsStore.ts@@ -176,6 +176,8 @@ const settingUpdaters: {commands.changeLazyStreamCloseSetting(value as boolean),overlay_style: (value) => commands.changeOverlayStyleSetting(value as string),vad_enabled: (value) => commands.changeVadEnabledSetting(value as boolean),+ long_form_chunking: (value) =>+ commands.changeLongFormChunkingSetting(value as boolean),vad_backend: async (value) => {const result = await commands.changeVadBackendSetting(value as VadBackend);if (result.status === "error") {