fix(frontend): keep silence out of the transcript

Cutting the live capture at a pause can hand over a piece made only of silence,
typically the end of a recording. Whisper does not return nothing on those: it
invents a line, which then ended up inside the question. A piece with no speech
in it is now dropped instead of transcribed, and so is a silent tail at the end
of the capture, so the words already transcribed are the whole question.
This commit is contained in:
BlackAngelTV committed 2026-10-06 20:26:53 +02:00
1 parent 20ee8c6699
commit 036a0d1ad7
4 files changed
+43 -6

No files matched your search

+9 -5
View File
@@ -746,10 +746,10 @@ impl JarviseApp {
let remaining = std::mem::take(&mut self.listening);
let remaining_seconds = remaining.len() as f32 / sample_rate.max(1) as f32;
// A sliver left at the end carries no word: transcribing it would only
// give Whisper a chance to invent one, so the pieces already done are
// the whole question.
if remaining_seconds < MIN_TAIL_SECONDS {
// A sliver left at the end carries no word, and a long silence carries
// none either: transcribing either would only give Whisper a chance to
// invent one, so the pieces already done are the whole question.
if remaining_seconds < MIN_TAIL_SECONDS || audio::is_silent(&remaining) {
self.finish_transcript(String::new());
return;
}
@@ -774,7 +774,11 @@ impl JarviseApp {
return;
};
let piece: Vec<f32> = self.listening.drain(..cut).collect();
self.voice.transcribe_part(piece, sample_rate);
// A piece cut at a pause may hold nothing but silence, and Whisper
// invents a line on those rather than returning nothing.
if !audio::is_silent(&piece) {
self.voice.transcribe_part(piece, sample_rate);
}
}
/// Ends the transcription: joins what the pieces already said with the last
+27
View File
@@ -131,6 +131,16 @@ pub fn rms(samples: &[f32]) -> f32 {
(sum / samples.len() as f32).sqrt()
}
/// Whether a whole piece carries no speech at all.
///
/// Cutting at a pause can hand over a piece made only of silence — the end of a
/// recording, or a silence that started right after a cut. Whisper does not
/// answer nothing on those: it invents a line (a subtitle credit, a thanks),
/// which would then land in the user's question.
pub fn is_silent(samples: &[f32]) -> bool {
rms(samples) < SILENCE_RMS
}
/// End of the last pause in `samples`, if the buffer holds one.
///
/// Cutting in a pause is what keeps a word whole: a cut in the middle of \"bonjour\"
@@ -441,6 +451,23 @@ mod tests {
assert_eq!(chunk_cut(&samples, RATE), None);
}
#[test]
fn a_piece_of_pure_silence_is_recognised() {
assert!(super::is_silent(&vec![0.0_f32; RATE as usize]));
// Microphone noise floor, well under any spoken level.
assert!(super::is_silent(&vec![0.0008_f32; RATE as usize]));
}
#[test]
fn a_piece_with_speech_in_it_is_not_silent() {
assert!(!super::is_silent(&vec![0.3_f32; RATE as usize / 10]));
// One short word followed by a long room silence must still be sent.
let mut piece = vec![0.2_f32; RATE as usize / 8];
piece.extend(std::iter::repeat_n(0.0, 2 * RATE as usize));
assert!(!super::is_silent(&piece), "the word would be lost");
}
#[test]
fn silence_is_cut_at_the_shorter_threshold() {
// A gap right after the minimum: no need to wait for the hard limit.