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

clip bs412 and make it simpler to understand, both by me and the compiler

This commit is contained in:
2025-07-08 23:04:38 +02:00
parent dced0383d5
commit 58a37da27c
3 changed files with 10 additions and 9 deletions

View File

@@ -11,7 +11,8 @@ inline float deviation_to_dbr(float deviation) {
return 10.0f * (log2f(deviation) - LOG2_19000) * 0.30103f;
}
void init_bs412(BS412Compressor* mpx, float target_power, float attack, float release, int sample_rate) {
void init_bs412(BS412Compressor* mpx, float mpx_deviation, float target_power, float attack, float release, int sample_rate) {
mpx->mpx_deviation = mpx_deviation;
mpx->sample_counter = 0;
mpx->sample = 0;
mpx->sample_rate = sample_rate;
@@ -24,8 +25,8 @@ void init_bs412(BS412Compressor* mpx, float target_power, float attack, float re
#endif
}
float bs412_compress(BS412Compressor* mpx, float deviation) {
mpx->sample += deviation * deviation; // rmS
float bs412_compress(BS412Compressor* mpx, float sample) {
mpx->sample += sample * sample * mpx->mpx_deviation; // rmS
mpx->sample_counter++;
float inv_counter = 1.0f / mpx->sample_counter;
@@ -52,6 +53,5 @@ float bs412_compress(BS412Compressor* mpx, float deviation) {
else
mpx->gain = mpx->gain * mpx->release + (1.0f - mpx->release) * gain_target;
return deviation*mpx->gain;
return fminf(sample*mpx->gain, dbr_to_deviation(mpx->target*1.1f));
}

View File

@@ -11,6 +11,7 @@
typedef struct
{
int mpx_deviation;
int sample_counter;
int sample_rate;
float target;
@@ -23,5 +24,5 @@ typedef struct
float dbr_to_deviation(float dbr);
float deviation_to_dbr(float deviation);
void init_bs412(BS412Compressor *mpx, float target_power, float attack, float release, int sample_rate);
float bs412_compress(BS412Compressor *mpx, float deviation);
void init_bs412(BS412Compressor *mpx, float mpx_deviation, float target_power, float attack, float release, int sample_rate);
float bs412_compress(BS412Compressor *mpx, float sample);

View File

@@ -167,7 +167,7 @@ int run_fm95(const FM95_Config config, FM95_Runtime* runtime) {
init_preemphasis(&preemp_r, config.preemphasis, config.sample_rate, config.preemp_unity_freq);
BS412Compressor bs412;
init_bs412(&bs412, config.mpx_power, config.bs412_attack, config.bs412_release, config.sample_rate);
init_bs412(&bs412, config.mpx_deviation, config.mpx_power, config.bs412_attack, config.bs412_release, config.sample_rate);
TiltCorrectionFilter tilter;
tilt_init(&tilter, config.tilt);
@@ -232,7 +232,7 @@ int run_fm95(const FM95_Config config, FM95_Runtime* runtime) {
}
}
mpx = bs412_compress(&bs412, mpx*config.mpx_deviation) / config.mpx_deviation;
mpx = bs412_compress(&bs412, mpx);
output[i] = hard_clip(tilt(&tilter, (mpx_in[i]+mpx))*config.master_volume, 1.0); // Ensure peak deviation of 75 khz, assuming we're calibrated correctly (lower)
advance_oscillator(&osc);