46256e7Marimba lab: per-key tuned bars, coupled resonator tubes, SoundFont exportJeremy Magland 1// Undercut tuner: adjusts bar length L, undercut depth d and undercut length a so the first
2// three bending partials land on f1 = note, f2 = r2·f1, f3 = r3·f1. Partials whose target
3// lies above the cutoff are left free (as makers do on the top bars); a tiny pull toward
4// the nominal arch then keeps the problem well posed. Levenberg–Marquardt on log errors.
6import { bendFreqs } from './fe.js';
7import { maxDepth, noteFreq, nominalDesign } from './bar.js';
9export const DEFAULT_TARGETS = { r2: 4, r3: 10, fCut: 9000 };
11const A_LO = 0.2, A_HI = 0.95;
12const logit = (u) => Math.log(u / (1 - u));
13const sigm = (t) => 1 / (1 + Math.exp(-t));
14const clampU = (u) => Math.min(1 - 1e-6, Math.max(1e-6, u));
16function toParams(b) {
17 return [Math.log(b.L), logit(clampU(b.d / maxDepth(b))), logit(clampU((b.a - A_LO) / (A_HI - A_LO)))];
18}
19function fromParams(base, t) {
20 const b = { ...base, L: Math.exp(t[0]) };
21 b.d = sigm(t[1]) * maxDepth(b);
22 b.a = A_LO + (A_HI - A_LO) * sigm(t[2]);
23 return b;
24}
26export function partialTargets(note, targets) {
27 const f1 = noteFreq(note);
28 return { f: [f1, f1 * targets.r2, f1 * targets.r3], on: [true, f1 * targets.r2 <= targets.fCut, f1 * targets.r3 <= targets.fCut] };
29}
31// cents error of each partial against its target
32export function partialErrors(f, note, targets) {
33 const t = partialTargets(note, targets);
34 return f.map((fi, i) => ({ f: fi, target: t.f[i], on: t.on[i], cents: 1200 * Math.log2(fi / t.f[i]) }));
35}
37function residuals(t, base, mat, tg, nominal, ne) {
38 const b = fromParams(base, t);
39 const f = bendFreqs(b, mat, 3, ne);
40 const r = [Math.log(f[0] / tg.f[0])];
41 r.push(tg.on[1] ? Math.log(f[1] / tg.f[1]) : 0);
42 r.push(tg.on[2] ? Math.log(f[2] / tg.f[2]) : 0);
43 const w = 3e-3;
44 r.push(w * (t[1] - nominal[1]), w * (t[2] - nominal[2]));
45 return r;
46}
48function solve3(A, g) {
49 const M = A.map((row, i) => [...row, g[i]]);
50 for (let c = 0; c < 3; c++) {
51 let p = c;
52 for (let r = c + 1; r < 3; r++) if (Math.abs(M[r][c]) > Math.abs(M[p][c])) p = r;
53 [M[c], M[p]] = [M[p], M[c]];
54 for (let r = c + 1; r < 3; r++) {
55 const f = M[r][c] / M[c][c];
56 for (let k = c; k < 4; k++) M[r][k] -= f * M[c][k];
57 }
58 }
59 const x = [0, 0, 0];
60 for (let r = 2; r >= 0; r--) {
61 let s = M[r][3];
62 for (let k = r + 1; k < 3; k++) s -= M[r][k] * x[k];
63 x[r] = s / M[r][r];
64 }
65 return x;
66}
68const cost = (r) => r.reduce((a, v) => a + v * v, 0);
70function lm(start, mat, note, targets, ne) {
71 const tg = partialTargets(note, targets);
72 const nominal = toParams(nominalDesign(note, mat));
73 let t = toParams(start);
74 let r = residuals(t, start, mat, tg, nominal, ne), c = cost(r);
75 let lam = 1e-3;
76 for (let it = 0; it < 60 && c > 1e-14; it++) {
77 const J = [];
78 for (let k = 0; k < 3; k++) {
79 const tt = [...t];
80 tt[k] += 1e-6;
81 const rk = residuals(tt, start, mat, tg, nominal, ne);
82 J.push(rk.map((v, i) => (v - r[i]) / 1e-6));
83 }
84 const A = [0, 1, 2].map((i) => [0, 1, 2].map((j) => J[i].reduce((a, v, q) => a + v * J[j][q], 0)));
85 const g = [0, 1, 2].map((i) => -J[i].reduce((a, v, q) => a + v * r[q], 0));
86 let improved = false;
87 for (let tries = 0; tries < 12; tries++) {
88 const Ad = A.map((row, i) => row.map((v, j) => v + (i === j ? lam * (A[i][i] + 1e-9) : 0)));
89 const dt = solve3(Ad, g);
90 const step = Math.max(...dt.map(Math.abs));
91 const s = step > 1.5 ? 1.5 / step : 1; // limit wild steps
92 const tn = t.map((v, i) => v + s * dt[i]);
93 const rn = residuals(tn, start, mat, tg, nominal, ne), cn = cost(rn);
94 if (cn < c) { t = tn; r = rn; c = cn; lam = Math.max(1e-9, lam / 4); improved = true; break; }
95 lam *= 5;
96 }
97 if (!improved) break;
98 }
99 return { design: fromParams(start, t), cost: c };
100}
102/**
103 * Tune a design for a note. Starts from `start` (or the nominal bar) and also from the
104 * nominal bar, keeping the better result. Width, thickness and arch shape are kept.
105 */
106export function tuneDesign(note, mat, targets = DEFAULT_TARGETS, start = null, ne = 120) {
107 const nominal = nominalDesign(note, mat);
108 const base = start ? { ...start } : nominal;
109 let best = lm(base, mat, note, targets, ne);
110 if (start && best.cost > 1e-8) {
111 const alt = lm({ ...nominal, W: base.W, h0: base.h0, p: base.p }, mat, note, targets, ne);
112 if (alt.cost < best.cost) best = alt;
113 }
114 // final polish of f1 at full resolution (length only)
115 let b = best.design;
116 for (let k = 0; k < 3; k++) {
117 const f1 = bendFreqs(b, mat, 1)[0];
118 b = { ...b, L: b.L * Math.sqrt(f1 / noteFreq(note)) };
119 }
120 return b;
121}