StreamData
Description
The StreamData sample shows how to stream a wave file to a DirectSound secondary buffer. It is similar to the PlaySound sample, but adds support for streaming.
Path
Source: (SDK root)\Samples\Multimedia\DirectSound\StreamData
Executable: (SDK root)\Samples\Multimedia\DirectSound\Bin
User's Guide
Load a wave 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:
- Call the DirectSoundCreate8 function to create the DirectSound object.
- Call IDirectSound8::SetCooperativeLevel.
- 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:
- Read the wave file header to get the data size and format.
- Choose a DirectSound buffer size. For this sample, the buffer holds about 3 seconds of data.
- Create a DirectSound buffer of that size and with the same format as the wave 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.
- 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:
- See if the buffer has been lost, and call IDirectSoundBuffer8::Restore if so.
- Fill the entire buffer with data from the file.
- 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 see if a notification is signaled:
- Look for a signaled event in the message loop or in a separate thread by calling MsgWaitForMultipleObjects.
- 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 wave 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