ws-audio2: music/mtm (MultiTracker) on the tracker engine; engine: m.engine family, m.preamp, flags (periods amiga/hz, fastPortas, autoTonePorta, autoReset, st2Tempo, oktSlides), extension effects (note slides, auto portamento, frequency adjust), codecs/audio/Tracker/Load helpers (OpenMPT BSD)

Engine changes are backward compatible: m.engine defaults to m.format; the channel / sample / instrument
global volumes now apply in every family (defaults leave MOD / S3M / XM unchanged); mod/s3m/xm/it tests green.
This commit is contained in:
4DBug committed 2026-10-31 14:11:20 -05:00
1 parent de4f14a343
commit 384cdc407d
13 files changed
+1026 -52

No files matched your search

+164 -40
View File
@@ -23,6 +23,19 @@ 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 }
-- engine extension effects: format-specific commands of the other tracker formats, in any family (see
-- codecs/audio/Tracker)
local EXT = {
NOTE_SLIDE_UP = 0x40,
NOTE_SLIDE_DOWN = 0x41,
NOTE_SLIDE_UP_RETRIG = 0x42,
NOTE_SLIDE_DOWN_RETRIG = 0x43,
AUTO_PORTA_UP = 0x44,
AUTO_PORTA_DOWN = 0x45,
FREQ_ADJUST = 0x46,
}
Player.EXT = EXT
local BG_VOICES = 64 -- voices beyond one per channel, for IT new note actions
-- ------------------------------------------------------------------------------------------------ state
@@ -171,12 +184,20 @@ local function newChannel(i: number, pan: number, vol: number): any
pitchEnvOn = true,
muted = false,
triggered = false,
autoPorta = 0, -- auto portamento (EXT 0x44 / 0x45): period units per tick, + down
autoTone = false, -- flags.autoTonePorta: the tone portamento keeps running
hzAdd = 0, -- EXT 0x46: Hz added to the frequency (669)
slideCount = 0, -- note slides: ticks to the next step
mNoteSlide = 0,
tonePortaRow = false,
}
end
-- new(module, opts) -> player state (module validated by the caller)
function Player.new(m: any, sampleRate: number, opts: any): any
local fmt = FAMILY[m.format] or MOD
local fmt = FAMILY[m.engine or m.format] or MOD
local flags = m.flags
local ptPeriods = fmt == MOD or flags.periods == "amiga"
local st: any = {
m = m,
fmt = fmt,
@@ -189,8 +210,17 @@ function Player.new(m: any, sampleRate: number, opts: any): any
insMode = m.instrumentMode and #m.instruments > 0,
oldFx = m.flags.oldEffects or false,
compatG = m.flags.compatGxx or false,
fastSlides = m.flags.fastSlides or false,
amigaLimits = m.flags.amigaLimits or false,
fastSlides = flags.fastSlides or false,
amigaLimits = flags.amigaLimits or false,
ptPeriods = ptPeriods, -- ProTracker periods (integer table periods, PAL clock)
hz = flags.periods == "hz", -- periods are frequencies in Hz
hzScale = if type(flags.hzSlide) == "number" then flags.hzSlide else 1,
hzTone = if type(flags.hzTonePorta) == "number" then flags.hzTonePorta else 1,
fastPortas = flags.fastPortas or false,
autoTonePorta = flags.autoTonePorta or false,
autoReset = flags.autoReset or false,
st2Tempo = flags.st2Tempo or false,
oktSlides = flags.oktSlides or false,
ord = 0,
row = 0,
tick = 0,
@@ -214,7 +244,7 @@ function Player.new(m: any, sampleRate: number, opts: any): any
master = 1,
rows = 0,
ramp = 64,
amiga = if fmt == MOD then AMIGA_PAL else AMIGA,
amiga = if ptPeriods then AMIGA_PAL else AMIGA,
loopAllowed = opts and opts.loop or false,
}
for i = 1, m.channels do
@@ -240,6 +270,9 @@ end
-- the output scale per format, calibrated against OpenMPT's default mix levels (tools/fixtures: a full-scale sine at
-- volume 64 on one centred channel)
function Player.masterGain(m: any, fmt: number): number
if type(m.preamp) == "number" then
return m.preamp / 128 -- OpenMPT's sample pre-amp, as set by the format's loader
end
if fmt == IT or fmt == S3M then
return (m.mixVolume or 48) / 128 -- OpenMPT: linear in the mix / master volume, 64 = 0.5
end
@@ -335,10 +368,15 @@ local function notePeriod(st: any, note: number, smp: any, ft: number?): (number
end
end
local plin = (ref - note) * 64 - tr
if st.fmt == MOD then
if st.hz then
-- periods are frequencies (Hz, rounded as OpenMPT does): the sample's rate at C-5
return floor((smp.c5speed or 8363) * 2 ^ ((note - 61) / 12)), base
end
if st.ptPeriods then
-- ProTracker: integer periods from the finetune tables (finetune 0 table scaled by 2^(-ft / 96), rounded)
local ftp = (ft or smp.finetune or 0) / 16
return 4 * floor(Tables.noteToPeriod(note) * 2 ^ (-ftp / 12 / 8) + 0.5), base
local n = note + (if st.fmt == MOD or st.fmt == XM then smp.relativeNote or 0 else 0)
return 4 * floor(Tables.noteToPeriod(n) * 2 ^ (-ftp / 12 / 8) + 0.5), base
end
if st.linear then
return plin, base
@@ -350,6 +388,9 @@ local function arpPeriod(st: any, period: number, semis: number): number
if semis == 0 then
return period
end
if st.hz then
return period * 2 ^ (semis / 12)
end
if st.linear then
return period - semis * 64
end
@@ -357,7 +398,9 @@ local function arpPeriod(st: any, period: number, semis: number): number
end
local function clampPeriod(st: any, c: any)
if st.linear then
if st.hz then
c.period = clamp(c.period, 1, 1e6)
elseif st.linear then
c.period = clamp(c.period, -6000, 6000)
elseif st.amigaLimits then
c.period = clamp(c.period, 113 * 4, 856 * 4)
@@ -366,6 +409,17 @@ local function clampPeriod(st: any, c: any)
end
end
-- slide(st, c, units): a pitch slide in period units (positive: down; 4 per effect unit); in Hz mode the frequency
-- moves units * hzSlide Hz the other way
local function slide(st: any, c: any, units: number)
if st.hz then
c.period -= units * st.hzScale
else
c.period += units
end
clampPeriod(st, c)
end
-- ------------------------------------------------------------------------------------------------ voices
local function allocVoice(st: any): any
local best, bestScore = nil, math.huge
@@ -674,6 +728,7 @@ local function trigger(st: any, c: any, note: number, insIdx: number, vcmd: numb
keep = if c.voice.ins == ins then true else "release"
end
c.triggered = true
c.hzAdd = 0
c.tremorOn = true
c.retrigCount = 0
v = startVoice(st, c, smp, smpIdx, ins, useIns, realNote, period, base, keep)
@@ -758,6 +813,9 @@ local function tonePorta(st: any, c: any, speed: number)
if c.period == c.target then
return
end
if st.hz then
speed *= st.hzScale * st.hzTone
end
if c.period < c.target then
c.period = min(c.period + speed, c.target)
else
@@ -837,23 +895,21 @@ local function fxModXm(st: any, c: any, tick: number)
if xm and param > 0 then
c.mPortaUp = param
end
if tick > 0 then
c.period -= (if xm then c.mPortaUp else param) * 4
clampPeriod(st, c)
if tick > 0 or st.fastPortas then
slide(st, c, -(if xm then c.mPortaUp else param) * 4)
end
elseif fx == 2 then
if xm and param > 0 then
c.mPortaDn = param
end
if tick > 0 then
c.period += (if xm then c.mPortaDn else param) * 4
clampPeriod(st, c)
if tick > 0 or st.fastPortas then
slide(st, c, (if xm then c.mPortaDn else param) * 4)
end
elseif fx == 3 or fx == 5 then
if fx == 3 and param > 0 then
c.portaSpeed = param
end
if tick > 0 then
if tick > 0 or st.fastPortas then
tonePorta(st, c, c.portaSpeed * 4)
end
elseif fx == 4 or fx == 6 then
@@ -909,16 +965,14 @@ local function fxModXm(st: any, c: any, tick: number)
c.mFineUp = y
end
if tick == 0 then
c.period -= (if xm then c.mFineUp else y) * 4
clampPeriod(st, c)
slide(st, c, -(if xm then c.mFineUp else y) * 4)
end
elseif x == 2 then
if xm and y > 0 then
c.mFineDn = y
end
if tick == 0 then
c.period += (if xm then c.mFineDn else y) * 4
clampPeriod(st, c)
slide(st, c, (if xm then c.mFineDn else y) * 4)
end
elseif x == 4 then
c.vibWave = y % 8
@@ -1054,16 +1108,14 @@ local function fxModXm(st: any, c: any, tick: number)
c.mXFineUp = y
end
if tick == 0 then
c.period -= c.mXFineUp
clampPeriod(st, c)
slide(st, c, -c.mXFineUp)
end
elseif x == 2 then
if y > 0 then
c.mXFineDn = y
end
if tick == 0 then
c.period += c.mXFineDn
clampPeriod(st, c)
slide(st, c, c.mXFineDn)
end
end
end
@@ -1073,10 +1125,10 @@ local function fxModXm(st: any, c: any, tick: number)
if param > 0 then
c.mVolSlide = param
end
if tick > 0 then
if tick > 0 or st.fastSlides then
volSlideMod(c, c.mVolSlide)
end
elseif tick > 0 then
elseif tick > 0 or st.fastSlides then
volSlideMod(c, param)
end
end
@@ -1161,16 +1213,15 @@ end
local function portaS3m(st: any, c: any, p: number, tick: number, sign: number)
if p >= 0xF0 then
if tick == 0 then
c.period += sign * (p % 16) * 4
slide(st, c, sign * (p % 16) * 4)
end
elseif p >= 0xE0 then
if tick == 0 then
c.period += sign * (p % 16)
slide(st, c, sign * (p % 16))
end
elseif tick > 0 then
c.period += sign * p * 4
elseif tick > 0 or st.fastPortas then
slide(st, c, sign * p * 4)
end
clampPeriod(st, c)
end
local function fxS3mIt(st: any, c: any, tick: number)
@@ -1178,7 +1229,11 @@ local function fxS3mIt(st: any, c: any, tick: number)
local it = st.fmt == IT
if fx == 1 then -- A speed
if tick == 0 and param > 0 then
st.speed = param
if st.st2Tempo then
st.speed, st.tempo = Tables.st2Tempo(param)
else
st.speed = param
end
end
elseif fx == 2 then -- B jump
if tick == 0 then
@@ -1446,7 +1501,7 @@ local function fxS3mIt(st: any, c: any, tick: number)
c.portaSpeed = c.mEF
end
end
if tick > 0 then
if tick > 0 or st.fastPortas then
tonePorta(st, c, c.portaSpeed * 4)
end
end
@@ -1514,8 +1569,7 @@ local function volColIt(st: any, c: any, tick: number)
c.mEF = p
end
if tick > 0 then
c.period += c.mEF * 4
clampPeriod(st, c)
slide(st, c, c.mEF * 4)
end
elseif v <= 124 then
local p = (v - 115) * 4
@@ -1523,8 +1577,7 @@ local function volColIt(st: any, c: any, tick: number)
c.mEF = p
end
if tick > 0 then
c.period -= c.mEF * 4
clampPeriod(st, c)
slide(st, c, -c.mEF * 4)
end
elseif v >= 193 and v <= 202 then
local p = Tables.IT_VOLPORTA[v - 192] or 0
@@ -1550,6 +1603,62 @@ local function volColIt(st: any, c: any, tick: number)
end
end
-- ------------------------------------------------------------------------------------------------ extension effects
-- the engine extension effects (EXT, codes 0x40..) and the auto slides, in any family
local function fxExt(st: any, c: any, tick: number)
local fx, param = c.fx, c.param
if fx >= EXT.NOTE_SLIDE_UP and fx <= EXT.NOTE_SLIDE_DOWN_RETRIG then
-- note slides (PTM, Oktalyzer): every (param >> 4) ticks by (param & 15) semitones; Oktalyzer (flags.oktSlides):
-- 1x = every tick, 5x = once on the first tick
if tick == 0 then
if param // 16 > 0 then
c.mNoteSlide = (param // 16) * 16 + c.mNoteSlide % 16
end
if param % 16 > 0 then
c.mNoteSlide = (c.mNoteSlide // 16) * 16 + param % 16
end
c.slideCount = c.mNoteSlide // 16
end
local p = c.mNoteSlide
local go
if st.oktSlides then
go = p // 16 == 1 or tick == 0
else
go = false
if tick > 0 then
c.slideCount -= 1
go = c.slideCount == 0
end
end
if go and c.smp and c.realNote > 0 then
c.slideCount = p // 16
local up = fx == EXT.NOTE_SLIDE_UP or fx == EXT.NOTE_SLIDE_UP_RETRIG
c.realNote = clamp(c.realNote + (if up then p % 16 else -(p % 16)), 1, 120)
c.period = notePeriod(st, c.realNote, c.smp, c.finetune)
if fx >= EXT.NOTE_SLIDE_UP_RETRIG then
retrigVoice(st, c)
end
end
elseif fx == EXT.AUTO_PORTA_UP or fx == EXT.AUTO_PORTA_DOWN then
if tick == 0 then
c.autoPorta = (if fx == EXT.AUTO_PORTA_UP then -param else param) * 4
end
elseif fx == EXT.FREQ_ADJUST then
if tick == 0 then
c.hzAdd = param * 80
end
end
-- auto slides: every tick (fast portamentos) or every tick but the first
if tick > 0 or st.fastPortas then
if c.autoPorta ~= 0 then
slide(st, c, c.autoPorta)
end
if c.autoTone and not c.tonePortaRow then
tonePorta(st, c, c.portaSpeed * 4)
end
end
end
-- ------------------------------------------------------------------------------------------------ rows and ticks
local function noteDelayOf(st: any, fx: number, param: number): number
if (st.fmt == MOD or st.fmt == XM) and fx == 0xE and param // 16 == 0xD then
@@ -1573,6 +1682,9 @@ end
local function runEffects(st: any, c: any, tick: number)
c.vibDelta, c.arp = 0, 0
if (c.fx >= 0x40 and c.fx < 0x50) or c.autoPorta ~= 0 or c.autoTone then
fxExt(st, c, tick)
end
if st.fmt == MOD or st.fmt == XM then
fxModXm(st, c, tick)
if st.fmt == XM then
@@ -1621,6 +1733,15 @@ local function processRow(st: any)
end
c.fx, c.param, c.vcmd = fx, param, vcmd
c.cutTick, c.keyOffTick = -1, -1
if st.autoReset and fx ~= 0 and fx ~= 255 then
-- 669: any effect stops the auto slides
c.autoPorta, c.autoTone = 0, false
end
local tp = isPorta(st, fx, 0)
c.tonePortaRow = tp
if tp and st.autoTonePorta then
c.autoTone = param ~= 0 or not st.autoReset
end
c.triggered = false
local delay = noteDelayOf(st, fx, param)
c.finetune = finetuneOf(st, fx, param)
@@ -1750,7 +1871,9 @@ local function updateVoice(st: any, v: any)
-- frequency
local P = v.periodOut + avDelta
local freq
if st.linear then
if st.hz then
freq = max(0, P + (v.hzAdd or 0))
elseif st.linear then
freq = v.base * 2 ^ (-P / 768)
else
freq = if P > 0 then st.amiga / P else 0
@@ -1762,9 +1885,9 @@ local function updateVoice(st: any, v: any)
-- volume
local smp = v.smp
local vol = v.vol / 64 * envVol / 64 * v.fade / 65536 * st.gvol / 128
if it then
vol *= v.chanVol / 64 * (if smp then (smp.globalVolume or 64) / 64 else 1) * (if ins then (ins.globalVolume or 128) / 128 else 1)
end
-- channel, sample and instrument global volumes (64 / 64 / 128 unless an IT file, or another format's loader,
-- sets them)
vol *= v.chanVol / 64 * (if smp then (smp.globalVolume or 64) / 64 else 1) * (if ins then (ins.globalVolume or 128) / 128 else 1)
if v.tremorMute then
vol = 0
end
@@ -1853,8 +1976,9 @@ function Player.tick(st: any): number
local c = st.chans[i]
local v = c.voice
if v and v.active and v.chan == i and not v.bg then
local P = arpPeriod(st, c.period, c.arp) + c.vibDelta
local P = arpPeriod(st, c.period, c.arp) + (if st.hz then -c.vibDelta * st.hzScale else c.vibDelta)
v.period, v.periodOut, v.base = c.period, P, c.base
v.hzAdd = c.hzAdd
v.vol = clamp(c.vol + c.tremDelta, 0, 64)
if c.muted then
v.vol = 0