FastLED 3.9.15
Loading...
Searching...
No Matches

◆ update() [2/2]

void fl::audio::detector::Note::update ( shared_ptr< Context > context)
overridevirtual

Implements fl::audio::Detector.

Definition at line 48 of file note.cpp.hpp.

48 {
49 // Update pitch detector if we own it
51 mPitchDetector->update(context);
52 }
53
54 // Get pitch detection results
55 if (!mPitchDetector) {
56 return; // No pitch detector available
57 }
58
59 float pitch = mPitchDetector->getPitch();
60 float confidence = mPitchDetector->getConfidence();
61 bool voiced = mPitchDetector->isVoiced();
62 u32 timestamp = context->getTimestamp();
63 float energy = context->getRMS();
64
65 mCurrentPitch = pitch;
66 mLastUpdateTime = timestamp;
67
68 // State machine: note-on, note-off, note-change detection
69 if (!mNoteActive) {
70 // No note currently active - check for note-on
71 if (shouldTriggerNoteOn(confidence, pitch)) {
72 u8 newNote = frequencyToMidiNote(pitch);
73 u8 velocity = calculateVelocity(energy, confidence);
74
75 mCurrentNote = newNote;
76 mLastVelocity = velocity;
77 mNoteActive = true;
78 mNoteOnTime = timestamp;
79 mNoteOnEnergy = energy;
80 mPitchBend = calculatePitchBend(pitch, newNote);
81
82 mFireNoteOn = true;
83 mPendingOnNote = newNote;
84 mPendingOnVelocity = velocity;
85 }
86 } else {
87 // Note currently active - check for note-off or note-change
88 if (shouldTriggerNoteOff(confidence, voiced)) {
89 // Check minimum note duration to prevent flicker
90 u32 noteDuration = timestamp - mNoteOnTime;
91 if (noteDuration >= mMinNoteDuration) {
92 mFireNoteOff = true;
94
96 mLastVelocity = 0;
97 mNoteActive = false;
98 mPitchBend = 0.0f;
99 }
100 } else if (voiced && confidence >= mNoteOnThreshold) {
101 // Note still active - check for note change
102 u8 newNote = frequencyToMidiNote(pitch);
104
106 u8 velocity = calculateVelocity(energy, confidence);
107
108 // Set pending note-off for old note
109 mFireNoteOff = true;
111
112 // Update to new note
113 mCurrentNote = newNote;
114 mLastVelocity = velocity;
115 mNoteOnTime = timestamp;
116 mPitchBend = calculatePitchBend(pitch, newNote);
117
118 // Set pending note-on for new note
119 mFireNoteOn = true;
120 mPendingOnNote = newNote;
121 mPendingOnVelocity = velocity;
122 mFireNoteChange = true;
123 }
124 }
125 }
126}
float calculatePitchBend(float hz, u8 note) const
Definition note.cpp.hpp:188
bool shouldTriggerNoteOn(float confidence, float pitch) const
Definition note.cpp.hpp:223
bool shouldTriggerNoteOff(float confidence, bool voiced) const
Definition note.cpp.hpp:230
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
bool shouldTriggerNoteChange(u8 newNote, u8 currentNote) const
Definition note.cpp.hpp:237
static constexpr u8 NO_NOTE
Definition note.h:110
unsigned char u8
Definition stdint.h:131

References Note(), calculatePitchBend(), calculateVelocity(), frequencyToMidiNote(), mCurrentNote, mCurrentPitch, mFireNoteChange, mFireNoteOff, mFireNoteOn, mLastUpdateTime, mLastVelocity, mMinNoteDuration, mNoteActive, mNoteOnEnergy, mNoteOnThreshold, mNoteOnTime, mOwnsPitchDetector, mPendingOffNote, mPendingOnNote, mPendingOnVelocity, mPitchBend, mPitchDetector, NO_NOTE, shouldTriggerNoteChange(), shouldTriggerNoteOff(), shouldTriggerNoteOn(), and update().

Referenced by ~Note(), update(), and update().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: