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

◆ update() [2/2]

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

Implements fl::audio::Detector.

Definition at line 42 of file percussion.cpp.hpp.

42 {
43 mRetainedFFT = context->getFFT16(fft::Mode::CQ_NAIVE);
44 const fft::Bins& fft = *mRetainedFFT;
45 u32 timestamp = context->getTimestamp();
46
47 // Step 0: Get zero-crossing factor from raw audio sample
48 mZeroCrossingFactor = context->getZCF();
49
50 // Step 1: Compute spectral features from 16-bin fft::FFT
51 computeFeatures(fft);
52
53 // Step 2: Compute total energy and onset sharpness via envelope
54 float totalEnergy = 0.0f;
55 for (fl::size i = 0; i < fft.raw().size(); ++i) {
56 totalEnergy += fft.raw()[i];
57 }
58 const float dt = computeAudioDt(context->getPCM().size(), context->getSampleRate());
59 float envValue = mTotalEnvelope.update(totalEnergy, dt);
60 float flux = fl::max(0.0f, totalEnergy - envValue);
61 mOnsetSharpness = (totalEnergy > 1e-6f) ? flux / totalEnergy : 0.0f;
62
63 // Step 3: Gate on onset — no onset means no percussion
64 static constexpr float kOnsetGate = 0.05f;
65 if (mOnsetSharpness < kOnsetGate) {
66 mKickDetected = false;
67 mSnareDetected = false;
68 mHiHatDetected = false;
69 mTomDetected = false;
70 mKickConfidence = 0.0f;
71 mSnareConfidence = 0.0f;
72 mHiHatConfidence = 0.0f;
73 mTomConfidence = 0.0f;
74 return;
75 }
76
77 // Step 4: Compute per-type confidence scores
79
80 // Step 5: Apply cross-band rejection
82
83 // Step 6: Threshold + cooldown → boolean state
85 (timestamp - mLastKickTime >= KICK_COOLDOWN_MS);
87 (timestamp - mLastSnareTime >= SNARE_COOLDOWN_MS);
89 (timestamp - mLastHiHatTime >= HIHAT_COOLDOWN_MS);
91 (timestamp - mLastTomTime >= TOM_COOLDOWN_MS);
92
93 // Update timestamps
94 if (mKickDetected) mLastKickTime = timestamp;
95 if (mSnareDetected) mLastSnareTime = timestamp;
96 if (mHiHatDetected) mLastHiHatTime = timestamp;
97 if (mTomDetected) mLastTomTime = timestamp;
98}
AudioAnalyzeFFT1024 fft
static constexpr u32 SNARE_COOLDOWN_MS
Definition percussion.h:108
shared_ptr< const fft::Bins > mRetainedFFT
Definition percussion.h:105
AttackDecayFilter< float > mTotalEnvelope
Definition percussion.h:97
static constexpr u32 KICK_COOLDOWN_MS
Definition percussion.h:107
static constexpr u32 HIHAT_COOLDOWN_MS
Definition percussion.h:109
void computeFeatures(const fft::Bins &fft)
static constexpr u32 TOM_COOLDOWN_MS
Definition percussion.h:110
float computeAudioDt(fl::size pcmSize, int sampleRate) FL_NOEXCEPT
Compute the time delta (in seconds) for an audio buffer.
constexpr common_type_t< T, U > max(T a, U b) FL_NOEXCEPT
Definition math.h:75

References Percussion(), applyCrossBandRejection(), fl::audio::computeAudioDt(), computeConfidences(), computeFeatures(), fl::audio::fft::CQ_NAIVE, HIHAT_COOLDOWN_MS, KICK_COOLDOWN_MS, fl::max(), mHiHatConfidence, mHiHatDetected, mHiHatThreshold, mKickConfidence, mKickDetected, mKickThreshold, mLastHiHatTime, mLastKickTime, mLastSnareTime, mLastTomTime, mOnsetSharpness, mRetainedFFT, mSnareConfidence, mSnareDetected, mSnareThreshold, mTomConfidence, mTomDetected, mTomThreshold, mTotalEnvelope, mZeroCrossingFactor, SNARE_COOLDOWN_MS, TOM_COOLDOWN_MS, and update().

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

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