mirror of
https://github.com/radio95-rnt/fm95.git
synced 2026-02-26 19:23:51 +01:00
agc?
This commit is contained in:
@@ -3,9 +3,9 @@
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int i;
|
||||
int sample_rate;
|
||||
double sample;
|
||||
int i;
|
||||
int sample_rate;
|
||||
double sample;
|
||||
} MPXPowerMeasurement;
|
||||
|
||||
float dbr_to_deviation(float dbr);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#pragma once
|
||||
#include <stdio.h>
|
||||
#define debug_printf(fmt, ...) \
|
||||
printf("[%s:%d in %s] " fmt, __FILE__, __LINE__, __func__, ##__VA_ARGS__)
|
||||
printf("[%s:%d in %s] " fmt, __FILE__, __LINE__, __func__, ##__VA_ARGS__)
|
||||
|
||||
@@ -7,12 +7,6 @@
|
||||
#include "optimization.h"
|
||||
#include "oscillator.h"
|
||||
|
||||
#if USE_NEON
|
||||
#define LPF_ORDER 20 // neon has to have divisable by 4
|
||||
#else
|
||||
#define LPF_ORDER 10
|
||||
#endif
|
||||
|
||||
typedef struct
|
||||
{
|
||||
float alpha;
|
||||
|
||||
40
lib/gain_control.c
Normal file
40
lib/gain_control.c
Normal file
@@ -0,0 +1,40 @@
|
||||
#include "gain_control.h"
|
||||
|
||||
void initAGC(AGC* agc, int sampleRate, float targetLevel, float minGain, float maxGain, float attackTime, float releaseTime) {
|
||||
agc->sampleRate = sampleRate;
|
||||
agc->targetLevel = targetLevel;
|
||||
agc->minGain = minGain;
|
||||
agc->maxGain = maxGain;
|
||||
agc->attackTime = attackTime;
|
||||
agc->releaseTime = releaseTime;
|
||||
|
||||
agc->attackCoef = expf(-1.0f / (sampleRate * attackTime));
|
||||
agc->releaseCoef = expf(-1.0f / (sampleRate * releaseTime));
|
||||
|
||||
agc->currentGain = 1.0f;
|
||||
agc->currentLevel = 0.0f;
|
||||
|
||||
agc->rms_buffer = 0.0f;
|
||||
}
|
||||
|
||||
float process_agc_stereo(AGC* agc, float left, float right, float *right_out) {
|
||||
float sample = (left+right)/2;
|
||||
|
||||
float x2 = sample * sample;
|
||||
|
||||
float rmsAlpha = expf(-1.0f / (agc->sampleRate * 0.04));
|
||||
agc->rms_buffer = rmsAlpha * agc->rms_buffer + (1.0f - rmsAlpha) * x2;
|
||||
float instantLevel = sqrtf(agc->rms_buffer);
|
||||
|
||||
float alpha = (instantLevel > agc->currentLevel) ? agc->attackCoef : agc->releaseCoef;
|
||||
agc->currentLevel = alpha * agc->currentLevel + (1.0f - alpha) * instantLevel;
|
||||
|
||||
float desiredGain = agc->targetLevel / fmaxf(agc->currentLevel, 1e-10f);
|
||||
desiredGain = fminf(fmaxf(desiredGain, agc->minGain), agc->maxGain);
|
||||
|
||||
float gainAlpha = (desiredGain > agc->currentGain) ? agc->attackCoef : agc->releaseCoef;
|
||||
agc->currentGain = gainAlpha * agc->currentGain + (1.0f - gainAlpha) * desiredGain;
|
||||
|
||||
*right_out = right * agc->currentGain;
|
||||
return left * agc->currentGain;
|
||||
}
|
||||
22
lib/gain_control.h
Normal file
22
lib/gain_control.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include <math.h>
|
||||
|
||||
typedef struct {
|
||||
float targetLevel;
|
||||
float maxGain;
|
||||
float minGain;
|
||||
float attackTime;
|
||||
float releaseTime;
|
||||
|
||||
float currentGain;
|
||||
float currentLevel;
|
||||
|
||||
int sampleRate;
|
||||
float attackCoef;
|
||||
float releaseCoef;
|
||||
|
||||
float rms_buffer;
|
||||
} AGC;
|
||||
|
||||
void initAGC(AGC* agc, int sampleRate, float targetLevel, float minGain, float maxGain, float attackTime, float releaseTime);
|
||||
float process_agc_stereo(AGC* agc, float left, float right, float *right_out);
|
||||
@@ -23,15 +23,15 @@ float get_oscillator_cos_sample(Oscillator *osc) {
|
||||
}
|
||||
|
||||
float get_oscillator_sin_multiplier_ni(Oscillator *osc, float multiplier) {
|
||||
float new_phase = osc->phase * multiplier;
|
||||
new_phase -= (new_phase >= M_2PI) ? M_2PI : 0.0f;
|
||||
return sinf(new_phase);
|
||||
float new_phase = osc->phase * multiplier;
|
||||
new_phase -= (new_phase >= M_2PI) ? M_2PI : 0.0f;
|
||||
return sinf(new_phase);
|
||||
}
|
||||
|
||||
float get_oscillator_cos_multiplier_ni(Oscillator *osc, float multiplier) {
|
||||
float new_phase = osc->phase * multiplier;
|
||||
new_phase -= (new_phase >= M_2PI) ? M_2PI : 0.0f;
|
||||
return cosf(new_phase);
|
||||
new_phase -= (new_phase >= M_2PI) ? M_2PI : 0.0f;
|
||||
return cosf(new_phase);
|
||||
}
|
||||
|
||||
void advance_oscillator(Oscillator *osc) {
|
||||
|
||||
Reference in New Issue
Block a user