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.
127 lines
4.8 KiB
Luau
127 lines
4.8 KiB
Luau
--!strict
|
|
--!native
|
|
-- codecs/audio/Tracker/Tables: lookup tables of the tracker engine. Waveforms for vibrato / tremolo / panbrello and the
|
|
-- auto-vibrato, the ProTracker period table (finetune 0, used to map MOD periods to notes and back) and the IT / S3M
|
|
-- volume-column tone-portamento and retrigger tables. All values are from the public format documents (ProTracker
|
|
-- 2.3 effect docs, FT2 / XM spec, ST3 TECH.DOC, ITTECH.TXT).
|
|
local Tables = {}
|
|
|
|
-- the ProTracker half-sine (32 steps, 0..255)
|
|
local SINE32 = { 0, 24, 49, 74, 97, 120, 141, 161, 180, 197, 212, 224, 235, 244, 250, 253, 255, 253, 250, 244, 235, 224, 212, 197, 180, 161, 141, 120, 97, 74, 49, 24 }
|
|
|
|
-- wave64(type, pos) -> -255..255 for pos 0..63: 0 sine, 1 ramp down, 2 square, 3 random (deterministic)
|
|
local RANDOM: { number } = {}
|
|
do
|
|
local s = 12345
|
|
for i = 1, 64 do
|
|
s = (s * 1103515245 + 12345) % 2147483648
|
|
RANDOM[i] = (s // 65536) % 511 - 255
|
|
end
|
|
end
|
|
function Tables.wave64(kind: number, pos: number): number
|
|
local p = pos % 64
|
|
if kind == 1 then
|
|
return 255 - p * 8
|
|
elseif kind == 2 then
|
|
return if p < 32 then 255 else -255
|
|
elseif kind == 3 then
|
|
return RANDOM[p + 1]
|
|
end
|
|
local v = SINE32[(p % 32) + 1]
|
|
return if p >= 32 then -v else v
|
|
end
|
|
|
|
-- autoWave(type, pos, it) -> -64..64 for pos 0..255. XM: 0 sine, 1 square, 2 ramp down, 3 ramp up.
|
|
-- IT: 0 sine, 1 ramp down, 2 square, 3 random.
|
|
local SINE256: { number } = {}
|
|
for i = 0, 255 do
|
|
SINE256[i + 1] = math.floor(math.sin(i * math.pi / 128) * 64 + 0.5)
|
|
end
|
|
function Tables.autoWave(kind: number, pos: number, it: boolean): number
|
|
local p = pos % 256
|
|
if it then
|
|
if kind == 1 then
|
|
return 64 - (p + 1) // 2
|
|
elseif kind == 2 then
|
|
return if p < 128 then 64 else -64
|
|
elseif kind == 3 then
|
|
return RANDOM[(p % 64) + 1] // 4
|
|
end
|
|
return SINE256[p + 1]
|
|
end
|
|
if kind == 1 then
|
|
return if p < 128 then 64 else -64
|
|
elseif kind == 2 then
|
|
return 64 - (p + 1) // 2
|
|
elseif kind == 3 then
|
|
return (p + 1) // 2 - 64
|
|
end
|
|
return SINE256[p + 1]
|
|
end
|
|
|
|
-- ProTracker periods, finetune 0, C-0 .. B-4 (60 notes; ProTracker itself uses C-1 .. B-3). Our note numbers:
|
|
-- 1 = C-0 (FT2 numbering), so ProTracker C-1 (856) = note 37 and C-2 (428) = note 49, the note that plays a sample at
|
|
-- its base rate (8363 Hz).
|
|
local PERIODS = {
|
|
3424, 3232, 3048, 2880, 2712, 2560, 2416, 2280, 2152, 2032, 1920, 1812,
|
|
1712, 1616, 1524, 1440, 1356, 1280, 1208, 1140, 1076, 1016, 960, 906,
|
|
856, 808, 762, 720, 678, 640, 604, 570, 538, 508, 480, 453,
|
|
428, 404, 381, 360, 339, 320, 302, 285, 269, 254, 240, 226,
|
|
214, 202, 190, 180, 170, 160, 151, 143, 135, 127, 120, 113,
|
|
107, 101, 95, 90, 85, 80, 75, 71, 67, 63, 60, 56,
|
|
}
|
|
Tables.PERIODS = PERIODS
|
|
Tables.PERIOD_FIRST_NOTE = 13 -- PERIODS[1] is note 13 (C-1 in FT2 numbering = ProTracker octave -1)
|
|
|
|
-- periodToNote(period) -> note (1..120), 0 for period 0
|
|
function Tables.periodToNote(period: number): number
|
|
if period <= 0 then
|
|
return 0
|
|
end
|
|
-- exact match first, then the nearest note on a log scale
|
|
for i = 1, #PERIODS do
|
|
if PERIODS[i] == period then
|
|
return i + Tables.PERIOD_FIRST_NOTE - 1
|
|
end
|
|
end
|
|
local n = 49 + math.floor(12 * math.log(428 / period) / math.log(2) + 0.5)
|
|
return math.clamp(n, 1, 120)
|
|
end
|
|
|
|
-- noteToPeriod(note) -> ProTracker period (finetune 0) for a note, computed past the table's ends
|
|
function Tables.noteToPeriod(note: number): number
|
|
local i = note - Tables.PERIOD_FIRST_NOTE + 1
|
|
if i >= 1 and i <= #PERIODS then
|
|
return PERIODS[i]
|
|
end
|
|
return math.max(1, math.floor(428 * 2 ^ ((49 - note) / 12) + 0.5))
|
|
end
|
|
|
|
-- IT volume column "g" (tone portamento) speeds
|
|
Tables.IT_VOLPORTA = { 0, 1, 4, 8, 16, 32, 64, 96, 128, 255 }
|
|
|
|
-- XM Rxy / S3M-IT Qxy volume changes: kind 0 add, 1 multiply; value
|
|
Tables.RETRIG_ADD = { 0, -1, -2, -4, -8, -16, 0, 0, 0, 1, 2, 4, 8, 16, 0, 0 }
|
|
Tables.RETRIG_MUL = { 1, 1, 1, 1, 1, 1, 2 / 3, 0.5, 1, 1, 1, 1, 1, 1, 1.5, 2 }
|
|
|
|
-- the Amiga clock constant: frequency = AMIGA / period, with period 1712 playing an 8363 Hz sample at its base rate
|
|
-- (FT2 / ST3 period units, 4x ProTracker's)
|
|
Tables.AMIGA = 8363 * 1712
|
|
-- MOD: the PAL Amiga clock (7093789.2 Hz / 2 per ProTracker period unit; period 428 plays at 8287 Hz)
|
|
Tables.AMIGA_PAL = 7093789.2 / 2 * 4
|
|
|
|
-- st2Tempo(byte) -> speed, tempo: a Scream Tracker 2 tempo byte (high nibble ticks per row, low nibble a fine factor)
|
|
-- as OpenMPT plays it (ConvertST2Tempo: ST2's tick length at its 23863 Hz mixing rate, as a fractional BPM)
|
|
local ST2_FACTOR = { 140, 50, 25, 15, 10, 7, 6, 4, 3, 3, 2, 2, 2, 2, 1, 1 }
|
|
function Tables.st2Tempo(byte: number): (number, number)
|
|
local hi, lo = byte // 16, byte % 16
|
|
local d = 50 - (ST2_FACTOR[hi + 1] * lo) // 16 -- never 0; negative at tempo 06..0F (C integer division truncates)
|
|
local spt = if d > 0 then 23863 // d else -(23863 // -d)
|
|
if spt <= 0 then
|
|
spt += 65536
|
|
end
|
|
return math.max(hi, 1), math.floor((23863 * 50000 + spt) / (spt * 2)) / 10000
|
|
end
|
|
|
|
return Tables
|