Microsoft DirectX 8.1 (C++)

Capture Buffer Notification

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

First, obtain a pointer to the IDirectSoundNotify8 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 IDirectSoundNotify8::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
 
// 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.
//
// LPDIRECTSOUNDNOTIFY8 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);