Thursday, October 05, 2006

Step 4 - Recording and adding the sound

We will now add the sound to the game!

You will need to record the different sounds and store each of them on your computer as a wave file *.wav.

You can tweak the sounds with Cool Edit Pro (now Adobe Audition). When finished, save them as 16-bits PCM, 1 channel and 11025Hz *.wav files.

Before starting the code you might use the FMOD module for Delphi that offers a broad range of functions to play the sounds.

Here is the code to initialize sound:

// Init FMOD sound system and load songs
FSOUND_Init(44100,32,0);
FSOUND_SetOutput(FSOUND_OUTPUT_DSOUND);
pfStart:=FSOUND_Sample_Load(FSOUND_FREE,'.\sound\start.wav',FSOUND_2D,0,0);
pfPlay:=FSOUND_Sample_Load(FSOUND_FREE,'.\sound\play.wav',FSOUND_2D,0,0);
pfEvent:=FSOUND_Sample_Load(FSOUND_FREE,'.\sound\event.wav',FSOUND_2D,0,0);
pfMiss:=FSOUND_Sample_Load(FSOUND_FREE,'.\sound\miss.wav',FSOUND_2D,0,0);
pfEnd:=FSOUND_Sample_Load(FSOUND_FREE,'.\sound\end.wav',FSOUND_2D,0,0);
bSound:=true; // sound is enabled
SetSound; // set volume to maximum
EmptyScreen;
ImageSoundOn.visible:=true;


And the procedure to set sound ON or OFF:

procedure TFormGame.SetSound;
// Set Sound ON or OFF
begin
if bSound
then FSOUND_SetSFXMasterVolume(255)
else FSOUND_SetSFXMasterVolume(0);
end;


Here the procedure used to play sounds:

procedure TFormGame.PlaySound(pf:PFSoundSample);
begin
FSOUND_PlaySound(FSOUND_FREE,pf);
end;


Finally here is the code when closing the application:

// Cleanup and shutdown
FSOUND_Close;


Links:
- FMOD Sound System
- Cool Edit Pro

No comments: