-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualizer.pde
More file actions
216 lines (184 loc) · 5.96 KB
/
visualizer.pde
File metadata and controls
216 lines (184 loc) · 5.96 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* This sketch demonstrates how to monitor the currently active audio input
* of the computer using an AudioInput. What you will actually
* be monitoring depends on the current settings of the machine the sketch is running on.
* Typically, you will be monitoring the built-in microphone, but if running on a desktop
* it's feasible that the user may have the actual audio output of the computer
* as the active audio input, or something else entirely.
* <p>
* Press 'm' to toggle monitoring on and off.
* <p>
* When you run your sketch as an applet you will need to sign it in order to get an input.
* <p>
* For more information about Minim and additional features,
* visit http://code.compartmental.net/minim/
*
* code adapted from http://www.pfesto.com/how-to-make-an-audio-visualizer-with-processing/
* and http://www.benfarahmand.com/2013/10/source-code-audio-visualizer-with.html
*/
import ddf.minim.*;
import ddf.minim.analysis.*;
Minim minim;
AudioInput in;
FFT fft;
//AudioPlayer song;
BeatDetect beat_freq;
BeatDetect beat_sound;
BeatListener listener;
float eRadius;
float kickSize, snareSize, hatSize;
float[] angle;
float[] y, x;
class BeatListener implements AudioListener
{
private BeatDetect beat;
private AudioSource source;
BeatListener(BeatDetect beat, AudioSource source)
{
this.source = source;
this.source.addListener(this);
this.beat = beat;
}
void samples(float[] samps)
{
beat.detect(source.mix);
}
void samples(float[] sampsL, float[] sampsR)
{
beat.detect(source.mix);
}
}
void setup()
{
size(1200, 600, P3D);
minim = new Minim(this);
// use the getLineIn method of the Minim object to get an AudioInput
in = minim.getLineIn();
println("BufferSize: " + in.bufferSize());
println("SampleRate: " + in.sampleRate());
//song = minim.loadFile("song.mp3", 2048);
//song.play();
fft = new FFT(in.bufferSize(), in.sampleRate());
y = new float[fft.specSize()];
x = new float[fft.specSize()];
angle = new float[fft.specSize()];
beat_sound = new BeatDetect();
beat_freq = new BeatDetect(in.bufferSize(), in.sampleRate());
beat_freq.setSensitivity(50);
kickSize = snareSize = hatSize = 16;
listener = new BeatListener(beat_freq, in);
ellipseMode(RADIUS);
eRadius = 20;
}
void draw() {
//it's important to put the background in the draw loop
//to make it animate rather than draw over itself
background(#222222);
fft.forward(in.mix);
beat_sound.detect(in.mix);
float a = map(eRadius, 20, 80, 60, 255);
fill(0, 0, 0, a);
if ( beat_sound.isOnset() ) eRadius = 80;
ellipse(250, 550, eRadius, eRadius);
eRadius *= 0.95;
if ( eRadius < 20 ) eRadius = 20;
//line characteristics
strokeWeight(1.3);
stroke(#FFF700);
//processing's transform tool
pushMatrix();
translate(200, 0);
//draw the frequency spectrum as a series of vertical lines
//I multiple the value of getBand by 4
//so that we can see the lines better
for(int i = 0; i < 0+fft.specSize(); i++)
{
line(i, 450, i, 450 - fft.getBand(i)*4);
}
//closing the transform tool
popMatrix();
//changing the line color
stroke(#FF0000);
//the waveform is drawn by connecting neighbor values with a line.
//The values in the buffers are between -1 and 1.
//If we don't scale them up our waveform will look like a straight line.
//Thus each of the values is multiplied by 50
int scale = 50;
for(int i = 200; i < in.left.size() - 1; i++)
{
line(i, 50 + in.left.get(i)*scale, i+1, 50 + in.left.get(i+1)*scale);
line(i, 150 + in.right.get(i)*scale, i+1, 150 + in.right.get(i+1)*scale);
line(i, 250 + in.mix.get(i)*scale, i+1, 250 + in.mix.get(i+1)*scale);
}
//blue rectangle on the left
noStroke();
fill(#0024FF);
rect(0, 0, 200, height);
//text
textSize(24);
fill(255);
text("left amplitude", 0, 50);
text("right amplitude", 0, 150);
text("mixed amplitude", 0, 250);
text("frequency", 0, 450);
text("beat detect", 0, 550);
if ( beat_freq.isKick() ) kickSize = 32;
if ( beat_freq.isSnare() ) snareSize = 32;
if ( beat_freq.isHat() ) hatSize = 32;
textSize(kickSize);
text("KICK", width/4, height/2);
textSize(snareSize);
text("SNARE", width/2, height/2);
textSize(hatSize);
text("HAT", 3*width/4, height/2);
kickSize = constrain(kickSize * 0.95, 16, 32);
snareSize = constrain(snareSize * 0.95, 16, 32);
hatSize = constrain(hatSize * 0.95, 16, 32);
doubleAtomicSprocket();
}
void doubleAtomicSprocket() {
noStroke();
pushMatrix();
translate(width/2, height/2);
for (int i = 0; i < fft.specSize() ; i++) {
y[i] = y[i] + fft.getBand(i)/100;
x[i] = x[i] + fft.getFreq(i)/100;
angle[i] = angle[i] + fft.getFreq(i)/2000;
rotateX(sin(angle[i]/2));
rotateY(cos(angle[i]/2));
// stroke(fft.getFreq(i)*2,0,fft.getBand(i)*2);
fill(fft.getFreq(i)*2, 0, fft.getBand(i)*2);
pushMatrix();
translate((x[i]+50)%width/3, (y[i]+50)%height/3);
box(fft.getBand(i)/20+fft.getFreq(i)/15);
popMatrix();
}
popMatrix();
pushMatrix();
translate(width/2, height/2, 0);
for (int i = 0; i < fft.specSize() ; i++) {
y[i] = y[i] + fft.getBand(i)/1000;
x[i] = x[i] + fft.getFreq(i)/1000;
angle[i] = angle[i] + fft.getFreq(i)/100000;
rotateX(sin(angle[i]/2));
rotateY(cos(angle[i]/2));
// stroke(fft.getFreq(i)*2,0,fft.getBand(i)*2);
fill(0, 255-fft.getFreq(i)*2, 255-fft.getBand(i)*2);
pushMatrix();
translate((x[i]+250)%width, (y[i]+250)%height);
box(fft.getBand(i)/20+fft.getFreq(i)/15);
popMatrix();
}
popMatrix();
}
void stop()
{
//close the AudioInput you got from Minim.getLineIn()
in.close();
minim.stop();
//this calls the stop method that
//you are overriding by defining your own
//it must be called so that your application
//can do all the cleanup it would normally do
super.stop();
}