Java Reference
In-Depth Information
This example shows a few other commands you can place in a sequence for a
ToneControl
instance. These commands influence the following options:
•
Tempo
: You can change the tempo of the playback at any time by using the
ToneControl.TEMPO
parameter, following it with the new tempo in beats per minute
divided by
4
.
•
Volume
: You can change the volume at any time by using the
ToneControl.VOLUME
parameter, following it with the new volume (a number between
0
and
100
).
•
Blocks of notes
: You can define up to 255 blocks of notes to be repeated. To begin a
block definition, use the
ToneControl.BLOCK_START
parameter, following it with a
block ID. Follow this with the commands in the block, and close the block using
the
ToneControl.BLOCK_START
parameter with the ID you used to start the block. You
can then play the block at any time in the sequence by specifying the
ToneControl.PLAY_BLOCK
parameter and the block ID.
•
Silence
: Music isn't just about notes; it's also about the spaces between the notes.
To specify a rest, use the
ToneControl.SILENCE
note value followed by the duration
of the rest.
•
Repetition
: You can repeat a single note multiple times by specifying the
ToneControl.
REPEAT
followed by the number of repetitions, then the note and duration. (For
example, with the default resolution, the sequence
ToneControl.REPEAT, 4, 60, 16
plays four quarter notes on Middle C.)
•
Resolution
: You can change the resolution from the default of 1/64 using the
ToneControl.RESOLUTION
by following it with the denominator of the new resolution.
However, you can do this only once at the beginning of a sequence.
Listing 16-10 builds on the pseudocode from Listings 16-1 and 16-9 to play “Mary
Had a Little Lamb.”
Listing 16-10.
Pseudocode to Play a Simple Song with a ToneControl Instance
Player player;
ToneControl control;
byte tempo = 30; // set tempo to 120 bpm
byted=8;
//eighth-note
byte C4 = ToneControl.C4;;
byte D4 = (byte)(C4 + 2); // two half steps, a whole step
byte E4 = (byte)(C4 + 4); // four half steps, a major third
byte G4 = (byte)(C4 + 7); // seven half steps, a fifth