Platform SDK: DirectX

Creating a Capture Buffer

[C++]

You create a capture buffer by calling the IDirectSoundCapture::CreateCaptureBuffer method of the DirectSoundCapture object.

One of the parameters to the method is a DSCBUFFERDESC structure that describes the characteristics of the desired buffer. The last member of this structure is a WAVEFORMATEX structure, which must be initialized with the details of the desired wave format. For more information on this structure, see Sound Data.

Note that if your application is using DirectSound as well as DirectSoundCapture, capture buffer creation can fail when the format of the capture buffer is not the same as that of the primary buffer. The reason is that some cards have only a single clock and cannot support capture and playback at two different frequencies.

The following example sets up a capture buffer that will hold 1 second of data:

/* In this example it is assumed that pDSC is a valid pointer to
   a DirectSoundCapture object. */
 
DSCBUFFERDESC              dscbd;
LPDIRECTSOUNDCAPTUREBUFFER pDSCB;
WAVEFORMATEX               wfx =
    {WAVE_FORMAT_PCM, 2, 44100, 176400, 4, 16, 0};
    // wFormatTag, nChannels, nSamplesPerSec, mAvgBytesPerSec,
    // nBlockAlign, wBitsPerSample, cbSize
dscbd.dwSize = sizeof(DSCBUFFERDESC);
dscbd.dwFlags = 0;
dscbd.dwBufferBytes = wfx.nAvgBytesPerSec;
dscbd.dwReserved = 0;
dscbd.lpwfxFormat = &wfx;
 
pDSCB = NULL;
 
HRESULT hr = pDSC->CreateCaptureBuffer(&dscbd, 
        &pDSCB, NULL);
 
[Visual Basic]

Create a capture buffer by calling the DirectSoundCapture.CreateCaptureBuffer method.

One parameter to the method is a DSCBUFFERDESC type that describes the characteristics of the desired buffer. The fxFormat member is a WAVEFORMATEX type, which must be initialized with the details of the desired wave format.

If your application is using DirectSound as well as DirectSoundCapture, capture buffer creation can fail when the format of the capture buffer is not the same as that of the primary buffer. The reason is that some cards have only a single clock and cannot support capture and playback at two different frequencies.

The following example sets up a capture buffer that will hold 1 second of data.

' DSC is a DirectSoundCapture object.
 
Dim dscbd As DSCBUFFERDESC
Dim DSCB As DirectSoundCaptureBuffer
Dim waveFormat As WAVEFORMATEX
 
' Set up the wave format.
 
waveFormat.nSize = LenB(waveFormat)
    waveFormat.nFormatTag = WAVE_FORMAT_PCM
    waveFormat.nChannels = 2
    waveFormat.lSamplesPerSec = 22050
    waveFormat.nBitsPerSample = 16
    waveFormat.nBlockAlign = _
            waveFormat.nBitsPerSample / 8 * WaveFormat.nChannels
    waveFormat.lAvgBytesPerSec = _
            waveFormat.lSamplesPerSec * waveFormat.nBlockAlign
 
' Set up the DSCBUFFERDESC.
 
dscbd.lFlags = DSCBCAPS_DEFAULT
dscbd.lBufferBytes = waveFormat.lAvgBytesPerSec
dscbd.fxFormat = waveFormat
 
' Create the capture buffer.
 
Set DSCB = DSC.CreateCaptureBuffer (dscbd)