From 3c06fb0ac12a1bc143b658d9fe6df32e6279963c Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Mon, 3 Feb 2020 15:34:08 +0100 Subject: [PATCH] Do not square energy as we are not complex. Do not clamp gain --- ambed/cagc.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ambed/cagc.cpp b/ambed/cagc.cpp index 619a59f..7cf5e2e 100644 --- a/ambed/cagc.cpp +++ b/ambed/cagc.cpp @@ -42,7 +42,7 @@ CAGC::CAGC(float initialLeveldB) m_GainMin = pow(10.0f, (initialLeveldB - 10.0f)/20.0f); m_EnergyPrime = 1.0f; - m_targetEnergy = 32768.0f;//TODO : Move to parameter ? + m_targetEnergy = 32767.0f;//TODO : Move to parameter ? m_Bandwidth = 1e-2f;//TODO : Move to parameter ? m_Alpha = m_Bandwidth; @@ -70,8 +70,8 @@ void CAGC::Apply(uint8 * voice, int size) // apply gain to input sample float output = input * m_Gain; - // compute output signal energy - float instantEnergy = (output * output) / m_targetEnergy; + // compute output signal energy, scaled to 0 to 1 + float instantEnergy = abs(output) / m_targetEnergy; // smooth energy estimate using single-pole low-pass filter m_EnergyPrime = (1.0f - m_Alpha) * m_EnergyPrime + m_Alpha * instantEnergy; @@ -81,10 +81,10 @@ void CAGC::Apply(uint8 * voice, int size) m_Gain *= exp( -0.5f * m_Alpha * log(m_EnergyPrime) ); // clamp gain - if (m_Gain > m_GainMax) + /*if (m_Gain > m_GainMax) m_Gain = m_GainMax; else if(m_Gain < m_GainMin) - m_Gain = m_GainMin; + m_Gain = m_GainMin;*/ //write processed sample back voice[i] = HIBYTE((short)output);