// Minimal SoundFont 2.01 writer: one preset -> one instrument -> many sample zones. const enc = new TextEncoder(); function str(s, len) { // fixed-length, zero-padded ASCII const b = new Uint8Array(len); b.set(enc.encode(s.replace(/[^\x20-\x7e]/g, '_')).slice(0, len - 1)); return b; } function zstr(s) { // zero-terminated, even length const t = enc.encode(s.replace(/[^\x20-\x7e]/g, '_')); const b = new Uint8Array(t.length + 1 + ((t.length + 1) % 2)); b.set(t); return b; } function concat(parts) { const n = parts.reduce((a, p) => a + p.length, 0); const out = new Uint8Array(n); let o = 0; for (const p of parts) { out.set(p, o); o += p.length; } return out; } function chunk(id, data) { const h = new Uint8Array(8); h.set(enc.encode(id)); new DataView(h.buffer).setUint32(4, data.length, true); return concat(data.length % 2 ? [h, data, new Uint8Array(1)] : [h, data]); } function list(type, chunks) { return chunk('LIST', concat([enc.encode(type), ...chunks])); } // Fixed-size record builder function records(size, items, fill) { const b = new Uint8Array(size * items.length); const dv = new DataView(b.buffer); items.forEach((it, i) => fill(dv, i * size, it, b)); return b; } export const GEN = { releaseVolEnv: 38, instrument: 41, keyRange: 43, velRange: 44, sampleModes: 54, sampleID: 53, overridingRootKey: 58 }; /** * opts: { * name, bank, program, sampleRate, * samples: [{ name, data: Int16Array, root, correction? }], * zones: [{ keyLo, keyHi, velLo, velHi, sample }], * releaseSec, disableVelocityAttenuation * } */ export function buildSF2(opts) { const { name, samples, zones } = opts; const PAD = 46; // ---- sdta ---- const total = samples.reduce((a, s) => a + s.data.length + PAD, 0); const smpl = new Int16Array(total); const offsets = []; let o = 0; for (const s of samples) { offsets.push(o); smpl.set(s.data, o); o += s.data.length + PAD; } const sdta = list('sdta', [chunk('smpl', new Uint8Array(smpl.buffer))]); // ---- INFO ---- const ifil = new Uint8Array(4); new DataView(ifil.buffer).setUint16(0, 2, true); new DataView(ifil.buffer).setUint16(2, 1, true); const info = list('INFO', [ chunk('ifil', ifil), chunk('isng', zstr('EMU8000')), chunk('INAM', zstr(name)), chunk('ISFT', zstr('Woodblock Lab')), ]); // ---- pdta ---- // instrument zones: global zone (release + velocity modulator override), then sample zones const igen = [], ibag = [], imod = []; ibag.push([igen.length, imod.length]); igen.push([GEN.releaseVolEnv, Math.round(1200 * Math.log2(opts.releaseSec ?? 2)), 's']); if (opts.disableVelocityAttenuation) { // same source/dest as the default "velocity -> initial attenuation" modulator, amount 0 imod.push([0x0502, 48, 0, 0, 0]); } for (const z of zones) { ibag.push([igen.length, imod.length]); igen.push([GEN.keyRange, [z.keyLo, z.keyHi], 'r']); igen.push([GEN.velRange, [z.velLo, z.velHi], 'r']); igen.push([GEN.sampleModes, 0, 'u']); igen.push([GEN.sampleID, z.sample, 'u']); } const nIbag = ibag.length; ibag.push([igen.length, imod.length]); // terminal igen.push([0, 0, 'u']); imod.push([0, 0, 0, 0, 0]); const genRec = (dv, off, [op, amt, kind]) => { dv.setUint16(off, op, true); if (kind === 'r') { dv.setUint8(off + 2, amt[0]); dv.setUint8(off + 3, amt[1]); } else if (kind === 's') dv.setInt16(off + 2, amt, true); else dv.setUint16(off + 2, amt, true); }; const bagRec = (dv, off, [g, m]) => { dv.setUint16(off, g, true); dv.setUint16(off + 2, m, true); }; const modRec = (dv, off, [src, dst, amt, asrc, tr]) => { dv.setUint16(off, src, true); dv.setUint16(off + 2, dst, true); dv.setInt16(off + 4, amt, true); dv.setUint16(off + 6, asrc, true); dv.setUint16(off + 8, tr, true); }; const inst = records(22, [[name, 0], ['EOI', nIbag]], (dv, off, [n, bag], b) => { b.set(str(n, 20), off); dv.setUint16(off + 20, bag, true); }); // preset: one zone pointing at instrument 0 const pgen = [[GEN.instrument, 0, 'u'], [0, 0, 'u']]; const pbag = [[0, 0], [1, 0]]; const pmod = [[0, 0, 0, 0, 0]]; const phdr = records(38, [[name, opts.program ?? 0, opts.bank ?? 0, 0], ['EOP', 0, 0, 1]], (dv, off, [n, prog, bank, bag], b) => { b.set(str(n, 20), off); dv.setUint16(off + 20, prog, true); dv.setUint16(off + 22, bank, true); dv.setUint16(off + 24, bag, true); }); const sr = opts.sampleRate; const shdrItems = samples.map((s, i) => ({ ...s, start: offsets[i], end: offsets[i] + s.data.length })); shdrItems.push({ name: 'EOS', start: 0, end: 0, root: 0, terminal: true }); const shdr = records(46, shdrItems, (dv, off, s, b) => { b.set(str(s.name, 20), off); dv.setUint32(off + 20, s.start, true); dv.setUint32(off + 24, s.end, true); const ls = s.terminal ? 0 : Math.min(s.start + 8, s.end); const le = s.terminal ? 0 : Math.max(ls, s.end - 8); dv.setUint32(off + 28, ls, true); dv.setUint32(off + 32, le, true); dv.setUint32(off + 36, s.terminal ? 0 : sr, true); dv.setUint8(off + 40, s.root); dv.setInt8(off + 41, s.correction ?? 0); dv.setUint16(off + 42, 0, true); dv.setUint16(off + 44, s.terminal ? 0 : 1, true); // 1 = mono sample }); const pdta = list('pdta', [ chunk('phdr', phdr), chunk('pbag', records(4, pbag, bagRec)), chunk('pmod', records(10, pmod, modRec)), chunk('pgen', records(4, pgen, genRec)), chunk('inst', inst), chunk('ibag', records(4, ibag, bagRec)), chunk('imod', records(10, imod, modRec)), chunk('igen', records(4, igen, genRec)), chunk('shdr', shdr), ]); return chunk('RIFF', concat([enc.encode('sfbk'), info, sdta, pdta])); }