"""Small wav I/O helpers (soundfile if present, wave fallback).""" import wave import numpy as np def read_wav(path): try: import soundfile as sf x, sr = sf.read(path, dtype="float32", always_2d=False) if x.ndim > 1: x = x.mean(axis=1) return np.ascontiguousarray(x, dtype=np.float32), int(sr) except Exception: with wave.open(path, "rb") as w: sr = w.getframerate() ch = w.getnchannels() sw = w.getsampwidth() raw = w.readframes(w.getnframes()) if sw == 2: x = np.frombuffer(raw, dtype=" 1: x = x.reshape(-1, ch).mean(axis=1) return np.ascontiguousarray(x, dtype=np.float32), int(sr) def write_wav(path, x, sr): x = np.clip(np.asarray(x, dtype=np.float32), -1.0, 1.0) try: import soundfile as sf sf.write(path, x, int(sr), subtype="PCM_16") return except Exception: pcm = (x * 32767.0).astype("