Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/source/agc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,15 +522,10 @@ where

#[inline]
fn next(&mut self) -> Option<Self::Item> {
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);
let detection = self.span.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")]
{
Expand Down
11 changes: 4 additions & 7 deletions src/source/blt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,12 @@ where
fn next(&mut self) -> Option<Sample> {
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);
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();
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();
Expand Down
10 changes: 3 additions & 7 deletions src/source/dither.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,11 @@ where
fn next(&mut self) -> Option<Self::Item> {
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);
let detection = self.span.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);
Expand Down
9 changes: 2 additions & 7 deletions src/source/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,15 +649,10 @@ where
fn next(&mut self) -> Option<Self::Item> {
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);
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();
let new_channels_count = current_channels.get() as usize;

let needs_reconstruction = match self.inner.as_ref().unwrap() {
Expand Down
12 changes: 3 additions & 9 deletions src/source/linear_ramp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,8 @@ where

#[inline]
fn next(&mut self) -> Option<I::Item> {
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);
let _ = self.span.advance(&self.input);

let factor = if self.elapsed >= self.total {
if self.clamp_end {
Expand All @@ -100,10 +94,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;
}

Expand Down
8 changes: 1 addition & 7 deletions src/source/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,12 @@ where
fn next(&mut self) -> Option<I::Item> {
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;
let old_channels = self.span.last_channels;

let detection = self
.span
.advance(input_span_len, current_sample_rate, current_channels);
let detection = self.span.advance(&self.input);

if detection.at_span_boundary {
// Accumulate duration using the OLD parameters. advance() increments
Expand Down
17 changes: 7 additions & 10 deletions src/source/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,11 @@ impl SpanTracker {

/// Advances the tracker by one sample and reports whether a span boundary was crossed.
#[inline]
pub fn advance(
&mut self,
input_span_len: Option<usize>,
current_sample_rate: SampleRate,
current_channels: ChannelCount,
) -> SpanDetection {
pub fn advance<I: Source>(&mut self, 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(|_| {
Expand All @@ -81,8 +78,12 @@ 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)
Expand All @@ -91,10 +92,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 {
Expand Down
10 changes: 2 additions & 8 deletions src/source/take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,15 @@ 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);
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);
self.samples_in_current_frame = 0;
}

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),
Expand Down
Loading