-
Notifications
You must be signed in to change notification settings - Fork 81
/
sketch.js
55 lines (44 loc) · 1.15 KB
/
sketch.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
47
48
49
50
51
52
53
54
55
/**
* p5.PeakDetect listens for peaks at a specific part of the
* frequency spectrum.
*
* In this example, we listen for the shaker.
*
* For more, see: http://p5js.org/reference/#/p5.PeakDetect
*/
var cnv, soundFile, fft, peakDetect;
var ellipseWidth = 10;
function setup() {
background(0);
createCanvas(windowWidth, windowHeight);
noStroke();
fill(255);
textAlign(CENTER);
soundFile = loadSound('../../music/YACHT_-_06_-_Summer_Song_Instrumental.mp3');
// p5.PeakDetect requires a p5.FFT
fft = new p5.FFT();
peakDetect = new p5.PeakDetect(4000, 12000, 0.2);
}
function draw() {
background(0);
text('click to play/pause', width/2, height/4);
// peakDetect accepts an fft post-analysis
fft.analyze();
peakDetect.update(fft);
if ( peakDetect.isDetected ) {
ellipseWidth = 300;
} else {
ellipseWidth *= 0.95;
}
ellipse(width/2, height/2, ellipseWidth, ellipseWidth);
}
// toggle play/stop when canvas is clicked
function mouseClicked() {
if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
if (soundFile.isPlaying() ) {
soundFile.stop();
} else {
soundFile.play();
}
}
}