Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions tests/test_awc_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 7 additions & 5 deletions tools/awc_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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


Expand Down
17 changes: 11 additions & 6 deletions tools/awc_opus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: `<id>` (8 hex digits) for the first stream with an
id and `<id>~2`, `<id>~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):
Expand Down Expand Up @@ -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))
Expand Down
Loading