audio.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. Copyright (C) 2005, 2010 - Cryptic Sea
  3. This file is part of Gish.
  4. Gish is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. See the GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. void setupaudio(void)
  17. {
  18. int count;
  19. ALsizei size,freq;
  20. ALenum format;
  21. ALvoid *data;
  22. ALboolean loop;
  23. int changeddir;
  24. aldevice=alcOpenDevice(NULL);
  25. if (aldevice!=NULL)
  26. {
  27. alcontext=alcCreateContext(aldevice,NULL);
  28. if (alcontext!=NULL)
  29. {
  30. alcMakeContextCurrent(alcontext);
  31. soundenabled=1;
  32. }
  33. }
  34. if (!soundenabled)
  35. return;
  36. for (count=0;count<2;count++)
  37. alGenBuffers(1,&oggbuffer[count]);
  38. for (count=0;count<30;count++)
  39. alGenBuffers(1,&soundbuffer[count]);
  40. changeddir=chdir("sound");
  41. loadwav(0,"blockbreak.wav");
  42. loadwav(1,"rockhit.wav");
  43. loadwav(2,"fleshhit.wav");
  44. loadwav(3,"ropebreak.wav");
  45. loadwav(4,"chainbreak.wav");
  46. loadwav(5,"gishhit.wav");
  47. loadwav(6,"rockfriction.wav");
  48. loadwav(7,"squish.wav");
  49. loadwav(8,"secrets1.wav");
  50. loadwav(9,"secrets2.wav");
  51. loadwav(10,"amber.wav");
  52. loadwav(11,"nibattack.wav");
  53. loadwav(12,"visattack.wav");
  54. loadwav(13,"bobattack.wav");
  55. loadwav(14,"switch.wav");
  56. loadwav(15,"points.wav");
  57. loadwav(16,"gishhurt.wav");
  58. loadwav(17,"splash.wav");
  59. loadwav(18,"lava.wav");
  60. loadwav(19,"necksnap.wav");
  61. loadwav(20,"tarball.wav");
  62. if (changeddir==0)
  63. chdir("..");
  64. #ifndef DEMO
  65. changeddir=chdir("data");
  66. loadwav(21,"cubemap.dat");
  67. loadwav(22,"specular.dat");
  68. loadwav(23,"stencil.dat");
  69. loadwav(24,"pixel.dat");
  70. if (changeddir==0)
  71. chdir("..");
  72. #endif
  73. alGenSources(1,&oggsource);
  74. for (count=0;count<12;count++)
  75. alGenSources(1,&sound[count].alname);
  76. //alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
  77. }
  78. int updateogg(void)
  79. {
  80. int processed;
  81. int buffernum;
  82. int active;
  83. if (!soundenabled)
  84. return(1);
  85. if (!option.music)
  86. return(1);
  87. active=1;
  88. alGetSourcei(oggsource,AL_BUFFERS_PROCESSED,&processed);
  89. while (processed>0)
  90. {
  91. alSourceUnqueueBuffers(oggsource,1,&buffernum);
  92. active=streamogg(buffernum);
  93. if (active)
  94. alSourceQueueBuffers(oggsource,1,&buffernum);
  95. processed--;
  96. }
  97. if (!active)
  98. {
  99. game.songnum=-1;
  100. /*
  101. if (level.gametype<10)
  102. {
  103. game.songnum++;
  104. if (game.songnum>3)
  105. game.songnum=0;
  106. }
  107. */
  108. }
  109. return(active);
  110. }
  111. int streamogg(int buffernum)
  112. {
  113. int size;
  114. int section;
  115. int result;
  116. int count;
  117. char temp;
  118. size=0;
  119. while (size<OGGBUFFERSIZE)
  120. {
  121. result=ov_read(&oggstream[0],oggdata+size,OGGBUFFERSIZE-size,0,2,1,&section);
  122. if (result>0)
  123. size+=result;
  124. else
  125. goto streamoggbypass;
  126. }
  127. streamoggbypass:;
  128. if (size==0)
  129. return(0);
  130. if (bigendian)
  131. for (count=0;count<size/2;count++)
  132. {
  133. temp=oggdata[count*2];
  134. oggdata[count*2]=oggdata[count*2+1];
  135. oggdata[count*2+1]=temp;
  136. }
  137. alBufferData(buffernum,oggformat,oggdata,size,vorbisinfo->rate);
  138. return(1);
  139. }
  140. void shutdownaudio(void)
  141. {
  142. alcontext=alcGetCurrentContext();
  143. aldevice=alcGetContextsDevice(alcontext);
  144. alcMakeContextCurrent(NULL);
  145. alcDestroyContext(alcontext);
  146. alcCloseDevice(aldevice);
  147. //ov_clear(&oggstream[0]);
  148. }
  149. void loadwav(int buffernum,char *filename)
  150. {
  151. int count;
  152. SDL_AudioSpec wavspec;
  153. unsigned int wavlength;
  154. unsigned char *wavbuffer;
  155. unsigned char temp;
  156. ALenum format;
  157. if (SDL_LoadWAV(filename,&wavspec,&wavbuffer,&wavlength))
  158. {
  159. if (wavspec.channels==1)
  160. {
  161. if (wavspec.format==AUDIO_U8 || wavspec.format==AUDIO_S8)
  162. format=AL_FORMAT_MONO8;
  163. else
  164. format=AL_FORMAT_MONO16;
  165. if (bigendian)
  166. if (format==AL_FORMAT_MONO16)
  167. {
  168. for (count=0;count<wavlength/2;count++)
  169. {
  170. temp=wavbuffer[count*2+0];
  171. wavbuffer[count*2+0]=wavbuffer[count*2+1];
  172. wavbuffer[count*2+1]=temp;
  173. }
  174. }
  175. alBufferData(soundbuffer[buffernum],format,wavbuffer,wavlength,wavspec.freq);
  176. bufferloaded[buffernum]=1;
  177. }
  178. SDL_FreeWAV(wavbuffer);
  179. }
  180. }