"""Self-delimiting live transport: 46 payload bits in each 48-bit record. Each six-byte frame has a leading one bit and a zero padding bit. A footer starts with a zero high bit and holds a 31-bit sample count plus CRC32. The 48-byte model header and eight-byte footer add 56 fixed bytes. Payload is 2300 bit/s; framing adds 100 bit/s. CRC is verified at EOS, so earlier indices are provisional. No seek, full-file pass or duration-sized buffer is needed. """ from dataclasses import dataclass import struct import zlib import numpy as np from tinyasr.codec_bitstream import BITS_PER_FRAME, DEFAULT_MAX_FRAMES, HOP, LEVELS, SAMPLE_RATE, WIDTHS, model_bytes MAGIC=b'TCLIVE1\0' HEADER=struct.Struct('<8sBBHI32s') FOOTER_PREFIX=struct.Struct('>I') CRC=struct.Struct('self.max_frames:raise ValueError('Live frame limit exceeded') if np.any(codes<0) or np.any(codes>=LEVELS[:,:,None]):raise ValueError('Scalar index outside its grid') output=bytearray() for index in range(codes.shape[-1]): word=0 for value,width in zip(codes[:,:,index].reshape(-1),WIDTHS):word=(word<=self.max_frames:raise ValueError('Live frame limit exceeded') word=(value>>1)&((1<>shift)&((1<limit: self.failed=True;raise ValueError('Live byte limit exceeded') self.received_bytes+=len(data) try: if self.end is not None: if len(data):raise ValueError('Unexpected trailing live data') return empty_codes() if not self.header_complete: need=HEADER.size-len(self.header_buffer) used=min(need,len(data));self.header_buffer.extend(data[:used]);data=data[used:] if len(self.header_buffer)