Playing Wave Files Directly From The Resource via DirectSound

Introduction
Public Interface
Usage
Download
Comments
 

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:
DirectSound.[h|cpp]

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



Comments

  • 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

    Reply
  • Good job!

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

    Originally posted by: Rene

    Thanks for this great class!

    Reply
  • 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
    Thanks

    Reply
  • 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.

    Reply
  • 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.
      		DSError(hRes);
      		m_pDsb = 0;
      		return FALSE;
      	}
      
      	return TRUE;
      }

      Reply
    Reply
  • 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 ?

    Biswajit


    Reply
  • 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

    Reply
  • 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?

    Reply
  • 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:

    LPDIRECTSOUNDBUFFER m_pDsbprim;
    // 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.

    WAVEFORMATEX wfx;
    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;

    m_pDsbprim->SetFormat(&wfx);

    return TRUE;
    }

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

    if(m_pDsbprim)
    m_pDsbprim->Release();

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

    /Jarek

    Reply
  • 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??

    /Jarek

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds