xxxxxxxxxx
33
let mic;
let avg_fast = 0.0;
let avg_slow = 0.0;
function setup(){
createCanvas(400, 400);
mic = new p5.AudioIn();
mic.start();
}
function draw() {
background(50);
let level = mic.getLevel();
// calculate an average of the level that is less "jumpy"
avg_fast = avg_fast * 0.90 + level * 0.10;
// and one that is even less jumpy
avg_slow = avg_slow * 0.99 + level * 0.01;
noStroke();
fill(150, 0, 255);
ellipse(width * 0.25, height/2, map(level, 0.0, 1.0, 0, 1000));
ellipse(width * 0.50, height/2, map(avg_fast, 0.0, 1.0, 0, 1000));
ellipse(width * 0.75, height/2, map(avg_slow, 0.0, 1.0, 0, 1000));
fill(255);
text("Click to start the microphone!", 10, 20);
text("Volume: " + level, 10, 50);
}