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

+136
View File
@@ -0,0 +1,136 @@
-- Shared checks of the lossless audio format tests (wavpack, ape, tta): every fixture opened, decoded bit-exact
-- against the reference decoder's samples (readInt for integer streams, the f32 bytes for float ones), f32 scaling
-- and output reuse, seeking to sample-accurate positions, reading past the end, tags, sniff. A fixture is a table
-- { name, data, rate, channels, bits, float, frames, crc, tags?, partial? } (partial: a cut file whose decode stops
-- with an error; the reference decoded its first `frames` frames).
local T = require("@tests/T")
local Bytes = require("@formats/core/Bytes")
local Crc32 = require("@formats/core/Crc32")
local L = {}
local f32buf = buffer.create(4)
local function f32(x: number): number
buffer.writef32(f32buf, 0, x)
return buffer.readf32(f32buf, 0)
end
local function open(mod: any, b: any, opts: any?): (any, any)
return mod.open(Bytes.reader(if type(b) == "string" then buffer.fromstring(b) else b), opts)
end
L.open = open
-- all(dec, fx) -> crc of every sample read (i32 for integer streams, f32 bytes for floats), frames, the values
function L.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
if fx.partial then -- a cut file: the reference decoded fx.frames (whole frames); the rest may decode too
got = math.min(got, fx.frames - n)
if got <= 0 then
continue
end
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
-- cases(t, mod, cases, optsOf?) -> seeks checked: the per-fixture checks; optsOf(fx) gives the open options
function L.cases(t: any, mod: any, cases: { any }, optsOf: ((any) -> any)?): number
local seekChecks = 0
for _, fx in cases do
local name = fx.name
local dec, err = open(mod, fx.data, optsOf and optsOf(fx))
t:check(dec, name .. ": open: " .. tostring(err))
t:check(dec.sampleRate == fx.rate and dec.channels == fx.channels, name .. ": format")
if not fx.partial then
t:eq(dec.frames, fx.frames, name .. ": frames")
t:check(math.abs(dec.duration - fx.frames / fx.rate) < 1e-9, name .. ": duration")
end
t:eq(dec.float, fx.float, name .. ": float")
local crc, n, vals = L.all(dec, fx)
if fx.partial then
t:check(dec.error ~= nil, name .. ": the cut frame is reported")
else
t:eq(dec.error, nil, name .. ": no decode error")
end
t:eq(n, fx.frames, name .. ": decoded frames")
t:eq(crc, fx.crc, name .. ": samples identical to the reference decoder")
-- 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 or fx.bits) - 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 last = if fx.partial then fx.frames - 50 else fx.frames
local rand = T.rng(7)
local targets = { 0, 1, last - 1, last // 2, 22050, 22049, 73729, 73727 }
for _ = 1, 6 do
table.insert(targets, rand(math.max(last, 1)))
end
for _, s in targets do
if s >= 0 and s < last 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 " .. s)
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(mod.sniff(Bytes.reader(buffer.fromstring(fx.data))), 1, name .. ": sniff")
end
return seekChecks
end
-- subrange(t, mod, fx, depth) : decode() from a Reader over a sub-range equals read(); meta.source
function L.subrange(t: any, mod: any, fx: any, format: string)
local name = fx.name
local big = buffer.create(#fx.data + 13)
buffer.writestring(big, 6, fx.data)
local a, err = mod.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(mod, fx.data):read(fx.frames)
t:check(buffer.tostring(ref) == buffer.tostring(a.samples), name .. ": decode = read")
t:eq(a.meta.source.format, format, name .. ": meta.source")
t:eq(a.meta.source.bitDepth, if fx.float then 32 else fx.bits, name .. ": bit depth")
end
return L
+90
View File
@@ -0,0 +1,90 @@
-- audio/ape tests: every fixture from tools/fixtures/ape.py (mac -c1000 .. -c5000, 8 - 32-bit int and 32-bit float,
-- 1 - 8 channels, special frames (silence, pseudo stereo), odd rates and lengths, a second frame, APEv2 tags, the FATE
-- sample of an older 3.99 encoder cut mid-frame) decoded bit-exact against `mac -d`; seeking; f32 scaling and output
-- reuse; decode() from a sub-range Reader; an ID3v2 tag in front; the header facts; sniff; the api; a damaged frame
-- (CRC); the fuzz pass. Fixtures: tools/fixtures/ape.py.
local T = require("@tests/T")
local Ape = require("@formats/audio/ape")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local F = require("@formats")
local Cases = require("@tests/gen/ape_cases")
local L = require("@tests/audio/_lossless")
local t = T.suite("ape")
local byName = {}
for _, c in Cases do
byName[c.name] = c
end
local function open(b: any, opts: any?): (any, any)
return L.open(Ape, b, opts)
end
t:eq(Ape.name, "ape", "name")
t:check(Ape.caps.decode and not Ape.caps.encode, "caps")
local seekChecks = L.cases(t, Ape, Cases)
t:note("%d fixtures bit-exact against mac -d, %d seeks", #Cases, seekChecks)
for _, name in { "s16_c2000", "tags", "f32", "ch6" } do
L.subrange(t, Ape, byName[name], "ape")
end
-- header facts
do
local d = open(byName.s16_c5000.data)
t:eq(d.level, "insane", "level")
t:eq(d.bitsPerSample, 16, "bits")
t:check(d.version >= 3990, "version")
t:eq(open(byName.s24_c4000.data).level, "extra high", "level 4000")
t:eq(open(byName.s16_c1000.data).level, "fast", "level 1000")
end
-- an ID3v2 tag in front of the descriptor (the seek table counts from the descriptor)
do
local fx = byName.s16_c3000
local id3 = "ID3\4\0\0\0\0\0\20" .. "TIT2\0\0\0\10\0\0\3hello id3"
local b = buffer.fromstring(id3 .. fx.data)
t:eq(Ape.sniff(Bytes.reader(b)), 1, "sniff ID3 + MAC")
local dec = open(b)
t:check(dec and (L.all(dec, fx)) == fx.crc, "ID3v2 in front")
end
-- the api
do
local fx = byName.s16_c2000
local _ = fx
t:eq(F.detect(buffer.fromstring(fx.data)), "ape", "detect")
local a = F.audio.load(fx.data)
t:check(a and a.frames == fx.frames and a.meta.source.format == "ape", "F.audio.load")
end
-- errors: a damaged frame fails its CRC, bad headers
do
local fx = byName.s16_long -- two frames: the second is damaged
local b = buffer.fromstring(fx.data)
local at = #fx.data - 2000
buffer.writeu8(b, at, bit32.bxor(buffer.readu8(b, at), 0x40))
local dec = open(b)
local _, got = dec:read(fx.frames)
t:check(dec.error and (dec.error.code == Err.CHECKSUM or dec.error.code == Err.CORRUPT) and got == 73728, "damaged frame: " .. tostring(dec.error))
local _, e0 = open(buffer.fromstring(string.sub(fx.data, 1, 2000) .. string.rep("\0", 100)))
t:check(e0 ~= nil, "a broken first frame fails open: " .. tostring(e0))
local _, e1 = open("MAC ")
t:check(e1 and Err.is(e1), "too short: " .. tostring(e1))
local old = buffer.fromstring(fx.data)
buffer.writeu16(old, 4, 3800)
local _, e2 = open(old)
t:check(e2 and e2.code == Err.UNSUPPORTED, "old version: " .. tostring(e2))
t:eq(Ape.sniff(Bytes.reader(buffer.fromstring("RIFF"))), 0, "sniff RIFF")
end
-- the fuzz pass
local samples = {}
for _, name in { "s16_c2000", "s16_c4000", "mono", "s24", "s32", "f32", "ch6", "silence", "pseudo_stereo" } do
table.insert(samples, buffer.fromstring(byName[name].data))
end
t:fuzz("decode", function(b: buffer)
return Ape.decode(Bytes.reader(b))
end, samples, { mutations = 50 })
t:done()
+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