Playing Wave Files Directly From The Resource via DirectSound
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.

Comments
how to do it in c #?
Posted by Carlos on 02/14/2013 09:40pmI 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
ReplyGood job!
Posted by Legacy on 05/21/2003 12:00amOriginally posted by: Rene
Thanks for this great class!
ReplyHow to record voice to *.WAV file from modem
Posted by Legacy on 09/18/2002 12:00amOriginally posted by: Duy
Anyone can show me How to record voice to *.WAV file from modem
ReplyThanks
How do I know if a file is playing? "isPlaying()" ?
Posted by Legacy on 07/07/2002 12:00amOriginally 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:00amOriginally posted by: Michael Olsen
when the application loses the focus, the sounds stops playing.
Are there any posible way to avoid this???
-
ReplyHere in fixed code.
Posted by UglyShark on 09/08/2004 01:29amBOOL 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; }ReplyThe demo project crashes
Posted by Legacy on 02/08/2002 12:00amOriginally posted by: Biswajit Biswas
ReplyExternal Wave
Posted by Legacy on 12/19/2000 12:00amOriginally posted by: Kenny
ReplyExternal wav files?
Posted by Legacy on 04/19/2000 12:00amOriginally 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?
ReplyModified the code for optimal output 16 bit 441 khz!
Posted by Legacy on 03/20/2000 12:00amOriginally 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
ReplyDistorsion!!!
Posted by Legacy on 03/16/2000 12:00amOriginally 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
ReplyLoading, Please Wait ...