|
DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
|
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. | |
Power-of-two oversampling processor with polyphase anti-aliasing.
Usage is a paired streaming operation per audio callback:
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.
| T | Sample type (float or double). |
| MaxChannels | Maximum number of channels supported. |
Definition at line 100 of file Oversampling.h.
|
strong |
The band the first stage keeps (see the constructor).
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.
|
strong |
Stopband depth of the cascade (see the constructor).
| 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.
|
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.
|
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.
|
inlineexplicit |
Constructs the oversampling engine.
| factor | Oversampling factor. Must be a power of two (1, 2, 4, 8, 16). |
| quality | Filter quality preset. Default is High (-80 dB). |
| mode | Filter family. Default is the linear-phase FIR. |
| depth | Stopband depth. Default is the preset's (Depth::Preset). |
| band | Band of the first stage. Default is the preset's (Band::Preset); Band::Audible fits it to the host rate at prepare(). |
Definition at line 209 of file Oversampling.h.
|
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).
| output | Buffer view where the base-rate samples will be written. Use the same block length as the paired upsample() input. |
Definition at line 490 of file Oversampling.h.
|
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.
| sampleRate | Host rate in Hz. A rate that is not a positive finite number gives 1. |
| minimumInternalRate | Internal rate to reach, in Hz. |
| maxFactor | Largest factor returned (at most 16). |
Definition at line 303 of file Oversampling.h.
|
inlinenoexcept |
Returns the band of the first stage (Band::Preset or Band::Audible).
Definition at line 329 of file Oversampling.h.
|
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.
|
inlinenoexcept |
Returns the stopband depth (Depth::Preset or Depth::Deep).
Definition at line 326 of file Oversampling.h.
|
inlinenoexcept |
Returns the current oversampling factor.
Definition at line 317 of file Oversampling.h.
|
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.
|
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.
| numSamples | Base-rate block length, clamped to prepare()'s maxBlockSize. |
Definition at line 421 of file Oversampling.h.
|
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.
|
inlinenoexcept |
Returns the half-band filter family.
Definition at line 323 of file Oversampling.h.
|
inlinenoexcept |
Returns the current quality preset.
Definition at line 320 of file Oversampling.h.
|
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.
| frequencyHz | Frequency in Hz, at the base rate of prepare() (48 kHz is assumed before prepare()). |
Definition at line 384 of file Oversampling.h.
|
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().
| stage | Stage, 0 the first (the one at the lowest rate). |
Definition at line 342 of file Oversampling.h.
|
inline |
Pre-allocates buffers and computes FIR filter coefficients.
| spec | The audio specification at the base (1x) sample rate. |
spec.maxBlockSize base samples. Definition at line 250 of file Oversampling.h.
|
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.
|
inlinenoexcept |
Upsamples the input base-rate signal into the internal high-rate buffer.
| input | View of the base-rate audio buffer. Blocks longer than prepare()'s maxBlockSize are truncated (release-safe); channels beyond the prepared channel count are ignored. |
Definition at line 437 of file Oversampling.h.