2020-01-28 15:06:43 -05:00
|
|
|
//
|
|
|
|
// cagc.cpp
|
|
|
|
// ambed
|
|
|
|
//
|
|
|
|
// Created by Jean-Luc Deltombe (LX3JL) on 28/04/2017.
|
|
|
|
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
|
|
|
|
//
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// This file is part of ambed.
|
|
|
|
//
|
|
|
|
// xlxd is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// xlxd is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Geoffrey Merck F4FXL / KC3FRA AGC code borrowed from Liquid DSP
|
|
|
|
// Only took the parts we need qnd recoeded it to be close the XLX coding style
|
|
|
|
// https://github.com/jgaeddert/liquid-dsp/blob/master/src/agc/src/agc.c
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include <math.h>
|
|
|
|
#include "cagc.h"
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// constructor
|
|
|
|
|
|
|
|
CAGC::CAGC(float initialLeveldB)
|
|
|
|
{
|
|
|
|
// set internal gain appropriately
|
2020-01-29 13:57:12 -05:00
|
|
|
m_Gain = pow(10.0f, initialLeveldB/20.0f);
|
|
|
|
//+- 10dB Margin, TODO Move margin to constant
|
|
|
|
m_GainMax = pow(10.0f, (initialLeveldB + 10.0f)/20.0f);
|
|
|
|
m_GainMin = pow(10.0f, (initialLeveldB - 10.0f)/20.0f);
|
|
|
|
|
|
|
|
m_EnergyPrime = 1.0f;
|
2020-02-03 09:34:08 -05:00
|
|
|
m_targetEnergy = 32767.0f;//TODO : Move to parameter ?
|
2020-01-28 15:06:43 -05:00
|
|
|
|
2020-01-29 13:57:12 -05:00
|
|
|
m_Bandwidth = 1e-2f;//TODO : Move to parameter ?
|
|
|
|
m_Alpha = m_Bandwidth;
|
2020-01-28 15:06:43 -05:00
|
|
|
}
|
|
|
|
|
2020-01-29 14:02:03 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// get
|
|
|
|
|
2020-01-29 11:30:22 -05:00
|
|
|
float CAGC::GetGain()
|
|
|
|
{
|
2020-01-29 13:57:12 -05:00
|
|
|
return 20.0f*log10(m_Gain);
|
2020-01-29 11:30:22 -05:00
|
|
|
}
|
|
|
|
|
2020-01-29 14:02:03 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// process
|
|
|
|
|
2020-01-28 15:06:43 -05:00
|
|
|
void CAGC::Apply(uint8 * voice, int size)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < size; i+=2)
|
|
|
|
{
|
|
|
|
//Get the sample
|
2020-01-29 13:57:12 -05:00
|
|
|
float input = (float)(short)MAKEWORD(voice[i+1], voice[i]);
|
2020-01-28 15:06:43 -05:00
|
|
|
|
|
|
|
//apply AGC
|
|
|
|
// apply gain to input sample
|
2020-01-29 13:57:12 -05:00
|
|
|
float output = input * m_Gain;
|
2020-01-28 15:06:43 -05:00
|
|
|
|
2020-02-03 09:34:08 -05:00
|
|
|
// compute output signal energy, scaled to 0 to 1
|
|
|
|
float instantEnergy = abs(output) / m_targetEnergy;
|
2020-01-28 15:06:43 -05:00
|
|
|
|
|
|
|
// smooth energy estimate using single-pole low-pass filter
|
2020-01-29 13:57:12 -05:00
|
|
|
m_EnergyPrime = (1.0f - m_Alpha) * m_EnergyPrime + m_Alpha * instantEnergy;
|
2020-01-28 15:06:43 -05:00
|
|
|
|
|
|
|
// update gain according to output energy
|
2020-01-29 13:57:12 -05:00
|
|
|
if (m_EnergyPrime > 1e-6f)
|
|
|
|
m_Gain *= exp( -0.5f * m_Alpha * log(m_EnergyPrime) );
|
2020-01-28 15:06:43 -05:00
|
|
|
|
2020-01-29 11:30:22 -05:00
|
|
|
// clamp gain
|
2020-02-03 14:10:40 -05:00
|
|
|
if (m_Gain > m_GainMax)
|
2020-01-29 13:57:12 -05:00
|
|
|
m_Gain = m_GainMax;
|
|
|
|
else if(m_Gain < m_GainMin)
|
2020-02-03 14:10:40 -05:00
|
|
|
m_Gain = m_GainMin;
|
2020-01-28 15:06:43 -05:00
|
|
|
|
2020-01-29 11:30:22 -05:00
|
|
|
//write processed sample back
|
2020-01-29 13:57:12 -05:00
|
|
|
voice[i] = HIBYTE((short)output);
|
|
|
|
voice[i+1] = LOBYTE((short)output);
|
2020-01-28 15:06:43 -05:00
|
|
|
}
|
|
|
|
}
|