Microsoft Windows CE 3.0  

Creating Secondary Buffers

Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

To create a sound buffer, your application fills a DSBUFFERDESCstructure and then passes its address to the IDirectSound::CreateSoundBuffermethod. This method creates a DirectSoundBuffer object and returns a pointer to an IDirectSoundBufferinterface. Your application uses this interface to manipulate and play the buffer.

The following example illustrates how to create a basic secondary sound buffer:

BOOL AppCreateBasicBuffer( LPDIRECTSOUND
lpDirectSound, LPDIRECTSOUNDBUFFER *lplpDsb) { PCMWAVEFORMAT pcmwf;
DSBUFFERDESC dsbdesc; HRESULT hr; // Set up wave format structure.
memset(&pcmwf, 0, sizeof(PCMWAVEFORMAT)); pcmwf.wf.wFormatTag =
WAVE_FORMAT_PCM; pcmwf.wf.nChannels = 2; pcmwf.wf.nSamplesPerSec =
22050; pcmwf.wf.nBlockAlign = 4; pcmwf.wf.nAvgBytesPerSec =
pcmwf.wf.nSamplesPerSec * pcmwf.wf.nBlockAlign;
pcmwf.wBitsPerSample = 16; // Set up DSBUFFERDESC structure.
memset(&dsbdesc, 0, sizeof(DSBUFFERDESC)); // Zero it out.
dsbdesc.dwSize = sizeof(DSBUFFERDESC); // Need default controls
(pan, volume, frequency). dsbdesc.dwFlags = DSBCAPS_CTRLDEFAULT; //
3-second buffer. dsbdesc.dwBufferBytes = 3 *
pcmwf.wf.nAvgBytesPerSec; dsbdesc.lpwfxFormat =
(LPWAVEFORMATEX)&pcmwf; // Create buffer. hr =
lpDirectSound->lpVtbl->CreateSoundBuffer(lpDirectSound,
&dsbdesc, lplpDsb, NULL); if SUCCEEDED(hr) { // Valid interface
is in *lplpDsb. return TRUE; } else { // Failed. *lplpDsb = NULL;
return FALSE; } }

Your application should create buffers for the most important sounds first, and then create buffers for other sounds in descending order of importance. DirectSound allocates hardware resources to the first buffers that can take advantage of them.

If your application must explicitly locate buffers in hardware or software, you can specify either the DSBCAPS_LOCHARDWARE or DSBCAPS_LOCSOFTWARE flag in the DSBUFFERDESCstructure. If the DSBCAPS_LOCHARDWARE flag is specified and there is insufficient hardware memory or mixing capacity, the buffer creation request fails.

You can ascertain the location of an existing buffer by using the IDirectSoundBuffer::GetCapsmethod and checking the dwFlagsmember of the DSBCAPSstructure for either the DSBCAPS_LOCHARDWARE or DSBCAPS_LOCSOFTWARE flags. One or the other is always specified.

When you create a sound buffer, you can indicate that a buffer is static by specifying the DSBCAPS_STATIC flag. If you do not specify this flag, the buffer is a streaming buffer. For more information, see Static and Streaming Sound Buffers.

DirectSoundBuffer objects are owned by the DirectSound object that created them. When the DirectSound object is released, all buffers created by that object also will be released and should not be referenced.



 Last updated on Tuesday, May 18, 2004

© 2004 Microsoft Corporation. All rights reserved.