mirror of
https://github.com/radio95-rnt/fm95.git
synced 2026-07-30 16:29:15 +02:00
bs412 gating and also fix it
This commit is contained in:
+30
-24
@@ -1,43 +1,54 @@
|
||||
#include "bs412.h"
|
||||
|
||||
#define BS412_TIME 60
|
||||
#define ENABLE_TIME 50
|
||||
#define ENABLE_TIME 45
|
||||
#define CLAMP(x, lo, hi) (((x) < (lo)) ? (lo) : ((x) > (hi) ? (hi) : (x)))
|
||||
|
||||
inline float dbr_to_deviation(float dbr) {
|
||||
return 19000.0f * sqrtf(pow(10.0, dbr / 10.0));
|
||||
}
|
||||
|
||||
#define REF_POWER (19000.0f * 19000.0f) // 361,000,000
|
||||
inline float power_to_dbr(float power) {
|
||||
inline float power_to_dbr(float power, float ref) {
|
||||
if (power < 1e-12f) return -100.0f;
|
||||
return 10*log10f(power / REF_POWER);
|
||||
return 10*log10f(power / ref);
|
||||
}
|
||||
|
||||
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;
|
||||
void init_bs412(BS412Compressor* comp, uint32_t mpx_deviation, float target_power, float attack, float release, float max_gain, float gate, uint32_t sample_rate) {
|
||||
comp->reference = (19000.0f / mpx_deviation) * (19000.0f / mpx_deviation); // 0 dbr is a signal which generates a deviation of 19 khz
|
||||
comp->avg_power = 0.0f;
|
||||
comp->alpha = 1.0f / (BS412_TIME * sample_rate);
|
||||
comp->sample_rate = sample_rate;
|
||||
comp->attack = expf(-1.0f / (attack * sample_rate));
|
||||
comp->release = expf(-1.0f / (release * sample_rate));
|
||||
comp->target = dbr_to_deviation(target_power) * dbr_to_deviation(target_power);
|
||||
comp->target = comp->reference * powf(10.0f, target_power / 10.0f);;
|
||||
comp->gain = 1.0f;
|
||||
comp->can_compress = 0;
|
||||
comp->second_counter = 0;
|
||||
comp->max_gain = max_gain;
|
||||
comp->gate_threshold = comp->reference * powf(10.0f, gate / 10.0f);
|
||||
comp->init = true;
|
||||
#ifdef BS412_DEBUG
|
||||
debug_printf("Initialized MPX power measurement with sample rate: %d\n", sample_rate);
|
||||
#endif
|
||||
}
|
||||
|
||||
void reinit_bs412(BS412Compressor* comp, uint32_t mpx_deviation, float target_power, float attack, float release, float max_gain, float gate) {
|
||||
comp->reference = (19000.0f / mpx_deviation) * (19000.0f / mpx_deviation); // 0 dbr is a signal which generates a deviation of 19 khz
|
||||
comp->target = comp->reference * powf(10.0f, target_power / 10.0f);;
|
||||
comp->gate_threshold = comp->reference * powf(10.0f, gate / 10.0f);
|
||||
comp->attack = expf(-1.0f / (attack * sample_rate));
|
||||
comp->release = expf(-1.0f / (release * sample_rate));
|
||||
comp->max_gain = max_gain;
|
||||
}
|
||||
|
||||
float bs412_compress(BS412Compressor* comp, float audio, float sample_mpx, float* mpx_power) {
|
||||
float combined = audio + sample_mpx;
|
||||
float output_sample = (audio * comp->gain) + sample_mpx;
|
||||
|
||||
comp->avg_power += comp->alpha * ((output_sample * output_sample * comp->mpx_deviation * comp->mpx_deviation) - comp->avg_power);
|
||||
float inst_power = output_sample * output_sample;
|
||||
float audio_power = (audio * comp->gain) * (audio * comp->gain);
|
||||
|
||||
if(comp->sample_counter > comp->sample_rate) {
|
||||
float w = (audio_power - comp->gate_threshold) / comp->gate_threshold;
|
||||
w = CLAMP(w, 0.0f, 1.0f);
|
||||
comp->avg_power += comp->alpha * w * (inst_power - comp->avg_power);
|
||||
|
||||
if(comp->sample_counter >= comp->sample_rate) {
|
||||
comp->sample_counter = 0;
|
||||
if(comp->can_compress == 0) comp->second_counter++;
|
||||
}
|
||||
@@ -52,21 +63,16 @@ float bs412_compress(BS412Compressor* comp, float audio, float sample_mpx, float
|
||||
|
||||
float safe_power = fmaxf(comp->avg_power, 1e-12f);
|
||||
float target_gain = sqrtf(comp->target / safe_power);
|
||||
if (comp->avg_power > comp->target) comp->gain = comp->attack * comp->gain + (1.0f - comp->attack) * target_gain;
|
||||
else comp->gain = comp->release * comp->gain + (1.0f - comp->release) * target_gain;
|
||||
if (comp->avg_power > comp->target)
|
||||
comp->gain = comp->attack * comp->gain + (1.0f - comp->attack) * target_gain;
|
||||
else
|
||||
comp->gain = comp->release * comp->gain + (1.0f - comp->release) * target_gain;
|
||||
|
||||
comp->gain = CLAMP(comp->gain, 0.0f, comp->max_gain);
|
||||
|
||||
float power_after_gain = comp->avg_power * comp->gain * comp->gain;
|
||||
|
||||
if(power_after_gain > comp->target && comp->avg_power < comp->target) {
|
||||
float reduction = sqrtf(comp->target / power_after_gain);
|
||||
comp->gain = CLAMP(comp->gain * reduction, 0.01f, comp->max_gain);
|
||||
}
|
||||
comp->gain = CLAMP(comp->gain, 0.01f, comp->max_gain);
|
||||
|
||||
comp->sample_counter++;
|
||||
|
||||
if(mpx_power != NULL) *mpx_power = power_to_dbr(comp->avg_power);
|
||||
if(mpx_power != NULL) *mpx_power = power_to_dbr(comp->avg_power, comp->reference);
|
||||
|
||||
if(comp->can_compress) return output_sample;
|
||||
return combined;
|
||||
|
||||
+6
-4
@@ -11,9 +11,8 @@
|
||||
#include "../lib/debug.h"
|
||||
#endif
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t mpx_deviation;
|
||||
typedef struct {
|
||||
float reference;
|
||||
uint32_t sample_rate;
|
||||
uint32_t sample_counter;
|
||||
float target;
|
||||
@@ -26,7 +25,10 @@ typedef struct
|
||||
uint8_t can_compress : 1;
|
||||
uint8_t second_counter;
|
||||
float last_output;
|
||||
float gate_threshold;
|
||||
bool init;
|
||||
} BS412Compressor;
|
||||
|
||||
void init_bs412(BS412Compressor *comp, uint32_t mpx_deviation, float target_power, float attack, float release, float max_gain, uint32_t sample_rate);
|
||||
void init_bs412(BS412Compressor *comp, uint32_t mpx_deviation, float target_power, float attack, float release, float max_gain, float gate, uint32_t sample_rate);
|
||||
void reinit_bs412(BS412Compressor *comp, uint32_t mpx_deviation, float target_power, float attack, float release, float max_gain, float gate);
|
||||
float bs412_compress(BS412Compressor *comp, float audio, float sample_mpx, float* mpx_power);
|
||||
+15
-19
@@ -66,6 +66,7 @@ typedef struct {
|
||||
float bs412_attack;
|
||||
float bs412_release;
|
||||
float bs412_max;
|
||||
float bs412_gate;
|
||||
float lpf_cutoff;
|
||||
} FM95_Config;
|
||||
|
||||
@@ -328,6 +329,7 @@ static int config_handler(void* user, const char* section, const char* name, con
|
||||
else if(MATCH("fm95", "bs412_attack")) 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("fm95", "bs412_gate")) pconfig->bs412_gate = strtof(value, NULL);
|
||||
else if(MATCH("advanced", "lpf_order")) pconfig->lpf_order = atoi(value);
|
||||
else if(MATCH("advanced", "stereo_ssb")) pconfig->stereo_ssb = atoi(value);
|
||||
else if(MATCH("advanced", "preemp_unity")) pconfig->preemp_unity_freq = strtof(value, NULL);
|
||||
@@ -437,25 +439,9 @@ void init_runtime(FM95_Runtime* runtime, const FM95_Config config) {
|
||||
init_preemphasis(&runtime->preemp_r, (float)config.preemphasis * 1.0e-6f, config.sample_rate, config.preemp_unity_freq);
|
||||
}
|
||||
|
||||
float last_gain = 0.0f;
|
||||
float last_power = 0.0f;
|
||||
uint8_t last_compress = 0;
|
||||
uint8_t last_sample_counter = 0;
|
||||
uint8_t last_second_counter = 0;
|
||||
if(runtime->bs412.sample_rate == config.sample_rate) {
|
||||
last_gain = runtime->bs412.gain;
|
||||
last_power = runtime->bs412.avg_power;
|
||||
last_compress = runtime->bs412.can_compress;
|
||||
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.bs412_max, config.sample_rate);
|
||||
runtime->bs412.gain = last_gain;
|
||||
runtime->bs412.avg_power = last_power;
|
||||
runtime->bs412.can_compress = last_compress;
|
||||
runtime->bs412.sample_counter = last_sample_counter;
|
||||
runtime->bs412.second_counter = last_second_counter;
|
||||
|
||||
if(runtime->bs412.init == true && (runtime->bs412.sample_rate == config.sample_rate)) {
|
||||
reinit_bs412(&runtime->bs412, config.mpx_deviation, config.mpx_power, config.bs412_attack, config.bs412_release, config.bs412_max, config.bs412_gate)
|
||||
} else init_bs412(&runtime->bs412, config.mpx_deviation, config.mpx_power, config.bs412_attack, config.bs412_release, config.bs412_max, config.bs412_gate, config.sample_rate);
|
||||
init_stereo_encoder(&runtime->stencode, config.stereo_ssb, 4.0f, &runtime->osc, config.volumes.audio, config.volumes.pilot);
|
||||
|
||||
if(config.agc_max != 0.0) {
|
||||
@@ -529,6 +515,14 @@ static void *handle_client(ipc_client_arg_t *arg) {
|
||||
data->config->master_volume = val;
|
||||
reply = 0;
|
||||
break;
|
||||
case 105:
|
||||
// Set BS412 gate
|
||||
memcpy(&val, buf + 1, sizeof(float));
|
||||
data->config->bs412_gate = val;
|
||||
reply = 0;
|
||||
to_run = 0;
|
||||
to_reload = 1;
|
||||
break;
|
||||
case 0xff:
|
||||
// Fetch data
|
||||
send(fd, data->run_result, sizeof(FM95_RunResult), 0);
|
||||
@@ -585,6 +579,7 @@ int main(int argc, char **argv) {
|
||||
.bs412_attack = 0.05f,
|
||||
.bs412_release = 0.025f,
|
||||
.bs412_max = 2.82f,
|
||||
.bs412_gate = -20.0f,
|
||||
.lpf_cutoff = 15000.0f,
|
||||
};
|
||||
|
||||
@@ -642,6 +637,7 @@ int main(int argc, char **argv) {
|
||||
.runtime = &runtime,
|
||||
.run_result = &runres
|
||||
};
|
||||
|
||||
ipc_ctx_t ctx;
|
||||
ipc_ctx_t *pctx = &ctx;
|
||||
if(create_ipc(pctx, handle_client, "/etc/fm95/ctl.socket", &fmdata) < 0) {
|
||||
|
||||
Reference in New Issue
Block a user