| 30 | Pitch::~Pitch() FL_NOEXCEPT = default; |
| 31 | |
| 32 | void Pitch::update(shared_ptr<Context> context) { |
| 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 | |
| 46 | mVoicedStateChanged = (mIsVoiced != mPreviousVoiced); |
| 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 | |
| 70 | mVoicedStateChanged = (mIsVoiced != mPreviousVoiced); |
| 71 | } |
| 72 | |
| 73 | void Pitch::fireCallbacks() { |
| 74 | if (mFirePitch) { |
no test coverage detected