Files
rosetta/src/Formats/music/ptm/init.luau
T

228 lines
7.5 KiB
Luau

--!strict
-- music/ptm: PolyTracker modules (PTMF, Lone Ranger's DOS tracker; up to 32 channels, S3M-like packed patterns, MOD
-- effect letters). Ported from OpenMPT's Load_ptm.cpp (BSD, licenses/openmpt-LICENSE.txt). Reads into the
-- codecs/audio/Tracker Module and plays as the "it" effect family with Amiga periods, IT old effects and linked Gxx
-- memory (as OpenMPT converts PTM): MOD effects whose slides take S3M-style fine values, global volume, retrigger,
-- fine vibrato and the note slides (G..J: Tracker.EXT), delta-coded 8-bit and 16-bit samples (8-bit deltas of the
-- byte stream, as PolyTracker stores them), ping-pong loops.
-- Not supported: reverse sample offset (K), sample offsets scaled for 16-bit samples, writing.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Types = require("@formats/Types")
local Tracker = require("@formats/codecs/audio/Tracker")
local Load = require("@formats/codecs/audio/Tracker/Load")
-- ------------------------------------------------------------------------------------------------ contract
local Ptm = {
name = "ptm",
category = "music",
extensions = { "ptm" },
mime = { "audio/x-ptm" },
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?, -- refuse more sample data in total (f32 bytes, default 2^28)
}
-- ------------------------------------------------------------------------------------------------ constants
-- PolyTracker's effects past MOD's 0..F
local EXTRA = { "globalvol", "retrig", "finevibrato", "noteslideup", "noteslidedown", "noteslideupretrig", "noteslidedownretrig" }
-- ------------------------------------------------------------------------------------------------ sniff
local function validHeader(r: Bytes.Reader): boolean
if r:size() < 608 or r:peek(4, 44) ~= "PTMF" or r:peekU8(28) ~= 26 or r:peekU8(30) > 2 then
return false
end
local h = r:sub(32, 10)
local nord, nsmp, npat, nch, flags = h:u16le(), h:u16le(), h:u16le(), h:u16le(), h:u16le()
return flags == 0 and nch >= 1 and nch <= 32 and nord >= 1 and nord <= 256 and nsmp >= 1 and nsmp <= 255 and npat >= 1 and npat <= 128
end
function Ptm.sniff(r: Bytes.Reader): number
return if validHeader(r) then 1 else 0
end
-- ------------------------------------------------------------------------------------------------ decode
-- 16-bit samples: the byte stream is 8-bit delta coded, then read as little-endian 16-bit values
local function delta16(buf: buffer, off: number, frames: number, limit: number): (buffer, number)
local n = math.max(0, math.min(frames, (limit - off) // 2))
local raw = buffer.create(n * 2)
local acc = 0
for i = 0, n * 2 - 1 do
acc = (acc + buffer.readu8(buf, off + i)) % 256
buffer.writeu8(raw, i, acc)
end
return raw, n
end
-- a PolyTracker effect -> name?, param
local function effect(cmd: number, param: number): (string?, number)
if cmd == 0x10 then
return "globalvol", math.min(param, 0x40) * 2
elseif cmd > 0x10 then
return EXTRA[cmd - 0x10 + 1], param
elseif cmd == 8 then
return "s3mex", 0x80 + (math.max(param // 8, 1) - 1) % 16 -- 8xx pans in 16 steps
end
return Load.modCommand(cmd, param)
end
-- one pattern (S3M-style packing: 0 ends a row; channel | 0x20 note + instrument, 0x40 effect, 0x80 volume)
local function readPattern(pr: Bytes.Reader, d: buffer, nch: number)
local row = 0
while row < 64 and not pr:eof() do
local b = pr:u8()
if b == 0 then
row += 1
else
local ch = b % 32
local q = (row * nch + ch) * 5
local ok = ch < nch
if bit32.band(b, 0x20) ~= 0 then
local note, ins = pr:u8(), pr:u8()
if note == 254 then
note = 254
elseif note == 0 or note > 120 then
note = 0
end
if ok then
buffer.writeu8(d, q, note)
buffer.writeu8(d, q + 1, ins)
end
end
if bit32.band(b, 0x40) ~= 0 then
local name, param = effect(pr:u8(), pr:u8())
if ok then
if name == "volume" then
if buffer.readu8(d, q + 2) == 0 then
buffer.writeu8(d, q + 2, math.min(param, 64) + 1)
end
else
local fx, fp = Load.itEffect(name, param)
buffer.writeu8(d, q + 3, fx)
buffer.writeu8(d, q + 4, fp)
end
end
end
if bit32.band(b, 0x80) ~= 0 then
local v = pr:u8()
if ok then
buffer.writeu8(d, q + 2, math.min(v, 64) + 1)
end
end
end
end
end
function Ptm.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
local budget = { left = opts and opts.maxBytes or 2 ^ 28 }
if not validHeader(r) then
return nil, Err.new(if r:size() < 608 then Err.TRUNCATED else Err.CORRUPT, "not a PolyTracker module")
end
local title = r:fixed(28)
r:skip(1)
local vlo, vhi = r:u8(), r:u8()
r:skip(1)
local nord, nsmp, npat, nch = r:u16le(), r:u16le(), r:u16le(), r:u16le()
r:seek(64)
local m = Tracker.newModule("ptm", nch, "it")
m.title = title
m.linear = false
m.instrumentMode = false
m.flags.oldEffects, m.flags.compatGxx = true, true
m.flags.panLaw = "ft2" -- OpenMPT plays PTM with its FT2 mix levels
for i = 1, 32 do
local p = r:u8()
if i <= nch then
m.channelPan[i] = (p % 16) * 16 + 4
end
end
for i = 1, 256 do
local o = r:u8()
if i <= nord then
m.order[i] = o
end
end
local patOff: { number } = {}
for i = 1, 128 do
patOff[i] = r:u16le() * 16
end
if r.err then
return nil, r.err
end
-- samples
local buf = r.buf
for i = 1, nsmp do
local h = r:slice(80)
if r.err then
return nil, r.err
end
local flags = h:u8()
h:skip(12)
local vol, c4 = h:u8(), h:u16le()
h:skip(2)
local off, len, ls, le = h:u32le(), h:u32le(), h:u32le(), h:u32le()
h:skip(14)
local s = Tracker.newSample(h:fixed(28))
m.samples[i] = s
s.volume = math.min(vol, 64)
s.c5speed = math.max(1, c4 * 2)
if flags % 4 == 1 and len > 0 then
local bits = if bit32.band(flags, 0x10) ~= 0 then 16 else 8
if le > ls then
le -= 1
end
local frames = len
if bits == 16 then
frames, ls, le = frames // 2, ls // 2, le // 2
end
local at = r.base + off
if at < r.limit then
local ok, err
if bits == 16 then
local raw, n = delta16(buf, at, frames, r.limit)
ok, err = Load.readSample(raw, 0, n, { bits = 16, signed = true }, s, budget)
else
ok, err = Load.readSample(buf, at, frames, { bits = 8, signed = true, delta = true, limit = r.limit }, s, budget)
end
if not ok then
return nil, err
end
end
if bit32.band(flags, 4) ~= 0 and le > ls then
s.loopStart, s.loopEnd = ls, math.min(le, s.length)
s.loopType = if bit32.band(flags, 8) ~= 0 then 2 else 1
end
end
end
for p = 1, npat do
local pat = Tracker.newPattern(64, nch)
m.patterns[p] = pat
if patOff[p] > 0 and patOff[p] < r:size() then
readPattern(r:sub(patOff[p]), pat.data, nch)
end
end
m.meta.source = { format = "ptm", version = string.format("%d.%02x", vhi, vlo) }
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 Ptm.open(r: Bytes.Reader, opts: any?): (Types.Decoder?, Err.Err?)
local m, err = Ptm.decode(r, opts)
if not m then
return nil, err
end
return Tracker.render(m, opts)
end
return Ptm