Skip to content

feat(processor): support 8 kHz to 192 kHz with a polyphase resampler - #53

Open
google-labs-stitch[bot] wants to merge 12 commits into
mainfrom
stitch/fixed-point-polyphase-resampler-jm0-662b6dcf-4fdc-47d8-82ae-ddddff85ab07
Open

google-labs-stitch[bot] wants to merge 12 commits into
mainfrom
stitch/fixed-point-polyphase-resampler-jm0-662b6dcf-4fdc-47d8-82ae-ddddff85ab07

Conversation

@google-labs-stitch

@google-labs-stitch google-labs-stitch Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Extend SampleRate and WasmProcessor::new (new Tunelet()) to accept all integer sample rates in 8000..=192000 Hz while SampleRate::Hz44100 and SampleRate::Hz48000 stay compatible.
  • Add a preallocated fixed-point Q14 polyphase sinc resampler (src/resample.rs) with coupled 32-bit phase accumulators, double-length contiguous FIR history rings, and zero floating-point core sample paths.
  • Bypass rate conversion at 44,100 Hz and 48,000 Hz, select the 44.1 kHz core for multiples and divisors of 44,100 Hz, and select the 48 kHz core for all other supported rates.
  • Route both i16 and normalized f32 audio and drain paths (process, process_planar, process_mono_in_place, process_f32, process_planar_f32, process_mono_in_place_f32, drain, drain_planar, drain_mono, drain_f32, drain_planar_f32, drain_mono_f32, and Wasm Float32Array and TuneletAudioViews methods) through process_into and drain_into with zero heap allocations.
  • Segment blocks at ValidatedEvents offsets (next_at and peek_offset, supporting ParameterControl and ControlEvent::Reset), pass OutputLayout across direct and resampled paths, and report latency_frames, tail_frames, processed_frames, Feedback::period_samples, and Telemetry::period_samples in host frames.
  • Validate Processor::formant_envelope and WasmProcessor::formant_envelope against the host Nyquist limit and write zero for points above half of the internal core rate when host_rate > core_rate.
  • Update rustdoc parameter ranges and error docs (Processor::new and WasmProcessor::latency_frames), normalized JSDoc / generated TypeScript declarations, scripts/verify-pack.mjs resampled-rate smoke checks, and latency and formant envelope notes in README.md.

Verification

  • Verified bit-identical 44,100 Hz and 48,000 Hz i16 and f32 output, Feedback, and Telemetry against main (8880205, including feat(events)!: add scalar parameter control events #61, feat!: add pitch fields to Telemetry and trim Wasm voice slices #104, ci: deny compiler and rustdoc warnings in scripts and docs #131, and build(cargo): bump the cargo group with 2 updates #139) across stereo, planar, mono-in-place, humanize, flex_tune, ParameterControl, multi-voice choir, harmony MIDI, formant analysis/synthesis, Punch, Warm, Alien, all six ReverbMode variants, drain, drain_mono, and drain_f32.
  • Verified single-sample impulse peaks (dry_mix = 1.0) match Processor::latency_frames() at 8000 (289), 16000 (554), 22050 (820), 32000 (1085), 44100 (1593), 48000 (1593), 88200 (3233), 96000 (3233), 176400 (6467), and 192000 (6467) Hz.
  • Verified 440.0 Hz vocal lock to Some(69.0) across integer rates from 8000 to 192000 Hz (including 44056 Hz), exact f32/i16 resampled parity, Telemetry::period_samples == Feedback::period_samples, zero heap allocations during audio/feedback/telemetry/control-activity/formant-envelope/drain/reset calls, and block-size invariance across block sizes 1 to 1024.
  • Verified formant_envelope and formantEnvelope across 16000, 44100, 48000, 48001, 88200, 96000, 176400, and 192000 Hz, confirming exact LPC magnitudes up to core Nyquist and exact zero-fill above core Nyquist without aliasing.
  • Verified WebAssembly Tunelet (Int16Array, Float32Array, and TuneletAudioViews) in Node.js and scripts/verify-pack.mjs across valid (8000..=192000 Hz) and invalid (0, 4000, 7999, 192001, 384000, 44100.5, NaN, Infinity) rates.
  • Ran cargo fmt --check, cargo clippy --locked --lib -- -D warnings, RUSTFLAGS="-D warnings" cargo build --locked --release, RUSTDOCFLAGS="-D warnings" cargo doc --locked --no-deps, cargo package --locked, npm run check:wasm, npm run verify:pack, npm run verify:pack -- --ignore-scripts, and MSRV 1.88 native/Wasm checks with zero warnings.

Automated PR initiated by user @byteland-app via Stitch Loop.

- Prepare the correction table for the internal rate at construction.
- Make CorrectionTable::set_sample_rate private to scale.
- Update the CorrectionConfig documentation for the initial table.
- Extend SampleRate and Tunelet to accept 8000..=192000 Hz integers.
- Add a fixed-point Q14 polyphase sinc resampler with 32-bit phases.
- Bypass rate conversion at 44.1 kHz and 48 kHz.
- Select the 44.1 kHz core for 44.1 kHz multiples and divisors.
- Map timed events to core frames and report delays in host frames.
- Document the sample rate range and the host latency in the README.
- Merge the latest main branch into the feature branch.
- Calculate output_gain in i64 inside Processor::next_core.
- Accumulate polyphase FIR dot products in i64 for round_q14.
- Merge the latest main branch into the feature branch.
- Share BlockBuffer across the three audio process methods.
- Keep downmix, reset_post_resynthesis and check_block from main.
@byteland-app

Copy link
Copy Markdown
Contributor

Review: Preferred over #73 for 8–192 kHz support (zero overhead at 44.1/48 kHz and constant CPU/memory at 192 kHz). Please address these items before merge:

  1. Rebase onto current main and run full CI (currently 10 commits behind with no CI run). Drop the 48 kHz initial scale fix commit now that fix(scale): initialize periods for the sample rate #28 is merged.
  2. Update WasmProcessor::latency_frames() docs in src/wasm.rs (still states '1,593 frames at both sample rates').
  3. Fix Processor::formant_envelope() Nyquist validation when host_rate > core_rate (e.g., 96/192 kHz): clamp/validate against min(host_rate, core_rate) / 2 or zero-fill above core Nyquist and document the behavior.

- Merge the latest main branch into the feature branch.
- Keep the scale period initialization from main.
- Pass OutputLayout through the direct and resampled paths.
- Update the latency documentation for the 1,593-frame dry delay.
- Write zero for formant envelope points above half of the core rate.
- Document the core rate selection on SampleRate.
- Document the formant envelope limit in Rust, Wasm and the README.
- Align the Feedback latency field documentation with Processor.
@google-labs-stitch

Copy link
Copy Markdown
Contributor Author

Addressed all three items:

  1. Synced with current main (451ea1e) and verified full CI: Merged main into the PR branch (fast-forward push without force-push), adopted upstream fix(scale): initialize periods for the sample rate #28 so src/scale.rs and src/resynthesis.rs have zero diff against main, propagated OutputLayout across both direct and resampled paths, and verified the full GitHub Actions verify workflow passes cleanly.
  2. Updated WasmProcessor::latency_frames() docs (src/wasm.rs): Replaced the two-rate wording with host-frame latency documentation matching Processor::latency_frames() (1,593 frames and matching dry delay at 44.1/48 kHz, plus scaled core delay and polyphase filter delay at other rates), and aligned Feedback::latency_frames docs.
  3. Zero-filled Processor::formant_envelope() above core Nyquist (src/formant.rs, src/processor.rs, src/wasm.rs, src/config.rs, README.md): Kept validation against sample_rate / 2 so callers can pass up to host Nyquist without knowing the internal core rate, wrote 0.0 for envelope points whose frequency exceeds core_rate / 2 when host_rate > core_rate (preventing aliased LPC mirror images at 88.2/96/176.4/192 kHz), and documented core-rate selection and the core-Nyquist zero-fill across Rust, Wasm/TypeScript declarations, and README.md.

@byteland-app

Copy link
Copy Markdown
Contributor

Round 2 review: Approved — polyphase 8–192 kHz resampler, Nyquist zero-fill in formant_envelope(), and OutputLayout adaptation are all solid. Please rebase onto updated main after #30 and #135.

- Merge main (0.3.4) with #30 and #135 into the branch.
- Fill bins above half of the core rate with zeros in Formant.
- Keep ControlActivity queries on Processor and WasmProcessor.
@google-labs-stitch

Copy link
Copy Markdown
Contributor Author

Synced with updated main (720690b, v0.3.4, after #30 and #135; merged via fast-forward push since force-push is disabled in this environment):

@byteland-app

Copy link
Copy Markdown
Contributor

Round 3 review: Approved — polyphase 8–192 kHz resampler, core-Nyquist zero-fill, and MSRV 1.88 support look solid. Please rebase onto updated main after #107 and #117 (updating process_f32/drain_f32 paths for resampled rates and adding any new rustdoc link mappings in scripts/normalize-jsdoc.mjs if needed).

- Merge main commit 0850e25 into the resampler branch.
- Route the f32 process methods through BlockBuffer and Resampler.
- Add backticks to the WasmProcessor::new range docs for JSDoc.
- Update README.md for 8000..=192000 Hz PCM and 32-bit float audio.
@google-labs-stitch

Copy link
Copy Markdown
Contributor Author

Merged updated main (0850e25, including #45, #107, and #117) into this branch in 8398369:

  • Routed process_f32, process_planar_f32, and process_mono_in_place_f32 through process_into with BlockBuffer implementations and closure-based Resampler::resample_input, while drain_f32, drain_planar_f32, and drain_mono_f32 share drain_into across direct and resampled rates (covering Wasm Float32Array and TuneletAudioViews methods as well) with zero heap allocations.
  • Formatted range tokens in WasmProcessor::new rustdoc (src/wasm.rs) so scripts/normalize-jsdoc.mjs normalizes pkg/tunelet.d.ts cleanly without requiring new link mappings (SampleRate was already mapped).
  • Verified bit-identical 44.1 kHz and 48 kHz output against 0850e25 across i16, f32, humanize, and flex_tune, resampled f32/i16 parity across 8000..=192000 Hz, zero runtime heap allocations, MSRV 1.88, and all CI checks.

@byteland-app

Copy link
Copy Markdown
Contributor

Review (local impulse verification):

  1. Off-by-1 latency error in Resampler::latency_frames (src/resample.rs): latency_frames currently returns self.input_filter.half + round((core_latency + self.output_filter.half) * host_hz / core_hz), which is 1 host frame higher than the actual end-to-end group delay at every resampled rate because build_polyphase_table uses center = (half - 1) + fraction and resample_output evaluates PHASES - lag_index.

    Verified locally with a single-sample impulse (dry_mix = 1.0):

    • 8,000 Hz: reported 290, measured peak at frame 289
    • 16,000 Hz: reported 555, measured peak at frame 554
    • 22,050 Hz: reported 821, measured peak at frame 820
    • 32,000 Hz: reported 1086, measured peak at frame 1085
    • 88,200 / 96,000 Hz: reported 3234, measured peak at frame 3233
    • 176,400 / 192,000 Hz: reported 6468, measured peak at frame 6467
    • 44,100 / 48,000 Hz: reported 1593, measured peak at frame 1593 (exact)

    Please change self.input_filter.half.saturating_add(scaled as usize) to self.input_filter.half.saturating_sub(1).saturating_add(scaled as usize).

  2. Rustdoc, smoke checks, and sync onto updated main (build(cargo): bump the cargo group with 2 updates #139, ci: deny compiler and rustdoc warnings in scripts and docs #131, feat!: add pitch fields to Telemetry and trim Wasm voice slices #104, feat(events)!: add scalar parameter control events #61):

- Merge main commit 8880205 into the resampler branch.
- Scale Telemetry::period_samples to host samples.
- Use set_aspiration_muted in drain_into across all sample rates.
- Update README.md for active voice slices and resampler latency.
- Subtract one host frame from input_filter.half in latency_frames.
- Note InvalidConfig for prepare.max_block_frames at resampled rates.
- Check invalid sample rates in verifyRuntimeImport.
- Check 8 kHz and 96 kHz audio, feedback, telemetry and drain.
@google-labs-stitch

Copy link
Copy Markdown
Contributor Author

Merged updated main (8880205, including #139, #131, #104, and #61) and addressed all items from the review:

  1. Fixed off-by-1 latency in Resampler::latency_frames (src/resample.rs): Updated the return expression to self.input_filter.half.saturating_sub(1).saturating_add(scaled as usize) and verified single-sample impulse peaks (dry_mix = 1.0) match Processor::latency_frames() across 8000 (289), 16000 (554), 22050 (820), 32000 (1085), 44100 (1593), 48000 (1593), 88200 (3233), 96000 (3233), 176400 (6467), and 192000 (6467) Hz.
  2. Updated Processor::new rustdoc, added verify-pack.mjs smoke checks, and synced with feat!: add pitch fields to Telemetry and trim Wasm voice slices #104 & feat(events)!: add scalar parameter control events #61:
    • Documented in Processor::new (src/processor.rs) that Error::InvalidConfig is also returned for prepare.max_block_frames when scaled core buffers exceed limits or fail to allocate.
    • Added invalid sample-rate checks plus 8000 Hz (Int16Array) and 96000 Hz (Float32Array + formantEnvelope) runtime smoke checks in scripts/verify-pack.mjs.
    • Scaled Telemetry::period_samples to host samples alongside Feedback::period_samples (feat!: add pitch fields to Telemetry and trim Wasm voice slices #104) and preserved ParameterControl and ValidatedEvents (next_at / peek_offset) across segmented resampled blocks (feat(events)!: add scalar parameter control events #61).

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.

1 participant