xxxxxxxxxx
62
let mic;
let song;
let button;
let fft;
function playSong() {
if (song.isPlaying()) {
song.pause();
} else {
song.play();
}
}
function preload() {
song = loadSound("peaches.mp3");
}
function setup() {
createCanvas(300, 300);
colorMode(HSB);
angleMode(DEGREES);
button = createButton("pause");
button.mousePressed(togglePlaying);
song.play();
fft = new p5.FFT(0.9, 64);
}
function togglePlaying() {
if (!song.isPlaying()) {
song.play();
song.setVolume(0.3);
button.html("pause");
} else {
song.pause();
button.html("play");
}
}
function draw() {
background(0);
let spectrum = fft.analyze();
noStroke();
translate(width / 2, height / 2);
beginShape();
for (let i = 0; i < spectrum.length; i++) {
let angle = map(i, 0, spectrum.length, 0, 360);
let amp = spectrum[i];
let r = map(amp, 0, 256, 50, 100);
let x = r * cos(angle);
let y = r * sin(angle);
stroke(255 - i, 255, 255);
line(0, 0, x, y);
//vertex(x, y);
// let y = map(amp, 0, 255, height, 0);
// line(i * w, height, i * w, y);
// rect(i * w, y, w - 2, height - y);
}
endShape();
// console.log(spectrum);
}