samplerate.converters – Sample rate converters

Converter types

class samplerate.converters.ConverterType

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

linear = <ConverterType.linear: 4>
property name
sinc_best = <ConverterType.sinc_best: 0>
sinc_fastest = <ConverterType.sinc_fastest: 2>
sinc_medium = <ConverterType.sinc_medium: 1>
property value
zero_order_hold = <ConverterType.zero_order_hold: 3>

Sample rate converters

Simple

samplerate.converters.resample(input: Annotated[numpy.typing.ArrayLike, numpy.float32], ratio: SupportsFloat, converter_type: object = 'sinc_best', verbose: bool = False) numpy.typing.NDArray[numpy.float32]

Resample the signal in input_data at once.

Parameters:
  • 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, str, or int) – Sample rate converter (default: sinc_best).

  • 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

Resampler.

Parameters:
  • converter_type (ConverterType, str, or int) – Sample rate converter (default: sinc_best).

  • num_channels (int) – Number of channels.

property channels

Number of channels.

clone(self: samplerate.converters.Resampler) samplerate.converters.Resampler

Creates a copy of the resampler object with the same internal state.

property converter_type

Converter type.

process(self: samplerate.converters.Resampler, input: Annotated[numpy.typing.ArrayLike, numpy.float32], ratio: SupportsFloat, end_of_input: bool = False) numpy.typing.NDArray[numpy.float32]

Resample the signal in input_data.

Parameters:
  • 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.

Returns:

output_data – Resampled input data.

Return type:

ndarray

reset(self: samplerate.converters.Resampler) None

Reset internal state.

set_ratio(self: samplerate.converters.Resampler, arg0: SupportsFloat) None

Set a new conversion ratio immediately.

Callback API

class samplerate.converters.CallbackResampler

CallbackResampler.

Parameters:
  • 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, str, or int) – Sample rate converter.

  • channels (int) – Number of channels.

property channels

Number of channels.

clone(self: samplerate.converters.CallbackResampler) samplerate.converters.CallbackResampler

Create a copy of the resampler object.

property converter_type

Converter type.

property ratio

Conversion ratio = output sample rate / input sample rate.

read(self: samplerate.converters.CallbackResampler, num_frames: SupportsInt) numpy.typing.NDArray[numpy.float32]

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(self: samplerate.converters.CallbackResampler) None

Reset state.

set_starting_ratio(self: samplerate.converters.CallbackResampler, arg0: SupportsFloat) None

Set the starting conversion ratio for the next read call.