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

◆ updateVolumeAndPeak()

void fl::audio::Reactive::updateVolumeAndPeak ( const Sample & sample)
private

Definition at line 335 of file audio_reactive.cpp.hpp.

335 {
336 // Get PCM data from Sample
337 const auto& pcmData = sample.pcm();
338 if (pcmData.empty()) {
339 mCurrentData.volume = 0.0f;
340 mCurrentData.volumeRaw = 0.0f;
341 mCurrentData.peak = 0.0f;
342 return;
343 }
344
345 // Raw volume: simple bounded normalization (no adaptive smoothing)
346 // 23170 ≈ max RMS of a full-scale 16-bit sine wave (32767 / √2).
347 // Clamp so clipped/square-wave input stays at 1.0.
348 float rms = sample.rms();
349 float raw = rms / 23170.0f;
350 mCurrentData.volumeRaw = (raw > 1.0f) ? 1.0f : raw;
351
352 // Normalized volume from Processor's EnergyAnalyzer (adaptive 0.0-1.0).
353 // mAudioProcessor is guaranteed non-null: ensureAudioProcessor() is called
354 // in processSample() immediately before this method.
355 FL_ASSERT(mAudioProcessor, "mAudioProcessor is null — was begin() called before processSample()?");
356 mCurrentData.volume = mAudioProcessor->getEnergy();
357
358 // Peak: simple bounded normalization
359 float maxSample = 0.0f;
360 for (fl::i16 pcmSample : pcmData) {
361 float absSample = (pcmSample < 0) ? -static_cast<float>(pcmSample) : static_cast<float>(pcmSample);
362 maxSample = (maxSample > absSample) ? maxSample : absSample;
363 }
364 mCurrentData.peak = maxSample / 32768.0f;
365}
float rms(fl::span< const int16_t > data)
Definition simple.h:104
#define FL_ASSERT(x, MSG)
Definition assert.h:6
fl::unique_ptr< Processor > mAudioProcessor
CRGB sample(const CRGB *grid, const XYMap &xyMap, float x, float y, SampleMode mode)
Sample a pixel from a 2D CRGB grid at floating-point coordinates.
Definition sample.cpp.hpp:9

References FL_ASSERT, mAudioProcessor, mCurrentData, rms(), and fl::sample().

Referenced by processSample().

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