Skip to main content
The oscillators module provides efficient waveform generation classes with both standard and optimized implementations.

IOscillator

Base template class for all oscillators. Provides common phase and frequency management.

Template Parameters

T
typename
Sample type (typically double or float)

Constructor

IOscillator(double startPhase = 0., double startFreq = 1.)
startPhase
double
default:"0.0"
Initial phase value (0.0 to 1.0)
startFreq
double
default:"1.0"
Initial frequency in Hz

Methods

Process

virtual inline T Process(double freqHz) = 0
Pure virtual method to generate the next sample.
freqHz
double
Frequency in Hz for this sample
Returns: Generated sample value

SetFreqCPS

inline void SetFreqCPS(double freqHz)
Set the oscillator frequency.
freqHz
double
Frequency in cycles per second (Hz)

SetSampleRate

void SetSampleRate(double sampleRate)
Set the sample rate for processing.
sampleRate
double
Sample rate in samples per second

Reset

void Reset()
Reset the oscillator phase to the initial start phase.

SetPhase

void SetPhase(double phase)
Set the current phase directly.
phase
double
Phase value (0.0 to 1.0)

Protected Members

mPhase
double
Current phase (0.0 to 1.0)
mPhaseIncr
double
Phase increment per sample
mSampleRate
double
default:"44100.0"
Current sample rate
mStartPhase
double
Initial phase for reset

SinOscillator

Standard sine wave oscillator using std::sin(). Suitable for general-purpose use where performance is not critical.

Template Parameters

T
typename
Sample type (typically double or float)

Constructor

SinOscillator(double startPhase = 0., double startFreq = 1.)
startPhase
double
default:"0.0"
Initial phase value (0.0 to 1.0)
startFreq
double
default:"1.0"
Initial frequency in Hz

Methods

Process (no arguments)

inline T Process()
Generate the next sample using the current frequency setting. Returns: Sine wave sample in range [-1.0, 1.0]

Process (with frequency)

inline T Process(double freqHz) override
Generate the next sample with per-sample frequency modulation.
freqHz
double
Frequency in Hz for this sample
Returns: Sine wave sample in range [-1.0, 1.0]

Usage Example

// Create a 440Hz sine oscillator
SinOscillator<double> osc(0.0, 440.0);
osc.SetSampleRate(44100.0);

// Generate samples
for (int i = 0; i < blockSize; i++) {
  output[i] = osc.Process();
}

// Or with per-sample frequency modulation
for (int i = 0; i < blockSize; i++) {
  double freq = 440.0 + lfo.Process() * 10.0; // vibrato
  output[i] = osc.Process(freq);
}

FastSinOscillator

High-performance sine wave oscillator using table lookup with linear interpolation. Based on Miller Puckette’s Pure Data implementation.
This oscillator uses a clever floating-point bit manipulation technique to extract table indices and fractional parts efficiently. It’s significantly faster than SinOscillator for CPU-intensive applications.

Template Parameters

T
typename
Sample type (typically double or float)

Constructor

FastSinOscillator(double startPhase = 0., double startFreq = 1.)
startPhase
double
default:"0.0"
Initial phase value (0.0 to 1.0)
startFreq
double
default:"1.0"
Initial frequency in Hz

Methods

Process (no arguments)

inline T Process()
Generate the next sample using the current frequency setting. Returns: Sine wave sample in range [-1.0, 1.0]

Process (with frequency)

inline T Process(double freqCPS) override
Generate the next sample with per-sample frequency modulation.
freqCPS
double
Frequency in Hz for this sample
Returns: Sine wave sample in range [-1.0, 1.0]

Lookup (static)

static inline T Lookup(double phaseRadians)
Static method to look up a sine value from the table without managing state.
phaseRadians
double
Phase in radians
Returns: Sine wave value at the given phase

ProcessBlock

void ProcessBlock(T* pOutput, int nFrames)
Process an entire block of samples for better efficiency.
pOutput
T*
Output buffer to fill with generated samples
nFrames
int
Number of samples to generate

Public Members

mLastOutput
T
default:"0.0"
Last generated output value

Usage Example

// Create a fast sine oscillator
FastSinOscillator<double> osc(0.0, 440.0);
osc.SetSampleRate(44100.0);

// Process a block of samples
double output[512];
osc.ProcessBlock(output, 512);

// Static lookup for one-off calculations
double phase = 1.57079632679; // π/2
double value = FastSinOscillator<double>::Lookup(phase); // Returns ~1.0

Performance Considerations

Table Size

Uses a 512-sample lookup table with linear interpolation

Bit Manipulation

Leverages IEEE 754 double precision format for fast indexing

Block Processing

ProcessBlock() is more efficient than calling Process() in a loop

Cache Friendly

Small table size fits in CPU cache

Implementation Details

Phase Representation

All oscillators use normalized phase values:
  • Range: 0.0 to 1.0
  • Wrapping: Automatic wrapping when phase exceeds 1.0
  • Precision: Double precision floating point

Frequency Calculation

Phase increment is calculated as:
mPhaseIncr = (1.0 / mSampleRate) * freqHz

FastSinOscillator Algorithm

The fast oscillator uses a technique originally by Robert Höldrich:
  1. Add a magic constant (UNITBIT32 = 1572864) to the phase
  2. Use a union to extract integer and fractional parts
  3. Use integer part as table index
  4. Use fractional part for linear interpolation
  5. Achieve sub-sample accuracy without expensive conversions
This technique only works with double precision floating point values in the range [-524288, +524288]. The oscillator phase is carefully managed to stay within this range.

Code Location

Source: IPlug/Extras/Oscillator.h:1

See Also

  • LFO - Low Frequency Oscillator with tempo sync
  • Envelopes - Envelope generators
  • Filters - Audio filtering