22.5. wave — Read and write WAV files
The wave module provides a convenient interface to the WAV sound format.
It does not support compression/decompression, but it does support mono/stereo.
The wave module defines the following function and exception:
If file is a string, open the file by that name, other treat it as a seekable
file-like object. mode can be any of
- 'r', 'rb'
- Read only mode.
- 'w', 'wb'
- Write only mode.
Note that it does not allow read/write WAV files.
A mode of 'r' or 'rb' returns a Wave_read object, while a
mode of 'w' or 'wb' returns a Wave_write object. If mode
is omitted and a file-like object is passed as file, file.mode is used as
the default value for mode (the 'b' flag is still added if necessary).
- A synonym for open(), maintained for backwards compatibility.
- An error raised when something is impossible because it violates the WAV
specification or hits an implementation deficiency.
22.5.1. Wave_read Objects
Wave_read objects, as returned by open(), have the following methods:
- Close the stream, and make the instance unusable. This is called automatically
on object collection.
- Returns number of audio channels (1 for mono, 2 for stereo).
- Returns sample width in bytes.
- Returns sampling frequency.
- Returns number of audio frames.
- Returns compression type ('NONE' is the only supported type).
- Human-readable version of getcomptype(). Usually 'not compressed'
- Returns a tuple (nchannels, sampwidth, framerate, nframes, comptype,
compname), equivalent to output of the get*() methods.
- Reads and returns at most n frames of audio, as a string of bytes.
- Rewind the file pointer to the beginning of the audio stream.
The following two methods are defined for compatibility with the aifc
module, and don’t do anything interesting.
- Returns None.
- Raise an error.
The following two methods define a term “position” which is compatible between
them, and is otherwise implementation dependent.
- Set the file pointer to the specified position.
- Return current file pointer position.
22.5.2. Wave_write Objects
Wave_write objects, as returned by open(), have the following methods:
- Make sure nframes is correct, and close the file. This method is called upon
- Set the number of channels.
- Set the sample width to n bytes.
- Set the frame rate to n.
- Set the number of frames to n. This will be changed later if more frames are
- Set the compression type and description. At the moment, only compression type
NONE is supported, meaning no compression.
- The tuple should be (nchannels, sampwidth, framerate, nframes, comptype,
compname), with values valid for the set*() methods. Sets all
- Return current position in the file, with the same disclaimer for the
Wave_read.tell() and Wave_read.setpos() methods.
- Write audio frames, without correcting nframes.
- Write audio frames and make sure nframes is correct.
Note that it is invalid to set any parameters after calling writeframes()
or writeframesraw(), and any attempt to do so will raise