ws-audio2: WavPack: codecs/audio/WavPack (port of WavPack 5.9.0's decoder, BSD) + audio/wavpack

Lossless and hybrid (lossy, and lossless with the .wvc via opts.correction / opts.resolve), 8-32 bit int (int32 modes,
wvx) and 32-bit float, 1-8+ channels, every decorrelation pass, joint stereo, cross decorrelation, noise shaping,
block CRCs; APEv2 tags; sample-accurate seeking. 58 fixtures (wavpack -f..-hhx6, ffmpeg) bit-exact against wvunpack.

codecs/audio/Stream (shared, backward compatible): Stream.packets takes optional spec.scale (read() writes pcm values
times scale) and spec.ints (adds dec:readInt for bit-exact integer reads).
This commit is contained in:
4DBug committed 2026-10-31 14:19:23 -05:00
1 parent b7006dbbed
commit 967b91db91
11 files changed
+2435 -6

No files matched your search

+205
View File
@@ -0,0 +1,205 @@
-- audio/wavpack tests: every fixture from tools/fixtures/wavpack.py (wavpack -f / -g / -h / -hh / -x1..6, joint stereo
-- on / off, 8 - 32-bit int and 32-bit float, 1 - 8 channels, odd rates and lengths, hybrid lossy and hybrid lossless
-- with the .wvc, noise shaping, ffmpeg's encoder) decoded bit-exact against wvunpack; seeking; f32 scaling and output
-- reuse; decode() from a sub-range Reader; APEv2 tags and the MD5; the correction file through opts.resolve; sniff;
-- the api; damaged blocks; the fuzz pass. Fixtures: tools/fixtures/wavpack.py.
local T = require("@tests/T")
local WavPack = require("@formats/audio/wavpack")
local Bytes = require("@formats/core/Bytes")
local Crc32 = require("@formats/core/Crc32")
local Err = require("@formats/core/Err")
local F = require("@formats")
local Cases = require("@tests/gen/wavpack_cases")
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 f32buf = buffer.create(4)
local function f32(x: number): number
buffer.writef32(f32buf, 0, x)
return buffer.readf32(f32buf, 0)
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")
-- all(dec, fx) -> crc of every sample (i32 for integer streams, the f32 bytes for floats), frames, the values
local function all(dec: any, fx: any): (number, number, { number })
local ch = dec.channels
local vals = table.create(fx.frames * ch, 0)
local crc, n, k = 0, 0, 0
local sizes = { 4096, 1, 777, 10000, 3 }
while true do
k += 1
local out, got
if fx.float then
out, got = dec:read(sizes[k % #sizes + 1])
else
out, got = dec:readInt(sizes[k % #sizes + 1])
end
if got == 0 then
break
end
crc = Crc32.of(out, 0, got * ch * 4, crc)
for i = 0, got * ch - 1 do
vals[n * ch + i + 1] = if fx.float then buffer.readf32(out, i * 4) else buffer.readi32(out, i * 4)
end
n += got
end
return crc, n, vals
end
local seekChecks = 0
for _, fx in Cases do
local name = fx.name
local opts = if fx.wvc then { correction = fx.wvc } else nil
local dec, err = open(fx.data, opts)
t:check(dec, name .. ": open: " .. tostring(err))
t:check(dec.sampleRate == fx.rate and dec.channels == fx.channels, name .. ": format")
t:eq(dec.frames, fx.frames, name .. ": frames")
t:eq(dec.float, fx.float, name .. ": float")
t:check(math.abs(dec.duration - fx.frames / fx.rate) < 1e-9, name .. ": duration")
local crc, n, vals = all(dec, fx)
t:eq(dec.error, nil, name .. ": no decode error")
t:eq(n, fx.frames, name .. ": decoded frames")
t:eq(crc, fx.crc, name .. ": samples identical to wvunpack")
t:eq(dec.lossless, not fx.lossy, name .. ": lossless")
if fx.wvc then
local lossy = open(fx.data)
t:eq(lossy.lossless, false, name .. ": without wvc: lossy")
t:eq((all(lossy, fx)), fx.lossy_crc, name .. ": without wvc: identical to wvunpack -i")
end
-- f32 scaling, output reuse
local ch = dec.channels
dec:seek(0)
local reuse = buffer.create(100 * ch * 4)
local f, gotf = dec:read(100, reuse)
t:check(f == reuse, name .. ": output buffer reused")
local scale = if fx.float then 1 else 2 ^ (fx.container - 1)
local bad = 0
for i = 0, math.min(gotf, 100) * ch - 1 do
if buffer.readf32(f, i * 4) ~= f32(vals[i + 1] / scale) then
bad += 1
end
end
t:eq(bad, 0, name .. ": f32 samples")
-- seeking (sample-accurate: seek(s / rate) lands on frame s)
local rand = T.rng(7)
local targets = { 0, 1, fx.frames - 1, fx.frames // 2, 22050, 22049, 22051 }
for _ = 1, 6 do
table.insert(targets, rand(fx.frames))
end
for _, s in targets do
if s >= 0 and s < fx.frames then
dec:seek(s / fx.rate)
t:check(math.abs(dec:position() * fx.rate - s) < 1e-6, name .. ": position after seek " .. s)
local out, got = dec:read(50)
t:eq(got, math.min(50, fx.frames - s), name .. ": read after seek")
local diff = 0
for i = 0, got * ch - 1 do
if buffer.readf32(out, i * 4) ~= f32(vals[s * ch + i + 1] / scale) then
diff += 1
end
end
t:eq(diff, 0, name .. ": samples after seek " .. s)
seekChecks += 1
end
end
dec:seek(dec.duration + 5)
t:eq(select(2, dec:read(10)), 0, name .. ": read past the end")
for key, v in fx.tags or {} do
t:eq(dec.tags[key], v, name .. ": tag " .. key)
end
t:eq(WavPack.sniff(Bytes.reader(buffer.fromstring(fx.data))), 1, name .. ": sniff")
end
t:note("%d fixtures bit-exact against wvunpack, %d seeks", #Cases, seekChecks)
-- decode() from a sub-range Reader
for _, name in { "s16_g", "md5_tags", "f32", "ch6" } do
local fx = byName[name]
local big = buffer.create(#fx.data + 13)
buffer.writestring(big, 6, fx.data)
local a, err = WavPack.decode(Bytes.reader(big, 6, #fx.data))
t:check(a and a.kind == "audio" and a.frames == fx.frames and a.channels == fx.channels, name .. ": decode: " .. tostring(err))
local ref = open(fx.data):read(fx.frames)
t:check(buffer.tostring(ref) == buffer.tostring(a.samples), name .. ": decode = read")
t:eq(a.meta.source.format, "wavpack", name .. ": meta.source")
t:eq(a.meta.source.bitDepth, if fx.float then 32 else fx.bits, name .. ": bit depth")
end
-- tags, md5, the source facts
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
-- the api
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
-- the fuzz pass
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()