Adding Sound to Your Program with PLAYWAVE

by Janet Terra

The PLAYWAVE command is fairly simple to use and almost foolproof. The PLAYWAVE command plays a wave (.wav) file.

From the Help File

PLAYWAVE "filename" [, mode ] Description: This plays a *.wav sound from a file on disk as specified in filename. If mode is specified, it must be one of the modes described below: sync (or synch) - wait for the wave file to finish playing (the default) async (or asynch) - don't wait for the wave file to finish playing loop - play the wave file over and over (cancel with: playwave "") Usage playwave "beep.wav", async playwave "beep.wav" playwave "hello.wav", loop playwave "" 'to stop previous wav from playing

Two wave files are included with Just BASIC: beep.wav and bump.wav. They are in the Just BASIC's MEDIA subfolder. These instructions assume you are running the demo from your Just BASIC directory. The DefaultDir$ captures your path from the root to the folder in which you're working. That's usually C:....\Just BASIC v1.0. The use of DefaultDir$ here allows the program to see 'into' the media folder and find one of those wave files.

To see the difference between sync and async, Run these two codes

      For i = 1 to 10
      Playwave DefaultDir$;"\media\beep.wav", sync
      Print "Sound heard ";i;" times."
      Next i
      Confirm "Did you hear it?";yn$

Notice that each sound is finished before the next one starts. The sync command causes program execution to stop until the wave file finishes playing. On the other hand,

      For i = 1 to 10
      Playwave DefaultDir$;"\media\beep.wav", async
      Print "Sound heard ";i;" times."
      Next i
      Confirm "Did you hear it?";yn$

the async mode causes program execution to continue, even if another playwave command is issued. In this case, the start of a second playwave always cancels the execution of the first. It is not possible to have two wave files playing simulatenously using the playwave command.

To stop a wave file in the midst of playing, simply use the PLAYWAVE "" command.

      playwave ""

Run the following code to see the PLAYWAVE "" command in action.

       Playwave DefaultDir$;"\media\beep.wav", async
      playwave ""
      Confirm "Did you hear it? ";yn$

If a wave file is playing when the program is ended, the wave file is automatically stopped by Just BASIC.

Try these same techniques out with longer playing wave files.

Hey, these wave files aren't exactly 'Surround Sound' quality, but they can add a little pizzazz to your programs!