FastLED 3.9.15
Loading...
Searching...
No Matches
percussion_detector.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stl/function.h"
4#include "fl/stl/noexcept.h"
5
6namespace fl {
7namespace audio {
8namespace detector {
9
10class Percussion {
11public:
14
15 void update(shared_ptr<Context> context);
16 void reset();
17
18 // Cooldown periods to prevent double-triggering (in milliseconds)
19 static constexpr u32 KICK_COOLDOWN_MS = 50;
20 static constexpr u32 SNARE_COOLDOWN_MS = 50;
21 static constexpr u32 HIHAT_COOLDOWN_MS = 30;
22
23 // Percussion detection callbacks (multiple listeners supported)
24 function_list<void()> onKick;
25 function_list<void()> onSnare;
26 function_list<void()> onHiHat;
27 function_list<void()> onTom;
28 function_list<void(const char* type)> onPercussionHit;
29
30 // Configuration methods
31 void setKickThreshold(float threshold) { mKickThreshold = threshold; }
32 void setSnareThreshold(float threshold) { mSnareThreshold = threshold; }
33 void setHiHatThreshold(float threshold) { mHiHatThreshold = threshold; }
34
35private:
36 // Detection thresholds
37 float mKickThreshold;
38 float mSnareThreshold;
39 float mHiHatThreshold;
40
41 // Previous energy tracking
45
46 // Timestamp tracking to prevent double-triggering
47 u32 mLastKickTime;
50
51 // Energy band calculations
53 float getMidEnergy(const fft::Bins& fft);
55
56 // Percussion type detection
57 bool detectKick(float bassEnergy, float bassFlux, u32 timestamp);
58 bool detectSnare(float midEnergy, float midFlux, u32 timestamp);
59 bool detectHiHat(float trebleEnergy, float trebleFlux, u32 timestamp);
60};
61
62} // namespace detector
63} // namespace audio
64} // namespace fl
static constexpr u32 SNARE_COOLDOWN_MS
Definition percussion.h:108
function_list< void(PercussionType type)> onPercussionHit
Definition percussion.h:33
function_list< void()> onKick
Definition percussion.h:34
bool detectSnare(float midEnergy, float midFlux, u32 timestamp)
static constexpr u32 KICK_COOLDOWN_MS
Definition percussion.h:107
void setSnareThreshold(float threshold)
function_list< void()> onSnare
Definition percussion.h:35
static constexpr u32 HIHAT_COOLDOWN_MS
Definition percussion.h:109
bool detectKick(float bassEnergy, float bassFlux, u32 timestamp)
float getMidEnergy(const fft::Bins &fft)
float getTrebleEnergy(const fft::Bins &fft)
float getBassEnergy(const fft::Bins &fft)
function_list< void()> onTom
Definition percussion.h:37
void setKickThreshold(float threshold)
Definition percussion.h:62
void setHiHatThreshold(float threshold)
bool detectHiHat(float trebleEnergy, float trebleFlux, u32 timestamp)
void update(shared_ptr< Context > context) override
function_list< void()> onHiHat
Definition percussion.h:36
#define constexpr
Declares that it is possible to evaluate a value at compile time, introduced in C++11.
Definition cpp_compat.h:15
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT