FastLED 3.10.8
Loading...
Searching...
No Matches
musical_beat_detector.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stl/int.h"
4#include "fl/stl/vector.h"
5#include "fl/stl/deque.h"
6#include "fl/stl/noexcept.h"
7
8namespace fl {
9namespace audio {
10namespace detector {
11
15 float minBPM = 50.0f;
16
18 float maxBPM = 250.0f;
19
22 float minBeatConfidence = 0.5f;
23
26 float bpmSmoothingAlpha = 0.9f;
27
29 u32 sampleRate = 22050;
30
32 u32 samplesPerFrame = 512;
33
37};
38
71public:
75
78
82 void processSample(bool onsetDetected, float onsetStrength) FL_NO_EXCEPT;
83
86 bool isBeat() const FL_NO_EXCEPT;
87
90 float getBPM() const FL_NO_EXCEPT;
91
94 float getBeatConfidence() const FL_NO_EXCEPT;
95
98 float getAverageIBI() const FL_NO_EXCEPT;
99
101 void reset() FL_NO_EXCEPT;
102
104 struct Stats {
105 u32 totalOnsets = 0; // Total onsets detected (lifetime)
106 u32 validatedBeats = 0; // Onsets validated as beats (lifetime)
107 u32 rejectedOnsets = 0; // Onsets rejected (not rhythmic)
108 float currentBPM = 0.0f; // Current BPM estimate
109 float averageIBI = 0.0f; // Average inter-beat interval (seconds)
110 u32 ibiCount = 0; // Number of IBIs in history
111 };
112
113 const Stats& getStats() const FL_NO_EXCEPT { return mStats; }
114
115private:
119 bool validateBeat(float onsetStrength) FL_NO_EXCEPT;
120
124 float calculateBeatConfidence(float currentIBI) FL_NO_EXCEPT;
125
128
132 bool isValidIBI(float ibi) const FL_NO_EXCEPT;
133
136 float calculateIBIStdDev() const FL_NO_EXCEPT;
137
140
142 bool mBeatDetected = false;
143
146
148 float mCurrentBPM = 120.0f;
149
152
155
158
160 float mAverageIBI = 0.0f;
161};
162
163} // namespace detector
164} // namespace audio
165} // namespace fl
deque< u32 > mIBIHistory
Inter-beat interval history (in frames)
float mAverageIBI
Average inter-beat interval (in frames)
bool validateBeat(float onsetStrength) FL_NO_EXCEPT
Validate if an onset is a true musical beat.
const Stats & getStats() const FL_NO_EXCEPT
void configure(const MusicalBeatDetectorConfig &config) FL_NO_EXCEPT
Configure the beat detector.
float getBeatConfidence() const FL_NO_EXCEPT
Get beat confidence for the last detected beat.
bool mBeatDetected
Last beat was detected.
float mCurrentBPM
Current BPM estimate (smoothed)
void processSample(bool onsetDetected, float onsetStrength) FL_NO_EXCEPT
Process one audio frame.
void reset() FL_NO_EXCEPT
Reset internal state (clear history, reset BPM)
void updateBPMEstimate() FL_NO_EXCEPT
Update BPM estimate from inter-beat intervals.
float getBPM() const FL_NO_EXCEPT
Get current BPM estimate.
float mLastBeatConfidence
Last beat confidence score.
float calculateIBIStdDev() const FL_NO_EXCEPT
Calculate standard deviation of IBI history.
u32 mCurrentFrame
Current frame counter.
bool isBeat() const FL_NO_EXCEPT
Check if a musical beat was detected in the last frame.
u32 mLastBeatFrame
Last beat timestamp (in frames)
float getAverageIBI() const FL_NO_EXCEPT
Get inter-beat interval (IBI) statistics.
bool isValidIBI(float ibi) const FL_NO_EXCEPT
Check if IBI is within valid BPM range.
float calculateBeatConfidence(float currentIBI) FL_NO_EXCEPT
Calculate beat confidence based on rhythmic consistency.
Get statistics (for debugging/monitoring)
u32 samplesPerFrame
Samples per frame - used for timing calculations.
u32 sampleRate
Sample rate (Hz) - used for timing calculations.
float maxBPM
Maximum BPM to detect (default: 250 BPM)
float minBeatConfidence
Minimum beat confidence to report a beat (0.0-1.0) Higher values = fewer false positives,...
u32 maxIBIHistory
Maximum number of inter-beat intervals to track Higher values = better BPM estimation,...
float minBPM
Minimum BPM to detect (default: 50 BPM)
float bpmSmoothingAlpha
BPM estimation smoothing factor (0.0-1.0) Higher values = slower BPM adaptation, more stable tempo.
Configuration for musical beat detection.
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
Base definition for an LED controller.
Definition crgb.hpp:179