-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSuper3dvisualizer.pde
304 lines (248 loc) · 7.67 KB
/
Super3dvisualizer.pde
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
/**
* Super3dvisualizer, a 3D Surface Spectogram Audio Visualizer in Processing
*
* @author Eliot Lash
* @copyright Copyright (c) 2010-2012 Eliot Lash
*/
import ddf.minim.analysis.*;
import ddf.minim.*;
import peasy.*;
import java.io.*;
Minim minim;
AudioInput in;
AudioPlayer groove;
FFT fft;
PeasyCam cam;
static int sampleRate = 512;
int bufferStart = 0;
static int bufferMax = 100;
float countRad = 0;
static float power = 1.5;
static float xStretch = 5.0;
static float yStretch = 1.0;
static float zStretch = 5.0;
float cameraCenterX = 255;
float cameraCenterY = 0;
float cameraCenterZ = 255;
static float alphaCycle = 0.03;
float savedmouseX;
float savedmouseY;
float savedmouseZ = 220.0;
float[][] geomBuffer;
void setup()
{
size(851, 800, P3D);
cam = new PeasyCam(this, 100);
cam.setMinimumDistance(50);
cam.setMaximumDistance(800);
CameraState camState = readCameraData();
if(camState != null) {
cam.setState(camState);
}
minim = new Minim(this);
//minim.debugOn();
// get a line in from Minim, default bit depth is 16
in = minim.getLineIn(Minim.STEREO, sampleRate);
in.mute();
groove = minim.loadFile("Legacy.mp3", sampleRate);
groove.play();
fft = new FFT(groove.bufferSize(), groove.sampleRate());
fft.logAverages(22, 3);
geomBuffer = new float[bufferMax][fft.specSize()];
for(int i = 0; i < fft.specSize(); i++)
{
geomBuffer[bufferStart][i] = fft.getBand(i);
}
//frameRate(1);
//smooth();
}
void draw()
{
// stroke(255);
// // draw the waveforms
// for(int i = 0; i < in.bufferSize() - 1; i++)
// {
// line(i, 50 + in.left.get(i)*50, i+1, 50 + in.left.get(i+1)*50);
// line(i, 150 + in.right.get(i)*50, i+1, 150 + in.right.get(i+1)*50);
// }
// Change height of the camera with mouseY
/*camera(30.0, mouseY * 10, 220.0, // eyeX, eyeY, eyeZ
0.0, 0.0, 0.0, // centerX, centerY, centerZ
0.0, 1.0, 0.0); // upX, upY, upZ*/
//lights();
background(10);
specular(#00BFFF);
lightSpecular(0, 191, 255);
directionalLight(49, 140, 231, 0, 0, -1);
//background((int)fft.calcAvg(10, 20000) * 10 + 80);
//camera shit
/*float cameraY = (height/2.0);
float fov = (savedmouseX /float(width) * PI/2) * -1;
float cameraZ = cameraY / tan(fov / 2.0);
float aspect = float(width)/float(height);
if (mousePressed) {
aspect = aspect / 2.0;
}
perspective(fov, aspect, cameraZ/10.0, cameraZ*10.0);
translate(width/2+30, height/2, 0);
rotateX(-PI/6);
rotateY(PI/3 + savedmouseY/float(height) * PI);*/
// camera(savedmouseX, savedmouseY, savedmouseZ, // eyeX, eyeY, eyeZ
// cameraCenterX, cameraCenterY, cameraCenterZ, // centerX, centerY, centerZ
// 0.0, -1.0, 0.0); // upX, upY, upZ
//draw FFT
translate(0, -1*height, 0);
fft.forward(groove.mix);
//fft.forward(groove.mix);
//print(fft.calcAvg(10, 20000));
//fill(bufferStart, bufferStart - 100, bufferStart -50);
//fill(#FF7E00);
//noStroke();
float colorPercentage = ((sin(countRad) * 0.5 + 0.5) * 255);
fill(#00BFFF, colorPercentage);
//println(colorPercentage);
stroke(#00BFFF);
strokeWeight(1);
//Evil hack to draw over the gap left by an error in how we implemented our ring buffer - this is the "scan line
beginShape(QUAD_STRIP);
for (int band = 0; band < fft.specSize(); band++) {
int i = bufferStart % (geomBuffer.length - 1);
if ((i + 1) < geomBuffer.length) {
vertex((band)* xStretch, height + pow(geomBuffer[i][band], power) * yStretch, bufferStart * zStretch);
vertex((band)* xStretch, height + pow(geomBuffer[i + 1][band], power) * yStretch, (bufferStart+1) * zStretch);
} else {
print ("i == "+ i +". geomBuffer.length == " + geomBuffer.length + ". i >= geomBuffer.length. Waaah!\n");
}
}
endShape();
// beginShape(TRIANGLE_STRIP);
// int bufferHack = bufferStart - 1;
// if (bufferHack != -1) {
// for (int band = 0; band < fft.specSize(); band++) {
// int i = bufferHack % (geomBuffer.length - 1);
//
// if ((i + 1) < geomBuffer.length) {
// vertex((band)* xStretch, height + geomBuffer[i][band] * yStretch, bufferHack * zStretch);
// vertex((band)* xStretch, height + geomBuffer[i + 1][band] * yStretch, (bufferHack+1) * zStretch);
// } else {
// print ("i == "+ i +". geomBuffer.length == " + geomBuffer.length + ". i >= geomBuffer.length. Waaah!\n");
// }
// }
// endShape();
// }
float redFactor = 30;
float greenFactor = 0;
float blueFactor = 200;
for (int zPosition = bufferStart + 1; zPosition != bufferStart;) {
//;pow(geomBuffer[i + 1][band], power) * -1
//println(bufferStart + " " + zPosition);
beginShape(TRIANGLE_STRIP);
for (int band = 0; band < fft.specSize(); band++) {
greenFactor += 0.004;
int i = (bufferStart + zPosition) % (geomBuffer.length -1);
stroke(redFactor, greenFactor, blueFactor);
if ((i + 1) < geomBuffer.length) {
vertex((band)* xStretch, height + pow(geomBuffer[i][band], power) * yStretch, zPosition * zStretch);
vertex((band)* xStretch, height + pow(geomBuffer[i + 1][band], power) * yStretch, (zPosition+1) * zStretch);
} else {
print ("i == "+ i +". geomBuffer.length == " + geomBuffer.length + ". i >= geomBuffer.length. Waaah!\n");
}
}
endShape();
zPosition++;
if(zPosition >= geomBuffer.length)
{
zPosition = 0;
}
}
//Update our FFT data
for(int i = 0; i < fft.specSize(); i++)
{
geomBuffer[bufferStart][i] = fft.getBand(i);
}
if (bufferStart < bufferMax - 1) {
bufferStart++;
} else {
bufferStart = 0;
}
if (countRad < 6.28) {
countRad += alphaCycle;
} else {
countRad = 0;
}
//Draw X, Y, Z axis
stroke(255,0,0);
line (0,height,0,50,height,0); //x+ axis
stroke(0,255,0);
line (0,height,0,0,height+50,0); //y+ axis
stroke(0,0,255);
line (0,height,0,0,height,50); //z axis
stroke(255);
fill(255);
}
void mouseDragged() {
savedmouseX = mouseX;
savedmouseY = mouseY;
}
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
savedmouseZ += 100;
} else if(keyCode == DOWN) {
savedmouseZ -= 100;
} else if(keyCode == LEFT) {
cameraCenterX -= 100;
} else if (keyCode == RIGHT) {
cameraCenterX += 100;
}
} else {
if (key == 'c') {
saveCameraData();
}
}
}
void saveCameraData() {
println("try to write camera state to " + sketchPath("camera.data"));
FileOutputStream f_out = null;
ObjectOutputStream obj_out = null;
try {
f_out = new FileOutputStream(sketchPath("camera.data"));
obj_out = new ObjectOutputStream(f_out);
CameraState state = cam.getState();
obj_out.writeObject(state);
println("wrote camera data");
} catch (Exception e) {
println(e);
} finally {
try {
if (f_out != null) f_out.close();
if (obj_out != null) obj_out.close();
} catch (Exception e) {}
}
}
CameraState readCameraData() {
FileInputStream f_in = null;
ObjectInputStream obj_in = null;
try {
f_in = new FileInputStream(sketchPath("camera.data"));
obj_in = new ObjectInputStream(f_in);
CameraState state = (CameraState)obj_in.readObject();
return state;
} catch (Exception e) {
println(e);
} finally {
try {
if (f_in != null) f_in.close();
if (obj_in != null) obj_in.close();
} catch (Exception e) {}
}
return null;
}
void stop()
{
// always close Minim audio classes when you are done with them
in.close();
//groove.close();
minim.stop();
super.stop();
}