python-soundfile

Read and write WAV, FLAC, OGG, and other audio formats as NumPy arrays via a libsndfile CFFI binding.

Library
PyPI
v0.14.0
861 stars
BSD 3-Clause License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
56 /100 Fair
Development Activity 32
Maintenance 36
Community 68
Maturity 60
Momentum 28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
73 /100 Good
Architecture 74
Code Quality 78
Innovation 55
Learning Curve 85

soundfile is a thin, well-established Python wrapper around the libsndfile C library, giving Python code direct access to dozens of sampled audio formats — WAV, FLAC, OGG, AIFF, MAT, RAW, and more — through a small, consistent API. Rather than reimplementing codec logic, it delegates decoding and encoding entirely to libsndfile (accessed via CFFI) and represents every block of audio as a NumPy array, so the data slots straight into the rest of the Python scientific stack (NumPy, SciPy, PyTorch, librosa) with no extra conversion step.

The module exposes three complementary entry points: module-level read()/write() for one-shot conversions, blocks() for streaming a large file in fixed-size (optionally overlapping) chunks without loading it entirely into memory, and a SoundFile object for stateful, seekable, frame-indexed access with an explicit close() and context-manager support. It also supports virtual/in-memory I/O — reading and writing through any file-like object, including io.BytesIO, so audio can be transcoded entirely in memory without touching disk.

Because libsndfile is bundled as a precompiled native library in the platform wheels (Windows, macOS Intel/ARM, Linux x86_64/AARCH64), most installs need nothing beyond pip install soundfile — no separate system package, no compiler. Errors from the underlying C library surface as a dedicated LibsndfileError carrying the original libsndfile error code and message, distinct from the ValueError/TypeError raised for API misuse, which keeps failure handling precise instead of collapsing every problem into a generic exception.

What You Get

  • One-call read()/write() functions that convert between audio files and NumPy arrays in a single line
  • Block-wise streaming via blocks() for processing files larger than memory, with configurable block size and overlap
  • A stateful SoundFile object with frame-indexed seek/read/write and context-manager support for precise, low-level control
  • Virtual I/O support for reading and writing through file-like objects (e.g. io.BytesIO, HTTP response streams) with no disk access required
  • Bitrate-mode and compression-level control for lossy formats like MP3 and Vorbis
  • Precompiled libsndfile bundled in platform wheels for Windows, macOS, and Linux, so most installs require no system library or compiler

Common Use Cases

  • Loading and saving audio datasets for machine-learning and signal-processing pipelines built on NumPy/PyTorch
  • Converting between audio containers (e.g. WAV to FLAC, or OGG to WAV) entirely in memory or on disk
  • Streaming very large audio files in fixed-size blocks to compute statistics (RMS level, spectral features) without exhausting memory
  • Reading audio fetched over HTTP directly into memory via virtual I/O, without writing temporary files
  • Building lightweight audio-processing scripts and command-line tools without hand-writing format-specific decoders

Under The Hood

Architecture The entire library lives in a single flat module, soundfile.py, which sits directly on top of a CFFI-generated _soundfile extension built by soundfile_build.py from a hand-written C declaration set (ffibuilder.cdef(...)) mirroring libsndfile’s public API (format/subtype enums, SF_INFO, open/read/write/seek calls). Three access patterns are layered over that binding: module-level read()/write()/info() convenience functions built on top of a SoundFile context-manager class, and a generator-based blocks() function for chunked streaming — all funneling through the same underlying SoundFile object, so there is a single source of truth for file state (mode, position, format) and no duplicated I/O logic between the high- and low-level APIs.

Tech Stack Pure Python 3.10+ with two runtime dependencies: cffi (the C foreign-function interface used to call libsndfile) and numpy (the array representation for all audio data), plus typing-extensions for Self typing on older interpreters. There is no web framework, ORM, or database layer — this is a systems-binding library. Packaging is handled by classic setuptools/setup.py with a custom bdist_wheel subclass that tags wheels as platform-specific-but-Python-independent (py2.py3-none-<platform>), and precompiled libsndfile binaries are vendored per-platform in _soundfile_data/ and bundled into wheels via MANIFEST.in/package_data.

Code Quality Tests live in tests/test_soundfile.py (over 1,200 lines) plus a dedicated test_argspec.py that asserts the keyword-argument defaults of sf.read, sf.write, and SoundFile.__init__ never drift out of sync with each other — a deliberate guard against a real class of regression in a library with three overlapping entry points. Error handling is layered and explicit: API misuse raises standard ValueError/TypeError, while failures reported by the C library raise a dedicated LibsndfileError (a SoundFileRuntimeError subclass) carrying the raw libsndfile error code and message, so callers can distinguish “you used the API wrong” from “the underlying codec failed.” Modern type hints (TypeAlias, Literal, generic NumPy array types) are used throughout the public signatures, and a pyrightconfig.json indicates the project is checked with a static type checker. No CI workflow badge appears in the README, but GitHub Actions config is present under .github/.

API Design The public surface is deliberately small and consistent: three ways to access audio (read/write for simplicity, blocks for streaming, SoundFile for control) share the same keyword arguments and defaults, verified by the dedicated argspec test. Getting started requires a single import and a single function call (sf.read('file.wav')), with sensible defaults (auto-detected format, float64 NumPy output) that only need overriding for RAW files or explicit dtype/subtype control. The README documents block processing, virtual I/O, in-memory transcoding, RAW-file handling, and thread-safety guarantees in worked code examples, which meaningfully lowers the barrier for common tasks beyond the basic read/write path.

Used by 9 apps in this directory

TypeScript
54%
MIT

Anarlog

AI Assistants · Note Taking · Productivity

9,404

Anarlog is an open-source, local-first AI meeting notetaker that records, transcribes, and summarizes meetings entirely on your device — no cloud lock-in, no mandatory account, and every note saved as a plain markdown file you own forever.

View details
86
Repo Health
77
Technical
68
Dependency
Built with
TypeScript 54%
Rust 34%
Updated 1 weeks ago
Python
97%
MIT

auto-news

AI Assistants · Productivity

908

An AI-powered personal news aggregator that filters multi-source feeds through LLMs and delivers curated, noise-free summaries to your Notion workspace.

View details
43
Repo Health
53
Technical
66
Dependency
Built with
Python 97%
Updated 1 years ago
Python
83%
MIT

Gemma Multimodal Fine-Tuner

AI Development

1,503

An Apple-Silicon-native LoRA fine-tuning tool for Gemma on text, image, and audio data — with a wizard CLI, live browser-based training visualizer, and streaming from GCS/BigQuery for datasets too large for local disk.

View details
65
Repo Health
68
Technical
72
Dependency
Built with
Python 83%
Updated 2 weeks ago
Python
82%
MIT

LiteLLM

AI Development · Developer Tools

59,745

Open source AI gateway and Python SDK that gives you one OpenAI-compatible interface to call 100+ LLM providers, with built-in routing, cost tracking, guardrails, and virtual keys.

View details
92
Repo Health
81
Technical
69
Dependency
Built with
Python 82%
TypeScript 12%
Updated 1 weeks ago
Python
37%
Other

Open WebUI

AI Agents · AI Assistants

153,390

The extensible, privacy-first AI platform that runs Ollama, OpenAI, and any LLM backend behind a polished, feature-packed web interface.

View details
91
Repo Health
75
Technical
66
Dependency
Built with
Python 37%
Svelte 34%
JavaScript 21%
Updated 1 weeks ago
Python
67%
Apache 2.0

SurfSense

AI Assistants · Search

16,270

The open-source, unlimited NotebookLM alternative with real-time collaboration, a desktop app, and no vendor lock-in.

View details
87
Repo Health
71
Technical
66
Dependency
Built with
Python 67%
TypeScript 30%
Updated 2 weeks ago
Python
73%
Apache 2.0

Unsloth

AI Assistants · AI Development

76,886

Run and fine-tune LLMs, diffusion, audio and embedding models on your own hardware, from a native desktop app, a browser UI, or a Python library.

View details
89
Repo Health
83
Technical
70
Dependency
Built with
Python 73%
TypeScript 21%
Updated 1 weeks ago
TypeScript
53%
MIT

Voicebox

AI Development · Productivity · Voice AI

55,864

Clone voices, dictate anywhere, and give AI agents your voice — all locally.

View details
79
Repo Health
76
Technical
68
Dependency
Built with
TypeScript 53%
Python 35%
Updated 2 months ago
Python
56%
AGPL 3.0

VoiceStudio

Mcp · Music Audio · Voice AI

51,962

Open-source, fully local ElevenLabs alternative for voice cloning, voice design, video dubbing, dictation, transcription and audiobooks, with a local API and MCP server for agents.

View details
85
Repo Health
83
Technical
70
Dependency
Built with
Python 56%
JavaScript 23%
TypeScript 19%
Updated 1 weeks ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers