Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Public Attributes | Static Public Attributes | List of all members
lampda::modes::audio::SoundEventTy< ConfigTy, eventCutoff, eventNorm, nbEventSample, windowSize, windowShort > Struct Template Reference

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 Member Functions

void reset (auto &ctx)
 Call this once inside the mode on_enter_mode callback.
 
void update (auto &ctx)
 Call this once every tick inside the mode loop callback.
 
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.
 

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, _dataLenghtdata
 raw microphone data
 
std::array< int16_t, _dataLenghtdataAutoGained
 dynamically sound adjusted data
 
FFTLogContainer fft_log
 fast fourrier transform as a log scale (closer to human perception)
 
std::array< bool, _fftChannelsbeatDetected
 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.
 

Detailed Description

template<typename ConfigTy = MicrophoneConfig, int eventCutoff = 500, int eventNorm = 500, int nbEventSample = 10, int windowSize = 100, int windowShort = 64>
struct lampda::modes::audio::SoundEventTy< ConfigTy, eventCutoff, eventNorm, nbEventSample, windowSize, windowShort >

Sound processor able to detect sound level events.

For example, it can be used as follows:

struct StateTy
{
};
static void on_enter_mode(auto& ctx) {
ctx.state.soundEvent.reset(ctx);
}
static void loop(auto& ctx) {
auto& snd = ctx.state.soundEvent;
// update SoundEvent internal variables
snd.update(ctx);
// ...
// code that uses avgDelta / hasEvent / eventScale / etc
// ...
}
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

Every time this class is updated, it will:

You 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.


The documentation for this struct was generated from the following file: