Adding sound to your programs

From CometWiki

Revision as of 19:26, 28 May 2009 by Badge (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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:

[1]

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:

[2]


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

Personal tools