Home   Comet Tips

Comet Tip


 

  August 24, 2001  

 

Adding Sound to Your Programs

 

Thereís an easy way to add sound to your programs. Use the (PlaySound) mnemonic. Hereís an example:

 

Print (PlaySound="c:\windows\media\chimes.wav",3)

 

As you can see, the mnemonic has two parameters. The first one is the name of the WAV file you want to play. In the above example, ďc:\windows\media\chimes.wavĒ is the familiar Windows chime sound. The second parameter is a numeric value that describes how the sound is to be played. In the above example, the 3 indicates that the sound is to be played asynchronously (i.e., the program proceeds to the next statement while the sound is playing) and that the sound driver will not play the default system sound if the specified WAV file isn't found. See below for more information about this parameter.

 

This feature lets your programs play any WAV file at all. At first, you might want to experiment with familiar WAV files such as the ones in the \windows\media folder. Once you've seen how easy it is to include sounds in your applications, you might want to record some of your own, such as start-up and ending sounds (or music, perhaps), program error messages (ďThat is not a valid customer number. Please try again.Ē), or program sound effects. (Note: Sounds effects can become annoying after a while, so be careful how and when you use them. You might also want to provide a way for your Comet users to switch the sounds off.) For some general hints about recording and using sounds in your programs, contact Forum at Signature Systems.

 

Please note that this is a Windows-only feature, so you must be running Comet98 or greater in order to use (PlaySound).

 

We wrote a simple demo program to show how easy it is to add sound to your applications. Itís a number guessing game, where a sound effect is played after each guess. Thereís a special sound when you guess the right number and another one if you lose the game. The source code and WAV files are contained in the following ZIP file:

 

http://www.signature.net/download/demos/playsounddemo.zip

 

As mentioned above, the second parameter in the (PlaySound) mnemonic describes how the sound is to be played. This parameter is a number whose value is determined by adding together the desired values from the following list:

 

0

Play the sound synchronously (i.e., donít proceed to the next statement until the WAV file has finished playing). Be careful when using this; if you play a 30-second WAV file, your program will pause for 30 seconds.

1

Play the sound asynchronously

2

If the WAV file isnít found, donít play the system default sound

8

Play the sound repeatedly until another (PlaySound) mnemonic is issued (i.e., make a sound loop)

16

Donít interrupt the sound driver if another sound is already playing

64

Stop all sound

8192

If the sound driver is busy, return immediately without playing the sound

 

Thus, in the above example, the value of 3 is the result of 1 (play asynchronously) plus 2 (donít play the default sound).

 

For more information, see the following page in the MTB documentation:

 

http://www.signature.net/signature/techdoc/mtb/playsound.htm

 

Have some fun adding sounds to your applications. Keep in touch with your ideas and discoveries!