MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / read_wav_mono

Function read_wav_mono

tests/utils/audio_tools.py:49–64  ·  view source on GitHub ↗

Read a 16-bit PCM WAV and return (rate, mono float samples in [-1, 1]).

(path)

Source from the content-addressed store, hash-verified

47
48
49def 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
67def _normalize(signal):

Callers 2

Calls 1

openMethod · 0.45

Tested by 2