FastLED 3.9.15
Loading...
Searching...
No Matches

◆ detectBeat()

bool detectBeat ( float energy)

Definition at line 123 of file advanced.h.

123 {
126
127 // Calculate average
128 beatAverage = 0;
129 for (int i = 0; i < 20; i++) {
131 }
132 beatAverage /= 20.0f;
133
134 // Calculate variance
135 beatVariance = 0;
136 for (int i = 0; i < 20; i++) {
137 float diff = beatHistory[i] - beatAverage;
138 beatVariance += diff * diff;
139 }
140 beatVariance /= 20.0f;
141
142 // Detect beat
143 float threshold = beatAverage + (beatSensitivity.value() * fl::sqrt(beatVariance));
144 uint32_t currentTime = millis();
145
146 if (energy > threshold && (currentTime - lastBeatTime) > 80) {
147 lastBeatTime = currentTime;
148 return true;
149 }
150 return false;
151}
uint32_t lastBeatTime
Definition advanced.h:88
float beatAverage
Definition advanced.h:86
float beatVariance
Definition advanced.h:87
int beatHistoryIndex
Definition advanced.h:85
float beatHistory[20]
Definition advanced.h:84
fl::UISlider beatSensitivity("Beat Sensitivity", 1.5f, 0.5f, 3.0f, 0.1f)
fl::u32 uint32_t
Definition s16x16x4.h:219
fl::u32 millis()
Universal millisecond timer - returns milliseconds since system startup.
constexpr enable_if< is_fixed_point< T >::value, T >::type sqrt(T x) FL_NOEXCEPT

References beatAverage, beatHistory, beatHistoryIndex, beatSensitivity(), beatVariance, lastBeatTime, and fl::sqrt().

Referenced by loop(), and fl::audio::detector::Beat::update().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: