FastLED 3.9.15
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);
39
44
46
47 void update(shared_ptr<Context> context) override;
48 void fireCallbacks() override;
49 bool needsFFT() const override { return true; }
50 bool needsFFTHistory() const override { return false; }
51 const char* getName() const override { return "Downbeat"; }
52 void reset() 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 { return mDownbeatDetected; }
72
74 u8 getCurrentBeat() const { return mCurrentBeat; }
75
78
80 float getMeasurePhase() const { return mMeasurePhase; }
81
83 float getConfidence() const { return mConfidence; }
84
85 // ----- Configuration -----
86
88 void setConfidenceThreshold(float threshold) { mConfidenceThreshold = threshold; }
89
91 void setAccentThreshold(float threshold) { mAccentThreshold = threshold; }
92
94 void setAutoMeterDetection(bool enable) { mAutoMeterDetection = enable; }
95
97 void setTimeSignature(u8 beatsPerMeasure);
98
100 void setBeatDetector(shared_ptr<Beat> beatDetector);
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);
146 bool detectDownbeat(u32 timestamp, float accent);
147 void detectMeter();
148 void updateMeasurePhase(u32 timestamp);
149 u8 findMostCommonMeter() const;
150};
151
152} // namespace detector
153} // namespace audio
154} // namespace fl
void setBeatDetector(shared_ptr< Beat > beatDetector)
Share an external Beat instance.
bool needsFFTHistory() const override
Definition downbeat.h:50
void setAccentThreshold(float threshold)
Set accent detection threshold (default: 1.2)
Definition downbeat.h:91
const char * getName() const override
Definition downbeat.h:51
float calculateBeatAccent(const fft::Bins &fft, float bassEnergy)
bool isDownbeat() const
Returns true if downbeat was detected this frame.
Definition downbeat.h:71
~Downbeat() FL_NOEXCEPT override
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
u8 getBeatsPerMeasure() const
Returns detected beats per measure (time signature numerator)
Definition downbeat.h:77
Downbeat(shared_ptr< Beat > beatDetector)
Construct with shared Beat (recommended)
u8 getCurrentBeat() const
Returns current beat number within measure (1-based, 1 = downbeat)
Definition downbeat.h:74
Downbeat() FL_NOEXCEPT
Construct with standalone Beat.
void setTimeSignature(u8 beatsPerMeasure)
Manually set time signature (disables auto-detection)
function_list< void()> onDownbeat
Fires on detected downbeat (first beat of measure)
Definition downbeat.h:57
float getConfidence() const
Returns downbeat detection confidence (0-1)
Definition downbeat.h:83
shared_ptr< Beat > mBeatDetector
Definition downbeat.h:104
void update(shared_ptr< Context > context) override
void updateMeasurePhase(u32 timestamp)
void updateBeatDetector(shared_ptr< Context > context)
void setAutoMeterDetection(bool enable)
Enable/disable automatic meter detection (default: true)
Definition downbeat.h:94
function_list< void(u8 beatsPerMeasure)> onMeterChange
Fires when time signature changes.
Definition downbeat.h:63
static constexpr size METER_HISTORY_SIZE
Definition downbeat.h:132
static constexpr size MAX_BEAT_HISTORY
Definition downbeat.h:128
bool needsFFT() const override
Definition downbeat.h:49
function_list< void(u8 beatNumber)> onMeasureBeat
Fires on each beat with beat number (1-based, downbeat = 1)
Definition downbeat.h:60
void setConfidenceThreshold(float threshold)
Set minimum confidence for downbeat detection (default: 0.6)
Definition downbeat.h:88
float getMeasurePhase() const
Returns measure phase (0-1, 0 = downbeat)
Definition downbeat.h:80
shared_ptr< const fft::Bins > mRetainedFFT
Definition downbeat.h:141
bool detectDownbeat(u32 timestamp, float accent)
unsigned char u8
Definition stdint.h:131
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT