FastLED 3.9.15
Loading...
Searching...
No Matches
fx_audio.h
Go to the documentation of this file.
1#pragma once
3#include "fl/math/math.h"
4
7public:
12 MaxFadeTracker(float attackTimeSec,
13 float decayTimeSec,
14 float outputTimeSec,
15 float sampleRate)
16 : mAttackRate(1.0f / attackTimeSec)
17 , mDecayRate(1.0f / decayTimeSec)
19 , mSampleRate(sampleRate)
20 , mCurrentLevel(0.0f)
21 , mSmoothedOutput(0.0f)
22 {}
23
24 void setAttackTime(float t){ mAttackRate = 1.0f/t; }
25 void setDecayTime (float t){ mDecayRate = 1.0f/t; }
26 void setOutputTime(float t){ mOutputRate = 1.0f/t; }
27
29 float operator()(const int16_t* samples, size_t length) {
30 assert(length == 512);
31 // 1) block peak
32 float peak = 0.0f;
33 for (size_t i = 0; i < length; ++i) {
34 float v = fl::abs(samples[i]) * (1.0f/32768.0f);
35 peak = fl::max(peak, v);
36 }
37
38 // 2) time delta
39 float dt = static_cast<float>(length) / mSampleRate;
40
41 // 3) update mCurrentLevel with attack/decay
42 if (peak > mCurrentLevel) {
43 float riseFactor = 1.0f - fl::exp(-mAttackRate * dt);
44 mCurrentLevel += (peak - mCurrentLevel) * riseFactor;
45 } else {
46 float decayFactor = fl::exp(-mDecayRate * dt);
47 mCurrentLevel *= decayFactor;
48 }
49
50 // 4) output inertia: smooth mSmoothedOutput → mCurrentLevel
51 float outFactor = 1.0f - fl::exp(-mOutputRate * dt);
53
54 return mSmoothedOutput;
55 }
56
57private:
58 float mAttackRate; // = 1/τ₁
59 float mDecayRate; // = 1/τ₂
60 float mOutputRate; // = 1/τ₃
62 float mCurrentLevel; // instantaneous peak with attack/decay
63 float mSmoothedOutput; // returned value with inertia
64};
fl::UISlider outputTimeSec("outputTimeSec",.17, 0, 2,.01)
float mSmoothedOutput
Definition fx_audio.h:63
float mOutputRate
Definition fx_audio.h:60
float mAttackRate
Definition fx_audio.h:58
MaxFadeTracker(float attackTimeSec, float decayTimeSec, float outputTimeSec, float sampleRate)
Definition fx_audio.h:12
float mDecayRate
Definition fx_audio.h:59
float operator()(const int16_t *samples, size_t length)
Process one 512-sample block; returns [0…1] with inertia.
Definition fx_audio.h:29
float mCurrentLevel
Definition fx_audio.h:62
void setAttackTime(float t)
Definition fx_audio.h:24
void setOutputTime(float t)
Definition fx_audio.h:26
float mSampleRate
Definition fx_audio.h:61
void setDecayTime(float t)
Definition fx_audio.h:25
fl::UISlider length("Length", 1.0f, 0.0f, 1.0f, 0.01f)
static uint32_t t
Definition Luminova.h:55
constexpr common_type_t< T, U > max(T a, U b) FL_NOEXCEPT
Definition math.h:75
enable_if< is_fixed_point< T >::value, T >::type exp(T x) FL_NOEXCEPT
constexpr enable_if< is_fixed_point< T >::value, T >::type abs(T x) FL_NOEXCEPT