sound_sfx_id.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. Copyright (C) 2004-2005 Michael Liebscher
  3. Copyright (C) 1997-2001 Id Software, Inc.
  4. This program 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. See the
  11. 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. /*
  17. * sound_sfx_id.c:
  18. *
  19. * Author: Michael Liebscher <johnnycanuck@users.sourceforge.net>
  20. * Date: 2004
  21. *
  22. * Acknowledgement:
  23. * This code was derived from Quake II, and was originally
  24. * written by Id Software, Inc.
  25. *
  26. * Acknowledgement:
  27. * Portion of this code was derived from Quake II Evolved.
  28. *
  29. */
  30. #include "../wolfiphone.h"
  31. #define SFX_HASHSIZE 256
  32. #define MAX_SFX 1024
  33. PRIVATE sfx_t *s_sfxHash[ SFX_HASHSIZE ];
  34. PRIVATE sfx_t *s_sfx[ MAX_SFX ];
  35. PRIVATE int s_numSfx;
  36. PRIVATE _boolean s_registering = false;
  37. PRIVATE W32 s_registration_sequence = 0;
  38. void Sound_SoundList_f( void )
  39. {
  40. }
  41. /*
  42. -----------------------------------------------------------------------------
  43. Function: Sound_UploadSound -Upload sound data to buffer.
  44. Parameters: data -[in] Sound data.
  45. sample_size -[in] Sound sample size.
  46. channels -[in] Number of sound channels.
  47. sfx -[in] valid pointer to sfx_t structure.
  48. Returns: Nothing.
  49. Notes:
  50. -----------------------------------------------------------------------------
  51. */
  52. PRIVATE void Sound_UploadSound( W8 *data, int sample_size, int channels, sfx_t *sfx )
  53. {
  54. int size;
  55. // Calculate buffer size
  56. size = sfx->samples * sample_size * channels;
  57. // Set buffer format
  58. if( sample_size == 2 )
  59. {
  60. if( channels == 2 )
  61. {
  62. sfx->format = AL_FORMAT_STEREO16;
  63. }
  64. else
  65. {
  66. sfx->format = AL_FORMAT_MONO16;
  67. }
  68. // Upload the sound
  69. pfalGenBuffers( 1, &sfx->bufferNum );
  70. pfalBufferData( sfx->bufferNum, sfx->format, data, size, sfx->rate );
  71. }
  72. else
  73. {
  74. /*
  75. We upsample the sound to 16 bit here because the iphone
  76. pops at the beginning and end of buffers with 8 bit. -Cass
  77. (Brian Harris wants to be acknowledeged for helping find
  78. this workaround.)
  79. */
  80. short *d = (short *)malloc( size * 2 );
  81. for ( int i = 0; i < size; i++ ) {
  82. d[i] = ((short)data[i] - 128) * 256;
  83. }
  84. if( channels == 2 )
  85. {
  86. sfx->format = AL_FORMAT_STEREO16;
  87. }
  88. else
  89. {
  90. sfx->format = AL_FORMAT_MONO16;
  91. }
  92. // Upload the sound
  93. pfalGenBuffers( 1, &sfx->bufferNum );
  94. pfalBufferData( sfx->bufferNum, sfx->format, d, size * 2, sfx->rate );
  95. free( d );
  96. }
  97. }
  98. /*
  99. -----------------------------------------------------------------------------
  100. Function: Sound_LoadSound -Load sound data.
  101. Parameters: sfx -[in] Pointer to valid sfx_t structure.
  102. Returns: Nothing.
  103. Notes:
  104. -----------------------------------------------------------------------------
  105. */
  106. PUBLIC _boolean Sound_LoadSound( sfx_t *sfx )
  107. {
  108. char name[ MAX_GAMEPATH ];
  109. W8 *data;
  110. soundInfo_t info;
  111. if( sfx->name[ 0 ] == '*' )
  112. {
  113. return false;
  114. }
  115. // See if still in memory
  116. if( sfx->loaded )
  117. {
  118. return true;
  119. }
  120. my_strlcpy( name, sfx->name, sizeof( name ) );
  121. if( ! LoadWavInfo( name, &data, &info ) )
  122. {
  123. if ( ! LoadOggInfo( name, &data, &info ) ) {
  124. sfx->defaulted = true;
  125. Com_Printf( "Could not find sound (%s)\n", name );
  126. return false;
  127. }
  128. }
  129. sfx->loaded = true;
  130. sfx->samples = info.samples;
  131. sfx->rate = info.sample_rate;
  132. Sound_UploadSound( data, info.sample_size, info.channels, sfx );
  133. Z_Free( data );
  134. return true;
  135. }
  136. /*
  137. -----------------------------------------------------------------------------
  138. Function: Sound_FindSound -Load sound data.
  139. Parameters: sfx -[in] Pointer to valid sfx_t structure.
  140. Returns: Nothing.
  141. Notes:
  142. -----------------------------------------------------------------------------
  143. */
  144. PUBLIC sfx_t *Sound_FindSound( const char *name )
  145. {
  146. sfx_t *sfx;
  147. unsigned hashKey;
  148. if( ! name || ! name[ 0 ] )
  149. {
  150. Com_Printf( "Sound_FindSound: NULL sound name\n" );
  151. return NULL;
  152. }
  153. if( strlen( name ) >= MAX_GAMEPATH )
  154. {
  155. Com_Printf( "Sound_FindSound: sound name exceeds MAX_GAMEPATH\n");
  156. return NULL;
  157. }
  158. // See if already loaded
  159. hashKey = (my_strhash( name ) % SFX_HASHSIZE);
  160. for( sfx = s_sfxHash[ hashKey ] ; sfx ; sfx = sfx->nextHash )
  161. {
  162. if( ! my_stricmp( sfx->name, name ) )
  163. {
  164. return sfx;
  165. }
  166. }
  167. // Create a new sfx_t
  168. if( s_numSfx == MAX_SFX )
  169. {
  170. Com_Printf( "Sound_FindSound: MAX_SFX hit\n" );
  171. return NULL;
  172. }
  173. s_sfx[ s_numSfx++ ] = sfx = Z_Malloc( sizeof( sfx_t ) );
  174. my_strlcpy( sfx->name, name, sizeof( sfx->name ) );
  175. // Add to hash table
  176. sfx->nextHash = s_sfxHash[ hashKey ];
  177. s_sfxHash[ hashKey ] = sfx;
  178. return sfx;
  179. }
  180. PUBLIC void Sound_BeginRegistration( void )
  181. {
  182. s_registration_sequence++;
  183. s_registering = true;
  184. }
  185. PUBLIC sfx_t *Sound_RegisterSound( const char *name )
  186. {
  187. sfx_t *sfx;
  188. if( ! sound_initialized )
  189. {
  190. return NULL;
  191. }
  192. if( g_version->value == 1 )
  193. {
  194. char tempname[ 256 ];
  195. my_snprintf( tempname, sizeof( tempname ), "sod%s", name );
  196. sfx = Sound_FindSound( tempname );
  197. }
  198. else
  199. {
  200. sfx = Sound_FindSound( name );
  201. }
  202. if( ! s_registering )
  203. {
  204. Sound_LoadSound( sfx );
  205. }
  206. return sfx;
  207. }
  208. PUBLIC void Sound_EndRegistration( void )
  209. {
  210. s_registering = false;
  211. }
  212. void Sound_FreeSounds( void )
  213. {
  214. sfx_t *sfx;
  215. int i;
  216. // Stop all sounds
  217. Sound_StopAllSounds();
  218. // Free all sounds
  219. for( i = 0 ; i < s_numSfx ; ++i )
  220. {
  221. sfx = s_sfx[ i ];
  222. pfalDeleteBuffers( 1, &sfx->bufferNum );
  223. Z_Free( sfx );
  224. }
  225. memset( s_sfxHash, 0, sizeof( s_sfxHash ) );
  226. memset( s_sfx, 0, sizeof( s_sfx ) );
  227. s_numSfx = 0;
  228. }