From 829d67717539b3646b65d821d93abb3e18834191 Mon Sep 17 00:00:00 2001 From: Keinsleif Date: Mon, 23 Mar 2026 19:11:32 +0900 Subject: [PATCH 1/3] [update] advance fn receive source --- src/source/span.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/source/span.rs b/src/source/span.rs index 33663028..554efda9 100644 --- a/src/source/span.rs +++ b/src/source/span.rs @@ -63,14 +63,14 @@ impl SpanTracker { /// Advances the tracker by one sample and reports whether a span boundary was crossed. #[inline] - pub fn advance( + pub fn advance( &mut self, - input_span_len: Option, - current_sample_rate: SampleRate, - current_channels: ChannelCount, + source: &I ) -> SpanDetection { self.samples_counted = self.samples_counted.saturating_add(1); + let input_span_len = source.current_span_len(); + // If input reports no span length, parameters are stable by contract. let mut parameters_changed = false; let at_span_boundary = input_span_len.is_some_and(|_| { @@ -81,8 +81,13 @@ impl SpanTracker { // In span-counting mode, parameters can only change at a boundary. // In seek mode, we check every sample for a parameter change. if known_boundary.is_none_or(|at_boundary| at_boundary) { + let current_channels = source.channels(); + let current_sample_rate = source.sample_rate(); parameters_changed = current_channels != self.last_channels || current_sample_rate != self.last_sample_rate; + self.last_channels = current_channels; + self.last_sample_rate = current_sample_rate; + } known_boundary.unwrap_or(parameters_changed) @@ -91,10 +96,6 @@ impl SpanTracker { if at_span_boundary { self.samples_counted = 0; self.cached_span_len = input_span_len; - if parameters_changed { - self.last_sample_rate = current_sample_rate; - self.last_channels = current_channels; - } } SpanDetection { From d251416d6cc9456b1a798fa6ea3f6abaeb851659 Mon Sep 17 00:00:00 2001 From: Keinsleif Date: Mon, 23 Mar 2026 19:11:58 +0900 Subject: [PATCH 2/3] [update] rewrite sources --- src/source/agc.rs | 7 ++----- src/source/blt.rs | 9 ++++----- src/source/dither.rs | 8 +++----- src/source/limit.rs | 7 ++----- src/source/linear_ramp.rs | 9 +++------ src/source/position.rs | 6 +----- src/source/take.rs | 8 ++------ 7 files changed, 17 insertions(+), 37 deletions(-) diff --git a/src/source/agc.rs b/src/source/agc.rs index 06642ffe..c61095da 100644 --- a/src/source/agc.rs +++ b/src/source/agc.rs @@ -522,15 +522,12 @@ where #[inline] fn next(&mut self) -> Option { - let current_sample_rate = self.input.sample_rate(); - let current_channels = self.input.channels(); - let input_span_len = self.input.current_span_len(); - let detection = self .span - .advance(input_span_len, current_sample_rate, current_channels); + .advance(&self.input); if detection.at_span_boundary && detection.parameters_changed { + let current_sample_rate = self.input.sample_rate(); // Recalculate coefficients for new sample rate #[cfg(feature = "experimental")] { diff --git a/src/source/blt.rs b/src/source/blt.rs index 3b7eb904..ec4bf873 100644 --- a/src/source/blt.rs +++ b/src/source/blt.rs @@ -119,15 +119,14 @@ where fn next(&mut self) -> Option { let sample = self.inner.as_mut().unwrap().next()?; - let input_span_len = self.inner.as_ref().unwrap().current_span_len(); - let current_sample_rate = self.inner.as_ref().unwrap().sample_rate(); - let current_channels = self.inner.as_ref().unwrap().channels(); - let detection = self .span - .advance(input_span_len, current_sample_rate, current_channels); + .advance(self.inner.as_ref().unwrap()); if detection.at_span_boundary && detection.parameters_changed { + let current_sample_rate = self.inner.as_ref().unwrap().sample_rate(); + let current_channels = self.inner.as_ref().unwrap().channels(); + if current_channels != self.inner.as_ref().unwrap().channels() { let old_inner = self.inner.take().unwrap(); let (input, formula) = old_inner.into_parts(); diff --git a/src/source/dither.rs b/src/source/dither.rs index 8370a449..afdb4a76 100644 --- a/src/source/dither.rs +++ b/src/source/dither.rs @@ -217,15 +217,13 @@ where fn next(&mut self) -> Option { let input_sample = self.input.next()?; - let input_span_len = self.input.current_span_len(); - let current_sample_rate = self.input.sample_rate(); - let current_channels = self.input.channels(); - let detection = self .span - .advance(input_span_len, current_sample_rate, current_channels); + .advance(&self.input); if detection.at_span_boundary { + let current_sample_rate = self.input.sample_rate(); + let current_channels = self.input.channels(); if detection.parameters_changed { self.noise .update_parameters(current_sample_rate, current_channels); diff --git a/src/source/limit.rs b/src/source/limit.rs index 495e7e94..a7ff1d3e 100644 --- a/src/source/limit.rs +++ b/src/source/limit.rs @@ -649,15 +649,12 @@ where fn next(&mut self) -> Option { let sample = self.inner.as_mut().unwrap().next()?; - let input_span_len = self.inner.as_ref().unwrap().current_span_len(); - let current_channels = self.inner.as_ref().unwrap().channels(); - let current_sample_rate = self.inner.as_ref().unwrap().sample_rate(); - let detection = self .span - .advance(input_span_len, current_sample_rate, current_channels); + .advance(self.inner.as_ref().unwrap()); if detection.at_span_boundary && detection.parameters_changed { + let current_channels = self.inner.as_ref().unwrap().channels(); let new_channels_count = current_channels.get() as usize; let needs_reconstruction = match self.inner.as_ref().unwrap() { diff --git a/src/source/linear_ramp.rs b/src/source/linear_ramp.rs index f5f00400..495278dc 100644 --- a/src/source/linear_ramp.rs +++ b/src/source/linear_ramp.rs @@ -77,14 +77,11 @@ where #[inline] fn next(&mut self) -> Option { - let current_sample_rate = self.input.sample_rate(); - let current_channels = self.input.channels(); - let input_span_len = self.input.current_span_len(); // Elapsed time was accumulated in real time and remains valid across parameter changes. let _ = self .span - .advance(input_span_len, current_sample_rate, current_channels); + .advance(&self.input); let factor = if self.elapsed >= self.total { if self.clamp_end { @@ -100,10 +97,10 @@ where if self .sample_idx - .is_multiple_of(current_channels.get() as u64) + .is_multiple_of(self.input.channels().get() as u64) { let sample_duration = - Duration::from_nanos(NANOS_PER_SEC / current_sample_rate.get() as u64); + Duration::from_nanos(NANOS_PER_SEC / self.input.sample_rate().get() as u64); self.elapsed += sample_duration; } diff --git a/src/source/position.rs b/src/source/position.rs index 7443c0a7..0de00741 100644 --- a/src/source/position.rs +++ b/src/source/position.rs @@ -82,10 +82,6 @@ where fn next(&mut self) -> Option { let item = self.input.next()?; - let input_span_len = self.input.current_span_len(); - let current_sample_rate = self.input.sample_rate(); - let current_channels = self.input.channels(); - // Capture state before advance() resets samples_counted at a boundary. let samples_before_boundary = self.span.samples_counted; let old_rate = self.span.last_sample_rate; @@ -93,7 +89,7 @@ where let detection = self .span - .advance(input_span_len, current_sample_rate, current_channels); + .advance(&self.input); if detection.at_span_boundary { // Accumulate duration using the OLD parameters. advance() increments diff --git a/src/source/take.rs b/src/source/take.rs index 17b5f157..283d620b 100644 --- a/src/source/take.rs +++ b/src/source/take.rs @@ -126,13 +126,9 @@ where // Try to get the next sample from the input. let sample = self.input.next()?; - let input_span_len = self.input.current_span_len(); - let current_sample_rate = self.input.sample_rate(); - let current_channels = self.input.channels(); - let detection = self.span - .advance(input_span_len, current_sample_rate, current_channels); + .advance(&self.input); if detection.at_span_boundary && detection.parameters_changed { self.duration_per_sample = Self::get_duration_per_sample(&self.input); @@ -140,7 +136,7 @@ where } self.samples_in_current_frame = - (self.samples_in_current_frame + 1) % current_channels.get() as usize; + (self.samples_in_current_frame + 1) % self.input.channels().get() as usize; let sample = match &self.filter { Some(filter) => filter.apply(sample, self), From 1c39d580c8923d5e4fd6c3845b9ebe696872bda1 Mon Sep 17 00:00:00 2001 From: Keinsleif Date: Thu, 16 Apr 2026 10:09:12 +0900 Subject: [PATCH 3/3] [fmt] cargo fmt --- src/source/agc.rs | 4 +--- src/source/blt.rs | 4 +--- src/source/dither.rs | 4 +--- src/source/limit.rs | 4 +--- src/source/linear_ramp.rs | 5 +---- src/source/position.rs | 4 +--- src/source/span.rs | 6 +----- src/source/take.rs | 4 +--- 8 files changed, 8 insertions(+), 27 deletions(-) diff --git a/src/source/agc.rs b/src/source/agc.rs index c61095da..0d10a517 100644 --- a/src/source/agc.rs +++ b/src/source/agc.rs @@ -522,9 +522,7 @@ where #[inline] fn next(&mut self) -> Option { - let detection = self - .span - .advance(&self.input); + let detection = self.span.advance(&self.input); if detection.at_span_boundary && detection.parameters_changed { let current_sample_rate = self.input.sample_rate(); diff --git a/src/source/blt.rs b/src/source/blt.rs index ec4bf873..ae88cb72 100644 --- a/src/source/blt.rs +++ b/src/source/blt.rs @@ -119,9 +119,7 @@ where fn next(&mut self) -> Option { let sample = self.inner.as_mut().unwrap().next()?; - let detection = self - .span - .advance(self.inner.as_ref().unwrap()); + let detection = self.span.advance(self.inner.as_ref().unwrap()); if detection.at_span_boundary && detection.parameters_changed { let current_sample_rate = self.inner.as_ref().unwrap().sample_rate(); diff --git a/src/source/dither.rs b/src/source/dither.rs index afdb4a76..3db8371d 100644 --- a/src/source/dither.rs +++ b/src/source/dither.rs @@ -217,9 +217,7 @@ where fn next(&mut self) -> Option { let input_sample = self.input.next()?; - let detection = self - .span - .advance(&self.input); + let detection = self.span.advance(&self.input); if detection.at_span_boundary { let current_sample_rate = self.input.sample_rate(); diff --git a/src/source/limit.rs b/src/source/limit.rs index a7ff1d3e..1d466e5a 100644 --- a/src/source/limit.rs +++ b/src/source/limit.rs @@ -649,9 +649,7 @@ where fn next(&mut self) -> Option { let sample = self.inner.as_mut().unwrap().next()?; - let detection = self - .span - .advance(self.inner.as_ref().unwrap()); + let detection = self.span.advance(self.inner.as_ref().unwrap()); if detection.at_span_boundary && detection.parameters_changed { let current_channels = self.inner.as_ref().unwrap().channels(); diff --git a/src/source/linear_ramp.rs b/src/source/linear_ramp.rs index 495278dc..4467e730 100644 --- a/src/source/linear_ramp.rs +++ b/src/source/linear_ramp.rs @@ -77,11 +77,8 @@ where #[inline] fn next(&mut self) -> Option { - // Elapsed time was accumulated in real time and remains valid across parameter changes. - let _ = self - .span - .advance(&self.input); + let _ = self.span.advance(&self.input); let factor = if self.elapsed >= self.total { if self.clamp_end { diff --git a/src/source/position.rs b/src/source/position.rs index 0de00741..f41848eb 100644 --- a/src/source/position.rs +++ b/src/source/position.rs @@ -87,9 +87,7 @@ where let old_rate = self.span.last_sample_rate; let old_channels = self.span.last_channels; - let detection = self - .span - .advance(&self.input); + let detection = self.span.advance(&self.input); if detection.at_span_boundary { // Accumulate duration using the OLD parameters. advance() increments diff --git a/src/source/span.rs b/src/source/span.rs index 554efda9..aef4d027 100644 --- a/src/source/span.rs +++ b/src/source/span.rs @@ -63,10 +63,7 @@ impl SpanTracker { /// Advances the tracker by one sample and reports whether a span boundary was crossed. #[inline] - pub fn advance( - &mut self, - source: &I - ) -> SpanDetection { + pub fn advance(&mut self, source: &I) -> SpanDetection { self.samples_counted = self.samples_counted.saturating_add(1); let input_span_len = source.current_span_len(); @@ -87,7 +84,6 @@ impl SpanTracker { || current_sample_rate != self.last_sample_rate; self.last_channels = current_channels; self.last_sample_rate = current_sample_rate; - } known_boundary.unwrap_or(parameters_changed) diff --git a/src/source/take.rs b/src/source/take.rs index 283d620b..fb6ef286 100644 --- a/src/source/take.rs +++ b/src/source/take.rs @@ -126,9 +126,7 @@ where // Try to get the next sample from the input. let sample = self.input.next()?; - let detection = - self.span - .advance(&self.input); + let detection = self.span.advance(&self.input); if detection.at_span_boundary && detection.parameters_changed { self.duration_per_sample = Self::get_duration_per_sample(&self.input);