write()

audiofile.write(file, signal, sampling_rate, bit_depth=16, normalize=False, **kwargs)[source]

Write (normalized) audio files.

Save audio data provided as an array of shape (channels, samples) or (samples,) to a WAV, FLAC, NP3, or OGG file. channels can be up to 65535 for WAV, 255 for OGG, 2 for MP3, and 8 for FLAC. For monaural audio the array can be one-dimensional.

It uses soundfile.write() to write the audio files.

Parameters
  • file (str) – file name of output audio file. The format (WAV, FLAC, MP3, OGG) will be inferred from the file name

  • signal (array) – audio data to write

  • sampling_rate (int) – sample rate of the audio data

  • bit_depth (int) – bit depth of written file in bit, can be 8, 16, 24 for WAV and FLAC files, and in addition 32 for WAV files

  • normalize (bool) – normalize audio data before writing

  • kwargs – pass on further arguments to soundfile.write()

Raises

RuntimeError – for non-supported bit depth or number of channels

Examples

>>> sampling_rate = 8000
>>> signal = np.random.uniform(-1, 1, (1, 1000))
>>> write("mono.wav", signal, sampling_rate)
>>> signal = np.random.uniform(-1.2, 1.2, (2, 1000))
>>> write("stereo.flac", signal, sampling_rate, normalize=True)