JavaScript EditorFree JavaScript Editor     Ajax Editor 



Main Page
  Previous Section Next Section

Starting Up DirectSound

The main DirectSound object represents a sound card(s). If you have more than one sound card, you'll have to enumerate, detect, and request their GUIDs (Globally Unique Identifiers). But if you just want to connect to the default sound device, you don't have to mess with detection; you can simply create a DirectSound object that represents the main sound card. Here's the interface pointer that represents a DirectSound object:

LPDIRECTSOUND  lpds; // directsound interface pointer

To create a DirectSound object, you must make a call to DirectSoundCreate(), prototyped here:

HRESULT DirectSoundCreate(
        LPGUID lpGuid, // guid of sound card
                       // NULL for default device
        LPDIRECTSOUND *lpDS,     // interface ptr to object
        IUnknown FAR *pUnkOuter) // always NULL

The preceding call is very similar to the one used to create the main DirectDraw object. In general, this stuff all looks alike; once you've mastered one part of DirectX, you've mastered them all. The problem is that Microsoft keeps adding new interfaces as fast as you can learn them! Anyway, to create a DirectSound object, do this:

LPDIRECTSOUND lpds; // pointer to directsound object

// create DirectSound object
if (DirectSoundCreate(NULL, &lpds, NULL)!=DS_OK )
   { /* error */ }

Notice that the success value is now DS_OK (DirectSound OK) rather than DD_OK (DirectDraw OK). However, that was just an example to show you the new OK code. Check for success/failure like you've been doing using the FAILURE() and SUCCESS() macros, like this:

// create DirectSound object
if (FAILED(DirectSoundCreate(NULL, &lpds, NULL)))
   { /* error */ }

And of course, when you're done with the DirectSound object, you must release it like this:

lpds->Release();

This step occurs during the shutdown stage of your application.

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 CooperationThis 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 CooperationWith 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 CooperationThis 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.


      Previous Section Next Section
    



    JavaScript EditorAjax Editor     JavaScript Editor