Hi, so for a group project, we have to make a piano that when you click on the notes, plays the corresponding sounds. We have everything figured out except for the black keys. When I click on a black key, it plays the right sound but also the sound of a white key that's underneath it. We don't know how to make it play just the black key. The white keys are just rectangles with the black keys on top of them. Is there a way to not play the white notes when the black notes are clicked? Or do we have to change the shape of the keys so the black keys don't overlap? I'm only going to put in the parts of the code that relate to the the keys. If I left out something important, let me know.
`import arb.soundcipher.*; SoundCipher midi; color black = color(0, 0, 0); color white = color(255, 255, 255); color darkgreen = color(50, 150, 50); color darkblue = color(25, 25, 112); color grey = color(150); float rectX = width/2; float rectY = 115; int i; int j;
Button[] clickableButtons; BlackButton[] blackClickableButtons;
void setup() { midi = new SoundCipher(this); size(1900, 500);
clickableButtons = new Button[15];
blackClickableButtons = new BlackButton[10];
//white keys
float xpos = 575;
float xpos1 = 613;
float gap = 50;
clickableButtons[0] = new Button(xpos, 200, "");
xpos = xpos + clickableButtons[0].getButtonWidth();
clickableButtons[1] = new Button(xpos, 200, "");
xpos = xpos + clickableButtons[1].getButtonWidth();
clickableButtons[2] = new Button(xpos, 200, "");
xpos = xpos + clickableButtons[2].getButtonWidth();
clickableButtons[3] = new Button(xpos, 200, "");
xpos = xpos + clickableButtons[3].getButtonWidth();
clickableButtons[4] = new Button(xpos, 200, "");
xpos = xpos + clickableButtons[4].getButtonWidth();
clickableButtons[5] = new Button(xpos, 200, "");
xpos = xpos + clickableButtons[5].getButtonWidth();
clickableButtons[6] = new Button(xpos, 200, "");
xpos = xpos + clickableButtons[6].getButtonWidth();
clickableButtons[7] = new Button(xpos, 200, "C");
xpos = xpos + clickableButtons[7].getButtonWidth();
clickableButtons[8] = new Button(xpos, 200, "");
xpos = xpos + clickableButtons[8].getButtonWidth();
clickableButtons[9] = new Button(xpos, 200, "");
xpos = xpos + clickableButtons[9].getButtonWidth();
clickableButtons[10] = new Button(xpos, 200, "");
xpos = xpos + clickableButtons[10].getButtonWidth();
clickableButtons[11] = new Button(xpos, 200, "");
xpos = xpos + clickableButtons[11].getButtonWidth();
clickableButtons[12] = new Button(xpos, 200, "");
xpos = xpos + clickableButtons[12].getButtonWidth();
clickableButtons[13] = new Button(xpos, 200, "");
xpos = xpos + clickableButtons[13].getButtonWidth();
clickableButtons[14] = new Button(xpos, 200, "");
//black keys blackClickableButtons[0] = new BlackButton(xpos1, 200, ""); xpos1 = xpos1 + clickableButtons[0].getButtonWidth(); blackClickableButtons[1] = new BlackButton(xpos1, 200, ""); xpos1 = xpos1 + gap + clickableButtons[1].getButtonWidth(); blackClickableButtons[2] = new BlackButton(xpos1, 200, ""); xpos1 = xpos1 + clickableButtons[2].getButtonWidth(); blackClickableButtons[3] = new BlackButton(xpos1, 200, ""); xpos1 = xpos1 + clickableButtons[3].getButtonWidth(); blackClickableButtons[4] = new BlackButton(xpos1, 200, ""); xpos1 = xpos1 + gap + clickableButtons[4].getButtonWidth(); blackClickableButtons[5] = new BlackButton(xpos1, 200, ""); xpos1 = xpos1 + clickableButtons[5].getButtonWidth(); blackClickableButtons[6] = new BlackButton(xpos1, 200, ""); xpos1 = xpos1 + gap + clickableButtons[6].getButtonWidth(); blackClickableButtons[7] = new BlackButton(xpos1, 200, ""); xpos1 = xpos1 + clickableButtons[7].getButtonWidth(); blackClickableButtons[8] = new BlackButton(xpos1, 200, ""); xpos1 = xpos1 + clickableButtons[8].getButtonWidth(); blackClickableButtons[9] = new BlackButton(xpos1, 200, ""); xpos1 = xpos1 + gap + clickableButtons[9].getButtonWidth(); }
class BlackButton { color black = color(0, 0, 0);
float textHeight1; float buttonX1; float buttonY1; String buttonText1 ; float buttonWidth1 ; float buttonHeight1; PFont buttonFont1;
public BlackButton(float buttonX1, float buttonY1, String buttonText1) { this.buttonX1 = buttonX1; this.buttonY1 = buttonY1; this.buttonText1 = buttonText1; this.textHeight1 = 15; this.buttonFont1 = createFont("Calibri", this.textHeight1); buttonWidth1 = 25; buttonHeight1 = 115; }
void show() { textFont(buttonFont1); fill(black); stroke(0); rect(buttonX1, buttonY1, buttonWidth1, buttonHeight1, 5); fill(white); text(buttonText1, buttonX1+4, buttonY1 + textHeight1+80); // put text inside the button }
boolean clicked() { return (mouseX >= buttonX1 && mouseX <= (buttonX1 + buttonWidth1) && mouseY >= buttonY1 && mouseY <= (buttonY1 + buttonHeight1)); }
String getButtonText1() { return buttonText1; } float getButtonWidth1() { return buttonWidth1; } }
class Button { color black = color(0, 0, 0);
float textHeight; float buttonX; float buttonY; String buttonText ; float buttonWidth ; float buttonHeight; PFont buttonFont;
public Button(float buttonX, float buttonY, String buttonText) { this.buttonX = buttonX; this.buttonY = buttonY; this.buttonText = buttonText; this.textHeight = 16; this.buttonFont = createFont("Calibri", this.textHeight); buttonWidth = 50; buttonHeight = 200; }
void show() { textFont(buttonFont); fill(white); stroke(0); rect(buttonX, buttonY, buttonWidth, buttonHeight, 7); fill(black); text(buttonText, buttonX+20, buttonY + textHeight+160); //put text inside the button }
boolean clicked() { return (mouseX >= buttonX && mouseX <= (buttonX + buttonWidth) && mouseY >= buttonY && mouseY <= (buttonY + buttonHeight)); }
String getButtonText() { return this.buttonText; } float getButtonWidth() { return this.buttonWidth; } }
void mousePressed() {
for (i=0; i < clickableButtons.length && !clickableButtons[i].clicked(); i++); if (i < clickableButtons.length){ text = "Note Played="; note = clickableButtons[i].getButtonText(); if (i==0) { midi.playNote(48, v, d); note = "C"; } if (i==1) { midi.playNote(50, v, d); note = "D"; } if (i==2) { midi.playNote(52, v, d); note = "E"; } if (i==3) { midi.playNote(53, v, d); note = "F"; } if (i==4) { midi.playNote(55, v, d); note = "G"; } if (i==5) { midi.playNote(57, v, d); note = "A"; } if (i==6) { midi.playNote(59, v, d); note = "B"; } if (i==7) { midi.playNote(60, v, d); note = "Middle C"; } if (i==8) { midi.playNote(62, v, d); note = "D"; } if (i==9) { midi.playNote(64, v, d); note = "E"; } if (i==10) { midi.playNote(65, v, d); note = "F"; } if (i==11) { midi.playNote(67, v, d); note = "G"; } if (i==12) { midi.playNote(69, v, d); note = "A"; } if (i==13) { midi.playNote(71, v, d); note = "B"; } if (i==14) { midi.playNote(72, v, d); note = "C"; } }
for (j=0; j < blackClickableButtons.length && !blackClickableButtons[j].clicked(); j++); if (j < blackClickableButtons.length) { text = "Note Played="; note= blackClickableButtons[j].getButtonText1(); if (j==0) { midi.playNote(49, v, d); note = "C#"; } if (j==1) { midi.playNote(51, v, d); note = "D#"; } if (j==2) { midi.playNote(54, v, d); note = "F#"; } if (j==3) { midi.playNote(56, v, d); note = "G#"; } if (j==4) { midi.playNote(58, v, d); note = "A#"; } if (j==5) { midi.playNote(61, v, d); note = "C#"; } if (j==6) { midi.playNote(63, v, d); note = "D#"; } if (j==7) { midi.playNote(66, v, d); note = "F#"; } if (j==8) { midi.playNote(68, v, d); note = "G#"; } if (j==9) { midi.playNote(70, v, d); note = "A#"; } } }
void draw() { background(black);
for (int i=0; i < clickableButtons.length; i++) { clickableButtons[i].show(); } for (int i=0; i < blackClickableButtons.length; i++) { blackClickableButtons[i].show(); } } `