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 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 = false
 Did an event happened last tick? (reset each loop)
 
uint8_t eventScale = 0
 Event scale (0-255)
 
float maxAmplitude = 0
 max detected frequency amplitude
 
float maxAmplitudeFrequency = 0
 max amplitude frequency, in Hertz
 
FFTLogContainer fft_log = {}
 fast fourrier transform as a log scale (closer to human perception)
 
etl::bitset< _fftChannels > beatDetected
 set to true when the corresponding frequency range registers a beat. Always false without beat tracking
 

Static Public Attributes

static constexpr float _eventCutoff = eventCutoff / 100.0f
 Floor level for computations using AvgMax (lowest peak average)
 
static constexpr float _eventNorm = eventNorm / 100.0f
 Inverse scaling factor for eventScale
 
static constexpr float _windowSize = windowSize / 4.0f
 Window size used for avgLevel and avgMax
 
static constexpr float _windowShort = windowShort / 4.0f
 Window size used for avgDelta
 
static constexpr float _windowSizeInv = 1.0f / (_windowSize + 1.0f)
 Precomputed rolling average factors: multiplications are faster than divisions on the target.
 
static constexpr float _windowShortInv = 1.0f / (_windowShort + 1.0f)
 Precomputed rolling average factor of avgDelta
 
static constexpr float _eventScaleFactor = 256.0f / _eventNorm
 Precomputed eventScale factor.
 
static constexpr size_t _fftChannels = component::microphone::SoundStruct::numberOfFFtChanels
 number of channels in the log fft
 
static constexpr float fftResolutionHz = component::microphone::SoundStruct::get_fft_resolution_Hz()
 Frequency resolution of the raw fft result.
 
static constexpr size_t _FFThistory_MaxSize = ConfigTy::useBeatTracking ? round(fftResolutionHz) : 0
 Size of the fourrier transform history. Not allocated without beat tracking.
 

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:96
void reset(auto &ctx)
Call this once inside the mode on_enter_mode callback.
Definition: utils.hpp:128

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 raw and auto gained samples, and the raw FFT, are not copied here: read them from ctx.lamp.get_sound_struct() if a mode needs them.


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