xxxxxxxxxx
107
let shapes = [];
let mic;
let helvetica;
var onOff;
// let osc, env;
function preload() {
helvetica = loadFont('Helvetica.otf');
}
function setup() {
createCanvas(windowWidth, windowHeight);
mic = new p5.AudioIn();
mic.start();
textFont(helvetica);
}
function draw() {
background(0,10);
noFill();
// read mic-input
let intensity = mic.getLevel();
let audioInThreshold = 0.05;
if(intensity > audioInThreshold) {
let x = random(width);
let y = random(height);
let s = intensity * 1000;
shapes.push(new Circle(x,y,s));
}
shapes.forEach(item => item.draw());
shapes = shapes.filter(item => item.isAlive());
push();
translate(width / 2, height /2);
fill(255);
textSize(10);
text('Unmute mic and click on silence to trigger action', 105,50);
textSize(50);
text('Sounds like',100, 100);
if(onOff==true){
translate(random(-5,5),random(-5,5));
fill(118, 120, 237, 50);
background(255);
}
text('silence',370,100);
pop();
}
class Circle {
constructor(theX, theY, theSize) {
this.x = theX;
this.y = theY;
this.size = theSize;
this.age = 0;
this.limit = 80;
}
draw() {
let s = map(this.age,0,this.limit,0,PI);
let xOffset = sin(s*1.25) * 2 * 100;
push();
translate(this.x ,this.y - xOffset);
stroke(175, 252, 65);
ellipse(0,0,sin(s)*this.size);
stroke(0,139,248);
rect(0,0,sin(s)*this.size);
stroke(242,0,137);
triangle(30,75,58,20,86,75);
pop();
this.age++;
}
isAlive() {
if(this.age>this.limit) {
return false;
} else {
return true;
}
}
}
function mousePressed(){
if(onOff==true){
onOff=false;
}else{
onOff=true;
}
}