ws-audio2: audio/brstm (BRSTM / BCSTM / BFSTM read: DSP ADPCM, PCM16, PCM8, loops)

Bit-exact with vgmstream; forward seeks start from the ADPC / SEEK history. codecs/audio/Adpcm/Decoder.interleaved
also lays out block-interleaved PCM (s16be / s16le / s8); snapshots take precedence over a format's history table.
This commit is contained in:
4DBug committed 2026-10-31 14:22:46 -05:00
1 parent fd9175883d
commit 5dfaaf7524
6 files changed
+454 -18

No files matched your search

+31 -11
View File
@@ -2,8 +2,8 @@
-- codecs/audio/Adpcm/Decoder: the streaming Decoder (Types.Decoder) over an ADPCM stream cut into packets, for the
-- formats whose state carries from packet to packet (ADX, VAG, BRSTM, raw IMA): codecs/audio/Stream.packets plus
-- exact seeking. The codec state (one buffer the packet decoder advances) is snapshotted at every packet start it
-- passes; a seek restores the snapshot, or the format's own state for that packet (BRSTM's ADPC history), or decodes
-- forward from the nearest earlier snapshot. New in formats-luau.
-- passes; a seek restores the snapshot, else the format's own state for that packet (BRSTM's ADPC history), else
-- decodes forward from the nearest earlier snapshot. New in formats-luau.
--
-- local dec, err = Decoder.packets(dec, { -- dec: the Decoder table (sampleRate, channels, frames, ...)
-- count = n, packetFrames = 4096, -- packets of packetFrames frames (the last may be shorter)
@@ -14,8 +14,8 @@
-- })
--
-- The layouts the formats share, each returning the Decoder (dec gets read / seek / position) | nil, Err:
-- Decoder.interleaved(dec, spec) psx / adx / dsp frames, channels interleaved every spec.interleave bytes (VAG,
-- ADX, BRSTM / BCSTM / BFSTM, raw PS-ADPCM)
-- Decoder.interleaved(dec, spec) psx / adx / dsp frames (or s16 / s8 PCM), channels interleaved every
-- spec.interleave bytes (VAG, ADX, BRSTM / BCSTM / BFSTM, raw PS-ADPCM)
-- Decoder.nibbles(dec, spec) an IMA nibble stream, channels interleaved by nibble (raw IMA, KVAG)
-- Decoder.blocks(dec, spec) self-contained blocks: "ms" / "ima_wav" (blockAlign bytes), "ima_qt" (34 bytes
-- per channel), raw or inside AIFF-C / CAF
@@ -55,10 +55,10 @@ function Decoder.packets(dec: any, spec: Spec): (any, Err.Err?)
snap(1)
local function decode(i: number): (number?, Err.Err?)
if i ~= nextPacket then
if restore and restore(i) then
-- the format set the state
elseif snaps[i] then
if snaps[i] then
buffer.copy(st, 0, snaps[i])
elseif restore and restore(i) then
-- the format set the state (BRSTM's seek table)
else
local j = i - 1
while j > 1 and not snaps[j] do
@@ -96,11 +96,29 @@ function Decoder.packets(dec: any, spec: Spec): (any, Err.Err?)
end
-- ------------------------------------------------------------------------------------------------ layouts
local FRAME: { [string]: number } = { psx = 16, adx = 18, dsp = 8 } -- bytes per frame
local SPF: { [string]: number } = { psx = 28, adx = 32, dsp = 14 } -- samples per frame
local FRAME: { [string]: number } = { psx = 16, adx = 18, dsp = 8, s16be = 2, s16le = 2, s8 = 1 } -- bytes per frame
local SPF: { [string]: number } = { psx = 28, adx = 32, dsp = 14, s16be = 1, s16le = 1, s8 = 1 } -- samples per frame
-- PCM samples of one channel into every ds-th float of dst from float d
local function pcm(kind: string, src: buffer, p: number, n: number, dst: buffer, d: number, ds: number)
if kind == "s8" then
for k = 0, n - 1 do
buffer.writef32(dst, (d + k * ds) * 4, buffer.readi8(src, p + k) / 128)
end
elseif kind == "s16le" then
for k = 0, n - 1 do
buffer.writef32(dst, (d + k * ds) * 4, buffer.readi16(src, p + k * 2) / 32768)
end
else
for k = 0, n - 1 do
local v = buffer.readu8(src, p + k * 2) * 256 + buffer.readu8(src, p + k * 2 + 1)
buffer.writef32(dst, (d + k * ds) * 4, (if v >= 32768 then v - 65536 else v) / 32768)
end
end
end
export type InterleavedSpec = {
kind: "psx" | "adx" | "dsp",
kind: "psx" | "adx" | "dsp" | "s16be" | "s16le" | "s8", -- ADPCM frames, or PCM samples laid out the same way
buf: buffer,
offset: number, -- the first row
size: number, -- data bytes (rows past it are cut; a truncated file decodes its whole frames)
@@ -177,8 +195,10 @@ function Decoder.interleaved(dec: any, spec: InterleavedSpec): (any, Err.Err?)
Adpcm.psx(buf, p, fsize, nf, st, c * 8, out, d, ch)
elseif kind == "adx" then
Adpcm.adx(buf, p, fsize, nf, c1, c2, v3, st, c * 8, out, d, ch)
else
elseif kind == "dsp" then
Adpcm.dsp(buf, p, n, coefs :: buffer, c * 64, st, c * 8, out, d, ch)
else
pcm(kind, buf, p, n, out, d, ch)
end
end
got += n