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(239,68,14);
// read mic-input intensity
let intensity = mic.getLevel();
let sizeOfCircle = map(intensity,0,10,80, 10000);
// let sizeOfRect = map(intensity,0,5,10, 9000000);
// 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(11, 128, 64);
ellipse(0,0, sin((frameCount + (i*a))*0.08) * 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.1)*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(209, 242, 0);
ellipse(0,0, sin((frameCount+(i*a))*0.01)*smoothSizeOfCircle)
pop();
}
}
pop();
push();
fill(255, 94, 53);
translate(20,20);
text("Intensity: " + nfs(intensity,0,10),0,0);
pop();
}