mirror of
https://github.com/radio95-rnt/fm95.git
synced 2026-02-27 03:23:54 +01:00
make the compressor upward
This commit is contained in:
104
lib/filters.c
104
lib/filters.c
@@ -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;
|
compressor->gainReduction = 0.0f; // now this will become negative for boost
|
||||||
compressor->rmsEnv = 0.0f;
|
compressor->rmsEnv = 0.0f;
|
||||||
compressor->rmsTime = rmsTime;
|
compressor->rmsTime = rmsTime;
|
||||||
}
|
}
|
||||||
@@ -143,23 +143,23 @@ 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 targetGR = 0.0f;
|
||||||
if(input_db > compressor->threshold) {
|
if(input_db < compressor->threshold) {
|
||||||
if(compressor->knee > 0.0f) {
|
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) {
|
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 {
|
} else {
|
||||||
targetGR = (1.0f - 1.0f / compressor->ratio) * delta;
|
targetGR = -(1.0f - 1.0f / compressor->ratio) * delta;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
targetGR = (1.0f - 1.0f / compressor->ratio) * (input_db - compressor->threshold);
|
targetGR = -(1.0f - 1.0f / compressor->ratio) * (compressor->threshold - input_db);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
targetGR = 0.0f;
|
targetGR = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
float coeff;
|
float coeff;
|
||||||
if(targetGR > compressor->gainReduction) {
|
if(targetGR < 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));
|
||||||
@@ -172,27 +172,26 @@ float rms_compress(Compressor *compressor, float sample) {
|
|||||||
|
|
||||||
float peak_compress(Compressor *compressor, float sample) {
|
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 targetGR = 0.0f;
|
||||||
if(input_db > compressor->threshold) {
|
if(input_db < compressor->threshold) {
|
||||||
if(compressor->knee > 0.0f) {
|
if(compressor->knee > 0.0f) {
|
||||||
float delta = input_db - compressor->threshold;
|
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;
|
targetGR = -(1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
|
||||||
} else {
|
} else {
|
||||||
targetGR = (1.0f - 1.0f / compressor->ratio) * delta;
|
targetGR = -(1.0f - 1.0f / compressor->ratio) * delta;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
targetGR = (1.0f - 1.0f / compressor->ratio) * (input_db - compressor->threshold);
|
targetGR = -(1.0f - 1.0f / compressor->ratio) * (compressor->threshold - input_db);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
targetGR = 0.0f;
|
targetGR = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
float coeff;
|
float coeff;
|
||||||
if(targetGR > compressor->gainReduction) {
|
if(targetGR < 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));
|
||||||
@@ -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 rms_compress_stereo(StereoCompressor *compressor, float l, float r, float *output_r) {
|
||||||
float env_l;
|
float env_l, env_r;
|
||||||
float env_r;
|
|
||||||
float rmsAlpha = 1.0f - exp(-1.0f / (compressor->rmsTime * compressor->sample_rate));
|
float rmsAlpha = 1.0f - exp(-1.0f / (compressor->rmsTime * compressor->sample_rate));
|
||||||
compressor->rmsEnv = (1.0f - rmsAlpha) * compressor->rmsEnv + rmsAlpha * (l * l);
|
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_l = sqrtf(compressor->rmsEnv);
|
||||||
env_r = sqrtf(compressor->rmsEnv2);
|
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 input_db_r = voltage_to_voltage_db(env_r);
|
||||||
|
|
||||||
float targetGR = 0.0f;
|
float targetGR_l = 0.0f;
|
||||||
if(input_db > compressor->threshold) {
|
if(input_db_l < compressor->threshold) {
|
||||||
if(compressor->knee > 0.0f) {
|
if(compressor->knee > 0.0f) {
|
||||||
float delta = input_db - compressor->threshold;
|
float delta = compressor->threshold - input_db_l;
|
||||||
if(delta < compressor->knee / 2.0f) {
|
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 {
|
} else {
|
||||||
targetGR = (1.0f - 1.0f / compressor->ratio) * delta;
|
targetGR_l = -(1.0f - 1.0f / compressor->ratio) * delta;
|
||||||
}
|
}
|
||||||
} else {
|
} 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 {
|
} else {
|
||||||
targetGR = 0.0f;
|
targetGR_l = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
float targetGR_r = 0.0f;
|
float targetGR_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 = input_db_r - compressor->threshold;
|
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;
|
targetGR_r = -(1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
|
||||||
} else {
|
} else {
|
||||||
targetGR_r = (1.0f - 1.0f / compressor->ratio) * delta;
|
targetGR_r = -(1.0f - 1.0f / compressor->ratio) * delta;
|
||||||
}
|
}
|
||||||
} else {
|
} 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 {
|
} else {
|
||||||
targetGR_r = 0.0f;
|
targetGR_r = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
float shared_target_gr;
|
float shared_target_gr = (targetGR_l < targetGR_r) ? targetGR_l : targetGR_r;
|
||||||
if(targetGR > targetGR_r) {
|
|
||||||
shared_target_gr = targetGR;
|
|
||||||
} else {
|
|
||||||
shared_target_gr = targetGR_r;
|
|
||||||
}
|
|
||||||
|
|
||||||
float coeff;
|
float coeff;
|
||||||
if(shared_target_gr > compressor->gainReduction) {
|
if(shared_target_gr < 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));
|
||||||
@@ -284,49 +278,45 @@ float peak_compress_stereo(StereoCompressor *compressor, float l, float r, float
|
|||||||
float env_l = fabsf(l);
|
float env_l = fabsf(l);
|
||||||
float env_r = fabsf(r);
|
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 input_db_r = voltage_to_voltage_db(env_r);
|
||||||
|
|
||||||
float targetGR = 0.0f;
|
float targetGR_l = 0.0f;
|
||||||
if(input_db > compressor->threshold) {
|
if(input_db_l < compressor->threshold) {
|
||||||
if(compressor->knee > 0.0f) {
|
if(compressor->knee > 0.0f) {
|
||||||
float delta = input_db - compressor->threshold;
|
float delta = compressor->threshold - input_db_l;
|
||||||
if(delta < compressor->knee / 2.0f) {
|
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 {
|
} else {
|
||||||
targetGR = (1.0f - 1.0f / compressor->ratio) * delta;
|
targetGR_l = -(1.0f - 1.0f / compressor->ratio) * delta;
|
||||||
}
|
}
|
||||||
} else {
|
} 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 {
|
} else {
|
||||||
targetGR = 0.0f;
|
targetGR_l = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
float targetGR_r = 0.0f;
|
float targetGR_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 = input_db_r - compressor->threshold;
|
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;
|
targetGR_r = -(1.0f - 1.0f / compressor->ratio) * (delta * delta) / compressor->knee;
|
||||||
} else {
|
} else {
|
||||||
targetGR_r = (1.0f - 1.0f / compressor->ratio) * delta;
|
targetGR_r = -(1.0f - 1.0f / compressor->ratio) * delta;
|
||||||
}
|
}
|
||||||
} else {
|
} 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 {
|
} else {
|
||||||
targetGR_r = 0.0f;
|
targetGR_r = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
float shared_target_gr;
|
float shared_target_gr = (targetGR_l < targetGR_r) ? targetGR_l : targetGR_r;
|
||||||
if(targetGR > targetGR_r) {
|
|
||||||
shared_target_gr = targetGR;
|
|
||||||
} else {
|
|
||||||
shared_target_gr = targetGR_r;
|
|
||||||
}
|
|
||||||
|
|
||||||
float coeff;
|
float coeff;
|
||||||
if(shared_target_gr > compressor->gainReduction) {
|
if(shared_target_gr < 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));
|
||||||
|
|||||||
@@ -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