Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1
5#ifndef MODES_INCLUDE_AUDIO_UTILS_HPP
6#define MODES_INCLUDE_AUDIO_UTILS_HPP
7
9
10#include <cmath>
11#include <cstdint>
12
13#include "src/system/ext/math8.h"
14
15#include <etl/bitset.h>
16
19
24{
26 static constexpr bool useBeatTracking = false;
27};
28
89template<typename ConfigTy = MicrophoneConfig,
90 int eventCutoff = 500,
91 int eventNorm = 500,
92 int nbEventSample = 10,
93 int windowSize = 100,
94 int windowShort = 64>
96{
98 static constexpr float _eventCutoff = eventCutoff / 100.0f;
99
101 static constexpr float _eventNorm = eventNorm / 100.0f;
102
104 static constexpr float _windowSize = windowSize / 4.0f;
105
107 static constexpr float _windowShort = windowShort / 4.0f;
108
110 static constexpr float _windowSizeInv = 1.0f / (_windowSize + 1.0f);
112 static constexpr float _windowShortInv = 1.0f / (_windowShort + 1.0f);
114 static constexpr float _eventScaleFactor = 256.0f / _eventNorm;
115
118
122 static constexpr size_t _FFThistory_MaxSize = ConfigTy::useBeatTracking ? round(fftResolutionHz) : 0;
123
124 using FFTLogContainer = std::array<float, _fftChannels>;
125 using FFTHistoryContainer = std::array<FFTLogContainer, _FFThistory_MaxSize>;
126
128 void reset(auto& ctx)
129 {
130 level = 10.0;
131 avgLevel = level;
132 avgMax = level;
133 delta = 0;
134 avgDelta = 0;
135 hasEvent = false;
136 eventScale = 0;
137 maxAmplitude = 0;
139 _eventCount = 0;
140 _eventScale = 0;
141
142 historyIndex = 0;
143 historySize = 0;
144
145 fft_log.fill(0);
146 beatDetected.reset();
147 }
148
150 void update(auto& ctx)
151 {
152 const component::microphone::SoundStruct& soundObject = ctx.lamp.get_sound_struct();
153
154 // copy microphone data
155 fft_log = soundObject.fft_log;
156 maxAmplitude = soundObject.maxAmplitude;
158
159 // average input sound over a second-long window (approx)
160 const auto soundLevel = soundObject.sound_level_Db;
161 level = (not std::isinf(soundLevel) and not std::isnan(soundLevel) and soundLevel > -70) ? soundLevel : -70;
163
164 // average maximum over the same long window (approx)
165 avgMax = (avgLevel + std::max<float>(level, avgMax) * _windowSize) * _windowSizeInv;
166 avgMax = std::max<float>(_eventCutoff, avgMax);
167
168 // delta between instantaneous level & average, squashed by its max
169 delta = std::max<float>(level - avgLevel, 0.0f) / avgMax;
170
171 // average of delta square (1.0 split around _eventCutoff)
173
174 // if avgDelta looks triggered, start sampling event
175 if (avgDelta > 1.0f)
176 {
177 _eventCount += 1;
178 // constrained before the conversion: an out of range float to integer conversion is undefined
179 _eventScale = std::min<float>(std::max<float>(_eventScale, _eventScaleFactor * avgMax * (1.0f + delta)), 255.0f);
180
181 // if no trigger, decay event
182 }
183 else
184 {
185 _eventCount = std::max<float>(1, _eventCount - 1);
186 }
187
188 // if event is active, and event decayed, reset eventScale & hasEvent
189 if (hasEvent)
190 {
191 _eventCount = 0;
192 _eventScale = 0;
193 eventScale = 0;
194 hasEvent = false;
195 }
196
197 // if no event is active, and enough samples, set eventScale & hasEvent
198 if (!hasEvent && _eventCount > nbEventSample)
199 {
200 eventScale = _eventScale;
201 hasEvent = true;
202 }
203
204 // beat tracking is heavy on performances
205 if constexpr (ConfigTy::useBeatTracking)
206 {
207 // constant, only needed by is_beat_on_freq_range
208 fft_log_end_frequencies = soundObject.fft_log_end_frequencies;
209
210 // update history size
211 if (historySize < _FFThistory_MaxSize)
212 historySize++;
213 else
214 beatDetected = track_beat_events(_FFTHistory_beatDetector, fft_log);
215
216 // add sample to history
217 _FFTHistory_beatDetector[historyIndex] = fft_log;
218 historyIndex++;
219 if (historyIndex >= _FFThistory_MaxSize)
220 historyIndex = 0;
221 }
222 }
223
225 bool is_beat_on_freq_range(const float minFreq, const float maxFreq)
226 {
227 if constexpr (not ConfigTy::useBeatTracking)
228 {
229 return false;
230 }
231
232 size_t nbDataPoints = 0;
233 size_t beatCnt = 0;
234
235 size_t i = 0;
236 // climb to min freq bin
237 for (; i < fft_log_end_frequencies.size(); ++i)
238 {
239 const float maxFrequencyForBin = fft_log_end_frequencies[i];
240 if (minFreq < maxFrequencyForBin)
241 break;
242 }
243 // register beat events
244 for (; i < fft_log_end_frequencies.size(); ++i)
245 {
246 nbDataPoints++;
247 if (beatDetected[i])
248 beatCnt++;
249
250 const float maxFrequencyForBin = fft_log_end_frequencies[i];
251 if (maxFrequencyForBin >= maxFreq)
252 break;
253 }
254 return beatCnt > 0 && beatCnt >= ceilf(0.5f * nbDataPoints);
255 }
256
257 float level = 10;
258 float avgLevel = 10;
259 float avgMax = 10;
260 float delta = 0;
261 float avgDelta = 0;
262 bool hasEvent = false;
263 uint8_t eventScale = 0;
264 float maxAmplitude = 0;
266
270 etl::bitset<_fftChannels> beatDetected;
271
272private:
273 uint8_t _eventCount = 0;
274 uint8_t _eventScale = 0;
275
277 FFTHistoryContainer _FFTHistory_beatDetector = {};
278 size_t historyIndex = 0;
279 size_t historySize = 0;
280
282 FFTLogContainer fft_log_end_frequencies = {};
283
285 template<size_t T> static inline etl::bitset<T> track_beat_events(const FFTHistoryContainer& dataHistory,
286 const std::array<float, T>& data)
287 {
288 etl::bitset<T> beats;
289 beats.reset();
290
291 // beat detection starts after a bit
292 const size_t dataHistoryCnt = dataHistory.size();
293 const float oneOverdataHistory = 1.0f / static_cast<float>(dataHistoryCnt);
294
295 std::array<float, T> averageFft;
296 averageFft.fill(0.0f);
297 for (const auto& fft: dataHistory)
298 {
299 for (size_t i = 0; i < T; i++)
300 averageFft[i] += fft[i] * oneOverdataHistory;
301 }
302
303 std::array<float, T> varianceFft;
304 varianceFft.fill(0.0f);
305 for (const auto& fft: dataHistory)
306 {
307 for (size_t i = 0; i < T; i++)
308 {
309 const float val = fft[i] - averageFft[i];
310 varianceFft[i] += val * val;
311 }
312 }
313
314 for (size_t i = 0; i < T; i++)
315 {
316 varianceFft[i] *= oneOverdataHistory;
317 const float stdDev = sqrtf(varianceFft[i]);
318 static constexpr float N = 1.5f;
319 // beat if > med + N * variance
320 // N=1 : greater than 68.0% of values
321 // N=2 : greater than 95.4% of values
322 // N=3 : greater than 99.6% of values
323 // N=4 : greater than 99.8% of values
324 beats[i] = data[i] > (averageFft[i] + N * stdDev);
325 }
326
327 return beats;
328 }
329};
330
331namespace __private {
332inline static SoundEventTy<> sharedSoundEvent = SoundEventTy();
333}
334
335static SoundEventTy<>& get_shared_sound_event() { return __private::sharedSoundEvent; }
336
337} // namespace lampda::modes::audio
338
339#endif
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