Skip to content

awc_markers: read the stereo bit from the first block that has a packet - #6405

Open
cynbun wants to merge 2 commits into
ItsNotRexed:mainfrom
cynbun:awc-markers-stereo-later-block
Open

cynbun wants to merge 2 commits into
ItsNotRexed:mainfrom
cynbun:awc-markers-stereo-later-block

Conversation

@cynbun

@cynbun cynbun commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Claimed in #6214 (comment) (our own finding).

Cause. tools/awc_markers.py:87-93 (music_frames): the channel table and the packet offset come from block 0 of the 0x55 data only. A channel with 0 subblocks in block 0 gets no packet, so its stereo bit reads as 0 and the interleaved samples is used as its frame count: twice the real length when its packets are stereo, so markers past the real end are not counted in chunks_past_end.

Doc. docs/audio.md:127-140: each block of the 0x55 data starts with its own 24-byte record per channel (+4 = that channel's subblock count), the chunk size is the distance between blocks, and the packets' TOC stereo bit decides samples / 2. The module docstring already says the count is "read from the TOC stereo bit of the channel's first packet". awc_opus.music_channels walks every block for this since #700 (ASSET-53).

Fix. Walk the blocks, chunk apart, and take each channel's first packet from the first block whose table gives it a subblock; stop as soon as every channel has one. Later blocks are read only when chunk >= 2048 (the bound awc_opus.music_channels uses) and the block's table is inside the tag. A bank whose channels all have a packet in block 0 reads exactly what it read before (one table, one 3-byte read per channel).

Test. test_stereo_bit_of_a_first_packet_in_a_later_block builds a synthetic two-block, one-channel bank whose only packet is in block 1, with a marker at frame 150,000 and 192,000 interleaved samples: stereo packets (TOC 0xFC) must give chunks_past_end 1 (96,000 frames, what awc_opus.music_channels reports for the same bytes), mono (0xF8) 0. It fails on main (0 for the stereo bank) and passes with the fix. Synthetic data only, no game files.

Tested on Linux, Python 3.14.8 (CPU only, no GPU involved):

AI-assisted (Claude Code).

🤖 Generated with Claude Code

music_frames() read the channel table of block 0 only, so a streamed-music
channel with no subblock in block 0 had no packet, counted as mono, and
its interleaved sample count was used as the frame count: twice the real
length for stereo packets. Each block has its own channel table
(docs/audio.md), so walk the blocks, the descriptor's chunk size apart,
until every channel's first packet is found, as awc_opus.music_channels
does since ASSET-53 (ItsNotRexed#700). A bank whose packets all start in block 0
reads exactly what it read before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Kev-Ball

Copy link
Copy Markdown

At bd0f70fb I found one case where the marker reader and the Opus exporter still disagree. A channel can have an allocated subblock whose first u16 is zero (no packets in that subblock). music_frames records its three bytes as soon as the subblock count is nonzero, so it stops looking before the first actual packet.

I used the existing test_awc_opus.build_repeating_music_bank fixture with one channel, 192,000 interleaved samples, an empty first block, and a stereo packet in block 1. Changing only the first block’s channel subblock count gave:

First block Main marker frames PR marker frames awc_opus.music_channels frames
0 subblocks 192,000 96,000 96,000
1 allocated, zero-filled subblock 192,000 192,000 96,000

The second row would still miss a marker at frame 150,000. Deferring the dictionary entry until an actual packet is found, including checking later subblocks belonging to the same channel, would cover this case.

I also compared the two marker readers on the complete Xbox 1.0.2.1 audio archive. All 3,116 returned channel entries across 3,008 music descriptors agreed. The discrepancy above is a synthetic control. CachyOS x86-64, CPU only, AI-assisted.

…rst packet

Kev-Ball's case on ItsNotRexed#6405: block 0 allocates a zero-filled subblock for the
channel, so music_frames took its three bytes and never saw the stereo TOC in
block 1. Walk the channel's subblocks and take the first one that holds a
packet, as awc_opus.opus_packets stops at a zero size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cynbun

cynbun commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @Kev-Ball — that case is real, and your table made it easy to reproduce. 29115ba68 fixes it: music_frames now walks the channel's subblocks in each block and takes the first one whose leading u16 is nonzero (the same stop awc_opus.opus_packets makes), so an allocated, zero-filled subblock no longer counts as the first packet.

New test test_an_empty_allocated_subblock_is_not_the_first_packet is your row 2: block 0 allocates one zero-filled subblock, the stereo packet is in block 1, and the marker at frame 150,000 is reported past the end (96,000 frames, matching awc_opus.music_channels). It fails without the change and passes with it; test_awc_markers.py (10), test_awc_opus.py and test_rsc8_audio.py pass, compileall and git diff --check clean (Linux, Python 3.14.8).

Thanks also for the full-archive comparison (3,116 channel entries in agreement) — that shows the change doesn't move any retail count. AI-assisted (Claude Code).

@Kev-Ball

Copy link
Copy Markdown

Confirmed 29115ba68 on the original allocated-but-empty-subblock reproduction. With the first stereo packet in block 1, the revised marker reader and the exporter both report 96,000 frames. I also placed the empty and populated subblocks in the same block. The revised reader again matched the exporter for stereo (96,000 frames) and mono (192,000 frames).

On the complete Xbox 1.0.2.1 audio archive, all 3,116 channel entries across 3,008 music descriptors still matched main. CachyOS x86-64, CPU only, AI-assisted.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 29115ba merged with main 5eb8f1d on Linux/Python 3.12.16: pass

  • python -I tests/test_awc_markers.py: 13 tests OK
  • Negative control: git checkout upstream/main -- tools/awc_markers.py, same test: FAILED (failures=2). Restored with git checkout HEAD --.
  • Related: python -I tests/test_awc_opus.py OK, python -I tests/test_awc_names.py OK (1 skipped).

Diff read: music_frames now walks the 0x55 data block by block (descriptor chunk apart, up to blocks), reading each block's own channel table, until every channel's first packet is found, instead of looking only in block 0; a subblock whose first u16 is 0 holds no packet and is skipped; it stops at a block whose table would not fit in data[1]. Synthetic inputs only; I did not run it on a real bank.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants