xxxxxxxxxx
88
// This code is inspired by the song Night Dancer. I created two parts of it: the first one is gentle and soothing, the second part is more intense and dynamic.
let song;
let paintbrushX;
let paintbrushY;
let offset = 0;
let amp;
let fft;
function preload() {
song = loadSound("nightDancer.mp3");
}
function setup() {
createCanvas(800, 400);
amp = new p5.Amplitude();
fft = new p5.FFT(0.9, 128);
fft.setInput(song);
song.play();
}
function draw() {
background(0, 50);
let now = round(millis() / 1000);
// first 9 seconds of the song
if (now < 10) {
drawFFT();
}
// after first 9 seconds of the song
if (now > 9) {
drawAmp(0, 0);
drawAmp(20, 30);
drawAmp(40, 90);
drawAmp(30, 60);
drawAmp(100, 50);
drawAmp(70, 100);
drawAmp(1, 4);
drawAmp(0, 50);
}
}
function drawFFT() {
{
background(0, 20);
let spectrum = fft.analyze();
noStroke();
for (let i = 0; i < spectrum.length; i++) {
strokeWeight(15);
let amp = spectrum[i];
let y = map(amp, 0, 256, 0, height);
let x = map(i, 0, spectrum.length, 0, width);
let redValue = map(amp, 0, 256, 0, 255);
fill(redValue, x, y, 20);
ellipse(x, height / 2, amp);//color and size of the ellipse determined by the amplitude (amp) and frequency index (i).
}
}
}
function drawAmp(a, b) {
let vol = amp.getLevel();
let rh = map(vol, 0, 1, 10, height);
paintbrushX = noise(offset + a) * width;
paintbrushY = noise(offset + b) * height;
r = noise(offset + 2) * 255;
g = noise(offset) * 255;
b = noise(offset * 0.1) * 255;
stroke(r, g, b, 100);
strokeWeight(rh);//changing accroding to the volume
point(paintbrushX, paintbrushY);
offset = offset + 0.001;
}
function mousePressed() {
let fs = fullscreen();
fullscreen(!fs);
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}