xxxxxxxxxx
53
var song;
var fft;
var button;
//var mic;
let w;
let bins = 16;
let smoothingValue = 0.0;
function toggleSong(){
if(song.isPlaying()){
song.pause();
}else{
song.play();
}
}
function preload(){
song = loadSound('bedtime.mp3')
}
function setup() {
createCanvas(256, 256);
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 = width/bins;
}
function draw() {
background(0);
let spectrum = fft.analyze();
//stroke(255);
noStroke();
for(let i = 0; i < spectrum.length; i++){
let amp = spectrum[i];
let ampHeight = map(amp,0,256,height,0);
fill(i,255,255);
rect(i * w, ampHeight, w - 2, height - ampHeight);
}
console.log(spectrum[0]);
}