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 4.8KB

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