--!strict -- audio/adx: CRI ADX (.adx), the game-audio ADPCM of Sega / CRI Middleware, over codecs/audio/Adpcm. New in -- formats-luau; the header layout and the decoder details follow vgmstream r2117 (ISC, licenses/vgmstream-LICENSE.txt), -- whose output it matches bit for bit (the scale + 1 of CRI's decoders, the version 3 rounding; ffmpeg's ADX decoder -- differs slightly on both). Supports: encoding type 3 (the standard predictor from the header's high-pass cutoff), -- versions 3, 4 and 5, 1-8 channels, the initial history of version 4, loop points (dec.loop and -- meta.source.loop = { start, stop } in frames, stop exclusive). Encode (Audio or Decoder): version 4, 1-8 channels, -- loop points from opts.loop. Not supported: encrypted ADX (versions 0x0408 / 0x0409, keys needed), the fixed (2) and -- exponential (4) encoding types, AHX. -- -- local dec = Adx.open(Bytes.reader(buf)) -- Types.Decoder (+ loop, version, cutoff) -- local file = Adx.encode(audio, { loop = { start = 44100, stop = 441000 } }) -- ------------------------------------------------------------------------------------------------ requires local Err = require("@formats/core/Err") local Bytes = require("@formats/core/Bytes") local Types = require("@formats/Types") local Pcm = require("@formats/codecs/audio/Pcm") local Adpcm = require("@formats/codecs/audio/Adpcm") local Decoder = require("@formats/codecs/audio/Adpcm/Decoder") -- ------------------------------------------------------------------------------------------------ contract local Adx = { name = "adx", category = "audio", extensions = { "adx" }, mime = { "audio/x-adx" }, deps = { "core/Err", "core/Bytes", "Types", "codecs/audio/Pcm", "codecs/audio/Adpcm", "codecs/audio/Adpcm/Decoder" }, caps = { decode = true, encode = true, streaming = true, lossy = true, loops = true, channels = 8 }, } -- ------------------------------------------------------------------------------------------------ types export type Loop = { start: number, stop: number } export type DecodeOptions = { maxFrames: number?, -- decode(): Err.LIMIT past this (default 2^28) } export type EncodeOptions = { loop: Loop?, -- loop points in frames (stop exclusive); default: the source's meta.source.loop / loop, else none cutoff: number?, -- the predictor's high-pass cutoff in Hz (default 500, as CRI's encoders) } -- ------------------------------------------------------------------------------------------------ constants local FRAME = 18 -- ------------------------------------------------------------------------------------------------ sniff function Adx.sniff(r: Bytes.Reader): number if r:peekU8(0) ~= 0x80 or r:peekU8(1) ~= 0 then return 0 end local off = r:peekU8(2) * 256 + r:peekU8(3) return if r:peek(6, off - 2) == "(c)CRI" then 1 else 0 end -- ------------------------------------------------------------------------------------------------ decode function Adx.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Audio?, Err.Err?) local dec, err = Adx.open(r) if not dec then return nil, err end local d = dec :: any local limit = opts and opts.maxFrames or 2 ^ 28 if d.frames > limit or d.frames * d.channels > 2 ^ 28 then return nil, Err.new(Err.LIMIT, string.format("%d frames is over the limit", d.frames)) end return Pcm.drain(d, { format = "adx", version = d.version, cutoff = d.cutoff, loop = d.loop }, limit) end -- ------------------------------------------------------------------------------------------------ encode function Adx.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?) local o: EncodeOptions = opts or {} local src, serr = Pcm.source(value) if not src then return nil, serr end local ch, rate = src.channels, src.sampleRate if ch > 8 then return nil, Err.new(Err.ARG, "ADX holds at most 8 channels") end if rate ~= math.floor(rate) or rate < 1 or rate >= 2 ^ 31 then return nil, Err.new(Err.ARG, "the sample rate must be a positive integer") end local loop = o.loop if loop == nil then local v = value :: any local m = type(v.meta) == "table" and type(v.meta.source) == "table" and v.meta.source.loop loop = if type(m) == "table" then m else (if type(v.loop) == "table" then v.loop else nil) end local a, cerr = Pcm.collect(src) if not a then return nil, cerr end local n = a.frames if loop and (type(loop.start) ~= "number" or type(loop.stop) ~= "number" or loop.start < 0 or loop.stop <= loop.start or loop.stop > n) then return nil, Err.new(Err.ARG, "loop must be { start, stop } inside the audio") end local cutoff = o.cutoff or 500 if type(cutoff) ~= "number" or cutoff < 1 or cutoff >= rate / 2 or cutoff > 65535 then return nil, Err.new(Err.ARG, "bad cutoff") end cutoff = math.floor(cutoff) local c1, c2 = Adpcm.adxCoefs(cutoff, rate) local hist = if ch > 1 then 4 * ch else 8 local start = 0x18 + hist + (if loop then 0x18 else 0) + 6 start = (start + 3) // 4 * 4 local nframes = (n + 31) // 32 local w = Bytes.writer(start + (nframes + 1) * FRAME * ch) w:u16be(0x8000) w:u16be(start - 4) w:u8(3) w:u8(FRAME) w:u8(4) w:u8(ch) w:u32be(rate) w:u32be(n) w:u16be(cutoff) w:u16be(0x0400) w:u32be(0) w:fill(0, hist) if loop then local bytesAt = function(f: number): number return start + (f // 32) * FRAME * ch end w:u16be(0) w:u16be(1) w:u32be(1) w:u32be(loop.start) w:u32be(bytesAt(loop.start)) w:u32be(loop.stop) w:u32be(bytesAt(loop.stop + 31)) end w:fill(0, start - 6 - w.pos) w:string("(c)CRI") local at = w:reserve(nframes * FRAME * ch) local st = buffer.create(8 * ch) for c = 0, ch - 1 do Adpcm.adxEncode(a.samples, c, ch, n, c1, c2, st, c * 8, w.buf, at + c * FRAME, FRAME * ch) end w.pos = at + nframes * FRAME * ch -- the end marker: scale 0x8001, then the padding size w:u16be(0x8001) w:u16be(FRAME - 4) w:fill(0, FRAME - 4) return w:finish(), nil end -- ------------------------------------------------------------------------------------------------ open function Adx.open(r: Bytes.Reader, _opts: any?): (Types.Decoder?, Err.Err?) if type(r) ~= "table" or type(r.buf) ~= "buffer" then return nil, Err.new(Err.ARG, "open: expected a Reader") end if Adx.sniff(r) == 0 then return nil, Err.new(Err.CORRUPT, "not an ADX file") end local h = r:sub(0) local copyright = (h:seek(2) and h:u16be()) or 0 local start = copyright + 4 local enc, fsize, bits, ch = h:u8(), h:u8(), h:u8(), h:u8() local rate, frames, cutoff, version = h:u32be(), h:u32be(), h:u16be(), h:u16be() if h.err then return nil, h.err end if version == 0x0408 or version == 0x0409 then return nil, Err.new(Err.UNSUPPORTED, "encrypted ADX (keys needed)") end if enc ~= 3 then return nil, Err.new(Err.UNSUPPORTED, "encoding type " .. enc .. (if enc == 0x10 or enc == 0x11 then " (AHX)" else "")) end if fsize ~= FRAME or bits ~= 4 then return nil, Err.new(Err.UNSUPPORTED, string.format("frame size %d, %d bits", fsize, bits)) end if ch < 1 or ch > 8 then return nil, Err.new(Err.CORRUPT, "bad channel count " .. ch) end if rate < 1 or rate >= 2 ^ 31 then return nil, Err.new(Err.CORRUPT, "bad sample rate " .. rate) end if version ~= 0x0300 and version ~= 0x0400 and version ~= 0x0500 then return nil, Err.new(Err.UNSUPPORTED, string.format("version 0x%04X", version)) end local size = r.limit - r.base - start if size < 0 then return nil, Err.new(Err.TRUNCATED, "the header runs past the end") end -- loop points and the initial history (vgmstream's reading of the extra header fields) local hist: { number }? = nil local loopsAt: number? = nil if version == 0x0300 then if start - 6 >= 0x14 + 0x18 then loopsAt = 0x14 end elseif version == 0x0400 then local hsize = if ch > 1 then 4 * ch else 8 hist = {} for c = 0, ch - 1 do local hh = r:sub(0x18 + c * 4, 4) local h1, h2 = hh:i16be(), hh:i16be() hist[c * 2 + 1], hist[c * 2 + 2] = if hh.err then 0 else h1, if hh.err then 0 else h2 end local ainf = 0 local ai = r:sub(0x18 + hsize + 4, 8) if ai:string(4) == "AINF" then ainf = ai:u32be() end if start - ainf - 6 >= 0x18 + hsize + 0x18 then loopsAt = 0x18 + hsize end end local loopTable: { start: number, stop: number }? = nil if loopsAt then local l = r:sub(loopsAt + 4, 0x14) local flag, ls, _, le = l:u32be(), l:u32be(), l:u32be(), l:u32be() if not l.err and flag ~= 0 and le > ls then loopTable = { start = ls, stop = le } end end local c1, c2 = Adpcm.adxCoefs(cutoff, rate) local dec: any = { sampleRate = rate, channels = ch, tags = {}, version = version, cutoff = cutoff, loop = loopTable } local d, err = Decoder.interleaved(dec, { kind = "adx", buf = r.buf, offset = r.base + start, size = size, interleave = FRAME, frames = frames, c1 = c1, c2 = c2, v3 = version == 0x0300, hist = hist, }) if not d then return nil, err end d.bitrate = rate * FRAME * ch * 8 // 32 return d, nil end return Adx