xxxxxxxxxx
105
let fft, mic, songfile;
let vizes = [];
let song;
function preload(){
song = loadSound("https://assets.editor.p5js.org/5875aa4887e17a6f70ef51bc/22868ab7-b4d5-4770-92d3-94f7604cdd6a.mp3")
}
function setup() {
createCanvas(windowWidth, windowHeight);
mic = new p5.AudioIn();
mic.start();
fft = new p5.FFT();
fft.setInput(mic);
//fft.setInput(song);
}
function draw() {
background(0);
let spectrum = fft.analyze();
let bass, lowMid, mid, highMid, treble;
bass = fft.getEnergy("bass");
lowMid = fft.getEnergy("lowMid");
mid = fft.getEnergy("mid");
highMid = fft.getEnergy("highMid");
treble = fft.getEnergy("treble");
let bins = [bass, lowMid, mid, highMid, treble]
for (let i = 0; i < bins.length; i++) {
fill(255);
rect(i * width / 5, height, width / 6, map(bins[i], 0, 255, 0, -255));
fill(255, 255,0);
rect(i * width / 5, height, width / 6, -20);
fill(0);
text(int(bins[i]), (i * width / 5) + 5, height - 5);
if (bins[i] > 70) {
newx = random(width);
newy = random(height);
newColor = color(random(255), random(255), random(255));
newSize = bins[i];
newXspeed = random(-2, 2);
newYspeed = random(-2, 2);
newGrowthRate = random(5, 10);
vizes.push(new Viz(newx,
newy,
newColor,
newSize,
newXspeed,
newYspeed,
newGrowthRate));
}
}
//loop through visuals
for (let i = 0; i < vizes.length; i++) {
vizes[i].animate();
// vizes[i].show();
if (vizes[i].sz > 150) {
vizes.splice(i, 1);
}
}
console.log("Bass: " + bass + " lowMid: " + lowMid + " mid: " + mid + " highMid: " + highMid + " treble: " + treble);
}
class Viz {
constructor(x, y, c, sz, xsp, ysp, growth) {
this.x = x;
this.y = y;
this.c = c;
this.sz = sz;
this.xsp = xsp;
this.ysp = ysp;
this.growth = growth;
}
show() {
noStroke();
fill(this.c);
ellipse(this.x, this.y, this.sz, this.sz);
}
animate() {
this.x += this.xsp;
this.y += this.ysp;
this.sz += this.growth;
}
}
function mousePressed(){
if(!song.isPlaying()){
song.play();
}else{
song.pause();
}
}