Microsoft DirectX 9.0

Stream Data Sample


The Stream Data sample shows how to stream a WAV file to a DirectSound secondary buffer. It is similar to the Play Sound Sample, but adds support for streaming.


Source: (SDK root)\Samples\C++\DirectSound\StreamData

Executable: (SDK root)\Samples\C++\DirectSound\Bin

User's Guide

Load a WAV file by clicking Sound File. Select Loop Sound if you want it to play repeatedly. Click Play.

Programming Notes

The sample shows the basic tasks required to play a streaming sound in a DirectSound secondary buffer.

Set up DirectSound:

  1. Call the DirectSoundCreate8 function to create the DirectSound object.
  2. Call IDirectSound8::SetCooperativeLevel.
  3. Set the primary buffer format. The sample calls the DSUtil_SetPrimaryBufferFormat function in Dsutil.cpp to do this.

Create a DirectSound buffer and set up the notifications:

  1. Read the WAV file header to get the data size and format.
  2. Choose a DirectSound buffer size. For this sample, the buffer holds about 3 seconds of data.
  3. Create a DirectSound buffer of that size and with the same format as the WAV file. Also set the DSBCAPS_CTRLPOSITIONNOTIFY flag so that the buffer can send notification events whenever playback has reached certain points in the buffer. On some drivers, using this flag limits the buffer to software, because the hardware buffers do not support position notifications.
  4. Set up the notifications on the buffer by obtaining an IDirectSoundNotify8 interface from the buffer and calling IDirectSoundNotify8::SetNotificationPositions. See the InitDSoundNotification function. When the play cursor passes a notification position, it signals a Win32 event.

Play the DirectSound buffer:

  1. Ascertain if the buffer has been lost, and call IDirectSoundBuffer8::Restore if so.
  2. Fill the entire buffer with data from the file.
  3. Call IDirectSoundBuffer8::Play with the DSBPLAY_LOOPING flag. The looping flag needs to be set so that the buffer will continue playing after the first batch of data has been played.

Check to ascertain if a notification is signaled:

  1. Look for a signaled event in the message loop or in a separate thread by calling MsgWaitForMultipleObjects.
  2. If a buffer notification event has been signaled, lock the section of the buffer that has just been played and fill it with new data. See the HandleNotification function.

Stop the buffer:

When handling the event notification, keep track of how much data has been put in the buffer.  When the entire WAV file has been put into the buffer, and after DirectSound has played it all, it is necessary to manually stop the buffer since the buffer will continuously loop otherwise.

See Also