Python wave Module
Example
Read a WAV audio file and get its properties:
import wave
with wave.open('audio.wav', 'rb') as wav_file:
print(f'Channels: {wav_file.getnchannels()}')
print(f'Sample width: {wav_file.getsampwidth()}')
print(f'Frame rate: {wav_file.getframerate()}')
print(f'Frames: {wav_file.getnframes()}')
Try it Yourself »
Definition and Usage
The wave module provides a convenient interface for reading and writing WAV audio files.
Use it to access and manipulate audio file properties, read or write audio data, or convert between different audio formats.
Note: This module only works with WAV format files (RIFF/WAVE). For other audio formats, consider using third-party libraries.
Members
Member | Description |
---|---|
Error | Exception raised when there is an error with a WAV file. |
Wave_read | Object for reading WAV audio files. |
Wave_read.close() | Finish reading and close the file. |
Wave_read.getcompname() | Get a text description of the compression format. |
Wave_read.getcomptype() | Get the compression type (always returns 'NONE' for uncompressed). |
Wave_read.getframerate() | Get how many frames are played per second (sample rate). |
Wave_read.getnchannels() | Get the number of audio channels (1=mono, 2=stereo). |
Wave_read.getnframes() | Get the total number of audio frames in the file. |
Wave_read.getparams() | Get all audio properties in one tuple. |
Wave_read.getsampwidth() | Get the sample width in bytes (1, 2, 3, or 4). |
Wave_read.readframes() | Read the next chunk of audio frames as raw bytes. |
Wave_read.rewind() | Jump back to the start of the audio data. |
Wave_read.setpos() | Jump to a specific frame number. |
Wave_read.tell() | Get the current frame position. |
Wave_write | Object for writing WAV audio files. |
Wave_write.close() | Finish writing and close the file properly. |
Wave_write.setnchannels() | Set how many audio channels (1 for mono, 2 for stereo). |
Wave_write.setcomptype() | Set compression type (only 'NONE' is supported). |
Wave_write.setframerate() | Set how many frames per second (sample rate). |
Wave_write.setnframes() | Set the total number of frames (optional, calculated automatically). |
Wave_write.setparams() | Set all audio properties at once using a tuple. |
Wave_write.setsampwidth() | Set sample width in bytes (1, 2, 3, or 4). |
Wave_write.tell() | Get the current frame position. |
Wave_write.writeframes() | Write audio frames and update the file header. |
Wave_write.writeframesraw() | Write audio frames without updating the header yet. |
open() | Open a WAV file for reading ('rb') or writing ('wb'). |