ws-audio2: music/okt (Oktalyzer): it family with ProTracker periods (PAL), mixed channel pairs and their shared volume, three arpeggios, note slides, sustain loops released by R
This commit is contained in:
1 parent
6b5e8ec6cf
commit
9a5f3796e2
6 files changed
+455
No files matched your search
@@ -902,6 +902,18 @@ local list: { Entry } = {
|
||||
return require("@formats/music/mtm")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "okt",
|
||||
category = "music",
|
||||
path = "music/okt",
|
||||
extensions = { "okt", "okta" },
|
||||
mime = { "audio/x-okt" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Chunks", "codecs/audio/Tracker", "codecs/audio/Tracker/Load" },
|
||||
caps = { decode = true, encode = false, render = true, channels = { 4, 8 } },
|
||||
load = function()
|
||||
return require("@formats/music/okt")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "ptm",
|
||||
category = "music",
|
||||
|
||||
@@ -0,0 +1,286 @@
|
||||
--!strict
|
||||
-- music/okt: Oktalyzer modules (OKTASONG, Armin Sander's Amiga tracker; 4 to 8 channels: each of the Amiga's four can
|
||||
-- be split into a mixed pair). Ported from OpenMPT's Load_okt.cpp (BSD, licenses/openmpt-LICENSE.txt). An IFF-style
|
||||
-- chunk list (containers/Chunks) read into the codecs/audio/Tracker Module, played as the "it" effect family with
|
||||
-- ProTracker periods and the PAL clock (flags.periods = "amiga"), Amiga period limits, slides on every tick, sample
|
||||
-- loops as sustain loops (released by R), Oktalyzer's three arpeggios, note slides (D / U every tick, L / H once:
|
||||
-- flags.oktSlides), and its volume command: on a mixed pair the volume is the pair's channel volume.
|
||||
-- Not supported: the Amiga LED filter (F), writing.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Chunks = require("@formats/containers/Chunks")
|
||||
local Tracker = require("@formats/codecs/audio/Tracker")
|
||||
local Load = require("@formats/codecs/audio/Tracker/Load")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Okt = {
|
||||
name = "okt",
|
||||
category = "music",
|
||||
extensions = { "okt", "okta" },
|
||||
mime = { "audio/x-okt" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Chunks", "codecs/audio/Tracker", "codecs/audio/Tracker/Load" },
|
||||
caps = { decode = true, encode = false, render = true, channels = { 4, 8 } },
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- refuse more sample data in total (f32 bytes, default 2^28)
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local IT = Load.IT
|
||||
local LAYOUT = table.freeze({ bigEndian = true }) :: Chunks.Layout
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Okt.sniff(r: Bytes.Reader): number
|
||||
return if r:peek(8) == "OKTASONG" then 1 else 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- a negative arpeggio offset as the equivalent positive one
|
||||
local function invert(p: number): number
|
||||
p %= 16
|
||||
if p == 0 then
|
||||
return 0
|
||||
end
|
||||
return if p <= 12 then 12 - p else 24 - p
|
||||
end
|
||||
|
||||
-- the Oktalyzer effect of a cell -> effect name?, param, volume (0..64, the volume column) or -1, a key off
|
||||
local function effect(e: number, p: number, paired: boolean): (string?, number, number, boolean)
|
||||
if e == 1 and p > 0 then
|
||||
return "portaup", p, -1, false -- "portamento down (period)": the period goes down
|
||||
elseif e == 2 and p > 0 then
|
||||
return "portadown", p, -1, false
|
||||
elseif e == 10 and p > 0 then
|
||||
return "arpeggio", p % 16 + invert(p // 16) * 16, -1, false -- down, original, up
|
||||
elseif e == 11 and p > 0 then
|
||||
return "arpeggio", p - p % 16 + invert(p % 16), -1, false -- original, up, original, down
|
||||
elseif e == 12 and p > 0 then
|
||||
return "arpeggio", p, -1, false -- up, up, original
|
||||
elseif e == 13 and p > 0 then
|
||||
return "noteslidedown", 0x10 + math.min(p, 15), -1, false
|
||||
elseif e == 30 and p > 0 then
|
||||
return "noteslideup", 0x10 + math.min(p, 15), -1, false
|
||||
elseif e == 21 and p > 0 then
|
||||
return "noteslidedown", 0x50 + math.min(p, 15), -1, false
|
||||
elseif e == 17 and p > 0 then
|
||||
return "noteslideup", 0x50 + math.min(p, 15), -1, false
|
||||
elseif e == 25 then
|
||||
return "jump", p, -1, false
|
||||
elseif e == 27 then
|
||||
return nil, 0, -1, true
|
||||
elseif e == 28 and p < 0x20 then
|
||||
return "speed", p, -1, false
|
||||
elseif e == 31 then
|
||||
local x, y = p // 16, p % 16
|
||||
local slide = if paired then "chanvolslide" else "volslide"
|
||||
if p <= 0x40 then
|
||||
if paired then
|
||||
return "chanvol", p, -1, false
|
||||
end
|
||||
return nil, 0, p, false
|
||||
elseif x == 4 then
|
||||
return slide, y, -1, false
|
||||
elseif x == 5 then
|
||||
return slide, y * 16, -1, false
|
||||
elseif x == 6 then
|
||||
return slide, 0xF0 + math.min(y, 0xE), -1, false
|
||||
elseif x == 7 then
|
||||
return slide, math.min(y, 0xE) * 16 + 0xF, -1, false
|
||||
end
|
||||
end
|
||||
return nil, 0, -1, false
|
||||
end
|
||||
|
||||
function Okt.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
local budget = { left = opts and opts.maxBytes or 2 ^ 28 }
|
||||
if r:peek(8) ~= "OKTASONG" then
|
||||
return nil, Err.new(if r:size() < 8 then Err.TRUNCATED else Err.CORRUPT, "no OKTASONG signature")
|
||||
end
|
||||
local buf = r.buf
|
||||
local list = Chunks.walk(buf, r.base + 8, r.limit, LAYOUT)
|
||||
local cmod = Chunks.find(list, "CMOD")
|
||||
if not cmod or cmod.avail < 8 then
|
||||
return nil, Err.new(Err.CORRUPT, "no channel table (CMOD)")
|
||||
end
|
||||
-- channels: each Amiga channel is one channel, or a mixed pair
|
||||
local paired: { number } = {} -- 1: the first of a pair, -1: the second, 0: a raw Paula channel
|
||||
local pans: { number } = {}
|
||||
for ch = 0, 3 do
|
||||
local pan = if ch == 1 or ch == 2 then 0xC0 else 0x40
|
||||
if buffer.readu16(buf, cmod.offset + ch * 2) ~= 0 then
|
||||
table.insert(paired, 1)
|
||||
table.insert(pans, pan)
|
||||
table.insert(paired, -1)
|
||||
else
|
||||
table.insert(paired, 0)
|
||||
end
|
||||
table.insert(pans, pan)
|
||||
end
|
||||
local nch = #paired
|
||||
local m = Tracker.newModule("okt", nch, "it")
|
||||
m.linear = false
|
||||
m.instrumentMode = false
|
||||
m.tempo = 125
|
||||
m.flags.periods, m.flags.amigaLimits, m.flags.fastPortas, m.flags.oktSlides = "amiga", true, true, true
|
||||
for i = 1, nch do
|
||||
m.channelPan[i] = pans[i]
|
||||
end
|
||||
-- samples: 32-byte headers (name, length, loop start / length in words, volume, type 0 / 1 / 2)
|
||||
local types: { number } = {}
|
||||
local samp = Chunks.find(list, "SAMP")
|
||||
if samp then
|
||||
for i = 1, math.min(samp.avail // 32, 36) do
|
||||
local h = Bytes.reader(buf, samp.offset + (i - 1) * 32, 32)
|
||||
local s = Tracker.newSample(h:fixed(20))
|
||||
local len, ls, ll, vol, typ = h:u32be(), h:u16be() * 2, h:u16be() * 2, h:u16be(), h:u16be()
|
||||
s.c5speed = 8287
|
||||
s.volume = math.min(vol, 64)
|
||||
s.length = len - len % 2
|
||||
if ll > 2 and ls + ll <= s.length then
|
||||
s.susStart, s.susEnd, s.susType = ls, ls + ll, 1
|
||||
end
|
||||
types[i] = typ
|
||||
m.samples[i] = s
|
||||
end
|
||||
end
|
||||
local spee = Chunks.find(list, "SPEE")
|
||||
if spee and spee.avail >= 2 then
|
||||
m.speed = math.clamp(buffer.readu8(buf, spee.offset) * 256 + buffer.readu8(buf, spee.offset + 1), 1, 255)
|
||||
end
|
||||
local nord = 0
|
||||
local plen = Chunks.find(list, "PLEN")
|
||||
if plen and plen.avail >= 2 then
|
||||
nord = buffer.readu8(buf, plen.offset) * 256 + buffer.readu8(buf, plen.offset + 1)
|
||||
end
|
||||
local patt = Chunks.find(list, "PATT")
|
||||
for i = 1, math.min(nord, if patt then patt.avail else 0) do
|
||||
m.order[i] = buffer.readu8(buf, (patt :: Chunks.Chunk).offset + i - 1)
|
||||
end
|
||||
-- patterns (PBOD: rows, then 4 bytes per cell: note 1..36, sample, effect, parameter)
|
||||
local copies: { [number]: boolean } = {} -- type B samples with a loop also need an unlooped copy (paired channels)
|
||||
local npat = 0
|
||||
for _, c in list do
|
||||
if c.id == "PBOD" and npat < 256 then
|
||||
local pr = Bytes.reader(buf, c.offset, c.avail)
|
||||
local rows = if c.avail >= 2 then math.clamp(pr:u16be(), 1, 1024) else 64
|
||||
local pat = Tracker.newPattern(rows, nch)
|
||||
npat += 1
|
||||
m.patterns[npat] = pat
|
||||
local d = pat.data
|
||||
for row = 0, rows - 1 do
|
||||
for ch = 0, nch - 1 do
|
||||
local note, ins, e, p = pr:u8(), pr:u8(), pr:u8(), pr:u8()
|
||||
if pr.err then
|
||||
break
|
||||
end
|
||||
local q = (row * nch + ch) * 5
|
||||
local pair = paired[ch + 1] ~= 0
|
||||
if note > 0 and note <= 36 then
|
||||
local n = note + 36 -- note 13 = C-4 = ProTracker period 428 (OpenMPT keeps MOD notes an octave up)
|
||||
if pair and n >= 49 + 22 then
|
||||
n = 49 + 21 -- the mixer's highest note
|
||||
end
|
||||
local smp = ins + 1
|
||||
local s = m.samples[smp]
|
||||
if s then
|
||||
if pair and s.volume < 64 then
|
||||
buffer.writeu8(d, q + 2, 64 + 1) -- the default volume only works on raw Paula channels
|
||||
end
|
||||
local t = types[smp]
|
||||
if (t == 1 and pair) or (t == 0 and not pair) then
|
||||
smp += 100 -- a sample of the wrong type stops the channel
|
||||
elseif t == 2 and pair and s.susType > 0 then
|
||||
copies[smp] = true
|
||||
smp += 36
|
||||
end
|
||||
end
|
||||
buffer.writeu8(d, q, n)
|
||||
buffer.writeu8(d, q + 1, smp)
|
||||
end
|
||||
local name, param, vol, off = effect(e, p, pair)
|
||||
if off then
|
||||
buffer.writeu8(d, q, 255)
|
||||
buffer.writeu8(d, q + 1, 0)
|
||||
end
|
||||
if vol >= 0 then
|
||||
buffer.writeu8(d, q + 2, vol + 1)
|
||||
end
|
||||
local fx, fp = Load.itEffect(name, param)
|
||||
if fx ~= 0 then
|
||||
buffer.writeu8(d, q + 3, fx)
|
||||
buffer.writeu8(d, q + 4, fp)
|
||||
if name == "chanvol" or name == "chanvolslide" then
|
||||
-- a mixed pair shares its volume: the other channel gets it too (the second one wins)
|
||||
local other = ch + paired[ch + 1]
|
||||
local o2 = (row * nch + other) * 5
|
||||
local ofx = buffer.readu8(d, o2 + 3)
|
||||
if ofx == 0 or ofx == IT.chanvol or ofx == IT.chanvolslide then
|
||||
buffer.writeu8(d, o2 + 3, fx)
|
||||
buffer.writeu8(d, o2 + 4, fp)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
-- sample data: SBOD chunks in order (signed 8-bit), for the samples with a length
|
||||
local k = 0
|
||||
local bodies = {}
|
||||
for _, c in list do
|
||||
if c.id == "SBOD" and c.avail > 0 then
|
||||
table.insert(bodies, c)
|
||||
end
|
||||
end
|
||||
local nsmp = #m.samples
|
||||
for i = 1, nsmp do
|
||||
local s = m.samples[i]
|
||||
if s.length > 0 then
|
||||
k += 1
|
||||
local c = bodies[k]
|
||||
if not c then
|
||||
s.length = 0
|
||||
continue
|
||||
end
|
||||
local ok, err = Load.readSample(buf, c.offset, math.min(s.length, c.avail), { bits = 8, signed = true, limit = c.offset + c.avail }, s, budget)
|
||||
if not ok then
|
||||
return nil, err
|
||||
end
|
||||
end
|
||||
end
|
||||
for i = 1, nsmp do
|
||||
local s = m.samples[i]
|
||||
if copies[i] then
|
||||
local cp = Tracker.newSample(s.name)
|
||||
cp.c5speed, cp.volume, cp.data, cp.length = s.c5speed, s.volume, s.data, s.length
|
||||
m.samples[i + 36] = cp
|
||||
end
|
||||
end
|
||||
for i = 1, (if next(copies) then 72 else nsmp) do
|
||||
if m.samples[i] == nil then
|
||||
m.samples[i] = Tracker.newSample()
|
||||
end
|
||||
end
|
||||
m.meta.source = { format = "okt", channels = nch }
|
||||
local ok, verr = Tracker.validate(m)
|
||||
if not ok then
|
||||
return nil, Err.new(Err.CORRUPT, verr and verr.message or "invalid module")
|
||||
end
|
||||
return m, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ open
|
||||
-- open(r, opts) -> the module rendered as an audio Decoder (opts: Tracker.RenderOptions + DecodeOptions)
|
||||
function Okt.open(r: Bytes.Reader, opts: any?): (Types.Decoder?, Err.Err?)
|
||||
local m, err = Okt.decode(r, opts)
|
||||
if not m then
|
||||
return nil, err
|
||||
end
|
||||
return Tracker.render(m, opts)
|
||||
end
|
||||
|
||||
return Okt
|
||||
Reference in new issue
Block a user