FastLED 3.10.8
Loading...
Searching...
No Matches
fl::audio::detector::Pitch Class Reference

Detailed Description

Pitch - Continuous pitch tracking using autocorrelation.

Detects the fundamental frequency (pitch) of audio signals using autocorrelation analysis on the time-domain PCM data. This detector provides continuous pitch tracking with configurable confidence thresholds, smoothing, and stability.

Key Features:

  • Autocorrelation-based pitch detection (time-domain analysis)
  • Configurable pitch range (default: 80-1000 Hz)
  • Confidence-based filtering to reject unreliable detections
  • Exponential smoothing for stable pitch output
  • Pitch change detection with configurable sensitivity
  • Support for both voiced (pitched) and unvoiced (unpitched) audio

Performance:

  • No FFT required (uses raw PCM data)
  • Update time: ~0.2-0.5ms per frame
  • Memory: ~100 bytes + autocorrelation buffer (~2KB for 512 samples)

Definition at line 33 of file pitch.h.

#include <pitch.h>

+ Inheritance diagram for fl::audio::detector::Pitch:
+ Collaboration diagram for fl::audio::detector::Pitch:

Public Types

using PitchCallback = void(*)(float pitch)
 
using PitchConfidenceCallback = void(*)(float pitch, float confidence)
 
using VoicedCallback = void(*)(u8 isVoiced)
 

Public Member Functions

 Pitch () FL_NO_EXCEPT
 
 Pitch () FL_NO_EXCEPT
 
 ~Pitch () FL_NO_EXCEPT
 
 ~Pitch () FL_NO_EXCEPT override
 
void fireCallbacks () FL_NO_EXCEPT override
 
float getConfidence () const
 
float getConfidence () const FL_NO_EXCEPT
 
const char * getName () const FL_NO_EXCEPT override
 
float getPitch () const
 
float getPitch () const FL_NO_EXCEPT
 
float getSmoothedPitch () const FL_NO_EXCEPT
 
bool isVoiced () const
 
bool isVoiced () const FL_NO_EXCEPT
 
bool needsFFT () const FL_NO_EXCEPT override
 
void reset ()
 
void reset () FL_NO_EXCEPT override
 
void setConfidenceThreshold (float threshold)
 
void setConfidenceThreshold (float threshold) FL_NO_EXCEPT
 
void setMaxFrequency (float hz)
 
void setMaxFrequency (float hz) FL_NO_EXCEPT
 
void setMinFrequency (float hz)
 
void setMinFrequency (float hz) FL_NO_EXCEPT
 
void setPitchChangeSensitivity (float sensitivity)
 
void setPitchChangeSensitivity (float sensitivity) FL_NO_EXCEPT
 
void setSampleRate (float rate)
 
void setSmoothingFactor (float factor)
 
void setSmoothingFactor (float) FL_NO_EXCEPT
 
void update (shared_ptr< Context > context)
 
void update (shared_ptr< Context > context) FL_NO_EXCEPT override
 
- Public Member Functions inherited from fl::audio::Detector
virtual ~Detector () FL_NO_EXCEPT=default
 
virtual bool needsFFTHistory () const FL_NO_EXCEPT
 
virtual void setSampleRate (int) FL_NO_EXCEPT
 

Public Attributes

function_list< void(float hz)> onPitch
 
PitchCallback onPitch = nullptr
 
function_list< void(float hz)> onPitchChange
 
PitchCallback onPitchChange = nullptr
 
function_list< void(float hz, float confidence)> onPitchWithConfidence
 
PitchConfidenceCallback onPitchWithConfidence = nullptr
 
function_list< void(u8 voiced)> onVoiced
 
VoicedCallback onVoiced = nullptr
 

Private Member Functions

float calculateAutocorrelation (const i16 *pcm, size numSamples)
 
float calculateAutocorrelation (const i16 *pcm, size numSamples) FL_NO_EXCEPT
 
float calculateConfidence (const fl::vector< float > &autocorr, int peakLag) const
 
float calculateConfidence (const vector< float > &autocorr, int peakLag) const FL_NO_EXCEPT
 
int findBestPeakLag (const fl::vector< float > &autocorr) const
 
int findBestPeakLag (const vector< float > &autocorr) const FL_NO_EXCEPT
 
int frequencyToPeriod (float frequency) const
 
int frequencyToPeriod (float frequency) const FL_NO_EXCEPT
 
float periodToFrequency (int period) const
 
float periodToFrequency (int period) const FL_NO_EXCEPT
 
bool shouldReportPitchChange (float newPitch) const
 
bool shouldReportPitchChange (float newPitch) const FL_NO_EXCEPT
 
void updatePeriodRange ()
 
void updatePeriodRange () FL_NO_EXCEPT
 
void updatePitchSmoothing (float newPitch)
 
void updatePitchSmoothing (float newPitch) FL_NO_EXCEPT
 

Private Attributes

vector< float > mAutocorrelation
 
fl::vector< float > mAutocorrelation
 
float mConfidence
 
float mConfidenceThreshold
 
float mCurrentPitch
 
bool mFirePitch = false
 
bool mFirePitchChange = false
 
bool mIsVoiced
 
float mLastDt = 0.023f
 
float mMaxFrequency
 
int mMaxPeriod
 
float mMinFrequency
 
int mMinPeriod
 
float mPitchChangeSensitivity
 
OneEuroFilter< float > mPitchSmoother {1.0f, 0.5f}
 
float mPreviousPitch
 
bool mPreviousVoiced
 
float mSampleRate
 
float mSmoothedPitch
 
float mSmoothingFactor
 
bool mVoicedStateChanged = false
 

The documentation for this class was generated from the following files: