FastLED 3.9.15
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_NOEXCEPT override;
17
18 void update(shared_ptr<Context> context) override;
19 void fireCallbacks() override;
20 bool needsFFT() const override { return true; }
21 bool needsFFTHistory() const override { return true; }
22 const char* getName() const override { return "Beat"; }
23 void reset() 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 { return mBeatDetected; }
33 float getBPM() const { return mBPM; }
34 float getPhase() const { return mPhase; }
35 float getConfidence() const { return mConfidence; }
36
37 // Configuration
38 void setThreshold(float threshold) { 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);
70 void updateTempo(u32 timestamp);
71 void updatePhase(u32 timestamp);
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
float getPhase() const
Definition beat.h:34
function_list< void(float bpm, float confidence)> onTempoChange
Definition beat.h:29
function_list< void()> onBeat
Definition beat.h:26
bool detectBeat(u32 timestamp)
Definition beat.cpp.hpp:110
void setSensitivity(float sensitivity)
Definition beat.h:39
bool needsFFT() const override
Definition beat.h:20
float calculateSpectralFlux(const fft::Bins &fft)
Definition beat.cpp.hpp:80
static constexpr size FLUX_HISTORY_SIZE
Definition beat.h:63
float getBPM() const
Definition beat.h:33
function_list< void(float phase)> onBeatPhase
Definition beat.h:27
bool needsFFTHistory() const override
Definition beat.h:21
~Beat() FL_NOEXCEPT override
vector< float > mPreviousMagnitudes
Definition beat.h:51
shared_ptr< const fft::Bins > mRetainedFFT
Definition beat.h:65
void update(shared_ptr< Context > context) override
Definition beat.cpp.hpp:28
float getConfidence() const
Definition beat.h:35
function_list< void(float strength)> onOnset
Definition beat.h:28
const char * getName() const override
Definition beat.h:22
void updateTempo(u32 timestamp)
Definition beat.cpp.hpp:138
void setThreshold(float threshold)
Definition beat.h:38
static constexpr u32 MAX_BEAT_INTERVAL_MS
Definition beat.h:58
bool isBeat() const
Definition beat.h:32
void fireCallbacks() override
Definition beat.cpp.hpp:177
void updatePhase(u32 timestamp)
Definition beat.cpp.hpp:161
void reset() override
Definition beat.cpp.hpp:67
static constexpr u32 MIN_BEAT_INTERVAL_MS
Definition beat.h:57
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT