Open Source Tomb Raider Engine
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Sound.cpp 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*!
  2. * \file src/Sound.cpp
  3. * \brief This is the audio manager Implementation
  4. *
  5. * \author Mongoose
  6. * \author xythobuz
  7. */
  8. #ifdef USING_OPENAL
  9. #ifdef __APPLE__
  10. #include <OpenAL/al.h>
  11. #include <OpenAL/alc.h>
  12. #include <AL/alut.h>
  13. #else
  14. #include <AL/al.h>
  15. #include <AL/alc.h>
  16. #include <AL/alut.h>
  17. #endif
  18. #else
  19. #warning "No OpenAL support. Won't play sound!"
  20. #endif
  21. #include <time.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <sys/time.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include <unistd.h>
  29. #include <Sound.h>
  30. #ifdef DEBUG_MEMORY
  31. #include <memory_test.h>
  32. #endif
  33. Sound::Sound()
  34. {
  35. mSource[0] = 0;
  36. mBuffer[0] = 0;
  37. mNextBuffer = 0;
  38. mNextSource = 0;
  39. mInit = false;
  40. }
  41. Sound::~Sound()
  42. {
  43. if (mInit)
  44. {
  45. #ifdef USING_OPENAL
  46. alutExit();
  47. #endif
  48. }
  49. }
  50. int Sound::init()
  51. {
  52. #ifndef __APPLE__
  53. int fd;
  54. fd = open("/dev/dsp", O_RDWR);
  55. if (fd < 0)
  56. {
  57. perror("Sound::Init> Could not open /dev/dsp : ");
  58. return -1;
  59. }
  60. close(fd);
  61. #endif
  62. #ifdef USING_OPENAL
  63. alutInit(NULL, 0);
  64. mInit = true;
  65. printf("Created OpenAL Context\n");
  66. #else
  67. printf("Couldn't create sound Context!\n");
  68. #endif
  69. return 0;
  70. }
  71. void Sound::listenAt(float pos[3], float angle[3])
  72. {
  73. if (!mInit)
  74. return;
  75. #ifdef USING_OPENAL
  76. alListenerfv(AL_POSITION, pos);
  77. alListenerfv(AL_ORIENTATION, angle);
  78. #endif
  79. }
  80. void Sound::sourceAt(int source, float pos[3])
  81. {
  82. if (!mInit || source < 0)
  83. return;
  84. #ifdef USING_OPENAL
  85. alSourcefv(mSource[source-1], AL_POSITION, pos);
  86. #endif
  87. }
  88. //! \fixme Seperate sourcing and buffering, Mongoose 2002.01.04
  89. int Sound::addFile(const char *filename, int *source, unsigned int flags)
  90. {
  91. #ifdef USING_OPENAL
  92. ALsizei size;
  93. ALfloat freq;
  94. ALenum format;
  95. ALvoid *data;
  96. #endif
  97. if (!mInit || !filename || !source)
  98. {
  99. printf("Sound::AddFile> ERROR pre condition assertion failed\n");
  100. return -1000;
  101. }
  102. *source = -1;
  103. #ifdef USING_OPENAL
  104. alGetError();
  105. alGenBuffers(1, &mBuffer[mNextBuffer]);
  106. if (alGetError() != AL_NO_ERROR)
  107. {
  108. fprintf(stderr, "Sound::AddFile> alGenBuffers call failed\n");
  109. return -1;
  110. }
  111. alGetError();
  112. alGenSources(1, &mSource[mNextSource]);
  113. if (alGetError() != AL_NO_ERROR)
  114. {
  115. fprintf(stderr, "Sound::AddFile> alGenSources call failed\n");
  116. return -2;
  117. }
  118. // err = alutLoadWAV(filename, &data, &format, &size, &bits, &freq);
  119. // is deprecated!
  120. data = alutLoadMemoryFromFile(filename, &format, &size, &freq);
  121. if (alGetError() != AL_NO_ERROR)
  122. {
  123. fprintf(stderr, "Could not load %s\n", filename);
  124. return -3;
  125. }
  126. alBufferData(mBuffer[mNextBuffer], format, data, size, static_cast<ALsizei>(freq));
  127. alSourcei(mSource[mNextSource], AL_BUFFER, mBuffer[mNextBuffer]);
  128. if (flags & SoundFlagsLoop)
  129. {
  130. alSourcei(mSource[mNextSource], AL_LOOPING, 1);
  131. }
  132. ++mNextBuffer;
  133. ++mNextSource;
  134. *source = mNextBuffer;
  135. return 0;
  136. #else
  137. return -1;
  138. #endif
  139. }
  140. int Sound::addWave(unsigned char *wav, unsigned int length, int *source, unsigned int flags)
  141. {
  142. #ifdef USING_OPENAL
  143. ALsizei size;
  144. ALfloat freq;
  145. ALenum format;
  146. ALvoid *data;
  147. #endif
  148. if (!mInit || !wav || !source)
  149. {
  150. printf("Sound::AddWave> ERROR pre condition assertion failed\n");
  151. return -1000;
  152. }
  153. *source = -1;
  154. #ifdef USING_OPENAL
  155. data = wav;
  156. alGetError();
  157. alGenBuffers(1, &mBuffer[mNextBuffer]);
  158. if (alGetError() != AL_NO_ERROR)
  159. {
  160. fprintf(stderr, "Sound::AddWave> alGenBuffers call failed\n");
  161. return -1;
  162. }
  163. alGetError();
  164. alGenSources(1, &mSource[mNextSource]);
  165. if (alGetError() != AL_NO_ERROR)
  166. {
  167. fprintf(stderr, "Sound::AddWave> alGenSources call failed\n");
  168. return -2;
  169. }
  170. //AL_FORMAT_WAVE_EXT does not exist on Mac!"
  171. // alBufferData(mBuffer[mNextBuffer], AL_FORMAT_WAVE_EXT, data, size, freq);
  172. // Idea: Fill Buffer with
  173. // alutLoadMemoryFromFileImage
  174. // (const ALvoid *data, ALsizei length, ALenum *format, ALsizei *size, ALfloat *frequency)
  175. data = alutLoadMemoryFromFileImage(wav, length, &format, &size, &freq);
  176. if ((alGetError() != AL_NO_ERROR) || (data == NULL)) {
  177. fprintf(stderr, "Could not load wav buffer\n");
  178. return -3;
  179. }
  180. alBufferData(mBuffer[mNextBuffer], format, data, size, static_cast<ALsizei>(freq));
  181. alSourcei(mSource[mNextSource], AL_BUFFER, mBuffer[mNextBuffer]);
  182. if (flags & SoundFlagsLoop)
  183. {
  184. alSourcei(mSource[mNextSource], AL_LOOPING, 1);
  185. }
  186. ++mNextBuffer;
  187. ++mNextSource;
  188. *source = mNextBuffer;
  189. return 0;
  190. #else
  191. return -1;
  192. #endif
  193. }
  194. void Sound::play(int source)
  195. {
  196. if (!mInit)
  197. {
  198. printf("Sound::Play> ERROR: Sound not initialized\n");
  199. return;
  200. }
  201. if (source < 0)
  202. {
  203. printf("Sound::Play> ERROR: Source Id invalid\n");
  204. return;
  205. }
  206. #ifdef USING_OPENAL
  207. alSourcePlay(mSource[source-1]);
  208. #endif
  209. }
  210. void Sound::stop(int source)
  211. {
  212. if (!mInit || source < 0)
  213. {
  214. printf("Sound::Stop> ERROR pre condition assertion failed\n");
  215. return;
  216. }
  217. #ifdef USING_OPENAL
  218. alSourceStop(mSource[source-1]);
  219. #endif
  220. }