0
1
mirror of https://github.com/radio95-rnt/fm95.git synced 2026-02-26 19:23:51 +01:00

make the compressor upward

This commit is contained in:
2025-03-02 09:35:40 +01:00
parent a27e2bb7a5
commit e68c4ac862
2 changed files with 48 additions and 58 deletions

View File

@@ -129,7 +129,7 @@ void init_compressor(Compressor *compressor, float threshold, float ratio, float
compressor->attack = attack;
compressor->release = release;
compressor->sample_rate = sample_rate;
compressor->gainReduction = 0.0f;
compressor->gainReduction = 0.0f; // now this will become negative for boost
compressor->rmsEnv = 0.0f;
compressor->rmsTime = rmsTime;
}
@@ -143,23 +143,23 @@ float rms_compress(Compressor *compressor, float sample) {
float input_db = voltage_to_voltage_db(env);
float targetGR = 0.0f;
if(input_db > compressor->threshold) {
if(input_db < compressor->threshold) {
if(compressor->knee > 0.0f) {
float delta = input_db - compressor->threshold;
float delta = compressor->threshold - input_db; // positive difference
if(delta < compressor->knee / 2.0f) {
targetGR = (1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
targetGR = -(1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
} else {
targetGR = (1.0f - 1.0f / compressor->ratio) * delta;
targetGR = -(1.0f - 1.0f / compressor->ratio) * delta;
}
} else {
targetGR = (1.0f - 1.0f / compressor->ratio) * (input_db - compressor->threshold);
targetGR = -(1.0f - 1.0f / compressor->ratio) * (compressor->threshold - input_db);
}
} else {
targetGR = 0.0f;
}
float coeff;
if(targetGR > compressor->gainReduction) {
if(targetGR < compressor->gainReduction) {
coeff = expf(-1.0f / (compressor->attack * compressor->sample_rate));
} else {
coeff = expf(-1.0f / (compressor->release * compressor->sample_rate));
@@ -172,27 +172,26 @@ float rms_compress(Compressor *compressor, float sample) {
float peak_compress(Compressor *compressor, float sample) {
float env = fabsf(sample);
float input_db = voltage_to_voltage_db(env);
float targetGR = 0.0f;
if(input_db > compressor->threshold) {
if(input_db < compressor->threshold) {
if(compressor->knee > 0.0f) {
float delta = input_db - compressor->threshold;
float delta = compressor->threshold - input_db;
if(delta < compressor->knee / 2.0f) {
targetGR = (1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
targetGR = -(1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
} else {
targetGR = (1.0f - 1.0f / compressor->ratio) * delta;
targetGR = -(1.0f - 1.0f / compressor->ratio) * delta;
}
} else {
targetGR = (1.0f - 1.0f / compressor->ratio) * (input_db - compressor->threshold);
targetGR = -(1.0f - 1.0f / compressor->ratio) * (compressor->threshold - input_db);
}
} else {
targetGR = 0.0f;
}
float coeff;
if(targetGR > compressor->gainReduction) {
if(targetGR < compressor->gainReduction) {
coeff = expf(-1.0f / (compressor->attack * compressor->sample_rate));
} else {
coeff = expf(-1.0f / (compressor->release * compressor->sample_rate));
@@ -218,57 +217,52 @@ void init_compressor_stereo(StereoCompressor *compressor, float threshold, float
}
float rms_compress_stereo(StereoCompressor *compressor, float l, float r, float *output_r) {
float env_l;
float env_r;
float env_l, env_r;
float rmsAlpha = 1.0f - exp(-1.0f / (compressor->rmsTime * compressor->sample_rate));
compressor->rmsEnv = (1.0f - rmsAlpha) * compressor->rmsEnv + rmsAlpha * (l * l);
compressor->rmsEnv2 = (1.0f - rmsAlpha) * compressor->rmsEnv + rmsAlpha * (r * r);
compressor->rmsEnv2 = (1.0f - rmsAlpha) * compressor->rmsEnv2 + rmsAlpha * (r * r);
env_l = sqrtf(compressor->rmsEnv);
env_r = sqrtf(compressor->rmsEnv2);
float input_db = 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 targetGR = 0.0f;
if(input_db > compressor->threshold) {
float targetGR_l = 0.0f;
if(input_db_l < compressor->threshold) {
if(compressor->knee > 0.0f) {
float delta = input_db - compressor->threshold;
float delta = compressor->threshold - input_db_l;
if(delta < compressor->knee / 2.0f) {
targetGR = (1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
targetGR_l = -(1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
} else {
targetGR = (1.0f - 1.0f / compressor->ratio) * delta;
targetGR_l = -(1.0f - 1.0f / compressor->ratio) * delta;
}
} else {
targetGR = (1.0f - 1.0f / compressor->ratio) * (input_db - compressor->threshold);
targetGR_l = -(1.0f - 1.0f / compressor->ratio) * (compressor->threshold - input_db_l);
}
} else {
targetGR = 0.0f;
targetGR_l = 0.0f;
}
float targetGR_r = 0.0f;
if(input_db_r > compressor->threshold) {
if(input_db_r < compressor->threshold) {
if(compressor->knee > 0.0f) {
float delta = input_db_r - compressor->threshold;
float delta = compressor->threshold - input_db_r;
if(delta < compressor->knee / 2.0f) {
targetGR_r = (1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
targetGR_r = -(1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
} else {
targetGR_r = (1.0f - 1.0f / compressor->ratio) * delta;
targetGR_r = -(1.0f - 1.0f / compressor->ratio) * delta;
}
} else {
targetGR_r = (1.0f - 1.0f / compressor->ratio) * (input_db_r - compressor->threshold);
targetGR_r = -(1.0f - 1.0f / compressor->ratio) * (compressor->threshold - input_db_r);
}
} else {
targetGR_r = 0.0f;
}
float shared_target_gr;
if(targetGR > targetGR_r) {
shared_target_gr = targetGR;
} else {
shared_target_gr = targetGR_r;
}
float shared_target_gr = (targetGR_l < targetGR_r) ? targetGR_l : targetGR_r;
float coeff;
if(shared_target_gr > compressor->gainReduction) {
if(shared_target_gr < compressor->gainReduction) {
coeff = expf(-1.0f / (compressor->attack * compressor->sample_rate));
} else {
coeff = expf(-1.0f / (compressor->release * compressor->sample_rate));
@@ -284,49 +278,45 @@ float peak_compress_stereo(StereoCompressor *compressor, float l, float r, float
float env_l = fabsf(l);
float env_r = fabsf(r);
float input_db = 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 targetGR = 0.0f;
if(input_db > compressor->threshold) {
float targetGR_l = 0.0f;
if(input_db_l < compressor->threshold) {
if(compressor->knee > 0.0f) {
float delta = input_db - compressor->threshold;
float delta = compressor->threshold - input_db_l;
if(delta < compressor->knee / 2.0f) {
targetGR = (1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
targetGR_l = -(1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
} else {
targetGR = (1.0f - 1.0f / compressor->ratio) * delta;
targetGR_l = -(1.0f - 1.0f / compressor->ratio) * delta;
}
} else {
targetGR = (1.0f - 1.0f / compressor->ratio) * (input_db - compressor->threshold);
targetGR_l = -(1.0f - 1.0f / compressor->ratio) * (compressor->threshold - input_db_l);
}
} else {
targetGR = 0.0f;
targetGR_l = 0.0f;
}
float targetGR_r = 0.0f;
if(input_db_r > compressor->threshold) {
if(input_db_r < compressor->threshold) {
if(compressor->knee > 0.0f) {
float delta = input_db_r - compressor->threshold;
float delta = compressor->threshold - input_db_r;
if(delta < compressor->knee / 2.0f) {
targetGR_r = (1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
targetGR_r = -(1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
} else {
targetGR_r = (1.0f - 1.0f / compressor->ratio) * delta;
targetGR_r = -(1.0f - 1.0f / compressor->ratio) * delta;
}
} else {
targetGR_r = (1.0f - 1.0f / compressor->ratio) * (input_db_r - compressor->threshold);
targetGR_r = -(1.0f - 1.0f / compressor->ratio) * (compressor->threshold - input_db_r);
}
} else {
targetGR_r = 0.0f;
}
float shared_target_gr;
if(targetGR > targetGR_r) {
shared_target_gr = targetGR;
} else {
shared_target_gr = targetGR_r;
}
float shared_target_gr = (targetGR_l < targetGR_r) ? targetGR_l : targetGR_r;
float coeff;
if(shared_target_gr > compressor->gainReduction) {
if(shared_target_gr < compressor->gainReduction) {
coeff = expf(-1.0f / (compressor->attack * compressor->sample_rate));
} else {
coeff = expf(-1.0f / (compressor->release * compressor->sample_rate));

View File

@@ -377,7 +377,7 @@ int main(int argc, char **argv) {
StereoCompressor comp;
// 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
signal(SIGINT, stop);