Piano 12 — Fitting to recordings: from MIDI and audio to model parameters
Builds on
all pages before. Runs in Julia; the result crosses to Rust as a model file (ADR-0003). Next: 13 Real-time implementation.
A physical model has hundreds of parameters per instrument (per key: tuning, , decay rates, mode pairs, hammer, strike position; plus soundboard responses). Textbook values give a generic piano. Fitting them to recordings of a real one gives that piano — and shows which mechanisms actually matter.
Datasets
| Dataset | Content | Instrument | Licence | Use for |
|---|---|---|---|---|
| Salamander Grand Piano (v3) | isolated notes, every third key (minor thirds), 16 velocity layers, release samples, pedal noises; 48 kHz / 24 bit, two AKG C414 | Yamaha C5 | CC BY 3.0 | primary fitting source: per-note analysis, velocity dependence, releases |
| MAPS (ISOL subset) | isolated notes, all keys, several velocities, with and without sustain pedal; also chords and pieces | Yamaha Disklavier (two recording setups) + software pianos | CC BY-NC-SA 2.0 FR | per-note analysis across all keys, pedal on/off comparison; research only |
| MAESTRO v3 | ~200 h of competition performances, MIDI (incl. pedals) aligned to audio within ~3 ms | Yamaha Disklaviers, concert recording | CC BY-NC-SA 4.0 | validating the whole instrument on real music: polyphony, pedalling, sympathetic resonance; research only |
| University of Iowa MIS | isolated notes at pp, mf, ff, anechoic | Steinway B | free use stated, terms not formal | cross-check against a second instrument |
| own recordings | single notes, key and pedal noises | any piano | ours | filling gaps, especially near-field and noise recordings |
Licences — ADR-0007
Recordings are never committed;
training/scripts/download.jldownloads and verifies them intodata/(sources, sizes, checksums and the MAESTRO subset:training/data-sources.toml, T-014). A fitted model file carries the licence of its most restrictive source: Salamander-only → CC BY 4.0 (the default piano in the plugin); anything from MAESTRO or MAPS → CC BY-NC-SA 4.0, published as a separate download, never compiled into the plugin. Iowa MIS: private cross-checks only.
Step 1 — Analyse single notes
For each isolated note and velocity (Julia, training/):
- Onset and partial tracking. Find the onset; take a long, high-resolution STFT; pick spectral peaks near the expected and track them over time.
- Inharmonicity. Fit to the tracked peak frequencies → and per note (04). The residuals show where the simple formula fails.
- Decay. For each partial fit the dB envelope with a two-slope model (two decay rates, the knee, the beat rate where visible) → mode-pair parameters (07).
- Velocity dependence. Compare the spectra of the same note at different velocities: the change in spectral slope fits the hammer felt (, ; 05); comb notches in the initial amplitudes give the strike position. Done for Salamander in T-035: the tilt against v4 rises by about 0.2 dB per octave of partial number per velocity step below MIDI 40 (0.12 in the low bass, ~0.3 from C3 to F#4, 0 from C6 up), and from v4 to v16 by 4–7 dB/octave in the mean. A felt law with one pulse width per layer explains the layers from F#2 to A3 (p 1.7–5.9, median 3.1) but not from C4 up, where its exponent runs to the bound (below F#2 the soft layers’ fundamental is at the floor); the strike position from comb notches is β ≈ 0.12 in the low bass and not determined above. The measured model keeps its per-layer excitation and gains the tilt below its first layer (format v4).
- Residual. Subtract the resynthesised partials from the recording. What remains — hammer knock, key thump, soundboard ring, room — is the material for the soundboard response and the noises (06, 10).
- Release. From release samples: damped decay rates per partial (09). Done for Salamander in T-039 from the string-release samples
harmL/S/V3(rel1–88are key noises): the damped rate hardly depends on the partial’s frequency, T60 1.1–1.5 s up to C4 and 1.7–2.7 s above over the first 30 dB, then a slow residual.
Done for Salamander in T-006 (items 1–3; code InstrumentsTraining.NoteAnalysis, tables in training/results/salamander/, CC BY 4.0). What the data changed:
- Tracking needs a prominence test: a mic near a node can miss the fundamental, and a noise peak accepted in its place derails the running / prediction.
- Decay: two slopes are not enough. After the prompt sound the aftersound itself beats (periods of seconds, for the whole note): the out-of-phase modes of the unison differ slightly in frequency. The fitted model is a prompt component plus a beating slow pair; 71 % of all partials need it. Prompt and slow components of one frequency interfere coherently, so a power sum of two exponentials is biased.
- Formula residuals: holds to < 1 cent in the middle and 2–5 cents in the bass; in the top two octaves only 2–4 partials are measurable and per note is unreliable.
Step 2 — Smooth across the keyboard
Recordings are noisy and Salamander has only every third key. Physical parameters vary smoothly with key number (string lengths, diameters and hammer weights are designed that way), apart from jumps at string-type breaks (single → double → triple strings, wound → plain, the bass bridge). So fit smooth curves over key number, piecewise across the breaks: , , the decay laws, the hammer parameters. This removes noise, fills missing keys, and gives physically consistent values to tweak later (“more inharmonic bass” means changing one curve).
Done for Salamander in T-006: tuning (stretch −16 cents at A0 to about +30 at C8; Salamander’s C8 sample is a semitone sharp and is ignored by the robust fit), with a break at A2 where the wound strings end, and the decay laws of the prompt and the slow component per key, fitted in the log domain because some slow components do not decay at all (curves.toml). Only one break is fitted; the string-count breaks are not visible in isolated-note data at this sampling (every third key).
Step 3 — Refine with a differentiable model
The analysis gives good starting values, but they are fitted one effect at a time. A final joint fit runs the model itself and compares its output with the recording:
- Model: the modal piano of page 13, written in Julia so it is differentiable. Decided by T-013: the per-sample recurrence in delta form (the arithmetic the Rust engine uses, and the only form that extends to the coupled hammer), differentiated with Enzyme. The closed form (a sum of damped sinusoids) gives the same gradients to 1e-11 at the same cost and stays as the test oracle. Code:
InstrumentsTraining.NoteFit. - Loss: a multi-resolution STFT loss on log magnitudes (robust to phase; Enzyme cannot differentiate FFTW, so its adjoint is hand-written and seeds the Enzyme pass), plus a term on partial frequencies, computed on the first second or two after the onset, where the room matters least. The frequency term is required: on its own the STFT loss converged from none of 12 starts 10–100 cents off.
- Start: and from the step-1 peak tracking, decays and amplitudes from steps 1–2 (for Salamander:
training/results/salamander/). Parametrise in semitones (so Adam’s step size means cents) and decay laws as logs. - Parameters: the curves of step 2, not free per-note values, so the fit can’t overfit single notes.
Run on a real note in T-036 (Salamander C4, all velocities, both mics; model InstrumentsTraining.PairFit: per partial a prompt component and a beating slow pair, an onset gate, the recording’s noise floor in the loss). What it showed:
- The model reproduces C4 to about 2 dB per partial envelope after the attack, 3–5 dB in the first 0.3 s (hammer knock and soundboard ring are not in it). The joint fit improves on step 1’s per-partial fits by only 3–12 % in the loss: on isolated notes the analysis is nearly as good as the joint fit.
- Shared decay laws cost little once converged but must start from the sample’s own step-1 rates; free per-partial rates are not unique (they trade against amplitudes).
- The beat rates are stable over velocity and mic (a property of the string); the decays depend on the mic, so the keyboard fit fits both channels jointly with shared rates.
- The beat rate has a narrow basin (about half a beat cycle over the window): the joint fit needs step 1’s beat rates as its start.
Run over the keyboard in T-037 (30 keys × 4 velocities, both mics jointly, InstrumentsTraining.KeyboardFit; results training/results/salamander/t037_*):
- Decay laws as curves over the key cost nothing measurable against per-sample laws (loss within −3 … +4 % at every key): the parameters of step 3 are curves, as planned.
- One string, two mics: shared frequencies, rates and beats with per-mic amplitudes fit both mics better than step 1’s per-mic fits at every key.
- The slow T60 comes out at about 20 s in the bass and 17 s in the middle, against step 1’s 30–45 s; the fit sees 6 s, and the tail decays more slowly than those 6 s show.
The slow tail in T-038 (the whole usable length, fitted in the envelope domain and checked in the audio domain; results training/results/salamander/t038_*):
-
Fit the whole length, up to each sample’s measured fade-out (Salamander’s fades last 2–4.5 s, longer in the bass), with the spans 0–2 s, 2–6 s and 6 s–end weighted alike: slow T60 at f0 about 30–36 s in the bass and 20–22 s in the middle; the tail after 6 s fits to 1.5–4 dB.
-
The decay law needs per-partial deviations. The partials’ own slow rates scatter ±30 % around any α + βf²: the fundamental decays much more slowly (40–52 s in the low bass, 25 s at C4), and around C4 the 2nd partial is fast and the 3rd slow. Without deviations the law’s value depends on how a loss weighs the partials, and the envelope and audio domains disagreed by up to 33 %. With a deviation curve over the key for each of partials 1–8 (penalised towards the law) they agree within 10 %. The parameters of step 3 are therefore the four law curves plus 16 deviation curves; no cost in the engine.
-
Three components per partial (prompt, beating slow pair) are enough for the whole tail.
-
What makes it work in practice: every partial up to the highest in the model (gaps are charged by the loss), STFT frames that resolve the bass partials, per-key decimation (8–24 kHz) for cost, and guarded starts from step 1 (mis-tracked mics, degenerate decompositions). The fitted piano as an instrument in T-007 (
InstrumentsTraining.ReferencePiano, modelmodels/piano/salamander-c5/): -
Levels at the attack were not measured by the earlier checks and were wrong: T-038’s prompt curve was too slow in the middle (attacks 5–10 dB quiet), and T-037’s STFT loss favours too-loud partials when a note has only a few (treble 10–57 dB loud). Fit with the attack as its own span (0–0.5 s, a quarter of the weight) and treble levels started from the recording; then within about 3 dB in the middle, and the treble within ±2 dB after the hammer knock. The fundamental of C2–F#3 falls faster than any component can (P-007).
-
Excitation per partial, response per component and mic: the hammer sets how strongly a partial is struck (velocity), and the string and soundboard decide how that splits into prompt, slow and partner and reaches each mic. Splitting per component instead broke the near-cancelling component pairs of 9 % of the strong bass partials.
-
Partial amplitudes interpolated over 6 semitones miss by 6–8 dB per envelope; the gaps Salamander leaves are 1–2 semitones.
-
Export: the fitted curves and tables go into
models/piano/<version>/as safetensors + manifest, with golden vectors, exactly as for a neural model (ADR-0003, Model pipeline).
Step 4 — Validate on performances
Render MAESTRO MIDI (notes and pedals) through the fitted model and compare with the audio: spectrogram distances, but above all listening. This tests what isolated notes can’t: overlapping notes, repeated notes, pedalling, sympathetic resonance.
What in a recording makes the difference — and should it be modelled?
| In the recording | Mechanism | Model it? |
|---|---|---|
| partials slightly sharp, octaves stretched | stiffness, tuning | yes (04) |
| brighter attack at higher velocity | nonlinear felt | yes (05) |
| fast-then-slow decay, slow beating | unisons, polarisations | yes (07) |
| uneven decay across partials | soundboard resonances at the bridge | yes, via fitted decays now, soundboard coupling later (06) |
| extra lines in ff bass | phantom partials | yes, second stage (08) |
| halo with pedal | sympathetic resonance | yes (09) |
| clicks and thumps | action and hammer noise | yes, as excitations (10) |
| colour that depends on the mic | radiation, mic placement | as virtual mics, not as physics (11) |
| long tails, smeared attacks | room reverb | no — remove from the fit (early window, or estimate the room from released notes); the user adds reverb (11) |
| hiss, hum, audience | recording | no |
| Disklavier solenoid noise, MIDI timing jitter | recording setup | no; tolerate in the fit |
| the specific piano’s tuning drift | condition of that instrument | as a parameter (unison detune, stretch), not hard-wired |
Questions the fit will answer
- How far does a modal model with mode pairs get on isolated notes, measured by the STFT loss and by listening?
- How much does the coupled hammer improve the velocity dependence over precomputed pulses?
- Where does a learned component (an SSM for the soundboard/noise residual, see Roadmap) beat the physical one?