mirror of
https://github.com/radio95-rnt/fm95.git
synced 2026-07-31 16:59:16 +02:00
try a diffrent compressor
This commit is contained in:
+47
-46
@@ -129,7 +129,7 @@ void init_compressor(Compressor *compressor, float threshold, float ratio, float
|
|||||||
compressor->attack = attack;
|
compressor->attack = attack;
|
||||||
compressor->release = release;
|
compressor->release = release;
|
||||||
compressor->sample_rate = sample_rate;
|
compressor->sample_rate = sample_rate;
|
||||||
compressor->gainReduction = 0.0f; // now this will become negative for boost
|
compressor->gainReduction = 0.0f;
|
||||||
compressor->rmsEnv = 0.0f;
|
compressor->rmsEnv = 0.0f;
|
||||||
compressor->rmsTime = rmsTime;
|
compressor->rmsTime = rmsTime;
|
||||||
}
|
}
|
||||||
@@ -142,31 +142,31 @@ float rms_compress(Compressor *compressor, float sample) {
|
|||||||
|
|
||||||
float input_db = voltage_to_voltage_db(env);
|
float input_db = voltage_to_voltage_db(env);
|
||||||
|
|
||||||
float targetGR = 0.0f;
|
float targetBoost = 0.0f;
|
||||||
if(input_db < compressor->threshold) {
|
if(input_db < compressor->threshold) {
|
||||||
if(compressor->knee > 0.0f) {
|
if(compressor->knee > 0.0f) {
|
||||||
float delta = compressor->threshold - input_db; // positive difference
|
float delta = compressor->threshold - input_db;
|
||||||
if(delta < compressor->knee / 2.0f) {
|
if(delta < compressor->knee / 2.0f) {
|
||||||
targetGR = -(1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
|
targetBoost = (1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
|
||||||
} else {
|
} else {
|
||||||
targetGR = -(1.0f - 1.0f / compressor->ratio) * delta;
|
targetBoost = (1.0f - 1.0f / compressor->ratio) * delta;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
targetGR = -(1.0f - 1.0f / compressor->ratio) * (compressor->threshold - input_db);
|
targetBoost = (1.0f - 1.0f / compressor->ratio) * (compressor->threshold - input_db);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
targetGR = 0.0f;
|
targetBoost = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
float coeff;
|
float coeff;
|
||||||
if(targetGR < compressor->gainReduction) {
|
if(targetBoost > compressor->gainReduction) {
|
||||||
coeff = expf(-1.0f / (compressor->attack * compressor->sample_rate));
|
coeff = expf(-1.0f / (compressor->attack * compressor->sample_rate));
|
||||||
} else {
|
} else {
|
||||||
coeff = expf(-1.0f / (compressor->release * compressor->sample_rate));
|
coeff = expf(-1.0f / (compressor->release * compressor->sample_rate));
|
||||||
}
|
}
|
||||||
compressor->gainReduction = coeff * compressor->gainReduction + (1.0f - coeff) * targetGR;
|
compressor->gainReduction = coeff * compressor->gainReduction + (1.0f - coeff) * targetBoost;
|
||||||
|
|
||||||
float gain = voltage_db_to_voltage(compressor->makeup_gain - compressor->gainReduction);
|
float gain = voltage_db_to_voltage(compressor->makeup_gain + compressor->gainReduction);
|
||||||
return sample * gain;
|
return sample * gain;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,34 +174,35 @@ float peak_compress(Compressor *compressor, float sample) {
|
|||||||
float env = fabsf(sample);
|
float env = fabsf(sample);
|
||||||
float input_db = voltage_to_voltage_db(env);
|
float input_db = voltage_to_voltage_db(env);
|
||||||
|
|
||||||
float targetGR = 0.0f;
|
float targetBoost = 0.0f;
|
||||||
if(input_db < compressor->threshold) {
|
if(input_db < compressor->threshold) {
|
||||||
if(compressor->knee > 0.0f) {
|
if(compressor->knee > 0.0f) {
|
||||||
float delta = compressor->threshold - input_db;
|
float delta = compressor->threshold - input_db;
|
||||||
if(delta < compressor->knee / 2.0f) {
|
if(delta < compressor->knee / 2.0f) {
|
||||||
targetGR = -(1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
|
targetBoost = (1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
|
||||||
} else {
|
} else {
|
||||||
targetGR = -(1.0f - 1.0f / compressor->ratio) * delta;
|
targetBoost = (1.0f - 1.0f / compressor->ratio) * delta;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
targetGR = -(1.0f - 1.0f / compressor->ratio) * (compressor->threshold - input_db);
|
targetBoost = (1.0f - 1.0f / compressor->ratio) * (compressor->threshold - input_db);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
targetGR = 0.0f;
|
targetBoost = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
float coeff;
|
float coeff;
|
||||||
if(targetGR < compressor->gainReduction) {
|
if(targetBoost > compressor->gainReduction) {
|
||||||
coeff = expf(-1.0f / (compressor->attack * compressor->sample_rate));
|
coeff = expf(-1.0f / (compressor->attack * compressor->sample_rate));
|
||||||
} else {
|
} else {
|
||||||
coeff = expf(-1.0f / (compressor->release * compressor->sample_rate));
|
coeff = expf(-1.0f / (compressor->release * compressor->sample_rate));
|
||||||
}
|
}
|
||||||
compressor->gainReduction = coeff * compressor->gainReduction + (1.0f - coeff) * targetGR;
|
compressor->gainReduction = coeff * compressor->gainReduction + (1.0f - coeff) * targetBoost;
|
||||||
|
|
||||||
float gain = voltage_db_to_voltage(compressor->makeup_gain - compressor->gainReduction);
|
float gain = voltage_db_to_voltage(compressor->makeup_gain + compressor->gainReduction);
|
||||||
return sample * gain;
|
return sample * gain;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void init_compressor_stereo(StereoCompressor *compressor, float threshold, float ratio, float knee, float makeup_gain, float attack, float release, float rmsTime, float sample_rate) {
|
void init_compressor_stereo(StereoCompressor *compressor, float threshold, float ratio, float knee, float makeup_gain, float attack, float release, float rmsTime, float sample_rate) {
|
||||||
compressor->threshold = threshold;
|
compressor->threshold = threshold;
|
||||||
compressor->ratio = ratio;
|
compressor->ratio = ratio;
|
||||||
@@ -227,49 +228,49 @@ float rms_compress_stereo(StereoCompressor *compressor, float l, float r, float
|
|||||||
float input_db_l = voltage_to_voltage_db(env_l);
|
float input_db_l = voltage_to_voltage_db(env_l);
|
||||||
float input_db_r = voltage_to_voltage_db(env_r);
|
float input_db_r = voltage_to_voltage_db(env_r);
|
||||||
|
|
||||||
float targetGR_l = 0.0f;
|
float targetBoost_l = 0.0f;
|
||||||
if(input_db_l < compressor->threshold) {
|
if(input_db_l < compressor->threshold) {
|
||||||
if(compressor->knee > 0.0f) {
|
if(compressor->knee > 0.0f) {
|
||||||
float delta = compressor->threshold - input_db_l;
|
float delta = compressor->threshold - input_db_l;
|
||||||
if(delta < compressor->knee / 2.0f) {
|
if(delta < compressor->knee / 2.0f) {
|
||||||
targetGR_l = -(1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
|
targetBoost_l = (1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
|
||||||
} else {
|
} else {
|
||||||
targetGR_l = -(1.0f - 1.0f / compressor->ratio) * delta;
|
targetBoost_l = (1.0f - 1.0f / compressor->ratio) * delta;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
targetGR_l = -(1.0f - 1.0f / compressor->ratio) * (compressor->threshold - input_db_l);
|
targetBoost_l = (1.0f - 1.0f / compressor->ratio) * (compressor->threshold - input_db_l);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
targetGR_l = 0.0f;
|
targetBoost_l = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
float targetGR_r = 0.0f;
|
float targetBoost_r = 0.0f;
|
||||||
if(input_db_r < compressor->threshold) {
|
if(input_db_r < compressor->threshold) {
|
||||||
if(compressor->knee > 0.0f) {
|
if(compressor->knee > 0.0f) {
|
||||||
float delta = compressor->threshold - input_db_r;
|
float delta = compressor->threshold - input_db_r;
|
||||||
if(delta < compressor->knee / 2.0f) {
|
if(delta < compressor->knee / 2.0f) {
|
||||||
targetGR_r = -(1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
|
targetBoost_r = (1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
|
||||||
} else {
|
} else {
|
||||||
targetGR_r = -(1.0f - 1.0f / compressor->ratio) * delta;
|
targetBoost_r = (1.0f - 1.0f / compressor->ratio) * delta;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
targetGR_r = -(1.0f - 1.0f / compressor->ratio) * (compressor->threshold - input_db_r);
|
targetBoost_r = (1.0f - 1.0f / compressor->ratio) * (compressor->threshold - input_db_r);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
targetGR_r = 0.0f;
|
targetBoost_r = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
float shared_target_gr = (targetGR_l < targetGR_r) ? targetGR_l : targetGR_r;
|
float shared_target_boost = (targetBoost_l > targetBoost_r) ? targetBoost_l : targetBoost_r;
|
||||||
|
|
||||||
float coeff;
|
float coeff;
|
||||||
if(shared_target_gr < compressor->gainReduction) {
|
if(shared_target_boost > compressor->gainReduction) {
|
||||||
coeff = expf(-1.0f / (compressor->attack * compressor->sample_rate));
|
coeff = expf(-1.0f / (compressor->attack * compressor->sample_rate));
|
||||||
} else {
|
} else {
|
||||||
coeff = expf(-1.0f / (compressor->release * compressor->sample_rate));
|
coeff = expf(-1.0f / (compressor->release * compressor->sample_rate));
|
||||||
}
|
}
|
||||||
compressor->gainReduction = coeff * compressor->gainReduction + (1.0f - coeff) * shared_target_gr;
|
compressor->gainReduction = coeff * compressor->gainReduction + (1.0f - coeff) * shared_target_boost;
|
||||||
|
|
||||||
float gain = voltage_db_to_voltage(compressor->makeup_gain - compressor->gainReduction);
|
float gain = voltage_db_to_voltage(compressor->makeup_gain + compressor->gainReduction);
|
||||||
*output_r = r * gain;
|
*output_r = r * gain;
|
||||||
return l * gain;
|
return l * gain;
|
||||||
}
|
}
|
||||||
@@ -281,49 +282,49 @@ float peak_compress_stereo(StereoCompressor *compressor, float l, float r, float
|
|||||||
float input_db_l = voltage_to_voltage_db(env_l);
|
float input_db_l = voltage_to_voltage_db(env_l);
|
||||||
float input_db_r = voltage_to_voltage_db(env_r);
|
float input_db_r = voltage_to_voltage_db(env_r);
|
||||||
|
|
||||||
float targetGR_l = 0.0f;
|
float targetBoost_l = 0.0f;
|
||||||
if(input_db_l < compressor->threshold) {
|
if(input_db_l < compressor->threshold) {
|
||||||
if(compressor->knee > 0.0f) {
|
if(compressor->knee > 0.0f) {
|
||||||
float delta = compressor->threshold - input_db_l;
|
float delta = compressor->threshold - input_db_l;
|
||||||
if(delta < compressor->knee / 2.0f) {
|
if(delta < compressor->knee / 2.0f) {
|
||||||
targetGR_l = -(1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
|
targetBoost_l = (1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
|
||||||
} else {
|
} else {
|
||||||
targetGR_l = -(1.0f - 1.0f / compressor->ratio) * delta;
|
targetBoost_l = (1.0f - 1.0f / compressor->ratio) * delta;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
targetGR_l = -(1.0f - 1.0f / compressor->ratio) * (compressor->threshold - input_db_l);
|
targetBoost_l = (1.0f - 1.0f / compressor->ratio) * (compressor->threshold - input_db_l);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
targetGR_l = 0.0f;
|
targetBoost_l = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
float targetGR_r = 0.0f;
|
float targetBoost_r = 0.0f;
|
||||||
if(input_db_r < compressor->threshold) {
|
if(input_db_r < compressor->threshold) {
|
||||||
if(compressor->knee > 0.0f) {
|
if(compressor->knee > 0.0f) {
|
||||||
float delta = compressor->threshold - input_db_r;
|
float delta = compressor->threshold - input_db_r;
|
||||||
if(delta < compressor->knee / 2.0f) {
|
if(delta < compressor->knee / 2.0f) {
|
||||||
targetGR_r = -(1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
|
targetBoost_r = (1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
|
||||||
} else {
|
} else {
|
||||||
targetGR_r = -(1.0f - 1.0f / compressor->ratio) * delta;
|
targetBoost_r = (1.0f - 1.0f / compressor->ratio) * delta;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
targetGR_r = -(1.0f - 1.0f / compressor->ratio) * (compressor->threshold - input_db_r);
|
targetBoost_r = (1.0f - 1.0f / compressor->ratio) * (compressor->threshold - input_db_r);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
targetGR_r = 0.0f;
|
targetBoost_r = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
float shared_target_gr = (targetGR_l < targetGR_r) ? targetGR_l : targetGR_r;
|
float shared_target_boost = (targetBoost_l > targetBoost_r) ? targetBoost_l : targetBoost_r;
|
||||||
|
|
||||||
float coeff;
|
float coeff;
|
||||||
if(shared_target_gr < compressor->gainReduction) {
|
if(shared_target_boost > compressor->gainReduction) {
|
||||||
coeff = expf(-1.0f / (compressor->attack * compressor->sample_rate));
|
coeff = expf(-1.0f / (compressor->attack * compressor->sample_rate));
|
||||||
} else {
|
} else {
|
||||||
coeff = expf(-1.0f / (compressor->release * compressor->sample_rate));
|
coeff = expf(-1.0f / (compressor->release * compressor->sample_rate));
|
||||||
}
|
}
|
||||||
compressor->gainReduction = coeff * compressor->gainReduction + (1.0f - coeff) * shared_target_gr;
|
compressor->gainReduction = coeff * compressor->gainReduction + (1.0f - coeff) * shared_target_boost;
|
||||||
|
|
||||||
float gain = voltage_db_to_voltage(compressor->makeup_gain - compressor->gainReduction);
|
float gain = voltage_db_to_voltage(compressor->makeup_gain + compressor->gainReduction);
|
||||||
*output_r = r * gain;
|
*output_r = r * gain;
|
||||||
return l * gain;
|
return l * gain;
|
||||||
}
|
}
|
||||||
+1
-1
@@ -377,7 +377,7 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
StereoCompressor comp;
|
StereoCompressor comp;
|
||||||
// THRESH RATIO KNE MAKE ATT REL RMS
|
// THRESH RATIO KNE MAKE ATT REL RMS
|
||||||
init_compressor_stereo(&comp, -2.0f, 8.0f, 2.0f, 12.0f, 0.025f, 0.4f, 0.04f, SAMPLE_RATE);
|
init_compressor_stereo(&comp, -2.0f, 8.0f, 2.0f, 3.0f, 0.025f, 0.4f, 0.04f, SAMPLE_RATE);
|
||||||
// #endregion
|
// #endregion
|
||||||
|
|
||||||
signal(SIGINT, stop);
|
signal(SIGINT, stop);
|
||||||
|
|||||||
Reference in New Issue
Block a user