0
1
mirror of https://github.com/radio95-rnt/fm95.git synced 2026-02-27 11:33:54 +01:00

kinda change the agc

This commit is contained in:
2025-07-10 20:09:49 +02:00
parent 7956b7d989
commit 4146110e6e
2 changed files with 24 additions and 25 deletions

View File

@@ -1,37 +1,35 @@
#include "gain_control.h" #include "gain_control.h"
void initAGC(AGC* agc, int sampleRate, float targetLevel, float minGain, float maxGain, float attackTime, float releaseTime) { void initAGC(AGC* agc, int sampleRate, float targetLevel, float minGain, float maxGain, float attackTime, float releaseTime) {
agc->sampleRate = sampleRate; agc->targetLevel = targetLevel;
agc->targetLevel = targetLevel; agc->minGain = minGain;
agc->minGain = minGain; agc->maxGain = maxGain;
agc->maxGain = maxGain;
agc->attackTime = attackTime;
agc->releaseTime = releaseTime;
agc->attackCoef = expf(-1.0f / (sampleRate * attackTime)); agc->attackCoef = expf(-1.0f / (sampleRate * attackTime));
agc->releaseCoef = expf(-1.0f / (sampleRate * releaseTime)); agc->releaseCoef = expf(-1.0f / (sampleRate * releaseTime));
agc->rmsAlpha = expf(-1.0f / (sampleRate * 0.025f));
agc->rmsBeta = 1.0f - agc->rmsAlpha;
agc->currentGain = 1.0f; agc->currentGain = 1.0f;
agc->currentLevel = 0.0f; agc->currentLevel = 0.0f;
agc->rms_buffer = 0.0f;
agc->rmsAlpha = expf(-1.0f / (sampleRate * 0.025f));
} }
float process_agc(AGC* agc, float sidechain) { float process_agc(AGC* agc, float sidechain) {
float x2 = sidechain * sidechain; const float signalPower = sidechain * sidechain;
agc->rms_buffer = agc->rmsAlpha * agc->rms_buffer + (1.0f - agc->rmsAlpha) * x2; agc->rmsBuffer = agc->rmsAlpha * agc->rmsBuffer + agc->rmsBeta * signalPower;
const float instantLevel = sqrtf(agc->rms_buffer);
const float levelAlpha = (instantLevel > agc->currentLevel) ? agc->attackCoef : agc->releaseCoef; const float rmsLevel = sqrtf(agc->rmsBuffer);
agc->currentLevel = levelAlpha * agc->currentLevel + (1.0f - levelAlpha) * instantLevel;
float desiredGain = agc->targetLevel / (agc->currentLevel + 1e-10f); const float levelAlpha = (rmsLevel > agc->currentLevel) ? agc->attackCoef : agc->releaseCoef;
desiredGain = fminf(fmaxf(desiredGain, agc->minGain), agc->maxGain); agc->currentLevel = levelAlpha * agc->currentLevel + (1.0f - levelAlpha) * rmsLevel;
const float gainAlpha = (desiredGain > agc->currentGain) ? agc->attackCoef : agc->releaseCoef; float desiredGain = agc->targetLevel / (agc->currentLevel + 1e-9f);
agc->currentGain = gainAlpha * agc->currentGain + (1.0f - gainAlpha) * desiredGain;
return agc->currentGain; desiredGain = fminf(fmaxf(desiredGain, agc->minGain), agc->maxGain);
const float gainAlpha = (desiredGain < agc->currentGain) ? agc->attackCoef : agc->releaseCoef;
agc->currentGain = gainAlpha * agc->currentGain + (1.0f - gainAlpha) * desiredGain;
return agc->currentGain;
} }

View File

@@ -15,8 +15,9 @@ typedef struct {
float attackCoef; float attackCoef;
float releaseCoef; float releaseCoef;
float rms_buffer; float rmsBuffer;
float rmsAlpha; float rmsAlpha;
float rmsBeta;
} AGC; } AGC;
void initAGC(AGC* agc, int sampleRate, float targetLevel, float minGain, float maxGain, float attackTime, float releaseTime); void initAGC(AGC* agc, int sampleRate, float targetLevel, float minGain, float maxGain, float attackTime, float releaseTime);