aldlist.cpp

Engine/source/sfx/openal/aldlist.cpp

More...

Detailed Description

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