Directsound Study Notes (6): Create a secondary buffer

Source: Internet
Author: User

Directsound Buffer

The directsound buffer object controls the propagation of waveform data from the source to the destination. The source may be a synthesizer, another synthesizer, a WAV file, or a resource. For most synthesizer, the destination is a hybrid device called the main buffer zone. Data is transmitted from the primary buffer to the hardware, which converts sampling to acoustic waves.

Buffer base

Your applicationProgramAt least one secondary buffer must be created to store the combined play sound.
A secondary buffer can exist throughout the application lifecycle or be destroyed if it is no longer needed. It can be a static buffer, including a single short sound, or a stream buffer, which updates data while playing in the buffer. To limit memory requirements, a long sound should be played through the stream buffer, which can hold sound data for several seconds.
The created secondary buffers are not all similar. The features of the buffer include:
Format: the format of the buffer must match the waveform data format played by the buffer.
Control: different buffers can have different controls, such as volume, frequency, and moving in two or three dimensions. When creating a buffer, you should specify only the control you need. For example, do not create a 3D sound buffer in a non-3D environment.
Location: a buffer can be located in hardware-managed memory or software-managed memory. The hardware buffer is more effective but the number is limited.

Create secondary buffer

Call the idirectsound8: createsoundbuffer method to create a buffer. This method returns a pointer to an idirectsoundbuffer interface, through which the application can obtain an idirectsoundbuffer8 interface.
The following example creates an auxiliary sound buffer and returns the idirectsoundbuffer8 interface.

Hresult createbasicbuffer (lpdirectsound8 lpdirectsound, lpdirectsoundbuffer8 * Ppdsb8)
{
Waveformatex WFX;
Dsbufferdesc dsbdesc;
Lpdirectsoundbuffer pdsb = NULL;
Hresult hr;
 
// Set up WAV format structure.

Memset ( & WFX, 0 , Sizeof (Waveformatex ));
WFX. wformattag = Wave_format_pcm;
WFX. nchannels = 2 ;
WFX. nsamplespersec = 22050 ;
WFX. nblockalign = 4 ;
WFX. navgbytespersec = WFX. nsamplespersec * WFX. nblockalign;
WFX. wbitspersample = 16 ;
 
// Set up dsbufferdesc structure.
 
Memset ( & Dsbdesc, 0 , Sizeof (Dsbufferdesc ));
Dsbdesc. dwsize = Sizeof (Dsbufferdesc );
Dsbdesc. dwflags =  
Dsbcaps_ctrlpan | Dsbcaps_ctrlvolume | Dsbcaps_ctrlfrequency;
Dsbdesc. dwbufferbytes = 3 * WFX. navgbytespersec;
Dsbdesc. lpwfxformat = & WFX;
 
// Create buffer.
 
HR = Lpdirectsound -> Createsoundbuffer ( & Dsbdesc, & Pdsb, null );
If (Succeeded (HR ))
{
HR=Pdsb->QueryInterface (iid_idirectsoundbuffer8, (lpvoid*) Ppdsb8 );
Pdsb->Release ();
}  
Return HR;
}

The example function creates a stream buffer with a size of three seconds. A non-stream buffer should be created in sufficient size to accommodate the whole sound data.

If the location of a buffer is not specified, directsound tries its best to put it in the hardware control memory. Because hardware buffers are mixed by sound card processors, they have little impact on application performance.
If you want to specify a buffer location, instead of letting directsound decide where it belongs, set the dsbcaps_lochardware or dsbcaps_locsoftware identifier in the sdbufferdesc structure. If the dsbcaps_lochardware identifier is set but there is not enough hardware resources, the request for creating a buffer will fail.
To use the sound management feature of direcsound, you must set the dsbcaps_locdeffer ID when creating a buffer zone. This identifier delays the allocation of buffer resources to buffer play.
You can use the idirectsoundbuffer8: getcaps method to check the dwflags Member of the dsbcaps structure to locate the buffer zone that already exists.
The buffer object belongs to the device object that created it. When a device object is released, all the buffers created by the object will also be released, so they should not be referenced again.

Copy Buffer

You can use the idirectsound8: duplicatesoundbuffer method to create more than two secondary buffers containing the same data. You cannot copy the primary buffer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.