ws-music: music/s3m read + render (OpenMPT-matched master volume / mono rule / default panning), finer comparison bands, voice ownership fix
This commit is contained in:
1 parent
ac3e143ce8
commit
cf7c1b8ad2
12 files changed
+598
-15
No files matched your search
@@ -50,6 +50,18 @@ local list: { Entry } = {
|
||||
return require("@formats/music/mod")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "s3m",
|
||||
category = "music",
|
||||
path = "music/s3m",
|
||||
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 } },
|
||||
load = function()
|
||||
return require("@formats/music/s3m")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "sf2",
|
||||
category = "music",
|
||||
|
||||
@@ -23,7 +23,7 @@ local MOD, S3M, XM, IT = 1, 2, 3, 4
|
||||
Player.MOD, Player.S3M, Player.XM, Player.IT = MOD, S3M, XM, IT
|
||||
local FAMILY = { mod = MOD, s3m = S3M, xm = XM, it = IT }
|
||||
|
||||
local MAX_VOICES = 96 -- channels + IT background voices
|
||||
local BG_VOICES = 64 -- voices beyond one per channel, for IT new note actions
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ state
|
||||
local function newVoice(): any
|
||||
@@ -86,6 +86,7 @@ local function newVoice(): any
|
||||
tremorMute = false,
|
||||
surround = false,
|
||||
rampN = 0,
|
||||
owner = 0, -- the channel whose foreground voice this is (0: free or background)
|
||||
order = 0, -- allocation age
|
||||
}
|
||||
end
|
||||
@@ -225,7 +226,7 @@ function Player.new(m: any, sampleRate: number, opts: any): any
|
||||
end
|
||||
st.chans[i] = c
|
||||
end
|
||||
for i = 1, MAX_VOICES do
|
||||
for i = 1, m.channels + BG_VOICES do
|
||||
st.voices[i] = newVoice()
|
||||
end
|
||||
st.master = Player.masterGain(m, fmt)
|
||||
@@ -239,7 +240,7 @@ function Player.masterGain(m: any, fmt: number): number
|
||||
if fmt == IT then
|
||||
return 0.5 * (m.mixVolume or 48) / 48
|
||||
elseif fmt == S3M then
|
||||
return 0.5 * (m.mixVolume or 48) / 48
|
||||
return (m.mixVolume or 48) / 128 -- OpenMPT: linear in the master volume, 64 = 0.5
|
||||
end
|
||||
-- MOD: OpenMPT scales by the channel count (4 channels: 0.5; 2 / channels between 2 and 8)
|
||||
return 2 / clamp(m.channels, 2, 8)
|
||||
@@ -361,7 +362,7 @@ end
|
||||
local function allocVoice(st: any): any
|
||||
local best, bestScore = nil, math.huge
|
||||
for _, v in st.voices do
|
||||
if not v.active then
|
||||
if not v.active and v.owner == 0 then
|
||||
return v
|
||||
end
|
||||
if v.bg then
|
||||
@@ -375,7 +376,17 @@ local function allocVoice(st: any): any
|
||||
best.active = false
|
||||
return best
|
||||
end
|
||||
return st.voices[1] -- unreachable while MAX_VOICES > channels: every channel holds one foreground voice
|
||||
-- every voice is busy in the foreground (more channels than voices): reuse an inactive one anyway
|
||||
for _, v in st.voices do
|
||||
if not v.active then
|
||||
local c = st.chans[v.owner]
|
||||
if c and c.voice == v then
|
||||
c.voice = nil
|
||||
end
|
||||
return v
|
||||
end
|
||||
end
|
||||
return st.voices[1] -- unreachable: there are more voices than channels
|
||||
end
|
||||
|
||||
local function keyOff(st: any, v: any)
|
||||
@@ -422,6 +433,7 @@ local function startVoice(st: any, c: any, smp: any, smpIdx: number, ins: any, i
|
||||
if v == nil or v.bg then
|
||||
v = allocVoice(st)
|
||||
c.voice = v
|
||||
v.owner = c.idx
|
||||
end
|
||||
local wasActive = v.active
|
||||
v.active = smp.length > 0 and smp.data ~= nil
|
||||
@@ -494,6 +506,7 @@ local function newNoteAction(st: any, c: any, ins: any, insIdx: number, note: nu
|
||||
return
|
||||
end
|
||||
v.bg = true
|
||||
v.owner = 0
|
||||
c.voice = nil
|
||||
if nna == 2 then
|
||||
keyOff(st, v)
|
||||
|
||||
@@ -0,0 +1,241 @@
|
||||
--!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, info = pr:u8(), 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
|
||||
Reference in new issue
Block a user