BsCAudioSource.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Components/BsCAudioSource.h"
  4. #include "Scene/BsSceneObject.h"
  5. #include "Utility/BsTime.h"
  6. #include "RTTI/BsCAudioSourceRTTI.h"
  7. using namespace std::placeholders;
  8. namespace bs
  9. {
  10. CAudioSource::CAudioSource()
  11. {
  12. setName("AudioSource");
  13. mNotifyFlags = TCF_Transform;
  14. }
  15. CAudioSource::CAudioSource(const HSceneObject& parent)
  16. : Component(parent)
  17. {
  18. setName("AudioSource");
  19. mNotifyFlags = TCF_Transform;
  20. }
  21. void CAudioSource::setClip(const HAudioClip& clip)
  22. {
  23. if (mAudioClip == clip)
  24. return;
  25. mAudioClip = clip;
  26. if (mInternal != nullptr)
  27. mInternal->setClip(clip);
  28. }
  29. void CAudioSource::setVolume(float volume)
  30. {
  31. if (mVolume == volume)
  32. return;
  33. mVolume = volume;
  34. if (mInternal != nullptr)
  35. mInternal->setVolume(volume);
  36. }
  37. void CAudioSource::setPitch(float pitch)
  38. {
  39. if (mPitch == pitch)
  40. return;
  41. mPitch = pitch;
  42. if (mInternal != nullptr)
  43. mInternal->setPitch(pitch);
  44. }
  45. void CAudioSource::setIsLooping(bool loop)
  46. {
  47. if (mLoop == loop)
  48. return;
  49. mLoop = loop;
  50. if (mInternal != nullptr)
  51. mInternal->setIsLooping(loop);
  52. }
  53. void CAudioSource::setPriority(UINT32 priority)
  54. {
  55. if (mPriority == priority)
  56. return;
  57. mPriority = priority;
  58. if (mInternal != nullptr)
  59. mInternal->setPriority(priority);
  60. }
  61. void CAudioSource::setMinDistance(float distance)
  62. {
  63. if (mMinDistance == distance)
  64. return;
  65. mMinDistance = distance;
  66. if (mInternal != nullptr)
  67. mInternal->setMinDistance(distance);
  68. }
  69. void CAudioSource::setAttenuation(float attenuation)
  70. {
  71. if (mAttenuation == attenuation)
  72. return;
  73. mAttenuation = attenuation;
  74. if (mInternal != nullptr)
  75. mInternal->setAttenuation(attenuation);
  76. }
  77. void CAudioSource::play()
  78. {
  79. if (mInternal != nullptr)
  80. mInternal->play();
  81. }
  82. void CAudioSource::pause()
  83. {
  84. if (mInternal != nullptr)
  85. mInternal->pause();
  86. }
  87. void CAudioSource::stop()
  88. {
  89. if (mInternal != nullptr)
  90. mInternal->stop();
  91. }
  92. void CAudioSource::setTime(float position)
  93. {
  94. if (mInternal != nullptr)
  95. mInternal->setTime(position);
  96. }
  97. float CAudioSource::getTime() const
  98. {
  99. if (mInternal != nullptr)
  100. return mInternal->getTime();
  101. return 0.0f;
  102. }
  103. AudioSourceState CAudioSource::getState() const
  104. {
  105. if (mInternal != nullptr)
  106. return mInternal->getState();
  107. return AudioSourceState::Stopped;
  108. }
  109. void CAudioSource::onInitialized()
  110. {
  111. }
  112. void CAudioSource::onDestroyed()
  113. {
  114. destroyInternal();
  115. }
  116. void CAudioSource::onDisabled()
  117. {
  118. destroyInternal();
  119. }
  120. void CAudioSource::onEnabled()
  121. {
  122. restoreInternal();
  123. if (mPlayOnStart)
  124. play();
  125. }
  126. void CAudioSource::onTransformChanged(TransformChangedFlags flags)
  127. {
  128. if (!SO()->getActive())
  129. return;
  130. if ((flags & (TCF_Parent | TCF_Transform)) != 0)
  131. updateTransform();
  132. }
  133. void CAudioSource::update()
  134. {
  135. Vector3 worldPos = SO()->getTransform().getPosition();
  136. mVelocity = (worldPos - mLastPosition) / gTime().getFrameDelta();
  137. mLastPosition = worldPos;
  138. }
  139. void CAudioSource::restoreInternal()
  140. {
  141. if (mInternal == nullptr)
  142. mInternal = AudioSource::create();
  143. // Note: Merge into one call to avoid many virtual function calls
  144. mInternal->setClip(mAudioClip);
  145. mInternal->setVolume(mVolume);
  146. mInternal->setPitch(mPitch);
  147. mInternal->setIsLooping(mLoop);
  148. mInternal->setPriority(mPriority);
  149. mInternal->setMinDistance(mMinDistance);
  150. mInternal->setAttenuation(mAttenuation);
  151. updateTransform();
  152. }
  153. void CAudioSource::destroyInternal()
  154. {
  155. // This should release the last reference and destroy the internal listener
  156. mInternal = nullptr;
  157. }
  158. void CAudioSource::updateTransform()
  159. {
  160. mInternal->setTransform(SO()->getTransform());
  161. mInternal->setVelocity(mVelocity);
  162. }
  163. RTTITypeBase* CAudioSource::getRTTIStatic()
  164. {
  165. return CAudioSourceRTTI::instance();
  166. }
  167. RTTITypeBase* CAudioSource::getRTTI() const
  168. {
  169. return CAudioSource::getRTTIStatic();
  170. }
  171. }