-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRampGenerator.cpp
194 lines (147 loc) · 4.73 KB
/
RampGenerator.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*** RampGenerator.cpp ***/
/* RampGenerator.cpp */
#include "RampGenerator.h"
RampGenerator::RampGenerator() {
numReadings = 20;
thresholdToTrigger = 0.005;
amountBelowPeak = 0.0001;
rolloffRate = 0.0004;
/* Average */
index = 0; // index of the current reading
avrTotal = 0; // running total
average = 0; // the average
for(int i=0;i<50;i++)
readings[i] = 0;
/* -------------------- */
/* Onset Detection */
previousSample = 0; // Value of the sensor the last time around
peakValue = 0;
triggered = 0;
startPlaying = 0;
/* -------------------- */
/* Envelope Trigger */
line = 0.0;
sampleCount = 0;
attack = 20000;
/* -------------------- */
/* Debounce */
debounceSampleCount = 0; // Sample counter for avoiding double onsets
debounceThresh = 1500; //1500 Duration in samples of window in which a second onset can't be triggered (equates to a trill of ~22Hz)
onsetFinished = 1;
debounce = 0;
/* -------------------- */
// DC OFFSET FILTER
prevReadingDCOffset = 0;
prevPiezoReading = 0;
readingDCOffset = 0;
R = 0.99;//1 - (250/44100);
//float piezoAmp;
// DEBUG
//float peakValueDebug;
}
RampGenerator::~RampGenerator() {
}
void RampGenerator::config(int numReadings, float thresholdToTrigger, float amountBelowPeak, float rolloffRate, float readings[], int index, float avrTotal, float average, int previousSample, float peakValue, int triggered, float line, float amps, int ADSRStates, int sampleCount, int attack, int debounceSampleCount, int debounceThresh, int onsetFinished, int debounce, int startPlaying) {
this->numReadings = numReadings;
this->thresholdToTrigger = thresholdToTrigger;
this->amountBelowPeak = amountBelowPeak;
this->rolloffRate = rolloffRate;
//this->readings[4] = readings[4];
this->index = index;
this->avrTotal = avrTotal;
this->average = average;
this->previousSample = previousSample;
this->peakValue = peakValue;
this->triggered = triggered;
this->line = line;
this->amps = amps;
this->ADSRStates = ADSRStates;
this->sampleCount = sampleCount;
this->attack = attack;
this->debounceSampleCount = debounceSampleCount;
this->debounceThresh = debounceThresh;
this->onsetFinished = onsetFinished;
this->debounce = debounce;
this->peakValueDebug = peakValueDebug;
// ...
}
float RampGenerator::processRamp(float in, float thresholdToTrigger, float amountBelowPeak, float rolloffRate) {
float currentSample;
float smoothedReading;
// DC Offset Filter y[n] = x[n] - x[n-1] + R * y[n-1]
readingDCOffset = in - prevPiezoReading + (R * prevReadingDCOffset);
prevPiezoReading = in;
prevReadingDCOffset = readingDCOffset;
/* -------------------------------- */
// Consider a first order high pass filter with cutoff of 50Hz.
currentSample = readingDCOffset;
// CRUDE DC OFFSET
// currentSample = (in - 0.4123);
// FULL WAVE RECTIFY
if(currentSample < 0.0)
currentSample *= -1.0;
// MOVING AVERAGE SMOOTHING
avrTotal = avrTotal - readings[(index+numReadings)%numReadings];
readings[index] = currentSample;
avrTotal = avrTotal + readings[index];
index = index + 1;
if (index >= numReadings)
index = 0;
// Calculate the average
average = avrTotal / numReadings;
smoothedReading = average;
// DEBOUNCE
if(debounce)
{
debounceSampleCount++;
}
if(debounceSampleCount > debounceThresh){
onsetFinished = 1;
debounce = 0;
debounceSampleCount = 0;
//rt_printf("End of debounce\n");
}
startPlaying = 0;
piezoAmp = 0;
// ONSET DETECTION
if(smoothedReading >= peakValue) // Record the highest incoming sample
{
peakValue = smoothedReading;
triggered = 0;
//startPlaying = 0;
}
else if(peakValue >= thresholdToTrigger) // But have the peak value decay over time
peakValue -= rolloffRate; // so we can catch the next peak later
if(smoothedReading < peakValue - amountBelowPeak && peakValue >= thresholdToTrigger && !triggered)
{
triggered = 1; // Indicate that we've triggered and wait for the next peak before triggering again.
if(onsetFinished) // DEBOUNCE
{
ADSRStates = kStateAttack;
piezoAmp = peakValue;
sampleCount = 0;
onsetFinished = 0;
debounce = 1;
startPlaying = triggered;
}
}
// RAMP GENERATOR
// if(ADSRStates == kStateOff)
// {
// amps = 0.0;
// }
// if(ADSRStates == kStateAttack)
// {
// amps = (1.0 + (0.0 - 1.0) * ((float)sampleCount/attack))*(piezoAmp*2);
// if(sampleCount >= attack)
// {
// ADSRStates = kStateOff;
// sampleCount = 0;
// amps = 0.0;
// }
// }
// sampleCount++;
return piezoAmp;
//return amps;
//return currentSample;
}