20#include "definitions_cxx.hpp"
21#include "dsp/filter/ladder_components.h"
22#include "dsp/stereo_sample.h"
26class [[gnu::hot]] RMSFeedbackCompressor {
28 RMSFeedbackCompressor();
31 constexpr void setup(q31_t a, q31_t r, q31_t t, q31_t rat, q31_t fc,
FixedPoint<31> blend,
float baseGain) {
58 void render(std::span<StereoSample> buffer, q31_t volAdjustL, q31_t volAdjustR, q31_t finalVolume);
63 void renderVolNeutral(std::span<StereoSample> buffer, q31_t finalVolume);
67 [[nodiscard]]
float runEnvelope(
float current,
float desired,
float numSamples)
const;
70 [[nodiscard]]
inline constexpr q31_t
getAttack()
const {
return attackKnobPos; }
73 [[nodiscard]]
inline constexpr float getAttackMS()
const {
return attackMS; }
80 attackMS = 0.5 + (std::exp(2 *
float(attack) / ONE_Q31f) - 1) * 10;
81 a_ = (-1000.0f / kSampleRate) / attackMS;
82 attackKnobPos = attack;
87 [[nodiscard]]
inline constexpr q31_t
getRelease()
const {
return releaseKnobPos; }
90 [[nodiscard]]
inline constexpr float getReleaseMS()
const {
return releaseMS; }
97 releaseMS = 50 + (std::exp(2 *
float(release) / ONE_Q31f) - 1) * 50;
98 r_ = (-1000.0f / kSampleRate) / releaseMS;
99 releaseKnobPos = release;
104 [[nodiscard]]
inline constexpr q31_t
getThreshold()
const {
return thresholdKnobPos; }
109 thresholdKnobPos = t;
110 threshold = 1 - 0.8f * (float(thresholdKnobPos) / ONE_Q31f);
114 [[nodiscard]]
inline constexpr q31_t
getRatio()
const {
return ratioKnobPos; }
124 fraction = 0.5f + (float(ratioKnobPos) / ONE_Q31f) / 2;
139 sideChainKnobPos = f;
142 fc_hz = (std::exp(1.5 *
float(f) / ONE_Q31f) - 1) * 30;
143 float fc = fc_hz / float(kSampleRate);
144 float wc = fc / (1 + fc);
145 hpfA_ = wc * ONE_Q31;
153 constexpr int32_t
getBlendForDisplay() {
return wet.raw() > (127 << 24) ? 100 : 100 * (wet.raw() >> 24) >> 7; }
171 void updateER(
float numSamples, q31_t finalVolume);
174 float calcRMS(std::span<StereoSample> buffer);
181 float a_ = (-1000.0f / kSampleRate);
183 float r_ = (-1000.0f / kSampleRate);
212 bool onLastTime =
false;
228 q31_t thresholdKnobPos = 0;
229 q31_t ratioKnobPos = 0;
230 q31_t attackKnobPos = 0;
231 q31_t releaseKnobPos = 0;
232 q31_t sideChainKnobPos = 0;
Fixed point number with a configurable number of fractional bits.
Definition fixedpoint.h:47
constexpr void setBaseGain(float baseGain)
Definition rms_feedback.h:168
constexpr void setThreshold(q31_t t)
Definition rms_feedback.h:108
constexpr q31_t getRatio() const
Get the current ratio as a full-scale (0 to 2^31) number.
Definition rms_feedback.h:114
uint8_t gainReduction
Amount of gain reduction applied during the last render pass, in 6.2 fixed point decibels.
Definition rms_feedback.h:177
constexpr int32_t setAttack(q31_t attack)
Definition rms_feedback.h:78
constexpr float getAttackMS() const
Get the current attack time constant in MS for a 3db change.
Definition rms_feedback.h:73
constexpr q31_t getAttack() const
Get the current attack time constant in terms of the full knob range (0 to 2^31)
Definition rms_feedback.h:70
float a_
Attack time constant, in inverse samples.
Definition rms_feedback.h:181
constexpr q31_t getRelease() const
Get the current release time constant in terms of the full knob range (0 to 2^31)
Definition rms_feedback.h:87
void reset()
Reset the state of the compressor so no gain reduction is applied at the start of the next render win...
Definition rms_feedback.h:42
uint32_t lastSaturationTanHWorkingValue[2]
tanh working values for output saturation
Definition rms_feedback.h:211
q31_t currentVolumeR
Current right channel volume as a 5.26 signed fixed-point number.
Definition rms_feedback.h:205
constexpr float getReleaseMS() const
Get the current release time constant in MS for a 3db change.
Definition rms_feedback.h:90
float threshdb
Threshold, in decibels.
Definition rms_feedback.h:194
constexpr int32_t setSidechain(q31_t f)
Definition rms_feedback.h:138
float state
State for the internal envelope follower.
Definition rms_feedback.h:201
float rms
Log-RMS value of the last render.
Definition rms_feedback.h:207
q31_t hpfA_
A parameter for the internal HPF.
Definition rms_feedback.h:198
constexpr q31_t getThreshold() const
Get the current theshold as a full-scale (0 to 2^31) number.
Definition rms_feedback.h:104
float er
Definition rms_feedback.h:192
constexpr float getSidechainForDisplay() const
Get the current sidechain cutoff frequency in hertz.
Definition rms_feedback.h:133
constexpr int32_t getBlendForDisplay()
returns blend as an integer percentage
Definition rms_feedback.h:153
constexpr int32_t setRatio(q31_t rat)
Definition rms_feedback.h:122
constexpr int32_t setRelease(q31_t release)
Definition rms_feedback.h:95
float r_
Release time constant, in inverse samples.
Definition rms_feedback.h:183
q31_t getSidechain()
Get the current sidechain cutoff frequency as a full-scale (0 to 2^31) integer.
Definition rms_feedback.h:130
constexpr FixedPoint< 31 > getBlend()
returns blend in q31
Definition rms_feedback.h:150
constexpr void setup(q31_t a, q31_t r, q31_t t, q31_t rat, q31_t fc, FixedPoint< 31 > blend, float baseGain)
takes in all values as knob positions in the range 0-ONE_Q31
Definition rms_feedback.h:31
float fraction
Definition rms_feedback.h:187
float threshold
The raw threshold value.
Definition rms_feedback.h:196
q31_t currentVolumeL
Current left channel volume as a 5.26 signed fixed-point number.
Definition rms_feedback.h:203
float mean
Mean value of the last render.
Definition rms_feedback.h:209
float baseGain_
Definition rms_feedback.h:225
constexpr int32_t setBlend(FixedPoint< 31 > blend)
Definition rms_feedback.h:157
constexpr float getRatioForDisplay() const
Get the current ratio as a float.
Definition rms_feedback.h:117
Definition ladder_components.h:23