Microsoft DirectX 8.1 (Visual Basic)

Using Streaming Buffers

 A streaming buffer plays a long sound that cannot all fit into the buffer at once. As the buffer plays, old data is periodically replaced with new data.

To play a streaming buffer, call the DirectSoundSecondaryBuffer8.Play method, specifying DSBPLAY_LOOPING in the flags parameter.

To halt playback, call the DirectSoundSecondaryBuffer8.Stop method. This method stops the buffer immediately, so you need to be sure that all data has been played. This can be done by polling the play position or by setting a notification position.

Stream data into a buffer by using the DirectSoundSecondaryBuffer8.WriteBuffer method. This method lets you write data to any portion of the buffer. Although it's possible to write to the entire buffer, you must not do so while it is playing. Refresh only a portion of the buffer each time. For example, you might lock and write to the first quarter of the buffer as soon as the play cursor reaches the second quarter, and so on. You must never write to the part of the buffer that lies between the play cursor and the write cursor. For more information, see Play and Write Cursors.

The following sample code writes 1000 bytes of data from the byte array myBuffer to the beginning of the buffer represented by dsb:

dsb.WriteBuffer 0, 1000, myBuffer(0), DSBLOCK_DEFAULT

WriteBuffer handles wraparound transparently. Suppose the secondary buffer has been created with a size of 10,000 bytes, and the application writes 5000 bytes to an offset past the midpoint of the buffer, as follows:

dsb.WriteBuffer 8000, 5000, myBuffer(0), DSBLOCK_DEFAULT

This call writes the first 2000 bytes to the last part of the buffer and the next 3000 bytes to the beginning of the buffer.

It is the application's responsibility to track the offset for the next data write. The easiest way to do this is by setting notification positions and writing a fixed number of bytes each time an event is triggered.

For more information, see the following topics: