Files

76 lines
2.7 KiB
Luau

-- music/ahx tests: AHX songs (tools/fixtures/_hively.py, karma.ahx) rendered by codecs/audio/Hively vs hvl2wav.
-- Fixtures: tools/fixtures/ahx.py.
local T = require("@tests/T")
local H = require("@tests/music/_hively")
local Ahx = require("@rosetta/music/ahx")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Cases = require("@tests/gen/ahx_cases")
local t = T.suite("ahx")
t:eq(Ahx.name, "ahx", "name")
t:eq(Ahx.category, "music", "category")
t:check(Ahx.caps.decode and not Ahx.caps.encode, "caps")
t:eq(Ahx.sniff(Bytes.reader(buffer.fromstring("HVL\1"))), 0, "sniff HVL")
local samples = {}
for _, c in Cases do
local song = H.check(t, Ahx, c)
if song and c.name == "demo" then
t:eq(song.kind, "music", "kind")
t:eq(song.format, "ahx", "format")
t:eq(song.title, "demo", "title")
t:eq(song.channels, 4, "channels")
t:eq(#song.order, 2, "positions")
t:eq(song.instruments[1].name, "tri", "instrument name")
t:eq(#song.instruments[3].plist.entries, 4, "play list")
t:eq(song.patterns[2].rows, 16, "track rows")
t:eq(buffer.readu8(song.patterns[2].data, 0), 25, "track 1 row 0 note")
t:eq(#song.subsongs, 1, "subsongs")
local dec = song:render()
local a = dec:read(4410)
dec:seek(0.5)
dec:seek(0)
local b = dec:read(4410)
t:check(buffer.tostring(a) == buffer.tostring(b), "seek restarts exactly")
end
if #samples < 4 then
table.insert(samples, buffer.fromstring(c.data))
end
end
local function code(s: string): string?
local _, e = Ahx.decode(Bytes.reader(buffer.fromstring(s)))
return e and e.code
end
t:eq(code("THX"), Err.TRUNCATED, "short header")
t:eq(code("THX\1\0\14\0\0\0\0\16\0\0\0"), Err.CORRUPT, "no positions")
t:eq(code(string.sub(Cases[1].data, 1, 60)), Err.TRUNCATED, "truncated")
local song = assert(Ahx.decode(Bytes.reader(buffer.fromstring(Cases[1].data))))
t:check(song:render({ subsong = 9 }).err ~= nil, "subsong out of range")
-- through the api: a Module saved as audio renders through its own engine
do
local F = require("@rosetta")
local wav, err = F.save(song, nil, { format = "wav", sampleRate = 22050 })
t:check(wav, "F.save as wav: " .. tostring(err))
t:eq(F.detect(wav :: buffer), "wav", "F.save as wav")
t:eq(F.detect(buffer.fromstring(Cases[1].data)), "ahx", "F.detect")
local _, e2 = F.save(song, nil, { format = "wav", subsong = 7 })
t:check(e2 ~= nil, "F.save: a failed render is an error")
end
t:fuzz("decode", function(b: buffer)
return Ahx.decode(Bytes.reader(b))
end, samples)
t:fuzz("decode + render", function(b: buffer)
local s = Ahx.decode(Bytes.reader(b))
if s then
local d = s:render({ sampleRate = 8000, maxDuration = 0.5 })
d:read(4000)
end
return s
end, samples, { mutations = 150, garbage = 20 })
t:done()