5#ifndef MODES_INCLUDE_AUDIO_UTILS_HPP
6#define MODES_INCLUDE_AUDIO_UTILS_HPP
10#include "src/system/ext/math8.h"
86 int eventCutoff = 500,
88 int nbEventSample = 10,
160 level = (not std::isinf(soundLevel) and not std::isnan(soundLevel) and soundLevel > -70) ? soundLevel : -70;
183 _eventCount = std::max<float>(1, _eventCount - 1);
196 if (!
hasEvent && _eventCount > nbEventSample)
203 if constexpr (ConfigTy::useBeatTracking)
212 _FFTHistory_beatDetector[historyIndex] =
fft_log;
222 if constexpr (not ConfigTy::useBeatTracking)
227 size_t nbDataPoints = 0;
232 for (; i < fft_log_end_frequencies.size(); ++i)
234 const float maxFrequencyForBin = fft_log_end_frequencies[i];
235 if (minFreq < maxFrequencyForBin)
239 for (; i < fft_log_end_frequencies.size(); ++i)
245 const float maxFrequencyForBin = fft_log_end_frequencies[i];
246 if (maxFrequencyForBin >= maxFreq)
249 return beatCnt > 0 && beatCnt >= ceil(0.5f * nbDataPoints);
263 std::array<int16_t, _dataLenght>
data;
274 uint8_t _eventCount = 0;
275 uint8_t _eventScale = 0;
282 std::array<float, _fftChannels> fft_log_end_frequencies;
285 template<
size_t T>
static inline std::array<bool, T> track_beat_events(
const FFTHistoryContainer& dataHistory,
286 const std::array<float, T>&
data)
288 std::array<bool, T> beats;
292 const size_t dataHistoryCnt = dataHistory.size();
293 const float oneOverdataHistory = 1.0f / 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);
User modes audio utilities.
Definition: utils.hpp:17
constexpr int16_t gainedSignalTarget
Desired output of the auto gain (0-INT16_MAX)
Definition: sound.h:26
Specific configuration for the sound object.
Definition: utils.hpp:23
static constexpr bool useBeatTracking
Set to true to activate the beat tracking algorithm.
Definition: utils.hpp:25
Sound processor able to detect sound level events.
Definition: utils.hpp:92
void reset(auto &ctx)
Call this once inside the mode on_enter_mode callback.
Definition: utils.hpp:122
float maxAmplitude
max detected frequency amplitude
Definition: utils.hpp:259
std::array< FFTLogContainer, _FFThistory_MaxSize > FFTHistoryContainer
history of the FFT
Definition: utils.hpp:119
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:220
static constexpr float _windowShort
Window size used for avgDelta
Definition: utils.hpp:103
float avgMax
Decaying sound "peak" level average.
Definition: utils.hpp:254
static constexpr int16_t _autoGainTargetValue
target value reached by the auto gain
Definition: utils.hpp:108
std::array< bool, _fftChannels > beatDetected
set to true when the corresponding frequency range registers a beat
Definition: utils.hpp:269
std::array< int16_t, _dataLenght > dataAutoGained
dynamically sound adjusted data
Definition: utils.hpp:265
static constexpr size_t _fftChannels
number of channels in the log fft
Definition: utils.hpp:110
std::array< float, _dataLenght/2 > fft_raw
fast fourrier transform raw results
Definition: utils.hpp:271
static constexpr float _windowSize
Window size used for avgLevel and avgMax
Definition: utils.hpp:100
float level
Last sound level measured.
Definition: utils.hpp:252
float maxAmplitudeFrequency
max amplitude frequency, in Hertz
Definition: utils.hpp:260
std::array< float, _dataLenght/2 > FFTContainer
Container for the fast fourrier linear results.
Definition: utils.hpp:117
static constexpr size_t _dataLenght
number of sample in a microphone run
Definition: utils.hpp:106
void update(auto &ctx)
Call this once every tick inside the mode loop callback.
Definition: utils.hpp:145
bool hasEvent
Did an event happened last tick? (reset each loop)
Definition: utils.hpp:257
static constexpr float _eventNorm
Inverse scaling factor for eventScale
Definition: utils.hpp:97
float avgDelta
Rolling sound "contrast" average (squared)
Definition: utils.hpp:256
static constexpr float _eventCutoff
Floor level for computations using AvgMax (lowest peak average)
Definition: utils.hpp:94
float delta
Last sound "contrast" computed (centered around 1.0)
Definition: utils.hpp:255
FFTLogContainer fft_log
fast fourrier transform as a log scale (closer to human perception)
Definition: utils.hpp:267
std::array< int16_t, _dataLenght > data
raw microphone data
Definition: utils.hpp:263
static constexpr size_t _FFThistory_MaxSize
Size of the fourrier transform history.
Definition: utils.hpp:115
uint8_t eventScale
Event scale (0-255)
Definition: utils.hpp:258
static constexpr float fftResolutionHz
Frequency resolution of the raw fft result.
Definition: utils.hpp:113
float avgLevel
Rolling sound level average.
Definition: utils.hpp:253
std::array< float, _fftChannels > FFTLogContainer
Container for the fast fourrier logarithmic results.
Definition: utils.hpp:118
Handle the analysis of a sound sample. This structure handled the FastFourrier analysis of a sound sa...
Definition: sound.h:33
std::array< float, SAMPLE_SIZE/2 > fft_raw
Results of the FFT process, in raw Hertz bins.
Definition: sound.h:72
std::array< int16_t, SAMPLE_SIZE > data
raw audio data
Definition: sound.h:45
std::array< float, numberOfFFtChanels > fft_log_end_frequencies
Results of the FFT process, in maximum frequency for every bin.
Definition: sound.h:76
static constexpr float get_fft_resolution_Hz()
Return the FFT resolution of a single FFT bin in Hertz.
Definition: sound.h:67
float maxAmplitudeFrequency
Maximum detected amplitude frequency of this sample, in Hertz.
Definition: sound.h:54
static constexpr auto SAMPLE_SIZE
Size fo the audio sample.
Definition: sound.h:42
float maxAmplitude
Maximum detected amplitude of this sample.
Definition: sound.h:52
std::array< int16_t, SAMPLE_SIZE > rectifiedData
audio data with auto gain enabled
Definition: sound.h:47
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:64
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:74
float sound_level_Db
Sound level of this sample, in Decibels A.
Definition: sound.h:50