|
Sound & Games
The Next Level
DirectSound with EasySoundTM
Though the WaveMix32.dll allowed
us to play multiple sounds, it is somewhat cumbersome to use both
in initialization and playback. So to use it effectively you most
likely need a good wrapper class to make the interface to the dll
a bit easier. It is also not quite perfect when it comes to
playback, since you might experience minor delays when you start
to playback and until the sound is actually heard.
So the only option you really
have to efficiently apply sounds in your games, is to use the de
facto standard of playing sounds, namely DirectSound. But
it is always easier to name a tool to use, than it is to actually
be using it. This statement is also fitting for DirectSound in
Visual Basic. Since DirectSound is, as of yet, still not
supported in Visual Basic you only have two choices; either get
an ActiveX control or some other third party tool, which will let
you access DirectSound or get a Type Library which will let you
access the DirectSound interface in Visual Basic. The first
option of getting a third party control could prove quite
expensive, and the second option of getting a Type Library would
require an in-depth knowledge of DirectSound.
So what can you do? Well you can
start to feel lucky, because this is were the nice authors of
this compendium steps in and presents the EasySound
control. This control wraps around the DirectSound interface and
provides you with a very easy way of playing sounds
and applying some special effect to the sounds. And best of all
it is royalty free.
NOTE: You need DirectX version
5.0 or later to use the control.
You can locate the zipped program
on the ToolBox page. Run the setup program and install the control.
There are, as with the
WaveMix32.dll, certain initializing steps, which must be handled
before we can start playing sounds with the control. Also, as
with the WaveMix32.dll, you have to pre-load the wave sounds and
obtain, not a handle, but an index to the sounds, before you can
play them. The process of initialization and playback is
illustrated in the following figure:

As you can see from the
illustration you can set special features of a wave sound through
the control. These features include Frequency, Pan, Volume,
PlayPosition and more.
Initializing the control
The first thing you should when
using the control, is to set the .Window property of the
control instance that you are using. The control will not
initialize if you have not set this property. The Window property
should be set with the .hWnd property of you main window:
EasySound.Window =
MainWindow.hWnd
After this you initialize the
control by calling the ControlName.InitializeSound(). You
should check the return value from this call, since it is the
only way you can be certain that the control is properly
initialized. If you do not check the value, and the control is
not properly initialized, you will get run time errors if you try
to load or play any sounds with the control. The reason that you
will get these errors is that the control is optimized for speed,
and thus does not check to see if the thing has been properly
initialized.
Loading and Playing
After proper initialization you
can start to load and play some wave sounds. The control supports
two types of sounds, Static and Streaming sounds.
Static sounds are short sounds, which should not have a playtime
longer than 4 seconds. Streaming sounds are sounds longer than 4
seconds. The difference lies in the way that the control plays
the sounds. Static sounds are completely loaded into memory on
either the soundcard or in the system, and are played directly
from there. Streaming sounds are loaded into memory in chunks of
data. The data is then played from the buffer, while the control
continuously feed the buffer more data, and thus saves system
resources. So it is quite obvious that you use the Static sounds
to the small quick sounds, and the Streaming sounds as the
background music, speech or whatever sounds.
To load a static sound you use
the CreateStaticSound method of the control:
CreateStaticSound(FileName
As String) As Long
The function returns an index to
the sound, which is used in playback or in setting special
settings of the particular wave sound. A very similar function is
used to load a Streaming sound:
CreateStreamingSound(FileName
As String) As Long
This function also returns an
index to the Streaming sound, which is used in playback or
functions relevant to the particular sound. To play a loaded
sound either call the PlayStaticSound or PlayStreamingSound
depending on the type of sound you wish to play.
PlayStreamingSound(Index
As Long, Loop As Long) As Long
Or
PlayStaticSound(Index
As Long, Loop As Long) As Long
Each of these functions must be
supplied with the Index of the sound that must be played.
The index is, as stated above, returned from Loading the sound
with either the CreateStaticSound() or the CreateStreamingSound()
function. The sample project EASYSOUND
demonstrates the use of some of the methods the control provides
for playback of sounds.
NOTE: you must install the
control before using this project.
The project uses three sounds,
one streaming sound and two static sounds. The control is
initialized and the sound files re loaded in the load event of
the form:
Private Sub Form_Load()
Dim rt As Long
Dim AppPath As String
'donīt forget this
ESound.Window = Me.hWnd
'Initialize the sound machine
rt = ESound.InitializeSound()
If rt <> EX_OK Then
MsgBox "Could Not initialize sound", vbOKOnly, "Failure"
Exit Sub
End If
AppPath = App.Path & "\"
StaticMachineGun = ESound.CreateStaticSound(AppPath & "machine.wav")
If StaticMachineGun < 0 Then
MsgBox "Could not load machine gun sound", vbOKOnly, "Failure"
cmdMachine.Enabled="False"
End If
StaticBoink="ESound.CreateStaticSound(AppPath" & "boink.wav")
If StaticBoink < 0 Then
MsgBox "Could not load boink sound", vbOKOnly, "Failure"
cmdBoink.Enabled="False"
End If
StreamCount="ESound.CreateStreamingSound(AppPath" & "countdown.wav")
If StreamCount < 0 Then
MsgBox "Could not load count sound", vbOKOnly, "Failure"
cmdStream.Enabled="False"
cmdStop.Enabled="False"
End If
End Sub
|
The first thing to do is to set
the Window property of the control. It is set to the hWnd
property of the form. The control is then initialized by a call
to the InitializeSound() function. The return value from
this function is verified to be ok by comparing it to the EX_OK
constant. The three sounds are then loaded, and the return value
from each load of a sound is stored in a public variable, to
ensure that we do not lose it.

The form is now loaded and
shown. As you can see then you can play the three sounds by
clicking the top row of buttons on the form. They each play
indifferently to the other sounds running. But the same sound can
not be played over itself, meaning that you have to wait for a
sound to stop before it can be played again. This is not very
lucky since many times you will have the need to play the same
sound on itself many times. You could of course just load it
again in a new sound index, but this would waste unnecessary
resources. Instead you would want to use DirectSounds Duplicate
Sound feature. This feature enables you to play the same
loaded sound buffer on itself over and over again, without
creating a new wasteful sound buffer. The EasySound control wraps
around this feature of DirectSound with the DuplicateStaticSound()
function:
DuplicateStaticSound(Index
As Long ) As Long
This function will return a new
index to the duplicated sound, which can be played independently
from the original sound. The same function, which played an
original static sound, is also used to play a duplicated static
sound in the same manner.
The sample project also
demonstrates how to get and set the frequency of static sound,
for more information on this and other special function you
should look in the Sound Appendix where the complete
documentation on the EasySound control is located.
You might now be thinking where
the catch on this control is, it plays multiple sounds
simultaneously, it is easy to use, it cleans up after itself, it
is free etc
Well beside the point that the user of the
control must have DirectSound version 5.0 or later, the catch is
that DirectSound (even version 6.0) can only play regular
PCM sound formats, and so the control can also only play regular
PCM sound formats. Beside that and perhaps a bug or two there is
no catch with this control.
[Sound & Games #1] [Sound & Games #2] [Sound & Games #3] [Download All Samples]
These tutorials were originally developed by
Soren Christensen and Burt Abreu as part of a book which we were working on.
The book idea didn't come to fruition and so we decided to post the completed
chapters here in the hopes that you would find them useful. We retain copyright
to this material and you may not reproduce it, post it, or otherwise disseminate
it in any fashion without our express written consent with the exception of making
copies for your personal use.
|