upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNotRexed <therealnothingfn@gmail.com>2026-11-20 22:28:16 -0500
committerNotRexed <therealnothingfn@gmail.com>2026-11-20 22:28:16 -0500
commitbfba26fbbb4c283a6c19405210deff4e8fc64847 (patch)
tree32de92829c8e9a31a50d53c1dbd00fdecd4d1979
parenta16d58e58e9ee7e8b644d676012c98d91510fe4a (diff)
Audio: streamed AWC music channels to Ogg Opus
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
-rw-r--r--docs/design.md2
-rw-r--r--tools/awc_opus.py64
2 files changed, 59 insertions, 7 deletions
diff --git a/docs/design.md b/docs/design.md
index 4a9166486..50873cec6 100644
--- a/docs/design.md
+++ b/docs/design.md
@@ -77,7 +77,7 @@ add a source file, add it to `runtime\CMakeLists.txt` and to the file list in `b
77| Models | **Done: 155,598 models** (levels.rpf 149,811 with 1.9 billion triangles, story000 3,303, data 1,350, shared_minigame000 783, packs 314, core_restofworld 37; 0 failures). Exported with `tools\rpf9dump` (built on breadbbbb/rpf9-explorer's Core; see below). Resources are found at page offset +16 after 16 zero bytes, followed by a zlib system blob with a drawable/fragment header and then the following zlib blocks as graphics data. Positions are quantized: `bias + scale * ((q >> 8) & 0xFFFFFF)`. Output is OBJ (positions, UVs, indices) plus a manifest with real model names (e.g. `cvar_int_grg2b_toys_1`), bounds, per-mesh shader index and material texture references (parameter hash, name hash, 64-bit asset key). My earlier `0x1380`/`0x1601` descriptor notes stand, but the "data not located" blocker was wrong. | `<work>\assets\<archive>\models\` | 77| Models | **Done: 155,598 models** (levels.rpf 149,811 with 1.9 billion triangles, story000 3,303, data 1,350, shared_minigame000 783, packs 314, core_restofworld 37; 0 failures). Exported with `tools\rpf9dump` (built on breadbbbb/rpf9-explorer's Core; see below). Resources are found at page offset +16 after 16 zero bytes, followed by a zlib system blob with a drawable/fragment header and then the following zlib blocks as graphics data. Positions are quantized: `bias + scale * ((q >> 8) & 0xFFFFFF)`. Output is OBJ (positions, UVs, indices) plus a manifest with real model names (e.g. `cvar_int_grg2b_toys_1`), bounds, per-mesh shader index and material texture references (parameter hash, name hash, 64-bit asset key). My earlier `0x1380`/`0x1601` descriptor notes stand, but the "data not located" blocker was wrong. | `<work>\assets\<archive>\models\` |
78| Texture dictionaries | Same tool: TXD resources become DDS per texture. | `<work>\assets\<archive>\textures\` | 78| Texture dictionaries | Same tool: TXD resources become DDS per texture. | `<work>\assets\<archive>\textures\` |
79| World textures (TTPF) | **Done: 308,895 textures from all 57 pools, 0 failures, 48,220 entries skipped** (BCPack or unmapped). 46 pools are in `data\ttdb\**`, 11 in `pack\<name>\data\ttdb\**` (exported under `ttpf_pack\<pack>\`). `data\ttdb\**\*.ttpf` (57 pools, 58 GiB) are loose RDS5 texture pools, **not** inside RPFs. rpf9-explorer's `TtpfReader` reads the catalog and detiles them; checked visually (`ttdb_graphics`: scope reticles, clean lines). Names are generated (`texture_N_<assetkey>`); models reference textures by asset key. Some rows are skipped (BCPack or unmapped shards). | `<work>\assets\ttpf\<pool>\` + `manifest.json` | 79| World textures (TTPF) | **Done: 308,895 textures from all 57 pools, 0 failures, 48,220 entries skipped** (BCPack or unmapped). 46 pools are in `data\ttdb\**`, 11 in `pack\<name>\data\ttdb\**` (exported under `ttpf_pack\<pack>\`). `data\ttdb\**\*.ttpf` (57 pools, 58 GiB) are loose RDS5 texture pools, **not** inside RPFs. rpf9-explorer's `TtpfReader` reads the catalog and detiles them; checked visually (`ttdb_graphics`: scope reticles, clean lines). Names are generated (`texture_N_<assetkey>`); models reference textures by asset key. Some rows are skipped (BCPack or unmapped shards). | `<work>\assets\ttpf\<pool>\` + `manifest.json` |
80| Audio | `audio.rpf` holds raw (not zlib) **AWC** banks: Opus (codec 0x0B) and XMA2 streams. Exported as whole `.awc` files plus `audio_manifest.json` (stream ids, codec, channels, rate, duration). **Done: 7,162 banks, 27 GiB, 1,603,231 streams, about 1,324 hours** (codec 0x0B Opus 1,601,167; 5: 1,818; 2: 227; 0 PCM: 19). `tools/awc_opus.py` repackages every Opus stream **losslessly** into a standard Ogg Opus file (no re-encode; PCM streams become .wav). The AWC framing is 2048-byte blocks of [u16 length][packet], with a zero length padding the block (as rpf9-explorer's OpusPreview reads it). The final granule is trimmed to the declared sample count. Checked: 300 random streams decode with ffmpeg with no errors, lengths match the declared counts. Streamed music (tag 0x48, interleaved multi-channel blocks) isn't converted yet. Output: `<work>\assets\audio\ogg\<bank>\<stream id>.ogg`. | `<work>\assets\audio\awc\` | 80| Audio | `audio.rpf` holds raw (not zlib) **AWC** banks: Opus (codec 0x0B) and XMA2 streams. Exported as whole `.awc` files plus `audio_manifest.json` (stream ids, codec, channels, rate, duration). **Done: 7,162 banks, 27 GiB, 1,603,231 streams, about 1,324 hours** (codec 0x0B Opus 1,601,167; 5: 1,818; 2: 227; 0 PCM: 19). `tools/awc_opus.py` repackages every Opus stream **losslessly** into a standard Ogg Opus file (no re-encode; PCM streams become .wav). The AWC framing is 2048-byte blocks of [u16 length][packet], with a zero length padding the block (as rpf9-explorer's OpusPreview reads it). The final granule is trimmed to the declared sample count. Checked: 300 random streams decode with ffmpeg with no errors, lengths match the declared counts. Streamed music (tag 0x48: chunks with a per-channel record table, each channel's 2048-byte subblocks after the aligned header, and a repeated-prefix skip per chunk) becomes one mono Ogg Opus per channel (3,008 banks contain music). Checked: a 4-channel music bank gives four 42.0 s channels with real levels and no decoder errors. Output: `<work>\assets\audio\ogg\<bank>\<stream id>.ogg`. | `<work>\assets\audio\awc\` |
81 81
82### rpf9dump (bulk export) 82### rpf9dump (bulk export)
83 83
diff --git a/tools/awc_opus.py b/tools/awc_opus.py
index 1289a0bbb..36f7c4c38 100644
--- a/tools/awc_opus.py
+++ b/tools/awc_opus.py
@@ -8,6 +8,12 @@ AWC layout (little-endian "ADAT"), as read by rpf9-explorer's OpusPreview.cpp: a
82048-byte blocks of [u16 length][packet] ..., a zero length pads the rest of the block. Codec 0 = PCM16 (written as 82048-byte blocks of [u16 length][packet] ..., a zero length pads the rest of the block. Codec 0 = PCM16 (written as
9.wav). Streamed music (tag 0x48, multi-channel interleaved blocks) is not handled here yet. 9.wav). Streamed music (tag 0x48, multi-channel interleaved blocks) is not handled here yet.
10 10
11Streamed music (tag 0x48 descriptor: u32 blocks, u32 chunk size, u32 channels, then 16 bytes per channel: id,
12samples, rate at +10, codec at +12) is split into one mono Ogg Opus per channel: each chunk starts with a
1324-byte record per channel (subblock count at +4, repeated-prefix samples to skip at +8), the channel data are
14its run of 2048-byte subblocks after the 2048-aligned header, and the first `skip` samples of a chunk repeat the
15previous chunk's tail and are dropped (whole packets).
16
11Output: <out>/<bank>/<stream id>.ogg (or .wav), plus a summary line per bank. 17Output: <out>/<bank>/<stream id>.ogg (or .wav), plus a summary line per bank.
12""" 18"""
13import argparse 19import argparse
@@ -107,11 +113,11 @@ def parse_awc(d: bytes):
107 p += (n + 1) * 4 113 p += (n + 1) * 4
108 info = p 114 info = p
109 p += n * 4 115 p += n * 4
110 streams = [] 116 streams, music = [], []
111 for i in range(n): 117 for i in range(n):
112 v = struct.unpack_from("<I", d, info + 4 * i)[0] 118 v = struct.unpack_from("<I", d, info + 4 * i)[0]
113 sid, ntags = v & 0x1FFFFFFF, v >> 29 119 sid, ntags = v & 0x1FFFFFFF, v >> 29
114 data = fmt = None 120 data = fmt = mus = None
115 for _ in range(ntags): 121 for _ in range(ntags):
116 t = struct.unpack_from("<Q", d, p)[0] 122 t = struct.unpack_from("<Q", d, p)[0]
117 p += 8 123 p += 8
@@ -120,15 +126,53 @@ def parse_awc(d: bytes):
120 data = (at, size) 126 data = (at, size)
121 elif typ == 0x76: 127 elif typ == 0x76:
122 fmt = (at, size) 128 fmt = (at, size)
129 elif typ == 0x48:
130 mus = (at, size)
131 if mus and data:
132 music.append((mus, data))
123 if data and fmt and fmt[1] >= 32: 133 if data and fmt and fmt[1] >= 32:
124 samples, = struct.unpack_from("<I", d, fmt[0]) 134 samples, = struct.unpack_from("<I", d, fmt[0])
125 rate, = struct.unpack_from("<H", d, fmt[0] + 8) 135 rate, = struct.unpack_from("<H", d, fmt[0] + 8)
126 codec, channels = d[fmt[0] + 28], d[fmt[0] + 29] 136 codec, channels = d[fmt[0] + 28], d[fmt[0] + 29]
127 streams.append({"id": sid, "samples": samples, "rate": rate, "codec": codec, "channels": channels, 137 streams.append({"id": sid, "samples": samples, "rate": rate, "codec": codec, "channels": channels,
128 "data": data}) 138 "data": data})
139 parse_awc.music = music
129 return streams 140 return streams
130 141
131 142
143def music_channels(d: bytes, mus, data):
144 """[(channel id, samples, rate, codec, [packets])] for one streamed-music descriptor."""
145 (m_at, m_size), (d_at, d_size) = mus, data
146 blocks, chunk, channels = struct.unpack_from("<III", d, m_at)
147 if not (1 <= channels <= 32 and 1 <= blocks and chunk >= 2048 and m_size == 12 + 16 * channels):
148 return []
149 info = []
150 for c in range(channels):
151 base = m_at + 12 + 16 * c
152 cid = struct.unpack_from("<I", d, base)[0] & 0x1FFFFFFF
153 samples, = struct.unpack_from("<I", d, base + 4)
154 rate, = struct.unpack_from("<H", d, base + 10)
155 info.append([cid, samples, rate, d[base + 12], []])
156 for b in range(blocks):
157 at = d_at + b * chunk
158 recs = [struct.unpack_from("<III", d, at + 24 * c + 4)[:2] for c in range(channels)] # (count, skip)
159 total = sum(r[0] for r in recs)
160 header = (channels * 24 + total * 4 + 2047) & ~2047
161 before = 0
162 for c, (count, skip) in enumerate(recs):
163 start = at + header + before * 2048
164 before += count
165 if info[c][3] != 0x0B or count == 0:
166 continue
167 for pk in opus_packets(d, start, count * 2048):
168 n = opus_samples(pk)
169 if skip >= n and skip > 0:
170 skip -= n
171 continue
172 info[c][4].append(pk)
173 return [tuple(x) for x in info]
174
175
132def opus_packets(d: bytes, at: int, size: int): 176def opus_packets(d: bytes, at: int, size: int):
133 out = [] 177 out = []
134 for b in range(at, at + size, 2048): 178 for b in range(at, at + size, 2048):
@@ -148,8 +192,15 @@ def export_bank(path: str, out_dir: str):
148 name = os.path.splitext(os.path.basename(path))[0] 192 name = os.path.splitext(os.path.basename(path))[0]
149 od = os.path.join(out_dir, name) 193 od = os.path.join(out_dir, name)
150 os.makedirs(od, exist_ok=True) 194 os.makedirs(od, exist_ok=True)
151 counts = {"opus": 0, "pcm": 0, "other": 0} 195 counts = {"opus": 0, "pcm": 0, "other": 0, "music": 0}
152 for s in parse_awc(d): 196 streams = parse_awc(d)
197 for mus, data in parse_awc.music:
198 for cid, samples, rate, codec, pk in music_channels(d, mus, data):
199 if pk:
200 n48 = samples * 48000 // rate if rate else 0
201 write_ogg_opus(os.path.join(od, f"{cid:08X}.ogg"), pk, 1, cid, n48)
202 counts["music"] += 1
203 for s in streams:
153 at, size = s["data"] 204 at, size = s["data"]
154 if s["codec"] == 0x0B and size % 2048 == 0 and s["channels"] in (1, 2): 205 if s["codec"] == 0x0B and size % 2048 == 0 and s["channels"] in (1, 2):
155 pk = opus_packets(d, at, size) 206 pk = opus_packets(d, at, size)
@@ -180,7 +231,7 @@ def main() -> int:
180 else: 231 else:
181 banks = [a.src] 232 banks = [a.src]
182 banks = banks[: a.limit] if a.limit else banks 233 banks = banks[: a.limit] if a.limit else banks
183 tot = {"opus": 0, "pcm": 0, "other": 0} 234 tot = {"opus": 0, "pcm": 0, "other": 0, "music": 0}
184 for i, b in enumerate(banks): 235 for i, b in enumerate(banks):
185 try: 236 try:
186 c = export_bank(b, a.out) 237 c = export_bank(b, a.out)
@@ -191,7 +242,8 @@ def main() -> int:
191 tot[k] += c[k] 242 tot[k] += c[k]
192 if (i + 1) % 200 == 0: 243 if (i + 1) % 200 == 0:
193 print(f" {i + 1}/{len(banks)} banks: {tot}", flush=True) 244 print(f" {i + 1}/{len(banks)} banks: {tot}", flush=True)
194 print(f"{len(banks)} banks: {tot['opus']} Ogg Opus, {tot['pcm']} WAV, {tot['other']} not handled (music/XMA/other)") 245 print(f"{len(banks)} banks: {tot['opus']} Ogg Opus, {tot['music']} music channels (Ogg Opus), {tot['pcm']} WAV, "
246 f"{tot['other']} not handled (XMA/other)")
195 return 0 247 return 0
196 248
197 249