xxxxxxxxxx
56
let mSound;
let mFilter;
let mFFT;
let FLABELS = ["bass", "lowMid", "mid", "highMid", "treble"];
let MAX_DIAM;
function preload() {
mSound = loadSound("./epic-hip-hop.mp3");
}
function setup() {
createCanvas(windowWidth, windowHeight);
mFilter = new p5.Filter("bandpass");
mFilter.res(4);
mFilter.gain(8.0);
mFFT = new p5.FFT();
mSound.disconnect();
mFilter.disconnect();
mSound.connect(mFilter);
mFilter.connect(p5.soundOut);
mFilter.connect(mFFT);
strokeWeight(2);
stroke(0);
noFill();
MAX_DIAM = height / FLABELS.length;
}
function draw() {
background(220, 20, 120, 20);
let mFreq = map(mouseX, 0, width, 50, 2000, true);
mFilter.freq(mFreq);
mFFT.analyze();
for (let i = 0; i < FLABELS.length; i++) {
let y = map(i, 0, FLABELS.length, MAX_DIAM / 2, height + MAX_DIAM / 2);
let d = map(mFFT.getEnergy(FLABELS[i]), 0, 255, 5, MAX_DIAM);
ellipse(width / 2, height - y, d, d);
}
}
function mouseClicked() {
if (mSound.isPlaying()) {
mSound.pause();
} else {
mSound.play();
}
}