BsAudioSource.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "Resources/BsIResourceListener.h"
  6. #include "Scene/BsSceneActor.h"
  7. #include "Math/BsVector3.h"
  8. namespace bs
  9. {
  10. /** @addtogroup Audio
  11. * @{
  12. */
  13. /** Valid states in which AudioSource can be in. */
  14. enum class BS_SCRIPT_EXPORT(m:Audio) AudioSourceState
  15. {
  16. Playing, /**< Source is currently playing. */
  17. Paused, /**< Source is currently paused (play will resume from paused point). */
  18. Stopped /**< Source is currently stopped (play will resume from start). */
  19. };
  20. /**
  21. * Represents a source for emitting audio. Audio can be played spatially (gun shot), or normally (music). Each audio
  22. * source must have an AudioClip to play-back, and it can also have a position in the case of spatial (3D) audio.
  23. *
  24. * Whether or not an audio source is spatial is controlled by the assigned AudioClip. The volume and the pitch of a
  25. * spatial audio source is controlled by its position and the AudioListener's position/direction/velocity.
  26. */
  27. class BS_CORE_EXPORT AudioSource : public IReflectable, public SceneActor, public IResourceListener
  28. {
  29. public:
  30. virtual ~AudioSource() { }
  31. /** Aaudio clip to play. */
  32. virtual void setClip(const HAudioClip& clip);
  33. /** @copydoc setClip() */
  34. HAudioClip getClip() const { return mAudioClip; }
  35. /**
  36. * Velocity of the source. Determines pitch in relation to AudioListener's position. Only relevant for spatial
  37. * (3D) sources.
  38. */
  39. virtual void setVelocity(const Vector3& velocity);
  40. /** @copydoc setVelocity() */
  41. Vector3 getVelocity() const { return mVelocity; }
  42. /** Volume of the audio played from this source, in [0, 1] range. */
  43. virtual void setVolume(float volume);
  44. /** @copydoc setVolume() */
  45. float getVolume() const { return mVolume; }
  46. /** Determines the pitch of the played audio. 1 is the default. */
  47. virtual void setPitch(float pitch);
  48. /** @copydoc setPitch() */
  49. float getPitch() const { return mPitch; }
  50. /** Determines whether the audio clip should loop when it finishes playing. */
  51. virtual void setIsLooping(bool loop);
  52. /** @copydoc setIsLooping() */
  53. bool getIsLooping() const { return mLoop; }
  54. /**
  55. * Determines the priority of the audio source. If more audio sources are playing than supported by the hardware,
  56. * some might get disabled. By setting a higher priority the audio source is guaranteed to be disabled after sources
  57. * with lower priority.
  58. */
  59. virtual void setPriority(INT32 priority);
  60. /** @copydoc setPriority() */
  61. UINT32 getPriority() const { return mPriority; }
  62. /**
  63. * Minimum distance at which audio attenuation starts. When the listener is closer to the source
  64. * than this value, audio is heard at full volume. Once farther away the audio starts attenuating.
  65. */
  66. virtual void setMinDistance(float distance);
  67. /** @copydoc setMinDistance() */
  68. float getMinDistance() const { return mMinDistance; }
  69. /**
  70. * Attenuation that controls how quickly does audio volume drop off as the listener moves further from the source.
  71. */
  72. virtual void setAttenuation(float attenuation);
  73. /** @copydoc setAttenuation() */
  74. float getAttenuation() const { return mAttenuation; }
  75. /** Starts playing the currently assigned audio clip. */
  76. virtual void play() = 0;
  77. /** Pauses the audio playback. */
  78. virtual void pause() = 0;
  79. /** Stops audio playback, rewinding it to the start. */
  80. virtual void stop() = 0;
  81. /**
  82. * Determines the current time of playback. If playback hasn't yet started, it specifies the time at which playback
  83. * will start at. The time is in seconds, in range [0, clipLength].
  84. */
  85. virtual void setTime(float time) = 0;
  86. /** @copydoc setTime() */
  87. virtual float getTime() const = 0;
  88. /** Returns the current state of the audio playback (playing/paused/stopped). */
  89. virtual AudioSourceState getState() const = 0;
  90. /** Creates a new audio source. */
  91. static SPtr<AudioSource> create();
  92. protected:
  93. AudioSource();
  94. /** @copydoc IResourceListener::getListenerResources */
  95. void getListenerResources(Vector<HResource>& resources) override;
  96. /** @copydoc IResourceListener::notifyResourceChanged */
  97. void notifyResourceChanged(const HResource& resource) override;
  98. /** Triggered by the resources system whenever the attached audio clip changed (e.g. was reimported.) */
  99. virtual void onClipChanged() { }
  100. HAudioClip mAudioClip;
  101. Vector3 mVelocity;
  102. float mVolume;
  103. float mPitch;
  104. bool mLoop;
  105. INT32 mPriority;
  106. float mMinDistance;
  107. float mAttenuation;
  108. /************************************************************************/
  109. /* RTTI */
  110. /************************************************************************/
  111. public:
  112. friend class AudioSourceRTTI;
  113. static RTTITypeBase* getRTTIStatic();
  114. RTTITypeBase* getRTTI() const override;
  115. };
  116. /** @} */
  117. }