From bde447d6c907640bd931f2eb8d79cbb2cb692376 Mon Sep 17 00:00:00 2001 From: m-m-adams Date: Wed, 8 Nov 2023 16:08:21 -0500 Subject: [PATCH] cleanup code --- .../master_compressor/master_compressor.cpp | 27 ++++-------- .../dsp/master_compressor/master_compressor.h | 43 ++++++++++++------- .../global_effectable/global_effectable.cpp | 28 ++++++------ src/deluge/model/song/song.cpp | 8 ++-- 4 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/deluge/dsp/master_compressor/master_compressor.cpp b/src/deluge/dsp/master_compressor/master_compressor.cpp index 0a956b45ff..540be3507d 100644 --- a/src/deluge/dsp/master_compressor/master_compressor.cpp +++ b/src/deluge/dsp/master_compressor/master_compressor.cpp @@ -23,17 +23,13 @@ #include "processing/engines/audio_engine.h" #include "util/fast_fixed_math.h" MasterCompressor::MasterCompressor() { - //compressor.setAttack((float)attack / 100.0); - //compressor.setRelease((float)release / 100.0); - //compressor.setThresh((float)threshold / 100.0); - //compressor.setRatio(1.0 / ((float)ratio / 100.0)); - shape = getParamFromUserValue(Param::Unpatched::COMPRESSOR_SHAPE, 0); + //an appropriate range is 0-50*one q 15 - threshold = ONE_Q31; + rawThreshold = 0; - //this is about a 1:1 ratio - ratio = ONE_Q31 >> 1; + //this is 2:1 + rawRatio = 0; currentVolumeL = 0; currentVolumeR = 0; @@ -57,14 +53,13 @@ void MasterCompressor::updateER() { else { songVolume = 16; } - threshdb = songVolume * (threshold / ONE_Q31f); + threshdb = songVolume * threshold; //16 is about the level of a single synth voice at max volume er = std::max((songVolume - threshdb - 2) * ratio, 0); } void MasterCompressor::render(StereoSample* buffer, uint16_t numSamples, q31_t volAdjustL, q31_t volAdjustR) { - ratio = 0.5 + (float(rawRatio) / ONE_Q31f) / 2; - threshold = ONE_Q31 - (rawThreshold >> 1); + updateER(); float over = std::max(0, (rms - threshdb)); @@ -85,7 +80,7 @@ void MasterCompressor::render(StereoSample* buffer, uint16_t numSamples, q31_t v float gain = exp((dbGain)); gain = std::min(gain, 31); - lastGain = dbGain; + float finalVolumeL = gain * float(volAdjustL >> 9); float finalVolumeR = gain * float(volAdjustR >> 9); @@ -141,9 +136,6 @@ float MasterCompressor::calc_rms(StereoSample* buffer, uint16_t numSamples) { //good math would use a long FIR, this is a one pole IIR instead mean = (mean + lastMean) / 2; float rms = ONE_Q31 * sqrt(mean); - // float dc = 0; - - // mean = rms - dc / 1.4f; float logmean = std::log(std::max(rms, 1.0f)); @@ -153,7 +145,6 @@ float MasterCompressor::calc_rms(StereoSample* buffer, uint16_t numSamples) { void MasterCompressor::setup(int32_t a, int32_t r, int32_t t, int32_t rat) { setAttack(a); setRelease(r); - rawThreshold = t; - rawRatio = rat; - updateER(); + setThreshold(t); + setRatio(rat); } diff --git a/src/deluge/dsp/master_compressor/master_compressor.h b/src/deluge/dsp/master_compressor/master_compressor.h index 8033dcecf1..392f425e9c 100644 --- a/src/deluge/dsp/master_compressor/master_compressor.h +++ b/src/deluge/dsp/master_compressor/master_compressor.h @@ -35,38 +35,49 @@ class MasterCompressor { void render(StereoSample* buffer, uint16_t numSamples, q31_t volAdjustL, q31_t volAdjustR); float runEnvelope(float in, float numSamples); //attack/release in ms + int32_t getAttack() { return attackms; } void setAttack(int32_t attack) { a_ = (-1000.0 / 44100) / (float(attack)); attackms = attack; }; + int32_t getRelease() { return releasems; } void setRelease(int32_t release) { r_ = (-1000.0 / 44100) / (float(10 * release)); releasems = release; }; - void setRelease(); + q31_t getThreshold() { return rawThreshold; } + void setThreshold(q31_t t) { + rawThreshold = t; + threshold = 1 - ((rawThreshold >> 1) / ONE_Q31f); + updateER(); + } + q31_t getRatio() { return rawRatio; } + void setRatio(q31_t rat) { + rawRatio = rat; + ratio = 0.5 + (float(rawRatio) / ONE_Q31f) / 2; + updateER(); + } + void updateER(); float calc_rms(StereoSample* buffer, uint16_t numSamples); uint8_t gainReduction; - bool dither; - q31_t rawRatio; - q31_t rawThreshold; - q31_t threshold; - q31_t shape; - - q31_t over; - q31_t currentVolumeL; - q31_t currentVolumeR; - int32_t attackms; - int32_t releasems; - float rms; - float mean; - float lastGain; private: float a_; float r_; float ratio; float er; - float state; + float threshdb; + float state; + float rms; + float mean; + q31_t currentVolumeL; + q31_t currentVolumeR; + + q31_t rawThreshold; + q31_t rawRatio; + int32_t attackms; + int32_t releasems; + float threshold; }; diff --git a/src/deluge/model/global_effectable/global_effectable.cpp b/src/deluge/model/global_effectable/global_effectable.cpp index 5944087db8..5ca54c9cef 100644 --- a/src/deluge/model/global_effectable/global_effectable.cpp +++ b/src/deluge/model/global_effectable/global_effectable.cpp @@ -295,36 +295,34 @@ bool GlobalEffectable::modEncoderButtonAction(uint8_t whichModEncoder, bool on, } int32_t GlobalEffectable::getKnobPosForNonExistentParam(int32_t whichModEncoder, ModelStackWithAutoParam* modelStack) { - int displayLevel = -64; + int current; if (*getModKnobMode() == 4) { - int current; //this is only reachable in comp editing mode, otherwise it's an existent param if (whichModEncoder == 1) { //sidechain (threshold) - current = AudioEngine::mastercompressor.rawThreshold >> 24; - displayLevel = current; + current = (AudioEngine::mastercompressor.getThreshold() >> 24); } else if (whichModEncoder == 0) { switch (currentCompParam) { case CompParam::RATIO: - current = AudioEngine::mastercompressor.rawRatio >> 24; - displayLevel = current; + current = (AudioEngine::mastercompressor.getRatio() >> 24); + break; case CompParam::ATTACK: - current = AudioEngine::mastercompressor.attackms; + current = AudioEngine::mastercompressor.getAttack(); break; case CompParam::RELEASE: - current = AudioEngine::mastercompressor.releasems; + current = AudioEngine::mastercompressor.getRelease(); break; } } } - return displayLevel - 64; + return current - 64; } ActionResult GlobalEffectable::modEncoderActionForNonExistentParam(int32_t offset, int32_t whichModEncoder, @@ -335,30 +333,30 @@ ActionResult GlobalEffectable::modEncoderActionForNonExistentParam(int32_t offse int ledLevel; //this is only reachable in comp editing mode, otherwise it's an existent param if (whichModEncoder == 1) { //sidechain (threshold) - current = (AudioEngine::mastercompressor.rawThreshold >> 24) - 64; + current = (AudioEngine::mastercompressor.getThreshold() >> 24) - 64; current += offset; current = std::clamp(current, -64, 64); ledLevel = (64 + current); displayLevel = ((ledLevel)*kMaxMenuValue) / 128; - AudioEngine::mastercompressor.rawThreshold = lshiftAndSaturate<24>(current + 64); + AudioEngine::mastercompressor.setThreshold(lshiftAndSaturate<24>(current + 64)); indicator_leds::setKnobIndicatorLevel(1, ledLevel); } else if (whichModEncoder == 0) { switch (currentCompParam) { case CompParam::RATIO: - current = (AudioEngine::mastercompressor.rawRatio >> 24) - 64; + current = (AudioEngine::mastercompressor.getRatio() >> 24) - 64; current += offset; //this range is ratio of 2 to 20 current = std::clamp(current, -64, 64); ledLevel = (64 + current); displayLevel = ((ledLevel)*kMaxMenuValue) / 128; - AudioEngine::mastercompressor.rawRatio = lshiftAndSaturate<24>(current + 64); + AudioEngine::mastercompressor.setRatio(lshiftAndSaturate<24>(current + 64)); break; case CompParam::ATTACK: - current = AudioEngine::mastercompressor.attackms - 64; + current = AudioEngine::mastercompressor.getAttack() - 64; current += offset; current = std::clamp(current, -63, 64); ledLevel = (64 + current); @@ -368,7 +366,7 @@ ActionResult GlobalEffectable::modEncoderActionForNonExistentParam(int32_t offse break; case CompParam::RELEASE: - current = AudioEngine::mastercompressor.releasems - 64; + current = AudioEngine::mastercompressor.getRelease() - 64; current += offset; current = std::clamp(current, -63, 64); ledLevel = (64 + current); diff --git a/src/deluge/model/song/song.cpp b/src/deluge/model/song/song.cpp index 50b9cf6047..cadbb6e389 100644 --- a/src/deluge/model/song/song.cpp +++ b/src/deluge/model/song/song.cpp @@ -1123,10 +1123,10 @@ void Song::writeToFile() { storageManager.writeClosingTag("reverb"); storageManager.writeOpeningTagBeginning("songCompressor"); - int32_t attack = AudioEngine::mastercompressor.attackms; - int32_t release = AudioEngine::mastercompressor.releasems; - int32_t thresh = AudioEngine::mastercompressor.rawThreshold; - int32_t ratio = AudioEngine::mastercompressor.rawRatio; + int32_t attack = AudioEngine::mastercompressor.getAttack(); + int32_t release = AudioEngine::mastercompressor.getRelease(); + int32_t thresh = AudioEngine::mastercompressor.getThreshold(); + int32_t ratio = AudioEngine::mastercompressor.getRatio(); storageManager.writeAttribute("attack", attack); storageManager.writeAttribute("release", release);