Sounddevice
Sounddevice is a Python library that provides a high-level API to play and record sound, built upon the PortAudio library. It allows for easy integration with NumPy for handling audio data. The current version is 0.5.5, and it maintains a relatively active release cadence with frequent patch and minor updates.
Warnings
- gotcha The `sounddevice` library requires the system-level PortAudio library, which is not automatically installed by `pip`. Missing PortAudio often leads to `PortAudioError` during runtime or installation failures.
- breaking Beginning with version 0.5.0, ASIO support was removed from the bundled Windows DLLs. Users requiring ASIO on Windows must now explicitly enable it by setting the `SD_ENABLE_ASIO` environment variable and ensure an ASIO-enabled PortAudio DLL is available.
- gotcha When using `sd.play()` or `sd.rec()` in a non-blocking context (e.g., a script that immediately exits), the audio playback/recording might stop prematurely. These functions initiate the audio stream but don't wait for completion by default.
- gotcha Audio data passed to `sounddevice` (especially NumPy arrays) must have an appropriate `dtype` (e.g., `float32`, `int16`). Incorrect data types can lead to `ValueError` or distorted audio during playback or recording.
- breaking Since version 0.4.5, `sounddevice` requires Python 3.7 or newer. Older Python versions are no longer supported and will lead to installation errors or `Requires-Python` conflicts during `pip` installation.
Install
-
pip install sounddevice
Imports
- sounddevice
import sounddevice as sd
Quickstart
import sounddevice as sd
import numpy as np
import time
samplerate = 44100 # samples per second
duration = 2.5 # seconds
frequency = 440 # Hz (A4 note)
# Generate a sine wave
t = np.linspace(0., duration, int(samplerate * duration), endpoint=False)
amplitude = 0.5 # To avoid clipping
data = amplitude * np.sin(2 * np.pi * frequency * t)
# Ensure data is float32, as recommended by sounddevice
data = data.astype(np.float32)
print(f"Playing a {frequency} Hz sine wave for {duration} seconds...")
sd.play(data, samplerate)
sd.wait() # Wait until playback is finished
print("Playback finished.")
# Example of recording (e.g., 2 seconds of stereo audio)
print("Recording 2 seconds of stereo audio...")
recording = sd.rec(int(2 * samplerate), samplerate=samplerate, channels=2, dtype='float32')
sd.wait() # Wait until recording is finished
print("Recording finished.")
print(f"Recorded data shape: {recording.shape}")