Playing Wave Files Directly From The Resource via DirectSound

Public Interface

Introduction (back to top)

This class is a "spin off" of the CGTetris application. The CDirectSound class makes it easy to play a WAVE file directly from the resource (without copying it into a temporary file) via Microsoft's DirectX component DirectSound. The class is designed to play small sounds, that can be placed completely into memory. No streaming is realized in this implementation. One can use the class to play WAVE files, too. In this case you have to load the entire file into memory and to pass the pointer to the beginning of the buffer to the Create() method. To use this class you need at least DirectX version 3.

Public Interface (back to top)

// create the sound object. You cannot use a MIDI object until
// you "Create()" one!
BOOL Create(LPVOID pSoundData, DWORD dwSize, CWnd * pParent = 0);
BOOL Create(LPCTSTR pszResID, CWnd * pParent = 0);
BOOL Create(UINT uResID, CWnd * pParent = 0);

// Play the sound file. Normally the playback will stop after
// the sound played off.
BOOL Play(DWORD dwStartPosition = 0, BOOL bInfinite = FALSE);

BOOL Stop();
BOOL Pause();
BOOL Continue();

// Enable/disable the sound object. If you call Play() on a
// disabled sound object, nothing will happen. This makes
// it somewhat easier to globally switch off sounds in an
// application.
BOOL EnableSound(BOOL bEnable = TRUE);
BOOL IsEnabled() const;

Usage (back to top)

The source consists of 2 files:

Add a WAVE resource into your application (from the Menu Insert->Resource->import->*.wav).
"Create()" your CDirectSound (derived) object giving it the resource-id of your WAVE resource. Now you can call Play(), Pause(), Stop() etc.

Download(back to top)

Download demo project - 5 KB

Download source - 71 KB


  • how to do it in c #?

    Posted by Carlos on 02/14/2013 09:40pm

    I like this tutorial, but as I can do it in C #, you could make a tutorial on how to do it in vb net or c #, I use some c + + Thanks

  • Good job!

    Posted by Legacy on 05/21/2003 12:00am

    Originally posted by: Rene

    Thanks for this great class!

  • How to record voice to *.WAV file from modem

    Posted by Legacy on 09/18/2002 12:00am

    Originally posted by: Duy

    Anyone can show me How to record voice to *.WAV file from modem

  • How do I know if a file is playing? "isPlaying()" ?

    Posted by Legacy on 07/07/2002 12:00am

    Originally posted by: Usul

    How can I detect if a wav file is still being played or not?
    There should be something like a "isPlaying()" function, I think.

    But anyway, the class really helps a lot, so thanks.

  • Bug?

    Posted by Legacy on 05/08/2002 12:00am

    Originally posted by: Michael Olsen

    when the application loses the focus, the sounds stops playing.
    Are there any posible way to avoid this???

    • Here in fixed code.

      Posted by UglyShark on 09/08/2004 01:29am

      BOOL CDirectSound::CreateSoundBuffer(WAVEFORMATEX * pcmwf)
      	DSBUFFERDESC dsbdesc;
      	// Set up DSBUFFERDESC structure.
      	memset(&dsbdesc, 0, sizeof(DSBUFFERDESC)); // Zero it out.
      	dsbdesc.dwSize = sizeof(DSBUFFERDESC);
      	// Need no controls (pan, volume, frequency).
      	dsbdesc.dwFlags = DSBCAPS_STATIC|DSBCAPS_GLOBALFOCUS;	// assumes that the sound is played often (!!FIXED!!)
      	dsbdesc.dwBufferBytes = m_dwTheSound;
      	dsbdesc.lpwfxFormat = pcmwf;    // Create buffer.
      	HRESULT hRes;
      	if( DS_OK != (hRes = m_lpDirectSound->CreateSoundBuffer(&dsbdesc, &m_pDsb, 0)) ) {
      		// Failed.
      		m_pDsb = 0;
      		return FALSE;
      	return TRUE;

  • The demo project crashes

    Posted by Legacy on 02/08/2002 12:00am

    Originally posted by: Biswajit Biswas

    I built the demo project using MS visual studio 6. It crashes at following place in 'sound.cpp' file.

    CSoundDlg dlg;
    m_pMainWnd = &dlg
    ---> int nResponse = dlg.DoModal();
    if (nResponse == IDOK)
    else if (nResponse == IDCANCEL)

    It crashes while creating the modal dialog.
    Why??? any reason?

    Also,how can I play wave file without using directX in windows machine ?


  • External Wave

    Posted by Legacy on 12/19/2000 12:00am

    Originally posted by: Kenny

    How can I use this code to call an external wave file?  I'm new to Visual c++ and MFC is greek to me.

    any reply/ email would be appriciated
    -kenny smith

  • External wav files?

    Posted by Legacy on 04/19/2000 12:00am

    Originally posted by: Ricky

    I want to play external wav files so that it plays once from start to finish, then repeats from a point to finish infinatly. I coulnd't find a simple method of sndPlaySound and then I found this, but It doesn't seem to work with external files. Is there a simple way to make this CDirectSound class work with external wav files?

  • Modified the code for optimal output 16 bit 441 khz!

    Posted by Legacy on 03/20/2000 12:00am

    Originally posted by: Jarek

    The temporary idirectsoundbuffer only suply you with a primarybuffer on 8 bit and 22khz :-(

    So with a litle modification in the code you will be able to get 16 bit 44,1 khz!

    In the DirectSound.h add this under private:

    // pointer to a Primary soundbuffer
    BOOL CreatePrimeryBuffer();
    //the function

    //in the DirectSound.cpp Create handle
    //add this to the SetCooperativeLevel DSSCL_PRIORITY
    //like this
    m_lpDirectSound->SetCooperativeLevel(pWnd->GetSafeHwnd(), DSSCL_NORMAL|DSSCL_PRIORITY );

    //replace the last lines in the Create function!

    if( ! CreatePrimeryBuffer() ||
    ! GetWaveData(pSoundData, pcmwf, m_pTheSound, m_dwTheSound) ||
    ! CreateSoundBuffer(pcmwf) ||
    ! SetSoundData(m_pTheSound, m_dwTheSound))
    return FALSE;

    //then add this handle

    BOOL CDirectSound::CreatePrimeryBuffer()
    //this creates a �primerybuffer
    DSBUFFERDESC dsbdesc;
    ZeroMemory(&dsbdesc, sizeof(DSBUFFERDESC));
    dsbdesc.dwSize = sizeof(DSBUFFERDESC);
    dsbdesc.dwFlags = DSBCAPS_PRIMARYBUFFER;
    if FAILED(m_lpDirectSound->CreateSoundBuffer (&dsbdesc, &m_pDsbprim, NULL))
    return FALSE;
    //this will let os set the format on the buffer
    //16 bit 44,1 khz.

    memset(&wfx, 0, sizeof(WAVEFORMATEX));
    wfx.wFormatTag = WAVE_FORMAT_PCM;
    wfx.nChannels = 2;
    wfx.nSamplesPerSec = 44100;
    wfx.wBitsPerSample = 16;
    wfx.nBlockAlign = wfx.wBitsPerSample / 8 * wfx.nChannels;
    wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;


    return TRUE;

    //At last in the Deconstructorn add this
    //we have to clean up!


    This will hopfully get rid of the 8 bit 22 khz noice!


  • Distorsion!!!

    Posted by Legacy on 03/16/2000 12:00am

    Originally posted by: Jarek

    When I am adding my own file like a bas kick *low frecuency*
    I can hear som noice in the background like distorsion??

    The sample is a mono 16 bit 44,1 khz and and have good quality!

    is there a way to get rid of the noice??


  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

  • Power and Automation Equipment makers face constrained CAPEX, equipment interoperability issues, and process inefficiencies. However, they can take advantage of secure Internet of Things (IoT) intelligent devices and systems to make better business decisions around asset utilization and performance, while simultaneously optimizing equipment interoperability and services efficiency. Watch this brief video to see how you can unlock the full potential of your assets and value chain.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date