We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, I try to create a piano on processing and i want to put a demo button. It need to show the key corresponding to the music note
(this is not my program, just an example what happen)
import arb.soundcipher.*; SoundCipher sc = new SoundCipher(this); void setup() { noStroke(); size(130, 130); background(0); } void draw() { sc.playNote(60, 100, 2.0); //play a note fill(255, 0, 0); rect(100, 100, 10, 10); delay(2000); sc.playNote(40, 100, 2.0); //play a note fill(0, 255, 0); rect(120, 120, 10, 10); delay(2000); }
when i run the program, first there is a white square and after the two notes were played all graphics sudenly appear. I test without importing the SoundCipher library and the same thing happen just without sound.
(Please excuse my english, it is not my native language so not sure of all the terms)
Answers
The draw method contains all the code to draw the next frame which only appears at the end of the draw method.
The code below shows how you can do something similar using the keyboard.
BTW the call to size() must be the first statement inside setup.
Technical FAQ: I display images in sequence but I see only the last one. Why?
Don't use delay(). Most of the time, it is wrongly used...
delay() is an undocumented Processing function! Its main use is to slowdown other threads.
Processing's already has its own internal slowdown engine that controls canvas rendering speed.
Instead of delay(), use frameRate() in order to set draw()'s FPS:
http://processing.org/reference/frameRate_.html
Without the delay:
frameCount is a variable which increments after each draw(). This draw method gets called about 60 times a second by Processing (by default), so after one second, frameCount equals 60. Of course, this assumes the draw method takes less than 1/60th of a second to finish.
If you want the program to loop, use frameCount%240 instead, this "resets" the frameCount variable once it reaches 240. The % sign is called the modulo operator which is explained in the documentation.
Other people have already answered you and gave you some alternatives, but I will also add that this is a result of Processing (and Swing in general) using double buffering to draw the frames. The entire frame is drawn to an off-screen buffer, and then that buffer is drawn to the screen all at once.
More info here, and google is your friend: http://en.wikipedia.org/wiki/Multiple_buffering#Double_buffering_in_computer_graphics
@KevinWorkman: If I wanted to draw directly to the screen, possible in processing? Possible in eclipse? Thank you! ;-)
Thanks a lot for all the solutions tou give me. I finally solve the problem by add a counter:
The play function play the note and diplay the key and the reset function reset the keyboard of the piano.
Thats an awful lot of if statements - in this scenario it is common to use the swith statement like this
@tigri:
is it correct that sometimes there are 2 plays at once?
@quark: Thanks for the switch trick, I do not use much the switch statement, so I don't have the reflex use.
@Chrisir: Yes it's correct, it's when the right hand and the left hand play together. For information, this demo is a part the opening of Beethoven's Für Elise :
I see, thank you!