bastibe/python-soundfile

SoundFile is an audio library based on libsndfile, CFFI, and NumPy

One-sentence summary

python-soundfile is a Python library that reads and writes sound files (like WAV, FLAC, OGG, MP3) by wrapping the C library libsndfile, with audio data handled as NumPy arrays.

Responsibilities / What it does

  • Read & write many sampled sound file formats through libsndfile — the README lists WAV, FLAC, OGG, MAT, and tells you MP3 support was added in 0.11.0, plus ALAC in 0.9.0.
  • Represent audio as NumPy arrays — read() returns (data, samplerate) and write() takes (file, data, samplerate).
  • Block processing — soundfile.blocks() reads a file in short, optionally overlapping blocks (e.g., to compute per-block RMS).
  • SoundFile objects — open a file with sf.SoundFile(...) for fine-grained control: seek, tell, read, write, frames, channels, sample rate; supports context manager (with) and explicit .close().
  • RAW files — since auto-detection can't identify raw data, you must pass channels, samplerate, and subtype (and optionally endian, defaulting to LITTLE on x86).
  • Virtual IO — read from an open file-like object (a real file, an HTTP response via BytesIO, or any in-memory buffer). You can even convert OGG to WAV entirely in memory without touching disk.
  • Compression controls — for supported formats (example given: MP3), you can set compression_level (float 0 to 1, 1 = highest compression) and bitrate_mode ('VARIABLE', 'CONSTANT', or 'AVERAGE').

Architecture summary

  • Built on libsndfile (a free, cross-platform, open-source LGPL C library) accessed through CFFI (Python's foreign-function interface for calling C code).
  • The Python code itself contains no compiled code; the wheel may optionally bundle a copy of libsndfile.
  • Audio data lives in NumPy arrays (NumPy is a required dependency as of 0.13.0).

Installation

  • Typical modern install: pip install soundfile — on Windows (64/32/ARM64), macOS (Intel/ARM), and Linux (64/AARCH64) this also installs a bundled libsndfile.
  • If you install from source instead, you must install libsndfile yourself (e.g., sudo apt install libsndfile1).
  • If on an unusual platform or old Python, you may need to install NumPy and CFFI separately (e.g., via Anaconda).
  • Since 0.12.0, the bundled libsndfile is tried before a system-installed one; to prefer the system's, install the source package/wheel.

Loading order and bundled library preference

  • The module tries the packaged libsndfile first, then falls back to a system one.
  • To force system libsndfile, install the source distribution instead of the platform wheel.

Thread safety

  • python-soundfile mirrors the C library's guarantees.
  • Safe: opening and reading the same file concurrently through separate per-thread handles.
  • Not safe: sharing a single handle between threads, or concurrently writing to the same file — you may get garbage results or crashes.
  • The README suggests looking at the Free-threaded Python guide for synchronization advice.

Known limitations / known issues (from the README)

  • OGG writing: with certain libsndfile versions, writing OGG files can produce empty files (tracked in their issue tracker).
  • Buildroot-style systems: Python may fail to locate libsndfile.so, so the module won't load; the README suggests a workaround in soundfile.py (remove _find_library, hardcode the library path).
  • Small inconsistencies in history: e.g., function argument order changed in 0.8.0, the import name changed from pysoundfile to soundfile in 0.7.0, and the default of always_2d changed from True to False in 0.8.0.

Brief history / notable changes (README's News section)

  • 2013 v0.1–0.5: initial prototype; added Windows binary installer, context manager, virtual IO, seeking/flush.
  • 2015–2017 v0.6–0.10: renamed to soundfile; float64 default; Numpy made optional; added buffer_read/write, info(); MP3 support and macOS M1 wheels in 0.11.0.
  • 2022–2023 v0.11–0.12.1: MP3, binary wheels for macOS M1 and Linux x86_64, libsndfile updates, prefers packaged libsndfile.
  • 2025 v0.13–0.13.1: libsndfile v1.2.2, Linux arm64 builds, NumPy became a dependency, compression/bitrate controls added, fixed a regression in blocks().
  • 2026 v0.14.0: added type annotations, fixed a race condition when opening files concurrently, removed support for Python ≤ 3.9, added Windows ARM64 support.

Error reporting

  • API usage errors raise standard ValueError or TypeError.
  • Other errors raise SoundFileError (previously RuntimeError).
  • Errors reported by libsndfile itself raise a LibsndfileError subclass, which exposes .code (the internal libsndfile error code) and .error_string (the raw error message).

Example usage (from the README)

import soundfile as sf
data, samplerate = sf.read('existing_file.wav')
sf.write('new_file.flac', data, samplerate)
with sf.SoundFile('myfile.wav', 'r+') as f:
    while f.tell() < f.frames:
        pos = f.tell()
        data = f.read(1024)
        f.seek(pos)
        f.write(data * 2)

Related

  • Project
  • Project
  • Project
  • Project