PYTHON-SAMPLERATE(1)
β¨ NAME β¨
okay bestie, welcome to python-samplerate β like, the manual page for it, PYTHON-SAMPLERATE(1), and yes it's exactly as iconic as it sounds π
This is python-samplerate β python-samplerate Documentation π
Badge corner, because of course we have badges: python-samplerate β python-samplerate Documentation, with the pypi badges pointing at https://pypi.python.org/pypi/samplerate and one of them shout-out-ing Documentation Status, plus a docs badge at http://python-samplerate.readthedocs.io/en/latest/?badge=latest β¨
So what even is this? It's a wrapper around Erik de Castro Lopo's libsamplerate (http://www.mega-nerd.com/libsamplerate/) β aka Secret Rabbit Code π° β and it does high-quality sample rate conversion. No cap.
It implements all three of the APIs that live at http://www.mega-nerd.com/libsamplerate/api.html inside libsamplerate (http://www.mega-nerd.com/libsamplerate/):
- Simple API β for resampling a big ol' chunk of data with a single library call. one and done, we love her π«Ά
- Full API β for getting the resampled signal out of successive chunks of data. it's giving "streaming, but make it chic"
- Callback API β same energy as Full API, except the input samples get handed to you by a callback function π
And the tea: 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 πΈ
literally one line, bestie:
$ pip install samplerate
Binary wheels of libsamplerate (http://www.mega-nerd.com/libsamplerate/) are available for:
- macOS (
x86_64,arm64) - Linux (
x86_64,aarch64) - Windows (
x86_64)
Building from source on other platforms requires a C++14 or later compiler. yeah. a whole compiler. it's giving "bring your own toolchain" π
π USAGE π
here's the vibe, copy-paste ready:
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.
so like, ratio = 1.5, converter is 'sinc_best' (or 'sinc_fastest', or one of the others β the ... is doing so much heavy lifting there π« ), and both APIs genuinely land on the same answer thanks to that assert np.allclose(...). main character energy from a test assertion, honestly.
For the full rundown, go look at samplerate.resample, samplerate.Resampler, and samplerate.CallbackResampler in the API documentation. details live there π
π¦ SEE ALSO π¦
scikits.samplerate(https://pypi.python.org/pypi/scikits.samplerate) β this one only implements the Simple API and usesCython(http://cython.org/) for extern calls. fun fact: theresamplefunction ofscikits.samplerateand this package share the same function signature for compatiblity (yes, that spelling, we keep it π ). so if you're migrating, the signature is a handshake, not a goodbye π«Άresampy(https://github.com/bmcfee/resampy) β sample rate conversion in Python + Cython. another icon in the resampling cinematic universe β
π LICENSE π
This project is licensed under the MIT license (https://opensource.org/licenses/MIT) π
And plot twist for the history girlies: 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) β¨
ngl bestie, license lore is real lore. version 0.1.9 is the divider line. remember it.
β¨ API DOCUMENTATION β¨
πΈ samplerate module documentation
samplerate β Python bindings for libsamplerate based on CFFI and NumPy. that's the whole vibe bestie, that's what we're working with π
π« Simple API
samplerate.resample()β Seesamplerate.converters.resample().
π« Full API
class samplerate.Resamplerβ Seesamplerate.converters.Resampler().
π« Callback API
class samplerate.CallbackResamplerβ Seesamplerate.converters.CallbackResampler().
π« Exceptions
class samplerate.ResamplingErrorβ Seesamplerate.converters.CallbackResampler().
π¦ samplerate.converters -- Sample rate converters
π Converter types
class samplerate.converters.ConverterType
Enum of samplerate converter types. like, a little menu of converter options, slay.
Pass any of the members, or their string or value representation, as converter_type in the resamplers. so flexible, we love options π
Members:
sinc_bestsinc_mediumsinc_fastestzero_order_holdlinear
and here's the receipts, literally:
linear = <ConverterType.linear: 4>ConverterType.name -> strsinc_best = <ConverterType.sinc_best: 0>sinc_fastest = <ConverterType.sinc_fastest: 2>sinc_medium = <ConverterType.sinc_medium: 1>property valuezero_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]
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. one and done, no chunking, just send it π
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 ofnum_frameslength. For use with libsamplerate,input_datais 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, orint) β Sample rate converter (default:sinc_best).verbose(bool) β IfTrue, print additional information about the conversion.
Returns
output_dataβ Resampled input data.
Return type
ndarray
Note: If samples are to be processed in chunks,ResamplerandCallbackResamplerwill provide better results and allow for variable conversion ratios. so if you're feeding it piece by piece? switch classes, bestie π«Ά
Full API
class samplerate.converters.Resampler
Resampler.
Parameters
converter_type(ConverterType,str, orint) β 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. a whole twin, same state, iconic π―
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 ofnum_frameslength. For use with libsamplerate,input_datais 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 toTrueif no more data is available, or toFalseotherwise.verbose(bool) β IfTrue, print additional information about the conversion.
Returns
output_dataβ Resampled input data.
Return type
ndarray
reset(self: samplerate.converters.Resampler) -> None
Reset internal state. clean slate, we're starting over β¨
set_ratio(self: samplerate.converters.Resampler, arg0: SupportsFloat) -> None
Set a new conversion ratio immediately. no waiting, right now, periodt.
Callback API
class samplerate.converters.CallbackResampler
CallbackResampler.
Parameters
callback(function) β Function that returns new frames on each call, orNoneotherwise. 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 ofnum_frameslength. For use with libsamplerate,input_datais 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, orint) β 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. don't panic if it comes up short, that's just the vibe when the input runs dry π
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.
π samplerate.exceptions -- Sample rate exceptions
class samplerate.exceptions.ResamplingError
π CHANGE LOG
π Change Log
0.1.0
- Initial release.
π INDICES AND TABLES
- Index
- Module Index
- Search Page
π· Author
Tino Wagner
π· Copyright
2017, Tino Wagner
okay so that's the whole page bestie, you made it to the end and honestly? iconic π
this was from version 0.2.4, and the page is dated Aug 16, 2026 (the source line calls itself PYTHON-SAMPLERATE(1), iykyk). go resample something gorgeous π¦β¨
π¦ where this page comes from
- ubuntu stonking: python-samplerate-doc 0.2.4-3
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms