Files
rosetta/src/Formats/codecs/Tracker/Player.luau
T

1891 lines
46 KiB
Luau

--!strict
--!native
-- codecs/Tracker/Player: the sequencer of the tracker engine. One player for MOD, S3M, XM and IT: rows, orders,
-- speed / tempo, every effect of the four formats (interpreted per format family: ProTracker / FastTracker 2 numeric
-- effects, ScreamTracker 3 / Impulse Tracker letter effects), instruments with volume / panning / pitch envelopes,
-- fadeout and auto-vibrato, IT new note actions (background voices), duplicate checks and resonant filters, and song
-- end / loop detection. Written from the format documents (ProTracker 2.3 docs, the XM spec and FT2 behaviour notes,
-- ST3 TECH.DOC, ITTECH.TXT); no third-party code. Mixing is codecs/Tracker/Mixer.
--
-- Never throws: the module is validated by codecs/Tracker before a player is made, and every table lookup here is
-- guarded.
local Tables = require("@formats/codecs/Tracker/Tables")
local wave64, autoWave = Tables.wave64, Tables.autoWave
local AMIGA = Tables.AMIGA
local AMIGA_PAL = Tables.AMIGA_PAL
local floor, min, max, clamp, abs = math.floor, math.min, math.max, math.clamp, math.abs
local readu8 = buffer.readu8
local Player = {}
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 BG_VOICES = 64 -- voices beyond one per channel, for IT new note actions
-- ------------------------------------------------------------------------------------------------ state
local function newVoice(): any
return {
active = false,
chan = 0,
bg = false,
data = nil :: buffer?,
nch = 1,
len = 0,
loopS = 0,
loopE = 0,
loopT = 0,
susS = 0,
susE = 0,
susT = 0,
p = 0,
dir = 1,
inc = 0,
smp = nil :: any,
smpIdx = 0,
ins = nil :: any,
insIdx = 0,
note = 0,
vol = 0,
pan = 128,
chanVol = 64,
period = 0,
periodOut = 0,
base = 8363,
keyOn = true,
fading = false,
fade = 65536,
volEnvPos = 0,
panEnvPos = 0,
pitchEnvPos = 0,
volEnvOn = true,
panEnvOn = true,
pitchEnvOn = true,
envVol = 64,
envPan = 0,
envPitch = 0,
avPos = 0,
avAmp = 0,
gl = 0,
gr = 0,
tl = 0,
tr = 0,
started = false,
cutoff = 127,
reso = 0,
fOn = false,
fa = 1,
fb0 = 0,
fb1 = 0,
y1l = 0,
y2l = 0,
y1r = 0,
y2r = 0,
tremorMute = false,
surround = false,
rampN = 0,
owner = 0, -- the channel whose foreground voice this is (0: free or background)
order = 0, -- allocation age
}
end
local function newChannel(i: number, pan: number, vol: number): any
return {
idx = i,
voice = nil :: any,
note = 0,
realNote = 0,
insIdx = 0,
smpIdx = 0,
smp = nil :: any,
vol = 0,
pan = pan,
basePan = pan,
chanVol = vol,
period = 0,
target = 0,
portaSpeed = 0,
base = 8363,
finetune = nil :: number?,
fx = 0,
param = 0,
vcmd = 0,
-- memories
mPortaUp = 0,
mPortaDn = 0,
mFineUp = 0,
mFineDn = 0,
mXFineUp = 0,
mXFineDn = 0,
mVolSlide = 0,
mFineVolUp = 0,
mFineVolDn = 0,
mOffset = 0,
mHighOffset = 0,
mPanSlide = 0,
mGVolSlide = 0,
mRetrig = 0,
mTremor = 0,
mArp = 0,
mS3M = 0,
mEF = 0,
mChanVolSlide = 0,
mTempo = 0,
mVolCol = 0,
mS = 0,
vibSpeed = 0,
vibDepth = 0,
vibPos = 0,
vibWave = 0,
tremSpeed = 0,
tremDepth = 0,
tremPos = 0,
tremWave = 0,
panbSpeed = 0,
panbDepth = 0,
panbPos = 0,
panbWave = 0,
vibDelta = 0,
tremDelta = 0,
panbDelta = 0,
arp = 0,
loopRow = 0,
loopCount = 0,
retrigCount = 0,
tremorCount = 0,
tremorOn = true,
delayTick = -1,
dNote = 0,
dIns = 0,
dVol = 0,
cutTick = -1,
keyOffTick = -1,
nna = -1,
surround = false,
cutoff = 127,
reso = 0,
volEnvOn = true,
panEnvOn = true,
pitchEnvOn = true,
muted = false,
triggered = 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 st: any = {
m = m,
fmt = fmt,
sr = sampleRate,
nch = m.channels,
speed = m.speed,
tempo = m.tempo,
gvol = m.globalVolume,
linear = m.linear,
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,
ord = 0,
row = 0,
tick = 0,
pat = nil :: any,
patIdx = -1,
patDelay = 0,
patDelayRep = 0,
fineDelay = 0,
jumpOrd = -1,
ft2BreakPos = 0,
breakRow = -1,
loopJump = -1,
ended = false,
visited = {} :: { [number]: boolean },
chans = {},
voices = {},
age = 0,
tickFrames = 0,
frac = 0,
separation = math.clamp((opts and opts.stereoSeparation or 100) / 100, 0, 2),
master = 1,
rows = 0,
ramp = 64,
amiga = if fmt == MOD then AMIGA_PAL else AMIGA,
loopAllowed = opts and opts.loop or false,
}
for i = 1, m.channels do
local pan = m.channelPan[i] or 128
local cv = m.channelVolume[i] or 64
local c = newChannel(i, pan, cv)
c.basePan = pan
c.muted = m.channelMute[i] or false
if pan == 512 then -- surround marker
c.pan = 128
c.surround = true
end
st.chans[i] = c
end
for i = 1, m.channels + BG_VOICES do
st.voices[i] = newVoice()
end
st.master = Player.masterGain(m, fmt)
Player.startOrder(st, 0, 0)
return st
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 fmt == IT or fmt == S3M then
return (m.mixVolume or 48) / 128 -- OpenMPT: linear in the mix / master volume, 64 = 0.5
end
if fmt == XM then
return 0.375 -- OpenMPT's XM pre-amp (48 / 128), whatever the channel count
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)
end
-- ------------------------------------------------------------------------------------------------ orders and rows
-- startOrder(st, ord, row): resolve skip markers / the end of the order list; sets st.pat or st.ended
function Player.startOrder(st: any, ord: number, row: number)
local m = st.m
local order = m.order
local n = #order
local guard = 0
local o = ord
while true do
guard += 1
if guard > n + 2 then
st.ended = true
return
end
if o >= n then
o = m.restart or 0
if o >= n or o < 0 then
o = 0
end
if n == 0 then
st.ended = true
return
end
end
local p = order[o + 1]
if p == 255 then
o = n -- the end marker: wrap to the restart position
elseif p == 254 or m.patterns[p + 1] == nil then
o += 1
else
st.ord = o
st.patIdx = p
st.pat = m.patterns[p + 1]
st.row = if row < st.pat.rows then row else 0
return
end
end
end
local function advanceRow(st: any)
if st.stopAfterRow then
st.ended = true
return
end
local loopRow = st.loopJump
if st.jumpOrd >= 0 or st.breakRow >= 0 then
local o = if st.jumpOrd >= 0 then st.jumpOrd else st.ord + 1
local r = if st.breakRow >= 0 then st.breakRow else 0
st.jumpOrd, st.breakRow, st.loopJump = -1, -1, -1
st.ft2BreakPos = 0
Player.startOrder(st, o, r)
return
end
if loopRow >= 0 then
st.loopJump = -1
-- the rows inside a pattern loop are played again on purpose: forget them for the song-end check
for r = loopRow, st.row do
st.visited[st.ord * 1024 + r] = nil
end
st.row = loopRow
return
end
st.row += 1
if st.row >= st.pat.rows then
-- FT2 (XM): the next pattern starts at the row a pattern loop last jumped to (the E6x "bug" FT2 players keep)
local r = if st.fmt == XM then st.ft2BreakPos else 0
st.ft2BreakPos = 0
Player.startOrder(st, st.ord + 1, r)
end
end
-- ------------------------------------------------------------------------------------------------ pitch
-- the period of a note for a sample (see codecs/Tracker for the two period spaces)
local function notePeriod(st: any, note: number, smp: any, ft: number?): (number, number)
local base, ref, tr
if st.fmt == MOD or st.fmt == XM then
base, ref = 8363, 49
tr = (smp.relativeNote or 0) * 64 + (ft or smp.finetune or 0) / 2
else
base, ref, tr = smp.c5speed or 8363, 61, 0
if base <= 0 then
base = 8363
end
end
local plin = (ref - note) * 64 - tr
if st.fmt == MOD 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
end
if st.linear then
return plin, base
end
return 1712 * 8363 / base * 2 ^ (plin / 768), base
end
local function arpPeriod(st: any, period: number, semis: number): number
if semis == 0 then
return period
end
if st.linear then
return period - semis * 64
end
return period * 2 ^ (-semis / 12)
end
local function clampPeriod(st: any, c: any)
if st.linear then
c.period = clamp(c.period, -6000, 6000)
elseif st.amigaLimits then
c.period = clamp(c.period, 113 * 4, 856 * 4)
else
c.period = clamp(c.period, 1, 1e6)
end
end
-- ------------------------------------------------------------------------------------------------ voices
local function allocVoice(st: any): any
local best, bestScore = nil, math.huge
for _, v in st.voices do
if not v.active and v.owner == 0 then
return v
end
if v.bg then
local s = (v.vol * v.envVol * v.fade) / 1e9 + v.order * 1e-9
if s < bestScore then
best, bestScore = v, s
end
end
end
if best then
best.active = false
return best
end
-- 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)
if not v or not v.active then
return
end
v.keyOn = false
local ins = v.ins
if st.fmt == XM then
if not (ins and ins.volEnv and ins.volEnv.enabled) then
v.vol = 0
local c = st.chans[v.chan]
if c and c.voice == v then
c.vol = 0
end
else
v.fading = true
end
elseif st.fmt == IT then
if ins then
local env = ins.volEnv
if not (env and env.enabled and v.volEnvOn) or env.loop then
v.fading = true
end
end
end
end
local function noteFade(v: any)
if v and v.active then
v.fading = true
end
end
local function cutVoice(v: any)
if v then
v.active = false
end
end
-- start(st, c, smp, smpIdx, ins, insIdx, note, period, base, offset): a fresh voice for channel c
local function startVoice(st: any, c: any, smp: any, smpIdx: number, ins: any, insIdx: number, note: number, period: number, base: number, keepEnv: any)
local v = c.voice
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
v.chan = c.idx
v.bg = false
v.smp = smp
v.smpIdx = smpIdx
v.data = smp.data
v.nch = smp.channels or 1
v.len = smp.length
v.loopS, v.loopE, v.loopT = smp.loopStart, smp.loopEnd, smp.loopType
v.susS, v.susE, v.susT = smp.susStart or 0, smp.susEnd or 0, smp.susType or 0
v.p = 0
v.dir = 1
v.ins = ins
v.insIdx = insIdx
v.note = note
v.period = period
v.periodOut = period
v.base = base
st.age += 1
v.order = st.age
if keepEnv ~= true then
if keepEnv ~= "release" then
v.keyOn = true
v.fading = false
end
v.fade = 65536
v.volEnvPos, v.panEnvPos, v.pitchEnvPos = 0, 0, 0
v.avPos, v.avAmp = 0, 0
v.envVol, v.envPan, v.envPitch = 64, 0, 0
v.volEnvOn, v.panEnvOn, v.pitchEnvOn = true, true, true
v.y1l, v.y2l, v.y1r, v.y2r = 0, 0, 0, 0
end
if not wasActive then
v.gl, v.gr = 0, 0
end
return v
end
-- IT new note actions on the channel's current voice (and duplicate checks on all its voices)
local function newNoteAction(st: any, c: any, ins: any, insIdx: number, note: number, smpIdx: number)
-- duplicate check: voices of this channel playing the same instrument
if ins and ins.dct and ins.dct > 0 then
for _, u in st.voices do
if u.active and u.chan == c.idx and u.insIdx == insIdx then
local dup = (ins.dct == 1 and u.note == note) or (ins.dct == 2 and u.smpIdx == smpIdx) or ins.dct == 3
if dup then
local a = ins.dca or 0
if a == 0 then
cutVoice(u)
elseif a == 1 then
keyOff(st, u)
else
noteFade(u)
end
end
end
end
end
local v = c.voice
if v == nil or not v.active then
return
end
local nna = c.nna
if nna < 0 then
nna = if v.ins then (v.ins.nna or 0) else 0
end
if nna == 0 then
-- cut: the voice is reused for the new note (a short click is avoided by its ramp from the old gains)
v.active = false
return
end
v.bg = true
v.owner = 0
c.voice = nil
if nna == 2 then
keyOff(st, v)
elseif nna == 3 then
noteFade(v)
end
end
-- ------------------------------------------------------------------------------------------------ triggering
local function isPorta(st: any, fx: number, vcmd: number): boolean
local f = st.fmt
if f == MOD then
return fx == 3 or fx == 5
elseif f == XM then
return fx == 3 or fx == 5 or (vcmd >= 0xF0)
end
-- S3M / IT: G (7) or L (12); IT volume column g (193..202, stored + 1)
return fx == 7 or fx == 12 or (f == IT and vcmd >= 194 and vcmd <= 203)
end
-- the sample and real note for an instrument number + note
local function resolve(st: any, insIdx: number, note: number): (any, number, any, number)
local m = st.m
if st.insMode then
local ins = m.instruments[insIdx]
if not ins or note < 1 or note > 120 then
return nil, 0, ins, note
end
local si = ins.sampleMap[note] or 0
local rn = ins.noteMap[note] or note
local smp = m.samples[si]
return smp, si, ins, rn
end
local smp = m.samples[insIdx]
return smp, insIdx, nil, note
end
local function setInstrumentDefaults(st: any, c: any, smp: any, ins: any)
if smp then
c.vol = smp.volume
end
if st.fmt == IT then
-- IT: a sample's or instrument's default panning holds for its notes; the channel panning (set by the
-- effects) comes back for the others
local p = (smp and smp.pan) or (ins and ins.pan)
c.pan = p or c.basePan
if p then
c.surround = false
end
elseif smp and smp.pan then
c.pan = smp.pan
c.basePan = c.pan
c.surround = false
end
if ins and st.fmt == IT then
if ins.cutoff then
c.cutoff = ins.cutoff
end
if ins.resonance then
c.reso = ins.resonance
end
end
end
local function sampleOffset(st: any, c: any, v: any)
local f = st.fmt
local fx, param = c.fx, c.param
local off = -1
if (f == MOD or f == XM) and fx == 9 then
if param > 0 then
c.mOffset = param
end
off = (if f == XM then c.mOffset else param) * 256
elseif (f == S3M or f == IT) and fx == 15 then
if param > 0 then
c.mOffset = param
end
off = c.mOffset * 256 + (if f == IT then c.mHighOffset * 65536 else 0)
end
if off > 0 and v and v.active then
if off >= v.len then
if v.loopT > 0 and f ~= MOD then
off = v.loopS
elseif f == MOD and v.loopT > 0 then
off = v.loopS
else
v.active = false
return
end
end
v.p = off
end
end
-- trigger(st, c, note, insIdx, vcmd): the note / instrument / volume column part of a cell
local function trigger(st: any, c: any, note: number, insIdx: number, vcmd: number)
local f = st.fmt
local m = st.m
local porta = isPorta(st, c.fx, vcmd)
local hasNote = note >= 1 and note <= 120
if insIdx > 0 then
local valid = if st.insMode then m.instruments[insIdx] ~= nil else m.samples[insIdx] ~= nil
if valid then
c.insIdx = insIdx
local n = if hasNote then note else c.note
local smp, _, ins = resolve(st, insIdx, if n > 0 then n else 61)
setInstrumentDefaults(st, c, smp, ins)
if not hasNote then
local v = c.voice
if f == XM and v then
-- an instrument number alone (FT2 / OpenMPT): the playing note's envelopes restart (key on, no
-- fadeout); the new instrument applies from the next note
v.keyOn, v.fading, v.fade = true, false, 65536
v.volEnvPos, v.panEnvPos, v.avPos, v.avAmp = 0, 0, 0, 0
elseif (f == MOD or f == S3M) and smp then
c.smp, c.smpIdx = smp, insIdx
end
end
elseif f == XM and hasNote then
-- an empty instrument: the note stops the channel
c.insIdx = insIdx
if c.voice then
c.voice.active = false
end
hasNote = false
end
end
if hasNote then
local useIns = c.insIdx
local smp, smpIdx, ins, realNote = resolve(st, useIns, note)
if smp == nil then
if (f == MOD or f == S3M) and c.smp then
smp, smpIdx = c.smp, c.smpIdx
end
end
if smp then
local ft = c.finetune
local period, base = notePeriod(st, realNote, smp, ft)
local v = c.voice
if porta and ((v and v.active) or f == MOD or f == XM) then
-- tone portamento: a new target, no new note (ProTracker / FT2 never start a note with it)
c.target = period
if st.fmt == IT or st.fmt == S3M then
c.note = note
end
else
if f == IT and st.insMode then
newNoteAction(st, c, ins, useIns, realNote, smpIdx)
end
-- FT2: a note without an instrument number restarts the sample only (envelopes, key off and
-- fadeout carry on); with another instrument than the playing one, its envelopes and fadeout start
-- over but a key off stays
local keep: any = false
if f == XM and insIdx == 0 and c.voice ~= nil then
keep = if c.voice.ins == ins then true else "release"
end
c.triggered = true
c.tremorOn = true
c.retrigCount = 0
v = startVoice(st, c, smp, smpIdx, ins, useIns, realNote, period, base, keep)
v.patNote = note
c.period, c.target, c.base = period, period, base
c.note, c.realNote = note, realNote
c.smp, c.smpIdx = smp, smpIdx
if insIdx > 0 or f == MOD or f == S3M then
-- volume was set by the instrument; MOD / S3M keep the channel volume
end
if c.vibWave < 4 then
c.vibPos = 0
end
if c.tremWave < 4 then
c.tremPos = 0
end
v.cutoff, v.reso = c.cutoff, c.reso
v.volEnvOn, v.panEnvOn, v.pitchEnvOn = true, true, true
c.nna = -1
sampleOffset(st, c, v)
end
elseif f == IT or f == XM then
-- a note mapped to no sample cuts
if c.voice and not porta then
if f == IT and st.insMode then
newNoteAction(st, c, nil, 0, note, 0)
end
if c.voice then
c.voice.active = false
end
end
end
elseif note == 255 then
if f == S3M or f == MOD then
cutVoice(c.voice)
elseif c.voice then
keyOff(st, c.voice)
end
elseif note == 254 then
cutVoice(c.voice)
if f ~= IT then
c.vol = 0
end
elseif note == 253 then
noteFade(c.voice)
end
-- the volume column's set-volume / set-panning parts (the rest runs with the effects)
if vcmd > 0 then
if f == S3M then
c.vol = min(vcmd - 1, 64)
elseif f == XM then
if vcmd >= 0x10 and vcmd <= 0x50 then
c.vol = vcmd - 0x10
elseif vcmd >= 0xC0 and vcmd <= 0xCF then
c.pan = (vcmd - 0xC0) * 16
c.basePan = c.pan
end
elseif f == IT then
local v = vcmd - 1
if v <= 64 then
c.vol = v
elseif v >= 128 and v <= 192 then
c.pan = (v - 128) * 4
c.basePan = c.pan
c.surround = false
end
end
end
end
-- ------------------------------------------------------------------------------------------------ effect helpers
local function volSlideMod(c: any, param: number)
local x, y = param // 16, param % 16
if x > 0 then
c.vol = min(64, c.vol + x)
else
c.vol = max(0, c.vol - y)
end
end
local function tonePorta(st: any, c: any, speed: number)
if c.period == c.target then
return
end
if c.period < c.target then
c.period = min(c.period + speed, c.target)
else
c.period = max(c.period - speed, c.target)
end
end
local function vibrato(st: any, c: any, scale: number)
c.vibDelta = wave64(c.vibWave % 4, c.vibPos) * c.vibDepth * scale
c.vibPos = (c.vibPos + c.vibSpeed) % 64
end
local function tremolo(c: any, scale: number)
c.tremDelta = floor(wave64(c.tremWave % 4, c.tremPos) * c.tremDepth * scale)
c.tremPos = (c.tremPos + c.tremSpeed) % 64
end
local function retrigVoice(st: any, c: any)
local v = c.voice
if v and v.smp then
v.active = v.smp.length > 0
v.p = 0
v.dir = 1
end
end
local function retrigVolume(c: any, x: number)
if x == 0 or x == 8 then
return
end
local mul = Tables.RETRIG_MUL[x + 1]
if mul ~= 1 then
c.vol = floor(c.vol * mul)
else
c.vol += Tables.RETRIG_ADD[x + 1]
end
c.vol = clamp(c.vol, 0, 64)
end
-- S3M / IT D-style slide of a value: returns the new value (fine parts only on tick 0, normal parts on other ticks;
-- fast: normal parts on tick 0 too)
local function dSlide(value: number, param: number, tick: number, fast: boolean, hi: number): number
local x, y = param // 16, param % 16
if y == 0xF and x > 0 then
if tick == 0 then
return min(hi, value + x)
end
return value
elseif x == 0xF and y > 0 then
if tick == 0 then
return max(0, value - y)
end
return value
end
if tick == 0 and not fast then
return value
end
if y == 0 then
return min(hi, value + x)
elseif x == 0 then
return max(0, value - y)
end
return value
end
-- ------------------------------------------------------------------------------------------------ MOD / XM effects
local function fxModXm(st: any, c: any, tick: number)
local fx, param = c.fx, c.param
local xm = st.fmt == XM
local x, y = param // 16, param % 16
if fx == 0 then
if param ~= 0 then
local k = tick % 3
c.arp = if k == 1 then x elseif k == 2 then y else 0
end
elseif fx == 1 then
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)
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)
end
elseif fx == 3 or fx == 5 then
if fx == 3 and param > 0 then
c.portaSpeed = param
end
if tick > 0 then
tonePorta(st, c, c.portaSpeed * 4)
end
elseif fx == 4 or fx == 6 then
if fx == 4 then
if x > 0 then
c.vibSpeed = x
end
if y > 0 then
c.vibDepth = y
end
end
if tick > 0 then
vibrato(st, c, 8 / 255)
end
elseif fx == 7 then
if x > 0 then
c.tremSpeed = x
end
if y > 0 then
c.tremDepth = y
end
if tick > 0 then
tremolo(c, 1 / 64)
end
elseif fx == 8 then
if tick == 0 then
c.pan = param
c.basePan = c.pan
c.surround = false
end
elseif fx == 0xB then
if tick == 0 then
st.jumpOrd = param
if st.breakRow < 0 then
st.breakRow = 0
end
end
elseif fx == 0xC then
if tick == 0 then
c.vol = min(param, 64)
end
elseif fx == 0xD then
if tick == 0 then
local r = x * 10 + y
if r > 63 then
r = 0
end
st.breakRow = r
end
elseif fx == 0xE then
if x == 1 then
if xm and y > 0 then
c.mFineUp = y
end
if tick == 0 then
c.period -= (if xm then c.mFineUp else y) * 4
clampPeriod(st, c)
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)
end
elseif x == 4 then
c.vibWave = y % 8
elseif x == 6 then
if tick == 0 then
if y == 0 then
c.loopRow = st.row
elseif c.loopCount == 0 then
c.loopCount = y
st.loopJump = c.loopRow
st.ft2BreakPos = c.loopRow
else
c.loopCount -= 1
if c.loopCount > 0 then
st.loopJump = c.loopRow
end
st.ft2BreakPos = c.loopRow
end
end
elseif x == 7 then
c.tremWave = y % 8
elseif x == 8 then
if tick == 0 then -- FT2 ignores E8x in XM; OpenMPT (the reference) applies it
c.pan = y * 17
c.basePan = c.pan
c.surround = false
end
elseif x == 9 then
if y > 0 and tick > 0 and tick % y == 0 then
retrigVoice(st, c)
end
elseif x == 0xA then
if xm and y > 0 then
c.mFineVolUp = y
end
if tick == 0 then
c.vol = min(64, c.vol + (if xm then c.mFineVolUp else y))
end
elseif x == 0xB then
if xm and y > 0 then
c.mFineVolDn = y
end
if tick == 0 then
c.vol = max(0, c.vol - (if xm then c.mFineVolDn else y))
end
elseif x == 0xC then
if tick == y then
c.vol = 0
end
elseif x == 0xE then
if tick == 0 and st.patDelayRep == 0 and st.patDelay == 0 then
st.patDelay = y
end
end
elseif fx == 0xF then
if tick == 0 then
if param == 0 then
if not xm then
st.stopAfterRow = true -- ProTracker F00: the song stops (after this row, as OpenMPT plays it)
end
elseif param < 32 then
st.speed = param
else
st.tempo = param
end
end
elseif fx == 16 then -- G
if tick == 0 then
st.gvol = min(param, 64) * 2
end
elseif fx == 17 then -- H
if param > 0 then
c.mGVolSlide = param
end
if tick > 0 then
local p = c.mGVolSlide
local px, py = p // 16, p % 16
if px > 0 then
st.gvol = min(128, st.gvol + px * 2)
else
st.gvol = max(0, st.gvol - py * 2)
end
end
elseif fx == 20 then -- K
if tick == param then
keyOff(st, c.voice)
end
elseif fx == 21 then -- L
if tick == 0 and c.voice then
c.voice.volEnvPos = param
c.voice.panEnvPos = param
end
elseif fx == 25 then -- P
if param > 0 then
c.mPanSlide = param
end
if tick > 0 then
local p = c.mPanSlide
local px, py = p // 16, p % 16
if px > 0 then
c.pan = min(255, c.pan + px)
c.basePan = c.pan
else
c.pan = max(0, c.pan - py)
c.basePan = c.pan
end
end
elseif fx == 27 then -- R
if x > 0 then
c.mRetrig = c.mRetrig % 16 + x * 16
end
if y > 0 then
c.mRetrig = (c.mRetrig // 16) * 16 + y
end
local interval = c.mRetrig % 16
if tick == 0 and c.triggered then
c.retrigCount = 0
else
c.retrigCount += 1
end
if interval > 0 and c.retrigCount >= interval then
c.retrigCount = 0
retrigVolume(c, c.mRetrig // 16)
retrigVoice(st, c)
end
elseif fx == 29 then -- T
if param > 0 then
c.mTremor = param
end
elseif fx == 33 then -- X
if x == 1 then
if y > 0 then
c.mXFineUp = y
end
if tick == 0 then
c.period -= c.mXFineUp
clampPeriod(st, c)
end
elseif x == 2 then
if y > 0 then
c.mXFineDn = y
end
if tick == 0 then
c.period += c.mXFineDn
clampPeriod(st, c)
end
end
end
-- the volume slide half of 5xy / 6xy and Axy
if fx == 0xA or fx == 5 or fx == 6 then
if xm then
if param > 0 then
c.mVolSlide = param
end
if tick > 0 then
volSlideMod(c, c.mVolSlide)
end
elseif tick > 0 then
volSlideMod(c, param)
end
end
-- tremor (XM Txy): on x + 1 ticks, off y + 1 ticks
if fx == 29 and tick > 0 then
local on, off = c.mTremor // 16 + 1, c.mTremor % 16 + 1
c.tremorCount %= on + off
c.tremorOn = c.tremorCount < on
c.tremorCount += 1
end
end
-- the XM volume column (everything but set volume / set panning, done by trigger)
local function volColXm(st: any, c: any, tick: number)
local v = c.vcmd
if v < 0x60 then
return
end
local hi, lo = v // 16, v % 16
if hi == 6 then
if tick > 0 then
c.vol = max(0, c.vol - lo)
end
elseif hi == 7 then
if tick > 0 then
c.vol = min(64, c.vol + lo)
end
elseif hi == 8 then
if tick == 0 then
c.vol = max(0, c.vol - lo)
end
elseif hi == 9 then
if tick == 0 then
c.vol = min(64, c.vol + lo)
end
elseif hi == 0xA then
if lo > 0 then
c.vibSpeed = lo
end
elseif hi == 0xB then
if lo > 0 then
c.vibDepth = lo
end
if tick > 0 then
vibrato(st, c, 8 / 255)
end
elseif hi == 0xD then
if tick > 0 then
c.pan = max(0, c.pan - lo)
c.basePan = c.pan
end
elseif hi == 0xE then
if tick > 0 then
c.pan = min(255, c.pan + lo)
c.basePan = c.pan
end
elseif hi == 0xF then
if lo > 0 then
c.portaSpeed = lo * 16
end
if tick > 0 then
tonePorta(st, c, c.portaSpeed * 4)
end
end
end
-- ------------------------------------------------------------------------------------------------ S3M / IT effects
-- the parameter after memory: IT keeps one memory per effect group, ST3 one memory for most effects
local function mem(st: any, c: any, field: string, param: number): number
if st.fmt == S3M then
if param > 0 then
c.mS3M = param
end
return c.mS3M
end
if param > 0 then
c[field] = param
end
return c[field]
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
end
elseif p >= 0xE0 then
if tick == 0 then
c.period += sign * (p % 16)
end
elseif tick > 0 then
c.period += sign * p * 4
end
clampPeriod(st, c)
end
local function fxS3mIt(st: any, c: any, tick: number)
local fx, param = c.fx, c.param
local it = st.fmt == IT
if fx == 1 then -- A speed
if tick == 0 and param > 0 then
st.speed = param
end
elseif fx == 2 then -- B jump
if tick == 0 then
st.jumpOrd = param
if st.breakRow < 0 then
st.breakRow = 0
end
end
elseif fx == 3 then -- C break
if tick == 0 then
st.breakRow = if it then param else (param // 16) * 10 + param % 16
end
elseif fx == 4 or fx == 11 or fx == 12 then -- D volume slide (K, L: + vibrato / tone portamento)
local p = mem(st, c, "mVolSlide", param)
c.vol = dSlide(c.vol, p, tick, st.fastSlides, 64)
elseif fx == 5 then -- E porta down
local p = mem(st, c, "mEF", param)
portaS3m(st, c, p, tick, 1)
elseif fx == 6 then -- F porta up
local p = mem(st, c, "mEF", param)
portaS3m(st, c, p, tick, -1)
elseif fx == 9 then -- I tremor
local p = mem(st, c, "mTremor", param)
if tick > 0 or not it then
local on, off = p // 16, p % 16
if it and st.oldFx or not it then
on, off = on + 1, off + 1
end
on, off = max(on, 1), max(off, 1)
c.tremorCount %= on + off
c.tremorOn = c.tremorCount < on
c.tremorCount += 1
end
elseif fx == 10 then -- J arpeggio
local p = mem(st, c, "mArp", param)
local k = tick % 3
c.arp = if k == 1 then p // 16 elseif k == 2 then p % 16 else 0
elseif fx == 13 then -- M channel volume
if tick == 0 and param <= 64 then
c.chanVol = param
end
elseif fx == 14 then -- N channel volume slide
local p = mem(st, c, "mChanVolSlide", param)
c.chanVol = dSlide(c.chanVol, p, tick, false, 64)
elseif fx == 16 then -- P panning slide (0..64 scale x 4)
local p = mem(st, c, "mPanSlide", param)
local pan64 = c.pan / 4
local x, y = p // 16, p % 16
if y == 0xF and x > 0 then
if tick == 0 then
pan64 = max(0, pan64 - x)
end
elseif x == 0xF and y > 0 then
if tick == 0 then
pan64 = min(64, pan64 + y)
end
elseif tick > 0 then
if y == 0 then
pan64 = max(0, pan64 - x)
elseif x == 0 then
pan64 = min(64, pan64 + y)
end
end
c.pan = pan64 * 4
c.basePan = c.pan
c.surround = false
elseif fx == 17 then -- Q retrigger + volume change
local p = mem(st, c, "mRetrig", param)
local interval = p % 16
if interval > 0 then
if tick == 0 and c.retrigCount <= 0 then
c.retrigCount = interval
end
if tick > 0 then
c.retrigCount -= 1
if c.retrigCount <= 0 then
c.retrigCount = interval
retrigVolume(c, p // 16)
retrigVoice(st, c)
end
end
end
elseif fx == 18 then -- R tremolo
if param // 16 > 0 then
c.tremSpeed = param // 16
end
if param % 16 > 0 then
c.tremDepth = param % 16
end
if tick > 0 then
tremolo(c, 1 / 64)
end
elseif fx == 19 then -- S extended
local p = if it then mem(st, c, "mS", param) else mem(st, c, "mS", param)
local x, y = p // 16, p % 16
if x == 3 then
c.vibWave = y % 8
elseif x == 4 then
c.tremWave = y % 8
elseif x == 5 then
c.panbWave = y % 8
elseif x == 6 then
if tick == 0 then
st.fineDelay += y
end
elseif x == 7 then
if tick == 0 then
if y <= 2 then
for _, u in st.voices do
if u.active and u.bg and u.chan == c.idx then
if y == 0 then
cutVoice(u)
elseif y == 1 then
keyOff(st, u)
else
noteFade(u)
end
end
end
elseif y <= 6 then
c.nna = y - 3
else
local v = c.voice
if v then
if y == 7 then
v.volEnvOn = false
elseif y == 8 then
v.volEnvOn = true
elseif y == 9 then
v.panEnvOn = false
elseif y == 10 then
v.panEnvOn = true
elseif y == 11 then
v.pitchEnvOn = false
elseif y == 12 then
v.pitchEnvOn = true
end
end
end
end
elseif x == 8 then
if tick == 0 then
c.pan = y * 17
c.basePan = c.pan
c.surround = false
end
elseif x == 9 then
if tick == 0 and y == 1 then
c.surround = true
end
elseif x == 0xA then
if tick == 0 then
c.mHighOffset = y
end
elseif x == 0xB then
if tick == 0 then
if y == 0 then
c.loopRow = st.row
elseif c.loopCount == 0 then
c.loopCount = y
st.loopJump = c.loopRow
else
c.loopCount -= 1
if c.loopCount > 0 then
st.loopJump = c.loopRow
elseif it then
c.loopRow = st.row + 1
end
end
end
elseif x == 0xC then
local at = if y == 0 and it then 1 else y
if tick == at and (y > 0 or it) then
if it then
cutVoice(c.voice)
end
c.vol = 0
end
elseif x == 0xE then
if tick == 0 and st.patDelayRep == 0 and st.patDelay == 0 then
st.patDelay = y
end
end
elseif fx == 20 then -- T tempo
if param >= 0x20 then
if tick == 0 then
st.tempo = param
end
elseif it then
if param > 0 then
c.mTempo = param
end
local p = c.mTempo
if tick > 0 then
if p >= 0x10 then
st.tempo = min(255, st.tempo + (p - 0x10))
else
st.tempo = max(32, st.tempo - p)
end
end
end
elseif fx == 22 then -- V global volume
if tick == 0 then
if it then
if param <= 128 then
st.gvol = param
end
else
st.gvol = min(param, 64) * 2
end
end
elseif fx == 23 then -- W global volume slide
local p = mem(st, c, "mGVolSlide", param)
st.gvol = dSlide(st.gvol, p, tick, false, 128)
elseif fx == 24 then -- X panning
if tick == 0 then
if it then
c.pan = param
c.basePan = c.pan
c.surround = false
elseif param <= 0x80 then
c.pan = param * 2
c.basePan = c.pan
c.surround = false
elseif param == 0xA4 then
c.surround = true
end
end
elseif fx == 25 then -- Y panbrello
if param // 16 > 0 then
c.panbSpeed = param // 16
end
if param % 16 > 0 then
c.panbDepth = param % 16
end
c.panbDelta = wave64(c.panbWave % 4, c.panbPos // 4) * c.panbDepth / 16
c.panbPos = (c.panbPos + c.panbSpeed) % 256
elseif fx == 26 then -- Z MIDI macro: the default IT macros (Z00..Z7F cutoff, Z80..Z8F resonance)
if tick == 0 then
if param < 0x80 then
c.cutoff = param
elseif param < 0x90 then
c.reso = (param - 0x80) * 8
end
if c.voice then
c.voice.cutoff, c.voice.reso = c.cutoff, c.reso
end
end
end
-- tone portamento (G, L)
if fx == 7 or fx == 12 then
if fx == 7 then
if st.fmt == S3M then
if param > 0 then
c.portaSpeed = param
end
elseif st.compatG then
if param > 0 then
c.portaSpeed = param
end
else
if param > 0 then
c.mEF = param
end
c.portaSpeed = c.mEF
end
end
if tick > 0 then
tonePorta(st, c, c.portaSpeed * 4)
end
end
-- vibrato (H, U, K)
if fx == 8 or fx == 21 or fx == 11 then
if fx ~= 11 then
if param // 16 > 0 then
c.vibSpeed = param // 16
end
if param % 16 > 0 then
c.vibDepth = param % 16
end
end
local scale = if fx == 21 then 2 / 255 else 8 / 255
if it and not st.oldFx then
scale /= 2
end
if tick > 0 or (it and not st.oldFx) then
vibrato(st, c, scale)
end
end
end
-- the IT volume column (value stored + 1): slides, portamento, vibrato
local function volColIt(st: any, c: any, tick: number)
local v = c.vcmd - 1
if v < 65 then
return
end
if v <= 74 then
local p = v - 65
if p > 0 then
c.mVolCol = p
end
if tick == 0 then
c.vol = min(64, c.vol + c.mVolCol)
end
elseif v <= 84 then
local p = v - 75
if p > 0 then
c.mVolCol = p
end
if tick == 0 then
c.vol = max(0, c.vol - c.mVolCol)
end
elseif v <= 94 then
local p = v - 85
if p > 0 then
c.mVolCol = p
end
if tick > 0 then
c.vol = min(64, c.vol + c.mVolCol)
end
elseif v <= 104 then
local p = v - 95
if p > 0 then
c.mVolCol = p
end
if tick > 0 then
c.vol = max(0, c.vol - c.mVolCol)
end
elseif v <= 114 then
local p = (v - 105) * 4
if p > 0 then
c.mEF = p
end
if tick > 0 then
c.period += c.mEF * 4
clampPeriod(st, c)
end
elseif v <= 124 then
local p = (v - 115) * 4
if p > 0 then
c.mEF = p
end
if tick > 0 then
c.period -= c.mEF * 4
clampPeriod(st, c)
end
elseif v >= 193 and v <= 202 then
local p = Tables.IT_VOLPORTA[v - 192] or 0
if p > 0 then
if st.compatG then
c.portaSpeed = p
else
c.mEF = p
c.portaSpeed = p
end
end
if tick > 0 then
tonePorta(st, c, c.portaSpeed * 4)
end
elseif v >= 203 and v <= 212 then
local d = v - 203
if d > 0 then
c.vibDepth = d
end
if tick > 0 or not st.oldFx then
vibrato(st, c, if st.oldFx then 8 / 255 else 4 / 255)
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
return param % 16
elseif (st.fmt == S3M or st.fmt == IT) and fx == 19 and param // 16 == 0xD then
return param % 16
end
return 0
end
local function finetuneOf(st: any, fx: number, param: number): number?
if (st.fmt == MOD or st.fmt == XM) and fx == 0xE and param // 16 == 5 then
local y = param % 16
if st.fmt == MOD then
return (if y >= 8 then y - 16 else y) * 16
end
return (y - 8) * 16
end
return nil
end
local function runEffects(st: any, c: any, tick: number)
c.vibDelta, c.arp = 0, 0
if st.fmt == MOD or st.fmt == XM then
fxModXm(st, c, tick)
if st.fmt == XM then
volColXm(st, c, tick)
end
else
if st.fmt == IT then
volColIt(st, c, tick)
end
fxS3mIt(st, c, tick)
end
-- keep the tremolo / panbrello offsets only while their effect runs
local fx = c.fx
if st.fmt == MOD or st.fmt == XM then
if fx ~= 7 then
c.tremDelta = 0
end
-- XM: a tremor that ends in its off phase keeps the channel silent until the next note (OpenMPT / FT2)
else
if fx ~= 18 then
c.tremDelta = 0
end
if fx ~= 9 then
c.tremorOn = true
end
if fx ~= 25 then
c.panbDelta = 0
end
end
end
local function processRow(st: any)
local m = st.m
local pat = st.pat
local nch = st.nch
local data = pat.data
local base = st.row * nch * 5
st.visited[st.ord * 1024 + st.row] = true
st.rows += 1
for i = 1, nch do
local c = st.chans[i]
local o = base + (i - 1) * 5
local note, ins, vcmd, fx, param = readu8(data, o), readu8(data, o + 1), readu8(data, o + 2), readu8(data, o + 3), readu8(data, o + 4)
if (st.fmt == MOD or st.fmt == XM) and fx == 0 and param == 0 then
fx = 255
end
c.fx, c.param, c.vcmd = fx, param, vcmd
c.cutTick, c.keyOffTick = -1, -1
c.triggered = false
local delay = noteDelayOf(st, fx, param)
c.finetune = finetuneOf(st, fx, param)
if delay > 0 then
c.delayTick, c.dNote, c.dIns, c.dVol = delay, note, ins, vcmd
else
c.delayTick = -1
trigger(st, c, note, ins, vcmd)
end
runEffects(st, c, 0)
end
local _ = m
end
local function updateEnvelope(env: any, pos: number, keyOn: boolean, it: boolean): (number, number, boolean)
-- returns value, next position, ended
local ticks, values = env.ticks, env.values
local n = #ticks
if n == 0 then
return 0, pos, true
end
local value
if pos <= ticks[1] then
value = values[1]
elseif pos >= ticks[n] then
value = values[n]
else
local i = 1
while i < n and ticks[i + 1] <= pos do
i += 1
end
local t0, t1 = ticks[i], ticks[i + 1]
if t1 > t0 then
value = values[i] + (values[i + 1] - values[i]) * (pos - t0) / (t1 - t0)
else
value = values[i + 1]
end
end
local nextPos = pos + 1
local ended = false
-- sustain / loop: only when the position runs over the end point (a position set past it, by Lxx, runs on)
-- FT2 jumps when the position reaches the end tick (its value is not played: a loop lasts end - start ticks),
-- IT after playing it (end - start + 1 ticks)
local se, le = ticks[env.susEnd], ticks[env.loopEnd]
local over = if it then 1 else 0
if keyOn and env.sustain and se and pos <= se and nextPos >= se + over then
nextPos = ticks[env.susStart] or nextPos
elseif env.loop and le and pos <= le and nextPos >= le + over then
nextPos = ticks[env.loopStart] or nextPos
elseif nextPos > ticks[n] then
nextPos = ticks[n]
ended = true
end
return value, nextPos, ended
end
-- per-tick voice update: envelopes, fadeout, auto-vibrato, final frequency, gains and filter
local function updateVoice(st: any, v: any)
local ins = v.ins
local it = st.fmt == IT
local envVol, envPan, envPitch = 64, 0, 0
local filterEnv = nil
if ins then
local e = ins.volEnv
if e and e.enabled and v.volEnvOn then
local val, np, ended = updateEnvelope(e, v.volEnvPos, v.keyOn, it)
envVol, v.volEnvPos = val, np
if ended and it then
if val <= 0 then
v.active = false
return
end
v.fading = true
end
end
e = ins.panEnv
if e and e.enabled and v.panEnvOn then
local val, np = updateEnvelope(e, v.panEnvPos, v.keyOn, it)
envPan, v.panEnvPos = val, np
end
e = ins.pitchEnv
if e and e.enabled and v.pitchEnvOn then
local val, np = updateEnvelope(e, v.pitchEnvPos, v.keyOn, it)
v.pitchEnvPos = np
if e.filter then
filterEnv = val
else
envPitch = val
end
end
if v.fading then
local fo = ins.fadeout or 0
v.fade -= if it then fo * 64 else fo * 2
if v.fade <= 0 then
v.fade = 0
v.active = false
return
end
end
end
v.envVol = envVol
-- auto-vibrato: XM per instrument, IT per sample
local avSrc = if it then v.smp else ins
local avDelta = 0
if avSrc and (avSrc.vibDepth or 0) > 0 then
local depth = avSrc.vibDepth
local sweep = avSrc.vibSweep or 0
if it then
-- IT: the depth ramps up by the rate (vibSweep here) every tick, in 1/256 steps
local target = depth * 256
if sweep == 0 then
v.avAmp = target
elseif v.avAmp < target then
v.avAmp = min(target, v.avAmp + sweep)
end
avDelta = autoWave(avSrc.vibType or 0, v.avPos, true) * v.avAmp / (256 * 64) * 4
else
if sweep == 0 then
v.avAmp = depth * 256
elseif v.keyOn then
v.avAmp = min(depth * 256, v.avAmp + depth * 256 / sweep)
end
avDelta = autoWave(avSrc.vibType or 0, v.avPos, false) * v.avAmp / (256 * 64)
end
v.avPos = (v.avPos + (avSrc.vibRate or 0)) % 256
end
-- frequency
local P = v.periodOut + avDelta
local freq
if st.linear then
freq = v.base * 2 ^ (-P / 768)
else
freq = if P > 0 then st.amiga / P else 0
end
if envPitch ~= 0 then
freq *= 2 ^ (envPitch / 24)
end
v.inc = freq / st.sr
-- 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
if v.tremorMute then
vol = 0
end
-- panning
local pan = v.pan
if it and ins and (ins.pps or 0) ~= 0 then
pan = clamp(pan + ((v.patNote or v.note) - (ins.ppc or 60) - 1) * ins.pps / 2, 0, 256)
end
if envPan ~= 0 then
local range = 128 - abs(pan - 128)
pan += envPan * range / 32
end
pan = clamp(pan, 0, 256)
pan = clamp(128 + (pan - 128) * st.separation, 0, 256)
local g = vol * st.master
if v.surround then
v.tl, v.tr = g * 0.5, -g * 0.5
else
v.tl, v.tr = g * (256 - pan) / 256, g * pan / 256
end
v.rampN = st.ramp
-- IT resonant filter
local cutoff = v.cutoff
if filterEnv ~= nil or cutoff < 127 or v.reso > 0 then
local cc = cutoff
if filterEnv ~= nil then
cc = cutoff * (filterEnv + 32) / 64
end
local fhz = 110 * 2 ^ (0.25 + cc / 24)
fhz = clamp(fhz, 120, min(20000, st.sr / 2))
local dmp = 10 ^ (-v.reso * (24 / 128) / 20)
local fc = fhz * 2 * math.pi / st.sr
local d = (1 - 2 * dmp) * fc
if d > 2 then
d = 2
end
d = (2 * dmp - d) / fc
local e = 1 / (fc * fc)
local den = 1 + d + e
v.fa, v.fb0, v.fb1 = 1 / den, (d + e + e) / den, -e / den
v.fOn = not (cc >= 127 and v.reso == 0)
else
v.fOn = false
end
end
-- tick(st) -> frames to mix for this tick (0 when the song ended)
function Player.tick(st: any): number
if st.ended then
return 0
end
local tick = st.tick
if tick == 0 and st.patDelayRep == 0 then
local key = st.ord * 1024 + st.row
if st.visited[key] and not st.loopAllowed then
st.ended = true
return 0
end
if st.visited[key] then
-- looping on: forget the song so far, keep playing
table.clear(st.visited)
end
st.fineDelay = 0
processRow(st)
else
local t = tick % st.speed
local rep = tick >= st.speed or st.patDelayRep > 0
for i = 1, st.nch do
local c = st.chans[i]
if c.delayTick == tick and not rep then
c.delayTick = -1
trigger(st, c, c.dNote, c.dIns, c.dVol)
end
if rep and t == 0 then
c.vibDelta, c.arp = 0, 0
else
runEffects(st, c, if t == 0 then 1 else t)
end
end
end
if st.ended then
return 0
end
-- channel -> foreground voice
for i = 1, st.nch do
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
v.period, v.periodOut, v.base = c.period, P, c.base
v.vol = clamp(c.vol + c.tremDelta, 0, 64)
if c.muted then
v.vol = 0
end
v.pan = clamp(c.pan + c.panbDelta, 0, 256)
v.surround = c.surround
v.chanVol = c.chanVol
v.tremorMute = not c.tremorOn
v.cutoff, v.reso = c.cutoff, c.reso
end
end
for _, v in st.voices do
if v.active then
updateVoice(st, v)
end
end
-- frames for this tick: 2.5 / tempo seconds, truncated per tick (as OpenMPT does)
local frames = floor(st.sr * 2.5 / max(1, st.tempo))
-- advance
st.tick += 1
if st.tick >= st.speed + st.fineDelay then
st.tick = 0
if st.patDelayRep < st.patDelay then
st.patDelayRep += 1
else
st.patDelay, st.patDelayRep = 0, 0
advanceRow(st)
end
end
return frames
end
return Player