DirectSound playback

The basic flow of DirectSound playback

1. Create a DirectSound object;

2. Set the cooperation level of the device;

3. Create a buffer and set the playback format;

4. Call the playback and control functions to achieve sound playback and control.

detailed steps:

1. Enumerate the output sound devices of the system and call the DirectSoundEnumerate function;

2. Create a DirectSound object, call the global function DirectSoundCreate8 to create an IDirectSound8 interface, by calling various methods of the interface, you can generate a buffer object, get and set the properties of the device object, etc.;

3. Set the cooperation level of the sound device and call the IDirectSound8::SetCooperativeLevel function;

4. Create a sound buffer object. To create a sound buffer, the application needs to fill a DSBUFFERDESC structure, and then call the IDirectSound8::CreateSoundBuffer method, CreateSoundBuffer will create a direct buffer object (DirectSoundBuffer) and return a pointer to the IDirectSoundBuffer interface, which can then be obtained through this interface pointer IDirectSoundBuffer8 interface for buffer playback and other buffer operations;

5. Load WAVE sound data, call mmioOpen, mmioDescend, mmioAscend, mmioRead and other functions;

6. Sound buffer playback. Note that certain steps must be followed to write sound data into the sound buffer:

Steps to load sound data into static buffer

①Call the IDirectSoundBuffer8::Lock method to lock the entire buffer, and set the offset of the data write buffer position (usually 0, that is, start writing from the buffer start address), and get the memory address of that point ( pointer);
②Using the standard memory copy method, write the sound data into the returned memory address;
③ Call the IdirectSoundBuffer8::Unlock method to unlock the buffer.

Steps to load audio data into the stream buffer

①Ensure the buffer to prepare for receiving data;
②Call the IDirectSoundBuffer8::Lock function to lock the position of the buffer, this function returns one or two addresses where data can be written;
③Use the standard method of copying data to write audio data into the buffer;
④ Use the IDirectSoundBuffer8::Unlock function to unlock the buffer.

Precautions

1. When the application uses the user's preferred output device (the default sound output device set by the control panel) to output sound, it is not necessary to enumerate all the output devices in the system. However, if two or more devices are required, or the function of selecting output devices by the user is provided, the sound output devices in the system need to be enumerated.

2. The method of collaboration level must be called before playing the audio buffer, usually immediately after the DirectSound object is created, otherwise the sound will not be heard. Because Windows uses a multitasking mechanism, there may be multiple applications using the same device driver at the same time. By setting the collaboration level, DirectX guarantees that each application will not gain access to the device at the wrong time or in the wrong way. Each DirectSound application has a collaboration level that determines the scope and extent of the device it is allowed to access.

3. When the application initializes DirectSound, it will automatically create a main sound buffer for mixing and sending the sound to the output device. The application must create one or more secondary buffers to store and play independent sound information. When creating a secondary sound buffer, you need to define whether it is a static sound buffer or a streaming sound buffer.


Guess you like

Origin blog.csdn.net/gkzscs/article/details/53995441