forked from MadeWithLight/LEDFireEffectLampWrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioLogic.h
100 lines (81 loc) · 2.93 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
/*
* Torch: https://github.com/evilgeniuslabs/torch
* Copyright (C) 2015 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/>.
*/
#define MSGEQ7_STROBE_PIN 2
#define MSGEQ7_RESET_PIN 3
#define MSGEQ7_LEFT_PIN A0
#define MSGEQ7_RIGHT_PIN A1
const uint8_t bandCount = 7;
int levelsLeft[bandCount];
int peaksLeft[bandCount];
int levelsRight[bandCount];
int peaksRight[bandCount];
static const uint8_t peakDecay = (1024 / MATRIX_HEIGHT) / 4;
bool drawPeaks = true;
int noiseCorrection[bandCount] = {
// -55, -50, -45, -55, -40, -55, -50,
0, 0, 0, 0, 0, 0, 0
};
uint8_t bandOffset = 3;
uint8_t horizontalPixelsPerBand = MATRIX_WIDTH / (bandCount * 2);
uint8_t levelsPerVerticalPixel = 63; // 1024 / MATRIX_HEIGHT;
uint8_t levelsPerHue = 1024 / 256;
void initializeAudio() {
pinMode(MSGEQ7_LEFT_PIN, INPUT);
pinMode(MSGEQ7_RIGHT_PIN, INPUT);
pinMode(MSGEQ7_RESET_PIN, OUTPUT);
pinMode(MSGEQ7_STROBE_PIN, OUTPUT);
digitalWrite(MSGEQ7_RESET_PIN, LOW);
digitalWrite(MSGEQ7_STROBE_PIN, HIGH);
}
void readAudio() {
digitalWrite(MSGEQ7_RESET_PIN, HIGH);
digitalWrite(MSGEQ7_RESET_PIN, LOW);
int levelLeft;
int levelRight;
for (uint8_t band = 0; band < bandCount; band++) {
digitalWrite(MSGEQ7_STROBE_PIN, LOW);
delayMicroseconds(30);
levelLeft = analogRead(MSGEQ7_LEFT_PIN);
levelRight = analogRead(MSGEQ7_RIGHT_PIN);
digitalWrite(MSGEQ7_STROBE_PIN, HIGH);
levelLeft += noiseCorrection[band];
levelRight += noiseCorrection[band];
// if (levelLeft < 0) levelLeft = 0;
// if (levelLeft > 1023) levelLeft = 1023;
//
// if (levelRight < 0) levelRight = 0;
// if (levelRight > 1023) levelRight = 1023;
levelsLeft[band] = levelLeft;
levelsRight[band] = levelRight;
// if (levelLeft >= peaksLeft[band]) {
peaksLeft[band] = levelLeft;
// }
// else if (peaksLeft[band] > 0) {
// peaksLeft[band] = peaksLeft[band] - peakDecay;
// if(peaksLeft[band] < 0) peaksLeft[band] = 0;
// }
//
// if (levelRight >= peaksRight[band]) {
peaksRight[band] = levelRight;
// }
// else if (peaksRight[band] > 0) {
// peaksRight[band] = peaksRight[band] - peakDecay;
// if(peaksRight[band] < 0) peaksRight[band] = 0;
// }
}
}