Add Slip resampler for cheap clock-drift rate matching - #134
Merged
Conversation
Slip matches two almost-equal sample rates by occasionally inserting or dropping a frame, hidden by a short smootherstep crossfade, instead of running a full resampler. It adds no delay and no high-frequency roll-off, and its ratio is meant to be adjusted at runtime through Adjustable by a feedback loop. Several frames can be slipped per chunk (up to the crossfade spacing), so it can track up to about 10% drift.
Bind FADE to a local before asserting so the checks run on a runtime value rather than a constant expression.
The hardened capability traits added `Resampler::is_adjustable`/`is_resizable` so the capability can be queried through a shared `&dyn Resampler`. `Slip` is both `Adjustable` and `Resizable`, so it must override both to return `true`; otherwise the defaulted `false` would under-report its capabilities through a trait object. Extend the `capability_queries` test to cover `Slip`. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Companion to process_all_f64: instead of converting between two fixed rates, this drives any of the adjustable resamplers (Async sinc/poly and Slip, fixed input or output) at a nominal 1:1 ratio and applies a small user-selected rate offset given in ppm. This is the clock-drift / rate-matching case. The offset is applied through `Resampler::as_adjustable`, so one code path handles every adjustable type without knowing the concrete resampler. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Replace the fixed 8-frame smootherstep crossfade with a linear ramp whose length adapts to the chunk size, up to 128 frames. Listening tests on sustained pure tones (the worst case) showed the slip artefact is governed by the peak retiming rate of the crossfade: its spectral spread scales with 1/length, so a longer fade keeps the disturbance narrow and masked by the signal. Any velocity shaping that peaks (smootherstep is 1.875x steeper in the middle than its average) or concentrates the retiming (end-loaded curves, a hard cut) only widens it. A linear ramp is the minimum-peak and minimum-energy monotonic fade, so the old 8-frame smootherstep was in practice barely better than a hard cut. - Drop the FADE compile-time table; compute the linear weight inline. - Replace the CROSSFADE_LEN constant with MAX_CROSSFADE_LEN (128) plus crossfade_len_for(chunk), stored per instance and recomputed on resize/reset. Large chunks get the full 128-frame fade; small chunks shrink it instead of being rejected, lowering the minimum chunk size from 18 to 4. - Update the docs to match, and adjust tests for the narrower sustainable ratio range at the longer fade. Also add examples/gen_test_tones.py, the stepped-tone generator used to audition the crossfade. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Adds a new “Slip” resampler intended for cheap clock-drift compensation by occasionally inserting/dropping a frame hidden by a short crossfade, and wires it into the public API and documentation alongside the recent capability-trait split.
Changes:
- Introduce
Slip<T>resampler (implementsResampler,Adjustable,Resizable) with crossfaded single-frame slip corrections. - Add
ResampleError::RatioOutsideRangeto signal saturation when a requested ratio exceeds what a given chunk size can sustain. - Add documentation and examples for clock-drift feedback-loop usage (Rust example + helper tone generator), plus library/README cross-references.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/slip.rs | New Slip resampler implementation + unit tests. |
| src/lib.rs | Add mod slip, export Slip, and update docs/tests for capability traits. |
| src/error.rs | Add ResampleError::RatioOutsideRange + Display formatting. |
| src/asynchro.rs | Doc cross-reference to Slip’s feedback-loop example. |
| README.md | Changelog entry noting Slip addition. |
| examples/gen_test_tones.py | New helper script to generate stepped-tone input for listening tests. |
| examples/adjust_ratio_f64.rs | New example demonstrating adjusting ratio (including Slip) via Adjustable trait objects. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Address two review findings: - Slip::process_into_buffer copied `input_len`/`output_len` frames from the input buffer, but validate_buffers only guarantees `frames_to_read` frames are present. On a short final chunk (Indexing::partial_len set) that read past the validated region. Copy only `frames_to_read` frames; the remainder of the scratch is already zero-padded. - The adjust_ratio_f64 example read samples with Read::read, which may return a short read and decode a corrupt trailing sample from leftover bytes. Use read_exact and stop on UnexpectedEof. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
Slip, a very cheap resampler that matches two almost-equal sample rates byoccasionally slipping (inserting or dropping) a frame hidden by a short crossfade,
for compensating small clock differences.
Slipresampler (FixedAsync::Input/Output): no delay, no high-frequencyroll-off, very low CPU.
Adjustable, tracking up to ~10% drift. AddsResampleError::RatioOutsideRange.AdjustableandAsync.Built on top of #132 (the trait split); that should land first, and the diff here
includes #132 until it does.