High-quality Audio Sample Rate Conversion

0.2.4 · active · verified Wed Apr 15

Samplerate is a Python wrapper for Erik de Castro Lopo's `libsamplerate` (also known as Secret Rabbit Code), providing high-quality sample rate conversion for audio data in NumPy arrays. It implements all three APIs available in `libsamplerate`: Simple API, Full API, and Callback API. The library is currently at version 0.2.4 and maintains an active development status with regular releases.

Warnings

Install

Imports

Quickstart

This example demonstrates both the Simple API for single-call resampling and the Full API for chunk-based processing, using a synthesized sine wave. It also includes an assertion to show that both APIs yield the same result for a complete signal.

import numpy as np
import samplerate

# Synthesize data
fs = 1000.0
t = np.arange(fs * 2) / fs
input_data = np.sin(2 * np.pi * 5 * t).astype(np.float32)

# Simple API
ratio = 1.5
converter = 'sinc_best'
output_data_simple = samplerate.resample(input_data, ratio, converter)

print(f"Original shape: {input_data.shape}, Resampled shape (Simple API): {output_data_simple.shape}")

# Full API
resampler = samplerate.Resampler(converter, channels=1)
output_data_full = resampler.process(input_data, ratio, end_of_input=True)

print(f"Resampled shape (Full API): {output_data_full.shape}")

assert np.allclose(output_data_simple, output_data_full)

view raw JSON →