Step 1: Set Up DirectSound

The tutorial requires the following definitions and global variables:

#define NUMEVENTS 2

LPDIRECTSOUND lpds;

DSBUFFERDESC dsbdesc;

LPDIRECTSOUNDBUFFER lpdsb;

LPDIRECTSOUNDNOTIFY lpdsNotify;

WAVEFORMATEX *pwfx;

HMMIO hmmio;

MMCKINFO mmckinfoData, mmckinfoParent;

DSBPOSITIONNOTIFY rgdsbpn[NUMEVENTS];

HANDLE rghEvent[NUMEVENTS];

The first step is to create the DirectSound object and establish a cooperative level. This is done in the InitDSound function shown in the following code. The function takes two parameters: the main window handle and a pointer to the GUID of the sound device. In most cases you will pass NULL as the second parameter, indicating the default device, but you may obtain a GUID by device enumeration.

BOOL InitDSound(HWND hwnd, GUID *pguid)

{

// Create DirectSound

if FAILED(DirectSoundCreate(pguid, &lpds, NULL))

return FALSE;

// Set co-op level

if FAILED(IDirectSound_SetCooperativeLevel(

lpds, hwnd, DSSCL_NORMAL))

return FALSE;

return TRUE;

} // InitDSound()

Note that if you want to change the format of the primary buffer to match that of the wave file or files your application will be using, you must set the cooperative level to DSSCL_PRIORITY rather than DSSC_NORMAL.