ws-music: music/xm read/write/render (FT2 / OpenMPT instrument, tremor, envelope and E6x quirks), tone portamento never starts MOD/XM notes
This commit is contained in:
1 parent
6c49ab38dd
commit
8b8b64885f
6 files changed
+858
-24
No files matched your search
@@ -199,6 +199,7 @@ function Player.new(m: any, sampleRate: number, opts: any): any
|
||||
patDelayRep = 0,
|
||||
fineDelay = 0,
|
||||
jumpOrd = -1,
|
||||
ft2BreakPos = 0,
|
||||
breakRow = -1,
|
||||
loopJump = -1,
|
||||
ended = false,
|
||||
@@ -242,6 +243,9 @@ function Player.masterGain(m: any, fmt: number): number
|
||||
elseif fmt == S3M then
|
||||
return (m.mixVolume or 48) / 128 -- OpenMPT: linear in the 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
|
||||
@@ -295,6 +299,7 @@ local function advanceRow(st: any)
|
||||
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
|
||||
@@ -309,7 +314,10 @@ local function advanceRow(st: any)
|
||||
end
|
||||
st.row += 1
|
||||
if st.row >= st.pat.rows then
|
||||
Player.startOrder(st, st.ord + 1, 0)
|
||||
-- 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
|
||||
|
||||
@@ -428,7 +436,7 @@ local function cutVoice(v: any)
|
||||
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: boolean)
|
||||
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)
|
||||
@@ -456,9 +464,11 @@ local function startVoice(st: any, c: any, smp: any, smpIdx: number, ins: any, i
|
||||
v.base = base
|
||||
st.age += 1
|
||||
v.order = st.age
|
||||
if not keepEnv then
|
||||
v.keyOn = true
|
||||
v.fading = false
|
||||
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
|
||||
@@ -612,8 +622,9 @@ local function trigger(st: any, c: any, note: number, insIdx: number, vcmd: numb
|
||||
setInstrumentDefaults(st, c, smp, ins)
|
||||
if not hasNote then
|
||||
local v = c.voice
|
||||
if f == XM and v and v.active and (prev == insIdx) then
|
||||
-- an instrument number alone restarts the envelopes (FT2)
|
||||
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
|
||||
@@ -641,7 +652,8 @@ local function trigger(st: any, c: any, note: number, insIdx: number, vcmd: numb
|
||||
local ft = c.finetune
|
||||
local period, base = notePeriod(st, realNote, smp, ft)
|
||||
local v = c.voice
|
||||
if porta and v and v.active then
|
||||
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
|
||||
@@ -650,8 +662,15 @@ local function trigger(st: any, c: any, note: number, insIdx: number, vcmd: numb
|
||||
if f == IT and st.insMode then
|
||||
newNoteAction(st, c, ins, useIns, realNote, smpIdx)
|
||||
end
|
||||
local keep = f == XM and insIdx == 0 and c.voice ~= nil
|
||||
-- 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)
|
||||
c.period, c.target, c.base = period, period, base
|
||||
@@ -902,17 +921,19 @@ local function fxModXm(st: any, c: any, tick: number)
|
||||
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 and not xm then
|
||||
if tick == 0 then -- FT2 ignores E8x in XM; OpenMPT (the reference) applies it
|
||||
c.pan = y * 17
|
||||
c.surround = false
|
||||
end
|
||||
@@ -1051,8 +1072,9 @@ local function fxModXm(st: any, c: any, tick: number)
|
||||
-- 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 = (c.tremorCount + 1) % (on + off)
|
||||
c.tremorCount %= on + off
|
||||
c.tremorOn = c.tremorCount < on
|
||||
c.tremorCount += 1
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1173,8 +1195,9 @@ local function fxS3mIt(st: any, c: any, tick: number)
|
||||
on, off = on + 1, off + 1
|
||||
end
|
||||
on, off = max(on, 1), max(off, 1)
|
||||
c.tremorCount = (c.tremorCount + 1) % (on + off)
|
||||
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)
|
||||
@@ -1550,9 +1573,7 @@ local function runEffects(st: any, c: any, tick: number)
|
||||
if fx ~= 7 then
|
||||
c.tremDelta = 0
|
||||
end
|
||||
if fx ~= 29 then
|
||||
c.tremorOn = true
|
||||
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
|
||||
@@ -1584,9 +1605,6 @@ local function processRow(st: any)
|
||||
c.fx, c.param, c.vcmd = fx, param, vcmd
|
||||
c.cutTick, c.keyOffTick = -1, -1
|
||||
c.triggered = false
|
||||
if fx ~= 29 and fx ~= 9 then
|
||||
c.tremorCount = 0
|
||||
end
|
||||
local delay = noteDelayOf(st, fx, param)
|
||||
c.finetune = finetuneOf(st, fx, param)
|
||||
if delay > 0 then
|
||||
@@ -1600,7 +1618,7 @@ local function processRow(st: any)
|
||||
local _ = m
|
||||
end
|
||||
|
||||
local function updateEnvelope(env: any, pos: number, keyOn: boolean): (number, number, boolean)
|
||||
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
|
||||
@@ -1626,9 +1644,14 @@ local function updateEnvelope(env: any, pos: number, keyOn: boolean): (number, n
|
||||
end
|
||||
local nextPos = pos + 1
|
||||
local ended = false
|
||||
if keyOn and env.sustain and ticks[env.susEnd] and nextPos > ticks[env.susEnd] then
|
||||
-- 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 ticks[env.loopEnd] and nextPos > ticks[env.loopEnd] then
|
||||
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]
|
||||
@@ -1646,7 +1669,7 @@ local function updateVoice(st: any, v: any)
|
||||
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)
|
||||
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
|
||||
@@ -1658,12 +1681,12 @@ local function updateVoice(st: any, v: any)
|
||||
end
|
||||
e = ins.panEnv
|
||||
if e and e.enabled and v.panEnvOn then
|
||||
local val, np = updateEnvelope(e, v.panEnvPos, v.keyOn)
|
||||
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)
|
||||
local val, np = updateEnvelope(e, v.pitchEnvPos, v.keyOn, it)
|
||||
v.pitchEnvPos = np
|
||||
if e.filter then
|
||||
filterEnv = val
|
||||
|
||||
Reference in new issue
Block a user