| 59 | } |
| 60 | |
| 61 | void ChordDetector::update(shared_ptr<Context> context) { |
| 62 | mRetainedFFT = context->getFFT(32); // Higher resolution for pitch detection |
| 63 | const fft::Bins& fft = *mRetainedFFT; |
| 64 | u32 timestamp = context->getTimestamp(); |
| 65 | |
| 66 | // Calculate chroma features |
| 67 | calculateChroma(fft); |
| 68 | |
| 69 | // Detect current chord |
| 70 | Chord detected = detectChord(mChroma, timestamp); |
| 71 | |
| 72 | // Check if chord changed |
| 73 | if (detected.isValid() && detected.confidence >= mConfidenceThreshold) { |
| 74 | if (!mCurrentChord.isValid() || !isSimilarChord(detected, mCurrentChord)) { |
| 75 | // New chord detected |
| 76 | if (mCurrentChord.isValid()) { |
| 77 | mFireChordEnd = true; |
| 78 | } |
| 79 | |
| 80 | mPreviousChord = mCurrentChord; |
| 81 | mCurrentChord = detected; |
| 82 | mChordStartTime = timestamp; |
| 83 | |
| 84 | mFireChordChange = true; |
| 85 | |
| 86 | FL_DBG("Chord detected: " << mCurrentChord.getRootName() |
| 87 | << mCurrentChord.getTypeName() |
| 88 | << " (conf: " << mCurrentChord.confidence << ")"); |
| 89 | } else { |
| 90 | // Same chord, update confidence |
| 91 | mCurrentChord.confidence = detected.confidence; |
| 92 | mCurrentChord.timestamp = timestamp; |
| 93 | } |
| 94 | |
| 95 | // Set flag for onChord callback every frame when chord is active |
| 96 | mFireChord = true; |
| 97 | } else { |
| 98 | // No valid chord or low confidence |
| 99 | if (mCurrentChord.isValid()) { |
| 100 | u32 duration = timestamp - mChordStartTime; |
| 101 | if (duration >= mMinChordDuration) { |
| 102 | mChordEndTime = timestamp; |
| 103 | mFireChordEnd = true; |
| 104 | } |
| 105 | mCurrentChord = Chord(); // Reset to invalid |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Save chroma for next frame |
| 110 | for (int i = 0; i < 12; i++) { |
| 111 | mPrevChroma[i] = mChroma[i]; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void ChordDetector::fireCallbacks() { |
| 116 | if (mFireChordEnd) { |
nothing calls this directly
no test coverage detected