// Small canvas plots: spectrum (log f, dB) and waveform overlay. function setup(canvas) { const dpr = window.devicePixelRatio || 1; const w = canvas.clientWidth, h = canvas.clientHeight; canvas.width = w * dpr; canvas.height = h * dpr; const g = canvas.getContext('2d'); g.setTransform(dpr, 0, 0, dpr, 0, 0); const cs = getComputedStyle(document.documentElement); const col = (n) => cs.getPropertyValue(n).trim(); g.clearRect(0, 0, w, h); g.font = '11px system-ui, sans-serif'; return { g, w, h, col }; } const SERIES = { modal: '--c-modal' }; // series: [{name:'modal', df, db}], marks: frequencies (Hz) export function drawSpectrum(canvas, series, marks = []) { const { g, w, h, col } = setup(canvas); const L = 34, R = 8, T = 8, B = 20; const f0 = 100, f1 = 16000; const X = (f) => L + ((Math.log(f) - Math.log(f0)) / (Math.log(f1) - Math.log(f0))) * (w - L - R); let top = -Infinity; 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]); if (!Number.isFinite(top)) top = 0; top = Math.ceil(top / 10) * 10; const range = 70; const Y = (d) => T + ((top - d) / range) * (h - T - B); g.strokeStyle = col('--grid'); g.fillStyle = col('--muted'); g.lineWidth = 1; for (const f of [100, 200, 500, 1000, 2000, 5000, 10000]) { g.beginPath(); g.moveTo(X(f), T); g.lineTo(X(f), h - B); g.stroke(); g.fillText(f >= 1000 ? f / 1000 + 'k' : String(f), X(f) - 8, h - 6); } for (let d = 0; d <= range; d += 20) { g.beginPath(); g.moveTo(L, Y(top - d)); g.lineTo(w - R, Y(top - d)); g.stroke(); g.fillText(String(-d), 4, Y(top - d) + 4); } g.strokeStyle = col('--mark'); 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(); } } for (const s of series) { g.strokeStyle = col(SERIES[s.name]); g.lineWidth = 1.4; g.beginPath(); let first = true; for (let k = 1; k < s.db.length; k++) { const f = k * s.df; if (f < f0 || f > f1) continue; const x = X(f), y = Math.min(h - B, Math.max(T, Y(s.db[k]))); if (first) { g.moveTo(x, y); first = false; } else g.lineTo(x, y); } g.stroke(); } g.fillStyle = col('--muted'); g.fillText('dB / Hz', w - 60, T + 10); } // series: [{name, y, sr}] shown over first `dur` seconds export function drawWave(canvas, series, dur = 0.25) { const { g, w, h, col } = setup(canvas); const L = 8, R = 8, T = 6, B = 18; let amax = 1e-12; 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])); g.strokeStyle = col('--grid'); g.beginPath(); g.moveTo(L, (T + h - B) / 2); g.lineTo(w - R, (T + h - B) / 2); g.stroke(); g.fillStyle = col('--muted'); 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); g.fillText('ms', w - 22, h - 5); for (const s of series) { g.strokeStyle = col(SERIES[s.name]); g.lineWidth = 1; g.beginPath(); const n = Math.min(s.y.length, Math.floor(dur * s.sr)); for (let i = 0; i < n; i++) { const x = L + (i / (dur * s.sr)) * (w - L - R); const y = (T + h - B) / 2 - (s.y[i] / amax) * ((h - T - B) / 2); i ? g.lineTo(x, y) : g.moveTo(x, y); } g.stroke(); } }