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
@@ -80,6 +80,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
|
||||
| music | mtm | .mtm | yes | - | render: true, channels: 1, 32 | `tests/music/mtm.luau` |
|
||||
| music | s3m | .s3m | yes | - | render: true, channels: 1, 32 | `tests/music/s3m.luau` |
|
||||
| music | sf2 | .sf2 | yes | - | sm24: true, sf3: false | `tests/music/sf2.luau` |
|
||||
| music | stm | .stm | yes | - | render: true, channels: 4, 4 | `tests/music/stm.luau` |
|
||||
| music | xm | .xm | yes | yes | render: true, channels: 1, 64 | `tests/music/xm.luau` |
|
||||
| playlist | m3u | .m3u .m3u8 | yes | yes | hls: true | `tests/playlist/m3u.luau` |
|
||||
| playlist | pls | .pls | yes | yes | | `tests/playlist/pls.luau` |
|
||||
|
||||
@@ -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
|
||||
@@ -0,0 +1,19 @@
|
||||
-- music/stm tests: generated Scream Tracker 2 modules (tools/fixtures/stm.py) parsed, rendered through
|
||||
-- codecs/audio/Tracker and compared with openmpt123's render (loudness envelope, band spectrum, stereo balance, song
|
||||
-- length), the contract, and the fuzz no-throw pass.
|
||||
local T = require("@tests/T")
|
||||
local S = require("@tests/music/_similar")
|
||||
local Stm = require("@formats/music/stm")
|
||||
local Cases = require("@tests/gen/stm_cases")
|
||||
|
||||
local t = T.suite("stm")
|
||||
|
||||
t:eq(Stm.name, "stm", "name")
|
||||
t:eq(Stm.category, "music", "category")
|
||||
t:check(Stm.caps.decode and not Stm.caps.encode, "caps")
|
||||
|
||||
local mods = S.against(t, Stm, Cases, { gainDb = 0.5, envErr = 1.0, spectral = 0.95, balance = 0.04, duration = 0.01 }, "s3m")
|
||||
t:check(mods[1] and mods[1].meta.source.tracker == "!Scream!", "tracker name")
|
||||
|
||||
t:fuzz("decode", S.fuzzer(Stm), S.samples(Cases), { mutations = 30 })
|
||||
t:done()
|
||||
@@ -575,3 +575,44 @@ def write_669(message, samples, orders, patterns, tempos, breaks, restart=0, ext
|
||||
for s in samples:
|
||||
out += s["data"]
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def write_stm(title, samples, orders, patterns, tempo=0x60, gvol=64, minor=21):
|
||||
"""samples: [{name, data (signed bytes), volume, c2spd, loop: (start, end) | None}] (up to 31); patterns: [[row:
|
||||
[(note, ins, vol, cmd, info)] * 4] * 64] with note 0 = none, 254 = cut, else our note (>= 37); vol None = none;
|
||||
cmd 0 = none (A = 1, letters as in S3M)"""
|
||||
head = bytearray(name(title, 20) + b"!Scream!" + bytes([0x1A, 2, 2, minor, tempo, len(patterns), gvol]) + b"\0" * 13)
|
||||
smp_at = len(head)
|
||||
head += b"\0" * (31 * 32)
|
||||
head += bytes(orders) + bytes([99] * ((64 if minor == 0 else 128) - len(orders)))
|
||||
for pat in patterns:
|
||||
for row in pat:
|
||||
for (note, ins, vol, cmd, info) in row:
|
||||
if note in ("skip", "fb"):
|
||||
head.append(0xFC if note == "skip" else 0xFB) # nothing / a C-0 at volume 0
|
||||
continue
|
||||
if note == 254:
|
||||
nb = 0xFE
|
||||
elif note:
|
||||
n = note - 37
|
||||
nb = (n // 12) << 4 | (n % 12)
|
||||
else:
|
||||
nb = 0xFF
|
||||
v = 65 if vol is None else vol
|
||||
head += bytes([nb, (ins << 3) | (v & 7), ((v >> 3) << 4) | cmd, info])
|
||||
body = head
|
||||
for i in range(31):
|
||||
h = bytearray(32)
|
||||
if i < len(samples):
|
||||
s = samples[i]
|
||||
while len(body) % 16:
|
||||
body.append(0)
|
||||
off = len(body) // 16
|
||||
body += s["data"]
|
||||
ls, le = s.get("loop") or (0, 0xFFFF)
|
||||
h[0:12] = name(s.get("name", ""), 12)
|
||||
struct.pack_into("<HHHHBBH", h, 14, off, len(s["data"]), ls, le, s.get("volume", 64), 0, s.get("c2spd", 8363))
|
||||
else:
|
||||
struct.pack_into("<HHHH", h, 14, 0, 0, 0, 0xFFFF)
|
||||
body[smp_at + 32 * i:smp_at + 32 * i + 32] = h
|
||||
return bytes(body)
|
||||
@@ -0,0 +1,90 @@
|
||||
# STM fixtures (tools/fixtures.py): generated Scream Tracker 2 modules exercising the effects, rendered by openmpt123.
|
||||
# tests/gen/stm_cases.luau { name, data (the module), ref (features of openmpt's render, tools/fixtures/_tracker.py),
|
||||
# duration (openmpt's song length) }
|
||||
import os, sys
|
||||
|
||||
# the shared tracker module, loaded by path (tools/fixtures/ must not go on sys.path: xml.py there would shadow xml)
|
||||
T = sys.modules.get("_tracker")
|
||||
if T is None:
|
||||
import importlib.util
|
||||
_spec = importlib.util.spec_from_file_location("_tracker", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_tracker.py"))
|
||||
T = importlib.util.module_from_spec(_spec)
|
||||
sys.modules["_tracker"] = T
|
||||
_spec.loader.exec_module(T)
|
||||
|
||||
INPUTS = ["tools/fixtures/_tracker.py"]
|
||||
|
||||
A, B, C, D, E, F, G, H, I, J = range(1, 11)
|
||||
|
||||
|
||||
def empty():
|
||||
return [[(0, 0, None, 0, 0) for _ in range(4)] for _ in range(64)]
|
||||
|
||||
|
||||
def put(p, row, ch, note=0, ins=0, vol=None, cmd=0, info=0):
|
||||
p[row][ch] = (note, ins, vol, cmd, info)
|
||||
|
||||
|
||||
def samples():
|
||||
return [
|
||||
{"name": "sine", "data": T.s8(T.synth("sine", 64, 64)), "volume": 64, "c2spd": 8363, "loop": (0, 64)},
|
||||
{"name": "square", "data": T.s8(T.synth("square", 64, 64, 0.5)), "volume": 40, "c2spd": 16726, "loop": (0, 64)},
|
||||
{"name": "saw", "data": T.s8(T.synth("saw", 100, 100, 0.6)), "volume": 64, "c2spd": 13000, "loop": (0, 100)},
|
||||
{"name": "noise", "data": T.s8(T.synth("noise", 6000, 1, 0.7, decay=1500)), "volume": 60, "c2spd": 8363},
|
||||
{"name": "tri", "data": T.s8(T.synth("tri", 50, 50, 0.8)), "volume": 50, "c2spd": 10000, "loop": (0, 50)},
|
||||
]
|
||||
|
||||
|
||||
def song_effects():
|
||||
a, b, c = empty(), empty(), empty()
|
||||
mel = [61, 65, 68, 73, 68, 65, 63, 61]
|
||||
for i in range(16):
|
||||
put(a, i * 4, 0, mel[i % 8], 1, None, J if i % 4 == 1 else 0, 0x47 if i % 4 == 1 else 0)
|
||||
put(a, 0, 1, 49, 2, 48)
|
||||
put(a, 4, 1, 0, 0, None, D, 0x04)
|
||||
put(a, 8, 1, 0, 0, None, D, 0x20)
|
||||
put(a, 16, 1, 51, 2, None, E, 0x03)
|
||||
put(a, 20, 1, 0, 0, None, F, 0x06)
|
||||
put(a, 28, 1, 56, 2, None, H, 0x46)
|
||||
put(a, 40, 1, 49, 2, None, G, 0x00)
|
||||
put(a, 41, 1, 61, 0, None, G, 0x10)
|
||||
put(a, 52, 1, 0, 0, None, I, 0x21)
|
||||
for r in range(0, 64, 8):
|
||||
put(a, r, 3, 61, 4, 64 - r // 2)
|
||||
put(a, 24, 3, 254)
|
||||
put(a, 48, 2, 37, 3)
|
||||
put(a, 32, 2, 0, 0, None, A, 0x40) # ST2 tempo byte: speed 4, a fine tick length
|
||||
put(b, 0, 2, 49, 3, None, A, 0x63)
|
||||
put(b, 8, 2, 52, 5)
|
||||
put(b, 10, 0, 64, 1)
|
||||
put(b, 20, 0, 61, 1, 30)
|
||||
put(b, 24, 0, 254)
|
||||
put(b, 16, 1, 0, 0, None, B, 0x02) # deferred: taken at the C below (unreachable orders would be openmpt subsongs)
|
||||
put(b, 40, 3, 0, 0, None, C, 0x08)
|
||||
put(c, 8, 0, 49, 1, 32)
|
||||
put(c, 12, 1, 61, 2)
|
||||
put(c, 20, 0, 0, 0, None, B, 0x00) # deferred to the end of the pattern
|
||||
put(c, 30, 2, 0, 0, None, A, 0x8F)
|
||||
return T.write_stm("effects", samples(), [0, 1, 2], [a, b, c], tempo=0x60)
|
||||
|
||||
|
||||
def song_old():
|
||||
"""an ST 2.00 file: decimal tempo bytes, a short order list, no global volume"""
|
||||
a = empty()
|
||||
put(a, 0, 0, 61, 1)
|
||||
put(a, 8, 1, 68, 2)
|
||||
put(a, 16, 0, 0, 0, None, H, 0x28)
|
||||
put(a, 32, 1, 0, 0, None, A, 50) # decimal 50 = 0x50
|
||||
put(a, 40, 2, 49, 5, 40)
|
||||
put(a, 44, 3, 56, 3)
|
||||
put(a, 52, 3, "fb")
|
||||
put(a, 2, 0, "skip")
|
||||
return T.write_stm("old", samples(), [0], [a], tempo=72, gvol=20, minor=0)
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
cases = []
|
||||
for nm, data in (("effects", song_effects()), ("old", song_old())):
|
||||
ref = T.reference(ctx, data, "stm")
|
||||
cases.append({"name": nm, "data": data, "ref": ref, "duration": ref["duration"]})
|
||||
ctx.emit("stm_cases", cases)
|
||||
Reference in new issue
Block a user