sound_rs v0.2.1

08 · Guides

Coming from another library

Most Python audio code is three lines. Here is what those three lines become.

From sounddevice

# before
import sounddevice as sd
import soundfile as sf

data, rate = sf.read("track.flac")
sd.play(data, rate)
sd.wait()

# after
import sound_rs

sound_rs.play_audio("track.flac")

You lose the array, which you did not need for playback, and gain no ffmpeg and no per-call device setup. If you do need the samples, sound_rs.load returns them in the same shape as sf.read.

For a persistent stream with pause and seek, sounddevice needs an OutputStream plus a callback queue; sound_rs.Player is that in one object.

From soundfile

# before
import soundfile as sf
data, rate = sf.read("track.m4a", dtype="float32")

# after
import sound_rs
data, rate = sound_rs.load("track.m4a")
AAC and M4Alibsndfile cannot read MP4/AAC. That is the case where this swap is not optional.

From pydub

# before — needs an ffmpeg binary on PATH
from pydub import AudioSegment
from pydub.playback import play

segment = AudioSegment.from_file("track.mp3")
play(segment)

# after
import sound_rs

sound_rs.play_audio("track.mp3")

No subprocess, no temporary WAV, no ffmpeg install step for your users. The trade: pydub also does cutting, concatenation and format conversion, which sound_rs does not.

From playsound / simpleaudio

# before — one shot, no controls, format support varies by platform
from playsound import playsound

playsound("track.mp3")

# after
import sound_rs

sound_rs.play_audio("track.mp3")

Same one-line call, but the format support is the same on every platform and the call releases the GIL.

What you gain, what you give up

sound_rssoundfile + sounddevicepydub
FormatsWAV, FLAC, MP3, OGG, M4AWAV, FLAC, MP3; no AACanything ffmpeg reads
External toolsnonenoneffmpeg binary
Playback without loadingyesnono
Editing (cut, mix, convert)nonoyes
Platforms3, wheelsmanyany with ffmpeg