-
Notifications
You must be signed in to change notification settings - Fork 7
/
SketchFFTSimple.java
executable file
·57 lines (47 loc) · 1.72 KB
/
SketchFFTSimple.java
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
package de.hfkbremen.algorithmiccliches.examples;
import ddf.minim.AudioPlayer;
import ddf.minim.Minim;
import ddf.minim.analysis.FFT;
import processing.core.PApplet;
public class SketchFFTSimple extends PApplet {
private Minim mMinim;
private AudioPlayer mPlayer;
private FFT mFFT;
public void settings() {
size(1024, 768, P3D);
}
public void stop() {
mPlayer.pause();
mMinim.stop();
super.stop();
}
public void setup() {
// // @TODO `JFileChooser` crashes on macOS 10.15
// javax.swing.JFileChooser mFileChooser = new javax.swing.JFileChooser();
// mFileChooser.showOpenDialog(frame);
// String mFileName = mFileChooser.getSelectedFile().getAbsolutePath();
String mFileName = "/path/to/sound/file";
mMinim = new Minim(this);
mPlayer = mMinim.loadFile(mFileName, 1024); // make sure the sketch can find the audio
mPlayer.play();
mFFT = new FFT(mPlayer.bufferSize(), mPlayer.sampleRate());
mFFT.logAverages(55, 4);
}
public void draw() {
mFFT.forward(mPlayer.left);
float mCurrentTime = (float) mPlayer.position() / (float) mPlayer.length();
/* draw bands */
background(255);
for (int i = 0; i < mFFT.avgSize(); i++) {
final float x = width * (float) i / mFFT.avgSize();
final float myHeight = mFFT.getAvg(i) * 16;
fill(0);
stroke(0);
rect(x, height, (float) width / mFFT.avgSize(), -myHeight);
}
line(0, height / 2.0f, mCurrentTime * width, height / 2.0f);
}
public static void main(String[] args) {
PApplet.main(new String[]{SketchFFTSimple.class.getName()});
}
}