FastLED 3.9.15
Loading...
Searching...
No Matches
note_detector.h
Go to the documentation of this file.
1#pragma once
2#include "fl/stl/noexcept.h"
3
4namespace fl {
5namespace audio {
6namespace detector {
7
8class Note {
9public:
10 // Constants
11 static constexpr u8 NO_NOTE = 255;
12 static constexpr float A4_FREQUENCY = 440.0f;
13 static constexpr u8 A4_MIDI_NOTE = 69;
14
15 // Constructors
17 explicit Note(shared_ptr<Pitch> pitchDetector);
19
20 // Update and reset
21 void update(shared_ptr<Context> context);
22 void reset();
23
24 // Callback types
25 using NoteVelocityCallback = void(*)(u8 note);
26 using NoteStartCallback = void(*)(u8 note, u8 velocity);
27
28 // Note-related callback functions
29 NoteStartCallback onNoteOn = nullptr; // Triggered on new note start with velocity
30 NoteVelocityCallback onNoteOff = nullptr; // Triggered on note end
31 NoteStartCallback onNoteChange = nullptr; // Triggered on note change
32
33 // Conversion utilities
34 u8 frequencyToMidiNote(float hz) const;
35 float midiNoteToFrequency(u8 note) const;
36
37 // Getters
38 u8 getCurrentNote() const { return mCurrentNote; }
39 u8 getLastVelocity() const { return mLastVelocity; }
40 float getCurrentPitch() const { return mCurrentPitch; }
41 float getPitchBend() const { return mPitchBend; }
42
43 // Configuration
44 void setNoteOnThreshold(float threshold) { mNoteOnThreshold = threshold; }
45 void setNoteOffThreshold(float threshold) { mNoteOffThreshold = threshold; }
46 void setMinNoteDuration(u32 ms) { mMinNoteDuration = ms; }
47 void setNoteChangeThreshold(int semitones) { mNoteChangeThreshold = semitones; }
49
50private:
51 // Pitch detector (may be owned or borrowed)
54
55 // Note state tracking
58 bool mNoteActive;
59 float mCurrentPitch;
60 float mPitchBend;
61 float mNoteOnEnergy;
62 u32 mNoteOnTime;
64
65 // Detection thresholds
66 float mNoteOnThreshold;
71
72 // Note detection helpers
73 float calculatePitchBend(float hz, u8 note) const;
74 u8 calculateVelocity(float energy, float confidence) const;
75
76 // Triggers
77 bool shouldTriggerNoteOn(float confidence, float pitch) const;
78 bool shouldTriggerNoteOff(float confidence, bool voiced) const;
79 bool shouldTriggerNoteChange(u8 newNote, u8 currentNote) const;
80};
81
82} // namespace detector
83} // namespace audio
84} // 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 threshold)
void(*)(u8 note) NoteVelocityCallback
void setNoteChangeThreshold(int semitones)
void setVelocitySensitivity(float sensitivity)
float calculatePitchBend(float hz, u8 note) const
void reset() override
Definition note.cpp.hpp:143
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
bool shouldTriggerNoteOff(float confidence, bool voiced) const
function_list< void(u8 note, u8 velocity)> onNoteChange
Definition note.h:47
u8 getCurrentNote() const
Definition note.h:50
function_list< void(u8 note)> onNoteOff
Definition note.h:46
void(*)(u8 note, u8 velocity) NoteStartCallback
void setMinNoteDuration(u32 ms)
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
void setNoteOffThreshold(float threshold)
float getCurrentPitch() const
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
static constexpr u8 NO_NOTE
Definition note.h:110
Note - Musical note detection with MIDI output.
Definition note.h:32
Pitch - Continuous pitch tracking using autocorrelation.
Definition pitch.h:33
unsigned char u8
Definition stdint.h:131
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT