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

Zero-latency lookup-table waveshaper with RT-safe gain modulation. More...

#include <WaveshapeTable.h>

Public Member Functions

 WaveshapeTable ()
 Constructor. Initializes a safe passthrough table to prevent RT crashes.
 
void buildFromFunction (std::function< T(T)> func, int tableSize=4096, T xMax=T(8))
 Builds the lookup table from an arbitrary transfer function.
 
void buildTanh (int tableSize=4096)
 Builds a normalized tanh (soft clip) table.
 
void buildHardClip (T threshold=T(0.8), int tableSize=4096)
 Builds a hard-clip table.
 
void buildSoftClip (int tableSize=4096)
 Builds a cubic soft-clip table.
 
void buildAsymmetric (int tableSize=4096)
 Builds an asymmetric clipping table for even-harmonic generation.
 
T process (T input, T preGain=T(1), T postGain=T(1)) const noexcept
 Processes a single sample through the waveshaper with Hermite interpolation.
 
void process (T *__restrict data, int numSamples, T preGain=T(1), T postGain=T(1)) const noexcept
 Processes a buffer in-place.
 
void prepare (const AudioSpec &spec)
 Prepares the waveshaper for oversampled block processing.
 
void setOversampling (int factor)
 Enables oversampling.
 
int getOversamplingFactor () const noexcept
 
void processBlock (AudioBufferView< T > buffer, T preGain=T(1), T postGain=T(1)) noexcept
 Processes a buffer view with optional oversampling.
 
void setAntialiasing (bool enabled) noexcept
 Enables first-order antiderivative anti-aliasing (ADAA) in processBlock().
 
bool isAntialiasingEnabled () const noexcept
 True when processBlock() applies ADAA.
 
void reset () noexcept
 
int getTableSize () const noexcept
 
bool isReady () const noexcept
 
T getInputRange () const noexcept
 Returns the half-range of the table's input domain.
 
int getLatency () const noexcept
 Reports the processing latency in samples.
 

Detailed Description

template<FloatType T>
class dspark::WaveshapeTable< T >

Zero-latency lookup-table waveshaper with RT-safe gain modulation.

Template Parameters
TSample type (float or double).

Definition at line 64 of file WaveshapeTable.h.

Constructor & Destructor Documentation

◆ WaveshapeTable()

template<FloatType T>
dspark::WaveshapeTable< T >::WaveshapeTable ( )
inline

Constructor. Initializes a safe passthrough table to prevent RT crashes.

Definition at line 70 of file WaveshapeTable.h.

Member Function Documentation

◆ buildAsymmetric()

template<FloatType T>
void dspark::WaveshapeTable< T >::buildAsymmetric ( int  tableSize = 4096)
inline

Builds an asymmetric clipping table for even-harmonic generation.

Parameters
tableSizeTable entries.

Definition at line 170 of file WaveshapeTable.h.

◆ buildFromFunction()

template<FloatType T>
void dspark::WaveshapeTable< T >::buildFromFunction ( std::function< T(T)>  func,
int  tableSize = 4096,
T  xMax = T(8) 
)
inline

Builds the lookup table from an arbitrary transfer function.

Warning
Allocates memory. Do not call from the audio thread.

The table spans the input range [-xMax, +xMax]. A range wider than the nominal [-1, 1] is what makes preGain work as a true drive control: driven samples keep following the curve instead of slamming into a hard plateau at the table edge.

Parameters
funcTransfer function mapping [-xMax, xMax] to output.
tableSizeNumber of active table entries (default: 4096).
xMaxHalf-range of the table input domain (default: 8).

Definition at line 89 of file WaveshapeTable.h.

◆ buildHardClip()

template<FloatType T>
void dspark::WaveshapeTable< T >::buildHardClip ( T  threshold = T(0.8),
int  tableSize = 4096 
)
inline

Builds a hard-clip table.

Parameters
thresholdClipping threshold (0 to 1, default: 0.8).
tableSizeTable entries.

Definition at line 146 of file WaveshapeTable.h.

◆ buildSoftClip()

template<FloatType T>
void dspark::WaveshapeTable< T >::buildSoftClip ( int  tableSize = 4096)
inline

Builds a cubic soft-clip table.

Parameters
tableSizeTable entries.

Definition at line 157 of file WaveshapeTable.h.

◆ buildTanh()

template<FloatType T>
void dspark::WaveshapeTable< T >::buildTanh ( int  tableSize = 4096)
inline

Builds a normalized tanh (soft clip) table.

Note
Use preGain in process() to drive the saturation in Real-Time.
Parameters
tableSizeTable entries (default: 4096).

Definition at line 136 of file WaveshapeTable.h.

◆ getInputRange()

template<FloatType T>
T dspark::WaveshapeTable< T >::getInputRange ( ) const
inlinenoexcept

Returns the half-range of the table's input domain.

Definition at line 332 of file WaveshapeTable.h.

◆ getLatency()

template<FloatType T>
int dspark::WaveshapeTable< T >::getLatency ( ) const
inlinenoexcept

Reports the processing latency in samples.

Returns
The oversampler group delay (0 when oversampling is off). Report this to the host for plugin delay compensation.

Definition at line 339 of file WaveshapeTable.h.

◆ getOversamplingFactor()

template<FloatType T>
int dspark::WaveshapeTable< T >::getOversamplingFactor ( ) const
inlinenoexcept

Definition at line 263 of file WaveshapeTable.h.

◆ getTableSize()

template<FloatType T>
int dspark::WaveshapeTable< T >::getTableSize ( ) const
inlinenoexcept

Definition at line 328 of file WaveshapeTable.h.

◆ isAntialiasingEnabled()

template<FloatType T>
bool dspark::WaveshapeTable< T >::isAntialiasingEnabled ( ) const
inlinenoexcept

True when processBlock() applies ADAA.

Definition at line 320 of file WaveshapeTable.h.

◆ isReady()

template<FloatType T>
bool dspark::WaveshapeTable< T >::isReady ( ) const
inlinenoexcept

Definition at line 329 of file WaveshapeTable.h.

◆ prepare()

template<FloatType T>
void dspark::WaveshapeTable< T >::prepare ( const AudioSpec &  spec)
inline

Prepares the waveshaper for oversampled block processing.

Parameters
specAudio specification.

Definition at line 230 of file WaveshapeTable.h.

◆ process() [1/2]

template<FloatType T>
void dspark::WaveshapeTable< T >::process ( T *__restrict  data,
int  numSamples,
T  preGain = T(1),
T  postGain = T(1) 
) const
inlinenoexcept

Processes a buffer in-place.

Tight scalar loop: the data-dependent table gather defeats autovectorization, so per-sample cost is the process() call inlined.

Parameters
dataAudio samples pointer (should ideally be __restrict).
numSamplesNumber of samples to process.
preGainGain to drive into the shaper.
postGainGain to compensate volume output.

Definition at line 218 of file WaveshapeTable.h.

◆ process() [2/2]

template<FloatType T>
T dspark::WaveshapeTable< T >::process ( T  input,
T  preGain = T(1),
T  postGain = T(1) 
) const
inlinenoexcept

Processes a single sample through the waveshaper with Hermite interpolation.

Parameters
inputInput sample.
preGainReal-time drive/gain applied before lookup (default 1.0).
postGainReal-time makeup gain applied after lookup (default 1.0).
Returns
Shaped output sample.

Definition at line 185 of file WaveshapeTable.h.

◆ processBlock()

template<FloatType T>
void dspark::WaveshapeTable< T >::processBlock ( AudioBufferView< T >  buffer,
T  preGain = T(1),
T  postGain = T(1) 
)
inlinenoexcept

Processes a buffer view with optional oversampling.

Parameters
bufferAudio buffer view.
preGainReal-time drive applied per-sample.
postGainReal-time makeup gain applied per-sample.

Definition at line 271 of file WaveshapeTable.h.

◆ reset()

template<FloatType T>
void dspark::WaveshapeTable< T >::reset ( )
inlinenoexcept

Definition at line 322 of file WaveshapeTable.h.

◆ setAntialiasing()

template<FloatType T>
void dspark::WaveshapeTable< T >::setAntialiasing ( bool  enabled)
inlinenoexcept

Enables first-order antiderivative anti-aliasing (ADAA) in processBlock().

Each output is the mean of the curve over the segment between two consecutive (driven) inputs, (F(x[n]) - F(x[n-1])) / (x[n] - x[n-1]), which suppresses the harmonics that would fold back. It shifts the signal by half a sample at the processing rate (not reported by getLatency()) and, like any first-order ADAA, averages adjacent samples: at 1x that rolls off the top octave (-11.7 dB at 20 kHz @ 48 kHz), so combine it with setOversampling(2) or more (-2.0 dB at 2x, -0.5 dB at 4x). The first 16 channels are anti-aliased; further channels run the plain memoryless curve. The per-sample process() overloads stay memoryless. Owner-managed like the other setters: call it from the processing thread or while no process call is running. Enabling starts each channel from its next input (no stale history, no click).

Definition at line 313 of file WaveshapeTable.h.

◆ setOversampling()

template<FloatType T>
void dspark::WaveshapeTable< T >::setOversampling ( int  factor)
inline

Enables oversampling.

Warning
Allocates memory. Do not call from the audio thread.
Parameters
factorOversampling factor (1 = off, 2, 4, 8, 16).

Definition at line 242 of file WaveshapeTable.h.


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