SynthLab SDK
SynthLab::SynthClock Class Reference

Compact modulo counter with wrapping used as the timebase for all oscillators. More...

#include <synthbase.h>

Public Types

enum  {
  MOD_COUNTER, PHASE_INC, PHASE_OFFSET, FREQUENCY_HZ,
  NUM_VARS
}
 

Public Member Functions

SynthClockoperator= (const SynthClock &params)
 Overloaded = operator for setting two clocks equal to each other by copying member values from a source. More...
 
void reset (double startValue=0.0)
 Reset to initial state. More...
 
void initWithClock (SynthClock &clock)
 Initialize this clock with another SynthClock. More...
 
void advanceClock (uint32_t renderInterval=1)
 Advance the clock some number of tics by adding the phaseInc value. More...
 
bool advanceWrapClock (uint32_t renderInterval=1)
 Advance the clock some number of tics by adding the phaseInc value and then check to see if modulo counter requires wrapping; wrap if needed. More...
 
bool wrapClock ()
 Wrap the modulo counter; note that this will wrap the modulo counter as many times as needed to get it back on the range of [0.0, +1.0] to support phase modulation values greater or less then +/- 1.0 which happens when the index of modulation is greater than 1.0.
 
void setFrequency (double _frequency_Hz, double _sampleRate)
 Set the clock frequency, which calculates the current phase increment value. More...
 
double getFrequency ()
 
void addPhaseOffset (double _phaseOffset, bool wrap=true)
 For phase modulation, this adds a phase offset and then optionally checks/wraps the counter as needed. More...
 
void removePhaseOffset ()
 For phase modulation, this removes a phase offset, notice that the function does not attempt to wrap the counter. More...
 
void addFrequencyOffset (double _freqOffset)
 For frequency modulation, this adds a frequency offset, and recalculates the phase increment value. More...
 
void removeFrequencyOffset ()
 For frequency modulation, this removes a frequency offset and recalculates the phase inc.
 
void saveState ()
 Freeze and save the current state of the clock; for PM and FM. More...
 
void restoreState ()
 Restore the clock to its last saved state; for PM and FM.
 

Public Attributes

double mcounter = 0.0
 modulo counter [0.0, +1.0], this is the value you use
 
double phaseInc = 0.0
 phase inc = fo/fs
 
double phaseOffset = 0.0
 PM.
 
double freqOffset = 0.0
 FM.
 
double frequency_Hz = 0.0
 clock frequency
 
double sampleRate = 0.0
 
double state [NUM_VARS] = { 0.0, 0.0, 0.0, 0.0 }
 for state save
 

Detailed Description

Compact modulo counter with wrapping used as the timebase for all oscillators.

  • the mcounter member variable is the modulo counter value on the range [0.0, 1.0]
  • the clock can run forwards or backwards (for negative frequencies)
  • you may temporarily shift the current phase of the clock forwards or backwards, with modulo wrap to implement phase modulation for the DX synths
  • functions allow saving the current phase so that the state can be restored after the phase modulation
Author
Will Pirkle http://www.willpirkle.com
Remarks
This object is included and described in further detail in Designing Software Synthesizer Plugins in C++ 2nd Ed. by Will Pirkle
Version
Revision : 1.0
Date
Date : 2021 / 04 / 26

Member Function Documentation

◆ addFrequencyOffset()

void SynthLab::SynthClock::addFrequencyOffset ( double  _freqOffset)

For frequency modulation, this adds a frequency offset, and recalculates the phase increment value.

Frequency Modulation support

Parameters
_freqOffsetthe amound of frequency shift in Hz

◆ addPhaseOffset()

void SynthLab::SynthClock::addPhaseOffset ( double  _phaseOffset,
bool  wrap = true 
)

For phase modulation, this adds a phase offset and then optionally checks/wraps the counter as needed.

Phase Modulation support

Parameters
_phaseOffsetthe amound of phase shift, normally on the range [-1.0, +1.0] but may be larger
wrapset true to automatically check and wrapt the counter as a result of phase offset

◆ advanceClock()

void SynthLab::SynthClock::advanceClock ( uint32_t  renderInterval = 1)

Advance the clock some number of tics by adding the phaseInc value.

advancing and wrapping the clock mcounter

Parameters
renderIntervalnumber of tics to advance the clock

◆ advanceWrapClock()

bool SynthLab::SynthClock::advanceWrapClock ( uint32_t  renderInterval = 1)

Advance the clock some number of tics by adding the phaseInc value and then check to see if modulo counter requires wrapping; wrap if needed.

Parameters
renderIntervalnumber of tics to advance the clock

◆ initWithClock()

void SynthLab::SynthClock::initWithClock ( SynthClock clock)

Initialize this clock with another SynthClock.

Parameters
clocksource SynthClock to copy

◆ operator=()

SynthClock & SynthLab::SynthClock::operator= ( const SynthClock params)

Overloaded = operator for setting two clocks equal to each other by copying member values from a source.

Parameters
paramssource SynthClock to copy

◆ removePhaseOffset()

void SynthLab::SynthClock::removePhaseOffset ( )

For phase modulation, this removes a phase offset, notice that the function does not attempt to wrap the counter.

  • many algorithms require checking/wrapping the counter after removal of the phase offset so you must add a call to wrapClock(); after calling this method

◆ reset()

void SynthLab::SynthClock::reset ( double  startValue = 0.0)

Reset to initial state.

Initialization and reset

Parameters
startValuephase offset at start time; this value is on the range [0.0, +1.0] corresponding to 0 to 360 degrees of phase shift

◆ saveState()

void SynthLab::SynthClock::saveState ( )

Freeze and save the current state of the clock; for PM and FM.

For both PM and FM support

◆ setFrequency()

void SynthLab::SynthClock::setFrequency ( double  _frequency_Hz,
double  _sampleRate 
)

Set the clock frequency, which calculates the current phase increment value.

get/set the clock frequency; which affects the phaseInc

Parameters
_frequency_Hznew clock frequency, OK if this value is negative, clock will run backwards
_sampleRatefs for calculating the phase inc value

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