// Plots of the instruments' frequency responses |H(f)| (instruments.js), one // small panel each, in dB against frequency on a log scale. import { color, prepare } from './canvas.js'; import { INSTRUMENTS, responseMagnitude } from './instruments.js'; const F_MIN = 50; const F_MAX = 4000; const DB_MIN = -40; const POINTS = 1500; // names: the instruments to plot; base: the tuning (Hz) for tuned ones. // Returns a sim for deck.js: { draw, pause }. export function createResponsePlot(canvas, { names, base = 220 }) { const fs = Array.from({ length: POINTS }, (_, i) => F_MIN * (F_MAX / F_MIN) ** (i / (POINTS - 1))); const curves = names.map((name) => responseMagnitude(name, base, fs).map((m) => Math.max(DB_MIN, 20 * Math.log10(m + 1e-12)))); function draw() { if (!canvas.clientWidth) return; const { ctx, w, h } = prepare(canvas); const fg = color('--fg'); const accent = color('--accent'); const left = 118; const right = 8; const bottom = 26; const gap = 8; const ph = (h - bottom - gap * (names.length - 1)) / names.length; const pw = w - left - right; const X = (f) => left + (Math.log(f / F_MIN) / Math.log(F_MAX / F_MIN)) * pw; ctx.font = '12px "Hanken Grotesk", system-ui, sans-serif'; names.forEach((name, k) => { const top = k * (ph + gap); const Y = (db) => top + (db / DB_MIN) * ph; ctx.strokeStyle = fg; ctx.lineWidth = 1; ctx.globalAlpha = 0.2; for (const f of [100, 200, 500, 1000, 2000]) { ctx.beginPath(); ctx.moveTo(X(f), top); ctx.lineTo(X(f), top + ph); ctx.stroke(); } ctx.globalAlpha = 1; ctx.strokeRect(left, top, pw, ph); ctx.fillStyle = fg; ctx.textAlign = 'right'; ctx.textBaseline = 'middle'; ctx.fillText(INSTRUMENTS[name].label.replace(/ \(.*\)$/, ''), left - 8, top + ph / 2); ctx.strokeStyle = accent; ctx.lineWidth = 1.2; ctx.beginPath(); curves[k].forEach((db, i) => (i ? ctx.lineTo(X(fs[i]), Y(db)) : ctx.moveTo(X(fs[i]), Y(db)))); ctx.stroke(); }); // frequency axis ctx.fillStyle = fg; ctx.textAlign = 'center'; ctx.textBaseline = 'top'; const yAxis = h - bottom + 4; for (const f of [50, 100, 200, 500, 1000, 2000, 4000]) ctx.fillText(f >= 1000 ? `${f / 1000} kHz` : `${f} Hz`, X(f), yAxis); } return { draw, pause: () => {} }; }