soxr
soxr is a high-quality, one-dimensional sample-rate conversion library for Python, leveraging libsoxr. It is currently at version 1.0.0, which signifies API stability and is actively maintained with regular updates and performance improvements, including significant speed optimizations.
Warnings
- breaking Version 0.5.0 removed deprecated implicit conversions within the `ResampleStream` class, requiring explicit type handling or conversion before processing. This affects code directly using `ResampleStream` from versions prior to 0.5.0.
- breaking Version 0.4.0 dropped support for Python versions older than 3.9 and introduced compatibility with NumPy 2. Projects targeting older Python versions (e.g., 3.8 or earlier) or NumPy 1 will encounter errors.
- gotcha Version 1.0.0 introduces support for free-threaded Python, but the release notes explicitly state it is 'not battle-tested yet'. Users deploying `soxr` in free-threaded environments should be aware of potential instabilities.
Install
-
pip install soxr
Imports
- soxr
import soxr
Quickstart
import soxr
import numpy as np
# Example: Resample stereo audio from 48 kHz to 44.1 kHz
input_sample_rate = 48000
output_sample_rate = 44100
duration_seconds = 1
num_samples_in = int(input_sample_rate * duration_seconds)
# Generate dummy stereo audio data (e.g., sine wave)
# Use float32, as it's common for audio processing
t = np.linspace(0, duration_seconds, num_samples_in, endpoint=False)
input_audio = np.array([
np.sin(2 * np.pi * 440 * t), # Left channel
np.sin(2 * np.pi * 660 * t) # Right channel
], dtype=np.float32).T # Transpose to (samples, channels) for soxr
# Perform resampling
output_audio = soxr.resample(input_audio, input_sample_rate, output_sample_rate)
print(f"Input audio shape: {input_audio.shape}") # (samples_in, channels)
print(f"Output audio shape: {output_audio.shape}") # (samples_out, channels)
print(f"Output sample rate: {output_sample_rate} Hz")