|
Lamp-Da 0.1
A compact lantern project
|
Sound processor able to detect sound level events. More...
#include <utils.hpp>
Public Types | |
| using | FFTContainer = std::array< float, _dataLenght/2 > |
| Container for the fast fourrier linear results. | |
| using | FFTLogContainer = std::array< float, _fftChannels > |
| Container for the fast fourrier logarithmic results. | |
| using | FFTHistoryContainer = std::array< FFTLogContainer, _FFThistory_MaxSize > |
| history of the FFT | |
Public Attributes | |
| float | level = 10 |
| Last sound level measured. | |
| float | avgLevel = 10 |
| Rolling sound level average. | |
| float | avgMax = 10 |
| Decaying sound "peak" level average. | |
| float | delta = 0 |
| Last sound "contrast" computed (centered around 1.0) | |
| float | avgDelta = 0 |
| Rolling sound "contrast" average (squared) | |
| bool | hasEvent |
| Did an event happened last tick? (reset each loop) | |
| uint8_t | eventScale |
| Event scale (0-255) | |
| float | maxAmplitude |
| max detected frequency amplitude | |
| float | maxAmplitudeFrequency |
| max amplitude frequency, in Hertz | |
| std::array< int16_t, _dataLenght > | data |
| raw microphone data | |
| std::array< int16_t, _dataLenght > | dataAutoGained |
| dynamically sound adjusted data | |
| FFTLogContainer | fft_log |
| fast fourrier transform as a log scale (closer to human perception) | |
| std::array< bool, _fftChannels > | beatDetected |
| set to true when the corresponding frequency range registers a beat | |
| std::array< float, _dataLenght/2 > | fft_raw |
| fast fourrier transform raw results | |
Static Public Attributes | |
| static constexpr float | _eventCutoff = eventCutoff / 100.0 |
Floor level for computations using AvgMax (lowest peak average) | |
| static constexpr float | _eventNorm = eventNorm / 100.0 |
Inverse scaling factor for eventScale | |
| static constexpr float | _windowSize = windowSize / 4.0 |
Window size used for avgLevel and avgMax | |
| static constexpr float | _windowShort = windowShort / 4.0 |
Window size used for avgDelta | |
| static constexpr size_t | _dataLenght = physical::microphone::SoundStruct::SAMPLE_SIZE |
| number of sample in a microphone run | |
| static constexpr int16_t | _autoGainTargetValue = physical::microphone::gainedSignalTarget |
| target value reached by the auto gain | |
| static constexpr size_t | _fftChannels = physical::microphone::SoundStruct::numberOfFFtChanels |
| number of channels in the log fft | |
| static constexpr float | fftResolutionHz = physical::microphone::SoundStruct::get_fft_resolution_Hz() |
| Frequency resolution of the raw fft result. | |
| static constexpr size_t | _FFThistory_MaxSize = round(fftResolutionHz) |
| Size of the fourrier transform history. | |
Sound processor able to detect sound level events.
For example, it can be used as follows:
Every time this class is updated, it will:
ctx.lamp.get_sound_level()avgLevel a sound level "rolling" averageavgMax a peak sound level "decaying" averagedelta "contrast" between measured level and its averageavgDelta an adjusted "rolling" average of that contrasthasEvent and eventScale accordinglyYou can use avgDelta as-is to build modes reactive to the surrounding sounds while being able to somewhat ignore noisy environments.
You can configure eventCutoff (in centibels) as the minimum sound level required to trigger a delta contrast of 1.0 (the larger the cut-off, the larger the required difference against the average level).
The hasEvent boolean is triggered if avgDelta is above 1.0 for at least nbEventSample ticks. The eventScale integer is proportional to the largest avgMax * delta value during this period, scaled down by eventNorm (in centibels).
The windowSize (in quarter-ticks) is used for avgLevel and avgMax (the larger the window, the less reactive the average) and windowShort is used for avgDelta (shorter to be more reactive).
Note that the algorithm implemented is simple and produces false positives.