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

A dynamic range compressor Processor that operates on a single band of audio. More...

#include <compressor.h>

Inheritance diagram for vital::Compressor:
vital::Processor

Public Types

enum  {
  kAudio , kUpperThreshold , kLowerThreshold , kUpperRatio ,
  kLowerRatio , kOutputGain , kAttack , kRelease ,
  kMix , kNumInputs
}
 
enum  { kAudioOut , kNumOutputs }
 

Public Member Functions

 Compressor (mono_float base_attack_ms_first, mono_float base_release_ms_first, mono_float base_attack_ms_second, mono_float base_release_ms_second)
 Constructs a Compressor Processor with given base attack and release times.
 
virtual ~Compressor ()
 Default destructor.
 
virtual Processorclone () const override
 Creates a clone of this Processor. (Not implemented for Compressor).
 
virtual void process (int num_samples) override
 Processes audio using the input audio buffer, modifying output buffer in-place.
 
virtual void processWithInput (const poly_float *audio_in, int num_samples) override
 Processes audio using the provided input buffer and writes to output.
 
void processRms (const poly_float *audio_in, int num_samples)
 Processes RMS for the input buffer and applies compression gain.
 
void scaleOutput (const poly_float *audio_input, int num_samples)
 Applies the final output scaling and dry/wet mix to the processed audio.
 
void reset (poly_mask reset_mask) override
 Resets internal states and envelopes.
 
force_inline poly_float getInputMeanSquared ()
 Retrieves the current input RMS value (mean squared).
 
force_inline poly_float getOutputMeanSquared ()
 Retrieves the current output RMS value (mean squared).
 
- 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 init ()
 Called after constructor, used for any additional initialization. Subclasses can override. Sets the initialized flag.
 
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

poly_float computeMeanSquared (const poly_float *audio_in, int num_samples, poly_float mean_squared)
 Computes the mean squared value over a buffer of samples.
 
- 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

poly_float input_mean_squared_
 Rolling mean squared value of the input signal.
 
poly_float output_mean_squared_
 Rolling mean squared value of the output signal.
 
poly_float high_enveloped_mean_squared_
 Internal high enveloped mean squared value for upper threshold detection.
 
poly_float low_enveloped_mean_squared_
 Internal low enveloped mean squared value for lower threshold detection.
 
poly_float mix_
 The current dry/wet mix (0.0 = fully dry, 1.0 = fully wet).
 
poly_float base_attack_ms_
 Base attack time in ms for the current voice.
 
poly_float base_release_ms_
 Base release time in ms for the current voice.
 
poly_float output_mult_
 Current multiplier for output gain (converted from dB).
 
- 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 dynamic range compressor Processor that operates on a single band of audio.

The Compressor class applies compression/expansion based on two thresholds (upper and lower) and corresponding ratios. It tracks the input and output RMS levels and adjusts audio samples in real time according to configured attack and release times.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
kAudio 

Input audio signal.

kUpperThreshold 

Upper threshold in dB.

kLowerThreshold 

Lower threshold in dB.

kUpperRatio 

Upper ratio (compression ratio above upper threshold)

kLowerRatio 

Lower ratio (expansion ratio below lower threshold)

kOutputGain 

Output gain (dB)

kAttack 

Attack time control (0.0 to 1.0 maps to exponential range)

kRelease 

Release time control (0.0 to 1.0 maps to exponential range)

kMix 

Dry/Wet mix.

kNumInputs 

Total number of inputs.

◆ anonymous enum

anonymous enum
Enumerator
kAudioOut 

Compressed audio output.

kNumOutputs 

Total number of outputs.

Constructor & Destructor Documentation

◆ Compressor()

vital::Compressor::Compressor ( mono_float base_attack_ms_first,
mono_float base_release_ms_first,
mono_float base_attack_ms_second,
mono_float base_release_ms_second )

Constructs a Compressor Processor with given base attack and release times.

Constructs a Compressor with specified base attack and release times for two voices.

Parameters
base_attack_ms_firstBase attack time in ms for the first voice.
base_release_ms_firstBase release time in ms for the first voice.
base_attack_ms_secondBase attack time in ms for the second voice (polyphonic).
base_release_ms_secondBase release time in ms for the second voice (polyphonic).

Initializes Processor with the correct number of inputs/outputs and sets envelope states.

◆ ~Compressor()

virtual vital::Compressor::~Compressor ( )
inlinevirtual

Default destructor.

Member Function Documentation

◆ clone()

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

Creates a clone of this Processor. (Not implemented for Compressor).

Returns
Returns nullptr.

Implements vital::Processor.

◆ computeMeanSquared()

poly_float vital::Compressor::computeMeanSquared ( const poly_float * audio_in,
int num_samples,
poly_float mean_squared )
protected

Computes the mean squared value over a buffer of samples.

Calculates a rolling mean squared value (RMS) over the input buffer.

Parameters
audio_inPointer to the audio buffer.
num_samplesNumber of samples to process.
mean_squaredThe current rolling mean squared value to update.
Returns
Updated mean squared value.
Parameters
audio_inPointer to the audio data.
num_samplesNumber of samples to process.
mean_squaredThe previous mean squared value to be updated.
Returns
Updated mean squared value after processing all samples.

◆ getInputMeanSquared()

force_inline poly_float vital::Compressor::getInputMeanSquared ( )
inline

Retrieves the current input RMS value (mean squared).

Returns
The input mean squared level.

◆ getOutputMeanSquared()

force_inline poly_float vital::Compressor::getOutputMeanSquared ( )
inline

Retrieves the current output RMS value (mean squared).

Returns
The output mean squared level.

◆ process()

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

Processes audio using the input audio buffer, modifying output buffer in-place.

Processes audio by pulling from kAudio input and writing to kAudioOut output.

Parameters
num_samplesNumber of samples to process.

Implements vital::Processor.

◆ processRms()

void vital::Compressor::processRms ( const poly_float * audio_in,
int num_samples )

Processes RMS for the input buffer and applies compression gain.

Computes a running envelope (RMS) for upper/lower thresholds and applies compression gain.

Parameters
audio_inPointer to the input buffer.
num_samplesNumber of samples to process.

◆ processWithInput()

void vital::Compressor::processWithInput ( const poly_float * audio_in,
int num_samples )
overridevirtual

Processes audio using the provided input buffer and writes to output.

Processes audio with the given input buffer.

Parameters
audio_inPointer to the input buffer.
num_samplesNumber of samples to process.

Calls processRms() to calculate RMS levels and then adjusts output volume accordingly.

Parameters
audio_inPointer to the input buffer.
num_samplesNumber of samples to process.

Reimplemented from vital::Processor.

◆ reset()

void vital::Compressor::reset ( poly_mask reset_mask)
overridevirtual

Resets internal states and envelopes.

Resets the compressor’s internal RMS, envelopes, and gains.

Parameters
reset_maskA poly_mask specifying which voices should be reset.

Reimplemented from vital::Processor.

◆ scaleOutput()

void vital::Compressor::scaleOutput ( const poly_float * audio_input,
int num_samples )

Applies the final output scaling and dry/wet mix to the processed audio.

Applies output gain and dry/wet mix to the processed audio signal.

Parameters
audio_inputPointer to the unmodified input buffer (used for dry/wet blend).
num_samplesNumber of samples to process.
audio_inputPointer to the original unprocessed input (for the dry signal).
num_samplesNumber of samples to process.

Member Data Documentation

◆ base_attack_ms_

poly_float vital::Compressor::base_attack_ms_
protected

Base attack time in ms for the current voice.

◆ base_release_ms_

poly_float vital::Compressor::base_release_ms_
protected

Base release time in ms for the current voice.

◆ high_enveloped_mean_squared_

poly_float vital::Compressor::high_enveloped_mean_squared_
protected

Internal high enveloped mean squared value for upper threshold detection.

◆ input_mean_squared_

poly_float vital::Compressor::input_mean_squared_
protected

Rolling mean squared value of the input signal.

◆ low_enveloped_mean_squared_

poly_float vital::Compressor::low_enveloped_mean_squared_
protected

Internal low enveloped mean squared value for lower threshold detection.

◆ mix_

poly_float vital::Compressor::mix_
protected

The current dry/wet mix (0.0 = fully dry, 1.0 = fully wet).

◆ output_mean_squared_

poly_float vital::Compressor::output_mean_squared_
protected

Rolling mean squared value of the output signal.

◆ output_mult_

poly_float vital::Compressor::output_mult_
protected

Current multiplier for output gain (converted from dB).


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