FastLED 3.9.15
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) override;
58 void fireCallbacks() override;
59 bool needsFFT() const override { return true; }
60 bool needsFFTHistory() const override { return false; }
61 const char* getName() const override { return "DropDetector"; }
62 void reset() 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 { return mLastDrop; }
71 u32 getTimeSinceLastDrop(u32 currentTime) const {
72 return currentTime - mLastDrop.timestamp;
73 }
74
75 // Configuration
76 void setImpactThreshold(float threshold) { mImpactThreshold = threshold; }
78 void setBassThreshold(float threshold) { mBassThreshold = threshold; }
79 void setEnergyFluxThreshold(float threshold) { 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;
106 float getMidEnergy(const fft::Bins& fft) const;
107 float getTrebleEnergy(const fft::Bins& fft) const;
108 float calculateSpectralNovelty(float bass, float mid, float treble) const;
109 float calculateEnergyFlux(float currentRMS) const;
110 float calculateBassFlux(float currentBass) const;
111 float calculateDropImpact(float energyFlux, float bassFlux, float spectralNovelty, float rms) const;
112 bool shouldTriggerDrop(float impact, u32 timestamp) const;
113 void updateBaselines(float rms, float bass);
114};
115
116} // namespace detector
117} // namespace audio
118} // namespace fl
float rms(fl::span< const int16_t > data)
Definition simple.h:104
void update(shared_ptr< Context > context) override
Definition drop.cpp.hpp:30
float getTrebleEnergy(const fft::Bins &fft) const
Definition drop.cpp.hpp:136
float calculateEnergyFlux(float currentRMS) const
Definition drop.cpp.hpp:163
function_list< void(const Drop &)> onDropEvent
Definition drop.h:66
bool shouldTriggerDrop(float impact, u32 timestamp) const
Definition drop.cpp.hpp:206
function_list< void()> onDrop
Definition drop.h:65
float calculateBassFlux(float currentBass) const
Definition drop.cpp.hpp:176
~DropDetector() FL_NOEXCEPT override
void setImpactThreshold(float threshold)
Definition drop.h:76
bool needsFFTHistory() const override
Definition drop.h:60
float getBassEnergy(const fft::Bins &fft) const
Definition drop.cpp.hpp:109
const char * getName() const override
Definition drop.h:61
bool needsFFT() const override
Definition drop.h:59
void setEnergyFluxThreshold(float threshold)
Definition drop.h:79
void setMinTimeBetweenDrops(u32 ms)
Definition drop.h:77
void setBassThreshold(float threshold)
Definition drop.h:78
shared_ptr< const fft::Bins > mRetainedFFT
Definition drop.h:102
float calculateDropImpact(float energyFlux, float bassFlux, float spectralNovelty, float rms) const
Definition drop.cpp.hpp:189
const Drop & getLastDrop() const
Definition drop.h:70
function_list< void(float impact)> onDropImpact
Definition drop.h:67
float getMidEnergy(const fft::Bins &fft) const
Definition drop.cpp.hpp:121
u32 getTimeSinceLastDrop(u32 currentTime) const
Definition drop.h:71
void updateBaselines(float rms, float bass)
Definition drop.cpp.hpp:234
float calculateSpectralNovelty(float bass, float mid, float treble) const
Definition drop.cpp.hpp:150
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
Drop() FL_NOEXCEPT
Definition drop.h:23