openal_main.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. Copyright (C) 2004 Michael Liebscher
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. /*
  16. * openal_main.c: Interface to Sound Device.
  17. *
  18. * Author: Michael Liebscher <johnnycanuck@users.sourceforge.net>
  19. * Date: 2004
  20. *
  21. */
  22. #include "../wolfiphone.h"
  23. #ifdef _WIN32
  24. #define OPENAL_DLL_NAME "openal32.dll"
  25. #elif __unix__
  26. #define OPENAL_DLL_NAME "libopenal.so"
  27. #elif IPHONE
  28. #define OPENAL_DLL_NAME "/System/Library/Frameworks/OpenAL.framework/OpenAL"
  29. #else
  30. #error "Please define OPENAL_DLL_NAME"
  31. #endif
  32. PRIVATE ALCcontext *Context;
  33. PRIVATE ALCdevice *Device;
  34. cvar_t *s_driver;
  35. cvar_t *s_device;
  36. char *deviceList;
  37. char *sound_devices[ 12 ];
  38. W16 numSoundDevices, numDefaultSoundDevice;
  39. /*
  40. -----------------------------------------------------------------------------
  41. Function: Sound_Device_getDeviceList -Get OpenAL device list.
  42. Parameters: Nothing.
  43. Returns: Nothing.
  44. Notes:
  45. -----------------------------------------------------------------------------
  46. */
  47. PRIVATE void Sound_Device_getDeviceList( void )
  48. {
  49. char deviceName[ 256 ];
  50. my_strlcpy( deviceName, s_device->string, sizeof( deviceName ) );
  51. if( pfalcIsExtensionPresent( NULL, (ALubyte*)"ALC_ENUMERATION_EXT") == AL_TRUE )
  52. {
  53. // try out enumeration extension
  54. deviceList = (char *)pfalcGetString( NULL, ALC_DEVICE_SPECIFIER );
  55. for( numSoundDevices = 0 ; numSoundDevices < 12 ; ++numSoundDevices )
  56. {
  57. sound_devices[ numSoundDevices ] = NULL;
  58. }
  59. for( numSoundDevices = 0 ; numSoundDevices < 12 ; ++numSoundDevices )
  60. {
  61. sound_devices[ numSoundDevices ] = deviceList;
  62. if( strcmp( sound_devices[ numSoundDevices ], deviceName ) == 0 )
  63. {
  64. numDefaultSoundDevice = numSoundDevices;
  65. }
  66. deviceList += strlen( deviceList );
  67. if( deviceList[ 0 ] == 0 )
  68. {
  69. if( deviceList[ 1 ] == 0 )
  70. {
  71. break;
  72. }
  73. else
  74. {
  75. deviceList += 1;
  76. }
  77. }
  78. } // End for numSoundDevices = 0 ; numSoundDevices < 12 ; ++numSoundDevices
  79. }
  80. }
  81. /*
  82. -----------------------------------------------------------------------------
  83. Function: Sound_Device_Register -Register OpenAL cvars.
  84. Parameters: Nothing.
  85. Returns: Nothing.
  86. Notes:
  87. -----------------------------------------------------------------------------
  88. */
  89. PRIVATE void Sound_Device_Register( void )
  90. {
  91. s_driver = Cvar_Get( "s_driver", OPENAL_DLL_NAME, CVAR_ARCHIVE );
  92. s_device = Cvar_Get( "s_device", "", CVAR_LATCH | CVAR_ARCHIVE );
  93. }
  94. /*
  95. -----------------------------------------------------------------------------
  96. Function: Sound_Device_Setup -Setup OpenAL sound device.
  97. Parameters: Nothing.
  98. Returns: true on success, otherwise false.
  99. Notes: Call Sound_Device_Shutdown() when you are done.
  100. -----------------------------------------------------------------------------
  101. */
  102. PUBLIC _boolean Sound_Device_Setup( void )
  103. {
  104. Com_Printf( "...Initializing OpenAL subsystem\n" );
  105. Sound_Device_Register();
  106. // Initialize our OpenAL dynamic bindings
  107. if( ! OpenAL_Init( s_driver->string ) )
  108. {
  109. Com_Printf( "[%s]: Dynamic binding of (%s) failed\n", "openal_main.c", s_driver->string );
  110. goto failed;
  111. }
  112. Sound_Device_getDeviceList();
  113. Device = pfalcOpenDevice( (ALCubyte *)( (s_device->string[ 0 ]) ? s_device->string : NULL ) );
  114. if( Device == NULL )
  115. {
  116. Com_Printf( "Failed to Initialize OpenAL\n" );
  117. goto failed;
  118. }
  119. // Create context(s)
  120. Context = pfalcCreateContext( Device, NULL );
  121. if( Context == NULL )
  122. {
  123. Com_Printf( "Failed to initialize OpenAL\n" );
  124. goto failed;
  125. }
  126. // Set active context
  127. pfalcGetError( Device );
  128. pfalcMakeContextCurrent( Context );
  129. if( pfalcGetError( Device ) != ALC_NO_ERROR )
  130. {
  131. Com_Printf( "Failed to Make Context Current\n" );
  132. goto failed;
  133. }
  134. return true;
  135. failed:
  136. OpenAL_Shutdown();
  137. if( Context )
  138. {
  139. pfalcDestroyContext( Context );
  140. Context = NULL;
  141. }
  142. if( Device )
  143. {
  144. pfalcCloseDevice( Device );
  145. Device = NULL;
  146. }
  147. return false;
  148. }
  149. /*
  150. -----------------------------------------------------------------------------
  151. Function: Sound_Device_Shutdown -Shutdown OpenAL sound device.
  152. Parameters: Nothing.
  153. Returns: Nothing.
  154. Notes:
  155. -----------------------------------------------------------------------------
  156. */
  157. PUBLIC void Sound_Device_Shutdown( void )
  158. {
  159. if( Context )
  160. {
  161. pfalcMakeContextCurrent( NULL );
  162. pfalcDestroyContext( Context );
  163. Context = NULL;
  164. }
  165. if( Device )
  166. {
  167. pfalcCloseDevice( Device );
  168. Device = NULL;
  169. }
  170. OpenAL_Shutdown();
  171. }