MCPcopy Create free account
hub / github.com/FastLED/FastLED / detectBeat

Function detectBeat

examples/Audio/advanced/advanced.h:123–151  ·  view source on GitHub ↗

Beat detection algorithm

Source from the content-addressed store, hash-verified

121
122// Beat detection algorithm
123bool detectBeat(float energy) {
124 beatHistory[beatHistoryIndex] = energy;
125 beatHistoryIndex = (beatHistoryIndex + 1) % 20;
126
127 // Calculate average
128 beatAverage = 0;
129 for (int i = 0; i < 20; i++) {
130 beatAverage += beatHistory[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}
152
153// Update auto gain
154void updateAutoGain(float level) {

Callers 3

processSampleMethod · 0.85
updateMethod · 0.85
loopFunction · 0.85

Calls 3

sqrtFunction · 0.85
millisFunction · 0.50
valueMethod · 0.45

Tested by

no test coverage detected