FastLED 3.10.8
Loading...
Searching...
No Matches
beat.h
Go to the documentation of this file.
1#pragma once
2
5#include "fl/stl/function.h"
6#include "fl/stl/vector.h"
7#include "fl/stl/noexcept.h"
8
9namespace fl {
10namespace audio {
11namespace detector {
12
13class Beat : public Detector {
14public:
16 ~Beat() FL_NO_EXCEPT override;
17
18 void update(shared_ptr<Context> context) FL_NO_EXCEPT override;
19 void fireCallbacks() FL_NO_EXCEPT override;
20 bool needsFFT() const FL_NO_EXCEPT override { return true; }
21 bool needsFFTHistory() const FL_NO_EXCEPT override { return true; }
22 const char* getName() const FL_NO_EXCEPT override { return "Beat"; }
23 void reset() FL_NO_EXCEPT override;
24
25 // Callbacks (multiple listeners supported)
26 function_list<void()> onBeat;
27 function_list<void(float phase)> onBeatPhase;
28 function_list<void(float strength)> onOnset;
29 function_list<void(float bpm, float confidence)> onTempoChange;
30
31 // State access
32 bool isBeat() const FL_NO_EXCEPT { return mBeatDetected; }
33 float getBPM() const FL_NO_EXCEPT { return mBPM; }
34 float getPhase() const FL_NO_EXCEPT { return mPhase; }
35 float getConfidence() const FL_NO_EXCEPT { return mConfidence; }
36
37 // Configuration
38 void setThreshold(float threshold) FL_NO_EXCEPT { mThreshold = threshold; }
40
41private:
43 bool mTempoChanged = false;
44 float mBPM;
45 float mPhase;
49
50 // Spectral flux tracking
53
54 // Temporal tracking
57 static constexpr u32 MIN_BEAT_INTERVAL_MS = 250; // Max 240 BPM
58 static constexpr u32 MAX_BEAT_INTERVAL_MS = 2000; // Min 30 BPM
59
60 // Adaptive threshold (O(1) running average)
63 static constexpr size FLUX_HISTORY_SIZE = 43; // ~1 second at 43fps
64
66
69 bool detectBeat(u32 timestamp) FL_NO_EXCEPT;
70 void updateTempo(u32 timestamp) FL_NO_EXCEPT;
71 void updatePhase(u32 timestamp) FL_NO_EXCEPT;
72};
73
74} // namespace detector
75} // namespace audio
76} // namespace fl
void bpm()
fl::UISlider sensitivity("Sensitivity", 1.5f, 0.3f, 4.0f, 0.1f)
MovingAverage< float, 43 > mFluxAvg
Definition beat.h:62
function_list< void(float bpm, float confidence)> onTempoChange
Definition beat.h:29
float getConfidence() const FL_NO_EXCEPT
Definition beat.h:35
float getPhase() const FL_NO_EXCEPT
Definition beat.h:34
function_list< void()> onBeat
Definition beat.h:26
void updateAdaptiveThreshold() FL_NO_EXCEPT
Definition beat.cpp.hpp:104
bool needsFFTHistory() const FL_NO_EXCEPT override
Definition beat.h:21
const char * getName() const FL_NO_EXCEPT override
Definition beat.h:22
float calculateSpectralFlux(const fft::Bins &fft) FL_NO_EXCEPT
Definition beat.cpp.hpp:80
void setSensitivity(float sensitivity) FL_NO_EXCEPT
Definition beat.h:39
bool isBeat() const FL_NO_EXCEPT
Definition beat.h:32
void reset() FL_NO_EXCEPT override
Definition beat.cpp.hpp:67
static constexpr size FLUX_HISTORY_SIZE
Definition beat.h:63
function_list< void(float phase)> onBeatPhase
Definition beat.h:27
vector< float > mPreviousMagnitudes
Definition beat.h:51
shared_ptr< const fft::Bins > mRetainedFFT
Definition beat.h:65
function_list< void(float strength)> onOnset
Definition beat.h:28
void updateTempo(u32 timestamp) FL_NO_EXCEPT
Definition beat.cpp.hpp:138
void updatePhase(u32 timestamp) FL_NO_EXCEPT
Definition beat.cpp.hpp:161
static constexpr u32 MAX_BEAT_INTERVAL_MS
Definition beat.h:58
float getBPM() const FL_NO_EXCEPT
Definition beat.h:33
void update(shared_ptr< Context > context) FL_NO_EXCEPT override
Definition beat.cpp.hpp:28
bool detectBeat(u32 timestamp) FL_NO_EXCEPT
Definition beat.cpp.hpp:110
bool needsFFT() const FL_NO_EXCEPT override
Definition beat.h:20
void setThreshold(float threshold) FL_NO_EXCEPT
Definition beat.h:38
void fireCallbacks() FL_NO_EXCEPT override
Definition beat.cpp.hpp:177
static constexpr u32 MIN_BEAT_INTERVAL_MS
Definition beat.h:57
~Beat() FL_NO_EXCEPT override
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
Base definition for an LED controller.
Definition crgb.hpp:179