// Instruments for the lab slides. Each is a fixed linear filter applied to the // oscillator's sound u(t), y = h * u, built as a sum of resonances. A // resonance { f, tau, gain } has impulse response (2 gain / tau) e^(-t/tau) // sin(2 pi f t): a peak of height gain at f, about 1 / (pi tau) wide. The tuned // instruments are built on a base frequency (the oscillator's natural // frequency |f0|), the marimba and harp on a scale (SCALES) from it. The // whole filter is scaled so that its response peaks at 1 (between MIN_F and // MAX_F). const MIN_F = 50; const MAX_F = 4000; // Scales for the tuned instruments, as semitones above the base within an // octave (equal temperament). export const SCALES = { 'major-pentatonic': { label: 'Major pentatonic', steps: [0, 2, 4, 7, 9] }, 'minor-pentatonic': { label: 'Minor pentatonic', steps: [0, 3, 5, 7, 10] }, major: { label: 'Major', steps: [0, 2, 4, 5, 7, 9, 11] }, minor: { label: 'Natural minor', steps: [0, 2, 3, 5, 7, 8, 10] }, 'harmonic-minor': { label: 'Harmonic minor', steps: [0, 2, 3, 5, 7, 8, 11] }, blues: { label: 'Blues', steps: [0, 3, 5, 6, 7, 10] }, 'whole-tone': { label: 'Whole tone', steps: [0, 2, 4, 6, 8, 10] }, chromatic: { label: 'Chromatic', steps: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] }, }; export const DEFAULT_SCALE = 'major-pentatonic'; // the notes of a scale from a frequency, over a number of octaves const notesOf = (scaleName) => (from, octaves) => { const { steps } = SCALES[scaleName] ?? SCALES[DEFAULT_SCALE]; const notes = []; for (let o = 0; o < octaves; o++) for (const s of steps) notes.push(from * 2 ** (o + s / 12)); return notes; }; const res = (f, tau, gain = 1) => ({ f, tau, gain }); // a fixed pseudo-random sequence, so the metal plate is the same every time function seeded(seed) { let s = seed; return () => { s = (s * 16807) % 2147483647; return (s - 1) / 2147483646; }; } export const INSTRUMENTS = { raw: { label: 'Raw (the oscillator alone)', parts: () => [] }, wood: { label: 'Wooden body', parts: () => [[100, 0.08, 1], [205, 0.06, 0.9], [275, 0.05, 0.8], [390, 0.04, 0.7], [460, 0.04, 0.7], [550, 0.035, 0.6], [700, 0.03, 0.5], [1100, 0.02, 0.4], [1500, 0.02, 0.3], [2500, 0.015, 0.2]].map(([f, t, g]) => res(f, t, g)), }, plate: { label: 'Metal plate', // 96 inharmonic resonances spread from 100 to 4000 Hz, not tuned parts: () => { const rand = seeded(7); return Array.from({ length: 96 }, (_, i) => { const f = 100 * (4000 / 100) ** ((i + rand()) / 96); return res(f, 0.3 + 0.9 * rand(), 1 / (1 + f / 800)); }); }, }, marimba: { label: 'Marimba (bars tuned to f₀)', tuned: true, usesScale: true, // bars on the scale from the base over two octaves, each with the // partials of a marimba bar at 1, 3.93 and 9.2 times its pitch parts: (b, scale) => scale(b, 2).flatMap((f) => [res(f, 0.6, 1), res(3.93 * f, 0.25, 0.3), res(9.2 * f, 0.1, 0.1)]), }, harp: { label: 'Harp (strings tuned to f₀)', tuned: true, // strings on the scale from an octave below the base, each with // its first six harmonics, higher ones fainter and shorter usesScale: true, parts: (b, scale) => scale(b / 2, 3).flatMap((f) => [1, 2, 3, 4, 5, 6].map((n) => res(n * f, 1.5 / Math.sqrt(n), 1 / n))), }, bell: { label: 'Bell (tuned to f₀)', tuned: true, // church bell partials: hum, prime, tierce (minor third), quint, nominal, ... parts: (b) => [[0.5, 2, 0.8], [1, 1.8, 1], [1.2, 1.5, 0.8], [1.5, 1.3, 0.6], [2, 1.1, 0.7], [2.5, 0.9, 0.4], [2.67, 0.8, 0.4], [3, 0.7, 0.3], [4, 0.5, 0.25], [5.33, 0.4, 0.2], [6.67, 0.3, 0.15]].map(([r, t, g]) => res(b * r, t, g)), }, }; const base = (b) => (b > 0 ? b : 220); const partsOf = (name, b, scaleName) => (INSTRUMENTS[name] ?? INSTRUMENTS.raw).parts(base(b), notesOf(scaleName)); // the unscaled frequency response at f (Hz), as [re, im] function rawResponse(parts, f) { const w = 2 * Math.PI * f; let re = 0; let im = 0; for (const p of parts) { // (2 gain / tau) wk / ((1/tau + i w)^2 + wk^2): of size gain at f = fk const wk = 2 * Math.PI * p.f; const a = 1 / p.tau; const dr = a * a - w * w + wk * wk; const di = 2 * a * w; const n = (2 * a * wk * p.gain) / (dr * dr + di * di); re += n * dr; im -= n * di; } return [re, im]; } const GRID = Array.from({ length: 800 }, (_, i) => MIN_F * (MAX_F / MIN_F) ** (i / 799)); // the factor that makes the response peak at 1: searched on a grid and at // every resonance (narrow peaks fall between grid points) function peakScale(parts) { let peak = 0; const fs = [...GRID, ...parts.map((p) => p.f)]; for (const f of fs) peak = Math.max(peak, Math.hypot(...rawResponse(parts, f))); return peak > 0 ? 1 / peak : 1; } // |H(f)| of an instrument tuned to base b, scaled to peak at 1, at the frequencies fs export function responseMagnitude(name, b, fs, scaleName = DEFAULT_SCALE) { const parts = partsOf(name, b, scaleName); const k = peakScale(parts); return fs.map((f) => k * Math.hypot(...rawResponse(parts, f))); } // The impulse response h, sampled at rate sr and scaled to peak at 1 (null for // raw), long enough for its slowest resonance to fall by e^5 (about 43 dB). export function impulseResponse(name, b, sr, scaleName = DEFAULT_SCALE) { const parts = partsOf(name, b, scaleName).filter((p) => p.f < 0.45 * sr); if (!parts.length) return null; const longest = Math.max(...parts.map((p) => p.tau)); const n = Math.max(1, Math.round(5 * longest * sr)); const h = new Float32Array(n); for (const p of parts) { // A r^i sin(w i) by its recurrence y[i] = 2 r cos(w) y[i-1] - r^2 y[i-2] const r = Math.exp(-1 / (p.tau * sr)); const w = (2 * Math.PI * p.f) / sr; const c1 = 2 * r * Math.cos(w); const c2 = -r * r; let y2 = 0; let y1 = ((2 * p.gain) / (p.tau * sr)) * r * Math.sin(w); for (let i = 1; i < n; i++) { h[i] += y1; const y = c1 * y1 + c2 * y2; y2 = y1; y1 = y; } } const k = peakScale(parts); for (let i = 0; i < n; i++) h[i] *= k; return h; }