xxxxxxxxxx
65
let label = "your-label-here";
let particles = [];
let mic, fft;
function setup() {
createCanvas(windowWidth, windowHeight);
colorMode(HSB);
noStroke();
mic = new p5.AudioIn();
mic.start();
fft = new p5.FFT();
fft.setInput(mic);
for (let i = 0; i < width; i += 20) { // Change increment to 20 for fewer particles
for (let j = 0; j < height; j += 20) { // Change increment to 20 for fewer particles
particles.push({
x: i,
y: j,
clr: color(random(178, 255), 100, 100),
xOff: random(1000),
yOff: random(2000),
});
}
}
}
function draw() {
background(243, 78, 28, 0.01);
let spectrum = fft.analyze();
for (let i = 0; i < particles.length; i++) {
let p = particles[i];
let spectrumIndex = Math.floor(map(p.x, 0, width, 0, spectrum.length - 1));
let amplitude = spectrum[spectrumIndex];
p.x += random(-amplitude, amplitude);
p.y += random(-amplitude, amplitude);
p.x += map(noise(p.xOff), 0, 1, -1, 1);
p.y += map(noise(p.yOff), 0, 1, -1, 1);
p.xOff += 0.01;
p.yOff += 0.01;
let hue = map(amplitude, 0, 255, 178, 255);
p.clr = color(hue, 100, 100);
fill(p.clr);
ellipse(p.x, p.y, 1);
}
}
function keyPressed() {
if (key === "s") {
if (this._renderer.elt.svg !== undefined) {
saveSVG(label + ".svg");
} else {
saveCanvas(label + ".png");
}
}
}