codecs/audio: comments to specs only

This commit is contained in:
4DBug committed 2026-11-01 01:55:39 -05:00
1 parent 0bd7b87d2b
commit 6dcce481bb
69 files changed
+523 -1604

No files matched your search

+11 -26
View File
@@ -1,9 +1,7 @@
--!strict
-- codecs/audio/MidiRender: the tempo map (ticks -> seconds, as python mido computes lengths), the time-ordered event list,
-- and the audio Decoder that plays a Midi through codecs/audio/SoundFont's synth (a SoundFont bank, or the built-in
-- waveform set). Format 0 / 1: one tempo map from every track's tempo events, tracks merged (ties keep track order).
-- Format 2: each track is its own song with its own tempo map, played one after another. Shared by every format that
-- decodes to a Types.Midi (music/midi, mus, xmi, hmp): attach() adds the meta summary and the render method.
-- codecs/audio/MidiRender: the MIDI tempo map, event timeline and the Decoder that plays a Midi through SoundFont.
-- Lengths as python mido computes them. Format 0 / 1: one tempo map from every track, tracks merged (ties keep track
-- order); format 2: each track is its own song, played one after another.
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local SoundFont = require("@rosetta/codecs/audio/SoundFont")
@@ -23,7 +21,6 @@ local function tempoOf(ev: any): number?
return nil
end
-- tempoMap(midi, track?) -> TempoMap: from every track (format 0 / 1) or from one track (format 2)
function Render.tempoMap(midi: Types.Midi, track: number?): TempoMap
local changes: { { number } } = {}
for ti, t in midi.tracks do
@@ -62,7 +59,6 @@ function Render.tempoMap(midi: Types.Midi, track: number?): TempoMap
return map
end
-- seconds(map, tick) -> seconds (PPQ through the tempo map; SMPTE divisions are fixed time)
function Render.seconds(map: TempoMap, tick: number): number
local div = map.division
if div < 0 then
@@ -74,7 +70,6 @@ function Render.seconds(map: TempoMap, tick: number): number
return tick / ((if fps == 29 then 29.97 else fps) * tpf)
end
local ticks = map.ticks
-- binary search for the last change at or before tick
local lo, hi = 1, #ticks
while lo < hi do
local mid = (lo + hi + 1) // 2
@@ -87,7 +82,6 @@ function Render.seconds(map: TempoMap, tick: number): number
return map.secs[lo] + (tick - ticks[lo]) * map.us[lo] * 1e-6 / div
end
-- length(midi) -> seconds to the last event (format 2: the tracks one after another)
function Render.length(midi: Types.Midi): number
if #midi.tracks == 0 then
return 0
@@ -116,7 +110,6 @@ end
export type Timeline = { time: { number }, status: { number }, d1: { number }, d2: { number }, length: number }
-- timeline(midi) -> the channel messages in play order with their times in seconds
function Render.timeline(midi: Types.Midi): Timeline
local items: { { any } } = {}
local offset = 0
@@ -162,14 +155,13 @@ function Render.timeline(midi: Types.Midi): Timeline
end
export type RenderOptions = {
sampleRate: number?, -- default 44100
soundfont: any?, -- a SoundFont (music/sf2 decode, codecs/audio/SoundFont.parse) or SF2 bytes (buffer / string); default the built-in waveforms
gain: number?, -- linear output gain, default 0.5
tail: number?, -- seconds rendered after the last event (release tails), default 1
maxVoices: number?, -- default 256
sampleRate: number?,
soundfont: any?,
gain: number?,
tail: number?,
maxVoices: number?,
}
-- decoder(midi, opts?) -> Types.Decoder | nil, Err
function Render.decoder(midi: Types.Midi, opts: RenderOptions?): (Types.Decoder?, Err.Err?)
if type(midi) ~= "table" or type(midi.tracks) ~= "table" or type(midi.division) ~= "number" or midi.division == 0 then
return nil, Err.new(Err.ARG, "render: needs a Midi value")
@@ -267,19 +259,16 @@ 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)
-- tempo meta making each tick last 1 / ticksPerSecond s at that division (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.
-- XMIDI / HMI note durations: 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
@@ -305,11 +294,9 @@ function Render.merge(events: { Types.MidiEvent }, offs: { PendingOff }): { Type
return out
end
-- ------------------------------------------------------------------------------------------------ attach
local function render(self: Types.Midi, opts: RenderOptions?): Types.Decoder
local dec, err = Render.decoder(self, opts)
if not dec then
-- the Types contract returns a Decoder; a failed render is an empty one (Midi.render returns the Err)
local empty: any = {
sampleRate = 44100, channels = 2, frames = 0, duration = 0, bitrate = 0, tags = {}, err = err,
read = function(_self: any, _n: number, out: buffer?): (buffer, number)
@@ -325,7 +312,6 @@ local function render(self: Types.Midi, opts: RenderOptions?): Types.Decoder
return dec
end
-- summarize(midi): meta.tempos / timeSignatures / keySignatures / length from the events
function Render.summarize(midi: Types.Midi)
local tempos, sigs, keys = {}, {}, {}
local map = Render.tempoMap(midi)
@@ -349,7 +335,6 @@ function Render.summarize(midi: Types.Midi)
midi.meta.length = Render.length(midi)
end
-- attach(midi, source): what every MIDI-like decoder does last: the meta summary, meta.source and the render method
function Render.attach(midi: Types.Midi, source: { [string]: any })
Render.summarize(midi)
midi.meta.source = source