ws-audio2: music/stm (Scream Tracker 2): s3m family with the ST2 tempo byte, deferred Bxx, no effect memory
This commit is contained in:
1 parent
33d87e0c4d
commit
80f5fd2c5b
6 files changed
+396
No files matched your search
@@ -914,6 +914,18 @@ local list: { Entry } = {
|
||||
return require("@formats/music/sf2")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "stm",
|
||||
category = "music",
|
||||
path = "music/stm",
|
||||
extensions = { "stm" },
|
||||
mime = { "audio/x-stm" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/audio/Tracker", "codecs/audio/Tracker/Load" },
|
||||
caps = { decode = true, encode = false, render = true, channels = { 4, 4 } },
|
||||
load = function()
|
||||
return require("@formats/music/stm")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "xm",
|
||||
category = "music",
|
||||
|
||||
@@ -0,0 +1,233 @@
|
||||
--!strict
|
||||
-- music/stm: Scream Tracker 2 modules (STM, "!Scream!" / BMOD2STM / WUZAMOD! / SWavePro; 4 channels, 31 samples).
|
||||
-- Ported from OpenMPT's Load_stm.cpp (BSD, licenses/openmpt-LICENSE.txt). Reads into the codecs/audio/Tracker Module
|
||||
-- and plays as the "s3m" effect family with Scream Tracker 2's tempo (flags.st2Tempo: the speed byte also sets the
|
||||
-- tick length), no effect memory, no fine slides (the loader drops what ST2 ignores) and its deferred Bxx (the jump
|
||||
-- happens at the end of the pattern or at the next Cxx).
|
||||
-- Not supported: STX (Scream Tracker Music Interface Kit), ST2's noise for samples at volume 0 (silent here), 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 Stm = {
|
||||
name = "stm",
|
||||
category = "music",
|
||||
extensions = { "stm" },
|
||||
mime = { "audio/x-stm" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/audio/Tracker", "codecs/audio/Tracker/Load" },
|
||||
caps = { decode = true, encode = false, render = true, channels = { 4, 4 } },
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- refuse more sample data in total (f32 bytes, default 2^28)
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local IT = Load.IT
|
||||
-- .ABCDEFGHIJ (K..O do nothing)
|
||||
local EFFECTS = { "speed", "jump", "break", "volslide", "portadown", "portaup", "toneporta", "vibrato", "tremor", "arpeggio" }
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
local function validHeader(r: Bytes.Reader): boolean
|
||||
if r:size() < 48 then
|
||||
return false
|
||||
end
|
||||
local eof, ftype, major, minor = r:peekU8(28), r:peekU8(29), r:peekU8(30), r:peekU8(31)
|
||||
local npat, gv = r:peekU8(33), r:peekU8(34)
|
||||
if ftype ~= 2 or (eof ~= 0x1A and eof ~= 2) or major ~= 2 or (minor ~= 0 and minor ~= 10 and minor ~= 20 and minor ~= 21) then
|
||||
return false
|
||||
end
|
||||
if npat > 64 or (gv > 64 and gv ~= 0x58) then
|
||||
return false
|
||||
end
|
||||
for i = 20, 27 do
|
||||
local c = r:peekU8(i)
|
||||
if c < 0x20 or c >= 0x7F then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function Stm.sniff(r: Bytes.Reader): number
|
||||
return if validHeader(r) then 0.7 else 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- a tempo byte of files before ST 2.21 is decimal
|
||||
local function tempoByte(v: number, minor: number): number
|
||||
return if minor < 21 then (v // 10) * 16 + v % 10 else v
|
||||
end
|
||||
|
||||
function Stm.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() < 48 then Err.TRUNCATED else Err.CORRUPT, "not a Scream Tracker 2 module")
|
||||
end
|
||||
local title = r:fixed(20)
|
||||
local tracker = r:fixed(8)
|
||||
r:skip(3)
|
||||
local minor, initTempo, npat, gv = r:u8(), r:u8(), r:u8(), r:u8()
|
||||
r:skip(13)
|
||||
local m = Tracker.newModule("stm", 4, "s3m")
|
||||
m.title = title
|
||||
m.linear = false
|
||||
m.instrumentMode = false
|
||||
m.flags.st2Tempo = true
|
||||
initTempo = tempoByte(initTempo, minor)
|
||||
if initTempo == 0 then
|
||||
initTempo = 0x60
|
||||
end
|
||||
m.speed, m.tempo = Tracker.st2Tempo(initTempo)
|
||||
if minor > 10 then
|
||||
m.globalVolume = math.min(gv, 64) * 2
|
||||
end
|
||||
local offsets: { number } = {}
|
||||
for i = 1, 31 do
|
||||
local s = Tracker.newSample(r:fixed(12))
|
||||
local zero = r:u8()
|
||||
r:skip(1)
|
||||
local off, len, ls, le = r:u16le(), r:u16le(), r:u16le(), r:u16le()
|
||||
local vol = r:u8()
|
||||
r:skip(1)
|
||||
local rate = r:u16le()
|
||||
r:skip(6)
|
||||
if r.err then
|
||||
return nil, r.err
|
||||
end
|
||||
if zero ~= 0 and zero ~= 46 then
|
||||
return nil, Err.new(Err.CORRUPT, "bad sample header " .. i)
|
||||
end
|
||||
s.c5speed = if rate > 0 then rate else 8363
|
||||
s.volume = math.min(vol, 64)
|
||||
if len < 2 then
|
||||
len = 0
|
||||
end
|
||||
if ls < len and le > ls and le ~= 0xFFFF then
|
||||
s.loopStart, s.loopEnd, s.loopType = ls, le, 1
|
||||
len = math.max(le, len) -- ST2 lets a loop run into the next sample's data
|
||||
end
|
||||
s.length = len
|
||||
offsets[i] = off * 16
|
||||
m.samples[i] = s
|
||||
end
|
||||
for i = 1, (if minor == 0 then 64 else 128) do
|
||||
local o = r:u8()
|
||||
if o == 99 or o == 255 then
|
||||
o = 255
|
||||
elseif o > 63 then
|
||||
return nil, Err.new(Err.CORRUPT, "bad order list")
|
||||
end
|
||||
m.order[i] = o
|
||||
end
|
||||
if r.err then
|
||||
return nil, r.err
|
||||
end
|
||||
while #m.order > 0 and m.order[#m.order] == 255 do
|
||||
table.remove(m.order)
|
||||
end
|
||||
-- patterns: 64 rows x 4 channels; one byte 0xFB (C-0, volume 0), 0xFC (nothing) or 0xFD (note cut), else 4 bytes
|
||||
for p = 0, npat - 1 do
|
||||
local pat = Tracker.newPattern(64, 4)
|
||||
m.patterns[p + 1] = pat
|
||||
local d = pat.data
|
||||
local breakPos, breakRow = -1, 63 -- ST2: Bxx only sets the next order, taken at the pattern's end or a Cxx
|
||||
for row = 0, 63 do
|
||||
for ch = 0, 3 do
|
||||
local q = (row * 4 + ch) * 5
|
||||
local note = r:u8()
|
||||
local insVol, volCmd, info = 0, 0, 0
|
||||
if note == 0xFB then
|
||||
note = 0 -- a C-0 at volume 0 (OpenMPT reads it so)
|
||||
elseif note == 0xFD then
|
||||
buffer.writeu8(d, q, 254)
|
||||
elseif note ~= 0xFC then
|
||||
insVol, volCmd, info = r:u8(), r:u8(), r:u8()
|
||||
end
|
||||
if note ~= 0xFC and note ~= 0xFD then
|
||||
if note == 0xFE then
|
||||
buffer.writeu8(d, q, 254)
|
||||
elseif note < 0x60 then
|
||||
buffer.writeu8(d, q, (note // 16) * 12 + note % 16 + 37)
|
||||
end
|
||||
local ins = insVol // 8
|
||||
buffer.writeu8(d, q + 1, if ins > 31 then 0 else ins)
|
||||
local vol = insVol % 8 + (volCmd // 16) * 8
|
||||
if vol <= 64 then
|
||||
buffer.writeu8(d, q + 2, vol + 1)
|
||||
end
|
||||
local name, param = EFFECTS[volCmd % 16], info
|
||||
if name == "volslide" then
|
||||
param = if param % 16 > 0 then param % 16 else param - param % 16
|
||||
elseif name == "break" then
|
||||
param = ((param - param % 16) * 10 + param % 16) % 256
|
||||
if breakPos >= 0 and param == 0 then
|
||||
name, param = "jump", breakPos -- Bxx + C00 is a plain Bxx
|
||||
breakPos = -1
|
||||
end
|
||||
breakRow = math.min(breakRow, row)
|
||||
if name == "break" then
|
||||
param = Load.bcd(param)
|
||||
end
|
||||
elseif name == "jump" then
|
||||
breakPos, breakRow = param, 63
|
||||
name = nil
|
||||
elseif name == "speed" then
|
||||
param = tempoByte(param, minor)
|
||||
if param == 0 then
|
||||
name = nil
|
||||
end
|
||||
elseif name ~= "tremor" and param == 0 then
|
||||
name = nil -- no effect memory in ST2
|
||||
end
|
||||
if name then
|
||||
buffer.writeu8(d, q + 3, IT[name])
|
||||
buffer.writeu8(d, q + 4, param)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if r.err then
|
||||
return nil, r.err
|
||||
end
|
||||
if breakPos >= 0 then
|
||||
Load.writeEffect(m, p, breakRow, IT.jump, breakPos, "prev")
|
||||
end
|
||||
end
|
||||
-- sample data: signed 8-bit at the paragraph offsets (samples at volume 0 are not read)
|
||||
local buf = r.buf
|
||||
for i = 1, 31 do
|
||||
local s = m.samples[i]
|
||||
local len = s.length
|
||||
s.length = 0
|
||||
if len > 0 and s.volume > 0 and offsets[i] > 48 then
|
||||
local ok, err = Load.readSample(buf, r.base + offsets[i], len, { bits = 8, signed = true, limit = r.limit }, s, budget)
|
||||
if not ok then
|
||||
return nil, err
|
||||
end
|
||||
end
|
||||
end
|
||||
m.meta.source = { format = "stm", tracker = tracker, version = "2." .. minor }
|
||||
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 Stm.open(r: Bytes.Reader, opts: any?): (Types.Decoder?, Err.Err?)
|
||||
local m, err = Stm.decode(r, opts)
|
||||
if not m then
|
||||
return nil, err
|
||||
end
|
||||
return Tracker.render(m, opts)
|
||||
end
|
||||
|
||||
return Stm
|
||||
Reference in new issue
Block a user