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

+20 -7
View File
@@ -149,6 +149,7 @@ def dsp_frame(xs, h1, h2, coefs):
a, b = h1, h2
err = 0
nibs = []
outs = []
for x in xs:
base = 1024 + c1 * a + c2 * b
want = ((x << 11) - base) / (scale << 11)
@@ -156,36 +157,48 @@ def dsp_frame(xs, h1, h2, coefs):
s = clamp16(((n * scale) << 11) + base >> 11)
err += (x - s) ** 2
nibs.append(n & 15)
outs.append(s)
a, b = s, a
if best is not None and err >= best[0]:
break
else:
if best is None or err < best[0]:
best = (err, p << 4 | sc, nibs, a, b)
best = (err, p << 4 | sc, nibs, outs)
return best
def dsp_encode(samples, coefs=DSP_COEFS):
def dsp_encode(samples, coefs=DSP_COEFS, decoded=None):
"""DSP frames; decoded (a list) receives the decoded samples (the history a seek table stores)"""
out = bytearray()
h1 = h2 = 0
for k in range(0, len(samples), 14):
xs = samples[k:k + 14]
n = len(xs)
xs = xs + [0] * (14 - n)
_, head, nibs, a, b = dsp_frame(xs, h1, h2, coefs)
_, head, nibs, outs = dsp_frame(xs, h1, h2, coefs)
out.append(head)
out += bytes(nibs[i] << 4 | nibs[i + 1] for i in range(0, 14, 2))
h1, h2 = a, b
h1, h2 = outs[-1], outs[-2]
if decoded is not None:
decoded += outs[:n]
return bytes(out)
def hist(dec, b, spb):
"""(yn1, yn2) before block b: the last two decoded samples of the previous block"""
if b == 0 or not dec:
return (0, 0)
return (dec[b * spb - 1], dec[b * spb - 2])
def rstm(samples, rate, ch, kind="RSTM", loop=None, block=0x2000, le=None, codec="dsp", coefs=DSP_COEFS):
"""a BRSTM (kind RSTM, big-endian), BCSTM (CSTM, little-endian) or BFSTM (FSTM, big-endian unless le) file"""
ints = s16(samples)
frames = len(ints) // ch
chans = [ints[c::ch] for c in range(ch)]
decs = [[] for _ in range(ch)]
if codec == "dsp":
enc = [dsp_encode(x, coefs) for x in chans]
enc = [dsp_encode(x, coefs, decs[c]) for c, x in enumerate(chans)]
spb = block // 8 * 14
elif codec == "pcm16":
big = kind == "RSTM" or (kind == "FSTM" and not le)
@@ -235,7 +248,7 @@ def rstm(samples, rate, ch, kind="RSTM", loop=None, block=0x2000, le=None, codec
head = b"HEAD" + struct.pack(">I", 0) + body
head = head.ljust((len(head) + 31) // 32 * 32, b"\0")
head = head[:4] + struct.pack(">I", len(head)) + head[8:]
adpc_body = b"".join(struct.pack(">hh", 0, 0) * ch for _ in range(nblocks))
adpc_body = b"".join(b"".join(struct.pack(">hh", *hist(decs[c], b, spb)) for c in range(ch)) for b in range(nblocks))
adpc = b"ADPC" + struct.pack(">I", 0) + adpc_body
adpc = adpc.ljust((len(adpc) + 31) // 32 * 32, b"\0")
adpc = adpc[:4] + struct.pack(">I", len(adpc)) + adpc[8:]
@@ -279,7 +292,7 @@ def rstm(samples, rate, ch, kind="RSTM", loop=None, block=0x2000, le=None, codec
info = b"INFO" + struct.pack(E + "I", 0) + body
info = info.ljust((len(info) + 31) // 32 * 32, b"\0")
info = info[:4] + struct.pack(E + "I", len(info)) + info[8:]
seek = b"SEEK" + struct.pack(E + "I", 0) + b"".join(struct.pack(E + "hh", 0, 0) * ch for _ in range(nblocks))
seek = b"SEEK" + struct.pack(E + "I", 0) + b"".join(b"".join(struct.pack(E + "hh", *hist(decs[c], b, spb)) for c in range(ch)) for b in range(nblocks))
seek = seek.ljust((len(seek) + 31) // 32 * 32, b"\0")
seek = seek[:4] + struct.pack(E + "I", len(seek)) + seek[8:]
datac = b"DATA" + struct.pack(E + "I", len(data) + 0x20) + b"\0" * 0x18 + bytes(data)
+47
View File
@@ -0,0 +1,47 @@
# Nintendo stream fixtures (tools/fixtures.py): audio/brstm. Our Python writer and DSP encoder (tools/fixtures/_adpcm.py)
# build BRSTM / BCSTM / BFSTM files (DSP ADPCM with a filled ADPC / SEEK history table, PCM16, PCM8; several blocks
# with a short padded last block; loops); vgmstream decodes them and reports the loops.
# tests/gen/brstm_cases.luau { name, data, rate, ch, ref (s16le), loopStart?, loopEnd? }
import importlib.util, os, re
_dir = os.path.dirname(os.path.abspath(__file__))
def _load(name):
spec = importlib.util.spec_from_file_location(name, os.path.join(_dir, name + ".py"))
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
return m
A = _load("_audio")
D = _load("_adpcm")
INPUTS = ["tools/fixtures/_adpcm.py", "src/Formats/codecs/audio/Adpcm", "src/Formats/audio/brstm"]
def generate(ctx):
stereo = A.signal("music", 32000, 2, 9000, seed=31)
mono = A.signal("sweep", 22050, 1, 8000, seed=32)
made = [
("rstm-dsp-stereo", "RSTM", stereo, 32000, 2, dict(block=0x800)),
("rstm-dsp-mono-loop", "RSTM", mono, 22050, 1, dict(block=0x400, loop=(1000, 8000))),
("rstm-pcm16-stereo", "RSTM", stereo, 32000, 2, dict(block=0x2000, codec="pcm16")),
("cstm-dsp-stereo-loop", "CSTM", stereo, 32000, 2, dict(block=0x800, loop=(4000, 9000))),
("cstm-pcm8-mono", "CSTM", mono, 22050, 1, dict(block=0x1000, codec="pcm8")),
("fstm-be-dsp-stereo", "FSTM", stereo, 32000, 2, dict(block=0x1000)),
("fstm-le-dsp-mono", "FSTM", mono, 22050, 1, dict(block=0x800, le=True)),
("fstm-le-pcm16-stereo", "FSTM", stereo, 32000, 2, dict(block=0x1000, le=True, codec="pcm16")),
]
cases = []
for name, kind, sig, rate, ch, kw in made:
data = D.rstm(sig, rate, ch, kind, **kw)
ext = {"RSTM": "brstm", "CSTM": "bcstm", "FSTM": "bfstm"}[kind]
r, c, ref = D.vgdecode(ctx, data, ext)
assert (r, c) == (rate, ch) and len(ref) == len(sig) * 2, (name, r, c, len(ref))
e = dict(name=name, data=data, rate=rate, ch=ch, ref=ref)
m = re.search(r"loop start: (\d+) samples.*?loop end: (\d+) samples", D.vginfo(ctx, data, ext), re.S)
if m:
e["loopStart"], e["loopEnd"] = int(m.group(1)), int(m.group(2))
cases.append(e)
ctx.emit("brstm_cases", cases)