Sound.cpp
上传用户:cjwanglu
上传日期:2013-07-10
资源大小:4744k
文件大小:6k
源码类别:

游戏

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "Sound.h"
  3. CSound::CSound(HWND hWnd)
  4. {
  5. /*volume=-1000;
  6.         pan=0;
  7.         result = DirectSoundCreate( NULL, &pDS, NULL ); 
  8. if(result != DS_OK) MessageBox(hWnd,"建立 DirectSound 对象失败!","提示",MB_OK);
  9. result = pDS->SetCooperativeLevel( hWnd, DSSCL_PRIORITY );
  10. if(result != DS_OK) MessageBox(hWnd,"设置协调层级失败!","提示",MB_OK);
  11. memset( &dsdesc,0, sizeof(dsdesc) );       
  12. dsdesc.dwSize        = sizeof(dsdesc);       
  13. dsdesc.dwFlags       = DSBCAPS_PRIMARYBUFFER;
  14. dsdesc.dwBufferBytes = 0;
  15. dsdesc.lpwfxFormat   = NULL;
  16. result = pDS->CreateSoundBuffer( &dsdesc, &pPBuf, NULL );
  17. if(result != DS_OK) MessageBox(hWnd,"建立主缓冲区失败!","提示",MB_OK);
  18. memset( &pwfmt,0, sizeof(pwfmt) );
  19. pwfmt.wFormatTag      = 0 ;
  20. pwfmt.nChannels       = 2;              
  21. pwfmt.nSamplesPerSec  = 44100;       
  22. pwfmt.wBitsPerSample  = 16;           
  23. pwfmt.nBlockAlign     = pwfmt.wBitsPerSample / 8 * pwfmt.nChannels;
  24. pwfmt.nAvgBytesPerSec = pwfmt.nSamplesPerSec * pwfmt.nBlockAlign;
  25. result = pPBuf->SetFormat(&pwfmt);     
  26. if(result != DS_OK) MessageBox(hWnd,"设置播放格式失败!","提示",MB_OK);
  27. hmmio = mmioOpen(pStr, NULL, MMIO_ALLOCBUF|MMIO_READ );
  28. if(hmmio == NULL) MessageBox(hWnd,"文件不存在!","提示",MB_OK);
  29. ckRiff.fccType = mmioFOURCC('W', 'A', 'V', 'E');
  30.         
  31. mmresult = mmioDescend(hmmio,&ckRiff,NULL,MMIO_FINDRIFF);
  32. if(mmresult != MMSYSERR_NOERROR) MessageBox(hWnd,"文件格式错误!","提示",MB_OK);
  33. ckInfo.ckid = mmioFOURCC('f','m','t',' '); 
  34. mmresult = mmioDescend(hmmio,&ckInfo,&ckRiff,MMIO_FINDCHUNK);
  35. if(mmresult != MMSYSERR_NOERROR) MessageBox(hWnd,"文件格式错误!","提示",MB_OK);
  36. if(mmioRead(hmmio,(HPSTR)&swfmt,sizeof(swfmt)) == -1) MessageBox(hWnd,"读取格式失败!","提示",MB_OK);
  37. mmresult = mmioAscend(hmmio,&ckInfo,0);   
  38. ckInfo.ckid = mmioFOURCC('d','a','t','a');  
  39. mmresult = mmioDescend(hmmio,&ckInfo,&ckRiff,MMIO_FINDCHUNK);
  40. //搜寻区块
  41. if(mmresult != MMSYSERR_NOERROR) MessageBox(hWnd,"文件格式错误!","提示",MB_OK);
  42. size = ckInfo.cksize;                 
  43. memset( &dsdesc,0,sizeof(dsdesc));    
  44. dsdesc.dwSize  = sizeof(dsdesc);    
  45. dsdesc.dwFlags =   DSBCAPS_STATIC |DSBCAPS_CTRLPAN |DSBCAPS_CTRLVOLUME| DSBCAPS_GLOBALFOCUS;
  46. dsdesc.dwBufferBytes = size;      
  47. dsdesc.lpwfxFormat   = &swfmt;      
  48. result = pDS->CreateSoundBuffer( &dsdesc, &pSBuf, NULL );
  49. if(result != DS_OK) MessageBox(hWnd,"建立次缓冲区失败!","提示",MB_OK);
  50. result = pSBuf->Lock(0,size,&pAudio,&bytesAudio,NULL,NULL,NULL);
  51. if(result != DS_OK) MessageBox(hWnd,"锁定缓冲区失败!","提示",MB_OK);
  52. mmresult = mmioRead(hmmio,(HPSTR)pAudio,bytesAudio);
  53. if(mmresult == -1) MessageBox(hWnd,"读取声音文件失败!","提示",MB_OK);
  54. result = pSBuf->Unlock(pAudio,bytesAudio,NULL,NULL);
  55. if(result != DS_OK) MessageBox(hWnd,"解除锁定缓冲区失败!","提示",MB_OK);
  56. mmioClose(hmmio,0);*/
  57.        
  58.     m_pDirectAudioPerformance = NULL;
  59. m_pDirectAudioLoader = NULL;
  60. m_pSegment = NULL;
  61. m_pGraph = NULL;
  62. m_pMediaControl = NULL;
  63. m_pMediaPosition = NULL;
  64. m_enumFormat = Unknown;
  65. CoInitialize(NULL);
  66. //Create the DirectAudio performance object
  67. CoCreateInstance(CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC, 
  68. IID_IDirectMusicPerformance8, (void**) &m_pDirectAudioPerformance);
  69. //Create the DirectAudio loader object
  70. CoCreateInstance(CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC, 
  71. IID_IDirectMusicLoader8, (void**) &m_pDirectAudioLoader);
  72. //Initialise the performance object
  73. m_pDirectAudioPerformance->InitAudio(NULL, NULL, hWnd, DMUS_APATH_SHARED_STEREOPLUSREVERB,
  74.  64, DMUS_AUDIOF_ALL, NULL);
  75. GetCurrentDirectory(MAX_PATH, strSoundPath);
  76. MultiByteToWideChar(CP_ACP, 0, strSoundPath, -1, wstrSoundPath, MAX_PATH);
  77. m_pDirectAudioLoader->SetSearchDirectory(GUID_DirectMusicAllTypes, wstrSoundPath, FALSE);
  78.     CoCreateInstance(CLSID_FilterGraph, NULL,
  79.             CLSCTX_INPROC, IID_IGraphBuilder, (void**)&m_pGraph);
  80. m_pGraph->QueryInterface(IID_IMediaControl, (void**)&m_pMediaControl);
  81. m_pGraph->QueryInterface(IID_IMediaPosition, (void**)&m_pMediaPosition);
  82.     
  83.     
  84. }
  85. CSound::~CSound()
  86. {       /*pSBuf->Stop();
  87.         pPBuf->Release();
  88. pSBuf->Release();
  89. pDS->Release();*/
  90.      m_pMediaControl->Stop();
  91.  m_pDirectAudioPerformance->StopEx(m_pSegment, 0, 0);
  92.      m_pDirectAudioPerformance->Stop(NULL, NULL, 0, 0);
  93. m_pDirectAudioPerformance->CloseDown();
  94. SafeRRelease(m_pDirectAudioLoader);
  95. SafeRRelease(m_pDirectAudioPerformance);
  96. //m_pDirectAudioLoader->Release();
  97. //m_pDirectAudioPerformance->Release();
  98. CoUninitialize();
  99. }
  100. void CSound::Play(int t,CHAR * pStr)
  101. {
  102.            /*// pSBuf->Stop();
  103. pSBuf->SetCurrentPosition(0); 
  104. pSBuf->SetVolume(volume);  
  105. pSBuf->SetPan(pan);        
  106. pSBuf->Play(0,0,1);   */
  107. switch(t)
  108. {
  109. case 0:
  110.             m_enumFormat = WavMidi;
  111. //初始化WAV文件,将文件名转换为unicode
  112. MultiByteToWideChar(CP_ACP, 0, pStr, -1, wstrSoundPath, MAX_PATH);
  113. //加载声音
  114. m_pDirectAudioLoader->LoadObjectFromFile(CLSID_DirectMusicSegment, IID_IDirectMusicSegment8,
  115.  wstrSoundPath, (void**) &m_pSegment);
  116. m_pSegment->Download(m_pDirectAudioPerformance);
  117. m_pSegment->SetRepeats(-1);  //这里可以设置循环次数,-1为循环播放
  118. m_pDirectAudioPerformance->PlaySegmentEx(m_pSegment, NULL, NULL, 0, 0, NULL, NULL, NULL);
  119. break;
  120. case 1:
  121.     m_enumFormat = MP3;
  122. //初始化MP3文件,得到MP3文件所在的目录
  123. GetCurrentDirectory(MAX_PATH, strSoundPath);
  124. strcat(strSoundPath, pStr);//\Love.mp3
  125. //将文件名转换为unicode
  126. MultiByteToWideChar(CP_ACP, 0, strSoundPath, -1, wstrSoundPath, MAX_PATH);
  127. m_pGraph->RenderFile(wstrSoundPath, NULL);
  128. m_pMediaPosition->put_CurrentPosition(0);
  129. m_pMediaControl->Run();
  130. break;
  131. }
  132. }
  133. void CSound::PlaySound(CHAR * pStr)
  134. {
  135.             m_enumFormat = WavMidi;
  136. //初始化WAV文件,将文件名转换为unicode
  137. MultiByteToWideChar(CP_ACP, 0, pStr, -1, wstrSoundPath, MAX_PATH);
  138. //加载声音
  139. m_pDirectAudioLoader->LoadObjectFromFile(CLSID_DirectMusicSegment, IID_IDirectMusicSegment8,
  140.  wstrSoundPath, (void**) &m_pSegment);
  141. m_pSegment->Download(m_pDirectAudioPerformance);
  142. m_pSegment->SetRepeats(0);  //这里可以设置循环次数,-1为循环播放
  143. m_pDirectAudioPerformance->PlaySegmentEx(m_pSegment, NULL, NULL, 0, 0, NULL, NULL, NULL);
  144. }