BsAudioSource.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "BsIResourceListener.h"
  6. #include "BsVector3.h"
  7. namespace bs
  8. {
  9. /** @addtogroup Audio
  10. * @{
  11. */
  12. /** Valid states in which AudioSource can be in. */
  13. enum class AudioSourceState
  14. {
  15. Playing, /**< Source is currently playing. */
  16. Paused, /**< Source is currently paused (play will resume from paused point). */
  17. Stopped /**< Source is currently stopped (play will resume from start). */
  18. };
  19. /**
  20. * Represents a source for emitting audio. Audio can be played spatially (gun shot), or normally (music). Each audio
  21. * source must have an AudioClip to play-back, and it can also have a position in the case of spatial (3D) audio.
  22. *
  23. * Whether or not an audio source is spatial is controlled by the assigned AudioClip. The volume and the pitch of a
  24. * spatial audio source is controlled by its position and the AudioListener's position/direction/velocity.
  25. */
  26. class BS_CORE_EXPORT AudioSource : public IResourceListener
  27. {
  28. public:
  29. virtual ~AudioSource() { }
  30. /** Sets the audio clip to play. */
  31. virtual void setClip(const HAudioClip& clip);
  32. /** Returns the attached audio clip. */
  33. HAudioClip getClip() const { return mAudioClip; }
  34. /** Sets the world position of the source. Only relevant for spatial (3D) sources. */
  35. virtual void setPosition(const Vector3& position);
  36. /** Returns the world position of the source. Only relevant for spatial (3D) sources. */
  37. Vector3 getPosition() const { return mPosition; }
  38. /** Sets the velocity of the source. Only relevant for spatial (3D) sources. */
  39. virtual void setVelocity(const Vector3& velocity);
  40. /** Gets the velocity of the source. Only relevant for spatial (3D) sources. */
  41. Vector3 getVelocity() const { return mVelocity; }
  42. /** Sets the volume of the audio source, in [0, 1] range. */
  43. virtual void setVolume(float volume);
  44. /** Retrieves the volume of the audio source, in [0, 1] range. */
  45. float getVolume() const { return mVolume; }
  46. /** Sets the pitch of the audio source. */
  47. virtual void setPitch(float pitch);
  48. /** Gets the pitch of the audio source. */
  49. float getPitch() const { return mPitch; }
  50. /** Sets whether the audio clip should loop when it finishes playing. */
  51. virtual void setIsLooping(bool loop);
  52. /** Gets whether the audio clip should loop when it finishes playing. */
  53. bool getIsLooping() const { return mLoop; }
  54. /**
  55. * Sets the priority of the audio source. If more audio sources are playing than supported by the hardware, some
  56. * 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. /**
  61. * Gets the priority of the audio source.
  62. *
  63. * @see setPriority
  64. */
  65. UINT32 getPriority() const { return mPriority; }
  66. /**
  67. * Returns the minimum distance at which audio attenuation starts. When the listener is closer to the source
  68. * than this value, audio is heard at full volume. Once farther away the audio starts attenuating.
  69. */
  70. virtual void setMinDistance(float distance);
  71. /**
  72. * Gets the minimum distance at which audio attenuation starts.
  73. *
  74. * @see setMinDistance
  75. */
  76. float getMinDistance() const { return mMinDistance; }
  77. /**
  78. * Sets attenuation that controls how quickly does audio volume drop off as the listener moves further from the
  79. * source.
  80. */
  81. virtual void setAttenuation(float attenuation);
  82. /**
  83. * Gets attenuation that controls how quickly does audio volume drop off as the listener moves further from the
  84. * source.
  85. */
  86. float getAttenuation() const { return mAttenuation; }
  87. /** Starts playing the currently assigned audio clip. */
  88. virtual void play() = 0;
  89. /** Pauses the audio playback. */
  90. virtual void pause() = 0;
  91. /** Stops audio playback, rewinding it to the start. */
  92. virtual void stop() = 0;
  93. /**
  94. * Sets the time at which playback will begin.
  95. *
  96. * @param[in] time Time in seconds, in range [0, clipLength].
  97. */
  98. virtual void setTime(float time) = 0;
  99. /**
  100. * Returns the current playback time.
  101. *
  102. * @return time Time in seconds, in range [0, clipLength].
  103. */
  104. virtual float getTime() const = 0;
  105. /** Returns the current state of the audio playback (playing/paused/stopped). */
  106. virtual AudioSourceState getState() const = 0;
  107. /** Creates a new audio source. */
  108. static SPtr<AudioSource> create();
  109. protected:
  110. AudioSource();
  111. /** @copydoc IResourceListener::getListenerResources */
  112. void getListenerResources(Vector<HResource>& resources) override;
  113. /** @copydoc IResourceListener::notifyResourceChanged */
  114. void notifyResourceChanged(const HResource& resource) override;
  115. /** Triggered by the resources system whenever the attached audio clip changed (e.g. was reimported.) */
  116. virtual void onClipChanged() { }
  117. HAudioClip mAudioClip;
  118. Vector3 mPosition;
  119. Vector3 mVelocity;
  120. float mVolume;
  121. float mPitch;
  122. bool mLoop;
  123. INT32 mPriority;
  124. float mMinDistance;
  125. float mAttenuation;
  126. };
  127. /** @} */
  128. }