Page 80 - The Unofficial Guide to Lego Mindstorms Robots
P. 80
69
(table continued from previous page)
Table 4-5. RCX System Sounds (continued)
Sound Name Description
SOUND_DOWN Descending arp eggio
SOUND_UP Ascending arpeggio
SOUND_LOW_BEEP Long low note
SOUND_FAST_UP Quick ascending arpeggio (same as SO UND_UP but faster)
If you'd prefer to make your own music, yo u can play individual notes with the PlayTone() command:
PlayTone(const frequency, const duration)
This command plays a note with the given frequency for the speci fied duration. The frequency is in Hz, so 440 is the pitch of
the A above middle C on a piano. The duration is in hundret hs of a second. You can only specify integer values for the
frequency, so don't expect the pitches to be exactly in tune. No one expects your little robot to sound like Pavorotti.
If you want to play a sequence of notes, you h ave to be a little tricky about it. Each time you call PlayTone(), the command
returns almost immediately, with-out waiting for the sound you've r equested to finish playing. The tone you've requested is put
in a queu e; the system plays it while the rest of your program exec utes. If you call PlayTone() repeatedly, the queue will
fill up.
Subsequent calls to PlayTone() will not fit on the queue and the tones you've requested will not be played. The queue is
lo ng enough to hold eight tones. If you want to play a sequence longer than this, you should insert calls to Wait() in your
program so that the queue has time to empty out as notes are played.
The following example demo nstrates this technique; it plays part of Quando men vo, from Giacomo Puccini's La Bohème.
#define SIXTH 12
#defi ne HALF 3∗SIXTH
#define BEAT 2∗HALF
#define GRACE 6
task main() {
PlayTone(330, 2∗BEAT);
Wait(2 ∗BEAT + 2∗SIXTH);
Pl ayTone(115, SIXTH);
PlayTone(208, SIXTH);
PlayTone(247, SIXTH);
PlayTone(330, SIXTH);
PlayTone(311, 2 ∗BEAT);
Wait(4∗SIXTH + 2∗BEAT + 2∗SIXTH);
PlayTone(115, SIXTH);
PlayTone(208, SIXTH);
PlayTone(247, SIXTH);