Repository navigation
Conversation
`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>
…ng no-spans (stable stream format)
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
left a comment
There was a problem hiding this comment.
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 reportsSome(0)while a padding sample is pending. Chain a stereo buffer cut after one sample withtake_durationto 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 finalDonecallback that rewinds itsSamplesBufferleaves a lower size hint of 1 even thoughnext()returnsNone.
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.
A source that still had a sample after reporting its end left the chain reporting that source's `Some(0)` while the sample was pending, so `buffered()` stopped there and never reached the next source. That sample is now a span of its own. Once the last source has returned `None`, the chain never asks it again, so it now reports the end of the stream whatever that source says afterwards, such as a `Done` callback that rewinds it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks @kapok-4o!
|
kapok-4o
left a comment
There was a problem hiding this comment.
The startup and rate-change tests pass, and the changelog now covers the fix (src/conversions/sample_rate/tests.rs:640, src/source/uniform.rs:517, CHANGELOG.md:55). At 1.1× speed on 44.1 kHz input resampled to 48 kHz, I measured about 0.05 output frames of shift for the linear resampler compared with #915 (src/conversions/sample_rate/rubato.rs:153).
Both chain() regressions pass (src/source/chain.rs:308, src/source/chain.rs:336). I merged this with #940 in both orders and both sets of tests passed (this PR’s src/source/uniform.rs:517 and #940’s src/source/uniform.rs:535).
The mid-span seek corruption from #915 still reproduces on this branch (src/conversions/sample_rate/rubato.rs:460).
A seek can land partway through a span, but the resampler then read the span's full length before checking the format again, so it fed the next span's samples to a resampler built for another channel count. After a seek past the start, it now checks the format before each sample until the format changes, as the passthrough already did. A seek that lands in a span of another format rebuilds the resampler for it right away. Before, the first sample after the seek went through the old one, and a passthrough yielded it unresampled. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: multica-agent <github@multica.ai>
…-frame # Conflicts: # src/conversions/sample_rate/rubato.rs # src/conversions/sample_rate/tests.rs
|
Thanks @kapok-4o! The mid-span seek fix from #915 (83e8da0) is merged here in 3f90ac0. The only conflict was |
A seek to the end leaves nothing to resample, so the rebuild after it makes a passthrough, which records the last span's rate. A seek back into a span of that format then kept the passthrough, and played the rest at the wrong rate. A seek now also rebuilds a passthrough whose input has samples again at another rate than the target's. The seek property now reads and seeks up to four times, with seeks to the start and the end made likely. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: multica-agent <github@multica.ai>
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
UniformSourceIteratorbuilds a new resampler at every format change, which is whatPlayer::set_speeddoes, and a seek resets one. There, the first frame dips: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()andis_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 an output frame: at 1.1× speed on 44.1 kHz input into 48 kHz, 0.05 of a frame with the linear resampler.Tests, all failing on #915's head:
UniformSourceIterator, from 2.5× back to 1×, comes out exactly as the input on both sidesThree 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_delayand the end-of-stream tests still pass.