www.pudn.com > 3DRPG.rar > Sound.cpp


#include "stdafx.h" 
#include "Sound.h" 
 
CSound::CSound(HWND hWnd) 
{ 
/*volume=-1000; 
        pan=0; 
        result = DirectSoundCreate( NULL, &pDS, NULL );  
		if(result != DS_OK)	MessageBox(hWnd,"建立 DirectSound 对象失败!","提示",MB_OK); 
		result = pDS->SetCooperativeLevel( hWnd, DSSCL_PRIORITY ); 
		if(result != DS_OK)	MessageBox(hWnd,"设置协调层级失败!","提示",MB_OK); 
		memset( &dsdesc,0, sizeof(dsdesc) );        
		dsdesc.dwSize        = sizeof(dsdesc);        
		dsdesc.dwFlags       = DSBCAPS_PRIMARYBUFFER; 
		dsdesc.dwBufferBytes = 0; 
		dsdesc.lpwfxFormat   = NULL; 
		result = pDS->CreateSoundBuffer( &dsdesc, &pPBuf, NULL ); 
		if(result != DS_OK)	MessageBox(hWnd,"建立主缓冲区失败!","提示",MB_OK); 
		memset( &pwfmt,0, sizeof(pwfmt) ); 
		pwfmt.wFormatTag      = 0 ; 
		pwfmt.nChannels       = 2;               
		pwfmt.nSamplesPerSec  = 44100;        
		pwfmt.wBitsPerSample  = 16;            
		pwfmt.nBlockAlign     = pwfmt.wBitsPerSample / 8 * pwfmt.nChannels; 
		pwfmt.nAvgBytesPerSec = pwfmt.nSamplesPerSec * pwfmt.nBlockAlign; 
		 
		 
		 
		result = pPBuf->SetFormat(&pwfmt);      
		if(result != DS_OK)	MessageBox(hWnd,"设置播放格式失败!","提示",MB_OK); 
		 
		hmmio = mmioOpen(pStr, NULL, MMIO_ALLOCBUF|MMIO_READ ); 
 
		if(hmmio == NULL)	MessageBox(hWnd,"文件不存在!","提示",MB_OK); 
		ckRiff.fccType = mmioFOURCC('W', 'A', 'V', 'E'); 
         
		mmresult = mmioDescend(hmmio,&ckRiff,NULL,MMIO_FINDRIFF); 
 
		if(mmresult != MMSYSERR_NOERROR)	MessageBox(hWnd,"文件格式错误!","提示",MB_OK); 
		ckInfo.ckid = mmioFOURCC('f','m','t',' ');  
		mmresult = mmioDescend(hmmio,&ckInfo,&ckRiff,MMIO_FINDCHUNK); 
 
		if(mmresult != MMSYSERR_NOERROR)	MessageBox(hWnd,"文件格式错误!","提示",MB_OK); 
		if(mmioRead(hmmio,(HPSTR)&swfmt,sizeof(swfmt)) == -1)	MessageBox(hWnd,"读取格式失败!","提示",MB_OK); 
		mmresult = mmioAscend(hmmio,&ckInfo,0);    
		ckInfo.ckid = mmioFOURCC('d','a','t','a');   
		mmresult = mmioDescend(hmmio,&ckInfo,&ckRiff,MMIO_FINDCHUNK); 
		//搜寻区块 
		if(mmresult != MMSYSERR_NOERROR)	MessageBox(hWnd,"文件格式错误!","提示",MB_OK); 
		size = ckInfo.cksize;                  
		memset( &dsdesc,0,sizeof(dsdesc));     
		dsdesc.dwSize  = sizeof(dsdesc);     
		dsdesc.dwFlags = 	 DSBCAPS_STATIC |DSBCAPS_CTRLPAN |DSBCAPS_CTRLVOLUME| DSBCAPS_GLOBALFOCUS; 
		dsdesc.dwBufferBytes = size;       
		dsdesc.lpwfxFormat   = &swfmt;       
		 
		 
		result = pDS->CreateSoundBuffer( &dsdesc, &pSBuf, NULL ); 
		if(result != DS_OK)	MessageBox(hWnd,"建立次缓冲区失败!","提示",MB_OK); 
 
		result = pSBuf->Lock(0,size,&pAudio,&bytesAudio,NULL,NULL,NULL); 
 
		if(result != DS_OK)	MessageBox(hWnd,"锁定缓冲区失败!","提示",MB_OK); 
		mmresult = mmioRead(hmmio,(HPSTR)pAudio,bytesAudio); 
 
		if(mmresult == -1)	MessageBox(hWnd,"读取声音文件失败!","提示",MB_OK); 
		result = pSBuf->Unlock(pAudio,bytesAudio,NULL,NULL); 
 
		if(result != DS_OK)	MessageBox(hWnd,"解除锁定缓冲区失败!","提示",MB_OK); 
		mmioClose(hmmio,0);*/ 
        
	 
	 
	    m_pDirectAudioPerformance = NULL; 
		m_pDirectAudioLoader = NULL; 
		m_pSegment = NULL; 
		m_pGraph = NULL; 
		m_pMediaControl = NULL; 
		m_pMediaPosition = NULL; 
		m_enumFormat = Unknown; 
 
		CoInitialize(NULL); 
		//Create the DirectAudio performance object 
		CoCreateInstance(CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC,  
						IID_IDirectMusicPerformance8, (void**) &m_pDirectAudioPerformance); 
		 
		 
		//Create the DirectAudio loader object 
		CoCreateInstance(CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC,  
						IID_IDirectMusicLoader8, (void**) &m_pDirectAudioLoader); 
	 
		 
		//Initialise the performance object 
		m_pDirectAudioPerformance->InitAudio(NULL, NULL, hWnd, DMUS_APATH_SHARED_STEREOPLUSREVERB, 
										 64, DMUS_AUDIOF_ALL, NULL); 
	 
		GetCurrentDirectory(MAX_PATH, strSoundPath); 
		MultiByteToWideChar(CP_ACP, 0, strSoundPath, -1, wstrSoundPath, MAX_PATH); 
 
		m_pDirectAudioLoader->SetSearchDirectory(GUID_DirectMusicAllTypes, wstrSoundPath, FALSE); 
	 
		    CoCreateInstance(CLSID_FilterGraph, NULL, 
            CLSCTX_INPROC, IID_IGraphBuilder, (void**)&m_pGraph); 
			m_pGraph->QueryInterface(IID_IMediaControl, (void**)&m_pMediaControl); 
			m_pGraph->QueryInterface(IID_IMediaPosition, (void**)&m_pMediaPosition); 
     
	     
} 
 
CSound::~CSound() 
{       /*pSBuf->Stop(); 
        pPBuf->Release(); 
		pSBuf->Release(); 
		pDS->Release();*/ 
	     m_pMediaControl->Stop(); 
		 m_pDirectAudioPerformance->StopEx(m_pSegment, 0, 0); 
	     m_pDirectAudioPerformance->Stop(NULL, NULL, 0, 0); 
		m_pDirectAudioPerformance->CloseDown(); 
		SafeRRelease(m_pDirectAudioLoader); 
		SafeRRelease(m_pDirectAudioPerformance); 
		//m_pDirectAudioLoader->Release(); 
		//m_pDirectAudioPerformance->Release(); 
		CoUninitialize(); 
} 
void CSound::Play(int t,CHAR * pStr) 
{ 
           /*// pSBuf->Stop(); 
			pSBuf->SetCurrentPosition(0);  
			pSBuf->SetVolume(volume);   
			pSBuf->SetPan(pan);         
			pSBuf->Play(0,0,1);   */ 
switch(t) 
	{ 
	case 0: 
            m_enumFormat = WavMidi; 
			//初始化WAV文件,将文件名转换为unicode 
			MultiByteToWideChar(CP_ACP, 0, pStr, -1, wstrSoundPath, MAX_PATH); 
			//加载声音 
			m_pDirectAudioLoader->LoadObjectFromFile(CLSID_DirectMusicSegment, IID_IDirectMusicSegment8, 
													 wstrSoundPath, (void**) &m_pSegment); 
			m_pSegment->Download(m_pDirectAudioPerformance); 
			m_pSegment->SetRepeats(-1);  //这里可以设置循环次数,-1为循环播放 
			m_pDirectAudioPerformance->PlaySegmentEx(m_pSegment, NULL, NULL, 0, 0, NULL, NULL, NULL); 
			break; 
	case 1: 
		    m_enumFormat = MP3; 
			//初始化MP3文件,得到MP3文件所在的目录 
			GetCurrentDirectory(MAX_PATH, strSoundPath); 
			strcat(strSoundPath, pStr);//\\Love.mp3 
			//将文件名转换为unicode 
			MultiByteToWideChar(CP_ACP, 0, strSoundPath, -1, wstrSoundPath, MAX_PATH); 
			m_pGraph->RenderFile(wstrSoundPath, NULL); 
			m_pMediaPosition->put_CurrentPosition(0); 
			m_pMediaControl->Run(); 
			break; 
	} 
} 
void CSound::PlaySound(CHAR * pStr) 
{ 
            m_enumFormat = WavMidi; 
			//初始化WAV文件,将文件名转换为unicode 
			MultiByteToWideChar(CP_ACP, 0, pStr, -1, wstrSoundPath, MAX_PATH); 
			//加载声音 
			m_pDirectAudioLoader->LoadObjectFromFile(CLSID_DirectMusicSegment, IID_IDirectMusicSegment8, 
													 wstrSoundPath, (void**) &m_pSegment); 
			m_pSegment->Download(m_pDirectAudioPerformance); 
			 
			m_pSegment->SetRepeats(0);  //这里可以设置循环次数,-1为循环播放 
			m_pDirectAudioPerformance->PlaySegmentEx(m_pSegment, NULL, NULL, 0, 0, NULL, NULL, NULL); 
}