/ music / marimba
Sign in
music / marimba
marimba / js / export.js
82 lines · 3.5 KBBlameHistoryRaw
1// Render a velocity-layered marimba SoundFont. Every sampled key is its own bar (the key's
2// edited design, or the auto-tuned one) over its own tuned tube, so the low end really is
3// longer, darker and slower to decay than the top.
5import { feBarModel } from './fe.js';
6import { prepare, strike } from './synth.js';
7import { buildSF2 } from './sf2.js';
9export const LAYERS = 4; // velocity layers
10export const VARIANTS = 3; // strike-point variations, interleaved in velocity
11const RR_OFFSETS = [[0, 0], [0.012, -0.06], [-0.01, 0.07]]; // fractions of L and W
13export function sampledKeys(lo, hi, step) {
14 const keys = [];
15 for (let k = lo; k <= hi; k += step) keys.push(k);
16 if (keys[keys.length - 1] !== hi && hi - keys[keys.length - 1] > step / 2) keys.push(hi);
17 return keys;
20/**
21 * opts: { lo, hi, step, name, sampleRate, designFor(note) -> design, mat,
22 * sound: { eta, slope, res }, malletFor(note) -> mallet (with v0), strike: [fx, fy], onProgress(frac, label) }
23 */
24export async function exportSF2(opts) {
25 const { lo, hi, step, name, mat, sound } = opts;
26 const sr = opts.sampleRate ?? 44100;
27 const keys = sampledKeys(lo, hi, step);
28 const ranges = keys.map((k, i) => [
29 i === 0 ? lo : Math.floor((keys[i - 1] + k) / 2) + 1,
30 i === keys.length - 1 ? hi : Math.floor((k + keys[i + 1]) / 2),
31 ]);
32 const nBands = LAYERS * VARIANTS;
33 const band = (b) => [1 + Math.floor((b * 127) / nBands), Math.floor(((b + 1) * 127) / nBands)];
35 const raw = [], zones = [];
36 const total = keys.length * nBands;
37 let done = 0, peak = 0;
38 for (let ki = 0; ki < keys.length; ki++) {
39 const k = keys[ki];
40 const design = opts.designFor(k);
41 const model = feBarModel(design, mat);
42 const prep = prepare(model, { note: k, eta: sound.eta, slope: sound.slope, res: sound.res });
43 for (let b = 0; b < nBands; b++) {
44 const layer = Math.floor(b / VARIANTS), rr = b % VARIANTS;
45 const x = (layer + 1) / LAYERS;
46 const m0 = opts.malletFor(k);
47 const ml = { ...m0, v0: m0.v0 * (0.12 + 0.88 * x), Ky: m0.Ky + 0.3 * (x - 1) };
48 const fx = Math.min(0.97, Math.max(0.03, opts.strike[0] + RR_OFFSETS[rr][0]));
49 const fy = Math.min(0.95, Math.max(0.05, opts.strike[1] + RR_OFFSETS[rr][1]));
50 const { y } = strike(prep, fx * design.L, fy * design.W, ml, sr, 3);
51 for (let i = 0; i < y.length; i++) peak = Math.max(peak, Math.abs(y[i]));
52 const [vlo, vhi] = band(b);
53 zones.push({ keyLo: ranges[ki][0], keyHi: ranges[ki][1], velLo: vlo, velHi: vhi, sample: raw.length });
54 raw.push({ y, root: k, name: `mb${k}v${layer + 1}r${rr + 1}` });
55 done++;
56 opts.onProgress?.(done / total, k);
57 await new Promise((r) => setTimeout(r, 0));
58 }
59 }
61 // one global gain so keys and layers keep their physical loudness; trim tails at -66 dB
62 const g = (0.89 * 32767) / (peak || 1);
63 const floor = peak * 10 ** (-66 / 20);
64 const fade = Math.round(0.01 * sr);
65 const samples = raw.map((s) => {
66 let end = s.y.length - 1;
67 while (end > 0 && Math.abs(s.y[end]) < floor) end--;
68 const n = Math.min(s.y.length, end + fade);
69 const d = new Int16Array(n);
70 for (let i = 0; i < n; i++) {
71 const fo = i > n - fade ? (n - i) / fade : 1;
72 d[i] = Math.max(-32768, Math.min(32767, Math.round(s.y[i] * g * fo)));
73 }
74 return { name: s.name, data: d, root: s.root };
75 });
77 const bytes = buildSF2({
78 name, bank: 0, program: 12, sampleRate: sr, samples, zones, // GM 13: Marimba
79 releaseSec: 1.5, disableVelocityAttenuation: true,
80 });
81 return { bytes, keys, nSamples: samples.length };
↑↓ move↵openescclose