Click here to Skip to main content
12,951,732 members (79,963 online)
Click here to Skip to main content
Add your own
alternative version


37 bookmarked
Posted 25 Oct 2005

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

You may also be interested in...

Comments and Discussions

GeneralRe: Why does my DirectSound application stop playing when the application window loses focus. Pin
parxal13-May-08 16:09
memberparxal13-May-08 16:09 
GeneralRe: Why does my DirectSound application stop playing when the application window loses focus. Pin
Scot McDermid8-May-09 8:36
memberScot McDermid8-May-09 8:36 
GeneralRe: Why does my DirectSound application stop playing when the application window loses focus. Pin
bingqitan25-Mar-09 19:21
memberbingqitan25-Mar-09 19:21 
QuestionIs it possible to play wave file using Resource? Pin
firstluv8111-Sep-06 20:54
memberfirstluv8111-Sep-06 20:54 
AnswerRe: Is it possible to play wave file using Resource? Pin
Arman Z. Sahakyan16-Sep-06 23:28
memberArman Z. Sahakyan16-Sep-06 23:28 
GeneralRe: Is it possible to play wave file using Resource? Pin
firstluv8117-Sep-06 6:56
memberfirstluv8117-Sep-06 6:56 
QuestionRecord L,R channels? Pin
guzeliang28-Aug-06 21:08
memberguzeliang28-Aug-06 21:08 
Questionconsole application [modified] Pin
laleh_sg12-Aug-06 0:55
memberlaleh_sg12-Aug-06 0:55 
GeneralVisual studio 2005 support Pin
dynz18-Jul-06 22:52
memberdynz18-Jul-06 22:52 
QuestionIs it possible to play multiple sounds at the same time? Pin
firstluv819-Jul-06 21:48
memberfirstluv819-Jul-06 21:48 
AnswerRe: Is it possible to play multiple sounds at the same time? Pin
Arman Z. Sahakyan10-Jul-06 2:19
memberArman Z. Sahakyan10-Jul-06 2:19 
GeneralRe: Is it possible to play multiple sounds at the same time? Pin
firstluv8110-Jul-06 20:58
memberfirstluv8110-Jul-06 20:58 
GeneralRe: Is it possible to play multiple sounds at the same time? Pin
Arman Z. Sahakyan11-Jul-06 1:03
memberArman Z. Sahakyan11-Jul-06 1:03 
GeneralRe: Is it possible to play multiple sounds at the same time? Pin
firstluv8111-Jul-06 18:28
memberfirstluv8111-Jul-06 18:28 
GeneralCWave source code Pin
misugi31-Jan-06 6:02
membermisugi31-Jan-06 6:02 
GeneralRe: CWave source code Pin
Arman Z. Sahakyan3-Feb-06 0:19
memberArman Z. Sahakyan3-Feb-06 0:19 
GeneralRe: CWave source code Pin
Arman Z. Sahakyan3-Feb-06 0:28
memberArman Z. Sahakyan3-Feb-06 0:28 
GeneralPlaying Unformatted data... Pin
rajeev8220-Jan-06 3:26
memberrajeev8220-Jan-06 3:26 
GeneralConflict when playing two directsound applications Pin
Kedzo7-Dec-05 0:54
memberKedzo7-Dec-05 0:54 

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.

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