From 05409e55f43a8aa1e3580e401cbc88073f7463f8 Mon Sep 17 00:00:00 2001 From: kapok-4o <200382837+kapok-4o@users.noreply.github.com> Date: Thu, 26 Nov 2026 18:10:18 +1100 Subject: [PATCH] Delay quantizes silence to whole frames --- CHANGELOG.md | 1 + src/source/delay.rs | 104 +++++++++++++++++++++++++++++++++----------- 2 files changed, 80 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0f149dec..a99152e5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Fixed `Delay` inserting partial frames for delays between frame boundaries. - Fixed `Player::skip_one` not decreasing the player's length immediately. - Fixed `Source::current_span_len()` to consistently return total span length. - Fixed `Source::size_hint()` to consistently report actual bounds based on current sources. diff --git a/src/source/delay.rs b/src/source/delay.rs index 5c835dc55..67c1d2e63 100644 --- a/src/source/delay.rs +++ b/src/source/delay.rs @@ -5,14 +5,8 @@ use crate::common::{ChannelCount, SampleRate}; use crate::math::NANOS_PER_SEC; use crate::Source; -fn remaining_samples( - until_playback: Duration, - sample_rate: SampleRate, - channels: ChannelCount, -) -> usize { - let ns = until_playback.as_nanos(); - let samples = ns * channels.get() as u128 * sample_rate.get() as u128 / NANOS_PER_SEC as u128; - samples as usize +fn whole_frames(duration: Duration, sample_rate: SampleRate) -> u128 { + duration.as_nanos() * u128::from(sample_rate.get()) / u128::from(NANOS_PER_SEC) } /// Internal function that builds a `Delay` object. @@ -20,10 +14,18 @@ pub fn delay(input: I, duration: Duration) -> Delay where I: Source, { + let silence_channels = input.channels(); + let silence_rate = input.sample_rate(); + let channels = usize::from(silence_channels.get()); + let silence_frames = + whole_frames(duration, silence_rate).min((usize::MAX / channels) as u128) as usize; + Delay { - remaining_samples: remaining_samples(duration, input.sample_rate(), input.channels()), - requested_duration: duration, input, + remaining_samples: silence_frames * channels, + silence_frames, + silence_channels, + silence_rate, } } @@ -32,13 +34,26 @@ where pub struct Delay { input: I, remaining_samples: usize, - requested_duration: Duration, + silence_frames: usize, + silence_channels: ChannelCount, + silence_rate: SampleRate, } impl Delay where I: Source, { + fn silence_duration(&self) -> Duration { + let nanos_per_sec = u128::from(NANOS_PER_SEC); + // Use the first representable timestamp at or after the end of the silence. + let nanos = (self.silence_frames as u128 * nanos_per_sec) + .div_ceil(u128::from(self.silence_rate.get())); + Duration::new( + (nanos / nanos_per_sec) as u64, + (nanos % nanos_per_sec) as u32, + ) + } + /// Returns a reference to the inner source. #[inline] pub fn inner(&self) -> &I { @@ -92,26 +107,36 @@ where { #[inline] fn current_span_len(&self) -> Option { - self.input - .current_span_len() - .map(|val| val + self.remaining_samples) + if self.remaining_samples > 0 { + Some(self.silence_frames * usize::from(self.silence_channels.get())) + } else { + self.input.current_span_len() + } } #[inline] fn channels(&self) -> ChannelCount { - self.input.channels() + if self.remaining_samples > 0 { + self.silence_channels + } else { + self.input.channels() + } } #[inline] fn sample_rate(&self) -> SampleRate { - self.input.sample_rate() + if self.remaining_samples > 0 { + self.silence_rate + } else { + self.input.sample_rate() + } } #[inline] fn total_duration(&self) -> Option { self.input .total_duration() - .map(|val| val + self.requested_duration) + .and_then(|duration| duration.checked_add(self.silence_duration())) } /// Pos is seen from the perspective of the api user. @@ -129,13 +154,42 @@ where /// #[inline] fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> { - if pos < self.requested_duration { - self.input.try_seek(Duration::ZERO)?; - let until_playback = self.requested_duration - pos; - self.remaining_samples = - remaining_samples(until_playback, self.sample_rate(), self.channels()); - } - let compensated_for_delay = pos.saturating_sub(self.requested_duration); - self.input.try_seek(compensated_for_delay) + self.input + .try_seek(pos.saturating_sub(self.silence_duration()))?; + let elapsed_frames = whole_frames(pos, self.silence_rate); + let remaining_frames = + (self.silence_frames as u128).saturating_sub(elapsed_frames) as usize; + self.remaining_samples = remaining_frames * usize::from(self.silence_channels.get()); + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::buffer::SamplesBuffer; + use crate::math::nz; + + #[test] + fn delay_emits_whole_silent_frames() { + let source = + SamplesBuffer::new(nz!(2), nz!(2), [0.25, -0.25]).delay(Duration::from_millis(250)); + assert_eq!(source.current_span_len(), Some(2)); + assert_eq!(source.collect::>(), [0.25, -0.25]); + } + + #[test] + fn delay_uses_the_quantized_duration_for_seeking() { + let mut source = SamplesBuffer::new(nz!(2), nz!(2), [0.25, -0.25, 0.5, -0.5]) + .delay(Duration::from_millis(750)); + assert_eq!(source.total_duration(), Some(Duration::from_millis(1500))); + + source.try_seek(Duration::from_millis(250)).unwrap(); + assert_eq!(source.next(), Some(0.0)); + assert_eq!(source.current_span_len(), Some(2)); + assert_eq!(source.next(), Some(0.0)); + + source.try_seek(Duration::from_secs(1)).unwrap(); + assert_eq!(source.collect::>(), [0.5, -0.5]); } }