DSPark 1.6.1
Header-only audio DSP framework in pure C++20 — zero dependencies
Loading...
Searching...
No Matches
dspark::Resampler< T > Class Template Reference

Windowed-sinc sample rate converter optimized for real-time DSP. More...

#include <Resampler.h>

Public Types

enum class  Quality { Draft , Normal , High , Ultra }
 

Public Member Functions

void prepare (double sourceRate, double targetRate, Quality quality=Quality::Normal)
 Prepares the resampler for a given rate conversion.
 
void prepare (const AudioSpec &spec, double targetRate, Quality quality=Quality::Normal)
 Prepares the resampler using AudioSpec (unified API).
 
void reset () noexcept
 Resets the internal state (delay lines, all channels) to zero.
 
std::vector< T > process (const T *input, int inputLength)
 Resamples an entire buffer (offline batch processing).
 
int processBlock (const T *input, int inputLength, T *output) noexcept
 Resamples a block of audio in single-channel streaming mode.
 
int processBlock (AudioBufferView< T > input, AudioBufferView< T > output) noexcept
 Resamples multi-channel audio using AudioBufferView (streaming).
 
int getMaxOutputSamples (int inputLength) const noexcept
 Returns the maximum number of output samples for a given input length.
 
double getRatio () const noexcept
 Returns the conversion ratio (targetRate / sourceRate).
 
int getLatency () const noexcept
 Returns the streaming latency in output samples.
 

Detailed Description

template<typename T>
class dspark::Resampler< T >

Windowed-sinc sample rate converter optimized for real-time DSP.

Quality tiers trade CPU for image/alias rejection and passband flatness. Measured on the float instantiation (each column states its test):

Quality Taps 20 kHz image (48->96) 26 kHz alias (96->44.1) 20 kHz gain (44.1->48)
Draft 8 -12 dB -10 dB -3.3 dB
Normal 32 -56 dB -27 dB -0.6 dB
High 64 -140 dB -69 dB -0.02 dB
Ultra 128 -139 dB -143 dB flat

The middle column probes the transition band right at the target Nyquist: only Ultra's kernel is long enough to keep it in the deep stopband. For extreme downsampling ratios (beyond ~8:1) no fixed tap count can hold the anti-alias transition band; cascade two resamplers instead.

Template Parameters
TSample type (float or double).

Definition at line 68 of file Resampler.h.

Member Enumeration Documentation

◆ Quality

template<typename T >
enum class dspark::Resampler::Quality
strong
Enumerator
Draft 

8-point sinc, fastest (previews; HF response drops early)

Normal 

32-point sinc, balanced (~-56 dB worst-case images)

High 

64-point sinc, high quality (~-140 dB beyond the transition band)

Ultra 

128-point sinc, mastering grade (deep stopband even at the band edge)

Definition at line 71 of file Resampler.h.

Member Function Documentation

◆ getLatency()

template<typename T >
int dspark::Resampler< T >::getLatency ( ) const
inlinenoexcept

Returns the streaming latency in output samples.

The streaming path (processBlock) delays the signal by exactly sincPoints/2 input samples – the sinc kernel's group delay – which this getter reports rounded to the nearest output sample. The offline process() path is already time-aligned and has zero latency.

Definition at line 246 of file Resampler.h.

◆ getMaxOutputSamples()

template<typename T >
int dspark::Resampler< T >::getMaxOutputSamples ( int  inputLength) const
inlinenoexcept

Returns the maximum number of output samples for a given input length.

Parameters
inputLengthNumber of input samples.
Returns
Maximum possible output samples.

Definition at line 227 of file Resampler.h.

◆ getRatio()

template<typename T >
double dspark::Resampler< T >::getRatio ( ) const
inlinenoexcept

Returns the conversion ratio (targetRate / sourceRate).

Definition at line 236 of file Resampler.h.

◆ prepare() [1/2]

template<typename T >
void dspark::Resampler< T >::prepare ( const AudioSpec spec,
double  targetRate,
Quality  quality = Quality::Normal 
)
inline

Prepares the resampler using AudioSpec (unified API).

Pre-allocates multi-channel states. MUST be called outside the audio thread.

Parameters
specAudio environment (sampleRate, numChannels used).
targetRateTarget sample rate in Hz.
qualityInterpolation quality (default: Normal).

Definition at line 114 of file Resampler.h.

◆ prepare() [2/2]

template<typename T >
void dspark::Resampler< T >::prepare ( double  sourceRate,
double  targetRate,
Quality  quality = Quality::Normal 
)
inline

Prepares the resampler for a given rate conversion.

Allocates internal buffers. MUST be called outside the audio thread.

Parameters
sourceRateSource sample rate in Hz.
targetRateTarget sample rate in Hz.
qualityInterpolation quality (default: Normal).

Definition at line 88 of file Resampler.h.

◆ process()

template<typename T >
std::vector< T > dspark::Resampler< T >::process ( const T *  input,
int  inputLength 
)
inline

Resamples an entire buffer (offline batch processing).

Stateless and time-aligned: output sample k interpolates the input at position k / getRatio() exactly (no latency; the buffer edges are zero-padded). Allocates the output vector – offline use only.

Parameters
inputSource audio samples.
inputLengthNumber of input samples.
Returns
Vector of resampled output samples (ceil(inputLength * ratio)).

Definition at line 149 of file Resampler.h.

◆ processBlock() [1/2]

template<typename T >
int dspark::Resampler< T >::processBlock ( AudioBufferView< T >  input,
AudioBufferView< T >  output 
)
inlinenoexcept

Resamples multi-channel audio using AudioBufferView (streaming).

Processes each channel sequentially with its independent state.

Parameters
inputInput audio buffer view.
outputOutput audio buffer view (pre-allocated).
Returns
Number of output samples produced per channel.

Definition at line 200 of file Resampler.h.

◆ processBlock() [2/2]

template<typename T >
int dspark::Resampler< T >::processBlock ( const T *  input,
int  inputLength,
T *  output 
)
inlinenoexcept

Resamples a block of audio in single-channel streaming mode.

Parameters
inputInput audio samples.
inputLengthNumber of input samples.
outputOutput buffer (must hold at least getMaxOutputSamples()).
Returns
Number of output samples produced.

Definition at line 186 of file Resampler.h.

◆ reset()

template<typename T >
void dspark::Resampler< T >::reset ( )
inlinenoexcept

Resets the internal state (delay lines, all channels) to zero.

Safe to call from the audio thread after prepare(): the assigns below only refill storage that prepare() already sized (no reallocation).

Definition at line 127 of file Resampler.h.


The documentation for this class was generated from the following file: