xxxxxxxxxx
242
var song
var fft
var amplitude
var bgColor = 0
var timer
var angle = 0
var delay = 10000000
function preload() {
soundFormats('mp3', 'ogg');
song = loadSound('Ableton assignment.mp3')
fft = new p5.FFT()
outputVolume(.3)
}
function setup() {
createCanvas(windowWidth, windowHeight);
background(100);
colorMode(HSB)
amplitude = new p5.Amplitude()
amplitude.setInput(song)
strokeWeight(2)
rectMode(CORNER)
ellipseMode(CORNER)
background(0)
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
function draw() {
if (millis() - delay < 12180) {
linewave()
}
if (millis() - delay > 12180) {
rectwave(); rectwave2()
}
if (millis() - delay > 36099) {
rectwave(); linewave2(); rectwave2();
}
if (millis() - delay > 60027) {
linewave3(); spiral();
}
}
function mouseClicked() {
if (!song.isPlaying()) {
let fs = fullscreen();
fullscreen(!fs);
delay = millis();
song.play();
}
}
function linewave() {
var wave = fft.waveform()
var amp = amplitude.getLevel()
bgColor = color('#00000033')
background(bgColor);
beginShape()
for (i = 0; i < width; i ++) {
var index = floor(map(i, 0, width, 0, wave.length))
noFill()
stroke(map(amp, 0, .5, 150, 360), 100, 100)
var x = i
var y = wave[index] * 150 + height / 2
vertex(x, y)
}
endShape()
}
function rectwave() {
var wave = fft.waveform()
var amp = amplitude.getLevel()
bgColor = color('#0000002D')
background(bgColor);
for (i = 0; i < width; i += 100) {
var index = floor(map(i, 0, width, 0, wave.length))
fill(map(amp, 0, 0.5, 150, 300), 100, 100)
noStroke()
var x = i
var y = wave[index] * 300 + 100
rect(x, height, 80, -y)
}
}
function linewave2() {
var wave = fft.waveform()
var amp = amplitude.getLevel()
beginShape()
for (i = 0; i < width; i ++) {
var index = floor(map(i, 0, width, 0, wave.length))
noFill()
stroke(map(amp, 0, 1, 50, 350), 100, 100)
var x = i
var y = wave[index] * 150 + height / 2
vertex(x, y)
}
endShape()
}
function rectwave2() {
var wave = fft.waveform()
var amp = amplitude.getLevel()
for (i = 0; i < width; i += 100) {
var index = floor(map(i, 0, width, 0, wave.length))
fill(map(amp, 0, 0.5, 150, 300), 100, 100)
noStroke()
var x = i
var y = wave[index] * 300 + 100
rect(x, 0, 80, y)
}
}
function spiral() {
var wave = fft.waveform()
var amp = amplitude.getLevel()
rectMode(CENTER)
translate(width/2, height/2)
for(i = 0; i < 40; i += 1) {
noFill()
stroke(map(amp, 0, .8, 150, 360), 100, 100)
scale(map(amp, 0, .8, 0.85, .95))
rotate(angle)
rect(0, 0, 500, 0)
rect(0, 0, map(amp, 0, 1, 800, 1000), map(amp, 0, 1, 800, 1000))
fill(map(amp, 0, .8, 100, 250), 100, 100)
noStroke()
circle(width/2, height/2, (map(amp, 0, 1, 50, 100)))
circle(-width/2, -height/2,(map(amp, 0, 1, 50, 100)))
}
angle += map(amp, 0, 1, 0, 0.04)
}
function linewave3() {
var wave = fft.waveform()
var amp = amplitude.getLevel()
background(0)
beginShape()
for (i = 0; i < width; i ++) {
var index = floor(map(i, 0, width, 0, wave.length))
noFill()
stroke(map(amp, 0, 1, 50, 350), 100, 100)
var x = i
var y = wave[index] * 150 + height / 2
vertex(x, y)
}
endShape()
}
function keyPressed() {
if(key == 's') save('MusicVisualizer.png')
}