python-samplerate documentation
TRANSCRIPT
![Page 1: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/1.jpg)
python-samplerate DocumentationRelease 0.1.0+6.ged73d7a
Tino Wagner
Apr 10, 2019
![Page 2: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/2.jpg)
![Page 3: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/3.jpg)
Contents
1 Installation 3
2 Usage 5
3 See also 7
4 License 9
5 API documentation 115.1 samplerate module documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
6 Change Log 176.1 Change Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
7 Indices and tables 19
Python Module Index 21
i
![Page 4: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/4.jpg)
ii
![Page 5: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/5.jpg)
python-samplerate Documentation, Release 0.1.0+6.ged73d7a
This is a wrapper around Erik de Castro Lopo’s libsamplerate (aka Secret Rabbit Code) for high-quality sample rateconversion.
It implements all three APIs available in 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
Library calls to libsamplerate are performed using CFFI.
Contents 1
![Page 6: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/6.jpg)
python-samplerate Documentation, Release 0.1.0+6.ged73d7a
2 Contents
![Page 7: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/7.jpg)
CHAPTER 1
Installation
$ pip install samplerate
Binaries of libsamplerate for macOS and Windows (32 and 64 bit) are included and used if not present on the system.On Linux systems, you should also install libsamplerate0 (Debian derivatives), libsamplerate (Arch), or similar.
3
![Page 8: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/8.jpg)
python-samplerate Documentation, Release 0.1.0+6.ged73d7a
4 Chapter 1. Installation
![Page 9: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/9.jpg)
CHAPTER 2
Usage
import numpy as npimport samplerate
# Synthesize datafs = 1000.t = np.arange(fs * 2) / fsinput_data = np.sin(2 * np.pi * 5 * t)
# Simple APIratio = 1.5converter = 'sinc_best' # or 'sinc_fastest', ...output_data_simple = samplerate.resample(input_data, ratio, converter)
# Full APIresampler = 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 theAPI documentation for details.
5
![Page 10: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/10.jpg)
python-samplerate Documentation, Release 0.1.0+6.ged73d7a
6 Chapter 2. Usage
![Page 11: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/11.jpg)
CHAPTER 3
See also
• scikits.samplerate implements only the Simple API and uses Cython for extern calls. The resample function ofscikits.samplerate and this package share the same function signature for compatiblity.
• resampy: sample rate conversion in Python + Cython.
7
![Page 12: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/12.jpg)
python-samplerate Documentation, Release 0.1.0+6.ged73d7a
8 Chapter 3. See also
![Page 13: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/13.jpg)
CHAPTER 4
License
This project is licensed under the MIT license.
As of version 0.1.9, libsamplerate is licensed under the 2-clause BSD license.
9
![Page 14: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/14.jpg)
python-samplerate Documentation, Release 0.1.0+6.ged73d7a
10 Chapter 4. License
![Page 15: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/15.jpg)
CHAPTER 5
API documentation
5.1 samplerate module documentation
5.1.1 samplerate – Python bindings for libsamplerate based on CFFI and NumPy
Simple API
samplerate.resample()See samplerate.converters.resample().
Full API
class samplerate.ResamplerSee samplerate.converters.Resampler().
Callback API
class samplerate.CallbackResamplerSee samplerate.converters.CallbackResampler().
Exceptions
class samplerate.ResamplingErrorSee samplerate.converters.CallbackResampler().
5.1.2 samplerate.converters – Sample rate converters
11
![Page 16: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/16.jpg)
python-samplerate Documentation, Release 0.1.0+6.ged73d7a
Converter types
class samplerate.converters.ConverterTypeEnum 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)Resample the signal in input_data at once.
Parameters
• input_data (ndarray) – Input data. A single channel is provided as a 1D array ofnum_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-bitfloat 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 resultsand allow for variable conversion ratios.
Full API
class samplerate.converters.Resampler(converter_type=’sinc_fastest’, channels=1)Resampler.
Parameters
• converter_type (ConverterType, str, or int) – Sample rate converter.
• num_channels (int) – Number of channels.
channelsNumber of channels.
12 Chapter 5. API documentation
![Page 17: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/17.jpg)
python-samplerate Documentation, Release 0.1.0+6.ged73d7a
converter_typeConverter type.
process(input_data, ratio, end_of_input=False, verbose=False)Resample the signal in input_data.
Parameters
• input_data (ndarray) – Input data. A single channel is provided as a 1D array ofnum_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()Reset internal state.
set_ratio(new_ratio)Set a new conversion ratio immediately.
Callback API
class samplerate.converters.CallbackResampler(callback, ratio, con-verter_type=’sinc_fastest’, channels=1)
CallbackResampler.
Parameters
• callback (function) – Function that returns new frames on each call, or None other-wise. A single channel is provided as a 1D array of num_frames length. Input data withseveral channels is represented as a 2D array of shape (num_frames, num_channels). Foruse with libsamplerate, input_data is converted to 32-bit float and C (row-major) memoryorder.
• ratio (float) – Conversion ratio = output sample rate / input sample rate.
• converter_type (ConverterType, str, or int) – Sample rate converter.
• channels (int) – Number of channels.
ratioConversion ratio = output sample rate / input sample rate.
read(num_frames)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, forexample when no more input is available.
Return type ndarray
5.1. samplerate module documentation 13
![Page 18: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/18.jpg)
python-samplerate Documentation, Release 0.1.0+6.ged73d7a
reset()Reset state.
set_starting_ratio(ratio)Set the starting conversion ratio for the next read call.
5.1.3 samplerate.exceptions – Sample rate exceptions
class samplerate.exceptions.ResamplingError(error)Runtime exception of libsamplerate
5.1.4 samplerate.lowlevel – Lowlevel wrappers around libsamplerate
Lowlevel wrappers around libsamplerate.
The docstrings of the src_* functions are adapted from the libsamplerate header file.
samplerate.lowlevel.src_callback_new(callback, converter_type, channels)Initialisation for the callback based API.
Parameters
• callback (function) – Called whenever new frames are to be read. Must return aNumPy array of shape (num_frames, channels).
• converter_type (int) – Converter to be used.
• channels (int) – Number of channels.
Returns
• state – An anonymous pointer to the internal state of the converter.
• handle – A CFFI handle to the callback data.
• error (int) – Error code.
samplerate.lowlevel.src_callback_read(state, ratio, frames, data)Read up to frames worth of data using the callback API.
Returns frames – Number of frames read or -1 on error.
Return type int
samplerate.lowlevel.src_delete(state)Release state.
Cleanup all internal allocations.
samplerate.lowlevel.src_error(state)Return an error number.
samplerate.lowlevel.src_get_description(converter_type)Return the description of the converter given by converter_type.
samplerate.lowlevel.src_get_name(converter_type)Return the name of the converter given by converter_type.
samplerate.lowlevel.src_get_version()Return the version string of libsamplerate.
samplerate.lowlevel.src_is_valid_ratio(ratio)Return True if ratio is a valid conversion ratio, False otherwise.
14 Chapter 5. API documentation
![Page 19: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/19.jpg)
python-samplerate Documentation, Release 0.1.0+6.ged73d7a
samplerate.lowlevel.src_new(converter_type, channels)Initialise a new sample rate converter.
Parameters
• converter_type (int) – Converter to be used.
• channels (int) – Number of channels.
Returns
• state – An anonymous pointer to the internal state of the converter.
• error (int) – Error code.
samplerate.lowlevel.src_process(state, input_data, output_data, ratio, end_of_input=0)Standard processing function.
Returns non zero on error.
samplerate.lowlevel.src_reset(state)Reset the internal SRC state.
Does not modify the quality settings. Does not free any memory allocations. Returns non zero on error.
samplerate.lowlevel.src_set_ratio(state, new_ratio)Set a new SRC ratio.
This allows step responses in the conversion ratio. Returns non zero on error.
samplerate.lowlevel.src_simple(input_data, output_data, ratio, converter_type, channels)Perform a single conversion from an input buffer to an output buffer.
Simple interface for performing a single conversion from input buffer to output buffer at a fixed conversion ratio.Simple interface does not require initialisation as it can only operate on a single buffer worth of audio.
samplerate.lowlevel.src_strerror(error)Convert the error number into a string.
5.1. samplerate module documentation 15
![Page 20: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/20.jpg)
python-samplerate Documentation, Release 0.1.0+6.ged73d7a
16 Chapter 5. API documentation
![Page 21: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/21.jpg)
CHAPTER 6
Change Log
6.1 Change Log
6.1.1 0.1.0
• Initial release.
17
![Page 22: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/22.jpg)
python-samplerate Documentation, Release 0.1.0+6.ged73d7a
18 Chapter 6. Change Log
![Page 23: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/23.jpg)
CHAPTER 7
Indices and tables
• genindex
• modindex
• search
19
![Page 24: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/24.jpg)
python-samplerate Documentation, Release 0.1.0+6.ged73d7a
20 Chapter 7. Indices and tables
![Page 25: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/25.jpg)
Python Module Index
ssamplerate, 11samplerate.converters, 11samplerate.exceptions, 14samplerate.lowlevel, 14
21
![Page 26: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/26.jpg)
python-samplerate Documentation, Release 0.1.0+6.ged73d7a
22 Python Module Index
![Page 27: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/27.jpg)
Index
CCallbackResampler (class in sampler-
ate.converters), 13channels (samplerate.converters.Resampler attribute),
12converter_type (samplerate.converters.Resampler
attribute), 12ConverterType (class in samplerate.converters), 12
Llinear (samplerate.converters.ConverterType at-
tribute), 12
Pprocess() (samplerate.converters.Resampler
method), 13
Rratio (samplerate.converters.CallbackResampler at-
tribute), 13read() (samplerate.converters.CallbackResampler
method), 13resample() (in module samplerate.converters), 12Resampler (class in samplerate.converters), 12ResamplingError (class in samplerate.exceptions),
14reset() (samplerate.converters.CallbackResampler
method), 13reset() (samplerate.converters.Resampler method),
13
Ssamplerate (module), 11samplerate.CallbackResampler (class in sam-
plerate), 11samplerate.converters (module), 11samplerate.exceptions (module), 14samplerate.lowlevel (module), 14
samplerate.resample() (in module samplerate),11
samplerate.Resampler (class in samplerate), 11samplerate.ResamplingError (class in sampler-
ate), 11set_ratio() (samplerate.converters.Resampler
method), 13set_starting_ratio() (sampler-
ate.converters.CallbackResampler method),14
sinc_best (samplerate.converters.ConverterType at-tribute), 12
sinc_fastest (samplerate.converters.ConverterTypeattribute), 12
sinc_medium (samplerate.converters.ConverterTypeattribute), 12
src_callback_new() (in module sampler-ate.lowlevel), 14
src_callback_read() (in module sampler-ate.lowlevel), 14
src_delete() (in module samplerate.lowlevel), 14src_error() (in module samplerate.lowlevel), 14src_get_description() (in module sampler-
ate.lowlevel), 14src_get_name() (in module samplerate.lowlevel), 14src_get_version() (in module sampler-
ate.lowlevel), 14src_is_valid_ratio() (in module sampler-
ate.lowlevel), 14src_new() (in module samplerate.lowlevel), 14src_process() (in module samplerate.lowlevel), 15src_reset() (in module samplerate.lowlevel), 15src_set_ratio() (in module samplerate.lowlevel),
15src_simple() (in module samplerate.lowlevel), 15src_strerror() (in module samplerate.lowlevel), 15
Zzero_order_hold (sampler-
ate.converters.ConverterType attribute),
23
![Page 28: python-samplerate Documentation](https://reader030.vdocuments.us/reader030/viewer/2022012715/61ad3c301ccf232a761e7a54/html5/thumbnails/28.jpg)
python-samplerate Documentation, Release 0.1.0+6.ged73d7a
12
24 Index