xxxxxxxxxx
243
let numFlowers = 50;
let spring = 0.005;
let gravity = 0.0005;
let friction = -0.7;
let flowers = [];
let pathList = [];
//this array will have 37 notes - 3 octaves of a chromatic scale on a flute
let chordList = [];
var playing;
var hr;
var mn;
var sec;
function preload() {
//load individual note files
lowC = loadSound('lowC.mp3');
lowCs = loadSound('lowCs.mp3');
lowD = loadSound('lowD.mp3');
lowDs = loadSound('lowDs.mp3');
lowE = loadSound('lowE.mp3');
lowF = loadSound('lowF.mp3');
lowFs = loadSound('lowFs.mp3');
lowG = loadSound('lowG.mp3');
lowGs = loadSound('lowGs.mp3');
lowA = loadSound('lowA.mp3');
lowAs = loadSound('lowAs.mp3');
lowB = loadSound('lowB.mp3');
midC = loadSound('midC.mp3');
midCs = loadSound('midCs.mp3');
midD = loadSound('midD.mp3');
midDs = loadSound('midDs.mp3');
midE = loadSound('midE.mp3');
midF = loadSound('midF.mp3');
midFs = loadSound('midFs.mp3');
midG = loadSound('midG.mp3');
midGs = loadSound('midGs.mp3');
midA = loadSound('midA.mp3');
midAs = loadSound('midAs.mp3');
midB = loadSound('midB.mp3');
hiC = loadSound('hiC.mp3');
hiCs = loadSound('midCs.mp3');
hiD = loadSound('midD.mp3');
hiDs = loadSound('midDs.mp3');
hiE = loadSound('midE.mp3');
hiF = loadSound('midF.mp3');
hiFs = loadSound('midFs.mp3');
hiG = loadSound('midG.mp3');
hiGs = loadSound('midGs.mp3');
hiA = loadSound('midA.mp3');
hiAs = loadSound('midAs.mp3');
hiB = loadSound('midB.mp3');
//hiCs = loadSound('notes/midCs.mp3');
flower1 = loadImage("purple.png");
flower2 = loadImage("pink2.png");
flower3 = loadImage("yellow.png");
flower4 = loadImage("red.png");
}
function setup() {
const width = 500;
const height = width;
createCanvas(500, 500);
pathList = [flower1, flower2, flower3, flower4]
for (let i = 0; i < numFlowers; i++) {
flowers[i] = new Flower(
random(width),
random(height),
random(30, 70),
i,
flowers,
random(pathList)
);
}
playing = true;
chordList = [
cAdd4 = new Chord([lowC, lowE, midC, midF, midA, hiC, hiE]),
dFlatSus2 = new Chord([lowCs, midCs, midGs, hiCs, hiDs, hiGs]),
dMaj9 = new Chord([lowD, midD, midA, hiCs, hiE, hiFs, hiA]),
eFlatMaj9 = new Chord([lowDs, lowAs, midDs, midF, midG, midAs, hiD]),
eMin9 = new Chord([lowE, lowB, midE, midG, midB, hiD, hiFs]),
fMaj7 = new Chord([lowF, lowA, midC, midF, midA, hiC, hiE]),
fSharpMin11 = new Chord([lowFs, midCs, midFs, midA, midB, hiE, hiGs]),
gMaj9 = new Chord([lowG, lowA, midD, midG, midA, hiD, hiFs]),
aFlatMin9 = new Chord([lowGs, midDs, midGs, midB, hiDs, hiFs, hiAs]),
aMaj9 = new Chord([lowA, lowB, midE, midGs, midB, hiCs, hiE]),
bFlat6 = new Chord([lowAs, midF, midG, midC, midAs, hiF, hiG]),
bMin7Add11 = new Chord([lowB, midFs, midA, midB, hiD, hiE, hiA]),
]
}
function draw() {
//flowers
background(3, 23, 43);
flowers.forEach(flower => {
flower.collide();
flower.move();
flower.display();
//music
hr = (hour() % 12);
//console.log(hr);
mn = minute();
sec = second();
//console.log(hr);
//console.log(chordList);
//change the chord every hour
//minutes are the third note of the chord
//seconds change
var currentChord = chordList[hr];
//console.log(currentChord);
currentChord.timePlay();
//currentChord.mousePlay();
});
}
//simulation code adapted from an example from the p5 website:
//https://p5js.org/examples/motion-bouncy-bubbles.html
//flower class
class Flower {
constructor(xin, yin, din, idin, oin, flower) {
this.x = xin;
this.y = yin;
this.vx = 0;
this.vy = 0;
this.diameter = din;
this.id = idin;
this.others = oin;
this.width = random(50, 150);
this.height = random(10);
this.flower = flower;
this.rotation = random(360);
//console.log(this.flower);
}
collide() {
for (let i = this.id + 1; i < numFlowers; i++) {
// console.log(others[i]);
let dx = this.others[i].x - this.x;
let dy = this.others[i].y - this.y;
let distance = sqrt(dx * dx + dy * dy);
let minDist = this.others[i].diameter / 2 + this.diameter / 2;
// console.log(distance);
//console.log(minDist);
if (distance < minDist) {
//console.log("2");
let angle = atan2(dy, dx);
let targetX = this.x + cos(angle) * minDist;
let targetY = this.y + sin(angle) * minDist;
let ax = (targetX - this.others[i].x) * spring;
let ay = (targetY - this.others[i].y) * spring;
this.vx -= ax;
this.vy -= ay;
this.others[i].vx += ax;
this.others[i].vy += ay;
}
}
}
move() {
this.vy += gravity;
this.x += this.vx;
this.y += this.vy;
if (this.x + this.diameter / 2 > width) {
this.x = width - this.diameter / 2;
gravity *= -1
this.vx *= friction;
} else if (this.x - this.diameter / 2 < 0) {
this.x = this.diameter / 2;
gravity *= -1
this.vx *= friction;
}
if (this.y + this.diameter / 2 > height) {
this.y = height - this.diameter / 2;
gravity *= -1
this.vy *= friction;
} else if (this.y - this.diameter / 2 < 0) {
this.y = this.diameter / 2;
gravity *= -1
this.vy *= friction;
}
}
display() {
push();
//translate(this.x, this.y);
rotate((radians(this.rotation)));
//imageMode(CENTER);
image(this.flower, this.x, this.y, this.width, this.width);
pop();
}
}
//chord class
class Chord {
constructor(notes) {
this.bass = notes[0];
var altoNote = round(random(2, 4));
this.alto = notes[altoNote];
//var soprNote = round(random(3, 5));
this.sopr = notes[3];
this.chrdNotes = notes;
this.bass.setVolume(0.03)
this.alto.setVolume(0.01)
this.sopr.setVolume(0.01)
}
timePlay() {
//play 3 files here
if (playing) {
var soprNote = sec % 5;
this.bass.play();
this.alto.play();
//this.sopr.play();
this.sopr.stop();
this.sopr = this.chrdNotes[soprNote];
this.sopr.play();
} else {
this.bass.stop();
this.alto.stop();
this.sopr.stop();
}
this.bass.setVolume(0.03)
this.alto.setVolume(0.01)
this.sopr.setVolume(0.01)
}
mousePlay() {
//map chord list to width
map(mouseX, 0, width, 0, this.chrdNotes.length);
this.chrdNotes[mouseX].play();
}
}
function mousePressed() {
if (playing) {
playing = false;
} else {
playing = true;
}
}