-
Notifications
You must be signed in to change notification settings - Fork 0
/
SonicEgg.pde
305 lines (246 loc) · 7.01 KB
/
SonicEgg.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
//3D Spectrogram with Microphone Input
//press 'p' to pause view
//press 'v' to toggle view
//press 't' to toggle freq labels
//press 'f' to toggle fade effect
//press 'd' to toggle dB amplitude scale
//Modified by Evan Morgan 2013 - http://you-rhythmic.com
//Based on version modified by kylejanzen 2011 - http://kylejanzen.wordpress.com
//Original script wwritten by John Locke 2011 - http://gracefulspoon.com
import processing.opengl.*;
import ddf.minim.analysis.*;
import ddf.minim.*;
FFT fftLog;
Waveform audio3D;
Minim minim;
AudioInput microphone;
boolean done = false;
boolean pause = false;
boolean altV = false; // toggle alternative view
boolean dB = true; // toggle display in dB
boolean fade = true; // toggle fade
boolean tex = true;
int depth = 220;
int Amp = 5;
int low = 220; //smallest octave Hz
int divi = 8; //divides octave by div
float cut = 0.06; //cut low level noise when in dB mode
PFont font;
int[] freqs = new int[0];
float[] levs = new float[1200];
int l = 0;
float camzoom;
float maxX = 0;
float maxY = 0;
float maxZ = 0;
float minX = 0;
float minY = 0;
float minZ = 0;
// choose sample frequency
int sf = 16000;
//int sf = 22050;
//int sf = 32000;
//int sf = 44100;
void setup()
{
frame.setBackground(new java.awt.Color(0, 0, 0));
size(1280, 720, OPENGL); //screen proportions
smooth();
noStroke();
minim = new Minim(this);
microphone = minim.getLineIn(Minim.MONO, 1024, sf); //repeat the song
frameRate(100);
background(255);
font = createFont("Arial", 16, true);
fftLog = new FFT(microphone.bufferSize(), microphone.sampleRate());
fftLog.logAverages(low, divi); //adjust numbers to adjust spacing;
float w = float (width/fftLog.avgSize());
float x = w;
float y = 0;
float z = 50;
float radius = 10;
audio3D = new Waveform(x, y, z, radius);
}
void draw()
{
background(0);
//println(fftLog.getAvg(20));
arrayCopy(levs, 1, levs, 0, 1199);
levs[1199] = microphone.left.level();
l += 1;
if (l == 1200)
l = 0;
pushMatrix();
camera(width/2.0, height/2.0, (height/2.0) / tan(PI*30.0 / 180.0), width/2.0, height/2.0, 0, 0, 1, 0);
//if (!altV)
//image(a, 0, height-170);
//else
//image(a, 0, 0);
for (int k =0; k<1200; k++) {
stroke(255, 255, 255, k/(1200/255));
strokeWeight(1);
int a = constrain(round(levs[k]*200), 0, 50);
if (altV)
line(40+k, (90)-a, 40+k, (90)+1+a);
}
popMatrix();
fftLog.window(FFT.HAMMING);
float zoom = 250;
int zoom2 =-100;
PVector foc = new PVector(audio3D.x, audio3D.y, 0);
PVector cam = new PVector(zoom, zoom, -zoom);
if (!altV)
camera(foc.x+cam.x+10+zoom2+3, foc.y+cam.y-50+zoom2-3, foc.z+cam.z+150-zoom2-120, foc.x+17, foc.y-40, foc.z, 0, 0, 1);
else
camera(round(width/2.0)-30, foc.y+cam.y+100, foc.z+cam.z-200, width/2.0-30, foc.y-100, foc.z-140, 0, 0, 1);
directionalLight(255,255,255,sin(radians(-90)),cos(radians(180)),1);
ambientLight(100,100,100,audio3D.x,audio3D.y,audio3D.z);
//run fft on input
fftLog.forward(microphone.mix);
if (done == false) {
done = true;
for (int j =0; j<fftLog.avgSize(); j++) {
freqs = expand(freqs, freqs.length+1);
freqs[j] = round(fftLog.getAverageCenterFrequency(j));//round(b+((t-1)*(b/divi2))+((b/divi2)/2.0)) ;
}
}
//println(freqs[55]);
audio3D.update();
audio3D.textdraw();
audio3D.plotTrace();
}
class Waveform
{
float x, y, z;
float radius;
PVector[] pts = new PVector[fftLog.avgSize()];
PVector[] trace = new PVector[0];
Waveform(float incomingX, float incomingY, float incomingZ, float incomingRadius)
{
x = incomingX;
y = incomingY;
z = incomingZ;
radius = incomingRadius;
}
void update()
{
plot();
}
void plot()
{
for (int i = 0; i < fftLog.avgSize(); i++)
{
int w = int(width/fftLog.avgSize());
x = i*w;
y = frameCount*5;
if (!dB)
z = height/4-fftLog.getAvg(fftLog.avgSize()-i-1)*Amp; //change multiplier to reduces height default '10'
else{ //convert amplitude to dB scale
z = (height/4)-(Math.round((Amp-3)*(20*(float)Math.log10(constrain(fftLog.getAvg(fftLog.avgSize()-i-1),cut,50))))*2.0)+(40*(Amp-3)*(float)Math.log10(cut)); //change multiplier to reduces height default '10'
}
// println(z);
stroke(0);
point(x, y, z);
pts[i] = new PVector(x, y, z);
//increase size of array trace by length+1
trace = (PVector[]) expand(trace, trace.length+1);
//always get the next to last
trace[trace.length-1] = new PVector(pts[i].x, pts[i].y, pts[i].z);
}
}
void textdraw()
{
if (tex){
for (int i =round(divi/2); i<fftLog.avgSize(); i=i+round(divi/2)) {
pushMatrix();
if (!altV) {
translate(pts[i].x, pts[i].y+10, height/4);
rotateY(PI/2);
rotateZ(PI/2);
}
else if (altV) {
translate(pts[i].x, pts[i].y+10, height/4+20);
rotateZ(PI);
rotateX(PI*.9);
}
textFont(font, 15);
fill(255);
text(freqs[fftLog.avgSize()-i-1]+" Hz", 0, 0, 0);
popMatrix();
}
}
}
void plotTrace()
{
stroke(255, 80);
int inc = fftLog.avgSize();
PVector[] trace2 = new PVector[fftLog.avgSize()*depth];
if (trace.length > fftLog.avgSize()*depth) {
arrayCopy(trace, trace.length-(fftLog.avgSize()*depth), trace2, 0, fftLog.avgSize()*depth);
trace = trace2;
}
int cnt = 1;
for (int i=1; i<trace.length-inc; i++)
{
if (i%inc != 0)
{
beginShape(TRIANGLE_STRIP);
strokeWeight(2);
strokeCap(SQUARE);
//noStroke();
float value = (trace[i].z);
float value2 = (trace[i].x);
float m = map(value, -10, 179, 150, 0);
float n = map(value2, 200, 1500, 180, 0);
float o = map(value2, 200, 1500, 0, 255);
if(fade){
stroke(255, 255, 255, cnt/3);
fill(o+m, 100-m/2, n-(m/2), cnt/0.7);
}
else{
stroke(255, 255, 255, 60);
fill(o+m, 100-m/2, n-(m/2), 180);
}
//fill(o, m, n, 150);
//specular(100, 100, 100);
//emissive(30,30, 30);
vertex(trace[i].x, trace[i].y, trace[i].z);
vertex(trace[i-1].x, trace[i-1].y, trace[i-1].z);
vertex(trace[i+inc].x, trace[i+inc].y, trace[i+inc].z);
vertex(trace[i-1+inc].x, trace[i-1+inc].y, trace[i-1+inc].z);
endShape(CLOSE);
}
else
cnt += 1;
}
}
}
void keyPressed() {
if (key == 'P' || key == 'p') {
pause = !pause;
}
//toggle alternative view
if (key == 'v' || key == 'V')
altV = !altV;
//toggle dB or linear amplitude
if (key == 'd' || key == 'D')
dB = !dB;
//toggle fade
if (key == 'f' || key == 'F')
fade = !fade;
//toggle text labels
if (key == 't' || key == 'T')
tex = !tex;
if (pause)
noLoop();
else
loop();
}
void stop()
{
// always close Minim audio classes when you finish with them
microphone.close();
// always stop Minim before exiting
minim.stop();
super.stop();
}