-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpectrum2.cpp
More file actions
195 lines (163 loc) · 5.51 KB
/
Spectrum2.cpp
File metadata and controls
195 lines (163 loc) · 5.51 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
#include "Spectrum2.h"
Spectrum2::Spectrum2(uint16_t columns,
uint16_t rows,
uint16_t rowOffset,
uint16_t length,
uint8_t hue,
uint8_t saturation,
bool invert,
bool fill,
CRGB * leds,
uint8_t cycle) : Visualization(columns, rows, hue, saturation, leds, cycle) {
this->rowOffset = rowOffset;
this->invert = invert;
this->length = length;
this->density = 0.08;
this->threshold = 1000.0;
this->peak = 2000.0;
this->totalMagnitudeMovingAverage = 10000.0;
this->fill = fill;
this->lowPassPeakMovingAverage = 20000.0;
this->lowPassTroughMovingAverage = 5000.0;
this->lowPassMovingAverage = 10000.0;
}
void Spectrum2::display(float* magnitudes) {
uint_fast32_t currentTime = millis();
uint_fast16_t peakCount = 0;
bool overPeak = false;
float sorted[this->length];
memcpy(sorted, magnitudes, sizeof(magnitudes[0]) * this->length);
std::sort(sorted, sorted+sizeof(sorted)/sizeof(sorted[0]));
const float alpha = 0.9;
float cutoffMagnitude = sorted[(uint_fast16_t)((1 - this->density)*this->length)];
float peakMagnitude = sorted[this->length - 2];
this->threshold = (this->threshold * alpha) + (cutoffMagnitude* (1 - alpha));
this->peak = (this->peak * alpha) + (peakMagnitude * (1 - alpha));
float magnitude;
float magnitudeSum = 0;
float lowPassMagnitudeSum = 0;
for (uint8_t y=0; y<this->length; y++) {
if (y < this->lowPassCount) {
lowPassMagnitudeSum += magnitudes[y];
}
magnitudeSum += magnitudes[y];
}
this->totalMagnitudeMovingAverage = (this->totalMagnitudeMovingAverage * 0.98) + magnitudeSum * 0.02;
this->lowPassMovingAverage = (this->lowPassMovingAverage * 0.98) + (lowPassMagnitudeSum * (1 - 0.98));
if (fill) {
float ratio = 0;
uint_fast8_t height = 0;
for (uint8_t column=0; column<this->columns; column++) {
magnitude = magnitudes[column];
ratio = magnitude / this->peak;
height = min((float) this->length, this->length * ratio);
uint_fast8_t intensity = min(ratio * 200, (float) 200);
for (uint8_t row=0; row<height; row++) {
if (this->invert) {
this->leds[this->xy2Pos(column, this->rowOffset + (this->length - row))] = CHSV(this->hue, this->saturation, intensity);
} else {
this->leds[this->xy2Pos(column, this->rowOffset + row)] = CHSV(this->hue, this->saturation, intensity);
}
}
}
// put things we want to log here
// if (currentTime > this->loggingTimestamp + 500) {
// this->loggingTimestamp = currentTime;
// // for (uint8_t column=0; column<15; column++) {
// // Serial.print(magnitudes[column]);
// // Serial.print("\t");
// // }
// // Serial.print(this->peak);
// // Serial.print("\t");
// // Serial.print(ratio);
// // Serial.print("\t");
// // Serial.print(height);
// // Serial.print("\t");
// // Serial.print(this->length);
// // Serial.println("");
// }
} else {
for (uint8_t y=0; y<this->length; y++) {
overPeak = false;
magnitudeSum += magnitudes[y];
if (magnitudes[y] < this->threshold) {
continue;
}
if (magnitudes[y] > this->peak * 1.2) {
peakCount++;
overPeak = true;
}
magnitude = ((magnitudes[y] - this->threshold) / (this->peak - this->threshold));
magnitude = min(magnitude, (float) 1);
magnitude = max(magnitude, (float) 0.15);
CRGB c = CHSV(this->hue, this->saturation, magnitude*255);
CRGB c2 = CHSV(this->hue, this->saturation, magnitude*128);
if (this->invert) {
this->displayRow(this->rowOffset - y, c);
if (overPeak) {
if (y > 0) {
this->displayRow(this->rowOffset - (y - 1), c2);
}
if (y < this->length - 1) {
this->displayRow(this->rowOffset - (y + 1), c2);
}
}
} else {
this->displayRow(this->rowOffset + y, c);
if (overPeak) {
if (y > 0) {
this->displayRow(this->rowOffset + (y - 1), c2);
}
if (y < this->length - 1) {
this->displayRow(this->rowOffset + (y + 1), c2);
}
}
}
if (overPeak) {
y++;
}
}
}
// Change hue to pink on big volume increases
// if (this->cycle > 0 && magnitudeSum > this->totalMagnitudeMovingAverage * 1.75) {
// this->hue = 240;
// }
// put things we want to log here
// if (currentTime > this->loggingTimestamp + 500) {
// this->loggingTimestamp = currentTime;
// Serial.print(currentTime);
// Serial.print("\t");
// Serial.print(this->cycle);
// Serial.print("\t");
// Serial.print(this->hue);
// // Serial.print(peakCount);
// // Serial.print(cutoffMagnitude);
// // Serial.print("\t");
// // Serial.print(peakMagnitude);
// // Serial.print("\t");
// // Serial.print(this->threshold);
// // Serial.print("\t");
// // Serial.print(this->peak);
// // Serial.print("\t");
// // Serial.print(this->hue);
// // Serial.print("\t");
// // Serial.print(magnitudeSum);
// // Serial.print("\t");
// // Serial.print(this->totalMagnitudeMovingAverage);
// Serial.println("");
// }
}
float Spectrum2::getDensity() {
return this->density;
}
void Spectrum2::setDensity(float density) {
this->density = density;
}
float Spectrum2::getMagnitude() {
return this->totalMagnitudeMovingAverage;
}
void Spectrum2::displayRow(uint8_t y, CRGB color) {
for (uint8_t x=0; x<this->columns; x++) {
leds[this->xy2Pos(x, y)] = color;
}
}