T-008 — Rust engine stage 1: the measured modal piano of format v1 in crates/dsp, matching the golden vectors
Status: done — D1–D7 accepted (maintainer, Dec 2026) · Area: rust, piano · Depends on: T-003, T-007, T-011, T-015
Read for that: Piano model file (v1: mechanisms, golden vectors), Piano 13 (key states, delta-form resonator, structure in crates/dsp), T-011 (Decision: f32 delta form, pulp K = 4, FTZ/DAZ), Plugin formats (audio-thread rules), ADR-0002, crates/model-format/src/piano.rs, training/src/ReferencePiano.jl (render_events, excitation_db, advance!: the reference to match), spikes/modal-bench/src/{bank,ftz}.rs.
Why: the fitted piano exists as a v1 model file with golden vectors (T-015), but nothing in Rust plays it. T-008 is the engine that does, in crates/dsp, framework-free, real-time safe and equal to the Julia reference within the golden tolerance. The plugin around it is T-009.
What
crates/dsp/src/piano/(Piano 13’s structure, only the files v1 needs):model.rs(the engine’s SoA layout built fromPianoModelV1),coeffs.rs(delta-form coefficients for the host rate, partials at or above its Nyquist culled),resonator.rs(the bank kernel and the A0 precision test moved from the spike),key.rs(88 key states: complex states, held, damped, active),engine.rs(events, pedal, damper rule, block processing, activity).PianoEngineimplementsProcessor:prepareallocates and computes coefficients,eventandrendernever allocate (checked withassert_no_alloc).- The golden test: the committed
salamander-c5model and its golden events rendered at each ofblock_sizes(1, 64, 513), compared withoutputwithinmax_abs_error_rel_peak(1e-4).
Decisions
| Question | Options | Proposal | |
|---|---|---|---|
| D1 | Scope: the backlog row lists Piano 13’s full stage 1 (precomputed hammer, soundboard convolution, stretched tuning) and T-017 as a dependency | (a) v1 only: the measured modal model as the format defines it — impulse excitation, complex output gains, fitted frequencies (stretch is in freq_hz), placeholder dampers; no convolution, so T-017 is not a dependency; hammer pulses, soundboard IR and tuning controls go with format v2 (T-035/T-012/T-010) and T-018; (b) build hammer pulses and convolution now with identity data | (a): the engine plays what the file holds; (b) would test machinery against a Dirac. The backlog row is rewritten accordingly |
| D2 | Events the engine needs beyond dsp::Event (sustain pedal; velocity as MIDI value) | (a) add Event::Sustain(bool) to dsp::Event; keep NoteOn.velocity in 0..1 and map it to MIDI velocity ×127 (CLAP’s float velocity; the velocity curve is T-018); SineVoices ignores the pedal; (b) a piano-only event type | (a): one event type for all processors; the plugin (T-009) forwards CC 64 |
| D3 | Kernel | (a) pulp as T-011 decided (feature x86-v4, K = 4 vectors in flight, delta form), with a scalar fallback for the tails; (b) plain scalar loops now, pulp later | (a): the decision is made and the spike’s kernel exists; the golden test runs against it, and a scalar reference kernel in the tests checks it |
| D4 | FTZ/DAZ (needs inline asm!; crates/dsp has #![forbid(unsafe_code)]) | (a) dsp::ftz::DenormalGuard (sets FTZ/DAZ, restores on drop; x86-64 and aarch64), from the spike; forbid becomes deny with one #[allow(unsafe_code)] module; the engine’s render takes the guard itself; (b) the plugin sets it (T-009), dsp stays forbid | (a): every caller (plugin, standalone, tests) gets it right without knowing; cost is two register writes per block |
| D5 | Activity: when does a key stop being computed | (a) per key, once per block: a bound over its modes below −110 dBFS (≈ 3·10⁻⁶) → states zeroed, key inactive; per-partial culling and compaction are T-029’s; (b) never (all struck keys forever); (c) per partial now | (a): bounded cost without touching the golden tolerance (1e-4 of a 0.85 peak is −81 dBFS, the threshold is 29 dB below) |
| D6 | Where the model is loaded | (a) PianoEngine::new(&PianoModelV1) builds the layout (off the audio thread); prepare builds coefficients; switching models while playing is T-016; (b) the engine loads the directory itself | (a): file I/O stays in model-format and the caller; crates/dsp depends on instruments-model-format as T-015 D2 planned |
| D7 | Output channels | (a) C = 2 → left/right; C = 1 → both; C > 2 refused at new with an error; (b) any C, mixed down | (a): v1’s Salamander model has two mics; a mic blend is a later control (T-020) |
Done when
Tests first, in crates/dsp/tests/ and the modules: the golden test at block sizes 1, 64 and 513 within 1e-4 of the peak; the pulp kernel equals a scalar delta-form reference to f32 rounding on random banks (lengths not multiples of the vector width); the A0 precision test (≤ 0.1 cent, ≤ 1 % amplitude after 20 s at 44.1/48/96 kHz); a partial at or above Nyquist at 44.1 kHz is culled; a re-strike adds to the ringing state; damper rule (held, pedal, no-damper key); a struck key goes inactive after its decay and is_active turns false; event and render allocate nothing. All gates green; the plugin is unchanged apart from matching the new event variant.
Pages: Piano 13’s structure marks what exists; the Piano model file page names the engine as the v1 consumer; backlog row (scope per D1) and Status.
Out of scope: the plugin (T-009), parameter swaps (T-016), convolution (T-017), velocity curve and tuning controls (T-018), quality levels and per-partial culling (T-029), the physical model (format v2).
Outcome
- Code:
crates/dsp/src/piano/—model.rs(PianoModel: per key its modes in the order string, polarisation, partial, with excitation per layer;ModelError::TooManyChannels),coeffs.rs(CoeffSetfor one rate: the bank, each key’s span padded to 16, excitation in bank order, modes at or above Nyquist left out),resonator.rs(Bank,Span,delta_coeff, the pulp kernelprocesswith K = 4 and its scalar referenceprocess_scalar),key.rs(KeyState: held, active, the damper rule),engine.rs(PianoEngine,ACTIVITY_THRESHOLD).crates/dsp/src/ftz.rs:DenormalGuard(D4; the crate isdeny(unsafe_code)with this one module allowed).dsp::Event::Sustain(bool)added (D2);SineVoicesignores it.crates/dspnow depends oninstruments-model-formatandpulp0.22 (x86-v4), both workspace dependencies. - Golden vectors: the committed
salamander-c5matches at block sizes 1, 64 and 513 with a largest error of 3.8e-5 / 4.5e-5 of the peak (left / right) against the tolerance 1e-4; identical at every block size. The margin is about 2×: the Julia reference renders from itsf64tables, the file (and so the engine) holdsf32frequencies, whose rounding shifts a 4 kHz partial’s phase by ~3 mrad over 2 s. If a future model fails narrowly, render the golden output from thef32-rounded tables in Julia rather than loosen the tolerance. - Tests (
crates/dsp/tests/piano.rs, unit tests inresonator.rsandftz.rs): golden; SIMD kernel = scalar reference on random banks (spans of 1, 3, 5, 6 vectors, damped and free, block lengths 1–64); A0 precision rule at 44.1/48/96 kHz; a re-strike equals the sum of the two strikes; dampers (released key loses > 60 dB of tail energy, the pedal and a key without damper keep it); a released key goes inactive; fewer modes at 44.1 kHz than at 48 kHz and finite output at 44.1/96 kHz; keys off the keyboard,AllNotesOff, velocity-0 note-on;eventandrenderunderassert_no_alloc. - Cost (release, this VM, AVX-512): 44 keys struck with the pedal down (7,904 modes in the bank) render at RTF 0.027 on one core, in line with T-011.
- Gates:
cargo fmt --check,cargo clippy --all-targets -- -D warnings,cargo nextest run(43 tests). No plugin code changed (the plugin still playsSineVoices; T-009 swaps in the engine and forwards CC 64), no Julia code changed.
T-041 uses a fixed-size active span array so cloned engines also render without allocation.