On this page
wave
Prev: warnings | Up: stdlib_module | Next: weakref
Rel: colorsys, gettext, locale, turtle, turtledemo
Ref: docs.python.org — wave
Tags: #public #python3 #stdlib
Import
import wave
Overview
Reader and writer for uncompressed PCM WAV audio files (wave.open()). Use wave to inspect audio headers (channels, sample width, frame rate), synthesize simple tones/test fixtures with struct or array, or extract raw PCM samples for DSP work.
Quick Start
import io
import math
import struct
import wave
buf = io.BytesIO()
sample_rate = 8000
with wave.open(buf, "wb") as wf:
wf.setnchannels(1) # mono
wf.setsampwidth(2) # 16-bit signed PCM
wf.setframerate(sample_rate)
frames = b"".join(
struct.pack("<h", int(16000 * math.sin(2 * math.pi * 440 * i / sample_rate)))
for i in range(800) # 0.1 seconds of 440 Hz tone
)
wf.writeframes(frames)
buf.seek(0)
with wave.open(buf, "rb") as rf:
assert (rf.getnchannels(), rf.getsampwidth(), rf.getframerate(), rf.getnframes()) == (1, 2, 8000, 800)
Implementation Walkthrough
1. Inspecting WAV Metadata and Duration
getparams() returns a named tuple (nchannels, sampwidth, framerate, nframes, comptype, compname):
import wave
def wav_duration_seconds(path: str) -> float:
with wave.open(path, "rb") as wf:
p = wf.getparams()
return p.nframes / float(p.framerate)
Gotchas & Boundaries
- Uncompressed PCM only:
wavecannot decode MP3, FLAC, OGG, AAC, or compressed WAV codecs (like ADPCM or IEEE float 32-bit WAV). Usesoundfile,pydub, orffmpegfor those. - Frame vs. byte counts:
readframes(n)readsnaudio frames, which equalsn * nchannels * sampwidthbytes. - Configure parameters before writing: In
"wb"mode, you must setnchannels,sampwidth, andframerate(or callsetparams()) beforewriteframes().
Common Functions
- wave.open() —
open(f, mode=None)—wave.open()function.
Classes & Objects
- wave.Error —
wave.Errorclass. (Methods:.add_note(),.with_traceback()) - wave.Wave_read — Variables used in this class: These variables are available to the user though appropriate methods of this class: (Methods:
.close(),.getcompname(),.getcomptype(),.getfp(),.getframerate(),.getmark()) - wave.Wave_write — Variables used in this class: These variables are user settable through appropriate methods of this class: (Methods:
.close(),.getcompname(),.getcomptype(),.getframerate(),.getmark(),.getmarkers())
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/cmd
- tuts/python3/stdlib_module/colorsys
- tuts/python3/stdlib_module/gettext
- tuts/python3/stdlib_module/idlelib
- tuts/python3/stdlib_module/locale
- tuts/python3/stdlib_module/shlex
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/tkinter
- tuts/python3/stdlib_module/turtle
- tuts/python3/stdlib_module/turtledemo
- tuts/python3/stdlib_module/warnings
- tuts/python3/stdlib_module/wave/Error
- tuts/python3/stdlib_module/wave/open
- tuts/python3/stdlib_module/wave/Wave_read
- tuts/python3/stdlib_module/wave/Wave_write
- tuts/python3/stdlib_module/weakref
Related notes
- tuts/python3/stdlib_module/colorsys Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/gettext Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/locale Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/turtle Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/turtledemo Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/warnings Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/wave/Error Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/wave/open Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/wave/Wave_read Linked here; links to this page; shared tags: #stdlib