ws-audio2: Monkey's Audio: codecs/audio/Ape (port of the MAC SDK 13.28 decoder, BSD-3) + audio/ape

File versions 3.95+ (3.99+ verified; the 3.95-3.98 range-coder model is ported without a reference fixture), every
level (fast .. insane), 8/16/24/32-bit int and 32-bit float, 1-32 channels, special frames, frame CRCs, seeking,
APEv2 tags, ID3v2 in front. 35 fixtures (mac 13.28, the FATE luckynight sample) bit-exact against mac -d.
tests/audio/_lossless.luau: the per-fixture checks shared by the lossless format tests. tools/bench.luau: the
ws-audio2 lossless block.
This commit is contained in:
4DBug committed 2026-10-31 14:52:08 -05:00
1 parent 66c3819063
commit ae8ff9d6bf
12 files changed
+1543 -99

No files matched your search

+10 -99
View File
@@ -6,21 +6,16 @@
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 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 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
@@ -29,108 +24,24 @@ 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
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 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")
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, name .. ": without wvc: lossy")
t:eq((all(lossy, fx)), fx.lossy_crc, name .. ": without wvc: identical to wvunpack -i")
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
-- 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")
L.subrange(t, WavPack, byName[name], "wavpack")
end
-- tags, md5, the source facts