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)