xxxxxxxxxx
98
let fft;
let song;
let amplitude;
let Particle = function(position) {
this.position = position;
this.speed = createVector(1, 1);
this.accelerationX = 0;
this.color = [0, 0, 0];
this.lifetime = random(200, 1500);
this.start = performance.now();
this.size = random(10, 15);
this.draw = function() {
blendMode(ADD);
circle(this.position.x, this.position.y, this.diameter);
fill(this.color);
}
this.update = function (energy) {
let windDirection = (mouseX - windowWidth / 2) / (windowWidth / 2);
this.accelerationX = random(-1, 1);
this.speed.x = max(min(this.speed.x + this.accelerationX, 2 + 1.5 * windDirection), -2 + 1.5 * windDirection);
this.position.x += this.speed.x;
this.position.y -= this.speed.y * (energy / 255 * 155 + amplitude.getLevel() * 100) / 255 * 10;
if (this.position.x < 0) {
this.position.x = windowWidth;
}
if (this.position.x > windowWidth) {
this.position.x = 0;
}
if (this.position.y < 0) {
this.position.y = windowHeight;
this.lifetime = random(200, 1500);
}
if (performance.now() - this.start > this.lifetime) {
this.position.y = windowHeight;
this.lifetime = random(200, 1500);
this.start = performance.now();
}
this.diameter = this.size * ((this.lifetime - (performance.now() - this.start)) / this.lifetime);
this.color = [
map(energy / 255 * 155 + amplitude.getLevel() * 100, 0, 255, 0, 43),
map(amplitude.getLevel(), 0, 0.6, 255, 0),
energy
];
}
}
function setup() {
createCanvas(windowWidth, windowHeight);
colorMode(HSB);
noStroke();
song = loadSound('dies_irae.mp3');
fft = new p5.FFT();
amplitude = new p5.Amplitude();
fft.setInput(song);
positionParticles();
}
function mousePressed() {
if (song.isPlaying()) {
song.stop();
} else {
song.play();
}
}
function draw() {
blendMode(BLEND);
background(0, 60);
if (!song.isPlaying()) {
textSize(14);
fill(255);
textAlign('center')
text('Move mouse to control wind direction', windowWidth / 2, 50);
textSize(14);
fill(255);
textAlign('center')
text('Click to begin', windowWidth / 2, 70);
}
textSize(32);
fill(255);
textAlign('center')
text('Verdi: Dies Irae', windowWidth / 2, windowHeight / 2);
fill(0);
drawParticles();
let spectrum = fft.analyze();
updateParticles(spectrum);
}