Files
rosetta/src/Formats/codecs/SoundFont/Fallback.luau
T
4DBug d2280d3b77 ws-music: music/midi (SMF 0/1/2 read/write, tempo map, render), codecs/SoundFont (SF2 parser + TinySoundFont-port synth + fallback waveform set), midi tests vs mido
Types.luau: Midi gets an optional render(self, opts) -> Decoder field (backward compatible; set by music/midi decode).
2026-10-31 08:37:25 -05:00

150 lines
6.8 KiB
Luau

--!strict
-- codecs/SoundFont/Fallback: the built-in General MIDI set used when no SoundFont is given. A generated SoundFont
-- (the same shape Parse returns, so the same synth plays it): single-cycle sine / triangle / square / saw waves and a
-- looped noise sample, one preset per GM family of 8 programs (piano, chromatic percussion, organ, guitar, bass,
-- strings, ensemble, brass, reed, pipe, lead, pad, synth fx, ethnic, percussive, sound fx) with family envelopes,
-- filters and vibrato, plus a drum kit on bank 128 (pitch-swept sine kicks and toms, filtered noise snares, hats with
-- an exclusive class, cymbals). Written for this library (no third-party code).
local Parse = require("@formats/codecs/SoundFont/Parse")
local Fallback = {}
local CYCLE = 256
local NOISE = 32768
local C4 = 261.6255653005986
local function hzToCents(hz: number): number
return 1200 * math.log(hz / 8.176, 2)
end
local cached: any = nil
-- build() -> a SoundFont value (kind "soundfont"); built once, shared (the synth never writes to it)
function Fallback.build(): any
if cached then
return cached
end
local waves = { "sine", "triangle", "square", "saw" }
local count = #waves * CYCLE + NOISE
local samples = buffer.create((count + 1) * 4)
local starts: { [string]: number } = {}
for w, name in waves do
local base = (w - 1) * CYCLE
starts[name] = base
for i = 0, CYCLE - 1 do
local ph = i / CYCLE
local v
if name == "sine" then
v = math.sin(2 * math.pi * ph)
elseif name == "triangle" then
v = if ph < 0.25 then 4 * ph elseif ph < 0.75 then 2 - 4 * ph else 4 * ph - 4
elseif name == "square" then
v = if ph < 0.5 then 0.6 else -0.6
else
v = (1 - 2 * ph) * 0.7
end
buffer.writef32(samples, (base + i) * 4, v)
end
end
local noiseStart = #waves * CYCLE
starts.noise = noiseStart
local seed = 12345
for i = 0, NOISE - 1 do
seed = (seed * 1103515245 + 12345) % 2147483648
buffer.writef32(samples, (noiseStart + i) * 4, (seed / 1073741824 - 1) * 0.7)
end
local function region(wave: string, fields: { [string]: number }): { [string]: number }
local r = Parse.defaultRegion()
local s = starts[wave]
r.offset = s
r.loopMode = 1
r.loopStart = s
if wave == "noise" then
r.stop, r.loopEnd, r.sampleRate, r.keycenter = s + NOISE, s + NOISE - 1, 44100, 60
else
r.stop, r.loopEnd, r.sampleRate, r.keycenter = s + CYCLE, s + CYCLE - 1, CYCLE * C4, 60
end
r.aRelease = 0.08
for k, v in fields do
r[k] = v
end
return r
end
-- the 16 GM families: wave + envelope / filter / vibrato
local families: { { any } } = {
{ "piano", "triangle", { aDecay = 2.5, aSustain = 0, aRelease = 0.25 } },
{ "chromatic", "sine", { aDecay = 1.2, aSustain = 0, aRelease = 0.3 } },
{ "organ", "square", { aAttack = 0.005, aRelease = 0.05, filterFc = hzToCents(5000) } },
{ "guitar", "saw", { aDecay = 1.5, aSustain = 0, aRelease = 0.15, filterFc = hzToCents(3000) } },
{ "bass", "triangle", { aDecay = 1.5, aSustain = 0.4, aRelease = 0.1 } },
{ "strings", "saw", { aAttack = 0.08, aRelease = 0.3, filterFc = hzToCents(4000), freqVibLFO = hzToCents(5.5), vibLfoToPitch = 8, delayVibLFO = 0.2 } },
{ "ensemble", "saw", { aAttack = 0.12, aRelease = 0.4, filterFc = hzToCents(3500), freqVibLFO = hzToCents(5), vibLfoToPitch = 10 } },
{ "brass", "saw", { aAttack = 0.03, aRelease = 0.12, filterFc = hzToCents(1200), mAttack = 0.05, mDecay = 0.3, mSustain = 0.4, modEnvToFilterFc = 2400 } },
{ "reed", "square", { aAttack = 0.02, aRelease = 0.08, filterFc = hzToCents(2500) } },
{ "pipe", "sine", { aAttack = 0.04, aRelease = 0.12, freqVibLFO = hzToCents(5), vibLfoToPitch = 12, delayVibLFO = 0.3 } },
{ "lead", "square", { aAttack = 0.005, aRelease = 0.1, filterFc = hzToCents(6000) } },
{ "pad", "saw", { aAttack = 0.4, aRelease = 0.8, filterFc = hzToCents(1800), freqModLFO = hzToCents(0.5), modLfoToFilterFc = 600 } },
{ "synthfx", "triangle", { aAttack = 0.2, aRelease = 1.0, freqModLFO = hzToCents(3), modLfoToVolume = 60 } },
{ "ethnic", "saw", { aDecay = 1.0, aSustain = 0, aRelease = 0.2, filterFc = hzToCents(3000) } },
{ "percussive", "sine", { aDecay = 0.4, aSustain = 0, aRelease = 0.1, mDecay = 0.1, mSustain = 0, modEnvToPitch = 1200 } },
{ "sfx", "noise", { aAttack = 0.05, aRelease = 0.5, filterFc = hzToCents(3000) } },
}
local presets = {}
for program = 0, 127 do
local f = families[program // 8 + 1]
table.insert(presets, { name = "fallback " .. f[1], preset = program, bank = 0, regions = { region(f[2], f[3]) } })
end
-- the drum kit (bank 128, every program): one region per key
local kit = {}
-- fields.semis: the pitch (semitones from the sample's natural pitch) at keytrack 0
local function drum(lo: number, hi: number, wave: string, fields: { [string]: number })
local r = region(wave, fields)
r.lokey, r.hikey = lo, hi
r.sampleRate *= 2 ^ ((fields.semis or 0) / 12)
r.semis = nil :: any
table.insert(kit, r)
end
local sweep = { mDecay = 0.06, mSustain = 0, modEnvToPitch = 2400 }
local function with(a: { [string]: number }, b: { [string]: number }): { [string]: number }
local c = table.clone(a)
for k, v in b do
c[k] = v
end
return c
end
drum(0, 34, "noise", { aDecay = 0.1, aSustain = 0, keytrack = 0, filterFc = hzToCents(4000) })
drum(35, 36, "sine", with(sweep, { aDecay = 0.35, aSustain = 0, aRelease = 0.05, keytrack = 0, semis = -26 }))
drum(37, 37, "noise", { aDecay = 0.05, aSustain = 0, keytrack = 0, filterFc = hzToCents(5000) })
drum(38, 38, "noise", { aDecay = 0.2, aSustain = 0, keytrack = 0, filterFc = hzToCents(6000) })
drum(39, 39, "noise", { aDecay = 0.15, aSustain = 0, keytrack = 0, filterFc = hzToCents(8000) })
drum(40, 40, "noise", { aDecay = 0.18, aSustain = 0, keytrack = 0, filterFc = hzToCents(7000) })
for _, k in { 41, 43, 45, 47, 48, 50 } do
drum(k, k, "sine", with(sweep, { aDecay = 0.4, aSustain = 0, semis = (k - 41) - 12, keytrack = 0 }))
end
drum(42, 42, "noise", { aDecay = 0.05, aSustain = 0, group = 1, keytrack = 0, semis = 12 })
drum(44, 44, "noise", { aDecay = 0.08, aSustain = 0, group = 1, keytrack = 0, semis = 12 })
drum(46, 46, "noise", { aDecay = 0.45, aSustain = 0, group = 1, keytrack = 0, semis = 12 })
drum(49, 49, "noise", { aDecay = 1.5, aSustain = 0, keytrack = 0, semis = 7 })
drum(51, 51, "noise", { aDecay = 1.0, aSustain = 0, keytrack = 0, semis = 10, filterFc = hzToCents(9000) })
drum(52, 59, "noise", { aDecay = 1.2, aSustain = 0, keytrack = 0, semis = 5 })
drum(60, 127, "noise", { aDecay = 0.12, aSustain = 0, keytrack = 30, filterFc = hzToCents(5000) })
table.insert(presets, { name = "fallback drums", preset = 0, bank = 128, regions = kit })
cached = {
kind = "soundfont",
info = { name = "formats-luau fallback" },
samples = samples,
sampleCount = count,
presets = presets,
sampleHeaders = {},
instruments = {},
meta = { source = { format = "fallback" } },
}
return cached
end
return Fallback