FastLED 3.10.8
Loading...
Searching...
No Matches
chord.h
Go to the documentation of this file.
1#pragma once
2
4#include "fl/stl/function.h"
5#include "fl/stl/shared_ptr.h"
6#include "fl/stl/flat_map.h"
7#include "fl/stl/noexcept.h"
8
9namespace fl {
10namespace audio {
11namespace detector {
12
13// Chord types supported by the detector
26
27// Forward declaration of chord template structure (defined in .cpp.hpp)
28struct ChordTemplate;
29
30// Detected chord information
31struct Chord {
32 int rootNote; // MIDI note number (0-11 for C-B)
33 ChordType type; // Chord type
34 float confidence; // Detection confidence (0.0-1.0)
35 u32 timestamp; // When detected
36
38 Chord(int root, ChordType t, float conf, u32 ts)
39 FL_NO_EXCEPT : rootNote(root), type(t), confidence(conf), timestamp(ts) {}
40
41 bool isValid() const FL_NO_EXCEPT { return rootNote >= 0 && rootNote < 12; }
42
43 // Declared here, defined in chord.cpp
44 const char* getRootName() const FL_NO_EXCEPT;
45 const char* getTypeName() const FL_NO_EXCEPT;
46};
47
48class ChordDetector : public Detector {
49public:
52
53 void update(shared_ptr<Context> context) FL_NO_EXCEPT override;
54 void fireCallbacks() FL_NO_EXCEPT override;
55 bool needsFFT() const FL_NO_EXCEPT override { return true; }
56 bool needsFFTHistory() const FL_NO_EXCEPT override { return true; }
57 const char* getName() const FL_NO_EXCEPT override { return "ChordDetector"; }
58 void reset() FL_NO_EXCEPT override;
59
60 // Callbacks (multiple listeners supported)
61 function_list<void(const Chord& chord)> onChord;
62 function_list<void(const Chord& chord)> onChordChange;
63 function_list<void()> onChordEnd;
64
65 // State access
67 bool hasChord() const FL_NO_EXCEPT { return mCurrentChord.isValid(); }
68
69 // Configuration
70 void setConfidenceThreshold(float threshold) FL_NO_EXCEPT { mConfidenceThreshold = threshold; }
72
73private:
74 // Current state
79
80 // Configuration
83
84 // Chroma feature (pitch class profile)
85 float mChroma[12]; // Energy for each pitch class (C, C#, D, ...)
86 float mPrevChroma[12];
87
88 bool mFireChordChange = false;
89 bool mFireChordEnd = false;
90 bool mFireChord = false;
91
92 // Template lookup map (O(1) access, pre-computed at init)
93 // Maps ChordType (as int) to ChordTemplate pointer for fast template lookups
95
97
98 // Detection methods
99 void initializeTemplateMap() FL_NO_EXCEPT; // Pre-compute template lookups
100 void calculateChroma(const fft::Bins& fft) FL_NO_EXCEPT;
101 Chord detectChord(const float* chroma, u32 timestamp) FL_NO_EXCEPT;
102 float matchChordPattern(const float* chroma, int root, ChordType type) FL_NO_EXCEPT;
103 bool isSimilarChord(const Chord& a, const Chord& b) FL_NO_EXCEPT;
104
105 // Helper methods
106 void normalizeChroma(float* chroma) FL_NO_EXCEPT;
107 float chromaDistance(const float* a, const float* b) FL_NO_EXCEPT;
108};
109
110} // namespace detector
111} // namespace audio
112} // namespace fl
function_list< void()> onChordEnd
Definition chord.h:63
const char * getName() const FL_NO_EXCEPT override
Definition chord.h:57
bool needsFFTHistory() const FL_NO_EXCEPT override
Definition chord.h:56
bool isSimilarChord(const Chord &a, const Chord &b) FL_NO_EXCEPT
float matchChordPattern(const float *chroma, int root, ChordType type) FL_NO_EXCEPT
flat_map< int, const ChordTemplate * > mTemplateMap
Definition chord.h:94
function_list< void(const Chord &chord)> onChord
Definition chord.h:61
Chord detectChord(const float *chroma, u32 timestamp) FL_NO_EXCEPT
function_list< void(const Chord &chord)> onChordChange
Definition chord.h:62
float chromaDistance(const float *a, const float *b) FL_NO_EXCEPT
void update(shared_ptr< Context > context) FL_NO_EXCEPT override
Definition chord.cpp.hpp:61
void setMinDuration(u32 ms) FL_NO_EXCEPT
Definition chord.h:71
void calculateChroma(const fft::Bins &fft) FL_NO_EXCEPT
void normalizeChroma(float *chroma) FL_NO_EXCEPT
~ChordDetector() FL_NO_EXCEPT override
bool hasChord() const FL_NO_EXCEPT
Definition chord.h:67
const Chord & getCurrentChord() const FL_NO_EXCEPT
Definition chord.h:66
shared_ptr< const fft::Bins > mRetainedFFT
Definition chord.h:96
void setConfidenceThreshold(float threshold) FL_NO_EXCEPT
Definition chord.h:70
void initializeTemplateMap() FL_NO_EXCEPT
Definition chord.cpp.hpp:53
void fireCallbacks() FL_NO_EXCEPT override
bool needsFFT() const FL_NO_EXCEPT override
Definition chord.h:55
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
Base definition for an LED controller.
Definition crgb.hpp:179
Chord(int root, ChordType t, float conf, u32 ts) FL_NO_EXCEPT
Definition chord.h:38
bool isValid() const FL_NO_EXCEPT
Definition chord.h:41
const char * getTypeName() const FL_NO_EXCEPT
const char * getRootName() const FL_NO_EXCEPT
Chord() FL_NO_EXCEPT
Definition chord.h:37