T-037 — The keyboard-wide differentiable fit: decay laws as curves over the keys, both mics jointly

Status: done · Area: julia, piano · Depends on: T-036

Read for that: the T-036 page (model PairFit, results and lessons), Piano 12 steps 2–3, the T-006 page (curves, the break at A2, the C8 sample).

Why: Piano 12 step 3 asks for the joint fit to run over curves, not free per-note values, so the parameters are smooth over the keyboard, fill the keys Salamander lacks (it samples every third key), and cannot overfit one note. T-036 ran the model on one key and left three lessons: (1) the decays depend on the mic when each mic is fitted alone, so fit both mics together with shared frequencies and rates; (2) shared decay laws must start from the sample’s own T-006 rates (from keys.csv one fit diverged); (3) the beat rate’s basin is narrow, so beat rates start from T-006. T-007 (the reference piano) builds on this task’s curves.

What

  1. Two-mic model: PairFit with frequencies, rates and beat rates shared by both channels; amplitudes and phases per channel (three per partial each) and the onset per channel (the mics are at different distances). Tests as in T-036 on a synthetic two-channel note.
  2. Stage A, per note: every chosen sample (E3) fitted alone with its own decay laws, both mics jointly, laws started from the sample’s T-006 rates (log-domain fit), beat rates and amplitudes from T-006. This is the best the model does per note, and the start of stage B.
  3. Stage B, curves: the decay-law coefficients become curves over the keys (E1); all samples are fitted together (their losses add; given the curves they are independent, so they run in parallel). Started from a smooth fit to stage A’s laws.
  4. Report: the cost of the curves (stage B’s loss per key vs stage A’s), where they fail (keys, registers, the A2 break), the two-mic result (do shared rates fit both mics? the T-036 question), the curves at all 88 keys against T-006’s, timings; renders of a few keys to listen to.
  5. Results committed to training/results/salamander/ (CC BY 4.0): the curves (TOML, like curves.toml) and per-sample parameters (CSV), with the README updated.

Decisions

All five proposals accepted by the maintainer (Nov 2026).

QuestionOptionsProposal
E1What becomes a curve over the keys(a) only the decay laws (); per note , , amplitudes, phases, beat rates, onset; (b) also (and tuning) as curves; (c) everything that varies smoothly, beat rates included(a): and per note are measured to 0.35 cent and 1–2 % (T-006) and the frequency term pins them, and tuning is a property of this instrument’s state, not of its design; beat rates scatter over partials with no trend (T-036). The law curves as in T-006: quadratic in octaves, break at A2 (key 45), of each coefficient. stays per note except in the top two octaves, where T-006 could not measure it (there: T-006’s curve, held fixed)
E2Mics(a) both jointly, shared frequencies and rates, amplitudes/phases/onset per mic; (b) each mic alone, as T-036(a): T-036 lesson 1; the joint fit will show whether three shared components per partial explain both mics (if not, that is the result, and the model needs more components)
E3Which velocity layers(a) all 16; (b) four (v4, v8, v12, v16); (c) one (v8)(b): the laws should not depend on velocity (linear strings), so four layers suffice to average over and to check that; it cuts the cost 4×. The quietest layers are near the floor in the treble. All 30 keys, C8 included (its is per note, so its semitone-sharp sample does not matter)
E4Window and cost per register(a) 6 s for every key at 48 kHz; (b) window = what the sample allows (≤ 6 s, without the fade: A7 2–3 s, C6 ~4 s), and each key decimated to a rate set by its highest fitted partial (8 kHz in the bass, 12–24 kHz in the middle, 48 kHz at the top), STFT sizes scaled to keep their duration(b): the bass holds most of the work (A0–D#2: ~300 of the ~550 partials per sample set) but its partials end near 1.5–3 kHz. Estimate from T-036’s 1.25 s per step (16 partials, 6 s, 48 kHz): (a) ~45 s per step per layer and mic, 400 steps × 4 layers × 2 mics ≈ 40 thread-hours per stage; (b) ~11 s, ≈ 10 thread-hours per stage, ~3 hours per stage on the 4-vCPU VM. Decimation by FFT resampling (FFTW is there; no new dependency)
E5Stage B: hard curves or a penalty(a) hard: the laws are the curves; (b) soft: per-note laws with a penalty pulling them to the curves, fitted jointly(a): what Piano 12 asks for and what the Rust engine will read; stage A already gives the unconstrained per-note answer to compare with. (b) only if (a) fails badly on some keys, to see whether those keys are real outliers

Done when

Tests: the two-mic model recovers a synthetic two-channel note with shared rates and different per-mic amplitudes; gradients of both renderers agree; the decimation is tested (a partial below the new Nyquist survives at its level and frequency). A results section here: stage A and B for 30 keys × the chosen layers × 2 mics, the loss and envelope error per key for both stages, the curves at 88 keys against T-006’s, the two-mic answer, timings. The tables in training/results/salamander/ with README; Piano 12 step 3 updated; follow-ups in the backlog.

Results

Code: training/src/KeyboardFit.jl (submodule InstrumentsTraining.KeyboardFit, tested in training/test/keyboardfit.jl): the two-mic model, a multi-channel NoteProblem, the CurveProblem and the decimator; NoteFit.STFTLoss gained max_hz. Driver training/scripts/t037_fit_keyboard.jl (checkpoints in data/t037/, results in training/results/salamander/t037_*.{csv,toml}, CC BY 4.0, columns in its README):

julia --project=training -t 4 training/scripts/t037_fit_keyboard.jl        # stages A and B (~7 h on the VM)
julia --project=training -t 4 training/scripts/t037_fit_keyboard.jl B      # B from stage A's checkpoint
julia --project=training -t 4 training/scripts/t037_fit_keyboard.jl BR     # resume B (it saves every 25 steps)
julia --project=training -t 4 training/scripts/t037_fit_keyboard.jl time   # cost per register

Method, as it ended up

  • Samples: 30 keys × layers 4, 8, 12, 16 × both mics; A0 v4 left out (T-006 mis-tracked it on both mics, see below): 119 samples.
  • Model per sample: the two-mic model (E2) with every partial up to the highest T-006 fitted at either mic: T-006’s gaps (A0: 42–49, C4: 16–19, a mic’s missing fundamental) start 6 dB above the floor, so that the loss does not charge the model for partials it does not have. , per note (E1); in the top two octaves (key > 84) is held at T-006’s curve and the frequency term is off.
  • Rate and window (E4): each sample decimated (windowed sinc, decimate) to the lowest of 8, 12, 16, 24 kHz that puts its highest partial below 0.35 of the rate (48 kHz without decimation where nothing lower fits: F#7, C8); window up to 6 s or what the sample has before its fade (C6 4.4 s, A7 2.8 s). The loss only judges the band up to half a partial spacing above the highest partial (max_hz).
  • STFT sizes: T-036’s durations (512 … 4096 samples at 48 kHz), but the largest frame must give 8 bins per partial spacing: scaled down with the rate, the bass partials (A0: 27 Hz apart) were not resolved, and the fit then traded individual decays for band energy (slow components to −400 dB, envelope errors of 15–25 dB) while the loss went down. A0 now uses 512 … 4096 at 8 kHz (up to 0.5 s).
  • Starts from T-006, guarded: (1) a mic whose T-006 is > 20 cent off its key’s median was mis-tracked (partial numbers shifted; 15 of T-006’s 960 channels, all in the bass): its rows are not used (C1 v8, C1 v12 mic 1; both mics of A0 v4: left out). Averaging such a mic’s peaks with the other’s had pulled C1’s by 48 cent and its loss to 490. (2) T-006’s envelope fits have degenerate solutions that are harmless as envelopes and poison as starts: two components above full scale of one rate in antiphase whose small difference is the partial (they stop cancelling once the rates come from a law: D#2 v16 started at an STFT loss of 50 on one mic), and prompt components of +500 dB decaying in a nanosecond. A row with a component above full scale, a prompt rate above 100/s, or a loudest component 20 dB above the row’s level at the onset starts from that level instead; 71 samples have at least one such row (mostly the bass). With both guards every start is within 1–2× the loss of T-006’s own fits rendered as they are.
  • Stage A: each sample alone, decay laws per sample started from a log-domain fit to its own T-006 rates (T-036 lesson), δ held at 0, 400 Adam steps (lr 0.03).
  • Stage B: initial curves = robust piecewise quadratics (break at A2) through stage A’s laws (mean over layers per key). B0: every sample re-fitted for 150 steps with its laws held at those curves, from the better of its stage A values and its T-006 start (117 of 119 from stage A), so that notes and curves agree before the joint fit, which then starts at a mean loss of 0.299. (A first attempt built on stage A fits from the unguarded starts began at 6.3: amplitudes fitted to collapsed laws.) Then the joint fit of the 24 curve coefficients and all samples’ own parameters, 250 steps (lr 0.02).

Cost on the 4-vCPU VM: stage A 3.4 h for 120 samples (about 1000 s per heavy bass sample with four fits sharing the machine, twice the single-thread estimate), B0 1.3 h, stage B 2.2 h (250 steps, ~30 s per step); the whole chain about 7 h. A container restart killed one stage B run near its end, so stage B now saves its state every 25 steps and resumes with BR.

Per key

Medians over the layers; loss = STFT loss, mean of the two mics; T-006 = T-006’s per-partial fits of each mic rendered as they are; envelope error over all partials from 0.3 s on; T60 at each key’s (A: the median of the samples’ own laws, B: the curves).

keyfs / kHzwindow / spartialsloss T-006loss Aloss BB/Aenv. error A / B (dB, 0.3 s on)T60 slow @ f0 A / B (s)T60 prompt @ f0 A / B (s)
A086.0580.68840.57360.57040.994.4 / 4.418.0 / 20.611.45 / 13.13
C186.0600.63980.45170.44290.984.0 / 3.825.3 / 22.518.34 / 14.40
D#186.0420.65190.49190.48920.993.0 / 2.923.1 / 24.114.73 / 15.30
F#186.0460.37490.32450.31910.982.2 / 2.430.4 / 25.420.87 / 15.73
A186.0390.43360.34560.34360.993.0 / 3.021.7 / 26.212.13 / 15.65
C2126.0470.35850.29420.28960.983.1 / 3.027.8 / 26.515.81 / 15.02
D#2126.0400.32760.25720.25230.982.9 / 2.922.9 / 26.312.26 / 13.87
F#2126.0360.30500.22880.22630.993.2 / 2.928.9 / 25.615.03 / 12.27
A286.0240.38010.27880.27490.992.9 / 3.021.9 / 18.910.39 / 8.50
C3166.0310.24020.19090.18540.974.7 / 4.120.1 / 18.911.14 / 7.54
D#3166.0270.19890.17010.16961.003.1 / 2.816.9 / 18.75.46 / 6.61
F#3126.0200.20940.18230.18341.013.7 / 3.020.2 / 18.45.48 / 5.74
A3126.0140.20660.19520.19240.993.5 / 3.320.1 / 18.06.85 / 4.94
C4126.0150.15240.13260.13351.013.3 / 4.119.2 / 17.43.32 / 4.23
D#4126.0120.15910.14810.14440.983.3 / 3.818.6 / 16.75.74 / 3.60
F#4126.080.18020.15240.15751.034.1 / 4.212.8 / 15.94.22 / 3.06
A4126.080.15230.12540.12611.014.2 / 4.812.3 / 15.02.71 / 2.59
C5166.090.14500.13070.13071.003.8 / 4.013.7 / 14.02.23 / 2.18
D#5166.080.15610.14780.15061.025.3 / 4.415.8 / 12.93.20 / 1.84
F#5166.060.10730.10220.10591.044.3 / 6.616.4 / 11.81.78 / 1.55
A5164.960.18430.17820.17931.014.1 / 4.613.6 / 10.61.44 / 1.31
C6163.840.29530.28610.28621.004.9 / 5.210.8 / 9.31.37 / 1.10
D#6164.640.28040.27400.27301.006.0 / 5.27.1 / 8.11.05 / 0.93
F#6243.940.28730.27270.26740.9810.5 / 9.85.9 / 6.80.84 / 0.79
A6164.430.25180.23910.23991.005.9 / 4.66.4 / 5.70.93 / 0.66
C7242.830.30780.28920.28901.006.7 / 6.73.7 / 4.60.63 / 0.56
D#7243.430.32260.30580.30390.9911.2 / 25.73.6 / 3.70.50 / 0.47
F#7483.330.39200.36350.36100.9910.1 / 9.44.1 / 2.80.62 / 0.39
A7242.720.39980.38040.37510.997.5 / 16.32.2 / 2.10.39 / 0.33
C8482.020.52070.48180.47270.9821.2 / 28.01.3 / 1.50.40 / 0.27

The curves against T-006’s laws (T60 at )

keyT-006 slow / prompt (s)T-037 slow / prompt (s)
A040.7 / 19.9020.6 / 13.13
C143.0 / 16.8522.5 / 14.40
F#144.8 / 12.6525.4 / 15.73
C242.2 / 10.0826.5 / 15.02
A231.7 / 7.0918.9 / 8.50
C332.0 / 6.9718.9 / 7.54
F#331.6 / 6.5018.4 / 5.74
C429.8 / 5.7817.4 / 4.23
A424.9 / 4.4015.0 / 2.59
C522.8 / 3.9214.0 / 2.18
F#518.2 / 2.9811.8 / 1.55
C613.6 / 2.159.3 / 1.10
F#69.3 / 1.486.8 / 0.79
C75.8 / 0.984.6 / 0.56
F#73.4 / 0.622.8 / 0.39
C81.8 / 0.391.6 / 0.27

What the keyboard fit showed

  • The curves cost nothing measurable. Stage B (laws as two piecewise quadratics over the key, 24 coefficients for the whole keyboard) ends within −3 … +4 % of stage A’s loss at every key (B/A 0.97–1.04, median 0.99); the joint loss went from 0.2988 to 0.2954 over 250 steps. Per-sample decay laws buy nothing that smooth curves do not, so the model file can carry curves (Piano 12 step 3 confirmed).
  • Two mics with shared rates work. Stage A fits both mics with one set of frequencies, rates and beats and ends below T-006’s per-mic fits (each mic with its own per-partial rates) at every key and on both mics: A/T-006 0.88 on average, at most 0.98, 0.6–0.85 in the bass. T-036’s mic-dependent decays were the non-uniqueness of per-channel three-component fits, not physics: one string seen by two mics is enough.
  • The slow decay comes out shorter than T-006’s. Slow T60 at is 18–26 s from A0 to C3 and 15–17 s around C4–A4, against T-006’s 30–45 s; T-036 saw the same at C4 (22–25 s vs 27 s). The fit sees 6 s, T-006 up to 14 s of envelope, so this is either the fit missing the late tail or T-006’s long-window rates carrying the floor and sympathetic ring (430 non-decaying components). It matters for the engine (the tail is what the player hears longest); T-038 fits longer windows to decide.
  • The two-stage decay is weak in the bass, strong above. Prompt T60 / slow T60 is about 0.6 from A0 to F#2 (13–16 s vs 19–26 s) and falls to 1/4 at C4, 1/6 at C5 and above: the bass’s unisons (two wound strings, few modes coupled to a stiff bridge) barely show a prompt sound.
  • Where it fails: the top two octaves. Envelope errors grow from 2–4 dB (bass, middle) to 5–11 dB at C6–F#7 and 16–28 dB at D#7, A7 and C8 with the curves: 2–3 partials per note, windows of 2–3 s, held at T-006’s curve, and C8’s semitone-sharp sample. Their losses still beat T-006’s; the dB errors come from the few partials near the floor.
  • T-006’s tables need care as starts: 15 of 960 channels mis-tracked (all bass; partial numbers shifted), and 376 partial fits in the chosen samples with degenerate decompositions (antiphase pairs above full scale, nanosecond spikes). Both are harmless in T-006’s own tables (robust medians, envelopes) and are now guarded in t037_fit_keyboard.jl; the T-006 page notes them.
  • Partial gaps matter: a model without T-006’s untracked partials (A0: 42–49) is charged for them by the loss; filling every partial up to the highest is what made the bass fits work, together with STFT frames long enough to resolve 27 Hz spacing.

Not done here

  • The slow tail beyond 6 s: T-038. The model file (curves + per-note , , amplitudes) is T-015; T-007 builds the reference piano from t037_curves.toml and t037_partials.csv.