Adding Sound to Your Program with PLAYWAVE


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 Liberty BASIC: beep.wav and bump.wav. They are in the Liberty BASIC's MEDIA subfolder. These instructions assume you are running the demo from your Liberty BASIC directory.

To see the difference between sync and async, Run this code first using synch.

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

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, using async,

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

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 "media\beep.wav", async
playwave ""
Confirm "Did you hear it? ";yn$
End

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

Try these same techniques out with longer playing wave files.

Wave files may not produce 'Surround Sound' quality, but they can add a little pizzazz to your programs!