DSPark 1.9.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.
 
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.
 
void setBank (const WavetableBank *bank) noexcept
 Plays an external bank instead of the oscillator's own table.
 
void setPosition (T position) noexcept
 Morph position across the bank's frames, 0 .. frames - 1 (fractional positions blend adjacent frames; clamped on read).
 
T getPosition () const noexcept
 
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.
 
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
 Samples of the shortest level table (brighter levels hold more; see WavetableBank).
 
static constexpr int kTableMask = kTableSize - 1
 
static constexpr int kMaxMipLevels = WavetableBank::kMipLevels
 Mip levels, half an octave apart.
 

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 65 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 sine amplitude for harmonic N (starts at 1, up to WavetableBank::kMaxHarmonics).
Note
Allocates memory. Call only during initialization/preparation.

Definition at line 162 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 110 of file WavetableOscillator.h.

◆ buildSine()

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

Builds a pure sine wavetable.

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 121 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 132 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 265 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 236 of file WavetableOscillator.h.

◆ getPosition()

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

Definition at line 211 of file WavetableOscillator.h.

◆ getSample()

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

Generates the next sample.

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

Definition at line 243 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.

The cycle is analysed exactly on its own length (no resampling), its DC discarded, and the mip levels rebuilt from its harmonics.

Parameters
dataPointer to wavetable samples.
sizeNumber of samples in the input data (3 or more; smaller sizes and null data are ignored).
Note
Allocates memory. Call only during initialization/preparation.

Definition at line 181 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 99 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 selection 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 85 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 255 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 281 of file WavetableOscillator.h.

◆ setBank()

template<FloatType T>
void dspark::WavetableOscillator< T >::setBank ( const WavetableBank *  bank)
inlinenoexcept

Plays an external bank instead of the oscillator's own table.

The bank is not owned and must outlive its use; it must not be rebuilt while this oscillator generates. nullptr returns to the own table. Setup-time (same contract as the build methods).

Definition at line 195 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 220 of file WavetableOscillator.h.

◆ setPosition()

template<FloatType T>
void dspark::WavetableOscillator< T >::setPosition ( T  position)
inlinenoexcept

Morph position across the bank's frames, 0 .. frames - 1 (fractional positions blend adjacent frames; clamped on read).

Definition at line 205 of file WavetableOscillator.h.

Member Data Documentation

◆ kMaxMipLevels

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

Mip levels, half an octave apart.

Definition at line 72 of file WavetableOscillator.h.

◆ kTableMask

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

Definition at line 70 of file WavetableOscillator.h.

◆ kTableSize

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

Samples of the shortest level table (brighter levels hold more; see WavetableBank).

Definition at line 69 of file WavetableOscillator.h.


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