FastLED 3.9.15
Loading...
Searching...
No Matches
drop_detector.h
Go to the documentation of this file.
1#pragma once
2#include "fl/stl/noexcept.h"
3
4namespace fl {
5namespace audio {
6namespace detector {
7
8struct Drop {
9 float impact = 0.0f; // Impact strength of the drop (0-1)
10 float bassEnergy = 0.0f; // Bass energy at the time of drop
11 float energyIncrease = 0.0f; // Relative energy increase
12 u32 timestamp = 0; // When the drop occurred
13
14 // Default constructor
15 Drop() FL_NOEXCEPT = default;
16};
17
18class DropDetector {
19public:
22
23 void update(shared_ptr<Context> context);
24 void reset();
25
26 // Callback types
27 using VoidCallback = void(*)();
28 using DropCallback = void(*)(const Drop&);
29 using ImpactCallback = void(*)(float);
30
31 // Callbacks for drop events
32 VoidCallback onDrop = nullptr; // Simplest drop event (just happened)
33 DropCallback onDropEvent = nullptr; // Detailed drop event information
34 ImpactCallback onDropImpact = nullptr; // Drop impact strength callback
35
36 // Configuration methods
37 void setImpactThreshold(float threshold) { mImpactThreshold = threshold; }
39 void setBassThreshold(float threshold) { mBassThreshold = threshold; }
40 void setEnergyFluxThreshold(float threshold) { mEnergyFluxThreshold = threshold; }
41
42 // Getters
43 const Drop& getLastDrop() const { return mLastDrop; }
45
46private:
47 // Previous frame data for comparisons
48 float mPrevRMS;
49 float mPrevBassEnergy;
50 float mPrevMidEnergy;
52
53 // Baselines for energy
54 float mEnergyBaseline;
55 float mBassBaseline;
56
57 // Detection thresholds
58 float mImpactThreshold; // Impact level required to trigger drop
59 u32 mMinTimeBetweenDrops; // Cooldown between detected drops
60 float mBassThreshold; // Bass energy threshold for drop
61 float mEnergyFluxThreshold; // Minimum energy change required
62
63 // Last detected drop
65
66 // Energy calculation helpers
67 float getBassEnergy(const fft::Bins& fft) const;
68 float getMidEnergy(const fft::Bins& fft) const;
69 float getTrebleEnergy(const fft::Bins& fft) const;
70
71 // Drop detection calculations
72 float calculateSpectralNovelty(float bass, float mid, float treble) const;
73 float calculateEnergyFlux(float currentRMS) const;
74 float calculateBassFlux(float currentBass) const;
75 float calculateDropImpact(float energyFlux, float bassFlux, float spectralNovelty, float rms) const;
76 bool shouldTriggerDrop(float impact, u32 timestamp) const;
77
78 // Baseline update method for adaptive detection
79 void updateBaselines(float rms, float bass);
80};
81
82} // namespace detector
83} // namespace audio
84} // 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
float calculateEnergyFlux(float currentRMS) const
function_list< void(const Drop &)> onDropEvent
Definition drop.h:66
bool shouldTriggerDrop(float impact, u32 timestamp) const
function_list< void()> onDrop
Definition drop.h:65
float calculateBassFlux(float currentBass) const
void setImpactThreshold(float threshold)
Definition drop.h:76
void(*)(const Drop &) DropCallback
float getBassEnergy(const fft::Bins &fft) const
void setEnergyFluxThreshold(float threshold)
void setBassThreshold(float threshold)
float getCurrentImpact(shared_ptr< Context > context) const
float calculateDropImpact(float energyFlux, float bassFlux, float spectralNovelty, float rms) const
const Drop & getLastDrop() const
function_list< void(float impact)> onDropImpact
Definition drop.h:67
float getMidEnergy(const fft::Bins &fft) const
void updateBaselines(float rms, float bass)
float calculateSpectralNovelty(float bass, float mid, float treble) const
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
Drop() FL_NOEXCEPT=default