From 9d24360e523fc9c0b635de5a709bdd3f0171fd15 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 17 Nov 2026 07:11:15 -0800 Subject: [PATCH] audio-dsp: resampler frames past the position limit read at the limit A block that starts near MAX_RESAMPLE_POSITION and runs past it now reads every later frame at the limit's position, as the constant's docs say, instead of reading past the held source and returning silence. --- crates/audio-dsp/src/resample.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/crates/audio-dsp/src/resample.rs b/crates/audio-dsp/src/resample.rs index 42f594bd0..150eba11c 100644 --- a/crates/audio-dsp/src/resample.rs +++ b/crates/audio-dsp/src/resample.rs @@ -152,6 +152,8 @@ impl StreamResampler { let to = self.to as i64; let (step, rem_step) = ((self.from / self.to) as i64, (self.from % self.to) as i64); let (mut s, mut rem) = (first, first_rem); + // Frames past `end` (the position limit) stay at its position. + let advancing = (end - out) as usize; for i in 0..n { let frac = (rem as f64 / to as f64) as f32; // held starts at first - 1, so the earlier frame is always held @@ -163,6 +165,9 @@ impl StreamResampler { *x = a + (b - a) * frac; } } + if i >= advancing { + continue; + } s = s.wrapping_add(step); rem += rem_step; if rem >= to { @@ -379,6 +384,12 @@ mod tests { let out = r.process(pos, 8, |_, frames| vec![vec![0.25; frames]]); assert!(out[0].iter().all(|v| v.is_finite()), "{pos}"); } + // Frames of a block past the limit read at the limit. + for (from, to) in [(48_000, 48_000), (44_100, 48_000)] { + let mut r = StreamResampler::new(from, to); + let out = r.process(MAX_RESAMPLE_POSITION - 2, 8, |_, frames| vec![vec![0.5; frames]]); + assert!(out[0].iter().all(|&v| v == 0.5), "{from} -> {to}: {:?}", out[0]); + } // A pull that returns too many channels keeps the first `MAX_CHANNELS`. let mut r = StreamResampler::new(48_000, 48_000); let out = r.process(0, 1, |_, frames| vec![vec![0.5; frames]; 1_000]);