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

243 lines
7.4 KiB
Luau

--!strict
-- music/s3m: Scream Tracker 3 modules (SCRM, ST3 TECH.DOC; also files from Impulse Tracker / OpenMPT / Schism
-- Tracker). Reads into the codecs/Tracker Module and plays through its engine (Module:render() / open() -> an audio
-- Decoder). Supports: up to 32 PCM channels, 8 / 16-bit signed or unsigned, mono or stereo samples, loops, the panning
-- table, ST3.00 fast volume slides, every ST3 effect. Notes are stored one octave up (S3M C-4 = note 61 = the C5
-- speed reference, as IT shows S3M files).
-- Not supported: AdLib (OPL) instruments and channels (read as silent), 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")
-- ------------------------------------------------------------------------------------------------ contract
local S3m = {
name = "s3m",
category = "music",
extensions = { "s3m" },
mime = { "audio/s3m", "audio/x-s3m" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Tracker" },
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)
}
-- ------------------------------------------------------------------------------------------------ sniff
function S3m.sniff(r: Bytes.Reader): number
if r:size() >= 48 and r:peek(4, 44) == "SCRM" then
return 1
end
return 0
end
-- ------------------------------------------------------------------------------------------------ decode
function S3m.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
local maxBytes = opts and opts.maxBytes or 2 ^ 28
if r:size() < 96 then
return nil, Err.new(Err.TRUNCATED, "header")
end
if r:peek(4, 44) ~= "SCRM" then
return nil, Err.new(Err.CORRUPT, "no SCRM signature")
end
local title = r:fixed(28)
r:seek(32)
local ordnum, insnum, patnum = r:u16le(), r:u16le(), r:u16le()
local flags, cwt, ffi = r:u16le(), r:u16le(), r:u16le()
r:seek(48)
local gv, speed, tempo, mv, _uc, dp = r:u8(), r:u8(), r:u8(), r:u8(), r:u8(), r:u8()
r:seek(64)
local chnset: { number } = {}
for i = 1, 32 do
chnset[i] = r:u8()
end
if r.err then
return nil, r.err
end
if ordnum > 256 or insnum > 256 or patnum > 256 then
return nil, Err.new(Err.CORRUPT, "bad header counts")
end
-- channels: enabled PCM channels (0..15); the count is the last used one
local nch = 0
for i = 1, 32 do
local c = chnset[i]
if c < 16 then
nch = i
end
end
if nch == 0 then
return nil, Err.new(Err.UNSUPPORTED, "no PCM channels (AdLib only)")
end
local m = Tracker.newModule("s3m", nch)
m.title = title
m.speed = if speed > 0 and speed < 255 then speed else 6
m.tempo = if tempo >= 33 then tempo else 125
m.globalVolume = math.min(gv, 64) * 2
m.mixVolume = math.max(mv % 128, 16)
if mv < 128 then
-- mono files play at 8 / 11 of the master volume (ST3's stereo boost is missing; OpenMPT does the same)
m.mixVolume = math.floor(m.mixVolume * 8 / 11 + 0.5)
end
m.linear = false
m.instrumentMode = false
m.flags.fastSlides = cwt == 0x1300 or bit32.band(flags, 64) ~= 0
m.flags.amigaLimits = bit32.band(flags, 16) ~= 0
m.meta.source = { format = "s3m", tracker = cwt, stereo = mv >= 128 }
-- orders, parapointers, panning
r:seek(96)
for i = 1, ordnum do
local o = r:u8()
table.insert(m.order, o)
end
local insPtr, patPtr = {}, {}
for i = 1, insnum do
insPtr[i] = r:u16le() * 16
end
for i = 1, patnum do
patPtr[i] = r:u16le() * 16
end
local pans: { number } = {}
if dp == 252 then
for i = 1, 32 do
pans[i] = r:u8()
end
end
if r.err then
return nil, r.err
end
-- drop trailing end markers; keep inner "+++" / "---"
while #m.order > 0 and m.order[#m.order] == 255 do
table.remove(m.order)
end
local stereo = mv >= 128
for i = 1, nch do
local c = chnset[i]
local pan = 128
if stereo then
pan = if c % 16 >= 8 then 0xCC else 0x33
local p = pans[i]
if p and bit32.band(p, 0x20) ~= 0 then
pan = (p % 16) * 0x11
end
end
m.channelPan[i] = pan
m.channelMute[i] = c >= 16
end
-- samples
local total = 0
local unsigned = ffi ~= 1
for i = 1, insnum do
local s = Tracker.newSample()
m.samples[i] = s
local h = r:sub(insPtr[i], 80)
if h:size() < 80 then
return nil, Err.new(Err.TRUNCATED, "sample header " .. i, insPtr[i])
end
local typ = h:u8()
h:skip(12)
local hi = h:u8()
local lo = h:u16le()
local len, ls, le = h:u32le(), h:u32le(), h:u32le()
local vol = h:u8()
h:skip(1)
local pack, sflags = h:u8(), h:u8()
local c2spd = h:u32le()
h:seek(48)
s.name = h:fixed(28)
s.volume = math.min(vol, 64)
s.c5speed = if c2spd > 0 then c2spd else 8363
if typ == 1 and len > 0 and pack == 0 then
local bits = if bit32.band(sflags, 4) ~= 0 then 16 else 8
local nchs = if bit32.band(sflags, 2) ~= 0 then 2 else 1
local off = (hi * 65536 + lo) * 16
len = math.min(len, 2 ^ 26)
-- a short file: keep what is there
local avail = math.max(0, r:size() - off) // (nchs * bits // 8)
len = math.min(len, avail)
total += len * nchs * 4
if total > maxBytes then
return nil, Err.new(Err.LIMIT, "sample data over the maxBytes limit")
end
local data = Tracker.Pcm.decode(r.buf, r.base + off, len, { bits = bits, signed = not unsigned, channels = nchs, planar = true })
if data then
s.data, s.length, s.bits, s.channels = data, len, bits, nchs
if bit32.band(sflags, 1) ~= 0 and le > ls then
s.loopStart, s.loopEnd, s.loopType = ls, math.min(le, len), 1
end
end
end
end
-- patterns
for p = 1, patnum do
local pat = Tracker.newPattern(64, nch)
m.patterns[p] = pat
local ptr = patPtr[p]
if ptr > 0 then
local pr = r:sub(ptr)
local plen = pr:u16le()
pr = pr:sub(2, math.max(0, plen - 2))
local d = pat.data
local row = 0
while row < 64 and not pr:eof() do
local what = pr:u8()
if what == 0 then
row += 1
else
local ch = what % 32
local note, ins, vol, cmd, info = 0, 0, 0, 0, 0
if bit32.band(what, 0x20) ~= 0 then
local nb = pr:u8()
ins = pr:u8()
if nb == 254 then
note = 254
elseif nb < 254 and nb % 16 < 12 then
note = (nb // 16) * 12 + nb % 16 + 13
if note > 120 then
note = 0
end
end
end
if bit32.band(what, 0x40) ~= 0 then
vol = math.min(pr:u8(), 64) + 1
end
if bit32.band(what, 0x80) ~= 0 then
cmd = pr:u8()
info = pr:u8()
if cmd > 26 then
cmd, info = 0, 0
end
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, cmd)
buffer.writeu8(d, o + 4, info)
end
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 S3m.open(r: Bytes.Reader, opts: any?): (Types.Decoder?, Err.Err?)
local m, err = S3m.decode(r, opts)
if not m then
return nil, err
end
return Tracker.render(m, opts)
end
return S3m