Master Class top

to MAIN MENU

Back to Section Menu

Direct Sound (DirectX5)
Jörg Knitter
spacer

DirectSound is one part of the big Microsoft DirectX package. Aim of DirectX was to deliver a hardware independent interface for multimedia programming for the 32-bit Windows systems. Additionally, Microsoft wanted to replace DOS as the standard gaming platform.
Microsoft makes a distinction between DirectX Media and DirectX Foundation.

DirectX Media includes

  • DirectAnimation
  • DirectPlay
  • Direct3D Retained Mode
  • DirectShow (formerly known as ActiveMovie)

These function libraries are based on the low level DirectX Foundation functions.

Apart from DirectSound the DirectX Foundation

  • DirectDraw
  • Direct3D Immediate Mode
  • DirectInput.

DirectSound delivers a hardware independent access to soundcards, i.e. it is not important if the soundcard supports 3D sound or sample ram. Only the CPU and memory usage shows the performance of the soundcard.

Static and streaming...

DirectSound distinguishes between static and streaming buffers. Static sounds are short sounds that can reside on the soundcard´s memory so that they can be played with low latency. If there is no or not enough memory on the soundcard, the sound is loaded into the computer´s memory and played and mixed using the CPU.
If the developer has a very time critical application, he can force the usage of soundcard´s RAM; if it is not available there will be an error message. Of course, it is also possible to force the use of main memory.

Streaming sounds are long sounds, like background music in games, that reside in the main memory. As they may be very large, only a little part of it is loaded into the memory. If a part has been played, the used memory area can be overwritten with the following part of the sound. So streaming sounds have a circular characteristic. You can compare this functionality with the hands of a clock. One 'hand' points to  the play position, while the other 'hand' points to the starting position where the data can be overwritten up to the current play position. If the play position reaches the end of the buffer, it continues playing from the beginning of the buffer. So the play position can be before the write position of the buffer! Finally, playback without interruptions depends on the buffer size and the frequency of buffer-filling.

3D Sound

Besides these standard functions, DirectSound5 offers the possibility to add 3D sound. If this has to be done by the software (like with the EWS, as 'AudioRendering' is still  not available), the CPU usage of an Intel Pentium 90 is at 5% per sound. The quality of this emulated 3D sound still has not the quality of a real HRTF, but the last one is more complicated and more CPU intensive. If you want to use it with Direct3D you can simply take the object coordinates and pass them to DirectSound without any conversion. Additionally, Doppler effects can be simulated with DirectSound3D. Delay or even reverb is still not implemented.

To get simple delay effects and high quality 3D sound and/or multiple channel output, you still have to wait for DirectX6 (planned for autumn 1998). You will be able to create something like channel maps, so that you can develop applications for multiple channels soundcards that also work on 'normal' soundcards. The support for streaming and recording sounds will be improved.

Additionally, DirectMusic will be introduced for the first time. The most interesting things are simple midi access and downloadable soundfonts.

If you want more information about DirectSound visit www.microsoft.com/directx<. There are two very interesting documents about how to program static and streaming sounds. Although both documents are based on DirectSound2, you will need the same code when developing for DirectSound5. There are still two points left that are not mentioned in the documents: Mixing with sampling rates other than the default 8Bit and 22kHz and 3D sound (introduced with DirectSound3).

Mixing with sampling rates other than the default

You normally do not have to create a primary soundbuffer. You simply create secondary sound buffers and send the play command. The sounds are mixed automatically with 8Bit and 22kHz. If you want a higher mixing quality, you have to obtain access to the primary sound buffer (do not create one!) and change it´s preferences. The following code shows how to achieve a mixing rate of 16Bit and 44kHz stereo. Notice that the buffer size of the primary buffer is set to 0 (dsbdesc.dwBufferBytes=0;)!

{

...

memset (&pcmwf, 0, sizeof(WAVEFORMATEX));

pcmwf.wFormatTag = WAVE_FORMAT_PCM;

pcmwf.nChannels = 2;

pcmwf.nSamplesPerSec = 44100;

pcmwf.nBlockAlign = 4;

pcmwf.nAvgBytesPerSec = pcmwf.nSamplesPerSec * pcmwf.nBlockAlign;

pcmwf.wBitsPerSample = 16;

ZeroMemory(&dsbdesc, sizeof(DSBUFFERDESC));

dsbdesc.dwSize = sizeof(DSBUFFERDESC);

dsbdesc.dwFlags = DSBCAPS_PRIMARYBUFFER;

dsbdesc.dwBufferBytes = 0;

if (DS_OK != lpDirectSound->CreateSoundBuffer(&dsbdesc, &lpDSB, NULL))

...

}

3D Sound

If you want to add 3D sound there is more to do. First of all, you have to add the parameter DSBCAPS_CTRL3D to the flags of the primary buffer (e.g. dsbdesc.dwFlags = DSBCAPS_CTRL3D | DSBCAPS_PRIMARYBUFFER). With the handle you get from the CreateSoundBuffer command (e.g. lpDSB) you can get access to the IDirectSound3DListener interface with the QueryInterface command (e.g. lpDSB->QueryInterface(IID_IDirectSound3DListener, (void**) &lpDS3DListener)). This interface has functions to change the position, speed and other preferences of the 3D listener. The lpDS3DListener from the last example must have the type LPDIRECTSOUND3DLISTENER.

Additionally, the initialisation of the secondary buffers has to be changed. The flag DSBCAPS_CTRL3D has to be set and the following line has to be added: lpDSB->QueryInterface(IID_IDirectSound3DBuffer, (void**) &lpDS3DB);. You have to make sure that you do not use DSBCAPS_CTRL3D and DSBCAPS_CTRLPAN as flags at the same time; otherwise you will get an error. Either you use 3D sound, or you use the panning capabilities of DirectSound.

Finally you must set preferences like lpDS3DB->SetMaxDistance(...)‘ or ‚lpDS3DB->SetVelocity(...) that are described in the DirectX SDK.

The DirectX SDK is free and can be downloaded for a limited time from www.microsoft.com/directx.

© 1998-1999, Computer ConText, Amsterdam, The Netherlands, except for contributions made by others.

Latest update on page: 08-02-99

ITED" S-Format="%d-%m-%y" startspan -->08-02-99