samplerate.converters – Sample rate converters

Converter types

class samplerate.converters.ConverterType[source]

Enum of samplerate converter types.

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

linear = 4
sinc_best = 0
sinc_fastest = 2
sinc_medium = 1
zero_order_hold = 3

Sample rate converters

Simple

samplerate.converters.resample(input_data, ratio, converter_type='sinc_best', verbose=False)[source]

Resample the signal in input_data at once.

Parameters:
  • input_data (ndarray) – Input data. A single channel is provided as a 1D array of num_frames length. Input data with several channels is represented as a 2D array of shape (num_frames, num_channels). 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, str, or int) – Sample rate converter.
  • verbose (bool) – If True, print additional information about the conversion.
Returns:

output_data – Resampled input data.

Return type:

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

class samplerate.converters.Resampler(converter_type='sinc_fastest', channels=1)[source]

Resampler.

Parameters:
  • converter_type (ConverterType, str, or int) – Sample rate converter.
  • num_channels (int) – Number of channels.
channels

Number of channels.

converter_type

Converter type.

process(input_data, ratio, end_of_input=False, verbose=False)[source]

Resample the signal in input_data.

Parameters:
  • input_data (ndarray) – Input data. A single channel is provided as a 1D array of num_frames length. Input data with several channels is represented as a 2D array of shape (num_frames, num_channels). 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.
Returns:

output_data – Resampled input data.

Return type:

ndarray

reset()[source]

Reset internal state.

set_ratio(new_ratio)[source]

Set a new conversion ratio immediately.

Callback API

class samplerate.converters.CallbackResampler(callback, ratio, converter_type='sinc_fastest', channels=1)[source]

CallbackResampler.

Parameters:
  • callback (function) – Function that returns new frames on each call, or None otherwise. A single channel is provided as a 1D array of num_frames length. Input data with several channels is represented as a 2D array of shape (num_frames, num_channels). 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, str, or int) – Sample rate converter.
  • channels (int) – Number of channels.
ratio

Conversion ratio = output sample rate / input sample rate.

read(num_frames)[source]

Read a number of frames from the resampler.

Parameters:num_frames (int) – Number of frames to read.
Returns: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.
Return type:ndarray
reset()[source]

Reset state.

set_starting_ratio(ratio)[source]

Set the starting conversion ratio for the next read call.