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>
1623f94 to
6c15966
Compare
|
I found one issue on this: in next(), you're returning I've opened a PR to your PR with a simple fix. The fix boils down to this: // No span length means the format is stable by contract.
// input_span_len?; // replace this with this:
let Some(input_span_len) = input_span_len else {
return Some(sample);
}; |
|
PR to your PR is here: benface#1 |
…ng no-spans (stable stream format)
|
Thank you so much @phayes! When I rewrote |
kapok-4o
left a comment
There was a problem hiding this comment.
I verified the Vorbis fix by comparing packetized input with the same decoded PCM in a single buffer. The samples now match in each resampler I tested.
- Through
Mixer::addat 48 kHz, mono spans of 128 and 64 frames at 24 kHz followed by 96 frames at 48 kHz yield 224 samples instead of 480.UniformSourceIteratorsees the new rate on the read-ahead input and drops the buffered tail of the old format. - Resampling a
chain()of two 24 kHz mono buffers (600 and 64 frames) to 48 kHz produces 1,200 of 1,328 samples. The first buffer'sSome(0)cuts off the second buffer. channels()can report stereo while mono samples are still buffered, andis_exhausted()can become true with a sample pending.size_hint()also applies the current rate to later spans. One case advertises an upper bound of 384 and emits 512 samples. Another advertises a lower bound of 384 and emits 320.
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>
|
Thanks a lot @kapok-4o, and for checking the Vorbis fix against the decoded PCM! I could reproduce all three with your numbers:
Each has a test that fails on the previous head. I also fuzzed random span layouts through the resampler and the mixer: lengths, both bounds at every step, |
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>
|
One more, found while testing the above: when a resampled mono source changed to stereo, |
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>
|
Following up on the extra CPU I mentioned: it's gone now (a17ba69). Instructions retired converting 10 s of audio 10 times:
The output is the same as the previous head, bit for bit, over 30,000 random span layouts, and two new tests cover the wrapper. |
kapok-4o
left a comment
There was a problem hiding this comment.
Thanks for the quick and thorough follow-up, and for the CPU table.
I re-ran my three earlier cases on a17ba69. The Mixer::add case now plays 480 samples, the resampled chain() gives 1,328, and size_hint() stayed within bounds at every step over 3,000 random span layouts through the resampling path. I also fuzzed 3,000 layouts through UniformSourceIterator (1-3 channels, four rates, into mono or stereo): lengths match the per-format sums, and no stereo frame came out with swapped channels. CI's fmt, clippy, test and check commands all pass here (stable 1.99, not the MSRV).
One regression from 6e4b0bf: Chain::skip_finished_sources (src/source/chain.rs:45-56, called at :76) steps past any source whose is_exhausted() is already true without calling its next(). An EmptyCallback in the middle of a chain never fires, although its docs describe exactly this use:
let sources: Vec<Box<dyn Source + Send>> = vec![
Box::new(SamplesBuffer::new(nz!(1), nz!(48000), vec![1.0, 2.0])),
Box::new(EmptyCallback::new(Box::new(move || { h.fetch_add(1, SeqCst); }))),
Box::new(SamplesBuffer::new(nz!(1), nz!(48000), vec![3.0])),
];
// samples are [1, 2, 3], but the callback ran 0 times (1 on 2f4c14e and master)A test for this would be good with the fix.
Minor, and older than this PR: the Passthrough and ChannelCount arms of UniformInner::size_hint (src/source/uniform.rs:112,114) still count past a format change. Mono 48 kHz ×2 then 3-channel 48 kHz ×64, into mono 48 kHz, reports a lower bound of 192 with 64 left. I see #940 already fixes exactly these two arms, so nothing to do here.
On CPU I only timed wall clock on one Apple-silicon machine: versus master, 44.1 → 48 kHz is ~14% faster, mono → stereo level, passthrough and stereo → mono 8-17% slower. Possibly layout noise, so no change requested.
Otherwise this looks good to me.
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>
|
Thanks again @kapok-4o, and good catch on Fixed in a643177: A few details:
New tests in |
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>
|
Thanks @kapok-4o for checking this again from #939 and #940. Both regressions were real. Fixed in bf1fb93:
Both new tests fail on a643177, and the |
kapok-4o
left a comment
There was a problem hiding this comment.
Thanks for fixing both chain() cases. The buffered cut now yields [1.0, 0.0, 3.0, 4.0], and a final Done that rewinds its source leaves a lower size hint of zero (src/source/chain.rs:308, src/source/chain.rs:336).
I still get corrupted stereo after a seek on bf1fb93. Using TestSource, feed 113 mono frames of 0.25 followed by 64 stereo frames of [0.75, -0.75], all at 24 kHz. Resample to 48 kHz with linear interpolation and 128-frame chunks, then seek to 2 ms before reading. Near the start of the stereo span I get [0.75, 0.0, -0.75, 0.0, ...] (src/conversions/sample_rate/tests.rs:54).
After the seek, start_of_span counts the whole span as unread, so the fill loop reads stereo samples into the mono resampler before checking the format (src/conversions/sample_rate/rubato.rs:220, src/conversions/sample_rate/rubato.rs:340, src/conversions/sample_rate/rubato.rs:374). Could you handle the partial span and add a regression test?
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>
|
Thanks @kapok-4o, good catch! After a seek, the resampler did count the whole span as unread. Fixed in 83e8da0:
Two new tests, both failing on bf1fb93: your example, and a quickcheck property that a seek anywhere over random span layouts (rates, channel counts, chunk sizes, poly and sinc) gives exactly what resampling the rest of the input from scratch gives, |
kapok-4o
left a comment
There was a problem hiding this comment.
Thanks for adding both regression tests.
- The original 2 ms seek keeps the mono samples at
0.25and produces all 256 stereo samples in the correct left/right pairs. The regression test fails on the previous revision (src/conversions/sample_rate/tests.rs:621). - Seeking across rates now matches a fresh conversion of the remaining samples, including when the old converter was passing samples through. The seek property test passes here and fails on the previous revision (
src/conversions/sample_rate/mod.rs:376,src/conversions/sample_rate/tests.rs:659).
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>
|
One more fix on top of 83e8da0, in 2b70ce2. A seek to the end left nothing to resample, so it built a passthrough, and a seek back into a span of the same format kept that passthrough and played the rest at the wrong rate. A seek now also rebuilds a passthrough whose input has samples again at a rate other than the target's. The seek property now reads and seeks up to four times, with seeks to the start and to the end made likely, and there's a test for this exact case. Both fail on 83e8da0 and pass over 20,000 layouts. This is merged into #939 (0de9f2d) and #940 (5e2b4e6) too. |
Resampler output changes depending on how the input happens to be split into spans, which it shouldn't.
fill_input_bufferstops at every span boundary and marks the read partial, so rubato zero-pads it as if the stream had ended. Decoders report a span per packet, so this happens constantly — a 48 kHz file on a 44.1 kHz device hisses the whole way through. It now reads across spans and only stops at a real end or a format change.The resamplers were also tracking input position against span length to spot boundaries, but they read ahead into their own buffer, so that count never matched where the input actually was. Filling already stops at a format change, so the tracking is gone — the passthrough keeps its own, where it's accurate. After a seek, a resampler can't know how much of the span is left, so it checks the format before each sample until the format changes, and a seek that lands in a span of another rate rebuilds it right away.
Tests cover the chunking, plus a sample rate change and a channel count change at a boundary. The chunking one fails on master.
Reading across spans also means the input runs ahead of the samples coming out. So the resampler now reports the format of those samples in
channels(),is_exhausted()andsize_hint(), andUniformSourceIteratorasks it rather than the input, so a rate change throughMixer::addno longer drops what it holds. It asks once the resampler's buffered output runs out, since that's all in one format, rather than before every sample. Downmixing still goes before resampling, behind a small wrapper that stops the resampler where the input's channel count changes. The last frames before a format change end on the input rather than falling to silence. An upmixed frame is finished in the format it started in, so a change from mono to stereo no longer swaps the channels after it.chainmoves on as soon as a source ends, rather than reportingSome(0)in between, still callingnext()on each source it leaves, so anEmptyCallbackorDonebetween two sources runs.