-
Notifications
You must be signed in to change notification settings - Fork 4
/
envelope_detector.cpp
70 lines (57 loc) · 1.4 KB
/
envelope_detector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* EasyListener for Teensy Audio Library
*
* (c) 2020 Clemens Wegener
* Bauhaus-Universität Weimar
* Department Interface Design
*
* This library uses machine learning to detect previously
* presented audio signals on the Teensy board.
*
* BSD license
*/
#include "envelope_detector.h"
#include <stdlib.h>
void EnvelopeDetector::process(const int16_t *data, uint16_t frame_num)
{
if( !data) return;
// detect peak of buffer
for (int i=0; i < AUDIO_BLOCK_SAMPLES; i++)
{
_envelope = abs(data[i]);
if(_envelope > _peak){
_peak = _envelope;
}
}
// detect if event is "ongoing"
if (_peak > _threshold){
_threshold_crossed = true;
}
else _threshold_crossed = false;
_peak = _peak * _decay;
_frame_num = frame_num;
_new_env_available = true;
_new_threshold_crossed_available = true;
if (_new_sound_detected) return;
// detect if event is valid
if (_threshold_crossed) _block_count++;
if(_block_count > _min_number_of_blocks)
{
_sound_is_long_enough = true;
} else
{
_sound_is_long_enough = false;
}
if(!_threshold_crossed && _sound_is_long_enough)
{
_new_sound_detected = true;
}
// reset
if (!_threshold_crossed) _block_count = 0;
}
void EnvelopeDetector::setDecayRate(float decay){
_decay = decay;
}
void EnvelopeDetector::setThreshold(float threshold){
_threshold = dbToa(threshold)*(float)32767.0;
}