AudioSource.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. // Create an audio buffer from this path.
  29. AudioBuffer* buffer = AudioBuffer::create(path);
  30. if (buffer == NULL)
  31. return NULL;
  32. // Load the audio source.
  33. ALuint alSource;
  34. alGenSources(1, &alSource);
  35. if (alGetError() != AL_NO_ERROR)
  36. {
  37. SAFE_RELEASE(buffer);
  38. LOG_ERROR("AudioSource::createAudioSource Error generating audio source.");
  39. return NULL;
  40. }
  41. return new AudioSource(buffer, alSource);
  42. }
  43. AudioSource* AudioSource::create(Properties* properties)
  44. {
  45. // TODO: Implement this!
  46. return NULL;
  47. }
  48. AudioSource::State AudioSource::getState() const
  49. {
  50. ALint state;
  51. alGetSourcei(_alSource, AL_SOURCE_STATE, &state);
  52. switch (state)
  53. {
  54. case AL_PLAYING:
  55. return PLAYING;
  56. case AL_PAUSED:
  57. return PAUSED;
  58. case AL_STOPPED:
  59. return STOPPED;
  60. default:
  61. return INITIAL;
  62. }
  63. return INITIAL;
  64. }
  65. void AudioSource::play()
  66. {
  67. alSourcePlay(_alSource);
  68. }
  69. void AudioSource::pause()
  70. {
  71. alSourcePause(_alSource);
  72. }
  73. void AudioSource::resume()
  74. {
  75. if (getState() == PAUSED)
  76. {
  77. play();
  78. }
  79. }
  80. void AudioSource::stop()
  81. {
  82. alSourceStop(_alSource);
  83. }
  84. void AudioSource::rewind()
  85. {
  86. alSourceRewind(_alSource);
  87. }
  88. bool AudioSource::isLooped() const
  89. {
  90. return _looped;
  91. }
  92. void AudioSource::setLooped(bool looped)
  93. {
  94. // Clear error state.
  95. alGetError();
  96. alSourcei(_alSource, AL_LOOPING, (looped) ? AL_TRUE : AL_FALSE);
  97. ALCenum error = alGetError();
  98. if (error != AL_NO_ERROR)
  99. {
  100. LOG_ERROR_VARG("AudioSource::setLooped Error: %d", error);
  101. }
  102. _looped = looped;
  103. }
  104. float AudioSource::getGain() const
  105. {
  106. return _gain;
  107. }
  108. void AudioSource::setGain(float gain)
  109. {
  110. alSourcef(_alSource, AL_GAIN, gain);
  111. _gain = gain;
  112. }
  113. float AudioSource::getPitch() const
  114. {
  115. return _pitch;
  116. }
  117. void AudioSource::setPitch(float pitch)
  118. {
  119. alSourcef(_alSource, AL_PITCH, pitch);
  120. _pitch = pitch;
  121. }
  122. const Vector3& AudioSource::getVelocity() const
  123. {
  124. return _velocity;
  125. }
  126. void AudioSource::setVelocity(const Vector3& velocity)
  127. {
  128. alSourcefv(_alSource, AL_VELOCITY, (ALfloat*)&velocity);
  129. _velocity = velocity;
  130. }
  131. Node* AudioSource::getNode() const
  132. {
  133. return _node;
  134. }
  135. void AudioSource::setNode(Node* node)
  136. {
  137. if (_node != node)
  138. {
  139. // Disconnect our current transform.
  140. if (_node)
  141. {
  142. _node->removeListener(this);
  143. }
  144. // Connect the new node.
  145. _node = node;
  146. if (_node)
  147. {
  148. _node->addListener(this);
  149. }
  150. }
  151. }
  152. void AudioSource::transformChanged(Transform* transform, long cookie)
  153. {
  154. alSourcefv(_alSource, AL_POSITION, (const ALfloat*)&transform->getTranslation());
  155. }
  156. }