Tricks of the Windows Game Programming Gurus:Sounding Off with DirectSound and DirectMusic

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

#### Working with Secondary Buffers

Secondary buffers represent the actual sounds that you want to play. They can be any size that you want, as long as you have the memory to hold them. However, the SRAM on the sound card can only hold so much sound data, so be careful when you’re requesting sounds to be stored on the sound card itself. But sounds that are stored on the sound card itself will take much less processing power to play, so keep that in mind.

Now there are two kinds of secondary buffers—static and streaming. Static sound buffers are sounds that you plan to keep around and play over and over. These are good candidates for SRAM or system memory. Streaming sound buffers are a little different. Imagine that you want to play an entire CD with DirectSound. I don’t think you have enough system RAM or SRAM to store all 650MB of audio data in memory, so you’d have to read the data in chunks and stream it out to a DirectSound buffer. This is what streaming buffers are for. You continually feed them with new sound data as they are playing. Sound tricky? Take a look at Figure 10.10.

Figure 10.10 Streaming audio data.

In general, all secondary sound buffers can be written to static or streaming. However, because it’s possible that the sound will be playing as you’re trying to write to it, DirectSound uses a scheme to take this into consideration: circular buffering. This means that each sound is stored in a circular data array that is continually read from at one point by the play cursor and written to at another point (slightly behind the first point) by the write cursor. Of course, if you don’t need to write to your sound buffers as they are playing, you don’t have to worry about this, but you will when you’re streaming audio.

To facilitate this complex, buffered real-time writing capability, the data access functions for sound buffers might return a memory space that’s broken up into two pieces because the data block you’re trying to write exists at the end of the buffer and overflows into the beginning of the buffer. The point is, you need to know this fact if you’re going to stream audio. However, in most games all this is moot, because as long as you keep all the sound effects to a few seconds each and the musical tracks are all loaded on demand, you can usually fit everything into a few megabytes of RAM. Using 2-4MB of storage for sound in a 32MB+ machine isn’t too much of a problem..

#### Creating Secondary Sound Buffers

To create a secondary sound buffer, you must make a call to `CreateSoundBuffer()` with the proper parameters. If successful, the function creates a sound buffer, initializes it, and returns an interface pointer to it of this type:

    LPDIRECTSOUNDBUFFER lpdsbuffer; // a directsound buffer

However, before you make the call to `CreateSoundBuffer()`, you must set up a DirectSoundBuffer description structure, which is similar to a DirectDrawSurface description. The description structure is of the type `DSBUFFERDESC` and is shown here:

    typedef struct

    {

    DWORD  dwSize;        // size of this structure

    DWORD  dwFlags;       // control flags

    DWORD  dwBufferBytes; // size of the sound buffer in bytes

    DWORD  dwReserved;    // unused

    LPWAVEFORMATEX  lpwfxFormat; // the wave format

    } DSBUFFERDESC, *LPDSBUFFERDESC;

The `dwSize` field is the standard DirectX structure size, `dwBufferBytes` is how big you want the buffer to be in bytes, and `dwReserved` is unused. The only fields of real interest are `dwFlags` and `lpwfxFormat`. `dwFlags` contains the creation flags of the sound buffer. Take a look at Table 10.3, which contains a partial list of the more basic flag settings.

Table 10.3 DirectSound Secondary Buffer Creation Flags

Value | Description
--- | ---

`DSBCAPS_CTRLALL` | The buffer must have all control capabilities.
`DSBCAPS_CTRLDEFAULT` | The buffer should have default control options. This is the same as specifying the `DSBCAPS_CTRLPAN`, `DSBCAPS_CTRLVOLUME`, and `DSBCAPS_CTRLFREQUENCY` flags.
`DSBCAPS_CTRLFREQUENCY` | The buffer must have frequency control capability.
`DSBCAPS_CTRLPAN` | The buffer must have pan control capability.
`DSBCAPS_CTRLVOLUME` | The buffer must have volume control capability.
`DSBCAPS_STATIC` | Indicates that the buffer will be used for static sound data. Most of the time you’ll create these buffers in hardware memory if possible.
`DSBCAPS_LOCHARDWARE` | Use hardware mixing and memory for this sound buffer if memory is available.
`DSBCAPS_LOCSOFTWARE` | Forces the buffer to be stored in software memory and use software mixing, even if `DSBCAPS_STATIC` is specified and hardware resources are available.
`DSBCAPS_PRIMARYBUFFER` | Indicates that the buffer is a primary sound buffer. Only set this if you want to create a primary buffer and you’re a sound god.

In most cases you’ll set the flags to `DSBCAPS_CTRLDEFAULT | DSBCAPS_STATIC | DSBCAPS_LOCSOFTWARE` for default controls, static sound, and system memory, respectively. If you want to use hardware memory, use `DSBCAPS_LOCHARDWARE` instead of `DSBCAPS_LOCSOFTWARE`.

> Note: The more capabilities you give a sound, the more stops (software filters) it has to go through before being heard. This means more processing time. Alas, if you don’t need volume, pan, and frequency shift ability, forget `DSBCAPS_CTRLDEFAULT` and just use the capabilities that you absolutely need.

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.