-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
46 lines (41 loc) · 914 Bytes
/
main.js
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
var song;
var fft;
var button;
var w;
function toggleSong() {
if (song.isPlaying()) {
song.pause();
} else {
song.play();
}
}
function preload() {
// song = loadSound('this-dot-kp.mp3');
// song = loadSound('rainbow.mp3');
song = loadSound('audio/attention.mp3');
}
function setup() {
createCanvas(250, 250);
// colorMode(HSB);
angleMode(DEGREES);
button = createButton('Double Click');
button.mousePressed(toggleSong);
song.play();
// https://p5js.org/reference/#/p5.FFT
fft = new p5.FFT(0.7, 64);
w = width / 5; // number of bars
}
function draw() {
background(0);
var spectrum = fft.analyze();
// console.log(spectrum);
// stroke(255);
noStroke();
for (var i = 0; i < spectrum.length; i++) {
var amp = spectrum[i];
var y = map(amp, 0, 256, height, 0);
rect(i * w, y, w - 2, height - y);
// fill(i, 255, 255);
fill(255, 255, 255);
}
}