Files

44 lines
2.0 KiB
Luau

-- tests/music/_hively: shared helper of the AHX / HVL tests: exact against hvl2wav (CRC-32 of the 16-bit stream).
-- Fixtures: tools/fixtures/ahx.py, hvl.py.
local Bytes = require("@rosetta/core/Bytes")
local Crc32 = require("@rosetta/core/Crc32")
local H = {}
function H.check(t: any, Format: any, c: any): any
local song, err = Format.decode(Bytes.reader(buffer.fromstring(c.data)))
t:check(song, c.name .. ": " .. tostring(err))
if not song then
return nil
end
t:check(Format.sniff(Bytes.reader(buffer.fromstring(c.data))) == 1, c.name .. ": sniff")
local dec = song:render({ sampleRate = c.sampleRate, stereo = c.stereo, subsong = c.subsong, maxDuration = c.maxDuration })
t:check(dec.err == nil, c.name .. ": render " .. tostring(dec.err))
local frames = if c.prefix then math.min(dec.frames, c.frames) else dec.frames
if not c.prefix then
t:eq(dec.frames, c.frames, c.name .. ": frames (song end as hvl2wav finds it)")
end
local f32 = dec:read(frames)
local s16 = buffer.create(frames * 4)
for i = 0, frames * 2 - 1 do
buffer.writei16(s16, i * 2, math.floor(buffer.readf32(f32, i * 4) * 32768 + 0.5))
end
-- (a time-limited reference whose song ends earlier than the limit: only the head can be compared)
local exact = (frames == c.frames and Crc32.of(s16) == c.crc) or (c.prefix and frames < c.frames)
local head = buffer.fromstring(c.head)
local firstDiff = -1
for i = 0, math.min(frames, buffer.len(head) // 4) * 2 - 1 do
if buffer.readi16(s16, i * 2) ~= buffer.readi16(head, i * 2) then
firstDiff = i // 2
break
end
end
t:check(firstDiff < 0, string.format("%s: first differing frame %d (ours %d, hvl2wav %d)", c.name, firstDiff,
if firstDiff >= 0 then buffer.readi16(s16, firstDiff * 4) else 0, if firstDiff >= 0 then buffer.readi16(head, firstDiff * 4) else 0))
t:check(exact, c.name .. ": samples equal hvl2wav's (CRC-32 of " .. frames .. " frames)")
t:note("%-22s %6d frames %s", c.name, frames, if exact and firstDiff < 0 then "bit-exact vs hvl2wav" else "DIFFERS")
return song
end
return H