Skip to content

fix!: crossfade voice cursors and ramp live stage changes - #74

Open
google-labs-stitch[bot] wants to merge 14 commits into
mainfrom
stitch/stage-ramps-and-voice-crossfades-jm1-a3009cce-0678-49e0-92fa-6bd0b4514faa
Open

google-labs-stitch[bot] wants to merge 14 commits into
mainfrom
stitch/stage-ramps-and-voice-crossfades-jm1-a3009cce-0678-49e0-92fa-6bd0b4514faa

Conversation

@google-labs-stitch

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

Copy link
Copy Markdown
Contributor

Summary

This change adds local Q14 stage ramps, validity counters, and 16-voice cursor crossfades so live parameter updates make smooth audio transitions without heap allocations after construction. The ! mark targets the 0.4.0 release window because multi-voice period wraps, live stage transitions, and tail_frames transition budgets change output samples relative to 0.3.x.

  • Crossfade period wraps and cursor jumps across all 16 Choir and Harmony voices (including #61 ParameterControl::ChoirVoices and ParameterControl::ChoirDelay updates), and silence secondary Choir readers until fresh frames from render_lead arrive.
  • Backfill 491 pre-emphasized samples when formant.enabled becomes true, maintain a persistent raw_filtered history for anti-aliased transition taps, track per-slot LPC state in overlap_lpc with a 256-frame lookahead_raw window, and step formant_fade over 256 frames without single-frame jumps on rapid toggles.
  • Remove the audio resets on resynthesis.stereo_output updates, copy channel zero formant state, ramp Q14 pan weights over 256 frames, and keep #102 mono fast paths and #104 Telemetry pitch reporting.
  • Capture a fresh loop in Alien without buffer clears, hold active_loop_len constant across 200-frame wet fade-outs before applying new loop lengths, and set the boundary fade length to 0 at loop_frames of 1 and min(200, loop_frames / 2) for longer loops.
  • Preserve #86, #21, and #61 reverb topologies and narrow setters, ramp the reverb wet gain over 256 frames across ReverbMode switches, ramp the network input to zero over 256 frames on disable before bypass tails decay and clear, keep reverb_wet_peak active during bypass tails, and document active transition and bypass budgets in Processor::tail_frames and tailFrames.

Verification

Stage transition verification

  • 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, and MSRV 1.88 native and wasm32 builds.
  • Verified 128-frame AudioWorklet block time (2,666.7 us budget at 48 kHz, 5 Harmony voices with crossfades at pitch_ratio = 2.0): 37.0 us steady median, 144.2 us transition p95, and 208.0 us max.
  • Verified rapid formant.enabled double-toggles (true -> false -> true across 1..=950 frame gaps), Alien mid-playback loop_frames grow/shrink transitions (max_abs = 0 on zero input after pre-enable history), and Reverb Echo disable input ramps (max |delta| = 64 PCM counts across the +4,000-frame tap) and active bypass reverb_wet_peak.
  • Verified bit-exact ParameterControl equivalence against stage ControlEvents and bit-exact steady-state PCM and telemetry parity against main (8880205), with 0 post-construction heap bytes.

- Crossfade period wraps and cursor jumps across all 16 voices.
- Silence secondary Choir voices until fresh lead frames arrive.
- Backfill the formant history when the formant stage becomes active.
- Crossfade delayed raw audio with LPC output after the overlap delay.
- Ramp the Q14 pan weights over 256 frames on stereo switches.
- Capture a fresh Alien loop before the 200-frame wet ramp.
- Ramp the Alien wet mix over 200 frames on each toggle.
- Set the Alien boundary fade to zero for a one-frame loop.
- Ramp the reverb wet gain over 256 frames across mode switches.
- Clear the reverb delay buffer and filters between modes.
- Feed zero input into the reverb until a bypass tail finishes.
- Include active transition and bypass tails in tail_frames.
- Update README.md for the continuous stereo_output behavior.
- Merge the latest main commits into the feature branch.
- Resolve the conflicts in reverb.rs and resynthesis.rs.
- Use Q14.abs and i64 mono sums in Reverb.
- Ramp mono harmony_gain with stereo_fade in Resynthesis.
- Merge the latest main commits into the feature branch.
- Resolve the conflicts in processor.rs and resynthesis.rs.
- Keep the stereo_output pan ramps without a stage reset.
- Share the reader limits across the harmony slots in Resynthesis.
- Skip the history interpolation for silent harmony voices.
- Merge the latest main commits into the feature branch.
- Resolve the conflict in src/resynthesis.rs.
- Update the two active position slices in process_sample.
@byteland-app

Copy link
Copy Markdown
Contributor

Review: Much better architecture than #54 (zero extra large delay buffers, zero idle LPC CPU burn, proper Alien capture handling, and Reverb bypass tail accounting). Please rework onto updated main (currently conflicting with 7eee352, 1668f6f, 7490b83, and #21/#111/#126):

  1. Preserve recent performance wins (dirty-span resets and zero-copy Formant::forward borrows).
  2. Deduplicate the ~300-line copy of the 6 reverb topologies in Reverb::process_sample and align with fix: align dry delay, stereo reverb and mono meters #21's phase/dry fixes.
  3. Fix the Formant transition fallback bypassing self.post anti-aliasing on upward pitch shifts and hard-cutting aspiration on frame 0.
  4. Consider splitting into 2–3 smaller PRs (voice/stereo fades, Alien/Reverb fades, Formant transition).

- Merge the latest main commits into the feature branch.
- Keep the dirty-span resets in Alien, Reverb, Formant and Resynthesis.
- Share one network evaluation in Reverb with stereo dry and wet peaks.
- Fill the reverse formant history and mirror tail on stage enable.
During formant transitions, the raw fallback path bypassed the
anti-alias filters, and `Formant::add_noise` stopped aspiration on
frame zero of disable.

- Apply the `post` anti-alias filters to delayed `raw` taps during
  formant transitions.
- Wait for settled `post` filter states before a reader returns to
  `filtered`.
- Keep aspiration active until `formant_fade` decreases to zero.
@google-labs-stitch

Copy link
Copy Markdown
Contributor Author

Merged main (451ea1e, 0.3.3) without history rewrites and resolved the review findings:

  1. Dirty-span resets and zero-copy formant borrows (7490b83, 1668f6f, #126):
    • Kept written and dirty reset guards across Resynthesis, Formant, Alien, and Reverb.
    • Kept zero-copy Formant::forward bank borrows and mirrored input and history layouts in Formant::prepare_enable.
  2. Reverb topology deduplication and #21 alignment (src/effects/reverb.rs):
    • Extracted Reverb::step_network so steady-state and transition frames share one copy of all six topologies.
    • Kept #21 stereo dry terms across all modes, in-phase Echo and MediumHall taps, and stereo/mono reverb_wet_peak measurement.
  3. Formant transition anti-alias filters and aspiration ramp (src/resynthesis.rs, src/formant.rs):
    • Applied the post anti-alias filters (interpolate_raw_filtered) and waited for settled post states (filtered_tap_ready) during formant transitions.
    • Kept Formant::add_noise active across the 256-frame formant_fade disable ramp.

@byteland-app

Copy link
Copy Markdown
Contributor

Round 2 review: Thank you for preserving #21/#111/#126 and dirty-span resets, and for ramping aspiration on formant disable. Two technical bugs still block merge:

  1. Severe real-time CPU spike in src/resynthesis.rs interpolate_raw_filtered: when pitch_ratio > 1.0 during formant transitions, each interpolation call resets two Biquads and runs 130 warmup samples (260 biquads). With 5 harmony voices x 2 crossfade cursors, that reaches up to 2,600 biquads/sample (332,800 per 128-frame AudioWorklet block, ~1,300x steady state). Please maintain a lightweight persistent filtered-raw history instead of re-warming 128 frames per lookup.
  2. Rapid double-toggle click: toggling formant.enabled true -> false -> true while overlap_pending > 0 clears filtered_valid and forces lpc_ready = false, jumping formant_fade straight to 0.0 in one frame.
  3. Update tail_frames / tailFrames docs in src/processor.rs and src/wasm.rs (they still claim it only reflects configuration), rebase onto updated main (perf(reverb): use a fixed ring with inline taps #86/perf(resynthesis): synthesize one channel when stereo is off #102), and target the 0.4.0 window (ideally split into smaller PRs).

- Keep the fixed-array delay ring and FIR sum in reverb
- Keep single-channel synthesis when voice pan is off
- Keep shared ring helpers, envelope cache and control activity
- Filter delayed raw input through persistent biquads
- Read raw transition taps without per-lookup warmups
- Pause the raw filter path when formants settle off
- Track per-slot LPC state and raw lookahead in the overlap ring
- Step the formant fade smoothly without single-frame resets
- Keep active model banks intact when formants turn on again
- Document transition ramps and bypass tails in tail_frames
- Update the Wasm tailFrames documentation and README table
@google-labs-stitch google-labs-stitch Bot changed the title fix: crossfade voice cursors and ramp live stage changes fix!: crossfade voice cursors and ramp live stage changes Nov 25, 2026
@google-labs-stitch

Copy link
Copy Markdown
Contributor Author

Merged main (720690b, 0.3.4) in cbe9089 (preserving #86, #102, and #135) and resolved all three Round 2 items:

  1. Persistent raw_filtered history (fa9c4b4, src/resynthesis.rs, src/resynthesis/history.rs):
    • Replaced the per-call 128-frame biquad warmup in interpolate_raw_filtered with a persistent raw_filtered ring (Box<[Q14; HISTORY]>) and raw_post biquad pair ([Biquad; 2]), so each cursor lookup reads three taps in O(1).
    • Paused raw_filtered updates (raw_filtered_active = false) once formants settle off so steady-state bypass runs zero extra biquads, and copied self.post into self.raw_post when formant.enabled turns on.
    • Measured 128-frame block time at 48 kHz (2,666.7 us budget, 5 Harmony voices with active crossfades, pitch_ratio = 2.0): transition p95 dropped from 4,000.3 us (8,202.4 us max) to 144.2 us (208.0 us max, 37.0 us steady median).
  2. Smooth ramps across rapid formant.enabled double-toggles (4903538, src/formant.rs, src/resynthesis.rs):
    • Replaced overlap_pending and overlap_mixed with a per-slot overlap_lpc bitset ([u64; 25]) and a sliding 256-frame lookahead_raw counter so filtered_valid tracks actual contiguous slot types and formant_fade ramps down ahead of any raw island.
    • Added step_formant_fade so formant_fade moves only by STAGE_RAMP_STEP (1/256 per frame) without single-frame snaps to 0.0, kept active LPC banks intact in Formant::prepare_enable, and ran Formant::reset_synthesis only when formant_fade leaves 0.0.
    • Verified true -> false -> true toggles across 1..=950 frame gaps: max_delta_d2 dropped from 5,188 PCM (gap = 64) to 350..368 PCM.
  3. tail_frames / tailFrames documentation and 0.4.0 target (7b7741f, PR title):
    • Updated Processor::tail_frames (src/processor.rs), WasmProcessor::tail_frames (src/wasm.rs), src/lib.rs, and README.md to state that the finite tail budget includes active Alien ramps, reverb mode fades, and reverb bypass tails.
    • Marked the PR title fix!: crossfade voice cursors and ramp live stage changes to target the 0.4.0 release window (note: because GitHub branch history on PR fix!: crossfade voice cursors and ramp live stage changes #74 already combines the stage transitions and force-push is disabled here, all fixes stay together on this 0.4.0 PR).

- Keep the Stamp and HISTORY helpers in src/formant.rs
- Keep the 16-voice crossfades in Resynthesis::move_choir_reader
- Use Resynthesis::apply_glottal in the formant transitions
- Keep the f32 drain methods and tail_frames documentation
- Keep the Float32 API rows and tailFrames label in README.md
@byteland-app

Copy link
Copy Markdown
Contributor

Review (local verification):

  1. Stale-buffer burst in Alien::set_config (src/effects/alien.rs): set_config updates self.loop_len = config.loop_frames as usize immediately even when self.wet_frames > 0 (restart_pending = true or fading out). Increasing loop_frames mid-playback stops phase from wrapping at the old loop length during the 200-frame fade-out, so reverse_sample() walks backwards past the capture start into pre-enable buffer audio.

    Verified locally: enable Alien on loud audio (20000), disable and flush with silence (dry_mix = 1.0), re-enable with loop_frames = 1000 on 1,998 pure zero frames (so phase = 999), then increase loop_frames = 20_000 while feeding pure zeros — output immediately produces a 19,798-amplitude burst on frame 1. Also, restarting capture via restart_pending contradicts AlienConfig's rustdoc ("A change of the loop length keeps the captured history and clamps the current phase into the new loop").

    Please keep active_loop_len until wet_frames == 0 (matching Reverb::active_config) and align AlienConfig's rustdoc with the actual loop-length change behavior.

  2. Minor Reverb transition notes (src/effects/reverb.rs):

    • wet_peak stops updating when self.config.enabled becomes false, so Telemetry::reverb_wet_peak reports 0.0 during an audible bypass tail after take_telemetry(). Please continue measuring active bypass tails in wet_peak.
    • On disable, (net_left, net_right) drops to zero in a single sample instead of ramping with enable_fade, which creates a hard step 4,000 frames later in Echo mode on loud sustained input.
  3. 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): Preserve feat(events)!: add scalar parameter control events #61's stage diff-guards/narrow setters and feat!: add pitch fields to Telemetry and trim Wasm voice slices #104's Telemetry pitch fields.

- Merge upstream main at 8880205 into the transition ramp branch.
- Combine diff-guarded stage setters and parameter events with ramps.
- Keep smooth choir delay crossfades and voice cursor initialization.
- Preserve pitch telemetry fields without stereo output resets.
- Hold active_loop_len constant while wet_frames is above zero.
- Ramp active wet playback to zero before a new loop length applies.
- Count post-enable input during loop-length fades without stale reads.
- Update AlienConfig and Telemetry loop documentation.
- Update reverb_wet_peak while an active bypass tail renders audio.
- Ramp network input to zero over 256 frames when reverb disables.
- Include the input ramp in bypass_remaining before zero-input decay.
- Update ReverbConfig, Processor and Telemetry documentation.
@google-labs-stitch

Copy link
Copy Markdown
Contributor Author

Addressed all three items and merged main (8880205):

  1. Alien active_loop_len across wet fade-outs (6c01ae2): Alien now holds active_loop_len constant whenever wet_frames > 0. Changing loop_frames during wet playback ramps the active loop to zero over 200 frames while wrapping phase at active_loop_len and counting valid post-enable input frames (captured), then updates active_loop_len, resets phase = 0, and captures any remaining frames before ramping the new loop in (0 stale-buffer leakage on both 1,000 -> 20,000 at phase = 999 and 2,998 -> 2,500, and alien_phase_frames < alien_loop_frames on every frame). Updated AlienConfig and Telemetry::alien_loop_frames docs accordingly.
  2. Reverb bypass reverb_wet_peak and disable input ramp (942d298): Removed the self.config.enabled guard around the wet_peak update in Reverb::process_sample (idle bypass already returns early) so Telemetry::reverb_wet_peak continues measuring audible bypass tails and reports 0.0 once idle. Also scaled (net_left, net_right) by enable_fade on disable and included ramp_frames(enable_fade) in bypass_remaining so disabling Echo on sustained input ramps smoothly over 256 frames instead of stepping 4,000 frames later.
  3. Merged main at 8880205 (20d3d8e): Integrated #139, #131, #104 (Telemetry pitch fields, with Telemetry::resample_ratio docs updated for non-resetting stereo_output), and #61 (ControlEvent::Parameter and stage diff-guards, keeping set_choir_voices/set_choir_delay and Reverb's narrow setters bit-identical to stage ControlEvents across active transitions).

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