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.

Sound.cpp 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. #endif
  13. #include <AL/alut.h>
  14. #include <time.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <sys/time.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <fcntl.h>
  21. #include <unistd.h>
  22. #include <assert.h>
  23. #include "Sound.h"
  24. Sound::Sound()
  25. {
  26. mSource[0] = 0;
  27. mBuffer[0] = 0;
  28. mNext = 0;
  29. mInit = false;
  30. }
  31. Sound::~Sound()
  32. {
  33. if (mInit)
  34. {
  35. alutExit();
  36. }
  37. }
  38. int Sound::init()
  39. {
  40. #ifndef __APPLE__
  41. int fd;
  42. fd = open("/dev/dsp", O_RDWR);
  43. if (fd < 0)
  44. {
  45. perror("Sound::Init> Could not open /dev/dsp : ");
  46. return -1;
  47. }
  48. close(fd);
  49. #endif
  50. ALCdevice *Device = alcOpenDevice("OSS");
  51. ALCcontext *Context = alcCreateContext(Device, NULL);
  52. alcMakeContextCurrent(Context);
  53. if (alutInitWithoutContext(NULL, NULL) == AL_FALSE) {
  54. printf("Sound::Init> Could not initialize alut (%s)\n", alutGetErrorString(alutGetError()));
  55. return -2;
  56. }
  57. mInit = true;
  58. printf("Created OpenAL Context\n");
  59. return 0;
  60. }
  61. int Sound::registeredSources() {
  62. return mNext;
  63. }
  64. void Sound::listenAt(float pos[3], float angle[3])
  65. {
  66. assert(mInit == true);
  67. assert(pos != NULL);
  68. assert(angle != NULL);
  69. alListenerfv(AL_POSITION, pos);
  70. alListenerfv(AL_ORIENTATION, angle);
  71. }
  72. void Sound::sourceAt(int source, float pos[3])
  73. {
  74. assert(mInit == true);
  75. assert(source >= 0);
  76. assert(source < mNext);
  77. assert(pos != NULL);
  78. alSourcefv(mSource[source], AL_POSITION, pos);
  79. }
  80. //! \fixme Seperate sourcing and buffering, Mongoose 2002.01.04
  81. int Sound::addFile(const char *filename, int *source, unsigned int flags)
  82. {
  83. ALsizei size;
  84. ALfloat freq;
  85. ALenum format;
  86. ALvoid *data;
  87. assert(mInit == true);
  88. assert(filename != NULL);
  89. assert(filename[0] != '\0');
  90. assert(source != NULL);
  91. *source = -1;
  92. alGetError();
  93. alGenBuffers(1, &mBuffer[mNext]);
  94. if (alGetError() != AL_NO_ERROR)
  95. {
  96. fprintf(stderr, "Sound::AddFile> alGenBuffers call failed\n");
  97. return -1;
  98. }
  99. alGetError();
  100. alGenSources(1, &mSource[mNext]);
  101. if (alGetError() != AL_NO_ERROR)
  102. {
  103. fprintf(stderr, "Sound::AddFile> alGenSources call failed\n");
  104. return -2;
  105. }
  106. // err = alutLoadWAV(filename, &data, &format, &size, &bits, &freq);
  107. // is deprecated!
  108. data = alutLoadMemoryFromFile(filename, &format, &size, &freq);
  109. if (alutGetError() != ALUT_ERROR_NO_ERROR)
  110. {
  111. fprintf(stderr, "Could not load %s\n", filename);
  112. return -3;
  113. }
  114. alBufferData(mBuffer[mNext], format, data, size, static_cast<ALsizei>(freq));
  115. alSourcei(mSource[mNext], AL_BUFFER, mBuffer[mNext]);
  116. if (flags & SoundFlagsLoop)
  117. {
  118. alSourcei(mSource[mNext], AL_LOOPING, 1);
  119. }
  120. *source = mNext++;
  121. return 0;
  122. }
  123. int Sound::addWave(unsigned char *wav, unsigned int length, int *source, unsigned int flags)
  124. {
  125. ALsizei size;
  126. ALfloat freq;
  127. ALenum format;
  128. ALvoid *data;
  129. int error = 0;
  130. assert(mInit == true);
  131. assert(wav != NULL);
  132. assert(source != NULL);
  133. *source = -1;
  134. data = wav;
  135. alGetError();
  136. alGenBuffers(1, &mBuffer[mNext]);
  137. if (alGetError() != AL_NO_ERROR)
  138. {
  139. fprintf(stderr, "Sound::AddWave> alGenBuffers call failed\n");
  140. return -1;
  141. }
  142. alGetError();
  143. alGenSources(1, &mSource[mNext]);
  144. if (alGetError() != AL_NO_ERROR)
  145. {
  146. fprintf(stderr, "Sound::AddWave> alGenSources call failed\n");
  147. return -2;
  148. }
  149. //AL_FORMAT_WAVE_EXT does not exist on Mac!"
  150. // alBufferData(mBuffer[mNext], AL_FORMAT_WAVE_EXT, data, size, freq);
  151. // Idea: Fill Buffer with
  152. // alutLoadMemoryFromFileImage
  153. // (const ALvoid *data, ALsizei length, ALenum *format, ALsizei *size, ALfloat *frequency)
  154. data = alutLoadMemoryFromFileImage(wav, length, &format, &size, &freq);
  155. if (((error = alutGetError()) != ALUT_ERROR_NO_ERROR) || (data == NULL)) {
  156. fprintf(stderr, "Could not load wav buffer (%s)\n", alutGetErrorString(error));
  157. return -3;
  158. }
  159. alBufferData(mBuffer[mNext], format, data, size, static_cast<ALsizei>(freq));
  160. alSourcei(mSource[mNext], AL_BUFFER, mBuffer[mNext]);
  161. if (flags & SoundFlagsLoop)
  162. {
  163. alSourcei(mSource[mNext], AL_LOOPING, 1);
  164. }
  165. *source = mNext++;
  166. //! \fixme Should free alut buffer?
  167. return 0;
  168. }
  169. void Sound::remove(int source) {
  170. assert(source >= 0);
  171. assert(source < mNext);
  172. alDeleteSources(1, &mSource[source]);
  173. alDeleteBuffers(1, &mBuffer[source]);
  174. if (source == (mNext - 1))
  175. mNext--;
  176. }
  177. void Sound::play(int source)
  178. {
  179. assert(mInit == true);
  180. assert(source >= 0);
  181. assert(source < mNext);
  182. alSourcePlay(mSource[source]);
  183. }
  184. void Sound::stop(int source)
  185. {
  186. assert(mInit == true);
  187. assert(source >= 0);
  188. assert(source < mNext);
  189. alSourceStop(mSource[source]);
  190. }