Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "DirectSound.h"
- #ifndef DSBLOCK_ENTIREBUFFER
- #define DSBLOCK_ENTIREBUFFER 0x00000002
- #endif
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- BOOL CDirectSound::CreateSoundBuffer(WAVEFORMATEX*pcmwf){DSBUFFERDESC dsbdesc;
- memset(&dsbdesc,0,sizeof(DSBUFFERDESC));dsbdesc.dwSize=sizeof(DSBUFFERDESC);
- dsbdesc.dwFlags=DSBCAPS_STATIC;dsbdesc.dwBufferBytes=m_dwTheSound;
- dsbdesc.lpwfxFormat=pcmwf;HRESULT hRes;if(DS_OK!=(hRes=m_lpDirectSound->
- CreateSoundBuffer(&dsbdesc,&m_pDsb,0))){DSError(hRes);m_pDsb=0;return FALSE;}
- return TRUE;}BOOL CDirectSound::SetSoundData(void*pSoundData,DWORD dwSoundSize)
- {LPVOID lpvPtr1;DWORD dwBytes1;HRESULT hr=m_pDsb->Lock(0,0,&lpvPtr1,&dwBytes1,0
- ,0,DSBLOCK_ENTIREBUFFER);if(DSERR_BUFFERLOST==hr){m_pDsb->Restore();hr=m_pDsb->
- Lock(0,0,&lpvPtr1,&dwBytes1,0,0,DSBLOCK_ENTIREBUFFER);}if(DS_OK==hr){::
- CopyMemory(lpvPtr1,pSoundData,dwBytes1);hr=m_pDsb->Unlock(lpvPtr1,dwBytes1,0,0)
- ;if(DS_OK==hr)return TRUE;}return FALSE;}void CDirectSound::Play(DWORD
- dwStartPosition,BOOL bLoop){if(!IsValid()||!IsEnabled())return;if(
- dwStartPosition>m_dwTheSound)dwStartPosition=m_dwTheSound;m_pDsb->
- SetCurrentPosition(dwStartPosition);if(DSERR_BUFFERLOST==m_pDsb->Play(0,0,bLoop
- ?DSBPLAY_LOOPING:0)){SetSoundData(m_pTheSound,m_dwTheSound);m_pDsb->Play(0,0,
- bLoop?DSBPLAY_LOOPING:0);}}void CDirectSound::Stop(){if(IsValid())m_pDsb->Stop(
- );}void CDirectSound::Pause(){Stop();}void CDirectSound::Continue(){if(IsValid(
- )){DWORD dwPlayCursor,dwWriteCursor;m_pDsb->GetCurrentPosition(&dwPlayCursor,&
- dwWriteCursor);Play(dwPlayCursor);}}BOOL CDirectSound::IsValid()const{return(
- m_lpDirectSound&&m_pDsb&&m_pTheSound&&m_dwTheSound)?TRUE:FALSE;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement