Overview of libsamplerate
master0.2.2.repository·master·Indexed 20 days ago
https://github.com/libsndfile/libsamplerateA specialized library for high-quality sample rate conversion of audio data. Version 0.2.2 provides multiple conversion algorithms ranging from high-quality sinc-based interpolators to high-speed linear converters. It features a full API for streaming data via the SRC_DATA struct, a callback-based API for custom data retrieval, and tools for generating filter coefficients using GNU Octave.
0.2.2.The SRC_DATA struct is the primary mechanism for passing audio buffers and control parameters to the sample rate converter in both simple and full-featured API modes.
| Field | Type | Description |
|---|---|---|
data_in | const float * | Pointer to the input audio data array. |
data_out | float * | Pointer to the array where the converter will write output data. |
input_frames | long | Number of input frames provided in data_in. |
output_frames | long | Maximum number of output frames the data_out array can hold. |
input_frames_used | long | [Output] Set by the converter; number of frames actually consumed from data_in. |
output_frames_gen | long | [Output] Set by the converter; number of frames actually generated in data_out. |
end_of_input | int | Set to 1 if this is the last buffer; otherwise 0. Used with src_process. |
src_ratio | double | The conversion ratio (output_rate / input_rate). Supports time-varying conversion via linear interpolation between calls. |
data_in and data_out must point to a single array of interleaved data. The input_frames and output_frames values should be the number of frames (total samples divided by the number of channels), not the total number of samples.input_frames_used and output_frames_gen after a call to src_process to know how much data was actually processed.typedef struct
{
const float *data_in;
float *data_out ;
long input_frames, output_frames ;
long input_frames_used, output_frames_gen ;
int end_of_input ;
double src_ratio ;
} SRC_DATA ;Converters like SRC_SINC_* introduce a transport delay. For example, if you input 1000 samples with a 2x upsampling ratio, the first call to src_process() might return fewer than 2000 samples (e.g., 1900) due to this delay. Subsequent calls will typically return the expected ratio.
Best Practice: To ensure you always receive the required number of output samples, always supply more input samples than are strictly necessary. Always track the number of input frames used via the return values of src_process() to manage your buffers correctly.
The output of libsamplerate may contain peak values that exceed the input range, even if the input is strictly within (-1.0, 1.0). This occurs because interpolation (e.g., upsampling) can create values larger than the original samples.
Action: Always normalize the output of libsamplerate before saving it to fixed-point formats like 16-bit WAV files to prevent clipping.
When implementing the src_callback_t function for the callback API, follow these rules:
long (*src_callback_t) (void *cb_data, float **data).data parameter is a pointer to a pointer to floats. You should populate the memory pointed to by *data with your input samples.0 signals to the converter that no more input data is available.typedef long (*src_callback_t) (void *cb_data, float **data) ;Regardless of which interface you choose (Simple, Full, or Callback), the following components are shared across the library:
libsamplerate provides three distinct interfaces depending on your application's requirements. All three methods support multi-channel interleaved data and operate on buffers of ISO C Standard float data.
When evaluating the performance of a Sample Rate Converter (SRC), three primary metrics are used to determine quality:
The src_ratio field uses double-precision floating point numbers. For long-running conversions, the precision of this ratio is sufficient to prevent cumulative errors.
Even for extremely long durations (e.g., over 1,400 years at 96kHz), the error introduced by the floating-point ratio is significantly smaller than the inaccuracies found in standard hardware crystal oscillators used for sampling.
The src_simple() function is designed for processing a whole audio file at once.
Do not use src_simple() for:
Instead, use:
src_process() API.You can join the Secret Rabbit Code (libsamplerate) mailing lists to receive announcements or participate in general discussions.
src-announce@mega-nerd.com.src@mega-nerd.com. Note that posting to this list is restricted to subscribers.To subscribe, send an email to:
- src-announce-request@mega-nerd.com (Announcements)
- src-request@mega-nerd.com (General list)
With the subject: subscribeIf you intend to build the examples and tests provided with libsamplerate, you must install the following dependencies:
libsndfilefftw3The recommended method for installation on Windows is using the Vcpkg package manager.
# Example using vcpkg (commands vary by installation)
vcpkg install libsndfile fftw3