Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

SoundAL.cpp 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*!
  2. * \file src/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. #include <fcntl.h>
  13. #include <unistd.h>
  14. #endif
  15. #include <AL/alut.h>
  16. #include <cstdio>
  17. #include <cstdlib>
  18. #include "global.h"
  19. #include "math/math.h"
  20. #include "SoundAL.h"
  21. SoundAL::SoundAL() {
  22. mEnabled = false;
  23. mInit = false;
  24. mVolume = 1.0f;
  25. }
  26. SoundAL::~SoundAL() {
  27. if (mInit)
  28. alutExit();
  29. }
  30. int SoundAL::initialize() {
  31. assert(mInit == false);
  32. if (!mEnabled)
  33. return 0;
  34. ALCdevice *Device = alcOpenDevice(NULL);
  35. ALCcontext *Context = alcCreateContext(Device, NULL);
  36. alcMakeContextCurrent(Context);
  37. if (alutInitWithoutContext(NULL, NULL) == AL_FALSE) {
  38. printf("ALUT-Error: %s\n", alutGetErrorString(alutGetError()));
  39. return -2;
  40. }
  41. mInit = true;
  42. return 0;
  43. }
  44. void SoundAL::setEnabled(bool on) {
  45. mEnabled = on;
  46. }
  47. void SoundAL::setVolume(float vol) {
  48. assert(mSource.size() == mBuffer.size());
  49. if (mEnabled) {
  50. if ((mSource.size() > 0) && (!equalEpsilon(mVolume, vol))) {
  51. // Apply new volume to old sources if needed
  52. for (size_t i = 0; i < mSource.size(); i++)
  53. alSourcef(mSource[i], AL_GAIN, vol);
  54. }
  55. }
  56. mVolume = vol;
  57. }
  58. unsigned long SoundAL::registeredSources() {
  59. assert(mSource.size() == mBuffer.size());
  60. if ((!mEnabled) || (!mInit))
  61. return 0;
  62. return mSource.size();
  63. }
  64. void SoundAL::clear() {
  65. assert(mSource.size() == mBuffer.size());
  66. if ((!mEnabled) || (!mInit))
  67. return;
  68. for (size_t i = 0; i < mSource.size(); i++) {
  69. alDeleteSources(1, &mSource[i]);
  70. alDeleteBuffers(1, &mBuffer[i]);
  71. }
  72. mSource.clear();
  73. mBuffer.clear();
  74. }
  75. void SoundAL::listenAt(float pos[3], float angle[3]) {
  76. assert(mSource.size() == mBuffer.size());
  77. assert(pos != NULL);
  78. assert(angle != NULL);
  79. if ((!mEnabled) || (!mInit))
  80. return;
  81. alListenerfv(AL_POSITION, pos);
  82. alListenerfv(AL_ORIENTATION, angle);
  83. }
  84. void SoundAL::sourceAt(unsigned long source, float pos[3]) {
  85. assert(mSource.size() == mBuffer.size());
  86. assert(source < mSource.size());
  87. assert(pos != NULL);
  88. if ((!mEnabled) || (!mInit))
  89. return;
  90. alSourcefv(mSource[source], AL_POSITION, pos);
  91. }
  92. //! \fixme Seperate sourcing and buffering, Mongoose 2002.01.04
  93. int SoundAL::addFile(const char *filename, unsigned long *source, unsigned int flags) {
  94. ALsizei size;
  95. ALfloat freq;
  96. ALenum format;
  97. ALvoid *data;
  98. unsigned long id;
  99. assert(mSource.size() == mBuffer.size());
  100. assert(filename != NULL);
  101. assert(filename[0] != '\0');
  102. assert(source != NULL);
  103. if ((!mEnabled) || (!mInit)) {
  104. *source = 0;
  105. return 0;
  106. }
  107. id = mSource.size();
  108. mSource.push_back(0);
  109. mBuffer.push_back(0);
  110. alGetError();
  111. alGenBuffers(1, &mBuffer[id]);
  112. if (alGetError() != AL_NO_ERROR) {
  113. printf("Could not load wav file (alGenBuffers)\n");
  114. return -1;
  115. }
  116. alGetError();
  117. alGenSources(1, &mSource[id]);
  118. if (alGetError() != AL_NO_ERROR) {
  119. printf("Could not load wav file (alGenSources)\n");
  120. return -2;
  121. }
  122. data = alutLoadMemoryFromFile(filename, &format, &size, &freq);
  123. if (alutGetError() != ALUT_ERROR_NO_ERROR) {
  124. printf("Could not load %s\n", filename);
  125. return -3;
  126. }
  127. alBufferData(mBuffer[id], format, data, size, static_cast<ALsizei>(freq));
  128. alSourcei(mSource[id], AL_BUFFER, mBuffer[id]);
  129. if (flags & SoundFlagsLoop) {
  130. alSourcei(mSource[id], AL_LOOPING, 1);
  131. }
  132. alSourcef(mSource[id], AL_GAIN, mVolume);
  133. *source = id;
  134. return 0;
  135. }
  136. int SoundAL::addWave(unsigned char *wav, unsigned int length, unsigned long *source, unsigned int flags) {
  137. ALsizei size;
  138. ALfloat freq;
  139. ALenum format;
  140. ALvoid *data;
  141. int error = 0;
  142. unsigned long id;
  143. assert(mSource.size() == mBuffer.size());
  144. assert(wav != NULL);
  145. assert(source != NULL);
  146. if ((!mEnabled) || (!mInit)) {
  147. *source = 0;
  148. return 0;
  149. }
  150. id = mSource.size();
  151. mSource.push_back(0);
  152. mBuffer.push_back(0);
  153. data = wav;
  154. alGetError();
  155. alGenBuffers(1, &mBuffer[id]);
  156. if (alGetError() != AL_NO_ERROR) {
  157. printf("Could not load wav (alGenBuffers)\n");
  158. return -1;
  159. }
  160. alGetError();
  161. alGenSources(1, &mSource[id]);
  162. if (alGetError() != AL_NO_ERROR) {
  163. printf("Could not load wav (alGenSources)\n");
  164. return -2;
  165. }
  166. data = alutLoadMemoryFromFileImage(wav, length, &format, &size, &freq);
  167. if (((error = alutGetError()) != ALUT_ERROR_NO_ERROR) || (data == NULL)) {
  168. printf("Could not load wav buffer (%s)\n", alutGetErrorString(error));
  169. return -3;
  170. }
  171. alBufferData(mBuffer[id], format, data, size, static_cast<ALsizei>(freq));
  172. alSourcei(mSource[id], AL_BUFFER, mBuffer[id]);
  173. if (flags & SoundFlagsLoop) {
  174. alSourcei(mSource[id], AL_LOOPING, 1);
  175. }
  176. alSourcef(mSource[id], AL_GAIN, mVolume);
  177. *source = id;
  178. //! \fixme Should free alut buffer?
  179. return 0;
  180. }
  181. void SoundAL::play(unsigned long source) {
  182. assert(mInit);
  183. assert(mSource.size() == mBuffer.size());
  184. assert(source < mSource.size());
  185. if (!mEnabled)
  186. return;
  187. alSourcePlay(mSource[source]);
  188. }
  189. void SoundAL::stop(unsigned long source) {
  190. assert(mInit);
  191. assert(mSource.size() == mBuffer.size());
  192. assert(source < mSource.size());
  193. if (!mEnabled)
  194. return;
  195. alSourceStop(mSource[source]);
  196. }