sound_rs v0.4.2

06 · Guides

Formats and gapless

Built-in WAV, FLAC, MP3, Ogg/Vorbis and AAC-in-MP4 decoding, with encoder-delay trimming where metadata is available.

Supported formats

FormatExtensionsDecoderNotes
WAV.wavsymphoniaPCM, including tested 24-bit and multichannel files
FLAC.flacsymphoniaLossless decode; supported codec layouts/rates
MP3.mp3minimp3 / symphoniaFull-file load uses minimp3; bytes/windows/streaming use Symphonia
OGG Vorbis.oggsymphonia—
AAC in MP4.m4a, .mp4symphoniapriming read from the edit list

There is no extension allowlist. Probing recognizes containers; a successful probe does not prove that every codec or packet inside is supported. Raw AAC and other containers need dedicated validation before relying on them.

Gapless

Lossy encoders add silence at both ends — a few thousand samples that are not part of the recording. Left in, every track gains a click of silence and offsets by a millisecond or two. sound_rs trims them:

Historical comparison: a 60-second AAC file decoded to 2 646 016 frames and agreed with ffmpeg to 8·10⁻⁸. AAC can retain less than one codec frame of tail padding; these measurements do not guarantee exact alignment for every file.

import sound_rs, soundfile

ours, _ = sound_rs.load("track.flac")
theirs, _ = soundfile.read("track.flac", dtype="float32")
print(ours.shape == theirs.shape, abs(ours - theirs).max())   # (True, 0.0)
MP3 is offset by 47 samplesThe tested full-file MP3 path starts 47 samples (~1 ms) earlier than libsndfile and ffmpeg. The measured fixture lengths match and samples agree to 1.6·10⁻⁵ once aligned. Bytes, windowed and streamed MP3 use a different decoder; do not assume sample identity or alignment across these paths.

Bit depth

Everything is converted to float32 on decode. 24-bit files keep their resolution; there is no silent truncation to 16-bit anywhere in the path. On output the device decides the sample format — f32, f64, i16, i32, u8 or u16 — and conversion happens in the callback.

When a file will not open

Library failures raise sound_rs.SoundRsError, a RuntimeError subclass. Python argument conversion can instead raise TypeError or OverflowError.

import sound_rs

try:
    sound_rs.play_audio("broken.mp3")
except sound_rs.SoundRsError as error:
    print(error)   # Unsupported or corrupt audio file 'broken.mp3': ...

Decoder errors normally fail the call. Unexpected end-of-file can return a decodable prefix, so a successful decode is not strict proof that the source file was complete.

Checking without decoding

import sound_rs

sound_rs.is_supported_format("maybe.mp3")   # True / False, header only
sound_rs.get_audio_info("maybe.mp3")        # (rate, channels, seconds)

is_supported_format probes without decoding. get_audio_info uses complete header metadata when available, decodes a packet to discover missing format details, or decodes the file to determine an unknown duration. Its cost can grow with file length.