5#ifndef MODES_INCLUDE_AUDIO_UTILS_HPP
6#define MODES_INCLUDE_AUDIO_UTILS_HPP
13#include "src/system/ext/math8.h"
15#include <etl/bitset.h>
90 int eventCutoff = 500,
92 int nbEventSample = 10,
161 level = (not std::isinf(soundLevel) and not std::isnan(soundLevel) and soundLevel > -70) ? soundLevel : -70;
185 _eventCount = std::max<float>(1, _eventCount - 1);
198 if (!
hasEvent && _eventCount > nbEventSample)
205 if constexpr (ConfigTy::useBeatTracking)
217 _FFTHistory_beatDetector[historyIndex] =
fft_log;
227 if constexpr (not ConfigTy::useBeatTracking)
232 size_t nbDataPoints = 0;
237 for (; i < fft_log_end_frequencies.size(); ++i)
239 const float maxFrequencyForBin = fft_log_end_frequencies[i];
240 if (minFreq < maxFrequencyForBin)
244 for (; i < fft_log_end_frequencies.size(); ++i)
250 const float maxFrequencyForBin = fft_log_end_frequencies[i];
251 if (maxFrequencyForBin >= maxFreq)
254 return beatCnt > 0 && beatCnt >= ceilf(0.5f * nbDataPoints);
273 uint8_t _eventCount = 0;
274 uint8_t _eventScale = 0;
278 size_t historyIndex = 0;
279 size_t historySize = 0;
285 template<
size_t T>
static inline etl::bitset<T> track_beat_events(
const FFTHistoryContainer& dataHistory,
286 const std::array<float, T>& data)
288 etl::bitset<T> beats;
292 const size_t dataHistoryCnt = dataHistory.size();
293 const float oneOverdataHistory = 1.0f /
static_cast<float>(dataHistoryCnt);
295 std::array<float, T> averageFft;
296 averageFft.fill(0.0f);
297 for (
const auto& fft: dataHistory)
299 for (
size_t i = 0; i < T; i++)
300 averageFft[i] += fft[i] * oneOverdataHistory;
303 std::array<float, T> varianceFft;
304 varianceFft.fill(0.0f);
305 for (
const auto& fft: dataHistory)
307 for (
size_t i = 0; i < T; i++)
309 const float val = fft[i] - averageFft[i];
310 varianceFft[i] += val * val;
314 for (
size_t i = 0; i < T; i++)
316 varianceFft[i] *= oneOverdataHistory;
317 const float stdDev = sqrtf(varianceFft[i]);
318 static constexpr float N = 1.5f;
324 beats[i] = data[i] > (averageFft[i] + N * stdDev);
332inline static SoundEventTy<> sharedSoundEvent = SoundEventTy();
335static SoundEventTy<>& get_shared_sound_event() {
return __private::sharedSoundEvent; }
User modes audio utilities.
Definition: utils.hpp:18
Handle the analysis of a sound sample. This structure handled the FastFourrier analysis of a sound sa...
Definition: sound.h:34
std::array< float, numberOfFFtChanels > fft_log
Results of the FFT process, in scaled logarithmic bins. This is closer to the sound sensitivity of th...
Definition: sound.h:75
float sound_level_Db
Sound level of this sample, in Decibels A.
Definition: sound.h:51
std::array< float, numberOfFFtChanels > fft_log_end_frequencies
Results of the FFT process, in maximum frequency for every bin.
Definition: sound.h:77
float maxAmplitude
Maximum detected amplitude of this sample.
Definition: sound.h:53
static constexpr float get_fft_resolution_Hz()
Return the FFT resolution of a single FFT bin in Hertz.
Definition: sound.h:68
float maxAmplitudeFrequency
Maximum detected amplitude frequency of this sample, in Hertz.
Definition: sound.h:55
static constexpr uint8_t numberOfFFtChanels
Define the number of FFt bins to use. You should set this number close to the lamp max X coordinates.
Definition: sound.h:65
Specific configuration for the sound object.
Definition: utils.hpp:24
static constexpr bool useBeatTracking
Set to true to activate the beat tracking algorithm.
Definition: utils.hpp:26
Sound processor able to detect sound level events.
Definition: utils.hpp:96
void reset(auto &ctx)
Call this once inside the mode on_enter_mode callback.
Definition: utils.hpp:128
etl::bitset< _fftChannels > beatDetected
set to true when the corresponding frequency range registers a beat. Always false without beat tracki...
Definition: utils.hpp:270
float maxAmplitude
max detected frequency amplitude
Definition: utils.hpp:264
std::array< FFTLogContainer, _FFThistory_MaxSize > FFTHistoryContainer
history of the FFT
Definition: utils.hpp:125
bool is_beat_on_freq_range(const float minFreq, const float maxFreq)
After the update, given a range, will return a boolean for beat detection.
Definition: utils.hpp:225
static constexpr float _windowShort
Window size used for avgDelta
Definition: utils.hpp:107
float avgMax
Decaying sound "peak" level average.
Definition: utils.hpp:259
static constexpr size_t _fftChannels
number of channels in the log fft
Definition: utils.hpp:117
static constexpr float _windowSize
Window size used for avgLevel and avgMax
Definition: utils.hpp:104
float level
Last sound level measured.
Definition: utils.hpp:257
float maxAmplitudeFrequency
max amplitude frequency, in Hertz
Definition: utils.hpp:265
static constexpr float _eventScaleFactor
Precomputed eventScale factor.
Definition: utils.hpp:114
static constexpr float _windowShortInv
Precomputed rolling average factor of avgDelta
Definition: utils.hpp:112
void update(auto &ctx)
Call this once every tick inside the mode loop callback.
Definition: utils.hpp:150
bool hasEvent
Did an event happened last tick? (reset each loop)
Definition: utils.hpp:262
static constexpr float _eventNorm
Inverse scaling factor for eventScale
Definition: utils.hpp:101
float avgDelta
Rolling sound "contrast" average (squared)
Definition: utils.hpp:261
static constexpr float _eventCutoff
Floor level for computations using AvgMax (lowest peak average)
Definition: utils.hpp:98
float delta
Last sound "contrast" computed (centered around 1.0)
Definition: utils.hpp:260
FFTLogContainer fft_log
fast fourrier transform as a log scale (closer to human perception)
Definition: utils.hpp:268
static constexpr size_t _FFThistory_MaxSize
Size of the fourrier transform history. Not allocated without beat tracking.
Definition: utils.hpp:122
uint8_t eventScale
Event scale (0-255)
Definition: utils.hpp:263
static constexpr float fftResolutionHz
Frequency resolution of the raw fft result.
Definition: utils.hpp:120
float avgLevel
Rolling sound level average.
Definition: utils.hpp:258
static constexpr float _windowSizeInv
Precomputed rolling average factors: multiplications are faster than divisions on the target.
Definition: utils.hpp:110
std::array< float, _fftChannels > FFTLogContainer
Container for the fast fourrier logarithmic results.
Definition: utils.hpp:124