Game Development Reference
In-Depth Information
set allows us to control the volume of the sound. There are other buffer flags too, of
course. Some examples are
ControlPan
, which lets you control the left/right bal-
ance of the sound,
ControlFrequency
, which lets you control the frequency of the
sound, and
ControlEffects
, which allows you to apply effects to the sound.
Next, we get the wave format from the
WaveStream
object and copy it into the
Format
property of the
SoundBufferDescription
object. Then, we create the
SecondarySoundBuffer
object using the
DirectSound
object, and the
SoundBufferDescription
object we just filled out.
The next block of code loads our sound file. The first line here creates a byte array
of the same size as our wave file. The next line reads all of the data from the wave
file into the byte array. The third line copies the data from the byte array into our
SecondarySoundBuffer
object. The second parameter here is the offset into the
buffer that we want to start writing the data at. Since we want to start at the begin-
ning of the buffer, we specify
0
for this. If we specified
10
, the wave data would be
written starting
10
bytes in from the beginning of the buffer.
The final line tells the
SecondarySoundBuffer
object to start playing our sound
by calling its
Play()
method. The first parameter is the priority for this sound. The
second parameter specifies flags that affect how the sound is played. In this case we
are using
PlayFlags.Looping
to make our music loop continuously.
Note
You will need your sound file to be properly set up for looping. A sound file that
is not meant for looping will probably not sound good when it loops back to the
start. A sound file that is meant for looping has a smooth transition at the end, so
that the end transitions nicely into the start so that the sound can repeat seam-
lessly.
The
SecondarySoundBuffer
object has other methods as well. The
Pause()
method, for example, will pause the sound. When you start playing it again, it will re-
sume from where it left off. The
Stop()
method, on the other hand, stops playback
and also rewinds the sound back to its beginning. So when you start the sound play-
ing again, it will start over from the beginning.