/ music / acoustic-base
Sign in
music / acoustic-base
acoustic-base / src / core / reference.ts
29 lines · 1.5 KBCodeBlameHistory
087c169Physically simulated pizzicato double bass and an SF2 built from itJeremy Magland 1// Render notes of a preset from an SF2 with spessasynth_core, dry (effects off), mono — used for
2// the reference comparison (GeneralUser GS "Acoustic Bass", program 32, by default) in the app's
3// worker and in scripts/render_reference.ts.
4import { SoundBankLoader, SpessaSynthProcessor } from "spessasynth_core";
6export async function renderReference(sf2: ArrayBuffer | Uint8Array, program: number, notes: [number, number][], hold: number, len: number, fs = 44100, bank = 0): Promise<Float32Array[]> {
7 const buf = sf2 instanceof Uint8Array ? (sf2.buffer.slice(sf2.byteOffset, sf2.byteOffset + sf2.byteLength) as ArrayBuffer) : sf2;
8 const out: Float32Array[] = [];
9 for (const [note, vel] of notes) {
10 const synth = new SpessaSynthProcessor(fs, { effectsEnabled: false } as never);
11 synth.soundBankManager.addSoundBank(SoundBankLoader.fromArrayBuffer(buf.slice(0)), "main");
12 await (synth as unknown as { ready: Promise<void> }).ready;
13 if (bank) synth.controllerChange(0, 0 as never, bank);
14 synth.programChange(0, program);
15 const n = Math.round(len * fs);
16 const L = new Float32Array(n), R = new Float32Array(n);
17 const block = 128, off = Math.round(hold * fs);
18 synth.noteOn(0, note, vel);
19 let released = false;
20 for (let s = 0; s < n; s += block) {
21 if (!released && s + block > off) { synth.noteOff(0, note); released = true; }
22 synth.process(L, R, s, Math.min(block, n - s));
23 }
24 const m = new Float32Array(n);
25 for (let i = 0; i < n; i++) m[i] = 0.5 * (L[i] + R[i]);
26 out.push(m);
27 }
28 return out;
↑↓ move↵openescclose