xxxxxxxxxx
260
// Camera
let angle = 0;
let cam;
let stars = [];
let song;
let amp;
let ss;
let blackHole;
let button;
let fft;
let detailX=3;
let detailXlist, detailXval;
let starcolor = ["#EDF67D","#CEEAF7","#F73B41","#F7AF9D"]
let colorCount = 0;
let bg = 0;
let volHistory = [];
let colors = [];
let numParticles = 500;
let noiseScale = 500, noiseStrength = 3
let particles = [numParticles];
let font1, font2, font3,
fontsize = 40;
// ending
let strokeW= 3;
function changeDetailX(val){
stars.push(new ShootingStar(cos(val)*200,sin(val)*200,-1000,10,255))
}
function togglePlay(){
if (!song.isPlaying()){
song.play();
button.html("Play");
} else {
song.pause();
button.html("Pause")
}
}
function drawWords(x) {
// The text() function needs three parameters:
// the text to draw, the horizontal position,
// and the vertical position
push()
textFont(font1);
textSize(fontsize);
textAlign(CENTER, CENTER);
fill(255);
noStroke();
text('Spaced-Out Sound', 0, -50);
pop();
push();
noStroke();
textFont(font2);
textSize(fontsize-14);
textAlign(CENTER, CENTER);
fill(255);
text('Chenyi Li, Jonathan Yun', 0, 0);
pop();
push();
noStroke();
textFont(font3);
textSize(fontsize-14);
textAlign(CENTER, CENTER);
fill(255);
text('Music: Adagio by Stan Kolev', 0, 50);
}
function createPulse(){
pulseSize = 250;
}
function preload() {
song = loadSound('adagio_cut2.mp3');
font1 = loadFont('assets/AllerDisplay.ttf');
font2 = loadFont('assets/Aller_Rg.ttf')
font3 = loadFont('assets/Aller_It.ttf')
}
function setup() {
createCanvas(800, 800, WEBGL);
camera(0, 0, (height/2) / tan(PI/6), 0, 0, 0, 0, 1, 0)
amp = new p5.Amplitude();
fft = new p5.FFT(0.9,256);
song.setVolume(0.02);
ss = new SpaceShip(defaultSpaceShip);
blackHole = new BlackHole(10);
noStroke();
for (let i=0; i<numParticles; i++) {
//x value start slightly outside the right of canvas, z value how close to viewer
var loc = createVector(random(width*1.2), random(height), 2);
var angle = 0; //any value to initialize
var dir = createVector(cos(angle), sin(angle));
var speed = random(0.5,2);
// var speed = random(5,map(mouseX,0,width,5,20)); // faster
particles[i]= new Particle(loc, dir, speed);
}
for (let x =0; x < 22; x ++) {
stars.push(new SlowStar(cos(x*36)*300,
sin(x*36)*300,
-3000,
1,
starcolor[colorCount]
))
colorCount = (colorCount +1)%4;
}
// phase1
detailXlist = [11,11.1,11.2,11.3,43,43.1,43.2,43.3]
detailXval = [1,2,3,4,5,6,7,8]
for (let i = 0; i < detailXlist.length; i += 1) {
song.addCue(detailXlist[i], changeDetailX, detailXval[i]);
}
//phase 2
//Define the color pallet
for(let i = 0; i < scale.length; i++) {
let newColor = color(i*360/scale.length, 50, 100, 0.9);
colors.push(newColor);
}
stroke(0, 0, 0, 0.5);
for (let _ = 0; _ < 200; _++) {
volHistory.push(127);
}
// text
}
function keyPressed(){
if(!song.isPlaying()) {
song.play();
}
}
function draw() {
if (song.isPlaying()){
// phase1
if (song.currentTime() <= 67) {
background(bg);
blackHole.render();
// adding new objects
if (frameCount%50==0){
for (let x =0; x < 11; x ++) {
stars.push(new SlowStar(cos(x*36)*random(300, 1000),
sin(x*36)*random(300, 1000),
-8000,
1,
starcolor[colorCount]));
colorCount = (colorCount +1)%4;
}
}
// display and move
for (let star of stars) {
star.move();
star.render();
}
if (stars.length>200){
stars.splice(0,10)
}
ss.render([random(-0.3, 0.3), random(-0.3, 0.3), random(-0.3, 0.3)]);
} else if (67 < song.currentTime() && song.currentTime() < 74){
background(bg);
push();
stroke(255);
line(-400, 0, 400, 0);
pop();
blackHole.render();
ss.render([random(-0.1, 0.1), random(-0.1, 0.1), random(-0.1, 0.1)]);
} else if (song.currentTime()>74 && song.currentTime() < 124){
stars = [];
push();
// let vol = amp.getLevel();
let spectrum = fft.analyze();
stroke(255);
strokeWeight(strokeW);
noFill();
beginShape();
volHistory.push(max(spectrum));
for (let i = 0; i < volHistory.length; i++) {
let h = map(volHistory[i], 0, 255, 0, height / 2);
vertex(i * width / volHistory.length - width / 2, h - 200);
}
endShape();
if (volHistory.length > 200){
volHistory.shift();
}
pop();
push();
translate(-400,-400)
fill(0, 10);
noStroke();
rect(0, 0, width, height);
for (let i=0; i<particles.length; i++) {
particles[i].run();
}
pop();
ss.render([random(-0.1, 0.1), random(-0.1, 0.1), random(-0.1, 0.1)]);
} else {
push();
// let vol = amp.getLevel();
let spectrum = fft.analyze();
strokeW += 20;
stroke(255);
strokeWeight(strokeW);
noFill();
beginShape();
volHistory.push(max(spectrum));
for (let i = 0; i < volHistory.length; i++) {
let h = map(volHistory[i], 0, 255, 0, height / 2);
vertex(i * width / volHistory.length - width / 2, h - 200);
}
endShape();
if (volHistory.length > 200){
volHistory.shift();
}
pop();
}
}
else {
// textAlign(CENTER);
drawWords(width * 0.5);
}
}