Open Source Tomb Raider Engine
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

SoundAL.cpp 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. float SoundAL::lastPosition[3] = { 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. return 0;
  37. }
  38. void SoundAL::shutdown() {
  39. if (!init)
  40. return;
  41. clear();
  42. if (alutExit() == AL_FALSE)
  43. Log::get(LOG_ERROR) << "SoundAL Error: " << alutGetErrorString(alutGetError()) << Log::endl;
  44. init = false;
  45. }
  46. void SoundAL::clear() {
  47. if (!init)
  48. return;
  49. stopAll();
  50. alGetError();
  51. alDeleteSources(sources.size(), &sources[0]);
  52. sources.clear();
  53. if (alGetError() != AL_NO_ERROR) {
  54. Log::get(LOG_ERROR) << "SoundAL: Error while deleting sources!" << Log::endl;
  55. }
  56. alGetError();
  57. alDeleteSources(listenerSources.size(), &listenerSources[0]);
  58. listenerSources.clear();
  59. if (alGetError() != AL_NO_ERROR) {
  60. Log::get(LOG_ERROR) << "SoundAL: Error while deleting listener sources!" << Log::endl;
  61. }
  62. alGetError();
  63. alDeleteBuffers(buffers.size(), &buffers[0]);
  64. buffers.clear();
  65. if (alGetError() != AL_NO_ERROR) {
  66. Log::get(LOG_ERROR) << "SoundAL: Error while deleting buffers!" << Log::endl;
  67. }
  68. for (int i = 0; i < 3; i++)
  69. lastPosition[i] = 0.0f;
  70. float orientation[6] = { 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f };
  71. listenAt(lastPosition, orientation);
  72. }
  73. int SoundAL::numBuffers() {
  74. return buffers.size();
  75. }
  76. int SoundAL::loadBuffer(unsigned char* buffer, unsigned int length) {
  77. if (!init)
  78. return -1;
  79. alGetError();
  80. unsigned int r = alutCreateBufferFromFileImage(buffer, length);
  81. if (r == AL_NONE) {
  82. Log::get(LOG_ERROR) << "SoundAL Error: " << alutGetErrorString(alutGetError()) << Log::endl;
  83. return -2;
  84. }
  85. buffers.push_back(r);
  86. return r;
  87. }
  88. int SoundAL::numSources(bool atListener) {
  89. if (atListener)
  90. return listenerSources.size();
  91. else
  92. return sources.size();
  93. }
  94. int SoundAL::addSource(int buffer, float volume, bool atListener, bool loop) {
  95. if ((!init) || (buffer < 0) || (buffer >= buffers.size()))
  96. return -1;
  97. unsigned int id;
  98. alGetError();
  99. alGenSources(1, &id);
  100. if (alGetError() != AL_NO_ERROR) {
  101. Log::get(LOG_ERROR) << "SoundAL Error: Could not create source!" << Log::endl;
  102. return -2;
  103. }
  104. alSourcei(id, AL_BUFFER, buffers.at(buffer));
  105. alSourcef(id, AL_GAIN, volume);
  106. if (loop)
  107. alSourcei(id, AL_LOOPING, AL_TRUE);
  108. if (atListener) {
  109. alSourcefv(id, AL_POSITION, lastPosition);
  110. listenerSources.push_back(id);
  111. return listenerSources.size() - 1;
  112. } else {
  113. sources.push_back(id);
  114. return sources.size() - 1;
  115. }
  116. }
  117. int SoundAL::sourceAt(int source, float pos[3]) {
  118. if (!init)
  119. return -1;
  120. if ((source < 0) || (source >= sources.size()) || (pos == nullptr)) {
  121. Log::get(LOG_ERROR) << "SoundAL: Can't position non-existing source!" << Log::endl;
  122. return -2;
  123. }
  124. alSourcefv(sources.at(source), AL_POSITION, pos);
  125. return 0;
  126. }
  127. void SoundAL::listenAt(float pos[3], float orientation[6]) {
  128. if ((!init) || (pos == nullptr) || (orientation == nullptr))
  129. return;
  130. alListenerfv(AL_POSITION, pos);
  131. alListenerfv(AL_ORIENTATION, orientation);
  132. for (auto& s : listenerSources) {
  133. alSourcefv(s, AL_POSITION, pos);
  134. }
  135. for (int i = 0; i < 3; i++)
  136. lastPosition[i] = pos[i];
  137. }
  138. void SoundAL::play(int source, bool atListener) {
  139. if ((!init) || (!enabled))
  140. return;
  141. if (atListener) {
  142. if ((source >= 0) && (source < listenerSources.size()))
  143. alSourcePlay(listenerSources.at(source));
  144. else
  145. Log::get(LOG_ERROR) << "SoundAL: Can't play non-existing listener source!" << Log::endl;
  146. } else {
  147. if ((source >= 0) && (source < sources.size()))
  148. alSourcePlay(sources.at(source));
  149. else
  150. Log::get(LOG_ERROR) << "SoundAL: Can't play non-existing source!" << Log::endl;
  151. }
  152. }
  153. void SoundAL::stopAll() {
  154. if (!init)
  155. return;
  156. alSourceStopv(sources.size(), &sources[0]);
  157. alSourceStopv(listenerSources.size(), &listenerSources[0]);
  158. }
  159. void SoundAL::setEnabled(bool on) {
  160. enabled = on;
  161. }
  162. bool SoundAL::getEnabled() {
  163. return enabled;
  164. }
  165. void SoundAL::setVolume(float vol) {
  166. volume = vol;
  167. if (init)
  168. alListenerf(AL_GAIN, volume);
  169. }
  170. float SoundAL::getVolume() {
  171. return volume;
  172. }