FastLED 3.9.15
Loading...
Searching...
No Matches
silence_envelope.h
Go to the documentation of this file.
1// SilenceEnvelope — decay-to-target helper for silence gating.
2//
3// Detectors compose a SilenceEnvelope into their update() to translate the
4// boolean `Context::isSilent()` flag into a smooth per-metric decay. During
5// audio the envelope is a pass-through; during silence it exponentially
6// decays the cached value toward `targetValue`. On silence→audio transition
7// it snaps back to the fresh value so beats hit hard with zero attack lag.
8//
9// Example:
10// SilenceEnvelope env; // default tau = 0.5s, target = 0
11// float vol = env.update(context->isSilent(), rawVol, dt);
12
13#pragma once
14
15#include "fl/stl/noexcept.h"
16
17namespace fl {
18namespace audio {
19
21public:
22 struct Config {
23 // Time constant for exponential decay toward targetValue during silence.
24 // After ~3*decayTauSeconds the envelope is within 5% of target.
25 float decayTauSeconds = 0.5f;
26 // Value the envelope decays toward during silence (usually 0).
27 float targetValue = 0.0f;
28 };
29
31 explicit SilenceEnvelope(const Config& cfg) FL_NOEXCEPT;
32
33 void configure(const Config& cfg) FL_NOEXCEPT { mConfig = cfg; }
34
35 // Pass-through when !isSilent (returns currentValue, caches it internally).
36 // Exponential decay toward Config::targetValue when isSilent. Snaps back
37 // to currentValue on silence→audio transition so re-attack has no lag.
38 // `dt` is seconds since last call; use computeAudioDt() from audio_context.h.
39 float update(bool isSilent, float currentValue, float dt) FL_NOEXCEPT;
40
41 // Snap the envelope to `initialValue` (used on reset or explicit jumps).
42 void reset(float initialValue = 0.0f) FL_NOEXCEPT;
43
44 // True once the envelope has decayed to within `epsilon` of targetValue.
45 bool isGated(float epsilon = 1e-4f) const FL_NOEXCEPT;
46
47 // Current envelope output (last value returned by update()).
48 float value() const FL_NOEXCEPT { return mCurrent; }
49
50 const Config& config() const FL_NOEXCEPT { return mConfig; }
51
52private:
54 float mCurrent = 0.0f;
55};
56
57} // namespace audio
58} // namespace fl
static bool isSilent
void configure(const Config &cfg) FL_NOEXCEPT
void reset(float initialValue=0.0f) FL_NOEXCEPT
bool isGated(float epsilon=1e-4f) const FL_NOEXCEPT
float update(bool isSilent, float currentValue, float dt) FL_NOEXCEPT
const Config & config() const FL_NOEXCEPT
float value() const FL_NOEXCEPT
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT