MCPcopy Create free account
hub / github.com/FastLED/FastLED / updateVolumeAndPeak

Method updateVolumeAndPeak

src/fl/audio/audio_reactive.cpp.hpp:335–365  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

333}
334
335void Reactive::updateVolumeAndPeak(const Sample& sample) {
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}
366
367void Reactive::detectBeat(fl::u32 currentTimeMs) {
368 // Need minimum time since last beat

Callers

nothing calls this directly

Calls 3

getEnergyMethod · 0.80
emptyMethod · 0.45
rmsMethod · 0.45

Tested by

no test coverage detected