-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch2.js
46 lines (40 loc) · 1014 Bytes
/
sketch2.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
function preload(){
sound = loadSound('assets/ylvis-both-choruses.mp3');
}
function setup(){
var cnv = createCanvas(100,100);
cnv.mouseClicked(togglePlay);
fft = new p5.FFT();
sound.amp(0.2);
}
function draw(){
background(0);
var spectrum = fft.analyze();
noStroke();
fill(0,255,0); // spectrum is green
for (var i = 0; i< spectrum.length; i++){
var x = map(i, 0, spectrum.length, 0, width);
var h = -height + map(spectrum[i], 0, 255, height, 0);
rect(x, height, width / spectrum.length, h )
}
var waveform = fft.waveform();
noFill();
beginShape();
stroke(255,0,0); // waveform is red
strokeWeight(1);
for (var i = 0; i< waveform.length; i++){
var x = map(i, 0, waveform.length, 0, width);
var y = map( waveform[i], -1, 1, 0, height);
vertex(x,y);
}
endShape();
text('click to play/pause', 4, 10);
}
// fade sound if mouse is over canvas
function togglePlay() {
if (sound.isPlaying()) {
sound.pause();
} else {
sound.loop();
}
}