aldlist.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright (c) 2006, Creative Labs Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification, are permitted provided
  6. * that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice, this list of conditions and
  9. * the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
  11. * and the following disclaimer in the documentation and/or other materials provided with the distribution.
  12. * * Neither the name of Creative Labs Inc. nor the names of its contributors may be used to endorse or
  13. * promote products derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
  16. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  17. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  19. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  22. * POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include "core/strings/stringFunctions.h"
  25. #include "aldlist.h"
  26. #if defined(TORQUE_OS_MAC)
  27. #include <OpenAL/alc.h>
  28. #elif defined(TORQUE_OS_LINUX)
  29. #include <AL/alc.h>
  30. #else
  31. #include <al/alc.h>
  32. #endif
  33. /*
  34. * Init call
  35. */
  36. ALDeviceList::ALDeviceList( const OPENALFNTABLE &oalft )
  37. {
  38. VECTOR_SET_ASSOCIATION( vDeviceInfo );
  39. ALDEVICEINFO ALDeviceInfo;
  40. char *devices;
  41. int index;
  42. const char *defaultDeviceName;
  43. const char *actualDeviceName;
  44. dMemcpy( &ALFunction, &oalft, sizeof( OPENALFNTABLE ) );
  45. // DeviceInfo vector stores, for each enumerated device, it's device name, selection status, spec version #, and extension support
  46. vDeviceInfo.clear();
  47. vDeviceInfo.reserve(10);
  48. defaultDeviceIndex = 0;
  49. // grab function pointers for 1.0-API functions, and if successful proceed to enumerate all devices
  50. if (ALFunction.alcIsExtensionPresent(NULL, "ALC_ENUMERATION_EXT")) {
  51. devices = (char *)ALFunction.alcGetString(NULL, ALC_DEVICE_SPECIFIER);
  52. defaultDeviceName = (char *)ALFunction.alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
  53. index = 0;
  54. // go through device list (each device terminated with a single NULL, list terminated with double NULL)
  55. while (*devices != 0) {
  56. if (dStrcmp(defaultDeviceName, devices) == 0) {
  57. defaultDeviceIndex = index;
  58. }
  59. ALCdevice *device = ALFunction.alcOpenDevice(devices);
  60. if (device) {
  61. ALCcontext *context = ALFunction.alcCreateContext(device, NULL);
  62. if (context) {
  63. ALFunction.alcMakeContextCurrent(context);
  64. // if new actual device name isn't already in the list, then add it...
  65. actualDeviceName = ALFunction.alcGetString(device, ALC_DEVICE_SPECIFIER);
  66. bool bNewName = true;
  67. for (int i = 0; i < GetNumDevices(); i++) {
  68. if (dStrcmp(GetDeviceName(i), actualDeviceName) == 0) {
  69. bNewName = false;
  70. }
  71. }
  72. if ((bNewName) && (actualDeviceName != NULL) && (dStrlen(actualDeviceName) > 0)) {
  73. dMemset(&ALDeviceInfo, 0, sizeof(ALDEVICEINFO));
  74. ALDeviceInfo.bSelected = true;
  75. dStrncpy(ALDeviceInfo.strDeviceName, actualDeviceName, sizeof(ALDeviceInfo.strDeviceName));
  76. ALFunction.alcGetIntegerv(device, ALC_MAJOR_VERSION, sizeof(int), &ALDeviceInfo.iMajorVersion);
  77. ALFunction.alcGetIntegerv(device, ALC_MINOR_VERSION, sizeof(int), &ALDeviceInfo.iMinorVersion);
  78. ALDeviceInfo.iCapsFlags = 0;
  79. // Check for ALC Extensions
  80. if (ALFunction.alcIsExtensionPresent(device, "ALC_EXT_CAPTURE") == AL_TRUE)
  81. ALDeviceInfo.iCapsFlags |= SFXALCapture;
  82. if (ALFunction.alcIsExtensionPresent(device, "ALC_EXT_EFX") == AL_TRUE)
  83. ALDeviceInfo.iCapsFlags |= SFXALEFX;
  84. // Check for AL Extensions
  85. if (ALFunction.alIsExtensionPresent("AL_EXT_OFFSET") == AL_TRUE)
  86. ALDeviceInfo.iCapsFlags |= SFXALOffset;
  87. if (ALFunction.alIsExtensionPresent("AL_EXT_LINEAR_DISTANCE") == AL_TRUE)
  88. ALDeviceInfo.iCapsFlags |= SFXALLinearDistance;
  89. if (ALFunction.alIsExtensionPresent("AL_EXT_EXPONENT_DISTANCE") == AL_TRUE)
  90. ALDeviceInfo.iCapsFlags |= SFXALExponentDistance;
  91. if (ALFunction.alIsExtensionPresent("EAX2.0") == AL_TRUE)
  92. ALDeviceInfo.iCapsFlags |= SFXALEAX2;
  93. if (ALFunction.alIsExtensionPresent("EAX3.0") == AL_TRUE)
  94. ALDeviceInfo.iCapsFlags |= SFXALEAX3;
  95. if (ALFunction.alIsExtensionPresent("EAX4.0") == AL_TRUE)
  96. ALDeviceInfo.iCapsFlags |= SFXALEAX4;
  97. if (ALFunction.alIsExtensionPresent("EAX5.0") == AL_TRUE)
  98. ALDeviceInfo.iCapsFlags |= SFXALEAX5;
  99. if (ALFunction.alIsExtensionPresent("EAX-RAM") == AL_TRUE)
  100. ALDeviceInfo.iCapsFlags |= SFXALEAXRAM;
  101. // Get Source Count
  102. ALDeviceInfo.uiSourceCount = GetMaxNumSources();
  103. vDeviceInfo.push_back(ALDeviceInfo);
  104. }
  105. ALFunction.alcMakeContextCurrent(NULL);
  106. ALFunction.alcDestroyContext(context);
  107. }
  108. ALFunction.alcCloseDevice(device);
  109. }
  110. devices += dStrlen(devices) + 1;
  111. index += 1;
  112. }
  113. }
  114. ResetFilters();
  115. }
  116. /*
  117. * Exit call
  118. */
  119. ALDeviceList::~ALDeviceList()
  120. {
  121. }
  122. /*
  123. * Returns the number of devices in the complete device list
  124. */
  125. int ALDeviceList::GetNumDevices()
  126. {
  127. return (int)vDeviceInfo.size();
  128. }
  129. /*
  130. * Returns the device name at an index in the complete device list
  131. */
  132. const char *ALDeviceList::GetDeviceName(int index)
  133. {
  134. if (index < GetNumDevices())
  135. return vDeviceInfo[index].strDeviceName;
  136. else
  137. return NULL;
  138. }
  139. /*
  140. * Returns the major and minor version numbers for a device at a specified index in the complete list
  141. */
  142. void ALDeviceList::GetDeviceVersion(int index, int *major, int *minor)
  143. {
  144. if (index < GetNumDevices()) {
  145. if (major)
  146. *major = vDeviceInfo[index].iMajorVersion;
  147. if (minor)
  148. *minor = vDeviceInfo[index].iMinorVersion;
  149. }
  150. return;
  151. }
  152. /*
  153. * Returns the maximum number of Sources that can be generate on the given device
  154. */
  155. U32 ALDeviceList::GetMaxNumSources(S32 index)
  156. {
  157. if (index < GetNumDevices())
  158. return vDeviceInfo[index].uiSourceCount;
  159. else
  160. return 0;
  161. }
  162. /*
  163. * Checks if the extension is supported on the given device
  164. */
  165. bool ALDeviceList::IsExtensionSupported(int index, SFXALCaps cap)
  166. {
  167. bool bReturn = false;
  168. if (index < GetNumDevices())
  169. bReturn = vDeviceInfo[index].iCapsFlags & cap;
  170. return bReturn;
  171. }
  172. /*
  173. * returns the index of the default device in the complete device list
  174. */
  175. int ALDeviceList::GetDefaultDevice()
  176. {
  177. return defaultDeviceIndex;
  178. }
  179. /*
  180. * Deselects devices which don't have the specified minimum version
  181. */
  182. void ALDeviceList::FilterDevicesMinVer(S32 major, S32 minor)
  183. {
  184. int dMajor, dMinor;
  185. for (U32 i = 0; i < vDeviceInfo.size(); i++) {
  186. GetDeviceVersion(i, &dMajor, &dMinor);
  187. if ((dMajor < major) || ((dMajor == major) && (dMinor < minor))) {
  188. vDeviceInfo[i].bSelected = false;
  189. }
  190. }
  191. }
  192. /*
  193. * Deselects devices which don't have the specified maximum version
  194. */
  195. void ALDeviceList::FilterDevicesMaxVer(S32 major, S32 minor)
  196. {
  197. S32 dMajor, dMinor;
  198. for (U32 i = 0; i < vDeviceInfo.size(); i++) {
  199. GetDeviceVersion(i, &dMajor, &dMinor);
  200. if ((dMajor > major) || ((dMajor == major) && (dMinor > minor))) {
  201. vDeviceInfo[i].bSelected = false;
  202. }
  203. }
  204. }
  205. /*
  206. * Deselects device which don't support the given extension name
  207. */
  208. void ALDeviceList::FilterDevicesExtension(SFXALCaps cap)
  209. {
  210. for (U32 i = 0; i < vDeviceInfo.size(); i++)
  211. vDeviceInfo[i].bSelected = vDeviceInfo[i].iCapsFlags & cap;
  212. }
  213. /*
  214. * Resets all filtering, such that all devices are in the list
  215. */
  216. void ALDeviceList::ResetFilters()
  217. {
  218. for (S32 i = 0; i < GetNumDevices(); i++) {
  219. vDeviceInfo[i].bSelected = true;
  220. }
  221. filterIndex = 0;
  222. }
  223. /*
  224. * Gets index of first filtered device
  225. */
  226. int ALDeviceList::GetFirstFilteredDevice()
  227. {
  228. int i;
  229. for (i = 0; i < GetNumDevices(); i++) {
  230. if (vDeviceInfo[i].bSelected == true) {
  231. break;
  232. }
  233. }
  234. filterIndex = i + 1;
  235. return i;
  236. }
  237. /*
  238. * Gets index of next filtered device
  239. */
  240. int ALDeviceList::GetNextFilteredDevice()
  241. {
  242. int i;
  243. for (i = filterIndex; i < GetNumDevices(); i++) {
  244. if (vDeviceInfo[i].bSelected == true) {
  245. break;
  246. }
  247. }
  248. filterIndex = i + 1;
  249. return i;
  250. }
  251. /*
  252. * Internal function to detemine max number of Sources that can be generated
  253. */
  254. unsigned int ALDeviceList::GetMaxNumSources()
  255. {
  256. ALuint uiSources[256];
  257. U32 iSourceCount = 0;
  258. // Clear AL Error Code
  259. ALFunction.alGetError();
  260. // Generate up to 256 Sources, checking for any errors
  261. for (iSourceCount = 0; iSourceCount < 256; iSourceCount++)
  262. {
  263. ALFunction.alGenSources(1, &uiSources[iSourceCount]);
  264. if (ALFunction.alGetError() != AL_NO_ERROR)
  265. break;
  266. }
  267. // Release the Sources
  268. ALFunction.alDeleteSources(iSourceCount, uiSources);
  269. if (ALFunction.alGetError() != AL_NO_ERROR)
  270. {
  271. for (U32 i = 0; i < 256; i++)
  272. {
  273. ALFunction.alDeleteSources(1, &uiSources[i]);
  274. }
  275. }
  276. return iSourceCount;
  277. }