FastLED 3.9.15
Loading...
Searching...
No Matches

◆ update() [2/2]

void fl::audio::detector::Pitch::update ( shared_ptr< Context > context)
overridevirtual

Implements fl::audio::Detector.

Definition at line 32 of file pitch.cpp.hpp.

32 {
33 // Get PCM data from context
34 span<const i16> pcm = context->getPCM();
35 size numSamples = pcm.size();
36
37 // Compute dt from actual audio buffer duration
38 mLastDt = computeAudioDt(pcm.size(), static_cast<int>(mSampleRate));
39
40 // Need at least 2x max period for autocorrelation
41 if (numSamples < static_cast<size>(mMaxPeriod * 2)) {
42 // Not enough samples for reliable pitch detection
43 mConfidence = 0.0f;
44 mIsVoiced = false;
45
47 return;
48 }
49
50 // Calculate autocorrelation and find pitch
51 float detectedPitch = calculateAutocorrelation(pcm.data(), numSamples);
52
53 // Check if pitch is valid and confidence is sufficient
54 if (detectedPitch > 0.0f && mConfidence >= mConfidenceThreshold) {
55 mIsVoiced = true;
56 mCurrentPitch = detectedPitch;
57 updatePitchSmoothing(detectedPitch);
58 mFirePitch = true;
59
60 if (shouldReportPitchChange(detectedPitch)) {
61 mFirePitchChange = true;
62 mPreviousPitch = detectedPitch;
63 }
64 } else {
65 mIsVoiced = false;
66 mCurrentPitch = 0.0f;
67 mFirePitch = false;
68 }
69
71}
bool shouldReportPitchChange(float newPitch) const
float calculateAutocorrelation(const i16 *pcm, size numSamples)
void updatePitchSmoothing(float newPitch)
float computeAudioDt(fl::size pcmSize, int sampleRate) FL_NOEXCEPT
Compute the time delta (in seconds) for an audio buffer.

References Pitch(), calculateAutocorrelation(), fl::audio::computeAudioDt(), fl::span< T, Extent >::data(), mConfidence, mConfidenceThreshold, mCurrentPitch, mFirePitch, mFirePitchChange, mIsVoiced, mLastDt, mMaxPeriod, mPreviousPitch, mPreviousVoiced, mSampleRate, mVoicedStateChanged, shouldReportPitchChange(), fl::span< T, Extent >::size(), update(), and updatePitchSmoothing().

Referenced by ~Pitch(), update(), and update().

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