// Render a chromatic, velocity-layered woodblock SoundFont from the modal model. import { modalStrike, dominantFreq } from './modal.js'; import { buildSF2 } from './sf2.js'; const NOTE = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']; export const noteName = (k) => NOTE[k % 12] + (Math.floor(k / 12) - 1); export const freqToMidi = (f) => 69 + 12 * Math.log2(f / 440); export const LAYERS = 4; // velocity layers export const VARIANTS = 3; // strike-point variations, interleaved in velocity const RR_OFFSETS = [[0, 0], [5, -3], [-4, 4]]; // mm /** * opts: { model, block, strike:[x,y], live:{eta,K,m,v0}, lo, hi, step, sampleRate, name, * onProgress(frac) } */ export async function exportSF2(opts) { const { model, block, live, lo, hi, step, name } = opts; const sr = opts.sampleRate ?? 44100; const fRef = dominantFreq(model, opts.strike[0], opts.strike[1], live); // sampled keys and the key range each one covers 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); 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), ]); // velocity bands: LAYERS x VARIANTS equal slices of 1..127 const nBands = LAYERS * VARIANTS; const band = (b) => [1 + Math.floor((b * 127) / nBands), Math.floor(((b + 1) * 127) / nBands)]; const pts = RR_OFFSETS.map(([dx, dy]) => [ Math.min(block.L - 3, Math.max(3, opts.strike[0] + dx)), Math.min(block.W - 3, Math.max(3, opts.strike[1] + dy)), ]); 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 pitch = (440 * 2 ** ((k - 69) / 12)) / fRef; for (let b = 0; b < nBands; b++) { const layer = Math.floor(b / VARIANTS), rr = b % VARIANTS; const x = (layer + 1) / LAYERS; const p = { pitch, eta: live.eta, m: live.m, v0: live.v0 * (0.12 + 0.88 * x), K: live.K * 10 ** (0.6 * (x - 1)) }; const { y } = modalStrike(model, pts[rr][0], pts[rr][1], p, sr, 2.5); 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: `wb${k}v${layer + 1}r${rr + 1}` }); done++; if (done % 4 === 0) { opts.onProgress?.(done / total); await new Promise((r) => setTimeout(r, 0)); } } } // one global gain so velocity layers keep their physical loudness; trim tails at -72 dB const g = (0.89 * 32767) / (peak || 1); const floor = peak * 10 ** (-72 / 20); const fade = Math.round(0.005 * 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: 115, sampleRate: sr, samples, zones, releaseSec: 2.5, disableVelocityAttenuation: true, }); return { bytes, fRef, keys, nSamples: samples.length }; }