We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi! I need help writing this code on Processing for a functioning piano. This is the instructions I was given. (It's due March 1st) Please help! I am genuinely lost (https://forum.processing.org/two/uploads/imageupload/130/GWX4ZGA7D0Z7.png "Screen Shot 2016-02-25 at 10.30.14 AM")
Answers
.
to
Do you have any code? You are given some stuff for free here. Like the
mousePressed()
class and theMusicBox.playNote(noteNumber, 1000);
I would use two for loops to draw the piano.
what is MusicBox? Do you have that class??
also you fail to show the image of the keyboard that is on the first pages of the handout!!!!!!!!!!!!!!!!
here is A LOT that you have to do
I was given a file called "MusicBox" and it's a Java code. He wants us to use that to create the sounds of each key
import java.util.Arrays; import javax.sound.midi.*; public class MusicBox { static Synthesizer synthesizer; static MidiChannel[] channels; static boolean noSound = false;
public static void initialize() { try { if (!noSound) { synthesizer = MidiSystem.getSynthesizer(); synthesizer.open();
}
public static void cleanUp() { if (synthesizer != null) synthesizer.close(); }
public static void playNote(final int note, final int milliseconds) { System.out.println("");
}
public static void playChord(int note1, int note2, int note3, int milliseconds) { playChord(new int[] {note1, note2, note3}, milliseconds); }
public static void playChord(final int[] notes, final int milliseconds) { System.out.println("");
}
public static void playScale(int note1, int note2, int note3, int note4, int note5, int note6, int note7, int note8, int milliseconds) { playScale(new int[] {note1, note2, note3, note4, note5, note6, note7, note8}, milliseconds);
}
public static void playScale(final int[] notes, final int milliseconds) { Thread t = new Thread() { public void run() { try { if (!noSound && channels != null && channels.length > 0) { for (int n : notes) { channels[0].noteOn(n, 120); sleep(milliseconds); channels[0].noteOff(n); } } } catch (Exception ex) { System.out.println("ERROR:" + ex); } } }; t.start(); }
private static void sleep(int length) { try { Thread.sleep(length); } catch (Exception ex) { } } }`
sorry the way the code posted was all weird
https://forum.Processing.org/two/discussion/8045/how-to-format-code-and-text
just join the
class MusicBox
with my code above
<= keyHeight+ blackWidth&& mouseY <= blackHeight&& blackHover== blackKey){ stroke(2); fill(50,255,50); }else{ fill(0); } rect(keyHeight, keyWidth, blackWidth, blackHeight); } }
That's what I've got and i need help with the white keys changing on mousePressed and then somehow putting the MusicBox class into my code
the posting of your code ends before the function draw() ends....
also you need a way that when the mouse is pressed you call
MusicBox.playNote(noteNumber, 1000);
for that key.Also each key needs to know its noteNumber, e.g. in an array :
here you define it the note values:
then play the note value for the current key
This is the code that I've come up with. the MusicBox file is a java file and I don't know how to place it into this code. And my mouse pressed function for the white key won't work.
You can just paste the entire musicBox code below your code.
Then call musicbox initialize first
And then playnote as I said above
this is what pops up when i copy and pasted the MusicBox code.
Yeah
Remove all static from the entire class pls
Also
MusicBox mb;
Before setup
Then in setup()
mb=new MusicBox(); mb.initialize();
then later
mb.playNote(........
Sorry I am on a journey and can't do it for you
I understand! Thank you for your help! Could you help me with my mousePressed for the white keys? It won't work.
This
mouseY <= blackHeight
In the if
Should be
mouseY <= keyHeight+blackHeight
What time is it in your place?
Hi, would like to know if this has been solved, thanks.
right now it's 5:12pm here. and not solved yet
Don't give up
the black keys kinda work, but they're playing the wrong note. the black key numbers are 61,63,66,68,70. And my white keys still won't work.
rect(keyHeight, keyWidth
the names of the variables are terrible
I mean, rect is rect(x,y, width, height);
So your names should be
rect( keyPositionX, keyPositionY, keyWidthBlack, keyHeightBlack );
you know good names help you understand your own code; bad names will confuse you in the long run
Anyway, like with the black keys the white keys have the same issue :
mouseY<= keyHeight
Must be
<= keyWidth+keyHeight
( using your bad names here )
In your rect you say height ------- you mean keyHeight
I GOT IT TO WORK! :) THANK YOU SO MUCH EVERYONE FOR HELPING!
Great!
And do the sounds work?
Yes! everything works!
Great!
Could you post your code please?
Are there Ressources for other free Instrument sounds?
Thank you!