xxxxxxxxxx
379
let whitekeys = ["C", "D", "E", "F", "G", "A", "B"];
// class for creating white keys on the keyboard
class WhiteKey {
constructor(x) {
this.note = whitekeys[x % 7];
this.octave = Math.floor(x / 7) + 3;
this.keycolor = "white";
this.keyname = this.note + str(this.octave);
}
// display function can be used in the future for animations
display(x) {
if (keyIsPressed) {
fill("white");
stroke("gray");
rect(10 + 30 * x, 300, 30, 150, 3);
fill("black");
textFont(font);
textAlign(CENTER);
text(whitekeys[x % 7], 25 + 30 * x, 435);
} else {
fill(this.keycolor);
stroke("gray");
rect(10 + 30 * x, 300, 30, 150, 3);
fill("black");
textFont(font);
textAlign(CENTER);
text(whitekeys[x % 7], 25 + 30 * x, 435);
}
}
}
let blackkeys = ["C#", "D#", "F#", "G#", "A#"];
// class for creating white keys on the keyboard
class BlackKey {
constructor(x, y) {
this.note = blackkeys[y % 5];
this.octave = Math.floor(y / 5);
}
display(x, y) {
fill("black");
rect(30 + 30 * x, 300, 20, 75, 3);
fill("white");
textFont(font);
textAlign(CENTER);
text(blackkeys[y % 5], 40 + 30 * x, 365);
}
}
function creatingbutton() {
// Code for creating buttons which would fit the page depending on the number of buttons
// Can be used for future ideas
let buttonwidth = (width - 10 * (numberofbuttons + 1)) / numberofbuttons;
for (i = 0; i < numberofbuttons; i++) {
noStroke();
fill(252, 207, 172);
rect(i * buttonwidth + 10 * (i + 1), height - 70, buttonwidth, 50, 3);
}
// creating a text for the buttons made
push();
textAlign(CENTER);
stroke("black");
textSize(14);
text("LEARN A NEW SONG (ADVANCED)", buttonwidth / 2 + 10, height - 41);
text(
"LEARN A NEW SONG (BEGINNER)",
buttonwidth / 2 + buttonwidth + 10,
height - 41
);
pop();
}
let screen = 0;
let advanced = 0;
let beginner = 0;
let numberofbuttons = 2;
let StartScreenImage;
let font;
let WhiteKeys = [];
let BlackKeys = [];
let soundfiles = [];
let ActiveButton;
// arrays with music sheets (translated to letters and keyboard)
let songs_advanced = [
"D-B-A-G-D-D-D-D-B-A-G-E E-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and functionality of the buttons
function mouseClicked() {
if (screen == 0) {
screen = 1;
}
if (screen == 1) {
// button functionality
if (
mouseX >= 10 &&
mouseX <= width / 2 - 7 &&
mouseY >= height - 70 &&
mouseY <= height - 20
) {
advanced += 1;
if (advanced == 4) {
advanced = 1;
}
ActiveButton = "advanced";
} else if (
mouseX >= width / 2 + 7 &&
mouseX <= width - 10 &&
mouseY >= height - 70 &&
mouseY <= height - 20
) {
beginner += 1;
if (beginner == 4) {
beginner = 1;
}
ActiveButton = "beginner";
}
}
}
let mainscreensound;
// Loading pictures, sounds, font, filling an array of piano notes
function preload() {
// loading images, font, soundfiles
StartScreenImage = loadImage("start_screen.png");
font = loadFont("Montserrat-VariableFont_wght.ttf");
soundFormats("mp3", "ogg");
mainscreensound = loadSound(
"x2mate.com - Ludovico Einaudi - Fly (Intouchables Soundtrack) (192 kbps).mp3"
);
let a = 0;
for (let letter = unchar("A"); letter < 72; letter++) {
for (i = 3; i < 6; i++) {
let fileName = char(letter) + i + ".mp3";
soundfiles[a] = loadSound(fileName);
a = a + 1;
}
if (letter != 67 && letter != 70) {
for (i = 3; i < 6; i++) {
let fileName = char(letter) + "b" + i + ".mp3";
soundfiles[a] = loadSound(fileName);
a = a + 1;
}
}
}
}
// SETUP
function setup() {
createCanvas(650, 650);
mainscreensound.play();
for (let i = 0; i < 21; i++) {
WhiteKeys[i] = new WhiteKey(i);
}
// variable kittie tracks the number of black keys
let kittie = 0;
for (let i = 0; i < 21; i++) {
if (i != 2 && i != 6 && i != 9 && i != 13 && i != 16 && i != 20) {
BlackKeys[kittie] = new BlackKey(i, kittie);
kittie = kittie + 1;
}
}
}
// DRAW
function draw() {
if (screen == 0) {
// setting up background of the main screen
background(252, 249, 245);
image(StartScreenImage, 0, 0, 651, 651);
// rect(width/2 - 50, height - 50, 100, 30, 3); button
}
if (screen == 1) {
// Setting up the bg for the second screen, turning off the music, creating a space for note sheets
background(252, 249, 245);
fill("black");
rect(0, 0, width, 300, 5);
mainscreensound.stop();
// Creating a keyboard using classes
for (let i = 0; i < WhiteKeys.length; i++) {
WhiteKeys[i].display(i);
}
let kittie = 0;
for (let i = 0; i < 21; i++) {
if (i != 2 && i != 6 && i != 9 && i != 13 && i != 16 && i != 20) {
BlackKeys[kittie].display(i, kittie);
kittie = kittie + 1;
}
}
creatingbutton();
// projecting the notes for user to learn songs
if (advanced >= 1 && ActiveButton == "advanced") {
push();
fill("black");
rect(0, 0, width, 300, 5);
fill("white");
textAlign(CENTER);
strokeWeight(5);
textWrap(WORD);
textSize(30);
text(songs_names[advanced - 1], width / 2, 50);
textSize(20);
text(songs_advanced[advanced - 1], 20, 100, width - 30, 250);
pop();
}
if (beginner >= 1 && ActiveButton == "beginner") {
push();
fill("black");
rect(0, 0, width, 300, 5);
fill("white");
textAlign(CENTER);
strokeWeight(5);
textWrap(WORD);
textSize(30);
text(songs_names[beginner - 1], width / 2, 50);
textSize(20);
text(songs_beginner[beginner - 1], 20, 100, width - 30, 250);
pop();
}
}
}
// assigning keyboard keys to execute corresponding sounds from the array of notes
function keyPressed() {
if (key == "q") {
soundfiles[12].play();
}
if (key == "2") {
soundfiles[18].play();
}
if (key == "w") {
soundfiles[15].play();
}
if (key == "3") {
soundfiles[24].play();
}
if (key == "e") {
soundfiles[21].play();
}
if (key == "r") {
soundfiles[27].play();
}
if (key == "5") {
soundfiles[33].play();
}
if (key == "t") {
soundfiles[30].play();
}
if (key == "6") {
soundfiles[3].play();
}
if (key == "y") {
soundfiles[0].play();
}
if (key == "7") {
soundfiles[9].play();
}
if (key == "u") {
soundfiles[6].play();
}
if (key == "i") {
soundfiles[13].play();
}
if (key == "9") {
soundfiles[19].play();
}
if (key == "o") {
soundfiles[16].play();
}
if (key == "0") {
soundfiles[25].play();
}
if (key == "p") {
soundfiles[22].play();
}
if (key == "[") {
soundfiles[28].play();
}
if (key == "=") {
soundfiles[34].play();
}
if (key == "]") {
soundfiles[31].play();
}
if (key == "a") {
soundfiles[4].play();
}
if (key == "z") {
soundfiles[1].play();
}
if (key == "s") {
soundfiles[10].play();
}
if (key == "x") {
soundfiles[7].play();
}
if (key == "c") {
soundfiles[14].play();
}
if (key == "f") {
soundfiles[20].play();
}
if (key == "v") {
soundfiles[17].play();
}
if (key == "g") {
soundfiles[26].play();
}
if (key == "b") {
soundfiles[23].play();
}
if (key == "n") {
soundfiles[29].play();
}
if (key == "j") {
soundfiles[35].play();
}
if (key == "m") {
soundfiles[32].play();
}
if (key == "k") {
soundfiles[5].play();
}
if (key == ",") {
soundfiles[2].play();
}
if (key == "l") {
soundfiles[11].play();
}
if (key == ".") {
soundfiles[8].play();
}
}