forked from pixelmatix/aurora
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadAudio.h
More file actions
357 lines (274 loc) · 7.49 KB
/
readAudio.h
File metadata and controls
357 lines (274 loc) · 7.49 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*************************************************************
audio.h - reads the audio level for each of 7 eq bands from
a MSGEQ07 IC. Thre is an AGC that adjusts the audio gain to
conpensate for different volume levels. See the global defs
for the various values returned.
Returns either 7, 8, or 16 EQ band values
MSGEQ07 Timing Requirements:
tr - Reset Pulse Width: 100 nS min
trs - Reset to Strobe: 72 uS min
ts - Strobe Pulse Width: 18 uS min
tss - Strobe to Strobe: 72 uS min
to - Output Settling Time: 36 uS min
MSGEQ07 band centers:
1: 63 Hz
2: 160 Hz
3: 400 Hz
4: 1 kHz
5: 2.5 kHz
6: 6.25 kHz
7: 16 kHz
version 3.5 Jun2022 tbitson
************************************************/
#pragma once
#define MAX_AUDIO 1023
//#define CAL_MODE
// prototypes
void initAudio();
void readAudio();
void getAudioData();
void adjustGain();
void calcAvg();
void findPeaks();
void interpolate();
void printAudioValues();
// ----------------------------------------------
// adjust these to your preference
const bool useLogScale = false;
const float maxGain = 16.0;
const float minGain = 0.1;
const uint16_t lowThreshold = 450;
const uint16_t highThreshold = 630;
const int peakDecay = 7;
const float minThreashold = 30;
const float avgFactor = 0.95;
bool usePeaks = true;
// ----------------------------------------------
// constants
// The MSGEQ7 ic uses has 7 internal EQ bands (0 -> 6) which
// are read using teensy's 10-Bit (0-1023) A to D
// which are interpolated into 16 bands
const uint8_t EQ_BANDS7 = 7;
const uint8_t EQ_BANDS8 = 8;
const uint8_t EQ_BANDS16 = 16;
// global results from chip
float rawAudio[EQ_BANDS7]; // audio levels for the 7 eq bands UNSCALED
float audio[EQ_BANDS7]; // audio levels for the 7 eq bands scaled
float peaks[EQ_BANDS7]; // peak audio values for each of the 7eq band
float audio16[EQ_BANDS16]; // audio levels for the 16 interpolated bands
// global results (calculated)
float maxLevel = 0; // max level in all 7 bands
uint8_t maxBand = 0; // band that the max level is in
float pkLevel = 0; // max peak level in all 7 bands
uint8_t pkBand = 0; // band that the max peak level is in
float maxRaw = 0; // max of the 7 raw audio level. No gain applied
float avgLevel = 0; // avg audio level for all 7 bands
float avgBand = 0; // running avg of maxBand
// adjusts how much gain is needed to keep display in range
// when using linear scale (< 1.0 decreases gain). It is adjusted
// dynamically based on input level. There are a couple of settings above
float gain;
// values subtracted for each band to minimize background noise
// should be tailored for each installation - see hardware.h
float background[EQ_BANDS7] = BACKGROUND_OFFSET;
// enabled from serial to show audio data
bool audioDebug = false;
void initAudio()
{
// setup & init the MSGEQ7
pinMode(MSGEQ7_RESET_PIN, OUTPUT);
pinMode(MSGEQ7_STROBE_PIN, OUTPUT);
digitalWrite(MSGEQ7_RESET_PIN, LOW);
digitalWrite(MSGEQ7_STROBE_PIN, HIGH);
delay(500);
// starting gain
if (useLogScale)
{
gain = 1.0;
Serial.println("using Log Audio Scale");
}
else
{
gain = 4.0;
Serial.println("using Linear Audio Scale");
}
}
void readAudio()
{
getAudioData();
adjustGain();
calcAvg();
findPeaks();
interpolate();
if (printLevel > 2)
printAudioValues();
}
void getAudioData()
{
float value;
// reset MSEG07 for data read, 100ns min
digitalWrite(MSGEQ7_RESET_PIN, HIGH);
delayMicroseconds(20);
digitalWrite(MSGEQ7_RESET_PIN, LOW);
delayMicroseconds(10);
maxLevel = 0;
maxRaw = 0;
maxBand = 0;
// read the MSGEQ7 value for all 7 EQ bands (0 - 6)
for (uint8_t band = 0; band < EQ_BANDS7; band++)
{
digitalWrite(MSGEQ7_STROBE_PIN, LOW);
delayMicroseconds(15);
// read audio band votage
value = (float)analogRead(MSGEQ7_AUDIO_PIN);
if (simAudio)
{
// sim data for 8 seconds then 2 seconds of silence
if (millis() % 10000 < 2000)
value = 0;
else
value = (float)random(100, 700);
}
// save raw value
rawAudio[band] = value;
// find max raw value
if (value > maxRaw)
maxRaw = value;
// calibration test mode - display raw
if (testMode)
{
Serial.print(" Band["); Serial.print(band); Serial.print("] = "); Serial.print(value);
if (band == 6)
{
Serial.println();
delay(50);
}
}
// remove background noise
value -= background[band];
// check if below minimum threshold (removes negative values)
if (value < minThreashold)
{
value = 0;
}
else
{
// scale it
if (useLogScale)
{
if (value > 0)
value = log(value * gain / 2) * 100.0;
}
else
{
// linear scaling
value = value * gain;
}
// bound it
value = constrain(value, 0, 1000);
// find max value and it's band
if (value > maxLevel)
{
maxLevel = value;
maxBand = band;
}
}
// save it to array
audio[band] = value;
if (audioDebug)
printAudioValues();
// toggle msgeq7 to next band
digitalWrite(MSGEQ7_STROBE_PIN, HIGH);
delayMicroseconds(10);
}
}
void adjustGain()
{
// adjust audio gain based on max level detected
if (maxLevel < lowThreshold && gain < maxGain)
gain += 0.05;
else if (maxLevel > highThreshold && gain > minGain)
gain -= 0.20;
}
void calcAvg()
{
// first, std avg all 7 bands
float sum = 0;
for (uint8_t band = 0; band < EQ_BANDS7; band++)
sum += audio[band];
float newAvg = sum / (float)EQ_BANDS7;
// then calc running average for level and and eq band
avgLevel = avgLevel * avgFactor + (1.0 - avgFactor) * newAvg;
//printValue("avgLevel", avgLevel);
avgBand = avgBand * avgFactor + (1.0 - avgFactor) * (float)maxBand;
//printValue("avgBand", avgBand);
}
void findPeaks()
{
float value;
pkLevel = 0;
// check for peak for each hw band
for (uint8_t band = 0; band < EQ_BANDS7; band++)
{
value = audio[band];
// find peak of all bands
if (value > pkLevel)
{
pkLevel = value;
pkBand = band;
}
// find peak in this band
if (value > peaks[band])
peaks[band] = value;
else
{
peaks[band] = peaks[band] - peakDecay;
if (peaks[band] < 0)
peaks[band] = 0;
}
}
}
// interpolate 7 bands into 16
void interpolate()
{
float step = (1.0 * (EQ_BANDS7 - 1)) / (EQ_BANDS16 - 1);
for (int i = 0; i < EQ_BANDS16; i++)
{
float v = i * step;
int x = v; // x = band
v = v - x; // percentage of next band
if (i < EQ_BANDS16 - 1)
{
if (usePeaks)
audio16[i] = (1.0 - v) * peaks[x] + v * peaks[x + 1];
else
audio16[i] = (1.0 - v) * audio[x] + v * audio[x + 1];
}
else
{
if (usePeaks)
audio16[i] = peaks[x];
else
audio16[i] = audio[x];
}
}
}
void printAudioValues()
{
// slow down output
if (ticks % 20 == 0)
{
for (uint8_t band = 0; band < EQ_BANDS7; band++)
{
Serial.print(audio[band]);
Serial.print("\t");
}
printValue();
printValue("gain", gain);
printValue("maxLevel", maxLevel);
printValue("maxBand", maxBand);
printValue("maxRaw", maxRaw);
printValue("avgLevel", avgLevel);
printValue();
}
}