Skip to content
Open
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed sources to detect parameter updates after mid-span seeks.
- Fixed `Stoppable` and `Skippable` not signaling exhaustion.
- Fixed `SpatialAudio` left and write channel swapping
- Fixed resampled output depending on how the input is split into spans.
- Fixed `chain` reporting the end of the stream between two of its sources.
- Fixed `UniformSourceIterator`'s lower size hint counting input past a format change.

## Version [0.22.2] (2026-03-05)

Expand Down
19 changes: 19 additions & 0 deletions src/conversions/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ where
pub fn inner_mut(&mut self) -> &mut I {
&mut self.input
}

/// Whether the next sample starts an output frame. Mid-frame, the
/// rest of the frame may still come from input already read.
#[inline]
pub(crate) fn at_frame_start(&self) -> bool {
self.next_output_sample_pos == 0
}

/// The channel count converted from.
#[inline]
pub(crate) fn channels_from(&self) -> ChannelCount {
self.from
}

/// How many samples `input_len` whole input frames convert to.
#[inline]
pub(crate) fn converted_len(&self, input_len: usize) -> usize {
input_len / self.from.get() as usize * self.to.get() as usize
}
}

impl<I> Iterator for ChannelCountConverter<I>
Expand Down
257 changes: 134 additions & 123 deletions src/conversions/sample_rate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,28 @@ where
}
}

/// Rebuild for the format the input has moved on to.
fn recreate(&mut self) {
let source = self.inner.take().expect("always set").into_inner();
self.inner = Some(Self::create_resampler(
source,
self.target_rate,
&self.config,
));
self.pending_recreate = false;
self.cached_input_span_len = self.resampler().input().current_span_len();
}

fn next_resampled(&mut self) -> Option<Sample> {
match self.resampler_mut() {
ResampleInner::Passthrough { source, .. } => source.next(),
ResampleInner::Poly(resampler) => resampler.next_sample(),
ResampleInner::Sinc(resampler) => resampler.next_sample(),
#[cfg(feature = "rubato-fft")]
ResampleInner::Fft(resampler) => resampler.next_sample(),
}
}

/// Helper method to create a resampler from a source using the stored config and target rate.
fn create_resampler(
source: I,
Expand Down Expand Up @@ -247,6 +269,42 @@ where
}
}

/// The channel count and sample rate of the input the next sample
/// comes from. The input itself may already be further along.
#[inline]
pub(crate) fn next_input_format(&self) -> (ChannelCount, SampleRate) {
self.resampler().next_format()
}

/// Whether samples of the format it was built for are still to come,
/// buffered or inside the resampler.
#[inline]
pub(crate) fn holds_samples(&self) -> bool {
match self.resampler() {
ResampleInner::Passthrough { .. } => false,
ResampleInner::Poly(resampler) | ResampleInner::Sinc(resampler) => {
resampler.holds_samples()
}
#[cfg(feature = "rubato-fft")]
ResampleInner::Fft(resampler) => resampler.holds_samples(),
}
}

/// How many samples `next` yields before the format may change: the
/// resampled samples buffered, all in the format it was built for.
/// Zero once they run out, or when passing through.
#[inline]
pub(crate) fn samples_in_format(&self) -> usize {
match self.resampler() {
ResampleInner::Passthrough { .. } => 0,
ResampleInner::Poly(resampler) | ResampleInner::Sinc(resampler) => {
resampler.buffered_len()
}
#[cfg(feature = "rubato-fft")]
ResampleInner::Fft(resampler) => resampler.buffered_len(),
}
}

/// Returns the inner source.
#[inline]
pub fn into_inner(self) -> I {
Expand Down Expand Up @@ -302,9 +360,11 @@ where
self.target_rate
}

/// The channel count of the samples `next` yields, which lags the
/// input's while samples read from before a change are still buffered.
#[inline]
fn channels(&self) -> ChannelCount {
self.resampler().input().channels()
self.resampler().next_format().0
}

#[inline]
Expand All @@ -330,35 +390,17 @@ where
self.pending_recreate = false;
let input_span_len = self.resampler().input().current_span_len();

match self.inner.as_mut().unwrap() {
ResampleInner::Passthrough {
input_span_pos: input_samples_consumed,
..
} => {
reset_seek_span_tracking(
input_samples_consumed.raw_mut(),
&mut self.cached_input_span_len,
position,
input_span_len,
);
}
ResampleInner::Poly(r) | ResampleInner::Sinc(r) => {
reset_seek_span_tracking(
r.pos_in_current_span.raw_mut(),
&mut self.cached_input_span_len,
position,
input_span_len,
);
}
#[cfg(feature = "rubato-fft")]
ResampleInner::Fft(r) => {
reset_seek_span_tracking(
r.pos_in_current_span.raw_mut(),
&mut self.cached_input_span_len,
position,
input_span_len,
);
}
// Only the passthrough counts samples against the span length;
// `reset` already cleared the resamplers' own tracking.
if let ResampleInner::Passthrough { input_span_pos, .. } =
self.inner.as_mut().expect("always set")
{
reset_seek_span_tracking(
input_span_pos.raw_mut(),
&mut self.cached_input_span_len,
position,
input_span_len,
);
}

Ok(())
Expand All @@ -376,60 +418,48 @@ where
// If a format change was detected at the previous span boundary, wait until the
// output buffer is fully drained before recreating the resampler. This guarantees
// that fill_input_buffer only ever reads from the current span.
if self.pending_recreate {
let output_empty = match self.resampler() {
ResampleInner::Passthrough { .. } => true,
ResampleInner::Poly(r) | ResampleInner::Sinc(r) => !r.output_has_samples(),
#[cfg(feature = "rubato-fft")]
ResampleInner::Fft(r) => !r.output_has_samples(),
};
if output_empty {
let source = self.inner.take().unwrap().into_inner();
self.inner = Some(Self::create_resampler(
source,
self.target_rate,
&self.config,
));
self.pending_recreate = false;
let sample = loop {
if self.pending_recreate {
let output_empty = match self.resampler() {
ResampleInner::Passthrough { .. } => true,
ResampleInner::Poly(r) | ResampleInner::Sinc(r) => !r.output_has_samples(),
#[cfg(feature = "rubato-fft")]
ResampleInner::Fft(r) => !r.output_has_samples(),
};
if output_empty {
self.recreate();
}
}
}

let sample = match self.resampler_mut() {
ResampleInner::Passthrough { source, .. } => source.next()?,
ResampleInner::Poly(resampler) => resampler.next_sample()?,
ResampleInner::Sinc(resampler) => resampler.next_sample()?,
#[cfg(feature = "rubato-fft")]
ResampleInner::Fft(resampler) => resampler.next_sample()?,
match self.next_resampled() {
Some(sample) => break sample,
// Built for one format, a resampler stops where its input
// changes rate or channels. Rebuilding matches the format
// it stopped at, so this retries at most once.
None if self.resampler().built_for_input() => return None,
None => self.recreate(),
}
};

// If input reports no span length, parameters are stable by contract
let input_span_len = self.resampler().input().current_span_len();
// Only the passthrough tracks spans here; a resampler stops at a
// format change itself while filling, and the loop above rebuilds it.
let ResampleInner::Passthrough {
input_span_pos,
channels,
source_rate,
source,
} = self.resampler_mut()
else {
return Some(sample);
};
let input_span_len = source.current_span_len();
// No span length means the format is stable by contract.
if input_span_len.is_none() {
return Some(sample);
}

let (expected_channels, expected_rate, samples_consumed) = match self.resampler_mut() {
ResampleInner::Passthrough {
input_span_pos: input_samples_consumed,
channels,
source_rate,
..
} => {
*input_samples_consumed += 1usize;
(*channels, *source_rate, *input_samples_consumed)
}
ResampleInner::Poly(r) | ResampleInner::Sinc(r) => (
r.output.channels,
r.input.sample_rate(),
r.pos_in_current_span,
),
#[cfg(feature = "rubato-fft")]
ResampleInner::Fft(r) => (
r.output.channels,
r.input.sample_rate(),
r.pos_in_current_span,
),
};
*input_span_pos += 1usize;
let (expected_channels, expected_rate, samples_consumed) =
(*channels, *source_rate, *input_span_pos);

let input = self.resampler().input();
let (at_boundary, parameters_changed) = Self::detect_boundary(
Expand All @@ -442,30 +472,11 @@ where
);

if at_boundary {
// Update cached span length (exits detection mode if we were in it)
self.cached_input_span_len = input_span_len;

if parameters_changed {
// Defer recreation until the output buffer is drained (handled above at the
// top of the next next() call) so no cross-span reads occur.
self.pending_recreate = true;
} else {
// Just crossed boundary without parameter change, reset counter
match self.resampler_mut() {
ResampleInner::Passthrough {
input_span_pos: input_samples_consumed,
..
} => {
*input_samples_consumed = InSamples::ZERO;
}
ResampleInner::Poly(r) | ResampleInner::Sinc(r) => {
r.pos_in_current_span = InSamples::ZERO;
}
#[cfg(feature = "rubato-fft")]
ResampleInner::Fft(r) => {
r.pos_in_current_span = InSamples::ZERO;
}
}
} else if let ResampleInner::Passthrough { input_span_pos, .. } = self.resampler_mut() {
*input_span_pos = InSamples::ZERO;
}
}

Expand All @@ -475,34 +486,34 @@ where
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
match self.resampler() {
ResampleInner::Passthrough { source, .. } => source.size_hint(),
ResampleInner::Poly(resampler) | ResampleInner::Sinc(resampler) => {
let adjusted_for_resampling = |samples| {
InSamples(samples).resampled_by(resampler.resample_ratio)
+ resampler.output.len()
+ resampler
.frames_being_resampled
.samples(resampler.output.channels)
ResampleInner::Passthrough {
source,
input_span_pos,
channels,
source_rate,
} => {
let (lower, upper) = source.size_hint();
if source.current_span_len().is_none() {
return (lower, upper);
}
// Past this span the rate may change and need resampling,
// so only the rest of it passes through one to one.
let in_span = if self.pending_recreate
|| source.channels() != *channels
|| source.sample_rate() != *source_rate
{
0
} else {
self.cached_input_span_len
.map_or(0, |len| len.saturating_sub(input_span_pos.raw()))
};
let (lower, upper) = resampler.input.size_hint();
let lower = adjusted_for_resampling(lower);
let upper = upper.map(adjusted_for_resampling);
(lower.raw(), upper.as_ref().map(OutSamples::raw))
(lower.min(in_span), upper.filter(|&upper| upper <= in_span))
}
#[cfg(feature = "rubato-fft")]
ResampleInner::Fft(resampler) => {
let adjusted_for_resampling = |samples| {
InSamples(samples).resampled_by(resampler.resample_ratio)
+ resampler.output.len()
+ resampler
.frames_being_resampled
.samples(resampler.output.channels)
};
let (lower, upper) = resampler.input.size_hint();
let lower = adjusted_for_resampling(lower);
let upper = upper.map(adjusted_for_resampling);
(lower.raw(), upper.as_ref().map(OutSamples::raw))
ResampleInner::Poly(resampler) | ResampleInner::Sinc(resampler) => {
resampler.size_hint()
}
#[cfg(feature = "rubato-fft")]
ResampleInner::Fft(resampler) => resampler.size_hint(),
}
}
}
Loading
Loading