292 lines
9.8 KiB
Luau
292 lines
9.8 KiB
Luau
--!strict
|
|
-- codecs/audio/Tracker/Load: helpers shared by the tracker loaders that map another format's effects onto one of the
|
|
-- engine's families (music/mtm, 669, stm, ult, okt, med, ptm, far). Effects are first named the way OpenMPT's loaders
|
|
-- name them ("portaup", "volslide", "modex", ...), then written in the family's own cell coding: S3M / IT letter
|
|
-- codes (A = 1) with the IT volume column, or MOD / XM numbers. Ported from OpenMPT's ModCommand helpers
|
|
-- (ConvertModCommand, ConvertToVolCommand, FillInTwoCommands, GetEffectWeight, CPattern::WriteEffect; BSD licence,
|
|
-- licenses/openmpt-LICENSE.txt), reduced to what the engine plays.
|
|
local Err = require("@formats/core/Err")
|
|
local Player = require("@formats/codecs/audio/Tracker/Player")
|
|
local Pcm = require("@formats/codecs/audio/Tracker/Pcm")
|
|
local Tables = require("@formats/codecs/audio/Tracker/Tables")
|
|
|
|
local Load = {}
|
|
|
|
local EXT = Player.EXT
|
|
|
|
-- the S3M / IT letter code of each effect name (engine extension effects included)
|
|
local IT: { [string]: number } = {
|
|
speed = 1,
|
|
jump = 2,
|
|
["break"] = 3,
|
|
volslide = 4,
|
|
portadown = 5,
|
|
portaup = 6,
|
|
toneporta = 7,
|
|
vibrato = 8,
|
|
tremor = 9,
|
|
arpeggio = 10,
|
|
vibratovol = 11,
|
|
toneportavol = 12,
|
|
chanvol = 13,
|
|
chanvolslide = 14,
|
|
offset = 15,
|
|
panslide = 16,
|
|
retrig = 17,
|
|
tremolo = 18,
|
|
s3mex = 19,
|
|
tempo = 20,
|
|
finevibrato = 21,
|
|
globalvol = 22,
|
|
globalvolslide = 23,
|
|
panning8 = 24,
|
|
panbrello = 25,
|
|
noteslideup = EXT.NOTE_SLIDE_UP,
|
|
noteslidedown = EXT.NOTE_SLIDE_DOWN,
|
|
noteslideupretrig = EXT.NOTE_SLIDE_UP_RETRIG,
|
|
noteslidedownretrig = EXT.NOTE_SLIDE_DOWN_RETRIG,
|
|
autoportaup = EXT.AUTO_PORTA_UP,
|
|
autoportadown = EXT.AUTO_PORTA_DOWN,
|
|
freqadjust = EXT.FREQ_ADJUST,
|
|
}
|
|
Load.IT = IT
|
|
|
|
-- OpenMPT's effect weights (low to high): which of two effects a cell keeps when only one fits
|
|
local WEIGHT: { [string]: number } = {}
|
|
for i, n in
|
|
{
|
|
"none", "keyoff", "tremolo", "finevibrato", "vibrato", "panbrello", "s3mex", "modex", "panslide", "panning8",
|
|
"noteslideupretrig", "noteslideup", "noteslidedownretrig", "noteslidedown", "portaup", "autoportaup",
|
|
"portadown", "autoportadown", "volslide", "vibratovol", "volume", "volume8", "offset", "tremor", "retrig",
|
|
"arpeggio", "toneporta", "toneportavol", "chanvolslide", "chanvol", "globalvolslide", "globalvol", "tempo",
|
|
"speed", "jump", "break",
|
|
}
|
|
do
|
|
WEIGHT[n] = i
|
|
end
|
|
|
|
local GLOBAL = { jump = true, ["break"] = true, speed = true, tempo = true, globalvol = true, globalvolslide = true }
|
|
|
|
-- isGlobal(name, param) -> the effect acts on the whole song (OpenMPT's IsGlobalCommand)
|
|
function Load.isGlobal(name: string?, param: number): boolean
|
|
if name == nil then
|
|
return false
|
|
end
|
|
if GLOBAL[name] then
|
|
return true
|
|
end
|
|
local x = param // 16
|
|
if name == "modex" then
|
|
return x == 0 or x == 6 or x == 0xE
|
|
elseif name == "s3mex" then
|
|
return x == 6 or x == 0xB or x == 0xE
|
|
end
|
|
return false
|
|
end
|
|
|
|
-- modCommand(cmd, param) -> name?, param: a MOD / XM effect number (0..F, XM letters 16..) by name (ConvertModCommand)
|
|
local MODFX: { [number]: string } = {
|
|
[0] = "arpeggio", [1] = "portaup", [2] = "portadown", [3] = "toneporta", [4] = "vibrato", [5] = "toneportavol",
|
|
[6] = "vibratovol", [7] = "tremolo", [8] = "panning8", [9] = "offset", [10] = "volslide", [11] = "jump",
|
|
[12] = "volume", [13] = "break", [14] = "modex", [15] = "tempo", [16] = "globalvol", [17] = "globalvolslide",
|
|
[20] = "keyoff", [25] = "panslide", [27] = "retrig", [29] = "tremor",
|
|
}
|
|
function Load.modCommand(cmd: number, param: number): (string?, number)
|
|
if cmd == 0 and param == 0 then
|
|
return nil, 0
|
|
elseif cmd == 0xF and param < 0x20 then
|
|
return "speed", param
|
|
end
|
|
local n = MODFX[cmd]
|
|
if n == "break" then
|
|
param = (param // 16) * 10 + param % 16
|
|
end
|
|
return n, param
|
|
end
|
|
|
|
-- itVolume(name, param, force) -> the IT volume-column byte + 1 (the engine's coding) for an effect that fits there,
|
|
-- or nil (OpenMPT's ConvertToVolCommand; force: fit it with some loss of precision)
|
|
function Load.itVolume(name: string?, param: number, force: boolean): number?
|
|
if name == "volume" then
|
|
return math.min(param, 64) + 1
|
|
elseif name == "volume8" then
|
|
if not force and param % 4 ~= 0 then
|
|
return nil
|
|
end
|
|
return (param + 3) // 4 + 1
|
|
elseif name == "portaup" or name == "portadown" then
|
|
if not force and (param % 4 ~= 0 or param >= 0xE0) then
|
|
return nil
|
|
end
|
|
return (if name == "portaup" then 115 else 105) + math.min(param // 4, 9) + 1
|
|
elseif name == "toneporta" then
|
|
if param >= 0xF0 then
|
|
return 193 + 9 + 1
|
|
end
|
|
for n = 0, 9 do
|
|
local v = Tables.IT_VOLPORTA[n + 1]
|
|
if (force and param <= v) or param == v then
|
|
return 193 + n + 1
|
|
end
|
|
end
|
|
return nil
|
|
elseif name == "vibrato" then
|
|
if force then
|
|
param = math.min(param % 16, 9)
|
|
elseif param % 16 > 9 or param >= 16 then
|
|
return nil
|
|
end
|
|
return 203 + param % 16 + 1
|
|
elseif name == "panning8" then
|
|
return 128 + (if param == 255 then 64 else param // 4) + 1
|
|
elseif name == "volslide" then
|
|
local x, y = param // 16, param % 16
|
|
if param == 0 then
|
|
return nil
|
|
elseif y == 0 then
|
|
return 85 + math.min(x, 9) + 1
|
|
elseif x == 0 then
|
|
return 95 + math.min(y, 9) + 1
|
|
elseif y == 0xF then
|
|
return 65 + math.min(x, 9) + 1
|
|
elseif x == 0xF then
|
|
return 75 + math.min(y, 9) + 1
|
|
end
|
|
return nil
|
|
elseif name == "s3mex" and param // 16 == 8 then
|
|
return 128 + (param % 16) * 4 + 2 + 1
|
|
end
|
|
return nil
|
|
end
|
|
|
|
-- itEffect(name, param) -> effect code, param in S3M / IT coding (0, 0 for none or an effect the family lacks; MOD's
|
|
-- extended E commands become their S3M equivalents)
|
|
function Load.itEffect(name: string?, param: number): (number, number)
|
|
if name == nil then
|
|
return 0, 0
|
|
end
|
|
if name == "modex" then
|
|
local x, y = param // 16, param % 16
|
|
if x == 1 then
|
|
return IT.portaup, 0xF0 + y
|
|
elseif x == 2 then
|
|
return IT.portadown, 0xF0 + y
|
|
elseif x == 9 then
|
|
return IT.retrig, y
|
|
elseif x == 0xA then
|
|
return IT.volslide, y * 16 + 0xF
|
|
elseif x == 0xB then
|
|
return IT.volslide, 0xF0 + y
|
|
end
|
|
local s = ({ [3] = 1, [4] = 3, [5] = 2, [6] = 0xB, [7] = 4, [8] = 8, [0xC] = 0xC, [0xD] = 0xD, [0xE] = 0xE })[x]
|
|
if s then
|
|
return IT.s3mex, s * 16 + y
|
|
end
|
|
return 0, 0
|
|
end
|
|
local code = IT[name]
|
|
if code == nil then
|
|
return 0, 0
|
|
end
|
|
return code, param
|
|
end
|
|
|
|
-- fillTwo(name1, param1, name2, param2, lowResOffset?) -> volume byte (engine IT coding, 0 none), effect name, param,
|
|
-- lost name?, lost param: two effects of one cell into the IT volume column and the effect column, keeping the
|
|
-- heavier one when both cannot fit (OpenMPT's FillInTwoCommands)
|
|
function Load.fillTwo(e1: string?, p1: number, e2: string?, p2: number): (number, string?, number, string?, number)
|
|
if e1 ~= nil and e1 == e2 then
|
|
e2 = nil -- the same effect twice: keep one (most are absolute; slides are kept once too, a simplification)
|
|
end
|
|
for n = 0, 3 do
|
|
local v = Load.itVolume(e1, p1, n > 1)
|
|
if e1 == nil or v then
|
|
if v == nil and (e2 == "volume" or e2 == "volume8") then
|
|
-- the engine's IT family sets volumes in the volume column only
|
|
return Load.itVolume(e2, p2, true) or 0, nil, 0, nil, 0
|
|
end
|
|
return v or 0, e2, p2, nil, 0
|
|
end
|
|
e1, p1, e2, p2 = e2, p2, e1, p1
|
|
end
|
|
if (WEIGHT[e1 :: string] or 0) > (WEIGHT[e2 :: string] or 0) then
|
|
e1, p1, e2, p2 = e2, p2, e1, p1
|
|
end
|
|
return 0, e2, p2, e1, p1
|
|
end
|
|
|
|
-- writeEffect(m, pattern (0-based), row, fx, param, retry?, channel?, multiple?) -> true when written: puts an effect
|
|
-- into a free effect slot of the row (any channel, or the given 1-based one); none free: the next row (retry "next") or
|
|
-- the previous one ("prev"). The same effect already on the row counts as written unless multiple (OpenMPT's
|
|
-- CPattern::WriteEffect).
|
|
function Load.writeEffect(m: any, pattern: number, row: number, fx: number, param: number, retry: string?, channel: number?, multiple: boolean?): boolean
|
|
local p = m.patterns[pattern + 1]
|
|
if not p then
|
|
return false
|
|
end
|
|
local fam = m.engine or m.format
|
|
local modLike = fam == "mod" or fam == "xm"
|
|
local nch = m.channels
|
|
local d = p.data
|
|
local guard = 0
|
|
while row >= 0 and row < p.rows and guard < 1024 do
|
|
guard += 1
|
|
local c0, c1 = 1, nch
|
|
if channel then
|
|
c0, c1 = channel, channel
|
|
end
|
|
for c = c0, c1 do
|
|
local o = (row * nch + c - 1) * 5
|
|
if not multiple and buffer.readu8(d, o + 3) == fx and (fx ~= 0 or not modLike) then
|
|
return true
|
|
end
|
|
end
|
|
for c = c0, c1 do
|
|
local o = (row * nch + c - 1) * 5
|
|
local f, q = buffer.readu8(d, o + 3), buffer.readu8(d, o + 4)
|
|
if f == 0 and (not modLike or q == 0) then
|
|
buffer.writeu8(d, o + 3, fx)
|
|
buffer.writeu8(d, o + 4, param)
|
|
return true
|
|
end
|
|
end
|
|
if retry == "next" then
|
|
row += 1
|
|
elseif retry == "prev" then
|
|
row -= 1
|
|
else
|
|
return false
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
|
|
-- readSample(buf, offset, frames, spec, sample, budget) -> true | nil, Err: decodes raw PCM (codecs/audio/Tracker/Pcm
|
|
-- spec) at the absolute buffer offset into the sample (data, length, bits, channels). A short file keeps the frames
|
|
-- that are there (end = the buffer's length, or spec.limit). budget = { left = f32 bytes still allowed } (maxBytes).
|
|
function Load.readSample(buf: buffer, offset: number, frames: number, spec: any, s: any, budget: { left: number }): (boolean?, Err.Err?)
|
|
local nch = spec.channels or 1
|
|
local bps = spec.bits // 8
|
|
local limit = math.min(spec.limit or buffer.len(buf), buffer.len(buf))
|
|
local avail = math.max(0, limit - offset) // (nch * bps)
|
|
local n = math.max(0, math.min(math.floor(frames), avail))
|
|
budget.left -= n * nch * 4
|
|
if budget.left < 0 then
|
|
return nil, Err.new(Err.LIMIT, "sample data over the maxBytes limit")
|
|
end
|
|
local data = Pcm.decode(buf, offset, n, spec)
|
|
if not data then
|
|
return nil, Err.new(Err.TRUNCATED, "sample data", offset)
|
|
end
|
|
s.data, s.length, s.bits, s.channels = data, n, spec.bits, nch
|
|
return true, nil
|
|
end
|
|
|
|
-- bcd(n) -> n as the two decimal digits S3M / MOD pattern breaks store (0..99)
|
|
function Load.bcd(n: number): number
|
|
n = math.clamp(math.floor(n), 0, 99)
|
|
return (n // 10) * 16 + n % 10
|
|
end
|
|
|
|
return Load
|