diff options
| author | NotRexed <therealnothingfn@gmail.com> | 2026-11-20 22:28:16 -0500 |
|---|---|---|
| committer | NotRexed <therealnothingfn@gmail.com> | 2026-11-20 22:28:16 -0500 |
| commit | bfba26fbbb4c283a6c19405210deff4e8fc64847 (patch) | |
| tree | 32de92829c8e9a31a50d53c1dbd00fdecd4d1979 /tools | |
| parent | a16d58e58e9ee7e8b644d676012c98d91510fe4a (diff) | |
Audio: streamed AWC music channels to Ogg Opus
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/awc_opus.py | 64 |
1 files changed, 58 insertions, 6 deletions
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 | |||
| 8 | 2048-byte blocks of [u16 length][packet] ..., a zero length pads the rest of the block. Codec 0 = PCM16 (written as | 8 | 2048-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 | ||
| 11 | Streamed music (tag 0x48 descriptor: u32 blocks, u32 chunk size, u32 channels, then 16 bytes per channel: id, | ||
| 12 | samples, rate at +10, codec at +12) is split into one mono Ogg Opus per channel: each chunk starts with a | ||
| 13 | 24-byte record per channel (subblock count at +4, repeated-prefix samples to skip at +8), the channel data are | ||
| 14 | its run of 2048-byte subblocks after the 2048-aligned header, and the first `skip` samples of a chunk repeat the | ||
| 15 | previous chunk's tail and are dropped (whole packets). | ||
| 16 | |||
| 11 | Output: <out>/<bank>/<stream id>.ogg (or .wav), plus a summary line per bank. | 17 | Output: <out>/<bank>/<stream id>.ogg (or .wav), plus a summary line per bank. |
| 12 | """ | 18 | """ |
| 13 | import argparse | 19 | import 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 | ||
| 143 | def 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 | |||
| 132 | def opus_packets(d: bytes, at: int, size: int): | 176 | def 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 | ||