303 lines
9.2 KiB
Luau
303 lines
9.2 KiB
Luau
--!strict
|
|
-- music/ult: UltraTracker modules (MAS_UTrack_V001 .. V004, up to 32 channels), ported from OpenMPT's Load_ult.cpp
|
|
-- (BSD, licenses/openmpt-LICENSE.txt); played as the "it" family with Amiga periods, as OpenMPT converts ULT. Not
|
|
-- supported: reverse (5x2), "no loop" (5x1), E0x, offsets past 64 KiB, pre-1.4 log volumes, 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 Ult = {
|
|
name = "ult",
|
|
category = "music",
|
|
extensions = { "ult" },
|
|
mime = { "audio/x-ult" },
|
|
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
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ constants
|
|
local EFFECTS: { [number]: string } = {
|
|
[0] = "arpeggio", [1] = "portaup", [2] = "portadown", [3] = "toneporta", [4] = "vibrato", [7] = "tremolo",
|
|
[9] = "offset", [10] = "volslide", [11] = "panning8", [12] = "volume8", [13] = "break", [15] = "speed",
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
function Ult.sniff(r: Bytes.Reader): number
|
|
local v = r:peekU8(14)
|
|
if r:size() >= 48 and r:peek(14) == "MAS_UTrack_V00" and v >= 0x31 and v <= 0x34 then
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode
|
|
-- OpenMPT's TranslateULTCommands
|
|
local function translate(e: number, param: number, version: number): (string?, number)
|
|
local name = EFFECTS[e]
|
|
if e == 0 then
|
|
if param == 0 or version < 3 then
|
|
name = nil
|
|
end
|
|
elseif e == 5 then
|
|
if (param % 16 == 0xC or param // 16 == 0xC) and version >= 3 then
|
|
return "keyoff", 0
|
|
end
|
|
elseif e == 7 then
|
|
if version < 4 then
|
|
name = nil
|
|
end
|
|
elseif e == 0xA then
|
|
if param >= 16 then
|
|
param -= param % 16
|
|
end
|
|
elseif e == 0xB then
|
|
param = (param % 16) * 0x11
|
|
elseif e == 0xD then
|
|
param = 10 * (param // 16) + param % 16
|
|
elseif e == 0xE then
|
|
local x, y = param // 16, param % 16
|
|
if x == 1 then
|
|
return "portaup", 0xF0 + y
|
|
elseif x == 2 then
|
|
return "portadown", 0xF0 + y
|
|
elseif x == 8 and version >= 4 then
|
|
return "s3mex", 0x60 + y
|
|
elseif x == 9 then
|
|
return "retrig", y
|
|
elseif x == 0xA then
|
|
return "volslide", y * 16 + 0xF
|
|
elseif x == 0xB then
|
|
return "volslide", 0xF0 + y
|
|
elseif x == 0xC or x == 0xD then
|
|
return "s3mex", param
|
|
end
|
|
return nil, param
|
|
elseif e == 0xF then
|
|
if param > 0x2F then
|
|
name = "tempo"
|
|
end
|
|
end
|
|
return name, param
|
|
end
|
|
|
|
-- OpenMPT's CombineEffects (volume slide + vibrato / tone portamento without a parameter)
|
|
local function combine(a: string?, pa: number, b: string?, pb: number): (string?, number, string?, number)
|
|
if a == "volslide" and (b == "vibrato" or b == "toneportavol") and pb == 0 then
|
|
return if b == "vibrato" then "vibratovol" else "toneportavol", pa, nil, 0
|
|
elseif b == "volslide" and (a == "vibrato" or a == "toneportavol") and pa == 0 then
|
|
return if a == "vibrato" then "vibratovol" else "toneportavol", pb, nil, 0
|
|
end
|
|
return a, pa, b, pb
|
|
end
|
|
|
|
-- event: [0xFC repeat] note instrument effects param1 param2
|
|
local function readEvent(r: Bytes.Reader, version: number): (number, number, number, number, number, number, string?, number)
|
|
local rep = 1
|
|
local b = r:u8()
|
|
if b == 0xFC then
|
|
rep = r:u8()
|
|
b = r:u8()
|
|
end
|
|
local note = if b > 0 and b < 97 then b + 24 else 0
|
|
local ins, cmd, p1, p2 = r:u8(), r:u8(), r:u8(), r:u8()
|
|
local c1, q1 = translate(cmd % 16, p1, version)
|
|
local c2, q2 = translate(cmd // 16, p2, version)
|
|
if c1 == "offset" and c2 == "offset" then
|
|
local off = (p2 * 256 + p1) // 64
|
|
return rep, note, ins, 0, Load.IT.offset, off % 256, nil, 0
|
|
elseif c1 == "offset" then
|
|
q1 = math.min(p1 * 4, 255)
|
|
elseif c2 == "offset" then
|
|
q2 = math.min(p2 * 4, 255)
|
|
elseif c1 ~= nil and c1 == c2 then
|
|
c2 = nil
|
|
end
|
|
if c2 == nil then -- (OpenMPT also swaps for CMD_VOLUME, which ULT never produces)
|
|
c1, q1, c2, q2 = c2, q2, c1, q1
|
|
end
|
|
c2, q2, c1, q1 = combine(c2, q2, c1, q1)
|
|
if c1 == "keyoff" or c2 == "keyoff" then
|
|
-- key off (5xC): a note-off when the cell has no note
|
|
if note == 0 then
|
|
note = 255
|
|
end
|
|
if c1 == "keyoff" then
|
|
c1 = nil
|
|
else
|
|
c2 = nil
|
|
end
|
|
end
|
|
local vol, name, param, lost, lostParam = Load.fillTwo(c1, q1, c2, q2)
|
|
local fx, fp = Load.itEffect(name, param)
|
|
return rep, note, ins, vol, fx, fp, lost, lostParam
|
|
end
|
|
|
|
function Ult.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
|
local budget = { left = opts and opts.maxBytes or 2 ^ 28 }
|
|
if Ult.sniff(r) == 0 then
|
|
return nil, Err.new(if r:size() < 48 then Err.TRUNCATED else Err.CORRUPT, "not an UltraTracker module")
|
|
end
|
|
r:skip(14)
|
|
local version = r:u8() - 0x30
|
|
local title = (string.gsub(r:fixed(32), " +$", ""))
|
|
local lines = r:u8()
|
|
local message = r:string(lines * 32)
|
|
local nsmp = r:u8()
|
|
if r.err then
|
|
return nil, r.err
|
|
end
|
|
local samples, sizes = {}, {}
|
|
for i = 1, nsmp do
|
|
local s = Tracker.newSample((string.gsub(r:fixed(32), " +$", "")))
|
|
r:skip(12)
|
|
local ls, le, ss, se = r:u32le(), r:u32le(), r:u32le(), r:u32le()
|
|
local vol, flags = r:u8(), r:u8()
|
|
local speed, finetune = 8363, 0
|
|
if version >= 4 then
|
|
speed, finetune = r:u16le(), r:i16le()
|
|
else
|
|
finetune = r:i16le()
|
|
end
|
|
if r.err then
|
|
return nil, r.err
|
|
end
|
|
local bits = if bit32.band(flags, 4) ~= 0 then 16 else 8
|
|
s.bits = bits
|
|
sizes[i] = 0
|
|
if se > ss then
|
|
s.length = se - ss
|
|
sizes[i] = s.length * bits // 8
|
|
s.volume = vol / 4
|
|
s.c5speed = math.max(1, speed * 2 * 2 ^ (finetune / (12 * 32768)))
|
|
if bit32.band(flags, 8) ~= 0 then
|
|
if bits == 16 then
|
|
ls, le = ls // 2, le // 2
|
|
end
|
|
s.susStart, s.susEnd = ls, le
|
|
s.susType = if bit32.band(flags, 16) ~= 0 then 2 else 1
|
|
end
|
|
end
|
|
samples[i] = s
|
|
end
|
|
local order = {}
|
|
for i = 1, 256 do
|
|
order[i] = r:u8()
|
|
end
|
|
local nch, npat = r:u8() + 1, r:u8() + 1
|
|
if r.err then
|
|
return nil, r.err
|
|
end
|
|
if nch > 32 then
|
|
return nil, Err.new(Err.UNSUPPORTED, nch .. " channels")
|
|
end
|
|
local m = Tracker.newModule("ult", nch, "it")
|
|
m.title = title
|
|
m.meta.message = message
|
|
m.linear = false
|
|
m.instrumentMode = false
|
|
m.flags.oldEffects, m.flags.compatGxx, m.flags.autoTonePorta = true, true, true
|
|
m.samples = samples
|
|
for i = 1, 256 do
|
|
m.order[i] = order[i]
|
|
end
|
|
while #m.order > 0 and m.order[#m.order] == 255 do
|
|
table.remove(m.order)
|
|
end
|
|
for i = 1, nch do
|
|
m.channelPan[i] = if version >= 3 then (r:u8() % 16) * 16 + 8 elseif i % 2 == 0 then 192 else 64
|
|
end
|
|
for p = 1, npat do
|
|
m.patterns[p] = Tracker.newPattern(64, nch)
|
|
end
|
|
-- events, channel by channel, pattern by pattern
|
|
local speed0 = false
|
|
for ch = 0, nch - 1 do
|
|
for p = 0, npat - 1 do
|
|
if r:remaining() < 5 then
|
|
break
|
|
end
|
|
local d = m.patterns[p + 1].data
|
|
local row = 0
|
|
while row < 64 do
|
|
local rep, note, ins, vol, fx, param, lost, lostParam = readEvent(r, version)
|
|
if r.err then
|
|
break
|
|
end
|
|
if lost and Load.isGlobal(lost, lostParam) then
|
|
local lf, lp = Load.itEffect(lost, lostParam)
|
|
Load.writeEffect(m, p, row, lf, lp, "next")
|
|
end
|
|
rep = math.min(rep, 64 - row)
|
|
if rep == 0 then
|
|
break
|
|
end
|
|
if fx == Load.IT.speed and param == 0 then
|
|
speed0 = true
|
|
end
|
|
for _ = 1, rep do
|
|
local q = (row * nch + ch) * 5
|
|
buffer.writeu8(d, q, note)
|
|
buffer.writeu8(d, q + 1, ins)
|
|
buffer.writeu8(d, q + 2, vol)
|
|
buffer.writeu8(d, q + 3, fx)
|
|
buffer.writeu8(d, q + 4, param)
|
|
row += 1
|
|
end
|
|
end
|
|
end
|
|
end
|
|
r.err = nil
|
|
if speed0 then
|
|
-- speed 0 resets the song speed and tempo (UltraTracker)
|
|
for p, pat in m.patterns do
|
|
for row = 0, 63 do
|
|
for ch = 0, nch - 1 do
|
|
local q = (row * nch + ch) * 5
|
|
if buffer.readu8(pat.data, q + 3) == Load.IT.speed and buffer.readu8(pat.data, q + 4) == 0 then
|
|
buffer.writeu8(pat.data, q + 4, 6)
|
|
Load.writeEffect(m, p - 1, row, Load.IT.tempo, 125, "next")
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
-- sample data: signed 8 / 16-bit
|
|
local pos = r.pos
|
|
for i = 1, nsmp do
|
|
local s = samples[i]
|
|
if sizes[i] > 0 then
|
|
local ok, err = Load.readSample(r.buf, pos, s.length, { bits = s.bits, signed = true, limit = r.limit }, s, budget)
|
|
if not ok then
|
|
return nil, err
|
|
end
|
|
pos += sizes[i]
|
|
end
|
|
end
|
|
m.meta.source = { format = "ult", version = version }
|
|
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 Ult.open(r: Bytes.Reader, opts: any?): (Types.Decoder?, Err.Err?)
|
|
local m, err = Ult.decode(r, opts)
|
|
if not m then
|
|
return nil, err
|
|
end
|
|
return Tracker.render(m, opts)
|
|
end
|
|
|
|
return Ult
|