Decode audio files using whichever backend is available. The library currently supports:
- Gstreamer via PyGObject.
- Core Audio on Mac OS X via ctypes. (PyObjC not required.)
- MAD via the pymad bindings.
- FFmpeg or Libav via its command-line interface.
- The standard library wave, aifc, and sunau modules (for uncompressed audio formats).
Use the library like so:
with audioread.audio_open(filename) as f: print(f.channels, f.samplerate, f.duration) for buf in f: do_something(buf)
Buffers in the file can be accessed by iterating over the object returned from
audio_open. Each buffer is a bytes-like object (
bytearray) containing raw 16-bit little-endian signed integer PCM data. (Currently, these PCM format parameters are not configurable, but this could be added to most of the backends.)
Additional values are available as fields on the audio file object:
channelsis the number of audio channels (an integer).
samplerateis given in Hz (an integer).
durationis the length of the audio in seconds (a float).
audio_open function transparently selects a backend that can read the file. (Each backend is implemented in a module inside the
audioread package.) If no backends succeed in opening the file, a
DecodeError exception is raised. This exception is only used when the file type is unsupported by the backends; if the file doesn’t exist, a standard
IOError will be raised.
A second optional parameter to
audio_open specifies which backends to try (instead of trying them all, which is the default). You can use the
available_backends function to get a list backends that are usable on the current system.
Audioread is “universal” and supports both Python 2 (2.6+) and Python 3 (3.2+).
decode.py script demonstrates using this package to convert compressed audio files to WAV files.