The wave module reads and writes WAV files containing uncompressed PCM audio. It exposes channel count, sample width, frame rate, and raw frames for inspection and simple transformations.
Inspect WAV parameters
import wave
with wave.open("input.wav", "rb") as audio:
channels = audio.getnchannels()
sample_width = audio.getsampwidth()
frame_rate = audio.getframerate()
frame_count = audio.getnframes()
frames = audio.readframes(frame_count)
print(channels, sample_width, frame_rate, len(frames))
Distinguish frames from samples
readframes(n) returns bytes for up to n frames. A frame contains one sample per channel, and each sample occupies the declared width. Numeric interpretation must account for byte order, signedness, and channel interleaving.
Write a consistent header
Before writeframes(), set channel count, sample width, and frame rate to match the byte stream. Context managers finalize headers on close. Validate sizes and parameters from untrusted files before allocating memory.
Keep learning
Continue with binary data parsing with struct. The official Python documentation, accessed July 22, 2026, covers read and write modes and supported WAV headers.