Click here to Skip to main content
11,415,390 members (85,214 online)
Click here to Skip to main content

DirectSound Wrapper Classes

, 6 Aug 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
An article on multimedia - playing Waves by using the DirectSound component.

Demo Application User Interafce


Microsoft DirectX is a heavy stone that cannot be broken without any tools. Each part of DirectX is worth separate attention by a developer. Looking deep inside of those components is too difficult. But to use them properly one must know what he is doing or wants to do...

But nowadays technologies are so developed that they free developers from having to cover much of the 'technical' aspects of their job and let them concentrate on the core issues they have. One of such helpers is the core concept of object-oriented languages, and C++ too, which of course is - the class.

Classes are the very basement on which object-oriented programming is built. The wiser you use them, the more you will sleep at nights. Not going into the depth of C++ notions, I want to introduce a few classes of mine to you that may help you with using MS DirectSound components in your applications. Particularly, I will speak about how to play .wav (wave) files by DirectSound.

What's New in This Update

The major new feature in this article is that CDSoundPlay now supports notification mechanism; it uses IDirectSoundNotify interface to handle that. CWave class's source is available in the source download. Also there are some corrections in the code from previous article.


First, let me say that .wav files may be played not only by DirectSound, but also by MMIO, MCI and so on. The problem is that those machines don't provide immediate attention to a user; the latency to start playing is much. This may be crucial in some situations, for example, in games, where the bomber-man wants his gun to sound (and shoot) as soon as possible. Here DirectSound may help.

DirectSound provides low-latency playback and immediate attention from users.

To minimize the job necessary for using DirectSound at a maximum, I've created four classes: one may say wrapper classes. Two of them are helper classes and are not supposed to be used directly, but you still can use them. Here are the classes:

  • CWave (helper)
  • CDSBuffer (helper)
  • CDSound
  • CDSoundPlay

CWave helps us with opening .wav files. This class is used by CDSBuffer. I have put CWave in a DLL to hide the unnecessary stuff from you (believe in me, there are lots of ugly things). Though the source code of CWave you can find in the download package of this article.

CDSound is the main entity in your system. It represents the DirectSound object. Within a given application there must be only one instance of this class. This class is used by CDSBuffer.

CDSBuffer is a low-level class that represents DirectSound buffers. This class is used by CDSoundPlay.

CDSoundPlay is a high-level class that represents the interface for playing, stopping, ... of Waves.

These classes use MFC and cannot live without it. For example, I've put CWave in a MFC extension DLL; it means that the client application also must be MFC based.

Using The Code

Now let's see how to use them. You can declare the objects of these classes in a view class, dialog class or main frame class; in other words, any window that has HWND (really I haven't seen any window without a HWND). I'll choose a dialog.


#include "DirectSound.h"
using namespace DirectSound;

class CMyDialog : public CDialog
    CDSound m_dsound; // only once
    CDsoundPlayer m_player; // any number
    // ...

Next construct the corresponding DirectSound object in OnInitDialog:

BOOL CMyDialog::OnInitDialog()
  // ...


  return FALSE;

Now you may load files. Assume you have a Load method to do it:

BOOL CMyDialog::Load(LPCTSTR lpszFileName)
    m_player.Destroy(); // guarantee multiple loads
    BOOL fOk = m_player.Create(&m_dsound, lpszFileName);
    return fOk;

Let's play and stop Wave files. Assume you have OnPlay and OnStop handlers to do it:

void CMyDialog::OnPlay()

void  CMyDialog::OnStop()

A couple of new functions has been added to CDSoundPlay which are for handling notifications; CDSounfPlay::SetNotifyFuction and CDSoundPlay::SetNotificationPositions. The former takes a parameter of type PNOTIFYFUNC which is a typedef for callback functions:


This means that a callback should take a LONG parameter and return a LONG value. This type was defined by me and you may change it for your convention if needed.

The second function looks like so:

BOOL SetNotificationPositions(DWORD dwOffsets[], SIZE_T nSize);

It takes an array of integer values showing positions to be considered, and an integer value showing how many positions are there in the first parameter.

For example, the demo application calls these functions to be notified when a wave starts and when it ends:

DWORD dwOffset[] = { 0, DSBPN_OFFSETSTOP };
m_player.SetNotificationPositions(dwOffset, 2);

CSounderDlg::NotifyCallback is a static function which has the type of PNOTIFYFUNC:

LONG CSounderDlg::NotifyCallback(LONG f) // static function
    // ...
    return 0;

That's all for playing Wave files. But CDSoundPlay has also other features; amongst them: looping, volume setting/getting, panorama setting/getting, frequency setting/getting. I've created a demo application that shows most of these features. It is a simple application and I'll not describe it thoroughly. Just see its interface and you'll understand it yourself.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Arman S.
Software Developer 13
United States United States
No Biography provided

Comments and Discussions

GeneralError unresolved external Pin
The3D12-Dec-09 13:53
memberThe3D12-Dec-09 13:53 
GeneralClicking when changing playback rate Pin
amnesty21-Oct-09 2:32
memberamnesty21-Oct-09 2:32 
GeneralRe: Clicking when changing playback rate Pin
Arman Z. Sahakyan1-Oct-09 7:28
memberArman Z. Sahakyan1-Oct-09 7:28 
Generalabout DSound.h Pin
jackywei7111232-Sep-09 18:27
memberjackywei7111232-Sep-09 18:27 
GeneralRe: about DSound.h Pin
Arman Z. Sahakyan29-Sep-09 11:08
memberArman Z. Sahakyan29-Sep-09 11:08 
GeneralRe: My vote of 2 Pin
Arman Z. Sahakyan29-Sep-09 11:11
memberArman Z. Sahakyan29-Sep-09 11:11 
GeneralWhy does my DirectSound application stop playing when the application window loses focus. Pin
cheesypeas13-Mar-07 7:47
membercheesypeas13-Mar-07 7:47 
GeneralRe: Why does my DirectSound application stop playing when the application window loses focus. Pin
Arman Z. Sahakyan14-Mar-07 0:46
memberArman Z. Sahakyan14-Mar-07 0:46 
GeneralRe: Why does my DirectSound application stop playing when the application window loses focus. Pin
cheesypeas14-Mar-07 2:24
membercheesypeas14-Mar-07 2:24 
Thanks Arman, that's a great help and confirms my suspicions. I'm having a rethink now. The sounds I am playing are probably going to be short voice messages so maybe playing them simultaneously isn't such a great idea after all (ever been in one of those meetings where everbody's talking at once?). Perhaps 'PlaySound' is the way to go.
Thanks again,
GeneralRe: Why does my DirectSound application stop playing when the application window loses focus. Pin
Arman Z. Sahakyan14-Mar-07 5:42
memberArman Z. Sahakyan14-Mar-07 5:42 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150427.4 | Last Updated 6 Aug 2006
Article Copyright 2005 by Arman S.
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid