46256e7Marimba lab: per-key tuned bars, coupled resonator tubes, SoundFont exportJeremy Magland 1// Small canvas plots: spectrum (log f, dB) and waveform overlay.
3function setup(canvas) {
4 const dpr = window.devicePixelRatio || 1;
5 const w = canvas.clientWidth, h = canvas.clientHeight;
6 canvas.width = w * dpr; canvas.height = h * dpr;
7 const g = canvas.getContext('2d');
8 g.setTransform(dpr, 0, 0, dpr, 0, 0);
9 const cs = getComputedStyle(document.documentElement);
10 const col = (n) => cs.getPropertyValue(n).trim();
11 g.clearRect(0, 0, w, h);
12 g.font = '11px system-ui, sans-serif';
13 return { g, w, h, col };
14}
16const SERIES = { modal: '--c-modal' };
18// series: [{name:'modal', df, db}], marks: frequencies (Hz), tri: frequencies drawn as ▲
19export function drawSpectrum(canvas, series, marks = [], tri = []) {
20 const { g, w, h, col } = setup(canvas);
21 const L = 34, R = 8, T = 8, B = 20;
22 const f0 = 40, f1 = 16000;
23 const X = (f) => L + ((Math.log(f) - Math.log(f0)) / (Math.log(f1) - Math.log(f0))) * (w - L - R);
24 let top = -Infinity;
25 for (const s of series) for (let k = 1; k < s.db.length; k++) if (k * s.df > f0 && k * s.df < f1) top = Math.max(top, s.db[k]);
26 if (!Number.isFinite(top)) top = 0;
27 top = Math.ceil(top / 10) * 10;
28 const range = 70;
29 const Y = (d) => T + ((top - d) / range) * (h - T - B);
30 g.strokeStyle = col('--grid'); g.fillStyle = col('--muted'); g.lineWidth = 1;
31 for (const f of [50, 100, 200, 500, 1000, 2000, 5000, 10000]) {
32 g.beginPath(); g.moveTo(X(f), T); g.lineTo(X(f), h - B); g.stroke();
33 g.fillText(f >= 1000 ? f / 1000 + 'k' : String(f), X(f) - 8, h - 6);
34 }
35 for (let d = 0; d <= range; d += 20) {
36 g.beginPath(); g.moveTo(L, Y(top - d)); g.lineTo(w - R, Y(top - d)); g.stroke();
37 g.fillText(String(-d), 4, Y(top - d) + 4);
38 }
39 g.strokeStyle = col('--mark');
40 for (const f of marks) { if (f > f0 && f < f1) { g.beginPath(); g.moveTo(X(f), h - B); g.lineTo(X(f), h - B - 6); g.stroke(); } }
41 g.fillStyle = col('--tube');
42 for (const f of tri) {
43 if (f > f0 && f < f1) { g.beginPath(); g.moveTo(X(f), h - B - 9); g.lineTo(X(f) - 4, h - B); g.lineTo(X(f) + 4, h - B); g.fill(); }
44 }
45 for (const s of series) {
46 g.strokeStyle = col(SERIES[s.name]); g.lineWidth = 1.4;
47 g.beginPath();
48 let first = true;
49 for (let k = 1; k < s.db.length; k++) {
50 const f = k * s.df;
51 if (f < f0 || f > f1) continue;
52 const x = X(f), y = Math.min(h - B, Math.max(T, Y(s.db[k])));
53 if (first) { g.moveTo(x, y); first = false; } else g.lineTo(x, y);
54 }
55 g.stroke();
56 }
57 g.fillStyle = col('--muted');
58 g.fillText('dB / Hz', w - 60, T + 10);
59}
61// series: [{name, y, sr}] shown over the first `dur` seconds (null = the whole signal)
62export function drawWave(canvas, series, dur = 0.25) {
63 const { g, w, h, col } = setup(canvas);
64 const L = 8, R = 8, T = 6, B = 18;
65 if (dur == null) dur = Math.max(0.05, ...series.map((s) => s.y.length / s.sr));
66 let amax = 1e-12;
67 for (const s of series) for (let i = 0; i < Math.min(s.y.length, dur * s.sr); i++) amax = Math.max(amax, Math.abs(s.y[i]));
68 g.strokeStyle = col('--grid');
69 g.beginPath(); g.moveTo(L, (T + h - B) / 2); g.lineTo(w - R, (T + h - B) / 2); g.stroke();
70 // time ticks: a 1-2-5 step giving about 4-8 labels; ms below half a second, s above
71 const step = [0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2].find((v) => dur / v <= 8) ?? 5;
72 const ms = dur <= 0.5;
73 g.fillStyle = col('--muted');
74 for (let t = 0; t <= dur + 1e-9; t += step) {
75 const lab = ms ? (t * 1000).toFixed(0) : t.toFixed(step < 1 ? 1 : 0);
76 const x = L + (t / dur) * (w - L - R) - 6;
77 if (x < w - R - 26) g.fillText(lab, x, h - 5);
78 }
79 g.fillText(ms ? 'ms' : 's', w - 14, h - 5);
80 const mid = (T + h - B) / 2, half = (h - T - B) / 2;
81 for (const s of series) {
82 g.strokeStyle = col(SERIES[s.name]); g.lineWidth = 1;
83 g.beginPath();
84 const n = Math.min(s.y.length, Math.floor(dur * s.sr));
85 const px = w - L - R, perPx = (dur * s.sr) / px;
86 if (perPx > 2) {
87 // many samples per pixel: draw the min..max range of each column
88 for (let c = 0; c < px; c++) {
89 const i0 = Math.floor(c * perPx), i1 = Math.min(n, Math.floor((c + 1) * perPx));
90 if (i0 >= n) break;
91 let lo = Infinity, hi = -Infinity;
92 for (let i = i0; i < Math.max(i1, i0 + 1); i++) { lo = Math.min(lo, s.y[i]); hi = Math.max(hi, s.y[i]); }
93 g.moveTo(L + c + 0.5, mid - (hi / amax) * half);
94 g.lineTo(L + c + 0.5, mid - (lo / amax) * half + 0.5);
95 }
96 } else {
97 for (let i = 0; i < n; i++) {
98 const x = L + (i / (dur * s.sr)) * px;
99 const y = mid - (s.y[i] / amax) * half;
100 i ? g.lineTo(x, y) : g.moveTo(x, y);
101 }
102 }
103 g.stroke();
104 }
105}