Source.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * Copyright (c) 2006-2009 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "Source.h"
  21. // STD
  22. #include <iostream>
  23. namespace love
  24. {
  25. namespace audio
  26. {
  27. namespace openal
  28. {
  29. Source::Source(Pool * pool)
  30. : pool(pool), source(0), pitch(1.0f), volume(1.0f)
  31. {
  32. }
  33. Source::Source(Pool * pool, Audible * audible)
  34. : pool(pool), source(0), pitch(1.0f), volume(1.0f)
  35. {
  36. setAudible(audible);
  37. }
  38. Source::~Source()
  39. {
  40. stop();
  41. }
  42. void Source::play()
  43. {
  44. if(source != 0)
  45. return; // Already playing.
  46. if(pool->isAvailable())
  47. source = pool->claim(this);
  48. if(source != 0)
  49. {
  50. audible->play(this);
  51. // Set these properties. These may have changed while we've
  52. // been without an AL source.
  53. alSourcef(source, AL_PITCH, pitch);
  54. alSourcef(source, AL_GAIN, volume);
  55. alSourcePlay(source);
  56. }
  57. }
  58. void Source::stop()
  59. {
  60. if(source)
  61. {
  62. alSourceStop(source);
  63. audible->stop(this);
  64. source = 0;
  65. }
  66. }
  67. void Source::pause()
  68. {
  69. if(source)
  70. {
  71. alSourcePause(source);
  72. }
  73. }
  74. void Source::resume()
  75. {
  76. if(source)
  77. {
  78. alSourcePlay(source);
  79. }
  80. }
  81. void Source::rewind()
  82. {
  83. if(audible != 0)
  84. audible->rewind(this);
  85. }
  86. bool Source::isStopped() const
  87. {
  88. if(source)
  89. {
  90. ALenum state;
  91. alGetSourcei(source, AL_SOURCE_STATE, &state);
  92. return (state == AL_STOPPED);
  93. }
  94. return true;
  95. }
  96. void Source::update()
  97. {
  98. if(audible != 0)
  99. audible->update(this);
  100. }
  101. void Source::setPitch(float pitch)
  102. {
  103. if(source)
  104. {
  105. alSourcef(source, AL_PITCH, pitch);
  106. }
  107. this->pitch = pitch;
  108. }
  109. float Source::getPitch() const
  110. {
  111. if(source)
  112. {
  113. ALfloat f;
  114. alGetSourcef(source, AL_PITCH, &f);
  115. return f;
  116. }
  117. // In case the Source isn't playing.
  118. return pitch;
  119. }
  120. void Source::setVolume(float volume)
  121. {
  122. if(source)
  123. {
  124. alSourcef(source, AL_GAIN, volume);
  125. }
  126. this->volume = volume;
  127. }
  128. float Source::getVolume() const
  129. {
  130. if(source)
  131. {
  132. ALfloat f;
  133. alGetSourcef(source, AL_GAIN, &f);
  134. return f;
  135. }
  136. // In case the Source isn't playing.
  137. return volume;
  138. }
  139. void Source::setPosition(float * v)
  140. {
  141. if(source)
  142. alSourcefv(source, AL_POSITION, v);
  143. }
  144. void Source::getPosition(float * v) const
  145. {
  146. if(source)
  147. alGetSourcefv(source, AL_POSITION, v);
  148. }
  149. void Source::setVelocity(float * v)
  150. {
  151. if(source)
  152. alSourcefv(source, AL_VELOCITY, v);
  153. }
  154. void Source::getVelocity(float * v) const
  155. {
  156. if(source)
  157. alGetSourcefv(source, AL_VELOCITY, v);
  158. }
  159. void Source::setDirection(float * v)
  160. {
  161. if(source)
  162. alSourcefv(source, AL_DIRECTION, v);
  163. }
  164. void Source::getDirection(float * v) const
  165. {
  166. if(source)
  167. alGetSourcefv(source, AL_DIRECTION, v);
  168. }
  169. } // openal
  170. } // audio
  171. } // love