sound_rs v0.8.0

09 · Reference

API reference

Functions for files and arrays, plus persistent Player and Recorder classes. Library failures use sound_rs.SoundRsError; Python argument conversion can raise TypeError or OverflowError. Every file-path argument accepts str or os.PathLike[str], such as pathlib.Path; non-UTF-8 path bytes raise SoundRsError(code="usage").

Module

sound_rs.__version__ — the installed version as a string, for example "0.2.1".

class sound_rs.SoundRsError(RuntimeError) — library failures carry a stable .code; see error codes.

play_audio

sound_rs.play_audio(file_path: str | os.PathLike[str], device: str | None = None) -> None

Plays a file and blocks until it ends, releasing the GIL while waiting for playback. Opens the output device for this call; startup cost depends on the backend and device. Use Player for repeated playback.

Raises SoundRsError for an unreadable file, a missing device, or a device that disconnects mid-playback.

import sound_rs

sound_rs.play_audio("music.flac")
sound_rs.play_audio("music.flac", device="MacBook Pro Speakers")

is_supported_format

sound_rs.is_supported_format(file_path: str | os.PathLike[str]) -> bool

Probes the container header. Does not decode or verify codec support and packet integrity. A missing or unrecognized file returns False. Probe time depends on the container and storage.

get_audio_info

sound_rs.get_audio_info(file_path: str | os.PathLike[str]) -> tuple[int, int, float]

Returns (sample_rate, channels, duration_seconds). Uses the header when complete; missing rate or channel information can require decoding a packet.

If the header has no frame count, the file is decoded to measure duration. The returned duration is a number; this function does not return None.

probe

sound_rs.probe(file_path: str | os.PathLike[str]) -> dict[str, object]

Returns metadata without decoding the whole file: file, sample_rate, channels, duration_seconds, frames, codec and bit_depth. Fields the container does not provide are None; missing rate/channel information can require one packet. This is the agent-friendly alternative to the get_audio_info tuple.

info = sound_rs.probe("voice.m4a")
print(info["sample_rate"], info["channels"], info["codec"])

load

sound_rs.load(file_path: str | os.PathLike[str], sample_rate: int | None = None,
              mono: bool = False, offset: float = 0.0,
              duration: float | None = None) -> tuple[numpy.ndarray, int]

Decodes to float32 with shape (frames, channels), returning the array and rate. Optional sample_rate resamples, mono folds channels, and offset/duration select a window in seconds. Times must be finite and non-negative; a requested rate must be positive. Requires NumPy. Memory includes the returned audio plus decoder and conversion/resampling temporaries.

Raises SoundRsError for an unreadable file or when the build has no NumPy support.

offset and duration are finite, non-negative seconds. duration=0 returns an empty array. A requested sample_rate must be positive; invalid options raise SoundRsError(code="usage") before file access. mono=True averages channels before resampling.

load_many

sound_rs.load_many(paths: Sequence[str | os.PathLike[str]], threads: int | None = None,
                   sample_rate: int | None = None, mono: bool = False,
                   on_error: str = "raise")
    -> list[tuple[numpy.ndarray, int] | None]

Decodes in parallel with a Rayon pool, releasing the GIL. Order matches paths. threads=None or threads=0 uses the automatic worker count. With on_error="raise" the first failure in input order raises; with on_error="skip" that slot is None and the rest of the batch still decodes. Any other value raises SoundRsError(code="usage") before decoding. A requested sample rate must be positive, including for an empty batch. Optional rate and mono conversion apply to every file; the whole batch is retained in memory.

clips = sound_rs.load_many(["a.flac", "b.flac"], threads=2, on_error="skip")
good = [clip for clip in clips if clip is not None]

play_array

sound_rs.play_array(array: numpy.ndarray, sample_rate: int, layout: str = "channels") -> None

Plays a float32 array: (frames,) for mono or, by default, (channels, frames) for multichannel. Pass layout="frames" for (frames, channels) — the layout load returns. Converts to the device layout and resamples if the rate differs, then blocks until playback ends.

Raises TypeError for an unsupported dtype/dimensionality, SoundRsError(code="usage") for invalid rates, channel counts or non-finite samples, and SoundRsError(code="device") for device failures.

play_sine_wave

sound_rs.play_sine_wave(frequency: float, duration: float, sample_rate: int) -> None

Generates a mono sine tone at half scale and plays it. Useful for checking that audio output works at all. Frequency and duration must be finite and non-negative, and the sample rate positive; invalid values raise SoundRsError(code="usage") before a device opens.

sound_rs.play_sine_wave(440.0, 1.0, 44100)

cli

sound_rs.cli() -> int

Entry point for the soundrs console script; reads sys.argv and returns an exit code. You do not call it from Python — see Command line.

write

sound_rs.write(file_path: str | os.PathLike[str], audio, sample_rate: int,
               subtype: str = "pcm16") -> None

Writes float32 audio to a WAV file. The array is (frames,) for mono or (frames, channels) — the same layout load returns, so sound_rs.write(path, *sound_rs.load(src)) round-trips. Samples outside −1…1 are clipped. Subtypes: pcm16, pcm24, float32. Samples must be finite and the rate positive. See Writing WAV files for memory and RIFF limits.

Only .wav is accepted so far; any other extension raises SoundRsError with code="usage". Returns nothing.

The sample rate and channel count must be positive and fit the WAV header. NaN and infinity, incomplete frames, and files too large for classic RIFF are rejected with code="usage" before opening the destination. A file-write failure has code="io". Encoding and disk I/O release the GIL after copying the array. Read-only and strided float32 arrays are accepted; float64 remains a TypeError.

audio, rate = sound_rs.load("take.flac")
sound_rs.write("take.wav", audio, rate, subtype="pcm24")

write_bytes

sound_rs.write_bytes(audio, sample_rate: int, subtype: str = "pcm16") -> bytes

Encodes the same (frames,) or (frames, channels) float32 input as write and returns the complete WAV file as bytes. Nothing is written to disk. Validation and error codes match write; encoding releases the GIL. Useful for HTTP responses, object storage and tests.

raw = sound_rs.write_bytes(audio, rate, subtype="pcm24")
open("take.wav", "wb").write(raw)

load_bytes

sound_rs.load_bytes(data: bytes) -> tuple[numpy.ndarray, int]

Decodes bytes to a float32 (frames, channels) array and its sample rate. AAC-in-MP4 priming is read from the buffer's edit list. MP3 uses the same Symphonia path as a full-file load. Malformed data can raise SoundRsError(code="format"); an unexpected EOF may yield a decodable prefix.

list_devices

sound_rs.list_devices() -> list[tuple[int, str, bool]]

Output devices as (index, name, is_default). The index is only for display — pass the name to play_audio or Player.

Recorder

sound_rs.Recorder(device: str | None = None)

Opens an input device. Raises SoundRsError with code="device" when there is no microphone or the OS denies access.

MemberReturnsNotes
start()NoneStarts capturing
stop()(ndarray, rate)Stops and returns what was captured
record(seconds)(ndarray, rate)start + wait + stop
record_to(path, seconds, subtype="pcm16")NoneCapture in memory, then write a WAV; path accepts str or os.PathLike, validated with the subtype before capture; also limited to 0…3600 seconds
device / sample_rate / channelsstr / int / intDevice properties

list_input_devices

sound_rs.list_input_devices() -> list[tuple[int, str, bool]]

Input devices as (index, name, is_default). The name is what Recorder(device=...) accepts.

Decoder

sound_rs.Decoder(file_path, block_frames: int = 48000,
                 sample_rate: int | None = None, mono: bool = False)

Incremental decoder for files larger than memory. Returns float32 (frames, channels) blocks, with the same channel/delay handling as load. sample_rate resamples with retained filter state and mono folds channels. Paths accept str or os.PathLike.

MemberReturnsNotes
sample_rateintOutput rate after optional resampling
channelsintOutput channel count; AAC is primed so this is known before the first read
durationfloat | NoneContainer duration when available
block_framesintDefault block size used by iteration and read()
read(frames=None)ndarrayUp to frames; empty (0, channels) at EOF
close()NoneReleases the decoder; later reads raise usage

Iteration returns blocks until EOF. The object is a context manager. For AAC-in-MP4, the streaming path can trim tail padding that a whole-file load may retain.

with sound_rs.Decoder("long.flac", block_frames=4800, sample_rate=16000, mono=True) as d:
    for block in d:
        process(block)          # (4800, 1) float32 until the final partial block

Player

sound_rs.Player(device: str | None = None)

Opens one output stream and keeps it open. Device name matching is exact; the default device is used when omitted. Create, use and destroy it on the same thread. A with block stops playback on exit; the device stream closes when the object is destroyed.

Methods

SignatureReturnsNotes
play_file(path)NoneProbes the header, then streams from disk and returns before playback finishes; path accepts str or os.PathLike
play_array(array, rate, layout="channels")None1D or 2D float32; layout="frames" for (frames, channels)
play_samples(samples, src_channels, sample_rate)NoneInterleaved Python sequence, converted to float32; no NumPy required
pause()NonePosition freezes
resume()NoneContinues an active source; a finished feed requires begin_feed again
stop()NoneClears source/queue and resets position; keeps device open
seek(seconds)NoneFinite file/array target, clamped to known duration; preserves pause
wait()NoneWaits for completion/drain or returns while paused/stopped; GIL released
enqueue(path)NoneRequires active file source; up to 128 waiting entries; path accepts str or os.PathLike
clear_queue()NoneDrops everything queued
begin_feed(rate)NoneSwitches to samples pushed from Python
feed(array)NoneRequires begin_feed; blocks on full ring, times out after five seconds without progress
end_feed()NoneNo more samples; finishes when the ring drains

play_samples is an existing Python-exposed method. Supply complete interleaved frames: [left0, right0, left1, right1] with src_channels=2. It copies the input and prepares it for the device, then returns while playback continues. Prefer play_array for NumPy arrays.

Properties

NameTypeNotes
devicestrName the player opened
sample_rateintDevice rate
channelsintDevice channel count
volumefloatRead/write, clamped to 0…4
positionfloatSeconds played
durationfloat | NoneSeconds; None until known
is_playingboolTrue while a source is active and not paused; False once stopped or finished
pausedboolRead-only; True after pause() until resume(), and False after stop()
queuelist[str]Paths waiting behind the current track
underrunsintRing-starvation callbacks after audio started; excludes silence from lock contention
latencyfloat | NoneLast device latency in milliseconds
import sound_rs

with sound_rs.Player() as player:
    player.play_file("track.flac")
    while player.position < 10:
        pass                      # do work while it plays
    player.seek(120.0)
    player.wait()

Exceptions

Raised byWhen
SoundRsErrorUnreadable file, no output device, device disconnected, stalled stream, no NumPy support
TypeErrorAn array that is not a 1D/2D float32 ndarray
OverflowErrorPython integer outside a Rust argument's representable range

SoundRsError subclasses RuntimeError, so existing except RuntimeError handlers keep working.