absorb: containers/Riff (lenient reader from wav-luau + new writer: RIFX, RF64/ds64, LIST, parts) and audio/wav (decoder from wav-luau fd9a909 on Riff + new encoder u8/s16/s24/s32/f32, EXTENSIBLE, RF64; byte-exact vs ffmpeg -bitexact)
This commit is contained in:
1 parent
e3f9d0fb38
commit
85fb079068
9 files changed
+1989
No files matched your search
@@ -0,0 +1,314 @@
|
||||
-- audio/wav tests: every format / container / damaged variant from tools/fixtures/wav.py decoded in reads of mixed
|
||||
-- sizes against ffmpeg's (or sox's) f32 decode, with tags, seeking and output-buffer reuse; hand-built files (header
|
||||
-- parsing, errors, argument clamping, EXTENSIBLE with valid bits, the truncated flag); the encoder byte-exact against
|
||||
-- ffmpeg -bitexact (u8 / s16 / s24 / s32 / f32, 1-8 channels, > 48 kHz, RF64), round trips from an Audio and from a
|
||||
-- streaming Decoder, tags, encodeParts, the RF64 switch; the api (F.audio.load / open / save); the fuzz pass.
|
||||
-- Ported from wav-luau tests/core.luau + tests/decode.luau. Fixtures: tools/fixtures/wav.py.
|
||||
local T = require("@tests/T")
|
||||
local Wav = require("@formats/audio/wav")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local F = require("@formats")
|
||||
local Cases = require("@tests/gen/wav_cases")
|
||||
local Enc = require("@tests/gen/wav_enc")
|
||||
|
||||
local t = T.suite("wav")
|
||||
local TOL = 2e-6
|
||||
|
||||
local function open(s: any): (any, any)
|
||||
local b = if type(s) == "string" then buffer.fromstring(s) else s
|
||||
return Wav.open(Bytes.reader(b))
|
||||
end
|
||||
|
||||
-- little-endian packers (no string.pack on LuaJIT)
|
||||
local function u16(v: number): string
|
||||
local b = buffer.create(2)
|
||||
buffer.writeu16(b, 0, v % 65536)
|
||||
return buffer.tostring(b)
|
||||
end
|
||||
local function u32(v: number): string
|
||||
local b = buffer.create(4)
|
||||
buffer.writeu32(b, 0, v % 4294967296)
|
||||
return buffer.tostring(b)
|
||||
end
|
||||
local function i32(v: number): string
|
||||
local b = buffer.create(4)
|
||||
buffer.writei32(b, 0, v)
|
||||
return buffer.tostring(b)
|
||||
end
|
||||
local function f32(v: number): string
|
||||
local b = buffer.create(4)
|
||||
buffer.writef32(b, 0, v)
|
||||
return buffer.tostring(b)
|
||||
end
|
||||
local function i16s(...: number): string
|
||||
local out = {}
|
||||
for i, v in { ... } do
|
||||
out[i] = u16(if v < 0 then v + 65536 else v)
|
||||
end
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
local function chunk(id: string, body: string): string
|
||||
local s = id .. u32(#body) .. body
|
||||
return if #body % 2 == 1 then s .. "\0" else s
|
||||
end
|
||||
local function fmt(tag: number, ch: number, rate: number, bits: number, extra: string?): string
|
||||
local ba = ch * ((bits + 7) // 8)
|
||||
return chunk("fmt ", u16(tag) .. u16(ch) .. u32(rate) .. u32(rate * ba) .. u16(ba) .. u16(bits) .. (extra or ""))
|
||||
end
|
||||
local function riff(...: string): string
|
||||
local body = "WAVE" .. table.concat({ ... })
|
||||
return "RIFF" .. u32(#body) .. body
|
||||
end
|
||||
|
||||
-- the contract
|
||||
t:eq(Wav.name, "wav", "name")
|
||||
t:eq(Wav.category, "audio", "category")
|
||||
t:check(Wav.caps.decode and Wav.caps.encode and Wav.open ~= nil, "caps / open")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ fixtures
|
||||
local function readAll(dec: any, sizes: { number }): (buffer, number)
|
||||
local out = buffer.create(dec.frames * dec.channels * 4)
|
||||
local got, i = 0, 0
|
||||
while true do
|
||||
i += 1
|
||||
local c, n = dec:read(sizes[(i - 1) % #sizes + 1])
|
||||
if n == 0 then
|
||||
break
|
||||
end
|
||||
buffer.copy(out, got * dec.channels * 4, c, 0, n * dec.channels * 4)
|
||||
got += n
|
||||
end
|
||||
return out, got
|
||||
end
|
||||
local function maxDiff(a: buffer, aOff: number, b: buffer, bOff: number, count: number): number
|
||||
local m = 0
|
||||
for i = 0, count - 1 do
|
||||
local d = math.abs(buffer.readf32(a, (aOff + i) * 4) - buffer.readf32(b, (bOff + i) * 4))
|
||||
if d > m then
|
||||
m = d
|
||||
end
|
||||
end
|
||||
return m
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, fx in Cases do
|
||||
local name = fx.name
|
||||
table.insert(samples, fx.file)
|
||||
local ref = buffer.fromstring(fx.ref)
|
||||
local dec, err = open(fx.file)
|
||||
t:check(dec, name .. ": open: " .. tostring(err))
|
||||
t:eq(dec.channels, fx.channels, name .. ": channels")
|
||||
t:eq(dec.sampleRate, fx.rate, name .. ": rate")
|
||||
t:eq(dec.format, fx.format, name .. ": format")
|
||||
t:eq(dec.container, fx.container, name .. ": container")
|
||||
t:eq(dec.frames, fx.frames, name .. ": frames")
|
||||
t:check(math.abs(dec.duration - fx.frames / fx.rate) < 1e-9, name .. ": duration")
|
||||
t:check(dec.bitrate > 0, name .. ": bitrate")
|
||||
for k, v in fx.tags or {} do
|
||||
t:eq(dec.tags[k], v, name .. ": tag " .. k)
|
||||
end
|
||||
local out, got = readAll(dec, { 1, 7, 333, 1000, 4096 })
|
||||
t:eq(got, fx.frames, name .. ": frames read")
|
||||
local d = maxDiff(out, 0, ref, 0, got * fx.channels)
|
||||
t:check(d <= TOL, string.format("%s: max sample error %g", name, d))
|
||||
t:eq(dec:position(), fx.frames / fx.rate, name .. ": position at the end")
|
||||
for _, f0 in { 1234, 0, fx.frames - 20, 517, 2047 } do
|
||||
local f = math.min(f0, fx.frames)
|
||||
dec:seek(f / fx.rate)
|
||||
t:eq(dec:tell(), f, name .. ": seek to " .. f)
|
||||
local c, n = dec:read(50)
|
||||
t:eq(n, math.min(50, fx.frames - f), name .. ": read after seek " .. f)
|
||||
t:check(maxDiff(c, 0, ref, f * fx.channels, n * fx.channels) <= TOL, name .. ": samples after seek " .. f)
|
||||
end
|
||||
dec:seek(0)
|
||||
local reuse = buffer.create(64 * fx.channels * 4)
|
||||
local b2, n2 = dec:read(64, reuse)
|
||||
t:check(b2 == reuse and n2 == 64, name .. ": output buffer reused")
|
||||
-- decode() = the whole stream as an Audio, also from a sub-range reader
|
||||
local big = buffer.create(#fx.file + 9)
|
||||
buffer.writestring(big, 5, fx.file)
|
||||
local a, aerr = Wav.decode(Bytes.reader(big, 5, #fx.file))
|
||||
t:check(a and a.kind == "audio" and a.frames == fx.frames, name .. ": decode: " .. tostring(aerr))
|
||||
t:check(maxDiff(a.samples, 0, ref, 0, a.frames * fx.channels) <= TOL, name .. ": decode samples")
|
||||
t:eq(a.meta.source.codec, fx.format, name .. ": meta.source.codec")
|
||||
end
|
||||
t:note("%d fixture files match ffmpeg / sox", #Cases)
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ hand-built files
|
||||
do
|
||||
local dec = open(riff(fmt(1, 2, 8000, 16), chunk("data", i16s(16384, -16384, 32767, -32768))))
|
||||
t:check(dec, "open minimal")
|
||||
t:check(dec.frames == 2 and dec.channels == 2 and dec.sampleRate == 8000 and dec.bitrate == 8000 * 4 * 8, "minimal fields")
|
||||
t:check(dec.duration == 2 / 8000 and dec.blockAlign == 4 and dec.bitsPerSample == 16 and not dec.truncated, "minimal more")
|
||||
local out, n = dec:read(10)
|
||||
t:eq(n, 2, "read clamps to the stream")
|
||||
t:check(buffer.readf32(out, 0) == 0.5 and buffer.readf32(out, 4) == -0.5, "s16 values")
|
||||
t:check(buffer.readf32(out, 8) == 32767 / 32768 and buffer.readf32(out, 12) == -1, "s16 extremes")
|
||||
t:eq(select(2, dec:read(10)), 0, "end of stream = 0 frames")
|
||||
dec:seek(-5)
|
||||
t:eq(dec:tell(), 0, "seek negative")
|
||||
dec:seek(1e9)
|
||||
t:eq(dec:tell(), 2, "seek past the end")
|
||||
dec:seek(0 / 0)
|
||||
t:eq(dec:tell(), 0, "seek NaN")
|
||||
dec:seekFrame(1)
|
||||
t:eq(dec:position(), 1 / 8000, "seekFrame / position")
|
||||
t:eq(select(2, dec:read(-3)), 0, "read negative")
|
||||
t:eq(select(2, dec:read(0 / 0)), 0, "read NaN")
|
||||
local b, n5 = dec:read(1, buffer.create(2))
|
||||
t:check(n5 == 1 and buffer.len(b) == 8, "too-small output buffer is replaced")
|
||||
end
|
||||
do
|
||||
local dec = open(riff(fmt(1, 1, 8000, 16), "data" .. u32(1000) .. i16s(1, 2, 3) .. "\9"))
|
||||
t:check(dec and dec.frames == 3 and dec.truncated, "truncated: whole frames, flagged")
|
||||
end
|
||||
do -- EXTENSIBLE: 24 valid bits in a 32-bit container
|
||||
local guid = u16(1) .. "\0\0\0\0\16\0\128\0\0\170\0\56\155\113"
|
||||
local ext = u16(22) .. u16(24) .. u32(3) .. guid
|
||||
local dec = open(riff(fmt(0xFFFE, 2, 48000, 32, ext), chunk("data", i32(0x40000000) .. i32(-0x40000000))))
|
||||
t:check(dec and dec.format == "pcm" and dec.validBits == 24 and dec.channelMask == 3, "extensible fields")
|
||||
local out = dec:read(1)
|
||||
t:check(buffer.readf32(out, 0) == 0.5 and buffer.readf32(out, 4) == -0.5, "extensible values")
|
||||
local guidF = u16(3) .. string.sub(guid, 3)
|
||||
local fd = open(riff(fmt(0xFFFE, 1, 48000, 32, u16(22) .. u16(32) .. u32(4) .. guidF), chunk("data", f32(0.25))))
|
||||
t:check(fd and fd.format == "float" and buffer.readf32(fd:read(1), 0) == 0.25, "extensible float")
|
||||
local _, e = open(riff(fmt(0xFFFE, 1, 8000, 16), chunk("data", "\0\0")))
|
||||
t:check(e and e.code == Err.CORRUPT, "extensible without extension")
|
||||
end
|
||||
do -- tags
|
||||
local info = "INFO" .. "INAM" .. u32(3) .. "abc\0" .. "IART" .. u32(6) .. "xy\0\0\0\0" .. "IXYZ" .. u32(2) .. "zz" .. "ITRK" .. u32(2) .. "7\0"
|
||||
local dec = open(riff(fmt(1, 1, 8000, 8), chunk("LIST", info), chunk("data", "\128\128\128")))
|
||||
t:check(dec and dec.tags.title == "abc" and dec.tags.artist == "xy" and dec.tags.IXYZ == "zz" and dec.tags.track == "7", "tags")
|
||||
t:check(dec and dec.frames == 3, "8-bit odd data")
|
||||
end
|
||||
do -- 8-bit, A-law and mu-law values
|
||||
local o = open(riff(fmt(1, 1, 8000, 8), chunk("data", "\0\128\255"))):read(3)
|
||||
t:check(buffer.readf32(o, 0) == -1 and buffer.readf32(o, 4) == 0 and buffer.readf32(o, 8) == 127 / 128, "u8 values")
|
||||
local m = open(riff(fmt(7, 1, 8000, 8), chunk("data", "\255\127\0\128"))):read(4)
|
||||
t:check(buffer.readf32(m, 0) == 0 and buffer.readf32(m, 4) == 0, "mu-law zero")
|
||||
t:check(buffer.readf32(m, 8) == -32124 / 32768 and buffer.readf32(m, 12) == 32124 / 32768, "mu-law extremes")
|
||||
local a = open(riff(fmt(6, 1, 8000, 8), chunk("data", "\213\85\42\170"))):read(4)
|
||||
t:check(buffer.readf32(a, 0) == 8 / 32768 and buffer.readf32(a, 4) == -8 / 32768, "A-law smallest")
|
||||
t:check(buffer.readf32(a, 8) == -32256 / 32768 and buffer.readf32(a, 12) == 32256 / 32768, "A-law extremes")
|
||||
end
|
||||
do -- errors: nil, Err
|
||||
local function fails(s: any, code: string, pattern: string, label: string)
|
||||
local ok, dec, err = pcall(open, s)
|
||||
t:check(ok, label .. ": threw " .. tostring(dec))
|
||||
t:check(dec == nil and Err.is(err) and err.code == code and string.find(err.message, pattern) ~= nil, label .. ": " .. tostring(err))
|
||||
end
|
||||
fails("RIFF", Err.TRUNCATED, "too short", "too short")
|
||||
fails("RIFF\0\0\0\0AVI LIST", Err.CORRUPT, "not a WAVE", "AVI")
|
||||
fails("OggS\0\0\0\0WAVEfmt ", Err.CORRUPT, "not a RIFF", "ogg")
|
||||
fails(riff(chunk("data", "\0\0")), Err.CORRUPT, "no fmt", "no fmt")
|
||||
fails(riff(fmt(1, 2, 8000, 16)), Err.CORRUPT, "no data", "no data")
|
||||
fails(riff(fmt(0x55, 2, 8000, 16), chunk("data", "")), Err.UNSUPPORTED, "format 0x0055", "mp3 in wav")
|
||||
fails(riff(fmt(1, 0, 8000, 16), chunk("data", "")), Err.CORRUPT, "channel", "0 channels")
|
||||
fails(riff(fmt(1, 1, 0, 16), chunk("data", "")), Err.CORRUPT, "sample rate", "rate 0")
|
||||
fails(riff(fmt(3, 1, 8000, 16), chunk("data", "")), Err.UNSUPPORTED, "float size", "float16")
|
||||
fails(riff(fmt(1, 1, 8000, 40), chunk("data", "")), Err.UNSUPPORTED, "PCM size", "pcm 40")
|
||||
fails(riff(fmt(0x11, 2, 8000, 4), chunk("data", "")), Err.CORRUPT, "block", "ima block too small")
|
||||
t:eq(Wav.sniff(Bytes.reader(buffer.fromstring(riff(fmt(1, 1, 8000, 8))))), 1, "sniff")
|
||||
t:eq(Wav.sniff(Bytes.reader(buffer.fromstring("RIFF\0\0\0\0AVI "))), 0, "sniff AVI")
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encoder
|
||||
do
|
||||
for _, c in Enc do
|
||||
local a = Types.Audio.new(c.rate, c.channels, c.frames)
|
||||
buffer.writestring(a.samples, 0, c.samples)
|
||||
local out, err = Wav.encode(a, { sampleFormat = c.kind, rf64 = if c.rf64 then true else nil })
|
||||
t:check(out, c.name .. ": encode: " .. tostring(err))
|
||||
local same = buffer.tostring(out :: buffer) == c.file
|
||||
if not same then
|
||||
t:note("%s: ours %s", c.name, T.hex(out :: buffer, 80))
|
||||
t:note("%s: ffmpeg %s", c.name, T.hex(buffer.fromstring(c.file), 80))
|
||||
end
|
||||
t:check(same, c.name .. ": identical to ffmpeg -bitexact")
|
||||
-- and back
|
||||
local back = Wav.decode(Bytes.reader(out :: buffer))
|
||||
t:check(back and back.frames == c.frames and buffer.tostring(back.samples) == c.samples, c.name .. ": round trip")
|
||||
-- from a streaming Decoder (our own, over ffmpeg's file)
|
||||
local dec = Wav.open(Bytes.reader(buffer.fromstring(c.file)))
|
||||
local out2 = Wav.encode(dec, { sampleFormat = c.kind, rf64 = if c.rf64 then true else nil })
|
||||
t:check(out2 and buffer.tostring(out2) == c.file, c.name .. ": encode from a Decoder")
|
||||
end
|
||||
t:note("encoder: %d files identical to ffmpeg -bitexact", #Enc)
|
||||
-- clamping and rounding
|
||||
local a = Types.Audio.new(8000, 1, 6)
|
||||
for i, v in { 2, -2, 0.5 / 32768, -0.5 / 32768, 1.5 / 32768, 0 / 0 } do
|
||||
buffer.writef32(a.samples, (i - 1) * 4, v)
|
||||
end
|
||||
local d = Wav.decode(Bytes.reader(Wav.encode(a) :: buffer)) :: any
|
||||
local want = { 32767, -32768, 1, 0, 2, 0 }
|
||||
for i, w in want do
|
||||
t:eq(math.floor(buffer.readf32(d.samples, (i - 1) * 4) * 32768 + 0.5), w, "s16 rounding / clamping " .. i)
|
||||
end
|
||||
-- tags
|
||||
a.tags = { title = "Hello", artist = "Someone", IXYZ = "kept", comment = "" }
|
||||
local tg = Wav.decode(Bytes.reader(Wav.encode(a, { sampleFormat = "u8" }) :: buffer)) :: any
|
||||
t:check(tg.tags.title == "Hello" and tg.tags.artist == "Someone" and tg.tags.IXYZ == "kept" and tg.tags.comment == nil, "tag round trip")
|
||||
-- encodeParts, and RF64 chosen automatically past the threshold
|
||||
local big = Types.Audio.new(44100, 2, 3000)
|
||||
for i = 0, 3000 * 2 - 1 do
|
||||
buffer.writef32(big.samples, i * 4, ((i * 37) % 200 - 100) / 128)
|
||||
end
|
||||
local parts = Wav.encodeParts(big, { rf64Threshold = 1000 }) :: { buffer }
|
||||
t:check(#parts >= 2, "encodeParts: pieces")
|
||||
local w = Bytes.writer()
|
||||
for _, p in parts do
|
||||
w:bytes(p)
|
||||
end
|
||||
local joined = w:finish()
|
||||
t:eq(buffer.readstring(joined, 0, 4), "RF64", "auto RF64 past the threshold")
|
||||
local rd = Wav.open(Bytes.reader(joined)) :: any
|
||||
t:check(rd.container == "RF64" and rd.frames == 3000, "RF64 reads back")
|
||||
t:check(buffer.tostring(rd:read(3000)) == buffer.tostring(Wav.decode(Bytes.reader(Wav.encode(big) :: buffer)).samples), "RF64 samples")
|
||||
t:eq(buffer.readstring(Wav.encode(big) :: buffer, 0, 4), "RIFF", "plain RIFF below the threshold")
|
||||
-- bad arguments
|
||||
local _, e1 = Wav.encode({} :: any)
|
||||
t:check(e1 and e1.code == Err.ARG, "encode {}: " .. tostring(e1))
|
||||
local _, e2 = Wav.encode(a, { sampleFormat = "s12" } :: any)
|
||||
t:check(e2 and e2.code == Err.ARG, "bad sampleFormat")
|
||||
local bad = Types.Audio.new(8000, 2, 10)
|
||||
bad.frames = 99
|
||||
local _, e3 = Wav.encode(bad)
|
||||
t:check(e3 and e3.code == Err.ARG, "short samples buffer")
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ api
|
||||
do
|
||||
local c = Enc[1]
|
||||
local a, err = F.audio.load(c.file)
|
||||
t:check(a and a.kind == "audio" and a.frames == c.frames, "F.audio.load: " .. tostring(err))
|
||||
local dec = F.audio.open(c.file)
|
||||
t:check(dec and dec.sampleRate == c.rate and type(dec.read) == "function", "F.audio.open")
|
||||
local out = F.save(a, nil, { format = "wav", sampleFormat = "s16" })
|
||||
t:check(out and buffer.tostring(out) == c.file, "F.save wav")
|
||||
t:eq(F.detect(c.file), "wav", "F.detect")
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ fuzz
|
||||
t:fuzz("open + read + seek", function(b: buffer)
|
||||
local dec = Wav.open(Bytes.reader(b))
|
||||
if dec then
|
||||
for _ = 1, 100 do
|
||||
local _, n = dec:read(1000)
|
||||
if n == 0 then
|
||||
break
|
||||
end
|
||||
end
|
||||
dec:seek(dec.duration / 2)
|
||||
dec:read(17)
|
||||
end
|
||||
return dec
|
||||
end, samples, { mutations = 12 })
|
||||
t:fuzz("header mutations", function(b: buffer)
|
||||
return Wav.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 6, garbage = 50, keepHeader = 0 })
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,114 @@
|
||||
-- containers/Riff tests: the writer (RIFF, RIFX, forced RF64 with ds64, LIST/INFO, odd pad bytes, parts) read back by
|
||||
-- the reader; lenient reading (missing pad byte, truncated chunk, open-ended data of size 0 / 0xFFFFFFFF, the ds64
|
||||
-- table, a sub-range Reader); errors; the fuzz pass. The WAVE form is checked against ffmpeg in tests/audio/wav.luau.
|
||||
local T = require("@tests/T")
|
||||
local Riff = require("@formats/containers/Riff")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
|
||||
local t = T.suite("riff")
|
||||
|
||||
local function u32(v: number): string
|
||||
local b = buffer.create(4)
|
||||
buffer.writeu32(b, 0, v)
|
||||
return buffer.tostring(b)
|
||||
end
|
||||
|
||||
-- writer -> reader
|
||||
local samples = {}
|
||||
for _, mode in { "riff", "rifx", "rf64" } do
|
||||
local w = Riff.writer("TEST", { bigEndian = mode == "rifx", rf64 = mode == "rf64", sampleCount = 77 })
|
||||
w:chunk("abcd", "odd")
|
||||
w:chunk("efgh", buffer.fromstring("xxevenxx"), 2, 4)
|
||||
w:list("INFO", { { "INAM", "name" }, { "IART", buffer.fromstring("raw") } })
|
||||
w:chunk("data", { buffer.fromstring("12345"), buffer.fromstring("678") })
|
||||
local file = w:finish() :: buffer
|
||||
table.insert(samples, buffer.tostring(file))
|
||||
local f, err = Riff.read(Bytes.reader(file))
|
||||
t:check(f, mode .. ": read: " .. tostring(err))
|
||||
t:eq(f.container, if mode == "rifx" then "RIFX" elseif mode == "rf64" then "RF64" else "RIFF", mode .. ": container")
|
||||
t:eq(f.form, "TEST", mode .. ": form")
|
||||
t:eq(f.size, buffer.len(file) - 8, mode .. ": RIFF size")
|
||||
local ids = {}
|
||||
for _, c in f.chunks do
|
||||
table.insert(ids, c.id)
|
||||
end
|
||||
t:eq(table.concat(ids, ","), if mode == "rf64" then "ds64,abcd,efgh,LIST,data" else "abcd,efgh,LIST,data", mode .. ": chunks")
|
||||
local a = Riff.find(f, "abcd") :: any
|
||||
t:check(a.size == 3 and buffer.readstring(file, a.offset, 3) == "odd", mode .. ": odd chunk")
|
||||
local e = Riff.find(f, "efgh") :: any
|
||||
t:eq(buffer.readstring(file, e.offset, e.size), "even", mode .. ": range chunk")
|
||||
local l = Riff.find(f, "LIST") :: any
|
||||
t:eq(Riff.listType(f, l), "INFO", mode .. ": list type")
|
||||
local subs = Riff.list(f, l)
|
||||
t:check(#subs == 2 and subs[1].id == "INAM" and buffer.readstring(file, subs[1].offset, subs[1].size) == "name\0", mode .. ": INFO item")
|
||||
t:check(subs[2].size == 3 and subs[2].avail == 3, mode .. ": raw item")
|
||||
local d = Riff.find(f, "data") :: any
|
||||
t:check(d.size == 8 and buffer.readstring(file, d.offset, 8) == "12345678", mode .. ": data parts")
|
||||
t:eq(buffer.tostring(Riff.body(f, d):bytes(8)), "12345678", mode .. ": body reader")
|
||||
if mode == "rf64" then
|
||||
t:check(f.ds64 and f.ds64.dataSize == 8 and f.ds64.sampleCount == 77 and d.declared == 0xFFFFFFFF, "rf64: ds64")
|
||||
end
|
||||
-- parts: the same bytes
|
||||
local parts = w:finish({ parts = true }) :: { buffer }
|
||||
local j = Bytes.writer()
|
||||
for _, p in parts do
|
||||
j:bytes(p)
|
||||
end
|
||||
t:check(buffer.tostring(j:finish()) == buffer.tostring(file), mode .. ": parts join to the file")
|
||||
end
|
||||
t:check(not Riff.writer("WAVE", { bigEndian = true, rf64 = true }):finish(), "RIFX RF64 refused")
|
||||
|
||||
-- lenient reading
|
||||
do
|
||||
-- a missing pad byte after an odd chunk
|
||||
local body = "WAVE" .. "abcd" .. u32(5) .. "12345" .. "next" .. u32(2) .. "ok"
|
||||
local f = Riff.read(buffer.fromstring("RIFF" .. u32(#body) .. body)) :: any
|
||||
t:check(#f.chunks == 2 and f.chunks[2].id == "next", "missing pad byte detected")
|
||||
-- truncated
|
||||
local tb = "WAVE" .. "data" .. u32(100) .. "short"
|
||||
local ft = Riff.read(buffer.fromstring("RIFF" .. u32(#tb) .. tb)) :: any
|
||||
t:check(ft.chunks[1].truncated and ft.chunks[1].avail == 5 and ft.chunks[1].size == 100, "truncated chunk")
|
||||
-- open-ended
|
||||
for _, sz in { 0, 0xFFFFFFFF } do
|
||||
local ob = "WAVE" .. "data" .. u32(sz) .. "abcdefLIST"
|
||||
local fo = Riff.read(buffer.fromstring("RIFF" .. u32(0) .. ob), { openEnded = { data = true } }) :: any
|
||||
t:check(#fo.chunks == 1 and fo.chunks[1].streamed and fo.chunks[1].size == 10, "open-ended data " .. sz)
|
||||
end
|
||||
-- ds64 table
|
||||
local ds = "ds64" .. u32(40) .. u32(0) .. u32(0) .. u32(4) .. u32(0) .. u32(0) .. u32(0) .. u32(1) .. "big " .. u32(3) .. u32(0)
|
||||
local rb = "WAVE" .. ds .. "big " .. u32(0xFFFFFFFF) .. "xyz" .. "\0"
|
||||
local fr = Riff.read(buffer.fromstring("RF64" .. u32(0xFFFFFFFF) .. rb)) :: any
|
||||
t:check(fr.ds64 and fr.ds64.table["big "] == 3 and fr.chunks[2].size == 3, "ds64 table")
|
||||
-- a sub-range reader
|
||||
local s = samples[1]
|
||||
local big = buffer.create(#s + 7)
|
||||
buffer.writestring(big, 3, s)
|
||||
local fs = Riff.read(Bytes.reader(big, 3, #s)) :: any
|
||||
t:check(fs and fs.form == "TEST" and #fs.chunks == 4 and fs.base == 3, "sub-range reader")
|
||||
t:eq(Riff.sniff(Bytes.reader(buffer.fromstring(s))), "TEST", "sniff")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local _, e1 = Riff.read(buffer.fromstring("RIFF"))
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "too short")
|
||||
local _, e2 = Riff.read(buffer.fromstring("OggS00000000"))
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "not RIFF")
|
||||
local _, e3 = Riff.read(42 :: any)
|
||||
t:check(e3 and e3.code == Err.ARG, "not a buffer")
|
||||
end
|
||||
|
||||
t:fuzz("read + list", function(b: buffer)
|
||||
local f = Riff.read(Bytes.reader(b), { openEnded = { data = true } })
|
||||
if f then
|
||||
for _, c in f.chunks do
|
||||
if c.id == "LIST" then
|
||||
Riff.list(f, c)
|
||||
end
|
||||
end
|
||||
end
|
||||
return f
|
||||
end, samples, { mutations = 30 })
|
||||
|
||||
t:done()
|
||||
Reference in new issue
Block a user