codecs/audio: comments to specs only

This commit is contained in:
4DBug committed 2026-11-01 01:55:39 -05:00
1 parent 0bd7b87d2b
commit 6dcce481bb
69 files changed
+523 -1604

No files matched your search

+11 -35
View File
@@ -1,16 +1,8 @@
--!strict
--!native
-- codecs/audio/SoundFont/Synth: a SoundFont 2 sample synthesizer with MIDI channels. A port of TinySoundFont (tsf.h v0.9,
-- MIT, (c) 2017-2025 Bernhard Schelling, based on SFZero (c) 2012 Steve Folta; licenses/TinySoundFont-LICENSE.txt):
-- voices with the DAHDSR volume envelope (exponential decay / release), the modulation envelope to pitch and filter
-- cutoff, the modulation and vibrato LFOs (triangle) to pitch / filter / volume, a resonant biquad low-pass, linear
-- interpolation, loop modes (none / continuous / until release), 3 dB pan law, exclusive classes; channels with
-- 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: 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).
-- codecs/audio/SoundFont/Synth: a SoundFont 2 sample synthesizer with MIDI channels.
-- A port of TinySoundFont (tsf.h v0.9, MIT, (c) 2017-2025 Bernhard Schelling, based on SFZero (c) 2012 Steve Folta;
-- licenses/TinySoundFont-LICENSE.txt). Unlike tsf.h, the envelope gain is ramped across each 64-sample block.
local Synth = {}
local readf32, writef32, blen = buffer.readf32, buffer.writef32, buffer.len
@@ -39,12 +31,12 @@ type Env = {
}
type Lfo = { untilStart: number, level: number, delta: number }
export type Voice = {
preset: number, -- 1-based preset index, -1 = idle
preset: number,
key: number,
channel: number,
held: boolean, -- note off while sustain was down
held: boolean,
region: { [string]: number },
pitchIn: number, -- timecents
pitchIn: number,
pitchOut: number,
pos: number,
gainDB: number,
@@ -83,14 +75,14 @@ export type Channel = {
}
export type State = {
presets: { { regions: { { [string]: number } }, preset: number, bank: number } },
lookup: { [number]: number }, -- bank * 128 + preset -> preset index
lookup: { [number]: number },
samples: buffer,
rate: number,
gainDB: number,
voices: { Voice },
maxVoices: number,
playIndex: number,
channels: { Channel }, -- [1..16] = MIDI channels 0..15
channels: { Channel },
}
local function tc2s(tc: number): number
@@ -106,7 +98,6 @@ local function gain2db(g: number): number
return if g <= 0.00001 then -100 else 20 * math.log10(g)
end
-- ------------------------------------------------------------------------------------------------ envelope
local function releaseSamples(e: Env, rate: number): number
return floor((if e.release <= 0 then FASTRELEASE else e.release) * rate)
end
@@ -216,7 +207,6 @@ local function newEnv(): Env
return { segment = DONE, isExp = false, isAmp = false, vel = 0, level = 0, slope = 0, untilNext = 0, delay = 0, attack = 0, hold = 0, decay = 0, sustain = 0, release = 0 }
end
-- ------------------------------------------------------------------------------------------------ filter / lfo
local function lowpassSetup(v: Voice, fc: number)
-- the biquad from earlevel.com (as tsf.h)
local k = tan(PI * fc)
@@ -249,7 +239,6 @@ local function lfoProcess(l: Lfo, n: number)
end
end
-- ------------------------------------------------------------------------------------------------ voices
local function calcPitch(v: Voice, shift: number, rate: number)
local r = v.region
local note = v.key + r.transpose + r.tune / 100
@@ -276,7 +265,7 @@ local function voiceEnd(s: State, v: Voice)
nextSegment(v.amp, SUSTAIN, s.rate)
nextSegment(v.mod, SUSTAIN, s.rate)
if v.region.loopMode == LOOP_SUSTAIN then
v.loopEnd = v.loopStart -- keep playing, stop looping
v.loopEnd = v.loopStart
end
end
@@ -303,8 +292,6 @@ local function newChannel(): Channel
}
end
-- new(sf, sampleRate, opts) -> State. sf: { presets, samples } (Parse.parse or Fallback.build).
-- opts.gain: output gain (linear, default 0.5); opts.maxVoices (default 256)
function Synth.new(sf: any, rate: number, gain: number?, maxVoices: number?): State
local lookup: { [number]: number } = {}
for i, p in sf.presets do
@@ -337,7 +324,6 @@ local function presetIndex(s: State, bank: number, preset: number): number
return s.lookup[bank * 128 + preset] or -1
end
-- noteOn(s, channel, key, velocity 0..127)
function Synth.noteOn(s: State, ch: number, key: number, velocity: number)
local c = s.channels[ch + 1]
if not c then
@@ -427,7 +413,6 @@ function Synth.noteOn(s: State, ch: number, key: number, velocity: number)
end
end
-- noteOff(s, channel, key): ends the oldest matching note (all its regions); held while sustain is down
function Synth.noteOff(s: State, ch: number, key: number)
local c = s.channels[ch + 1]
if not c then
@@ -518,7 +503,7 @@ local function setSustain(s: State, ch: number, c: Channel, on: boolean)
end
end
-- program(s, channel, program): bank select + program change (channel 9: drums, bank 128)
-- channel 9: drums (bank 128)
function Synth.program(s: State, ch: number, program: number)
local c = s.channels[ch + 1]
if not c then
@@ -548,7 +533,6 @@ function Synth.program(s: State, ch: number, program: number)
end
end
-- pitchBend(s, channel, 0..16383)
function Synth.pitchBend(s: State, ch: number, value: number)
local c = s.channels[ch + 1]
if not c or c.pitchWheel == value then
@@ -558,7 +542,6 @@ function Synth.pitchBend(s: State, ch: number, value: number)
applyPitch(s, ch, c)
end
-- control(s, channel, controller, value 0..127)
function Synth.control(s: State, ch: number, cc: number, value: number)
local c = s.channels[ch + 1]
if not c then
@@ -640,7 +623,6 @@ function Synth.control(s: State, ch: number, cc: number, value: number)
end
end
-- midi(s, status, d1, d2): one channel message
function Synth.midi(s: State, status: number, d1: number, d2: number)
local kind, ch = status // 16, status % 16
if kind == 9 then
@@ -656,7 +638,6 @@ function Synth.midi(s: State, status: number, d1: number, d2: number)
end
end
-- reset(s): all voices off, channels back to their defaults
function Synth.reset(s: State)
for _, v in s.voices do
v.preset = -1
@@ -669,7 +650,6 @@ function Synth.reset(s: State)
end
end
-- active(s) -> the number of sounding voices
function Synth.active(s: State): number
local n = 0
for _, v in s.voices do
@@ -680,7 +660,6 @@ function Synth.active(s: State): number
return n
end
-- ------------------------------------------------------------------------------------------------ render
local function voiceRender(s: State, v: Voice, out: buffer, frame: number, numSamples: number)
local region = v.region
local input = s.samples
@@ -772,8 +751,6 @@ local function voiceRender(s: State, v: Voice, out: buffer, frame: number, numSa
v.lpActive, v.z1, v.z2 = lpActive, z1, z2
end
-- render(s, out, frame, n): mixes n stereo frames into out (interleaved f32) starting at frame; out-of-range
-- requests are clipped to the buffer
function Synth.render(s: State, out: buffer, frame: number, n: number)
local room = blen(out) // 8 - frame
if frame < 0 or room <= 0 then
@@ -785,8 +762,7 @@ function Synth.render(s: State, out: buffer, frame: number, n: number)
if n <= 0 then
return
end
-- the sample buffer must hold every region's range + the pad sample (Parse / Fallback guarantee it; checked here
-- once so a hand-made SoundFont cannot index outside it)
-- the sample buffer must hold every region's range + the pad sample (checked once against hand-made SoundFonts)
local count = blen(s.samples) // 4 - 1
for _, v in s.voices do
if v.preset ~= -1 then