audio perf: Tracker mixer fast path for filtered mono voices

This commit is contained in:
4DBug committed 2026-11-01 19:33:29 -06:00
1 parent 0d05130526
commit 3905426eff
1 file changed
+55 -22
+55 -22
View File
@@ -3,7 +3,7 @@
--!optimize 2 --!optimize 2
-- codecs/audio/Tracker/Mixer: tracker voice mixing (loops, IT sustain loops, IT two-pole resonant filter, click ramps). -- codecs/audio/Tracker/Mixer: tracker voice mixing (loops, IT sustain loops, IT two-pole resonant filter, click ramps).
-- No third-party code. -- No third-party code.
local floor, ceil, min = math.floor, math.ceil, math.min local floor, ceil, min, max = math.floor, math.ceil, math.min, math.max
local readf32, writef32 = buffer.readf32, buffer.writef32 local readf32, writef32 = buffer.readf32, buffer.writef32
local Mixer = {} local Mixer = {}
@@ -88,8 +88,8 @@ local function mixVoice(v: any, out: buffer?, off: number, n: number, linear: bo
local fa, fb0, fb1 = v.fa, v.fb0, v.fb1 local fa, fb0, fb1 = v.fa, v.fb0, v.fb1
local y1l, y2l, y1r, y2r = v.y1l, v.y2l, v.y1r, v.y2r local y1l, y2l, y1r, y2r = v.y1l, v.y2l, v.y1r, v.y2r
local o = (off + i) * 8 local o = (off + i) * 8
-- the common voice (mono, interpolated, unfiltered) runs the general loop only while its volume ramps -- the common voice (mono, interpolated) runs the general loop only while its volume ramps
local fast = nch == 1 and linear and not fOn local fast = nch == 1 and linear
local mg = if fast then (if rl > 0 then min(rl, m) else 0) else m local mg = if fast then (if rl > 0 then min(rl, m) else 0) else m
for _ = 1, mg do for _ = 1, mg do
local idx = floor(p) local idx = floor(p)
@@ -152,26 +152,59 @@ local function mixVoice(v: any, out: buffer?, off: number, n: number, linear: bo
o += 8 o += 8
p += step p += step
end end
if mg < m then -- the general loop, specialised to the fast voice after its ramp -- while p < e - 1 (forward), idx = floor(p) is in range and idx + 1 < e needs no wrap: those steps skip the
local k0 = mg + 1 -- checks; two steps of margin for the rounding of the running p
if step >= 0.001 and p >= 0 and s >= 0 and e <= len then local k0 = mg + 1
-- while p < e - 1, idx = floor(p) is in range and idx + 1 < e needs no wrap; two steps of margin local safe = mg
-- for the rounding of the running p if mg < m and step >= 0.001 and p >= 0 and s >= 0 and e <= len then
local safe = min(m, mg + floor((e - 1 - p) / step) - 2) safe = min(m, mg + floor((e - 1 - p) / step) - 2)
for _ = k0, safe do end
local idx = floor(p) if mg < m and fOn then -- the general loop, specialised to a filtered mono voice after its ramp
local a = readf32(data, idx * 4) for _ = k0, safe do
local x = a + (readf32(data, idx * 4 + 4) - a) * (p - idx) local idx = floor(p)
writef32(out, o, readf32(out, o) + x * gl) local a = readf32(data, idx * 4)
writef32(out, o + 4, readf32(out, o + 4) + x * gr) local x = a + (readf32(data, idx * 4 + 4) - a) * (p - idx)
o += 8 local y = x * fa + y1l * fb0 + y2l * fb1
p += step y2l, y1l = y1l, y
end writef32(out, o, readf32(out, o) + y * gl)
if safe >= k0 then writef32(out, o + 4, readf32(out, o + 4) + y * gr)
k0 = safe + 1 o += 8
end p += step
end end
for _ = k0, m do for _ = max(k0, safe + 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 a = readf32(data, idx * 4)
local x = a + (readf32(data, j * 4) - a) * (p - idx)
local y = x * fa + y1l * fb0 + y2l * fb1
y2l, y1l = y1l, y
writef32(out, o, readf32(out, o) + y * gl)
writef32(out, o + 4, readf32(out, o + 4) + y * gr)
o += 8
p += step
end
elseif mg < m then -- the same, unfiltered
for _ = k0, safe do
local idx = floor(p)
local a = readf32(data, idx * 4)
local x = a + (readf32(data, idx * 4 + 4) - a) * (p - idx)
writef32(out, o, readf32(out, o) + x * gl)
writef32(out, o + 4, readf32(out, o + 4) + x * gr)
o += 8
p += step
end
for _ = max(k0, safe + 1), m do
local idx = floor(p) local idx = floor(p)
if idx > hi then if idx > hi then
idx = hi idx = hi