ws-music: music/sf2 + SoundFont tests vs FluidSynth (envelope r 0.995, band spectra r >= 0.969, pitch peaks within 0.6%), gain ramp per block, bench lines

tools/bench.sh: also generates the midi + sf2 fixtures (the bench renders their songs). licenses: TinySoundFont MIT row in NOTICE.md.
This commit is contained in:
4DBug committed 2026-10-31 08:44:56 -05:00
1 parent 08af33d82a
commit d7c2d2eeb5
10 files changed
+707 -11

No files matched your search

+5 -5
View File
@@ -29,7 +29,7 @@ export type SoundFont = {
meta: { [string]: any },
}
local GEN_INSTRUMENT, GEN_KEYRANGE, GEN_VELRANGE, GEN_SAMPLEID = 41, 43, 44, 53
local GEN_INSTRUMENT, GEN_SAMPLEID = 41, 53
-- generator op -> { field, mode, limit }. modes: f / i (value, merged by adding then clamping), a (sample address
-- add), a15 (coarse address add: * 32768), kr / vr (key / velocity range), loop, group, key (overriding root key)
@@ -235,7 +235,7 @@ local LAYOUT: { [string]: { number } } = {
igen = { 2, 2 },
shdr = { 20, 4, 4, 4, 4, 4, 1, -1, 2, 2 },
}
local RECSIZE = { phdr = 38, pbag = 4, pmod = 10, pgen = 4, inst = 22, ibag = 4, imod = 10, igen = 4, shdr = 46 }
local RECSIZE: { [string]: number } = { phdr = 38, pbag = 4, pmod = 10, pgen = 4, inst = 22, ibag = 4, imod = 10, igen = 4, shdr = 46 }
local INFO_NAMES = {
isng = "engine", INAM = "name", irom = "rom", ICRD = "date", IENG = "author", IPRD = "product",
@@ -296,7 +296,7 @@ function Parse.parse(r: Bytes.Reader, opts: Options?): (SoundFont?, Err.Err?)
if layout and not hydra[sid] then
local rs = RECSIZE[sid]
local n = sub:remaining() // rs
local recs = table.create(n)
local recs: { { number } } = table.create(n)
for i = 1, n do
local rec = {}
for _, w in layout do
@@ -487,7 +487,7 @@ function Parse.parse(r: Bytes.Reader, opts: Options?): (SoundFont?, Err.Err?)
for _, p in presets do
(p :: any)._order = nil
end
local headers = table.create(nShdr)
local headers: { { [string]: any } } = table.create(nShdr)
for i = 1, nShdr do
local sh = shdr[i]
headers[i] = {
@@ -503,7 +503,7 @@ function Parse.parse(r: Bytes.Reader, opts: Options?): (SoundFont?, Err.Err?)
type = sh[10],
}
end
local instruments = table.create(nInst)
local instruments: { { name: string, zones: number } } = table.create(nInst)
for i = 1, nInst do
instruments[i] = { name = instNames[i], zones = math.max(0, (inst[i + 1][2] or 0) - inst[i][2]) }
end
+10 -4
View File
@@ -8,7 +8,7 @@
-- program / bank select (CC0 / CC32), drums (bank 128), pitch bend with RPN 0 range and RPN 1 / 2 tuning, CC7 volume,
-- CC10 pan, CC11 expression, CC64 sustain, CC120 / 121 / 123. Effects (reverb, chorus) and modulators are not
-- implemented (as in tsf.h). Output: interleaved stereo f32, mixed into the caller's buffer.
-- Differences from tsf.h: a voice limit (oldest release voice stolen, else the note is dropped), channels 0..15 only.
-- Differences from tsf.h: the envelope gain is ramped across each 64-sample block (tsf.h steps it); a voice limit (oldest release voice stolen, else the note is dropped), channels 0..15 only.
-- Native, never throws: every buffer index is in range by construction (the regions are clamped by Parse / Fallback,
-- the sample buffer has a zero pad sample, the output range is checked before mixing).
local Synth = {}
@@ -734,7 +734,11 @@ local function voiceRender(s: State, v: Voice, out: buffer, frame: number, numSa
if updateVibLfo then
lfoProcess(v.viblfo, block)
end
local gl, gr = gainMono * v.panL, gainMono * v.panR
-- ramp the gain across the block to the envelope's next level (tsf.h steps it per block: zipper noise)
local gainNext = noteGain * v.amp.level
local step = (gainNext - gainMono) / block
local panL, panR = v.panL, v.panR
local g = gainMono
local k = block
while k > 0 and pos < endD do
k -= 1
@@ -748,8 +752,10 @@ local function voiceRender(s: State, v: Voice, out: buffer, frame: number, numSa
z2 = val * a0 - b2 * y
val = y
end
writef32(out, o, readf32(out, o) + val * gl)
writef32(out, o + 4, readf32(out, o + 4) + val * gr)
val *= g
g += step
writef32(out, o, readf32(out, o) + val * panL)
writef32(out, o + 4, readf32(out, o + 4) + val * panR)
o += 8
pos += pitchRatio
if pos >= loopEndD and isLooping then
+1 -1
View File
@@ -25,7 +25,7 @@ export type WriteOptions = {
}
local function bytesOf(b: buffer, p: number, n: number): { number }
local t = table.create(n)
local t: { number } = table.create(n)
for i = 0, n - 1 do
t[i + 1] = readu8(b, p + i)
end