Files
rosetta/src/Formats/music/it/Itsex.luau
T

121 lines
3.4 KiB
Luau

--!strict
--!native
-- music/it/Itsex: Impulse Tracker sample decompression (IT 2.14 and the IT 2.15 double-delta variant, 8 and 16-bit).
-- Written from the algorithm description in ITTECH.TXT / the public notes on the format: blocks of 0x8000 bytes of
-- output, each a u16 length then an LSB-first bit stream of deltas with three width-change methods.
--
-- Never throws: every read is bounds-checked; corrupt streams return nil, message.
local readu8, readu16, writef32 = buffer.readu8, buffer.readu16, buffer.writef32
local Itsex = {}
-- decompress(buf, off, limit, frames, bits, it215) -> f32 buffer (frames), bytes consumed | nil, message
function Itsex.decompress(buf: buffer, off: number, limit: number, frames: number, bits: number, it215: boolean): (buffer?, number | string)
local out = buffer.create(frames * 4)
local blockLen = if bits == 16 then 0x4000 else 0x8000
local topWidth = if bits == 16 then 17 else 9
local fetchA = if bits == 16 then 4 else 3
local lowerB = if bits == 16 then -8 else -4
local upperB = if bits == 16 then 7 else 3
local scale = if bits == 16 then 1 / 32768 else 1 / 128
local wrap = if bits == 16 then 65536 else 256
local half = wrap // 2
local p = off
local done = 0
while done < frames do
if p + 2 > limit then
return nil, "compressed sample truncated"
end
local clen = readu16(buf, p)
p += 2
local bstart = p
local bend = p + clen
if bend > limit then
return nil, "compressed block runs past the end"
end
p = bend
-- bit reader over [bstart, bend)
local bytePos = bstart
local bitBuf = 0
local bitCount = 0
local function readBits(n: number): number
while bitCount < n do
if bytePos >= bend then
return -1
end
bitBuf += readu8(buf, bytePos) * 2 ^ bitCount
bytePos += 1
bitCount += 8
end
local v = bitBuf % 2 ^ n
bitBuf = (bitBuf - v) / 2 ^ n
bitCount -= n
return v
end
local count = math.min(blockLen, frames - done)
local width = topWidth
local d1, d2 = 0, 0
local i = 0
while i < count do
if width < 1 or width > topWidth then
return nil, "bad bit width"
end
local v = readBits(width)
if v < 0 then
return nil, "compressed block ends early"
end
if width < 7 then
-- method A: a marker value, then the new width in fetchA bits
if v == 2 ^ (width - 1) then
local nw = readBits(fetchA)
if nw < 0 then
return nil, "compressed block ends early"
end
nw += 1
width = if nw < width then nw else nw + 1
continue
end
elseif width < topWidth then
-- method B: values just below the top of the range encode the new width
local border = bit32.rshift(if bits == 16 then 0xFFFF else 0xFF, topWidth - width) + lowerB
if v > border and v <= border + (upperB - lowerB + 1) then
local nw = v - border
width = if nw < width then nw else nw + 1
continue
end
else
-- method C: the top bit flags a width change
if v >= wrap then
width = (v + 1) % 256
continue
end
end
-- a delta, sign-extended from its width
local x = v
if width < topWidth then
local shift = 2 ^ width
if x >= shift / 2 then
x -= shift
end
else
x = v % wrap
if x >= half then
x -= wrap
end
end
d1 = (d1 + x + half) % wrap - half
local s = d1
if it215 then
d2 = (d2 + d1 + half) % wrap - half
s = d2
end
writef32(out, (done + i) * 4, s * scale)
i += 1
end
done += count
end
return out, p - off
end
return Itsex