DSPark 1.8.0
Header-only C++20 DSP for real-time and offline audio
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).
 
std::vector< T > processRange (const T *input, int inputLength, int64_t firstOutput, int64_t count)
 Offline, time-aligned conversion of any span of output samples.
 
int64_t getReach () const noexcept
 How far, in output samples, the kernel reaches before an input sample and after it: processRange() over [-getReach(), ceil(inputLength * getRatio()) + getReach()) holds the whole converted signal.
 
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 getFilterLength () const noexcept
 Kernel length in input samples (1 at a ratio of exactly 1).
 
int getLatency () const noexcept
 Returns the streaming latency in output samples.
 

Static Public Attributes

static constexpr int kMaxTaps = 8192
 Longest kernel, in input samples (see the class note).
 

Detailed Description

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

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

Quality tiers are filter specifications; the kernel length follows from them and from the ratio (a downsampler's kernel is longer by the ratio, since its band edge is lower relative to the input rate):

Quality Passband edge Stopband Taps, 44.1 <-> 48 kHz
Draft 0.80 Nyquist 60 dB 38 / 40
Normal 0.90 Nyquist 100 dB 130 / 140
High 0.91 Nyquist 140 dB 206 / 224
Ultra 0.915 Nyquist 210 dB 332 / 362

"Nyquist" is the lower of the two rates' Nyquist frequencies; Ultra's passband reaches 20.2 kHz at 44.1 kHz. The float instantiation is limited by float arithmetic to roughly -140 dB; the double one reaches the table. Kernels are capped at kMaxTaps taps, which binds only for downsampling beyond about 25:1 at Ultra: past it the transition band widens.

Template Parameters
TSample type (float or double).

Definition at line 85 of file Resampler.h.

Member Enumeration Documentation

◆ Quality

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

Passband 0.80 of Nyquist, 60 dB stopband: previews.

Normal 

Passband 0.90 of Nyquist, 100 dB stopband.

High 

Passband 0.91 of Nyquist (20 kHz at 44.1), 140 dB stopband.

Ultra 

Passband 0.915 of Nyquist, 210 dB stopband: mastering.

Definition at line 88 of file Resampler.h.

Member Function Documentation

◆ getFilterLength()

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

Kernel length in input samples (1 at a ratio of exactly 1).

Definition at line 315 of file Resampler.h.

◆ 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 half the kernel length in input samples – the 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 325 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 303 of file Resampler.h.

◆ getRatio()

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

Returns the conversion ratio (targetRate / sourceRate).

Definition at line 312 of file Resampler.h.

◆ getReach()

template<typename T >
int64_t dspark::Resampler< T >::getReach ( ) const
inlinenoexcept

How far, in output samples, the kernel reaches before an input sample and after it: processRange() over [-getReach(), ceil(inputLength * getRatio()) + getReach()) holds the whole converted signal.

Definition at line 249 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.
qualityFilter specification (default: Normal).

Definition at line 124 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.
qualityFilter specification (default: Normal).

Definition at line 108 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 158 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 276 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 262 of file Resampler.h.

◆ processRange()

template<typename T >
std::vector< T > dspark::Resampler< T >::processRange ( const T *  input,
int  inputLength,
int64_t  firstOutput,
int64_t  count 
)
inline

Offline, time-aligned conversion of any span of output samples.

Output sample k, for k in [firstOutput, firstOutput + count), is the input interpolated at position k / getRatio(), with the input taken as zero outside [0, inputLength). Unlike process(), which stops at the input's last sample, the span may start before 0 and run past the end, so it can hold the kernel's whole response to the signal: the ringing before its first sample and after its last. For an impulse response, whose first sample is often its loudest, that ringing is part of the converted response. getReach() gives the span that holds all of it. Allocates the output vector – offline use only.

Definition at line 208 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(): it only fills storage that prepare() already sized and never allocates.

Definition at line 140 of file Resampler.h.

Member Data Documentation

◆ kMaxTaps

template<typename T >
constexpr int dspark::Resampler< T >::kMaxTaps = 8192
staticconstexpr

Longest kernel, in input samples (see the class note).

Definition at line 97 of file Resampler.h.


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