Skip to content

Commit

Permalink
cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
m-m-adams committed Nov 8, 2023
1 parent fb0d494 commit bde447d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 53 deletions.
27 changes: 9 additions & 18 deletions src/deluge/dsp/master_compressor/master_compressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<float>((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<float>(0, (rms - threshdb));
Expand All @@ -85,7 +80,7 @@ void MasterCompressor::render(StereoSample* buffer, uint16_t numSamples, q31_t v

float gain = exp((dbGain));
gain = std::min<float>(gain, 31);
lastGain = dbGain;

float finalVolumeL = gain * float(volAdjustL >> 9);
float finalVolumeR = gain * float(volAdjustR >> 9);

Expand Down Expand Up @@ -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));

Expand All @@ -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);
}
43 changes: 27 additions & 16 deletions src/deluge/dsp/master_compressor/master_compressor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
28 changes: 13 additions & 15 deletions src/deluge/model/global_effectable/global_effectable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/deluge/model/song/song.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit bde447d

Please sign in to comment.