73 lines
2.6 KiB
Luau
73 lines
2.6 KiB
Luau
-- music/xmi tests: XMIDI files against wildmidi -x's conversion of song 1 (120 Hz).
|
|
-- Fixtures: tools/fixtures/xmi.py.
|
|
local T = require("@tests/T")
|
|
local V = require("@tests/music/_midivar")
|
|
local Xmi = require("@rosetta/music/xmi")
|
|
local Midi = require("@rosetta/music/midi")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Cases = require("@tests/gen/xmi_cases")
|
|
|
|
local t = T.suite("xmi")
|
|
|
|
local function decode(s: string, opts: any?): (any, any)
|
|
return Xmi.decode(Bytes.reader(buffer.fromstring(s)), opts)
|
|
end
|
|
|
|
t:eq(Xmi.name, "xmi", "name")
|
|
t:check(Xmi.caps.decode and not Xmi.caps.encode, "caps")
|
|
t:eq(Xmi.sniff(Bytes.reader(buffer.fromstring("FORM\0\0\0\0XDIR"))), 1, "sniff XDIR")
|
|
t:eq(Xmi.sniff(Bytes.reader(buffer.fromstring("FORM\0\0\0\0AIFF"))), 0, "sniff AIFF")
|
|
|
|
local samples = {}
|
|
for _, c in Cases do
|
|
local m, err = decode(c.data)
|
|
t:check(m, c.name .. ": " .. tostring(err))
|
|
if m then
|
|
t:eq(m.meta.source.songs, c.songs, c.name .. ": songs")
|
|
t:eq(m.division, 60, c.name .. ": division")
|
|
V.compare(t, m, c.ref, c.name, { hangingOffs = true })
|
|
if c.songs > 1 then
|
|
local s2 = assert(decode(c.data, { track = 2 }))
|
|
t:eq(s2.meta.source.song, 2, c.name .. ": song 2")
|
|
t:check(#s2.tracks[1].events ~= #m.tracks[1].events, c.name .. ": song 2 differs")
|
|
local _, e = decode(c.data, { track = 3 })
|
|
t:eq(e and e.code, Err.ARG, c.name .. ": no song 3")
|
|
end
|
|
-- export to a Standard MIDI File and back: the same timeline
|
|
local smf = assert(Midi.encode(m))
|
|
local back = assert(Midi.decode(Bytes.reader(smf)))
|
|
t:check(math.abs(back.meta.length - m.meta.length) < 1e-9, c.name .. ": SMF export keeps the timing")
|
|
end
|
|
table.insert(samples, buffer.fromstring(c.data))
|
|
end
|
|
|
|
do
|
|
local m = assert(decode(Cases[1].data))
|
|
local dec = m:render({ sampleRate = 22050, tail = 0.2 })
|
|
t:check(math.abs(dec.duration - (m.meta.length + 0.2)) < 0.01, "render duration")
|
|
local buf, n = dec:read(dec.frames)
|
|
local peak = 0
|
|
for i = 0, n * 2 - 1 do
|
|
peak = math.max(peak, math.abs(buffer.readf32(buf, i * 4)))
|
|
end
|
|
t:check(peak > 0.01, "render makes sound")
|
|
end
|
|
|
|
local _, e1 = decode("FORM\0\0\0\4XDIR")
|
|
t:eq(e1 and e1.code, Err.CORRUPT, "no songs")
|
|
local _, e2 = decode("FORM\0\0\0\14XMIDEVNT\0\0\0\2\144\60")
|
|
t:eq(e2 and e2.code, Err.TRUNCATED, "truncated note")
|
|
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Xmi.decode(Bytes.reader(b))
|
|
end, samples)
|
|
t:fuzz("decode + render", function(b: buffer)
|
|
local m = Xmi.decode(Bytes.reader(b))
|
|
if m and m.meta.length < 30 then
|
|
m:render({ sampleRate = 8000 }):read(2000)
|
|
end
|
|
return m
|
|
end, samples, { mutations = 60, garbage = 10 })
|
|
t:done()
|