Open Source Tomb Raider Engine
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Sound.cpp 5.1KB

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 "Sound.h"
  23. Sound::Sound()
  24. {
  25. mSource[0] = 0;
  26. mBuffer[0] = 0;
  27. mNextBuffer = 0;
  28. mNextSource = 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. void Sound::listenAt(float pos[3], float angle[3])
  62. {
  63. if (!mInit)
  64. return;
  65. alListenerfv(AL_POSITION, pos);
  66. alListenerfv(AL_ORIENTATION, angle);
  67. }
  68. void Sound::sourceAt(int source, float pos[3])
  69. {
  70. if (!mInit || source < 0)
  71. return;
  72. alSourcefv(mSource[source-1], AL_POSITION, pos);
  73. }
  74. //! \fixme Seperate sourcing and buffering, Mongoose 2002.01.04
  75. int Sound::addFile(const char *filename, int *source, unsigned int flags)
  76. {
  77. ALsizei size;
  78. ALfloat freq;
  79. ALenum format;
  80. ALvoid *data;
  81. if (!mInit || !filename || !source)
  82. {
  83. printf("Sound::AddFile> ERROR pre condition assertion failed\n");
  84. return -1000;
  85. }
  86. *source = -1;
  87. alGetError();
  88. alGenBuffers(1, &mBuffer[mNextBuffer]);
  89. if (alGetError() != AL_NO_ERROR)
  90. {
  91. fprintf(stderr, "Sound::AddFile> alGenBuffers call failed\n");
  92. return -1;
  93. }
  94. alGetError();
  95. alGenSources(1, &mSource[mNextSource]);
  96. if (alGetError() != AL_NO_ERROR)
  97. {
  98. fprintf(stderr, "Sound::AddFile> alGenSources call failed\n");
  99. return -2;
  100. }
  101. // err = alutLoadWAV(filename, &data, &format, &size, &bits, &freq);
  102. // is deprecated!
  103. data = alutLoadMemoryFromFile(filename, &format, &size, &freq);
  104. if (alutGetError() != ALUT_ERROR_NO_ERROR)
  105. {
  106. fprintf(stderr, "Could not load %s\n", filename);
  107. return -3;
  108. }
  109. alBufferData(mBuffer[mNextBuffer], format, data, size, static_cast<ALsizei>(freq));
  110. alSourcei(mSource[mNextSource], AL_BUFFER, mBuffer[mNextBuffer]);
  111. if (flags & SoundFlagsLoop)
  112. {
  113. alSourcei(mSource[mNextSource], AL_LOOPING, 1);
  114. }
  115. ++mNextBuffer;
  116. ++mNextSource;
  117. *source = mNextBuffer;
  118. return 0;
  119. }
  120. int Sound::addWave(unsigned char *wav, unsigned int length, int *source, unsigned int flags)
  121. {
  122. ALsizei size;
  123. ALfloat freq;
  124. ALenum format;
  125. ALvoid *data;
  126. int error = 0;
  127. if (!mInit || !wav || !source)
  128. {
  129. printf("Sound::AddWave> ERROR pre condition assertion failed\n");
  130. return -1000;
  131. }
  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. if (!mInit)
  173. {
  174. printf("Sound::Play> ERROR: Sound not initialized\n");
  175. return;
  176. }
  177. if (source < 0)
  178. {
  179. printf("Sound::Play> ERROR: Source Id invalid\n");
  180. return;
  181. }
  182. alSourcePlay(mSource[source-1]);
  183. }
  184. void Sound::stop(int source)
  185. {
  186. if (!mInit || source < 0)
  187. {
  188. printf("Sound::Stop> ERROR pre condition assertion failed\n");
  189. return;
  190. }
  191. alSourceStop(mSource[source-1]);
  192. }