FastLED 3.10.8
Loading...
Searching...
No Matches

◆ frequencyToMidiNote() [2/2]

u8 fl::audio::detector::Note::frequencyToMidiNote ( float hz) const
private

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

158 {
159 if (hz <= 0.0f) {
160 return NO_NOTE;
161 }
162
163 // MIDI note = 69 + 12 × log₂(f / 440)
164 float semitones = 12.0f * (fl::logf(hz / A4_FREQUENCY) / fl::logf(2.0f));
165 int midiNote = static_cast<int>(A4_MIDI_NOTE + semitones + 0.5f); // Round to nearest
166
167 // Clamp to valid MIDI range (0-127)
168 if (midiNote < 0) {
169 return 0;
170 }
171 if (midiNote > 127) {
172 return 127;
173 }
174
175 return static_cast<u8>(midiNote);
176}
static constexpr u8 A4_MIDI_NOTE
Definition note.h:109
static constexpr float A4_FREQUENCY
Definition note.h:108
static constexpr u8 NO_NOTE
Definition note.h:110
float logf(float value) FL_NO_EXCEPT
Definition math.h:426
unsigned char u8
Definition stdint.h:131

References A4_FREQUENCY, A4_MIDI_NOTE, fl::FL_NO_EXCEPT, fl::logf(), and NO_NOTE.

Referenced by frequencyToMidiNote(), and update().

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