--!strict --!native --!optimize 2 -- codecs/audio/Tracker/Tables: tracker lookup tables, 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 } 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 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; note 1 = C-0 (FT2 numbering), C-2 (428) = note 49 plays at 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) function Tables.periodToNote(period: number): number if period <= 0 then return 0 end 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 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 } -- frequency = AMIGA / period; period 1712 plays an 8363 Hz sample at its base rate (FT2 / ST3 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 -- Scream Tracker 2 tempo byte as OpenMPT plays it (ConvertST2Tempo: ST2's tick length at 23863 Hz, 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