FastLED 3.10.8
Loading...
Searching...
No Matches
downbeat.h
Go to the documentation of this file.
1#pragma once
2
4#include "fl/stl/function.h"
5#include "fl/stl/vector.h"
6#include "fl/stl/deque.h"
7#include "fl/stl/shared_ptr.h"
8#include "fl/stl/noexcept.h"
9
10namespace fl {
11namespace audio {
12namespace detector {
13
33class Downbeat : public Detector {
34public:
38 explicit Downbeat(shared_ptr<Beat> beatDetector) FL_NO_EXCEPT;
39
44
46
47 void update(shared_ptr<Context> context) FL_NO_EXCEPT override;
48 void fireCallbacks() FL_NO_EXCEPT override;
49 bool needsFFT() const FL_NO_EXCEPT override { return true; }
50 bool needsFFTHistory() const FL_NO_EXCEPT override { return false; }
51 const char* getName() const FL_NO_EXCEPT override { return "Downbeat"; }
52 void reset() FL_NO_EXCEPT override;
53
54 // ----- Callbacks (multiple listeners supported) -----
55
57 function_list<void()> onDownbeat;
58
60 function_list<void(u8 beatNumber)> onMeasureBeat;
61
63 function_list<void(u8 beatsPerMeasure)> onMeterChange;
64
66 function_list<void(float phase)> onMeasurePhase;
67
68 // ----- State Access -----
69
71 bool isDownbeat() const FL_NO_EXCEPT { return mDownbeatDetected; }
72
75
78
80 float getMeasurePhase() const FL_NO_EXCEPT { return mMeasurePhase; }
81
83 float getConfidence() const FL_NO_EXCEPT { return mConfidence; }
84
85 // ----- Configuration -----
86
88 void setConfidenceThreshold(float threshold) FL_NO_EXCEPT { mConfidenceThreshold = threshold; }
89
91 void setAccentThreshold(float threshold) FL_NO_EXCEPT { mAccentThreshold = threshold; }
92
95
97 void setTimeSignature(u8 beatsPerMeasure) FL_NO_EXCEPT;
98
101
102private:
103 // ----- Beat Management -----
105 bool mOwnsBeatDetector; // True if we created our own Beat
106
107 // ----- State -----
109 u8 mCurrentBeat; // 1-based beat number (1 = downbeat)
110 u8 mBeatsPerMeasure; // Detected time signature
111 float mMeasurePhase; // 0-1 within measure
113
114 // ----- Configuration -----
119
120 // ----- Beat Tracking -----
124
125 // ----- Accent Detection -----
127 deque<float> mBeatAccents; // Recent beat accent strengths
128 static constexpr size MAX_BEAT_HISTORY = 32;
129
130 // ----- Meter Detection -----
131 deque<u8> mMeterCandidates; // Recent detected meters
132 static constexpr size METER_HISTORY_SIZE = 8;
133
134 // ----- Pending Callback Flags -----
135 bool mFireDownbeat = false;
136 bool mFireMeasureBeat = false;
138 bool mFireMeterChange = false;
140
142
143 // ----- Helper Methods -----
145 float calculateBeatAccent(const fft::Bins& fft, float bassEnergy) FL_NO_EXCEPT;
146 bool detectDownbeat(u32 timestamp, float accent) FL_NO_EXCEPT;
148 void updateMeasurePhase(u32 timestamp) FL_NO_EXCEPT;
150};
151
152} // namespace detector
153} // namespace audio
154} // namespace fl
void detectMeter() FL_NO_EXCEPT
bool needsFFT() const FL_NO_EXCEPT override
Definition downbeat.h:49
void setTimeSignature(u8 beatsPerMeasure) FL_NO_EXCEPT
Manually set time signature (disables auto-detection)
float getMeasurePhase() const FL_NO_EXCEPT
Returns measure phase (0-1, 0 = downbeat)
Definition downbeat.h:80
void setAutoMeterDetection(bool enable) FL_NO_EXCEPT
Enable/disable automatic meter detection (default: true)
Definition downbeat.h:94
void updateBeatDetector(shared_ptr< Context > context) FL_NO_EXCEPT
const char * getName() const FL_NO_EXCEPT override
Definition downbeat.h:51
float getConfidence() const FL_NO_EXCEPT
Returns downbeat detection confidence (0-1)
Definition downbeat.h:83
u8 getBeatsPerMeasure() const FL_NO_EXCEPT
Returns detected beats per measure (time signature numerator)
Definition downbeat.h:77
float calculateBeatAccent(const fft::Bins &fft, float bassEnergy) FL_NO_EXCEPT
void fireCallbacks() FL_NO_EXCEPT override
void updateMeasurePhase(u32 timestamp) FL_NO_EXCEPT
deque< float > mBeatAccents
Definition downbeat.h:127
function_list< void(float phase)> onMeasurePhase
Fires with measure phase each frame (0-1 range)
Definition downbeat.h:66
Downbeat(shared_ptr< Beat > beatDetector) FL_NO_EXCEPT
Construct with shared Beat (recommended)
void reset() FL_NO_EXCEPT override
Downbeat() FL_NO_EXCEPT
Construct with standalone Beat.
function_list< void()> onDownbeat
Fires on detected downbeat (first beat of measure)
Definition downbeat.h:57
bool needsFFTHistory() const FL_NO_EXCEPT override
Definition downbeat.h:50
bool detectDownbeat(u32 timestamp, float accent) FL_NO_EXCEPT
u8 findMostCommonMeter() const FL_NO_EXCEPT
void update(shared_ptr< Context > context) FL_NO_EXCEPT override
void setConfidenceThreshold(float threshold) FL_NO_EXCEPT
Set minimum confidence for downbeat detection (default: 0.6)
Definition downbeat.h:88
shared_ptr< Beat > mBeatDetector
Definition downbeat.h:104
~Downbeat() FL_NO_EXCEPT override
bool isDownbeat() const FL_NO_EXCEPT
Returns true if downbeat was detected this frame.
Definition downbeat.h:71
void setAccentThreshold(float threshold) FL_NO_EXCEPT
Set accent detection threshold (default: 1.2)
Definition downbeat.h:91
u8 getCurrentBeat() const FL_NO_EXCEPT
Returns current beat number within measure (1-based, 1 = downbeat)
Definition downbeat.h:74
function_list< void(u8 beatsPerMeasure)> onMeterChange
Fires when time signature changes.
Definition downbeat.h:63
void setBeatDetector(shared_ptr< Beat > beatDetector) FL_NO_EXCEPT
Share an external Beat instance.
static constexpr size METER_HISTORY_SIZE
Definition downbeat.h:132
static constexpr size MAX_BEAT_HISTORY
Definition downbeat.h:128
function_list< void(u8 beatNumber)> onMeasureBeat
Fires on each beat with beat number (1-based, downbeat = 1)
Definition downbeat.h:60
shared_ptr< const fft::Bins > mRetainedFFT
Definition downbeat.h:141
unsigned char u8
Definition stdint.h:131
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
Base definition for an LED controller.
Definition crgb.hpp:179