1// Offline audition of hum candidates (modal-synth engine in Node, no browser).
2// Each candidate holds one note while its timbre sweeps 0 → 1 (as the energy would),
3// with the same slow loudness normalization the app uses. Prints brightness/stability
4// per stage and, with --wav, writes renders/hum-<name>.wav.
5// node scripts/render-hum.ts [--wav] [--pitch 38] [--only name]
6import fs from "node:fs";
7import { Synth } from "../../modal-synth/src/engine/synth.ts";
8import { DEFAULTS, PARAM_BY_ID, type Params } from "../../modal-synth/src/engine/params.ts";
9import { PRESETS } from "../../modal-synth/src/presets.ts";
11const args = process.argv.slice(2);
12const opt = (k: string, d: string) => (args.includes(`--${k}`) ? args[args.indexOf(`--${k}`) + 1] : d);
13const WAV = args.includes("--wav");
14const PITCH = Number(opt("pitch", "38"));
15const ONLY = opt("only", "");
16const SR = 48000, BLOCK = 128;
17const preset = (n: string) => PRESETS.find((p) => p.name === n)!.p;
19interface Map { target: string; from: number; to: number } // actual values, not fractions
20interface Cand { name: string; p: Params; map: Map[] }
22export const CANDIDATES: Cand[] = [
23 { name: "clarinet", p: preset("Clarinet-ish"), map: [{ target: "pickup", from: 0.25, to: 0.75 }, { target: "tilt", from: 0, to: 4 }] },
24 { 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 }] },
25 { 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 }] },
26 { name: "bowl", p: preset("Singing bowl"), map: [{ target: "selfGain", from: 0.1, to: 0.6 }, { target: "pickup", from: 0.15, to: 0.4 }] },
27 // van der Pol on a string: weak self-excitation is nearly sinusoidal, strong becomes a rich relaxation oscillation
28 {
29 name: "string-vdp",
30 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 },
31 map: [{ target: "selfGain", from: 0.1, to: 1.2 }, { target: "tilt", from: -4, to: 0 }],
32 },
33 {
34 name: "tube-vdp",
35 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 },
36 map: [{ target: "selfGain", from: 0.1, to: 1.2 }, { target: "tilt", from: -4, to: 0 }],
37 },
38 {
39 name: "dark-reed",
40 p: { ...preset("Clarinet-ish"), breath: 0.01, pickup: 0.15 },
41 map: [{ target: "pickup", from: 0.15, to: 0.5 }, { target: "tilt", from: -4, to: 1 }],
42 },
43 {
44 name: "warm-reed",
45 p: { ...preset("Clarinet-ish"), breath: 0.01, pickup: 0.15, reverbMix: 0.3, reverbSize: 4 },
46 map: [{ target: "pickup", from: 0.15, to: 0.6 }, { target: "tilt", from: -4, to: -1 }],
47 },
48 {
49 name: "mellow-reed",
50 p: { ...preset("Clarinet-ish"), breath: 0.01, pickup: 0.15, reverbMix: 0.3, reverbSize: 4 },
51 map: [{ target: "pickup", from: 0.15, to: 0.75 }, { target: "tilt", from: -4, to: 2 }],
52 },
53 {
54 name: "tube-co-vdp",
55 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 },
56 map: [{ target: "selfGain", from: 0.15, to: 1.5 }, { target: "pickup", from: 0.15, to: 0.5 }],
57 },
58 {
59 name: "bass-tasto",
60 p: { ...preset("Double bass"), vibDepth: 0, tilt: -6 },
61 map: [{ target: "exPos", from: 0.3, to: 0.12 }, { target: "tilt", from: -7, to: -3 }],
62 },
63];
65const STAGES = 5, STAGE_S = 3, LEAD_S = 2.5;
67function fftMag(x: Float64Array): Float64Array {
68 const n = x.length, re = Float64Array.from(x), im = new Float64Array(n);
69 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]]; }
70 for (let len = 2; len <= n; len <<= 1) {
71 const a = (-2 * Math.PI) / len;
72 for (let i = 0; i < n; i += len) for (let k = 0; k < len / 2; k++) {
73 const c = Math.cos(a * k), s = Math.sin(a * k), h = len / 2;
74 const vr = re[i + k + h] * c - im[i + k + h] * s, vi = re[i + k + h] * s + im[i + k + h] * c;
75 re[i + k + h] = re[i + k] - vr; im[i + k + h] = im[i + k] - vi; re[i + k] += vr; im[i + k] += vi;
76 }
77 }
78 const m = new Float64Array(n / 2);
79 for (let i = 0; i < n / 2; i++) m[i] = Math.hypot(re[i], im[i]);
80 return m;
81}
83/** brightness (centroid, × f0), share of energy within ±3% of harmonics, and fundamental's share */
84function analyze(sig: Float32Array, start: number, f0: number) {
85 const n = 32768, x = new Float64Array(n);
86 for (let i = 0; i < n; i++) x[i] = (sig[start + i] ?? 0) * (0.5 - 0.5 * Math.cos((2 * Math.PI * i) / n));
87 const m = fftMag(x), df = SR / n;
88 let num = 0, den = 0, harm = 0, fund = 0;
89 for (let i = 1; i < m.length && i * df < 8000; i++) {
90 const f = i * df, p = m[i] * m[i];
91 num += p * f; den += p;
92 const k = Math.round(f / f0);
93 if (k >= 1 && Math.abs(f - k * f0) < 0.03 * k * f0 + 2 * df) { harm += p; if (k === 1) fund += p; }
94 }
95 return { cent: num / den / f0, harm: harm / den, fund: fund / den };
96}
98function render(c: Cand) {
99 const synth = new Synth(SR);
100 const base: Params = { ...DEFAULTS, ...c.p, voices: 1 };
101 synth.message({ type: "params", params: base });
102 const f0 = 440 * Math.pow(2, (PITCH - 69) / 12);
103 synth.message({ type: "noteOn", note: PITCH, vel: 1 });
104 const total = Math.round((LEAD_S + STAGES * STAGE_S) * SR);
105 const L = new Float32Array(total), R = new Float32Array(total);
106 let msq = 0.09, lastParam = -1;
107 const outL = new Float32Array(BLOCK), outR = new Float32Array(BLOCK);
108 const rawL = new Float32Array(total);
109 let comp = 1;
110 for (let i = 0; i < total; i += BLOCK) {
111 const t = i / SR;
112 // color: 0 during lead-in, then stepped stages 0, .25, .5, .75, 1
113 const stage = t < LEAD_S ? 0 : Math.min(STAGES - 1, Math.floor((t - LEAD_S) / STAGE_S));
114 const color = stage / (STAGES - 1);
115 if (stage !== lastParam) {
116 lastParam = stage;
117 const next: Params = { ...base };
118 for (const m of c.map) next[m.target] = m.from + (m.to - m.from) * color;
119 synth.message({ type: "params", params: next });
120 }
121 outL.fill(0); outR.fill(0);
122 synth.process(outL, outR, BLOCK, t);
123 let s = 0;
124 for (let j = 0; j < BLOCK; j++) s += outL[j] * outL[j];
125 s /= BLOCK;
126 if (s > 1e-12) msq += (s - msq) * (1 - Math.exp(-BLOCK / SR / 1.5));
127 comp = Math.max(1 / 20, Math.min(20, 0.3 / Math.sqrt(msq)));
128 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; }
129 }
130 const rows: string[] = [];
131 for (let s = 0; s < STAGES; s++) {
132 const start = Math.round((LEAD_S + s * STAGE_S + STAGE_S - 0.75) * SR);
133 const a = analyze(rawL, start, f0);
134 let e = 0; for (let j = 0; j < 9600; j++) e += rawL[start + j] ** 2;
135 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)}%`);
136 }
137 console.log(`\n${c.name} @ midi ${PITCH} (${f0.toFixed(1)} Hz) ${c.map.map((m) => `${m.target} ${m.from}→${m.to}`).join(", ")}`);
138 for (const r of rows) console.log(" " + r);
139 if (WAV) {
140 fs.mkdirSync("renders", { recursive: true });
141 const n = L.length, buf = Buffer.alloc(44 + n * 4);
142 buf.write("RIFF", 0); buf.writeUInt32LE(36 + n * 4, 4); buf.write("WAVE", 8); buf.write("fmt ", 12);
143 buf.writeUInt32LE(16, 16); buf.writeUInt16LE(1, 20); buf.writeUInt16LE(2, 22); buf.writeUInt32LE(SR, 24);
144 buf.writeUInt32LE(SR * 4, 28); buf.writeUInt16LE(4, 32); buf.writeUInt16LE(16, 34); buf.write("data", 36); buf.writeUInt32LE(n * 4, 40);
145 let peak = 1e-9; for (let i = 0; i < n; i++) peak = Math.max(peak, Math.abs(L[i]), Math.abs(R[i]));
146 const g = 0.5 / peak;
147 for (let i = 0; i < n; i++) {
148 const fade = Math.min(1, i / (0.3 * SR), (n - i) / (0.5 * SR));
149 buf.writeInt16LE(Math.round(Math.max(-1, Math.min(1, L[i] * g * fade)) * 32767), 44 + i * 4);
150 buf.writeInt16LE(Math.round(Math.max(-1, Math.min(1, R[i] * g * fade)) * 32767), 46 + i * 4);
151 }
152 fs.writeFileSync(`renders/hum-${c.name}-${PITCH}.wav`, buf);
153 }
154}
156for (const c of CANDIDATES) if (!ONLY || c.name === ONLY) {
157 for (const m of c.map) if (!PARAM_BY_ID[m.target]) throw new Error(`unknown target ${m.target}`);
158 render(c);
159}