From 33d87e0c4d1277f394e4c3c539a0b8c71fc09472 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 14:13:48 -0500 Subject: [PATCH] ws-audio2: music/669 (Composer 669 / UNIS 669): s3m family with Hz periods, fast + auto slides, frequency adjust, per-pattern speed and break rows --- docs/FORMATS.md | 3 +- src/Formats/Registry.luau | 12 ++ src/Formats/music/669/init.luau | 209 ++++++++++++++++++++++++++++++++ tests/music/669.luau | 19 +++ tools/fixtures/669.py | 94 ++++++++++++++ tools/fixtures/_tracker.py | 30 +++++ 6 files changed, 366 insertions(+), 1 deletion(-) create mode 100644 src/Formats/music/669/init.luau create mode 100644 tests/music/669.luau create mode 100644 tools/fixtures/669.py diff --git a/docs/FORMATS.md b/docs/FORMATS.md index 891c26c..4432463 100644 --- a/docs/FORMATS.md +++ b/docs/FORMATS.md @@ -73,6 +73,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build | subtitle | ass | .ass .ssa | yes | yes | styles: true | `tests/subtitle/ass.luau` | | subtitle | srt | .srt | yes | yes | styles: false | `tests/subtitle/srt.luau` | | subtitle | vtt | .vtt .webvtt | yes | yes | styles: true | `tests/subtitle/vtt.luau` | +| music | 669 | .669 | yes | - | render: true, channels: 8, 8 | `tests/music/669.luau` | | music | it | .it | yes | - | render: true, channels: 1, 64, compressedSamples: true | `tests/music/it.luau` | | music | midi | .mid .midi .smf .kar .rmi | yes | yes | formats: 0, 1, 2, render: true, streaming: true, rmid: true | `tests/music/midi.luau` | | music | mod | .mod .m15 .stk .nst | yes | yes | render: true, channels: 1, 32 | `tests/music/mod.luau` | @@ -97,7 +98,7 @@ Codecs (layer 3, shared by formats; not loadable on their own), by domain: - codecs/audio/Resample: audio sample-rate conversion by band-limited (Kaiser-windowed sinc) interpolation, streaming or - codecs/audio/SoundFont: SoundFont 2 banks and the sample synth that plays MIDI through them. A codec (layer 1) shared by - codecs/audio/Stream: the streaming Decoder (Types.Decoder: read / seek / position) over a packetised codec, the part - - codecs/audio/Tracker: the shared tracker engine behind music/mod, music/s3m, music/xm and music/it. The four loaders read + - codecs/audio/Tracker: the shared tracker engine behind music/mod, music/s3m, music/xm and music/it, and the loaders - codecs/audio/Vorbis: the Vorbis I decoder without its container: identification / comment / setup headers (Setup), - codecs/audio/VorbisComment: Vorbis comment fields ("KEY=value", the tag block of Vorbis, Opus and FLAC) <-> Types.Tags. - **compress** diff --git a/src/Formats/Registry.luau b/src/Formats/Registry.luau index af40316..2638300 100644 --- a/src/Formats/Registry.luau +++ b/src/Formats/Registry.luau @@ -830,6 +830,18 @@ local list: { Entry } = { return require("@formats/subtitle/vtt") end, }, + { + name = "669", + category = "music", + path = "music/669", + 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 } }, + load = function() + return require("@formats/music/669") + end, + }, { name = "it", category = "music", diff --git a/src/Formats/music/669/init.luau b/src/Formats/music/669/init.luau new file mode 100644 index 0000000..051ed29 --- /dev/null +++ b/src/Formats/music/669/init.luau @@ -0,0 +1,209 @@ +--!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("@formats/core/Err") +local Bytes = require("@formats/core/Bytes") +local Types = require("@formats/Types") +local Tracker = require("@formats/codecs/audio/Tracker") +local Load = require("@formats/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 diff --git a/tests/music/669.luau b/tests/music/669.luau new file mode 100644 index 0000000..668b967 --- /dev/null +++ b/tests/music/669.luau @@ -0,0 +1,19 @@ +-- music/669 tests: generated Composer 669 / UNIS 669 modules (tools/fixtures/669.py) parsed, rendered through +-- codecs/audio/Tracker and compared with openmpt123's render (loudness envelope, band spectrum, stereo balance, song +-- length), the contract, and the fuzz no-throw pass. +local T = require("@tests/T") +local S = require("@tests/music/_similar") +local Six69 = require("@formats/music/669") +local Cases = require("@tests/gen/669_cases") + +local t = T.suite("669") + +t:eq(Six69.name, "669", "name") +t:eq(Six69.category, "music", "category") +t:check(Six69.caps.decode and not Six69.caps.encode, "caps") + +local mods = S.against(t, Six69, Cases, { gainDb = 0.5, envErr = 1.0, spectral = 0.95, balance = 0.04, duration = 0.01 }, "s3m") +t:check(mods[2] and mods[2].meta.source.tracker == "UNIS 669", "UNIS 669") + +t:fuzz("decode", S.fuzzer(Six69), S.samples(Cases), { mutations = 30 }) +t:done() diff --git a/tools/fixtures/669.py b/tools/fixtures/669.py new file mode 100644 index 0000000..df848ab --- /dev/null +++ b/tools/fixtures/669.py @@ -0,0 +1,94 @@ +# 669 fixtures (tools/fixtures.py): generated Composer 669 / UNIS 669 modules exercising the effects, rendered by +# openmpt123. +# tests/gen/669_cases.luau { name, data (the module), ref (features of openmpt's render, tools/fixtures/_tracker.py), +# duration (openmpt's song length) } +import os, sys + +# the shared tracker module, loaded by path (tools/fixtures/ must not go on sys.path: xml.py there would shadow xml) +T = sys.modules.get("_tracker") +if T is None: + import importlib.util + _spec = importlib.util.spec_from_file_location("_tracker", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_tracker.py")) + T = importlib.util.module_from_spec(_spec) + sys.modules["_tracker"] = T + _spec.loader.exec_module(T) + +INPUTS = ["tools/fixtures/_tracker.py"] + + +def empty(): + return [[(0, 0, None, None) for _ in range(8)] for _ in range(64)] + + +def put(p, row, ch, note=0, ins=0, vol=None, cmd=None, param=0): + p[row][ch] = (note, ins, vol, None if cmd is None else (cmd << 4) | param) + + +def samples(): + return [ + {"name": "sine", "data": T.u8(T.synth("sine", 64, 64)), "loop": (0, 64)}, + {"name": "square", "data": T.u8(T.synth("square", 64, 64, 0.5)), "loop": (0, 64)}, + {"name": "saw", "data": T.u8(T.synth("saw", 100, 100, 0.6)), "loop": (0, 100)}, + {"name": "noise", "data": T.u8(T.synth("noise", 6000, 1, 0.7, decay=1500))}, + {"name": "tri", "data": T.u8(T.synth("tri", 50, 50, 0.8)), "loop": (0, 50)}, + ] + + +def song_effects(keep=None): + a, b = empty(), empty() + mel = [61, 65, 68, 73, 68, 65, 63, 61] + for i in range(16): + put(a, i * 4, 0, mel[i % 8], 1, 15) + put(a, 0, 1, 49, 2, 12) + put(a, 8, 1, 0, 0, None, 0, 2) # auto portamento up (runs until the next effect) + put(a, 16, 1, 0, 0, None, 1, 3) # auto portamento down + put(a, 24, 1, 0, 0, None, 5, 3) # speed (stops the auto slide) + put(a, 28, 1, 56, 2, 10) + put(a, 32, 1, 61, 2, None, 2, 2) # tone portamento (keeps going) + put(a, 40, 1, 49, 2, None, 2, 4) + put(a, 48, 1, 0, 0, 6, 3, 5) # frequency adjust + put(a, 52, 1, 0, 0, None, 4, 3) # frequency vibrato + for r in range(0, 64, 8): + put(a, r, 3, 61, 4, 15 - r // 8) + put(a, 4, 2, 37, 3, 15) + put(a, 36, 2, 0, 0, 4) + put(a, 10, 4, 73, 5, 9) + put(a, 20, 4, 0, 0, None, 4, 6) + # b: speed per pattern, a break row, more slides + put(b, 0, 0, 49, 1, 15) + put(b, 4, 0, 0, 0, None, 1, 1) + put(b, 12, 0, 61, 1, 15) + put(b, 16, 0, 0, 0, None, 0, 1) + put(b, 20, 0, 0, 0, 10, None) + put(b, 0, 2, 37, 3, 12) + put(b, 8, 2, 44, 3, None, 2, 1) + put(b, 24, 2, 0, 0, None, 3, 2) + put(b, 0, 5, 56, 5, 13) + put(b, 16, 5, 52, 2, 13) + if keep is not None: + for p in (a, b): + for row in p: + for ch in range(8): + if ch not in keep: + row[ch] = (0, 0, None, None) + return T.write_669("effects 669", samples(), [0, 1], [a, b], [4, 3], [63, 40]) + + +def song_unis(): + """UNIS 669: balance slides, slot retrigger, a restart position""" + a = empty() + put(a, 0, 0, 61, 1, 15, 6, 0) # balance slide left (repeats until the next note) + put(a, 16, 0, 64, 1, 15, 6, 1) # right + put(a, 32, 1, 49, 4, 15, 7, 3) # slot retrigger + put(a, 40, 1, 49, 4, 15, 7, 6) + put(a, 8, 2, 68, 2, 11) + put(a, 48, 3, 56, 3, 14) + return T.write_669("unis", samples(), [0, 0], [a], [5], [63], extended=True) + + +def generate(ctx): + cases = [] + for nm, data in (("effects", song_effects()), ("unis", song_unis())): + ref = T.reference(ctx, data, "669") + cases.append({"name": nm, "data": data, "ref": ref, "duration": ref["duration"]}) + ctx.emit("669_cases", cases) diff --git a/tools/fixtures/_tracker.py b/tools/fixtures/_tracker.py index db34ec3..361f948 100644 --- a/tools/fixtures/_tracker.py +++ b/tools/fixtures/_tracker.py @@ -545,3 +545,33 @@ def write_mtm(title, samples, orders, patterns, channels, pans=None, rows=64, co def u16(xs): return b"".join(struct.pack("= 37), ins 1-based, vol 0..15 or None, effect byte (cmd << 4 | param) + or None; tempos / breaks: per pattern""" + out = bytearray(b"JN" if extended else b"if") + msg = message.encode()[:108] + out += msg + b" " * (108 - len(msg)) + out += bytes([len(samples), len(patterns), restart]) + out += bytes(orders) + b"\xff" * (128 - len(orders)) + out += bytes(tempos) + b"\x04" * (128 - len(tempos)) + out += bytes(breaks) + b"\x3f" * (128 - len(breaks)) + for s in samples: + ls, le = s.get("loop") or (0, 0xFFFFF) + out += name(s.get("name", ""), 13) + struct.pack("> 4), ((i & 15) << 4) | (vol if vol is not None else 0)]) + elif vol is not None: + out += bytes([0xFE, vol]) + else: + out += b"\xff\x00" + out.append(0xFF if eff is None else eff) + for s in samples: + out += s["data"] + return bytes(out)