Skip to content

feat(resynthesis): add causal low-latency formant mode - #114

Open
google-labs-stitch[bot] wants to merge 6 commits into
mainfrom
stitch/feat/causal-low-latency-formant-mode-jm0-c7fadc39-71cb-4d2b-bfcd-761bec3fe967
Open

google-labs-stitch[bot] wants to merge 6 commits into
mainfrom
stitch/feat/causal-low-latency-formant-mode-jm0-c7fadc39-71cb-4d2b-bfcd-761bec3fe967

Conversation

@google-labs-stitch

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

Copy link
Copy Markdown
Contributor

Summary

This change adds a causal low-latency resynthesis and LPC formant mode (LatencyMode::LowLatency) that reduces the nominal resynthesis and formant delay from 1,593 frames (33.19 ms at 48 kHz) to 50 frames (1.04 ms at 48 kHz) at 44.1 kHz and 48 kHz.

  • Export LatencyMode (#[non_exhaustive], Standard, LowLatency) from src/resynthesis.rs, src/lib.rs, and the generated TypeScript declarations.
  • Add latency_mode to ResynthesisConfig (LatencyMode::Standard by default; optional "standard" default in JavaScript when absent or undefined, with LatencyMode | undefined TypeScript typing for exactOptionalPropertyTypes, #123).
  • Add causal 128-frame LPC model updates and zero-delay inverse analysis (Formant::process_causal_analysis) in src/formant.rs, aligned with the reverse-order mirrored input buffer (#126), unified Stamp helpers (#20), fused envelope rotations / cache epochs (#30), and aspiration_muted / scalar parameter setters (#61).
  • Preallocate lead_periods: Box<[Q6; HISTORY]> in Resynthesis::new and use lead_period_for in move_choir_reader (#22), so self.periods keeps unmodified input periods for Formant::design and Resynthesis::estimate, and adapt reset_audio to clear_dirty_ring (#92).
  • Bypass self.overlap in Resynthesis::process_sample in LowLatency, keep the post-filter bypass path (#128), humanize / flex_tune controls (#45), scalar ParameterControl events (#61), and single-channel mono Choir and Harmony synthesis (#102), set delayed = write, delay dry by latency_frames(latency_mode) (1,593 or 50 frames, #21), and align Choir and Harmony formant_position with zero offset.
  • Keep the 2 * sample_rate formant tail in Resynthesis::tail_frames, clear lead_periods in reset_audio, and reset audio and post-resynthesis state on latency_mode changes.
  • Return 50 or 1,593 from Processor::latency_frames, Processor::feedback, and Tunelet.latencyFrames alongside Telemetry pitch fields and Float32Array voice slices (#104), ControlActivity (#135), and Float32 audio methods (#117), add JSDoc normalization rules and pack checks (#107), and update crate, Wasm, and README.md documentation.

Verification

Causal Low-Latency Formant Mode Verification

  • LatencyMode::Standard (ProcessorConfig::default()) reports 1,593 frames and renders bit-identical i16 PCM, process_planar_f32 (#117), mono in-place, Telemetry (#104), formant_envelope, and meter output compared to main (8880205) across 14 / 14 golden vectors (including humanize / flex_tune, #45, and ParameterControl events, #61) at 44.1 kHz and 48 kHz.
  • LatencyMode::LowLatency reports 50 frames from Processor::latency_frames(), Processor::feedback().latency_frames, and Tunelet.latencyFrames with formant.enabled off and on.
  • Impulse and 220 Hz vocal inputs at 44.1 kHz and 48 kHz measure 50 wet and dry frames (1.13 ms at 44.1 kHz, 1.04 ms at 48 kHz) across i16 and f32, mono and stereo Choir and Harmony with active formants (width = 0.75, stretch = 1.25), 0.0009 dB dry/wet phase difference at 480 Hz, valid formant_envelope() outputs, unmodified pitch feedback and Telemetry (formant_generation = 129) in multi-voice Choir, and zero runtime heap allocations.
  • Passed .githooks/pre-commit, .githooks/commit-msg, cargo fmt --check, cargo rustc --locked --lib --release -- -D warnings, RUSTFLAGS="-D warnings" cargo build --locked --release, cargo clippy --locked --lib -- -D warnings, RUSTDOCFLAGS="-D warnings" cargo doc --locked --no-deps, cargo rustdoc --locked --lib -- -D warnings, cargo package --locked, npm run check:wasm, npm run verify:pack, npm run verify:pack -- --ignore-scripts, and MSRV 1.88 native/Wasm builds.

- Export LatencyMode with Standard and LowLatency variants
- Add the optional latency_mode field to ResynthesisConfig
- Add causal 128-frame LPC analysis with zero inverse delay
- Store resampled choir lead periods in preallocated lead_periods
- Bypass the overlap buffer in LowLatency resynthesis
- Align Choir and Harmony formant stamps with zero offset in LowLatency
- Clear lead_periods on reset and on latency_mode changes
- Return 50 or 1,593 nominal frames from latency_frames
- Document LatencyMode in the crate docs, Wasm docs and README.md
@byteland-app

Copy link
Copy Markdown
Contributor

Review: Approved in principle — causal 128-frame Formant low-latency mode (50 frames) is well-designed, numerically stable, and backward compatible in Wasm. Please rebase onto updated main (conflicts with #21 and #128 in src/resynthesis.rs, src/processor.rs, src/lib.rs, and README.md).

Integrate upstream changes from v0.3.3 (#21, #126 and #128) with
LatencyMode::LowLatency.

The dry delay uses latency_frames(latency_mode): 1,593 frames in
Standard mode and 50 frames in LowLatency mode. Causal formant analysis
writes pre-emphasized samples into the reverse-order mirrored input
history from #126. Resynthesis keeps the post-filter bypass path from
#128 when pitch_ratio is at or below 1.0.
@google-labs-stitch

Copy link
Copy Markdown
Contributor Author

Updated the branch with main (v0.3.3, commit 451ea1e) via fast-forward push (7574ddc):

  • src/resynthesis.rs (#21 & #128): Aligned the dry delay with latency_frames(self.config.latency_mode) (1,593 frames in Standard, 50 frames in LowLatency) so dry and unity-pitch wet paths sum in phase in both modes, and kept #128's bypass_sample path on self.post when pitch_ratio <= 1.0.
  • src/formant.rs (#126): Updated Formant::process_causal_analysis to write pre-emphasized samples into the reverse-order mirrored self.input buffer (HISTORY_FRAMES - 1 - write plus the < ORDER tail mirror) before design and inverse_at(write).
  • src/processor.rs, src/lib.rs, src/wasm.rs, README.md: Resolved documentation conflicts with #21 (1,593-frame / 50-frame nominal resynthesis and dry delay, plus mono post-downmix peak notes) in ASD-STE100.

Verified 8 / 8 Standard-mode golden vectors bit-identical to v0.3.3 (stereo, drain, formant_envelope, mono in-place, and peak meters), 50-frame wet/dry onset and 0.0009 dB phase alignment in LowLatency, 0 runtime heap allocations, and all Rust/Wasm/npm pack checks.

@byteland-app

Copy link
Copy Markdown
Contributor

Round 2 review: Approved — causal 128-frame Formant low-latency mode is cleanly adapted to #21, #126, and #128. Please rebase onto updated main after #92, #102, #123, and #135.

- Clear periods with clear_dirty_ring and reset lead_periods spans (#92)
- Keep mono Choir and Harmony synthesis in LowLatency mode (#102)
- Decode undefined latencyMode to Standard for strict TypeScript (#123)
- Keep ControlActivity queries and fused formant envelopes (#30, #135)
@google-labs-stitch

Copy link
Copy Markdown
Contributor Author

Updated the branch with main (v0.3.4, commit 720690b) via fast-forward push (a91319c):

  • src/resynthesis.rs (#92, #102, #123):
    • Cleared self.periods via clear_dirty_ring and updated self.lead_periods to use inclusive dirty-span ranges (1..=written, ..=written) alongside record_history (#92).
    • Integrated zero-offset formant_offset_frames(), lead_period_for, and latency_frames-aligned dry mixing with #102's single-channel mono Choir and Harmony (forward_mono / histories_match) synthesis paths.
    • Added deserialize_latency_mode and tsify(optional, type = "LatencyMode | undefined") (#123) so absent or undefined resynthesis.latencyMode values decode to "standard" and pass --exactOptionalPropertyTypes across all four TypeScript consumer profiles.
  • src/formant.rs, src/processor.rs, src/lib.rs, src/wasm.rs, README.md (#30, #123, #135): Kept ControlActivity queries (#135), fused formant envelope rotations and EnvelopeKey caching (#30), and updated documentation for absent or undefined resynthesis.latencyMode.

Verified 10 / 10 Standard-mode golden vectors bit-identical to v0.3.4 (including mono Harmony formants and fused envelopes), 50-frame wet/dry onset and mono/stereo Choir & Harmony behavior in LowLatency, 0 runtime heap allocations, MSRV 1.88 native/Wasm builds, and all Rust/Wasm/npm pack checks.

@byteland-app

Copy link
Copy Markdown
Contributor

Round 3 review: Approved — causal 128-frame Formant low-latency mode is cleanly adapted to v0.3.4 and bit-exact in Standard mode. Please rebase onto updated main after #20, #22, #45, and #107 (adding LatencyMode rustdoc link mappings in scripts/normalize-jsdoc.mjs).

- Use HISTORY in process_causal_analysis after the formant cleanup.
- Use lead_period_for in move_choir_reader for choir voices.
- Update README and wasm.rs for latencyMode, humanize and flexTune.
- Add LatencyMode JSDoc replacements and pack checks.
@google-labs-stitch

Copy link
Copy Markdown
Contributor Author

Updated the branch with main (0850e25) via fast-forward push (e2f51ac):

  • src/formant.rs (#20): Adapted Formant::process_causal_analysis, Formant::design, and Formant::inverse_at to #20's unified HISTORY constant, Stamp (steady_bank / blend_banks), and tap_sum helpers.
  • src/resynthesis.rs (#22, #45): Updated move_choir_reader (#22) to read self.lead_period_for(delay, self.write) so secondary choir voices align to resampled lead periods while self.periods keeps unmodified input periods, and preserved #45's humanize and flex_tune controls and dwell resets.
  • scripts/normalize-jsdoc.mjs & scripts/verify-pack.mjs (#107): Added JSDoc link and field mappings for [LatencyMode::Standard], [crate::LatencyMode::Standard], [LatencyMode::LowLatency], [crate::LatencyMode::LowLatency], [ResynthesisConfig::stereo_output], [crate::ResynthesisConfig::stereo_output], and `latency_mode`, plus JSDoc, runtime, and --exactOptionalPropertyTypes pack assertions across all TypeScript profiles.
  • src/processor.rs, src/wasm.rs, README.md (#45, #117): Integrated latency_mode / latencyMode documentation and state resets alongside Float32 audio views/methods (#117) and humanize / flexTune (#45).

Verified 12 / 12 Standard-mode golden vectors bit-identical to 0850e25 (including process_planar_f32 and humanize / flex_tune), 50-frame wet/dry onset and phase alignment in LowLatency across i16 and f32, 0 runtime heap allocations, MSRV 1.88 native/Wasm builds, and all Rust/Wasm/npm pack checks.

@byteland-app

Copy link
Copy Markdown
Contributor

Review: Causal 128-frame Formant analysis, 50-frame LowLatency alignment, lead_periods separation, and Standard-mode bit-exactness look solid.

Please sync onto updated main (#139, #131, #104, #61), integrating #61's Formant::aspiration_muted, stage diff-guards, and ParameterControl, plus #104's Telemetry and verify-pack.mjs. Minor suggestion: consider marking LatencyMode #[non_exhaustive] for forward compatibility alongside the other 0.4.0 enums.

- Keep ParameterControl validation, handlers and aspiration_muted.
- Update Telemetry docs and formant_generation for low-latency mode.
- Merge LatencyMode JSDoc replacements and pack checks with Telemetry.
- Update README for latencyMode, ParameterControl and Telemetry.
@google-labs-stitch

Copy link
Copy Markdown
Contributor Author

Synced the branch onto main (8880205, including #139, #131, #104, and #61) in 7dbaa8e:

  • LatencyMode (src/resynthesis.rs, src/lib.rs): Marked LatencyMode #[non_exhaustive] alongside the other 0.4.0 enums (Telemetry, ParameterControl, ControlEvent).
  • #61 integration (src/formant.rs, src/resynthesis.rs, src/processor.rs, src/events.rs, src/wasm.rs): Integrated Formant::aspiration_muted (set_aspiration_muted, drain muting in Processor::drain_into, and reset), preserved stage diff-guards in Formant::set_config and Resynthesis::apply_config (resetting audio and post-resynthesis state when either stereo_output or latency_mode changes), and kept ParameterControl validation, scalar setters, and cached-key Wasm decoding.
  • #104 & #131 integration (src/processor.rs, src/wasm.rs, scripts/normalize-jsdoc.mjs, scripts/verify-pack.mjs, README.md): Integrated Telemetry pitch fields and Float32Array active voice slices, updated Telemetry / Telemetry::resample_ratio / Telemetry::formant_generation (441 frames in Standard, 128 frames in LowLatency) rustdoc and README.md, merged JSDoc normalization rules, and combined latencyMode / LatencyMode runtime, --exactOptionalPropertyTypes, and JSDoc checks with Telemetry and ParameterControl in scripts/verify-pack.mjs.

Verified 14 / 14 Standard-mode golden vectors bit-identical to main (8880205), 50-frame LowLatency wet/dry alignment, ParameterControl, aspiration_muted drain behavior, Telemetry (formant_generation 128-frame cadence), 0 runtime heap allocations, MSRV 1.88, and all Rust/Wasm/npm CI checks.

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