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.
pip install samplerateVerified import paths — ran on the pinned version, not inferred.
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.
Ensure `libsamplerate` is installed via your system's package manager on Linux: `sudo apt-get install libsamplerate0` (Debian/Ubuntu) or `sudo pacman -S libsamplerate` (Arch).
Convert your input NumPy array to `np.float32` using `array.astype(np.float32)` and ensure it's C-contiguous.
Ensure you are installing `samplerate` (the package described here) and not `scikits.samplerate` if you intend to use the Full or Callback APIs or benefit from the `pybind11` implementation.
Install the library using pip: `pip install samplerate`
Install the `libsamplerate` development package for your operating system (e.g., `sudo apt-get install libsamplerate0-dev` on Debian/Ubuntu, `brew install libsamplerate` on macOS, `yum install libsamplerate-devel` on Fedora/RHEL).
Ensure the input data is a NumPy array and convert it to `float32` or `float64` before passing it to `samplerate` functions (e.g., `input_data.astype(numpy.float32)`).
Provide a positive floating-point number for `samplerate_ratio` representing the ratio of the output sample rate to the input sample rate.
Use one of the valid converter type strings, which can be listed by `samplerate.available_converters()`. Common valid types include `'sinc_fastest'`, `'sinc_medium'`, `'sinc_best'`, `'zero_order_hold'`, or `'linear'`.