FastLED 3.10.8
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) FL_NO_EXCEPT;
90
92 void configure(const AutoGainConfig& config) FL_NO_EXCEPT;
93
98
100 void reset() FL_NO_EXCEPT;
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 FL_NO_EXCEPT { return mStats; }
114
116 float getGain() const FL_NO_EXCEPT { return mStats.currentGain; }
117
119 void setSampleRate(int sampleRate) FL_NO_EXCEPT { mSampleRate = sampleRate; }
120
121private:
124
127
132 float updatePIController(float targetGain, float dt) FL_NO_EXCEPT;
133
138 void applyGain(const vector<i16>& input, float gain, vector<i16>& output) FL_NO_EXCEPT;
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
152 AttackDecayFilter<float> mPeakEnvelope{0.01f, 3.3f, 0.0f};
153
155 float mIntegrator = 0.0f;
156
158 float mLastGain = 1.0f;
159
162};
163
164} // namespace audio
165} // namespace fl
void applyGain(const vector< i16 > &input, float gain, vector< i16 > &output) FL_NO_EXCEPT
Apply gain to audio samples.
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.
void reset() FL_NO_EXCEPT
Reset internal state.
float mLastGain
Last smoothed gain output.
Definition auto_gain.h:158
void setSampleRate(int sampleRate) FL_NO_EXCEPT
Set sample rate for dt computation.
Definition auto_gain.h:119
AttackDecayFilter< float > mPeakEnvelope
Peak envelope tracker: fast attack (10ms), slow decay (preset-dependent)
Definition auto_gain.h:152
float updatePIController(float targetGain, float dt) FL_NO_EXCEPT
Update PI controller toward target gain.
AutoGain() FL_NO_EXCEPT
vector< i16 > mOutputBuffer
Working buffer (reused to avoid allocations)
Definition auto_gain.h:161
float computeTargetGain() FL_NO_EXCEPT
Compute target gain from peak envelope.
float getGain() const FL_NO_EXCEPT
Get current gain multiplier.
Definition auto_gain.h:116
~AutoGain() FL_NO_EXCEPT
const Stats & getStats() const FL_NO_EXCEPT
Definition auto_gain.h:113
float mIntegrator
PI integrator state.
Definition auto_gain.h:155
AutoGainConfig mConfig
Definition auto_gain.h:140
void resolvePreset() FL_NO_EXCEPT
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
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
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.
Base definition for an LED controller.