Files
rosetta/src/Formats/codecs/SoundFont/init.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

70 lines
3.0 KiB
Luau

--!strict
-- codecs/SoundFont: SoundFont 2 banks and the sample synth that plays MIDI through them. A codec (layer 1) shared by
-- music/sf2 (the file format) and music/midi (rendering). The synth is a port of TinySoundFont (tsf.h v0.9, MIT,
-- (c) 2017-2025 Bernhard Schelling; licenses/TinySoundFont-LICENSE.txt); see Parse / Synth for what is covered.
-- Without a bank, fallback() is a generated General MIDI set of simple waveforms (sine / triangle / square / saw /
-- noise) with a drum kit.
--
-- local SoundFont = require("@formats/codecs/SoundFont")
-- local sf, err = SoundFont.parse(buf) -- buffer | string | core/Bytes Reader -> SoundFont | nil, Err
-- local sf = SoundFont.fallback() -- the built-in waveform set
-- local s = SoundFont.synth(sf, 44100, { gain = 0.5 }) -- a synth State; SoundFont.Synth.midi(s, 0x90, 60, 100)
-- SoundFont.Synth.render(s, out, frame, n) -- mix n stereo f32 frames into out at frame
--
-- Never throws: bad data returns nil, Err.
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Parse = require("@formats/codecs/SoundFont/Parse")
local Synth = require("@formats/codecs/SoundFont/Synth")
local Fallback = require("@formats/codecs/SoundFont/Fallback")
local SoundFont = {}
export type SoundFont = Parse.SoundFont
export type State = Synth.State
SoundFont.Synth = Synth
SoundFont.defaultRegion = Parse.defaultRegion
-- sniff(r) -> true when r starts with RIFF....sfbk
function SoundFont.sniff(r: Bytes.Reader): boolean
return r:peek(4) == "RIFF" and r:peek(4, 8) == "sfbk"
end
-- parse(src, opts?) -> SoundFont | nil, Err. src: buffer | string | Reader. opts.maxRegions (default 10^6),
-- opts.maxSamples (default 2^28)
function SoundFont.parse(src: any, opts: Parse.Options?): (SoundFont?, Err.Err?)
local r: Bytes.Reader
if type(src) == "string" then
r = Bytes.reader(buffer.fromstring(src))
elseif type(src) == "buffer" then
r = Bytes.reader(src)
elseif type(src) == "table" and type(src.buf) == "buffer" then
r = Bytes.reader(src.buf, src.pos, src.limit - src.pos)
else
return nil, Err.new(Err.ARG, "soundfont: expected a buffer, string or Reader")
end
local ok, sf, err = pcall(Parse.parse, r, opts)
if not ok then
return nil, Err.new(Err.CORRUPT, "soundfont parser failed: " .. tostring(sf))
end
return sf, err
end
-- fallback() -> the built-in General MIDI waveform set
function SoundFont.fallback(): SoundFont
return Fallback.build()
end
-- isSoundFont(v) -> true for a parsed / fallback SoundFont value
function SoundFont.isSoundFont(v: any): boolean
return type(v) == "table" and v.kind == "soundfont" and type(v.samples) == "buffer" and type(v.presets) == "table"
end
-- synth(sf?, sampleRate?, opts?) -> State. opts.gain (linear, default 0.5), opts.maxVoices (default 256)
function SoundFont.synth(sf: SoundFont?, sampleRate: number?, opts: { gain: number?, maxVoices: number? }?): State
return Synth.new(sf or Fallback.build(), sampleRate or 44100, opts and opts.gain, opts and opts.maxVoices)
end
return SoundFont