Platform SDK: DirectX

Step 4: Set Up Play Notifications

[Visual Basic]

The information in this topic pertains only to applications written in C and C++. See DirectSound Visual Basic Tutorials.

[C++]

Now that you've successfully created a streaming buffer, you ask to be notified whenever the current play position reaches certain points in the buffer, so you'll know when it's time to stream more data. In the example, those positions will be set at the beginning and halfway mark of the buffer.

First you create the required number of events and store their handles in the rghEvent array:

    for (int i = 0; i < NUMEVENTS; i++)
    {
        rghEvent[i] = CreateEvent(NULL, FALSE, FALSE, NULL);
        if (NULL == rghEvent[i]) return FALSE;
    }
 

Next, you initialize the array of DSBPOSITIONNOTIFY structures, each of which associates a position in the buffer with an event handle:

    rgdsbpn[0].dwOffset = 0;
    rgdsbpn[0].hEventNotify = rghEvent[0];
    rgdsbpn[1].dwOffset = (dsbdesc.dwBufferBytes/2);
    rgdsbpn[1].hEventNotify = rghEvent[1];
 

Then you get the IDirectSoundNotify interface from the secondary buffer and pass the DSBPOSITIONNOTIFY array to the SetNotificationPositions method:

    if FAILED(IDirectSoundBuffer_QueryInterface(lpdsb, 
            IID_IDirectSoundNotify, (VOID **)&lpdsNotify))
        return FALSE; 
 
    if FAILED(IDirectSoundNotify_SetNotificationPositions(
             lpdsNotify, NUMEVENTS, rgdsbpn))
    {
        IDirectSoundNotify_Release(lpdsNotify);
        return FALSE;
    }
 

That completes the setup of the streaming buffer. Because you've already opened the wave file and are ready to start streaming data, you can set the buffer in motion here. You want it to continue running till the complete sound has been played, so you set the DSBPLAY_LOOPING flag.

    IDirectSoundBuffer_Play(lpdsb, 0, 0, DSBPLAY_LOOPING);
 
    return TRUE;
} // end of SetupStreamBuffer()

Next: Step 5: Handle the Play Notifications