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

Modulation oscillator with tempo sync, delay, fade-in and seeded random shapes. More...

#include <Lfo.h>

Public Types

enum class  Shape {
  Sine , Triangle , SawUp , SawDown ,
  Square , SampleAndHold , SmoothRandom , Steps
}
 
enum class  Division {
  EightBars , FourBars , TwoBars , Whole ,
  HalfDotted , Half , HalfTriplet , QuarterDotted ,
  Quarter , QuarterTriplet , EighthDotted , Eighth ,
  EighthTriplet , SixteenthDotted , Sixteenth , SixteenthTriplet ,
  ThirtySecond , ThirtySecondTriplet , SixtyFourth
}
 Note values for tempo sync (four beats to the whole note). More...
 

Public Member Functions

void prepare (double sampleRate) noexcept
 Prepares for a sample rate (invalid rates are ignored).
 
void reset () noexcept
 Restarts the cycle at the start phase, the cycle count at zero and the delay and fade-in from the beginning.
 
void setShape (Shape s) noexcept
 
Shape getShape () const noexcept
 
void setRateHz (double hz) noexcept
 Free rate in Hz, 0..kMaxRateHz (NaN ignored). Used while sync is off.
 
void setTempoSync (bool on) noexcept
 Tempo sync on or off; when on, the rate is the division of the tempo.
 
void setDivision (Division d) noexcept
 
void setTempo (double bpm) noexcept
 Tempo in BPM for tempo sync, 1..999 (invalid values ignored).
 
void setStartPhase (double phase) noexcept
 Cycle position reset() starts at, 0..1 (wrapped).
 
void setDelayMs (double ms) noexcept
 Silence before the depth starts rising, in ms (>= 0).
 
void setFadeInMs (double ms) noexcept
 Linear rise to full depth after the delay, in ms (>= 0).
 
void setSeed (uint64_t seed) noexcept
 Seed of the random shapes (the same seed repeats the same sequence).
 
void setRetriggerGlideMs (double ms) noexcept
 How long reset() glides from the output it was at to the new start, in ms (0, the default, jumps; >= 0, non-finite ignored).
 
void setNumSteps (int count) noexcept
 Steps in a cycle, 1..kMaxSteps (16, the default).
 
int getNumSteps () const noexcept
 
void setStep (int index, double value, double glide=0.0, double curve=0.0) noexcept
 One step: its value, how much of its length it glides to it from the step before, and the curve of that glide.
 
void setStepRandom (int index, bool random) noexcept
 Makes a step draw its value from the seed every cycle (see the file notes).
 
double getStepValue (int index) const noexcept
 
double getStepGlide (int index) const noexcept
 
double getStepCurve (int index) const noexcept
 
bool isStepRandom (int index) const noexcept
 
void setStepSwing (double swing) noexcept
 Moves the boundary inside each pair of steps, -1..1: at 0.5 the first step of a pair lasts 1.25 steps and the second 0.75 (the boundary moves by half the value, in steps).
 
double getStepSwing () const noexcept
 
void setStepQuantize (int divisions) noexcept
 Quantizes the step values to a grid of 1/divisions (0, the default, leaves them free; 12 puts them on semitones when the owner maps -1..1 to -12..12 semitones). Glides move between the quantized values.
 
int getStepQuantize () const noexcept
 
double stepsAt (double cycles) const noexcept
 The Steps shape's value at an absolute cycle position (the integer part is the cycle whose random steps are drawn).
 
double getRateHz () const noexcept
 Effective rate in Hz (the synced one when tempo sync is on).
 
double getCycles () const noexcept
 Cycles run since reset(), start phase included.
 
T value () const noexcept
 The output now (shape times delay/fade depth), without advancing.
 
T advance (int samples=1) noexcept
 Returns the output now, then advances by samples at the current rate (a control-rate owner advances its control period).
 
T advanceScaled (int samples, double rateScale) noexcept
 Advances by samples at rateScale times the current rate (rate modulation; the scale is clamped to 0..64).
 
double depthAt (int64_t samples) const noexcept
 Depth of the delay and fade-in samples after reset(): 0 during the delay, then a linear rise to 1.
 

Static Public Member Functions

static constexpr double beatsPerCycle (Division d) noexcept
 Beats (quarter notes) per cycle of a division.
 
static double valueAt (Shape shape, double cycles, uint64_t seed) noexcept
 The shape's value at an absolute cycle position.
 
static double random (uint64_t seed, int64_t index) noexcept
 Uniform value in [-1, 1) for a cycle index (splitmix64 hash).
 

Static Public Attributes

static constexpr int kNumShapes = 8
 
static constexpr int kMaxSteps = 16
 Most steps of the Steps shape.
 
static constexpr int kNumDivisions = 19
 
static constexpr double kMaxRateHz = 1000.0
 Highest free rate (Hz).
 

Detailed Description

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

Modulation oscillator with tempo sync, delay, fade-in and seeded random shapes.

lfo.prepare(48000.0);
lfo.setRateHz(5.0);
lfo.setDelayMs(200.0); // vibrato that waits...
lfo.setFadeInMs(300.0); // ...and grows in
lfo.reset(); // at note-on
float v = lfo.advance(16); // value now, then 16 samples on
Modulation oscillator with tempo sync, delay, fade-in and seeded random shapes.
Definition Lfo.h:80
void prepare(double sampleRate) noexcept
Prepares for a sample rate (invalid rates are ignored).
Definition Lfo.h:142
void setShape(Shape s) noexcept
Definition Lfo.h:165
void reset() noexcept
Restarts the cycle at the start phase, the cycle count at zero and the delay and fade-in from the beg...
Definition Lfo.h:154
void setRateHz(double hz) noexcept
Free rate in Hz, 0..kMaxRateHz (NaN ignored). Used while sync is off.
Definition Lfo.h:169
T advance(int samples=1) noexcept
Returns the output now, then advances by samples at the current rate (a control-rate owner advances i...
Definition Lfo.h:355
void setFadeInMs(double ms) noexcept
Linear rise to full depth after the delay, in ms (>= 0).
Definition Lfo.h:204
void setDelayMs(double ms) noexcept
Silence before the depth starts rising, in ms (>= 0).
Definition Lfo.h:196
Template Parameters
TOutput type (float or double). Phase arithmetic is double.

Definition at line 79 of file Lfo.h.

Member Enumeration Documentation

◆ Division

template<FloatType T>
enum class dspark::Lfo::Division
strong

Note values for tempo sync (four beats to the whole note).

Enumerator
EightBars 
FourBars 
TwoBars 
Whole 
HalfDotted 
Half 
HalfTriplet 
QuarterDotted 
Quarter 
QuarterTriplet 
EighthDotted 
Eighth 
EighthTriplet 
SixteenthDotted 
Sixteenth 
SixteenthTriplet 
ThirtySecond 
ThirtySecondTriplet 
SixtyFourth 

Definition at line 99 of file Lfo.h.

◆ Shape

template<FloatType T>
enum class dspark::Lfo::Shape
strong
Enumerator
Sine 
Triangle 
SawUp 
SawDown 
Square 
SampleAndHold 
SmoothRandom 
Steps 

Definition at line 82 of file Lfo.h.

Member Function Documentation

◆ advance()

template<FloatType T>
T dspark::Lfo< T >::advance ( int  samples = 1)
inlinenoexcept

Returns the output now, then advances by samples at the current rate (a control-rate owner advances its control period).

Definition at line 355 of file Lfo.h.

◆ advanceScaled()

template<FloatType T>
T dspark::Lfo< T >::advanceScaled ( int  samples,
double  rateScale 
)
inlinenoexcept

Advances by samples at rateScale times the current rate (rate modulation; the scale is clamped to 0..64).

Definition at line 369 of file Lfo.h.

◆ beatsPerCycle()

template<FloatType T>
static constexpr double dspark::Lfo< T >::beatsPerCycle ( Division  d)
inlinestaticconstexprnoexcept

Beats (quarter notes) per cycle of a division.

Definition at line 111 of file Lfo.h.

◆ depthAt()

template<FloatType T>
double dspark::Lfo< T >::depthAt ( int64_t  samples) const
inlinenoexcept

Depth of the delay and fade-in samples after reset(): 0 during the delay, then a linear rise to 1.

Definition at line 384 of file Lfo.h.

◆ getCycles()

template<FloatType T>
double dspark::Lfo< T >::getCycles ( ) const
inlinenoexcept

Cycles run since reset(), start phase included.

Definition at line 337 of file Lfo.h.

◆ getNumSteps()

template<FloatType T>
int dspark::Lfo< T >::getNumSteps ( ) const
inlinenoexcept

Definition at line 233 of file Lfo.h.

◆ getRateHz()

template<FloatType T>
double dspark::Lfo< T >::getRateHz ( ) const
inlinenoexcept

Effective rate in Hz (the synced one when tempo sync is on).

Definition at line 334 of file Lfo.h.

◆ getShape()

template<FloatType T>
Shape dspark::Lfo< T >::getShape ( ) const
inlinenoexcept

Definition at line 166 of file Lfo.h.

◆ getStepCurve()

template<FloatType T>
double dspark::Lfo< T >::getStepCurve ( int  index) const
inlinenoexcept

Definition at line 265 of file Lfo.h.

◆ getStepGlide()

template<FloatType T>
double dspark::Lfo< T >::getStepGlide ( int  index) const
inlinenoexcept

Definition at line 264 of file Lfo.h.

◆ getStepQuantize()

template<FloatType T>
int dspark::Lfo< T >::getStepQuantize ( ) const
inlinenoexcept

Definition at line 290 of file Lfo.h.

◆ getStepSwing()

template<FloatType T>
double dspark::Lfo< T >::getStepSwing ( ) const
inlinenoexcept

Definition at line 277 of file Lfo.h.

◆ getStepValue()

template<FloatType T>
double dspark::Lfo< T >::getStepValue ( int  index) const
inlinenoexcept

Definition at line 263 of file Lfo.h.

◆ isStepRandom()

template<FloatType T>
bool dspark::Lfo< T >::isStepRandom ( int  index) const
inlinenoexcept

Definition at line 266 of file Lfo.h.

◆ prepare()

template<FloatType T>
void dspark::Lfo< T >::prepare ( double  sampleRate)
inlinenoexcept

Prepares for a sample rate (invalid rates are ignored).

Definition at line 142 of file Lfo.h.

◆ random()

template<FloatType T>
static double dspark::Lfo< T >::random ( uint64_t  seed,
int64_t  index 
)
inlinestaticnoexcept

Uniform value in [-1, 1) for a cycle index (splitmix64 hash).

Definition at line 427 of file Lfo.h.

◆ reset()

template<FloatType T>
void dspark::Lfo< T >::reset ( )
inlinenoexcept

Restarts the cycle at the start phase, the cycle count at zero and the delay and fade-in from the beginning.

Definition at line 154 of file Lfo.h.

◆ setDelayMs()

template<FloatType T>
void dspark::Lfo< T >::setDelayMs ( double  ms)
inlinenoexcept

Silence before the depth starts rising, in ms (>= 0).

Definition at line 196 of file Lfo.h.

◆ setDivision()

template<FloatType T>
void dspark::Lfo< T >::setDivision ( Division  d)
inlinenoexcept

Definition at line 178 of file Lfo.h.

◆ setFadeInMs()

template<FloatType T>
void dspark::Lfo< T >::setFadeInMs ( double  ms)
inlinenoexcept

Linear rise to full depth after the delay, in ms (>= 0).

Definition at line 204 of file Lfo.h.

◆ setNumSteps()

template<FloatType T>
void dspark::Lfo< T >::setNumSteps ( int  count)
inlinenoexcept

Steps in a cycle, 1..kMaxSteps (16, the default).

Definition at line 228 of file Lfo.h.

◆ setRateHz()

template<FloatType T>
void dspark::Lfo< T >::setRateHz ( double  hz)
inlinenoexcept

Free rate in Hz, 0..kMaxRateHz (NaN ignored). Used while sync is off.

Definition at line 169 of file Lfo.h.

◆ setRetriggerGlideMs()

template<FloatType T>
void dspark::Lfo< T >::setRetriggerGlideMs ( double  ms)
inlinenoexcept

How long reset() glides from the output it was at to the new start, in ms (0, the default, jumps; >= 0, non-finite ignored).

Definition at line 218 of file Lfo.h.

◆ setSeed()

template<FloatType T>
void dspark::Lfo< T >::setSeed ( uint64_t  seed)
inlinenoexcept

Seed of the random shapes (the same seed repeats the same sequence).

Definition at line 212 of file Lfo.h.

◆ setShape()

template<FloatType T>
void dspark::Lfo< T >::setShape ( Shape  s)
inlinenoexcept

Definition at line 165 of file Lfo.h.

◆ setStartPhase()

template<FloatType T>
void dspark::Lfo< T >::setStartPhase ( double  phase)
inlinenoexcept

Cycle position reset() starts at, 0..1 (wrapped).

Definition at line 189 of file Lfo.h.

◆ setStep()

template<FloatType T>
void dspark::Lfo< T >::setStep ( int  index,
double  value,
double  glide = 0.0,
double  curve = 0.0 
)
inlinenoexcept

One step: its value, how much of its length it glides to it from the step before, and the curve of that glide.

Parameters
index0..kMaxSteps-1 (others are ignored).
value-1..1.
glide0 (holds from its start) to 1 (glides over all of it).
curve-1 (slow start) to 1 (fast start); 0 is a straight line. Non-finite values are ignored.

Definition at line 245 of file Lfo.h.

◆ setStepQuantize()

template<FloatType T>
void dspark::Lfo< T >::setStepQuantize ( int  divisions)
inlinenoexcept

Quantizes the step values to a grid of 1/divisions (0, the default, leaves them free; 12 puts them on semitones when the owner maps -1..1 to -12..12 semitones). Glides move between the quantized values.

Definition at line 285 of file Lfo.h.

◆ setStepRandom()

template<FloatType T>
void dspark::Lfo< T >::setStepRandom ( int  index,
bool  random 
)
inlinenoexcept

Makes a step draw its value from the seed every cycle (see the file notes).

Definition at line 256 of file Lfo.h.

◆ setStepSwing()

template<FloatType T>
void dspark::Lfo< T >::setStepSwing ( double  swing)
inlinenoexcept

Moves the boundary inside each pair of steps, -1..1: at 0.5 the first step of a pair lasts 1.25 steps and the second 0.75 (the boundary moves by half the value, in steps).

Definition at line 273 of file Lfo.h.

◆ setTempo()

template<FloatType T>
void dspark::Lfo< T >::setTempo ( double  bpm)
inlinenoexcept

Tempo in BPM for tempo sync, 1..999 (invalid values ignored).

Definition at line 181 of file Lfo.h.

◆ setTempoSync()

template<FloatType T>
void dspark::Lfo< T >::setTempoSync ( bool  on)
inlinenoexcept

Tempo sync on or off; when on, the rate is the division of the tempo.

Definition at line 177 of file Lfo.h.

◆ stepsAt()

template<FloatType T>
double dspark::Lfo< T >::stepsAt ( double  cycles) const
inlinenoexcept

The Steps shape's value at an absolute cycle position (the integer part is the cycle whose random steps are drawn).

Definition at line 296 of file Lfo.h.

◆ value()

template<FloatType T>
T dspark::Lfo< T >::value ( ) const
inlinenoexcept

The output now (shape times delay/fade depth), without advancing.

Definition at line 342 of file Lfo.h.

◆ valueAt()

template<FloatType T>
static double dspark::Lfo< T >::valueAt ( Shape  shape,
double  cycles,
uint64_t  seed 
)
inlinestaticnoexcept

The shape's value at an absolute cycle position.

The integer part of cycles is the cycle index the random shapes hash with seed; the fraction is the phase within the cycle. Steps depends on an instance's steps: it reads 0 here (use stepsAt()).

Definition at line 399 of file Lfo.h.

Member Data Documentation

◆ kMaxRateHz

template<FloatType T>
constexpr double dspark::Lfo< T >::kMaxRateHz = 1000.0
staticconstexpr

Highest free rate (Hz).

Definition at line 139 of file Lfo.h.

◆ kMaxSteps

template<FloatType T>
constexpr int dspark::Lfo< T >::kMaxSteps = 16
staticconstexpr

Most steps of the Steps shape.

Definition at line 96 of file Lfo.h.

◆ kNumDivisions

template<FloatType T>
constexpr int dspark::Lfo< T >::kNumDivisions = 19
staticconstexpr

Definition at line 108 of file Lfo.h.

◆ kNumShapes

template<FloatType T>
constexpr int dspark::Lfo< T >::kNumShapes = 8
staticconstexpr

Definition at line 93 of file Lfo.h.


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