xxxxxxxxxx
84
let song;
let fft;
let particles = [];
let glyphs = ['?', '!', '-', '='];
const num = 5000; //initial number of particles
const noiseScale = 0.001;
function preload() {
song = loadSound('I got this short.mp3');
}
function setup() {
createCanvas(800, 500);
colorMode(RGB);
for (let i = 0; i < num; i++) {
let particle = {
position: createVector(random(width), random(height)),
glyph: random(glyphs) // Assign a random glyph
};
particles.push(particle);
}
stroke(53,52,64);
strokeWeight(5);
fft = new p5.FFT();
song.play();
}
function draw() {
background(193,203,217, 30); //second value messes with how fast the trails disappear
let spectrum = fft.analyze();
let bass = fft.getEnergy(40, 100); //kick drum frequency range
let volume = fft.getEnergy(150, 10000); //adjust the range
for (let i = 0; i < num; i++) {
let p = particles[i];
let prevX = p.x;
let prevY = p.y;
let freqValue = spectrum[i % spectrum.length];
let minSize = 2;
let maxSize = 3;
let dynamicSize = map(freqValue, 0, 255, 0, maxSize - minSize);
let particleSize = minSize - dynamicSize;
strokeWeight(particleSize * 5);
point(p.x, p.y); // Draw particle
let n = map(freqValue, 0, 255, 0, 1);
let noiseFactor = noise(p.x * noiseScale, p.y * noiseScale) * TAU * 2;
let a = TAU * n + noiseFactor;
let speed = map(volume, 0, 255, 1, 10); //speed based on volume
//react more strongly to bass frequencies
if (bass > 180) { //threshold for bass
speed *= 5;
particleSize *= bass;
}
//if (volume > ){
//}
//move particle
p.x += cos(a^(random(1,10))) * speed;
p.y += sin(a) * speed;
//line(prevX, prevY, p.x, p.y);
if (!onScreen(p)) {
p.x = random(width);
p.y = random(height);
}
}
}
function onScreen(v) {
//checks whether or not a particle is on the screen
return v.x >= 0 && v.x <= width && v.y >= 0 && v.y <= height;
}