"""Shared constants and tiny I/O helpers. Every other module imports these, so the frame grid, label ids and file layout are defined exactly once.""" import json import wave from pathlib import Path import numpy as np SR = 16000 # input sample rate (caller audio, and self audio as the controller sees it) FRAME_MS = 80 FRAME = SR * FRAME_MS // 1000 # 1280 samples per 80 ms frame HOP = 160 # 10 ms mel hop (same as the baseline encoder's front end) N_FFT = 400 N_MELS = 128 PATCH = 4 # mel frames per encoder token: 40 ms, 25 Hz (the baseline encoder's output rate) TOK_PER_FRAME = FRAME // (HOP * PATCH) # 2 encoder tokens per 80 ms frame TEACHER_DIM = 1280 # baseline AudioEncoder n_audio_state (config.json of Step-Audio-R1.1) OUT_SR = 24000 # reply audio sample rate (Step-Audio 2 token2wav and Kokoro both emit 24 kHz) OUT_FRAME = OUT_SR * FRAME_MS // 1000 # 1920 samples LABELS = ["WAIT", "START", "STOP", "BACKCHANNEL"] WAIT, START, STOP, BACKCHANNEL = range(4) CALLER, AGENT = 0, 1 # channel order in every dataset wav def read_wav(path, start=0, frames=None): """PCM16 wav -> float32 [T, C] in [-1, 1], sample rate. Reads only the requested slice.""" with wave.open(str(path), "rb") as w: assert w.getsampwidth() == 2, f"{path}: expected 16-bit PCM" n = w.getnframes() start = min(start, n) frames = n - start if frames is None else min(frames, n - start) w.setpos(start) x = np.frombuffer(w.readframes(frames), dtype=" PCM16 wav.""" x = np.asarray(x, dtype=np.float32) if x.ndim == 1: x = x[:, None] pcm = (np.clip(x, -1.0, 1.0) * 32767.0).astype("