xxxxxxxxxx
76
var song;
var fft;
var button;
//var mic;
let w;
let bins = 16;
let smoothingValue = 0.0;
let soundWindowWidth = 128;
let soundWindowHeight = 128;
function toggleSong(){
if(song.isPlaying()){
song.pause();
}else{
song.play();
}
}
function preload(){
song = loadSound('bedtime.mp3')
}
let timer = 0;
function setup() {
createCanvas(600, 600);
background(100);
angleMode(DEGREES);
colorMode(HSB);
button = createButton('toggle');
button.mousePressed(toggleSong);
//mic = new p5.AudioIn();
//mic.start();
song.play();
fft = new p5.FFT(smoothingValue,bins);
w = soundWindowWidth/bins;
}
function draw() {
let spectrum = fft.analyze();
//stroke(255);
noStroke();
fill(0);
rect(0,0,soundWindowWidth,soundWindowHeight);
for(let i = 0; i < spectrum.length; i++){
let amp = spectrum[i];
let ampHeight = map(amp,0,256,soundWindowHeight,0);
fill(i,255,255);
rect(i * w, ampHeight, w - 2, soundWindowHeight - ampHeight);
}
fill(0,0,0,0.1);
rect(0,0,width,height);
let bass1 = spectrum[0];
let bass2 = spectrum[1];
//let bassAverage = (bass1 + bass2)/2
if(bass1 >= 240 && bass2 >= 240){
if(timer > 2){
fill(random(255),random(255),random(255));
circle(random(0,width),random(soundWindowHeight,height),10);
}
timer = 0;
}else{
timer ++;
}
}