codecs/audio: comments to specs only

This commit is contained in:
4DBug committed 2026-11-01 01:55:39 -05:00
1 parent 0bd7b87d2b
commit 6dcce481bb
69 files changed
+523 -1604

No files matched your search

+13 -15
View File
@@ -1,8 +1,7 @@
--!strict
-- codecs/audio/Hively/Load: AHX ("THX\0".."THX\2") and HivelyTracker ("HVL\0", "HVL\1") module files -> the replayer's
-- Tune. Ported from hvl2wav's replay.c 1.9 loaders (hvl_load_ahx / hvl_reset; BSD-3-Clause, (c) 2006-2018 Pete Gordon;
-- licenses/hivelytracker-LICENSE.txt), with bounds checks the C code does not have. Errors are returned as
-- (nil, message, kind) for core/Err.from.
-- codecs/audio/Hively/Load: AHX ("THX\0".."THX\2") and HivelyTracker ("HVL\0", "HVL\1") module files -> Tune.
-- Ported from hvl2wav replay.c 1.9 (hvl_load_ahx / hvl_reset; BSD-3-Clause, (c) 2006-2018 Pete Gordon;
-- licenses/hivelytracker-LICENSE.txt), with bounds checks the C code does not have.
local Load = {}
local readu8, readi8 = buffer.readu8, buffer.readi8
@@ -27,23 +26,23 @@ export type Instrument = {
plist: { speed: number, length: number, entries: { PlistEntry } },
}
export type Tune = {
format: string, -- "ahx" | "hvl"
version: number, -- the 4th byte: AHX 0..2, HVL 0..1
format: string,
version: number,
title: string,
channels: number, -- 4 (AHX), 4..16 (HVL)
channels: number,
positionNr: number,
restart: number,
speedMultiplier: number, -- 1..4 player calls per 50 Hz frame
trackLength: number, -- rows per track, 1..64
trackNr: number, -- the highest track number stored
speedMultiplier: number,
trackLength: number,
trackNr: number,
instrumentNr: number,
subsongs: { number }, -- first positions of subsongs 1..n
subsongs: { number },
positions: buffer, -- positionNr * 16 * 2 bytes: per channel track (u8), transpose (i8)
tracks: buffer, -- 257 * 64 * 6 bytes: per track row note, instrument, fx, param, fxb, paramb
instruments: { Instrument }, -- 1-based, as numbered in the file
instruments: { Instrument },
mixgain: number, -- (percent * 256) / 100, HVL only (AHX: set by the stereo separation at render time)
stereo: number, -- HVL: the default stereo separation 0..4
emptyTrack0: boolean, -- track 0 is not stored (always empty)
stereo: number,
emptyTrack0: boolean,
}
local function cstring(b: buffer, p: number, limit: number, max: number): (string, number)
@@ -54,7 +53,6 @@ local function cstring(b: buffer, p: number, limit: number, max: number): (strin
return buffer.readstring(b, p, e - p), e
end
-- parse(b, base, limit) -> Tune | nil, message, kind
function Load.parse(b: buffer, base: number, limit: number): (Tune?, string?, string?)
local size = limit - base
if size < 14 then
+5 -19
View File
@@ -1,10 +1,8 @@
--!strict
--!native
-- codecs/audio/Hively/Player: the AHX / HivelyTracker replayer: positions and tracks (two effect columns in HVL), the
-- instrument engine (ADSR, vibrato, the PList performance list, square modulation, the resonant filter sweep, hard cut,
-- ring modulation and panning for HVL) and the integer mixer, all as hvl2wav's replay.c 1.9 computes them so the output
-- matches hvl2wav sample for sample. Ported from HivelyTracker (BSD-3-Clause, (c) 2006-2018 Pete Gordon;
-- licenses/hivelytracker-LICENSE.txt). Where the C code reads outside its arrays on bad data this port clamps instead.
-- codecs/audio/Hively/Player: the AHX / HivelyTracker replayer and integer mixer, as hvl2wav's replay.c 1.9 computes them.
-- Ported from HivelyTracker (BSD-3-Clause, (c) 2006-2018 Pete Gordon; licenses/hivelytracker-LICENSE.txt). Where the C
-- code reads outside its arrays on bad data this port clamps instead.
local Tables = require("@rosetta/codecs/audio/Hively/Tables")
local Load = require("@rosetta/codecs/audio/Hively/Load")
@@ -102,7 +100,6 @@ local function newVoice(i: number): Voice
}
end
-- new(tune, freq, stereo, subsong) -> State | nil (subsong out of range). stereo: AHX separation 0..4 (HVL: the file's)
function Player.new(tune: Load.Tune, freq: number, stereo: number, subsong: number): State?
if subsong < 0 or subsong > #tune.subsongs then
return nil
@@ -130,7 +127,6 @@ function Player.new(tune: Load.Tune, freq: number, stereo: number, subsong: numb
return st
end
-- ------------------------------------------------------------------------------------------------ steps
local function setPan(st: State, v: Voice, p: number)
v.pan = p
v.panMultLeft, v.panMultRight = st.panL[p], st.panR[p]
@@ -370,7 +366,6 @@ local function processStep(st: State, v: Voice)
stepfx3(st, v, fxb, fxbp)
end
-- ------------------------------------------------------------------------------------------------ frames
local function plistCommand(st: State, v: Voice, fx: number, param: number)
if fx == 0 then
if param > 0 and param < 0x40 then
@@ -537,7 +532,6 @@ local function processFrame(st: State, v: Voice)
end
end
-- ADSR envelope
if v.aFrames ~= 0 then
v.adsrVolume += v.aVolume
v.aFrames -= 1
@@ -560,10 +554,8 @@ local function processFrame(st: State, v: Voice)
end
end
-- volume slide
v.noteMaxVolume = math.clamp(v.noteMaxVolume + v.volumeSlideUp - v.volumeSlideDown, 0, 0x40)
-- portamento
if v.periodSlideOn then
if v.periodSlideWithLimit then
local d0 = v.periodSlidePeriod - v.periodSlideLimit
@@ -588,7 +580,6 @@ local function processFrame(st: State, v: Voice)
end
end
-- vibrato
if v.vibratoDepth ~= 0 then
if v.vibratoDelay <= 0 then
v.vibratoPeriod = (VIB[v.vibratoCurrent] * v.vibratoDepth) // 128
@@ -599,7 +590,6 @@ local function processFrame(st: State, v: Voice)
end
end
-- PList
local pl = v.perfList
if pl then
if v.instrument and v.perfCurrent < v.instrument.plist.length then
@@ -636,7 +626,6 @@ local function processFrame(st: State, v: Voice)
end
end
-- perf portamento
if v.periodPerfSlideOn then
v.periodPerfSlidePeriod = s16(v.periodPerfSlidePeriod - v.periodPerfSlideSpeed)
if v.periodPerfSlidePeriod ~= 0 then
@@ -754,7 +743,6 @@ local function processFrame(st: State, v: Voice)
end
end
-- ring modulation period
if v.ringAudioOff >= 0 then
local rp = v.ringBasePeriod
if v.ringFixedPeriod == 0 then
@@ -772,7 +760,6 @@ local function processFrame(st: State, v: Voice)
v.ringAudioPeriod = math.clamp(rp, 0x71, 0xd60)
end
-- the period
local ap = v.instrPeriod
if v.fixedNote == 0 then
if v.overrideTranspose ~= 1000 then
@@ -794,7 +781,6 @@ local function processFrame(st: State, v: Voice)
v.audioVolume = s16(vol)
end
-- fill the 0x280-byte voice buffer with repeats of the 4 << waveLength cycle at (src, off)
local function fillCycles(dst: buffer, src: buffer, off: number, len: number)
if off < 0 or off + len > buffer.len(src) then
buffer.fill(dst, 0, 0, 0x281)
@@ -830,7 +816,7 @@ local function setAudio(st: State, v: Voice)
end
end
-- irq(st, audio): one player call (hvl_play_irq); audio = false skips the audio side (duration scans)
-- hvl_play_irq (audio = false skips the audio side)
function Player.irq(st: State, audio: boolean)
local tune = st.tune
local nch = st.channels
@@ -888,7 +874,7 @@ function Player.irq(st: State, audio: boolean)
end
end
-- mix(st, out, at, n): n stereo frames of f32 into out at frame `at` (hvl_mixchunk: integer mixing, s16 clipping, / 32768)
-- hvl_mixchunk: integer mixing, s16 clipping, / 32768
function Player.mix(st: State, out: buffer, at: number, n: number)
local nch = st.channels
local voices = st.voices
+3 -7
View File
@@ -1,15 +1,12 @@
--!strict
--!native
-- codecs/audio/Hively/Tables: the AHX / HivelyTracker replayer's tables: the waveform block (triangle and sawtooth at
-- six lengths, 32 square pulse widths, white noise, and the 31 low-pass and 31 high-pass filtered copies of all of
-- them, generated as hvl_replay.c does), the sine panning tables, the vibrato sine and the Amiga period table. Ported
-- from HivelyTracker's hvl2wav replay.c 1.9 (BSD-3-Clause, (c) 2006-2018 Pete Gordon; licenses/hivelytracker-LICENSE.txt)
-- as hvl2wav builds on 64-bit Linux (`long` is 64-bit there: the noise generator's shifts do not wrap at 32 bits).
-- codecs/audio/Hively/Tables: the AHX / HivelyTracker replayer's waveform, panning, vibrato and period tables.
-- Ported from hvl2wav replay.c 1.9 (BSD-3-Clause, (c) 2006-2018 Pete Gordon; licenses/hivelytracker-LICENSE.txt) as
-- built on 64-bit Linux (`long` is 64-bit: the noise generator's shifts do not wrap at 32 bits).
local Tables = {}
local band, bor, bxor, lshift, rshift = bit32.band, bit32.bor, bit32.bxor, bit32.lshift, bit32.rshift
-- the offsets inside `waves` (bytes)
local FILTER_SET = 0xfc + 0xfc + 0x80 * 0x1f + 0x80 + 3 * 0x280 -- 6520: one filtered copy of every waveform
local WHITENOISELEN = 0x280 * 3
local WO_LOWPASSES = 0
@@ -184,7 +181,6 @@ end
local cache: { waves: buffer, panL: { number }, panR: { number } }? = nil
-- get() -> { waves (int8 buffer), panL, panR (0-based, 256 entries) }: built once, shared read only
function Tables.get(): { waves: buffer, panL: { number }, panR: { number } }
if cache then
return cache
+9 -24
View File
@@ -1,17 +1,8 @@
--!strict
-- codecs/audio/Hively: the AHX / HivelyTracker engine behind music/ahx and music/hvl: the loader for both file formats
-- (Load), the replayer with its waveform generator (Tables: triangle / sawtooth / square / noise and the 31 + 31
-- filtered copies) and integer mixer (Player), and the render glue to a Types.Decoder (stereo f32). A port of
-- HivelyTracker's hvl2wav replay.c 1.9 (BSD-3-Clause, (c) 2006-2018 Pete Gordon; licenses/hivelytracker-LICENSE.txt):
-- at hvl2wav's settings the output equals hvl2wav's 16-bit samples / 32768, sample for sample.
--
-- local Hively = require("@rosetta/codecs/audio/Hively")
-- local tune, err = Hively.parse(buf, 0, buffer.len(buf)) -- the replayer's Tune
-- local song = Hively.module(tune) -- a Types.Module-shaped value with render()
-- local dec = Hively.decoder(tune, { sampleRate = 44100 }) -- a Types.Decoder
--
-- Not supported: hvl2wav's -a auto gain; a speed multiplier of 4 at 44.1 kHz renders 880 samples per 50 Hz frame
-- (hvl2wav pads each frame to 882 with stale samples).
-- codecs/audio/Hively: the AHX / HivelyTracker engine (loader, replayer, waveform tables, render glue).
-- A port of hvl2wav replay.c 1.9 (BSD-3-Clause, (c) 2006-2018 Pete Gordon; licenses/hivelytracker-LICENSE.txt); at
-- hvl2wav's settings the output equals its 16-bit samples / 32768. hvl2wav pads a 44.1 kHz speed-multiplier-4 frame to
-- 882 samples with stale data; this renders 880.
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Load = require("@rosetta/codecs/audio/Hively/Load")
@@ -22,14 +13,13 @@ local Hively = {}
export type Tune = Load.Tune
export type RenderOptions = {
sampleRate: number?, -- default 44100 (the replayer runs at any rate: 8000..192000)
subsong: number?, -- 0 = the main song (default), 1..n the subsongs
stereo: number?, -- AHX stereo separation 0..4 (0 mono, 4 = 100 % Amiga L R R L); default 0 as hvl2wav. HVL: the file's
loop: boolean?, -- keep playing past the song end (frames / duration still give one pass)
sampleRate: number?,
subsong: number?,
stereo: number?, -- AHX stereo separation 0..4 (4 = 100 % Amiga L R R L); default 0 as hvl2wav
loop: boolean?,
maxDuration: number?, -- seconds, default 600 (hvl2wav's 10 minute limit)
}
-- parse(b, base?, limit?) -> Tune | nil, Err
function Hively.parse(b: buffer, base: number?, limit: number?): (Tune?, Err.Err?)
local tune, msg, kind = Load.parse(b, base or 0, limit or buffer.len(b))
if not tune then
@@ -51,7 +41,6 @@ local function settings(tune: Tune, opts: RenderOptions?): (number?, number, num
return rate, sub, tonumber(o.stereo) or 0, nil
end
-- frameCount(tune, opts?) -> 50 Hz frames until the song end (as hvl2wav stops), capped by maxDuration
function Hively.frameCount(tune: Tune, opts: RenderOptions?): number
local rate, sub, stereo = settings(tune, opts)
local st = rate and Player.new(tune, rate :: number, stereo, sub)
@@ -69,7 +58,6 @@ function Hively.frameCount(tune: Tune, opts: RenderOptions?): number
return n
end
-- decoder(tune, opts?) -> Types.Decoder | nil, Err
function Hively.decoder(tune: Tune, opts: RenderOptions?): (Types.Decoder?, Err.Err?)
local rate, sub, stereo, err = settings(tune, opts)
if not rate then
@@ -84,7 +72,7 @@ function Hively.decoder(tune: Tune, opts: RenderOptions?): (Types.Decoder?, Err.
local loop = opts ~= nil and opts.loop == true
local st = Player.new(tune, sr, stereo, sub) :: Player.State
local fbuf = buffer.create(perFrame * 8)
local fpos, ffill = 0, 0 -- read position / filled frames inside fbuf
local fpos, ffill = 0, 0
local pos = 0
local function nextFrame()
@@ -147,8 +135,6 @@ function Hively.decoder(tune: Tune, opts: RenderOptions?): (Types.Decoder?, Err.
return (dec :: any) :: Types.Decoder, nil
end
-- module(tune) -> a Types.Module-shaped value (kind "music"): tracks as patterns (rows * 6 bytes: note, instrument,
-- fx, param, fxb, paramb), positions, instruments with their play lists, and render(opts) -> Decoder
function Hively.module(tune: Tune): Types.Module
local patterns = {}
for t = 0, tune.trackNr do
@@ -198,7 +184,6 @@ function Hively.module(tune: Tune): Types.Module
if dec then
return dec
end
-- the Types contract returns a Decoder: a failed render is an empty one carrying the Err
local empty: any = Pcm.audioDecoder(Types.Audio.new(44100, 2, 0))
empty.err = derr
return empty