Skip to content

armgit / Handy

Public Imported from GitHub
0

A free, open source, and extensible speech-to-text application that works completely offline.

fix: reset resampler state between recordings to prevent audio crosstalk (#1344)

Andrew Leecheb9301e01c8f
diff --git a/src-tauri/src/audio_toolkit/audio/recorder.rs b/src-tauri/src/audio_toolkit/audio/recorder.rs
index 4870fd34..82297cb6 100644
--- a/src-tauri/src/audio_toolkit/audio/recorder.rs
+++ b/src-tauri/src/audio_toolkit/audio/recorder.rs
@@ -617,6 +617,7 @@ fn run_consumer(
processed_samples.clear();
recording = true;
visualizer.reset();
+ frame_resampler.reset();
// Reconfigure the single VAD engine for this session's policy
// and clear its smoothing + recurrent state before it sees
// any frames.
diff --git a/src-tauri/src/audio_toolkit/audio/resampler.rs b/src-tauri/src/audio_toolkit/audio/resampler.rs
index cfb4ad51..94538d9d 100644
--- a/src-tauri/src/audio_toolkit/audio/resampler.rs
+++ b/src-tauri/src/audio_toolkit/audio/resampler.rs
@@ -87,6 +87,18 @@ impl FrameResampler {
}
}
+ /// Clear all internal buffers so the next `push()` starts from a clean state.
+ ///
+ /// Call this between recordings to prevent stale audio from the previous
+ /// session leaking into the start of the next one via the FFT overlap buffers.
+ pub fn reset(&mut self) {
+ self.in_buf.clear();
+ self.pending.clear();
+ if let Some(ref mut resampler) = self.resampler {
+ resampler.reset();
+ }
+ }
+
fn emit_frames(&mut self, mut data: &[f32], emit: &mut impl FnMut(&[f32])) {
while !data.is_empty() {
let space = self.frame_samples - self.pending.len();
@@ -106,6 +118,130 @@ impl FrameResampler {
mod tests {
use super::*;
+ /// Generate a 1kHz sine wave at the given sample rate and duration.
+ fn sine_wave(sample_rate: usize, freq: f64, duration_secs: f64) -> Vec<f32> {
+ let n = (sample_rate as f64 * duration_secs) as usize;
+ (0..n)
+ .map(|i| {
+ (2.0 * std::f64::consts::PI * freq * i as f64 / sample_rate as f64).sin() as f32
+ })
+ .collect()
+ }
+
+ fn collect_output(resampler: &mut FrameResampler, input: &[f32]) -> Vec<f32> {
+ let mut out = Vec::new();
+ resampler.push(input, |frame| out.extend_from_slice(frame));
+ out
+ }
+
+ #[test]
+ fn reset_clears_in_buf_and_pending() {
+ let mut r = FrameResampler::new(48000, 16000, Duration::from_millis(30));
+
+ // Push less than one chunk (1024 samples) to leave data in in_buf
+ let partial = vec![0.5f32; 500];
+ let _ = collect_output(&mut r, &partial);
+
+ r.reset();
+
+ // Now push silence — should get only silence out, no remnants of 0.5
+ let silence = vec![0.0f32; 4096];
+ let out = collect_output(&mut r, &silence);
+
+ let max_abs = out.iter().map(|s| s.abs()).fold(0.0f32, f32::max);
+ assert!(
+ max_abs < 0.01,
+ "After reset, silence input should produce near-silence output, got max_abs={}",
+ max_abs
+ );
+ }
+
+ #[test]
+ fn reset_clears_fft_overlap_buffers() {
+ let mut r = FrameResampler::new(48000, 16000, Duration::from_millis(30));
+
+ // Push a loud 1kHz sine wave through the resampler (simulates recording 1)
+ let sine = sine_wave(48000, 1000.0, 0.5); // 500ms of audio
+ let _ = collect_output(&mut r, &sine);
+ r.finish(|_| {});
+
+ // Reset (simulates new recording starting)
+ r.reset();
+
+ // Push silence (simulates recording 2 starting with no speech)
+ let silence = vec![0.0f32; 4096];
+ let out = collect_output(&mut r, &silence);
+
+ // The output should be near-zero. If the FFT overlap buffers weren't
+ // cleared, the sine wave's tail would leak into this output.
+ let max_abs = out.iter().map(|s| s.abs()).fold(0.0f32, f32::max);
+ assert!(
+ max_abs < 0.01,
+ "FFT overlap should not leak after reset; got max_abs={} (expected near-zero)",
+ max_abs
+ );
+ }
+
+ #[test]
+ fn reset_between_recordings_no_crosstalk() {
+ let mut r = FrameResampler::new(48000, 16000, Duration::from_millis(30));
+
+ // Recording 1: ascending ramp (distinctive pattern)
+ let ramp: Vec<f32> = (0..48000).map(|i| i as f32 / 48000.0).collect(); // 1 second
+ let out1 = collect_output(&mut r, &ramp);
+ r.finish(|_| {});
+ assert!(!out1.is_empty(), "Recording 1 should produce output");
+
+ // Reset between recordings
+ r.reset();
+
+ // Recording 2: constant DC signal of -0.5
+ let dc = vec![-0.5f32; 48000]; // 1 second
+ let out2 = collect_output(&mut r, &dc);
+
+ // After the resampler settles (skip first frame which may have transient),
+ // all samples should be near -0.5, not contaminated by the ascending ramp.
+ if out2.len() > 480 {
+ // Skip first frame (480 samples at 16kHz/30ms), check the rest
+ let tail = &out2[480..];
+ for (i, &s) in tail.iter().enumerate() {
+ assert!(
+ (s - (-0.5)).abs() < 0.05,
+ "Recording 2 sample {} = {} (expected ~-0.5); ramp leaked through",
+ i + 480,
+ s
+ );
+ }
+ }
+ }
+
+ #[test]
+ fn reset_passthrough_mode_clears_pending() {
+ // When in_hz == out_hz, no rubato resampler is created (passthrough mode).
+ // Reset should still clear the pending frame buffer.
+ let mut r = FrameResampler::new(16000, 16000, Duration::from_millis(30));
+
+ // Push partial frame (less than 480 samples) to leave data in pending
+ let partial = vec![1.0f32; 200];
+ let _ = collect_output(&mut r, &partial);
+
+ r.reset();
+
+ // Push silence
+ let silence = vec![0.0f32; 960];
+ let out = collect_output(&mut r, &silence);
+
+ // First complete frame should be all zeros, not contain the 1.0 values
+ if !out.is_empty() {
+ let max_abs = out.iter().take(480).map(|s| s.abs()).fold(0.0f32, f32::max);
+ assert!(
+ max_abs < 0.001,
+ "Passthrough mode: pending buffer should be cleared after reset, got max_abs={}",
+ max_abs
+ );
+ }
+ }
+
#[test]
fn finish_does_not_leak_tail_into_next_session() {
// 48kHz -> 16kHz, 30ms frames (480 output samples per frame).