Use a global handle to store the process-wide context
[openal-soft.git] / Alc / ALc.c
blobe75906867c5463dc4100ad74b834df794d82f4e2
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 { "alcSetThreadContext", (ALvoid *) alcSetThreadContext },
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 size_t alcDeviceListSize;
173 static ALCchar *alcAllDeviceList;
174 static size_t alcAllDeviceListSize;
175 static ALCchar *alcCaptureDeviceList;
176 static size_t alcCaptureDeviceListSize;
177 // Default is always the first in the list
178 static ALCchar *alcDefaultDeviceSpecifier;
179 static ALCchar *alcDefaultAllDeviceSpecifier;
180 static ALCchar *alcCaptureDefaultDeviceSpecifier;
183 static const ALCchar alcNoDeviceExtList[] =
184 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
185 "ALC_EXTX_thread_local_context";
186 static const ALCchar alcExtensionList[] =
187 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
188 "ALC_EXT_disconnect ALC_EXT_EFX ALC_EXTX_thread_local_context";
189 static const ALCint alcMajorVersion = 1;
190 static const ALCint alcMinorVersion = 1;
192 static const ALCint alcEFXMajorVersion = 1;
193 static const ALCint alcEFXMinorVersion = 0;
195 ///////////////////////////////////////////////////////
198 ///////////////////////////////////////////////////////
199 // Global Variables
201 static ALCdevice *g_pDeviceList = NULL;
202 static ALCuint g_ulDeviceCount = 0;
204 static CRITICAL_SECTION g_csMutex;
206 // Context List
207 static ALCcontext *g_pContextList = NULL;
208 static ALCuint g_ulContextCount = 0;
210 // Thread-local current context
211 static tls_type LocalContext;
212 // Process-wide current context
213 static ALCcontext *GlobalContext;
215 // Context Error
216 static ALCenum g_eLastContextError = ALC_NO_ERROR;
218 // Default context extensions
219 static const ALchar alExtList[] =
220 "AL_EXTX_buffer_sub_data AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE "
221 "AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS "
222 "AL_EXT_MULAW AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET "
223 "AL_EXTX_sample_buffer_object AL_EXT_source_distance_model "
224 "AL_LOKI_quadriphonic";
226 // Mixing Priority Level
227 ALint RTPrioLevel;
229 // Resampler Quality
230 resampler_t DefaultResampler;
232 ///////////////////////////////////////////////////////
235 ///////////////////////////////////////////////////////
236 // ALC Related helper functions
237 #ifdef _WIN32
238 static void alc_init(void);
239 static void alc_deinit(void);
241 BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
243 (void)lpReserved;
245 // Perform actions based on the reason for calling.
246 switch(ul_reason_for_call)
248 case DLL_PROCESS_ATTACH:
249 DisableThreadLibraryCalls(hModule);
250 alc_init();
251 break;
253 case DLL_PROCESS_DETACH:
254 alc_deinit();
255 break;
257 return TRUE;
259 #else
260 #ifdef HAVE_GCC_DESTRUCTOR
261 static void alc_init(void) __attribute__((constructor));
262 static void alc_deinit(void) __attribute__((destructor));
263 #endif
264 #endif
266 static void alc_init(void)
268 int i;
269 const char *devs, *str;
271 InitializeCriticalSection(&g_csMutex);
272 ALTHUNK_INIT();
273 ReadALConfig();
275 tls_create(&LocalContext);
277 RTPrioLevel = GetConfigValueInt(NULL, "rt-prio", 0);
279 DefaultResampler = GetConfigValueInt(NULL, "resampler", RESAMPLER_DEFAULT);
280 if(DefaultResampler >= RESAMPLER_MAX || DefaultResampler <= RESAMPLER_MIN)
281 DefaultResampler = RESAMPLER_DEFAULT;
283 devs = GetConfigValue(NULL, "drivers", "");
284 if(devs[0])
286 int n;
287 size_t len;
288 const char *next = devs;
289 int endlist;
291 i = 0;
292 do {
293 devs = next;
294 next = strchr(devs, ',');
296 if(!devs[0] || devs[0] == ',')
298 endlist = 0;
299 continue;
301 endlist = 1;
303 len = (next ? ((size_t)(next-devs)) : strlen(devs));
304 for(n = i;BackendList[n].Init;n++)
306 if(len == strlen(BackendList[n].name) &&
307 strncmp(BackendList[n].name, devs, len) == 0)
309 BackendInfo Bkp = BackendList[n];
310 while(n > i)
312 BackendList[n] = BackendList[n-1];
313 --n;
315 BackendList[n] = Bkp;
317 i++;
318 break;
321 } while(next++);
323 if(endlist)
325 BackendList[i].name = NULL;
326 BackendList[i].Init = NULL;
327 BackendList[i].Deinit = NULL;
328 BackendList[i].Probe = NULL;
332 for(i = 0;BackendList[i].Init;i++)
333 BackendList[i].Init(&BackendList[i].Funcs);
335 DuplicateStereo = GetConfigValueBool(NULL, "stereodup", 0);
337 str = GetConfigValue(NULL, "excludefx", "");
338 if(str[0])
340 const struct {
341 const char *name;
342 int type;
343 } EffectList[] = {
344 { "eaxreverb", EAXREVERB },
345 { "reverb", REVERB },
346 { "echo", ECHO },
347 { NULL, 0 }
349 int n;
350 size_t len;
351 const char *next = str;
353 do {
354 str = next;
355 next = strchr(str, ',');
357 if(!str[0] || next == str)
358 continue;
360 len = (next ? ((size_t)(next-str)) : strlen(str));
361 for(n = 0;EffectList[n].name;n++)
363 if(len == strlen(EffectList[n].name) &&
364 strncmp(EffectList[n].name, str, len) == 0)
365 DisabledEffects[EffectList[n].type] = AL_TRUE;
367 } while(next++);
371 static void alc_deinit(void)
373 int i;
375 ReleaseALC();
377 for(i = 0;BackendList[i].Deinit;i++)
378 BackendList[i].Deinit();
380 tls_delete(LocalContext);
382 FreeALConfig();
383 ALTHUNK_EXIT();
384 DeleteCriticalSection(&g_csMutex);
388 static void ProbeDeviceList()
390 ALint i;
392 free(alcDeviceList); alcDeviceList = NULL;
393 alcDeviceListSize = 0;
395 for(i = 0;BackendList[i].Probe;i++)
396 BackendList[i].Probe(DEVICE_PROBE);
399 static void ProbeAllDeviceList()
401 ALint i;
403 free(alcAllDeviceList); alcAllDeviceList = NULL;
404 alcAllDeviceListSize = 0;
406 for(i = 0;BackendList[i].Probe;i++)
407 BackendList[i].Probe(ALL_DEVICE_PROBE);
410 static void ProbeCaptureDeviceList()
412 ALint i;
414 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
415 alcCaptureDeviceListSize = 0;
417 for(i = 0;BackendList[i].Probe;i++)
418 BackendList[i].Probe(CAPTURE_DEVICE_PROBE);
422 #define DECL_APPEND_LIST_FUNC(type) \
423 void Append##type##List(const ALCchar *name) \
425 size_t len = strlen(name); \
426 void *temp; \
428 if(len == 0) \
429 return; \
431 temp = realloc(alc##type##List, alc##type##ListSize + len + 2); \
432 if(!temp) \
434 AL_PRINT("Realloc failed to add %s!\n", name); \
435 return; \
437 alc##type##List = temp; \
438 sprintf(alc##type##List+alc##type##ListSize, "%s", name); \
439 alc##type##ListSize += len+1; \
440 alc##type##List[alc##type##ListSize] = 0; \
443 DECL_APPEND_LIST_FUNC(Device)
444 DECL_APPEND_LIST_FUNC(AllDevice)
445 DECL_APPEND_LIST_FUNC(CaptureDevice)
448 void al_print(const char *fname, unsigned int line, const char *fmt, ...)
450 const char *fn;
451 char str[256];
452 int i;
454 fn = strrchr(fname, '/');
455 if(!fn) fn = strrchr(fname, '\\');;
456 if(!fn) fn = fname;
457 else fn += 1;
459 i = snprintf(str, sizeof(str), "AL lib: %s:%d: ", fn, line);
460 if(i < (int)sizeof(str) && i > 0)
462 va_list ap;
463 va_start(ap, fmt);
464 vsnprintf(str+i, sizeof(str)-i, fmt, ap);
465 va_end(ap);
467 str[sizeof(str)-1] = 0;
469 fprintf(stderr, "%s", str);
472 void EnableRTPrio(ALint level)
474 ALboolean failed;
476 #ifdef _WIN32
477 if(level > 0)
478 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
479 else
480 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
481 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM)
482 struct sched_param param;
484 if(level > 0)
486 /* Use the minimum real-time priority possible for now (on Linux this
487 * should be 1 for SCHED_RR) */
488 param.sched_priority = sched_get_priority_min(SCHED_RR);
489 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
491 else
493 param.sched_priority = 0;
494 failed = !!pthread_setschedparam(pthread_self(), SCHED_OTHER, &param);
496 #else
497 /* Real-time priority not available */
498 failed = !!level;
499 #endif
500 if(failed)
501 AL_PRINT("Failed to set priority level for thread\n");
506 IsDevice
508 Check pDevice is a valid Device pointer
510 static ALCboolean IsDevice(ALCdevice *pDevice)
512 ALCdevice *pTempDevice;
514 SuspendContext(NULL);
516 pTempDevice = g_pDeviceList;
517 while(pTempDevice && pTempDevice != pDevice)
518 pTempDevice = pTempDevice->next;
520 ProcessContext(NULL);
522 return (pTempDevice ? ALC_TRUE : ALC_FALSE);
526 IsContext
528 Check pContext is a valid Context pointer
530 static ALCboolean IsContext(ALCcontext *pContext)
532 ALCcontext *pTempContext;
534 SuspendContext(NULL);
536 pTempContext = g_pContextList;
537 while (pTempContext && pTempContext != pContext)
538 pTempContext = pTempContext->next;
540 ProcessContext(NULL);
542 return (pTempContext ? ALC_TRUE : ALC_FALSE);
547 alcSetError
549 Store latest ALC Error
551 ALCvoid alcSetError(ALCdevice *device, ALenum errorCode)
553 if(IsDevice(device))
554 device->LastError = errorCode;
555 else
556 g_eLastContextError = errorCode;
561 SuspendContext
563 Thread-safe entry
565 ALCvoid SuspendContext(ALCcontext *pContext)
567 (void)pContext;
568 EnterCriticalSection(&g_csMutex);
573 ProcessContext
575 Thread-safe exit
577 ALCvoid ProcessContext(ALCcontext *pContext)
579 (void)pContext;
580 LeaveCriticalSection(&g_csMutex);
585 GetContextSuspended
587 Returns the currently active Context, in a locked state
589 ALCcontext *GetContextSuspended(void)
591 ALCcontext *pContext = NULL;
593 SuspendContext(NULL);
595 pContext = tls_get(LocalContext);
596 if(pContext && !IsContext(pContext))
598 tls_set(LocalContext, NULL);
599 pContext = NULL;
601 if(!pContext)
602 pContext = GlobalContext;
604 if(pContext)
605 SuspendContext(pContext);
607 ProcessContext(NULL);
609 return pContext;
614 InitContext
616 Initialize Context variables
618 static ALvoid InitContext(ALCcontext *pContext)
620 //Initialise listener
621 pContext->Listener.Gain = 1.0f;
622 pContext->Listener.MetersPerUnit = 1.0f;
623 pContext->Listener.Position[0] = 0.0f;
624 pContext->Listener.Position[1] = 0.0f;
625 pContext->Listener.Position[2] = 0.0f;
626 pContext->Listener.Velocity[0] = 0.0f;
627 pContext->Listener.Velocity[1] = 0.0f;
628 pContext->Listener.Velocity[2] = 0.0f;
629 pContext->Listener.Forward[0] = 0.0f;
630 pContext->Listener.Forward[1] = 0.0f;
631 pContext->Listener.Forward[2] = -1.0f;
632 pContext->Listener.Up[0] = 0.0f;
633 pContext->Listener.Up[1] = 1.0f;
634 pContext->Listener.Up[2] = 0.0f;
636 //Validate pContext
637 pContext->LastError = AL_NO_ERROR;
638 pContext->Suspended = AL_FALSE;
640 //Set globals
641 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
642 pContext->SourceDistanceModel = AL_FALSE;
643 pContext->DopplerFactor = 1.0f;
644 pContext->DopplerVelocity = 1.0f;
645 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
647 pContext->ExtensionList = alExtList;
652 ExitContext
654 Clean up Context, destroy any remaining Sources
656 static ALCvoid ExitContext(ALCcontext *pContext)
658 //Invalidate context
659 pContext->LastError = AL_NO_ERROR;
662 ///////////////////////////////////////////////////////
665 ///////////////////////////////////////////////////////
666 // ALC Functions calls
669 // This should probably move to another c file but for now ...
670 ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
672 ALCboolean DeviceFound = ALC_FALSE;
673 ALCdevice *device = NULL;
674 ALCint i;
676 if(SampleSize <= 0)
678 alcSetError(NULL, ALC_INVALID_VALUE);
679 return NULL;
682 if(deviceName && !deviceName[0])
683 deviceName = NULL;
685 device = calloc(1, sizeof(ALCdevice));
686 if(!device)
688 alcSetError(NULL, ALC_OUT_OF_MEMORY);
689 return NULL;
692 //Validate device
693 device->Connected = ALC_TRUE;
694 device->IsCaptureDevice = AL_TRUE;
696 device->szDeviceName = NULL;
698 device->Frequency = frequency;
699 device->Format = format;
700 device->UpdateSize = SampleSize;
701 device->NumUpdates = 1;
703 SuspendContext(NULL);
704 for(i = 0;BackendList[i].Init;i++)
706 device->Funcs = &BackendList[i].Funcs;
707 if(ALCdevice_OpenCapture(device, deviceName))
709 device->next = g_pDeviceList;
710 g_pDeviceList = device;
711 g_ulDeviceCount++;
713 DeviceFound = ALC_TRUE;
714 break;
717 ProcessContext(NULL);
719 if(!DeviceFound)
721 alcSetError(NULL, ALC_INVALID_VALUE);
722 free(device);
723 device = NULL;
726 return device;
729 ALC_API ALCboolean ALC_APIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
731 ALCdevice **list;
733 if(!IsDevice(pDevice) || !pDevice->IsCaptureDevice)
735 alcSetError(pDevice, ALC_INVALID_DEVICE);
736 return ALC_FALSE;
739 SuspendContext(NULL);
741 list = &g_pDeviceList;
742 while(*list != pDevice)
743 list = &(*list)->next;
745 *list = (*list)->next;
746 g_ulDeviceCount--;
748 ProcessContext(NULL);
750 ALCdevice_CloseCapture(pDevice);
752 free(pDevice->szDeviceName);
753 pDevice->szDeviceName = NULL;
755 free(pDevice);
757 return ALC_TRUE;
760 ALC_API void ALC_APIENTRY alcCaptureStart(ALCdevice *device)
762 if(!IsDevice(device) || !device->IsCaptureDevice)
764 alcSetError(device, ALC_INVALID_DEVICE);
765 return;
767 SuspendContext(NULL);
768 ALCdevice_StartCapture(device);
769 ProcessContext(NULL);
772 ALC_API void ALC_APIENTRY alcCaptureStop(ALCdevice *device)
774 if(!IsDevice(device) || !device->IsCaptureDevice)
776 alcSetError(device, ALC_INVALID_DEVICE);
777 return;
779 SuspendContext(NULL);
780 ALCdevice_StopCapture(device);
781 ProcessContext(NULL);
784 ALC_API void ALC_APIENTRY alcCaptureSamples(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
786 if(!IsDevice(device) || !device->IsCaptureDevice)
788 alcSetError(device, ALC_INVALID_DEVICE);
789 return;
791 SuspendContext(NULL);
792 ALCdevice_CaptureSamples(device, buffer, samples);
793 ProcessContext(NULL);
797 alcGetError
799 Return last ALC generated error code
801 ALC_API ALCenum ALC_APIENTRY alcGetError(ALCdevice *device)
803 ALCenum errorCode = ALC_NO_ERROR;
805 if(IsDevice(device))
807 errorCode = device->LastError;
808 device->LastError = ALC_NO_ERROR;
810 else
812 errorCode = g_eLastContextError;
813 g_eLastContextError = ALC_NO_ERROR;
815 return errorCode;
820 alcSuspendContext
822 Not functional
824 ALC_API ALCvoid ALC_APIENTRY alcSuspendContext(ALCcontext *pContext)
826 SuspendContext(NULL);
827 if(IsContext(pContext))
828 pContext->Suspended = AL_TRUE;
829 ProcessContext(NULL);
834 alcProcessContext
836 Not functional
838 ALC_API ALCvoid ALC_APIENTRY alcProcessContext(ALCcontext *pContext)
840 SuspendContext(NULL);
841 if(IsContext(pContext))
842 pContext->Suspended = AL_FALSE;
843 ProcessContext(NULL);
848 alcGetString
850 Returns information about the Device, and error strings
852 ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
854 const ALCchar *value = NULL;
856 switch (param)
858 case ALC_NO_ERROR:
859 value = alcNoError;
860 break;
862 case ALC_INVALID_ENUM:
863 value = alcErrInvalidEnum;
864 break;
866 case ALC_INVALID_VALUE:
867 value = alcErrInvalidValue;
868 break;
870 case ALC_INVALID_DEVICE:
871 value = alcErrInvalidDevice;
872 break;
874 case ALC_INVALID_CONTEXT:
875 value = alcErrInvalidContext;
876 break;
878 case ALC_OUT_OF_MEMORY:
879 value = alcErrOutOfMemory;
880 break;
882 case ALC_DEVICE_SPECIFIER:
883 if(IsDevice(pDevice))
884 value = pDevice->szDeviceName;
885 else
887 ProbeDeviceList();
888 value = alcDeviceList;
890 break;
892 case ALC_ALL_DEVICES_SPECIFIER:
893 ProbeAllDeviceList();
894 value = alcAllDeviceList;
895 break;
897 case ALC_CAPTURE_DEVICE_SPECIFIER:
898 if(IsDevice(pDevice))
899 value = pDevice->szDeviceName;
900 else
902 ProbeCaptureDeviceList();
903 value = alcCaptureDeviceList;
905 break;
907 /* Default devices are always first in the list */
908 case ALC_DEFAULT_DEVICE_SPECIFIER:
909 if(!alcDeviceList)
910 ProbeDeviceList();
912 free(alcDefaultDeviceSpecifier);
913 alcDefaultDeviceSpecifier = strdup(alcDeviceList ? alcDeviceList : "");
914 if(!alcDefaultDeviceSpecifier)
915 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
916 value = alcDefaultDeviceSpecifier;
917 break;
919 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
920 if(!alcAllDeviceList)
921 ProbeAllDeviceList();
923 free(alcDefaultAllDeviceSpecifier);
924 alcDefaultAllDeviceSpecifier = strdup(alcAllDeviceList ?
925 alcAllDeviceList : "");
926 if(!alcDefaultAllDeviceSpecifier)
927 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
928 value = alcDefaultAllDeviceSpecifier;
929 break;
931 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
932 if(!alcCaptureDeviceList)
933 ProbeCaptureDeviceList();
935 free(alcCaptureDefaultDeviceSpecifier);
936 alcCaptureDefaultDeviceSpecifier = strdup(alcCaptureDeviceList ?
937 alcCaptureDeviceList : "");
938 if(!alcCaptureDefaultDeviceSpecifier)
939 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
940 value = alcCaptureDefaultDeviceSpecifier;
941 break;
943 case ALC_EXTENSIONS:
944 if(IsDevice(pDevice))
945 value = alcExtensionList;
946 else
947 value = alcNoDeviceExtList;
948 break;
950 default:
951 alcSetError(pDevice, ALC_INVALID_ENUM);
952 break;
955 return value;
960 alcGetIntegerv
962 Returns information about the Device and the version of Open AL
964 ALC_API ALCvoid ALC_APIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
966 if(size == 0 || data == NULL)
968 alcSetError(device, ALC_INVALID_VALUE);
969 return;
972 if(IsDevice(device) && device->IsCaptureDevice)
974 SuspendContext(NULL);
976 // Capture device
977 switch (param)
979 case ALC_CAPTURE_SAMPLES:
980 *data = ALCdevice_AvailableSamples(device);
981 break;
983 case ALC_CONNECTED:
984 *data = device->Connected;
985 break;
987 default:
988 alcSetError(device, ALC_INVALID_ENUM);
989 break;
992 ProcessContext(NULL);
993 return;
996 // Playback Device
997 switch (param)
999 case ALC_MAJOR_VERSION:
1000 *data = alcMajorVersion;
1001 break;
1003 case ALC_MINOR_VERSION:
1004 *data = alcMinorVersion;
1005 break;
1007 case ALC_EFX_MAJOR_VERSION:
1008 *data = alcEFXMajorVersion;
1009 break;
1011 case ALC_EFX_MINOR_VERSION:
1012 *data = alcEFXMinorVersion;
1013 break;
1015 case ALC_MAX_AUXILIARY_SENDS:
1016 if(!IsDevice(device))
1017 alcSetError(device, ALC_INVALID_DEVICE);
1018 else
1019 *data = device->NumAuxSends;
1020 break;
1022 case ALC_ATTRIBUTES_SIZE:
1023 if(!IsDevice(device))
1024 alcSetError(device, ALC_INVALID_DEVICE);
1025 else
1026 *data = 13;
1027 break;
1029 case ALC_ALL_ATTRIBUTES:
1030 if(!IsDevice(device))
1031 alcSetError(device, ALC_INVALID_DEVICE);
1032 else if (size < 13)
1033 alcSetError(device, ALC_INVALID_VALUE);
1034 else
1036 int i = 0;
1038 SuspendContext(NULL);
1039 data[i++] = ALC_FREQUENCY;
1040 data[i++] = device->Frequency;
1042 data[i++] = ALC_REFRESH;
1043 data[i++] = device->Frequency / device->UpdateSize;
1045 data[i++] = ALC_SYNC;
1046 data[i++] = ALC_FALSE;
1048 data[i++] = ALC_MONO_SOURCES;
1049 data[i++] = device->lNumMonoSources;
1051 data[i++] = ALC_STEREO_SOURCES;
1052 data[i++] = device->lNumStereoSources;
1054 data[i++] = ALC_MAX_AUXILIARY_SENDS;
1055 data[i++] = device->NumAuxSends;
1057 data[i++] = 0;
1058 ProcessContext(NULL);
1060 break;
1062 case ALC_FREQUENCY:
1063 if(!IsDevice(device))
1064 alcSetError(device, ALC_INVALID_DEVICE);
1065 else
1066 *data = device->Frequency;
1067 break;
1069 case ALC_REFRESH:
1070 if(!IsDevice(device))
1071 alcSetError(device, ALC_INVALID_DEVICE);
1072 else
1073 *data = device->Frequency / device->UpdateSize;
1074 break;
1076 case ALC_SYNC:
1077 if(!IsDevice(device))
1078 alcSetError(device, ALC_INVALID_DEVICE);
1079 else
1080 *data = ALC_FALSE;
1081 break;
1083 case ALC_MONO_SOURCES:
1084 if(!IsDevice(device))
1085 alcSetError(device, ALC_INVALID_DEVICE);
1086 else
1087 *data = device->lNumMonoSources;
1088 break;
1090 case ALC_STEREO_SOURCES:
1091 if(!IsDevice(device))
1092 alcSetError(device, ALC_INVALID_DEVICE);
1093 else
1094 *data = device->lNumStereoSources;
1095 break;
1097 case ALC_CONNECTED:
1098 if(!IsDevice(device))
1099 alcSetError(device, ALC_INVALID_DEVICE);
1100 else
1101 *data = device->Connected;
1102 break;
1104 default:
1105 alcSetError(device, ALC_INVALID_ENUM);
1106 break;
1112 alcIsExtensionPresent
1114 Determines if there is support for a particular extension
1116 ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
1118 ALCboolean bResult = ALC_FALSE;
1119 const char *ptr;
1120 size_t len;
1122 if(!extName)
1124 alcSetError(device, ALC_INVALID_VALUE);
1125 return ALC_FALSE;
1128 len = strlen(extName);
1129 ptr = (IsDevice(device) ? alcExtensionList : alcNoDeviceExtList);
1130 while(ptr && *ptr)
1132 if(strncasecmp(ptr, extName, len) == 0 &&
1133 (ptr[len] == '\0' || isspace(ptr[len])))
1135 bResult = ALC_TRUE;
1136 break;
1138 if((ptr=strchr(ptr, ' ')) != NULL)
1140 do {
1141 ++ptr;
1142 } while(isspace(*ptr));
1146 return bResult;
1151 alcGetProcAddress
1153 Retrieves the function address for a particular extension function
1155 ALC_API ALCvoid* ALC_APIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
1157 ALsizei i = 0;
1159 if(!funcName)
1161 alcSetError(device, ALC_INVALID_VALUE);
1162 return NULL;
1165 while(alcFunctions[i].funcName && strcmp(alcFunctions[i].funcName,funcName) != 0)
1166 i++;
1167 return alcFunctions[i].address;
1172 alcGetEnumValue
1174 Get the value for a particular ALC Enumerated Value
1176 ALC_API ALCenum ALC_APIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
1178 ALsizei i = 0;
1179 ALCenum val;
1181 while(enumeration[i].enumName && strcmp(enumeration[i].enumName,enumName) == 0)
1182 i++;
1183 val = enumeration[i].value;
1185 if(!enumeration[i].enumName)
1186 alcSetError(device, ALC_INVALID_VALUE);
1188 return val;
1193 alcCreateContext
1195 Create and attach a Context to a particular Device.
1197 ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
1199 ALuint attrIdx, reqStereoSources;
1200 ALCcontext *ALContext;
1201 void *temp;
1202 ALuint i;
1204 SuspendContext(NULL);
1206 if(!IsDevice(device) || device->IsCaptureDevice || !device->Connected)
1208 alcSetError(device, ALC_INVALID_DEVICE);
1209 ProcessContext(NULL);
1210 return NULL;
1213 // Reset Context Last Error code
1214 device->LastError = ALC_NO_ERROR;
1216 // If a context is already running on the device, stop playback so the
1217 // device attributes can be updated
1218 if(device->NumContexts > 0)
1220 ProcessContext(NULL);
1221 ALCdevice_StopPlayback(device);
1222 SuspendContext(NULL);
1225 // Check for attributes
1226 if(attrList)
1228 ALCint level = device->Bs2bLevel;
1229 ALCuint freq = device->Frequency;
1230 ALCint numMono = device->lNumMonoSources;
1231 ALCint numStereo = device->lNumStereoSources;
1232 ALCuint numSends = device->NumAuxSends;
1234 attrIdx = 0;
1235 while(attrList[attrIdx])
1237 if(attrList[attrIdx] == ALC_FREQUENCY &&
1238 !ConfigValueExists(NULL, "frequency"))
1240 freq = attrList[attrIdx + 1];
1241 if(freq < 8000)
1242 freq = 8000;
1245 if(attrList[attrIdx] == ALC_STEREO_SOURCES)
1247 reqStereoSources = attrList[attrIdx + 1];
1248 if(reqStereoSources > device->MaxNoOfSources)
1249 reqStereoSources = device->MaxNoOfSources;
1251 numStereo = reqStereoSources;
1252 numMono = device->MaxNoOfSources - numStereo;
1255 if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS &&
1256 !ConfigValueExists(NULL, "sends"))
1258 numSends = attrList[attrIdx + 1];
1259 if(numSends > MAX_SENDS)
1260 numSends = MAX_SENDS;
1263 attrIdx += 2;
1266 device->UpdateSize = (ALuint64)device->UpdateSize * freq /
1267 device->Frequency;
1269 device->Bs2bLevel = level;
1270 device->Frequency = freq;
1271 device->lNumMonoSources = numMono;
1272 device->lNumStereoSources = numStereo;
1273 device->NumAuxSends = numSends;
1276 if(ALCdevice_ResetPlayback(device) == ALC_FALSE)
1278 alcSetError(device, ALC_INVALID_DEVICE);
1279 aluHandleDisconnect(device);
1280 ProcessContext(NULL);
1281 return NULL;
1284 for(i = 0;i < device->NumContexts;i++)
1286 ALCcontext *context = device->Contexts[i];
1287 ALeffectslot *slot;
1288 ALsource *source;
1290 SuspendContext(context);
1291 for(slot = context->EffectSlotList;slot != NULL;slot = slot->next)
1293 if(!slot->EffectState)
1294 continue;
1296 if(ALEffect_DeviceUpdate(slot->EffectState, device) == AL_FALSE)
1298 alcSetError(device, ALC_INVALID_DEVICE);
1299 aluHandleDisconnect(device);
1300 ProcessContext(context);
1301 ProcessContext(NULL);
1302 ALCdevice_StopPlayback(device);
1303 return NULL;
1305 ALEffect_Update(slot->EffectState, context, &slot->effect);
1308 for(source = context->SourceList;source != NULL;source = source->next)
1310 ALuint s = device->NumAuxSends;
1311 while(s < MAX_SENDS)
1313 if(source->Send[s].Slot)
1314 source->Send[s].Slot->refcount--;
1315 source->Send[s].Slot = NULL;
1316 source->Send[s].WetFilter.type = 0;
1317 source->Send[s].WetFilter.filter = 0;
1318 s++;
1320 source->NeedsUpdate = AL_TRUE;
1322 aluInitPanning(context);
1323 ProcessContext(context);
1326 if(device->Bs2bLevel > 0 && device->Bs2bLevel <= 6)
1328 if(!device->Bs2b)
1330 device->Bs2b = calloc(1, sizeof(*device->Bs2b));
1331 bs2b_clear(device->Bs2b);
1333 bs2b_set_srate(device->Bs2b, device->Frequency);
1334 bs2b_set_level(device->Bs2b, device->Bs2bLevel);
1336 else
1338 free(device->Bs2b);
1339 device->Bs2b = NULL;
1342 temp = realloc(device->Contexts, (device->NumContexts+1) * sizeof(*device->Contexts));
1343 if(!temp)
1345 alcSetError(device, ALC_OUT_OF_MEMORY);
1346 ProcessContext(NULL);
1347 return NULL;
1349 device->Contexts = temp;
1351 ALContext = calloc(1, sizeof(ALCcontext));
1352 if(!ALContext)
1354 alcSetError(device, ALC_OUT_OF_MEMORY);
1355 ProcessContext(NULL);
1356 return NULL;
1359 device->Contexts[device->NumContexts++] = ALContext;
1360 ALContext->Device = device;
1362 InitContext(ALContext);
1363 aluInitPanning(ALContext);
1365 ALContext->next = g_pContextList;
1366 g_pContextList = ALContext;
1367 g_ulContextCount++;
1369 ProcessContext(NULL);
1371 return ALContext;
1376 alcDestroyContext
1378 Remove a Context
1380 ALC_API ALCvoid ALC_APIENTRY alcDestroyContext(ALCcontext *context)
1382 ALCdevice *Device;
1383 ALCcontext **list;
1384 ALuint i;
1386 if(!IsContext(context))
1388 alcSetError(NULL, ALC_INVALID_CONTEXT);
1389 return;
1392 Device = context->Device;
1394 if(Device->NumContexts == 1)
1395 ALCdevice_StopPlayback(Device);
1397 SuspendContext(NULL);
1399 if(context == GlobalContext)
1400 GlobalContext = NULL;
1402 for(i = 0;i < Device->NumContexts-1;i++)
1404 if(Device->Contexts[i] == context)
1406 Device->Contexts[i] = Device->Contexts[Device->NumContexts-1];
1407 break;
1410 Device->NumContexts--;
1412 // Lock context
1413 SuspendContext(context);
1415 if(context->SourceCount > 0)
1417 #ifdef _DEBUG
1418 AL_PRINT("alcDestroyContext(): deleting %d Source(s)\n", context->SourceCount);
1419 #endif
1420 ReleaseALSources(context);
1422 if(context->EffectSlotCount > 0)
1424 #ifdef _DEBUG
1425 AL_PRINT("alcDestroyContext(): deleting %d AuxiliaryEffectSlot(s)\n", context->EffectSlotCount);
1426 #endif
1427 ReleaseALAuxiliaryEffectSlots(context);
1430 list = &g_pContextList;
1431 while(*list != context)
1432 list = &(*list)->next;
1434 *list = (*list)->next;
1435 g_ulContextCount--;
1437 // Unlock context
1438 ProcessContext(context);
1439 ProcessContext(NULL);
1441 ExitContext(context);
1443 // Free memory (MUST do this after ProcessContext)
1444 memset(context, 0, sizeof(ALCcontext));
1445 free(context);
1450 alcGetCurrentContext
1452 Returns the currently active Context
1454 ALC_API ALCcontext* ALC_APIENTRY alcGetCurrentContext(ALCvoid)
1456 ALCcontext *pContext;
1458 if((pContext=GetContextSuspended()) != NULL)
1459 ProcessContext(pContext);
1461 return pContext;
1465 alcGetThreadContext
1467 Returns the currently active thread-local Context
1469 ALC_API ALCcontext* ALC_APIENTRY alcGetThreadContext(void)
1471 ALCcontext *pContext = NULL;
1473 SuspendContext(NULL);
1475 pContext = tls_get(LocalContext);
1476 if(pContext && !IsContext(pContext))
1478 tls_set(LocalContext, NULL);
1479 pContext = NULL;
1482 ProcessContext(NULL);
1484 return pContext;
1489 alcGetContextsDevice
1491 Returns the Device that a particular Context is attached to
1493 ALC_API ALCdevice* ALC_APIENTRY alcGetContextsDevice(ALCcontext *pContext)
1495 ALCdevice *pDevice = NULL;
1497 SuspendContext(NULL);
1498 if(IsContext(pContext))
1499 pDevice = pContext->Device;
1500 else
1501 alcSetError(NULL, ALC_INVALID_CONTEXT);
1502 ProcessContext(NULL);
1504 return pDevice;
1509 alcMakeContextCurrent
1511 Makes the given Context the active Context
1513 ALC_API ALCboolean ALC_APIENTRY alcMakeContextCurrent(ALCcontext *context)
1515 ALboolean bReturn = AL_TRUE;
1517 SuspendContext(NULL);
1519 // context must be a valid Context or NULL
1520 if(context == NULL || IsContext(context))
1522 GlobalContext = context;
1523 tls_set(LocalContext, NULL);
1525 else
1527 alcSetError(NULL, ALC_INVALID_CONTEXT);
1528 bReturn = AL_FALSE;
1531 ProcessContext(NULL);
1533 return bReturn;
1537 alcSetThreadContext
1539 Makes the given Context the active Context for the current thread
1541 ALC_API ALCboolean ALC_APIENTRY alcSetThreadContext(ALCcontext *context)
1543 ALboolean bReturn = AL_TRUE;
1545 SuspendContext(NULL);
1547 // context must be a valid Context or NULL
1548 if(context == NULL || IsContext(context))
1549 tls_set(LocalContext, context);
1550 else
1552 alcSetError(NULL, ALC_INVALID_CONTEXT);
1553 bReturn = AL_FALSE;
1556 ProcessContext(NULL);
1558 return bReturn;
1562 // Sets the default channel order used by most non-WaveFormatEx-based APIs
1563 void SetDefaultChannelOrder(ALCdevice *device)
1565 switch(aluChannelsFromFormat(device->Format))
1567 case 1: device->DevChannels[0] = FRONT_CENTER; break;
1569 case 2: device->DevChannels[0] = FRONT_LEFT;
1570 device->DevChannels[1] = FRONT_RIGHT; break;
1572 case 4: device->DevChannels[0] = FRONT_LEFT;
1573 device->DevChannels[1] = FRONT_RIGHT;
1574 device->DevChannels[2] = BACK_LEFT;
1575 device->DevChannels[3] = BACK_RIGHT; break;
1577 case 6: device->DevChannels[0] = FRONT_LEFT;
1578 device->DevChannels[1] = FRONT_RIGHT;
1579 device->DevChannels[2] = BACK_LEFT;
1580 device->DevChannels[3] = BACK_RIGHT;
1581 device->DevChannels[4] = FRONT_CENTER;
1582 device->DevChannels[5] = LFE; break;
1584 case 7: device->DevChannels[0] = FRONT_LEFT;
1585 device->DevChannels[1] = FRONT_RIGHT;
1586 device->DevChannels[2] = FRONT_CENTER;
1587 device->DevChannels[3] = LFE;
1588 device->DevChannels[4] = BACK_CENTER;
1589 device->DevChannels[5] = SIDE_LEFT;
1590 device->DevChannels[6] = SIDE_RIGHT; break;
1592 case 8: device->DevChannels[0] = FRONT_LEFT;
1593 device->DevChannels[1] = FRONT_RIGHT;
1594 device->DevChannels[2] = BACK_LEFT;
1595 device->DevChannels[3] = BACK_RIGHT;
1596 device->DevChannels[4] = FRONT_CENTER;
1597 device->DevChannels[5] = LFE;
1598 device->DevChannels[6] = SIDE_LEFT;
1599 device->DevChannels[7] = SIDE_RIGHT; break;
1602 // Sets the default order used by WaveFormatEx
1603 void SetDefaultWFXChannelOrder(ALCdevice *device)
1605 switch(aluChannelsFromFormat(device->Format))
1607 case 1: device->DevChannels[0] = FRONT_CENTER; break;
1609 case 2: device->DevChannels[0] = FRONT_LEFT;
1610 device->DevChannels[1] = FRONT_RIGHT; break;
1612 case 4: device->DevChannels[0] = FRONT_LEFT;
1613 device->DevChannels[1] = FRONT_RIGHT;
1614 device->DevChannels[2] = BACK_LEFT;
1615 device->DevChannels[3] = BACK_RIGHT; break;
1617 case 6: device->DevChannels[0] = FRONT_LEFT;
1618 device->DevChannels[1] = FRONT_RIGHT;
1619 device->DevChannels[2] = FRONT_CENTER;
1620 device->DevChannels[3] = LFE;
1621 device->DevChannels[4] = BACK_LEFT;
1622 device->DevChannels[5] = BACK_RIGHT; break;
1624 case 7: device->DevChannels[0] = FRONT_LEFT;
1625 device->DevChannels[1] = FRONT_RIGHT;
1626 device->DevChannels[2] = FRONT_CENTER;
1627 device->DevChannels[3] = LFE;
1628 device->DevChannels[4] = BACK_CENTER;
1629 device->DevChannels[5] = SIDE_LEFT;
1630 device->DevChannels[6] = SIDE_RIGHT; break;
1632 case 8: device->DevChannels[0] = FRONT_LEFT;
1633 device->DevChannels[1] = FRONT_RIGHT;
1634 device->DevChannels[2] = FRONT_CENTER;
1635 device->DevChannels[3] = LFE;
1636 device->DevChannels[4] = BACK_LEFT;
1637 device->DevChannels[5] = BACK_RIGHT;
1638 device->DevChannels[6] = SIDE_LEFT;
1639 device->DevChannels[7] = SIDE_RIGHT; break;
1643 static ALenum GetFormatFromString(const char *str)
1645 if(strcasecmp(str, "AL_FORMAT_MONO32") == 0) return AL_FORMAT_MONO_FLOAT32;
1646 if(strcasecmp(str, "AL_FORMAT_STEREO32") == 0) return AL_FORMAT_STEREO_FLOAT32;
1647 if(strcasecmp(str, "AL_FORMAT_QUAD32") == 0) return AL_FORMAT_QUAD32;
1648 if(strcasecmp(str, "AL_FORMAT_51CHN32") == 0) return AL_FORMAT_51CHN32;
1649 if(strcasecmp(str, "AL_FORMAT_61CHN32") == 0) return AL_FORMAT_61CHN32;
1650 if(strcasecmp(str, "AL_FORMAT_71CHN32") == 0) return AL_FORMAT_71CHN32;
1652 if(strcasecmp(str, "AL_FORMAT_MONO16") == 0) return AL_FORMAT_MONO16;
1653 if(strcasecmp(str, "AL_FORMAT_STEREO16") == 0) return AL_FORMAT_STEREO16;
1654 if(strcasecmp(str, "AL_FORMAT_QUAD16") == 0) return AL_FORMAT_QUAD16;
1655 if(strcasecmp(str, "AL_FORMAT_51CHN16") == 0) return AL_FORMAT_51CHN16;
1656 if(strcasecmp(str, "AL_FORMAT_61CHN16") == 0) return AL_FORMAT_61CHN16;
1657 if(strcasecmp(str, "AL_FORMAT_71CHN16") == 0) return AL_FORMAT_71CHN16;
1659 if(strcasecmp(str, "AL_FORMAT_MONO8") == 0) return AL_FORMAT_MONO8;
1660 if(strcasecmp(str, "AL_FORMAT_STEREO8") == 0) return AL_FORMAT_STEREO8;
1661 if(strcasecmp(str, "AL_FORMAT_QUAD8") == 0) return AL_FORMAT_QUAD8;
1662 if(strcasecmp(str, "AL_FORMAT_51CHN8") == 0) return AL_FORMAT_51CHN8;
1663 if(strcasecmp(str, "AL_FORMAT_61CHN8") == 0) return AL_FORMAT_61CHN8;
1664 if(strcasecmp(str, "AL_FORMAT_71CHN8") == 0) return AL_FORMAT_71CHN8;
1666 AL_PRINT("Unknown format: \"%s\"\n", str);
1667 return AL_FORMAT_STEREO16;
1671 alcOpenDevice
1673 Open the Device specified.
1675 ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName)
1677 ALboolean bDeviceFound = AL_FALSE;
1678 const ALCchar *fmt;
1679 ALCdevice *device;
1680 ALint i;
1682 if(deviceName && !deviceName[0])
1683 deviceName = NULL;
1685 device = calloc(1, sizeof(ALCdevice));
1686 if(!device)
1688 alcSetError(NULL, ALC_OUT_OF_MEMORY);
1689 return NULL;
1692 //Validate device
1693 device->Connected = ALC_TRUE;
1694 device->IsCaptureDevice = AL_FALSE;
1695 device->LastError = ALC_NO_ERROR;
1697 device->Bs2b = NULL;
1698 device->szDeviceName = NULL;
1700 device->Contexts = NULL;
1701 device->NumContexts = 0;
1703 //Set output format
1704 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1705 if(device->Frequency < 8000)
1706 device->Frequency = 8000;
1708 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1709 device->Format = GetFormatFromString(fmt);
1711 device->NumUpdates = GetConfigValueInt(NULL, "periods", 4);
1712 if(device->NumUpdates < 2)
1713 device->NumUpdates = 4;
1715 i = GetConfigValueInt(NULL, "refresh", 4096);
1716 if(i <= 0) i = 4096;
1718 device->UpdateSize = GetConfigValueInt(NULL, "period_size", i/device->NumUpdates);
1719 if(device->UpdateSize <= 0)
1720 device->UpdateSize = i/device->NumUpdates;
1722 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1723 if((ALint)device->MaxNoOfSources <= 0)
1724 device->MaxNoOfSources = 256;
1726 device->AuxiliaryEffectSlotMax = GetConfigValueInt(NULL, "slots", 4);
1727 if((ALint)device->AuxiliaryEffectSlotMax <= 0)
1728 device->AuxiliaryEffectSlotMax = 4;
1730 device->lNumStereoSources = 1;
1731 device->lNumMonoSources = device->MaxNoOfSources - device->lNumStereoSources;
1733 device->NumAuxSends = GetConfigValueInt(NULL, "sends", MAX_SENDS);
1734 if(device->NumAuxSends > MAX_SENDS)
1735 device->NumAuxSends = MAX_SENDS;
1737 device->Bs2bLevel = GetConfigValueInt(NULL, "cf_level", 0);
1739 if(aluChannelsFromFormat(device->Format) <= 2)
1741 device->HeadDampen = GetConfigValueFloat(NULL, "head_dampen", DEFAULT_HEAD_DAMPEN);
1742 device->HeadDampen = __min(device->HeadDampen, 1.0f);
1743 device->HeadDampen = __max(device->HeadDampen, 0.0f);
1745 else
1746 device->HeadDampen = 0.0f;
1748 // Find a playback device to open
1749 SuspendContext(NULL);
1750 for(i = 0;BackendList[i].Init;i++)
1752 device->Funcs = &BackendList[i].Funcs;
1753 if(ALCdevice_OpenPlayback(device, deviceName))
1755 device->next = g_pDeviceList;
1756 g_pDeviceList = device;
1757 g_ulDeviceCount++;
1759 bDeviceFound = AL_TRUE;
1760 break;
1763 ProcessContext(NULL);
1765 if(!bDeviceFound)
1767 // No suitable output device found
1768 alcSetError(NULL, ALC_INVALID_VALUE);
1769 free(device);
1770 device = NULL;
1773 return device;
1778 alcCloseDevice
1780 Close the specified Device
1782 ALC_API ALCboolean ALC_APIENTRY alcCloseDevice(ALCdevice *pDevice)
1784 ALCdevice **list;
1786 if(!IsDevice(pDevice) || pDevice->IsCaptureDevice)
1788 alcSetError(pDevice, ALC_INVALID_DEVICE);
1789 return ALC_FALSE;
1792 SuspendContext(NULL);
1794 list = &g_pDeviceList;
1795 while(*list != pDevice)
1796 list = &(*list)->next;
1798 *list = (*list)->next;
1799 g_ulDeviceCount--;
1801 ProcessContext(NULL);
1803 if(pDevice->NumContexts > 0)
1805 #ifdef _DEBUG
1806 AL_PRINT("alcCloseDevice(): destroying %u Context(s)\n", pDevice->NumContexts);
1807 #endif
1808 while(pDevice->NumContexts > 0)
1809 alcDestroyContext(pDevice->Contexts[0]);
1811 ALCdevice_ClosePlayback(pDevice);
1813 if(pDevice->BufferCount > 0)
1815 #ifdef _DEBUG
1816 AL_PRINT("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice->BufferCount);
1817 #endif
1818 ReleaseALBuffers(pDevice);
1820 if(pDevice->EffectCount > 0)
1822 #ifdef _DEBUG
1823 AL_PRINT("alcCloseDevice(): deleting %d Effect(s)\n", pDevice->EffectCount);
1824 #endif
1825 ReleaseALEffects(pDevice);
1827 if(pDevice->FilterCount > 0)
1829 #ifdef _DEBUG
1830 AL_PRINT("alcCloseDevice(): deleting %d Filter(s)\n", pDevice->FilterCount);
1831 #endif
1832 ReleaseALFilters(pDevice);
1834 if(pDevice->DatabufferCount > 0)
1836 #ifdef _DEBUG
1837 AL_PRINT("alcCloseDevice(): deleting %d Databuffer(s)\n", pDevice->DatabufferCount);
1838 #endif
1839 ReleaseALDatabuffers(pDevice);
1842 free(pDevice->Bs2b);
1843 pDevice->Bs2b = NULL;
1845 free(pDevice->szDeviceName);
1846 pDevice->szDeviceName = NULL;
1848 free(pDevice->Contexts);
1849 pDevice->Contexts = NULL;
1851 //Release device structure
1852 memset(pDevice, 0, sizeof(ALCdevice));
1853 free(pDevice);
1855 return ALC_TRUE;
1859 ALCvoid ReleaseALC(ALCvoid)
1861 free(alcDeviceList); alcDeviceList = NULL;
1862 alcDeviceListSize = 0;
1863 free(alcAllDeviceList); alcAllDeviceList = NULL;
1864 alcAllDeviceListSize = 0;
1865 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
1866 alcCaptureDeviceListSize = 0;
1868 free(alcDefaultDeviceSpecifier);
1869 alcDefaultDeviceSpecifier = NULL;
1870 free(alcDefaultAllDeviceSpecifier);
1871 alcDefaultAllDeviceSpecifier = NULL;
1872 free(alcCaptureDefaultDeviceSpecifier);
1873 alcCaptureDefaultDeviceSpecifier = NULL;
1875 #ifdef _DEBUG
1876 if(g_ulDeviceCount > 0)
1877 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
1878 #endif
1880 while(g_pDeviceList)
1882 if(g_pDeviceList->IsCaptureDevice)
1883 alcCaptureCloseDevice(g_pDeviceList);
1884 else
1885 alcCloseDevice(g_pDeviceList);
1889 ///////////////////////////////////////////////////////