Files
rosetta/src/Formats/codecs/Tracker/Mixer.luau
T

174 lines
4.0 KiB
Luau

--!strict
--!native
-- codecs/Tracker/Mixer: mixes the player's active voices into an interleaved stereo f32 buffer. Per voice: sample
-- loops (forward, ping-pong, IT sustain loops while the key is held), linear or no interpolation, the IT two-pole
-- resonant filter, and a short gain ramp per tick against clicks. Written for this library (no third-party code).
--
-- Never throws: sample indexes are clamped to the sample, every loop is bounded, and the caller (codecs/Tracker)
-- validates the sample buffers' sizes.
local floor, ceil, min = math.floor, math.ceil, math.min
local readf32, writef32 = buffer.readf32, buffer.writef32
local Mixer = {}
-- mixVoice(v, out?, off, n, linear, ramp): n frames of voice v added at frame off of out (nil: only advance)
local function mixVoice(v: any, out: buffer?, off: number, n: number, linear: boolean, ramp: number)
local data: buffer = v.data
local nch = v.nch
local inc = v.inc
if not (inc > 0 and inc < 1e6) then
return
end
local len = v.len
local hi = len - 1
local i = 0
local guard = 0
while i < n do
guard += 1
if guard > n + 64 then
return
end
local s, e, t
if v.keyOn and v.susT > 0 then
s, e, t = v.susS, v.susE, v.susT
elseif v.loopT > 0 then
s, e, t = v.loopS, v.loopE, v.loopT
else
s, e, t = 0, len, 0
end
local p = v.p
local dir = v.dir
local count
if dir > 0 then
local lim = if t == 2 then e - 1 else e
if p >= lim then
if t == 0 then
v.active = false
return
elseif t == 1 then
p = s + (p - e) % (e - s)
else
p = 2 * (e - 1) - p
if p < s then
p = s
end
v.dir = -1
end
v.p = p
continue
end
count = ceil((lim - p) / inc)
else
if p < s then
p = 2 * s - p
if p > e - 1 then
p = e - 1
end
v.p = p
v.dir = 1
continue
end
count = floor((p - s) / inc) + 1
end
if count < 1 then
count = 1
end
local m = min(n - i, count)
local step = inc * dir
if out == nil then
v.p = p + step * m
i += m
continue
end
local wrapJ = if t == 1 then s else e - 1
local gl, gr = v.gl, v.gr
local rl = v.rampN
local dl, dr = 0, 0
local tl, tr = v.tl, v.tr
if rl > 0 then
dl, dr = (tl - gl) / rl, (tr - gr) / rl
end
local fOn = v.fOn
local fa, fb0, fb1 = v.fa, v.fb0, v.fb1
local y1l, y2l, y1r, y2r = v.y1l, v.y2l, v.y1r, v.y2r
local o = (off + i) * 8
for _ = 1, m do
local idx = floor(p)
if idx > hi then
idx = hi
elseif idx < 0 then
idx = 0
end
local j = idx + 1
if j >= e then
j = wrapJ
end
if j > hi then
j = hi
end
local xl, xr
if nch == 1 then
local a = readf32(data, idx * 4)
if linear then
local b = readf32(data, j * 4)
xl = a + (b - a) * (p - idx)
else
xl = a
end
if fOn then
local y = xl * fa + y1l * fb0 + y2l * fb1
y2l, y1l = y1l, y
xl = y
end
xr = xl
else
local al = readf32(data, idx * 8)
local ar = readf32(data, idx * 8 + 4)
if linear then
local f = p - idx
xl = al + (readf32(data, j * 8) - al) * f
xr = ar + (readf32(data, j * 8 + 4) - ar) * f
else
xl, xr = al, ar
end
if fOn then
local y = xl * fa + y1l * fb0 + y2l * fb1
y2l, y1l = y1l, y
xl = y
y = xr * fa + y1r * fb0 + y2r * fb1
y2r, y1r = y1r, y
xr = y
end
end
if rl > 0 then
gl += dl
gr += dr
rl -= 1
if rl == 0 then
gl, gr = tl, tr
end
end
writef32(out, o, readf32(out, o) + xl * gl)
writef32(out, o + 4, readf32(out, o + 4) + xr * gr)
o += 8
p += step
end
v.p = p
v.gl, v.gr, v.rampN = gl, gr, rl
v.y1l, v.y2l, v.y1r, v.y2r = y1l, y2l, y1r, y2r
i += m
end
local _ = ramp
end
-- mix(st, out?, off, n, linear): every active voice of player st; out nil only advances the sample positions
function Mixer.mix(st: any, out: buffer?, off: number, n: number, linear: boolean)
for _, v in st.voices do
if v.active and v.data then
mixVoice(v, out, off, n, linear, 0)
end
end
end
return Mixer