xxxxxxxxxx
89
let song;
let mic;
let smoothSizeOfCircle = 0;
function setup() {
song = loadSound('assets/tame_impala_a.mp3');
createCanvas(windowWidth, windowHeight);
mic = new p5.AudioIn();
mic.start();
}
function mousePressed() {
if (song.isPlaying()) {
// .isPlaying() returns a boolean
song.stop();
background(255, 0, 0);
} else {
song.play();
background(0, 255, 0);
}
}
function draw() {
background(17, 24, 38);
// read mic-input intensity
let intensity = mic.getLevel();
let sizeOfCircle = map(intensity,0,1,10, 2000);
let sizeOfRect = map(intensity,0,5,10, 9000);
// smoothen the change in intensity
smoothSizeOfCircle += (sizeOfCircle - smoothSizeOfCircle) * 0.5;
let n =8;
let w = width/n;
let h =height/n;
strokeWeight(2)
//circle 1
push();
translate(w/2,h/2);
for(let i=0; i<n; i++) {
for(let a=0; a<n; a++){
push()
translate(i*w,a*h)
fill(117, 255, 67);
ellipse(0,0, sin((frameCount + (i*a))*0.04) * smoothSizeOfCircle);
pop();
}
}
pop();
// circle 2
push();
translate(w/2,h/2);
for(let i=0; i<n; i++) {
for(let a=0; a<n; a++){
push()
translate(i*w,a*h)
fill(217, 24, 165);
ellipse(0,0, sin((frameCount+(i*a))*0.05)*smoothSizeOfCircle)
pop();
}
}
pop();
// circle 3
push();
translate(w/2,h/2);
for(let i=0; i<n; i++) {
for(let a=0; a<n; a++){
push()
translate(i*w,a*h)
fill(217, 216, 215);
ellipse(0,0, sin((frameCount+(i*a))*0.01)*smoothSizeOfCircle)
pop();
}
}
pop();
push();
fill(140, 121, 109);
translate(20,20);
text("Intensity: " + nfs(intensity,0,3),0,0);
pop();
}