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

#### Understanding the Cooperation Level

After you create the main DirectSound object, it’s time to set the cooperation level of DirectSound. DirectSound is a little trickier than DirectDraw as far as cooperation level is concerned. You can’t be as brutal when taking over the sound system as you can with graphics. Well, you can if you want, but Microsoft advises that you don’t, so take their advice.

There are a number of cooperation levels that DirectSound can be set to. They are divided into two groups: settings that give you control over the primary sound buffer, and settings that don’t. Remember, the primary sound buffer represents the actual mixing hardware (or software) that is mixing sounds at all times and sending them out to the speaker. If you mess with the primary buffer, DirectSound will want you to make sure you know what you’re doing because it could crash or distort not only your application’s sound, but others as well. Here’s a general briefing on each cooperation level:

  • Normal Cooperation—This is the most cooperative of all the settings. While your application has the focus, it will be able to play sounds, but so will other applications. Furthermore, you don’t have write permission to the primary buffer, and DirectSound will create a default primary buffer of 22 KHz, stereo, 8-bit for you. I suggest using this setting most of the time.
  • Priority Cooperation—With this setting you have first access to all the hardware, you can change the setting of the primary mixer, and you can request the sound hardware to perform advanced memory operations such as compaction. This setting is only necessary if you must change the data format of the primary buffer—which you might do if you wanted to play 16-bit samples, for example.
  • Exclusive Cooperation—Same as Priority, but your application will be audible only when it’s in the foreground.
  • `Write_Primary` Cooperation—This is the highest priority. You have total control and must control the primary buffer yourself to hear anything. You would only use this mode if you were writing your own sound mixer or engine—I think only John Miles uses this one. :)

#### Setting the Cooperation Level

In my opinion, you should use the normal priority level until you get the hang of DirectSound. It’s the easiest to get working and has the smoothest operation. To set the cooperation level, use the `SetCooperativeLevel()` function from the interface of the main DirectSound object. Here’s the prototype:

    HRESULT SetCooperativeLevel(HWND hwnd, // window handle

                                DWORD dwLevel); // cooperation level setting

The function returns `DS_OK` if successful and something else otherwise. But make sure to check for errors because it’s more than possible that another application has taken control of the sound card. Table 10.2 lists the flag settings for the various cooperation levels.

Table 10.2 Settings for DirectSound `SetCooperativeLevel()`

Value | Description
--- | ---

`DSSCL_NORMAL` | Sets normal cooperation.
`DSSCL_PRIORITY` | Sets priority cooperation level, allowing you to set the data format of the primary buffer.
`DSSCL_EXCLUSIVE` | Gives you priority cooperation, in addition to exclusive control when your application is in the foreground.
`DSSCL_WRITEPRIMARY` | Gives you total control of the primary buffer.

Here’s how you would set the cooperation level to normal after creating the DirectSound object:

    if (FAILED(lpds->SetCooperativeLevel(main_window_handle,

                           DSSCL_NORMAL)))

       { /* error setting cooperation level */ }

Cool, huh? Take a look at `DEMO10_1.CPP|EXE` on the CD. It creates a DirectSound object, sets the cooperation level, and then releases the object on exit. It doesn’t make any sound, though—that’s next!

> Tip: When you’re compiling programs from this chapter, make sure to include `DSOUND.LIB` in your project.

### Primary and Secondary Sound Buffers

The DirectSound object that represents the sound card itself has a single primary buffer. The primary buffer represents the mixing hardware (or software) on the card and processes all the time, like a little conveyor belt. Manual primary buffer mixing is very advanced, and luckily you don’t have to do it. DirectSound takes care of the primary buffer for you as long as you don’t set the cooperation level to the highest priority. In addition, you don’t need to create a primary buffer because DirectSound creates one for you, as long as you set the cooperation level to one of the lower levels, such as `DSSCL_NORMAL`.

The only drawback is that the primary buffer will be set for 22 KHz stereo in 8-bit. If you want 16-bit sound or a higher playback rate, you’ll have to at least set the cooperation level to `DSSCL_PRIORITY` and then set a new data format for the primary buffer. But for now, just use the default because it makes life much easier.

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.