C2, the hierarchy (tools/move.py, every reference rewritten mechanically): - codecs/compress: Deflate Lzma Zstd Brotli Bzip2 Lzw; codecs/entropy: Huffman (from core) RangeCoder; codecs/image: Jpeg Dct Vp8 Vp8L Yuv Quantize Raster Blend Pixels; codecs/audio: Mdct Aac Alac Pcm Resample VorbisComment Tracker SoundFont Stream; codecs/video: H264 Mpeg1 Video; codecs/text: Json Xml Text Url; codecs/font: TrueType Cff; codecs/model: MeshFlatten Mat4. containers/ stays flat; core/ keeps Bytes Bits Err Crc32 Adler32 Base64 Md5 Input Sniff. - LAYERS lint: codecs must sit in a domain folder; gen_registry lists codecs by domain (docs/FORMATS.md). - Checked: build.py both targets, rojo build of build/roblox, the full suite on both runtimes. C3, generic code out of format folders: - containers/Chunks (new): the chunked-file walker (RIFF / RIFX / IFF / PNG / CAF layouts: endianness, size first, trailer, padding, lenient missing pad, open-ended ids, size hooks). containers/Riff walks with it (RF64 ds64 through the resize hook); image/png, audio/aiff and audio/caf list their chunk handlers over it. - codecs/audio/Stream (new): Stream.packets, the packet-decoder Decoder (read / seek with pre-roll / position, priming and end trimming) that aac, caf and m4a each carried (~70 lines each). - codecs/image/Pixels: image/bmp/Pixels promoted (+ rgb555); image/ico's DIB decoder uses its row loops instead of its own (the AND mask / alpha rule stay ICO's). - codecs/model/Mat4 (new): matrices / quaternions / TRS shared by FBX (Xform keeps only the FBX transform model), Collada (Math keeps only <lookat> / <skew>) and USD (its matrix helpers and general 4x4 inverse); Mat4.mul is Types.Scene.mul. The Collada normal matrix is now the inverse transpose (it was the inverse; same result for the fixtures' bind shapes). - README rewritten (install for Rojo and LÖVE / LuaJIT, quick start per category, F.* and F.ops, host notes: FORMATS_NO_JIT_TUNE, paths, layout); docs/STYLE.md: the codec domains, "what belongs in a format folder" with the shared building blocks and the DUP rule, conventions (WAVE order, lower-case tags, loops, the api hooks decodeImage / encodeImage / openAudio / plugins / resolve, the codecs/video/Video registry), Err.from, the type baseline. New tests: tests/containers/chunks, tests/codecs/mat4. Type baseline 93.
150 lines
6.8 KiB
Luau
150 lines
6.8 KiB
Luau
--!strict
|
|
-- codecs/audio/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/audio/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
|