Read a 16-bit PCM WAV and return (rate, mono float samples in [-1, 1]).
(path)
| 47 | |
| 48 | |
| 49 | def read_wav_mono(path): |
| 50 | """Read a 16-bit PCM WAV and return (rate, mono float samples in [-1, 1]).""" |
| 51 | with wave.open(str(path), "rb") as wav: |
| 52 | rate = wav.getframerate() |
| 53 | channels = wav.getnchannels() |
| 54 | width = wav.getsampwidth() |
| 55 | frames = wav.readframes(wav.getnframes()) |
| 56 | |
| 57 | if width != 2: |
| 58 | raise ValueError(f"Expected 16-bit PCM, got {width * 8}-bit") |
| 59 | |
| 60 | data = np.frombuffer(frames, dtype="<i2").astype(np.float64) / INT16_PEAK |
| 61 | if channels > 1: |
| 62 | data = data.reshape(-1, channels).mean(axis=1) |
| 63 | |
| 64 | return rate, data |
| 65 | |
| 66 | |
| 67 | def _normalize(signal): |