mirror of
https://github.com/radio95-rnt/fm95.git
synced 2026-02-27 11:33:54 +01:00
sliding window?
This commit is contained in:
@@ -11,8 +11,8 @@ inline float deviation_to_dbr(float deviation) {
|
|||||||
|
|
||||||
void init_bs412(BS412Compressor* mpx, uint32_t mpx_deviation, float target_power, float attack, float release, float max, uint32_t sample_rate) {
|
void init_bs412(BS412Compressor* mpx, uint32_t mpx_deviation, float target_power, float attack, float release, float max, uint32_t sample_rate) {
|
||||||
mpx->mpx_deviation = mpx_deviation;
|
mpx->mpx_deviation = mpx_deviation;
|
||||||
mpx->average_counter = 0;
|
mpx->avg_power = 0.0f;
|
||||||
mpx->average = 0;
|
mpx->alpha = 1.0f / (60.0f * sample_rate);
|
||||||
mpx->sample_rate = sample_rate;
|
mpx->sample_rate = sample_rate;
|
||||||
mpx->attack = expf(-1.0f / (attack * sample_rate));
|
mpx->attack = expf(-1.0f / (attack * sample_rate));
|
||||||
mpx->release = expf(-1.0f / (release * sample_rate));
|
mpx->release = expf(-1.0f / (release * sample_rate));
|
||||||
@@ -34,33 +34,26 @@ static inline float soft_clip_tanh(float sample, float threshold) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
float bs412_compress(BS412Compressor* mpx, float sample) {
|
float bs412_compress(BS412Compressor* mpx, float sample) {
|
||||||
mpx->average += sample * sample * mpx->mpx_deviation * mpx->mpx_deviation;
|
mpx->avg_power += mpx->alpha * ((sample * sample * mpx->mpx_deviation * mpx->mpx_deviation) - mpx->avg_power);
|
||||||
mpx->average_counter++;
|
|
||||||
|
|
||||||
float avg_power = mpx->average / mpx->average_counter;
|
float avg_deviation = sqrtf(mpx->avg_power);
|
||||||
float avg_deviation = sqrtf(avg_power) * sqrtf(2);
|
|
||||||
float modulation_power = deviation_to_dbr(avg_deviation);
|
float modulation_power = deviation_to_dbr(avg_deviation);
|
||||||
|
|
||||||
if (mpx->average_counter >= mpx->sample_rate * 60) {
|
|
||||||
#ifdef BS412_DEBUG
|
|
||||||
debug_printf("Resetting MPX power measurement\n");
|
|
||||||
#endif
|
|
||||||
mpx->average = avg_power;
|
|
||||||
mpx->average_counter = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(mpx->target <= -100.0f) {
|
if(mpx->target <= -100.0f) {
|
||||||
#ifdef BS412_DEBUG
|
#ifdef BS412_DEBUG
|
||||||
if(mpx->average_counter % mpx->sample_rate == 0) {
|
if(mpx->sample_counter > mpx->sample_rate) {
|
||||||
debug_printf("MPX power: %.2f dBr (%.1f Hz)\n", modulation_power, avg_deviation);
|
debug_printf("MPX power: %.2f dBr (%.1f Hz)\n", modulation_power, avg_deviation);
|
||||||
|
mpx->sample_counter = 0;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
mpx->sample_counter++;
|
||||||
return sample;
|
return sample;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef BS412_DEBUG
|
#ifdef BS412_DEBUG
|
||||||
if(mpx->average_counter % mpx->sample_rate == 0) {
|
if(mpx->sample_counter > mpx->sample_rate) {
|
||||||
debug_printf("MPX power: %.2f dBr with gain %.2fx (%.2f dBr)\n", modulation_power, mpx->gain, deviation_to_dbr(avg_deviation * mpx->gain));
|
debug_printf("MPX power: %.2f dBr with gain %.2fx (%.2f dBr)\n", modulation_power, mpx->gain, deviation_to_dbr(avg_deviation * mpx->gain));
|
||||||
|
mpx->sample_counter = 0;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -77,5 +70,6 @@ float bs412_compress(BS412Compressor* mpx, float sample) {
|
|||||||
float limit_threshold = dbr_to_deviation(mpx->target + 0.1f) / mpx->mpx_deviation;
|
float limit_threshold = dbr_to_deviation(mpx->target + 0.1f) / mpx->mpx_deviation;
|
||||||
output_sample = soft_clip_tanh(output_sample, limit_threshold);
|
output_sample = soft_clip_tanh(output_sample, limit_threshold);
|
||||||
|
|
||||||
|
mpx->sample_counter++;
|
||||||
return output_sample;
|
return output_sample;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,14 +14,15 @@
|
|||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
uint32_t mpx_deviation;
|
uint32_t mpx_deviation;
|
||||||
uint32_t average_counter;
|
|
||||||
uint32_t sample_rate;
|
uint32_t sample_rate;
|
||||||
|
uint32_t sample_counter;
|
||||||
float target;
|
float target;
|
||||||
float attack;
|
float attack;
|
||||||
float release;
|
float release;
|
||||||
float max;
|
float max;
|
||||||
float gain;
|
float gain;
|
||||||
double average;
|
double avg_power;
|
||||||
|
double alpha;
|
||||||
} BS412Compressor;
|
} BS412Compressor;
|
||||||
|
|
||||||
float dbr_to_deviation(float dbr);
|
float dbr_to_deviation(float dbr);
|
||||||
|
|||||||
Reference in New Issue
Block a user