|
|
|
|
@ -62,27 +62,33 @@ float CAGC::GetGain()
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// process
|
|
|
|
|
|
|
|
|
|
inline float CAGC::ProcessSample(float input)
|
|
|
|
|
inline void CAGC::ProcessSampleBlock(uint8* voice, int length)
|
|
|
|
|
{
|
|
|
|
|
//apply AGC
|
|
|
|
|
// apply gain to input sample
|
|
|
|
|
float output = input * m_Gain;
|
|
|
|
|
for(int i = 0; i < length; i += 2)
|
|
|
|
|
{
|
|
|
|
|
float input = (float)(short)MAKEWORD(voice[i+1], voice[i]);
|
|
|
|
|
//apply AGC
|
|
|
|
|
// apply gain to input sample
|
|
|
|
|
float output = input * m_Gain;
|
|
|
|
|
|
|
|
|
|
// compute output signal energy, scaled to 0 to 1
|
|
|
|
|
float instantEnergy = abs(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;
|
|
|
|
|
// smooth energy estimate using single-pole low-pass filter
|
|
|
|
|
m_EnergyPrime = (1.0f - m_Alpha) * m_EnergyPrime + m_Alpha * instantEnergy;
|
|
|
|
|
|
|
|
|
|
// update gain according to output energy
|
|
|
|
|
if (m_EnergyPrime > 1e-6f)
|
|
|
|
|
m_Gain *= exp( -0.5f * m_Alpha * log(m_EnergyPrime) );
|
|
|
|
|
// update gain according to output energy
|
|
|
|
|
if (m_EnergyPrime > 1e-6f)
|
|
|
|
|
m_Gain *= exp( -0.5f * m_Alpha * log(m_EnergyPrime) );
|
|
|
|
|
|
|
|
|
|
// clamp gain
|
|
|
|
|
if (m_Gain > m_GainMax)
|
|
|
|
|
m_Gain = m_GainMax;
|
|
|
|
|
else if(m_Gain < m_GainMin)
|
|
|
|
|
m_Gain = m_GainMin;
|
|
|
|
|
// clamp gain
|
|
|
|
|
if (m_Gain > m_GainMax)
|
|
|
|
|
m_Gain = m_GainMax;
|
|
|
|
|
else if(m_Gain < m_GainMin)
|
|
|
|
|
m_Gain = m_GainMin;
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
//write processed sample back
|
|
|
|
|
voice[i] = HIBYTE((short)output);
|
|
|
|
|
voice[i+1] = LOBYTE((short)output);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|