AudioSource.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include "Base.h"
  2. #include "Node.h"
  3. #include "AudioBuffer.h"
  4. #include "AudioSource.h"
  5. namespace gameplay
  6. {
  7. AudioSource::AudioSource(AudioBuffer* buffer, ALuint source)
  8. : _alSource(source), _buffer(buffer), _looped(true), _gain(1.0f), _pitch(1.0f), _node(NULL)
  9. {
  10. alSourcei(_alSource, AL_BUFFER, buffer->_alBuffer);
  11. alSourcei(_alSource, AL_LOOPING, _looped);
  12. alSourcef(_alSource, AL_PITCH, _pitch);
  13. alSourcef(_alSource, AL_GAIN, _gain);
  14. alSourcefv(_alSource, AL_VELOCITY, (const ALfloat*)&_velocity);
  15. }
  16. AudioSource::~AudioSource()
  17. {
  18. if (_alSource)
  19. {
  20. alDeleteSources(1, &_alSource);
  21. _alSource = 0;
  22. }
  23. SAFE_RELEASE(_buffer);
  24. }
  25. AudioSource* AudioSource::create(const char* path)
  26. {
  27. assert(path);
  28. // Load from a .audio file.
  29. std::string pathStr = path;
  30. if (pathStr.find(".audio") != pathStr.npos)
  31. {
  32. Properties* properties = Properties::create(path);
  33. assert(properties);
  34. if (properties == NULL)
  35. {
  36. return NULL;
  37. }
  38. AudioSource* audioSource = create(properties->getNextNamespace());
  39. SAFE_DELETE(properties);
  40. return audioSource;
  41. }
  42. // Create an audio buffer from this path.
  43. AudioBuffer* buffer = AudioBuffer::create(path);
  44. if (buffer == NULL)
  45. return NULL;
  46. // Load the audio source.
  47. ALuint alSource;
  48. alGenSources(1, &alSource);
  49. if (alGetError() != AL_NO_ERROR)
  50. {
  51. SAFE_RELEASE(buffer);
  52. LOG_ERROR("AudioSource::createAudioSource Error generating audio source.");
  53. return NULL;
  54. }
  55. return new AudioSource(buffer, alSource);
  56. }
  57. AudioSource* AudioSource::create(Properties* properties)
  58. {
  59. // Check if the properties is valid and has a valid namespace.
  60. assert(properties);
  61. if (!properties || !(strcmp(properties->getNamespace(), "audio") == 0))
  62. {
  63. WARN("Failed to load audio source from properties object: must be non-null object and have namespace equal to \'audio\'.");
  64. return NULL;
  65. }
  66. const char* path = properties->getString("path");
  67. if (path == NULL)
  68. {
  69. WARN("Audio file failed to load; the file path was not specified.");
  70. return NULL;
  71. }
  72. // Create the audio source.
  73. AudioSource* audio = AudioSource::create(path);
  74. if (audio == NULL)
  75. {
  76. WARN_VARG("Audio file '%s' failed to load properly.", path);
  77. return NULL;
  78. }
  79. // Set any properties that the user specified in the .audio file.
  80. if (properties->getString("looped") != NULL)
  81. {
  82. audio->setLooped(properties->getBool("looped"));
  83. }
  84. if (properties->getString("gain") != NULL)
  85. {
  86. audio->setGain(properties->getFloat("gain"));
  87. }
  88. if (properties->getString("pitch") != NULL)
  89. {
  90. audio->setPitch(properties->getFloat("pitch"));
  91. }
  92. Vector3 v;
  93. if (properties->getVector3("velocity", &v))
  94. {
  95. audio->setVelocity(v);
  96. }
  97. return audio;
  98. }
  99. AudioSource::State AudioSource::getState() const
  100. {
  101. ALint state;
  102. alGetSourcei(_alSource, AL_SOURCE_STATE, &state);
  103. switch (state)
  104. {
  105. case AL_PLAYING:
  106. return PLAYING;
  107. case AL_PAUSED:
  108. return PAUSED;
  109. case AL_STOPPED:
  110. return STOPPED;
  111. default:
  112. return INITIAL;
  113. }
  114. return INITIAL;
  115. }
  116. void AudioSource::play()
  117. {
  118. alSourcePlay(_alSource);
  119. }
  120. void AudioSource::pause()
  121. {
  122. alSourcePause(_alSource);
  123. }
  124. void AudioSource::resume()
  125. {
  126. if (getState() == PAUSED)
  127. {
  128. play();
  129. }
  130. }
  131. void AudioSource::stop()
  132. {
  133. alSourceStop(_alSource);
  134. }
  135. void AudioSource::rewind()
  136. {
  137. alSourceRewind(_alSource);
  138. }
  139. bool AudioSource::isLooped() const
  140. {
  141. return _looped;
  142. }
  143. void AudioSource::setLooped(bool looped)
  144. {
  145. // Clear error state.
  146. alGetError();
  147. alSourcei(_alSource, AL_LOOPING, (looped) ? AL_TRUE : AL_FALSE);
  148. ALCenum error = alGetError();
  149. if (error != AL_NO_ERROR)
  150. {
  151. LOG_ERROR_VARG("AudioSource::setLooped Error: %d", error);
  152. }
  153. _looped = looped;
  154. }
  155. float AudioSource::getGain() const
  156. {
  157. return _gain;
  158. }
  159. void AudioSource::setGain(float gain)
  160. {
  161. alSourcef(_alSource, AL_GAIN, gain);
  162. _gain = gain;
  163. }
  164. float AudioSource::getPitch() const
  165. {
  166. return _pitch;
  167. }
  168. void AudioSource::setPitch(float pitch)
  169. {
  170. alSourcef(_alSource, AL_PITCH, pitch);
  171. _pitch = pitch;
  172. }
  173. const Vector3& AudioSource::getVelocity() const
  174. {
  175. return _velocity;
  176. }
  177. void AudioSource::setVelocity(const Vector3& velocity)
  178. {
  179. alSourcefv(_alSource, AL_VELOCITY, (ALfloat*)&velocity);
  180. _velocity = velocity;
  181. }
  182. Node* AudioSource::getNode() const
  183. {
  184. return _node;
  185. }
  186. void AudioSource::setNode(Node* node)
  187. {
  188. if (_node != node)
  189. {
  190. // Disconnect our current transform.
  191. if (_node)
  192. {
  193. _node->removeListener(this);
  194. }
  195. // Connect the new node.
  196. _node = node;
  197. if (_node)
  198. {
  199. _node->addListener(this);
  200. }
  201. }
  202. }
  203. void AudioSource::transformChanged(Transform* transform, long cookie)
  204. {
  205. alSourcefv(_alSource, AL_POSITION, (const ALfloat*)&transform->getTranslation());
  206. }
  207. }