FastLED 3.10.8
Loading...
Searching...
No Matches
note.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/noexcept.h"
7
8namespace fl {
9namespace audio {
10namespace detector {
11
32class Note : public Detector {
33public:
35 explicit Note(shared_ptr<Pitch> pitchDetector) FL_NO_EXCEPT;
36 ~Note() FL_NO_EXCEPT override;
37
38 void update(shared_ptr<Context> context) FL_NO_EXCEPT override;
39 void fireCallbacks() FL_NO_EXCEPT override;
40 bool needsFFT() const FL_NO_EXCEPT override { return false; } // Uses pitch detection
41 const char* getName() const FL_NO_EXCEPT override { return "Note"; }
42 void reset() FL_NO_EXCEPT override;
43
44 // Callbacks (multiple listeners supported)
45 function_list<void(u8 note, u8 velocity)> onNoteOn; // Note started
46 function_list<void(u8 note)> onNoteOff; // Note ended
47 function_list<void(u8 note, u8 velocity)> onNoteChange; // Note changed while held
48
49 // State access
52 bool isNoteActive() const FL_NO_EXCEPT { return mNoteActive; }
53 float getCurrentPitch() const FL_NO_EXCEPT { return mCurrentPitch; }
54 float getPitchBend() const FL_NO_EXCEPT { return mPitchBend; } // Cents from note center
55
56 // Configuration
57 void setNoteOnThreshold(float confidenceThreshold) FL_NO_EXCEPT { mNoteOnThreshold = confidenceThreshold; }
58 void setNoteOffThreshold(float confidenceThreshold) FL_NO_EXCEPT { mNoteOffThreshold = confidenceThreshold; }
62
63 // Shared pitch detector access
64 void setPitchDetector(shared_ptr<Pitch> pitchDetector) FL_NO_EXCEPT { mPitchDetector = pitchDetector; }
66
67private:
68 // Shared pitch detector (may be shared with Processor)
70 bool mOwnsPitchDetector; // True if we created our own instance
71
72 // Current state
73 u8 mCurrentNote; // Current MIDI note (0-127, 255 = none)
74 u8 mLastVelocity; // Velocity of last note-on event (0-127)
75 bool mNoteActive; // True if note is currently held
76 float mCurrentPitch; // Current pitch in Hz
77 float mPitchBend; // Cents deviation from note center (-50 to +50)
78 float mNoteOnEnergy; // Energy level at note onset
79
80 // Timing
81 u32 mNoteOnTime; // Timestamp of note-on event
82 u32 mLastUpdateTime; // Timestamp of last update
83
84 // Configuration
85 float mNoteOnThreshold; // Confidence threshold for note-on (default: 0.6)
86 float mNoteOffThreshold; // Confidence threshold for note-off (default: 0.4)
87 u32 mMinNoteDuration;// Minimum note duration to prevent flicker (ms)
88 u8 mNoteChangeThreshold; // Semitone threshold for note change event
89 float mVelocitySensitivity; // Velocity calculation sensitivity
90
91 bool mFireNoteOn = false;
92 bool mFireNoteOff = false;
93 bool mFireNoteChange = false;
97
98 // Helper methods
99 u8 frequencyToMidiNote(float hz) const FL_NO_EXCEPT;
100 float midiNoteToFrequency(u8 note) const FL_NO_EXCEPT;
101 float calculatePitchBend(float hz, u8 note) const FL_NO_EXCEPT;
102 u8 calculateVelocity(float energy, float confidence) const FL_NO_EXCEPT;
103 bool shouldTriggerNoteOn(float confidence, float pitch) const FL_NO_EXCEPT;
104 bool shouldTriggerNoteOff(float confidence, bool voiced) const FL_NO_EXCEPT;
105 bool shouldTriggerNoteChange(u8 newNote, u8 currentNote) const FL_NO_EXCEPT;
106
107 // Constants
108 static constexpr float A4_FREQUENCY = 440.0f; // A4 reference pitch
109 static constexpr u8 A4_MIDI_NOTE = 69; // A4 MIDI note number
110 static constexpr u8 NO_NOTE = 255; // Sentinel for no active note
111};
112
113} // namespace detector
114} // namespace audio
115} // namespace fl
fl::UISlider sensitivity("Sensitivity", 1.5f, 0.3f, 4.0f, 0.1f)
function_list< void(u8 note, u8 velocity)> onNoteOn
Definition note.h:45
void setNoteOnThreshold(float confidenceThreshold) FL_NO_EXCEPT
Definition note.h:57
float getPitchBend() const FL_NO_EXCEPT
Definition note.h:54
shared_ptr< Pitch > getPitchDetector() const FL_NO_EXCEPT
Definition note.h:65
~Note() FL_NO_EXCEPT override
void setVelocitySensitivity(float sensitivity) FL_NO_EXCEPT
Definition note.h:61
bool shouldTriggerNoteChange(u8 newNote, u8 currentNote) const FL_NO_EXCEPT
Definition note.cpp.hpp:237
static constexpr u8 A4_MIDI_NOTE
Definition note.h:109
void fireCallbacks() FL_NO_EXCEPT override
Definition note.cpp.hpp:128
bool needsFFT() const FL_NO_EXCEPT override
Definition note.h:40
function_list< void(u8 note, u8 velocity)> onNoteChange
Definition note.h:47
void setNoteChangeThreshold(u8 semitones) FL_NO_EXCEPT
Definition note.h:60
void setMinNoteDuration(u32 ms) FL_NO_EXCEPT
Definition note.h:59
float calculatePitchBend(float hz, u8 note) const FL_NO_EXCEPT
Definition note.cpp.hpp:188
bool isNoteActive() const FL_NO_EXCEPT
Definition note.h:52
void setNoteOffThreshold(float confidenceThreshold) FL_NO_EXCEPT
Definition note.h:58
function_list< void(u8 note)> onNoteOff
Definition note.h:46
float midiNoteToFrequency(u8 note) const FL_NO_EXCEPT
Definition note.cpp.hpp:178
void reset() FL_NO_EXCEPT override
Definition note.cpp.hpp:143
u8 frequencyToMidiNote(float hz) const FL_NO_EXCEPT
Definition note.cpp.hpp:158
void update(shared_ptr< Context > context) FL_NO_EXCEPT override
Definition note.cpp.hpp:48
shared_ptr< Pitch > mPitchDetector
Definition note.h:69
void setPitchDetector(shared_ptr< Pitch > pitchDetector) FL_NO_EXCEPT
Definition note.h:64
u8 getLastVelocity() const FL_NO_EXCEPT
Definition note.h:51
bool shouldTriggerNoteOn(float confidence, float pitch) const FL_NO_EXCEPT
Definition note.cpp.hpp:223
u8 calculateVelocity(float energy, float confidence) const FL_NO_EXCEPT
Definition note.cpp.hpp:206
u8 getCurrentNote() const FL_NO_EXCEPT
Definition note.h:50
static constexpr float A4_FREQUENCY
Definition note.h:108
bool shouldTriggerNoteOff(float confidence, bool voiced) const FL_NO_EXCEPT
Definition note.cpp.hpp:230
const char * getName() const FL_NO_EXCEPT override
Definition note.h:41
static constexpr u8 NO_NOTE
Definition note.h:110
float getCurrentPitch() const FL_NO_EXCEPT
Definition note.h:53
unsigned char u8
Definition stdint.h:131
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
Base definition for an LED controller.
Definition crgb.hpp:179