audio perf: resample planar history + shared dot loop, mdct inverse fused unfold + fft index hoisting, AudioOps strided loops / remix term lists, wav sample loops, SILK LPC in locals, upvalue hoisting (Celt, Mpa), --!optimize 2 on every audio module; tools/bench_audio.luau

This commit is contained in:
4DBug committed 2026-11-01 19:00:38 -06:00
1 parent 01c044805f
commit 051aca5b74
98 files changed
+707 -171

No files matched your search

+70 -34
View File
@@ -105,7 +105,7 @@ type State = {
f: Filter,
step: number, -- M = from / g: the input advance per output frame, in units of 1 / L
L: number, -- to / g
hist: { number },
hist: { { number } }, -- per channel: input frame base + i - 1 at [i]
histFrames: number,
base: number,
inFrames: number,
@@ -115,6 +115,16 @@ type State = {
flush: any,
}
-- dot(x, xi, h, hi, n): sum of x[xi + k] * h[hi + k] for k = 0 .. n - 1, in that order
local function dot(x: { number }, xi: number, h: { number }, hi: number, n: number): number
local acc = 0
local d = xi - hi
for k = hi, hi + n - 1 do
acc += x[k + d] * h[k]
end
return acc
end
local function run(st: State, limitOut: number, out: buffer?): (buffer, number)
local f = st.f
local taps, half, h = f.taps, f.half, f.h
@@ -140,61 +150,78 @@ local function run(st: State, limitOut: number, out: buffer?): (buffer, number)
local b = if out and buffer.len(out) >= bytes then out else buffer.create(bytes)
local exact = f.exact
local phases = f.phases
local x0, x1 = hist[1], hist[2]
local o = 0
for j = j0, jmax - 1 do
local num = j * M
local ip = num // L -- floor(t)
local first = ip - half + 1
local rel = first - base
local num = j0 * M
local ip = num // L -- floor(t)
local ph = num - ip * L -- num mod L
for _ = j0, jmax - 1 do
local rel = ip - half + 1 - base
local kLo = if rel < 0 then -rel else 0
local kHi = taps - 1
if rel + kHi >= histFrames then
kHi = histFrames - 1 - rel
end
local cnt = kHi - kLo + 1
local xi = rel + kLo + 1
if exact then
local hb = (num - ip * L) * taps + 1 -- phase * taps + 1
for c = 0, ch - 1 do
local acc = 0
local hi = (rel + kLo) * ch + c + 1
for k = kLo, kHi do
acc += hist[hi] * h[hb + k]
hi += ch
local hb = ph * taps + kLo + 1
if ch == 2 then
local a0, a1 = 0, 0
local d = xi - hb
for k = hb, hb + cnt - 1 do
local hk = h[k]
a0 += x0[k + d] * hk
a1 += x1[k + d] * hk
end
writef32(b, o, acc)
writef32(b, o, a0)
writef32(b, o + 4, a1)
o += 8
elseif ch == 1 then
writef32(b, o, dot(x0, xi, h, hb, cnt))
o += 4
else
for c = 1, ch do
writef32(b, o, dot(hist[c], xi, h, hb, cnt))
o += 4
end
end
else
local fp = (num - ip * L) / L * phases
local fp = ph / L * phases
local p = floor(fp)
local a = fp - p
local hb = p * taps + 1
local hb = p * taps + kLo + 1
local hb2 = hb + taps
for c = 0, ch - 1 do
local acc, acc2 = 0, 0
local hi = (rel + kLo) * ch + c + 1
for k = kLo, kHi do
local x = hist[hi]
acc += x * h[hb + k]
acc2 += x * h[hb2 + k]
hi += ch
end
for c = 1, ch do
local acc = dot(hist[c], xi, h, hb, cnt)
local acc2 = dot(hist[c], xi, h, hb2, cnt)
writef32(b, o, acc + (acc2 - acc) * a)
o += 4
end
end
ph += M
if ph >= L then
local q = ph // L
ip += q
ph -= q * L
end
end
st.outPos = jmax
local keepFrom = (jmax * M) // L - half + 1
local drop = keepFrom - base
if drop > 0 then
if drop >= histFrames then
st.hist = {}
for c = 1, ch do
hist[c] = {}
end
st.histFrames = 0
st.base = base + histFrames
else
local nh: { number } = table.create((histFrames - drop) * ch) :: any
table.move(hist, drop * ch + 1, histFrames * ch, 1, nh)
st.hist = nh
for c = 1, ch do
local nh: { number } = table.create(histFrames - drop) :: any
table.move(hist[c], drop + 1, histFrames, 1, nh)
hist[c] = nh
end
st.histFrames = histFrames - drop
st.base = base + drop
end
@@ -208,10 +235,15 @@ local function process(self: State, src: buffer, frames: number, out: buffer?):
end
local ch = self.channels
frames = min(floor(frames), buffer.len(src) // (ch * 4))
local hist = self.hist
local p = self.histFrames * ch
for i = 0, frames * ch - 1 do
hist[p + i + 1] = readf32(src, i * 4)
local p = self.histFrames
for c = 1, ch do
local x = self.hist[c]
local o = (c - 1) * 4
local step = ch * 4
for i = p + 1, p + frames do
x[i] = readf32(src, o)
o += step
end
end
self.histFrames += frames
self.inFrames += frames
@@ -250,6 +282,10 @@ function Resample.new(from: number, to: number, channels: number, opts: Options?
local L: number, M: number = ti // g, fi // g
local exact = L <= MAX_EXACT
local phases = if exact then L else INTERP_PHASES
local hist = table.create(channels)
for c = 1, channels do
hist[c] = {}
end
local st: State = {
from = from,
to = to,
@@ -258,7 +294,7 @@ function Resample.new(from: number, to: number, channels: number, opts: Options?
f = if L == 1 and M == 1 then { taps = 2, half = 1, phases = 1, exact = true, h = { 1, 0 } } else makeFilter(to / from, phases, exact, q[1], q[2], cut),
step = M,
L = L,
hist = {},
hist = hist,
histFrames = 0,
base = 0,
inFrames = 0,