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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
  2. /*================================================================
  3. *
  4. * Project : OpenRaider
  5. * Author : Mongoose
  6. * Website : http://www.westga.edu/~stu7440/
  7. * Email : stu7440@westga.edu
  8. * Object : Sound
  9. * License : No use w/o permission (C) 2001 Mongoose
  10. * Comments: This is the audio manager for OpenRaider
  11. *
  12. *
  13. * This file was generated using Mongoose's C++
  14. * template generator script. <stu7440@westga.edu>
  15. *
  16. *-- History -------------------------------------------------
  17. *
  18. * 2001.05.23:
  19. * Mongoose - Created
  20. =================================================================*/
  21. #ifdef HAVE_OPENAL
  22. #ifdef __APPLE__
  23. #include <OpenAL/al.h>
  24. #include <OpenAL/alc.h>
  25. #include <AL/alut.h>
  26. #else
  27. # include <AL/al.h>
  28. # include <AL/alc.h>
  29. # include <AL/alut.h>
  30. # include <AL/alext.h>
  31. #endif
  32. #endif
  33. #include <time.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <sys/time.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <fcntl.h>
  40. #include <unistd.h>
  41. #include "Sound.h"
  42. #ifdef DEBUG_MEMEORY
  43. # include "memeory_test.h"
  44. #endif
  45. Sound::Sound()
  46. {
  47. mSource[0] = 0;
  48. mBuffer[0] = 0;
  49. mNextBuffer = 0;
  50. mNextSource = 0;
  51. mInit = false;
  52. }
  53. Sound::~Sound()
  54. {
  55. if (mInit)
  56. {
  57. #ifdef HAVE_OPENAL
  58. alutExit();
  59. #endif
  60. }
  61. }
  62. int Sound::init()
  63. {
  64. #ifndef __APPLE__
  65. int fd;
  66. fd = open("/dev/dsp", O_RDWR);
  67. if (fd < 0)
  68. {
  69. perror("Sound::Init> Could not open /dev/dsp : ");
  70. return -1;
  71. }
  72. close(fd);
  73. #endif
  74. #ifdef HAVE_OPENAL
  75. alutInit(NULL, 0);
  76. mInit = true;
  77. printf("@Created OpenAL Context...\n");
  78. #else
  79. printf("*Couldn't create sound Context...\n");
  80. #endif
  81. return 0;
  82. }
  83. void Sound::listenAt(float pos[3], float angle[3])
  84. {
  85. if (!mInit)
  86. return;
  87. #ifdef HAVE_OPENAL
  88. alListenerfv(AL_POSITION, pos);
  89. alListenerfv(AL_ORIENTATION, angle);
  90. #endif
  91. }
  92. void Sound::sourceAt(int source, float pos[3])
  93. {
  94. if (!mInit || source < 0)
  95. return;
  96. #ifdef HAVE_OPENAL
  97. alSourcefv(mSource[source-1], AL_POSITION, pos);
  98. #endif
  99. }
  100. // Mongoose 2002.01.04, FIXME seperate sourcing and buffering
  101. int Sound::add(char *filename, int *source, unsigned int flags)
  102. {
  103. #ifdef HAVE_OPENAL
  104. ALsizei size;
  105. ALfloat freq;
  106. ALenum format;
  107. ALvoid *data;
  108. #endif
  109. if (!mInit || !filename || !source)
  110. {
  111. printf("Sound::Add> ERROR pre condition assertion failed\n");
  112. return -1000;
  113. }
  114. *source = -1;
  115. #ifdef HAVE_OPENAL
  116. alGetError();
  117. alGenBuffers(1, &mBuffer[mNextBuffer]);
  118. if (alGetError() != AL_NO_ERROR)
  119. {
  120. fprintf(stderr, "Sound::Init> alGenBuffers call failed\n");
  121. return -1;
  122. }
  123. alGetError();
  124. alGenSources(1, &mSource[mNextSource]);
  125. if (alGetError() != AL_NO_ERROR)
  126. {
  127. fprintf(stderr, "Sound::Init> alGenSources call failed\n");
  128. return -2;
  129. }
  130. // err = alutLoadWAV(filename, &data, &format, &size, &bits, &freq);
  131. // is deprecated!
  132. data = alutLoadMemoryFromFile(filename, &format, &size, &freq);
  133. if (alGetError() != AL_NO_ERROR)
  134. {
  135. fprintf(stderr, "Could not load %s\n", filename);
  136. return -3;
  137. }
  138. alBufferData(mBuffer[mNextBuffer], format, data, size, freq);
  139. alSourcei(mSource[mNextSource], AL_BUFFER, mBuffer[mNextBuffer]);
  140. if (flags & SoundFlagsLoop)
  141. {
  142. alSourcei(mSource[mNextSource], AL_LOOPING, 1);
  143. }
  144. ++mNextBuffer;
  145. ++mNextSource;
  146. *source = mNextBuffer;
  147. return 0;
  148. #else
  149. return -1;
  150. #endif
  151. }
  152. int Sound::add(unsigned char *wav, int *source, unsigned int flags)
  153. {
  154. #ifdef HAVE_OPENAL
  155. ALsizei size = 0, freq = 0;
  156. ALvoid *data;
  157. #endif
  158. if (!mInit || !wav || !source)
  159. {
  160. printf("Sound::Add> ERROR pre condition assertion failed\n");
  161. return -1000;
  162. }
  163. *source = -1;
  164. #ifdef HAVE_OPENAL
  165. data = wav;
  166. alGetError();
  167. alGenBuffers(1, &mBuffer[mNextBuffer]);
  168. if (alGetError() != AL_NO_ERROR)
  169. {
  170. fprintf(stderr, "Sound::Init> alGenBuffers call failed\n");
  171. return -1;
  172. }
  173. alGetError();
  174. alGenSources(1, &mSource[mNextSource]);
  175. if (alGetError() != AL_NO_ERROR)
  176. {
  177. fprintf(stderr, "Sound::Init> alGenSources call failed\n");
  178. return -2;
  179. }
  180. #warning "AL_FORMAT_WAVE_EXT does not exist on Mac!"
  181. // alBufferData(mBuffer[mNextBuffer], AL_FORMAT_WAVE_EXT, data, size, freq);
  182. alBufferData(mBuffer[mNextBuffer], 0x10002, data, size, freq);
  183. alSourcei(mSource[mNextSource], AL_BUFFER, mBuffer[mNextBuffer]);
  184. if (flags & SoundFlagsLoop)
  185. {
  186. alSourcei(mSource[mNextSource], AL_LOOPING, 1);
  187. }
  188. ++mNextBuffer;
  189. ++mNextSource;
  190. *source = mNextBuffer;
  191. return 0;
  192. #else
  193. return -1;
  194. #endif
  195. }
  196. void Sound::play(int source)
  197. {
  198. if (!mInit)
  199. {
  200. printf("Sound::Play> ERROR: Sound not initialized\n");
  201. return;
  202. }
  203. if (source < 0)
  204. {
  205. printf("Sound::Play> ERROR: Source Id invalid\n");
  206. return;
  207. }
  208. #ifdef HAVE_OPENAL
  209. alSourcePlay(mSource[source-1]);
  210. #endif
  211. }
  212. void Sound::stop(int source)
  213. {
  214. if (!mInit || source < 0)
  215. {
  216. printf("Sound::Stop> ERROR pre condition assertion failed\n");
  217. return;
  218. }
  219. #ifdef HAVE_OPENAL
  220. alSourceStop(mSource[source-1]);
  221. #endif
  222. }
  223. ///////////////////////////////////////////////////////
  224. #ifdef UNIT_TEST_SOUND
  225. int main(int argc, char* argv[])
  226. {
  227. Sound snd;
  228. FILE *f;
  229. unsigned int l;
  230. unsigned char *buf;
  231. int id, ret;
  232. if (argc > 1)
  233. {
  234. snd.init();
  235. printf("Loading %s\n", argv[1]);
  236. ret = snd.add(argv[1], &id, SoundFlagsNone);
  237. printf("Load returned %i\n", ret);
  238. printf("Playing %u::%s\n", id, argv[1]);
  239. snd.play(id);
  240. printf("Waiting...\n");
  241. sleep(5);
  242. f = fopen(argv[1], "rb");
  243. if (f)
  244. {
  245. fseek(f, 0, SEEK_END);
  246. l = ftell(f);
  247. fseek(f, 0, SEEK_SET);
  248. buf = new unsigned char[l];
  249. fread(buf, l, 1, f);
  250. fclose(f);
  251. printf("Loading buffer of %s\n", argv[1]);
  252. ret = snd.add(buf, &id, SoundFlagsNone);
  253. printf("Load returned %i\n", ret);
  254. printf("Playing buffer of %u::%s\n", id, argv[1]);
  255. snd.play(id);
  256. delete [] buf;
  257. printf("Waiting...\n");
  258. sleep(5);
  259. }
  260. }
  261. else
  262. {
  263. printf("%s filename.wav\n", argv[0]);
  264. }
  265. return 0;
  266. }
  267. #endif