mort8088 Just another WordPress site


MDX 9 – Tutorial 2

Originally Posted 2006

Play sound samples

CRASH, BANG, DING!! that's what we want in our games, noise is a vital part of a players experience, it can be as simple as a dripping in the background for a cave setting, or the sound of a zombie getting its head cut off with a scythe.

In this tutorial we'll be putting together the methods needed to play samples in 2D that is the sound plays and you hear it at as if it was played right next to you, rather than the sound happening elsewhere and the volume and pitch being altered to give the feeling of immersion.

To kick off we're going to build this as a library so we can use it again on other projects. as with the last tutorial we will start with the basic Direct3D project. Now right click on the combine in the project panel and select "Add/Add New Project..." the new project dialog opens select C# under Categories and Class Library under Templates. Enter a name for your Library and click create.

Once the project has been added rename the main code file from "MyClass.cs" to something like "SondFX.cs" this will be source file for the soundFX Class, Open the file.

To start with this file contains a generic class and namespace. so we need to put that right.

change the code from fig.1.0 to look like fig.1.1


fig 1.0


fig 1.1

As you can see there are a few changes you need to make right from the start over what is deemed the "standard" way of doing things.

First off I have moved the using inside the namespace I'm not too sure what the implications are for this but its the way I was shown so It's the way I do things, If some one wants to explain this to me then just drop me an Email. I've also added two new ones that are needed for the class to compile. The System.Windows.Form provides us the "hook" we need to the controlling form and Microsoft.DirectX.DirectSound that provides us the bulk of the sound functions we are going to use. Direct sound has been Aliased to DSound as a short hand so I don't have to type the full name of types.

Next the class is given a meaningful name and the base-list is set to object, Languages typically do not require a class to declare inheritance from Object because the inheritance is implicit, again it's just the way I do things.


fig 2.0

Now we're going to add the global data for the class. We have a device that works in the same way as the GFX device in Direct3D, allowing us an interface to the hardware, a SampleBuffer array that will hold the samples for us to play back & a Control that woks as the "hook" back to the Form.

The other two variables are a count of samples we have loaded and the volume for the device to play the samples.


fig 3.0

Right this is where the main part of the work is done, the Class constructor takes two parameters, the first being the list of samples to load in, the second is that "hook" to the form.

In the first few lines we set the MaxSamples to the length of the sample list, store the form control, create an instants of the DirectSound Device.

As you can see in line 24 we set the co-operative Level of the DirectSound Device using the form control.

Line 26 is just to check that the device was created ok before we create the sample buffer array in line 29, once that is done we fill the array using a for loop to load the samples by creating a DirectSound SecondaryBuffer class in each element of the SoundBuffer array then set it's volume attribute to the same value as the master volume.

That's it we now have a class that contains the samples we are going to play in our Game, But how do we play the Samples the SoundBuffer is private?


fig 4.0

Simple we make a play function that sorts it all out for us, taking just one parameter to specify the sample to play, we check that the buffer entry is not null, set the sample position to the beginning and then call it's play method, see simple.

now all you have to do to get this to run in your sample app is to add some samples to the bin directory, and at the following to the run method of your main app.


fig 5.0

Then all you have to do is call mysoundFX.PlaySFX(sampleNum). Remember that when you call the PlaySFX method that it is a 0 based array in C#.

Comments (0) Trackbacks (0)

Sorry, the comment form is closed at this time.

Trackbacks are disabled.