// 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), tri: frequencies drawn as ▲ export function drawSpectrum(canvas, series, marks = [], tri = []) { const { g, w, h, col } = setup(canvas); const L = 34, R = 8, T = 8, B = 20; const f0 = 40, 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 [50, 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(); } } g.fillStyle = col('--tube'); for (const f of tri) { 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(); } } 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 the first `dur` seconds (null = the whole signal) export function drawWave(canvas, series, dur = 0.25) { const { g, w, h, col } = setup(canvas); const L = 8, R = 8, T = 6, B = 18; if (dur == null) dur = Math.max(0.05, ...series.map((s) => s.y.length / s.sr)); 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(); // time ticks: a 1-2-5 step giving about 4-8 labels; ms below half a second, s above const step = [0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2].find((v) => dur / v <= 8) ?? 5; const ms = dur <= 0.5; g.fillStyle = col('--muted'); for (let t = 0; t <= dur + 1e-9; t += step) { const lab = ms ? (t * 1000).toFixed(0) : t.toFixed(step < 1 ? 1 : 0); const x = L + (t / dur) * (w - L - R) - 6; if (x < w - R - 26) g.fillText(lab, x, h - 5); } g.fillText(ms ? 'ms' : 's', w - 14, h - 5); const mid = (T + h - B) / 2, half = (h - T - B) / 2; 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)); const px = w - L - R, perPx = (dur * s.sr) / px; if (perPx > 2) { // many samples per pixel: draw the min..max range of each column for (let c = 0; c < px; c++) { const i0 = Math.floor(c * perPx), i1 = Math.min(n, Math.floor((c + 1) * perPx)); if (i0 >= n) break; let lo = Infinity, hi = -Infinity; 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]); } g.moveTo(L + c + 0.5, mid - (hi / amax) * half); g.lineTo(L + c + 0.5, mid - (lo / amax) * half + 0.5); } } else { for (let i = 0; i < n; i++) { const x = L + (i / (dur * s.sr)) * px; const y = mid - (s.y[i] / amax) * half; i ? g.lineTo(x, y) : g.moveTo(x, y); } } g.stroke(); } }