FastLED 3.9.15
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:
73 explicit MusicalBeat(const MusicalBeatDetectorConfig& config);
75
77 void configure(const MusicalBeatDetectorConfig& config);
78
82 void processSample(bool onsetDetected, float onsetStrength);
83
86 bool isBeat() const;
87
90 float getBPM() const;
91
94 float getBeatConfidence() const;
95
98 float getAverageIBI() const;
99
101 void reset();
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 { return mStats; }
114
115private:
119 bool validateBeat(float onsetStrength);
120
124 float calculateBeatConfidence(float currentIBI);
125
127 void updateBPMEstimate();
128
132 bool isValidIBI(float ibi) const;
133
136 float calculateIBIStdDev() const;
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)
void processSample(bool onsetDetected, float onsetStrength)
Process one audio frame.
bool isValidIBI(float ibi) const
Check if IBI is within valid BPM range.
float calculateBeatConfidence(float currentIBI)
Calculate beat confidence based on rhythmic consistency.
float getBeatConfidence() const
Get beat confidence for the last detected beat.
bool mBeatDetected
Last beat was detected.
float mCurrentBPM
Current BPM estimate (smoothed)
bool isBeat() const
Check if a musical beat was detected in the last frame.
float mLastBeatConfidence
Last beat confidence score.
void reset()
Reset internal state (clear history, reset BPM)
u32 mCurrentFrame
Current frame counter.
void configure(const MusicalBeatDetectorConfig &config)
Configure the beat detector.
float getAverageIBI() const
Get inter-beat interval (IBI) statistics.
u32 mLastBeatFrame
Last beat timestamp (in frames)
bool validateBeat(float onsetStrength)
Validate if an onset is a true musical beat.
float getBPM() const
Get current BPM estimate.
void updateBPMEstimate()
Update BPM estimate from inter-beat intervals.
float calculateIBIStdDev() const
Calculate standard deviation of IBI history.
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.
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT