"""Look at what's at the divergence boundary.""" from __future__ import annotations import sys from pathlib import Path REPO = Path(__file__).resolve().parents[1] sys.path.insert(0, str(REPO)) from minimateplus.waveform_codec import walk_body, find_data_start, parse_segment_header from analysis_idf.recon import TARGET, TXT, load_sidecar_samples def main(): buf = TARGET.read_bytes() body = buf[0x0f1f:] start = find_data_start(body) print(f"data_start: {start} (= file offset 0x{0x0f1f + start:04x})") blocks = walk_body(body, start) print(f"{len(blocks)} blocks total") print() # First 25 blocks print("=== first 30 blocks ===") for i, b in enumerate(blocks[:30]): body_off = 0x0f1f + b.offset if b.tag_hi == 0x40: hdr = parse_segment_header(b) print(f" [{i:3d}] @0x{body_off:04x} {b.kind} (segment header) counter={hdr['counter'] if hdr else '?'} field2={hdr['field2'].hex() if hdr else '?'} anchor={hdr['anchor_bytes'].hex() if hdr else '?'} tail={hdr['tail'].hex() if hdr else '?'}") else: print(f" [{i:3d}] @0x{body_off:04x} {b.kind} len={b.length} data={b.data[:16].hex()}") print() # Cumulative sample counts per block to find which block contains sample 254 print("=== cumulative samples through blocks ===") cur_ch = "Tran" rotation = ["Vert", "Long", "MicL", "Tran"] seg_count = 0 samples_in_curseg = 2 # preamble Tran[0], Tran[1] for i, b in enumerate(blocks[:30]): if b.tag_hi == 0x40: seg_count += 1 prev_ch = cur_ch cur_ch = rotation[(seg_count - 1) % 4] print(f" [{i:3d}] 40 02 -> end of {prev_ch} segment, start {cur_ch} (segment {seg_count})") samples_in_curseg = 2 # anchors elif (b.tag_hi & 0xF0) == 0x10: nn = ((b.tag_hi & 0x0F) << 8) | b.tag_lo samples_in_curseg += nn print(f" [{i:3d}] {b.kind} nibble: +{nn} samples, ch={cur_ch}, ch_total~{samples_in_curseg}") elif (b.tag_hi & 0xF0) == 0x20: nn = ((b.tag_hi & 0x0F) << 8) | b.tag_lo samples_in_curseg += nn print(f" [{i:3d}] {b.kind} int8: +{nn} samples, ch={cur_ch}, ch_total~{samples_in_curseg}") elif b.tag_hi == 0x00: samples_in_curseg += b.tag_lo print(f" [{i:3d}] {b.kind} RLE: +{b.tag_lo}, ch={cur_ch}, ch_total~{samples_in_curseg}") elif b.tag_hi == 0x30: samples_in_curseg += b.tag_lo print(f" [{i:3d}] {b.kind} packed12: +{b.tag_lo} samples, ch={cur_ch}, ch_total~{samples_in_curseg}") if __name__ == "__main__": main()