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: Union[str, PathLike, IO], sample_rate: int = 44100, num_channels: Optional[int] = None, bits_per_sample: int = 16, fmt: WavFormat = WavFormat.PCM)

Bases: Wavfile

Class for writing a wave file

__init__(fp: Union[str, PathLike, IO], sample_rate: int = 44100, num_channels: Optional[int] = None, bits_per_sample: int = 16, fmt: WavFormat = WavFormat.PCM) None

Initialise the WavWrite object.

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 (chunk.WavFormat.PCM, chunk.WavFormat.IEEE_FLOAT)

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[Union[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 int or float; 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)

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.

See chunk.InfoItem for a list of supported tags.

Parameters

kwargs – The metadata to write, provided as keyword arguments.

Returns

Dict[str, str]

close() None

Close the file.