xxxxxxxxxx
72
// Analize the volume
alert('clicca per far partire l\'audio')
let song, amp
let history = []
function preload(){
song = loadSound('assets/sunny.mp3');
}
function setup(){
let cnv = createCanvas(windowWidth,windowHeight);
cnv.mouseClicked(togglePlay);
// Classe per audio che carichi
amp = new p5.Amplitude();
amp.setInput(song)
// Classe per mic
mic = new p5.AudioIn();
mic.start();
}
function draw(){
clear()
noFill()
// Volume traccia che carichi
let vol = amp.getLevel();
// Volume microfono
let micVol = mic.getLevel();
history.push(vol)
push()
stroke('red');
strokeWeight(2.5);
beginShape();
for(let i = 0; i < history.length; i++){
// Lo 0.1 è in base al valore del volume in basso
let y = map(history[i], 0, 0.1, height / 2, 0)
curveVertex(i * 2, y)
}
endShape();
if(history.length > width / 2){
history.splice(0,1)
}
pop()
}
function togglePlay() {
if (song.isPlaying() ){
song.pause();
} else {
song.play();
song.setVolume(0.1)
}
}