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

Professional mipmapped wavetable oscillator for bandlimited synthesis. More...

#include <WavetableOscillator.h>

Public Member Functions

 WavetableOscillator ()=default
 
void prepare (double sampleRate)
 Prepares the oscillator for a given sample rate.
 
void prepare (const AudioSpec &spec)
 Prepares from AudioSpec (unified API).
 
void buildSaw ()
 Builds a bandlimited sawtooth wavetable with mipmaps.
 
void buildSquare ()
 Builds a bandlimited square wavetable with mipmaps.
 
void buildTriangle ()
 Builds a bandlimited triangle wavetable with mipmaps.
 
void buildSine ()
 Builds a pure sine wavetable. Requires only 1 level since it contains only the fundamental.
 
template<typename HarmonicFunc >
void buildFromHarmonics (HarmonicFunc harmonicFunc)
 Builds contiguous mipmapped tables from a harmonic amplitude function.
 
void loadWavetable (const T *data, int size)
 Loads and analyzes a custom single-cycle wavetable using DFT.
 
void setFrequency (T frequencyHz) noexcept
 Sets the fundamental oscillation frequency.
 
T getFrequency () const noexcept
 Returns the current internal frequency.
 
T getSample () noexcept
 Generates the next sample using Hermite interpolation.
 
void processBlock (T *output, int numSamples) noexcept
 Generates a block of samples.
 
void generateBlock (AudioBufferView< T > buffer) noexcept
 Fills every channel of the view with the generated waveform. Satisfies the GeneratorProcessor concept (mono source, all channels equal).
 
void reset (T phase=T(0)) noexcept
 Hard resets the oscillator phase.
 

Static Public Attributes

static constexpr int kTableSize = 2048
 
static constexpr int kTableMask = kTableSize - 1
 
static constexpr int kMaxMipLevels = 20
 Mip levels, half an octave apart (harmonic budgets in kLevelHarmonics).
 

Detailed Description

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

Professional mipmapped wavetable oscillator for bandlimited synthesis.

Template Parameters
TSample type (float or double). Must satisfy std::floating_point.

Definition at line 64 of file WavetableOscillator.h.

Constructor & Destructor Documentation

◆ WavetableOscillator()

template<FloatType T>
dspark::WavetableOscillator< T >::WavetableOscillator ( )
default

Member Function Documentation

◆ buildFromHarmonics()

template<FloatType T>
template<typename HarmonicFunc >
void dspark::WavetableOscillator< T >::buildFromHarmonics ( HarmonicFunc  harmonicFunc)
inline

Builds contiguous mipmapped tables from a harmonic amplitude function.

Template Parameters
HarmonicFuncCallable signature: T(int harmonic)
Parameters
harmonicFuncFunction returning the amplitude for harmonic N (starts at 1).
Note
Allocates memory. Call only during initialization/preparation.

Definition at line 169 of file WavetableOscillator.h.

◆ buildSaw()

template<FloatType T>
void dspark::WavetableOscillator< T >::buildSaw ( )
inline

Builds a bandlimited sawtooth wavetable with mipmaps.

Note
Allocates memory. Call only during initialization/preparation.

Definition at line 109 of file WavetableOscillator.h.

◆ buildSine()

template<FloatType T>
void dspark::WavetableOscillator< T >::buildSine ( )
inline

Builds a pure sine wavetable. Requires only 1 level since it contains only the fundamental.

Note
Allocates memory. Call only during initialization/preparation.

Definition at line 145 of file WavetableOscillator.h.

◆ buildSquare()

template<FloatType T>
void dspark::WavetableOscillator< T >::buildSquare ( )
inline

Builds a bandlimited square wavetable with mipmaps.

Note
Allocates memory. Call only during initialization/preparation.

Definition at line 120 of file WavetableOscillator.h.

◆ buildTriangle()

template<FloatType T>
void dspark::WavetableOscillator< T >::buildTriangle ( )
inline

Builds a bandlimited triangle wavetable with mipmaps.

Note
Allocates memory. Call only during initialization/preparation.

Definition at line 131 of file WavetableOscillator.h.

◆ generateBlock()

template<FloatType T>
void dspark::WavetableOscillator< T >::generateBlock ( AudioBufferView< T >  buffer)
inlinenoexcept

Fills every channel of the view with the generated waveform. Satisfies the GeneratorProcessor concept (mono source, all channels equal).

Definition at line 274 of file WavetableOscillator.h.

◆ getFrequency()

template<FloatType T>
T dspark::WavetableOscillator< T >::getFrequency ( ) const
inlinenoexcept

Returns the current internal frequency.

Returns
Frequency in Hz.

Definition at line 246 of file WavetableOscillator.h.

◆ getSample()

template<FloatType T>
T dspark::WavetableOscillator< T >::getSample ( )
inlinenoexcept

Generates the next sample using Hermite interpolation.

Returns
Output sample, nominally in [-1.0, 1.0] (tables are peak normalised; interpolation may overshoot slightly between table points).

Definition at line 253 of file WavetableOscillator.h.

◆ loadWavetable()

template<FloatType T>
void dspark::WavetableOscillator< T >::loadWavetable ( const T *  data,
int  size 
)
inline

Loads and analyzes a custom single-cycle wavetable using DFT.

Performs a Discrete Fourier Transform strictly on the original data length to avoid low-pass smearing, then reconstructs bandlimited mipmaps.

Parameters
dataPointer to wavetable samples.
sizeNumber of samples in the input data.
Note
Allocates memory. Call only during initialization/preparation.

Definition at line 188 of file WavetableOscillator.h.

◆ prepare() [1/2]

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

Prepares from AudioSpec (unified API).

Parameters
specFramework AudioSpec instance containing sample rate.

Definition at line 98 of file WavetableOscillator.h.

◆ prepare() [2/2]

template<FloatType T>
void dspark::WavetableOscillator< T >::prepare ( double  sampleRate)
inline

Prepares the oscillator for a given sample rate.

Re-derives the mip cutoff frequencies for tables that were already built, so a rate change after build*() keeps the anti-aliasing exact.

Parameters
sampleRateSample rate in Hz. Must be > 0 (invalid values, including NaN, are ignored).

Definition at line 83 of file WavetableOscillator.h.

◆ processBlock()

template<FloatType T>
void dspark::WavetableOscillator< T >::processBlock ( T *  output,
int  numSamples 
)
inlinenoexcept

Generates a block of samples.

Parameters
outputPointer to output buffer.
numSamplesNumber of samples to process.

Definition at line 264 of file WavetableOscillator.h.

◆ reset()

template<FloatType T>
void dspark::WavetableOscillator< T >::reset ( T  phase = T(0))
inlinenoexcept

Hard resets the oscillator phase.

Parameters
phaseInitial phase normalized [0.0, 1.0].

Definition at line 290 of file WavetableOscillator.h.

◆ setFrequency()

template<FloatType T>
void dspark::WavetableOscillator< T >::setFrequency ( T  frequencyHz)
inlinenoexcept

Sets the fundamental oscillation frequency.

Parameters
frequencyHzFrequency in Hz.
Note
Not inherently thread-safe. Synchronization should be managed externally.

Definition at line 230 of file WavetableOscillator.h.

Member Data Documentation

◆ kMaxMipLevels

template<FloatType T>
constexpr int dspark::WavetableOscillator< T >::kMaxMipLevels = 20
staticconstexpr

Mip levels, half an octave apart (harmonic budgets in kLevelHarmonics).

Definition at line 70 of file WavetableOscillator.h.

◆ kTableMask

template<FloatType T>
constexpr int dspark::WavetableOscillator< T >::kTableMask = kTableSize - 1
staticconstexpr

Definition at line 68 of file WavetableOscillator.h.

◆ kTableSize

template<FloatType T>
constexpr int dspark::WavetableOscillator< T >::kTableSize = 2048
staticconstexpr

Definition at line 67 of file WavetableOscillator.h.


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