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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. int 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(int source, float pos[3]) {
  85. assert(mSource.size() == mBuffer.size());
  86. assert(source >= 0);
  87. assert(source < (int)mSource.size());
  88. assert(pos != NULL);
  89. if ((!mEnabled) || (!mInit))
  90. return;
  91. alSourcefv(mSource[source], AL_POSITION, pos);
  92. }
  93. //! \fixme Seperate sourcing and buffering, Mongoose 2002.01.04
  94. int SoundAL::addFile(const char *filename, int *source, unsigned int flags) {
  95. ALsizei size;
  96. ALfloat freq;
  97. ALenum format;
  98. ALvoid *data;
  99. int id;
  100. assert(mSource.size() == mBuffer.size());
  101. assert(filename != NULL);
  102. assert(filename[0] != '\0');
  103. assert(source != NULL);
  104. if ((!mEnabled) || (!mInit)) {
  105. *source = 0;
  106. return 0;
  107. }
  108. *source = -1;
  109. id = mSource.size();
  110. mSource.push_back(0);
  111. mBuffer.push_back(0);
  112. alGetError();
  113. alGenBuffers(1, &mBuffer[id]);
  114. if (alGetError() != AL_NO_ERROR) {
  115. printf("Could not load wav file (alGenBuffers)\n");
  116. return -1;
  117. }
  118. alGetError();
  119. alGenSources(1, &mSource[id]);
  120. if (alGetError() != AL_NO_ERROR) {
  121. printf("Could not load wav file (alGenSources)\n");
  122. return -2;
  123. }
  124. data = alutLoadMemoryFromFile(filename, &format, &size, &freq);
  125. if (alutGetError() != ALUT_ERROR_NO_ERROR) {
  126. printf("Could not load %s\n", filename);
  127. return -3;
  128. }
  129. alBufferData(mBuffer[id], format, data, size, static_cast<ALsizei>(freq));
  130. alSourcei(mSource[id], AL_BUFFER, mBuffer[id]);
  131. if (flags & SoundFlagsLoop) {
  132. alSourcei(mSource[id], AL_LOOPING, 1);
  133. }
  134. alSourcef(mSource[id], AL_GAIN, mVolume);
  135. *source = id;
  136. return 0;
  137. }
  138. int SoundAL::addWave(unsigned char *wav, unsigned int length, int *source, unsigned int flags) {
  139. ALsizei size;
  140. ALfloat freq;
  141. ALenum format;
  142. ALvoid *data;
  143. int error = 0;
  144. int id;
  145. assert(mSource.size() == mBuffer.size());
  146. assert(wav != NULL);
  147. assert(source != NULL);
  148. if ((!mEnabled) || (!mInit)) {
  149. *source = 0;
  150. return 0;
  151. }
  152. *source = -1;
  153. id = mSource.size();
  154. mSource.push_back(0);
  155. mBuffer.push_back(0);
  156. data = wav;
  157. alGetError();
  158. alGenBuffers(1, &mBuffer[id]);
  159. if (alGetError() != AL_NO_ERROR) {
  160. printf("Could not load wav (alGenBuffers)\n");
  161. return -1;
  162. }
  163. alGetError();
  164. alGenSources(1, &mSource[id]);
  165. if (alGetError() != AL_NO_ERROR) {
  166. printf("Could not load wav (alGenSources)\n");
  167. return -2;
  168. }
  169. data = alutLoadMemoryFromFileImage(wav, length, &format, &size, &freq);
  170. if (((error = alutGetError()) != ALUT_ERROR_NO_ERROR) || (data == NULL)) {
  171. printf("Could not load wav buffer (%s)\n", alutGetErrorString(error));
  172. return -3;
  173. }
  174. alBufferData(mBuffer[id], format, data, size, static_cast<ALsizei>(freq));
  175. alSourcei(mSource[id], AL_BUFFER, mBuffer[id]);
  176. if (flags & SoundFlagsLoop) {
  177. alSourcei(mSource[id], AL_LOOPING, 1);
  178. }
  179. alSourcef(mSource[id], AL_GAIN, mVolume);
  180. *source = id;
  181. //! \fixme Should free alut buffer?
  182. return 0;
  183. }
  184. void SoundAL::play(int source) {
  185. assert(mSource.size() == mBuffer.size());
  186. assert(source >= 0);
  187. assert(source < (int)mSource.size());
  188. if ((!mEnabled) || (!mInit))
  189. return;
  190. alSourcePlay(mSource[source]);
  191. }
  192. void SoundAL::stop(int source) {
  193. assert(mSource.size() == mBuffer.size());
  194. assert(source >= 0);
  195. assert(source < (int)mSource.size());
  196. if ((!mEnabled) || (!mInit))
  197. return;
  198. alSourceStop(mSource[source]);
  199. }