ws-audio2: MUS, XMI, HMP / HMI (music/mus, music/xmi, music/hmp) -> Types.Midi through codecs/audio/MidiRender (merge, tempoBytes); event streams match wildmidi -x
This commit is contained in:
1 parent
848e4bfff6
commit
a3e234cc0d
14 files changed
+1439
No files matched your search
@@ -0,0 +1,92 @@
|
||||
-- tests/music/_midivar: shared helper of the MUS / XMI / HMP tests (not a test itself): compares a decoded Midi's
|
||||
-- channel events (codecs/audio/MidiRender.timeline: play order, seconds) with wildmidi -x's conversion
|
||||
-- (tools/fixtures/_midivar.py): notes on / off, program changes, pitch bends (MSB) and controllers, matched per
|
||||
-- (kind, channel, data) in time order, times within 0.1 ms + 1e-5 relative.
|
||||
local Render = require("@formats/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
|
||||
|
||||
-- the comparable events of a list of { seconds, status, d1, d2 }: key -> { seconds }
|
||||
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
|
||||
|
||||
-- compare(t, midi, ref, label, opts?) -> worst time difference (s). opts.skip = { cc = true, ... }
|
||||
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
|
||||
@@ -0,0 +1,57 @@
|
||||
-- music/hmp tests: HMP (old and "013195" headers, several tick rates) and HMI files (tools/fixtures/hmp.py) decoded and
|
||||
-- compared with wildmidi -x's conversion (notes, HMI note durations, programs, bends, controllers), the FE / loop
|
||||
-- events skipped, a render, bad input, and the fuzz no-throw pass.
|
||||
local T = require("@tests/T")
|
||||
local V = require("@tests/music/_midivar")
|
||||
local Hmp = require("@formats/music/hmp")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Cases = require("@tests/gen/hmp_cases")
|
||||
|
||||
local t = T.suite("hmp")
|
||||
|
||||
local function decode(s: string, opts: any?): (any, any)
|
||||
return Hmp.decode(Bytes.reader(buffer.fromstring(s)), opts)
|
||||
end
|
||||
|
||||
t:eq(Hmp.name, "hmp", "name")
|
||||
t:check(Hmp.caps.decode and not Hmp.caps.encode, "caps")
|
||||
t:eq(Hmp.sniff(Bytes.reader(buffer.fromstring("HMIMIDIP013195"))), 1, "sniff HMP")
|
||||
t:eq(Hmp.sniff(Bytes.reader(buffer.fromstring("HMI-MIDISONG061595"))), 1, "sniff HMI")
|
||||
t:eq(Hmp.sniff(Bytes.reader(buffer.fromstring("HMI-MIDITRACK"))), 0, "sniff other")
|
||||
|
||||
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.format, c.kind, c.name .. ": kind")
|
||||
t:eq(m.meta.source.rate, c.rate, c.name .. ": tick rate")
|
||||
V.compare(t, m, c.ref, c.name)
|
||||
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")
|
||||
end
|
||||
|
||||
local _, e1 = decode("HMIMIDIP")
|
||||
t:eq(e1 and e1.code, Err.TRUNCATED, "short header")
|
||||
local bad = string.sub(Cases[1].data, 1, 0x308 + 20)
|
||||
local _, e2 = decode(bad)
|
||||
t:eq(e2 and e2.code, Err.TRUNCATED, "truncated chunk")
|
||||
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Hmp.decode(Bytes.reader(b))
|
||||
end, samples)
|
||||
t:fuzz("decode + render", function(b: buffer)
|
||||
local m = Hmp.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()
|
||||
@@ -0,0 +1,93 @@
|
||||
-- music/mus tests: Doom MUS files (tools/fixtures/mus.py) decoded and compared with wildmidi -x's conversion (notes,
|
||||
-- programs, pitch bend MSB, controllers; times at wildmidi's tick rate), system events / measure ends, the tick rate
|
||||
-- option, a render, bad input, and the fuzz no-throw pass.
|
||||
local T = require("@tests/T")
|
||||
local V = require("@tests/music/_midivar")
|
||||
local Mus = require("@formats/music/mus")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Render = require("@formats/codecs/audio/MidiRender")
|
||||
local Cases = require("@tests/gen/mus_cases")
|
||||
local Measures = require("@tests/gen/mus_measures")
|
||||
|
||||
local t = T.suite("mus")
|
||||
|
||||
local function decode(s: string, opts: any?): (any, any)
|
||||
return Mus.decode(Bytes.reader(buffer.fromstring(s)), opts)
|
||||
end
|
||||
|
||||
t:eq(Mus.name, "mus", "name")
|
||||
t:check(Mus.caps.decode and not Mus.caps.encode, "caps")
|
||||
t:eq(Mus.sniff(Bytes.reader(buffer.fromstring("MUS\x1a"))), 1, "sniff")
|
||||
t:eq(Mus.sniff(Bytes.reader(buffer.fromstring("MThd"))), 0, "sniff MThd")
|
||||
|
||||
local samples = {}
|
||||
for _, c in Cases do
|
||||
local m, err = decode(c.data, { rate = c.rate })
|
||||
t:check(m, c.name .. ": " .. tostring(err))
|
||||
if m then
|
||||
t:eq(m.kind, "midi", c.name .. ": kind")
|
||||
t:eq(m.meta.source.format, "mus", c.name .. ": source")
|
||||
t:eq(#m.meta.source.instruments, 3, c.name .. ": instrument list")
|
||||
V.compare(t, m, c.ref, c.name)
|
||||
-- Doom's 140 Hz: the same events, times scaled by rate / 140
|
||||
local d = assert(decode(c.data))
|
||||
t:check(math.abs(d.meta.length * 140 - m.meta.length * c.rate) < 1e-5 * d.meta.length * 140 + 1e-6, c.name .. ": 140 Hz default")
|
||||
end
|
||||
table.insert(samples, buffer.fromstring(c.data))
|
||||
end
|
||||
|
||||
-- system events and measure ends inserted with no delay: the same notes at the same times, more controllers
|
||||
do
|
||||
local p, q = assert(decode(Measures.plain)), assert(decode(Measures.marked))
|
||||
local a, b = Render.timeline(p), Render.timeline(q)
|
||||
local function notes(tl: any): { string }
|
||||
local out = {}
|
||||
for i = 1, #tl.time do
|
||||
if tl.status[i] // 16 == 8 or tl.status[i] // 16 == 9 then
|
||||
table.insert(out, string.format("%.6f %d %d %d", tl.time[i], tl.status[i], tl.d1[i], tl.d2[i]))
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
local na, nb = notes(a), notes(b)
|
||||
t:eq(#nb, #na, "measures: note count")
|
||||
t:eq(table.concat(nb, ","), table.concat(na, ","), "measures: same notes")
|
||||
t:check(#b.time > #a.time, "measures: system events become controllers")
|
||||
t:eq(q.meta.length, p.meta.length, "measures: same length")
|
||||
end
|
||||
|
||||
-- a render through the built-in waveforms
|
||||
do
|
||||
local m = assert(decode(Cases[1].data))
|
||||
local dec = m:render({ sampleRate = 22050, tail = 0.2 })
|
||||
t:check(dec.frames > 0 and 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
|
||||
|
||||
-- bad input
|
||||
local _, e1 = decode("MUS\x1a\1\0")
|
||||
t:eq(e1 and e1.code, Err.TRUNCATED, "short header")
|
||||
local _, e2 = decode("MUS\x1a\2\0\200\0\1\0\0\0\0\0\0\0")
|
||||
t:eq(e2 and e2.code, Err.TRUNCATED, "score past the end")
|
||||
local _, e3 = decode("MUS\x1a\2\0\16\0\1\0\0\0\0\0\0\0\112\0")
|
||||
t:eq(e3 and e3.code, Err.CORRUPT, "event type 7")
|
||||
local _, e4 = decode(Cases[1].data, { rate = 0 })
|
||||
t:eq(e4 and e4.code, Err.ARG, "bad rate")
|
||||
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Mus.decode(Bytes.reader(b))
|
||||
end, samples)
|
||||
t:fuzz("decode + render", function(b: buffer)
|
||||
local m = Mus.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()
|
||||
@@ -0,0 +1,73 @@
|
||||
-- music/xmi tests: XMIDI files (tools/fixtures/xmi.py: XDIR + CAT XMID with one or two songs, a bare FORM XMID, with
|
||||
-- and without TIMB) decoded and compared with wildmidi -x's conversion of song 1 (notes from durations, programs,
|
||||
-- bends, controllers; 120 Hz), song selection, export to SMF, a render, bad input, and the fuzz no-throw pass.
|
||||
local T = require("@tests/T")
|
||||
local V = require("@tests/music/_midivar")
|
||||
local Xmi = require("@formats/music/xmi")
|
||||
local Midi = require("@formats/music/midi")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/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()
|
||||
Reference in new issue
Block a user