-
Notifications
You must be signed in to change notification settings - Fork 0
/
bufferManager.cpp
79 lines (63 loc) · 1.84 KB
/
bufferManager.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
71
72
73
74
75
76
77
78
79
/*
*/
#include <Arduino.h>
#include "bufferManager.h"
// Constructor
BufferManager::BufferManager() {};
BufferManager::BufferManager(float *vReal, float *weight, short *vShort, unsigned short samples, unsigned short packingNum) {
this->_vReal = vReal;
this->_weight = weight;
this->_vShort = vShort;
this->_samples = samples;
this->_packingNum = packingNum;
this->_nextSample = 0;
this->_sampleSum = 0;
this->_DCBias = 0;
this->_packCount = 0;
this->_packedValue = 0;
};
// Add the new sample to the circular buffer
void BufferManager::addSample(short value) {
// Accumulate the new value
_packedValue += value;
_packCount++;
// look to see if we have a new packed value
if (_packCount == _packingNum) {
value = _packedValue / _packingNum;
/*
if (_packingNum > 1 ) {
Serial.print("400 -400 ");
Serial.println(value - _DCBias);
}
*/
//Serial.print("P");
// put new averaged value into the head of the list and update sum.
_sampleSum -= _vShort[_nextSample];
_vShort[_nextSample] = value;
_sampleSum += _vShort[_nextSample];
_nextSample++ ;
// wrap the pointer at the end of the buffer.
if (_nextSample == _samples) {
_nextSample = 0;
}
// reset the packing counter.
_packedValue = 0;
_packCount = 0;
} else {
//Serial.print(".");
}
}
// Transfer from the circular buffer. Remove the DC Bias and apply weight
void BufferManager::transfer(float inputScale){
unsigned short fromIndex = _nextSample;
unsigned short toIndex = 0;
float audioVal;
_DCBias = _sampleSum / _samples;
while (toIndex < _samples) {
audioVal = ((float)(_vShort[fromIndex++] - _DCBias) * _weight[toIndex]) * inputScale;
_vReal[toIndex++] = audioVal;
if (fromIndex >= _samples) {
fromIndex = 0;
}
}
}