-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessing_fadecandy_spectrum_analyzer.pde
More file actions
138 lines (104 loc) · 3.65 KB
/
Processing_fadecandy_spectrum_analyzer.pde
File metadata and controls
138 lines (104 loc) · 3.65 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
/**
* Processing_fadecandy_spectrum_analyzer.pde
*/
import ddf.minim.analysis.*;
import ddf.minim.*;
Minim minim;
AudioPlayer sound; // mp3 input
// AudioInput sound; // microphone input
FFT fftLog;
final int maxAmplitude = 255;
OPC opc;
final String fcServerHost = "127.0.0.1";
final int fcServerPort = 7890;
final int boxesAcross = 2;
final int boxesDown = 2;
final int ledsAcross = 8;
final int ledsDown = 8;
// initialized in setup()
float spacing;
int x0;
int y0;
final color setColour = color(200, 150, 100);
final color unsetColour = color(0, 0, 50);
int exitTimer = 0; // Run forever unless set by command line
void setup() {
apply_cmdline_args();
size(720, 480, P2D);
opc = new OPC(this, fcServerHost, fcServerPort); // Connect to an instance of fcserver
spacing = (float)min(height / (boxesDown * ledsDown + 1), width / (boxesAcross * ledsAcross + 1));
x0 = (int)(width - spacing * (boxesAcross * ledsAcross - 1)) / 2;
y0 = (int)(height - spacing * (boxesDown * ledsDown - 1)) / 2;
final int boxCentre = (int)((ledsAcross - 1) / 2.0 * spacing); // probably using the centre in the ledGrid8x8 method
int ledCount = 0;
for (int y = 0; y < boxesDown; y++) {
for (int x = 0; x < boxesAcross; x++) {
opc.ledGrid8x8(ledCount, x0 + spacing * x * ledsAcross + boxCentre, y0 + spacing * y * ledsDown + boxCentre, spacing, 0, false, false);
ledCount += ledsAcross * ledsDown;
}
}
minim = new Minim(this);
sound = minim.loadFile("083_trippy-ringysnarebeat-3bars.mp3", 1024); // mp3 input
// sound = minim.getLineIn(Minim.MONO, 1024); // microphone input
// loop the file
sound.loop(); // mp3 input
// create an FFT object for calculating logarithmically spaced averages
fftLog = new FFT(sound.bufferSize(), sound.sampleRate()); // may fail if the microphone device is already in use!
fftLog.logAverages(22, 3);
// fftLog.logAverages(11, 1);
}
// draw the display like it is the descrete LEDs
public void draw() {
fftLog.forward(sound.mix);
int numSpectrumBars = fftLog.avgSize();
float horizRatio = (float)numSpectrumBars / (boxesAcross * ledsAcross);
if (horizRatio > 1.0) { // map every one, 2nd or 3rd etc. to the display
horizRatio = floor(horizRatio);
}
int startBar = (int)(numSpectrumBars - horizRatio * boxesAcross * ledsAcross);
if (startBar >= 2) { // use the middle of the spectrum in the display
startBar /= 2;
}
float vertLogarithm = pow(maxAmplitude, (1.0 / (boxesDown * ledsDown)));
background(0);
noStroke();
for (int x = 0; x < boxesAcross * ledsAcross; x++) {
float vertMax = maxAmplitude; // pow(vertLogarithm, boxesDown * ledsDown);
int curSpectrumBar = (int)(startBar + horizRatio * x);
// float freq = fftLog.getAverageCenterFrequency(curSpectrumBar);
float amplitude = fftLog.getBand(curSpectrumBar);
for (int y = 0; y < boxesDown * ledsDown; y++) {
vertMax /= vertLogarithm;
int c = (amplitude > vertMax) ? setColour : unsetColour;
fill(c);
square(x0 + spacing * x - spacing / 2, y0 + spacing * y - spacing / 2, spacing - 1);
}
}
fill(128);
text(String.format("%5.1f fps", frameRate), 5, 15);
check_exit();
}
void apply_cmdline_args() {
if (args == null) {
return;
}
for (String exp: args) {
String[] comp = exp.split("=");
switch (comp[0]) {
case "exit":
exitTimer = parseInt(comp[1], 10);
println("exit after " + exitTimer + "s");
break;
}
}
}
void check_exit() {
if (exitTimer == 0) { // skip if not run from cmd line
return;
}
int m = millis();
if (m / 1000 >= exitTimer) {
println(String.format("average %.1f fps", (float)frameCount / exitTimer));
exit();
}
}