2
0

SoundEvent.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #include "SoundEvent.h"
  9. #include "AudioSystem.h"
  10. #include <fmod_studio.hpp>
  11. SoundEvent::SoundEvent(class AudioSystem* system, unsigned int id)
  12. :mSystem(system)
  13. ,mID(id)
  14. {
  15. }
  16. SoundEvent::SoundEvent()
  17. :mSystem(nullptr)
  18. ,mID(0)
  19. {
  20. }
  21. bool SoundEvent::IsValid()
  22. {
  23. return (mSystem && mSystem->GetEventInstance(mID) != nullptr);
  24. }
  25. void SoundEvent::Restart()
  26. {
  27. auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
  28. if (event)
  29. {
  30. event->start();
  31. }
  32. }
  33. void SoundEvent::Stop(bool allowFadeOut /* true */)
  34. {
  35. auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
  36. if (event)
  37. {
  38. FMOD_STUDIO_STOP_MODE mode = allowFadeOut ?
  39. FMOD_STUDIO_STOP_ALLOWFADEOUT :
  40. FMOD_STUDIO_STOP_IMMEDIATE;
  41. event->stop(mode);
  42. }
  43. }
  44. void SoundEvent::SetPaused(bool pause)
  45. {
  46. auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
  47. if (event)
  48. {
  49. event->setPaused(pause);
  50. }
  51. }
  52. void SoundEvent::SetVolume(float value)
  53. {
  54. auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
  55. if (event)
  56. {
  57. event->setVolume(value);
  58. }
  59. }
  60. void SoundEvent::SetPitch(float value)
  61. {
  62. auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
  63. if (event)
  64. {
  65. event->setPitch(value);
  66. }
  67. }
  68. void SoundEvent::SetParameter(const std::string& name, float value)
  69. {
  70. auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
  71. if (event)
  72. {
  73. event->setParameterValue(name.c_str(), value);
  74. }
  75. }
  76. bool SoundEvent::GetPaused() const
  77. {
  78. bool retVal = false;
  79. auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
  80. if (event)
  81. {
  82. event->getPaused(&retVal);
  83. }
  84. return retVal;
  85. }
  86. float SoundEvent::GetVolume() const
  87. {
  88. float retVal = 0.0f;
  89. auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
  90. if (event)
  91. {
  92. event->getVolume(&retVal);
  93. }
  94. return retVal;
  95. }
  96. float SoundEvent::GetPitch() const
  97. {
  98. float retVal = 0.0f;
  99. auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
  100. if (event)
  101. {
  102. event->getPitch(&retVal);
  103. }
  104. return retVal;
  105. }
  106. float SoundEvent::GetParameter(const std::string& name)
  107. {
  108. float retVal = 0.0f;
  109. auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
  110. if (event)
  111. {
  112. event->getParameterValue(name.c_str(), &retVal);
  113. }
  114. return retVal;
  115. }
  116. bool SoundEvent::Is3D() const
  117. {
  118. bool retVal = false;
  119. auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
  120. if (event)
  121. {
  122. // Get the event description
  123. FMOD::Studio::EventDescription* ed = nullptr;
  124. event->getDescription(&ed);
  125. if (ed)
  126. {
  127. ed->is3D(&retVal);
  128. }
  129. }
  130. return retVal;
  131. }
  132. namespace
  133. {
  134. FMOD_VECTOR VecToFMOD(const Vector3& in)
  135. {
  136. // Convert from our coordinates (+x forward, +y right, +z up)
  137. // to FMOD (+z forward, +x right, +y up)
  138. FMOD_VECTOR v;
  139. v.x = in.y;
  140. v.y = in.z;
  141. v.z = in.x;
  142. return v;
  143. }
  144. }
  145. void SoundEvent::Set3DAttributes(const Matrix4& worldTrans)
  146. {
  147. auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
  148. if (event)
  149. {
  150. FMOD_3D_ATTRIBUTES attr;
  151. // Set position, forward, up
  152. attr.position = VecToFMOD(worldTrans.GetTranslation());
  153. // In world transform, first row is forward
  154. attr.forward = VecToFMOD(worldTrans.GetXAxis());
  155. // Third row is up
  156. attr.up = VecToFMOD(worldTrans.GetZAxis());
  157. // Set velocity to zero (fix if using Doppler effect)
  158. attr.velocity = { 0.0f, 0.0f, 0.0f };
  159. event->set3DAttributes(&attr);
  160. }
  161. }