FastLED 3.9.15
Loading...
Searching...
No Matches
pitch.h
Go to the documentation of this file.
1#pragma once
2
5#include "fl/stl/function.h"
6#include "fl/stl/vector.h"
7#include "fl/stl/noexcept.h"
8
9namespace fl {
10namespace audio {
11namespace detector {
12
33class Pitch : public Detector {
34public:
36 ~Pitch() FL_NOEXCEPT override;
37
38 void update(shared_ptr<Context> context) override;
39 void fireCallbacks() override;
40 bool needsFFT() const override { return false; } // Uses PCM data directly
41 const char* getName() const override { return "Pitch"; }
42 void reset() override;
43
44 // Callbacks (multiple listeners supported)
45 function_list<void(float hz)> onPitch; // Continuous pitch updates
46 function_list<void(float hz, float confidence)> onPitchWithConfidence;
47 function_list<void(float hz)> onPitchChange; // Fires when pitch changes significantly
48 function_list<void(u8 voiced)> onVoiced; // Fires when voiced/unvoiced state changes
49
50 // State access
51 float getPitch() const { return mCurrentPitch; }
52 float getConfidence() const { return mConfidence; }
53 bool isVoiced() const { return mIsVoiced; } // True if pitched sound detected
54 float getSmoothedPitch() const { return mSmoothedPitch; }
55
56 // Configuration
59 void setConfidenceThreshold(float threshold) { mConfidenceThreshold = threshold; }
60 void setSmoothingFactor(float) { /* OneEuroFilter adapts automatically */ }
62
63private:
64 // Current state
65 float mCurrentPitch; // Current detected pitch in Hz
66 float mSmoothedPitch; // Exponentially smoothed pitch
67 float mConfidence; // Detection confidence (0-1)
68 bool mIsVoiced; // True if currently detecting pitched sound
69 bool mPreviousVoiced; // Previous voiced state for change detection
70 float mPreviousPitch; // Previous pitch for change detection
71 bool mFirePitch = false;
72 bool mFirePitchChange = false;
73 bool mVoicedStateChanged = false;
74
75 // Configuration
76 float mMinFrequency; // Minimum detectable frequency (Hz)
77 float mMaxFrequency; // Maximum detectable frequency (Hz)
78 float mConfidenceThreshold; // Minimum confidence to report pitch
79 float mPitchChangeSensitivity; // Sensitivity for pitch change detection (Hz)
80
81 // Adaptive pitch smoothing: low jitter when stable, low lag on changes
83 float mLastDt = 0.023f; // Computed from pcmSize / sampleRate each frame
84
85 // Autocorrelation parameters
86 int mMinPeriod; // Minimum period in samples
87 int mMaxPeriod; // Maximum period in samples
88 float mSampleRate; // Audio sample rate (Hz)
89
90 // Autocorrelation buffer
92
93 // Helper methods
94 void updatePeriodRange();
95 float calculateAutocorrelation(const i16* pcm, size numSamples);
96 float periodToFrequency(int period) const;
97 int frequencyToPeriod(float frequency) const;
98 float calculateConfidence(const vector<float>& autocorr, int peakLag) const;
99 int findBestPeakLag(const vector<float>& autocorr) const;
100 void updatePitchSmoothing(float newPitch);
101 bool shouldReportPitchChange(float newPitch) const;
102};
103
104} // namespace detector
105} // namespace audio
106} // namespace fl
fl::UISlider sensitivity("Sensitivity", 1.5f, 0.3f, 4.0f, 0.1f)
function_list< void(float hz)> onPitch
Definition pitch.h:45
function_list< void(float hz)> onPitchChange
Definition pitch.h:47
~Pitch() FL_NOEXCEPT override
vector< float > mAutocorrelation
Definition pitch.h:91
bool isVoiced() const
Definition pitch.h:53
float calculateConfidence(const vector< float > &autocorr, int peakLag) const
float periodToFrequency(int period) const
void setPitchChangeSensitivity(float sensitivity)
Definition pitch.h:61
float getConfidence() const
Definition pitch.h:52
bool shouldReportPitchChange(float newPitch) const
float calculateAutocorrelation(const i16 *pcm, size numSamples)
void fireCallbacks() override
Definition pitch.cpp.hpp:73
void update(shared_ptr< Context > context) override
Definition pitch.cpp.hpp:32
const char * getName() const override
Definition pitch.h:41
void updatePitchSmoothing(float newPitch)
bool needsFFT() const override
Definition pitch.h:40
function_list< void(float hz, float confidence)> onPitchWithConfidence
Definition pitch.h:46
int findBestPeakLag(const vector< float > &autocorr) const
void setMaxFrequency(float hz)
Definition pitch.h:58
int frequencyToPeriod(float frequency) const
function_list< void(u8 voiced)> onVoiced
Definition pitch.h:48
float getSmoothedPitch() const
Definition pitch.h:54
OneEuroFilter< float > mPitchSmoother
Definition pitch.h:82
void setSmoothingFactor(float)
Definition pitch.h:60
float getPitch() const
Definition pitch.h:51
void setConfidenceThreshold(float threshold)
Definition pitch.h:59
void setMinFrequency(float hz)
Definition pitch.h:57
unsigned char u8
Definition stdint.h:131
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT