audioread
audioread is a Python library that provides a unified, cross-platform interface for decoding audio files using various underlying system libraries such as GStreamer, FFmpeg, or libmad. It aims to offer a simple way to read raw audio data and its properties from many common audio formats. The current version is 3.1.0, and it maintains a moderate release cadence, primarily for bug fixes and compatibility with newer Python versions or backend libraries.
Warnings
- gotcha audioread itself only provides a Python interface; it does not bundle audio decoding libraries. You *must* install external system libraries like GStreamer, FFmpeg, or libmad for it to function. Without them, `audioread.NoBackendError` will be raised.
- gotcha The `f` object (from `audioread.audio_open`) yields raw `bytes` objects representing audio frames. These are not automatically decoded into numerical arrays (e.g., NumPy) by audioread. Users must handle the byte-to-array conversion themselves if higher-level processing is needed.
- gotcha The `duration` attribute is often an estimate, especially for VBR (Variable Bit Rate) compressed formats, and may not be perfectly accurate. Seeking operations (if the backend supports them) might also not be sample-accurate or might be slow for certain formats/backends.
- breaking As of version 3.0.0, audioread officially supports Python 3.9 and newer. Older Python 2.x or earlier Python 3.x versions are no longer supported, and attempting to use modern `audioread` versions on an incompatible Python environment will lead to installation failures or runtime errors.
Install
-
pip install audioread
Imports
- audio_open
import audioread audio_open = audioread.audio_open
- NoBackendError
from audioread import NoBackendError
Quickstart
import audioread
import os
# This quickstart assumes an audio file named 'test.mp3' exists in the current directory.
# Replace 'test.mp3' with the path to your actual audio file.
# For a real application, ensure the file exists and is accessible.
audio_file_path = 'test.mp3'
try:
with audioread.audio_open(audio_file_path) as f:
print(f"Input file: {f.name}")
print(f"Channels: {f.channels}")
print(f"Samplerate: {f.samplerate} Hz")
print(f"Duration: {f.duration:.2f} seconds")
print("\nReading frames (raw audio data):")
# Read and discard frames for demonstration.
# In a real application, 'buf' would be processed (e.g., written to another format, analyzed).
frame_count = 0
for buf in f:
frame_count += 1
if frame_count % 100 == 0:
print(f" ...processed {frame_count} frames, last buffer size: {len(buf)} bytes")
print(f"Finished processing. Total frames read: {frame_count}")
except audioread.NoBackendError:
print("\nERROR: No audio backend found.")
print("audioread requires external libraries like GStreamer, FFmpeg, or libmad.")
print("Please install one of these system-wide (e.g., 'brew install ffmpeg' or 'apt-get install gstreamer1.0-plugins-base').")
except FileNotFoundError:
print(f"\nERROR: Audio file '{audio_file_path}' not found.")
print("Please ensure the file exists or update the 'audio_file_path' variable.")
except Exception as e:
print(f"\nAn unexpected error occurred: {e}")