AudioSource.h 5.3 KB

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