FastLED 3.9.15
Loading...
Searching...
No Matches
backbeat.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
18 float bass; // Bass band accent strength (0-1)
19 float mid; // Mid band accent strength (0-1) - critical for snare
20 float high; // High band accent strength (0-1)
21 float total; // Weighted combination of all bands
22};
23
45class Backbeat : public Detector {
46public:
50 explicit Backbeat(shared_ptr<Beat> beatDetector);
51
55 explicit Backbeat(shared_ptr<Beat> beatDetector,
56 shared_ptr<Downbeat> downbeatDetector);
57
62
64
65 void update(shared_ptr<Context> context) override;
66 void fireCallbacks() override;
67 bool needsFFT() const override { return true; }
68 bool needsFFTHistory() const override { return false; }
69 const char* getName() const override { return "Backbeat"; }
70 void reset() override;
71
72 // ----- Callbacks (multiple listeners supported) -----
73
75 function_list<void(u8 beatNumber, float confidence, float strength)> onBackbeat;
76
77 // ----- State Access -----
78
80 bool isBackbeat() const { return mBackbeatDetected; }
81
84
86 float getConfidence() const { return mConfidence; }
87
89 float getStrength() const { return mCurrentStrength; }
90
92 float getBackbeatRatio() const { return mBackbeatRatio; }
93
94 // ----- Configuration -----
95
97 void setConfidenceThreshold(float threshold) { mConfidenceThreshold = threshold; }
98
100 void setBassThreshold(float threshold) { mBassThreshold = threshold; }
101
103 void setMidThreshold(float threshold) { mMidThreshold = threshold; }
104
106 void setHighThreshold(float threshold) { mHighThreshold = threshold; }
107
109 void setBackbeatExpectedBeats(u8 beatMask) { mBackbeatMask = beatMask; }
110
112 void setAdaptive(bool enable) { mAdaptive = enable; }
113
115 void setBeatDetector(shared_ptr<Beat> beatDetector);
116
118 void setDownbeatDetector(shared_ptr<Downbeat> downbeatDetector);
119
120private:
121 // ----- Detector Dependencies -----
126
127 // ----- State -----
133
134 // ----- Configuration -----
139 u8 mBackbeatMask; // Bitmask: which beats are backbeats
141
142 // ----- Beat Tracking -----
146
147 // ----- Accent History -----
149 deque<float> mBackbeatAccents; // Accent strengths on backbeats
150 deque<float> mNonBackbeatAccents; // Accent strengths on non-backbeats
151 static constexpr size MAX_ACCENT_HISTORY = 16;
152
153 // ----- Adaptive Thresholds -----
157
158 // ----- Spectral Profile Learning -----
159 vector<float> mBackbeatSpectralProfile; // Average spectrum of backbeats
160 static constexpr size SPECTRAL_PROFILE_SIZE = 16;
161 float mProfileAlpha; // EMA smoothing factor for profile updates
162
164
165 // ----- Helper Methods -----
167 void updateBeatPosition();
169 float detectBackbeatAccent(const MultibandAccent& accent);
170 bool isBackbeatPosition() const;
171 bool detectBackbeat(float accentStrength, const fft::Bins& fft);
175};
176
177} // namespace detector
178} // namespace audio
179} // namespace fl
void setDownbeatDetector(shared_ptr< Downbeat > downbeatDetector)
Share an external Downbeat instance.
bool needsFFT() const override
Definition backbeat.h:67
shared_ptr< Beat > mBeatDetector
Definition backbeat.h:122
void setBackbeatExpectedBeats(u8 beatMask)
Set which beats are backbeats using bitmask (bit 0=beat 1, bit 1=beat 2, etc.)
Definition backbeat.h:109
void update(shared_ptr< Context > context) override
MultibandAccent calculateMultibandAccent(const fft::Bins &fft)
float detectBackbeatAccent(const MultibandAccent &accent)
void setAdaptive(bool enable)
Enable/disable adaptive threshold learning (default: true)
Definition backbeat.h:112
void setMidThreshold(float threshold)
Set mid accent threshold (default: 1.3) - critical for snare.
Definition backbeat.h:103
u8 getLastBackbeatNumber() const
Returns the beat number of the last detected backbeat (1-based)
Definition backbeat.h:83
deque< float > mBackbeatAccents
Definition backbeat.h:149
void setHighThreshold(float threshold)
Set high accent threshold (default: 1.1)
Definition backbeat.h:106
MultibandAccent mPreviousAccent
Definition backbeat.h:148
bool isBackbeat() const
Returns true if backbeat was detected this frame.
Definition backbeat.h:80
void setBeatDetector(shared_ptr< Beat > beatDetector)
Share an external Beat instance.
float getConfidence() const
Returns backbeat detection confidence (0-1)
Definition backbeat.h:86
float getBackbeatRatio() const
Returns ratio of backbeat to downbeat energy (0-2+)
Definition backbeat.h:92
static constexpr size MAX_ACCENT_HISTORY
Definition backbeat.h:151
function_list< void(u8 beatNumber, float confidence, float strength)> onBackbeat
Fires on detected backbeat (beats 2, 4) with beat number, confidence, and strength.
Definition backbeat.h:75
bool detectBackbeat(float accentStrength, const fft::Bins &fft)
void updateBeatDetector(shared_ptr< Context > context)
float calculatePatternConfidence(const fft::Bins &fft)
vector< float > mBackbeatSpectralProfile
Definition backbeat.h:159
void setBassThreshold(float threshold)
Set bass accent threshold (default: 1.2)
Definition backbeat.h:100
float getStrength() const
Returns current backbeat accent strength (0-1+)
Definition backbeat.h:89
~Backbeat() FL_NOEXCEPT override
shared_ptr< Downbeat > mDownbeatDetector
Definition backbeat.h:123
const char * getName() const override
Definition backbeat.h:69
shared_ptr< const fft::Bins > mRetainedFFT
Definition backbeat.h:163
void updateBackbeatProfile(const fft::Bins &fft)
Backbeat() FL_NOEXCEPT
Construct with standalone Beat.
bool needsFFTHistory() const override
Definition backbeat.h:68
static constexpr size SPECTRAL_PROFILE_SIZE
Definition backbeat.h:160
Backbeat(shared_ptr< Beat > beatDetector)
Construct with shared Beat.
deque< float > mNonBackbeatAccents
Definition backbeat.h:150
void setConfidenceThreshold(float threshold)
Set minimum confidence for backbeat detection (default: 0.6)
Definition backbeat.h:97
Multi-band accent information for backbeat detection.
Definition backbeat.h:17
unsigned char u8
Definition stdint.h:131
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT