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

◆ detectBeat()

void fl::audio::Reactive::detectBeat ( fl::u32 currentTimeMs)
private

Definition at line 367 of file audio_reactive.cpp.hpp.

367 {
368 // Need minimum time since last beat
369 if (currentTimeMs - mLastBeatTime < BEAT_COOLDOWN) {
370 mCurrentData.beatDetected = false;
371 return;
372 }
373
374 // Use raw volume for beat detection — it is proportional to amplitude.
375 // Adaptive volume (mCurrentData.volume) converges toward a steady state,
376 // masking the transient jumps that indicate beats.
377 // NOTE: this runs BEFORE applyGain(), so beat detection is gain-independent.
378 float currentVolume = mCurrentData.volumeRaw;
379
380 // Beat detected if volume significantly increased
381 if (currentVolume > mPreviousVolume + mVolumeThreshold &&
382 currentVolume > 0.02f) { // Minimum volume threshold
383 mCurrentData.beatDetected = true;
384 mLastBeatTime = currentTimeMs;
385 } else {
386 mCurrentData.beatDetected = false;
387 }
388
389 // Update previous volume for next comparison using attack/decay
390 float beatAttackRate = mConfig.attack / 255.0f * 0.5f + 0.1f; // 0.1 to 0.6
391 float beatDecayRate = mConfig.decay / 255.0f * 0.3f + 0.05f; // 0.05 to 0.35
392
393 if (currentVolume > mPreviousVolume) {
394 // Rising volume - use attack rate (faster tracking)
395 mPreviousVolume = mPreviousVolume * (1.0f - beatAttackRate) + currentVolume * beatAttackRate;
396 } else {
397 // Falling volume - use decay rate (slower tracking)
398 mPreviousVolume = mPreviousVolume * (1.0f - beatDecayRate) + currentVolume * beatDecayRate;
399 }
400}
ReactiveConfig mConfig
static constexpr fl::u32 BEAT_COOLDOWN

References BEAT_COOLDOWN, mConfig, mCurrentData, mLastBeatTime, mPreviousVolume, and mVolumeThreshold.

Referenced by processSample().

+ Here is the caller graph for this function: