convert_to_wav()

audiofile.convert_to_wav(infile, outfile=None, offset=None, duration=None, bit_depth=16, normalize=False, overwrite=False, **kwargs)[source]

Convert any audio/video file to WAV.

It uses soundfile for reading WAV, FLAC, MP3, OGG files, and sox or ffmpeg for reading all other files. If duration and/or offset are specified the resulting WAV file will be shortened accordingly.

duration and offset support all formats mentioned in audmath.duration_in_seconds(), like '2 ms', or pd.to_timedelta(2, 's'). The exception is that float and integer values are always interpreted as seconds and strings without unit always as samples. If duration and/or offset are negative, they are interpreted from right to left, whereas duration starts from the end of the signal for offset=None. If the signal is shorter than the requested duration and/or offset only the part of the signal overlapping with the requested signal is returned, e.g. for a file containing the signal [0, 1, 2], duration=2, offset=-4 will return [0].

duration and offset are evenly rounded after conversion to samples.

It then uses soundfile.write() to write the WAV file, which limits the number of supported channels to 65535.

Parameters
  • infile (str) – audio/video file name

  • outfile (Optional[str]) – WAV file name. If None same path as infile but file extension is replaced by 'wav'

  • duration (Union[float, int, str, timedelta64, None]) – return only a specified duration

  • offset (Union[float, int, str, timedelta64, None]) – start reading at offset

  • bit_depth (int) – bit depth of written file in bit, can be 8, 16, 24

  • normalize (bool) – normalize audio data before writing

  • overwrite (bool) – force overwriting if outfile is identical to outfile

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

Return type

str

Returns

absolute path to resulting WAV file

Raises
  • FileNotFoundError – if ffmpeg binary is needed, but cannot be found

  • RuntimeError – if file is missing, broken or format is not supported

  • RuntimeError – if infile would need to be overwritten and overwrite is False

  • ValueError – if duration is a string that does not match a valid ‘<value><unit>’ pattern or the provided unit is not supported

Examples

>>> path = convert_to_wav("stereo.flac")
>>> os.path.basename(path)
'stereo.wav'