Step 3: Creating the Secondary Buffer

Still inside the SetupStreamBuffer function, you now create a secondary sound buffer in the same format as the wave file. The process is similar for that you used in Step 1 to create a primary buffer. First you describe the buffer in the global DSBUFFERDESC structure, then you pass this description to the IDirectSound::CreateSoundBuffer method.

    memset(&dsbdesc, 0, sizeof(DSBUFFERDESC)); 
    dsbdesc.dwSize = sizeof(DSBUFFERDESC); 
    dsbdesc.dwFlags = 
            DSBCAPS_GETCURRENTPOSITION2   // Always a good idea
            | DSBCAPS_GLOBALFOCUS         // Allows background playing
            | DSBCAPS_CTRLPOSITIONNOTIFY; // Needed for notification
 
    // The size of the buffer is arbitrary, but should be at least
    // two seconds, to keep data writes well ahead of the play
    // position.
 
    dsbdesc.dwBufferBytes = pwfx->nAvgBytesPerSec * 2;  
    dsbdesc.lpwfxFormat = pwfx; 
 
    if FAILED(IDirectSound_CreateSoundBuffer(
            lpds, &dsbdesc, &lpdsb, NULL))
    {
        WaveCloseReadFile(&hmmio, &pwfx);
        return FALSE; 
    }