Avoid probing for devices on lib initialization
[openal-soft.git] / Alc / ALc.c
blob2f998c70dcfb6ac5bf026eb0976fa24b63732c55
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <math.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <memory.h>
27 #include <ctype.h>
28 #include "alMain.h"
29 #include "alSource.h"
30 #include "AL/al.h"
31 #include "AL/alc.h"
32 #include "alThunk.h"
33 #include "alSource.h"
34 #include "alBuffer.h"
35 #include "alExtension.h"
36 #include "alAuxEffectSlot.h"
37 #include "alDatabuffer.h"
38 #include "bs2b.h"
39 #include "alu.h"
42 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
43 typedef struct BackendInfo {
44 const char *name;
45 void (*Init)(BackendFuncs*);
46 void (*Deinit)(void);
47 void (*Probe)(int);
48 BackendFuncs Funcs;
49 } BackendInfo;
50 static BackendInfo BackendList[] = {
51 #ifdef HAVE_PULSEAUDIO
52 { "pulse", alc_pulse_init, alc_pulse_deinit, alc_pulse_probe, EmptyFuncs },
53 #endif
54 #ifdef HAVE_ALSA
55 { "alsa", alc_alsa_init, alc_alsa_deinit, alc_alsa_probe, EmptyFuncs },
56 #endif
57 #ifdef HAVE_OSS
58 { "oss", alc_oss_init, alc_oss_deinit, alc_oss_probe, EmptyFuncs },
59 #endif
60 #ifdef HAVE_SOLARIS
61 { "solaris", alc_solaris_init, alc_solaris_deinit, alc_solaris_probe, EmptyFuncs },
62 #endif
63 #ifdef HAVE_DSOUND
64 { "dsound", alcDSoundInit, alcDSoundDeinit, alcDSoundProbe, EmptyFuncs },
65 #endif
66 #ifdef HAVE_WINMM
67 { "winmm", alcWinMMInit, alcWinMMDeinit, alcWinMMProbe, EmptyFuncs },
68 #endif
69 #ifdef HAVE_PORTAUDIO
70 { "port", alc_pa_init, alc_pa_deinit, alc_pa_probe, EmptyFuncs },
71 #endif
73 { "wave", alc_wave_init, alc_wave_deinit, alc_wave_probe, EmptyFuncs },
75 { NULL, NULL, NULL, NULL, EmptyFuncs }
77 #undef EmptyFuncs
79 ///////////////////////////////////////////////////////
81 #define ALC_EFX_MAJOR_VERSION 0x20001
82 #define ALC_EFX_MINOR_VERSION 0x20002
83 #define ALC_MAX_AUXILIARY_SENDS 0x20003
85 ///////////////////////////////////////////////////////
86 // STRING and EXTENSIONS
88 typedef struct ALCfunction_struct
90 ALCchar *funcName;
91 ALvoid *address;
92 } ALCfunction;
94 static ALCfunction alcFunctions[] = {
95 { "alcCreateContext", (ALvoid *) alcCreateContext },
96 { "alcMakeContextCurrent", (ALvoid *) alcMakeContextCurrent },
97 { "alcProcessContext", (ALvoid *) alcProcessContext },
98 { "alcSuspendContext", (ALvoid *) alcSuspendContext },
99 { "alcDestroyContext", (ALvoid *) alcDestroyContext },
100 { "alcGetCurrentContext", (ALvoid *) alcGetCurrentContext },
101 { "alcGetContextsDevice", (ALvoid *) alcGetContextsDevice },
102 { "alcOpenDevice", (ALvoid *) alcOpenDevice },
103 { "alcCloseDevice", (ALvoid *) alcCloseDevice },
104 { "alcGetError", (ALvoid *) alcGetError },
105 { "alcIsExtensionPresent", (ALvoid *) alcIsExtensionPresent },
106 { "alcGetProcAddress", (ALvoid *) alcGetProcAddress },
107 { "alcGetEnumValue", (ALvoid *) alcGetEnumValue },
108 { "alcGetString", (ALvoid *) alcGetString },
109 { "alcGetIntegerv", (ALvoid *) alcGetIntegerv },
110 { "alcCaptureOpenDevice", (ALvoid *) alcCaptureOpenDevice },
111 { "alcCaptureCloseDevice", (ALvoid *) alcCaptureCloseDevice },
112 { "alcCaptureStart", (ALvoid *) alcCaptureStart },
113 { "alcCaptureStop", (ALvoid *) alcCaptureStop },
114 { "alcCaptureSamples", (ALvoid *) alcCaptureSamples },
116 { "alcMakeCurrent", (ALvoid *) alcMakeCurrent },
117 { "alcGetThreadContext", (ALvoid *) alcGetThreadContext },
119 { NULL, (ALvoid *) NULL }
122 static ALenums enumeration[]={
123 // Types
124 { (ALchar *)"ALC_INVALID", ALC_INVALID },
125 { (ALchar *)"ALC_FALSE", ALC_FALSE },
126 { (ALchar *)"ALC_TRUE", ALC_TRUE },
128 // ALC Properties
129 { (ALchar *)"ALC_MAJOR_VERSION", ALC_MAJOR_VERSION },
130 { (ALchar *)"ALC_MINOR_VERSION", ALC_MINOR_VERSION },
131 { (ALchar *)"ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE },
132 { (ALchar *)"ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES },
133 { (ALchar *)"ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER },
134 { (ALchar *)"ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER },
135 { (ALchar *)"ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER },
136 { (ALchar *)"ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER },
137 { (ALchar *)"ALC_EXTENSIONS", ALC_EXTENSIONS },
138 { (ALchar *)"ALC_FREQUENCY", ALC_FREQUENCY },
139 { (ALchar *)"ALC_REFRESH", ALC_REFRESH },
140 { (ALchar *)"ALC_SYNC", ALC_SYNC },
141 { (ALchar *)"ALC_MONO_SOURCES", ALC_MONO_SOURCES },
142 { (ALchar *)"ALC_STEREO_SOURCES", ALC_STEREO_SOURCES },
143 { (ALchar *)"ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER },
144 { (ALchar *)"ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER},
145 { (ALchar *)"ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES },
147 // EFX Properties
148 { (ALchar *)"ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION },
149 { (ALchar *)"ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION },
150 { (ALchar *)"ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS },
152 // ALC Error Message
153 { (ALchar *)"ALC_NO_ERROR", ALC_NO_ERROR },
154 { (ALchar *)"ALC_INVALID_DEVICE", ALC_INVALID_DEVICE },
155 { (ALchar *)"ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT },
156 { (ALchar *)"ALC_INVALID_ENUM", ALC_INVALID_ENUM },
157 { (ALchar *)"ALC_INVALID_VALUE", ALC_INVALID_VALUE },
158 { (ALchar *)"ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY },
159 { (ALchar *)NULL, (ALenum)0 }
161 // Error strings
162 static const ALCchar alcNoError[] = "No Error";
163 static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
164 static const ALCchar alcErrInvalidContext[] = "Invalid Context";
165 static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
166 static const ALCchar alcErrInvalidValue[] = "Invalid Value";
167 static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
169 /* Device lists. Sizes only include the first ending null character, not the
170 * second */
171 static ALCchar *alcDeviceList;
172 static ALCuint alcDeviceListSize;
173 static ALCchar *alcAllDeviceList;
174 static ALCuint alcAllDeviceListSize;
175 static ALCchar *alcCaptureDeviceList;
176 static ALCuint alcCaptureDeviceListSize;
177 // Default is always the first in the list
178 static ALCchar *alcDefaultDeviceSpecifier;
179 static ALCchar *alcDefaultAllDeviceSpecifier;
180 static ALCchar *alcCaptureDefaultDeviceSpecifier;
183 static ALCchar alcExtensionList[] = "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_disconnect ALC_EXT_EFX ALC_EXTX_thread_local_context";
184 static ALCint alcMajorVersion = 1;
185 static ALCint alcMinorVersion = 1;
187 static ALCint alcEFXMajorVersion = 1;
188 static ALCint alcEFXMinorVersion = 0;
190 ///////////////////////////////////////////////////////
193 ///////////////////////////////////////////////////////
194 // Global Variables
196 static ALCdevice *g_pDeviceList = NULL;
197 static ALCuint g_ulDeviceCount = 0;
199 static CRITICAL_SECTION g_csMutex;
201 // Context List
202 static ALCcontext *g_pContextList = NULL;
203 static ALCuint g_ulContextCount = 0;
205 // Thread-local current context
206 static tls_type LocalContext;
208 // Context Error
209 static ALCenum g_eLastContextError = ALC_NO_ERROR;
211 // Default context extensions
212 static const ALchar alExtList[] =
213 "AL_EXTX_buffer_sub_data AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 "
214 "AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_OFFSET "
215 "AL_EXTX_sample_buffer_object AL_EXT_source_distance_model "
216 "AL_LOKI_quadriphonic";
218 // Mixing Priority Level
219 ALint RTPrioLevel;
221 // Resampler Quality
222 resampler_t DefaultResampler;
224 ///////////////////////////////////////////////////////
227 ///////////////////////////////////////////////////////
228 // ALC Related helper functions
229 #ifdef _WIN32
230 static void alc_init(void);
231 static void alc_deinit(void);
233 BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
235 (void)lpReserved;
237 // Perform actions based on the reason for calling.
238 switch(ul_reason_for_call)
240 case DLL_PROCESS_ATTACH:
241 DisableThreadLibraryCalls(hModule);
242 alc_init();
243 break;
245 case DLL_PROCESS_DETACH:
246 alc_deinit();
247 break;
249 return TRUE;
251 #else
252 #ifdef HAVE_GCC_DESTRUCTOR
253 static void alc_init(void) __attribute__((constructor));
254 static void alc_deinit(void) __attribute__((destructor));
255 #endif
256 #endif
258 static void alc_init(void)
260 int i;
261 const char *devs, *str;
263 InitializeCriticalSection(&g_csMutex);
264 ALTHUNK_INIT();
265 ReadALConfig();
267 tls_create(&LocalContext);
269 RTPrioLevel = GetConfigValueInt(NULL, "rt-prio", 0);
271 DefaultResampler = GetConfigValueInt(NULL, "resampler", RESAMPLER_DEFAULT);
272 if(DefaultResampler >= RESAMPLER_MAX || DefaultResampler <= RESAMPLER_MIN)
273 DefaultResampler = RESAMPLER_DEFAULT;
275 devs = GetConfigValue(NULL, "drivers", "");
276 if(devs[0])
278 int n;
279 size_t len;
280 const char *next = devs;
281 int endlist;
283 i = 0;
284 do {
285 devs = next;
286 next = strchr(devs, ',');
288 if(!devs[0] || devs[0] == ',')
290 endlist = 0;
291 continue;
293 endlist = 1;
295 len = (next ? ((size_t)(next-devs)) : strlen(devs));
296 for(n = i;BackendList[n].Init;n++)
298 if(len == strlen(BackendList[n].name) &&
299 strncmp(BackendList[n].name, devs, len) == 0)
301 BackendInfo Bkp = BackendList[n];
302 while(n > i)
304 BackendList[n] = BackendList[n-1];
305 --n;
307 BackendList[n] = Bkp;
309 i++;
310 break;
313 } while(next++);
315 if(endlist)
317 BackendList[i].name = NULL;
318 BackendList[i].Init = NULL;
319 BackendList[i].Deinit = NULL;
320 BackendList[i].Probe = NULL;
324 for(i = 0;BackendList[i].Init;i++)
325 BackendList[i].Init(&BackendList[i].Funcs);
327 DuplicateStereo = GetConfigValueBool(NULL, "stereodup", 0);
329 str = GetConfigValue(NULL, "excludefx", "");
330 if(str[0])
332 const struct {
333 const char *name;
334 int type;
335 } EffectList[] = {
336 { "eaxreverb", EAXREVERB },
337 { "reverb", REVERB },
338 { "echo", ECHO },
339 { NULL, 0 }
341 int n;
342 size_t len;
343 const char *next = str;
345 do {
346 str = next;
347 next = strchr(str, ',');
349 if(!str[0] || next == str)
350 continue;
352 len = (next ? ((size_t)(next-str)) : strlen(str));
353 for(n = 0;EffectList[n].name;n++)
355 if(len == strlen(EffectList[n].name) &&
356 strncmp(EffectList[n].name, str, len) == 0)
357 DisabledEffects[EffectList[n].type] = AL_TRUE;
359 } while(next++);
363 static void alc_deinit(void)
365 int i;
367 ReleaseALC();
369 for(i = 0;BackendList[i].Deinit;i++)
370 BackendList[i].Deinit();
372 tls_delete(LocalContext);
374 FreeALConfig();
375 ALTHUNK_EXIT();
376 DeleteCriticalSection(&g_csMutex);
380 static void ProbeDeviceList()
382 ALint i;
384 free(alcDeviceList); alcDeviceList = NULL;
385 alcDeviceListSize = 0;
387 for(i = 0;BackendList[i].Probe;i++)
388 BackendList[i].Probe(DEVICE_PROBE);
391 static void ProbeAllDeviceList()
393 ALint i;
395 free(alcAllDeviceList); alcAllDeviceList = NULL;
396 alcAllDeviceListSize = 0;
398 for(i = 0;BackendList[i].Probe;i++)
399 BackendList[i].Probe(ALL_DEVICE_PROBE);
402 static void ProbeCaptureDeviceList()
404 ALint i;
406 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
407 alcCaptureDeviceListSize = 0;
409 for(i = 0;BackendList[i].Probe;i++)
410 BackendList[i].Probe(CAPTURE_DEVICE_PROBE);
414 #define DECL_APPEND_LIST_FUNC(type) \
415 void Append##type##List(const ALCchar *name) \
417 ALCuint len = strlen(name); \
418 void *temp; \
420 if(len == 0) \
421 return; \
423 temp = realloc(alc##type##List, alc##type##ListSize + len + 2); \
424 if(!temp) \
426 AL_PRINT("Realloc failed to add %s!\n", name); \
427 return; \
429 alc##type##List = temp; \
430 sprintf(alc##type##List+alc##type##ListSize, "%s", name); \
431 alc##type##ListSize += len+1; \
432 alc##type##List[alc##type##ListSize] = 0; \
435 DECL_APPEND_LIST_FUNC(Device)
436 DECL_APPEND_LIST_FUNC(AllDevice)
437 DECL_APPEND_LIST_FUNC(CaptureDevice)
440 void al_print(const char *fname, unsigned int line, const char *fmt, ...)
442 const char *fn;
443 char str[256];
444 int i;
446 fn = strrchr(fname, '/');
447 if(!fn) fn = strrchr(fname, '\\');;
448 if(!fn) fn = fname;
449 else fn += 1;
451 i = snprintf(str, sizeof(str), "AL lib: %s:%d: ", fn, line);
452 if(i < (int)sizeof(str) && i > 0)
454 va_list ap;
455 va_start(ap, fmt);
456 vsnprintf(str+i, sizeof(str)-i, fmt, ap);
457 va_end(ap);
459 str[sizeof(str)-1] = 0;
461 fprintf(stderr, "%s", str);
464 void EnableRTPrio(ALint level)
466 ALboolean failed;
468 #ifdef _WIN32
469 if(level > 0)
470 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
471 else
472 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
473 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM)
474 struct sched_param param;
476 if(level > 0)
478 /* Use the minimum real-time priority possible for now (on Linux this
479 * should be 1 for SCHED_RR) */
480 param.sched_priority = sched_get_priority_min(SCHED_RR);
481 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
483 else
485 param.sched_priority = 0;
486 failed = !!pthread_setschedparam(pthread_self(), SCHED_OTHER, &param);
488 #else
489 /* Real-time priority not available */
490 failed = !!level;
491 #endif
492 if(failed)
493 AL_PRINT("Failed to set priority level for thread\n");
498 IsDevice
500 Check pDevice is a valid Device pointer
502 static ALCboolean IsDevice(ALCdevice *pDevice)
504 ALCdevice *pTempDevice;
506 SuspendContext(NULL);
508 pTempDevice = g_pDeviceList;
509 while(pTempDevice && pTempDevice != pDevice)
510 pTempDevice = pTempDevice->next;
512 ProcessContext(NULL);
514 return (pTempDevice ? ALC_TRUE : ALC_FALSE);
518 IsContext
520 Check pContext is a valid Context pointer
522 static ALCboolean IsContext(ALCcontext *pContext)
524 ALCcontext *pTempContext;
526 SuspendContext(NULL);
528 pTempContext = g_pContextList;
529 while (pTempContext && pTempContext != pContext)
530 pTempContext = pTempContext->next;
532 ProcessContext(NULL);
534 return (pTempContext ? ALC_TRUE : ALC_FALSE);
539 alcSetError
541 Store latest ALC Error
543 ALCvoid alcSetError(ALCdevice *device, ALenum errorCode)
545 if(IsDevice(device))
546 device->LastError = errorCode;
547 else
548 g_eLastContextError = errorCode;
553 SuspendContext
555 Thread-safe entry
557 ALCvoid SuspendContext(ALCcontext *pContext)
559 (void)pContext;
560 EnterCriticalSection(&g_csMutex);
565 ProcessContext
567 Thread-safe exit
569 ALCvoid ProcessContext(ALCcontext *pContext)
571 (void)pContext;
572 LeaveCriticalSection(&g_csMutex);
577 GetContextSuspended
579 Returns the currently active Context, in a locked state
581 ALCcontext *GetContextSuspended(void)
583 ALCcontext *pContext = NULL;
585 SuspendContext(NULL);
587 pContext = tls_get(LocalContext);
588 if(pContext && !IsContext(pContext))
590 tls_set(LocalContext, NULL);
591 pContext = NULL;
593 if(!pContext)
595 pContext = g_pContextList;
596 while(pContext && !pContext->InUse)
597 pContext = pContext->next;
599 if(pContext)
600 SuspendContext(pContext);
602 ProcessContext(NULL);
604 return pContext;
609 InitContext
611 Initialize Context variables
613 static ALvoid InitContext(ALCcontext *pContext)
615 //Initialise listener
616 pContext->Listener.Gain = 1.0f;
617 pContext->Listener.MetersPerUnit = 1.0f;
618 pContext->Listener.Position[0] = 0.0f;
619 pContext->Listener.Position[1] = 0.0f;
620 pContext->Listener.Position[2] = 0.0f;
621 pContext->Listener.Velocity[0] = 0.0f;
622 pContext->Listener.Velocity[1] = 0.0f;
623 pContext->Listener.Velocity[2] = 0.0f;
624 pContext->Listener.Forward[0] = 0.0f;
625 pContext->Listener.Forward[1] = 0.0f;
626 pContext->Listener.Forward[2] = -1.0f;
627 pContext->Listener.Up[0] = 0.0f;
628 pContext->Listener.Up[1] = 1.0f;
629 pContext->Listener.Up[2] = 0.0f;
631 //Validate pContext
632 pContext->LastError = AL_NO_ERROR;
633 pContext->InUse = AL_FALSE;
634 pContext->Suspended = AL_FALSE;
636 //Set globals
637 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
638 pContext->SourceDistanceModel = AL_FALSE;
639 pContext->DopplerFactor = 1.0f;
640 pContext->DopplerVelocity = 1.0f;
641 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
643 pContext->ExtensionList = alExtList;
648 ExitContext
650 Clean up Context, destroy any remaining Sources
652 static ALCvoid ExitContext(ALCcontext *pContext)
654 //Invalidate context
655 pContext->LastError = AL_NO_ERROR;
656 pContext->InUse = AL_FALSE;
659 ///////////////////////////////////////////////////////
662 ///////////////////////////////////////////////////////
663 // ALC Functions calls
666 // This should probably move to another c file but for now ...
667 ALCAPI ALCdevice* ALCAPIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
669 ALCboolean DeviceFound = ALC_FALSE;
670 ALCdevice *pDevice = NULL;
671 ALCint i;
673 if(SampleSize <= 0)
675 alcSetError(NULL, ALC_INVALID_VALUE);
676 return NULL;
679 if(deviceName && !deviceName[0])
680 deviceName = NULL;
682 if(!alcCaptureDeviceList)
683 ProbeCaptureDeviceList();
685 pDevice = malloc(sizeof(ALCdevice));
686 if (pDevice)
688 //Initialise device structure
689 memset(pDevice, 0, sizeof(ALCdevice));
691 //Validate device
692 pDevice->Connected = ALC_TRUE;
693 pDevice->IsCaptureDevice = AL_TRUE;
695 pDevice->szDeviceName = NULL;
697 pDevice->Frequency = frequency;
698 pDevice->Format = format;
699 pDevice->UpdateSize = SampleSize;
700 pDevice->NumUpdates = 1;
702 SuspendContext(NULL);
703 for(i = 0;BackendList[i].Init;i++)
705 pDevice->Funcs = &BackendList[i].Funcs;
706 if(ALCdevice_OpenCapture(pDevice, deviceName))
708 pDevice->next = g_pDeviceList;
709 g_pDeviceList = pDevice;
710 g_ulDeviceCount++;
712 DeviceFound = ALC_TRUE;
713 break;
716 ProcessContext(NULL);
718 if(!DeviceFound)
720 alcSetError(NULL, ALC_INVALID_VALUE);
721 free(pDevice);
722 pDevice = NULL;
725 else
726 alcSetError(NULL, ALC_OUT_OF_MEMORY);
728 return pDevice;
731 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
733 ALCboolean bReturn = ALC_FALSE;
734 ALCdevice **list;
736 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
738 SuspendContext(NULL);
740 list = &g_pDeviceList;
741 while(*list != pDevice)
742 list = &(*list)->next;
744 *list = (*list)->next;
745 g_ulDeviceCount--;
747 ProcessContext(NULL);
749 ALCdevice_CloseCapture(pDevice);
751 free(pDevice->szDeviceName);
752 pDevice->szDeviceName = NULL;
754 free(pDevice);
756 bReturn = ALC_TRUE;
758 else
759 alcSetError(pDevice, ALC_INVALID_DEVICE);
761 return bReturn;
764 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
766 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
767 ALCdevice_StartCapture(pDevice);
768 else
769 alcSetError(pDevice, ALC_INVALID_DEVICE);
772 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
774 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
775 ALCdevice_StopCapture(pDevice);
776 else
777 alcSetError(pDevice, ALC_INVALID_DEVICE);
780 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
782 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
783 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
784 else
785 alcSetError(pDevice, ALC_INVALID_DEVICE);
789 alcGetError
791 Return last ALC generated error code
793 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
795 ALCenum errorCode = ALC_NO_ERROR;
797 if(IsDevice(device))
799 errorCode = device->LastError;
800 device->LastError = ALC_NO_ERROR;
802 else
804 errorCode = g_eLastContextError;
805 g_eLastContextError = ALC_NO_ERROR;
807 return errorCode;
812 alcSuspendContext
814 Not functional
816 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
818 SuspendContext(NULL);
819 if(IsContext(pContext))
820 pContext->Suspended = AL_TRUE;
821 ProcessContext(NULL);
826 alcProcessContext
828 Not functional
830 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
832 SuspendContext(NULL);
833 if(IsContext(pContext))
834 pContext->Suspended = AL_FALSE;
835 ProcessContext(NULL);
840 alcGetString
842 Returns information about the Device, and error strings
844 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
846 const ALCchar *value = NULL;
848 switch (param)
850 case ALC_NO_ERROR:
851 value = alcNoError;
852 break;
854 case ALC_INVALID_ENUM:
855 value = alcErrInvalidEnum;
856 break;
858 case ALC_INVALID_VALUE:
859 value = alcErrInvalidValue;
860 break;
862 case ALC_INVALID_DEVICE:
863 value = alcErrInvalidDevice;
864 break;
866 case ALC_INVALID_CONTEXT:
867 value = alcErrInvalidContext;
868 break;
870 case ALC_OUT_OF_MEMORY:
871 value = alcErrOutOfMemory;
872 break;
874 case ALC_DEVICE_SPECIFIER:
875 if(IsDevice(pDevice))
876 value = pDevice->szDeviceName;
877 else
879 ProbeDeviceList();
880 value = alcDeviceList;
882 break;
884 case ALC_ALL_DEVICES_SPECIFIER:
885 ProbeAllDeviceList();
886 value = alcAllDeviceList;
887 break;
889 case ALC_CAPTURE_DEVICE_SPECIFIER:
890 if(IsDevice(pDevice))
891 value = pDevice->szDeviceName;
892 else
894 ProbeCaptureDeviceList();
895 value = alcCaptureDeviceList;
897 break;
899 /* Default devices are always first in the list */
900 case ALC_DEFAULT_DEVICE_SPECIFIER:
901 if(!alcDeviceList)
902 ProbeDeviceList();
904 free(alcDefaultDeviceSpecifier);
905 alcDefaultDeviceSpecifier = strdup(alcDeviceList ? alcDeviceList : "");
906 if(!alcDefaultDeviceSpecifier)
907 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
908 value = alcDefaultDeviceSpecifier;
909 break;
911 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
912 if(!alcAllDeviceList)
913 ProbeAllDeviceList();
915 free(alcDefaultAllDeviceSpecifier);
916 alcDefaultAllDeviceSpecifier = strdup(alcAllDeviceList ?
917 alcAllDeviceList : "");
918 if(!alcDefaultAllDeviceSpecifier)
919 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
920 value = alcDefaultAllDeviceSpecifier;
921 break;
923 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
924 if(!alcCaptureDeviceList)
925 ProbeCaptureDeviceList();
927 free(alcCaptureDefaultDeviceSpecifier);
928 alcCaptureDefaultDeviceSpecifier = strdup(alcCaptureDeviceList ?
929 alcCaptureDeviceList : "");
930 if(!alcCaptureDefaultDeviceSpecifier)
931 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
932 value = alcCaptureDefaultDeviceSpecifier;
933 break;
935 case ALC_EXTENSIONS:
936 value = alcExtensionList;
937 break;
939 default:
940 alcSetError(pDevice, ALC_INVALID_ENUM);
941 break;
944 return value;
949 alcGetIntegerv
951 Returns information about the Device and the version of Open AL
953 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
955 if(size == 0 || data == NULL)
957 alcSetError(device, ALC_INVALID_VALUE);
958 return;
961 if(IsDevice(device) && device->IsCaptureDevice)
963 SuspendContext(NULL);
965 // Capture device
966 switch (param)
968 case ALC_CAPTURE_SAMPLES:
969 *data = ALCdevice_AvailableSamples(device);
970 break;
972 case ALC_CONNECTED:
973 *data = device->Connected;
974 break;
976 default:
977 alcSetError(device, ALC_INVALID_ENUM);
978 break;
981 ProcessContext(NULL);
982 return;
985 // Playback Device
986 switch (param)
988 case ALC_MAJOR_VERSION:
989 *data = alcMajorVersion;
990 break;
992 case ALC_MINOR_VERSION:
993 *data = alcMinorVersion;
994 break;
996 case ALC_EFX_MAJOR_VERSION:
997 *data = alcEFXMajorVersion;
998 break;
1000 case ALC_EFX_MINOR_VERSION:
1001 *data = alcEFXMinorVersion;
1002 break;
1004 case ALC_MAX_AUXILIARY_SENDS:
1005 if(!IsDevice(device))
1006 alcSetError(device, ALC_INVALID_DEVICE);
1007 else
1008 *data = device->NumAuxSends;
1009 break;
1011 case ALC_ATTRIBUTES_SIZE:
1012 if(!IsDevice(device))
1013 alcSetError(device, ALC_INVALID_DEVICE);
1014 else
1015 *data = 13;
1016 break;
1018 case ALC_ALL_ATTRIBUTES:
1019 if(!IsDevice(device))
1020 alcSetError(device, ALC_INVALID_DEVICE);
1021 else if (size < 13)
1022 alcSetError(device, ALC_INVALID_VALUE);
1023 else
1025 int i = 0;
1027 SuspendContext(NULL);
1028 data[i++] = ALC_FREQUENCY;
1029 data[i++] = device->Frequency;
1031 data[i++] = ALC_REFRESH;
1032 data[i++] = device->Frequency / device->UpdateSize;
1034 data[i++] = ALC_SYNC;
1035 data[i++] = ALC_FALSE;
1037 data[i++] = ALC_MONO_SOURCES;
1038 data[i++] = device->lNumMonoSources;
1040 data[i++] = ALC_STEREO_SOURCES;
1041 data[i++] = device->lNumStereoSources;
1043 data[i++] = ALC_MAX_AUXILIARY_SENDS;
1044 data[i++] = device->NumAuxSends;
1046 data[i++] = 0;
1047 ProcessContext(NULL);
1049 break;
1051 case ALC_FREQUENCY:
1052 if(!IsDevice(device))
1053 alcSetError(device, ALC_INVALID_DEVICE);
1054 else
1055 *data = device->Frequency;
1056 break;
1058 case ALC_REFRESH:
1059 if(!IsDevice(device))
1060 alcSetError(device, ALC_INVALID_DEVICE);
1061 else
1062 *data = device->Frequency / device->UpdateSize;
1063 break;
1065 case ALC_SYNC:
1066 if(!IsDevice(device))
1067 alcSetError(device, ALC_INVALID_DEVICE);
1068 else
1069 *data = ALC_FALSE;
1070 break;
1072 case ALC_MONO_SOURCES:
1073 if(!IsDevice(device))
1074 alcSetError(device, ALC_INVALID_DEVICE);
1075 else
1076 *data = device->lNumMonoSources;
1077 break;
1079 case ALC_STEREO_SOURCES:
1080 if(!IsDevice(device))
1081 alcSetError(device, ALC_INVALID_DEVICE);
1082 else
1083 *data = device->lNumStereoSources;
1084 break;
1086 case ALC_CONNECTED:
1087 if(!IsDevice(device))
1088 alcSetError(device, ALC_INVALID_DEVICE);
1089 else
1090 *data = device->Connected;
1091 break;
1093 default:
1094 alcSetError(device, ALC_INVALID_ENUM);
1095 break;
1101 alcIsExtensionPresent
1103 Determines if there is support for a particular extension
1105 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
1107 ALCboolean bResult = ALC_FALSE;
1109 if (extName)
1111 const char *ptr;
1112 size_t len;
1114 len = strlen(extName);
1115 ptr = alcExtensionList;
1116 while(ptr && *ptr)
1118 if(strncasecmp(ptr, extName, len) == 0 &&
1119 (ptr[len] == '\0' || isspace(ptr[len])))
1121 bResult = ALC_TRUE;
1122 break;
1124 if((ptr=strchr(ptr, ' ')) != NULL)
1126 do {
1127 ++ptr;
1128 } while(isspace(*ptr));
1132 else
1133 alcSetError(device, ALC_INVALID_VALUE);
1135 return bResult;
1140 alcGetProcAddress
1142 Retrieves the function address for a particular extension function
1144 ALCAPI ALCvoid * ALCAPIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
1146 ALCvoid *pFunction = NULL;
1147 ALsizei i = 0;
1149 if (funcName)
1151 while(alcFunctions[i].funcName &&
1152 strcmp(alcFunctions[i].funcName,funcName) != 0)
1153 i++;
1154 pFunction = alcFunctions[i].address;
1156 else
1157 alcSetError(device, ALC_INVALID_VALUE);
1159 return pFunction;
1164 alcGetEnumValue
1166 Get the value for a particular ALC Enumerated Value
1168 ALCAPI ALCenum ALCAPIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
1170 ALsizei i = 0;
1171 ALCenum val;
1173 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
1174 i++;
1175 val = enumeration[i].value;
1177 if(!enumeration[i].enumName)
1178 alcSetError(device, ALC_INVALID_VALUE);
1180 return val;
1185 alcCreateContext
1187 Create and attach a Context to a particular Device.
1189 ALCAPI ALCcontext* ALCAPIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
1191 ALuint attrIdx, reqStereoSources;
1192 ALCcontext *ALContext;
1193 void *temp;
1194 ALuint i;
1196 SuspendContext(NULL);
1198 if(!IsDevice(device) || device->IsCaptureDevice || !device->Connected)
1200 alcSetError(device, ALC_INVALID_DEVICE);
1201 ProcessContext(NULL);
1202 return NULL;
1205 // Reset Context Last Error code
1206 device->LastError = ALC_NO_ERROR;
1208 // If a context is already running on the device, stop playback so the
1209 // device attributes can be updated
1210 if(device->NumContexts > 0)
1212 ProcessContext(NULL);
1213 ALCdevice_StopPlayback(device);
1214 SuspendContext(NULL);
1217 // Check for attributes
1218 if(attrList)
1220 ALCint level = device->Bs2bLevel;
1221 ALCuint freq = device->Frequency;
1222 ALCint numMono = device->lNumMonoSources;
1223 ALCint numStereo = device->lNumStereoSources;
1224 ALCuint numSends = device->NumAuxSends;
1226 attrIdx = 0;
1227 while(attrList[attrIdx])
1229 if(attrList[attrIdx] == ALC_FREQUENCY &&
1230 !ConfigValueExists(NULL, "frequency"))
1232 freq = attrList[attrIdx + 1];
1233 if(freq < 8000)
1234 freq = 8000;
1237 if(attrList[attrIdx] == ALC_STEREO_SOURCES)
1239 reqStereoSources = attrList[attrIdx + 1];
1240 if(reqStereoSources > device->MaxNoOfSources)
1241 reqStereoSources = device->MaxNoOfSources;
1243 numStereo = reqStereoSources;
1244 numMono = device->MaxNoOfSources - numStereo;
1247 if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS &&
1248 !ConfigValueExists(NULL, "sends"))
1250 numSends = attrList[attrIdx + 1];
1251 if(numSends > MAX_SENDS)
1252 numSends = MAX_SENDS;
1255 attrIdx += 2;
1258 device->Bs2bLevel = level;
1259 device->Frequency = freq;
1260 device->lNumMonoSources = numMono;
1261 device->lNumStereoSources = numStereo;
1262 device->NumAuxSends = numSends;
1265 if(ALCdevice_ResetPlayback(device) == ALC_FALSE)
1267 alcSetError(device, ALC_INVALID_DEVICE);
1268 aluHandleDisconnect(device);
1269 ProcessContext(NULL);
1270 return NULL;
1273 for(i = 0;i < device->NumContexts;i++)
1275 ALCcontext *context = device->Contexts[i];
1276 ALeffectslot *slot;
1277 ALsource *source;
1279 SuspendContext(context);
1280 for(slot = context->AuxiliaryEffectSlot;slot != NULL;slot = slot->next)
1282 if(!slot->EffectState)
1283 continue;
1285 if(ALEffect_DeviceUpdate(slot->EffectState, device) == AL_FALSE)
1287 alcSetError(device, ALC_INVALID_DEVICE);
1288 aluHandleDisconnect(device);
1289 ProcessContext(context);
1290 ProcessContext(NULL);
1291 ALCdevice_StopPlayback(device);
1292 return NULL;
1294 ALEffect_Update(slot->EffectState, context, &slot->effect);
1297 for(source = context->Source;source != NULL;source = source->next)
1299 ALuint s = device->NumAuxSends;
1300 while(s < MAX_SENDS)
1302 if(source->Send[s].Slot)
1303 source->Send[s].Slot->refcount--;
1304 source->Send[s].Slot = NULL;
1305 source->Send[s].WetFilter.type = 0;
1306 source->Send[s].WetFilter.filter = 0;
1307 s++;
1309 source->NeedsUpdate = AL_TRUE;
1311 aluInitPanning(context);
1312 ProcessContext(context);
1315 if(device->Bs2bLevel > 0 && device->Bs2bLevel <= 6)
1317 if(!device->Bs2b)
1319 device->Bs2b = calloc(1, sizeof(*device->Bs2b));
1320 bs2b_clear(device->Bs2b);
1322 bs2b_set_srate(device->Bs2b, device->Frequency);
1323 bs2b_set_level(device->Bs2b, device->Bs2bLevel);
1325 else
1327 free(device->Bs2b);
1328 device->Bs2b = NULL;
1331 temp = realloc(device->Contexts, (device->NumContexts+1) * sizeof(*device->Contexts));
1332 if(!temp)
1334 alcSetError(device, ALC_OUT_OF_MEMORY);
1335 ProcessContext(NULL);
1336 return NULL;
1338 device->Contexts = temp;
1340 ALContext = calloc(1, sizeof(ALCcontext));
1341 if(!ALContext)
1343 alcSetError(device, ALC_OUT_OF_MEMORY);
1344 ProcessContext(NULL);
1345 return NULL;
1348 device->Contexts[device->NumContexts++] = ALContext;
1349 ALContext->Device = device;
1351 InitContext(ALContext);
1352 aluInitPanning(ALContext);
1354 ALContext->next = g_pContextList;
1355 g_pContextList = ALContext;
1356 g_ulContextCount++;
1358 ProcessContext(NULL);
1360 return ALContext;
1365 alcDestroyContext
1367 Remove a Context
1369 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
1371 ALCcontext **list;
1372 ALuint i;
1374 if (IsContext(context))
1376 ALCdevice *Device = context->Device;
1378 if(Device->NumContexts == 1)
1379 ALCdevice_StopPlayback(Device);
1381 SuspendContext(NULL);
1383 for(i = 0;i < Device->NumContexts-1;i++)
1385 if(Device->Contexts[i] == context)
1387 memmove(&Device->Contexts[i], &Device->Contexts[i+1],
1388 (Device->NumContexts-i-1) * sizeof(*Device->Contexts));
1389 break;
1392 Device->NumContexts--;
1394 // Lock context
1395 SuspendContext(context);
1397 if(context->SourceCount > 0)
1399 #ifdef _DEBUG
1400 AL_PRINT("alcDestroyContext(): deleting %d Source(s)\n", context->SourceCount);
1401 #endif
1402 ReleaseALSources(context);
1404 if(context->AuxiliaryEffectSlotCount > 0)
1406 #ifdef _DEBUG
1407 AL_PRINT("alcDestroyContext(): deleting %d AuxiliaryEffectSlot(s)\n", context->AuxiliaryEffectSlotCount);
1408 #endif
1409 ReleaseALAuxiliaryEffectSlots(context);
1412 list = &g_pContextList;
1413 while(*list != context)
1414 list = &(*list)->next;
1416 *list = (*list)->next;
1417 g_ulContextCount--;
1419 // Unlock context
1420 ProcessContext(context);
1421 ProcessContext(NULL);
1423 ExitContext(context);
1425 // Free memory (MUST do this after ProcessContext)
1426 memset(context, 0, sizeof(ALCcontext));
1427 free(context);
1429 else
1430 alcSetError(NULL, ALC_INVALID_CONTEXT);
1435 alcGetCurrentContext
1437 Returns the currently active Context
1439 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1441 ALCcontext *pContext;
1443 if((pContext=GetContextSuspended()) != NULL)
1444 ProcessContext(pContext);
1446 return pContext;
1450 alcGetThreadContext
1452 Returns the currently active thread-local Context
1454 ALCcontext * ALCAPIENTRY alcGetThreadContext(void)
1456 ALCcontext *pContext = NULL;
1458 SuspendContext(NULL);
1460 pContext = tls_get(LocalContext);
1461 if(pContext && !IsContext(pContext))
1463 tls_set(LocalContext, NULL);
1464 pContext = NULL;
1467 ProcessContext(NULL);
1469 return pContext;
1474 alcGetContextsDevice
1476 Returns the Device that a particular Context is attached to
1478 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1480 ALCdevice *pDevice = NULL;
1482 SuspendContext(NULL);
1483 if (IsContext(pContext))
1484 pDevice = pContext->Device;
1485 else
1486 alcSetError(NULL, ALC_INVALID_CONTEXT);
1487 ProcessContext(NULL);
1489 return pDevice;
1494 alcMakeContextCurrent
1496 Makes the given Context the active Context
1498 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1500 ALCcontext *ALContext;
1501 ALboolean bReturn = AL_TRUE;
1503 SuspendContext(NULL);
1505 // context must be a valid Context or NULL
1506 if(context == NULL || IsContext(context))
1508 ALContext = g_pContextList;
1509 while(ALContext && !ALContext->InUse)
1510 ALContext = ALContext->next;
1512 if(ALContext != NULL)
1514 SuspendContext(ALContext);
1515 ALContext->InUse=AL_FALSE;
1516 ProcessContext(ALContext);
1519 if((ALContext=context) != NULL && ALContext->Device)
1521 SuspendContext(ALContext);
1522 ALContext->InUse=AL_TRUE;
1523 ProcessContext(ALContext);
1526 tls_set(LocalContext, NULL);
1528 else
1530 alcSetError(NULL, ALC_INVALID_CONTEXT);
1531 bReturn = AL_FALSE;
1534 ProcessContext(NULL);
1536 return bReturn;
1540 alcMakeCurrent
1542 Makes the given Context the active Context for the current thread
1544 ALCboolean ALCAPIENTRY alcMakeCurrent(ALCcontext *context)
1546 ALboolean bReturn = AL_TRUE;
1548 SuspendContext(NULL);
1550 // context must be a valid Context or NULL
1551 if(context == NULL || IsContext(context))
1552 tls_set(LocalContext, context);
1553 else
1555 alcSetError(NULL, ALC_INVALID_CONTEXT);
1556 bReturn = AL_FALSE;
1559 ProcessContext(NULL);
1561 return bReturn;
1565 // Sets the default channel order used by most non-WaveFormatEx-based APIs
1566 void SetDefaultChannelOrder(ALCdevice *device)
1568 switch(aluChannelsFromFormat(device->Format))
1570 case 1: device->DevChannels[0] = FRONT_CENTER; break;
1572 case 2: device->DevChannels[0] = FRONT_LEFT;
1573 device->DevChannels[1] = FRONT_RIGHT; break;
1575 case 4: device->DevChannels[0] = FRONT_LEFT;
1576 device->DevChannels[1] = FRONT_RIGHT;
1577 device->DevChannels[2] = BACK_LEFT;
1578 device->DevChannels[3] = BACK_RIGHT; break;
1580 case 6: device->DevChannels[0] = FRONT_LEFT;
1581 device->DevChannels[1] = FRONT_RIGHT;
1582 device->DevChannels[2] = BACK_LEFT;
1583 device->DevChannels[3] = BACK_RIGHT;
1584 device->DevChannels[4] = FRONT_CENTER;
1585 device->DevChannels[5] = LFE; break;
1587 case 7: device->DevChannels[0] = FRONT_LEFT;
1588 device->DevChannels[1] = FRONT_RIGHT;
1589 device->DevChannels[2] = FRONT_CENTER;
1590 device->DevChannels[3] = LFE;
1591 device->DevChannels[4] = BACK_CENTER;
1592 device->DevChannels[5] = SIDE_LEFT;
1593 device->DevChannels[6] = SIDE_RIGHT; break;
1595 case 8: device->DevChannels[0] = FRONT_LEFT;
1596 device->DevChannels[1] = FRONT_RIGHT;
1597 device->DevChannels[2] = BACK_LEFT;
1598 device->DevChannels[3] = BACK_RIGHT;
1599 device->DevChannels[4] = FRONT_CENTER;
1600 device->DevChannels[5] = LFE;
1601 device->DevChannels[6] = SIDE_LEFT;
1602 device->DevChannels[7] = SIDE_RIGHT; break;
1605 // Sets the default order used by WaveFormatEx
1606 void SetDefaultWFXChannelOrder(ALCdevice *device)
1608 switch(aluChannelsFromFormat(device->Format))
1610 case 1: device->DevChannels[0] = FRONT_CENTER; break;
1612 case 2: device->DevChannels[0] = FRONT_LEFT;
1613 device->DevChannels[1] = FRONT_RIGHT; break;
1615 case 4: device->DevChannels[0] = FRONT_LEFT;
1616 device->DevChannels[1] = FRONT_RIGHT;
1617 device->DevChannels[2] = BACK_LEFT;
1618 device->DevChannels[3] = BACK_RIGHT; break;
1620 case 6: device->DevChannels[0] = FRONT_LEFT;
1621 device->DevChannels[1] = FRONT_RIGHT;
1622 device->DevChannels[2] = FRONT_CENTER;
1623 device->DevChannels[3] = LFE;
1624 device->DevChannels[4] = BACK_LEFT;
1625 device->DevChannels[5] = BACK_RIGHT; break;
1627 case 7: device->DevChannels[0] = FRONT_LEFT;
1628 device->DevChannels[1] = FRONT_RIGHT;
1629 device->DevChannels[2] = FRONT_CENTER;
1630 device->DevChannels[3] = LFE;
1631 device->DevChannels[4] = BACK_CENTER;
1632 device->DevChannels[5] = SIDE_LEFT;
1633 device->DevChannels[6] = SIDE_RIGHT; break;
1635 case 8: device->DevChannels[0] = FRONT_LEFT;
1636 device->DevChannels[1] = FRONT_RIGHT;
1637 device->DevChannels[2] = FRONT_CENTER;
1638 device->DevChannels[3] = LFE;
1639 device->DevChannels[4] = BACK_LEFT;
1640 device->DevChannels[5] = BACK_RIGHT;
1641 device->DevChannels[6] = SIDE_LEFT;
1642 device->DevChannels[7] = SIDE_RIGHT; break;
1646 static ALenum GetFormatFromString(const char *str)
1648 if(strcasecmp(str, "AL_FORMAT_MONO32") == 0) return AL_FORMAT_MONO_FLOAT32;
1649 if(strcasecmp(str, "AL_FORMAT_STEREO32") == 0) return AL_FORMAT_STEREO_FLOAT32;
1650 if(strcasecmp(str, "AL_FORMAT_QUAD32") == 0) return AL_FORMAT_QUAD32;
1651 if(strcasecmp(str, "AL_FORMAT_51CHN32") == 0) return AL_FORMAT_51CHN32;
1652 if(strcasecmp(str, "AL_FORMAT_61CHN32") == 0) return AL_FORMAT_61CHN32;
1653 if(strcasecmp(str, "AL_FORMAT_71CHN32") == 0) return AL_FORMAT_71CHN32;
1655 if(strcasecmp(str, "AL_FORMAT_MONO16") == 0) return AL_FORMAT_MONO16;
1656 if(strcasecmp(str, "AL_FORMAT_STEREO16") == 0) return AL_FORMAT_STEREO16;
1657 if(strcasecmp(str, "AL_FORMAT_QUAD16") == 0) return AL_FORMAT_QUAD16;
1658 if(strcasecmp(str, "AL_FORMAT_51CHN16") == 0) return AL_FORMAT_51CHN16;
1659 if(strcasecmp(str, "AL_FORMAT_61CHN16") == 0) return AL_FORMAT_61CHN16;
1660 if(strcasecmp(str, "AL_FORMAT_71CHN16") == 0) return AL_FORMAT_71CHN16;
1662 if(strcasecmp(str, "AL_FORMAT_MONO8") == 0) return AL_FORMAT_MONO8;
1663 if(strcasecmp(str, "AL_FORMAT_STEREO8") == 0) return AL_FORMAT_STEREO8;
1664 if(strcasecmp(str, "AL_FORMAT_QUAD8") == 0) return AL_FORMAT_QUAD8;
1665 if(strcasecmp(str, "AL_FORMAT_51CHN8") == 0) return AL_FORMAT_51CHN8;
1666 if(strcasecmp(str, "AL_FORMAT_61CHN8") == 0) return AL_FORMAT_61CHN8;
1667 if(strcasecmp(str, "AL_FORMAT_71CHN8") == 0) return AL_FORMAT_71CHN8;
1669 AL_PRINT("Unknown format: \"%s\"\n", str);
1670 return AL_FORMAT_STEREO16;
1674 alcOpenDevice
1676 Open the Device specified.
1678 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1680 ALboolean bDeviceFound = AL_FALSE;
1681 ALCdevice *device;
1682 ALint i;
1684 if(deviceName && !deviceName[0])
1685 deviceName = NULL;
1687 if(!alcDeviceList)
1688 ProbeDeviceList();
1689 if(!alcAllDeviceList)
1690 ProbeAllDeviceList();
1692 device = malloc(sizeof(ALCdevice));
1693 if (device)
1695 const char *fmt;
1697 //Initialise device structure
1698 memset(device, 0, sizeof(ALCdevice));
1700 //Validate device
1701 device->Connected = ALC_TRUE;
1702 device->IsCaptureDevice = AL_FALSE;
1703 device->LastError = ALC_NO_ERROR;
1705 device->Bs2b = NULL;
1706 device->szDeviceName = NULL;
1708 device->Contexts = NULL;
1709 device->NumContexts = 0;
1711 //Set output format
1712 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1713 if(device->Frequency < 8000)
1714 device->Frequency = 8000;
1716 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1717 device->Format = GetFormatFromString(fmt);
1719 device->NumUpdates = GetConfigValueInt(NULL, "periods", 4);
1720 if(device->NumUpdates < 2)
1721 device->NumUpdates = 4;
1723 i = GetConfigValueInt(NULL, "refresh", 4096);
1724 if(i <= 0) i = 4096;
1726 device->UpdateSize = GetConfigValueInt(NULL, "period_size", i/device->NumUpdates);
1727 if(device->UpdateSize <= 0)
1728 device->UpdateSize = i/device->NumUpdates;
1730 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1731 if((ALint)device->MaxNoOfSources <= 0)
1732 device->MaxNoOfSources = 256;
1734 device->AuxiliaryEffectSlotMax = GetConfigValueInt(NULL, "slots", 4);
1735 if((ALint)device->AuxiliaryEffectSlotMax <= 0)
1736 device->AuxiliaryEffectSlotMax = 4;
1738 device->lNumStereoSources = 1;
1739 device->lNumMonoSources = device->MaxNoOfSources - device->lNumStereoSources;
1741 device->NumAuxSends = GetConfigValueInt(NULL, "sends", MAX_SENDS);
1742 if(device->NumAuxSends > MAX_SENDS)
1743 device->NumAuxSends = MAX_SENDS;
1745 device->Bs2bLevel = GetConfigValueInt(NULL, "cf_level", 0);
1747 if(aluChannelsFromFormat(device->Format) <= 2)
1749 device->HeadDampen = GetConfigValueFloat(NULL, "head_dampen", DEFAULT_HEAD_DAMPEN);
1750 device->HeadDampen = __min(device->HeadDampen, 1.0f);
1751 device->HeadDampen = __max(device->HeadDampen, 0.0f);
1753 else
1754 device->HeadDampen = 0.0f;
1756 // Find a playback device to open
1757 SuspendContext(NULL);
1758 for(i = 0;BackendList[i].Init;i++)
1760 device->Funcs = &BackendList[i].Funcs;
1761 if(ALCdevice_OpenPlayback(device, deviceName))
1763 device->next = g_pDeviceList;
1764 g_pDeviceList = device;
1765 g_ulDeviceCount++;
1767 bDeviceFound = AL_TRUE;
1768 break;
1771 ProcessContext(NULL);
1773 if (!bDeviceFound)
1775 // No suitable output device found
1776 alcSetError(NULL, ALC_INVALID_VALUE);
1777 free(device);
1778 device = NULL;
1781 else
1782 alcSetError(NULL, ALC_OUT_OF_MEMORY);
1784 return device;
1789 alcCloseDevice
1791 Close the specified Device
1793 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1795 ALCboolean bReturn = ALC_FALSE;
1796 ALCdevice **list;
1798 if(IsDevice(pDevice) && !pDevice->IsCaptureDevice)
1800 SuspendContext(NULL);
1802 list = &g_pDeviceList;
1803 while(*list != pDevice)
1804 list = &(*list)->next;
1806 *list = (*list)->next;
1807 g_ulDeviceCount--;
1809 ProcessContext(NULL);
1811 if(pDevice->NumContexts > 0)
1813 #ifdef _DEBUG
1814 AL_PRINT("alcCloseDevice(): destroying %u Context(s)\n", pDevice->NumContexts);
1815 #endif
1816 while(pDevice->NumContexts > 0)
1817 alcDestroyContext(pDevice->Contexts[0]);
1819 ALCdevice_ClosePlayback(pDevice);
1821 if(pDevice->BufferCount > 0)
1823 #ifdef _DEBUG
1824 AL_PRINT("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice->BufferCount);
1825 #endif
1826 ReleaseALBuffers(pDevice);
1828 if(pDevice->EffectCount > 0)
1830 #ifdef _DEBUG
1831 AL_PRINT("alcCloseDevice(): deleting %d Effect(s)\n", pDevice->EffectCount);
1832 #endif
1833 ReleaseALEffects(pDevice);
1835 if(pDevice->FilterCount > 0)
1837 #ifdef _DEBUG
1838 AL_PRINT("alcCloseDevice(): deleting %d Filter(s)\n", pDevice->FilterCount);
1839 #endif
1840 ReleaseALFilters(pDevice);
1842 if(pDevice->DatabufferCount > 0)
1844 #ifdef _DEBUG
1845 AL_PRINT("alcCloseDevice(): deleting %d Databuffer(s)\n", pDevice->DatabufferCount);
1846 #endif
1847 ReleaseALDatabuffers(pDevice);
1850 free(pDevice->Bs2b);
1851 pDevice->Bs2b = NULL;
1853 free(pDevice->szDeviceName);
1854 pDevice->szDeviceName = NULL;
1856 free(pDevice->Contexts);
1857 pDevice->Contexts = NULL;
1859 //Release device structure
1860 memset(pDevice, 0, sizeof(ALCdevice));
1861 free(pDevice);
1863 bReturn = ALC_TRUE;
1865 else
1866 alcSetError(pDevice, ALC_INVALID_DEVICE);
1868 return bReturn;
1872 ALCvoid ReleaseALC(ALCvoid)
1874 free(alcDeviceList); alcDeviceList = NULL;
1875 alcDeviceListSize = 0;
1876 free(alcAllDeviceList); alcAllDeviceList = NULL;
1877 alcAllDeviceListSize = 0;
1878 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
1879 alcCaptureDeviceListSize = 0;
1881 free(alcDefaultDeviceSpecifier);
1882 alcDefaultDeviceSpecifier = NULL;
1883 free(alcDefaultAllDeviceSpecifier);
1884 alcDefaultAllDeviceSpecifier = NULL;
1885 free(alcCaptureDefaultDeviceSpecifier);
1886 alcCaptureDefaultDeviceSpecifier = NULL;
1888 #ifdef _DEBUG
1889 if(g_ulDeviceCount > 0)
1890 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
1891 #endif
1893 while(g_pDeviceList)
1895 if(g_pDeviceList->IsCaptureDevice)
1896 alcCaptureCloseDevice(g_pDeviceList);
1897 else
1898 alcCloseDevice(g_pDeviceList);
1902 ///////////////////////////////////////////////////////