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

TPDF dithering processor with optional noise shaping. More...

#include <Dither.h>

Public Types

enum class  Shaper { FirstOrder , Lipshitz5 }
 Error-feedback filter used when noise shaping is enabled. More...
 

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 noise shaping.
 
void setShaper (Shaper shaper) noexcept
 Selects the noise-shaping filter (used while shaping is enabled).
 
Shaper getShaper () const noexcept
 Returns the selected noise-shaping filter.
 
void reset () noexcept
 Resets the noise shaping state. Call this when playback stops or flushes.
 
T 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
 
T getQuantisationStep () const noexcept
 

Detailed Description

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

TPDF dithering processor with optional 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 filter of the past errors:

  • Shaper::FirstOrder (default): one-sample delay, first-order highpass shaping of the entire noise floor: about 15 dB less noise at 1 kHz (44.1 kHz rate) in exchange for a gentle rise towards Nyquist.
  • Shaper::Lipshitz5: the 5-tap error filter {2.033, -2.165, 1.959, -1.590, 0.6149} of Lipshitz, Vanderkooy and Wannamaker, "Minimally Audible Noise Shaping" (JAES 1991), designed for 44.1 kHz. It moves the noise out of the 1-5 kHz region where hearing is most sensitive: about 5 dB less noise there than first order (measured -113.1 vs -108.2 dB in 1-5 kHz for 16 bits), more above 15 kHz. At other rates the curve shifts with the rate.

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.

Non-finite input is sanitised on entry (NaN becomes silence, infinities clip to full scale), so a single bad sample cannot poison the error feedback: before, one NaN made every later sample NaN until reset().

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 68 of file Dither.h.

Member Enumeration Documentation

◆ Shaper

template<typename T >
enum class dspark::Dither::Shaper
strong

Error-feedback filter used when noise shaping is enabled.

Enumerator
FirstOrder 

(1 - z^-1) shaping of the whole noise floor (default).

Lipshitz5 

5-tap psychoacoustic shaping for 44.1 kHz (Lipshitz et al. 1991).

Definition at line 74 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 noise shaping (Shaper::FirstOrder unless setShaper() says otherwise).

Definition at line 89 of file Dither.h.

Member Function Documentation

◆ getQuantisationStep()

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

Definition at line 188 of file Dither.h.

◆ getShaper()

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

Returns the selected noise-shaping filter.

Definition at line 136 of file Dither.h.

◆ getTargetBitDepth()

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

Definition at line 187 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 169 of file Dither.h.

◆ processSample()

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

Applies TPDF dither and quantises a single sample.

Parameters
inputInput sample in [-1.0, 1.0]. NaN is treated as silence.
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 151 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 139 of file Dither.h.

◆ setNoiseShaping()

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

Enables or disables noise shaping.

Definition at line 127 of file Dither.h.

◆ setShaper()

template<typename T >
void dspark::Dither< T >::setShaper ( Shaper  shaper)
inlinenoexcept

Selects the noise-shaping filter (used while shaping is enabled).

Definition at line 130 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 106 of file Dither.h.


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