wavfile.wavwrite module
Provides the WavWrite class for writing wave files.
The WavWrite class is returned by wavfile.open() when opening a file in write mode.
- class wavfile.wavwrite.WavWrite(fp: str | PathLike | IO, sample_rate: int = 44100, num_channels: int | None = None, bits_per_sample: int = 16, fmt: WavFormat = WavFormat.PCM)
Bases:
WavfileClass for writing a wave file
- __init__(fp: str | PathLike | IO, sample_rate: int = 44100, num_channels: int | None = None, bits_per_sample: int = 16, fmt: WavFormat = WavFormat.PCM) None
Initialise the WavWrite object. The fmt argument may be any of the enumerations of wavfile.chunk.WavFormat. If wavfile.chunk.WavFormat.EXTENSIBLE is specified, then the audio data are PCM-encoded.
- Parameters:
fp – Either a path to a wave file or a pointer to an open file.
sample_rate – The sample rate for the new file.
num_channels – The number of audio channels for the new file.
bits_per_sample – The number of bits to encode each audio sample.
fmt – The audio format (
wavfile.chunk.WavFormat)
- write_float(audio: List[List[float]]) None
Write float data to the file. If the file format is PCM then the data will be converted to floats, otherwise there will be no conversion.
- Parameters:
audio – Audio frames to write.
- write_int(audio: List[List[int]]) None
Write integer data to the file. If the file format is IEEE_FLOAT then the data will be converted to floats, otherwise there will be no conversion.
- Parameters:
audio – Audio frames to write.
- write(audio: List[List[int | float]]) None
Write audio data to the file. The data should be a list of lists with dimensions (N,C), where N is the number of frames and C is the number of audio channels. The data may be
intorfloat; the method will attempt to determine the format of the data, and choose the appropriate scaling and conversion when writing the file.- Parameters:
audio – Audio frames to write.
- add_metadata(**kwargs: str | int) None
Add metadata to the wav file. Note that this method can only be called once, and the metadata cannot be updated once it is written. The metadata chunk will be written before or after the data chunk, depending on when this method is called. Note that if the method is called after writing audio data, then it will not be possible to write additional audio data (since this may overwrite the trailing metadata chunk).
See
wavfile.chunk.InfoItemfor a list of supported tags.- Parameters:
kwargs – The metadata to write, provided as keyword arguments.
- close() None
Close the file.