428 lines
13 KiB
Luau
428 lines
13 KiB
Luau
--!strict
|
|
-- music/it: Impulse Tracker modules (IMPM, ITTECH.TXT; also files from OpenMPT / Schism Tracker). Reads into the
|
|
-- codecs/Tracker Module and plays through its engine (Module:render() / open() -> an audio Decoder). Supports: up to
|
|
-- 64 channels, instrument and sample mode, new and old (pre-2.0) instrument headers, volume / panning / pitch (or
|
|
-- filter) envelopes with loops and sustain loops, new note actions and duplicate checks, pitch-pan separation,
|
|
-- resonant filters (instrument cutoff / resonance, default Zxx macros), 8 / 16-bit signed or unsigned mono / stereo
|
|
-- samples, IT 2.14 / 2.15 compressed samples, sustain loops, auto-vibrato, the song message.
|
|
-- Not supported: embedded MIDI macro configurations (the default filter macros are used), MIDI output, OpenMPT's
|
|
-- extension chunks (plugins, extended instrument properties), writing.
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@formats/core/Err")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Types = require("@formats/Types")
|
|
local Tracker = require("@formats/codecs/Tracker")
|
|
local Itsex = require("@formats/music/it/Itsex")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ contract
|
|
local It = {
|
|
name = "it",
|
|
category = "music",
|
|
extensions = { "it" },
|
|
mime = { "audio/it", "audio/x-it" },
|
|
deps = { "core/Err", "core/Bytes", "Types", "codecs/Tracker" },
|
|
caps = { decode = true, encode = false, render = true, channels = { 1, 64 }, compressedSamples = true },
|
|
}
|
|
|
|
export type DecodeOptions = {
|
|
maxBytes: number?, -- refuse more sample data in total (f32 bytes, default 2^28)
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
function It.sniff(r: Bytes.Reader): number
|
|
return if r:peek(4) == "IMPM" then 1 else 0
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode
|
|
-- the 82-byte envelope of a new-format instrument
|
|
local function readEnvelope(r: Bytes.Reader, kind: string): any
|
|
local flags, n = r:u8(), r:u8()
|
|
local lb, le, sb, se = r:u8(), r:u8(), r:u8(), r:u8()
|
|
local e = Tracker.newEnvelope()
|
|
n = math.min(n, 25)
|
|
for i = 1, 25 do
|
|
local y, x = r:i8(), r:u16le()
|
|
if i <= n then
|
|
e.ticks[i] = x
|
|
e.values[i] = if kind == "vol" then math.clamp(y, 0, 64) else math.clamp(y, -32, 32)
|
|
end
|
|
end
|
|
r:skip(1)
|
|
e.enabled = bit32.band(flags, 1) ~= 0 and n > 0
|
|
e.loop = bit32.band(flags, 2) ~= 0
|
|
e.sustain = bit32.band(flags, 4) ~= 0
|
|
e.carry = bit32.band(flags, 8) ~= 0
|
|
e.filter = kind == "pitch" and bit32.band(flags, 128) ~= 0
|
|
e.loopStart, e.loopEnd = lb + 1, le + 1
|
|
e.susStart, e.susEnd = sb + 1, se + 1
|
|
return e
|
|
end
|
|
|
|
local function readInstrument(r: Bytes.Reader, old: boolean): (any?, string?)
|
|
if r:string(4) ~= "IMPI" then
|
|
return nil, "bad instrument signature"
|
|
end
|
|
local ins = Tracker.newInstrument()
|
|
r:skip(13)
|
|
if old then
|
|
-- the pre-2.0 layout: volume envelope only (flags, loop and sustain points), fadeout 0..64 of 512
|
|
local flg, vls, vle, sls, sle = r:u8(), r:u8(), r:u8(), r:u8(), r:u8()
|
|
r:skip(2)
|
|
ins.fadeout = r:u16le() * 2
|
|
ins.nna = r:u8()
|
|
ins.dct = if r:u8() ~= 0 then 1 else 0
|
|
r:skip(4)
|
|
ins.name = r:fixed(26)
|
|
r:skip(6)
|
|
for n = 1, 120 do
|
|
local note, smp = r:u8(), r:u8()
|
|
ins.noteMap[n] = math.clamp(note + 1, 1, 120)
|
|
ins.sampleMap[n] = smp
|
|
end
|
|
r:skip(200)
|
|
local e = Tracker.newEnvelope()
|
|
for i = 1, 25 do
|
|
local t, v = r:u8(), r:u8()
|
|
if t == 0xFF or (i > 1 and t <= (e.ticks[#e.ticks] or -1)) then
|
|
r:skip((25 - i) * 2)
|
|
break
|
|
end
|
|
table.insert(e.ticks, t)
|
|
table.insert(e.values, math.min(v, 64))
|
|
end
|
|
e.enabled = bit32.band(flg, 1) ~= 0 and #e.ticks > 0
|
|
e.loop = bit32.band(flg, 2) ~= 0
|
|
e.sustain = bit32.band(flg, 4) ~= 0
|
|
e.loopStart, e.loopEnd, e.susStart, e.susEnd = vls + 1, vle + 1, sls + 1, sle + 1
|
|
ins.volEnv = e
|
|
return ins
|
|
end
|
|
ins.nna, ins.dct, ins.dca = r:u8() % 4, r:u8() % 4, r:u8() % 3
|
|
ins.fadeout = math.min(r:u16le(), 1024)
|
|
ins.pps, ins.ppc = r:i8(), r:u8()
|
|
ins.globalVolume = math.min(r:u8(), 128)
|
|
local dfp = r:u8()
|
|
if dfp < 128 then
|
|
ins.pan = math.min(dfp, 64) * 4
|
|
end
|
|
r:skip(2 + 2 + 1 + 1)
|
|
ins.name = r:fixed(26)
|
|
local ifc, ifr = r:u8(), r:u8()
|
|
if ifc >= 128 then
|
|
ins.cutoff = ifc - 128
|
|
end
|
|
if ifr >= 128 then
|
|
ins.resonance = ifr - 128
|
|
end
|
|
r:skip(4)
|
|
for n = 1, 120 do
|
|
local note, smp = r:u8(), r:u8()
|
|
ins.noteMap[n] = math.clamp(note + 1, 1, 120)
|
|
ins.sampleMap[n] = smp
|
|
end
|
|
ins.volEnv = readEnvelope(r, "vol")
|
|
ins.panEnv = readEnvelope(r, "pan")
|
|
ins.pitchEnv = readEnvelope(r, "pitch")
|
|
if r.err then
|
|
return nil, "instrument truncated"
|
|
end
|
|
return ins
|
|
end
|
|
|
|
function It.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
|
local maxBytes = opts and opts.maxBytes or 2 ^ 28
|
|
if r:peek(4) ~= "IMPM" then
|
|
return nil, Err.new(Err.CORRUPT, "no IMPM signature")
|
|
end
|
|
r:skip(4)
|
|
local title = r:fixed(26)
|
|
r:skip(2)
|
|
local ordnum, insnum, smpnum, patnum = r:u16le(), r:u16le(), r:u16le(), r:u16le()
|
|
local cwt, cmwt, flags, special = r:u16le(), r:u16le(), r:u16le(), r:u16le()
|
|
local gv, mv, speed, tempo, _sep, _pwd = r:u8(), r:u8(), r:u8(), r:u8(), r:u8(), r:u8()
|
|
local msgLen, msgOff = r:u16le(), r:u32le()
|
|
r:skip(4)
|
|
local chnPan, chnVol = {}, {}
|
|
for i = 1, 64 do
|
|
chnPan[i] = r:u8()
|
|
end
|
|
for i = 1, 64 do
|
|
chnVol[i] = r:u8()
|
|
end
|
|
if r.err then
|
|
return nil, r.err
|
|
end
|
|
if ordnum > 256 or insnum > 255 or smpnum > 255 or patnum > 255 then
|
|
return nil, Err.new(Err.CORRUPT, "bad header counts")
|
|
end
|
|
local orders = {}
|
|
for i = 1, ordnum do
|
|
orders[i] = r:u8()
|
|
end
|
|
local insOff, smpOff, patOff = {}, {}, {}
|
|
for i = 1, insnum do
|
|
insOff[i] = r:u32le()
|
|
end
|
|
for i = 1, smpnum do
|
|
smpOff[i] = r:u32le()
|
|
end
|
|
for i = 1, patnum do
|
|
patOff[i] = r:u32le()
|
|
end
|
|
if r.err then
|
|
return nil, r.err
|
|
end
|
|
-- channels: the highest one any pattern uses
|
|
local nch = 1
|
|
local packed: { Bytes.Reader? } = {}
|
|
local rowsOf: { number } = {}
|
|
for p = 1, patnum do
|
|
if patOff[p] > 0 then
|
|
local pr = r:sub(patOff[p])
|
|
local len, rows = pr:u16le(), pr:u16le()
|
|
pr:skip(4)
|
|
if pr.err or rows < 1 or rows > 1024 then
|
|
return nil, Err.new(Err.CORRUPT, "bad pattern " .. (p - 1), r.base + patOff[p])
|
|
end
|
|
local data = pr:slice(len)
|
|
packed[p], rowsOf[p] = data, rows
|
|
-- scan for the channel count
|
|
local scan = data:sub(0)
|
|
local seen: { [number]: number } = {}
|
|
while not scan:eof() do
|
|
local cv = scan:u8()
|
|
if cv == 0 then
|
|
continue
|
|
end
|
|
local ch = (cv - 1) % 64
|
|
nch = math.max(nch, ch + 1)
|
|
local mask = 0
|
|
if cv >= 128 then
|
|
mask = scan:u8()
|
|
seen[ch] = mask
|
|
else
|
|
mask = seen[ch] or 0
|
|
end
|
|
if bit32.band(mask, 1) ~= 0 then
|
|
scan:skip(1)
|
|
end
|
|
if bit32.band(mask, 2) ~= 0 then
|
|
scan:skip(1)
|
|
end
|
|
if bit32.band(mask, 4) ~= 0 then
|
|
scan:skip(1)
|
|
end
|
|
if bit32.band(mask, 8) ~= 0 then
|
|
scan:skip(2)
|
|
end
|
|
end
|
|
else
|
|
rowsOf[p] = 64
|
|
end
|
|
end
|
|
local m = Tracker.newModule("it", nch)
|
|
m.title = title
|
|
m.speed = if speed > 0 then speed else 6
|
|
m.tempo = if tempo >= 31 then tempo else 125
|
|
m.globalVolume = math.min(gv, 128)
|
|
m.mixVolume = math.min(mv, 128)
|
|
m.linear = bit32.band(flags, 8) ~= 0
|
|
m.instrumentMode = bit32.band(flags, 4) ~= 0
|
|
m.flags.oldEffects = bit32.band(flags, 16) ~= 0
|
|
m.flags.compatGxx = bit32.band(flags, 32) ~= 0
|
|
local stereo = bit32.band(flags, 1) ~= 0
|
|
for i = 1, nch do
|
|
local p = chnPan[i]
|
|
if p >= 128 then
|
|
m.channelMute[i] = true
|
|
p -= 128
|
|
end
|
|
if not stereo then
|
|
m.channelPan[i] = 128
|
|
elseif p == 100 then
|
|
m.channelPan[i] = 512
|
|
else
|
|
m.channelPan[i] = math.min(p, 64) * 4
|
|
end
|
|
m.channelVolume[i] = math.min(chnVol[i], 64)
|
|
end
|
|
for i = 1, ordnum do
|
|
if orders[i] == 255 then
|
|
break
|
|
end
|
|
table.insert(m.order, orders[i])
|
|
end
|
|
m.meta.source = { format = "it", tracker = cwt, compatible = cmwt }
|
|
if bit32.band(special, 1) ~= 0 and msgLen > 0 then
|
|
local mr = r:sub(msgOff, msgLen)
|
|
m.meta.message = string.gsub(mr:fixed(msgLen), "\r", "\n")
|
|
end
|
|
-- instruments
|
|
for i = 1, insnum do
|
|
local ins, ierr = readInstrument(r:sub(insOff[i]), cmwt < 0x200)
|
|
if not ins then
|
|
return nil, Err.new(Err.CORRUPT, "instrument " .. i .. ": " .. tostring(ierr), r.base + insOff[i])
|
|
end
|
|
m.instruments[i] = ins
|
|
end
|
|
-- samples
|
|
local total = 0
|
|
for i = 1, smpnum do
|
|
local h = r:sub(smpOff[i], 80)
|
|
if h:string(4) ~= "IMPS" then
|
|
return nil, Err.new(Err.CORRUPT, "sample " .. i .. ": bad signature", r.base + smpOff[i])
|
|
end
|
|
h:skip(13)
|
|
local s = Tracker.newSample()
|
|
m.samples[i] = s
|
|
local gvl, flg, vol = h:u8(), h:u8(), h:u8()
|
|
s.name = h:fixed(26)
|
|
local cvt, dfp = h:u8(), h:u8()
|
|
local len, lb, le, c5, slb, sle, ptr = h:u32le(), h:u32le(), h:u32le(), h:u32le(), h:u32le(), h:u32le(), h:u32le()
|
|
local vis, vid, vir, vit = h:u8(), h:u8(), h:u8(), h:u8()
|
|
if h.err then
|
|
return nil, Err.new(Err.TRUNCATED, "sample header " .. i)
|
|
end
|
|
s.globalVolume = math.min(gvl, 64)
|
|
s.volume = math.min(vol, 64)
|
|
if dfp >= 128 then
|
|
s.pan = math.min(dfp - 128, 64) * 4
|
|
end
|
|
s.c5speed = if c5 > 0 then c5 else 8363
|
|
s.vibRate, s.vibDepth, s.vibSweep, s.vibType = vis, vid, vir, vit % 4
|
|
local bits = if bit32.band(flg, 2) ~= 0 then 16 else 8
|
|
local nchs = if bit32.band(flg, 4) ~= 0 then 2 else 1
|
|
s.bits = bits
|
|
if bit32.band(flg, 1) ~= 0 and len > 0 then
|
|
len = math.min(len, 2 ^ 26)
|
|
total += len * nchs * 4
|
|
if total > maxBytes then
|
|
return nil, Err.new(Err.LIMIT, "sample data over the maxBytes limit")
|
|
end
|
|
local data: buffer? = nil
|
|
if bit32.band(flg, 8) ~= 0 then
|
|
-- compressed: each channel separately
|
|
local it215 = bit32.band(cvt, 4) ~= 0
|
|
local p = r.base + ptr
|
|
local parts = {}
|
|
for c = 1, nchs do
|
|
local d, used = Itsex.decompress(r.buf, p, r.limit, len, bits, it215)
|
|
if not d then
|
|
return nil, Err.new(Err.CORRUPT, "sample " .. i .. ": " .. tostring(used))
|
|
end
|
|
parts[c] = d
|
|
p += used :: number
|
|
end
|
|
if nchs == 1 then
|
|
data = parts[1]
|
|
else
|
|
local inter = buffer.create(len * 8)
|
|
for f = 0, len - 1 do
|
|
buffer.writef32(inter, f * 8, buffer.readf32(parts[1], f * 4))
|
|
buffer.writef32(inter, f * 8 + 4, buffer.readf32(parts[2], f * 4))
|
|
end
|
|
data = inter
|
|
end
|
|
else
|
|
local bps = bits // 8
|
|
local avail = math.max(0, r:size() - ptr) // (bps * nchs)
|
|
len = math.min(len, avail)
|
|
data = Tracker.Pcm.decode(r.buf, r.base + ptr, len, {
|
|
bits = bits,
|
|
signed = bit32.band(cvt, 1) ~= 0,
|
|
bigEndian = bit32.band(cvt, 2) ~= 0,
|
|
delta = bit32.band(cvt, 4) ~= 0,
|
|
channels = nchs,
|
|
planar = true,
|
|
})
|
|
end
|
|
if data then
|
|
s.data, s.length, s.channels = data, len, nchs
|
|
if bit32.band(flg, 16) ~= 0 then
|
|
s.loopStart, s.loopEnd, s.loopType = lb, math.min(le, len), if bit32.band(flg, 64) ~= 0 then 2 else 1
|
|
end
|
|
if bit32.band(flg, 32) ~= 0 then
|
|
s.susStart, s.susEnd, s.susType = slb, math.min(sle, len), if bit32.band(flg, 128) ~= 0 then 2 else 1
|
|
end
|
|
end
|
|
end
|
|
end
|
|
-- patterns
|
|
for p = 1, patnum do
|
|
local pat = Tracker.newPattern(rowsOf[p], nch)
|
|
m.patterns[p] = pat
|
|
local pr = packed[p]
|
|
if pr then
|
|
local d = pat.data
|
|
local lastMask, lastNote, lastIns, lastVol, lastFx, lastParam = {}, {}, {}, {}, {}, {}
|
|
local row = 0
|
|
while row < pat.rows and not pr:eof() do
|
|
local cv = pr:u8()
|
|
if cv == 0 then
|
|
row += 1
|
|
continue
|
|
end
|
|
local ch = (cv - 1) % 64
|
|
local mask = if cv >= 128 then pr:u8() else (lastMask[ch] or 0)
|
|
lastMask[ch] = mask
|
|
local note, ins, vol, fx, param = 0, 0, 0, 0, 0
|
|
if bit32.band(mask, 1) ~= 0 then
|
|
local n = pr:u8()
|
|
-- 255 note off, 254 note cut, 246 note fade (as IT and OpenMPT store "~~~"); other values: nothing
|
|
note = if n < 120 then n + 1 elseif n == 255 then 255 elseif n == 254 then 254 elseif n == 246 then 253 else 0
|
|
lastNote[ch] = note
|
|
elseif bit32.band(mask, 16) ~= 0 then
|
|
note = lastNote[ch] or 0
|
|
end
|
|
if bit32.band(mask, 2) ~= 0 then
|
|
ins = pr:u8()
|
|
lastIns[ch] = ins
|
|
elseif bit32.band(mask, 32) ~= 0 then
|
|
ins = lastIns[ch] or 0
|
|
end
|
|
if bit32.band(mask, 4) ~= 0 then
|
|
vol = math.min(pr:u8(), 212) + 1
|
|
lastVol[ch] = vol
|
|
elseif bit32.band(mask, 64) ~= 0 then
|
|
vol = lastVol[ch] or 0
|
|
end
|
|
if bit32.band(mask, 8) ~= 0 then
|
|
fx, param = pr:u8(), pr:u8()
|
|
if fx > 26 then
|
|
fx, param = 0, 0
|
|
end
|
|
lastFx[ch], lastParam[ch] = fx, param
|
|
elseif bit32.band(mask, 128) ~= 0 then
|
|
fx, param = lastFx[ch] or 0, lastParam[ch] or 0
|
|
end
|
|
if ch < nch then
|
|
local o = (row * nch + ch) * 5
|
|
buffer.writeu8(d, o, note)
|
|
buffer.writeu8(d, o + 1, ins)
|
|
buffer.writeu8(d, o + 2, vol)
|
|
buffer.writeu8(d, o + 3, fx)
|
|
buffer.writeu8(d, o + 4, param)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
local ok, verr = Tracker.validate(m)
|
|
if not ok then
|
|
return nil, Err.new(Err.CORRUPT, verr and verr.message or "invalid module")
|
|
end
|
|
return m, nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ open
|
|
-- open(r, opts) -> the module rendered as an audio Decoder (opts: Tracker.RenderOptions + DecodeOptions)
|
|
function It.open(r: Bytes.Reader, opts: any?): (Types.Decoder?, Err.Err?)
|
|
local m, err = It.decode(r, opts)
|
|
if not m then
|
|
return nil, err
|
|
end
|
|
return Tracker.render(m, opts)
|
|
end
|
|
|
|
return It
|