From d820603da85bb6a7cbeb70dac63f5f4d867a5091 Mon Sep 17 00:00:00 2001 From: KubaPro010 Date: Sun, 15 Feb 2026 18:47:39 +0100 Subject: [PATCH] add bs412 max --- filter/bs412.c | 5 +++-- filter/bs412.h | 3 ++- src/fm95.c | 14 +++++++++----- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/filter/bs412.c b/filter/bs412.c index 9a57c08..46fb6b3 100644 --- a/filter/bs412.c +++ b/filter/bs412.c @@ -13,7 +13,7 @@ inline float deviation_to_dbr(float deviation) { return 10*log10f((deviation*deviation)/SQRT19000); } -void init_bs412(BS412Compressor* comp, uint32_t mpx_deviation, float target_power, float attack, float release, uint32_t sample_rate) { +void init_bs412(BS412Compressor* comp, uint32_t mpx_deviation, float target_power, float attack, float release, float max_gain, uint32_t sample_rate) { comp->mpx_deviation = mpx_deviation; comp->avg_power = 0.0f; comp->alpha = 1.0f / (BS412_TIME * sample_rate); @@ -25,6 +25,7 @@ void init_bs412(BS412Compressor* comp, uint32_t mpx_deviation, float target_powe comp->can_compress = 0; comp->second_counter = 0; comp->last_output = 0.0f; + comp->max_gain = max_gain; #ifdef BS412_DEBUG debug_printf("Initialized MPX power measurement with sample rate: %d\n", sample_rate); #endif @@ -82,7 +83,7 @@ float bs412_compress(BS412Compressor* comp, float audio, float sample_mpx) { float overshoot_dbr = deviation_to_dbr(avg_deviation * comp->gain) - comp->target; float reduction_factor = powf(10.0f, -overshoot_dbr / 10.0f); comp->gain *= reduction_factor; - comp->gain = fmaxf(0.01f, fminf(2.82f, comp->gain)); + comp->gain = fmaxf(0.01f, fminf(comp->max_gain, comp->gain)); } comp->sample_counter++; diff --git a/filter/bs412.h b/filter/bs412.h index ebf3b5b..a51355d 100644 --- a/filter/bs412.h +++ b/filter/bs412.h @@ -19,6 +19,7 @@ typedef struct float target; float attack; float release; + float max_gain; float gain; double avg_power; double alpha; @@ -30,5 +31,5 @@ typedef struct // float dbr_to_deviation(float dbr); float deviation_to_dbr(float deviation); -void init_bs412(BS412Compressor *comp, uint32_t mpx_deviation, float target_power, float attack, float release, uint32_t sample_rate); +void init_bs412(BS412Compressor *comp, uint32_t mpx_deviation, float target_power, float attack, float release, float max_gain, uint32_t sample_rate); float bs412_compress(BS412Compressor *comp, float audio, float sample_mpx); \ No newline at end of file diff --git a/src/fm95.c b/src/fm95.c index 79d178a..21fb71d 100644 --- a/src/fm95.c +++ b/src/fm95.c @@ -70,6 +70,7 @@ typedef struct { float agc_min; float bs412_attack; float bs412_release; + float bs412_max; float lpf_cutoff; } FM95_Config; @@ -344,6 +345,8 @@ static int config_handler(void* user, const char* section, const char* name, con pconfig->bs412_attack = strtof(value, NULL); } else if(MATCH("fm95", "bs412_release")) { pconfig->bs412_release = strtof(value, NULL); + } else if(MATCH("fm95", "bs412_max")) { + pconfig->bs412_max = strtof(value, NULL); } else if(MATCH("advanced", "lpf_order")) { pconfig->lpf_order = atoi(value); } else if(MATCH("advanced", "stereo_ssb")) { @@ -389,7 +392,7 @@ int setup_audio(FM95_Runtime* runtime, const FM95_DeviceNames dv_names, const FM pa_buffer_attr output_buffer_atr = { .maxlength = buffer_maxlength, .tlength = buffer_tlength_fragsize, - .prebuf = 64 + .prebuf = 512 }; int opentime_pulse_error; @@ -473,7 +476,7 @@ void init_runtime(FM95_Runtime* runtime, const FM95_Config config) { last_sample_counter = runtime->bs412.sample_counter; last_second_counter = runtime->bs412.second_counter; } - init_bs412(&runtime->bs412, config.mpx_deviation, config.mpx_power, config.bs412_attack, config.bs412_release, config.sample_rate); + init_bs412(&runtime->bs412, config.mpx_deviation, config.mpx_power, config.bs412_attack, config.bs412_release, config.bs412_max, config.sample_rate); runtime->bs412.gain = last_gain; runtime->bs412.avg_power = last_power; runtime->bs412.can_compress = last_compress; @@ -493,7 +496,7 @@ void init_runtime(FM95_Runtime* runtime, const FM95_Config config) { } int main(int argc, char **argv) { - printf("fm95 (an FM Processor by radio95) version 2.3\n"); + printf("fm95 (an FM Processor by radio95) version 2.4\n"); FM95_Config config = { .volumes = { @@ -529,8 +532,9 @@ int main(int argc, char **argv) { .agc_min = 0.1f, .agc_max = 1.5f, .bs412_attack = 0.05f, - .bs412_release = 0.025, - .lpf_cutoff = 15000, + .bs412_release = 0.025f, + .bs412_max = 2.82f, + .lpf_cutoff = 15000.0f, }; FM95_DeviceNames dv_names = {