DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
dspark::Oversampling< T, MaxChannels > Class Template Reference

Power-of-two oversampling processor with polyphase anti-aliasing. More...

#include <Oversampling.h>

Public Types

enum class  Quality { Low , Medium , High , Maximum }
 Quality presets defining the steepness and rejection of the anti-aliasing filter. More...
 
enum class  Mode { LinearPhaseFir , PolyphaseIir }
 Half-band filter family (see the file notes). More...
 
enum class  Depth { Preset , Deep }
 Stopband depth of the cascade (see the constructor). More...
 
enum class  Band { Preset , Audible }
 The band the first stage keeps (see the constructor). More...
 

Public Member Functions

 Oversampling (int factor=2, Quality quality=Quality::High, Mode mode=Mode::LinearPhaseFir, Depth depth=Depth::Preset, Band band=Band::Preset)
 Constructs the oversampling engine.
 
void prepare (const AudioSpec &spec)
 Pre-allocates buffers and computes FIR filter coefficients.
 
void reset () noexcept
 Flushes all internal delay lines and history buffers. Call this when resetting the transport or to clear audio tails.
 
int getFactor () const noexcept
 Returns the current oversampling factor.
 
Quality getQuality () const noexcept
 Returns the current quality preset.
 
Mode getMode () const noexcept
 Returns the half-band filter family.
 
Depth getDepth () const noexcept
 Returns the stopband depth (Depth::Preset or Depth::Deep).
 
Band getBand () const noexcept
 Returns the band of the first stage (Band::Preset or Band::Audible).
 
int getStageLength (int stage) const noexcept
 Length of a linear-phase stage's kernel, in taps at its high rate.
 
double getFirstStageTransition () const noexcept
 Transition band of the first stage, as a fraction of the 2x rate.
 
int getLatency () const noexcept
 Round-trip (upsample + downsample) delay at the BASE rate, in samples.
 
double getRoundTripDelay (double frequencyHz) const noexcept
 Exact round-trip delay at a frequency, in base-rate samples.
 
int getDecimationDelay () const noexcept
 Group delay of the decimation path alone, in HIGH-RATE samples.
 
AudioBufferView< T > getHighRateBlock (int numSamples) noexcept
 Writable high-rate block for a source that renders directly at the oversampled rate (a synthesizer, for example).
 
AudioBufferView< T > upsample (AudioBufferView< const T > input) noexcept
 Upsamples the input base-rate signal into the internal high-rate buffer.
 
void downsample (AudioBufferView< T > output) noexcept
 Downsamples the internal high-rate buffer back to base-rate.
 

Static Public Member Functions

static constexpr int factorForRate (double sampleRate, double minimumInternalRate=176400.0, int maxFactor=1<< kMaxStages) noexcept
 The smallest factor that brings a host rate up to an internal rate.
 

Detailed Description

template<typename T, int MaxChannels = 16>
class dspark::Oversampling< T, MaxChannels >

Power-of-two oversampling processor with polyphase anti-aliasing.

Usage is a paired streaming operation per audio callback:

auto up = os.upsample(block); // 1. base rate -> high rate
process(up); // 2. nonlinear processing at the high rate
os.downsample(block); // 3. high rate -> base rate, in place
Note
upsample() and downsample() form a pair: downsample() consumes the high-rate samples the most recent upsample() left in the internal buffer, so call them once each per block, with the same block length.

A source that generates at the high rate (a synthesizer voice bank) skips the upsampling: it fills getHighRateBlock() and calls downsample(), and its delay is getDecimationDelay() high-rate samples.

Threading: prepare() and reset() belong to the setup thread (prepare allocates). upsample() and downsample() belong to the audio thread and are noexcept and allocation-free. There are no runtime setters – factor, quality, mode, depth and band are fixed at construction and all filter state is only touched by the audio thread – so no cross-thread synchronisation is needed.

Template Parameters
TSample type (float or double).
MaxChannelsMaximum number of channels supported.

Definition at line 100 of file Oversampling.h.

Member Enumeration Documentation

◆ Band

template<typename T , int MaxChannels = 16>
enum class dspark::Oversampling::Band
strong

The band the first stage keeps (see the constructor).

  • Preset (default): the quality preset's own transition band, a fixed share of the host rate around its Nyquist frequency (High: from 0.460 to 0.540 of the rate), whatever the rate.
  • Audible: the first stage keeps everything up to 20 kHz and stops everything from the image of 20 kHz (the host rate less 20 kHz) up, to the depth of the preset; what lies between, which nobody hears, is neither kept whole nor stopped. The transition is then 1/2 - 20 kHz / rate of the 2x rate: 0.046 at 44.1 kHz, 0.083 at 48 kHz, 0.292 at 96 kHz and 0.396 at 192 kHz, against a fixed 0.040 for High. prepare() takes the shortest kernel that measures the preset's depth over that stopband, so the stage gets shorter as the rate rises (and where the preset's own band is the wider one, as Low and Medium are at 44.1 kHz, it stays). Later stages keep their designs: they pass 20 kHz and stop its images with room to spare. The latency is known after prepare() and stays a whole number of samples. In Mode::PolyphaseIir the first stage gets the same band and the coefficient count that reaches the depth over it (at 96 kHz High delays 2.27 samples at 1 kHz in place of 3.07).

First stage of High, in taps, at 44.1, 48, 88.2, 96, 176.4 and 192 kHz: 111, 63, 23, 19, 15 and 15 against 127, which at 2x is a latency of 56, 32, 12, 10, 8 and 8 samples against 64. At 96 kHz Low, Medium and Maximum take 7, 15 and 27 taps against 31, 63 and 255, and Depth::Deep 27 against 163. What folds under 20 kHz at 96 kHz measures -82.6 dB or less for High (the preset's first stage: -79.2) and a 19.9 kHz tone comes back within 0.0006 dB.

What Audible gives up: content of the input between 20 kHz and its Nyquist frequency is not carried whole to the high rate, and its images are not removed whole; on the way down, what a process leaves between the Nyquist frequency and the image of 20 kHz folds back above 20 kHz. Nothing folds below 20 kHz that the preset would have stopped. At 96 kHz with High a 30 kHz tone comes back at 0.948 of its level and a 40 kHz one at 0.672, where the preset returns both whole.

Enumerator
Preset 

The preset's transition band (default).

Audible 

The first stage keeps 20 kHz and stops its image, by the host rate.

Definition at line 190 of file Oversampling.h.

◆ Depth

template<typename T , int MaxChannels = 16>
enum class dspark::Oversampling::Depth
strong

Stopband depth of the cascade (see the constructor).

  • Preset (default): the depth of the quality preset, as above.
  • Deep: every stage at least 100 dB deep over the band it protects, with the preset's transition band (the same passband edge and stop edge). A nonlinear process at the high rate leaves harmonics across the whole band above the base Nyquist at close to their full level, and the preset's depth is then the floor of what folds back. Linear-phase FIR taps per stage (first stage first): Low 91/35/23/31, Medium 115/31/23/31, High 163/31/23/31 (with Preset 31/11/7/15, 63/19/15/15 and 127/23/23/15). Round-trip latency at High: 82/90/93/95 base samples at 2x/4x/8x/16x (Preset 64/70/73/74). Maximum already has that depth and is unchanged. In Mode::PolyphaseIir every stage gets the coefficient count that reaches 100 dB over its band (104 dB or deeper for every preset).
Enumerator
Preset 

The quality preset's depth (default).

Deep 

At least 100 dB in every stage, with the preset's band.

Definition at line 146 of file Oversampling.h.

◆ Mode

template<typename T , int MaxChannels = 16>
enum class dspark::Oversampling::Mode
strong

Half-band filter family (see the file notes).

Enumerator
LinearPhaseFir 

Exact integer delay at every frequency (default).

PolyphaseIir 

Elliptic allpass pairs: a few samples of nonlinear-phase delay.

Definition at line 123 of file Oversampling.h.

◆ Quality

template<typename T , int MaxChannels = 16>
enum class dspark::Oversampling::Quality
strong

Quality presets defining the steepness and rejection of the anti-aliasing filter.

The figures are the worst image/alias level of the whole cascade, which the first stage sets (its stop edge); later stages are designed to be no worse over the band that folds back into the first stage's passband and transition. Linear-phase FIR taps of the first stage are 31/63/127/255. In Mode::PolyphaseIir the same transition band gets at least the same depth (worst image or alias over every factor, measured: Low -55, Medium -83, High -91, Maximum -107 dB).

Enumerator
Low 

~-40 dB stopband. For fast previewing.

Medium 

~-60 dB stopband. General purpose.

High 

~-80 dB stopband. Professional standard.

Maximum 

~-100 dB stopband. Mastering grade.

Definition at line 114 of file Oversampling.h.

Constructor & Destructor Documentation

◆ Oversampling()

template<typename T , int MaxChannels = 16>
dspark::Oversampling< T, MaxChannels >::Oversampling ( int  factor = 2,
Quality  quality = Quality::High,
Mode  mode = Mode::LinearPhaseFir,
Depth  depth = Depth::Preset,
Band  band = Band::Preset 
)
inlineexplicit

Constructs the oversampling engine.

Parameters
factorOversampling factor. Must be a power of two (1, 2, 4, 8, 16).
qualityFilter quality preset. Default is High (-80 dB).
modeFilter family. Default is the linear-phase FIR.
depthStopband depth. Default is the preset's (Depth::Preset).
bandBand of the first stage. Default is the preset's (Band::Preset); Band::Audible fits it to the host rate at prepare().
Precondition
factor >= 1 and is a power of 2.
Note
Release builds normalise an invalid factor into range and round it down to a power of two (debug builds assert).

Definition at line 209 of file Oversampling.h.

Member Function Documentation

◆ downsample()

template<typename T , int MaxChannels = 16>
void dspark::Oversampling< T, MaxChannels >::downsample ( AudioBufferView< T >  output)
inlinenoexcept

Downsamples the internal high-rate buffer back to base-rate.

Consumes the high-rate samples written by the most recent upsample() (and modified in place by the caller in between).

Parameters
outputBuffer view where the base-rate samples will be written. Use the same block length as the paired upsample() input.
Note
Real-time safe.

Definition at line 490 of file Oversampling.h.

◆ factorForRate()

template<typename T , int MaxChannels = 16>
static constexpr int dspark::Oversampling< T, MaxChannels >::factorForRate ( double  sampleRate,
double  minimumInternalRate = 176400.0,
int  maxFactor = 1 << kMaxStages 
)
inlinestaticconstexprnoexcept

The smallest factor that brings a host rate up to an internal rate.

A nonlinear stage that always runs at about the same internal rate sounds the same at every host rate and stops paying for oversampling the host already provides: with the default minimum the factor is 4 at 44.1 and 48 kHz, 2 at 88.2 and 96 kHz and 1 from 176.4 kHz up.

Parameters
sampleRateHost rate in Hz. A rate that is not a positive finite number gives 1.
minimumInternalRateInternal rate to reach, in Hz.
maxFactorLargest factor returned (at most 16).
Returns
A power of two between 1 and maxFactor.

Definition at line 303 of file Oversampling.h.

◆ getBand()

template<typename T , int MaxChannels = 16>
Band dspark::Oversampling< T, MaxChannels >::getBand ( ) const
inlinenoexcept

Returns the band of the first stage (Band::Preset or Band::Audible).

Definition at line 329 of file Oversampling.h.

◆ getDecimationDelay()

template<typename T , int MaxChannels = 16>
int dspark::Oversampling< T, MaxChannels >::getDecimationDelay ( ) const
inlinenoexcept

Group delay of the decimation path alone, in HIGH-RATE samples.

The delay of a source rendered directly at the high rate through getHighRateBlock() and downsample(). Linear-phase FIR: every half-band stage delays by its centre tap, halfOrder_s samples at its input rate, which is exact and generally fractional at the base rate (31.5 samples at 2x High); a source that must report an integer base-rate latency L starts its content L * getFactor() minus this value high-rate samples later. Polyphase IIR: the low-frequency delay, rounded (the true delay is fractional and frequency dependent).

Definition at line 405 of file Oversampling.h.

◆ getDepth()

template<typename T , int MaxChannels = 16>
Depth dspark::Oversampling< T, MaxChannels >::getDepth ( ) const
inlinenoexcept

Returns the stopband depth (Depth::Preset or Depth::Deep).

Definition at line 326 of file Oversampling.h.

◆ getFactor()

template<typename T , int MaxChannels = 16>
int dspark::Oversampling< T, MaxChannels >::getFactor ( ) const
inlinenoexcept

Returns the current oversampling factor.

Definition at line 317 of file Oversampling.h.

◆ getFirstStageTransition()

template<typename T , int MaxChannels = 16>
double dspark::Oversampling< T, MaxChannels >::getFirstStageTransition ( ) const
inlinenoexcept

Transition band of the first stage, as a fraction of the 2x rate.

The stage keeps everything under (1/4 - half of this) of the 2x rate and stops everything over (1/4 + half of it): the preset's own figure, or 1/2 - 20 kHz / rate with Band::Audible where that is wider.

Definition at line 355 of file Oversampling.h.

◆ getHighRateBlock()

template<typename T , int MaxChannels = 16>
AudioBufferView< T > dspark::Oversampling< T, MaxChannels >::getHighRateBlock ( int  numSamples)
inlinenoexcept

Writable high-rate block for a source that renders directly at the oversampled rate (a synthesizer, for example).

The decimation-only counterpart of upsample(): no upsampling filter runs. Fill every sample of the returned view, then call downsample() with the same base block length. The path delays by getDecimationDelay() high-rate samples. Real-time safe.

Parameters
numSamplesBase-rate block length, clamped to prepare()'s maxBlockSize.
Returns
View of the internal buffer, numSamples * getFactor() long, with the prepared channel count.

Definition at line 421 of file Oversampling.h.

◆ getLatency()

template<typename T , int MaxChannels = 16>
int dspark::Oversampling< T, MaxChannels >::getLatency ( ) const
inlinenoexcept

Round-trip (upsample + downsample) delay at the BASE rate, in samples.

Linear-phase FIR: exact at every frequency. Each stage s contributes its centre-tap delay on the way up and down, (halfOrder_s + 1) samples at its high rate, i.e. (halfOrder_s + 1) / 2^s base samples; the stage lengths are chosen so every term is an integer. Verified sample-exact against an impulse round trip for every factor and preset.

Polyphase IIR: the low-frequency round-trip delay rounded to the nearest sample. The true delay is fractional and rises toward the band edge; query it with getRoundTripDelay().

Definition at line 370 of file Oversampling.h.

◆ getMode()

template<typename T , int MaxChannels = 16>
Mode dspark::Oversampling< T, MaxChannels >::getMode ( ) const
inlinenoexcept

Returns the half-band filter family.

Definition at line 323 of file Oversampling.h.

◆ getQuality()

template<typename T , int MaxChannels = 16>
Quality dspark::Oversampling< T, MaxChannels >::getQuality ( ) const
inlinenoexcept

Returns the current quality preset.

Definition at line 320 of file Oversampling.h.

◆ getRoundTripDelay()

template<typename T , int MaxChannels = 16>
double dspark::Oversampling< T, MaxChannels >::getRoundTripDelay ( double  frequencyHz) const
inlinenoexcept

Exact round-trip delay at a frequency, in base-rate samples.

Constant (getLatency()) in the linear-phase mode. In Mode::PolyphaseIir it is the phase delay of the elliptic allpass pairs, evaluated from the designed coefficients: for example 3.07 samples at 1 kHz rising to 4.23 samples at 18 kHz for 2x High at 48 kHz. Not real-time (it evaluates the transfer functions); call it from a setup or UI thread.

Parameters
frequencyHzFrequency in Hz, at the base rate of prepare() (48 kHz is assumed before prepare()).

Definition at line 384 of file Oversampling.h.

◆ getStageLength()

template<typename T , int MaxChannels = 16>
int dspark::Oversampling< T, MaxChannels >::getStageLength ( int  stage) const
inlinenoexcept

Length of a linear-phase stage's kernel, in taps at its high rate.

Half of them (the odd ones from the centre) are the half-band's zeros and cost nothing. With Band::Audible the first stage's length is that of the rate of the last prepare().

Parameters
stageStage, 0 the first (the one at the lowest rate).
Returns
The length, or 0 for a stage the engine does not have or in Mode::PolyphaseIir.

Definition at line 342 of file Oversampling.h.

◆ prepare()

template<typename T , int MaxChannels = 16>
void dspark::Oversampling< T, MaxChannels >::prepare ( const AudioSpec &  spec)
inline

Pre-allocates buffers and computes FIR filter coefficients.

Parameters
specThe audio specification at the base (1x) sample rate.
Postcondition
The processor is ready to handle up to spec.maxBlockSize base samples.
Note
With Band::Audible the first stage is fitted to spec.sampleRate here, so getLatency() and getDecimationDelay() are those of this rate from now on (before the first prepare() they are the preset's). The fit tries a few lengths and windows and measures each, which takes longer the longer the stage: for High, 0.2 to 0.4 ms at 96 kHz, 1 to 1.7 ms at 48 kHz and 3 to 7 ms at 44.1 kHz; for Maximum at 44.1 kHz, 5 to 8 ms (six builds).

Definition at line 250 of file Oversampling.h.

◆ reset()

template<typename T , int MaxChannels = 16>
void dspark::Oversampling< T, MaxChannels >::reset ( )
inlinenoexcept

Flushes all internal delay lines and history buffers. Call this when resetting the transport or to clear audio tails.

Definition at line 279 of file Oversampling.h.

◆ upsample()

template<typename T , int MaxChannels = 16>
AudioBufferView< T > dspark::Oversampling< T, MaxChannels >::upsample ( AudioBufferView< const T >  input)
inlinenoexcept

Upsamples the input base-rate signal into the internal high-rate buffer.

Parameters
inputView of the base-rate audio buffer. Blocks longer than prepare()'s maxBlockSize are truncated (release-safe); channels beyond the prepared channel count are ignored.
Returns
A view of the internal oversampled buffer (length = input samples * factor).
Note
Real-time safe. Pair every call with one downsample() of the same block length before the next upsample().

Definition at line 437 of file Oversampling.h.


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