DSPark 1.6.1
Header-only audio DSP framework in pure C++20 — zero dependencies
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.
 
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 reset () noexcept
 
int getTableSize () const noexcept
 
bool isReady () const noexcept
 
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 59 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 65 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 155 of file WaveshapeTable.h.

◆ buildFromFunction()

template<FloatType T>
void dspark::WaveshapeTable< T >::buildFromFunction ( std::function< T(T)>  func,
int  tableSize = 4096,
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 84 of file WaveshapeTable.h.

◆ buildHardClip()

template<FloatType T>
void dspark::WaveshapeTable< T >::buildHardClip ( 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 131 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 142 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 121 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 285 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 292 of file WaveshapeTable.h.

◆ getOversamplingFactor()

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

Definition at line 248 of file WaveshapeTable.h.

◆ getTableSize()

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

Definition at line 281 of file WaveshapeTable.h.

◆ isReady()

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

Definition at line 282 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 215 of file WaveshapeTable.h.

◆ process() [1/2]

template<FloatType T>
void dspark::WaveshapeTable< T >::process ( T *__restrict  data,
int  numSamples,
preGain = T(1),
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 203 of file WaveshapeTable.h.

◆ process() [2/2]

template<FloatType T>
T dspark::WaveshapeTable< T >::process ( input,
preGain = T(1),
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 170 of file WaveshapeTable.h.

◆ processBlock()

template<FloatType T>
void dspark::WaveshapeTable< T >::processBlock ( AudioBufferView< T >  buffer,
preGain = T(1),
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 256 of file WaveshapeTable.h.

◆ reset()

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

Definition at line 276 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 227 of file WaveshapeTable.h.


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