Open Source Tomb Raider Engine
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Sound.cpp 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*!
  2. * \file src/Sound.cpp
  3. * \brief This is the audio manager Implementation
  4. *
  5. * \author xythobuz
  6. * \author Mongoose
  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. mInit = false;
  26. mVolume = 1.0f;
  27. }
  28. Sound::~Sound() {
  29. if (mInit)
  30. alutExit();
  31. }
  32. int Sound::init() {
  33. assert(mInit == false);
  34. #ifndef __APPLE__
  35. int fd;
  36. fd = open("/dev/dsp", O_RDWR);
  37. if (fd < 0) {
  38. perror("Sound::Init> Could not open /dev/dsp : ");
  39. return -1;
  40. }
  41. close(fd);
  42. #endif
  43. ALCdevice *Device = alcOpenDevice("OSS");
  44. ALCcontext *Context = alcCreateContext(Device, NULL);
  45. alcMakeContextCurrent(Context);
  46. if (alutInitWithoutContext(NULL, NULL) == AL_FALSE) {
  47. printf("Sound::Init> Could not initialize alut (%s)\n", alutGetErrorString(alutGetError()));
  48. return -2;
  49. }
  50. mInit = true;
  51. printf("Created OpenAL Context\n");
  52. return 0;
  53. }
  54. int Sound::registeredSources() {
  55. assert(mInit == true);
  56. assert(mSource.size() == mBuffer.size());
  57. return mSource.size();
  58. }
  59. void Sound::clear() {
  60. assert(mInit == true);
  61. assert(mSource.size() == mBuffer.size());
  62. for (size_t i = 0; i < mSource.size(); i++) {
  63. alDeleteSources(1, &mSource[i]);
  64. alDeleteBuffers(1, &mBuffer[i]);
  65. }
  66. mSource.clear();
  67. mBuffer.clear();
  68. }
  69. void Sound::setVolume(float vol) {
  70. assert(mInit == true);
  71. assert(mSource.size() == mBuffer.size());
  72. if ((mSource.size() > 0) && (mVolume != vol)) {
  73. // Apply new volume to old sources if needed
  74. for (size_t i = 0; i < mSource.size(); i++)
  75. alSourcef(mSource[i], AL_GAIN, vol);
  76. }
  77. mVolume = vol;
  78. }
  79. void Sound::listenAt(float pos[3], float angle[3]) {
  80. assert(mInit == true);
  81. assert(mSource.size() == mBuffer.size());
  82. assert(pos != NULL);
  83. assert(angle != NULL);
  84. alListenerfv(AL_POSITION, pos);
  85. alListenerfv(AL_ORIENTATION, angle);
  86. }
  87. void Sound::sourceAt(int source, float pos[3]) {
  88. assert(mInit == true);
  89. assert(mSource.size() == mBuffer.size());
  90. assert(source >= 0);
  91. assert(source < (int)mSource.size());
  92. assert(pos != NULL);
  93. alSourcefv(mSource[source], AL_POSITION, pos);
  94. }
  95. //! \fixme Seperate sourcing and buffering, Mongoose 2002.01.04
  96. int Sound::addFile(const char *filename, int *source, unsigned int flags)
  97. {
  98. ALsizei size;
  99. ALfloat freq;
  100. ALenum format;
  101. ALvoid *data;
  102. int id;
  103. assert(mInit == true);
  104. assert(mSource.size() == mBuffer.size());
  105. assert(filename != NULL);
  106. assert(filename[0] != '\0');
  107. assert(source != NULL);
  108. *source = -1;
  109. id = mSource.size();
  110. alGetError();
  111. alGenBuffers(1, &mBuffer[id]);
  112. if (alGetError() != AL_NO_ERROR) {
  113. fprintf(stderr, "Sound::AddFile> alGenBuffers call failed\n");
  114. return -1;
  115. }
  116. alGetError();
  117. alGenSources(1, &mSource[id]);
  118. if (alGetError() != AL_NO_ERROR) {
  119. fprintf(stderr, "Sound::AddFile> alGenSources call failed\n");
  120. return -2;
  121. }
  122. // err = alutLoadWAV(filename, &data, &format, &size, &bits, &freq);
  123. // is deprecated!
  124. data = alutLoadMemoryFromFile(filename, &format, &size, &freq);
  125. if (alutGetError() != ALUT_ERROR_NO_ERROR) {
  126. fprintf(stderr, "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 Sound::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(mInit == true);
  146. assert(mSource.size() == mBuffer.size());
  147. assert(wav != NULL);
  148. assert(source != NULL);
  149. *source = -1;
  150. id = mSource.size();
  151. data = wav;
  152. alGetError();
  153. alGenBuffers(1, &mBuffer[id]);
  154. if (alGetError() != AL_NO_ERROR) {
  155. fprintf(stderr, "Sound::AddWave> alGenBuffers call failed\n");
  156. return -1;
  157. }
  158. alGetError();
  159. alGenSources(1, &mSource[id]);
  160. if (alGetError() != AL_NO_ERROR) {
  161. fprintf(stderr, "Sound::AddWave> alGenSources call failed\n");
  162. return -2;
  163. }
  164. //AL_FORMAT_WAVE_EXT does not exist on Mac!"
  165. // alBufferData(mBuffer[id], AL_FORMAT_WAVE_EXT, data, size, freq);
  166. // Idea: Fill Buffer with
  167. // alutLoadMemoryFromFileImage
  168. // (const ALvoid *data, ALsizei length, ALenum *format, ALsizei *size, ALfloat *frequency)
  169. data = alutLoadMemoryFromFileImage(wav, length, &format, &size, &freq);
  170. if (((error = alutGetError()) != ALUT_ERROR_NO_ERROR) || (data == NULL)) {
  171. fprintf(stderr, "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 Sound::play(int source) {
  185. assert(mInit == true);
  186. assert(mSource.size() == mBuffer.size());
  187. assert(source >= 0);
  188. assert(source < (int)mSource.size());
  189. alSourcePlay(mSource[source]);
  190. }
  191. void Sound::stop(int source) {
  192. assert(mInit == true);
  193. assert(mSource.size() == mBuffer.size());
  194. assert(source >= 0);
  195. assert(source < (int)mSource.size());
  196. alSourceStop(mSource[source]);
  197. }