-- music/hmp tests: HMP and HMI files against wildmidi -x's conversion. -- Fixtures: tools/fixtures/hmp.py. local T = require("@tests/T") local V = require("@tests/music/_midivar") local Hmp = require("@rosetta/music/hmp") local Bytes = require("@rosetta/core/Bytes") local Err = require("@rosetta/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()