Using the Capture Buffer

[The feature associated with this page, DirectSound, is a legacy feature. It has been superseded by WASAPI and Audio Graphs. Media Casting have been optimized for Windows 10 and Windows 11. Microsoft strongly recommends that new code use Media Casting instead of DirectSound, when possible. Microsoft suggests that existing code that uses the legacy APIs be rewritten to use the new APIs if possible.]

Capturing a sound consists of the following steps:

  1. Start the buffer by calling the IDirectSoundCaptureBuffer8::Start method. Normally you should pass DSCBSTART_LOOPING in the dwFlags parameter so that the buffer will keep running continuously rather than stopping when it reaches the end. Audio data from the input device begins filling the buffer from the beginning.

  2. Wait until the desired amount of data is available. For one method of determining when the capture cursor reaches a certain point, see Capture Buffer Notification.

  3. When sufficient data is available, lock a portion of the capture buffer by calling the IDirectSoundCaptureBuffer8::Lock method.

    To make sure you are not attempting to lock a portion of memory that is about to be used for capture, you can first obtain the position of the read cursor by calling IDirectSoundCaptureBuffer8::GetCurrentPosition.

    As parameters to the Lock method, you pass the size and offset of the block of memory you want to read. The method returns a pointer to the address where the memory block begins, and the size of the block. If the block wraps around from the end of the buffer to the beginning, two pointers are returned, one for each section of the block. The second pointer is NULL if the locked portion of the buffer does not wrap around.

  4. Copy the data from the buffer, using the addresses and block sizes returned by the Lock method.

  5. Unlock the buffer with the IDirectSoundCaptureBuffer8::Unlock method.

  6. Repeat steps 2 to 5 until you are ready to stop capturing data. Then call the IDirectSoundCaptureBuffer8::Stop method.