BsCAudioSource.cpp 4.1 KB

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