|
DSPark 1.8.0
Header-only C++20 DSP for real-time and offline audio
|
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). | |
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.
| T | Sample type (float or double). |
Definition at line 85 of file Resampler.h.
|
strong |
Definition at line 88 of file Resampler.h.
|
inlinenoexcept |
Kernel length in input samples (1 at a ratio of exactly 1).
Definition at line 315 of file Resampler.h.
|
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.
|
inlinenoexcept |
Returns the maximum number of output samples for a given input length.
| inputLength | Number of input samples. |
Definition at line 303 of file Resampler.h.
|
inlinenoexcept |
Returns the conversion ratio (targetRate / sourceRate).
Definition at line 312 of file Resampler.h.
|
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.
|
inline |
Prepares the resampler using AudioSpec (unified API).
Pre-allocates multi-channel states. MUST be called outside the audio thread.
| spec | Audio environment (sampleRate, numChannels used). |
| targetRate | Target sample rate in Hz. |
| quality | Filter specification (default: Normal). |
Definition at line 124 of file Resampler.h.
|
inline |
Prepares the resampler for a given rate conversion.
Allocates internal buffers. MUST be called outside the audio thread.
| sourceRate | Source sample rate in Hz. |
| targetRate | Target sample rate in Hz. |
| quality | Filter specification (default: Normal). |
Definition at line 108 of file Resampler.h.
|
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.
| input | Source audio samples. |
| inputLength | Number of input samples. |
Definition at line 158 of file Resampler.h.
|
inlinenoexcept |
Resamples multi-channel audio using AudioBufferView (streaming).
Processes each channel sequentially with its independent state.
| input | Input audio buffer view. |
| output | Output audio buffer view (pre-allocated). |
Definition at line 276 of file Resampler.h.
|
inlinenoexcept |
Resamples a block of audio in single-channel streaming mode.
| input | Input audio samples. |
| inputLength | Number of input samples. |
| output | Output buffer (must hold at least getMaxOutputSamples()). |
Definition at line 262 of file Resampler.h.
|
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.
|
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.
|
staticconstexpr |
Longest kernel, in input samples (see the class note).
Definition at line 97 of file Resampler.h.