From 0a06b267485f591cb8bc2069dac4447f21507bfd Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Wed, 29 Jan 2020 19:37:35 +0100 Subject: [PATCH] Apply Scale to energy, value still needs to be tweaked --- ambed/cagc.cpp | 12 ++---------- ambed/cagc.h | 2 +- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/ambed/cagc.cpp b/ambed/cagc.cpp index d388442..477bd7f 100644 --- a/ambed/cagc.cpp +++ b/ambed/cagc.cpp @@ -42,7 +42,7 @@ CAGC::CAGC(float initialLeveldB) if (m_g < 1e-16f) m_g = 1e-16f; - m_scale = (float)0xFFFF; + m_scale = 32768.0f; m_bandwidth = 1e-2f; //TODO : Move to parameter ? m_gMax = pow(10.0f, (initialLeveldB + 10.0f)/20.0f);//+- 10dB Margin, TODO Move margin to constant m_gMin = pow(10.0f, (initialLeveldB - 10.0f)/20.0f); @@ -62,17 +62,12 @@ void CAGC::Apply(uint8 * voice, int size) //Get the sample float _x = (float)(short)MAKEWORD(voice[i+1], voice[i]); - //This AGC tries to smooth energy so it does not exceed 1 - //Therefore divide by our max supposed value - //Maybe we could also change y2 prime calculation below, but for now stick with this - _x /= m_scale; - //apply AGC // apply gain to input sample float _y = _x * m_g; // compute output signal energy - float y2 = _y * _y; + float y2 = (_y * _y) / m_scale; // smooth energy estimate using single-pole low-pass filter m_y2_prime = (1.0f - m_alpha) * m_y2_prime + m_alpha*y2; @@ -87,9 +82,6 @@ void CAGC::Apply(uint8 * voice, int size) else if(m_g < m_gMin) m_g = m_gMin; - // apply output scale - _y *= m_scale; - //write processed sample back voice[i] = HIBYTE((short)_y); voice[i+1] = LOBYTE((short)_y); diff --git a/ambed/cagc.h b/ambed/cagc.h index ad8aa6e..080d665 100644 --- a/ambed/cagc.h +++ b/ambed/cagc.h @@ -43,7 +43,7 @@ public: private: float m_g; // current gain value float m_gMax, m_gMin; //gain clamping - float m_scale; // scale value + float m_scale; // scale value for target energy // gain control loop filter parameters float m_bandwidth; // bandwidth-time constant