75 mStats.samplesProcessed = 0;
90 const float inputRMS =
sample.rms();
91 mStats.inputRMS = inputRMS;
100 const float silenceThreshold = 10.0f;
101 if (inputRMS < silenceThreshold) {
109 mStats.peakEnvelope = peakEnv;
113 mStats.targetGain = targetGain;
121 mStats.currentGain = clampedGain;
125 const auto& pcm =
sample.pcm();
152 if (peakEnv < 1.0f) {
156 return mConfig.targetRMSLevel / peakEnv;
160 const float error = targetGain -
mLastGain;
164 const bool largeError = (
mLastGain > 0.1f)
170 const float pTerm =
mKp * error;
174 const float piTarget = targetGain + pTerm +
mIntegrator;
178 if (tau > 0.0f && dt > 0.0f) {
179 alpha = 1.0f -
fl::exp(-dt / tau);
186 const bool saturated = (unclamped >
mConfig.maxGain) || (unclamped <
mConfig.minGain);
193 const float maxIntegral = 4.0f *
mConfig.maxGain;
201 output.reserve(input.
size());
203 for (size i = 0; i < input.
size(); ++i) {
205 float amplified =
static_cast<float>(input[i]) * gain;
208 if (amplified > 32767.0f) amplified = 32767.0f;
209 if (amplified < -32768.0f) amplified = -32768.0f;
211 output.push_back(
static_cast<i16
>(amplified));
void reset()
Reset internal state.
float mLastGain
Last smoothed gain output.
AttackDecayFilter< float > mPeakEnvelope
Peak envelope tracker: fast attack (10ms), slow decay (preset-dependent)
void applyGain(const vector< i16 > &input, float gain, vector< i16 > &output)
Apply gain to audio samples.
vector< i16 > mOutputBuffer
Working buffer (reused to avoid allocations)
float updatePIController(float targetGain, float dt)
Update PI controller toward target gain.
void configure(const AutoGainConfig &config)
Configure the auto gain controller.
float computeTargetGain()
Compute target gain from peak envelope.
float mIntegrator
PI integrator state.
Sample process(const Sample &sample)
Process audio sample with automatic gain adjustment.
void resolvePreset()
Resolve preset enum into concrete PI tuning parameters.
fl::size size() const FL_NOEXCEPT
@ AGCPreset_Vivid
Faster response: 1.3s peak decay, higher PI gains.
@ AGCPreset_Normal
Balanced: 3.3s peak decay, moderate PI gains.
@ AGCPreset_Custom
Use custom PI tuning fields below.
@ AGCPreset_Lazy
Slower, more stable: 6.7s peak decay, lower PI gains.
Configuration for automatic gain control.
FL_DISABLE_WARNING_PUSH U constexpr common_type_t< T, U > min(T a, U b) FL_NOEXCEPT
float sqrtf(float value) FL_NOEXCEPT
constexpr common_type_t< T, U > max(T a, U b) FL_NOEXCEPT
CRGB sample(const CRGB *grid, const XYMap &xyMap, float x, float y, SampleMode mode)
Sample a pixel from a 2D CRGB grid at floating-point coordinates.
shared_ptr< T > make_shared(Args &&... args) FL_NOEXCEPT
enable_if< is_fixed_point< T >::value, T >::type exp(T x) FL_NOEXCEPT
constexpr enable_if< is_fixed_point< T >::value, T >::type abs(T x) FL_NOEXCEPT
Base definition for an LED controller.