--!strict -- music/669: Composer 669 and UNIS 669 modules ("if" / "JN"; 8 channels, 64-row patterns with their own speed and -- break row). Ported from OpenMPT's Load_669.cpp (BSD, licenses/openmpt-LICENSE.txt). Reads into the -- codecs/audio/Tracker Module and plays as the "s3m" effect family with 669's quirks (flags): periods in Hz (slides -- move the frequency 80 Hz per step, tone portamento 40 Hz), slides on every tick, auto portamentos that run until -- the channel's next effect, a tone portamento that keeps going, frequency adjust (D), frequency vibrato (E), speed (F), -- UNIS 669 balance slides (G) and slot retrigger (H). The tempo is fixed (78 BPM in tracker terms). -- Not supported: UNIS 669's "super fast" speed 0, writing. -- ------------------------------------------------------------------------------------------------ requires local Err = require("@rosetta/core/Err") local Bytes = require("@rosetta/core/Bytes") local Types = require("@rosetta/Types") local Tracker = require("@rosetta/codecs/audio/Tracker") local Load = require("@rosetta/codecs/audio/Tracker/Load") -- ------------------------------------------------------------------------------------------------ contract local Six69 = { name = "669", category = "music", extensions = { "669" }, mime = { "audio/x-669" }, deps = { "core/Err", "core/Bytes", "Types", "codecs/audio/Tracker", "codecs/audio/Tracker/Load" }, caps = { decode = true, encode = false, render = true, channels = { 8, 8 } }, } export type DecodeOptions = { maxBytes: number?, -- refuse more sample data in total (f32 bytes, default 2^28) } -- ------------------------------------------------------------------------------------------------ constants local IT = Load.IT -- the 669 effects (high nibble of the effect byte) by name local EFFECTS = { "autoportaup", "autoportadown", "toneporta", "freqadjust", "vibrato", "speed", "panslide", "retrig" } -- ------------------------------------------------------------------------------------------------ sniff -- the 497-byte header is valid (669 has a two-byte magic only, so the rest is checked too) local function validHeader(r: Bytes.Reader): boolean if r:size() < 497 then return false end local magic = r:peek(2) if magic ~= "if" and magic ~= "JN" then return false end local nsmp, npat, restart = r:peekU8(110), r:peekU8(111), r:peekU8(112) if nsmp > 64 or restart >= 128 or npat > 128 then return false end local bad = 0 for i = 2, 109 do local c = r:peekU8(i) if c > 0 and c <= 31 then bad += 1 if bad > 40 then return false end end end for i = 0, 127 do local o, t, b = r:peekU8(113 + i), r:peekU8(241 + i), r:peekU8(369 + i) if (o >= 128 and o < 0xFE) or (o < 128 and t == 0) or t > 15 or b >= 64 then return false end end return true end function Six69.sniff(r: Bytes.Reader): number return if validHeader(r) then 0.6 else 0 end -- ------------------------------------------------------------------------------------------------ decode function Six69.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?) local budget = { left = opts and opts.maxBytes or 2 ^ 28 } if not validHeader(r) then return nil, Err.new(if r:size() < 497 then Err.TRUNCATED else Err.CORRUPT, "not a 669 module") end local extended = r:peek(2) == "JN" r:skip(2) local message = r:string(108) local nsmp, npat, restart = r:u8(), r:u8(), r:u8() local orders, tempos, breaks = {}, {}, {} for i = 1, 128 do orders[i] = r:u8() end for i = 1, 128 do tempos[i] = r:u8() end for i = 1, 128 do breaks[i] = r:u8() end if not r:need(nsmp * 25 + npat * 1536) then return nil, r.err end local m = Tracker.newModule("669", 8, "s3m") m.title = (string.gsub(string.sub(message, 1, 36), "[%z ]+$", "")) m.meta.message = message m.speed, m.tempo = 4, 78 m.linear = false m.instrumentMode = false m.flags.periods, m.flags.hzSlide, m.flags.hzTonePorta = "hz", 20, 0.5 m.flags.fastPortas, m.flags.autoTonePorta, m.flags.autoReset = true, true, true for i = 1, 8 do m.channelPan[i] = if i % 2 == 0 then 0xD0 else 0x30 end local lengths: { number } = {} for i = 1, nsmp do local s = Tracker.newSample(r:fixed(13)) local len, ls, le = r:u32le(), r:u32le(), r:u32le() if len >= 0x4000000 then return nil, Err.new(Err.CORRUPT, "sample " .. i .. " too long") end s.c5speed = 8363 if not (le > len and ls == 0) and le ~= 0 then s.loopStart, s.loopEnd, s.loopType = ls, math.min(le, len), 1 end lengths[i] = len m.samples[i] = s end for i = 1, 128 do m.order[i] = orders[i] end while #m.order > 0 and m.order[#m.order] == 255 do table.remove(m.order) end if orders[restart + 1] < npat then m.restart = restart end -- patterns: 64 rows x 8 channels x 3 bytes (note / instrument, instrument / volume, effect / parameter) local buf = r.buf for p = 0, npat - 1 do local pat = Tracker.newPattern(64, 8) m.patterns[p + 1] = pat local d = pat.data local effect = table.create(8, 0xFF) -- the last effect per channel (balance slides repeat) for row = 0, 63 do for ch = 0, 7 do local o = r.pos + (row * 8 + ch) * 3 local ni, iv, ep = buffer.readu8(buf, o), buffer.readu8(buf, o + 1), buffer.readu8(buf, o + 2) local q = (row * 8 + ch) * 5 local hasNote = ni < 0xFE if hasNote then buffer.writeu8(d, q, ni // 4 + 37) buffer.writeu8(d, q + 1, (ni % 4) * 16 + iv // 16 + 1) effect[ch + 1] = 0xFF end if ni <= 0xFE then buffer.writeu8(d, q + 2, ((iv % 16) * 64 + 8) // 15 + 1) end if ep ~= 0xFF then effect[ch + 1] = ep end local e = effect[ch + 1] if e ~= 0xFF then local cmd, param = e // 16, e % 16 local name = EFFECTS[cmd + 1] if cmd == 4 then param = param * 17 -- vibrato: speed = depth elseif cmd == 6 then param = if param == 0 then 0x4F elseif param == 1 then 0xF4 else -1 elseif cmd == 7 and not (hasNote and extended) then name = nil end if name ~= "panslide" then effect[ch + 1] = 0xFF end if name and param >= 0 then buffer.writeu8(d, q + 3, IT[name]) buffer.writeu8(d, q + 4, param) end end end end r:skip(1536) if breaks[p + 1] < 63 then Load.writeEffect(m, p, breaks[p + 1], IT["break"], 0, "next") end Load.writeEffect(m, p, 0, IT.speed, tempos[p + 1], "next") end -- sample data: unsigned 8-bit local pos = r.pos for i = 1, nsmp do local s = m.samples[i] local ok, err = Load.readSample(buf, pos, lengths[i], { bits = 8, signed = false, limit = r.limit }, s, budget) if not ok then return nil, err end pos += lengths[i] end m.meta.source = { format = "669", tracker = if extended then "UNIS 669" else "Composer 669" } 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 Six69.open(r: Bytes.Reader, opts: any?): (Types.Decoder?, Err.Err?) local m, err = Six69.decode(r, opts) if not m then return nil, err end return Tracker.render(m, opts) end return Six69