please format code with </> button * homework policy * asking questions
Hi!
I’m making a virtual piano for a school assignment and since the black keys and white keys overlap, two notes are played when I tap on black keys. How do I fix this so that only the black key is played?
I know I have sound files in this but I don’t know how to attach but if you need to try it out just comment them out. Also, I don’t know how to format code for these topics…
here’s the code:
import processing.sound.*;
SoundFile file1;
SoundFile file2;
SoundFile file3;
SoundFile file4;
SoundFile file5;
void setup() {
size(150,200);
file1 = new SoundFile(this, “c5.wav”);
file2 = new SoundFile(this, “d5.wav”);
file3 = new SoundFile(this, “e5.wav”);
file4 = new SoundFile(this, “c-5.wav”);
file5 = new SoundFile(this, “d-5.wav”);
}
void draw() {
//loop for the white and black keys
for (int x = 0; x < width + 40; x += 50) {
whiteKeys(x, 0);
}
for (int y = -15; y < width + 40; y += 50) {
if ((y != -15)&&(y != 135)&&(y != 335)&&(y != 485)&&(y != 685)) {
blackKeys(y, 0);
}
//key labels
textSize(25);
text(“C D E”, 15, 180);
fill(255);
textSize(18);
text(“C# D#”, 36, 80);
text(“Db Eb”, 36, 105);
}
if (mouseX>0&&mouseX<50&&mousePressed) {
file1.play();//plays the note
fill(#A0A0A0, 60);//makes the key darker
rect(0,0,50,200);
}
//these allow the keys to “light up” as I move my mouse
if (mouseX>0&&mouseX<50) {
fill(#A0A0A0, 50);
rect(0,0,50,200);
}
if (mouseX>50&&mouseX<100&&mousePressed) {
file2.play();
fill(#A0A0A0, 60);
rect(50,0,50,200);
}
if (mouseX>50&&mouseX<100) {
fill(#A0A0A0, 50);
rect(50,0,50,200);
}
if (mouseX>100&&mouseX<150&&mousePressed) {
file3.play();
fill(#A0A0A0, 60);
rect(100,0,50,200);
}
if (mouseX>100&&mouseX<150) {
fill(#A0A0A0, 50);
rect(100,0,50,200);
}
if (mouseX>35&&mouseX<65&&mouseY<115&&mousePressed) {
file4.play();
}
if (mouseX>85&&mouseX<115&&mouseY<115&&mousePressed) {
file5.play();
}
}
void whiteKeys(int x, int y) {
pushMatrix();
stroke(0);
fill(255);
rect(x, y, 60, 200);
fill(0);
popMatrix();
}
void blackKeys(int x, int y) {
pushMatrix();
stroke(0);
fill(0);
rect(x, y, 30, 115);
fill(0);
popMatrix();
}