FastLED 3.10.8
Loading...
Searching...
No Matches
fl::audio::AutoGain Class Reference

Detailed Description

AutoGain implements adaptive gain control using a PI (proportional-integral) controller with slow peak envelope tracking, inspired by WLED Sound Reactive.

The algorithm:

  1. Track peak envelope of input RMS (fast attack ~10ms, slow decay ~3-7s)
  2. Compute target gain = targetRMSLevel / peakEnvelope
  3. PI controller smoothly drives actual gain toward target gain
  4. Large errors use fast time constant, small errors use slow time constant
  5. Integrator has anti-windup clamping

This design adapts to source-level differences (mic sensitivity, line-in voltage) without tracking musical dynamics, preventing cross-band coupling.

Usage:

config.preset = AGCPreset_Vivid; // Faster response
agc.configure(config);
Sample sample = ...;
Sample amplified = agc.process(sample);
Sample process(const Sample &sample) FL_NO_EXCEPT
Process audio sample with automatic gain adjustment.
void configure(const AutoGainConfig &config) FL_NO_EXCEPT
Configure the auto gain controller.
AutoGain() FL_NO_EXCEPT
@ AGCPreset_Vivid
Faster response: 1.3s peak decay, higher PI gains.
Definition auto_gain.h:15
AGCPreset preset
AGC behavior preset (default: Normal)
Definition auto_gain.h:41
Configuration for automatic gain control.
Definition auto_gain.h:26
CRGB sample(const CRGB *grid, const XYMap &xyMap, float x, float y, SampleMode mode) FL_NO_EXCEPT
Sample a pixel from a 2D CRGB grid at floating-point coordinates.

Definition at line 85 of file auto_gain.h.

#include <auto_gain.h>

+ Collaboration diagram for fl::audio::AutoGain:

Classes

struct  Stats
 Get current statistics (for monitoring/debugging) More...
 

Public Member Functions

 AutoGain () FL_NO_EXCEPT
 
 AutoGain (const AutoGainConfig &config) FL_NO_EXCEPT
 
 ~AutoGain () FL_NO_EXCEPT
 
void configure (const AutoGainConfig &config) FL_NO_EXCEPT
 Configure the auto gain controller.
 
float getGain () const FL_NO_EXCEPT
 Get current gain multiplier.
 
const Stats & getStats () const FL_NO_EXCEPT
 
Sample process (const Sample &sample) FL_NO_EXCEPT
 Process audio sample with automatic gain adjustment.
 
void reset () FL_NO_EXCEPT
 Reset internal state.
 
void setSampleRate (int sampleRate) FL_NO_EXCEPT
 Set sample rate for dt computation.
 

Private Member Functions

void applyGain (const vector< i16 > &input, float gain, vector< i16 > &output) FL_NO_EXCEPT
 Apply gain to audio samples.
 
float computeTargetGain () FL_NO_EXCEPT
 Compute target gain from peak envelope.
 
void resolvePreset () FL_NO_EXCEPT
 Resolve preset enum into concrete PI tuning parameters.
 
float updatePIController (float targetGain, float dt) FL_NO_EXCEPT
 Update PI controller toward target gain.
 

Private Attributes

AutoGainConfig mConfig
 
float mGainFollowFastTau = 0.38f
 
float mGainFollowSlowTau = 12.3f
 
float mIntegrator = 0.0f
 PI integrator state.
 
float mKi = 1.7f
 
float mKp = 0.6f
 
float mLastGain = 1.0f
 Last smoothed gain output.
 
vector< i16 > mOutputBuffer
 Working buffer (reused to avoid allocations)
 
float mPeakDecayTau = 3.3f
 
AttackDecayFilter< float > mPeakEnvelope {0.01f, 3.3f, 0.0f}
 Peak envelope tracker: fast attack (10ms), slow decay (preset-dependent)
 
int mSampleRate = 44100
 
Stats mStats
 

The documentation for this class was generated from the following files: