Skip to content

Add Slip resampler for cheap clock-drift rate matching - #134

Merged
HEnquist merged 7 commits into
next4.0from
drift-resampler
Jul 7, 2026
Merged

Add Slip resampler for cheap clock-drift rate matching#134
HEnquist merged 7 commits into
next4.0from
drift-resampler

Conversation

@HEnquist

Copy link
Copy Markdown
Owner

Adds Slip, a very cheap resampler that matches two almost-equal sample rates by
occasionally slipping (inserting or dropping) a frame hidden by a short crossfade,
for compensating small clock differences.

  • New Slip resampler (FixedAsync::Input/Output): no delay, no high-frequency
    roll-off, very low CPU.
  • Ratio tuned at runtime via Adjustable, tracking up to ~10% drift. Adds
    ResampleError::RatioOutsideRange.
  • Worked clock-drift feedback-loop example in the docs, cross-referenced from
    Adjustable and Async.

Built on top of #132 (the trait split); that should land first, and the diff here
includes #132 until it does.

HEnquist and others added 4 commits July 6, 2026 23:15
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>
HEnquist and others added 2 commits July 6, 2026 23:37
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>

Copilot AI 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.

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 (implements Resampler, Adjustable, Resizable) with crossfaded single-frame slip corrections.
  • Add ResampleError::RatioOutsideRange to 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.

Comment thread src/slip.rs
Comment thread examples/adjust_ratio_f64.rs
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>
@HEnquist
HEnquist merged commit 5815bb9 into next4.0 Jul 7, 2026
10 checks passed
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