Skip to content

Keep UniformSourceIterator's lower size hint within the current format - #940

Open
benface wants to merge 8 commits into
RustAudio:masterfrom
benface:fix/uniform-size-hint
Open

benface wants to merge 8 commits into
RustAudio:masterfrom
benface:fix/uniform-size-hint

Conversation

@benface

@benface benface commented Nov 25, 2026 •

Copy link
Copy Markdown

Builds on #915, since it rewrites uniform.rs: only the last commit is new here.

UniformSourceIterator::size_hint returns its current converter's lower bound. Passed through or channel-converted, that counts the rest of the input in the current conversion, so it runs past what's left as soon as a later span converts to fewer samples. A chain of a mono 96 kHz span of 2 samples, then a stereo 96 kHz span of 4,000 frames, into mono 96 kHz, gives a lower bound of 8,000 once the mono span is used up, with 4,000 samples left (7,999 on master). Mixer::add wraps every source in one, and callers that preallocate from the lower bound, like collect(), allocate too much.

The lower bound now counts only the rest of the span UniformSourceIterator already tracks, unless the input's format can't change (current_span_len() is None). Between spans, it counts the next span if the format still holds. A resampler's own bound already stops at its format, as #915 made it, so it's left as is. next() doesn't change.

A fuzz of random span layouts through UniformSourceIterator (chains of SamplesBuffers, 1 to 4 channels in, 1, 2, 4 or 6 out, six rates from 8 to 96 kHz), checking the lower bound before every sample, finds it too high in this many of 30,000 cases:

Cases
master 4,969
#915 2,719
this PR 0

The output is bit for bit the same as #915's in every case, and the bound loses little: the first lower bound averages 0.456 of the length, against 0.471.

Tests:

  • size_hint_holds_across_format_changes: passed through then downmixed, downmixed further, upmixed then passed through, and resampled into passthrough then downmixed. It fails on master and on Don't let span chunking change the resampler's output #915.
  • size_hint_is_exact_in_one_format: a single span passed through or upmixed still starts with its exact length.

benface and others added 8 commits August 5, 2026 21:17
`fill_input_buffer` treats `current_span_len()` as the samples left in
the span, but it's the span's total length, so it stops reading at the
end of every span. Vorbis packets are smaller than a resampler chunk,
so every chunk ends up short, gets marked `partial_len`, and rubato
pads it with zeros. The same audio comes out different depending on how
the decoder split it.

Stop only where the rate or channel count actually changes, and check
that before reading anything from the next span. When reading stops
there, rebuild the resampler.

That fixes a second bug: a span that changed the sample rate was still
being resampled at the old one. 441 frames at 44.1k then 441 at 22.05k,
converted to 48k, gave 962 samples instead of 1440.

Drop the counter the boundary detection was using — reading stops at
the change itself now, so there's nothing left to count. Only the
passthrough still needs it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Until the next call to `next`, a finished source stayed current, so the
chain reported its `Some(0)`: the end of the whole stream, per the
`Source` contract. A resampler reading across spans takes it at its
word, so resampling a chain of a 600-frame buffer and a 64-frame one at
24 kHz to 48 kHz stopped at 1200 samples instead of 1328.

Advance right after the last sample instead, as `Repeat` does, so the
span length, channel count and sample rate always describe the next
sample. That also fixes the FIXME in the `basic` test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
Reading across spans means the input is often further along than the
samples being yielded, but `channels()`, `is_exhausted()` and the
format checks in `UniformSourceIterator` all looked at the input:

- `channels()` turned stereo while mono samples were still buffered.
- `is_exhausted()` was true once the input ran out, with rubato still
  holding the tail of a sinc filter.
- `UniformSourceIterator` saw the next span's rate on the input and
  rebuilt, dropping what the resampler had buffered. Through
  `Mixer::add` at 48 kHz, 128 and 64 frames at 24 kHz then 96 at
  48 kHz played 224 samples instead of 480.

The resampler now reports the format it was built for while it holds
samples of it, and the input's after. `UniformSourceIterator` asks it
before every sample while resampling, since counting spans can't tell
where its format changes, and resamples before remixing channels, so a
resampler always reads its raw input and sees where a span changes
format. Its span count starts from what it yields, and it checks the
format on the first pull, as a queue added while empty has moved on.

`size_hint()` applied the current ratio to every span after the one
being read, whose rate may differ, and the passthrough counted them
one to one. Only the span being read is counted now, with room for
each chunk rounding up, and the upper bound is unknown past it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
Keeping what a resampler buffered past a format change, rather than
dropping it, brought out two things wrong with its last frames:

- Output was counted by rounding up each chunk on its own, which can
  count a frame more than the input gives, once per chunk. At the end
  of a stream that's a little silence; before a format change it's a
  gap in the middle: 44.1 kHz music sped up to 2.5x, as a player's
  `set_speed` does, came out with 19 frames of silence at the change.
  Count from the totals in and out instead, in integers, which is
  also what `size_hint` now counts with.
- The chunk that stops at a format change was marked partial, so
  rubato padded it with silence and the last frames were interpolated
  towards it, ending in a click. Pad it with the last frame read
  instead: the frames taken out only cover real input, so the padding
  only stands in for the input that would have followed.

A reset now also forgets what was being resampled before it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
The channel converter repeats a mono sample into the next channel
after reading it. Once the resampler has moved on to the next span,
UniformSourceIterator saw the new format between those two samples and
rebuilt there, dropping the repeat and swapping every channel after it.
Only switch formats between output frames.

Co-authored-by: multica-agent <github@multica.ai>
UniformSourceIterator asked a resampler for the format of every sample
it yielded, since neither the input's spans nor the resampler's own
tell where the format of its output changes. But what the resampler
has buffered is all in the format it was built for, so count that like
a span, and check again once it runs out. The check now happens once
or twice per chunk instead of once per sample.

Downmixing also goes before resampling again, so the channels dropped
aren't resampled. It sits behind a small wrapper that reports another
channel count once the input's changes, which stops the resampler
there as its raw input would have.

Instructions retired converting 10 s of audio 10 times, previous head
to this one: stereo 48 kHz passthrough 484M to 358M, stereo 44.1 to
48 kHz 1,676M to 1,293M, mono 24 kHz to stereo 48 kHz 1,323M to
1,080M, stereo 44.1 kHz to mono 48 kHz 1,886M to 811M. All four are
back to where they were before this PR's fixes, or a little under.
The output is the same, bit for bit, over 30,000 random span layouts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
Passed through or channel-converted, the lower bound counted the rest of
the input in the current conversion. Once a later span converts to fewer
samples, like a mono span followed by a stereo one into mono, that runs
past what's left. Count only the rest of the span being tracked, unless
the input's format can't change. A resampler's own bound already stops
at its format.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants