ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
MicrophoneSimple_MAX9814.cpp
Go to the documentation of this file.
2
6
7MicrophoneSimple::MicrophoneSimple(uint8_t pin, float gain, float clipping) {
8 this->pin = pin;
9 this->gain = gain;
10
11 analogReadRes(12);
12 analogReadAveraging(32);
13
14 pinMode(pin, INPUT);
15
16 startMillis = millis();
17}
18
22
24 float read = analogRead(pin) * gain;
25 float change = read - previousReading;
26 float dT = ((float)millis() - (float)previousMillis) / 1000.0f;
27 float changeRate = change / dT;
28 float amplitude = mv.Filter(fabs(changeRate));
29 float minimum = minF.Filter(amplitude);
30 float normalized = Mathematics::Constrain(amplitude - minimum - 20000, 0.0f, 40000.0f);
31 float truncate = output.Filter(normalized / 100.0f / clipping);
32
33 previousReading = read;
34 previousMillis = millis();
35
36 currentValue = Mathematics::Constrain(truncate, 0.0f, 1.0f);
37
38 return currentValue;
39}
40
41float MicrophoneSimple::Update(float read) {
42 float change = read - previousReading;
43 float dT = ((float)millis() - (float)previousMillis) / 1000.0f;
44 float changeRate = change / dT;
45 float amplitude = mv.Filter(fabs(changeRate));
46 float minimum = minF.Filter(amplitude);
47 float normalized = Mathematics::Constrain(amplitude - minimum - 40000, 0.0f, 40000.0f);
48 float truncate = output.Filter(normalized / 100.0f / clipping);
49
50 previousReading = read;
51 previousMillis = millis();
52
53 currentValue = Mathematics::Constrain(truncate, 0.0f, 1.0f);
54
55 return currentValue;
56}
A simple microphone processing class for analog input.
static T Constrain(T value, T minimum, T maximum)
Constrains a value between minimum and maximum.
float previousReading
Previous reading from the microphone.
long previousMillis
Time of the previous reading.
long startMillis
Start time for processing.
float clipping
Clipping level for the microphone signal.
RunningAverageFilter< 5 > mv
Moving average filter for smoothing input.
MinFilter< 100 > minF
Minimum filter for peak detection.
float GetCurrentValue()
Retrieves the current processed microphone value.
float currentValue
Current processed microphone value.
float Update()
Updates the microphone reading and processes the signal.
uint8_t pin
Pin number for the microphone input.
float gain
Gain applied to the microphone signal.
RunningAverageFilter< 5 > output
Filter for smoothing the output.
MicrophoneSimple()
Constructs a default MicrophoneSimple instance.
float Filter(float value)
Filters the given value, updating the minimum value within the memory window.
float Filter(float value)
Filters the input value using the running average.