FastLED 3.9.15
Loading...
Searching...
No Matches
fx.h
Go to the documentation of this file.
1#include <algorithm>
2#include <cstdint>
3#include <cmath>
4#include <cassert>
5
6#include "fl/time_alpha.h"
7
10public:
15 MaxFadeTracker(float attackTimeSec,
16 float decayTimeSec,
17 float outputTimeSec,
18 float sampleRate)
19 : attackRate_(1.0f / attackTimeSec)
20 , decayRate_(1.0f / decayTimeSec)
22 , sampleRate_(sampleRate)
23 , currentLevel_(0.0f)
24 , smoothedOutput_(0.0f)
25 {}
26
27 void setAttackTime(float t){ attackRate_ = 1.0f/t; }
28 void setDecayTime (float t){ decayRate_ = 1.0f/t; }
29 void setOutputTime(float t){ outputRate_ = 1.0f/t; }
30
32 float operator()(const int16_t* samples, size_t length) {
33 assert(length == 512);
34 // 1) block peak
35 float peak = 0.0f;
36 for (size_t i = 0; i < length; ++i) {
37 float v = std::abs(samples[i]) * (1.0f/32768.0f);
38 peak = std::max(peak, v);
39 }
40
41 // 2) time delta
42 float dt = static_cast<float>(length) / sampleRate_;
43
44 // 3) update currentLevel_ with attack/decay
45 if (peak > currentLevel_) {
46 float riseFactor = 1.0f - std::exp(-attackRate_ * dt);
47 currentLevel_ += (peak - currentLevel_) * riseFactor;
48 } else {
49 float decayFactor = std::exp(-decayRate_ * dt);
50 currentLevel_ *= decayFactor;
51 }
52
53 // 4) output inertia: smooth smoothedOutput_ → currentLevel_
54 float outFactor = 1.0f - std::exp(-outputRate_ * dt);
56
57 return smoothedOutput_;
58 }
59
60private:
61 float attackRate_; // = 1/τ₁
62 float decayRate_; // = 1/τ₂
63 float outputRate_; // = 1/τ₃
65 float currentLevel_; // instantaneous peak with attack/decay
66 float smoothedOutput_; // returned value with inertia
67};
UISlider outputTimeSec("outputTimeSec",.17, 0, 2,.01)
float sampleRate_
Definition fx.h:64
float outputRate_
Definition fx.h:63
MaxFadeTracker(float attackTimeSec, float decayTimeSec, float outputTimeSec, float sampleRate)
Definition fx.h:15
float currentLevel_
Definition fx.h:65
float decayRate_
Definition fx.h:62
float smoothedOutput_
Definition fx.h:66
float operator()(const int16_t *samples, size_t length)
Process one 512-sample block; returns [0…1] with inertia.
Definition fx.h:32
void setAttackTime(float t)
Definition fx.h:27
void setOutputTime(float t)
Definition fx.h:29
void setDecayTime(float t)
Definition fx.h:28
float attackRate_
Definition fx.h:61
UISlider length("Length", 1.0f, 0.0f, 1.0f, 0.01f)