FastLED 3.9.15
Loading...
Searching...
No Matches
auto_gain.h
Go to the documentation of this file.
1#pragma once
2
4#include "fl/stl/int.h"
5#include "fl/stl/vector.h"
6#include "fl/stl/noexcept.h"
7
8namespace fl {
9namespace audio {
10
19
28 bool enabled = true;
29
31 float minGain = 1.0f / 64.0f;
32
34 float maxGain = 32.0f;
35
38 float targetRMSLevel = 8000.0f;
39
42
43 // --- Custom PI tuning (only used when preset == AGCPreset_Custom) ---
44
47 float peakDecayTau = 3.3f;
48
50 float kp = 0.6f;
51
53 float ki = 1.7f;
54
56 float gainFollowSlowTau = 12.3f;
57
59 float gainFollowFastTau = 0.38f;
60};
61
85class AutoGain {
86public:
88 explicit AutoGain(const AutoGainConfig& config);
90
92 void configure(const AutoGainConfig& config);
93
98
100 void reset();
101
103 struct Stats {
104 float currentGain = 1.0f; // Current gain multiplier
105 float peakEnvelope = 0.0f; // Current peak envelope estimate
106 float targetGain = 1.0f; // Target gain from PI controller
107 float integrator = 0.0f; // PI integrator state
108 float inputRMS = 0.0f; // Most recent input RMS
109 float outputRMS = 0.0f; // Most recent output RMS
110 u32 samplesProcessed = 0; // Total samples processed
111 };
112
113 const Stats& getStats() const { return mStats; }
114
116 float getGain() const { return mStats.currentGain; }
117
119 void setSampleRate(int sampleRate) { mSampleRate = sampleRate; }
120
121private:
123 void resolvePreset();
124
126 float computeTargetGain();
127
132 float updatePIController(float targetGain, float dt);
133
138 void applyGain(const vector<i16>& input, float gain, vector<i16>& output);
139
142 int mSampleRate = 44100;
143
144 // Resolved preset parameters (populated by resolvePreset())
145 float mPeakDecayTau = 3.3f;
146 float mKp = 0.6f;
147 float mKi = 1.7f;
148 float mGainFollowSlowTau = 12.3f;
149 float mGainFollowFastTau = 0.38f;
150
153
155 float mIntegrator = 0.0f;
156
158 float mLastGain = 1.0f;
159
162};
163
164} // namespace audio
165} // namespace fl
void reset()
Reset internal state.
AutoGain() FL_NOEXCEPT
~AutoGain() FL_NOEXCEPT
float mLastGain
Last smoothed gain output.
Definition auto_gain.h:158
float getGain() const
Get current gain multiplier.
Definition auto_gain.h:116
AttackDecayFilter< float > mPeakEnvelope
Peak envelope tracker: fast attack (10ms), slow decay (preset-dependent)
Definition auto_gain.h:152
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)
Definition auto_gain.h:161
float updatePIController(float targetGain, float dt)
Update PI controller toward target gain.
void setSampleRate(int sampleRate)
Set sample rate for dt computation.
Definition auto_gain.h:119
void configure(const AutoGainConfig &config)
Configure the auto gain controller.
float computeTargetGain()
Compute target gain from peak envelope.
const Stats & getStats() const
Definition auto_gain.h:113
float mIntegrator
PI integrator state.
Definition auto_gain.h:155
AutoGainConfig mConfig
Definition auto_gain.h:140
Sample process(const Sample &sample)
Process audio sample with automatic gain adjustment.
void resolvePreset()
Resolve preset enum into concrete PI tuning parameters.
Get current statistics (for monitoring/debugging)
Definition auto_gain.h:103
AGCPreset
AGC preset selection — derived from WLED Sound Reactive's proven approach.
Definition auto_gain.h:13
@ AGCPreset_Vivid
Faster response: 1.3s peak decay, higher PI gains.
Definition auto_gain.h:15
@ AGCPreset_Normal
Balanced: 3.3s peak decay, moderate PI gains.
Definition auto_gain.h:14
@ AGCPreset_Custom
Use custom PI tuning fields below.
Definition auto_gain.h:17
@ AGCPreset_Lazy
Slower, more stable: 6.7s peak decay, lower PI gains.
Definition auto_gain.h:16
float kp
Proportional gain for PI controller.
Definition auto_gain.h:50
float ki
Integral gain for PI controller.
Definition auto_gain.h:53
float targetRMSLevel
Target RMS level after gain (0-32767) The AGC will adjust gain to maintain this average level.
Definition auto_gain.h:38
float peakDecayTau
Peak envelope decay time constant (seconds).
Definition auto_gain.h:47
float maxGain
Maximum gain multiplier (prevents over-amplification)
Definition auto_gain.h:34
AGCPreset preset
AGC behavior preset (default: Normal)
Definition auto_gain.h:41
float minGain
Minimum gain multiplier (prevents over-attenuation)
Definition auto_gain.h:31
float gainFollowSlowTau
Slow gain-follow time constant (seconds) — used when error is small.
Definition auto_gain.h:56
float gainFollowFastTau
Fast gain-follow time constant (seconds) — used when error is large.
Definition auto_gain.h:59
bool enabled
Enable automatic gain adjustment.
Definition auto_gain.h:28
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
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT