CSound类源文件

#ifndef __SoundH__
#define __SoundH__

#include <dsound.h>
#include "SDKwavefile.h"
#include <vector>
using namespace std;

struct SSound
{
 IDirectSoundBuffer8* pSoundBuffer;
 bool bLooping;
 SSound() { pSoundBuffer = 0; bLooping = false; }
};

class CSound
{
private:
 IDirectSound8* m_pSound;
 vector<SSound> m_Sound;
public:
 void Init(HWND hWnd);
 void CreateSound(WCHAR* wsFileName);
 void Play(int nIndex);
 void Pause(int nIndex);
 void ChangeState(int nIndex);
 void Start(int nIndex);
};
void CSound::Init(HWND hWnd)
{
 DirectSoundCreate8(0, &m_pSound, 0);
 m_pSound->SetCooperativeLevel(hWnd, DSSCL_PRIORITY); 
}
void CSound::CreateSound(WCHAR* wsFileName)
{
 SSound Sound, Sound0;
 CWaveFile WaveFile;
 ZeroMemory(&WaveFile, sizeof(CWaveFile));
 if(WaveFile.Open(wsFileName, 0, WAVEFILE_READ) != S_OK) { m_Sound.push_back(Sound0); return; }
 DSBUFFERDESC dsbd;
 ZeroMemory(&dsbd, sizeof(DSBUFFERDESC));
 dsbd.dwSize = sizeof(DSBUFFERDESC);
 dsbd.dwFlags = DSBCAPS_CTRLPAN |   DSBCAPS_CTRLVOLUME | DSBCAPS_GLOBALFOCUS | DSBCAPS_CTRLPOSITIONNOTIFY;
 dsbd.dwBufferBytes = WaveFile.GetSize();
 dsbd.lpwfxFormat = WaveFile.m_pwfx;
 IDirectSoundBuffer* pBuffer = 0;
 if(m_pSound->CreateSoundBuffer(&dsbd, &pBuffer, 0) != S_OK) { m_Sound.push_back(Sound0); return; }
 if(pBuffer->QueryInterface(IID_IDirectSoundBuffer8, (void**)&Sound.pSoundBuffer) != S_OK) { m_Sound.push_back(Sound0); return; }
 pBuffer->Release();
 void* pvoid = 0;
 DWORD nLen = 0;
 DWORD dwWrite = 0;
 if(Sound.pSoundBuffer->Lock(0, 0, &pvoid, &nLen, 0, 0, DSBLOCK_ENTIREBUFFER) != S_OK) { m_Sound.push_back(Sound0); return; }
 if(WaveFile.Read((BYTE*)pvoid, nLen, &dwWrite) != S_OK) { m_Sound.push_back(Sound0); return; }
 if(Sound.pSoundBuffer->Unlock(pvoid, nLen, 0, 0)) { { m_Sound.push_back(Sound0); return; } }
 m_Sound.push_back(Sound);
}
void CSound::Play(int nIndex)
{
 if(nIndex >= m_Sound.size() || m_Sound[nIndex].pSoundBuffer == NULL) return;
 m_Sound[nIndex].pSoundBuffer->Play(0, 0, DSBPLAY_LOOPING);
 m_Sound[nIndex].bLooping = true;
}
void CSound::Pause(int nIndex)
{
 if(nIndex >= m_Sound.size() || m_Sound[nIndex].pSoundBuffer == NULL) return;
 m_Sound[nIndex].pSoundBuffer->Stop();
 m_Sound[nIndex].bLooping = false;
}
void CSound::ChangeState(int nIndex)
{
 if(nIndex >= m_Sound.size() || m_Sound[nIndex].pSoundBuffer == NULL) return;
 if(m_Sound[nIndex].bLooping) Pause(nIndex);
 else Play(nIndex);
}
void CSound::Start(int nIndex)
{
 if(nIndex >= m_Sound.size() || m_Sound[nIndex].pSoundBuffer == NULL) return;
 m_Sound[nIndex].pSoundBuffer->SetCurrentPosition(0);
 m_Sound[nIndex].pSoundBuffer->Play(0, 0, DSBPLAY_LOOPING);
 m_Sound[nIndex].bLooping = true;
}

#endif

//Init初始化
//CreateSound加载声音资源,没加载成功的仍进行管理,这样就可以根据加载的顺序知道对应声音文件的下标
//Play继续播放
//Pause暂停播放
//ChangeState在继续播放与暂停播放间改变状态
//Start开始播放

//由于使用了DXUT,所以需要把DXUT的相关文件都添加进项目

//可能需要的lib文件包括dsound.lib;version.lib;imm32.lib;comctl32.lib;d3dx10d.lib;d3dx9d.lib;dxerr.lib;dxguid.lib;winmm.lib;d3d9.lib;d3dx9.lib;

posted on 2012-04-23 17:53  紫澜  阅读(966)  评论(0编辑  收藏  举报