210 lines
6.2 KiB
Luau
210 lines
6.2 KiB
Luau
-- tests/music/_similar: shared helper of the tracker tests (not a test itself): renders a module through a Decoder and
|
|
-- compares it with a reference render's features (tools/fixtures/_tracker.py: per 2048-frame block the RMS of left,
|
|
-- right and mid, and 40 log-spaced band magnitudes of mid from a Hann-windowed FFT). Same definitions on both sides.
|
|
local S = {}
|
|
|
|
local SR, HOP, BANDS = 44100, 2048, 40
|
|
local F_LO, F_HI = 150, 16000
|
|
S.SR = SR
|
|
|
|
local WINDOW = table.create(HOP, 0)
|
|
for i = 0, HOP - 1 do
|
|
WINDOW[i + 1] = 0.5 - 0.5 * math.cos(2 * math.pi * i / HOP)
|
|
end
|
|
local BAND_OF = table.create(HOP // 2, -1)
|
|
for b = 1, HOP // 2 - 1 do
|
|
local f = b * SR / HOP
|
|
if f >= F_LO and f < F_HI then
|
|
BAND_OF[b + 1] = math.min(BANDS - 1, math.floor(math.log(f / F_LO) / math.log(F_HI / F_LO) * BANDS))
|
|
end
|
|
end
|
|
|
|
local function fft(re: { number }, im: { number }, n: number)
|
|
local j = 0
|
|
for i = 1, n - 1 do
|
|
local bit = n // 2
|
|
while bit > 0 and j >= bit and (j // bit) % 2 == 1 do
|
|
j -= bit
|
|
bit //= 2
|
|
end
|
|
j += bit
|
|
if i < j then
|
|
re[i + 1], re[j + 1] = re[j + 1], re[i + 1]
|
|
im[i + 1], im[j + 1] = im[j + 1], im[i + 1]
|
|
end
|
|
end
|
|
local size = 2
|
|
while size <= n do
|
|
local ang = -2 * math.pi / size
|
|
local wr, wi = math.cos(ang), math.sin(ang)
|
|
local half = size // 2
|
|
for start = 0, n - 1, size do
|
|
local cr, ci = 1, 0
|
|
for k = 0, half - 1 do
|
|
local a, b = start + k + 1, start + k + half + 1
|
|
local tr = re[b] * cr - im[b] * ci
|
|
local ti = re[b] * ci + im[b] * cr
|
|
re[b], im[b] = re[a] - tr, im[a] - ti
|
|
re[a], im[a] = re[a] + tr, im[a] + ti
|
|
cr, ci = cr * wr - ci * wi, cr * wi + ci * wr
|
|
end
|
|
end
|
|
size *= 2
|
|
end
|
|
end
|
|
|
|
-- features(samples (interleaved stereo f32 buffer), frames) -> { frames, rmsL, rmsR, rmsM, bands }
|
|
function S.features(buf: buffer, frames: number): any
|
|
local n = frames // HOP
|
|
local out = { frames = n, rmsL = {}, rmsR = {}, rmsM = {}, bands = {} }
|
|
local re, im = table.create(HOP, 0), table.create(HOP, 0)
|
|
for k = 0, n - 1 do
|
|
local sl, sr, sm = 0, 0, 0
|
|
for i = 0, HOP - 1 do
|
|
local o = ((k * HOP + i) * 2) * 4
|
|
local l, r = buffer.readf32(buf, o), buffer.readf32(buf, o + 4)
|
|
local m = (l + r) / 2
|
|
sl += l * l
|
|
sr += r * r
|
|
sm += m * m
|
|
re[i + 1] = m * WINDOW[i + 1]
|
|
im[i + 1] = 0
|
|
end
|
|
table.insert(out.rmsL, math.sqrt(sl / HOP))
|
|
table.insert(out.rmsR, math.sqrt(sr / HOP))
|
|
table.insert(out.rmsM, math.sqrt(sm / HOP))
|
|
fft(re, im, HOP)
|
|
local e = table.create(BANDS, 0)
|
|
for b = 1, HOP // 2 - 1 do
|
|
local j = BAND_OF[b + 1]
|
|
if j >= 0 then
|
|
e[j + 1] += re[b + 1] * re[b + 1] + im[b + 1] * im[b + 1]
|
|
end
|
|
end
|
|
for j = 1, BANDS do
|
|
table.insert(out.bands, math.sqrt(e[j]))
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
-- renderAll(decoder, maxFrames) -> interleaved buffer, frames
|
|
function S.renderAll(dec: any, maxFrames: number): (buffer, number)
|
|
local total = math.min(dec.frames, maxFrames)
|
|
local out = buffer.create(math.max(1, total) * 8)
|
|
local pos = 0
|
|
local chunk = buffer.create(4096 * 8)
|
|
while pos < total do
|
|
local _, n = dec:read(math.min(4096, total - pos), chunk)
|
|
if n == 0 then
|
|
break
|
|
end
|
|
buffer.copy(out, pos * 8, chunk, 0, n * 8)
|
|
pos += n
|
|
end
|
|
return out, pos
|
|
end
|
|
|
|
local function db(x: number): number
|
|
return 20 * math.log10(x + 1e-6)
|
|
end
|
|
|
|
-- compare(ours, ref) -> { gainDb, envCorr, spectral, balance } over the common frames:
|
|
-- gainDb total mid energy ratio in dB (ours / ref)
|
|
-- envCorr Pearson correlation of the per-block mid loudness in dB (blocks above -60 dB in either)
|
|
-- envErr mean absolute difference of the per-block mid loudness in dB, after removing gainDb (same blocks)
|
|
-- spectral mean cosine similarity of the band magnitude vectors (blocks where ref is above -50 dB)
|
|
-- balance mean absolute difference of the per-block left / right balance (L - R) / (L + R) (loud blocks)
|
|
function S.compare(a: any, b: any): any
|
|
local n = math.min(a.frames, b.frames)
|
|
local ea, eb = 0, 0
|
|
for k = 1, n do
|
|
ea += a.rmsM[k] ^ 2
|
|
eb += b.rmsM[k] ^ 2
|
|
end
|
|
local gainDb = 10 * math.log10((ea + 1e-12) / (eb + 1e-12))
|
|
local xs, ys = {}, {}
|
|
for k = 1, n do
|
|
local x, y = db(a.rmsM[k]), db(b.rmsM[k])
|
|
if x > -60 or y > -60 then
|
|
table.insert(xs, x)
|
|
table.insert(ys, y)
|
|
end
|
|
end
|
|
local mx, my = 0, 0
|
|
for i = 1, #xs do
|
|
mx += xs[i]
|
|
my += ys[i]
|
|
end
|
|
mx /= math.max(1, #xs)
|
|
my /= math.max(1, #ys)
|
|
local sxy, sxx, syy = 0, 0, 0
|
|
for i = 1, #xs do
|
|
sxy += (xs[i] - mx) * (ys[i] - my)
|
|
sxx += (xs[i] - mx) ^ 2
|
|
syy += (ys[i] - my) ^ 2
|
|
end
|
|
local envCorr = if sxx > 0 and syy > 0 then sxy / math.sqrt(sxx * syy) else 1
|
|
-- the mean loudness difference per block once the overall gain is taken out (dB, each side clamped at -60)
|
|
local envErr = 0
|
|
for i = 1, #xs do
|
|
envErr += math.abs(math.max(xs[i], -60) - math.max(ys[i], -60) - gainDb)
|
|
end
|
|
envErr /= math.max(1, #xs)
|
|
local cs, cn = 0, 0
|
|
local bal, bn = 0, 0
|
|
for k = 1, n do
|
|
if db(b.rmsM[k]) > -50 then
|
|
local dot, na, nb = 0, 0, 0
|
|
for j = 1, BANDS do
|
|
local x, y = a.bands[(k - 1) * BANDS + j], b.bands[(k - 1) * BANDS + j]
|
|
dot += x * y
|
|
na += x * x
|
|
nb += y * y
|
|
end
|
|
cs += if na > 0 and nb > 0 then dot / math.sqrt(na * nb) else 0
|
|
cn += 1
|
|
end
|
|
local la, ra, lb, rb = a.rmsL[k], a.rmsR[k], b.rmsL[k], b.rmsR[k]
|
|
if lb + rb > 0.01 then
|
|
bal += math.abs((la - ra) / (la + ra + 1e-9) - (lb - rb) / (lb + rb))
|
|
bn += 1
|
|
end
|
|
end
|
|
return {
|
|
gainDb = gainDb,
|
|
envCorr = envCorr,
|
|
envErr = envErr,
|
|
spectral = if cn > 0 then cs / cn else 1,
|
|
balance = if bn > 0 then bal / bn else 0,
|
|
frames = n,
|
|
}
|
|
end
|
|
|
|
-- worst(ours, ref, count) -> the blocks with the lowest spectral similarity (for debugging): { {block, sim} }
|
|
function S.worst(a: any, b: any, count: number): { { number } }
|
|
local list = {}
|
|
for k = 1, math.min(a.frames, b.frames) do
|
|
if db(b.rmsM[k]) > -50 then
|
|
local dot, na, nb = 0, 0, 0
|
|
for j = 1, BANDS do
|
|
local x, y = a.bands[(k - 1) * BANDS + j], b.bands[(k - 1) * BANDS + j]
|
|
dot += x * y
|
|
na += x * x
|
|
nb += y * y
|
|
end
|
|
table.insert(list, { k, if na > 0 and nb > 0 then dot / math.sqrt(na * nb) else 0, db(a.rmsM[k]), db(b.rmsM[k]) })
|
|
end
|
|
end
|
|
table.sort(list, function(x, y)
|
|
return x[2] < y[2]
|
|
end)
|
|
local out = {}
|
|
for i = 1, math.min(count, #list) do
|
|
out[i] = list[i]
|
|
end
|
|
return out
|
|
end
|
|
|
|
return S
|