components used: TDXSound TDXWave TDXWaveList TImage TTimer TTrackBar TButton

delphix does NOT work with borland kylix

download source code

New adventures in Hi-Fi
In this tutorial we'll delve into the world of sound via the use of the DXSound component, the DXWave component and the DXWaveList component. The areas we're going to cover here are not too hard to grasp, and should make things a lot clearer than they are in the DelphiX help files.

The DXSound Component
Okay, the first thing you need to do is place the DXSound component on your form. This sets up DirectX Sound for your game and allows you to make use of the capabilities this brings. You don't have to set any properties on this component for this tutorial so you can now move on and add the DXWave component.

The DXWave Component
This is where you set up the actual sound file you want to use. We're not going to go into any detail with this component as the DXWaveList component is far more useful if you need to use more than one sound file in your games (as you undoubtedly will). Basically, this component allows you to play a sound file in conjunction with the DXSound component.

The DXWaveList Component
This is where everything really starts to happen. With this component we can really start to make good use of as many sounds as necessary. We can set up the volume, frequency and pan of each sound so as to create effects that make it sound like say, a car speeding by. We'll move onto an example now to show you how to make use of this component.

An example...
First of all we need to set up what sound files we are going to use. In this first example we'll use just the one. Add a DXSound and a DXWaveList component to your form. Click once on the DXWaveList component that you've just added so that its properties come up in the Object Inspector window on the left (or somewhere else if you've moved it). You'll see that the first property is called DXSound. If you click on the drop-down menu you should see the name of your DXSound component, in this case called DXSound1. Select it and you've now provided a link between the two components.

Next we need to add the sound to our list so click on the 'three-dots button' beside the Items property to bring up the edit box for sound files. You'll see that only one button is available to click on. This is the Add New Sound button and clicking on it will add a new entry. You can see that in the Object Inspector window things have changed. These new properties are relevant only to this new entry and the one we're interested in is the Wave property. Click on its 'three-dot button' and then the 'Load' button to browse to the location of your sound file. Click on OK both times to return to the Edit list for your sound file. You may want to enter a name for the sound file, in my case I called it Clip1. Also, set the looped property to true so that once the sound file starts to play it will repeat continuously until you cancel the sound playing. When you've finished doing that, you should have something that doesn't look to dissimilar to the one shown below.

DXWaveList Item Editor

Now close the editing window to return back to our form. Before we continue we'll need to add a TImage component to our form so that we can set up our program to pan the sound while our image moves along the screen. Choose something appropriate to your sound if you want, in my case I've chosen a car which I've called CarImg. Now that we have just about everything in place we'll need to find a way of moving the car along and changing our sound with it.

Out of Time
We of course need to add a TTimer component. Place it on your form and then double-click on it to add the program lines to set what happens on each time interval. We're going to make use of the pan facilities to set how much sound is coming from the left and right speakers. We do this by using the dxwavelist1.items[0].pan command to set a value between -10000 and 10000, where -10000 means that only the left speaker has sound, zero means both speakers have the same amount of sound, and 10000 means only the right speaker has sound. So, if we wanted a pan of 10000 we would enter dxwavelist1.items[0].pan := 10000. The items[0] bit refers to the number of item in our list of sounds, so items[12] would refer to our twelfth sound if we had one. What we need to do is set the pan to change from -10000 to 10000 as our image moves along so that it appears as though the object is passing by us. The following code does just that:

procedure TForm1.Timer1Timer(Sender: TObject);
 CurrentPan: integer;
CarImg.Left + CarImg.Width < ClientWidth - 5 then

  CarImg.Left := CarImg.Left + 5;
  CarX := CarImg.Left;
  CurrentPan := 20000 * CarX div ClientWidth - 10000;
  dxwavelist1.items[0].pan := CurrentPan end else

The first couple of lines should be familiar with you if you've done the earlier tutorials as this basically moves the image along the screen until the right edge is reached. The remaining lines will not however be familiar. Obviously the third line is just setting up our CarX variable which we can add by placing the CarX: integer; line in our public section. The following line is less easy to understand though.

Basically we have a CurrentPan variable which we'll use to set the pan of our sound file. This value changes each time it is called, as the CarX variable will have gone up by another five until it reaches the right-hand side of the screen. This line is pretty mathematical and you don't really need to know the details of it. Basically though it stops our value being less than -10000 and greater than 10000. The line after this simply puts our newly found CurrentPan variable value to our sound file.

Right, that's the timer event set up now, but there is one other thing that needs to be done first. Before the sound will actually play, we need to add a line in our FormCreate procedure. To create one just double-click on an empty part of your form. Then enter the following line:


This starts the sound actually playing so you can set the pan/frequency/volume levels. The reason it's entered here is to make the sound start straight away when the program starts but you can enter it anywhere as long as it comes BEFORE the pan etc. entries.

What's the Frequency, Kenneth?
Okay then, we've dealt with setting the pan of our sound but that's not all we can do. We can also set the frequency and volume level. To demonstrate the frequency we'll add a track bar to our form so that we can 'drag' between the different frequencies. Head to the Win32 tab and add the TTrackbar to your form. Now set the Max entry as 44100 and the Min entry as 4410. Now double-click on the Trackbar component on your form to create a OnChange event in your program listing then enter the following code:

dxwavelist1.items[0].Frequency := FreqTrackBar.Position;

This will take the value from the trackbar and 'feed' it to the Frequency value of our sound clip. The value you use for your frequency is very important if you're going to use it as while the help file states you can have a value of between 100 and 100,000, actually setting your frequency at either of these extremes will provide little or no sound. As a guide, 44100 is usually the frequency your wave files will run at playing normal and you can always play around with values higher and lower than this in 1000 steps to see what gives the result you require. For example, try changing the Max entry to 441000 and the Min entry to 44100, and you'll get something ranging from normal to high speed.

The volume is far easier to grasp though, as you can simply set its value between -10,000 and 10,000 where the lower value provides a muted sound, zero provides normal volume level and 10,000 provides the loudest volume. So if we wanted a louder volume we'd just enter the following code:

dxwavelist1.items[0].volume:= dxwavelist1.items[0].volume + 100;

This simply takes the existing volume and increases it by 100. You'll see in the sample code that you can download, that I've added two buttons that move the volume up and down by 100.

So to summarise...
There are effectively four procedures you carry out on a sound file at any point in your program:

If you've already entered the above you can then do the following:

Oh, and to stop the sounds...
One other thing you'll need to obviously know is how to stop the sounds. Normally this isn't necessary if the sound you're playing is only to be played once, such as an explosion sound, but in situations where your sound is set to be looped (as in our program here) you'll need to add the following line to stop the looping:

dxwavelist1.items[0].Looped := False;

That's all you need to do.

That's 'it'...
So that's just about all you need to know to make good use of your sound files. You'll also find in the downloadable source code another example program that uses a second TTimer to move another image across from the other side of the screen with an accompanying sound clip. If you've got any more questions let me know by e-mailing me or leave a message on the message board.
Please note: all R.E.M. references are purely coincidental ;-)

download source code

Subscribe to the feed NEWS Feed
archived news | mailing list

[ all tutorials found at are Copyright © 2008 Ben Watt ]