// Render a velocity-layered marimba SoundFont. Every sampled key is its own bar (the key's // edited design, or the auto-tuned one) over its own tuned tube, so the low end really is // longer, darker and slower to decay than the top. import { feBarModel } from './fe.js'; import { prepare, strike } from './synth.js'; import { buildSF2 } from './sf2.js'; export const LAYERS = 4; // velocity layers export const VARIANTS = 3; // strike-point variations, interleaved in velocity const RR_OFFSETS = [[0, 0], [0.012, -0.06], [-0.01, 0.07]]; // fractions of L and W export function sampledKeys(lo, hi, step) { const keys = []; for (let k = lo; k <= hi; k += step) keys.push(k); if (keys[keys.length - 1] !== hi && hi - keys[keys.length - 1] > step / 2) keys.push(hi); return keys; } /** * opts: { lo, hi, step, name, sampleRate, designFor(note) -> design, mat, * sound: { eta, slope, res }, malletFor(note) -> mallet (with v0), strike: [fx, fy], onProgress(frac, label) } */ export async function exportSF2(opts) { const { lo, hi, step, name, mat, sound } = opts; const sr = opts.sampleRate ?? 44100; const keys = sampledKeys(lo, hi, step); const ranges = keys.map((k, i) => [ i === 0 ? lo : Math.floor((keys[i - 1] + k) / 2) + 1, i === keys.length - 1 ? hi : Math.floor((k + keys[i + 1]) / 2), ]); const nBands = LAYERS * VARIANTS; const band = (b) => [1 + Math.floor((b * 127) / nBands), Math.floor(((b + 1) * 127) / nBands)]; const raw = [], zones = []; const total = keys.length * nBands; let done = 0, peak = 0; for (let ki = 0; ki < keys.length; ki++) { const k = keys[ki]; const design = opts.designFor(k); const model = feBarModel(design, mat); const prep = prepare(model, { note: k, eta: sound.eta, slope: sound.slope, res: sound.res }); for (let b = 0; b < nBands; b++) { const layer = Math.floor(b / VARIANTS), rr = b % VARIANTS; const x = (layer + 1) / LAYERS; const m0 = opts.malletFor(k); const ml = { ...m0, v0: m0.v0 * (0.12 + 0.88 * x), Ky: m0.Ky + 0.3 * (x - 1) }; const fx = Math.min(0.97, Math.max(0.03, opts.strike[0] + RR_OFFSETS[rr][0])); const fy = Math.min(0.95, Math.max(0.05, opts.strike[1] + RR_OFFSETS[rr][1])); const { y } = strike(prep, fx * design.L, fy * design.W, ml, sr, 3); for (let i = 0; i < y.length; i++) peak = Math.max(peak, Math.abs(y[i])); const [vlo, vhi] = band(b); zones.push({ keyLo: ranges[ki][0], keyHi: ranges[ki][1], velLo: vlo, velHi: vhi, sample: raw.length }); raw.push({ y, root: k, name: `mb${k}v${layer + 1}r${rr + 1}` }); done++; opts.onProgress?.(done / total, k); await new Promise((r) => setTimeout(r, 0)); } } // one global gain so keys and layers keep their physical loudness; trim tails at -66 dB const g = (0.89 * 32767) / (peak || 1); const floor = peak * 10 ** (-66 / 20); const fade = Math.round(0.01 * sr); const samples = raw.map((s) => { let end = s.y.length - 1; while (end > 0 && Math.abs(s.y[end]) < floor) end--; const n = Math.min(s.y.length, end + fade); const d = new Int16Array(n); for (let i = 0; i < n; i++) { const fo = i > n - fade ? (n - i) / fade : 1; d[i] = Math.max(-32768, Math.min(32767, Math.round(s.y[i] * g * fo))); } return { name: s.name, data: d, root: s.root }; }); const bytes = buildSF2({ name, bank: 0, program: 12, sampleRate: sr, samples, zones, // GM 13: Marimba releaseSec: 1.5, disableVelocityAttenuation: true, }); return { bytes, keys, nSamples: samples.length }; }