Resampling in audio programming is the process of changing the sampling rate of an audio signal. The sampling rate is the number of samples of an analog signal that are taken per second to convert it into a digital signal. Resampling can be used to change the playback speed of an audio signal, to match the sampling rate of a different device or application, or to change the pitch of the audio signal.
When resampling, the original audio signal is typically converted from a continuous-time signal to a discrete-time signal by sampling it at a specific rate. The new sampling rate is typically either higher or lower than the original sampling rate. If the new sampling rate is higher, new samples are interpolated between the original samples to create a smoother waveform. If the new sampling rate is lower, some of the original samples are discarded or averaged together to reduce the amount of data.
Resampling can be achieved using a variety of techniques, including linear interpolation, cubic interpolation, and FFT-based techniques. However, resampling can introduce some artifacts, such as aliasing or distortion, especially when the new sampling rate is significantly different from the original sampling rate. To minimize these artifacts, advanced resampling techniques can be used, such as oversampling, noise shaping, or dithering.
Using the Resampler Class
With the help of the
Resampler class we can resample an input buffer of sampling rate N, to an output buffer of sampling rate M.
It can work with both
AudioBuffer and raw pointers with the following characteristics:
- interleaved stereo or mono
- short int or float
Here is an example how to resample an
AudioBuffer with a sample rate of 44.1Khz to 16kHz.
// Create an input and an output buffer
AudioData<float> inputData(constants::MONO, 512);
AudioBuffer<float> inputBuffer(constants::MONO, 512, true, 44100, inputData.getBuffers());
AudioData<float> outputData(constants::MONO, 512);
AudioBuffer<float> outputBuffer(constants::MONO, 512, true, constants::SAMPLE_RATE_UNDEFINED, outputData.getBuffers());
// Create a Resampler instance
// The process function returns the number of frames in the resampled output buffer
const uint numOfOutputSamples = resampler.process(
Read more about the C++ classes in our API reference.