Java Reference
In-Depth Information
If you think this is a little bizarre, rest assured you're not alone. Feeding a sequence of
tones to a player via a control completely breaks the source-player-controller abstraction.
Worse, the scheme to encode the tone sequence is a Byzantine affair reminiscent of note-
control sequences defined by the MIDI standard, likely because most mobile devices have
sound hardware that accepts MIDI commands. The byte stream that encodes the tone
sequence consists of an initial version number indicating the version of the ToneControl
instance you're using, followed by pairs of notes and durations. Unlike playTone , the dura-
tion you specify for the ToneControl instance isn't in milliseconds but is rather a multiple of
the resolution —or fundamental tempo—of the ToneControl instance. By default, the resolu-
tion is 1/64 th of one measure of four beats (4/4 time, if you're a musician). Thus, in the
default configuration, a duration of 64 is a whole note (four beats), a duration of 16 is a
quarter note (one beat), a duration of 8 is an eighth note, and so on. Listing 16-9 shows an
example of a simple sequence of notes for a ToneControl instance.
Listing 16-9. Some Sample Data for a ToneControl Instance
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
byte rest = ToneControl.SILENCE;
byte block1 = 0;
byte block2 = 1;
byte[] song = new byte[] {
// As transcribed in JSR-135
ToneControl.VERSION, 1,
ToneControl.TEMPO, tempo,
ToneControl.SET_VOLUME, 50,
ToneControl.BLOCK_START, block1,// Start defining a block
E4,d, D4,d, C4,d, E4,d, // content of block 1
E4,d, E4,d, E4,d, rest,d,
ToneControl.BLOCK_END, block1, // end block definition
ToneControl.BLOCK_START, block2,
D4,d, D4,d, D4,d, rest,d, // content of block 2
E4,d, G4,d, G4,d, rest,d,
ToneControl.BLOCK_END, block2,
ToneControl.PLAY_BLOCK, block1, // play the first block
ToneControl.PLAY_BLOCK, block2, // play the next block
D4,d, D4,d, E4,d, D4,d, C4,d // play the last section
};
 
Search WWH ::




Custom Search