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

TPDF dithering processor with optional 1st-order noise shaping. More...

#include <Dither.h>

Public Member Functions

 Dither (int targetBits=16, bool noiseShaping=false) noexcept
 Constructs a dithering processor.
 
void setTargetBitDepth (int bits) noexcept
 Sets the target bit depth and recalculates scaling factors.
 
void setNoiseShaping (bool enabled) noexcept
 Enables or disables 1st-order noise shaping.
 
void reset () noexcept
 Resets the noise shaping state. Call this when playback stops or flushes.
 
processSample (T input, int channel=0) noexcept
 Applies TPDF dither and quantises a single sample.
 
void processBlock (T *data, int numSamples, int channel=0) noexcept
 Applies dithering to an entire audio buffer in-place.
 
int getTargetBitDepth () const noexcept
 
getQuantisationStep () const noexcept
 

Detailed Description

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

TPDF dithering processor with optional 1st-order noise shaping.

Adds Triangular Probability Density Function (TPDF) noise of 2 LSB peak-to-peak before quantisation. This replaces correlated quantisation distortion (truncation harmonics, noise modulation) with a constant, signal-independent noise floor.

The optional noise shaper feeds back the total requantisation error (dither + quantiser) through a one-sample delay, first-order highpass shaping the entire noise floor: about 15 dB less noise at 1 kHz (44.1 kHz rate) in exchange for a gentle rise towards Nyquist where hearing is least sensitive.

Quantised output lies exactly on the integer level grid of the target depth: levels in [-2^(bits-1), 2^(bits-1) - 1], so positive full scale is one step below 1.0, exactly like the int16/int24 file writers. Feeding the output to WavFile round-trips bit-exactly.

Threading is owner-managed: call setters and processing from the thread that owns the stream (the PRNG and error state are plain members by design). Use one instance per processing thread.

Template Parameters
TSample type (float or double).

Definition at line 55 of file Dither.h.

Constructor & Destructor Documentation

◆ Dither()

template<typename T >
dspark::Dither< T >::Dither ( int  targetBits = 16,
bool  noiseShaping = false 
)
inlineexplicitnoexcept

Constructs a dithering processor.

Automatically seeds the internal PRNG with a globally unique seed to ensure uncorrelated noise between multiple instances (e.g., Left and Right channels).

Parameters
targetBitsTarget bit depth (min 8). Max is 24 for float, 32 for double.
noiseShapingEnable 1st-order noise shaping.

Definition at line 69 of file Dither.h.

Member Function Documentation

◆ getQuantisationStep()

template<typename T >
T dspark::Dither< T >::getQuantisationStep ( ) const
inlinenoexcept

Definition at line 155 of file Dither.h.

◆ getTargetBitDepth()

template<typename T >
int dspark::Dither< T >::getTargetBitDepth ( ) const
inlinenoexcept

Definition at line 154 of file Dither.h.

◆ processBlock()

template<typename T >
void dspark::Dither< T >::processBlock ( T *  data,
int  numSamples,
int  channel = 0 
)
inlinenoexcept

Applies dithering to an entire audio buffer in-place.

The noise shaping branch is resolved once outside the sample loop. (The PRNG recurrence is serial, so this loop does not vectorise.)

Parameters
dataPointer to audio data.
numSamplesNumber of samples to process.
channelChannel index.

Definition at line 140 of file Dither.h.

◆ processSample()

template<typename T >
T dspark::Dither< T >::processSample ( input,
int  channel = 0 
)
inlinenoexcept

Applies TPDF dither and quantises a single sample.

Parameters
inputInput sample in [-1.0, 1.0].
channelChannel index for independent noise shaping history (0 to 15; out-of-range indices dither without shaping).
Returns
Quantised sample on the target grid, in [-1, 1 - step].

Definition at line 122 of file Dither.h.

◆ reset()

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

Resets the noise shaping state. Call this when playback stops or flushes.

Definition at line 110 of file Dither.h.

◆ setNoiseShaping()

template<typename T >
void dspark::Dither< T >::setNoiseShaping ( bool  enabled)
inlinenoexcept

Enables or disables 1st-order noise shaping.

Definition at line 107 of file Dither.h.

◆ setTargetBitDepth()

template<typename T >
void dspark::Dither< T >::setTargetBitDepth ( int  bits)
inlinenoexcept

Sets the target bit depth and recalculates scaling factors.

Parameters
bitsTarget bit depth. Automatically clamped based on the precision of T.

Definition at line 86 of file Dither.h.


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