1// Render the reference instrument (by default GeneralUser GS, bank 0 program 32 "Acoustic Bass")
2// with spessasynth_core, dry (no reverb/chorus), to refs/ref_<note>_<vel>.wav, for comparison.
3// The reference samples belong to that SoundFont; refs/ is git-ignored.
4// node scripts/render_reference.ts [path/to.sf2] [--program 32] [--hold 2.5] [--len 4] note:vel ...
5import fs from "node:fs";
6import os from "node:os";
7import path from "node:path";
8import { encodeWav } from "../src/core/dsp.ts";
9import { renderReference } from "../src/core/reference.ts";
11export const DEFAULT_REF = path.join(os.homedir(), "Downloads", "GeneralUser-GS", "GeneralUser-GS.sf2");
13if (import.meta.main) {
14 const args = process.argv.slice(2);
15 let file = DEFAULT_REF, program = 32, hold = 2.5, len = 4;
16 const notes: [number, number][] = [];
17 for (let i = 0; i < args.length; i++) {
18 const a = args[i];
19 if (a === "--program") program = Number(args[++i]);
20 else if (a === "--hold") hold = Number(args[++i]);
21 else if (a === "--len") len = Number(args[++i]);
22 else if (a.endsWith(".sf2")) file = a;
23 else { const [n, v] = a.split(":").map(Number); notes.push([n, v ?? 100]); }
24 }
25 if (!notes.length) for (let n = 28; n <= 58; n += 3) for (const v of [40, 80, 118]) notes.push([n, v]);
26 const sf2 = new Uint8Array(fs.readFileSync(file));
27 const dir = path.join(import.meta.dirname, "..", "refs");
28 fs.mkdirSync(dir, { recursive: true });
29 const res = await renderReference(sf2, program, notes, hold, len);
30 res.forEach((x, i) => {
31 const [n, v] = notes[i];
32 let pk = 0; for (const s of x) pk = Math.max(pk, Math.abs(s));
33 fs.writeFileSync(path.join(dir, `ref_${n}_${v}.wav`), encodeWav(x, 44100, { float: true }));
34 console.log(`ref ${n}:${v} peak ${pk.toFixed(3)}`);
35 });
36}