-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathAudioLogic.h
289 lines (234 loc) · 8.39 KB
/
AudioLogic.h
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
/*
* Aurora: https://github.com/pixelmatix/aurora
* Copyright (c) 2014 Jason Coon
*
* Portions of this code are adapted from "Funky Clouds" by Stefan Petrick: https://gist.github.com/anonymous/876f908333cd95315c35
* Copyright (c) 2014 Stefan Petrick
* http://www.stefan-petrick.de/wordpress_beta
*
* Portions of this code are adapted from "Audio Spectrum Display" by Tony DiCola https://github.com/tdicola/adafruit_guide_fft
* Copyright (c) 2013 Tony DiCola
*
* Portions of this code (octave remapping) are adapted from "Spectrum Analyzer" https://github.com/Vipor26/Spectrum-Analyzer/
* Copyright (c) 2014 Brian Hamilton
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef AudioLogic_H
#define AudioLogic_H
// MSGEQ7 wiring
#define AUDIO_LEFT_PIN A14
#define MSGEQ7_STROBE_PIN 1
#define MSGEQ7_RESET_PIN 0
const int bandCount = 7;
// the 10Bit (0-1023) audio band values
int levels[bandCount];
int peaks[bandCount];
static const int defaultPeakDecay = (1024 / MATRIX_HEIGHT) / 4;
int peakDecay = defaultPeakDecay;
bool drawPeaks = true;
int correction[bandCount] = {
// -64, -76, -115, -120, -106, -116, -141,
0, 0, 0, 0, 0, 0, 0,
};
const uint8_t interpolatedBandCount = 16;
int interpolatedLevels[interpolatedBandCount];
int horizontalPixelsPerBand = MATRIX_WIDTH / interpolatedBandCount;
int levelsPerVerticalPixel = 1024 / MATRIX_HEIGHT;
// AGC scale adjustment parameters
// adjust these thresholds to control at what level the scale is adjusted
uint16_t lowThreshold = 512;
uint16_t highThreshold = 1025;
// adjust these timeouts (in ms) to control how long the thresholds above have
// to be crossed before making any adjustments
uint16_t lowTimout = 1000;
uint16_t highTimout = 1;
// these timers keep track of how long the thresholds have been crossed
uint32_t lowTimer = 0;
uint32_t highTimer = 0;
// audio scale steps, likely don't need the higher values, but keeping them for testing for now
const uint8_t audioScaleCount = 12;
const uint8_t minAudioLevel = 192;
boolean hasAudio = false;
uint8_t audioScaleMap[audioScaleCount] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
// defines where the AGC level starts (I'm having it start high for now)
uint8_t autoAudioScale = audioScaleMap[audioScaleCount - 1];
// The audioScale sets how much sound is needed in each frequency range to
// show full height. Higher numbers are more sensitive.
uint8_t audioScale = 0;
bool audioScaleChanged = false;
bool showingAudioScaleIndicator = false;
unsigned int audioScaleIndicatorTimout = 0;
unsigned int audioScaleIndicatorDuration = 1000;
int getAudioScaleLevel(float audioScale) {
int level = 0;
for (int i = 0; i < audioScaleCount; i++) {
if (audioScaleMap[i] >= audioScale) {
level = i;
break;
}
}
return level;
}
float boundAudioScale(float audioScale) {
if (audioScale < 0)
audioScale = 255;
else if (audioScale > 255)
audioScale = 0;
return audioScale;
}
void boundAudioScale() {
audioScale = boundAudioScale(audioScale);
}
float adjustAudioScale(float audioScale, int delta) {
int level = getAudioScaleLevel(audioScale);
level += delta;
if (level < 0)
level = audioScaleCount - 1;
if (level >= audioScaleCount)
level = 0;
audioScale = audioScaleMap[level];
audioScale = boundAudioScale(audioScale);
return audioScale;
}
void saveAudioScaleSetting() {
saveByteSetting(audiosclFilename, audioScale);
}
void adjustAudioScale(int delta) {
audioScale = adjustAudioScale(audioScale, delta);
saveAudioScaleSetting();
}
void InitAudio() {
// wake up the MSGEQ7
pinMode(MSGEQ7_RESET_PIN, OUTPUT);
pinMode(MSGEQ7_STROBE_PIN, OUTPUT);
digitalWrite(MSGEQ7_RESET_PIN, LOW);
digitalWrite(MSGEQ7_STROBE_PIN, HIGH);
}
// get the data from the MSGEQ7
void ReadAudio() {
hasAudio = false;
digitalWrite(MSGEQ7_RESET_PIN, HIGH);
digitalWrite(MSGEQ7_RESET_PIN, LOW);
int max = 0;
for (byte band = 0; band < bandCount; band++) {
digitalWrite(MSGEQ7_STROBE_PIN, LOW);
delayMicroseconds(20);
int level = analogRead(AUDIO_LEFT_PIN) + correction[band];
if (level < 0) level = 0;
levels[band] = level;
if (level > max) {
max = level;
}
digitalWrite(MSGEQ7_STROBE_PIN, HIGH);
}
float tempScale = audioScale;
if (audioScale <= 0) {
int maxLevel = (int) (max * autoAudioScale);
//Serial.println(maxLevel);
// is the max level too high and is there room to adjust down??
if (maxLevel >= highThreshold && autoAudioScale >= audioScaleMap[2]) {
uint32_t now = millis();
if (highTimer == 0) {
// start the high timer
highTimer = now;
//Serial.println(F("-----------------------"));
}
else if (now > highTimout + highTimer) {
// it's been high long enough, scale down
autoAudioScale = adjustAudioScale(autoAudioScale, -1);
// reset the timers
highTimer = 0;
lowTimer = 0;
//Serial.println(F("-----------------------"));
//Serial.println(F("decreased scale"));
// // show an indicator of the auto scale change, for now, while testing
// audioScaleChanged = true;
// showingAudioScaleIndicator = true;
// audioScaleIndicatorTimout = millis() + audioScaleIndicatorDuration;
}
else {
// it hasn't been high long enough, do nothing
//Serial.print(maxLevel);
//Serial.print(F(" too high for "));
//Serial.print(now - highTimer);
//Serial.println(F("ms"));
}
}
// is the max level too low and is there room to adjust up??
else if (maxLevel <= lowThreshold && autoAudioScale < audioScaleMap[audioScaleCount - 1]) {
uint32_t now = millis();
if (lowTimer == 0) {
// start the low timer
lowTimer = now;
//Serial.println(F("-----------------------"));
}
else if (now > lowTimout + lowTimer) {
// it's been low long enough, scale up
autoAudioScale = adjustAudioScale(autoAudioScale, 1);
//Serial.println(F("-----------------------"));
//Serial.println(F("increased scale"));
// reset the timers
lowTimer = 0;
highTimer = 0;
// show an indicator of the auto scale change, for now, while testing
// audioScaleChanged = true;
// showingAudioScaleIndicator = true;
// audioScaleIndicatorTimout = millis() + audioScaleIndicatorDuration;
}
else {
//Serial.print(maxLevel);
//Serial.print(F(" too low for "));
//Serial.print(now - lowTimer);
//Serial.println(F("ms"));
}
}
else {
lowTimer = 0;
highTimer = 0;
}
tempScale = autoAudioScale;
}
for (byte band = 0; band < bandCount; band++) {
int level = levels[band] * tempScale;
if (level > 1023) {
level = 1023;
}
else if(level < 0) level = 0;
levels[band] = level;
if (level >= peaks[band]) {
peaks[band] = level;
}
else if (peaks[band] > 0) {
peaks[band] = peaks[band] - peakDecay;
if(peaks[band] < 0) peaks[band] = 0;
}
if (level > minAudioLevel)
hasAudio = true;
}
float step = (1.0 * (bandCount - 1)) / (interpolatedBandCount - 1);
for (int i = 0; i < interpolatedBandCount; i++)
{
float v = i * step;
int x = v;
v = v - x;
if (i < interpolatedBandCount - 1) interpolatedLevels[i] = (1 - v) * peaks[x] + v * peaks[x + 1];
else interpolatedLevels[i] = peaks[x]; // prevent out of index error
}
}
#endif