9 / Créer un analyseur d’amplitude
var son;
var analyzer;
function preload() {
son = loadSound('sons/electro.mp3');
}
function setup() {
createCanvas(720, 200);
son.loop();
analyzer = new p5.Amplitude();
analyzer.setInput(son);
}
function draw() {
background(255);
var vol = analyzer.getLevel();
fill(236,36,94);
noStroke();
ellipse(width/2, height/2, 100+vol*800, 100+vol*800);
}
10 / Créer un analyseur d’amplitude (suite)
var taille;
var soundFile;
var amplitude;
var description;
var p1;
var smoothing = .01;
var smoothSlider, smoothLabel;
function preload() {
soundFile = loadSound('voix.mp3');
}
function setup() {
createCanvas(780, 300);
background(236,36,94);
noStroke();
fill(255);
amplitude = new p5.Amplitude(smoothing);
textFont("Helvetica");
description = "Touche S : pause/play<br><br>Touche N : activer Normalize";
p1 = createP(description);
smoothSlider = createSlider(0.0, 99.9, smoothing*100);
smoothLabel = createP('Lissage: ' + smoothing);
}
function draw() {
background(236,36,94);
var volume = amplitude.getLevel();
taille = map(volume, 0, 1.0, 25, 400);
ellipse(width/2, height/2, taille, taille);
smoothing = smoothSlider.value()/100;
smoothLabel.html('Lissage: ' + smoothing);
amplitude.smooth(smoothing);
}
function keyPressed() {
if (key == 's' || key == 'S') {
if (soundFile.isPlaying()) {
soundFile.pause();
} else {
soundFile.loop();
}
}
if (key == 'n' || key == 'N') {
amplitude.toggleNormalize();
}
}
11 /Tracer la courbe d’un son
function setup() {
createCanvas(780,300);
soundFormats('ogg', 'mp3');
soundFile = loadSound('sons/caisse', soundReady);
}
function soundReady(){
text('Cliquer pour jouer le son / Cliquer pour mettre en pause', 10, 10);
peaks = soundFile.getPeaks();
beginShape();
for (i = 0; i< peaks.length; i++){
stroke(236,36,94);
vertex(map(i, 0, peaks.length, 0, width), map(peaks[i], -.2 , .2, height/2, 200) );
}
endShape();
}
function mousePressed(){
if (soundFile.isPlaying()){
soundFile.pause();
} else {
soundFile.play();
}
}