Platform SDK: DirectX

Capture Buffer Notification

[C++]

You may want your application to be notified when the current read position reaches a certain point in the buffer, or when it reaches the end. The current read position is the point up to which it is safe to read data from the buffer. With the IDirectSoundNotify::SetNotificationPositions method you can set any number of points within the buffer where events are to be signaled.

First you have to obtain a pointer to the IDirectSoundNotify interface. You can do this with the capture buffer's QueryInterface method, as shown in the example under Play Buffer Notification.

Next create an event object with the Win32 CreateEvent function. You put the handle to this event in the hEventNotify member of a DSBPOSITIONNOTIFY structure, and in the dwOffset member of that structure you specify the offset within the buffer where you want the event to be signaled. Then you pass the address of the structure—or array of structures, if you want to set more than one notification position—to the IDirectSoundNotify::SetNotificationPositions method.

The following example sets up three notification positions in a one-second buffer. One event will be signaled when the read position nears the halfway point in the buffer, another will be signaled when it nears the end of the buffer, and the third will be signaled when capture stops.

#define cEvents  3
 
/* In this example it is assumed that the following variables have
   been properly initialized, and that wfx was included in the buffer
   description when the buffer was created.
 
   LPDIRECTSOUNDNOTIFY  lpDsNotify;
   WAVEFORMATEX          wfx;  
*/
 
HANDLE             rghEvent[cEvents] = {0};
DSBPOSITIONNOTIFY  rgdsbpn[cEvents];
HRESULT            hr;
int                i;
 
// Create the events
for (i = 0; i < cEvents; ++i)
{
    rghEvent[i] = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (NULL == rghEvent[i])
    {
        hr = GetLastError();
        goto Error;
    }
}
 
// Describe notifications. 
 
rgdsbpn[0].dwOffset = (wfx.nAvgBytesPerSec/2) -1;
rgdsbpn[0].hEventNotify = rghEvent[0];
 
rgdsbpn[1].dwOffset = wfx.nAvgBytesPerSec - 1;
rgdsbpn[1].hEventNotify = rghEvent[1];
 
rgdsbpn[2].dwOffset = DSBPN_OFFSETSTOP;
rgdsbpn[2].hEventNotify = rghEvent[2];
 
// Create notifications.
 
hr = lpDsNotify->SetNotificationPositions(cEvents, rgdsbpn);
 
[Visual Basic]

You may want your application to be notified when the current read position reaches a certain point in the buffer, or when it reaches the end. The current read position is the point up to which it is safe to read data from the buffer. With the DirectSoundCaptureBuffer.SetNotificationPositions method you can set any number of points within the buffer where events are to be signaled.

To set a notification event on a buffer, you must first implement a DirectXEvent object in the form that contains the code for the buffer. Then you create an event handle by calling the DirectX7.CreateEvent method and pass the event handle in the hEventNotify member of a DSBPOSITIONNOTIFY type. Pass the DSBPOSITIONNOTIFY type to the DirectSoundCaptureBuffer.SetNotificationPositions method.

When the play position reaches a notification position in the buffer, the DirectXEvent.DXCallback is called.