This example shows how the
SoundCipher scheduler can be used to control both music and drawing to
keep them synchronised.
Normally
in Processing the draw() method normally repeats at the framerate, to
prevent this we call noLoop() to halt the Processing draw thread.
The draw() method will now be executed only as required with the
redraw() command.
The
redraw() and playNote()
commands
are located together inside the handleCallbacks() method (so
that they are sychonised). The handleCallbacks() method is called
when callbacks are scheduled during playback.
Details:
First the SoundCipher library is imported and a SoundCipher instance
called sc is created.
A SoundChiper score (SCScore) object called score is declared.
In
the setup() method the drawing loop is halted, the score is created.
The
addCallbackListener() command sets up this program to listen for
callbacks sent from the score during playback. The tempo
(speed)
of the score is set to 180 beats per minute. The instrument used by sc
is set to sound number 83 (from the general MIDI sound bank used by
Java).
In the for-loop, callbacks (with an id of 3) are added to the score at
semi-random
intervals of either 1 or 2 beats apart. This determines the timing of
our sound playback and drawing.
The final step in the setup() method is to begin score playback.
The
draw() method first redraws background of the
display window, then it draws a horizontal line at a random height. The
draw() method is executed each time redraw() is called.
When,
during score playback, the time for each callback in the score is
reached the
handleCallbacks() method is triggered. The handleCallbacks() method checks that the
callback id (identifier) is 3 and, if so, calls redraw() and plays the note. |