FastLED 3.9.15
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);
AutoGain() FL_NOEXCEPT
void configure(const AutoGainConfig &config)
Configure the auto gain controller.
Sample process(const Sample &sample)
Process audio sample with automatic gain adjustment.
@ 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)
Sample a pixel from a 2D CRGB grid at floating-point coordinates.
Definition sample.cpp.hpp:9

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_NOEXCEPT
 
 AutoGain (const AutoGainConfig &config)
 
 ~AutoGain () FL_NOEXCEPT
 
void configure (const AutoGainConfig &config)
 Configure the auto gain controller.
 
float getGain () const
 Get current gain multiplier.
 
const StatsgetStats () const
 
Sample process (const Sample &sample)
 Process audio sample with automatic gain adjustment.
 
void reset ()
 Reset internal state.
 
void setSampleRate (int sampleRate)
 Set sample rate for dt computation.
 

Private Member Functions

void applyGain (const vector< i16 > &input, float gain, vector< i16 > &output)
 Apply gain to audio samples.
 
float computeTargetGain ()
 Compute target gain from peak envelope.
 
void resolvePreset ()
 Resolve preset enum into concrete PI tuning parameters.
 
float updatePIController (float targetGain, float dt)
 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: