private void ProcessInternal(float value) { // Add value to running average if (_sampleCount > 0) { _state -= _samples[(_sampleIndex + 1) == _samples.Length ? 0 : (_sampleIndex + 1)]; } InsertSample(value); _state += value; value = (float)/*Math.Sqrt*/ (_state / _sampleCount); //_state = (_state * 0.9995f) + ((outputVol * outputVol) * 0.0005f); //outputVol = (float)Math.Sqrt(_state); // Adjust gain with error value float error = _targetAmp - value; if (_isAdaptAllowed) { _gain = _gainIntegrator.Process(error); _gain = Math.Max(Math.Min(_gain, _maxGain), 0); if (_gain == _maxGain) { _gainIntegrator.SetValue(_maxGain); } } }
public AGC(float targetAmplitude, float maxGain) { _targetAmp = targetAmplitude; _maxGain = maxGain; _state = 0f; _samples = new float[200]; _isAdaptAllowed = true; _gainIntegrator = new Integrator(1f, (1f / 44100f) * 1000f); _gainIntegrator.SetValue(1.0f); _gain = 1.0f; _clampLevel = 1.0f; }