Scroll to navigation

PYTHON-SAMPLERATE(1) python-samplerate PYTHON-SAMPLERATE(1)

NAME

python-samplerate - python-samplerate Documentation [image]
<https://pypi.python.org/pypi/samplerate>[image]
<https://pypi.python.org/pypi/samplerate>[image]
<https://pypi.python.org/pypi/samplerate>[image]
<https://pypi.python.org/pypi/samplerate>[image: Documentation Status] [image]
<http://python-samplerate.readthedocs.io/en/latest/?badge=latest>

This is a wrapper around Erik de Castro Lopo's libsamplerate <http://www.mega-nerd.com/libsamplerate/> (aka Secret Rabbit Code) for high-quality sample rate conversion.

It implements all three APIs <http://www.mega-nerd.com/libsamplerate/api.html> available in libsamplerate <http://www.mega-nerd.com/libsamplerate/>:

  • Simple API: for resampling a large chunk of data with a single library call
  • Full API: for obtaining the resampled signal from successive chunks of data
  • Callback API: like Full API, but input samples are provided by a callback function

The libsamplerate <http://www.mega-nerd.com/libsamplerate/> library is statically built together with the python bindings using pybind11 <https://github.com/pybind/pybind11/>.

INSTALLATION

$ pip install samplerate


Binary wheels of libsamplerate <http://www.mega-nerd.com/libsamplerate/> for macOS and Windows (64 bit) are available. For other systems, a C++ 14 or above compiler is required to build the package.

USAGE

import numpy as np
import samplerate
# Synthesize data
fs = 1000.
t = np.arange(fs * 2) / fs
input_data = np.sin(2 * np.pi * 5 * t)
# Simple API
ratio = 1.5
converter = 'sinc_best'  # or 'sinc_fastest', ...
output_data_simple = samplerate.resample(input_data, ratio, converter)
# Full API
resampler = samplerate.Resampler(converter, channels=1)
output_data_full = resampler.process(input_data, ratio, end_of_input=True)
# The result is the same for both APIs.
assert np.allclose(output_data_simple, output_data_full)
# See `samplerate.CallbackResampler` for the Callback API, or
# `examples/play_modulation.py` for an example.


See samplerate.resample, samplerate.Resampler, and samplerate.CallbackResampler in the API documentation for details.

SEE ALSO


LICENSE

This project is licensed under the MIT license <https://opensource.org/licenses/MIT>.

As of version 0.1.9, libsamplerate <http://www.mega-nerd.com/libsamplerate/> is licensed under the 2-clause BSD license <https://opensource.org/licenses/BSD-2-Clause>.

API DOCUMENTATION

samplerate module documentation

samplerate -- Python bindings for libsamplerate based on CFFI and NumPy

Simple API

See samplerate.converters.resample() <#samplerate.converters.resample>.

Full API

See samplerate.converters.Resampler() <#samplerate.converters.Resampler>.

Callback API

See samplerate.converters.CallbackResampler() <#samplerate.converters.CallbackResampler>.

Exceptions

See samplerate.converters.CallbackResampler() <#samplerate.converters.CallbackResampler>.

samplerate.converters -- Sample rate converters

Converter types

Enum of samplerate converter types.

Pass any of the members, or their string or value representation, as converter_type in the resamplers.



Members:

sinc_best

sinc_medium

sinc_fastest

zero_order_hold

linear











Sample rate converters

Simple

Resample the signal in input_data at once.
  • input_data (ndarray) -- Input data. Input data with one or more channels is represented as a 2D array of shape (num_frames, num_channels). A single channel can be provided as a 1D array of num_frames length. For use with libsamplerate, input_data is converted to 32-bit float and C (row-major) memory order.
  • ratio (float) -- Conversion ratio = output sample rate / input sample rate.
  • converter_type (ConverterType <#samplerate.converters.ConverterType>, str, or int) -- Sample rate converter (default: sinc_best).
  • verbose (bool) -- If True, print additional information about the conversion.

output_data -- Resampled input data.
ndarray

Note:

If samples are to be processed in chunks, Resampler and CallbackResampler will provide better results and allow for variable conversion ratios.



Full API

Resampler.
  • converter_type (ConverterType <#samplerate.converters.ConverterType>, str, or int) -- Sample rate converter (default: sinc_best).
  • num_channels (int) -- Number of channels.


Number of channels.


Converter type.

Resample the signal in input_data.
  • input_data (ndarray) -- Input data. Input data with one or more channels is represented as a 2D array of shape (num_frames, num_channels). A single channel can be provided as a 1D array of num_frames length. For use with libsamplerate, input_data is converted to 32-bit float and C (row-major) memory order.
  • ratio (float) -- Conversion ratio = output sample rate / input sample rate.
  • end_of_input (int) -- Set to True if no more data is available, or to False otherwise.
  • verbose (bool) -- If True, print additional information about the conversion.

output_data -- Resampled input data.
ndarray





Callback API

CallbackResampler.
  • callback (function) -- Function that returns new frames on each call, or None otherwise. Input data with one or more channels is represented as a 2D array of shape (num_frames, num_channels). A single channel can be provided as a 1D array of num_frames length. For use with libsamplerate, input_data is converted to 32-bit float and C (row-major) memory order.
  • ratio (float) -- Conversion ratio = output sample rate / input sample rate.
  • converter_type (ConverterType <#samplerate.converters.ConverterType>, str, or int) -- Sample rate converter.
  • channels (int) -- Number of channels.


Number of channels.


Converter type.

Conversion ratio = output sample rate / input sample rate.

Read a number of frames from the resampler.
num_frames (int) -- Number of frames to read.
output_data -- Resampled frames as a (num_output_frames, num_channels) or (num_output_frames,) array. Note that this may return fewer frames than requested, for example when no more input is available.
ndarray





samplerate.exceptions -- Sample rate exceptions


CHANGE LOG

Change Log

0.1.0

Initial release.

INDICES AND TABLES

  • Index <>
  • Module Index <>
  • Search Page <>

Author

Tino Wagner

Copyright

2017, Tino Wagner

March 8, 2026 0.0.0