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:
4DBug committed 2026-10-31 14:27:51 -05:00
1 parent 848e4bfff6
commit a3e234cc0d
14 files changed
+1439

No files matched your search

+38
View File
@@ -267,6 +267,44 @@ function Render.decoder(midi: Types.Midi, opts: RenderOptions?): (Types.Decoder?
return (dec :: any) :: Types.Decoder, nil
end
-- tempoBytes(division, ticksPerSecond) -> the 3 data bytes of a tempo meta event making each tick last
-- 1 / ticksPerSecond seconds at that division (for formats with a fixed tick rate: MUS, XMIDI, HMP / HMI)
function Render.tempoBytes(division: number, ticksPerSecond: number): { number }
local us = math.clamp(math.floor(division * 1e6 / ticksPerSecond + 0.5), 1, 0xFFFFFF)
return { us // 65536, (us // 256) % 256, us % 256 }
end
-- ------------------------------------------------------------------------------------------------ durations
export type PendingOff = { tick: number, onTick: number, ev: Types.MidiEvent }
-- merge(events, offs) -> one tick-ordered event list: for formats that store a duration with each note (XMIDI, HMI).
-- events are in file order (ticks never decrease); offs are the generated note-offs. At equal ticks an off whose note
-- started earlier goes before the file's events, an off of a zero-length note after them.
function Render.merge(events: { Types.MidiEvent }, offs: { PendingOff }): { Types.MidiEvent }
for i, o in offs do
(o :: any).seq = i
end
table.sort(offs, function(a: any, b: any): boolean
if a.tick ~= b.tick then
return a.tick < b.tick
end
return a.seq < b.seq
end)
local out: { Types.MidiEvent } = table.create(#events + #offs)
local k = 1
for _, ev in events do
while k <= #offs and (offs[k].tick < ev.tick or (offs[k].tick == ev.tick and offs[k].onTick < ev.tick)) do
table.insert(out, offs[k].ev)
k += 1
end
table.insert(out, ev)
end
for j = k, #offs do
table.insert(out, offs[j].ev)
end
return out
end
-- ------------------------------------------------------------------------------------------------ attach
local function render(self: Types.Midi, opts: RenderOptions?): Types.Decoder
local dec, err = Render.decoder(self, opts)