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

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