audioread

3.1.0 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates how to open an audio file using `audioread.audio_open`, access its properties (channels, samplerate, duration), and iterate through its raw audio data buffers. It also includes error handling for the common `NoBackendError` (when no system audio decoding library is found) and `FileNotFoundError`.

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}")

view raw JSON →