T-048 — Velocity curve calibrated on MAESTRO: Disklavier velocity → attack loudness, fitted to the model’s layers

Status: done · Implementation: Claude · Area: julia · Depends on: T-018

Read for that: T-018 (D2: Velocity curve , , MIDI velocity ), ADR-0007 (MAESTRO-derived data is CC BY-NC-SA 4.0, never compiled into the plugin), T-014 (the MAESTRO subset), T-019 (D3 preset format, D4 user preset directory), training/scripts/t007_reference_piano.jl (LAYER_VEL, render_events), crates/dsp/src/piano/model.rs (velocity_weights).

Why: the model’s dynamics come from Salamander’s four velocity layers (MIDI 40, 60.5, 92.5, 124, with a fixed dB slope below the first). MIDI from a real keyboard, and above all MAESTRO’s Disklavier performances, which Piano 12’s step 5 renders for validation, assume a different velocity → loudness law. A curve fitted on MAESTRO makes those renders, and a Disklavier-like keyboard, sound at the right dynamics.

What

  1. Data: extend the MAESTRO audio subset of training/data-sources.toml (D3).
  2. Measurement (training/scripts/t048_velocity.jl measure): read each piece’s MIDI (a small SMF reader: note on/off, CC64), pick isolated onsets (no other onset within 150 ms before or 120 ms after) of keys C2–C7, and measure the attack level in the key’s harmonic bands 1–6 below 5 kHz (centred on the reference model’s partials, ± min(0.35 , 80 Hz); Hann window 10–110 ms after the onset, both channels’ power averaged, the band power 110–10 ms before the onset subtracted). A band is used if no note struck within the last second has a partial within it (+10 Hz) and the onset rises ≥ 10 dB above the pre-onset power in it; older ringing notes are removed by the subtraction. An onset is dropped if its own key still rings. Output: data/t048/onsets.csv (piece, year, key, Disklavier velocity , used bands, level dB).
  3. Model levels (model): the same feature, per band, on renders of the reference model (Julia render_events on T-007’s build_tables(), the tables of the compiled-in model) for keys C2–C7 at every MIDI velocity 1–127 → data/t048/model.csv; an onset’s model level sums the bands it used.
  4. Fit (fit): find minimising the Huber loss (2 dB) of with a free gain per piece and per key (weighted least squares for the offsets inside IRLS, grid and golden-section search for ); report , the residual, per year, and per velocity bin the residual and the measured curve (the velocity that zeroes the bin’s median residual), as tables and a plot in training/results/maestro/.
  5. Result (D1): a preset presets/maestro/maestro-velocity.inpiano with the fitted Velocity curve, a LICENSE (CC BY-NC-SA 4.0) and the MAESTRO attribution beside it; the README there says to copy it into the user preset directory (T-019 D4). Not compiled into the plugin; the default stays 0.

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

QuestionOptionsProposal
D1Where the result goes (the backlog row says “default or factory preset”)(a) a separate preset file under CC BY-NC-SA 4.0, outside crates/, plus the report; the default stays 0; (b) MAESTRO only as evidence, the default set by hand; (c) amend ADR-0007(a): ADR-0007 forbids MAESTRO-derived data in the plugin binary, and factory presets are compiled in; a user preset file is the “separate download” the ADR allows
D2Curve shape(a) fit T-018’s single exponent; if the residual of the shape is large (median per-year RMS > 1.5 dB, or a systematic bend in the residual over ), record a follow-up task for a richer curve; (b) a new multi-point curve parameter now(a): needs no Rust change; whether one exponent suffices is exactly what the fit shows
D3Data(a) extend the audio subset to the five shortest test-split recordings of each year (≈ 50 pieces, ≈ 1.5 GB), read by range requests as in T-014; (b) keep T-014’s ten pieces(a): isolated onsets in polyphonic music are a small fraction of all notes; ten pieces give too few per key and velocity. Test split, so no overlap with later training

Only the shape is matched (how loudness changes with velocity), not absolute level: the per-piece and per-key offsets absorb recording chains, mastering and the voicing of the Disklaviers against the Salamander piano (per-year and per-octave offsets, the first plan, left ±10 dB of per-key voicing in the residual). The 2015 recording from mp3 is used (attack band levels below 5 kHz are unaffected), but checked per year in the report.

Done when

  • t048_velocity.jl measure | model | fit run from the downloaded data; the report gives , , the number of onsets per year and octave, the residuals, and a plot of level vs velocity (data and fitted model) per year.
  • Tests (in training/test/, written from this page): the SMF reader on a hand-made file (running status, tempo change, CC64); isolated-onset selection on a synthetic note list; the band-level feature on a synthetic signal with a known amplitude ratio (±0.5 dB); the fit recovers a known from synthetic data with random offsets.
  • The preset loads in the plugin’s preset tests’ parser (a test reads presets/maestro/*.inpiano), and its value lies within the parameter’s range (clamped and reported if the fit leaves it).
  • Gates of AGENTS.md (Julia; Rust only for the parser test).

Outcome

Result: , Velocity curve −25.6 %, in presets/maestro/maestro-velocity.inpiano (CC BY-NC-SA 4.0, with LICENSE, ATTRIBUTION.md and a README on installing it as a user preset). From 1,136 onsets of 44 of the 50 pieces (114,397 notes, 5,820 isolated; dropped: own key ringing 1,887, all bands overlapped by recent notes 2,567, no band rising 10 dB 116, outside C2–C7 114). The per-year exponents agree: 1.14 (2013) … 1.71 (2015, 11 onsets), most 1.36–1.57.

One exponent is not enough (D2 → T-050 in the Backlog). The Huber-clamped RMS residual drops only from 3.79 dB (default curve) to 3.68 dB; per-year RMS is 2.2–3.4 dB (above D2’s 1.5 dB), and the residual bends with velocity. The measured curve (training/results/maestro/t048_velocity.png):

Disklavier onsetsmean residual dB at fitted measured
1–2940−6.66.018.6
30–44224+0.321.923.0
45–54230+2.033.129.4
55–64257+1.143.139.6
65–74214−0.453.856.0
75–84114−2.065.174.6
85–9442−2.977.194.6
95–10915−6.192.9121.7

So the Disklaviers’ soft range (u 30–65) sounds softer than the model plays it, the softest notes (u < 30) louder, and the top louder. The bottom of this curve lies where the model has no layer (below MIDI 40 it extrapolates with T-007’s guessed 40 dB/decade) and the top near its last layer (124): part of the bend may be the model’s dynamics rather than the curve. T-050 decides between a shaped (multi-point) velocity curve and refitting the model’s level outside its layers.

What was built:

  • training/src/VelocityFit.jl: read_smf (format 0/1, running status, tempo map, CC64, restrikes), isolated, sounding_until, band_power (Hann, scaled so a sine of amplitude gives ), fit_gamma (Huber IRLS, offsets by pivoted QR so groupings that do not connect still solve), curve_c/curve_gamma. Tests in training/test/velocityfit.jl (hand-made SMF bytes, synthetic onsets, a two-sine signal, a known under offsets, noise and outliers).
  • training/scripts/t048_velocity.jl model | measure | fit (~12 s, ~10 s, ~40 s with -t 8); MIDI is taken from the MIDI package zip into data/t048/midi/. training/scripts/t048_plot.py draws the plot (matplotlib in a venv). Results with LICENSE and README in training/results/maestro/.
  • training/data-sources.toml: 40 more MAESTRO members (five shortest test-split recordings per year, 1.90 GB in all), checksums recorded; download.jl verify maestro gives 52 ok.
  • crates/piano/tests/host.rs: maestro_velocity_preset_loads_from_file parses the preset, checks its model and range, and loads it through preset-load from the file.
  • Six pieces give no onset: dense pieces with few isolated notes (one each of 2006, 2008, 2014 and three of 2015). 2015, the mp3-sourced year, contributes 11 onsets, too few to change the fit; its spectrum was not checked further.
  • Gates (2026-12-03): cargo fmt --check, cargo clippy --all-targets -- -D warnings, cargo nextest run (160 passed, 1 skipped), Julia Pkg.test() (2,472 passed; the dataset test’s member count 10 → 50). No plugin or GUI code changed, so no bundle or GUI gate.

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

Reviewed against the page as first written (its sandbox refused Julia, so it checked the numerics with Python replicas: band_power’s scaling, the tempo map, the fit’s recovery of a known , all 50 MIDI files).

PointRight?Done
const PRE redefined: T-036’s script, included through T-007’s, already binds PRE in Mainplausible (Julia 1.13 accepted it and the run worked, but older code reading PRE would see the tuple)renamed PRE48/POST48; model, measure, fit rerun with byte-identical outputs
read_smf keeps running status across meta and sysex events (SMF 1.0 cancels it)yes (failing test first)fixed, test kept
read_smf does not check the format byte (format 2 would be merged)yes (failing test first)refused now, test kept
windows, rise threshold, offsets per piece and key, results path differ from the pagethe page was out of date, not the codepage’s What updated to the code, with the reason for per-key offsets
all bands use the key’s half-widthby design (± min(0.35 , 80 Hz) per key)—
D2’s follow-up task missingwas written after the review startedT-050