BsAudioSource.h 4.8 KB

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