Platform SDK: DirectX

Current Play and Write Positions

DirectSound maintains two pointers into the buffer: the current play position (sometimes called the play cursor) and the current write position (or write cursor). These positions are byte offsets into the buffer, not absolute memory addresses.

[C++]

The IDirectSoundBuffer::Play method always starts playing at the buffer's current play position. When a buffer is created, the play position is set to zero. As a sound is played, the play position moves and always points to the next byte of data to be output. When the buffer is stopped, the play position remains at the next byte of data.

[Visual Basic]

The DirectSoundBuffer.Play method always starts playing at the buffer's current play position. When a buffer is created, the play position is set to zero. As a sound is played, the play position moves and always points to the next byte of data to be output. When the buffer is stopped, the play position remains at the next byte of data.

The current write position is the point after which it is safe to write data into the buffer. The block between the current play position and the current write position is already committed to be played, and cannot be changed safely.

Visualize the buffer as a clock face, with data written to it in a clockwise direction. The play position and the write position are like two hands sweeping around the face at the same speed, the write position always keeping a little ahead of the play position. If the play position points to the 1 and the write position points to the 2, it is only safe to write data after the 2. Data between the 1 and the 2 may already have been queued for playback by DirectSound and should not be touched.

The write position moves with the play position, not with data written to the buffer. If you're streaming data, you are responsible for maintaining your own pointer into the buffer to indicate where the next block of data should be written.

[C++]

Note that the dwWriteCursor parameter to the IDirectSoundBuffer::Lock method is not the current write position; it is the offset within the buffer where you actually intend to begin writing data. If you do want to begin writing at the current write position, specify DSBLOCK_FROMWRITECURSOR in the dwFlags parameter. In this case the dwWriteCursor parameter is ignored.

An application can retrieve the current play and write positions by calling the IDirectSoundBuffer::GetCurrentPosition method. The IDirectSoundBuffer::SetCurrentPosition method lets you set the current play position, but the current write position cannot be changed.

To ensure that the current play position is reported as accurately as possible, always specify the DSBCAPS_GETCURRENTPOSITION2 flag when creating a secondary buffer. For more information, see DSBUFFERDESC.

[Visual Basic]

Note that the start parameter to the DirectSoundBuffer.WriteBuffer method is not the current write position; it is the offset within the buffer where you actually intend to begin writing data. If you do want to begin writing at the current write position, you specify DSBLOCK_FROMWRITECURSOR in the flags parameter. In this case the start parameter is ignored.

An application can retrieve the current play and write positions by calling the DirectSoundBuffer.GetCurrentPosition method. The DirectSoundBuffer.SetCurrentPosition method lets you set the current play position, but the current write position cannot be changed.

To ensure that the current play position is reported as accurately as possible, always specify the DSBCAPS_GETCURRENTPOSITION2 flag when creating a secondary buffer. For more information, see DSBUFFERDESC.