diff --git a/src/Formats/Types.luau b/src/Formats/Types.luau index 9ec4443..998576b 100644 --- a/src/Formats/Types.luau +++ b/src/Formats/Types.luau @@ -138,7 +138,7 @@ Types.Audio = Audio export type Module = { kind: "music", title: string, - format: string, -- "mod", "s3m", "xm", "it", "mid" + format: string, -- "mod", "s3m", "xm", "it" (codecs/audio/Tracker); "ahx", "hvl" (codecs/audio/Hively, own render) channels: number, patterns: { any }, order: { number }, diff --git a/src/Formats/codecs/audio/Hively/Load.luau b/src/Formats/codecs/audio/Hively/Load.luau index 0becae4..342fb09 100644 --- a/src/Formats/codecs/audio/Hively/Load.luau +++ b/src/Formats/codecs/audio/Hively/Load.luau @@ -66,7 +66,7 @@ function Load.parse(b: buffer, base: number, limit: number): (Tune?, string?, st local magic = buffer.readstring(b, base, 3) local ahx = magic == "THX" and u8(3) < 3 if not ahx and not (magic == "HVL" and u8(3) <= 1) then - return nil, "not an AHX / HVL module" + return nil, "not an AHX / HVL module", nil end if not ahx and size < 16 then return nil, "truncated header", "truncated" @@ -94,7 +94,7 @@ function Load.parse(b: buffer, base: number, limit: number): (Tune?, string?, st emptyTrack0 = bit32.band(u8(6), 0x80) ~= 0, } if posn == 0 or posn > 1000 or trkl > 64 or trkl == 0 or insn > 64 then - return nil, string.format("bad header (positions %d, track length %d, instruments %d)", posn, trkl, insn) + return nil, string.format("bad header (positions %d, track length %d, instruments %d)", posn, trkl, insn), nil end if chnn > 16 then return nil, "unsupported: more than 16 channels", "unsupported" diff --git a/src/Formats/codecs/audio/Hively/Player.luau b/src/Formats/codecs/audio/Hively/Player.luau index b639be6..4f9be2a 100644 --- a/src/Formats/codecs/audio/Hively/Player.luau +++ b/src/Formats/codecs/audio/Hively/Player.luau @@ -668,7 +668,8 @@ local function processFrame(st: State, v: Voice) d3 += v.squareSign v.squarePos = s16(d3) v.plantSquare = true - v.squareWait = if v.instrument then v.instrument.squareSpeed else 0 + local ins: any = v.instrument + v.squareWait = if ins then ins.squareSpeed else 0 end end @@ -815,12 +816,9 @@ local function setAudio(st: State, v: Voice) v.plantPeriod = false v.delta = deltaOf(v.audioPeriod, st.freq) end - if v.newWaveform and v.audioSrc then - if v.waveform == 3 then - fillCycles(v.voiceBuf, v.audioSrc, v.audioOff, 0x280) - else - fillCycles(v.voiceBuf, v.audioSrc, v.audioOff, 4 * 2 ^ v.waveLength) - end + local src: buffer? = v.audioSrc or nil + if v.newWaveform and src then + fillCycles(v.voiceBuf, src, v.audioOff, if v.waveform == 3 then 0x280 else 4 * 2 ^ v.waveLength) end if v.ringPlantPeriod then v.ringPlantPeriod = false diff --git a/src/Formats/codecs/audio/MidiRender.luau b/src/Formats/codecs/audio/MidiRender.luau index e2ed3df..c0c31e7 100644 --- a/src/Formats/codecs/audio/MidiRender.luau +++ b/src/Formats/codecs/audio/MidiRender.luau @@ -290,7 +290,7 @@ function Render.merge(events: { Types.MidiEvent }, offs: { PendingOff }): { Type end return a.seq < b.seq end) - local out: { Types.MidiEvent } = table.create(#events + #offs) + local out: { Types.MidiEvent } = table.create(#events + #offs) :: { Types.MidiEvent } 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 diff --git a/src/Formats/music/hmp/init.luau b/src/Formats/music/hmp/init.luau index 7645f67..b93ddc8 100644 --- a/src/Formats/music/hmp/init.luau +++ b/src/Formats/music/hmp/init.luau @@ -70,7 +70,7 @@ local function delta(b: buffer, p: number, e: number, hmp: boolean): (number?, n end local function bytes(b: buffer, p: number, n: number): { number } - local t = table.create(n) + local t: { number } = table.create(n) :: { number } for i = 1, n do t[i] = readu8(b, p + i - 1) end @@ -176,7 +176,7 @@ local function hmpTracks(b: buffer, base: number, limit: number): ({ { number } local n = readu32(b, base + 0x30) local p = base + (if new then 0x388 else 0x308) if n == 0 or n > 256 then - return nil, string.format("bad track count %d", n) + return nil, string.format("bad track count %d", n), nil end local ranges = {} for _ = 1, n do @@ -196,7 +196,7 @@ end local function hmiTracks(b: buffer, base: number, limit: number): ({ { number } }?, string?, string?) local n = readu8(b, base + 228) if n == 0 then - return nil, "no tracks" + return nil, "no tracks", nil end if base + 370 + n * 4 > limit then return nil, "truncated track table", "truncated" @@ -205,7 +205,7 @@ local function hmiTracks(b: buffer, base: number, limit: number): ({ { number } for i = 0, n - 1 do local at = base + readu32(b, base + 370 + i * 4) if at < base or at + 0x5B > limit or buffer.readstring(b, at, 13) ~= "HMI-MIDITRACK" then - return nil, string.format("bad track %d header", i) + return nil, string.format("bad track %d header", i), nil end local start = at + readu32(b, at + 0x57) if start >= limit then diff --git a/src/Formats/music/midi/init.luau b/src/Formats/music/midi/init.luau index aa8cb1f..4bbaad9 100644 --- a/src/Formats/music/midi/init.luau +++ b/src/Formats/music/midi/init.luau @@ -84,7 +84,7 @@ local function karaoke(midi: Types.Midi) if #lyrics == 0 and next(headers) == nil then return end - table.sort(lyrics, function(a, b): boolean + table.sort(lyrics, function(a: { time: number, tick: number, text: string }, b: { time: number, tick: number, text: string }): boolean return a.time < b.time end) midi.meta.lyrics = lyrics diff --git a/src/Formats/music/xmi/init.luau b/src/Formats/music/xmi/init.luau index 47bc433..0d18f74 100644 --- a/src/Formats/music/xmi/init.luau +++ b/src/Formats/music/xmi/init.luau @@ -63,7 +63,7 @@ local function vlq(b: buffer, p: number, e: number): (number?, number) end local function bytes(b: buffer, p: number, n: number): { number } - local t = table.create(n) + local t: { number } = table.create(n) :: { number } for i = 1, n do t[i] = readu8(b, p + i - 1) end @@ -192,10 +192,12 @@ function Xmi.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Midi?, Err.Er table.insert(timbres, { patch = readu8(r.buf, tb.offset + 2 + i * 2), bank = readu8(r.buf, tb.offset + 3 + i * 2) }) end end + local rb = song.RBRN + local branches = if rb and rb.avail >= 2 then buffer.readu16(r.buf, rb.offset) else 0 local midi = Types.Midi.new(60) midi.format = 0 midi.tracks = { { events = evs } } - Render.attach(midi, { format = "xmi", songs = #list, song = index, timbres = timbres, tempo = tempo, branches = if song.RBRN and song.RBRN.avail >= 2 then buffer.readu16(r.buf, song.RBRN.offset) else 0 }) + Render.attach(midi, { format = "xmi", songs = #list, song = index, timbres = timbres, tempo = tempo, branches = branches }) midi.meta.truncated = ev.truncated return midi, nil end diff --git a/tools/bench.luau b/tools/bench.luau index b32ee01..1320f67 100644 --- a/tools/bench.luau +++ b/tools/bench.luau @@ -1078,3 +1078,24 @@ do jitTune(false) end end +-- ws-audio2: chip/midi +do + -- AHX / HVL (codecs/audio/Hively): a whole song rendered at 44.1 kHz (tools/fixtures/ahx.py karma.ahx, 3 s; hvl.py + -- the 6-channel demo with ring modulation); MB/s of stereo f32 output, and how many times faster than real time + local Ahx, Hvl = require("@formats/music/ahx"), require("@formats/music/hvl") + local AC, HC = require("@tests/gen/ahx_cases"), require("@tests/gen/hvl_cases") + for _, spec in { { Ahx, AC, "karma.ahx", "ahx render karma (4 ch)" }, { Hvl, HC, "demo", "hvl render demo (6 ch)" } } do + for _, c in spec[2] do + if c.name == spec[3] then + local song = assert(spec[1].decode(Bytes.reader(buffer.fromstring(c.data)))) + local frames = 0 + local dt = bench(spec[4], 3, c.frames * 8, function() + local dec = song:render({ maxDuration = c.maxDuration }) + frames = dec.frames + dec:read(frames) + end) + print(string.format("%-8s %-30s %8.1f x real time", runtime, "", frames / 44100 / dt)) + end + end + end +end