-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathAudio.h
385 lines (297 loc) · 10.3 KB
/
Audio.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
ESP8266 + FastLED + IR Remote: https://github.com/jasoncoon/esp8266-fastled-audio
Copyright (C) 2015-2016 Jason Coon
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Portions of this file are adapted from the work of Stefan Petrick:
// https://plus.google.com/u/0/115124694226931502095
// Portions of this file are adapted from RGB Shades Audio Demo Code by Garrett Mace:
// https://github.com/macetech/RGBShadesAudio
// Pin definitions
#define MSGEQ7_AUDIO_PIN A0
#define MSGEQ7_STROBE_PIN D0
#define MSGEQ7_RESET_PIN D3
#define AUDIODELAY 0
// Smooth/average settings
#define SPECTRUMSMOOTH 0.08
#define PEAKDECAY 0.01
#define NOISEFLOOR 65
// AGC settings
#define AGCSMOOTH 0.004
#define GAINUPPERLIMIT 15.0
#define GAINLOWERLIMIT 0.1
// Global variables
unsigned int spectrumValue[7]; // holds raw adc values
float spectrumDecay[7] = {0}; // holds time-averaged values
float spectrumPeaks[7] = {0}; // holds peak values
float audioAvg = 270.0;
float gainAGC = 0.0;
uint8_t spectrumByte[7]; // holds 8-bit adjusted adc values
uint8_t spectrumAvg;
unsigned long currentMillis; // store current loop's millis value
unsigned long audioMillis; // store time of last audio update
void initializeAudio() {
pinMode(MSGEQ7_AUDIO_PIN, INPUT);
pinMode(MSGEQ7_RESET_PIN, OUTPUT);
pinMode(MSGEQ7_STROBE_PIN, OUTPUT);
digitalWrite(MSGEQ7_RESET_PIN, LOW);
digitalWrite(MSGEQ7_STROBE_PIN, HIGH);
}
void readAudio() {
static PROGMEM const byte spectrumFactors[7] = {9, 11, 13, 13, 12, 12, 13};
// reset MSGEQ7 to first frequency bin
digitalWrite(MSGEQ7_RESET_PIN, HIGH);
delayMicroseconds(5);
digitalWrite(MSGEQ7_RESET_PIN, LOW);
// store sum of values for AGC
int analogsum = 0;
// cycle through each MSGEQ7 bin and read the analog values
for (int i = 0; i < 7; i++) {
// set up the MSGEQ7
digitalWrite(MSGEQ7_STROBE_PIN, LOW);
delayMicroseconds(50); // to allow the output to settle
// read the analog value
spectrumValue[i] = analogRead(MSGEQ7_AUDIO_PIN);
digitalWrite(MSGEQ7_STROBE_PIN, HIGH);
// noise floor filter
if (spectrumValue[i] < NOISEFLOOR) {
spectrumValue[i] = 0;
} else {
spectrumValue[i] -= NOISEFLOOR;
}
// apply correction factor per frequency bin
spectrumValue[i] = (spectrumValue[i] * pgm_read_byte_near(spectrumFactors + i)) / 10;
// prepare average for AGC
analogsum += spectrumValue[i];
// apply current gain value
spectrumValue[i] *= gainAGC;
// process time-averaged values
spectrumDecay[i] = (1.0 - SPECTRUMSMOOTH) * spectrumDecay[i] + SPECTRUMSMOOTH * spectrumValue[i];
// process peak values
if (spectrumPeaks[i] < spectrumDecay[i]) spectrumPeaks[i] = spectrumDecay[i];
spectrumPeaks[i] = spectrumPeaks[i] * (1.0 - PEAKDECAY);
spectrumByte[i] = spectrumValue[i] / 4;
}
// Calculate audio levels for automatic gain
audioAvg = (1.0 - AGCSMOOTH) * audioAvg + AGCSMOOTH * (analogsum / 7.0);
spectrumAvg = (analogsum / 7.0) / 4;
// Calculate gain adjustment factor
gainAGC = 270.0 / audioAvg;
if (gainAGC > GAINUPPERLIMIT) gainAGC = GAINUPPERLIMIT;
if (gainAGC < GAINLOWERLIMIT) gainAGC = GAINLOWERLIMIT;
}
// Attempt at beat detection
byte beatTriggered = 0;
#define beatLevel 20.0
#define beatDeadzone 30.0
#define beatDelay 50
float lastBeatVal = 0;
byte beatDetect() {
static float beatAvg = 0;
static unsigned long lastBeatMillis;
float specCombo = (spectrumDecay[0] + spectrumDecay[1]) / 2.0;
beatAvg = (1.0 - AGCSMOOTH) * beatAvg + AGCSMOOTH * specCombo;
if (lastBeatVal < beatAvg) lastBeatVal = beatAvg;
if ((specCombo - beatAvg) > beatLevel && beatTriggered == 0 && currentMillis - lastBeatMillis > beatDelay) {
beatTriggered = 1;
lastBeatVal = specCombo;
lastBeatMillis = currentMillis;
return 1;
} else if ((lastBeatVal - specCombo) > beatDeadzone) {
beatTriggered = 0;
return 0;
} else {
return 0;
}
}
void fade_down(uint8_t value) {
for (int i = 0; i < NUM_LEDS; i++)
{
leds[i].fadeToBlackBy(value);
}
}
void spectrumPaletteWaves()
{
// fade_down(1);
CRGB color6 = ColorFromPalette(gCurrentPalette, spectrumByte[6], spectrumByte[6]);
CRGB color5 = ColorFromPalette(gCurrentPalette, spectrumByte[5] / 8, spectrumByte[5] / 8);
CRGB color1 = ColorFromPalette(gCurrentPalette, spectrumByte[1] / 2, spectrumByte[1] / 2);
CRGB color = nblend(color6, color5, 256 / 8);
color = nblend(color, color1, 256 / 2);
leds[CENTER_LED] = color;
leds[CENTER_LED].fadeToBlackBy(spectrumByte[3] / 12);
leds[CENTER_LED - 1] = color;
leds[CENTER_LED - 1].fadeToBlackBy(spectrumByte[3] / 12);
//move to the left
for (int i = NUM_LEDS - 1; i > CENTER_LED; i--) {
leds[i] = leds[i - 1];
}
// move to the right
for (int i = 0; i < CENTER_LED; i++) {
leds[i] = leds[i + 1];
}
}
void spectrumPaletteWaves2()
{
// fade_down(1);
CRGBPalette16 palette = palettes[currentPaletteIndex];
CRGB color6 = ColorFromPalette(palette, 255 - spectrumByte[6], spectrumByte[6]);
CRGB color5 = ColorFromPalette(palette, 255 - spectrumByte[5] / 8, spectrumByte[5] / 8);
CRGB color1 = ColorFromPalette(palette, 255 - spectrumByte[1] / 2, spectrumByte[1] / 2);
CRGB color = nblend(color6, color5, 256 / 8);
color = nblend(color, color1, 256 / 2);
leds[CENTER_LED] = color;
leds[CENTER_LED].fadeToBlackBy(spectrumByte[3] / 12);
leds[CENTER_LED - 1] = color;
leds[CENTER_LED - 1].fadeToBlackBy(spectrumByte[3] / 12);
//move to the left
for (int i = NUM_LEDS - 1; i > CENTER_LED; i--) {
leds[i] = leds[i - 1];
}
// move to the right
for (int i = 0; i < CENTER_LED; i++) {
leds[i] = leds[i + 1];
}
}
void spectrumWaves()
{
fade_down(2);
CRGB color = CRGB(spectrumByte[6], spectrumByte[5] / 8, spectrumByte[1] / 2);
leds[CENTER_LED] = color;
leds[CENTER_LED].fadeToBlackBy(spectrumByte[3] / 12);
leds[CENTER_LED - 1] = color;
leds[CENTER_LED - 1].fadeToBlackBy(spectrumByte[3] / 12);
//move to the left
for (int i = NUM_LEDS - 1; i > CENTER_LED; i--) {
leds[i] = leds[i - 1];
}
// move to the right
for (int i = 0; i < CENTER_LED; i++) {
leds[i] = leds[i + 1];
}
}
void spectrumWaves2()
{
fade_down(2);
CRGB color = CRGB(spectrumByte[5] / 8, spectrumByte[6], spectrumByte[1] / 2);
leds[CENTER_LED] = color;
leds[CENTER_LED].fadeToBlackBy(spectrumByte[3] / 12);
leds[CENTER_LED - 1] = color;
leds[CENTER_LED - 1].fadeToBlackBy(spectrumByte[3] / 12);
//move to the left
for (int i = NUM_LEDS - 1; i > CENTER_LED; i--) {
leds[i] = leds[i - 1];
}
// move to the right
for (int i = 0; i < CENTER_LED; i++) {
leds[i] = leds[i + 1];
}
}
void spectrumWaves3()
{
fade_down(2);
CRGB color = CRGB(spectrumByte[1] / 2, spectrumByte[5] / 8, spectrumByte[6]);
leds[CENTER_LED] = color;
leds[CENTER_LED].fadeToBlackBy(spectrumByte[3] / 12);
leds[CENTER_LED - 1] = color;
leds[CENTER_LED - 1].fadeToBlackBy(spectrumByte[3] / 12);
//move to the left
for (int i = NUM_LEDS - 1; i > CENTER_LED; i--) {
leds[i] = leds[i - 1];
}
// move to the right
for (int i = 0; i < CENTER_LED; i++) {
leds[i] = leds[i + 1];
}
}
void analyzerColumns()
{
fill_solid(leds, NUM_LEDS, CRGB::Black);
const uint8_t columnSize = NUM_LEDS / 7;
for (uint8_t i = 0; i < 7; i++) {
uint8_t columnStart = i * columnSize;
uint8_t columnEnd = columnStart + columnSize;
if (columnEnd >= NUM_LEDS) columnEnd = NUM_LEDS - 1;
uint8_t columnHeight = map8(spectrumByte[i], 1, columnSize);
for (uint8_t j = columnStart; j < columnStart + columnHeight; j++) {
if (j >= NUM_LEDS || j >= columnEnd)
continue;
leds[j] = CHSV(i * 40, 255, 255);
}
}
}
void analyzerPeakColumns()
{
fill_solid(leds, NUM_LEDS, CRGB::Black);
const uint8_t columnSize = NUM_LEDS / 7;
for (uint8_t i = 0; i < 7; i++) {
uint8_t columnStart = i * columnSize;
uint8_t columnEnd = columnStart + columnSize;
if (columnEnd >= NUM_LEDS) columnEnd = NUM_LEDS - 1;
uint8_t columnHeight = map(spectrumValue[i], 0, 1023, 0, columnSize);
uint8_t peakHeight = map(spectrumPeaks[i], 0, 1023, 0, columnSize);
for (uint8_t j = columnStart; j < columnStart + columnHeight; j++) {
if (j < NUM_LEDS && j <= columnEnd) {
leds[j] = CHSV(i * 40, 255, 128);
}
}
uint8_t k = columnStart + peakHeight;
if (k < NUM_LEDS && k <= columnEnd)
leds[k] = CHSV(i * 40, 255, 255);
}
}
void beatWaves()
{
fade_down(2);
if (beatDetect()) {
leds[CENTER_LED] = CRGB::Red;
}
//move to the left
for (int i = NUM_LEDS - 1; i > CENTER_LED; i--) {
leds[i] = leds[i - 1];
}
// move to the right
for (int i = 0; i < CENTER_LED; i++) {
leds[i] = leds[i + 1];
}
}
#define VUFadeFactor 5
#define VUScaleFactor 2.0
#define VUPaletteFactor 1.5
void drawVU() {
CRGB pixelColor;
const float xScale = 255.0 / (NUM_LEDS / 2);
float specCombo = (spectrumDecay[0] + spectrumDecay[1] + spectrumDecay[2] + spectrumDecay[3]) / 4.0;
for (byte x = 0; x < NUM_LEDS / 2; x++) {
int senseValue = specCombo / VUScaleFactor - xScale * x;
int pixelBrightness = senseValue * VUFadeFactor;
if (pixelBrightness > 255) pixelBrightness = 255;
if (pixelBrightness < 0) pixelBrightness = 0;
int pixelPaletteIndex = senseValue / VUPaletteFactor - 15;
if (pixelPaletteIndex > 240) pixelPaletteIndex = 240;
if (pixelPaletteIndex < 0) pixelPaletteIndex = 0;
pixelColor = ColorFromPalette(palettes[currentPaletteIndex], pixelPaletteIndex, pixelBrightness);
leds[x] = pixelColor;
leds[NUM_LEDS - x - 1] = pixelColor;
}
}
void drawVU2() {
uint8_t avg = map8(spectrumAvg, 0, NUM_LEDS - 1);
for (uint8_t i = 0; i < NUM_LEDS; i++) {
if(i <= avg) {
leds[i] = ColorFromPalette(palettes[currentPaletteIndex], (240 / NUM_LEDS) * i);
}
else {
leds[i] = CRGB::Black;
}
}
}