249 lines
7.9 KiB
Luau
249 lines
7.9 KiB
Luau
--!strict
|
|
--!optimize 2
|
|
-- 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
|
|
|
|
export type PlistEntry = { note: number, waveform: number, fixed: number, fx: { number }, param: { number } }
|
|
export type Instrument = {
|
|
name: string,
|
|
volume: number,
|
|
waveLength: number,
|
|
filterLower: number,
|
|
filterUpper: number,
|
|
filterSpeed: number,
|
|
squareLower: number,
|
|
squareUpper: number,
|
|
squareSpeed: number,
|
|
vibratoDelay: number,
|
|
vibratoSpeed: number,
|
|
vibratoDepth: number,
|
|
hardCutRelease: number,
|
|
hardCutReleaseFrames: number,
|
|
envelope: { aFrames: number, aVolume: number, dFrames: number, dVolume: number, sFrames: number, rFrames: number, rVolume: number },
|
|
plist: { speed: number, length: number, entries: { PlistEntry } },
|
|
}
|
|
export type Tune = {
|
|
format: string,
|
|
version: number,
|
|
title: string,
|
|
channels: number,
|
|
positionNr: number,
|
|
restart: number,
|
|
speedMultiplier: number,
|
|
trackLength: number,
|
|
trackNr: number,
|
|
instrumentNr: number,
|
|
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 },
|
|
mixgain: number, -- (percent * 256) / 100, HVL only (AHX: set by the stereo separation at render time)
|
|
stereo: number,
|
|
emptyTrack0: boolean,
|
|
}
|
|
|
|
local function cstring(b: buffer, p: number, limit: number, max: number): (string, number)
|
|
local e = p
|
|
while e < limit and e - p < max and readu8(b, e) ~= 0 do
|
|
e += 1
|
|
end
|
|
return buffer.readstring(b, p, e - p), e
|
|
end
|
|
|
|
function Load.parse(b: buffer, base: number, limit: number): (Tune?, string?, string?)
|
|
local size = limit - base
|
|
if size < 14 then
|
|
return nil, "truncated header", "truncated"
|
|
end
|
|
local function u8(o: number): number
|
|
return readu8(b, base + o)
|
|
end
|
|
local magic = buffer.readstring(b, base, 3)
|
|
local ahx = magic == "THX" and u8(3) < 3
|
|
if not ahx and not (magic == "HVL" and u8(3) <= 1) then
|
|
return nil, "not an AHX / HVL module", nil
|
|
end
|
|
if not ahx and size < 16 then
|
|
return nil, "truncated header", "truncated"
|
|
end
|
|
local posn = bit32.band(u8(6), 0x0f) * 256 + u8(7)
|
|
local insn, ssn, trkl, trkn = u8(12), u8(13), u8(10), u8(11)
|
|
local chnn = if ahx then 4 else bit32.rshift(u8(8), 2) + 4
|
|
local tune: Tune = {
|
|
format = if ahx then "ahx" else "hvl",
|
|
version = u8(3),
|
|
title = "",
|
|
channels = chnn,
|
|
positionNr = posn,
|
|
restart = if ahx then u8(8) * 256 + u8(9) else bit32.band(u8(8), 3) * 256 + u8(9),
|
|
speedMultiplier = bit32.band(bit32.rshift(u8(6), 5), 3) + 1,
|
|
trackLength = trkl,
|
|
trackNr = trkn,
|
|
instrumentNr = insn,
|
|
subsongs = {},
|
|
positions = buffer.create(math.max(posn, 1) * 32),
|
|
tracks = buffer.create(257 * 64 * 6),
|
|
instruments = {},
|
|
mixgain = if ahx then 256 else (u8(14) * 256) // 100,
|
|
stereo = if ahx then 0 else math.min(u8(15), 4),
|
|
emptyTrack0 = bit32.band(u8(6), 0x80) ~= 0,
|
|
}
|
|
if posn == 0 or posn > 1000 or trkl > 64 or trkl == 0 or insn > 64 then
|
|
return nil, string.format("bad header (positions %d, track length %d, instruments %d)", posn, trkl, insn), nil
|
|
end
|
|
if chnn > 16 then
|
|
return nil, "unsupported: more than 16 channels", "unsupported"
|
|
end
|
|
if tune.restart >= posn then
|
|
tune.restart = posn - 1
|
|
end
|
|
local nameAt = u8(4) * 256 + u8(5)
|
|
local nptr = limit
|
|
if nameAt < size then
|
|
local title, e = cstring(b, base + nameAt, limit, 128)
|
|
tune.title = title
|
|
nptr = e + 1
|
|
end
|
|
|
|
local p = base + (if ahx then 14 else 16)
|
|
local function need(n: number): boolean
|
|
return p + n <= limit
|
|
end
|
|
if not need(ssn * 2 + posn * chnn * 2) then
|
|
return nil, "truncated position list", "truncated"
|
|
end
|
|
for i = 1, ssn do
|
|
local s = readu8(b, p) * 256 + readu8(b, p + 1)
|
|
if s >= posn then
|
|
s = 0
|
|
end
|
|
tune.subsongs[i] = s
|
|
p += 2
|
|
end
|
|
for i = 0, posn - 1 do
|
|
for j = 0, chnn - 1 do
|
|
buffer.writeu8(tune.positions, (i * 16 + j) * 2, readu8(b, p))
|
|
buffer.writei8(tune.positions, (i * 16 + j) * 2 + 1, readi8(b, p + 1))
|
|
p += 2
|
|
end
|
|
end
|
|
local tracks = tune.tracks
|
|
for i = 0, trkn do
|
|
if not (tune.emptyTrack0 and i == 0) then
|
|
for j = 0, trkl - 1 do
|
|
local o = (i * 64 + j) * 6
|
|
if ahx then
|
|
if not need(3) then
|
|
return nil, "truncated track data", "truncated"
|
|
end
|
|
local b0, b1 = readu8(b, p), readu8(b, p + 1)
|
|
buffer.writeu8(tracks, o, bit32.band(bit32.rshift(b0, 2), 0x3f))
|
|
buffer.writeu8(tracks, o + 1, bit32.band(b0, 3) * 16 + bit32.rshift(b1, 4))
|
|
buffer.writeu8(tracks, o + 2, bit32.band(b1, 0xf))
|
|
buffer.writeu8(tracks, o + 3, readu8(b, p + 2))
|
|
p += 3
|
|
else
|
|
if not need(1) then
|
|
return nil, "truncated track data", "truncated"
|
|
end
|
|
if readu8(b, p) == 0x3f then
|
|
p += 1
|
|
else
|
|
if not need(5) then
|
|
return nil, "truncated track data", "truncated"
|
|
end
|
|
local fx = readu8(b, p + 2)
|
|
buffer.writeu8(tracks, o, readu8(b, p))
|
|
buffer.writeu8(tracks, o + 1, readu8(b, p + 1))
|
|
buffer.writeu8(tracks, o + 2, bit32.rshift(fx, 4))
|
|
buffer.writeu8(tracks, o + 3, readu8(b, p + 3))
|
|
buffer.writeu8(tracks, o + 4, bit32.band(fx, 0xf))
|
|
buffer.writeu8(tracks, o + 5, readu8(b, p + 4))
|
|
p += 5
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
local plSize = if ahx then 4 else 5
|
|
for i = 1, insn do
|
|
if not need(22) then
|
|
return nil, "truncated instrument", "truncated"
|
|
end
|
|
local function at(k: number): number
|
|
return readu8(b, p + k)
|
|
end
|
|
local name = ""
|
|
if nptr < limit then
|
|
local e
|
|
name, e = cstring(b, nptr, limit, 128)
|
|
nptr = e + 1
|
|
end
|
|
local plen = at(21)
|
|
local ins: Instrument = {
|
|
name = name,
|
|
volume = at(0),
|
|
filterSpeed = bit32.bor(bit32.band(bit32.rshift(at(1), 3), 0x1f), bit32.band(bit32.rshift(at(12), 2), 0x20)),
|
|
waveLength = bit32.band(at(1), 7),
|
|
envelope = { aFrames = at(2), aVolume = at(3), dFrames = at(4), dVolume = at(5), sFrames = at(6), rFrames = at(7), rVolume = at(8) },
|
|
filterLower = bit32.band(at(12), 0x7f),
|
|
vibratoDelay = at(13),
|
|
hardCutReleaseFrames = bit32.band(bit32.rshift(at(14), 4), 7),
|
|
hardCutRelease = if bit32.band(at(14), 0x80) ~= 0 then 1 else 0,
|
|
vibratoDepth = bit32.band(at(14), 0x0f),
|
|
vibratoSpeed = at(15),
|
|
squareLower = at(16),
|
|
squareUpper = at(17),
|
|
squareSpeed = at(18),
|
|
filterUpper = bit32.band(at(19), 0x3f),
|
|
plist = { speed = at(20), length = plen, entries = {} },
|
|
}
|
|
p += 22
|
|
if not need(plen * plSize) then
|
|
return nil, "truncated instrument play list", "truncated"
|
|
end
|
|
for j = 1, plen do
|
|
local e: PlistEntry
|
|
local b0, b1, b2, b3 = readu8(b, p), readu8(b, p + 1), readu8(b, p + 2), readu8(b, p + 3)
|
|
if ahx then
|
|
local k = bit32.band(bit32.rshift(b0, 5), 7)
|
|
k = if k == 6 then 12 elseif k == 7 then 15 else k
|
|
local l = bit32.band(bit32.rshift(b0, 2), 7)
|
|
l = if l == 6 then 12 elseif l == 7 then 15 else l
|
|
e = {
|
|
fx = { l, k },
|
|
waveform = bit32.band(b0 * 2, 6) + bit32.rshift(b1, 7),
|
|
fixed = bit32.band(bit32.rshift(b1, 6), 1),
|
|
note = bit32.band(b1, 0x3f),
|
|
param = { b2, b3 },
|
|
}
|
|
-- 1.6: version 0 modules (before filters) drop the "toggle filter" half of command 4
|
|
if tune.version == 0 and l == 4 and bit32.band(b2, 0xf0) ~= 0 then
|
|
e.param[1] = bit32.band(b2, 0x0f)
|
|
end
|
|
if tune.version == 0 and k == 4 and bit32.band(b3, 0xf0) ~= 0 then
|
|
e.param[2] = bit32.band(b3, 0x0f)
|
|
end
|
|
else
|
|
e = {
|
|
fx = { bit32.band(b0, 0xf), bit32.band(bit32.rshift(b1, 3), 0xf) },
|
|
waveform = bit32.band(b1, 7),
|
|
fixed = bit32.band(bit32.rshift(b2, 6), 1),
|
|
note = bit32.band(b2, 0x3f),
|
|
param = { b3, readu8(b, p + 4) },
|
|
}
|
|
end
|
|
ins.plist.entries[j] = e
|
|
p += plSize
|
|
end
|
|
tune.instruments[i] = ins
|
|
end
|
|
return tune, nil, nil
|
|
end
|
|
|
|
return Load
|