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)
19export function drawSpectrum(canvas, series, marks = []) {
20 const { g, w, h, col } = setup(canvas);
21 const L = 34, R = 8, T = 8, B = 20;
22 const f0 = 100, 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 [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 for (const s of series) {
42 g.strokeStyle = col(SERIES[s.name]); g.lineWidth = 1.4;
43 g.beginPath();
44 let first = true;
45 for (let k = 1; k < s.db.length; k++) {
46 const f = k * s.df;
47 if (f < f0 || f > f1) continue;
48 const x = X(f), y = Math.min(h - B, Math.max(T, Y(s.db[k])));
49 if (first) { g.moveTo(x, y); first = false; } else g.lineTo(x, y);
50 }
51 g.stroke();
52 }
53 g.fillStyle = col('--muted');
54 g.fillText('dB / Hz', w - 60, T + 10);
55}
57// series: [{name, y, sr}] shown over first `dur` seconds
58export function drawWave(canvas, series, dur = 0.25) {
59 const { g, w, h, col } = setup(canvas);
60 const L = 8, R = 8, T = 6, B = 18;
61 let amax = 1e-12;
62 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]));
63 g.strokeStyle = col('--grid');
64 g.beginPath(); g.moveTo(L, (T + h - B) / 2); g.lineTo(w - R, (T + h - B) / 2); g.stroke();
65 g.fillStyle = col('--muted');
66 for (let t = 0; t <= dur + 1e-9; t += 0.05) g.fillText((t * 1000).toFixed(0), L + (t / dur) * (w - L - R) - 6, h - 5);
67 g.fillText('ms', w - 22, h - 5);
68 for (const s of series) {
69 g.strokeStyle = col(SERIES[s.name]); g.lineWidth = 1;
70 g.beginPath();
71 const n = Math.min(s.y.length, Math.floor(dur * s.sr));
72 for (let i = 0; i < n; i++) {
73 const x = L + (i / (dur * s.sr)) * (w - L - R);
74 const y = (T + h - B) / 2 - (s.y[i] / amax) * ((h - T - B) / 2);
75 i ? g.lineTo(x, y) : g.moveTo(x, y);
76 }
77 g.stroke();
78 }
79}