diff --git a/src/Formats/music/ptm/init.luau b/src/Formats/music/ptm/init.luau index ad06f91..66818a1 100644 --- a/src/Formats/music/ptm/init.luau +++ b/src/Formats/music/ptm/init.luau @@ -59,6 +59,65 @@ local function delta16(buf: buffer, off: number, frames: number, limit: number): 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 @@ -140,67 +199,11 @@ function Ptm.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?) end end end - -- patterns (S3M-style packing: 0 ends a row; channel | 0x20 note + instrument, 0x40 effect, 0x80 volume) for p = 1, npat do local pat = Tracker.newPattern(64, nch) m.patterns[p] = pat - local d = pat.data if patOff[p] > 0 and patOff[p] < r:size() then - local pr = r:sub(patOff[p]) - 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 cmd, param = pr:u8(), pr:u8() - local name - if cmd < 0x10 then - name, param = Load.modCommand(cmd, param) - else - name = EXTRA[cmd - 0x10 + 1] - end - if name == "panning8" then - name, param = "s3mex", 0x80 + (math.max(param // 8, 1) - 1) % 16 - elseif name == "globalvol" then - param = math.min(param, 0x40) * 2 - end - 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 + readPattern(r:sub(patOff[p]), pat.data, nch) end end m.meta.source = { format = "ptm", version = string.format("%d.%02x", vhi, vlo) }