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 callCoUninitialize
At 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 isDirectSoundBuffer
Object, 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 isWAVEFORMATEX
Structure 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 isDirectSoundBuffer
Object and is created by the directsound object.
In my library, I use a Wrapper classDirectSoundBuffer
For the second buffer. This Wrapper class hasDirectSoundBuffer
Object, and the methods to set sound CES, such as adjustment of volume, frequency and channel.
A special feature ofDirectSoundBuffer
Wrapper 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 inDirectSoundBuffer
Wrapper 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