xxxxxxxxxx
202
// Certain parts of the code - specifically the parts concerning midi and oscillation - are taken/inspired by the following sketch: https://editor.p5js.org/monniqian/sketches/TwckaaeGl
//Declare Variables
var notes = [38, 57, 60, 74, 77];
var osc;
var key;
let keys = [];
let b1P = false;
let b2P = false;
let b3P = false;
let b4P = false;
let b5P = false;
let button6;
let moodChord;
//Create Array for Check Circle
let check = [];
//Create Class for Check Circle
class Check{
constructor(){
this.xPos = 510;
this.yPos = 470;
this.colour = 100;
}
draw(){
fill(this.colour);
ellipse(this.xPos, this.yPos, 20);
}
}
//Preload Font, Images, Sounds
function preload() {
bgImage = loadImage("SMGLibrary.jpg");
moodChord = loadSound("MoodChord.mp3");
}
//Set Up Buttons and Notes
function setup() {
createCanvas(700, 500);
image(bgImage, 0, 0, 700, 500);
let button = createButton('Happy');
button.size(100, 50);
button.position(30, 370);
button.style("font-family", "Courier New");
button.style("font-size", "20px");
button.mousePressed(b1Pressed);
let button2 = createButton('Sad');
button2.size(100, 50);
button2.position(150, 370);
button2.style("font-family", "Courier New");
button2.style("font-size", "20px");
button2.mousePressed(b2Pressed);
let button3 = createButton('Angry');
button3.size(100, 50);
button3.position(270, 370);
button3.style("font-family", "Courier New");
button3.style("font-size", "20px");
button3.mousePressed(b3Pressed);
let button4 = createButton('Excited');
button4.size(100, 50);
button4.position(90, 435);
button4.style("font-family", "Courier New");
button4.style("font-size", "20px");
button4.mousePressed(b4Pressed);
let button5 = createButton('Anxious');
button5.size(100, 50);
button5.position(210, 435);
button5.style("font-family", "Courier New");
button5.style("font-size", "20px");
button5.mousePressed(b5Pressed);
button6 = createButton('This Is How We Feel');
button6.size(150, 80);
button6.position(490, 370);
button6.style("font-family", "Courier New");
button6.style("font-size", "18px");
button6.mousePressed(b6Pressed);
for (let i = 0; i < 5; i++){
check[i] = new Check();
}
// A triangle oscillator
osc = new p5.TriOsc();
// Start silent
osc.start();
osc.amp(0);
}
// A function to play a note
function playNote(note, duration) {
osc.freq(midiToFreq(note));
// Fade it in
osc.fade(0.5,0.2);
// If we set a duration, fade it out
if (duration) {
setTimeout(function() {
osc.fade(0,0.2);
}, duration-50);
}
}
function draw() {
textFont('Courier New');
textSize(20);
fill('white')
text("Hello. Welcome to Mindscape.", 50, 50);
text("Take a seat and tell us how you feel today.", 30, 140);
text("Sing us your song. Alone . .", 110, 170);
text("Strike a chord. Together . .", 70, 200);
text("I feel . . .", 40, 350);
text("We feel . . .", 500, 350);
// Only show button for combined chord when all notes have been pressed at least once
if (keys.length < 5){
button6.hide();
} else {
button6.show();
}
fill(100);
rect(490, 370, 150, 80);
fill('white');
text("Everyone", 500, 390);
text("can", 590, 415);
text("sing", 550, 440);
for (let i = 0; i < 5; i++){
check[i].xPos = 510 + (i * 27.5);
check[i].draw();
}
}
// Fade note upon release
function mouseReleased() {
osc.fade(0,0.5);
}
// Button Press Functions
function b1Pressed() {
key = 3;
print(key);
playNote(notes[key]);
if (b1P == false){
keys.push(key);
}
b1P = true;
check[3].colour = 255;
print(keys);
}
function b2Pressed() {
key = 1;
print(key);
playNote(notes[key]);
if (b2P == false){
keys.push(key);
}
b2P = true;
check[1].colour = 255;
print(keys);
}
function b3Pressed() {
key = 0;
print(key);
playNote(notes[key]);
if (b3P == false){
keys.push(key);
}
b3P = true;
check[0].colour = 255;
print(keys);
}
function b4Pressed() {
key = 4;
print(key);
playNote(notes[key]);
if (b4P == false){
keys.push(key);
}
b4P = true;
check[4].colour = 255;
print(keys);
}
function b5Pressed() {
key = 2;
print(key);
playNote(notes[key]);
if (b5P == false){
keys.push(key);
}
b5P = true;
check[2].colour = 255;
print(keys);
}
function b6Pressed() {
moodChord.play();
}