FastLED 3.9.15
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 : rootNote(root), type(t), confidence(conf), timestamp(ts) {}
40
41 bool isValid() const { return rootNote >= 0 && rootNote < 12; }
42
43 // Declared here, defined in chord.cpp
44 const char* getRootName() const;
45 const char* getTypeName() const;
46};
47
48class ChordDetector : public Detector {
49public:
52
53 void update(shared_ptr<Context> context) override;
54 void fireCallbacks() override;
55 bool needsFFT() const override { return true; }
56 bool needsFFTHistory() const override { return true; }
57 const char* getName() const override { return "ChordDetector"; }
58 void reset() 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
66 const Chord& getCurrentChord() const { return mCurrentChord; }
67 bool hasChord() const { return mCurrentChord.isValid(); }
68
69 // Configuration
70 void setConfidenceThreshold(float threshold) { mConfidenceThreshold = threshold; }
71 void setMinDuration(u32 ms) { mMinChordDuration = ms; }
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(); // Pre-compute template lookups
100 void calculateChroma(const fft::Bins& fft);
101 Chord detectChord(const float* chroma, u32 timestamp);
102 float matchChordPattern(const float* chroma, int root, ChordType type);
103 bool isSimilarChord(const Chord& a, const Chord& b);
104
105 // Helper methods
106 void normalizeChroma(float* chroma);
107 float chromaDistance(const float* a, const float* b);
108};
109
110} // namespace detector
111} // namespace audio
112} // namespace fl
function_list< void()> onChordEnd
Definition chord.h:63
bool isSimilarChord(const Chord &a, const Chord &b)
flat_map< int, const ChordTemplate * > mTemplateMap
Definition chord.h:94
void normalizeChroma(float *chroma)
function_list< void(const Chord &chord)> onChord
Definition chord.h:61
float chromaDistance(const float *a, const float *b)
void setConfidenceThreshold(float threshold)
Definition chord.h:70
function_list< void(const Chord &chord)> onChordChange
Definition chord.h:62
void calculateChroma(const fft::Bins &fft)
void update(shared_ptr< Context > context) override
Definition chord.cpp.hpp:61
float matchChordPattern(const float *chroma, int root, ChordType type)
bool needsFFT() const override
Definition chord.h:55
shared_ptr< const fft::Bins > mRetainedFFT
Definition chord.h:96
const char * getName() const override
Definition chord.h:57
~ChordDetector() FL_NOEXCEPT override
Chord detectChord(const float *chroma, u32 timestamp)
bool needsFFTHistory() const override
Definition chord.h:56
const Chord & getCurrentChord() const
Definition chord.h:66
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
const char * getTypeName() const
Chord() FL_NOEXCEPT
Definition chord.h:37
bool isValid() const
Definition chord.h:41
Chord(int root, ChordType t, float conf, u32 ts)
Definition chord.h:38
const char * getRootName() const