From 6cd9392ccefec2a7fc0a909d7b6e10e2d9367801 Mon Sep 17 00:00:00 2001 From: kirigayakazima <46666711+kirigayakazima@users.noreply.github.com> Date: Fri, 27 Nov 2026 19:04:46 +0800 Subject: [PATCH] awc_opus: preserve music channels with colliding stream IDs --- tests/test_awc_manifest.py | 17 +++++++++++++++++ tools/awc_manifest.py | 12 +++++++----- tools/awc_opus.py | 17 +++++++++++------ 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/tests/test_awc_manifest.py b/tests/test_awc_manifest.py index 346b23b382..14fc9fd707 100644 --- a/tests/test_awc_manifest.py +++ b/tests/test_awc_manifest.py @@ -91,6 +91,23 @@ def run_tool(self, *args): return subprocess.run([sys.executable, "-I", TOOL, *args], capture_output=True, text=True, errors="replace", timeout=60) + def test_music_id_collisions_keep_every_channel_and_manifest_row(self): + regular = entries(with_music=False)[-1] + desc, audio = music_blob([0x30, 0x30], 960, 48000) + audio = bytearray(audio) + audio[2050] = 0x78 + audio[4098] = 0x7C + bank = Path(self.tmp.name) / "mix.awc" + bank.write_bytes(build_bank([regular, (0x50, [(0x48, desc), (0x55, audio)])])) + awc_manifest.awc_opus.export_bank(bank, self.tmp.name) + files = sorted((bank.parent / "mix").glob("*.ogg")) + self.assertEqual([p.stem for p in files], ["00000030", "00000030~2", "00000030~3"]) + self.assertEqual([p.read_bytes()[-1] for p in files], [0xF8, 0x78, 0x7C]) + rows = awc_manifest._bank_rows(bank, bank.name) + self.assertEqual([row["streamed_music"] for row in rows], [False, True, True]) + for path, row in zip(files[1:], rows[1:]): + self.assertIn(path.stem, row["notes"]) + def test_csv_rows_codecs_and_bad_bank_skip(self): bank = self.write_bank() with open(os.path.join(self.tmp.name, "bad.awc"), "wb") as f: diff --git a/tools/awc_manifest.py b/tools/awc_manifest.py index 89372f41f9..4a47677d8b 100644 --- a/tools/awc_manifest.py +++ b/tools/awc_manifest.py @@ -24,6 +24,7 @@ Usage: `python -I tools/awc_manifest.py --help` lists the options. """ import argparse +from collections import Counter import csv import json import os @@ -129,6 +130,7 @@ def _bank_rows(path: str, bank: str) -> list: note = f"same id as an earlier stream in this bank; awc_opus exports it as {stem}" row["notes"] = "; ".join(x for x in (row["notes"], note) if x) rows.append(row) + used_stems = Counter(s["id"] for s in streams) for mus, data in music or []: try: channels = awc_opus.music_channels(d, mus, data, collect_packets=False) @@ -141,11 +143,11 @@ def _bank_rows(path: str, bank: str) -> list: continue for cid, samples, rate, codec, _packets, packet_channels in channels: row = _row(bank, cid, codec, packet_channels, rate, samples, data, bank_size, True) - same = next((i for i, r in enumerate(rows) if r["stream"] == row["stream"]), None) - if same is None: - rows.append(row) - else: # as before: a streamed-music channel replaces a row with its id - rows[same] = row + stem = awc_opus.next_export_stem(cid, used_stems) + if stem != row["stream"]: + note = f"same id as an earlier stream in this bank; awc_opus exports it as {stem}" + row["notes"] = "; ".join(x for x in (row["notes"], note) if x) + rows.append(row) return rows diff --git a/tools/awc_opus.py b/tools/awc_opus.py index 71b6d52b22..c149b99a1a 100644 --- a/tools/awc_opus.py +++ b/tools/awc_opus.py @@ -341,15 +341,17 @@ def opus_packets(d: bytes, at: int, size: int): return out +def next_export_stem(sid, seen): + seen[sid] += 1 + return f"{sid:08X}" if seen[sid] == 1 else f"{sid:08X}~{seen[sid]}" + + def export_stems(ids): """The file stem for each stream, in stream-table order: `` (8 hex digits) for the first stream with an id and `~2`, `~3`, ... for later ones. A bank can hold two streams with the same 29-bit id; naming them apart keeps the second from overwriting the first. awc_manifest notes the same stems.""" - seen, out = {}, [] - for sid in ids: - seen[sid] = seen.get(sid, 0) + 1 - out.append(f"{sid:08X}" if seen[sid] == 1 else f"{sid:08X}~{seen[sid]}") - return out + seen = Counter() + return [next_export_stem(sid, seen) for sid in ids] def bank_output_dir(path, out_dir): @@ -401,14 +403,17 @@ def export_bank(path: str, out_dir: str, pre_skip: int = OPUS_PRE_SKIP, cue_shee os.makedirs(od, exist_ok=True) counts = {"opus": 0, "pcm": 0, "other": 0, "music": 0} channel_counts = Counter() + # Keep existing standalone stream names, even though music is written first. + used_stems = Counter(s["id"] for s in streams) frame_limits = {} for mus, data in music: for cid, samples, rate, codec, pk, packet_channels in music_channels(d, mus, data): + stem = next_export_stem(cid, used_stems) if cue_sheet: channel_counts[cid] += 1 if pk: n48 = samples * 48000 // rate if rate else 0 - granule = write_ogg_opus(os.path.join(od, f"{cid:08X}.ogg"), pk, packet_channels, cid, n48, + granule = write_ogg_opus(os.path.join(od, stem + ".ogg"), pk, packet_channels, cid, n48, pre_skip=pre_skip) if cue_sheet and rate == 48000 and samples > 0: frame_limits[cid] = min(samples, max(0, granule - pre_skip))