Vital
Loading...
Searching...
No Matches
vital::RandomLfo Class Reference

A Low-Frequency Oscillator (LFO) that generates random modulation signals. More...

#include <random_lfo.h>

Inheritance diagram for vital::RandomLfo:
vital::Processor

Classes

struct  RandomState
 Holds the internal state of the RandomLfo for a given voice or channel. More...
 

Public Types

enum  {
  kFrequency , kAmplitude , kReset , kSync ,
  kStyle , kRandomType , kStereo , kNumInputs
}
 Input parameter indices for the RandomLfo. More...
 
enum  RandomType {
  kPerlin , kSampleAndHold , kSinInterpolate , kLorenzAttractor ,
  kNumStyles
}
 The types of random waveforms supported by RandomLfo. More...
 

Public Member Functions

 RandomLfo ()
 Constructs a RandomLfo processor with default parameters.
 
virtual Processorclone () const override
 Clones the RandomLfo, creating an identical instance.
 
void process (int num_samples) override
 Processes a block of samples.
 
void process (RandomState *state, int num_samples)
 Processes the LFO using the given state.
 
void processSampleAndHold (RandomState *state, int num_samples)
 Processes the LFO in Sample-And-Hold mode.
 
void processLorenzAttractor (RandomState *state, int num_samples)
 Processes the LFO using a Lorenz attractor model.
 
void correctToTime (double seconds)
 Adjusts the LFO to match a specific time reference (in seconds), for synchronization.
 
- Public Member Functions inherited from vital::Processor
 Processor (int num_inputs, int num_outputs, bool control_rate=false, int max_oversample=1)
 Constructs a Processor with a given number of inputs/outputs and oversampling.
 
virtual ~Processor ()
 Virtual destructor.
 
virtual bool hasState () const
 Indicates whether this Processor requires per-voice state.
 
virtual void processWithInput (const poly_float *audio_in, int num_samples)
 An optional processing function taking explicit input buffer. Fallback is an assertion failure (not supported).
 
virtual void init ()
 Called after constructor, used for any additional initialization. Subclasses can override. Sets the initialized flag.
 
virtual void reset (poly_mask reset_mask)
 Called to reset the Processor's per-voice state (e.g., on note-on).
 
virtual void hardReset ()
 Called to perform a "hard" reset for all voices.
 
bool initialized ()
 Returns whether this Processor has been initialized.
 
virtual void setSampleRate (int sample_rate)
 Updates the sample rate of this Processor (scaled by oversampling).
 
virtual void setOversampleAmount (int oversample)
 Sets the oversampling amount and updates the effective sample rate.
 
force_inline bool enabled () const
 Checks if this Processor is enabled.
 
virtual void enable (bool enable)
 Enables or disables this Processor.
 
force_inline int getSampleRate () const
 Retrieves the current (effective) sample rate.
 
force_inline int getOversampleAmount () const
 Retrieves the current oversampling factor.
 
force_inline bool isControlRate () const
 Checks if this Processor is running at control rate (buffer_size == 1).
 
virtual void setControlRate (bool control_rate)
 Sets whether this Processor runs at control rate.
 
force_inline poly_mask getResetMask (int input_index) const
 Retrieves a mask indicating which voices triggered a note-on event. Compares the input's trigger_value to kVoiceOn.
 
force_inline void clearOutputBufferForReset (poly_mask reset_mask, int input_index, int output_index) const
 Clears output samples for voices that are about to be reset, based on the trigger offset.
 
bool inputMatchesBufferSize (int input=0)
 Checks whether the buffer size of a particular input matches the size needed by this Processor.
 
bool checkInputAndOutputSize (int num_samples)
 Checks if all inputs and outputs have buffers big enough for num_samples.
 
virtual bool isPolyphonic () const
 Checks if this Processor is polyphonic by querying its ProcessorRouter.
 
void plug (const Output *source)
 Connects an external Output to this Processor's first input.
 
void plug (const Output *source, unsigned int input_index)
 Connects an external Output to a specified input index.
 
void plug (const Processor *source)
 Connects the first output of a Processor to this Processor's first input.
 
void plug (const Processor *source, unsigned int input_index)
 Connects the first output of a Processor to a specified input index.
 
void plugNext (const Output *source)
 Connects an external Output to the first available (unplugged) input.
 
void plugNext (const Processor *source)
 Connects the first output of a Processor to the first available (unplugged) input.
 
void useInput (Input *input)
 Uses an existing Input object as this Processor's first input.
 
void useInput (Input *input, int index)
 Uses an existing Input object at a specified input index.
 
void useOutput (Output *output)
 Uses an existing Output object as this Processor's first output.
 
void useOutput (Output *output, int index)
 Uses an existing Output object at a specified output index.
 
int connectedInputs ()
 Counts how many inputs are connected to a real source (not null_source_).
 
virtual void unplugIndex (unsigned int input_index)
 Removes the connection at a specified input index, if any.
 
virtual void unplug (const Output *source)
 Removes a connection to a given Output from all inputs.
 
virtual void unplug (const Processor *source)
 Removes connections to all outputs from a given Processor.
 
virtual void numInputsChanged ()
 Called when the number of inputs changes (e.g., new connections). Subclasses may override for dynamic behavior.
 
force_inline void router (ProcessorRouter *router)
 Sets the ProcessorRouter that owns or manages this Processor.
 
force_inline ProcessorRouterrouter () const
 Returns the ProcessorRouter that currently owns this Processor.
 
ProcessorRoutergetTopLevelRouter () const
 Gets the topmost (root) ProcessorRouter by traversing parent routers.
 
virtual void registerInput (Input *input, int index)
 Registers a new input, appending it to the input list.
 
virtual OutputregisterOutput (Output *output, int index)
 Registers a new Output in the output list at a specified index.
 
virtual void registerInput (Input *input)
 Registers a new Input by appending it to the end of the input list.
 
virtual OutputregisterOutput (Output *output)
 Registers a new Output by appending it to the end of the output list.
 
force_inline int numInputs () const
 Returns the total number of Input pointers (owned or otherwise).
 
force_inline int numOutputs () const
 Returns the total number of Output pointers (owned or otherwise).
 
force_inline int numOwnedInputs () const
 Returns how many Input objects this Processor owns.
 
force_inline int numOwnedOutputs () const
 Returns how many Output objects this Processor owns.
 
force_inline Inputinput (unsigned int index=0) const
 Retrieves the Input pointer at a given index.
 
force_inline bool isInputSourcePolyphonic (int index=0)
 Checks if the input source at a given index is polyphonic.
 
force_inline InputownedInput (unsigned int index=0) const
 Retrieves an owned Input pointer at a given index.
 
force_inline Outputoutput (unsigned int index=0) const
 Retrieves the Output pointer at a given index.
 
force_inline OutputownedOutput (unsigned int index=0) const
 Retrieves an owned Output pointer at a given index.
 
void setPluggingStart (int start)
 Sets the position at which plugNext starts searching for an open input.
 

Protected Member Functions

void doReset (RandomState *state, bool mono, poly_float frequency)
 Resets the LFO phase and random values if a reset trigger occurs.
 
poly_int updatePhase (RandomState *state, int num_samples)
 Updates the LFO phase and determines if a new random value is needed.
 
- Protected Member Functions inherited from vital::Processor
OutputaddOutput (int oversample=1)
 Creates and registers a new Output. Handles control rate vs. audio rate.
 
InputaddInput ()
 Creates and registers a new Input, initially connected to null_source_.
 

Protected Attributes

RandomState state_
 The main internal RandomState for this LFO instance.
 
std::shared_ptr< RandomStateshared_state_
 A shared RandomState (used when syncing across instances).
 
utils::RandomGenerator random_generator_
 A random generator to produce random values for the LFO.
 
poly_float last_value_
 The last output value of the LFO.
 
std::shared_ptr< double > sync_seconds_
 A shared double holding the sync reference time in seconds.
 
std::shared_ptr< double > last_sync_
 A shared double holding the last sync time for comparison.
 
- Protected Attributes inherited from vital::Processor
std::shared_ptr< ProcessorStatestate_
 Shared state (sample rate, oversample, etc.)
 
int plugging_start_
 The index at which plugNext starts searching for an unplugged input.
 
std::vector< std::shared_ptr< Input > > owned_inputs_
 Inputs owned by this Processor.
 
std::vector< std::shared_ptr< Output > > owned_outputs_
 Outputs owned by this Processor.
 
std::shared_ptr< std::vector< Input * > > inputs_
 All inputs, owned or external.
 
std::shared_ptr< std::vector< Output * > > outputs_
 All outputs, owned or external.
 
ProcessorRouterrouter_
 The ProcessorRouter that manages this Processor.
 

Additional Inherited Members

- Static Protected Attributes inherited from vital::Processor
static const Output null_source_
 A null (dummy) source used for unconnected inputs.
 

Detailed Description

A Low-Frequency Oscillator (LFO) that generates random modulation signals.

The RandomLfo class creates a variety of random waveforms, including Perlin-like noise, sample-and-hold stepped values, sinusoidal interpolations, and chaotic Lorenz attractors. It supports resetting on triggers, stereo or mono modes, frequency control, and optional tempo synchronization via an external time reference.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum

Input parameter indices for the RandomLfo.

  • kFrequency: The LFO's frequency.
  • kAmplitude: The amplitude or scaling factor of the output.
  • kReset: A trigger that resets the LFO state.
  • kSync: Enables syncing of the LFO to an external time reference.
  • kStyle: Determines the type of random waveform (Perlin, Sample & Hold, etc.).
  • kRandomType: Another parameter that selects random generation style (currently matches kStyle).
  • kStereo: Determines if LFO is mono or stereo. In stereo, voices may differ.
Enumerator
kFrequency 
kAmplitude 
kReset 
kSync 
kStyle 
kRandomType 
kStereo 
kNumInputs 

◆ RandomType

The types of random waveforms supported by RandomLfo.

  • kPerlin: Smooth noise (Perlin-like interpolation between random values).
  • kSampleAndHold: Stepped random values that change on each cycle.
  • kSinInterpolate: Random values with sinusoidal interpolation between them.
  • kLorenzAttractor: A chaotic waveform derived from the Lorenz attractor system.
Enumerator
kPerlin 
kSampleAndHold 
kSinInterpolate 
kLorenzAttractor 
kNumStyles 

Constructor & Destructor Documentation

◆ RandomLfo()

vital::RandomLfo::RandomLfo ( )

Constructs a RandomLfo processor with default parameters.

Member Function Documentation

◆ clone()

virtual Processor * vital::RandomLfo::clone ( ) const
inlineoverridevirtual

Clones the RandomLfo, creating an identical instance.

Returns
A pointer to the newly cloned RandomLfo.

Implements vital::Processor.

◆ correctToTime()

void vital::RandomLfo::correctToTime ( double seconds)

Adjusts the LFO to match a specific time reference (in seconds), for synchronization.

Parameters
secondsThe time offset in seconds to which the LFO should sync.

Adjusts the LFO to align with a given time in seconds, enabling tempo sync.

◆ doReset()

void vital::RandomLfo::doReset ( RandomState * state,
bool mono,
poly_float frequency )
protected

Resets the LFO phase and random values if a reset trigger occurs.

Parameters
stateThe RandomState to modify.
monoTrue if LFO is mono, false if stereo.
frequencyThe LFO frequency used to determine phase increments.

Handles resetting the LFO if a reset trigger occurs.

It checks for a reset trigger and, if present, resets the offset to account for the trigger time, and chooses new random values for the start and end points of the interpolation.

◆ process() [1/2]

void vital::RandomLfo::process ( int num_samples)
overridevirtual

Processes a block of samples.

Depending on control-rate or audio-rate processing, this method updates the LFO output. It also checks if synchronization or resetting is needed and applies the selected random style.

Parameters
num_samplesThe number of samples to process in the current block.

High-level processing entry point.

If syncing is enabled and the sync time has changed, processes the shared state. Otherwise, processes the internal state. It also handles control-rate vs. audio-rate interpolation of values.

Implements vital::Processor.

◆ process() [2/2]

void vital::RandomLfo::process ( RandomState * state,
int num_samples )

Processes the LFO using the given state.

This is a helper function for handling shared states between voices or instances. It updates random values, phases, and interpolations based on the configured style.

Parameters
statePointer to a RandomState to update.
num_samplesThe number of samples to process.

Processes the LFO for a given state without external sync considerations.

It selects the random type and processes accordingly. If it's Lorenz or Sample-And-Hold, it delegates to those specific methods. Otherwise, it updates the phase, interpolates random values, and writes the output. On audio-rate processing, it smoothly transitions between the old and new values.

◆ processLorenzAttractor()

void vital::RandomLfo::processLorenzAttractor ( RandomState * state,
int num_samples )

Processes the LFO using a Lorenz attractor model.

The Lorenz attractor produces chaotic waveforms influenced by nonlinear dynamics. This method simulates a small step of the Lorenz system for each sample.

Parameters
statePointer to the RandomState to update.
num_samplesThe number of samples to process.

Processes the LFO using the Lorenz attractor.

The Lorenz system generates a chaotic waveform. This method integrates the Lorenz equations over time to produce a varying output. The output is normalized to fit within [0, 1].

◆ processSampleAndHold()

void vital::RandomLfo::processSampleAndHold ( RandomState * state,
int num_samples )

Processes the LFO in Sample-And-Hold mode.

In Sample-And-Hold mode, the LFO outputs a constant random value until the next cycle, when it suddenly jumps to a new random value.

Parameters
statePointer to the RandomState to update.
num_samplesThe number of samples to process.

Processes the LFO in Sample-And-Hold mode.

The output stays constant over each cycle until the phase wraps around, at which point a new random value is chosen and held.

◆ updatePhase()

poly_int vital::RandomLfo::updatePhase ( RandomState * state,
int num_samples )
protected

Updates the LFO phase and determines if a new random value is needed.

It increments the phase based on the frequency and sample count. When the phase wraps past 1.0, a new random value is selected.

Parameters
stateThe RandomState being processed.
num_samplesThe number of samples to process.
Returns
An integer representing how many samples until a wrap occurs (for timing).

Advances the LFO phase by an amount determined by frequency and sample count.

If the LFO is sync'd, it sets the offset based on external timing. Otherwise, it moves forward normally and checks if a new random value should be chosen when wrapping around.

Returns
The number of samples until the phase wraps around, or 0 if no wrap occurs.

Member Data Documentation

◆ last_sync_

std::shared_ptr<double> vital::RandomLfo::last_sync_
protected

A shared double holding the last sync time for comparison.

◆ last_value_

poly_float vital::RandomLfo::last_value_
protected

The last output value of the LFO.

◆ random_generator_

utils::RandomGenerator vital::RandomLfo::random_generator_
protected

A random generator to produce random values for the LFO.

◆ shared_state_

std::shared_ptr<RandomState> vital::RandomLfo::shared_state_
protected

A shared RandomState (used when syncing across instances).

◆ state_

RandomState vital::RandomLfo::state_
protected

The main internal RandomState for this LFO instance.

◆ sync_seconds_

std::shared_ptr<double> vital::RandomLfo::sync_seconds_
protected

A shared double holding the sync reference time in seconds.


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