FastLED 3.9.15
Loading...
Searching...
No Matches
pitch_detector.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stl/vector.h"
4#include "fl/stl/noexcept.h"
5
6namespace fl {
7namespace audio {
8namespace detector {
9
10class Pitch {
11public:
12 // Constructors and destructor
15
16 // Update method
17 void update(shared_ptr<Context> context);
18 void reset();
19
20 // Callback types
21 using PitchCallback = void(*)(float pitch);
22 using PitchConfidenceCallback = void(*)(float pitch, float confidence);
23 using VoicedCallback = void(*)(u8 isVoiced);
24
25 // Pitch-related callbacks
26 PitchCallback onPitch = nullptr; // Called continuously when pitch is detected
27 PitchConfidenceCallback onPitchWithConfidence = nullptr; // Pitch with confidence score
28 PitchCallback onPitchChange = nullptr; // Called when pitch changes significantly
29 VoicedCallback onVoiced = nullptr; // Called when voiced/unvoiced state changes
30
31 // Configuration methods
34 void setConfidenceThreshold(float threshold) { mConfidenceThreshold = threshold; }
35 void setSmoothingFactor(float factor) { mSmoothingFactor = factor; }
37 void setSampleRate(float rate) { mSampleRate = rate; updatePeriodRange(); }
38
39 // Getters
40 float getPitch() const { return mSmoothedPitch; }
41 float getConfidence() const { return mConfidence; }
42 bool isVoiced() const { return mIsVoiced; }
43
44private:
45 // Pitch tracking variables
46 float mCurrentPitch;
47 float mSmoothedPitch;
48 float mConfidence;
49 bool mIsVoiced;
50 bool mPreviousVoiced;
51 float mPreviousPitch;
52
53 // Detection configuration
54 float mMinFrequency; // Minimum detectable frequency
55 float mMaxFrequency; // Maximum detectable frequency
56 float mConfidenceThreshold; // Minimum confidence to consider pitch valid
57 float mSmoothingFactor; // Pitch smoothing factor
58 float mPitchChangeSensitivity; // Threshold for pitch change events
59
60 // Sample rate and period information
61 float mSampleRate;
62 int mMinPeriod;
63 int mMaxPeriod;
64
65 // Autocorrelation buffer
67
68 // Internal helper methods
70 float calculateAutocorrelation(const i16* pcm, size numSamples);
71 int findBestPeakLag(const fl::vector<float>& autocorr) const;
72 float calculateConfidence(const fl::vector<float>& autocorr, int peakLag) const;
73 float periodToFrequency(int period) const;
74 int frequencyToPeriod(float frequency) const;
75 void updatePitchSmoothing(float newPitch);
76 bool shouldReportPitchChange(float newPitch) const;
77};
78
79} // namespace detector
80} // namespace audio
81} // namespace fl
fl::UISlider sensitivity("Sensitivity", 1.5f, 0.3f, 4.0f, 0.1f)
function_list< void(float hz)> onPitch
Definition pitch.h:45
void setSampleRate(float rate)
void(*)(float pitch, float confidence) PitchConfidenceCallback
function_list< void(float hz)> onPitchChange
Definition pitch.h:47
vector< float > mAutocorrelation
Definition pitch.h:91
bool isVoiced() const
Definition pitch.h:53
int findBestPeakLag(const fl::vector< float > &autocorr) const
float periodToFrequency(int period) const
void setPitchChangeSensitivity(float sensitivity)
void(*)(u8 isVoiced) VoicedCallback
float calculateConfidence(const fl::vector< float > &autocorr, int peakLag) const
bool shouldReportPitchChange(float newPitch) const
float calculateAutocorrelation(const i16 *pcm, size numSamples)
void update(shared_ptr< Context > context) override
Definition pitch.cpp.hpp:32
void(*)(float pitch) PitchCallback
void updatePitchSmoothing(float newPitch)
function_list< void(float hz, float confidence)> onPitchWithConfidence
Definition pitch.h:46
void setSmoothingFactor(float factor)
int frequencyToPeriod(float frequency) const
function_list< void(u8 voiced)> onVoiced
Definition pitch.h:48
void setConfidenceThreshold(float threshold)
void setMinFrequency(float hz)
Definition pitch.h:57
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