A Wrapper class for the directsound API

Source: Internet
Author: User
Document directory
  • The structure of the Wrapper class
  • Directsound object in the Wrapper class
  • Primary Buffer
  • Second Buffer
  • Timer event
Download source files and test program-376 KB

Introduction

This article focuses on giving an example of using the DirectX API in PC game software development by using a directsound Wrapper class.

The directsound Wrapper class

The Wrapper class has the functionality to handle various aspects of sound playback.

The structure of the Wrapper class

The directsound object uses double buffers to manipulate the sound data processing and playback. the directsound object creates a single primary buffer to deal with output devices to play the sound data. the directsound object creates multiple second buffers to load, store varous sound data, and configure sound data performance. the object loads and mixes sound data from the secondary buffers and sends the processed sound data to the primary buffer to play it through the output device.

The Wrapper class contains a pointer to the directsound object, a primary buffer object (a pointer of TypeDirectSoundBuffer), And the secondary buffer array (DirectSoundBuffer Object array)

Directsound object in the Wrapper class

Directsound is a COM interface, so the directsound object is the COM Component Object (a pointer to directsound ). the directsound object is the core of sound playback and all of buffers, both primary buffer and secondary buffers are created by it, so it must be instantiated before doing any sound play back.

Because the directsound object is a COM object, it can be instantiated by either the generic way of creation COM components, or by callingDirectSoundCreate. In my Wrapper class, I find the generic method of creating a COM object is more reliable. Remember: The DirectX interfaces are a subset of the COM interfaces, so before you use them, callCoInitialize/CoInitializeEx,And callCoUninitializeAt the end of your application.

Collapse
// To start...BOOL CSoundManager::CoInitializeDSound(void){    HRESULT  hr;    GUID     guID;    // Clear the GUID    ::memset(&guID, 0, sizeof(GUID));     // Initialize COM service, create STA     hr = ::CoInitialize(NULL);    .................................     .................................    // Create the IDirectSound object with the realiable way (create    // standard COM object)    hr = CoCreateInstance(CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,              IID_IDirectSound, (void**)&m_pIDS);    .................................     ................................    // Initialize the IDirectSound object    hr = IDirectSound_Initialize(m_pIDS, &guID);    .................................     .................................    return TRUE;}// To finish up...void CSoundManager::CoUnInitializeDSound(void){    if(m_pIDS)    {        m_pIDS->Release();        m_pIDS = NULL;        ::CoUninitialize();    }}
Primary Buffer

The primary buffer isDirectSoundBufferObject, and its main task is to receive the processed sound data from the directsound object and send it to the output device to play. the primary's format settings determine the Sound Play effect of the output device. the primary's format isWAVEFORMATEXStructure and must be set before play.

Second Buffer

The second buffer loads sound data and sets the sound performance, then sends the sound to the directsound object. The second buffer isDirectSoundBufferObject and is created by the directsound object.

In my library, I use a Wrapper classDirectSoundBufferFor the second buffer. This Wrapper class hasDirectSoundBufferObject, and the methods to set sound CES, such as adjustment of volume, frequency and channel.

A special feature ofDirectSoundBufferWrapper class is the special sound effect of Fade-in (volume up step by step) and fade-out (volume down step by step ).

To avoid the concurrency of incoming function CILS, A mutex object is used inDirectSoundBufferWrapper class to synchronize the function CILS.

Timer event

The directsound Wrapper class needs a timer to monitor and manipulate the sound status and performance in a special time interval. the directsound Wrapper class is a generic class, so it is hard to use a Windows timer in the class. as an alternative, a waitable timer is used in the class. A thread is created to check the timer event when the class object is instantiated.

The test program

The test program is a dialog box application written in MFC. it tests the various Wrapper class features, such as volume, frequency, channel, looping, mixing and fade-in and fade-out, etc.

License

This article has no explicit license attached to it but may contain in usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors may to use can be found here

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.