T-018 — Velocity curve and tuning: stretch from B, historical temperaments, a stepped parameter kind

Status: done (maintainer checked the plugin in a DAW, 2026-12-04) · Implementation: Codex · Area: rust · Depends on: T-008, T-016, T-025

Read for that: Piano 13 (Velocity and tuning, the parameter classes), Piano 04 (partials ), Model file (partial.freq_hz, curve.cents_et, curve.log10_B, velocity.layers), T-016 (the recompute path), T-025 (Outcome), crates/dsp/src/piano/{coeffs,exchange,engine}.rs, crates/params/src/lib.rs, crates/piano/src/{params,audio,gui}.rs.

Why: the piano plays velocity as CLAP delivers it (×127 → MIDI velocity → layer interpolation) and at the tuning of the Salamander recording, scaled by A4. Spec 001’s first controls include tuning (A4, stretch), and every keyboard player expects a velocity curve. Piano 13 plans a tuning computed from the model’s own so that octaves do not beat, plus historical temperaments.

What

  1. Velocity curve (at-strike class): input velocity → MIDI velocity before the engine’s layer interpolation; the default is the identity.
  2. Tuning (recompute class): every mode of key is multiplied by one ratio , so inharmonicity, unison beats and the layout of the model stay as measured. is the recorded deviation of the key’s first partial from 12-TET (from partial.freq_hz of component 0, partial 1); is the target:
    • Recorded: (today’s sound);
    • Stretched from B: octaves pure between the lower note’s 2nd and the upper note’s 1st partial, from = curve.log10_B: in partial-1 frequencies, an octave up adds cents; A4 is 0, A3 is one stretched octave below it, the keys between divide that octave equally, the rest follow octave by octave up and down; Stretch scales the resulting deviation from 12-TET (0 % = unstretched equal temperament, 100 % = pure octaves, up to 200 %);
    • plus a temperament: per pitch class a cents offset from 12-TET, relative to a root, normalised so that A has offset 0 (A4 stays at the A4 parameter).
  3. A stepped parameter kind in crates/params for Tuning, Temperament and Root: integer values with labels, CLAP IS_STEPPED, text from and to the labels, a combo box in the editor from the table.
  4. Factory presets: one or two that use the new controls (e.g. Werckmeister III).

Out of scope (D1): MAESTRO calibration of the velocity curve (new task T-048; its follow-up T-050 added Hardest touch beside the exponent, the input velocity that reaches the loudest strike), MTS-ESP (new task T-049), unison detune and inharmonicity scale (need a model with per-string data; with T-010/T-035), the physical hammer’s mapping of Piano 13 (format with a hammer).

Decisions (accepted by the maintainer, 2026-12-03)

QuestionOptionsProposal
D1Scope of the backlog row(a) velocity curve + tuning (stretch, temperaments) now; MAESTRO calibration and MTS-ESP each a new task; (b) all four now(a): MAESTRO is ~120 GB of audio and a Julia analysis of its own (Disklavier velocity → loudness per key), and its result is a curve this task’s parameter can already express; MTS-ESP needs ODDSound’s client library (C++, loaded at run time) and per-note retuning at any moment, which is a different path from a block-accurate recompute. Both stand on what this task builds
D2Velocity control(a) one parameter Velocity curve, −100 … +100 %, default 0, ( → or ; positive = louder for a light touch), class AtStrike, applied in audio.rs at note-on (one powf, no allocation); (b) (a) plus Softest and Hardest (the MIDI velocities and map to); (c) a curve with several points edited in the GUI(a): covers what a player adjusts; (b)‘s range is easy to add later as two more ids; (c) needs editor work that Spec 003 does not yet describe
D3Tuning controls and default(a) three parameters: Tuning (stepped: Recorded, Stretched from B), Stretch (0–200 %, default 100 %, only in Stretched), Temperament (stepped) with Root (stepped C … B, default C); default Tuning = Recorded, so the default sound, golden tests and demos are unchanged; (b) the same with default = Stretched from B, as Piano 13 says(a): the recorded tuning is a real tuner’s stretch of this very piano (−16 cents at A0, +27 at C8), arguably the best default for a measured model; Stretched becomes the default for the physical model, where nothing is recorded. Piano 13’s sentence is changed accordingly. (b) changes the sound of every existing project
D4Temperaments(a) Equal, Pythagorean, ¼-comma meantone, Werckmeister III, Kirnberger III, Vallotti, Young (1799), offsets as published (cents per pitch class from C, e.g. Werckmeister III: C 0, C♯ −9.8, D −7.8, E♭ −5.9, E −9.8, F −2.0, F♯ −11.7, G −3.9, G♯ −7.8, A −11.7, B♭ −3.9, B −7.8), shifted by Root and normalised to A = 0; applied on top of Recorded and Stretched alike; (b) fewer (Equal, Werckmeister III, meantone); (c) Scala .scl import(a): the common set of piano and harpsichord software; a table of 12 numbers each, tested against the published values. (c) belongs with MTS-ESP (T-049)
D5Where tuning lives(a) crates/dsp/src/piano/tuning.rs (as Piano 13’s layout says): a pure function key_cents(model, &Settings) -> [f64; 88] used by CoeffSet::new; Settings gains tuning, stretch, temperament, root; SettingsRequests gains their atomics; a model without curve.log10_B treats Stretched as 0 % (equal) and the plugin logs it once; (b) compute in the plugin and pass ratios(a): the engine stays usable without the plugin (demos, tests); one place for the maths
D6Stepped parameters in crates/params(a) ParamDesc gains labels: &'static [&'static str] (empty = continuous); non-empty ⇒ min = 0, max = len − 1, integer default, Scale::Linear, values rounded on every write (host, GUI, state, preset); CLAP IS_STEPPED (+ IS_ENUM), value_to_text/text_to_value by label; the editor draws a combo box; state and presets store the index as f32 (no format change); (b) a separate Scale::Stepped variant(a): one field, checked by ParamTable::new at compile time; labels never stored, so renaming a label keeps saved projects

Velocity in hosts: CLAP’s note velocity is 0..1; MIDI velocity 0 stays a note-off as now; the curve is applied after the existing max(f32::MIN_POSITIVE).

Done when

  • The gates of AGENTS.md pass, including the plugin bundle with clap-validator and the GUI smoke test (new controls).
  • Tests (written from this page): velocity curve identity at 0, monotonic, endpoints, values; key_cents with Recorded reproduces today’s frequencies bit for bit (golden test unchanged); Stretched at 0 % is 12-TET at A4; at 100 % every octave has the upper first partial equal to the lower second partial within 0.01 cents; Stretch scales the deviation linearly; each temperament’s table matches its published values, Root shifts it, A stays at 0; A4 still scales all; modes moved above Nyquist are muted (as today); a stepped parameter rounds on every path, text_to_value takes labels, ParamTable::new refuses bad stepped entries; state and preset round-trips with the new ids; no_alloc passes with a velocity-curve change and a tuning change while playing.
  • A host test (clack-host) switches Temperament while a note sounds and sees the new frequency after the recompute.
  • The default sound is unchanged: golden test, and cargo xtask render-demos produces identical files (no demo refresh needed).

Outcome

  • crates/params/src/lib.rs validates stepped descriptors, normalizes values at the cell write paths and formats/parses labels. Tests in crates/params/src/tests.rs cover all write paths (stepped_parameters_round_on_every_write), text (stepped_parameter_text_is_the_label) and invalid tables (invalid_stepped_descriptions_are_rejected).
  • crates/dsp/src/piano/{model,tuning,coeffs,exchange}.rs reads the optional B curve, computes first-partial targets and applies one frequency multiplier per key. The historical tables use Yamaha’s Pythagorean, ¼-comma meantone, Werckmeister III and Kirnberger III values, Vallotti’s six tempered fifths starting at F, and Young’s practical 1799 scheme (Young II). Yamaha HD-200 reference table, Vallotti comparison table, Young’s description.
  • crates/dsp/src/piano/tuning/tests.rs contains recorded_equal_keeps_legacy_coefficients_bit_for_bit, stretch_anchors_a4_and_is_linear_with_pure_octaves_at_full_stretch, temperaments_match_published_intervals_shift_root_and_preserve_a and stretched_coefficients_scale_a4_and_mute_modes_crossing_nyquist. The golden test passed unchanged.
  • crates/piano/src/{audio,params,gui,presets}.rs wires velocity and tuning to ids 4–8, provides table-driven combo boxes and registers crates/piano/presets/werckmeister-iii.inpiano. Tests include velocity_curve_identity_monotonicity_and_endpoints, host_temperament_automation_retunes_a_held_note, stepped_clap_metadata_labels_and_inactive_host_rounding and new_parameters_round_trip_through_state_and_preset. process_does_not_allocate now changes the velocity curve and tuning while notes ring.
  • Gates (2026-12-03): cargo fmt --check, cargo clippy --all-targets -- -D warnings, cargo nextest run (145 passed, 1 skipped), Julia Pkg.test() (2,439 passed), cargo xtask bundle piano --release, clap-validator validate (38 passed, 0 failed, 6 skipped) and scripts/gui-smoke-test.sh all green. The default sound is unchanged: golden test, and all seven demo WAVs byte-identical to the render before the change, so no demo refresh.
  • Recorded tuning keeps the model’s own A4 first partial (440.08 Hz at A4 = 440); Stretched anchors A4 exactly. Sparse log10_B knots are interpolated linearly in log10 units.

Review triage (Mistral Large, 2026-12-03)

Reviewed against this page; it re-derived the temperament tables, the stretch recursion, the velocity curve and every stepped write path and found no defects. One unverified suspicion: warn_if_stretch_unavailable logs from PianoAudioProcessor::activate. Not a defect: CLAP calls activate on the main thread (plugin.h: [main-thread & !active]), as the existing allocation there already relies on. Nothing changed.