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
849stars
BSD 3-Clause License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
66/100Good
Development Activity56
Maintenance52
Community68
Maturity60
Momentum28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
73/100Good
Architecture74
Code Quality78
Innovation55
Learning Curve85

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 7 apps in this directory

TypeScript
53%
MIT

Anarlog

Note Taking · AI Assistants · Productivity

9,192

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
69
Dependency
Built with
TypeScript53%
Rust36%
Updated today
Python
97%
MIT

auto-news

AI Assistants · Productivity

906

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
Python97%
Updated 1 years ago
Python
91%
MIT

Gemma Multimodal Fine-Tuner

AI Development

1,498

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
49
Repo Health
68
Technical
74
Dependency
Built with
Python91%
Updated 2 weeks ago
Python
84%
MIT

LiteLLM

AI Development · Developer Tools

57,577

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
70
Dependency
Built with
Python84%
TypeScript13%
Updated today
Python
37%
Other

Open WebUI

AI Assistants · AI Agents

150,393

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
67
Dependency
Built with
Python37%
Svelte34%
JavaScript21%
Updated yesterday
Python
70%
Apache 2.0

SurfSense

Search · AI Assistants

16,032

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

View details
88
Repo Health
71
Technical
66
Dependency
Built with
Python70%
TypeScript28%
Updated yesterday
TypeScript
53%
MIT

Voicebox

AI Development · Productivity

51,846

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

View details
83
Repo Health
76
Technical
68
Dependency
Built with
TypeScript53%
Python35%
Updated 3 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

Search