// Magnitude spectrum of the render, log-frequency axis, with the coupled // modes of the first note overlaid, colored by how much of each mode's // energy lives in the body. A pizzicato bass spectrum is a comb of slightly // stretched partials standing on the body resonances; where a string // partial meets a body mode the overlay shows the pair trading color. import { magnitudeSpectrum } from '../../lib/fft'; import { useCanvas, drawFrame, PLOT_COLORS } from './canvasUtils'; interface Props { y: Float32Array; sampleRate: number; modeF: Float32Array; modeBodyFrac: Float32Array; } const F_LO = 30; const F_HI = 8000; const DB_RANGE = 80; function mixColor(t: number, alpha: number): string { // cyan (string) -> amber (body) const r = Math.round(77 + t * (232 - 77)); const g = Math.round(208 + t * (163 - 208)); const b = Math.round(225 + t * (61 - 225)); return `rgba(${r}, ${g}, ${b}, ${alpha})`; } export default function SpectrumPlot({ y, sampleRate, modeF, modeBodyFrac }: Props) { const ref = useCanvas( (ctx, w, h) => { drawFrame(ctx, w, h); const padL = 34; const padB = 18; const pw = w - padL - 6; const ph = h - padB - 8; const fx = (f: number) => padL + ((Math.log10(f) - Math.log10(F_LO)) / (Math.log10(F_HI) - Math.log10(F_LO))) * pw; ctx.font = '10px system-ui'; ctx.textAlign = 'center'; for (const f of [30, 50, 100, 200, 500, 1000, 2000, 5000]) { const x = fx(f); ctx.strokeStyle = PLOT_COLORS.grid; ctx.beginPath(); ctx.moveTo(x, 8); ctx.lineTo(x, 8 + ph); ctx.stroke(); ctx.fillStyle = PLOT_COLORS.axis; ctx.fillText(f >= 1000 ? `${f / 1000}k` : `${f}`, x, h - 5); } ctx.textAlign = 'right'; for (let db = 0; db >= -DB_RANGE; db -= 20) { const yy = 8 + (-db / DB_RANGE) * ph; ctx.strokeStyle = PLOT_COLORS.grid; ctx.beginPath(); ctx.moveTo(padL, yy); ctx.lineTo(padL + pw, yy); ctx.stroke(); ctx.fillStyle = PLOT_COLORS.axis; ctx.fillText(`${db}`, padL - 4, yy + 3); } const { mag, size } = magnitudeSpectrum(y, 65536); let peak = 0; for (let i = 1; i < mag.length; i++) peak = Math.max(peak, mag[i]); if (peak <= 0) return; // overlays first so the trace draws on top ctx.setLineDash([4, 3]); for (let i = 0; i < modeF.length; i++) { const f = modeF[i]; if (f < F_LO || f > F_HI) continue; ctx.strokeStyle = mixColor(Math.min(1, modeBodyFrac[i]), 0.45); ctx.beginPath(); ctx.moveTo(fx(f), 8); ctx.lineTo(fx(f), 8 + ph); ctx.stroke(); } ctx.setLineDash([]); ctx.strokeStyle = PLOT_COLORS.trace; ctx.beginPath(); let started = false; for (let px = 0; px <= pw; px++) { const f = 10 ** (Math.log10(F_LO) + (px / pw) * (Math.log10(F_HI) - Math.log10(F_LO))); const bin = Math.min(mag.length - 1, Math.round((f / sampleRate) * size)); const fNext = 10 ** (Math.log10(F_LO) + ((px + 1) / pw) * (Math.log10(F_HI) - Math.log10(F_LO))); const binNext = Math.min( mag.length - 1, Math.max(bin + 1, Math.round((fNext / sampleRate) * size)), ); let m = 0; for (let b = bin; b < binNext; b++) m = Math.max(m, mag[b]); const db = 20 * Math.log10(m / peak + 1e-12); const yy = 8 + Math.min(ph, (-db / DB_RANGE) * ph); if (!started) { ctx.moveTo(padL + px, yy); started = true; } else { ctx.lineTo(padL + px, yy); } } ctx.stroke(); ctx.textAlign = 'left'; ctx.fillStyle = mixColor(0, 0.9); ctx.fillText('string-dominated modes', padL + 4, 18); ctx.fillStyle = mixColor(1, 0.9); ctx.fillText('body-dominated modes', padL + 134, 18); ctx.fillStyle = PLOT_COLORS.axis; ctx.fillText('(first note)', padL + 258, 18); }, [y, sampleRate, modeF, modeBodyFrac], 200, ); return ; }