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.

Sound.cpp 5.6KB

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