FastLED 3.9.15
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);
36 ~Note() FL_NOEXCEPT override;
37
38 void update(shared_ptr<Context> context) override;
39 void fireCallbacks() override;
40 bool needsFFT() const override { return false; } // Uses pitch detection
41 const char* getName() const override { return "Note"; }
42 void reset() 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
50 u8 getCurrentNote() const { return mCurrentNote; }
51 u8 getLastVelocity() const { return mLastVelocity; }
52 bool isNoteActive() const { return mNoteActive; }
53 float getCurrentPitch() const { return mCurrentPitch; }
54 float getPitchBend() const { return mPitchBend; } // Cents from note center
55
56 // Configuration
57 void setNoteOnThreshold(float confidenceThreshold) { mNoteOnThreshold = confidenceThreshold; }
58 void setNoteOffThreshold(float confidenceThreshold) { mNoteOffThreshold = confidenceThreshold; }
59 void setMinNoteDuration(u32 ms) { mMinNoteDuration = ms; }
60 void setNoteChangeThreshold(u8 semitones) { mNoteChangeThreshold = semitones; }
62
63 // Shared pitch detector access
64 void setPitchDetector(shared_ptr<Pitch> pitchDetector) { 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;
100 float midiNoteToFrequency(u8 note) const;
101 float calculatePitchBend(float hz, u8 note) const;
102 u8 calculateVelocity(float energy, float confidence) const;
103 bool shouldTriggerNoteOn(float confidence, float pitch) const;
104 bool shouldTriggerNoteOff(float confidence, bool voiced) const;
105 bool shouldTriggerNoteChange(u8 newNote, u8 currentNote) const;
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
bool needsFFT() const override
Definition note.h:40
void setNoteOnThreshold(float confidenceThreshold)
Definition note.h:57
void setNoteChangeThreshold(u8 semitones)
Definition note.h:60
void setVelocitySensitivity(float sensitivity)
Definition note.h:61
float calculatePitchBend(float hz, u8 note) const
Definition note.cpp.hpp:188
void reset() override
Definition note.cpp.hpp:143
const char * getName() const override
Definition note.h:41
void fireCallbacks() override
Definition note.cpp.hpp:128
bool isNoteActive() const
Definition note.h:52
~Note() FL_NOEXCEPT override
static constexpr u8 A4_MIDI_NOTE
Definition note.h:109
float midiNoteToFrequency(u8 note) const
Definition note.cpp.hpp:178
bool shouldTriggerNoteOn(float confidence, float pitch) const
Definition note.cpp.hpp:223
bool shouldTriggerNoteOff(float confidence, bool voiced) const
Definition note.cpp.hpp:230
function_list< void(u8 note, u8 velocity)> onNoteChange
Definition note.h:47
float getPitchBend() const
Definition note.h:54
u8 getCurrentNote() const
Definition note.h:50
void setPitchDetector(shared_ptr< Pitch > pitchDetector)
Definition note.h:64
void setNoteOffThreshold(float confidenceThreshold)
Definition note.h:58
function_list< void(u8 note)> onNoteOff
Definition note.h:46
void setMinNoteDuration(u32 ms)
Definition note.h:59
shared_ptr< Pitch > mPitchDetector
Definition note.h:69
u8 frequencyToMidiNote(float hz) const
Definition note.cpp.hpp:158
u8 calculateVelocity(float energy, float confidence) const
Definition note.cpp.hpp:206
float getCurrentPitch() const
Definition note.h:53
void update(shared_ptr< Context > context) override
Definition note.cpp.hpp:48
static constexpr float A4_FREQUENCY
Definition note.h:108
bool shouldTriggerNoteChange(u8 newNote, u8 currentNote) const
Definition note.cpp.hpp:237
u8 getLastVelocity() const
Definition note.h:51
shared_ptr< Pitch > getPitchDetector() const
Definition note.h:65
static constexpr u8 NO_NOTE
Definition note.h:110
unsigned char u8
Definition stdint.h:131
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT