// Offline audition of hum candidates (modal-synth engine in Node, no browser). // Each candidate holds one note while its timbre sweeps 0 → 1 (as the energy would), // with the same slow loudness normalization the app uses. Prints brightness/stability // per stage and, with --wav, writes renders/hum-.wav. // node scripts/render-hum.ts [--wav] [--pitch 38] [--only name] import fs from "node:fs"; import { Synth } from "../../modal-synth/src/engine/synth.ts"; import { DEFAULTS, PARAM_BY_ID, type Params } from "../../modal-synth/src/engine/params.ts"; import { PRESETS } from "../../modal-synth/src/presets.ts"; const args = process.argv.slice(2); const opt = (k: string, d: string) => (args.includes(`--${k}`) ? args[args.indexOf(`--${k}`) + 1] : d); const WAV = args.includes("--wav"); const PITCH = Number(opt("pitch", "38")); const ONLY = opt("only", ""); const SR = 48000, BLOCK = 128; const preset = (n: string) => PRESETS.find((p) => p.name === n)!.p; interface Map { target: string; from: number; to: number } // actual values, not fractions interface Cand { name: string; p: Params; map: Map[] } export const CANDIDATES: Cand[] = [ { name: "clarinet", p: preset("Clarinet-ish"), map: [{ target: "pickup", from: 0.25, to: 0.75 }, { target: "tilt", from: 0, to: 4 }] }, { name: "bass-bowed", p: { ...preset("Double bass"), vibDepth: 0 }, map: [{ target: "exPos", from: 0.2, to: 0.07 }, { target: "bowForce", from: 0.5, to: 1.4 }] }, { name: "cello-bowed", p: { ...preset("Cello"), vibDepth: 0 }, map: [{ target: "exPos", from: 0.2, to: 0.07 }, { target: "bowForce", from: 0.5, to: 1.4 }] }, { name: "bowl", p: preset("Singing bowl"), map: [{ target: "selfGain", from: 0.1, to: 0.6 }, { target: "pickup", from: 0.15, to: 0.4 }] }, // van der Pol on a string: weak self-excitation is nearly sinusoidal, strong becomes a rich relaxation oscillation { name: "string-vdp", p: { geometry: "string", modes: 32, t60: 6, hfDamp: 2, damper: 6, exciter: "selfosc", selfGain: 0.3, selfSat: 0.5, exPos: 0.12, exWidth: 0.05, attack: 400, release: 1500, pickup: 0.3, spread: 0.3, width: 0.4, tilt: -3, reverbMix: 0.3, reverbSize: 4 }, map: [{ target: "selfGain", from: 0.1, to: 1.2 }, { target: "tilt", from: -4, to: 0 }], }, { name: "tube-vdp", p: { geometry: "tube_oo", modes: 24, t60: 4, hfDamp: 3, damper: 6, exciter: "selfosc", selfGain: 0.3, selfSat: 0.5, exPos: 0.02, exWidth: 0.02, attack: 400, release: 1500, pickup: 0.5, spread: 0.3, width: 0.4, tilt: -3, reverbMix: 0.3, reverbSize: 4 }, map: [{ target: "selfGain", from: 0.1, to: 1.2 }, { target: "tilt", from: -4, to: 0 }], }, { name: "dark-reed", p: { ...preset("Clarinet-ish"), breath: 0.01, pickup: 0.15 }, map: [{ target: "pickup", from: 0.15, to: 0.5 }, { target: "tilt", from: -4, to: 1 }], }, { name: "warm-reed", p: { ...preset("Clarinet-ish"), breath: 0.01, pickup: 0.15, reverbMix: 0.3, reverbSize: 4 }, map: [{ target: "pickup", from: 0.15, to: 0.6 }, { target: "tilt", from: -4, to: -1 }], }, { name: "mellow-reed", p: { ...preset("Clarinet-ish"), breath: 0.01, pickup: 0.15, reverbMix: 0.3, reverbSize: 4 }, map: [{ target: "pickup", from: 0.15, to: 0.75 }, { target: "tilt", from: -4, to: 2 }], }, { name: "tube-co-vdp", p: { geometry: "tube_co", modes: 24, t60: 4, hfDamp: 3, damper: 6, exciter: "selfosc", selfGain: 0.3, selfSat: 0.5, exPos: 0, exWidth: 0.01, attack: 400, release: 1500, pickup: 0.2, spread: 0.3, width: 0.4, tilt: -4, reverbMix: 0.3, reverbSize: 4 }, map: [{ target: "selfGain", from: 0.15, to: 1.5 }, { target: "pickup", from: 0.15, to: 0.5 }], }, { name: "bass-tasto", p: { ...preset("Double bass"), vibDepth: 0, tilt: -6 }, map: [{ target: "exPos", from: 0.3, to: 0.12 }, { target: "tilt", from: -7, to: -3 }], }, ]; const STAGES = 5, STAGE_S = 3, LEAD_S = 2.5; function fftMag(x: Float64Array): Float64Array { const n = x.length, re = Float64Array.from(x), im = new Float64Array(n); for (let i = 1, j = 0; i < n; i++) { let bit = n >> 1; for (; j & bit; bit >>= 1) j ^= bit; j ^= bit; if (i < j) [re[i], re[j]] = [re[j], re[i]]; } for (let len = 2; len <= n; len <<= 1) { const a = (-2 * Math.PI) / len; for (let i = 0; i < n; i += len) for (let k = 0; k < len / 2; k++) { const c = Math.cos(a * k), s = Math.sin(a * k), h = len / 2; const vr = re[i + k + h] * c - im[i + k + h] * s, vi = re[i + k + h] * s + im[i + k + h] * c; re[i + k + h] = re[i + k] - vr; im[i + k + h] = im[i + k] - vi; re[i + k] += vr; im[i + k] += vi; } } const m = new Float64Array(n / 2); for (let i = 0; i < n / 2; i++) m[i] = Math.hypot(re[i], im[i]); return m; } /** brightness (centroid, × f0), share of energy within ±3% of harmonics, and fundamental's share */ function analyze(sig: Float32Array, start: number, f0: number) { const n = 32768, x = new Float64Array(n); for (let i = 0; i < n; i++) x[i] = (sig[start + i] ?? 0) * (0.5 - 0.5 * Math.cos((2 * Math.PI * i) / n)); const m = fftMag(x), df = SR / n; let num = 0, den = 0, harm = 0, fund = 0; for (let i = 1; i < m.length && i * df < 8000; i++) { const f = i * df, p = m[i] * m[i]; num += p * f; den += p; const k = Math.round(f / f0); if (k >= 1 && Math.abs(f - k * f0) < 0.03 * k * f0 + 2 * df) { harm += p; if (k === 1) fund += p; } } return { cent: num / den / f0, harm: harm / den, fund: fund / den }; } function render(c: Cand) { const synth = new Synth(SR); const base: Params = { ...DEFAULTS, ...c.p, voices: 1 }; synth.message({ type: "params", params: base }); const f0 = 440 * Math.pow(2, (PITCH - 69) / 12); synth.message({ type: "noteOn", note: PITCH, vel: 1 }); const total = Math.round((LEAD_S + STAGES * STAGE_S) * SR); const L = new Float32Array(total), R = new Float32Array(total); let msq = 0.09, lastParam = -1; const outL = new Float32Array(BLOCK), outR = new Float32Array(BLOCK); const rawL = new Float32Array(total); let comp = 1; for (let i = 0; i < total; i += BLOCK) { const t = i / SR; // color: 0 during lead-in, then stepped stages 0, .25, .5, .75, 1 const stage = t < LEAD_S ? 0 : Math.min(STAGES - 1, Math.floor((t - LEAD_S) / STAGE_S)); const color = stage / (STAGES - 1); if (stage !== lastParam) { lastParam = stage; const next: Params = { ...base }; for (const m of c.map) next[m.target] = m.from + (m.to - m.from) * color; synth.message({ type: "params", params: next }); } outL.fill(0); outR.fill(0); synth.process(outL, outR, BLOCK, t); let s = 0; for (let j = 0; j < BLOCK; j++) s += outL[j] * outL[j]; s /= BLOCK; if (s > 1e-12) msq += (s - msq) * (1 - Math.exp(-BLOCK / SR / 1.5)); comp = Math.max(1 / 20, Math.min(20, 0.3 / Math.sqrt(msq))); for (let j = 0; j < BLOCK && i + j < total; j++) { rawL[i + j] = outL[j]; L[i + j] = outL[j] * comp; R[i + j] = outR[j] * comp; } } const rows: string[] = []; for (let s = 0; s < STAGES; s++) { const start = Math.round((LEAD_S + s * STAGE_S + STAGE_S - 0.75) * SR); const a = analyze(rawL, start, f0); let e = 0; for (let j = 0; j < 9600; j++) e += rawL[start + j] ** 2; rows.push(`${(s / (STAGES - 1)).toFixed(2)}: ${(10 * Math.log10(e / 9600 + 1e-20)).toFixed(0)}dB bright ${a.cent.toFixed(2)}× harmonic ${(a.harm * 100).toFixed(0)}% fund ${(a.fund * 100).toFixed(0)}%`); } console.log(`\n${c.name} @ midi ${PITCH} (${f0.toFixed(1)} Hz) ${c.map.map((m) => `${m.target} ${m.from}→${m.to}`).join(", ")}`); for (const r of rows) console.log(" " + r); if (WAV) { fs.mkdirSync("renders", { recursive: true }); const n = L.length, buf = Buffer.alloc(44 + n * 4); buf.write("RIFF", 0); buf.writeUInt32LE(36 + n * 4, 4); buf.write("WAVE", 8); buf.write("fmt ", 12); buf.writeUInt32LE(16, 16); buf.writeUInt16LE(1, 20); buf.writeUInt16LE(2, 22); buf.writeUInt32LE(SR, 24); buf.writeUInt32LE(SR * 4, 28); buf.writeUInt16LE(4, 32); buf.writeUInt16LE(16, 34); buf.write("data", 36); buf.writeUInt32LE(n * 4, 40); let peak = 1e-9; for (let i = 0; i < n; i++) peak = Math.max(peak, Math.abs(L[i]), Math.abs(R[i])); const g = 0.5 / peak; for (let i = 0; i < n; i++) { const fade = Math.min(1, i / (0.3 * SR), (n - i) / (0.5 * SR)); buf.writeInt16LE(Math.round(Math.max(-1, Math.min(1, L[i] * g * fade)) * 32767), 44 + i * 4); buf.writeInt16LE(Math.round(Math.max(-1, Math.min(1, R[i] * g * fade)) * 32767), 46 + i * 4); } fs.writeFileSync(`renders/hum-${c.name}-${PITCH}.wav`, buf); } } for (const c of CANDIDATES) if (!ONLY || c.name === ONLY) { for (const m of c.map) if (!PARAM_BY_ID[m.target]) throw new Error(`unknown target ${m.target}`); render(c); }