Audio.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * Copyright (c) 2006-2010 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 "Audio.h"
  21. namespace love
  22. {
  23. namespace audio
  24. {
  25. namespace openal
  26. {
  27. Audio::Audio()
  28. : finish(false)
  29. {
  30. // Passing zero for default device.
  31. device = alcOpenDevice(0);
  32. if(device == 0)
  33. throw love::Exception("Could not open device.");
  34. context = alcCreateContext(device, 0);
  35. if(context == 0)
  36. throw love::Exception("Could not create context.");
  37. alcMakeContextCurrent(context);
  38. if(alcGetError(device) != ALC_NO_ERROR)
  39. throw love::Exception("Could not make context current.");
  40. // pool must be allocated after AL context.
  41. pool = new Pool();
  42. thread = SDL_CreateThread(Audio::run, (void*)this);
  43. }
  44. Audio::~Audio()
  45. {
  46. finish = true;
  47. SDL_WaitThread(thread, 0);
  48. delete pool;
  49. alcMakeContextCurrent(0);
  50. alcDestroyContext(context);
  51. alcCloseDevice(device);
  52. }
  53. int Audio::run(void * d)
  54. {
  55. Audio * instance = (Audio*)d;
  56. while(!instance->finish)
  57. {
  58. instance->pool->update();
  59. SDL_Delay(5);
  60. }
  61. return 0;
  62. }
  63. const char * Audio::getName() const
  64. {
  65. return "love.audio.openal";
  66. }
  67. love::audio::Source * Audio::newSource(love::sound::Decoder * decoder)
  68. {
  69. return new Source(pool, decoder);
  70. }
  71. love::audio::Source * Audio::newSource(love::sound::SoundData * soundData)
  72. {
  73. return new Source(pool, soundData);
  74. }
  75. int Audio::getNumSources() const
  76. {
  77. return pool->getNumSources();
  78. }
  79. int Audio::getMaxSources() const
  80. {
  81. return pool->getMaxSources();
  82. }
  83. void Audio::play(love::audio::Source * source)
  84. {
  85. source->play();
  86. }
  87. void Audio::stop(love::audio::Source * source)
  88. {
  89. source->stop();
  90. }
  91. void Audio::stop()
  92. {
  93. pool->stop();
  94. }
  95. void Audio::pause(love::audio::Source * source)
  96. {
  97. source->pause();
  98. }
  99. void Audio::pause()
  100. {
  101. pool->pause();
  102. }
  103. void Audio::resume(love::audio::Source * source)
  104. {
  105. source->resume();
  106. }
  107. void Audio::resume()
  108. {
  109. pool->resume();
  110. }
  111. void Audio::rewind(love::audio::Source * source)
  112. {
  113. source->rewind();
  114. }
  115. void Audio::rewind()
  116. {
  117. pool->rewind();
  118. }
  119. void Audio::setVolume(float volume)
  120. {
  121. alListenerf(AL_GAIN, volume);
  122. }
  123. float Audio::getVolume() const
  124. {
  125. ALfloat volume;
  126. alGetListenerf(AL_GAIN, &volume);
  127. return volume;
  128. }
  129. void Audio::getPosition(float * v) const
  130. {
  131. alGetListenerfv(AL_POSITION, v);
  132. }
  133. void Audio::setPosition(float * v)
  134. {
  135. alListenerfv(AL_POSITION, v);
  136. }
  137. void Audio::getOrientation(float * v) const
  138. {
  139. alGetListenerfv(AL_ORIENTATION, v);
  140. }
  141. void Audio::setOrientation(float * v)
  142. {
  143. alListenerfv(AL_ORIENTATION, v);
  144. }
  145. void Audio::getVelocity(float * v) const
  146. {
  147. alGetListenerfv(AL_VELOCITY, v);
  148. }
  149. void Audio::setVelocity(float * v)
  150. {
  151. alListenerfv(AL_VELOCITY, v);
  152. }
  153. } // openal
  154. } // audio
  155. } // love