-- audio/wavpack tests: tools/fixtures/wavpack.py's files (every mode, hybrid + .wvc) bit-exact against wvunpack. -- Fixtures: tools/fixtures/wavpack.py. local T = require("@tests/T") local WavPack = require("@rosetta/audio/wavpack") local Bytes = require("@rosetta/core/Bytes") local Err = require("@rosetta/core/Err") local F = require("@rosetta") local Cases = require("@tests/gen/wavpack_cases") local L = require("@tests/audio/_lossless") local t = T.suite("wavpack") local function open(b: any, opts: any?): (any, any) return WavPack.open(Bytes.reader(if type(b) == "string" then buffer.fromstring(b) else b), opts) end local byName = {} for _, c in Cases do byName[c.name] = c end t:eq(WavPack.name, "wavpack", "name") t:check(WavPack.caps.decode and not WavPack.caps.encode, "caps") local all = L.all local seekChecks = L.cases(t, WavPack, Cases, function(fx: any): any return if fx.wvc then { correction = fx.wvc } else nil end) for _, fx in Cases do local dec = open(fx.data, if fx.wvc then { correction = fx.wvc } else nil) t:eq(dec.lossless, not fx.lossy, fx.name .. ": lossless") if fx.wvc then local lossy = open(fx.data) t:eq(lossy.lossless, false, fx.name .. ": without wvc: lossy") t:eq((all(lossy, fx)), fx.lossy_crc, fx.name .. ": without wvc: identical to wvunpack -i") end end t:note("%d fixtures bit-exact against wvunpack, %d seeks", #Cases, seekChecks) for _, name in { "s16_g", "md5_tags", "f32", "ch6" } do L.subrange(t, WavPack, byName[name], "wavpack") end do local dec = open(byName.md5_tags.data) t:eq(#dec.md5, 32, "md5") t:eq(dec.bitsPerSample, 16, "bitsPerSample") t:eq(open(byName.s20.data).bitsPerSample, 20, "20-bit") t:eq(open(byName.s12.data).bitsPerSample, 12, "12-bit") t:eq(open(byName.hyb_c3.data).hybrid, true, "hybrid") t:eq(open(byName.ch6.data).channelMask, 0x3F, "channel mask") end -- the correction file through opts.resolve + opts.name (as the api passes them for paths) do local fx = byName.hyb_c3 local asked = nil local dec = open(fx.data, { name = "music/song.wv", resolve = function(uri: string): buffer? asked = uri return buffer.fromstring(fx.wvc) end, }) t:eq(asked, "song.wvc", "resolve asks for the .wvc") t:check(dec.lossless and (all(dec, fx)) == fx.crc, "resolve: lossless") local dec2 = open(fx.data, { name = "x.wv", resolve = function(): buffer? return nil end }) t:check(dec2 and not dec2.lossless, "resolve: no wvc -> lossy") end do local fx = byName.s16_g t:eq(F.detect(buffer.fromstring(fx.data)), "wavpack", "detect") local a = F.audio.load(fx.data) t:check(a and a.frames == fx.frames and a.meta.source.format == "wavpack", "F.audio.load") local d = F.audio.open(fx.data) t:check(d and d.sampleRate == fx.rate, "F.audio.open") end -- errors: damaged data, a bad CRC, no audio do local fx = byName.s16_g local b = buffer.fromstring(fx.data) buffer.writeu8(b, 200, bit32.bxor(buffer.readu8(b, 200), 0x55)) local dec = open(b) if dec then local _, got = dec:read(fx.frames) t:check(dec.error ~= nil or got < fx.frames, "damaged block reported") else t:check(true, "damaged block reported") end local _, e1 = open("wvpk") t:check(e1 and Err.is(e1), "too short: " .. tostring(e1)) local _, e2 = open(string.rep("\0", 100)) t:check(e2 and e2.code == Err.CORRUPT, "no blocks: " .. tostring(e2)) t:eq(WavPack.sniff(Bytes.reader(buffer.fromstring("RIFF"))), 0, "sniff RIFF") end local samples = {} for _, name in { "s16_g", "f32", "hyb_b3", "s32_hh", "ch6", "u8" } do table.insert(samples, buffer.fromstring(byName[name].data)) end t:fuzz("decode", function(b: buffer) return WavPack.decode(Bytes.reader(b)) end, samples, { mutations = 150 }) local fxw = byName.hyb_c3 t:fuzz("decode with wvc", function(b: buffer) return WavPack.decode(Bytes.reader(buffer.fromstring(fxw.data)), { correction = b }) end, { buffer.fromstring(fxw.wvc) }, { mutations = 100 }) t:done()