wavfile package
Read/write wave audio files to/from lists of native Python types.
The library currently supports PCM (integer) and IEEE float formats, and supports arbitrary integer precision, including: 16-, 24-, 32-, and 64-bit samples.
- wavfile.open(f: str | PathLike | IO, mode: str | None = None, sample_rate: int = 44100, num_channels: int | None = None, bits_per_sample: int = 16, fmt: WavFormat = WavFormat.PCM) WavRead | WavWrite
Open the wave file.
If writing and
num_channelsis unspecified, it is determined automatically from the first block of samples.The format code is either
wavfile.chunk.WavFormat.PCM,wavfile.chunk.WavFormat.IEEE_FLOAT, orwavfile.chunk.WavFormat.EXTENSIBLE. The extensible code corresponds to a variation of the wave file format intended for audio with: a bit depth of greater than 16 bits, or more than two channels. The file will be updated automatically to use the extensible format as appropriate. If the extensible format is specified explicitly, then the audio data will be PCM encoded.- Parameters:
f – Either a path to a wave file or a pointer to an open file.
mode – Open the file for reading (‘r’, ‘rb’) or writing (‘w’, ‘wb’).
sample_rate – The sample rate for the new file (write only).
num_channels – The number of audio channels for the new file (write only).
bits_per_sample – The number of bits to encode each audio sample (write only).
fmt – The format code (write only) (
chunk.WavFormat)
- Returns:
Returns a
wavread.WavReadobject orwavwrite.WavWriteobject.
- wavfile.read(path: str | PathLike, fmt: str = 'int') Tuple[List[List[int | float]], int, int]
Shortcut function to read a wave file. The audio can be read directly (
fmt='native'), converted to integers (fmt='int'), or converted to floating point (fmt='float').- Parameters:
path – Path to the wave audio file.
fmt – Read the file as ‘int’, ‘float’, or ‘native’.
- Returns:
The audio data, the sample rate, and the bit depth.
- wavfile.write(path: str | PathLike, audio_data: List[List[int | float]], sample_rate: int = 44100, bits_per_sample: int = 16, fmt: WavFormat = WavFormat.PCM, metadata: Dict[str, int | str] | None = None) None
Shortcut function to write a wave file. The data should be contained in a list of lists with size (N,C), where C is the number of audio channels. The data may be int or float. The data may be converted if they do match the format of the destination file.
Metadata can be added to the wav file by providing an appropriate dict. See chunk.InfoItem for a list of valid metadata fields.
- Parameters:
path – Path to the newly created wave file.
audio_data – The data to be written to the file.
sample_rate – The sample rate for the new file.
bits_per_sample – The number of bits to encode each audio sample (write only).
fmt – The audio format (
chunk.WavFormat)metadata – The metadata to write, provided as a dictionary.
- wavfile.split(path: str | PathLike) None
Split a multichannel wave file in to multiple mono wave files.
- Parameters:
path – Path to the multichannel wave file.
- wavfile.join(filename: str | PathLike, *paths: str | PathLike) None
Join several wave files in to a single multichannel wave file.
The resulting wave file’s duration will be equal to the longest wave file, with other shorter channels being appended with zeros. The order of the channels will match the order of the specified files.
- Parameters:
filename – Name of the output file.
paths – Paths to the wave files.
Submodules
- wavfile.base module
- wavfile.chunk module
BytesEnumRiffFormatChunkIDWavFormatListTypeInfoItemChunkChunk.alignChunk.offsetChunk.word_sizeChunk.__init__()Chunk.fpChunk.bigendianChunk.startChunk.chunk_idChunk.sizeChunk.read_onlyChunk.endiannessChunk.content_startChunk.padChunk.skip()Chunk.read()Chunk.write()Chunk.write_header()Chunk.bytes_to_int()Chunk.read_int()Chunk.int_to_bytes()Chunk.write_int()Chunk.bytes_to_float()Chunk.read_float()Chunk.float_to_bytes()Chunk.write_float()Chunk.close()
RiffChunkWavFmtChunkWavFmtExtensibleChunkWavDataChunkListChunk
- wavfile.exception module
- wavfile.version module
- wavfile.wavread module
- wavfile.wavwrite module