89 lines
2.7 KiB
Luau
89 lines
2.7 KiB
Luau
-- tests/music/_midivar: shared helper of the MUS / XMI / HMP tests: channel events against wildmidi -x's conversion
|
|
-- (tools/fixtures/_midivar.py), matched per (kind, channel, data) in time order, within 0.1 ms + 1e-5 relative.
|
|
local Render = require("@rosetta/codecs/audio/MidiRender")
|
|
|
|
local M = {}
|
|
|
|
local function kindOf(status: number, d1: number, d2: number): string?
|
|
local hi = status // 16
|
|
if hi == 9 and d2 > 0 then
|
|
return "on"
|
|
elseif hi == 8 or hi == 9 then
|
|
return "off"
|
|
elseif hi == 12 then
|
|
return "program"
|
|
elseif hi == 14 then
|
|
return "bend"
|
|
elseif hi == 11 then
|
|
return "cc"
|
|
end
|
|
return nil
|
|
end
|
|
|
|
local function index(list: { { number } }, opts: any): ({ [string]: { number } }, { [string]: number })
|
|
local by: { [string]: { number } }, counts: { [string]: number } = {}, {}
|
|
for _, e in list do
|
|
local k = kindOf(e[2], e[3], e[4])
|
|
if k and not (opts.skip and opts.skip[k]) then
|
|
local d = if k == "bend" then e[4] elseif k == "cc" then e[3] * 256 + e[4] else e[3]
|
|
local key = string.format("%s %d %d", k, e[2] % 16, d)
|
|
by[key] = by[key] or {}
|
|
table.insert(by[key], e[1])
|
|
counts[k] = (counts[k] or 0) + 1
|
|
end
|
|
end
|
|
return by, counts
|
|
end
|
|
|
|
function M.compare(t: any, midi: any, ref: any, label: string, opts: any?): number
|
|
local o = opts or {}
|
|
local tl = Render.timeline(midi)
|
|
local ours = {}
|
|
local refEnd = 0
|
|
for _, e in ref.events do
|
|
refEnd = math.max(refEnd, e[1])
|
|
end
|
|
for i = 1, #tl.time do
|
|
-- o.hangingOffs: wildmidi drops the note-offs past the end of the song (we end those notes at the end)
|
|
local hanging = o.hangingOffs and kindOf(tl.status[i], tl.d1[i], tl.d2[i]) == "off" and tl.time[i] > refEnd + 1e-6
|
|
if not hanging then
|
|
table.insert(ours, { tl.time[i], tl.status[i], tl.d1[i], tl.d2[i] })
|
|
end
|
|
end
|
|
local a, ca = index(ours, o)
|
|
local b, cb = index(ref.events, o)
|
|
for _, k in { "on", "off", "program", "bend", "cc" } do
|
|
if not (o.skip and o.skip[k]) then
|
|
t:eq(ca[k] or 0, cb[k] or 0, label .. ": " .. k .. " events")
|
|
end
|
|
end
|
|
local worst, bad = 0, 0
|
|
for key, times in b do
|
|
local mine = a[key] or {}
|
|
table.sort(times)
|
|
table.sort(mine)
|
|
if #mine ~= #times then
|
|
bad += 1
|
|
if bad <= 3 then
|
|
t:check(false, string.format("%s: %s: %d events, wildmidi %d", label, key, #mine, #times))
|
|
end
|
|
else
|
|
for i, tm in times do
|
|
local d = math.abs(mine[i] - tm)
|
|
worst = math.max(worst, d)
|
|
if d > 1e-4 + 1e-5 * tm then
|
|
bad += 1
|
|
if bad <= 3 then
|
|
t:check(false, string.format("%s: %s at %.4f s, wildmidi %.4f s", label, key, mine[i], tm))
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
t:check(bad == 0, label .. ": event times match wildmidi's")
|
|
t:note("%-12s %4d notes, worst time difference %.2g s", label, ca.on or 0, worst)
|
|
return worst
|
|
end
|
|
|
|
return M
|