-- audio/mpc tests: Musepack SV8 (mpcenc 1.30: stereo / mono, 32 / 44.1 / 48 kHz, quality 1 .. 9, 1 / 4 / 16 / 64 -- frames per packet) and SV7 (mppenc 1.16, quality 2 / 5 / 8) against libmpcdec's float output (mpcf32: gapless -- length, near bit-exact) and ffmpeg's decode (SNR); APEv2 tags; seeking (SV8 exact, SV7 like libmpcdec); a leading -- ID3v2 tag; truncated files; error codes; the fuzz no-throw pass. Fixtures: tools/fixtures/mpc.py. local T = require("@tests/T") local Mpc = require("@rosetta/audio/mpc") local Bytes = require("@rosetta/core/Bytes") local Err = require("@rosetta/core/Err") local Fx = require("@tests/gen/mpc_cases") local Seek = require("@tests/gen/mpc_seek") local t = T.suite("mpc") local function reader(s: string): Bytes.Reader return Bytes.reader(buffer.fromstring(s)) end -- SNR (dB) and the largest difference of frames [from, to) of got against a f32le reference (frames from 0) local function compare(got: buffer, gotFrom: number, ref: string, ch: number, frames: number): (number, number) local rb = buffer.fromstring(ref) local n = math.min(frames * ch, #ref // 4) local sig, err, maxd = 0, 0, 0 for i = 0, n - 1 do local r = buffer.readf32(rb, i * 4) local d = buffer.readf32(got, (gotFrom * ch + i) * 4) - r sig += r * r err += d * d maxd = math.max(maxd, math.abs(d)) end return if err == 0 then math.huge else 10 * math.log10(sig / err), maxd end t:eq(Mpc.name, "mpc", "name") t:check(Mpc.caps.decode and not Mpc.caps.encode and Mpc.encode == nil, "caps: decode only") local byName = {} for _, c in Fx do byName[c.name] = c local r = reader(c.data) t:check(Mpc.sniff(r) >= 0.9, c.name .. ": sniff") local a, err = Mpc.decode(r) t:check(a, c.name .. ": " .. tostring(err)) if not a then continue end t:check(a.channels == c.channels and a.sampleRate == c.rate, c.name .. ": format") t:eq(a.frames, c.frames, c.name .. ": gapless length") t:eq(a.meta.source.version, c.version, c.name .. ": stream version") local s, maxd = compare(a.samples, 0, c.ref, c.channels, c.exact) t:check(s > 120 and maxd < 1e-5, string.format("%s: SNR %.1f dB, max diff %g vs libmpcdec", c.name, s, maxd)) local all = compare(a.samples, 0, c.ref, c.channels, c.frames) t:check(all > 60, string.format("%s: whole SNR %.1f dB", c.name, all)) local line = string.format("%s: SV%d %d ch %d Hz, %d frames, SNR %.1f dB (max diff %.2g) vs libmpcdec", c.name, c.version, c.channels, c.rate, a.frames, s, maxd) if c.ff then local f = compare(a.samples, 0, c.ff, c.channels, c.frames) t:check(f > 80, string.format("%s: SNR %.1f dB vs ffmpeg", c.name, f)) line ..= string.format(", %.1f dB vs ffmpeg (s16)", f) end t:note("%s", line) if c.tags then for k, v in c.tags do t:eq(a.tags[k], v, c.name .. ": tag " .. k) end end end -- seeking: SV8 is exact (one packet of pre-roll); SV7 decodes like libmpcdec's seek (32 frames of pre-roll from reset -- scalefactors) for _, sk in Seek do local c = byName[sk.name] local whole = Mpc.decode(reader(c.data)) :: any local dec = Mpc.open(reader(c.data)) :: any local _ = dec:read(5000) -- decode a little first: the seek must forget the state dec:seek(sk.at / c.rate) t:check(math.abs(dec:position() - sk.at / c.rate) < 1e-9, sk.name .. ": position after seek") local b, n = dec:read(4096) t:eq(n, 4096, sk.name .. ": read after seek") if c.version == 8 then local _, own = compare(b, 0, string.sub(buffer.tostring(whole.samples), sk.at * 8 + 1, (sk.at + 4096) * 8), 2, 4096) t:check(own < 1e-6, string.format("%s: seek to %d is exact (max diff %g)", sk.name, sk.at, own)) -- libmpcdec seeks without warming the synthesis up: compare after its first frame local _, lib = compare(b, 1152, string.sub(sk.ref, 1152 * 8 + 1), 2, 4096 - 1152) t:check(lib < 1e-5, string.format("%s: seek matches libmpcdec past 1152 frames (max diff %g)", sk.name, lib)) else local s, lib = compare(b, 0, sk.ref, 2, 4096) t:check(lib < 1e-5, string.format("%s: seek to %d matches libmpcdec's (max diff %g)", sk.name, sk.at, lib)) local own = compare(b, 0, string.sub(buffer.tostring(whole.samples), sk.at * 8 + 1, (sk.at + 4096) * 8), 2, 4096) t:note("%s: seek %d: SNR %.1f dB vs the continuous decode (SV7 seeks restart the scalefactors: not always exact)", sk.name, sk.at, own) local _ = s end dec:seek(0) local b0, n0 = dec:read(1000) local _, d0 = compare(b0, 0, c.ref, 2, n0) t:check(n0 == 1000 and d0 < 1e-5, sk.name .. ": seek back to 0") end -- an ID3v2 tag in front; truncated files do local c = byName.sv8_q5_stereo local id3 = "ID3\3\0\0\0\0\0\20" .. string.rep("\0", 20) local a = Mpc.decode(reader(id3 .. c.data)) :: any t:check(a and a.frames == c.frames, "ID3v2 in front") t:check(Mpc.sniff(reader(id3 .. c.data)) == 1, "ID3v2 in front: sniff") for _, name in { "sv8_q5_stereo", "sv7_q5_stereo" } do local d = byName[name] local cut = Mpc.decode(reader(string.sub(d.data, 1, #d.data * 2 // 3))) :: any t:check(cut and cut.frames > 0 and cut.frames < d.frames, name .. ": truncated file decodes its start") if cut then local _, m = compare(cut.samples, 0, d.ref, 2, math.min(cut.frames - 1152, d.exact)) t:check(m < 1e-5, name .. ": truncated file: same start") end end end -- errors do local c8, c7 = byName.sv8_q5_stereo.data, byName.sv7_q5_stereo.data t:eq(Mpc.sniff(reader("RIFF....WAVE")), 0, "sniff a WAV") local _, e = Mpc.decode(reader("MPCX" .. string.rep("\0", 64))) t:check(e and e.code == Err.CORRUPT, "bad magic: " .. tostring(e)) -- the stream header's CRC (SH is the first packet: "SH", size, CRC, version ...) local p = string.find(c8, "SH", 5, true) :: number local bad = string.sub(c8, 1, p + 7) .. string.char((string.byte(c8, p + 8) + 1) % 256) .. string.sub(c8, p + 9) _, e = Mpc.decode(reader(bad)) t:check(e and e.code == Err.CHECKSUM, "SH CRC: " .. tostring(e)) _, e = Mpc.decode(reader("MP+\6" .. string.sub(c7, 5))) t:check(e and e.code == Err.UNSUPPORTED, "SV6: " .. tostring(e)) _, e = Mpc.decode(reader(string.sub(c7, 1, 20))) t:check(e and e.code == Err.TRUNCATED, "short SV7 header: " .. tostring(e)) _, e = Mpc.decode(reader(c8), { maxFrames = 1000 }) t:check(e and e.code == Err.LIMIT, "maxFrames: " .. tostring(e)) end -- fuzz do local samples = {} for _, c in Fx do table.insert(samples, string.sub(c.data, 1, 3000)) end t:fuzz("decode", function(b: buffer) return Mpc.decode(Bytes.reader(b)) end, samples, { mutations = 25, keepHeader = 4 }) end t:done()