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 of an 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. 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
// LPDIRECTSOUNDNOTIFY lpDsNotify;
// lpDsNotify was initialized with QueryInterface.
// WAVEFORMATEX wfx;
// wfx was initialized when the buffer was created.
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;
}
}
// Set notification positions.
// Notify us when read position is halfway through the buffer,
// assuming buffer holds one second of audio.
rgdsbpn[0].dwOffset = (wfx.nAvgBytesPerSec/2) -1;
rgdsbpn[0].hEventNotify = rghEvent[0];
// Notify us when capture is at the end of the buffer.
rgdsbpn[1].dwOffset = wfx.nAvgBytesPerSec - 1;
rgdsbpn[1].hEventNotify = rghEvent[1];
rgdsbpn[2].dwOffset = DSBPN_OFFSETSTOP;
rgdsbpn[2].hEventNotify = rghEvent[2];
hr = lpDsNotify->SetNotificationPositions(cEvents, rgdsbpn);