Skip to content

Start a resampler on its input's first frame, not on silence - #939

Open
benface wants to merge 12 commits into
RustAudio:masterfrom
benface:fix/resampler-first-frame
Open

benface wants to merge 12 commits into
RustAudio:masterfrom
benface:fix/resampler-first-frame

Conversation

@benface

@benface benface commented Nov 25, 2026 •

Copy link
Copy Markdown

Builds on #915, since it changes the same code: only the last commit is new here.

A resampler's first frames come out of rubato's history, which starts as silence. They land just before the input's start, so they're interpolated partly from that silence. At the start of a sound, that's just a soft first frame. But UniformSourceIterator builds a new resampler at every format change, which is what Player::set_speed does, and a seek resets one. There, the first frame dips:

Resampler, to 48 kHz First frame of a constant 0.8
linear (what the mixer uses), from 44.1 kHz 0.735
linear, from 32 kHz 0.533
balanced sinc, from 44.1 kHz 0.448
balanced sinc, from 110.25 kHz 0.026

That can click on every speed change on an output whose rate isn't the source's.

The resampler now feeds rubato copies of the first frame ahead of it, as far back as its filter reaches, and skips their output along with the delay. The lead-in isn't counted as input, so lengths, size_hint() and is_exhausted() are as before. When the rates allow it within 1024 frames, the lead-in is a whole number of output frames, so the output keeps its timing exactly. That covers 44.1 ↔ 48 kHz and the usual rates. Otherwise it moves by less than half a frame.

Tests, all failing on #915's head:

  • every resampler kind starts on the input's first frame, from five rates
  • the same after a seek
  • a rate change through UniformSourceIterator, from 2.5× back to 1×, comes out exactly as the input on both sides

Three more tests cover a lead-in longer than a chunk; one that fills a chunk exactly, which leaves the first frame itself for the next chunk, at every chunk size around those, from the start and after a seek, against one chunk for the whole input; and a single frame, alone or before a format change. test_current_span_len_excludes_delay and the end-of-stream tests still pass.

benface and others added 12 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>
Rubato's history starts as silence, and the first frames out land just
before the input's start, so they were interpolated partly from it. At
the start of a sound that's a soft first frame. But UniformSourceIterator
builds a new resampler at every format change, which `Player::set_speed`
makes, and a seek resets one: there, the first frame dipped. A linear
resampler from 44.1 to 48 kHz gave 92% of the first frame; the balanced
sinc resampler as little as 3% at 110.25 kHz, and 56% at 44.1 kHz.

Feed rubato copies of the first frame ahead of it, as far back as its
filter reaches, and skip their output along with the delay. Where the
rates allow it within 1024 frames, the lead-in is a whole number of
output frames, so the output keeps its timing exactly. Otherwise it
moves by less than half a frame.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
The lead-in counted only the copies ahead of the first frame, so when
they filled a chunk exactly, it reached zero with the frame itself
still to feed, and the next chunk read on from the second frame. The
output ran a frame ahead, with silence at its end. Count the frame as
the lead-in's last copy instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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>
# Conflicts:
#	src/source/uniform.rs
Moving on as soon as a source reports its end skipped any source that
starts out exhausted, without calling its `next()`. So an
`EmptyCallback` between two sources never ran, and a `Done` wrapping a
source in the middle of a chain never signalled.

Chain now calls `next()` on each source it leaves until it returns
`None`, and never calls it again after that, so a trailing
`EmptyCallback` runs once even if the chain is asked again. A source
that still yields a sample after reporting its end keeps it: the
chain plays it next instead of dropping it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>

@kapok-4o kapok-4o left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rechecked cafc65e6. The two resampler commits are unchanged since 307802b0 and still look good to me. The new code comes from #915 (a643177), where I found two regressions.

  • In src/source/chain.rs:125, the chain reports Some(0) while a padding sample is pending. Chain a stereo buffer cut after one sample with take_duration to another stereo buffer, then wrap it in .buffered(). Only the first sample comes through. Direct iteration returns all four samples, and the old chain reached the second buffer even with .buffered().
  • In src/source/chain.rs:107, a final Done callback that rewinds its SamplesBuffer leaves a lower size hint of 1 even though next() returns None.

The macOS workflow checks passed with stable Rust 1.99.0, including both doctest runs. The pinned Rust 1.95 toolchain and the nightly docs build were unavailable locally.

The tests still conflict with #940 in src/source/uniform.rs. A changelog entry for the resampler startup fix would be helpful. The fractional-frame shift can also occur with common speed changes, such as 1.1× on 44.1 kHz input resampled to 48 kHz.

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.

3 participants