FastLED 3.10.8
Loading...
Searching...
No Matches
drop.h
Go to the documentation of this file.
1// DropDetector.h - EDM drop detection for FastLED audio system
2// Detects sudden energy bursts and dramatic changes typical of EDM drops
3
4#pragma once
5
7#include "fl/stl/function.h"
8#include "fl/stl/noexcept.h"
9
10namespace fl {
11namespace audio {
12namespace detector {
13
14// Forward declarations
15
16// Drop event structure
17struct Drop {
18 float impact; // Impact strength (0.0 to 1.0)
19 float bassEnergy; // Bass energy at drop
20 float energyIncrease; // Energy increase from previous state
21 u32 timestamp; // When drop occurred
22
24 : impact(0.0f)
25 , bassEnergy(0.0f)
26 , energyIncrease(0.0f)
27 , timestamp(0) {}
28};
29
30// DropDetector - Detects sudden energy bursts and drops in EDM music
31//
32// EDM drops are characterized by:
33// 1. Sudden, dramatic energy increase (energy burst)
34// 2. Strong bass impact (sub-bass and bass frequencies)
35// 3. High spectral change (new elements introduced)
36// 4. Often follows a buildup or break
37//
38// The detector uses multiple indicators to identify drops with high confidence:
39// - Energy flux (rate of energy change)
40// - Bass energy surge
41// - Spectral novelty (dramatic frequency content change)
42// - Temporal context (time since last drop)
43//
44// Optimized for EDM, trap, dubstep, future bass, and similar genres.
45//
46// Usage:
47// DropDetector drop;
48// drop.onDrop([](const Drop& dropEvent) { /* React to drop */ });
49// drop.onDropImpact([](float impact) { /* Scale effect by impact */ });
50//
51class DropDetector : public Detector {
52public:
55
56 // Detector interface
57 void update(shared_ptr<Context> context) FL_NO_EXCEPT override;
58 void fireCallbacks() FL_NO_EXCEPT override;
59 bool needsFFT() const FL_NO_EXCEPT override { return true; }
60 bool needsFFTHistory() const FL_NO_EXCEPT override { return false; }
61 const char* getName() const FL_NO_EXCEPT override { return "DropDetector"; }
62 void reset() FL_NO_EXCEPT override;
63
64 // Event callbacks (multiple listeners supported)
65 function_list<void()> onDrop; // Fired when drop detected
66 function_list<void(const Drop&)> onDropEvent; // Fired with drop details
67 function_list<void(float impact)> onDropImpact; // Fired with impact strength
68
69 // State access
70 const Drop& getLastDrop() const FL_NO_EXCEPT { return mLastDrop; }
71 u32 getTimeSinceLastDrop(u32 currentTime) const FL_NO_EXCEPT {
72 return currentTime - mLastDrop.timestamp;
73 }
74
75 // Configuration
76 void setImpactThreshold(float threshold) FL_NO_EXCEPT { mImpactThreshold = threshold; }
78 void setBassThreshold(float threshold) FL_NO_EXCEPT { mBassThreshold = threshold; }
79 void setEnergyFluxThreshold(float threshold) FL_NO_EXCEPT { mEnergyFluxThreshold = threshold; }
80
81private:
82 // Last drop event
85
86 // Previous frame state for flux calculation
87 float mPrevRMS;
91
92 // Short-term energy tracking (for baseline)
93 float mEnergyBaseline; // Rolling average of recent energy
94 float mBassBaseline; // Rolling average of recent bass
95
96 // Configuration
97 float mImpactThreshold; // Minimum impact to trigger drop
98 u32 mMinTimeBetweenDrops; // Cooldown period between drops (ms)
99 float mBassThreshold; // Minimum bass energy ratio
100 float mEnergyFluxThreshold; // Minimum energy increase ratio
101
103
104 // Analysis methods
105 float getBassEnergy(const fft::Bins& fft) const FL_NO_EXCEPT;
106 float getMidEnergy(const fft::Bins& fft) const FL_NO_EXCEPT;
107 float getTrebleEnergy(const fft::Bins& fft) const FL_NO_EXCEPT;
108 float calculateSpectralNovelty(float bass, float mid, float treble) const FL_NO_EXCEPT;
109 float calculateEnergyFlux(float currentRMS) const FL_NO_EXCEPT;
110 float calculateBassFlux(float currentBass) const FL_NO_EXCEPT;
111 float calculateDropImpact(float energyFlux, float bassFlux, float spectralNovelty, float rms) const FL_NO_EXCEPT;
112 bool shouldTriggerDrop(float impact, u32 timestamp) const FL_NO_EXCEPT;
113 void updateBaselines(float rms, float bass) FL_NO_EXCEPT;
114};
115
116} // namespace detector
117} // namespace audio
118} // namespace fl
float rms(fl::span< const int16_t > data)
Definition simple.h:104
function_list< void(const Drop &)> onDropEvent
Definition drop.h:66
void fireCallbacks() FL_NO_EXCEPT override
Definition drop.cpp.hpp:82
function_list< void()> onDrop
Definition drop.h:65
const Drop & getLastDrop() const FL_NO_EXCEPT
Definition drop.h:70
bool needsFFTHistory() const FL_NO_EXCEPT override
Definition drop.h:60
~DropDetector() FL_NO_EXCEPT override
void setImpactThreshold(float threshold) FL_NO_EXCEPT
Definition drop.h:76
void setBassThreshold(float threshold) FL_NO_EXCEPT
Definition drop.h:78
float getBassEnergy(const fft::Bins &fft) const FL_NO_EXCEPT
Definition drop.cpp.hpp:107
void update(shared_ptr< Context > context) FL_NO_EXCEPT override
Definition drop.cpp.hpp:30
u32 getTimeSinceLastDrop(u32 currentTime) const FL_NO_EXCEPT
Definition drop.h:71
float calculateSpectralNovelty(float bass, float mid, float treble) const FL_NO_EXCEPT
Definition drop.cpp.hpp:148
bool needsFFT() const FL_NO_EXCEPT override
Definition drop.h:59
void updateBaselines(float rms, float bass) FL_NO_EXCEPT
Definition drop.cpp.hpp:232
shared_ptr< const fft::Bins > mRetainedFFT
Definition drop.h:102
const char * getName() const FL_NO_EXCEPT override
Definition drop.h:61
float getMidEnergy(const fft::Bins &fft) const FL_NO_EXCEPT
Definition drop.cpp.hpp:119
float calculateBassFlux(float currentBass) const FL_NO_EXCEPT
Definition drop.cpp.hpp:174
function_list< void(float impact)> onDropImpact
Definition drop.h:67
void setEnergyFluxThreshold(float threshold) FL_NO_EXCEPT
Definition drop.h:79
float getTrebleEnergy(const fft::Bins &fft) const FL_NO_EXCEPT
Definition drop.cpp.hpp:134
float calculateEnergyFlux(float currentRMS) const FL_NO_EXCEPT
Definition drop.cpp.hpp:161
void setMinTimeBetweenDrops(u32 ms) FL_NO_EXCEPT
Definition drop.h:77
void reset() FL_NO_EXCEPT override
Definition drop.cpp.hpp:97
float calculateDropImpact(float energyFlux, float bassFlux, float spectralNovelty, float rms) const FL_NO_EXCEPT
Definition drop.cpp.hpp:187
bool shouldTriggerDrop(float impact, u32 timestamp) const FL_NO_EXCEPT
Definition drop.cpp.hpp:204
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
Base definition for an LED controller.
Definition crgb.hpp:179
Drop() FL_NO_EXCEPT
Definition drop.h:23