Monday, 11 February 2013

Other Sounds


8.2.2 Other Sounds

In addition to playing musical notes, it is possible to play other sounds. Using the File
Management system, as described in the RobotC On-line Support on the left side-bar under
the ROBOTC Interface ! NXT Brick Menu ! File Management section, it is possible to
place sound les onto the NXT brick for subsequent use during programs. Because of memory
limitations, these sounds must be short (less than a few seconds). To place your own sounds
on the NXT, you must rst nd a sound le in the .wav format. There are many programs
available to convert sound les (like .mp3) to .wav format. A good free program for this
purpose is Audacity. You can use Audacity to clip out a section of a larger sound le and
save it to a .wav format.
RobotC sound les are a special format and have the le extension .rso. Once you have
created the .wav le you are interested in, you must convert it to .rso format. A simple,
free converter program for Windows is here.
Once you have created your custom sound le, copy it to the NXT using the File Manage-
ment window, you can play the sound le from within a program using the PlaySoundFile()
function. Its single argument is the lename of the sound as a string. For example, suppose
you have Homer Simpsons famous \Doh!" in a sound le called doh.rso, then
 
PlaySoundFile ("doh. rso ");
 
will cause your NXT to say \Doh!". This is particularly useful if your robot bumps into a
wall!

// Note Frequencies
const float D5 =587.33;
const float G5 =783.99;
const float F5Sharp =739.99;
const float A5 =880.00;
const float B5 =987.77;
const float C6 =1046.50;
const float D6 =1174.66;
const int BEAT =20; // in 10 Msec
task main () {
// Measure 1
PlayTone (G5 ,2* BEAT );
while ( bSoundActive ){} // wait for tone(s) to finish
wait10Msec ( BEAT );
PlayTone (D5 , BEAT );
PlayTone (G5 ,2* BEAT );
while ( bSoundActive ){} // wait for tone(s) to finish
wait10Msec ( BEAT );
PlayTone (D5 , BEAT );
while ( bSoundActive ){} // wait for tone(s) to finish
// Measure 2
PlayTone (G5 , BEAT );
PlayTone (D5 , BEAT );
PlayTone (G5 , BEAT );
PlayTone (B5 , BEAT );
PlayTone (D6 ,2* BEAT );
while ( bSoundActive ){} // wait for tone(s) to finish
wait10Msec (2* BEAT );
while ( bSoundActive ){} // wait for tone(s) to finish
// Measure 3 and Measure 4 omitted.
}
 
Listing 8.2: This program uses the PlayTone() function to play the opening passage of
Mozart's Eine Kleine Nachtmusik.


No comments:

Post a Comment