xxxxxxxxxx
72
/**
* Audio: 'Ain't we got fun' performed by the Benson Orchestra
* Public Domain.
* http://www.digitalhistory.uh.edu/music/music.cfm
*/
let song;
let fft;
function preload(){
song = loadSound('assets/aint_we_got_fun_benson_orch.mp3');
// song = loadSound('assets/aint_we_got_fun_benson_orch-8.wav');
}
function setup() {
createCanvas(windowWidth, windowHeight);
stroke(255,0,0);
strokeWeight(4);
noFill();
textAlign(CENTER, CENTER);
textStyle(BOLD);
textSize(24);
// song.play();
fft = new p5.FFT();
}
function draw() {
background(0);
let waveform = fft.waveform();
if (song.isPlaying()){
stroke(255, 0, 0);
} else {
stroke(150);
}
for (let i = 0; i< waveform.length; i++){
let x = map(i, 0, waveform.length, 0, width);
let y = map( waveform[i], -1, 1, 0, height);
point(x,y);
}
if (!song.isPlaying()){
push();
noStroke();
fill(150);
rect(width / 2 - 100, height / 2 - 50, 200, 100, 20);
fill(0);
text("Click to play", width / 2, height / 2);
pop();
}
}
function toggleSongPlayback(){
if (song.isPlaying()){
song.pause();
} else {
song.play();
}
}
function mouseClicked(){
toggleSongPlayback();
}
function keyPressed(){
toggleSongPlayback();
}