Click here to Skip to main content
15,396,498 members
Articles / Desktop Programming / MFC
Posted 21 Mar 2001


62 bookmarked

A wrapper class for the DirectSound API

Rate me:
Please Sign up or sign in to vote.
4.65/5 (21 votes)
21 Mar 2001
This article focuses on giving an example of using the DirectX API in PC game software development by using a DirectSound wrapper class
<!-- Download Links -->


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 various 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 type DirectSoundBuffer), 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 calling DirectSoundCreate. 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, call CoInitialize/ CoInitializeEx, and call CoUninitialize at the end of your application.

// 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)
        m_pIDS = NULL;

Primary Buffer

The primary buffer is a DirectSoundBuffer 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 is a WAVEFORMATEX 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 is a DirectSoundBuffer object and is created by the DirectSound object.

In my library, I use a wrapper class of DirectSoundBuffer for the second buffer. This wrapper class has the DirectSoundBuffer object, and the methods to set sound performances, such as adjustment of volume, frequency and channel.

A special feature of the DirectSoundBuffer 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 calls, a mutex object is used in the DirectSoundBuffer wrapper class to synchronize the function calls.

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.


This article has no explicit license attached to it, but may contain 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 might use can be found here.


About the Author

No Biography provided

Comments and Discussions

GeneralMy vote of 1 Pin
Member 4024921-Sep-09 12:34
MemberMember 4024921-Sep-09 12:34 
GeneralQuestion Pin
Kuryn23-Jul-07 10:54
MemberKuryn23-Jul-07 10:54 
Generaldata Capturing from FM Tuner card Pin
amit_007_bond26-May-07 1:02
Memberamit_007_bond26-May-07 1:02 
QuestionDirectSoundFullDuplexCreate8 return E_INVALIDARG???? [modified] Pin
~Jabeen~24-May-07 19:42
Member~Jabeen~24-May-07 19:42 
GeneralTo get PCM data Pin
yasin1166-May-07 23:39
Memberyasin1166-May-07 23:39 
GeneralWell Done! Pin
mmress21-Sep-06 19:04
Membermmress21-Sep-06 19:04 
GeneralMore sound applications playing at same time Pin
8ki1-Dec-05 3:31
Member8ki1-Dec-05 3:31 
GeneralWAV file mixing Pin
CalicoSkies16-Feb-05 16:05
MemberCalicoSkies16-Feb-05 16:05 
Generaldo not use static buffer Pin
superliu27-Nov-04 16:25
Membersuperliu27-Nov-04 16:25 
GeneralGreat Job Pin
Anonymous20-Jan-04 2:53
MemberAnonymous20-Jan-04 2:53 
Generalerror: cannot open file &quot;nafxcwd.lib&quot; Pin
erik1876-Jan-04 18:02
Membererik1876-Jan-04 18:02 
GeneralRe: error: cannot open file &quot;nafxcwd.lib&quot; Pin
Anonymous5-Oct-04 20:48
MemberAnonymous5-Oct-04 20:48 
GeneralErrors Pin
Snillet2k28-Aug-03 8:20
MemberSnillet2k28-Aug-03 8:20 
GeneralRe: Errors Pin
swarecki2-Mar-04 9:37
Memberswarecki2-Mar-04 9:37 
GeneralRe: Errors Pin
Member 150178018-Nov-04 10:34
MemberMember 150178018-Nov-04 10:34 
QuestionHow to capture all outgoing pcm data Pin
Anonymous24-Aug-03 19:43
MemberAnonymous24-Aug-03 19:43 
QuestionHow to play a vedio file in DesktopWindow and under the Icon which on the DesktopWindow Pin
paperfly12-Jun-03 19:59
Memberpaperfly12-Jun-03 19:59 
Generala problem about DirectSound's AEC effect( Pin
gongyong13-Mar-03 1:11
Membergongyong13-Mar-03 1:11 
GeneralRe: a problem about DirectSound's AEC effect( Pin
pfluger20-Jul-03 3:00
Memberpfluger20-Jul-03 3:00 
GeneralRe: a problem about DirectSound's AEC effect( Pin
nissim zur8-Nov-03 9:53
Membernissim zur8-Nov-03 9:53 
GeneralRe: a problem about DirectSound's AEC effect( Pin
pfluger9-Nov-03 9:15
Memberpfluger9-Nov-03 9:15 
GeneralRe: a problem about DirectSound's AEC effect( Pin
Member 94017014-Sep-04 23:29
MemberMember 94017014-Sep-04 23:29 
GeneralRe: a problem about DirectSound's AEC effect( Pin
Provat113-Jun-06 0:26
MemberProvat113-Jun-06 0:26 
GeneralRe: a problem about DirectSound's AEC effect( Pin
Hung Nguyen Viet12-May-06 17:21
MemberHung Nguyen Viet12-May-06 17:21 
AnswerRe: a problem about DirectSound's AEC effect( Pin
Chris P.28-Mar-07 4:49
MemberChris P.28-Mar-07 4:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.