-- tests/music/_similar: shared helper of the tracker tests: a render's features against a reference render's -- (tools/fixtures/_tracker.py: per 2048-frame block the RMS of L / R / mid, 40 log bands of mid). Same definitions. 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 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 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) 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 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 -- the shared body of the tracker loader tests: durations within thresh.duration s of openmpt's render function S.against(t: any, Fmt: any, cases: { any }, thresh: any, family: string?): { any } local Bytes = require("@rosetta/core/Bytes") local Tracker = require("@rosetta/codecs/audio/Tracker") local mods = {} for _, c in cases do local m, err = Fmt.decode(Bytes.reader(buffer.fromstring(c.data))) t:check(m, c.name .. ": " .. tostring(err)) if not m then continue end table.insert(mods, m) t:eq(m.format, Fmt.name, c.name .. ": format") if family then t:eq(Tracker.family(m), family, c.name .. ": engine family") end t:check(Fmt.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 f = S.features(buf, frames) local r = S.compare(f, c.ref) t:note("%-8s gain %+.2f dB, loudness error %.2f dB, spectrum %.3f, balance %.3f, %.2f s", c.name, r.gainDb, r.envErr, r.spectral, r.balance, dec.duration) if r.spectral < thresh.spectral or r.envErr > thresh.envErr then for _, w in S.worst(f, 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.envErr < thresh.envErr, c.name .. ": loudness envelope") t:check(r.spectral > thresh.spectral, c.name .. ": spectrum") t:check(r.balance < thresh.balance, c.name .. ": stereo balance") end -- seek: the position after a seek and the output continuing local m = mods[1] if m then local dec = m:render() dec:seek(1) t:check(math.abs(dec:position() - 1) < 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 return mods end function S.fuzzer(Fmt: any): (buffer) -> any local Bytes = require("@rosetta/core/Bytes") local Tracker = require("@rosetta/codecs/audio/Tracker") return function(b: buffer): any local fm = Fmt.decode(Bytes.reader(b)) if fm then local d = Tracker.render(fm, { maxDuration = 0.5, sampleRate = 8000 }) if d then d:read(4000) end end return fm end end function S.samples(cases: { any }): { string } local out = {} for _, c in cases do table.insert(out, c.data) end return out end return S