AUDIOSYSTEM.CPP
Upload User: nthssl
Upload Date: 2022-04-05
Package Size: 25357k
Code Size: 8k
Category:

OpenCV

Development Platform:

Visual C++

  1. #include <dmusicc.h>               // DirectMusic includes
  2. #include <dmusici.h>
  3. #include <d3d8types.h>             // for D3DVECTOR
  4. #include <cguid.h>                 // for GUID_NULL
  5. #include "audiosystem.h"
  6. ////// The CAudio implementation
  7. void CAudio::Set3DBuffer(IDirectSound3DBuffer *dsBuff)
  8. {
  9. ds3DBuffer = dsBuff;
  10. }
  11. void CAudio::Set3DPos(float x, float y, float z)
  12. {
  13. ds3DBuffer->SetPosition(x, y, -z, DS3D_IMMEDIATE);
  14. }
  15. void CAudio::Set3DParams(float minDistance, float maxDistance)
  16. {
  17. DS3DBUFFER dsBufferParams;
  18.      // 设置最大最小距离
  19.      dsBufferParams.flMinDistance = minDistance;
  20.      dsBufferParams.flMaxDistance = maxDistance;
  21.      if (ds3DBuffer)
  22.           ds3DBuffer->SetAllParameters(&dsBufferParams, DS3D_IMMEDIATE);
  23. }
  24. CAudioSystem::CAudioSystem()
  25. {
  26. dmusic3DAudioPath = NULL;
  27. dmusicLoader = NULL;
  28. dmusicPerformance = NULL;
  29. ds3DListener = NULL;
  30. }
  31. CAudioSystem::~CAudioSystem()
  32. {
  33. if (dmusic3DAudioPath)
  34. dmusic3DAudioPath->Release();
  35. if (dmusicPerformance)
  36. dmusicPerformance->Release();
  37. if (dmusicLoader)
  38. dmusicLoader->Release();
  39. }
  40. // 初始化音频系统
  41. bool CAudioSystem::InitDirectXAudio(HWND hwnd)
  42. {
  43. char pathStr[MAX_PATH]; // 音频文件的路径
  44. WCHAR wcharStr[MAX_PATH];
  45. // 生成装入器对象
  46. if (FAILED(CoCreateInstance(CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC,
  47.    IID_IDirectMusicLoader8, (void**)&dmusicLoader)))
  48. {
  49. MessageBox(hwnd, "Unable to create the IDirectMusicLoader8 object!nPress OK to exit",
  50.  "ERROR!", MB_OK);
  51. return false;
  52. }
  53. // 生成操作对象
  54. if (FAILED(CoCreateInstance(CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC,
  55.    IID_IDirectMusicPerformance8, (void**)&dmusicPerformance)))
  56. {
  57. MessageBox(hwnd, "Unable to create the IDirectMusicPerformance8 object!nPress OK to exit",
  58.  "ERROR!", MB_OK);
  59. return false;
  60. }
  61. dmusicPerformance->InitAudio(NULL, NULL, hwnd, DMUS_APATH_SHARED_STEREOPLUSREVERB, 64,
  62.     DMUS_AUDIOF_ALL, NULL);
  63.      // 生成标准3D音频路径
  64.      if (FAILED(dmusicPerformance->CreateStandardAudioPath(DMUS_APATH_DYNAMIC_3D,
  65.                                                            64, TRUE, &dmusic3DAudioPath)))
  66.      {
  67.           MessageBox(hwnd, "Unable to create standard 3D audiopath! Press OK to exit",
  68.                      "ERROR!", MB_OK);
  69.           return false;
  70.      }
  71.      // retrieve the listener from the audiopath
  72.      if (FAILED(dmusic3DAudioPath->GetObjectInPath(0, DMUS_PATH_PRIMARY_BUFFER, 0, GUID_NULL, 0, 
  73.                                                   IID_IDirectSound3DListener8,
  74.                                                   (void**)&ds3DListener)))
  75.      {
  76.           MessageBox(hwnd, "Unable to retrieve the listener! Press OK to exit",
  77.                      "ERROR!", MB_OK);
  78.           return false;
  79.      }
  80.      dsListenerParams.dwSize = sizeof(DS3DLISTENER);
  81.      ds3DListener->GetAllParameters(&dsListenerParams);
  82.      dsListenerParams.vPosition.x = 0.0f;
  83.      dsListenerParams.vPosition.y = 0.0f;
  84.      dsListenerParams.vPosition.z = 0.0f;
  85.      ds3DListener->SetAllParameters(&dsListenerParams, DS3D_IMMEDIATE);
  86. // 获取当前路径
  87. GetCurrentDirectory(MAX_PATH, pathStr);
  88. // 转换统一字符串
  89. MultiByteToWideChar(CP_ACP, 0, pathStr, -1, wcharStr, MAX_PATH);
  90. // 设置查找路径
  91. dmusicLoader->SetSearchDirectory(GUID_DirectMusicAllTypes, wcharStr, FALSE);
  92. return true;
  93. }
  94. // 关闭音频系统
  95. void CAudioSystem::Shutdown()
  96. {
  97. // 停止音乐
  98. dmusicPerformance->Stop(NULL, NULL, 0, 0);
  99. // 关闭DirectMusic
  100. dmusicPerformance->CloseDown();
  101. }
  102. // PlaySegment()
  103. // desc: play a segment
  104. void CAudioSystem::PlaySegment(IDirectMusicSegment8 *dmSeg, bool is3DSound, DWORD numRepeats)
  105. {
  106.      // set the number of repeats
  107.      dmSeg->SetRepeats(numRepeats);//DMUS_SEG_REPEAT_INFINITE);
  108. if (!is3DSound)
  109. // download the segment's instruments to the audiopath
  110. dmSeg->Download(dmusicPerformance);
  111. else
  112. {
  113. dmSeg->Download(dmusic3DAudioPath);
  114. // play the segment on the next beat using the 3d audio path
  115. dmusicPerformance->PlaySegmentEx(dmSeg, NULL, NULL, DMUS_SEGF_DEFAULT, 0,
  116.                            NULL, NULL, dmusic3DAudioPath);
  117. }
  118. }
  119. // Play()
  120. // desc: play a CAudio object
  121. void CAudioSystem::Play(CAudio *audio, DWORD numRepeats)
  122. {
  123. // set number of repeats
  124. audio->GetSegment()->SetRepeats(numRepeats);
  125. if (audio->Is3DSound())
  126. {
  127. audio->GetSegment()->Download(dmusic3DAudioPath);
  128. // play the segment on the next beat using the 3d audio path
  129. dmusicPerformance->PlaySegmentEx(audio->GetSegment(), NULL, NULL, DMUS_SEGF_SECONDARY, 0,
  130.                            NULL, NULL, dmusic3DAudioPath);
  131. }
  132. else
  133. {
  134. audio->GetSegment()->Download(dmusicPerformance);
  135. // play the segment on the next beat using the 3d audio path
  136. dmusicPerformance->PlaySegmentEx(audio->GetSegment(), NULL, NULL, DMUS_SEGF_DEFAULT, 0,
  137.                            NULL, NULL, NULL);
  138. }
  139. }
  140. // Stop()
  141. // desc: stop a CAudio object from playing
  142. void CAudioSystem::Stop(CAudio *audio)
  143. {
  144. dmusicPerformance->StopEx(audio->GetSegment(), 0, 0);
  145. }
  146. // StopSegment()
  147. // desc: stop a segment from playing
  148. void CAudioSystem::StopSegment(IDirectMusicSegment8 *dmSeg)
  149. {
  150. // stop the dmSeg from playing
  151. dmusicPerformance->StopEx(dmSeg, 0, 0);
  152. }
  153. // SetListenerPos()
  154. // desc: set the listener position
  155. void CAudioSystem::SetListenerPos(float cameraX, float cameraY, float cameraZ)
  156. {
  157.      // set the listener position
  158. //ds3DListener->SetPosition(cameraX, cameraY, -cameraZ, DS3D_IMMEDIATE);
  159. // get the listener parameters
  160.      dsListenerParams.dwSize = sizeof(DS3DLISTENER);
  161.      ds3DListener->GetAllParameters(&dsListenerParams);
  162.      // set position of listener
  163.      dsListenerParams.vPosition.x = cameraX;
  164.      dsListenerParams.vPosition.y = cameraY;
  165.      dsListenerParams.vPosition.z = -cameraZ;
  166.      ds3DListener->SetAllParameters(&dsListenerParams, DS3D_IMMEDIATE);
  167. }
  168. // SetListenerRolloff()
  169. // desc: set the listener rolloff
  170. void CAudioSystem::SetListenerRolloff(float rolloff)
  171. {
  172. if (ds3DListener)
  173. ds3DListener->SetRolloffFactor(rolloff, DS3D_IMMEDIATE);
  174. }
  175. // Create3DBuffer()
  176. // desc: create a DX Audio 3D buffer
  177. IDirectSound3DBuffer *CAudioSystem::Create3DBuffer()
  178. {
  179. IDirectSound3DBuffer *buff;
  180. DS3DBUFFER dsBufferParams;          // 3d buffer properties
  181.      // get the 3D buffer in the audiopath
  182.      if (FAILED(dmusic3DAudioPath->GetObjectInPath(DMUS_PCHANNEL_ALL, DMUS_PATH_BUFFER, 0, GUID_NULL, 0,
  183.                                                    IID_IDirectSound3DBuffer,
  184.                                                    (void**)&buff)))
  185.      {
  186.           MessageBox(NULL, "Unable to retrieve 3D buffer from audiopath! Press OK to exit",
  187.                      "ERROR!", MB_OK);
  188.           return NULL;
  189.      }
  190.      // get the 3D buffer parameters
  191.      dsBufferParams.dwSize = sizeof(DS3DBUFFER);
  192.      buff->GetAllParameters(&dsBufferParams);
  193.      // set the new 3D buffer parameters
  194.      dsBufferParams.dwMode = DS3DMODE_HEADRELATIVE;    // relative to the listener
  195.      buff->SetAllParameters(&dsBufferParams, DS3D_IMMEDIATE);
  196. return buff;
  197. }
  198. // CreateSegment()
  199. // desc: create a DX Audio segment
  200. IDirectMusicSegment8 *CAudioSystem::CreateSegment(char *filename, bool is3DSound)
  201. {
  202. IDirectMusicSegment8 *seg;
  203.      WCHAR wcharStr[MAX_PATH];
  204.      // convert filename to unicode string
  205.      MultiByteToWideChar(CP_ACP, 0, filename, -1, wcharStr, MAX_PATH);
  206.      // load the segment from file
  207.      if (FAILED(dmusicLoader->LoadObjectFromFile(CLSID_DirectMusicSegment,
  208.                                                  IID_IDirectMusicSegment8,
  209.                                                  wcharStr,
  210.                                                  (void**)&seg)))
  211.      {
  212.           MessageBox(NULL, "Audio file not found! Press OK to exit",
  213.                      "ERROR!", MB_OK);
  214.           return NULL;
  215.      }
  216. return seg;
  217. }
  218. // Create()
  219. // desc: create a CAudio object
  220. CAudio *CAudioSystem::Create(char *filename, bool is3DSound)
  221. {
  222. CAudio *audio;
  223. IDirectMusicSegment8 *dmSeg;
  224. IDirectSound3DBuffer8 *ds3D;
  225. audio = new CAudio;
  226. dmSeg = CreateSegment(filename, is3DSound);
  227. audio->SetSegment(dmSeg);
  228. if (is3DSound)
  229. {
  230. ds3D = Create3DBuffer();
  231. audio->Set3DBuffer(ds3D);
  232. audio->Set3DSound(true);
  233. }
  234. else
  235. {
  236. audio->Set3DBuffer(NULL);
  237. audio->Set3DSound(false);
  238. }
  239. return audio;
  240. }