DSPark 1.6.1
Header-only audio DSP framework in pure C++20 — zero dependencies
Loading...
Searching...
No Matches
dspark::Oscillator< T > Class Template Reference

Band-limited oscillator featuring PolyBLEP anti-aliasing and analog-modeled integration. More...

#include <Oscillator.h>

Public Types

enum class  Waveform { Sine , Saw , Square , Triangle }
 

Public Member Functions

void prepare (double sampleRate) noexcept
 Prepares the oscillator with the system sample rate.
 
void prepare (const AudioSpec &spec) noexcept
 Prepares the oscillator from an AudioSpec configuration.
 
void setFrequency (T freq) noexcept
 Sets the oscillator's fundamental frequency.
 
void setWaveform (Waveform w) noexcept
 Changes the active waveform.
 
void setSyncRatio (T ratio) noexcept
 Enables band-limited hard sync.
 
void setPhase (T phase) noexcept
 Forces the oscillator phase to a specific value.
 
void reset () noexcept
 Hard-resets the oscillator phase and integrator state.
 
getNextSample () noexcept
 Computes and returns the next single audio sample.
 
void processBlock (T *buffer, size_t numSamples) noexcept
 Fills a buffer with generated samples.
 
getSample () noexcept
 Generator contract alias for getNextSample() (GeneratorProcessor).
 
void generateBlock (AudioBufferView< T > buffer) noexcept
 Fills every channel of the view with the generated waveform. Satisfies the GeneratorProcessor concept. The oscillator is mono, so all channels receive the same signal (channel 0 is generated, the rest copy it).
 
getPhase () const noexcept
 
getFrequency () const noexcept
 
Waveform getWaveform () const noexcept
 
getSyncRatio () const noexcept
 

Detailed Description

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

Band-limited oscillator featuring PolyBLEP anti-aliasing and analog-modeled integration.

This oscillator provides high-quality waveform generation suitable for both audio-rate synthesis and low-frequency modulation (LFO). It utilizes PolyBLEP (Polynomial Band-Limited Step) to drastically reduce aliasing artifacts in discontinuous waveforms (Saw, Square). The Triangle wave is generated via a leaky integrator driven by a PolyBLEP square, providing an analog-style curve. Under hard sync (setSyncRatio) every discontinuity is instead corrected with a table minBLEP (MinBlepTable) – a causal minimum-phase kernel whose alias rejection (~-90 dB measured) survives the arbitrary jump amplitudes sync creates. Exception: a hard-synced Sine has no value discontinuity, only a derivative kink at the reset, which the minBLEP does not address; its residual alias floor is ~-64 dB (a minBLAMP table is the future direction).

Note
This class is not internally thread-safe: apply parameter changes (e.g. setFrequency) from the audio thread between process calls, publishing values from other threads via atomics, as the framework effects do.
Template Parameters
TSample type (must be float or double).

Definition at line 63 of file Oscillator.h.

Member Enumeration Documentation

◆ Waveform

template<typename T >
enum class dspark::Oscillator::Waveform
strong
Enumerator
Sine 
Saw 
Square 
Triangle 

Definition at line 68 of file Oscillator.h.

Member Function Documentation

◆ generateBlock()

template<typename T >
void dspark::Oscillator< T >::generateBlock ( AudioBufferView< T >  buffer)
inlinenoexcept

Fills every channel of the view with the generated waveform. Satisfies the GeneratorProcessor concept. The oscillator is mono, so all channels receive the same signal (channel 0 is generated, the rest copy it).

Definition at line 284 of file Oscillator.h.

◆ getFrequency()

template<typename T >
T dspark::Oscillator< T >::getFrequency ( ) const
inlinenoexcept

Definition at line 302 of file Oscillator.h.

◆ getNextSample()

template<typename T >
T dspark::Oscillator< T >::getNextSample ( )
inlinenoexcept

Computes and returns the next single audio sample.

Returns
A band-limited sample in the range [-1.0, 1.0] (hard sync may overshoot to ~1.5x full scale – Gibbs, see setSyncRatio()).

Definition at line 198 of file Oscillator.h.

◆ getPhase()

template<typename T >
T dspark::Oscillator< T >::getPhase ( ) const
inlinenoexcept

Definition at line 301 of file Oscillator.h.

◆ getSample()

template<typename T >
T dspark::Oscillator< T >::getSample ( )
inlinenoexcept

Generator contract alias for getNextSample() (GeneratorProcessor).

Definition at line 277 of file Oscillator.h.

◆ getSyncRatio()

template<typename T >
T dspark::Oscillator< T >::getSyncRatio ( ) const
inlinenoexcept

Definition at line 304 of file Oscillator.h.

◆ getWaveform()

template<typename T >
Waveform dspark::Oscillator< T >::getWaveform ( ) const
inlinenoexcept

Definition at line 303 of file Oscillator.h.

◆ prepare() [1/2]

template<typename T >
void dspark::Oscillator< T >::prepare ( const AudioSpec spec)
inlinenoexcept

Prepares the oscillator from an AudioSpec configuration.

Parameters
specThe structural audio specification of the processing chain.

Definition at line 93 of file Oscillator.h.

◆ prepare() [2/2]

template<typename T >
void dspark::Oscillator< T >::prepare ( double  sampleRate)
inlinenoexcept

Prepares the oscillator with the system sample rate.

Also re-clamps the stored frequency against the new Nyquist bound, so calling prepare() again with a LOWER sample rate leaves the oscillator in a valid state (the PolyBLEP correction assumes increment <= 0.5).

Parameters
sampleRateThe operating sample rate in Hz. Must be > 0.

Definition at line 79 of file Oscillator.h.

◆ processBlock()

template<typename T >
void dspark::Oscillator< T >::processBlock ( T *  buffer,
size_t  numSamples 
)
inlinenoexcept

Fills a buffer with generated samples.

Plain per-sample loop over getNextSample(): waveform generation is inherently serial (recursive phase/integrator state), so there is no SIMD variant and no alignment requirement on buffer.

Parameters
bufferDestination pointer (any alignment).
numSamplesNumber of samples to generate.

Definition at line 268 of file Oscillator.h.

◆ reset()

template<typename T >
void dspark::Oscillator< T >::reset ( )
inlinenoexcept

Hard-resets the oscillator phase and integrator state.

The triangle integrator is seeded at the negative steady-state peak (phase 0 drives the underlying square positive, so the steady cycle starts at -peak). Seeding at zero made the first half-cycle overshoot to (1+q)x the nominal level – an audible +4 dB pop on note retrigger.

Definition at line 184 of file Oscillator.h.

◆ setFrequency()

template<typename T >
void dspark::Oscillator< T >::setFrequency ( freq)
inlinenoexcept

Sets the oscillator's fundamental frequency.

Parameters
freqFrequency in Hz. Will be clamped between 0 and Nyquist.

Definition at line 102 of file Oscillator.h.

◆ setPhase()

template<typename T >
void dspark::Oscillator< T >::setPhase ( phase)
inlinenoexcept

Forces the oscillator phase to a specific value.

The triangle integrator is re-seeded at its steady-state value for the requested phase, so a Triangle LFO lands on the expected waveform point immediately instead of easing in over ~1/increment samples (the stereo-spread re-phasing pattern used by Chorus relies on this). Under hard sync the slave is re-phased coherently and pending minBLEP corrections are discarded.

Parameters
phaseNormalized phase in the range [0.0, 1.0).

Definition at line 159 of file Oscillator.h.

◆ setSyncRatio()

template<typename T >
void dspark::Oscillator< T >::setSyncRatio ( ratio)
inlinenoexcept

Enables band-limited hard sync.

The oscillator's frequency becomes the sync MASTER; an internal slave runs at ratio times that frequency and is phase-reset every master cycle – the classic ripping sync timbre. Every discontinuity (the slave's own edges and the reset jump, scaled to its actual amplitude) is corrected with a table minBLEP (see MinBlepTable): a minimum-phase band-limited step whose correction is fully causal, pushing aliasing to the windowed-sinc stopband instead of the ~-40 dB envelope of a 2-point polynomial kernel.

Note
A band-limited (brickwall) rendition of the synced waveform legitimately overshoots the naive one: peaks may reach ~1.5x full scale (Gibbs), unlike the sync-off waveforms which stay within [-1, 1]. Leave headroom or follow with a gain stage.
Parameters
ratioSlave/master frequency ratio. Values <= 1 disable sync (a small guard band just above 1 also disables it – a 1:1 slave adds nothing but correction noise).

Definition at line 141 of file Oscillator.h.

◆ setWaveform()

template<typename T >
void dspark::Oscillator< T >::setWaveform ( Waveform  w)
inlinenoexcept

Changes the active waveform.

Parameters
wThe desired waveform type.

Definition at line 118 of file Oscillator.h.


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