forked from macetech/RGBShadesAudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.h
114 lines (85 loc) · 2.94 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
// Interface with MSGEQ7 chip for audio analysis
#define AUDIODELAY 10
// Pin definitions
#define ANALOGPIN 3
#define STROBEPIN 8
#define RESETPIN 7
// 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;
void doAnalogs() {
static PROGMEM const byte spectrumFactors[7] = {9, 11, 13, 13, 12, 12, 13};
// reset MSGEQ7 to first frequency bin
digitalWrite(RESETPIN, HIGH);
delayMicroseconds(5);
digitalWrite(RESETPIN, 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(STROBEPIN, LOW);
delayMicroseconds(50); // to allow the output to settle
// read the analog value
spectrumValue[i] = analogRead(ANALOGPIN);
digitalWrite(STROBEPIN, 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);
}
// Calculate audio levels for automatic gain
audioAvg = (1.0 - AGCSMOOTH) * audioAvg + AGCSMOOTH * (analogsum / 7.0);
// 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;
}
}