Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SoundAL.cpp 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*!
  2. * \file src/system/SoundAL.cpp
  3. * \brief This is the OpenAL audio manager Implementation
  4. *
  5. * \author Mongoose
  6. * \author xythobuz
  7. */
  8. #ifdef __APPLE__
  9. #include <OpenAL/al.h>
  10. #else
  11. #include <AL/al.h>
  12. #endif
  13. #include <AL/alut.h>
  14. #include "global.h"
  15. #include "Log.h"
  16. #include "system/SoundAL.h"
  17. bool SoundAL::init = false;
  18. bool SoundAL::enabled = true;
  19. float SoundAL::volume = 1.0f;
  20. std::vector<unsigned int> SoundAL::buffers;
  21. std::vector<unsigned int> SoundAL::sources;
  22. std::vector<unsigned int> SoundAL::listenerSources;
  23. glm::vec3 SoundAL::lastPosition(0.0f, 0.0f, 0.0f);
  24. int SoundAL::initialize() {
  25. if (init)
  26. return 0;
  27. ALCdevice* device = alcOpenDevice(nullptr);
  28. ALCcontext* context = alcCreateContext(device, nullptr);
  29. alcMakeContextCurrent(context);
  30. if (alutInitWithoutContext(nullptr, nullptr) == AL_FALSE) {
  31. Log::get(LOG_ERROR) << "SoundAL Error: " << alutGetErrorString(alutGetError()) << Log::endl;
  32. return -1;
  33. }
  34. init = true;
  35. setVolume(volume);
  36. lastPosition = glm::vec3(0.0f, 0.0f, 0.0f);
  37. glm::vec3 at(0.0f, 0.0f, -1.0f);
  38. glm::vec3 up(0.0f, 1.0f, 0.0f);
  39. listenAt(lastPosition, at, up);
  40. return 0;
  41. }
  42. void SoundAL::shutdown() {
  43. if (!init)
  44. return;
  45. clear();
  46. if (alutExit() == AL_FALSE)
  47. Log::get(LOG_ERROR) << "SoundAL Error: " << alutGetErrorString(alutGetError()) << Log::endl;
  48. init = false;
  49. }
  50. void SoundAL::clear() {
  51. if (!init)
  52. return;
  53. stopAll();
  54. alGetError();
  55. alDeleteSources(sources.size(), &sources[0]);
  56. sources.clear();
  57. if (alGetError() != AL_NO_ERROR) {
  58. Log::get(LOG_ERROR) << "SoundAL: Error while deleting sources!" << Log::endl;
  59. }
  60. alGetError();
  61. alDeleteSources(listenerSources.size(), &listenerSources[0]);
  62. listenerSources.clear();
  63. if (alGetError() != AL_NO_ERROR) {
  64. Log::get(LOG_ERROR) << "SoundAL: Error while deleting listener sources!" << Log::endl;
  65. }
  66. alGetError();
  67. alDeleteBuffers(buffers.size(), &buffers[0]);
  68. buffers.clear();
  69. if (alGetError() != AL_NO_ERROR) {
  70. Log::get(LOG_ERROR) << "SoundAL: Error while deleting buffers!" << Log::endl;
  71. }
  72. lastPosition = glm::vec3(0.0f, 0.0f, 0.0f);
  73. glm::vec3 at(0.0f, 0.0f, -1.0f);
  74. glm::vec3 up(0.0f, 1.0f, 0.0f);
  75. listenAt(lastPosition, at, up);
  76. }
  77. int SoundAL::numBuffers() {
  78. return buffers.size();
  79. }
  80. int SoundAL::loadBuffer(unsigned char* buffer, unsigned int length) {
  81. if (!init)
  82. return -1;
  83. alGetError();
  84. unsigned int r = alutCreateBufferFromFileImage(buffer, length);
  85. if (r == AL_NONE) {
  86. Log::get(LOG_ERROR) << "SoundAL Error: " << alutGetErrorString(alutGetError()) << Log::endl;
  87. return -2;
  88. }
  89. buffers.push_back(r);
  90. return r;
  91. }
  92. int SoundAL::numSources(bool atListener) {
  93. if (atListener)
  94. return listenerSources.size();
  95. else
  96. return sources.size();
  97. }
  98. int SoundAL::addSource(int buffer, float volume, bool atListener, bool loop) {
  99. if ((!init) || (buffer < 0) || (buffer >= buffers.size()))
  100. return -1;
  101. unsigned int id;
  102. alGetError();
  103. alGenSources(1, &id);
  104. if (alGetError() != AL_NO_ERROR) {
  105. Log::get(LOG_ERROR) << "SoundAL Error: Could not create source!" << Log::endl;
  106. return -2;
  107. }
  108. alSourcei(id, AL_BUFFER, buffers.at(buffer));
  109. alSourcef(id, AL_GAIN, volume);
  110. if (loop)
  111. alSourcei(id, AL_LOOPING, AL_TRUE);
  112. if (atListener) {
  113. alSourcefv(id, AL_POSITION, &lastPosition[0]);
  114. listenerSources.push_back(id);
  115. return listenerSources.size() - 1;
  116. } else {
  117. sources.push_back(id);
  118. return sources.size() - 1;
  119. }
  120. }
  121. int SoundAL::sourceAt(int source, glm::vec3 pos) {
  122. if (!init)
  123. return -1;
  124. if ((source < 0) || (source >= sources.size())) {
  125. Log::get(LOG_ERROR) << "SoundAL: Can't position non-existing source!" << Log::endl;
  126. return -2;
  127. }
  128. alSourcefv(sources.at(source), AL_POSITION, &pos[0]);
  129. return 0;
  130. }
  131. void SoundAL::listenAt(glm::vec3 pos, glm::vec3 at, glm::vec3 up) {
  132. if (!init)
  133. return;
  134. float orientation[6] = { at.x, at.y, at.z, up.x, up.y, up.z };
  135. alListenerfv(AL_POSITION, &pos[0]);
  136. alListenerfv(AL_ORIENTATION, orientation);
  137. for (auto& s : listenerSources) {
  138. alSourcefv(s, AL_POSITION, &pos[0]);
  139. }
  140. lastPosition = pos;
  141. }
  142. void SoundAL::play(int source, bool atListener) {
  143. if ((!init) || (!enabled))
  144. return;
  145. if (atListener) {
  146. if ((source >= 0) && (source < listenerSources.size()))
  147. alSourcePlay(listenerSources.at(source));
  148. else
  149. Log::get(LOG_ERROR) << "SoundAL: Can't play non-existing listener source!" << Log::endl;
  150. } else {
  151. if ((source >= 0) && (source < sources.size()))
  152. alSourcePlay(sources.at(source));
  153. else
  154. Log::get(LOG_ERROR) << "SoundAL: Can't play non-existing source!" << Log::endl;
  155. }
  156. }
  157. void SoundAL::stopAll() {
  158. if (!init)
  159. return;
  160. alSourceStopv(sources.size(), &sources[0]);
  161. alSourceStopv(listenerSources.size(), &listenerSources[0]);
  162. }
  163. void SoundAL::setEnabled(bool on) {
  164. enabled = on;
  165. }
  166. bool SoundAL::getEnabled() {
  167. return enabled;
  168. }
  169. void SoundAL::setVolume(float vol) {
  170. volume = vol;
  171. if (init)
  172. alListenerf(AL_GAIN, volume);
  173. }
  174. float SoundAL::getVolume() {
  175. return volume;
  176. }