AudioSource.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #ifndef AUDIOSOURCE_H_
  2. #define AUDIOSOURCE_H_
  3. #include "Vector3.h"
  4. #include "Ref.h"
  5. #include "Node.h"
  6. namespace gameplay
  7. {
  8. class AudioBuffer;
  9. class Node;
  10. class NodeCloneContext;
  11. /**
  12. * Declares an audio source in 3D space.
  13. */
  14. class AudioSource : public Ref, public Transform::Listener
  15. {
  16. public:
  17. friend class Node;
  18. friend class AudioController;
  19. /**
  20. * The audio source's audio state.
  21. */
  22. enum State
  23. {
  24. INITIAL,
  25. PLAYING,
  26. PAUSED,
  27. STOPPED
  28. };
  29. /**
  30. * Create an audio source. This is used to instantiate an Audio Source. Currently only wav, au, and raw files are supported.
  31. * Alternately, a URL specifying a Properties object that defines an audio source can be used (where the URL is of the format
  32. * "<file-path>.<extension>#<namespace-id>/<namespace-id>/.../<namespace-id>" and "#<namespace-id>/<namespace-id>/.../<namespace-id>" is optional).
  33. *
  34. * @param url The relative location on disk of the sound file or a URL specifying a Properties object defining an audio source.
  35. * @return The newly created audio source, or NULL if an audio source cannot be created.
  36. * @script{create}
  37. */
  38. static AudioSource* create(const char* url);
  39. /**
  40. * Create an audio source from the given properties object.
  41. *
  42. * @param properties The properties object defining the audio source (must have namespace equal to 'audio').
  43. * @return The newly created audio source, or <code>NULL</code> if the audio source failed to load.
  44. * @script{create}
  45. */
  46. static AudioSource* create(Properties* properties);
  47. /**
  48. * Plays the audio source.
  49. */
  50. void play();
  51. /**
  52. * Pauses the playing of the audio source.
  53. */
  54. void pause();
  55. /**
  56. * Resumes playing of the audio source.
  57. */
  58. void resume();
  59. /**
  60. * Stops the playing of the audio source.
  61. */
  62. void stop();
  63. /**
  64. * Rewinds the audio source to the beginning.
  65. */
  66. void rewind();
  67. /**
  68. * Gets the current state of the audio source.
  69. *
  70. * @return PLAYING if the source is playing, STOPPED if the source is stopped, PAUSED if the source is paused and INITIAL otherwise.
  71. */
  72. AudioSource::State getState() const;
  73. /**
  74. * Determines whether the audio source is looped or not.
  75. *
  76. * @return true if the audio source is looped, false if not.
  77. */
  78. bool isLooped() const;
  79. /**
  80. * Sets the state of the audio source to be looping or not.
  81. *
  82. * @param looped true to loop the sound, false to not loop it.
  83. */
  84. void setLooped(bool looped);
  85. /**
  86. * Returns the gain of the audio source.
  87. *
  88. * @return The gain.
  89. */
  90. float getGain() const;
  91. /**
  92. * Sets the gain/volume of the audio source.
  93. *
  94. * @param gain The gain/volume of the source.
  95. */
  96. void setGain(float gain);
  97. /**
  98. * Returns the pitch of the audio source.
  99. *
  100. * @return The pitch.
  101. */
  102. float getPitch() const;
  103. /**
  104. * Sets the pitch of the audio source.
  105. *
  106. * @param pitch The pitch of the source.
  107. */
  108. void setPitch(float pitch);
  109. /**
  110. * Gets the velocity of the audio source.
  111. *
  112. * @return The velocity as a vector.
  113. */
  114. const Vector3& getVelocity() const;
  115. /**
  116. * Sets the velocity of the audio source.
  117. *
  118. * @param velocity A vector representing the velocity.
  119. */
  120. void setVelocity(const Vector3& velocity);
  121. /**
  122. * Sets the velocity of the audio source.
  123. *
  124. * @param x The x coordinate of the velocity.
  125. * @param y The y coordinate of the velocity.
  126. * @param z The z coordinate of the velocity.
  127. */
  128. void setVelocity(float x, float y, float z);
  129. /**
  130. * Gets the node that this source is attached to.
  131. *
  132. * @return The node that this audio source is attached to.
  133. */
  134. Node* getNode() const;
  135. private:
  136. /**
  137. * Constructor that takes an AudioBuffer.
  138. */
  139. AudioSource(AudioBuffer* buffer, ALuint source);
  140. /**
  141. * Destructor.
  142. */
  143. virtual ~AudioSource();
  144. /**
  145. * Hidden copy assignment operator.
  146. */
  147. AudioSource& operator=(const AudioSource&);
  148. /**
  149. * Sets the node for this audio source.
  150. */
  151. void setNode(Node* node);
  152. /**
  153. * @see Transform::Listener::transformChanged
  154. */
  155. void transformChanged(Transform* transform, long cookie);
  156. /**
  157. * Clones the audio source and returns a new audio source.
  158. *
  159. * @param context The clone context.
  160. *
  161. * @return The newly created audio source.
  162. */
  163. AudioSource* clone(NodeCloneContext &context) const;
  164. ALuint _alSource;
  165. AudioBuffer* _buffer;
  166. bool _looped;
  167. float _gain;
  168. float _pitch;
  169. Vector3 _velocity;
  170. Node* _node;
  171. };
  172. }
  173. #endif