ws-music: codecs/Tracker engine (player, mixer, pcm, tables) + music/mod read/write/render, tested against openmpt123
This commit is contained in:
1 parent
ccb06775c5
commit
4c1f5a9987
12 files changed
+4020
No files matched your search
@@ -0,0 +1,201 @@
|
||||
-- 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 20 log-spaced band magnitudes of mid from a Hann-windowed FFT). Same definitions on both sides.
|
||||
local S = {}
|
||||
|
||||
local SR, HOP, BANDS = 44100, 2048, 20
|
||||
local F_LO, F_HI = 100, 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)
|
||||
-- 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
|
||||
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,
|
||||
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
|
||||
@@ -0,0 +1,77 @@
|
||||
-- music/mod tests: generated ProTracker modules (tools/fixtures/mod.py) parsed, rendered through codecs/Tracker and
|
||||
-- compared with openmpt123's render (loudness envelope, band spectrum, stereo balance, song length), the MOD writer's
|
||||
-- round trip, the contract, and the fuzz no-throw pass.
|
||||
local T = require("@tests/T")
|
||||
local S = require("@tests/music/_similar")
|
||||
local Mod = require("@formats/music/mod")
|
||||
local Tracker = require("@formats/codecs/Tracker")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Cases = require("@tests/gen/mod_cases")
|
||||
|
||||
local t = T.suite("mod")
|
||||
|
||||
t:eq(Mod.name, "mod", "name")
|
||||
t:eq(Mod.category, "music", "category")
|
||||
t:check(Mod.caps.decode and Mod.caps.encode, "caps")
|
||||
|
||||
local function load(data: string): any
|
||||
return Mod.decode(Bytes.reader(buffer.fromstring(data)))
|
||||
end
|
||||
|
||||
local THRESH = { gainDb = 1.5, envCorr = 0.95, spectral = 0.93, balance = 0.05, duration = 0.01 }
|
||||
|
||||
for _, c in Cases do
|
||||
local m, err = load(c.data)
|
||||
t:check(m, c.name .. ": " .. tostring(err))
|
||||
t:check(Mod.sniff(Bytes.reader(buffer.fromstring(c.data))) > 0, c.name .. ": sniff")
|
||||
local dec = m:render({ sampleRate = S.SR })
|
||||
t:check(dec, c.name .. ": render")
|
||||
t:check(math.abs(dec.duration - c.duration) < THRESH.duration, string.format("%s: duration %.3f s, openmpt %.3f s", c.name, dec.duration, c.duration))
|
||||
local buf, frames = S.renderAll(dec, math.floor(c.duration * S.SR))
|
||||
local r = S.compare(S.features(buf, frames), c.ref)
|
||||
t:note("%-6s gain %+.2f dB, envelope r %.3f, spectrum %.3f, balance %.3f, %.2f s", c.name, r.gainDb, r.envCorr, r.spectral, r.balance, dec.duration)
|
||||
if r.spectral < THRESH.spectral or r.envCorr < THRESH.envCorr then
|
||||
for _, w in S.worst(S.features(buf, frames), c.ref, 6) do
|
||||
t:note(" block %d (%.2f s): spectrum %.3f, ours %.1f dB, ref %.1f dB", w[1], (w[1] - 1) * 2048 / S.SR, w[2], w[3], w[4])
|
||||
end
|
||||
end
|
||||
t:check(math.abs(r.gainDb) < THRESH.gainDb, c.name .. ": loudness")
|
||||
t:check(r.envCorr > THRESH.envCorr, c.name .. ": loudness envelope")
|
||||
t:check(r.spectral > THRESH.spectral, c.name .. ": spectrum")
|
||||
t:check(r.balance < THRESH.balance, c.name .. ": stereo balance")
|
||||
-- the writer: MOD -> MOD gives the same bytes back (the fixtures are canonical ProTracker files)
|
||||
local out, werr = Mod.encode(m)
|
||||
t:check(out, c.name .. ": encode " .. tostring(werr))
|
||||
local m2 = Mod.decode(Bytes.reader(out :: buffer))
|
||||
t:check(m2, c.name .. ": re-decode")
|
||||
t:eq(buffer.tostring(out :: buffer), c.data, c.name .. ": round trip bytes")
|
||||
end
|
||||
|
||||
-- seek: the position after a seek and the output continuing
|
||||
do
|
||||
local m = load(Cases[1].data)
|
||||
local dec = m:render()
|
||||
dec:seek(2)
|
||||
t:check(math.abs(dec:position() - 2) < 0.001, "seek position")
|
||||
local _, n = dec:read(1000)
|
||||
t:eq(n, 1000, "read after seek")
|
||||
t:check(Tracker.duration(m) == dec.duration, "duration()")
|
||||
end
|
||||
|
||||
-- the fuzz pass: decode, and a short render of whatever decodes
|
||||
local samples = {}
|
||||
for _, c in Cases do
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
local m = Mod.decode(Bytes.reader(b))
|
||||
if m then
|
||||
local dec = Tracker.render(m, { maxDuration = 0.5, sampleRate = 8000 })
|
||||
if dec then
|
||||
dec:read(4000)
|
||||
end
|
||||
end
|
||||
return m
|
||||
end, samples, { mutations = 30 })
|
||||
|
||||
t:done()
|
||||
Reference in new issue
Block a user