221 lines
7.0 KiB
Luau
221 lines
7.0 KiB
Luau
--!strict
|
|
-- music/mtm: MultiTracker modules (MTM, 1..32 channels, patterns of shared tracks), ported from OpenMPT's Load_mtm.cpp
|
|
-- (BSD, licenses/openmpt-LICENSE.txt); played as the "xm" family with Amiga periods, as OpenMPT does. Not supported:
|
|
-- MultiTracker's E9x / EDx retrigger timing and vibrato start phase (played as in XM), writing.
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@rosetta/core/Err")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Types = require("@rosetta/Types")
|
|
local Tracker = require("@rosetta/codecs/audio/Tracker")
|
|
local Load = require("@rosetta/codecs/audio/Tracker/Load")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ contract
|
|
local Mtm = {
|
|
name = "mtm",
|
|
category = "music",
|
|
extensions = { "mtm" },
|
|
mime = { "audio/x-mtm" },
|
|
deps = { "core/Err", "core/Bytes", "Types", "codecs/audio/Tracker", "codecs/audio/Tracker/Load" },
|
|
caps = { decode = true, encode = false, render = true, channels = { 1, 32 } },
|
|
}
|
|
|
|
export type DecodeOptions = {
|
|
maxBytes: number?, -- f32 bytes of sample data in total, default 2^28
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
function Mtm.sniff(r: Bytes.Reader): number
|
|
if r:size() >= 66 and r:peek(3) == "MTM" and r:peekU8(3) < 0x20 and r:peekU8(33) >= 1 and r:peekU8(33) <= 32 then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode
|
|
-- MultiTracker: a speed command resets the tempo to 125 and a tempo command the speed to 6, unless the module puts
|
|
-- both on one row somewhere (then it was written for players that treat them as ProTracker does)
|
|
local function speedTempoQuirk(m: any)
|
|
local speed, tempo, together = false, false, false
|
|
for _, p in m.patterns do
|
|
for row = 0, p.rows - 1 do
|
|
local s, t = false, false
|
|
for c = 1, m.channels do
|
|
local o = (row * m.channels + c - 1) * 5
|
|
local fx, param = buffer.readu8(p.data, o + 3), buffer.readu8(p.data, o + 4)
|
|
if fx == 0xF then
|
|
if param < 0x20 then
|
|
s = true
|
|
else
|
|
t = true
|
|
end
|
|
end
|
|
end
|
|
speed, tempo, together = speed or s, tempo or t, together or (s and t)
|
|
end
|
|
end
|
|
if not (speed and tempo) or together then
|
|
return
|
|
end
|
|
for pi, p in m.patterns do
|
|
for row = 0, p.rows - 1 do
|
|
for c = 1, m.channels do
|
|
local o = (row * m.channels + c - 1) * 5
|
|
local fx, param = buffer.readu8(p.data, o + 3), buffer.readu8(p.data, o + 4)
|
|
if fx == 0xF then
|
|
Load.writeEffect(m, pi - 1, row, 0xF, if param < 0x20 then 125 else 6, nil, nil, true)
|
|
break
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
function Mtm.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
|
local budget = { left = opts and opts.maxBytes or 2 ^ 28 }
|
|
if r:size() < 66 then
|
|
return nil, Err.new(Err.TRUNCATED, "header")
|
|
end
|
|
if r:peek(3) ~= "MTM" then
|
|
return nil, Err.new(Err.CORRUPT, "no MTM signature")
|
|
end
|
|
r:skip(3)
|
|
local version = r:u8()
|
|
local title = r:fixed(20)
|
|
local numTracks, lastPattern, lastOrder = r:u16le(), r:u8(), r:u8()
|
|
local commentSize, numSamples, _attr, rowsPerTrack, nch = r:u16le(), r:u8(), r:u8(), r:u8(), r:u8()
|
|
local pans: { number } = {}
|
|
for i = 1, 32 do
|
|
pans[i] = r:u8()
|
|
end
|
|
if r.err then
|
|
return nil, r.err
|
|
end
|
|
if version >= 0x20 or lastOrder > 127 or rowsPerTrack > 64 or nch == 0 or nch > 32 then
|
|
return nil, Err.new(Err.CORRUPT, "bad header")
|
|
end
|
|
local m = Tracker.newModule("mtm", nch, "xm")
|
|
m.title = title
|
|
m.linear = false
|
|
m.instrumentMode = false
|
|
for i = 1, nch do
|
|
m.channelPan[i] = (pans[i] % 16) * 16 + 8
|
|
end
|
|
local sizes: { number } = {}
|
|
for i = 1, numSamples do
|
|
local s = Tracker.newSample(r:fixed(22))
|
|
local len, ls, le = r:u32le(), r:u32le(), r:u32le()
|
|
local ft, vol, attr = r:i8(), r:u8(), r:u8()
|
|
if r.err then
|
|
return nil, r.err
|
|
end
|
|
s.volume = math.min(vol, 64)
|
|
s.finetune = math.clamp(ft * 16, -128, 127)
|
|
local bits = if attr % 2 == 1 then 16 else 8
|
|
s.bits = bits
|
|
local frames = 0
|
|
if len > 2 then
|
|
frames = len
|
|
le = math.min(math.max(le, 1) - 1, len)
|
|
if ls + 4 >= le then
|
|
ls, le = 0, 0
|
|
end
|
|
if bits == 16 then
|
|
frames, ls, le = frames // 2, ls // 2, le // 2
|
|
end
|
|
if le > 2 then
|
|
s.loopStart, s.loopEnd, s.loopType = ls, le, 1
|
|
end
|
|
end
|
|
sizes[i] = if len > 2 then len else 0
|
|
s.length = frames
|
|
m.samples[i] = s
|
|
end
|
|
for i = 1, 128 do
|
|
local o = r:u8()
|
|
if i <= lastOrder + 1 then
|
|
m.order[i] = o
|
|
end
|
|
end
|
|
if r.err then
|
|
return nil, r.err
|
|
end
|
|
-- tracks (64 rows of 3 bytes each) and the patterns made of them
|
|
local rows = if rowsPerTrack > 0 then rowsPerTrack else 64
|
|
local tracks = r:tell()
|
|
if not r:skip(192 * numTracks) then
|
|
return nil, r.err
|
|
end
|
|
local buf = r.buf
|
|
for p = 0, lastPattern do
|
|
local pat = Tracker.newPattern(rows, nch)
|
|
m.patterns[p + 1] = pat
|
|
for ch = 0, 31 do
|
|
local track = r:u16le()
|
|
if r.err then
|
|
return nil, r.err
|
|
end
|
|
if track > 0 and track <= numTracks and ch < nch then
|
|
local base = r.base + tracks + 192 * (track - 1)
|
|
for row = 0, rows - 1 do
|
|
local o = base + row * 3
|
|
local b0, b1, b2 = buffer.readu8(buf, o), buffer.readu8(buf, o + 1), buffer.readu8(buf, o + 2)
|
|
local q = (row * nch + ch) * 5
|
|
if b0 >= 4 then
|
|
buffer.writeu8(pat.data, q, b0 // 4 + 25) -- C-4 = 49 plays the sample at 8363 Hz (OpenMPT keeps MTM / XM notes an octave up)
|
|
end
|
|
buffer.writeu8(pat.data, q + 1, (b0 % 4) * 16 + b1 // 16)
|
|
local cmd, param = b1 % 16, b2
|
|
if cmd == 0xA then
|
|
param = if param >= 16 then param - param % 16 else param
|
|
elseif cmd == 8 then
|
|
cmd, param = 0, 0 -- no 8xx panning in MultiTracker
|
|
elseif cmd == 0xE then
|
|
local x = param // 16
|
|
if x == 0 or x == 3 or x == 4 or x == 6 or x == 7 or x == 0xF then
|
|
cmd, param = 0, 0 -- not in MultiTracker
|
|
end
|
|
end
|
|
buffer.writeu8(pat.data, q + 3, cmd)
|
|
buffer.writeu8(pat.data, q + 4, param)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
speedTempoQuirk(m)
|
|
-- the song message (40-character lines), then the sample data (unsigned PCM)
|
|
m.meta.message = r:string(commentSize)
|
|
if r.err then
|
|
r.err = nil
|
|
end
|
|
local pos = r.pos
|
|
for i = 1, numSamples do
|
|
local s = m.samples[i]
|
|
if sizes[i] > 0 then
|
|
local ok, err = Load.readSample(buf, pos, s.length, { bits = s.bits, signed = false, limit = r.limit }, s, budget)
|
|
if not ok then
|
|
return nil, err
|
|
end
|
|
pos += sizes[i]
|
|
s.loopEnd = math.min(s.loopEnd, s.length)
|
|
end
|
|
end
|
|
m.meta.source = { format = "mtm", version = version, tracks = numTracks }
|
|
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
|
|
function Mtm.open(r: Bytes.Reader, opts: any?): (Types.Decoder?, Err.Err?)
|
|
local m, err = Mtm.decode(r, opts)
|
|
if not m then
|
|
return nil, err
|
|
end
|
|
return Tracker.render(m, opts)
|
|
end
|
|
|
|
return Mtm
|