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:
4 files changed
+43
-6
No files matched your search
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user