Audacity 3.2.0
Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
DanielRudrich::GainReductionComputer Class Reference

#include <GainReductionComputer.h>

Collaboration diagram for DanielRudrich::GainReductionComputer:
[legend]

Public Member Functions

 GainReductionComputer ()
 
 ~GainReductionComputer ()
 
void setAttackTime (const float attackTimeInSeconds)
 
void setReleaseTime (const float releaseTimeInSeconds)
 
void setKnee (const float kneeInDecibels)
 
const float getKnee ()
 
void setThreshold (const float thresholdInDecibels)
 
const float getThreshold ()
 
void setMakeUpGain (const float makeUpGainInDecibels)
 
const float getMakeUpGain ()
 
void setRatio (const float ratio)
 
void getCharacteristic (float *inputLevelsInDecibels, float *destination, const int numSamples)
 
float getCharacteristicSample (const float inputLevelInDecibels)
 
void prepare (const double sampleRate)
 
void reset ()
 
void computeGainInDecibelsFromSidechainSignal (const float *sideChainSignal, float *destination, const int numSamples)
 
void computeLinearGainFromSidechainSignal (const float *sideChainSignal, float *destination, const int numSamples)
 
const float getMaxInputLevelInDecibels ()
 
const float getMaxGainReductionInDecibels ()
 

Static Public Member Functions

static float getCharacteristicSample (float inputLevelInDecibels, float kneeInDecibels, float thresholdInDecibels, float ratio, float makeUpGainInDecibels)
 

Private Member Functions

const float timeToGain (const float timeInSeconds)
 
const float applyCharacteristicToOverShoot (const float overShootInDecibels)
 

Static Private Member Functions

static float applyCharacteristicToOverShoot (float overShootInDecibels, float knee, float slope)
 

Private Attributes

double sampleRate
 
float knee
 
float kneeHalf
 
float threshold
 
float attackTime
 
float releaseTime
 
float slope
 
float makeUpGain
 
std::atomic< float > maxInputLevel {-std::numeric_limits<float>::infinity()}
 
std::atomic< float > maxGainReduction {0}
 
float state
 
float alphaAttack
 
float alphaRelease
 

Detailed Description

This class acts as the side-chain path of a dynamic range compressor. It processes a given side-chain signal and computes the gain reduction samples depending on the parameters threshold, knee, attack-time, release-time, ratio, and make-up gain.

Definition at line 30 of file GainReductionComputer.h.

Constructor & Destructor Documentation

◆ GainReductionComputer()

DanielRudrich::GainReductionComputer::GainReductionComputer ( )

Definition at line 41 of file GainReductionComputer.cpp.

References attackTime, knee, kneeHalf, makeUpGain, releaseTime, reset(), sampleRate, setRatio(), and threshold.

Here is the call graph for this function:

◆ ~GainReductionComputer()

DanielRudrich::GainReductionComputer::~GainReductionComputer ( )
inline

Definition at line 34 of file GainReductionComputer.h.

34{}

Member Function Documentation

◆ applyCharacteristicToOverShoot() [1/2]

const float DanielRudrich::GainReductionComputer::applyCharacteristicToOverShoot ( const float  overShootInDecibels)
inlineprivate

Definition at line 102 of file GainReductionComputer.cpp.

103{
104 return applyCharacteristicToOverShoot(overShootInDecibels, knee, slope);
105}
static float applyCharacteristicToOverShoot(float overShootInDecibels, float knee, float slope)

References applyCharacteristicToOverShoot(), knee, and slope.

Here is the call graph for this function:

◆ applyCharacteristicToOverShoot() [2/2]

float DanielRudrich::GainReductionComputer::applyCharacteristicToOverShoot ( float  overShootInDecibels,
float  knee,
float  slope 
)
staticprivate

Definition at line 107 of file GainReductionComputer.cpp.

109{
110 const auto kneeHalf = knee / 2;
111 if (overShootInDecibels <= -kneeHalf)
112 return 0.0f;
113 else if (overShootInDecibels > -kneeHalf && overShootInDecibels <= kneeHalf)
114 return 0.5f * slope * (overShootInDecibels + kneeHalf) * (overShootInDecibels + kneeHalf) / knee;
115 else
116 return slope * overShootInDecibels;
117}

References knee, kneeHalf, and slope.

Referenced by applyCharacteristicToOverShoot(), computeGainInDecibelsFromSidechainSignal(), and getCharacteristicSample().

Here is the caller graph for this function:

◆ computeGainInDecibelsFromSidechainSignal()

void DanielRudrich::GainReductionComputer::computeGainInDecibelsFromSidechainSignal ( const float *  sideChainSignal,
float *  destination,
const int  numSamples 
)

Computes the gain reduction for a given side-chain signal. The values will be in decibels and will NOT contain the make-up gain.

Definition at line 119 of file GainReductionComputer.cpp.

120{
121 maxInputLevel = -std::numeric_limits<float>::infinity();
122 maxGainReduction = 0.0f;
123
124 for (int i = 0; i < numSamples; ++i)
125 {
126 // convert sample to decibels
127 const float levelInDecibels =
128 log2ToDb * FastLog2(std::abs(sideChainSignal[i]));
129
130 if (levelInDecibels > maxInputLevel)
131 maxInputLevel = levelInDecibels;
132
133 // calculate overshoot and apply knee and ratio
134 const float overShoot = levelInDecibels - threshold;
135 const float gainReduction = applyCharacteristicToOverShoot (overShoot);
136
137 // apply ballistics
138 const float diff = gainReduction - state;
139 if (diff < 0.0f) // wanted gain reduction is below state -> attack phase
140 state += alphaAttack * diff;
141 else // release phase
142 state += alphaRelease * diff;
143
144 // write back gain reduction
145 destination[i] = state;
146
149 }
150}
constexpr float FastLog2(float x)
Approximates the base-2 logarithm of a float to two decimal places, adapted from https://stackoverflo...
Definition: MathApprox.h:32
static constexpr float log2ToDb
Definition: MathApprox.h:46

References alphaAttack, alphaRelease, applyCharacteristicToOverShoot(), FastLog2(), log2ToDb, maxGainReduction, maxInputLevel, state, and threshold.

Referenced by computeLinearGainFromSidechainSignal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ computeLinearGainFromSidechainSignal()

void DanielRudrich::GainReductionComputer::computeLinearGainFromSidechainSignal ( const float *  sideChainSignal,
float *  destination,
const int  numSamples 
)

Computes the linear gain including make-up gain for a given side-chain signal. The gain written to the destination can be directly applied to the signals which should be compressed.

Definition at line 152 of file GainReductionComputer.cpp.

153{
154 computeGainInDecibelsFromSidechainSignal (sideChainSignal, destination, numSamples);
155 for (int i = 0; i < numSamples; ++i)
156 destination[i] = std::pow (10.0f, 0.05f * (destination[i] + makeUpGain));
157}
void computeGainInDecibelsFromSidechainSignal(const float *sideChainSignal, float *destination, const int numSamples)

References computeGainInDecibelsFromSidechainSignal(), and makeUpGain.

Here is the call graph for this function:

◆ getCharacteristic()

void DanielRudrich::GainReductionComputer::getCharacteristic ( float *  inputLevelsInDecibels,
float *  destination,
const int  numSamples 
)

Computes the static output levels for an array of input levels in decibels. Useful for visualization of the compressor's characteristic. Will contain make-up gain.

Definition at line 160 of file GainReductionComputer.cpp.

161{
162 for (int i = 0; i < numSamples; ++i)
163 dest[i] = getCharacteristicSample (inputLevelsInDecibels[i]);
164}
static float getCharacteristicSample(float inputLevelInDecibels, float kneeInDecibels, float thresholdInDecibels, float ratio, float makeUpGainInDecibels)

References getCharacteristicSample().

Here is the call graph for this function:

◆ getCharacteristicSample() [1/2]

float DanielRudrich::GainReductionComputer::getCharacteristicSample ( const float  inputLevelInDecibels)

Computes the static output levels for a given input level in decibels. Useful for visualization of the compressor's characteristic. Will contain make-up gain.

Definition at line 166 of file GainReductionComputer.cpp.

References getCharacteristicSample(), DanielRudrich::anonymous_namespace{GainReductionComputer.cpp}::getRatio(), knee, makeUpGain, slope, and threshold.

Here is the call graph for this function:

◆ getCharacteristicSample() [2/2]

float DanielRudrich::GainReductionComputer::getCharacteristicSample ( float  inputLevelInDecibels,
float  kneeInDecibels,
float  thresholdInDecibels,
float  ratio,
float  makeUpGainInDecibels 
)
static

Definition at line 29 of file GainReductionComputer.cpp.

34{
35 const auto slope = getSlope(ratio);
36 float overShoot = inputLevelInDecibels - thresholdInDecibels;
37 overShoot = applyCharacteristicToOverShoot(overShoot, kneeInDecibels, slope);
38 return overShoot + inputLevelInDecibels + makeUpGainInDecibels;
39}

References applyCharacteristicToOverShoot(), DanielRudrich::anonymous_namespace{GainReductionComputer.cpp}::getSlope(), and slope.

Referenced by CompressorProcessor::EvaluateTransferFunction(), getCharacteristic(), and getCharacteristicSample().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getKnee()

const float DanielRudrich::GainReductionComputer::getKnee ( )
inline

Definition at line 56 of file GainReductionComputer.h.

56{ return knee; }

References knee.

◆ getMakeUpGain()

const float DanielRudrich::GainReductionComputer::getMakeUpGain ( )
inline

Definition at line 68 of file GainReductionComputer.h.

68{ return makeUpGain; }

References makeUpGain.

◆ getMaxGainReductionInDecibels()

const float DanielRudrich::GainReductionComputer::getMaxGainReductionInDecibels ( )
inline

Definition at line 108 of file GainReductionComputer.h.

108{ return maxGainReduction; }

References maxGainReduction.

◆ getMaxInputLevelInDecibels()

const float DanielRudrich::GainReductionComputer::getMaxInputLevelInDecibels ( )
inline

Definition at line 107 of file GainReductionComputer.h.

107{ return maxInputLevel; }

References maxInputLevel.

◆ getThreshold()

const float DanielRudrich::GainReductionComputer::getThreshold ( )
inline

Definition at line 62 of file GainReductionComputer.h.

62{ return threshold; }

References threshold.

◆ prepare()

void DanielRudrich::GainReductionComputer::prepare ( const double  sampleRate)

Prepares the compressor with sampleRate and expected blockSize. Make sure you call this before you do any processing!

Definition at line 55 of file GainReductionComputer.cpp.

56{
57 sampleRate = newSampleRate;
58
61}
const float timeToGain(const float timeInSeconds)

References alphaAttack, alphaRelease, attackTime, releaseTime, sampleRate, and timeToGain().

Here is the call graph for this function:

◆ reset()

void DanielRudrich::GainReductionComputer::reset ( )
inline

Resets the internal state of the compressor.

Definition at line 95 of file GainReductionComputer.h.

95{ state = 0.0f; }

References state.

Referenced by GainReductionComputer().

Here is the caller graph for this function:

◆ setAttackTime()

void DanielRudrich::GainReductionComputer::setAttackTime ( const float  attackTimeInSeconds)

Sets the attack time of the compressor in seconds.

Definition at line 63 of file GainReductionComputer.cpp.

64{
65 attackTime = attackTimeInSeconds;
67}

References alphaAttack, attackTime, and timeToGain().

Here is the call graph for this function:

◆ setKnee()

void DanielRudrich::GainReductionComputer::setKnee ( const float  kneeInDecibels)

Sets the knee-width in decibels.

Definition at line 80 of file GainReductionComputer.cpp.

81{
82 knee = kneeInDecibels;
83 kneeHalf = knee / 2.0f;
84}

References knee, and kneeHalf.

◆ setMakeUpGain()

void DanielRudrich::GainReductionComputer::setMakeUpGain ( const float  makeUpGainInDecibels)

Sets the make-up-gain of the compressor in decibels.

Definition at line 91 of file GainReductionComputer.cpp.

92{
93 makeUpGain = makeUpGainInDecibels;
94}

References makeUpGain.

◆ setRatio()

void DanielRudrich::GainReductionComputer::setRatio ( const float  ratio)

Sets the ratio of input-output signal above threshold. Set to 1 for no compression, up to infinity for a brickwall limiter.

Definition at line 96 of file GainReductionComputer.cpp.

97{
98 slope = getSlope(ratio);
99}

References DanielRudrich::anonymous_namespace{GainReductionComputer.cpp}::getSlope(), and slope.

Referenced by GainReductionComputer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setReleaseTime()

void DanielRudrich::GainReductionComputer::setReleaseTime ( const float  releaseTimeInSeconds)

Sets the release time of the compressorin seconds

Definition at line 69 of file GainReductionComputer.cpp.

70{
71 releaseTime = releaseTimeInSeconds;
73}

References alphaRelease, releaseTime, and timeToGain().

Here is the call graph for this function:

◆ setThreshold()

void DanielRudrich::GainReductionComputer::setThreshold ( const float  thresholdInDecibels)

Sets the threshold above which the compressor will start to compress the signal.

Definition at line 86 of file GainReductionComputer.cpp.

87{
88 threshold = thresholdInDecibels;
89}

References threshold.

◆ timeToGain()

const float DanielRudrich::GainReductionComputer::timeToGain ( const float  timeInSeconds)
inlineprivate

Definition at line 75 of file GainReductionComputer.cpp.

76{
77 return std::exp (-1.0f / (static_cast<float> (sampleRate) * timeInSeconds));
78}

References sampleRate.

Referenced by prepare(), setAttackTime(), and setReleaseTime().

Here is the caller graph for this function:

Member Data Documentation

◆ alphaAttack

float DanielRudrich::GainReductionComputer::alphaAttack
private

◆ alphaRelease

float DanielRudrich::GainReductionComputer::alphaRelease
private

◆ attackTime

float DanielRudrich::GainReductionComputer::attackTime
private

Definition at line 122 of file GainReductionComputer.h.

Referenced by GainReductionComputer(), prepare(), and setAttackTime().

◆ knee

float DanielRudrich::GainReductionComputer::knee
private

◆ kneeHalf

float DanielRudrich::GainReductionComputer::kneeHalf
private

◆ makeUpGain

float DanielRudrich::GainReductionComputer::makeUpGain
private

◆ maxGainReduction

std::atomic<float> DanielRudrich::GainReductionComputer::maxGainReduction {0}
private

◆ maxInputLevel

std::atomic<float> DanielRudrich::GainReductionComputer::maxInputLevel {-std::numeric_limits<float>::infinity()}
private

◆ releaseTime

float DanielRudrich::GainReductionComputer::releaseTime
private

Definition at line 123 of file GainReductionComputer.h.

Referenced by GainReductionComputer(), prepare(), and setReleaseTime().

◆ sampleRate

double DanielRudrich::GainReductionComputer::sampleRate
private

Definition at line 117 of file GainReductionComputer.h.

Referenced by GainReductionComputer(), prepare(), and timeToGain().

◆ slope

float DanielRudrich::GainReductionComputer::slope
private

◆ state

float DanielRudrich::GainReductionComputer::state
private

Definition at line 131 of file GainReductionComputer.h.

Referenced by computeGainInDecibelsFromSidechainSignal(), and reset().

◆ threshold

float DanielRudrich::GainReductionComputer::threshold
private

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