Protect some capture calls with a mutex
[openal-soft/android/lowlatency.git] / Alc / ALc.c
blob78d25a283ac03bec85d003affa8b323a74ebbd2f
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 const ALCchar alcExtensionList[] =
184 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
185 "ALC_EXT_disconnect ALC_EXT_EFX ALC_EXTX_thread_local_context";
186 static const ALCint alcMajorVersion = 1;
187 static const ALCint alcMinorVersion = 1;
189 static const ALCint alcEFXMajorVersion = 1;
190 static const ALCint alcEFXMinorVersion = 0;
192 ///////////////////////////////////////////////////////
195 ///////////////////////////////////////////////////////
196 // Global Variables
198 static ALCdevice *g_pDeviceList = NULL;
199 static ALCuint g_ulDeviceCount = 0;
201 static CRITICAL_SECTION g_csMutex;
203 // Context List
204 static ALCcontext *g_pContextList = NULL;
205 static ALCuint g_ulContextCount = 0;
207 // Thread-local current context
208 static tls_type LocalContext;
210 // Context Error
211 static ALCenum g_eLastContextError = ALC_NO_ERROR;
213 // Default context extensions
214 static const ALchar alExtList[] =
215 "AL_EXTX_buffer_sub_data AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 "
216 "AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_OFFSET "
217 "AL_EXTX_sample_buffer_object AL_EXT_source_distance_model "
218 "AL_LOKI_quadriphonic";
220 // Mixing Priority Level
221 ALint RTPrioLevel;
223 // Resampler Quality
224 resampler_t DefaultResampler;
226 ///////////////////////////////////////////////////////
229 ///////////////////////////////////////////////////////
230 // ALC Related helper functions
231 #ifdef _WIN32
232 static void alc_init(void);
233 static void alc_deinit(void);
235 BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
237 (void)lpReserved;
239 // Perform actions based on the reason for calling.
240 switch(ul_reason_for_call)
242 case DLL_PROCESS_ATTACH:
243 DisableThreadLibraryCalls(hModule);
244 alc_init();
245 break;
247 case DLL_PROCESS_DETACH:
248 alc_deinit();
249 break;
251 return TRUE;
253 #else
254 #ifdef HAVE_GCC_DESTRUCTOR
255 static void alc_init(void) __attribute__((constructor));
256 static void alc_deinit(void) __attribute__((destructor));
257 #endif
258 #endif
260 static void alc_init(void)
262 int i;
263 const char *devs, *str;
265 InitializeCriticalSection(&g_csMutex);
266 ALTHUNK_INIT();
267 ReadALConfig();
269 tls_create(&LocalContext);
271 RTPrioLevel = GetConfigValueInt(NULL, "rt-prio", 0);
273 DefaultResampler = GetConfigValueInt(NULL, "resampler", RESAMPLER_DEFAULT);
274 if(DefaultResampler >= RESAMPLER_MAX || DefaultResampler <= RESAMPLER_MIN)
275 DefaultResampler = RESAMPLER_DEFAULT;
277 devs = GetConfigValue(NULL, "drivers", "");
278 if(devs[0])
280 int n;
281 size_t len;
282 const char *next = devs;
283 int endlist;
285 i = 0;
286 do {
287 devs = next;
288 next = strchr(devs, ',');
290 if(!devs[0] || devs[0] == ',')
292 endlist = 0;
293 continue;
295 endlist = 1;
297 len = (next ? ((size_t)(next-devs)) : strlen(devs));
298 for(n = i;BackendList[n].Init;n++)
300 if(len == strlen(BackendList[n].name) &&
301 strncmp(BackendList[n].name, devs, len) == 0)
303 BackendInfo Bkp = BackendList[n];
304 while(n > i)
306 BackendList[n] = BackendList[n-1];
307 --n;
309 BackendList[n] = Bkp;
311 i++;
312 break;
315 } while(next++);
317 if(endlist)
319 BackendList[i].name = NULL;
320 BackendList[i].Init = NULL;
321 BackendList[i].Deinit = NULL;
322 BackendList[i].Probe = NULL;
326 for(i = 0;BackendList[i].Init;i++)
327 BackendList[i].Init(&BackendList[i].Funcs);
329 DuplicateStereo = GetConfigValueBool(NULL, "stereodup", 0);
331 str = GetConfigValue(NULL, "excludefx", "");
332 if(str[0])
334 const struct {
335 const char *name;
336 int type;
337 } EffectList[] = {
338 { "eaxreverb", EAXREVERB },
339 { "reverb", REVERB },
340 { "echo", ECHO },
341 { NULL, 0 }
343 int n;
344 size_t len;
345 const char *next = str;
347 do {
348 str = next;
349 next = strchr(str, ',');
351 if(!str[0] || next == str)
352 continue;
354 len = (next ? ((size_t)(next-str)) : strlen(str));
355 for(n = 0;EffectList[n].name;n++)
357 if(len == strlen(EffectList[n].name) &&
358 strncmp(EffectList[n].name, str, len) == 0)
359 DisabledEffects[EffectList[n].type] = AL_TRUE;
361 } while(next++);
365 static void alc_deinit(void)
367 int i;
369 ReleaseALC();
371 for(i = 0;BackendList[i].Deinit;i++)
372 BackendList[i].Deinit();
374 tls_delete(LocalContext);
376 FreeALConfig();
377 ALTHUNK_EXIT();
378 DeleteCriticalSection(&g_csMutex);
382 static void ProbeDeviceList()
384 ALint i;
386 free(alcDeviceList); alcDeviceList = NULL;
387 alcDeviceListSize = 0;
389 for(i = 0;BackendList[i].Probe;i++)
390 BackendList[i].Probe(DEVICE_PROBE);
393 static void ProbeAllDeviceList()
395 ALint i;
397 free(alcAllDeviceList); alcAllDeviceList = NULL;
398 alcAllDeviceListSize = 0;
400 for(i = 0;BackendList[i].Probe;i++)
401 BackendList[i].Probe(ALL_DEVICE_PROBE);
404 static void ProbeCaptureDeviceList()
406 ALint i;
408 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
409 alcCaptureDeviceListSize = 0;
411 for(i = 0;BackendList[i].Probe;i++)
412 BackendList[i].Probe(CAPTURE_DEVICE_PROBE);
416 #define DECL_APPEND_LIST_FUNC(type) \
417 void Append##type##List(const ALCchar *name) \
419 ALCuint len = strlen(name); \
420 void *temp; \
422 if(len == 0) \
423 return; \
425 temp = realloc(alc##type##List, alc##type##ListSize + len + 2); \
426 if(!temp) \
428 AL_PRINT("Realloc failed to add %s!\n", name); \
429 return; \
431 alc##type##List = temp; \
432 sprintf(alc##type##List+alc##type##ListSize, "%s", name); \
433 alc##type##ListSize += len+1; \
434 alc##type##List[alc##type##ListSize] = 0; \
437 DECL_APPEND_LIST_FUNC(Device)
438 DECL_APPEND_LIST_FUNC(AllDevice)
439 DECL_APPEND_LIST_FUNC(CaptureDevice)
442 void al_print(const char *fname, unsigned int line, const char *fmt, ...)
444 const char *fn;
445 char str[256];
446 int i;
448 fn = strrchr(fname, '/');
449 if(!fn) fn = strrchr(fname, '\\');;
450 if(!fn) fn = fname;
451 else fn += 1;
453 i = snprintf(str, sizeof(str), "AL lib: %s:%d: ", fn, line);
454 if(i < (int)sizeof(str) && i > 0)
456 va_list ap;
457 va_start(ap, fmt);
458 vsnprintf(str+i, sizeof(str)-i, fmt, ap);
459 va_end(ap);
461 str[sizeof(str)-1] = 0;
463 fprintf(stderr, "%s", str);
466 void EnableRTPrio(ALint level)
468 ALboolean failed;
470 #ifdef _WIN32
471 if(level > 0)
472 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
473 else
474 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
475 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM)
476 struct sched_param param;
478 if(level > 0)
480 /* Use the minimum real-time priority possible for now (on Linux this
481 * should be 1 for SCHED_RR) */
482 param.sched_priority = sched_get_priority_min(SCHED_RR);
483 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
485 else
487 param.sched_priority = 0;
488 failed = !!pthread_setschedparam(pthread_self(), SCHED_OTHER, &param);
490 #else
491 /* Real-time priority not available */
492 failed = !!level;
493 #endif
494 if(failed)
495 AL_PRINT("Failed to set priority level for thread\n");
500 IsDevice
502 Check pDevice is a valid Device pointer
504 static ALCboolean IsDevice(ALCdevice *pDevice)
506 ALCdevice *pTempDevice;
508 SuspendContext(NULL);
510 pTempDevice = g_pDeviceList;
511 while(pTempDevice && pTempDevice != pDevice)
512 pTempDevice = pTempDevice->next;
514 ProcessContext(NULL);
516 return (pTempDevice ? ALC_TRUE : ALC_FALSE);
520 IsContext
522 Check pContext is a valid Context pointer
524 static ALCboolean IsContext(ALCcontext *pContext)
526 ALCcontext *pTempContext;
528 SuspendContext(NULL);
530 pTempContext = g_pContextList;
531 while (pTempContext && pTempContext != pContext)
532 pTempContext = pTempContext->next;
534 ProcessContext(NULL);
536 return (pTempContext ? ALC_TRUE : ALC_FALSE);
541 alcSetError
543 Store latest ALC Error
545 ALCvoid alcSetError(ALCdevice *device, ALenum errorCode)
547 if(IsDevice(device))
548 device->LastError = errorCode;
549 else
550 g_eLastContextError = errorCode;
555 SuspendContext
557 Thread-safe entry
559 ALCvoid SuspendContext(ALCcontext *pContext)
561 (void)pContext;
562 EnterCriticalSection(&g_csMutex);
567 ProcessContext
569 Thread-safe exit
571 ALCvoid ProcessContext(ALCcontext *pContext)
573 (void)pContext;
574 LeaveCriticalSection(&g_csMutex);
579 GetContextSuspended
581 Returns the currently active Context, in a locked state
583 ALCcontext *GetContextSuspended(void)
585 ALCcontext *pContext = NULL;
587 SuspendContext(NULL);
589 pContext = tls_get(LocalContext);
590 if(pContext && !IsContext(pContext))
592 tls_set(LocalContext, NULL);
593 pContext = NULL;
595 if(!pContext)
597 pContext = g_pContextList;
598 while(pContext && !pContext->InUse)
599 pContext = pContext->next;
601 if(pContext)
602 SuspendContext(pContext);
604 ProcessContext(NULL);
606 return pContext;
611 InitContext
613 Initialize Context variables
615 static ALvoid InitContext(ALCcontext *pContext)
617 //Initialise listener
618 pContext->Listener.Gain = 1.0f;
619 pContext->Listener.MetersPerUnit = 1.0f;
620 pContext->Listener.Position[0] = 0.0f;
621 pContext->Listener.Position[1] = 0.0f;
622 pContext->Listener.Position[2] = 0.0f;
623 pContext->Listener.Velocity[0] = 0.0f;
624 pContext->Listener.Velocity[1] = 0.0f;
625 pContext->Listener.Velocity[2] = 0.0f;
626 pContext->Listener.Forward[0] = 0.0f;
627 pContext->Listener.Forward[1] = 0.0f;
628 pContext->Listener.Forward[2] = -1.0f;
629 pContext->Listener.Up[0] = 0.0f;
630 pContext->Listener.Up[1] = 1.0f;
631 pContext->Listener.Up[2] = 0.0f;
633 //Validate pContext
634 pContext->LastError = AL_NO_ERROR;
635 pContext->InUse = AL_FALSE;
636 pContext->Suspended = AL_FALSE;
638 //Set globals
639 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
640 pContext->SourceDistanceModel = AL_FALSE;
641 pContext->DopplerFactor = 1.0f;
642 pContext->DopplerVelocity = 1.0f;
643 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
645 pContext->ExtensionList = alExtList;
650 ExitContext
652 Clean up Context, destroy any remaining Sources
654 static ALCvoid ExitContext(ALCcontext *pContext)
656 //Invalidate context
657 pContext->LastError = AL_NO_ERROR;
658 pContext->InUse = AL_FALSE;
661 ///////////////////////////////////////////////////////
664 ///////////////////////////////////////////////////////
665 // ALC Functions calls
668 // This should probably move to another c file but for now ...
669 ALCAPI ALCdevice* ALCAPIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
671 ALCboolean DeviceFound = ALC_FALSE;
672 ALCdevice *pDevice = NULL;
673 ALCint i;
675 if(SampleSize <= 0)
677 alcSetError(NULL, ALC_INVALID_VALUE);
678 return NULL;
681 if(deviceName && !deviceName[0])
682 deviceName = NULL;
684 pDevice = malloc(sizeof(ALCdevice));
685 if (pDevice)
687 //Initialise device structure
688 memset(pDevice, 0, sizeof(ALCdevice));
690 //Validate device
691 pDevice->Connected = ALC_TRUE;
692 pDevice->IsCaptureDevice = AL_TRUE;
694 pDevice->szDeviceName = NULL;
696 pDevice->Frequency = frequency;
697 pDevice->Format = format;
698 pDevice->UpdateSize = SampleSize;
699 pDevice->NumUpdates = 1;
701 SuspendContext(NULL);
702 for(i = 0;BackendList[i].Init;i++)
704 pDevice->Funcs = &BackendList[i].Funcs;
705 if(ALCdevice_OpenCapture(pDevice, deviceName))
707 pDevice->next = g_pDeviceList;
708 g_pDeviceList = pDevice;
709 g_ulDeviceCount++;
711 DeviceFound = ALC_TRUE;
712 break;
715 ProcessContext(NULL);
717 if(!DeviceFound)
719 alcSetError(NULL, ALC_INVALID_VALUE);
720 free(pDevice);
721 pDevice = NULL;
724 else
725 alcSetError(NULL, ALC_OUT_OF_MEMORY);
727 return pDevice;
730 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
732 ALCboolean bReturn = ALC_FALSE;
733 ALCdevice **list;
735 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
737 SuspendContext(NULL);
739 list = &g_pDeviceList;
740 while(*list != pDevice)
741 list = &(*list)->next;
743 *list = (*list)->next;
744 g_ulDeviceCount--;
746 ProcessContext(NULL);
748 ALCdevice_CloseCapture(pDevice);
750 free(pDevice->szDeviceName);
751 pDevice->szDeviceName = NULL;
753 free(pDevice);
755 bReturn = ALC_TRUE;
757 else
758 alcSetError(pDevice, ALC_INVALID_DEVICE);
760 return bReturn;
763 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
765 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
767 SuspendContext(NULL);
768 ALCdevice_StartCapture(pDevice);
769 ProcessContext(NULL);
771 else
772 alcSetError(pDevice, ALC_INVALID_DEVICE);
775 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
777 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
779 SuspendContext(NULL);
780 ALCdevice_StopCapture(pDevice);
781 ProcessContext(NULL);
783 else
784 alcSetError(pDevice, ALC_INVALID_DEVICE);
787 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
789 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
791 SuspendContext(NULL);
792 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
793 ProcessContext(NULL);
795 else
796 alcSetError(pDevice, ALC_INVALID_DEVICE);
800 alcGetError
802 Return last ALC generated error code
804 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
806 ALCenum errorCode = ALC_NO_ERROR;
808 if(IsDevice(device))
810 errorCode = device->LastError;
811 device->LastError = ALC_NO_ERROR;
813 else
815 errorCode = g_eLastContextError;
816 g_eLastContextError = ALC_NO_ERROR;
818 return errorCode;
823 alcSuspendContext
825 Not functional
827 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
829 SuspendContext(NULL);
830 if(IsContext(pContext))
831 pContext->Suspended = AL_TRUE;
832 ProcessContext(NULL);
837 alcProcessContext
839 Not functional
841 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
843 SuspendContext(NULL);
844 if(IsContext(pContext))
845 pContext->Suspended = AL_FALSE;
846 ProcessContext(NULL);
851 alcGetString
853 Returns information about the Device, and error strings
855 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
857 const ALCchar *value = NULL;
859 switch (param)
861 case ALC_NO_ERROR:
862 value = alcNoError;
863 break;
865 case ALC_INVALID_ENUM:
866 value = alcErrInvalidEnum;
867 break;
869 case ALC_INVALID_VALUE:
870 value = alcErrInvalidValue;
871 break;
873 case ALC_INVALID_DEVICE:
874 value = alcErrInvalidDevice;
875 break;
877 case ALC_INVALID_CONTEXT:
878 value = alcErrInvalidContext;
879 break;
881 case ALC_OUT_OF_MEMORY:
882 value = alcErrOutOfMemory;
883 break;
885 case ALC_DEVICE_SPECIFIER:
886 if(IsDevice(pDevice))
887 value = pDevice->szDeviceName;
888 else
890 ProbeDeviceList();
891 value = alcDeviceList;
893 break;
895 case ALC_ALL_DEVICES_SPECIFIER:
896 ProbeAllDeviceList();
897 value = alcAllDeviceList;
898 break;
900 case ALC_CAPTURE_DEVICE_SPECIFIER:
901 if(IsDevice(pDevice))
902 value = pDevice->szDeviceName;
903 else
905 ProbeCaptureDeviceList();
906 value = alcCaptureDeviceList;
908 break;
910 /* Default devices are always first in the list */
911 case ALC_DEFAULT_DEVICE_SPECIFIER:
912 if(!alcDeviceList)
913 ProbeDeviceList();
915 free(alcDefaultDeviceSpecifier);
916 alcDefaultDeviceSpecifier = strdup(alcDeviceList ? alcDeviceList : "");
917 if(!alcDefaultDeviceSpecifier)
918 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
919 value = alcDefaultDeviceSpecifier;
920 break;
922 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
923 if(!alcAllDeviceList)
924 ProbeAllDeviceList();
926 free(alcDefaultAllDeviceSpecifier);
927 alcDefaultAllDeviceSpecifier = strdup(alcAllDeviceList ?
928 alcAllDeviceList : "");
929 if(!alcDefaultAllDeviceSpecifier)
930 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
931 value = alcDefaultAllDeviceSpecifier;
932 break;
934 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
935 if(!alcCaptureDeviceList)
936 ProbeCaptureDeviceList();
938 free(alcCaptureDefaultDeviceSpecifier);
939 alcCaptureDefaultDeviceSpecifier = strdup(alcCaptureDeviceList ?
940 alcCaptureDeviceList : "");
941 if(!alcCaptureDefaultDeviceSpecifier)
942 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
943 value = alcCaptureDefaultDeviceSpecifier;
944 break;
946 case ALC_EXTENSIONS:
947 value = alcExtensionList;
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 ALCAPI ALCvoid ALCAPIENTRY 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 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
1118 ALCboolean bResult = ALC_FALSE;
1120 if (extName)
1122 const char *ptr;
1123 size_t len;
1125 len = strlen(extName);
1126 ptr = alcExtensionList;
1127 while(ptr && *ptr)
1129 if(strncasecmp(ptr, extName, len) == 0 &&
1130 (ptr[len] == '\0' || isspace(ptr[len])))
1132 bResult = ALC_TRUE;
1133 break;
1135 if((ptr=strchr(ptr, ' ')) != NULL)
1137 do {
1138 ++ptr;
1139 } while(isspace(*ptr));
1143 else
1144 alcSetError(device, ALC_INVALID_VALUE);
1146 return bResult;
1151 alcGetProcAddress
1153 Retrieves the function address for a particular extension function
1155 ALCAPI ALCvoid * ALCAPIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
1157 ALCvoid *pFunction = NULL;
1158 ALsizei i = 0;
1160 if (funcName)
1162 while(alcFunctions[i].funcName &&
1163 strcmp(alcFunctions[i].funcName,funcName) != 0)
1164 i++;
1165 pFunction = alcFunctions[i].address;
1167 else
1168 alcSetError(device, ALC_INVALID_VALUE);
1170 return pFunction;
1175 alcGetEnumValue
1177 Get the value for a particular ALC Enumerated Value
1179 ALCAPI ALCenum ALCAPIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
1181 ALsizei i = 0;
1182 ALCenum val;
1184 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
1185 i++;
1186 val = enumeration[i].value;
1188 if(!enumeration[i].enumName)
1189 alcSetError(device, ALC_INVALID_VALUE);
1191 return val;
1196 alcCreateContext
1198 Create and attach a Context to a particular Device.
1200 ALCAPI ALCcontext* ALCAPIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
1202 ALuint attrIdx, reqStereoSources;
1203 ALCcontext *ALContext;
1204 void *temp;
1205 ALuint i;
1207 SuspendContext(NULL);
1209 if(!IsDevice(device) || device->IsCaptureDevice || !device->Connected)
1211 alcSetError(device, ALC_INVALID_DEVICE);
1212 ProcessContext(NULL);
1213 return NULL;
1216 // Reset Context Last Error code
1217 device->LastError = ALC_NO_ERROR;
1219 // If a context is already running on the device, stop playback so the
1220 // device attributes can be updated
1221 if(device->NumContexts > 0)
1223 ProcessContext(NULL);
1224 ALCdevice_StopPlayback(device);
1225 SuspendContext(NULL);
1228 // Check for attributes
1229 if(attrList)
1231 ALCint level = device->Bs2bLevel;
1232 ALCuint freq = device->Frequency;
1233 ALCint numMono = device->lNumMonoSources;
1234 ALCint numStereo = device->lNumStereoSources;
1235 ALCuint numSends = device->NumAuxSends;
1237 attrIdx = 0;
1238 while(attrList[attrIdx])
1240 if(attrList[attrIdx] == ALC_FREQUENCY &&
1241 !ConfigValueExists(NULL, "frequency"))
1243 freq = attrList[attrIdx + 1];
1244 if(freq < 8000)
1245 freq = 8000;
1248 if(attrList[attrIdx] == ALC_STEREO_SOURCES)
1250 reqStereoSources = attrList[attrIdx + 1];
1251 if(reqStereoSources > device->MaxNoOfSources)
1252 reqStereoSources = device->MaxNoOfSources;
1254 numStereo = reqStereoSources;
1255 numMono = device->MaxNoOfSources - numStereo;
1258 if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS &&
1259 !ConfigValueExists(NULL, "sends"))
1261 numSends = attrList[attrIdx + 1];
1262 if(numSends > MAX_SENDS)
1263 numSends = MAX_SENDS;
1266 attrIdx += 2;
1269 device->UpdateSize = (ALuint64)device->UpdateSize * freq /
1270 device->Frequency;
1272 device->Bs2bLevel = level;
1273 device->Frequency = freq;
1274 device->lNumMonoSources = numMono;
1275 device->lNumStereoSources = numStereo;
1276 device->NumAuxSends = numSends;
1279 if(ALCdevice_ResetPlayback(device) == ALC_FALSE)
1281 alcSetError(device, ALC_INVALID_DEVICE);
1282 aluHandleDisconnect(device);
1283 ProcessContext(NULL);
1284 return NULL;
1287 for(i = 0;i < device->NumContexts;i++)
1289 ALCcontext *context = device->Contexts[i];
1290 ALeffectslot *slot;
1291 ALsource *source;
1293 SuspendContext(context);
1294 for(slot = context->AuxiliaryEffectSlot;slot != NULL;slot = slot->next)
1296 if(!slot->EffectState)
1297 continue;
1299 if(ALEffect_DeviceUpdate(slot->EffectState, device) == AL_FALSE)
1301 alcSetError(device, ALC_INVALID_DEVICE);
1302 aluHandleDisconnect(device);
1303 ProcessContext(context);
1304 ProcessContext(NULL);
1305 ALCdevice_StopPlayback(device);
1306 return NULL;
1308 ALEffect_Update(slot->EffectState, context, &slot->effect);
1311 for(source = context->Source;source != NULL;source = source->next)
1313 ALuint s = device->NumAuxSends;
1314 while(s < MAX_SENDS)
1316 if(source->Send[s].Slot)
1317 source->Send[s].Slot->refcount--;
1318 source->Send[s].Slot = NULL;
1319 source->Send[s].WetFilter.type = 0;
1320 source->Send[s].WetFilter.filter = 0;
1321 s++;
1323 source->NeedsUpdate = AL_TRUE;
1325 aluInitPanning(context);
1326 ProcessContext(context);
1329 if(device->Bs2bLevel > 0 && device->Bs2bLevel <= 6)
1331 if(!device->Bs2b)
1333 device->Bs2b = calloc(1, sizeof(*device->Bs2b));
1334 bs2b_clear(device->Bs2b);
1336 bs2b_set_srate(device->Bs2b, device->Frequency);
1337 bs2b_set_level(device->Bs2b, device->Bs2bLevel);
1339 else
1341 free(device->Bs2b);
1342 device->Bs2b = NULL;
1345 temp = realloc(device->Contexts, (device->NumContexts+1) * sizeof(*device->Contexts));
1346 if(!temp)
1348 alcSetError(device, ALC_OUT_OF_MEMORY);
1349 ProcessContext(NULL);
1350 return NULL;
1352 device->Contexts = temp;
1354 ALContext = calloc(1, sizeof(ALCcontext));
1355 if(!ALContext)
1357 alcSetError(device, ALC_OUT_OF_MEMORY);
1358 ProcessContext(NULL);
1359 return NULL;
1362 device->Contexts[device->NumContexts++] = ALContext;
1363 ALContext->Device = device;
1365 InitContext(ALContext);
1366 aluInitPanning(ALContext);
1368 ALContext->next = g_pContextList;
1369 g_pContextList = ALContext;
1370 g_ulContextCount++;
1372 ProcessContext(NULL);
1374 return ALContext;
1379 alcDestroyContext
1381 Remove a Context
1383 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
1385 ALCcontext **list;
1386 ALuint i;
1388 if (IsContext(context))
1390 ALCdevice *Device = context->Device;
1392 if(Device->NumContexts == 1)
1393 ALCdevice_StopPlayback(Device);
1395 SuspendContext(NULL);
1397 for(i = 0;i < Device->NumContexts-1;i++)
1399 if(Device->Contexts[i] == context)
1401 memmove(&Device->Contexts[i], &Device->Contexts[i+1],
1402 (Device->NumContexts-i-1) * sizeof(*Device->Contexts));
1403 break;
1406 Device->NumContexts--;
1408 // Lock context
1409 SuspendContext(context);
1411 if(context->SourceCount > 0)
1413 #ifdef _DEBUG
1414 AL_PRINT("alcDestroyContext(): deleting %d Source(s)\n", context->SourceCount);
1415 #endif
1416 ReleaseALSources(context);
1418 if(context->AuxiliaryEffectSlotCount > 0)
1420 #ifdef _DEBUG
1421 AL_PRINT("alcDestroyContext(): deleting %d AuxiliaryEffectSlot(s)\n", context->AuxiliaryEffectSlotCount);
1422 #endif
1423 ReleaseALAuxiliaryEffectSlots(context);
1426 list = &g_pContextList;
1427 while(*list != context)
1428 list = &(*list)->next;
1430 *list = (*list)->next;
1431 g_ulContextCount--;
1433 // Unlock context
1434 ProcessContext(context);
1435 ProcessContext(NULL);
1437 ExitContext(context);
1439 // Free memory (MUST do this after ProcessContext)
1440 memset(context, 0, sizeof(ALCcontext));
1441 free(context);
1443 else
1444 alcSetError(NULL, ALC_INVALID_CONTEXT);
1449 alcGetCurrentContext
1451 Returns the currently active Context
1453 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1455 ALCcontext *pContext;
1457 if((pContext=GetContextSuspended()) != NULL)
1458 ProcessContext(pContext);
1460 return pContext;
1464 alcGetThreadContext
1466 Returns the currently active thread-local Context
1468 ALCcontext * ALCAPIENTRY alcGetThreadContext(void)
1470 ALCcontext *pContext = NULL;
1472 SuspendContext(NULL);
1474 pContext = tls_get(LocalContext);
1475 if(pContext && !IsContext(pContext))
1477 tls_set(LocalContext, NULL);
1478 pContext = NULL;
1481 ProcessContext(NULL);
1483 return pContext;
1488 alcGetContextsDevice
1490 Returns the Device that a particular Context is attached to
1492 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1494 ALCdevice *pDevice = NULL;
1496 SuspendContext(NULL);
1497 if (IsContext(pContext))
1498 pDevice = pContext->Device;
1499 else
1500 alcSetError(NULL, ALC_INVALID_CONTEXT);
1501 ProcessContext(NULL);
1503 return pDevice;
1508 alcMakeContextCurrent
1510 Makes the given Context the active Context
1512 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1514 ALCcontext *ALContext;
1515 ALboolean bReturn = AL_TRUE;
1517 SuspendContext(NULL);
1519 // context must be a valid Context or NULL
1520 if(context == NULL || IsContext(context))
1522 ALContext = g_pContextList;
1523 while(ALContext && !ALContext->InUse)
1524 ALContext = ALContext->next;
1526 if(ALContext != NULL)
1528 SuspendContext(ALContext);
1529 ALContext->InUse=AL_FALSE;
1530 ProcessContext(ALContext);
1533 if((ALContext=context) != NULL && ALContext->Device)
1535 SuspendContext(ALContext);
1536 ALContext->InUse=AL_TRUE;
1537 ProcessContext(ALContext);
1540 tls_set(LocalContext, NULL);
1542 else
1544 alcSetError(NULL, ALC_INVALID_CONTEXT);
1545 bReturn = AL_FALSE;
1548 ProcessContext(NULL);
1550 return bReturn;
1554 alcMakeCurrent
1556 Makes the given Context the active Context for the current thread
1558 ALCboolean ALCAPIENTRY alcMakeCurrent(ALCcontext *context)
1560 ALboolean bReturn = AL_TRUE;
1562 SuspendContext(NULL);
1564 // context must be a valid Context or NULL
1565 if(context == NULL || IsContext(context))
1566 tls_set(LocalContext, context);
1567 else
1569 alcSetError(NULL, ALC_INVALID_CONTEXT);
1570 bReturn = AL_FALSE;
1573 ProcessContext(NULL);
1575 return bReturn;
1579 // Sets the default channel order used by most non-WaveFormatEx-based APIs
1580 void SetDefaultChannelOrder(ALCdevice *device)
1582 switch(aluChannelsFromFormat(device->Format))
1584 case 1: device->DevChannels[0] = FRONT_CENTER; break;
1586 case 2: device->DevChannels[0] = FRONT_LEFT;
1587 device->DevChannels[1] = FRONT_RIGHT; break;
1589 case 4: device->DevChannels[0] = FRONT_LEFT;
1590 device->DevChannels[1] = FRONT_RIGHT;
1591 device->DevChannels[2] = BACK_LEFT;
1592 device->DevChannels[3] = BACK_RIGHT; break;
1594 case 6: device->DevChannels[0] = FRONT_LEFT;
1595 device->DevChannels[1] = FRONT_RIGHT;
1596 device->DevChannels[2] = BACK_LEFT;
1597 device->DevChannels[3] = BACK_RIGHT;
1598 device->DevChannels[4] = FRONT_CENTER;
1599 device->DevChannels[5] = LFE; break;
1601 case 7: device->DevChannels[0] = FRONT_LEFT;
1602 device->DevChannels[1] = FRONT_RIGHT;
1603 device->DevChannels[2] = FRONT_CENTER;
1604 device->DevChannels[3] = LFE;
1605 device->DevChannels[4] = BACK_CENTER;
1606 device->DevChannels[5] = SIDE_LEFT;
1607 device->DevChannels[6] = SIDE_RIGHT; break;
1609 case 8: device->DevChannels[0] = FRONT_LEFT;
1610 device->DevChannels[1] = FRONT_RIGHT;
1611 device->DevChannels[2] = BACK_LEFT;
1612 device->DevChannels[3] = BACK_RIGHT;
1613 device->DevChannels[4] = FRONT_CENTER;
1614 device->DevChannels[5] = LFE;
1615 device->DevChannels[6] = SIDE_LEFT;
1616 device->DevChannels[7] = SIDE_RIGHT; break;
1619 // Sets the default order used by WaveFormatEx
1620 void SetDefaultWFXChannelOrder(ALCdevice *device)
1622 switch(aluChannelsFromFormat(device->Format))
1624 case 1: device->DevChannels[0] = FRONT_CENTER; break;
1626 case 2: device->DevChannels[0] = FRONT_LEFT;
1627 device->DevChannels[1] = FRONT_RIGHT; break;
1629 case 4: device->DevChannels[0] = FRONT_LEFT;
1630 device->DevChannels[1] = FRONT_RIGHT;
1631 device->DevChannels[2] = BACK_LEFT;
1632 device->DevChannels[3] = BACK_RIGHT; break;
1634 case 6: device->DevChannels[0] = FRONT_LEFT;
1635 device->DevChannels[1] = FRONT_RIGHT;
1636 device->DevChannels[2] = FRONT_CENTER;
1637 device->DevChannels[3] = LFE;
1638 device->DevChannels[4] = BACK_LEFT;
1639 device->DevChannels[5] = BACK_RIGHT; break;
1641 case 7: device->DevChannels[0] = FRONT_LEFT;
1642 device->DevChannels[1] = FRONT_RIGHT;
1643 device->DevChannels[2] = FRONT_CENTER;
1644 device->DevChannels[3] = LFE;
1645 device->DevChannels[4] = BACK_CENTER;
1646 device->DevChannels[5] = SIDE_LEFT;
1647 device->DevChannels[6] = SIDE_RIGHT; break;
1649 case 8: device->DevChannels[0] = FRONT_LEFT;
1650 device->DevChannels[1] = FRONT_RIGHT;
1651 device->DevChannels[2] = FRONT_CENTER;
1652 device->DevChannels[3] = LFE;
1653 device->DevChannels[4] = BACK_LEFT;
1654 device->DevChannels[5] = BACK_RIGHT;
1655 device->DevChannels[6] = SIDE_LEFT;
1656 device->DevChannels[7] = SIDE_RIGHT; break;
1660 static ALenum GetFormatFromString(const char *str)
1662 if(strcasecmp(str, "AL_FORMAT_MONO32") == 0) return AL_FORMAT_MONO_FLOAT32;
1663 if(strcasecmp(str, "AL_FORMAT_STEREO32") == 0) return AL_FORMAT_STEREO_FLOAT32;
1664 if(strcasecmp(str, "AL_FORMAT_QUAD32") == 0) return AL_FORMAT_QUAD32;
1665 if(strcasecmp(str, "AL_FORMAT_51CHN32") == 0) return AL_FORMAT_51CHN32;
1666 if(strcasecmp(str, "AL_FORMAT_61CHN32") == 0) return AL_FORMAT_61CHN32;
1667 if(strcasecmp(str, "AL_FORMAT_71CHN32") == 0) return AL_FORMAT_71CHN32;
1669 if(strcasecmp(str, "AL_FORMAT_MONO16") == 0) return AL_FORMAT_MONO16;
1670 if(strcasecmp(str, "AL_FORMAT_STEREO16") == 0) return AL_FORMAT_STEREO16;
1671 if(strcasecmp(str, "AL_FORMAT_QUAD16") == 0) return AL_FORMAT_QUAD16;
1672 if(strcasecmp(str, "AL_FORMAT_51CHN16") == 0) return AL_FORMAT_51CHN16;
1673 if(strcasecmp(str, "AL_FORMAT_61CHN16") == 0) return AL_FORMAT_61CHN16;
1674 if(strcasecmp(str, "AL_FORMAT_71CHN16") == 0) return AL_FORMAT_71CHN16;
1676 if(strcasecmp(str, "AL_FORMAT_MONO8") == 0) return AL_FORMAT_MONO8;
1677 if(strcasecmp(str, "AL_FORMAT_STEREO8") == 0) return AL_FORMAT_STEREO8;
1678 if(strcasecmp(str, "AL_FORMAT_QUAD8") == 0) return AL_FORMAT_QUAD8;
1679 if(strcasecmp(str, "AL_FORMAT_51CHN8") == 0) return AL_FORMAT_51CHN8;
1680 if(strcasecmp(str, "AL_FORMAT_61CHN8") == 0) return AL_FORMAT_61CHN8;
1681 if(strcasecmp(str, "AL_FORMAT_71CHN8") == 0) return AL_FORMAT_71CHN8;
1683 AL_PRINT("Unknown format: \"%s\"\n", str);
1684 return AL_FORMAT_STEREO16;
1688 alcOpenDevice
1690 Open the Device specified.
1692 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1694 ALboolean bDeviceFound = AL_FALSE;
1695 ALCdevice *device;
1696 ALint i;
1698 if(deviceName && !deviceName[0])
1699 deviceName = NULL;
1701 device = malloc(sizeof(ALCdevice));
1702 if (device)
1704 const char *fmt;
1706 //Initialise device structure
1707 memset(device, 0, sizeof(ALCdevice));
1709 //Validate device
1710 device->Connected = ALC_TRUE;
1711 device->IsCaptureDevice = AL_FALSE;
1712 device->LastError = ALC_NO_ERROR;
1714 device->Bs2b = NULL;
1715 device->szDeviceName = NULL;
1717 device->Contexts = NULL;
1718 device->NumContexts = 0;
1720 //Set output format
1721 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1722 if(device->Frequency < 8000)
1723 device->Frequency = 8000;
1725 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1726 device->Format = GetFormatFromString(fmt);
1728 device->NumUpdates = GetConfigValueInt(NULL, "periods", 4);
1729 if(device->NumUpdates < 2)
1730 device->NumUpdates = 4;
1732 i = GetConfigValueInt(NULL, "refresh", 4096);
1733 if(i <= 0) i = 4096;
1735 device->UpdateSize = GetConfigValueInt(NULL, "period_size", i/device->NumUpdates);
1736 if(device->UpdateSize <= 0)
1737 device->UpdateSize = i/device->NumUpdates;
1739 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1740 if((ALint)device->MaxNoOfSources <= 0)
1741 device->MaxNoOfSources = 256;
1743 device->AuxiliaryEffectSlotMax = GetConfigValueInt(NULL, "slots", 4);
1744 if((ALint)device->AuxiliaryEffectSlotMax <= 0)
1745 device->AuxiliaryEffectSlotMax = 4;
1747 device->lNumStereoSources = 1;
1748 device->lNumMonoSources = device->MaxNoOfSources - device->lNumStereoSources;
1750 device->NumAuxSends = GetConfigValueInt(NULL, "sends", MAX_SENDS);
1751 if(device->NumAuxSends > MAX_SENDS)
1752 device->NumAuxSends = MAX_SENDS;
1754 device->Bs2bLevel = GetConfigValueInt(NULL, "cf_level", 0);
1756 if(aluChannelsFromFormat(device->Format) <= 2)
1758 device->HeadDampen = GetConfigValueFloat(NULL, "head_dampen", DEFAULT_HEAD_DAMPEN);
1759 device->HeadDampen = __min(device->HeadDampen, 1.0f);
1760 device->HeadDampen = __max(device->HeadDampen, 0.0f);
1762 else
1763 device->HeadDampen = 0.0f;
1765 // Find a playback device to open
1766 SuspendContext(NULL);
1767 for(i = 0;BackendList[i].Init;i++)
1769 device->Funcs = &BackendList[i].Funcs;
1770 if(ALCdevice_OpenPlayback(device, deviceName))
1772 device->next = g_pDeviceList;
1773 g_pDeviceList = device;
1774 g_ulDeviceCount++;
1776 bDeviceFound = AL_TRUE;
1777 break;
1780 ProcessContext(NULL);
1782 if (!bDeviceFound)
1784 // No suitable output device found
1785 alcSetError(NULL, ALC_INVALID_VALUE);
1786 free(device);
1787 device = NULL;
1790 else
1791 alcSetError(NULL, ALC_OUT_OF_MEMORY);
1793 return device;
1798 alcCloseDevice
1800 Close the specified Device
1802 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1804 ALCboolean bReturn = ALC_FALSE;
1805 ALCdevice **list;
1807 if(IsDevice(pDevice) && !pDevice->IsCaptureDevice)
1809 SuspendContext(NULL);
1811 list = &g_pDeviceList;
1812 while(*list != pDevice)
1813 list = &(*list)->next;
1815 *list = (*list)->next;
1816 g_ulDeviceCount--;
1818 ProcessContext(NULL);
1820 if(pDevice->NumContexts > 0)
1822 #ifdef _DEBUG
1823 AL_PRINT("alcCloseDevice(): destroying %u Context(s)\n", pDevice->NumContexts);
1824 #endif
1825 while(pDevice->NumContexts > 0)
1826 alcDestroyContext(pDevice->Contexts[0]);
1828 ALCdevice_ClosePlayback(pDevice);
1830 if(pDevice->BufferCount > 0)
1832 #ifdef _DEBUG
1833 AL_PRINT("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice->BufferCount);
1834 #endif
1835 ReleaseALBuffers(pDevice);
1837 if(pDevice->EffectCount > 0)
1839 #ifdef _DEBUG
1840 AL_PRINT("alcCloseDevice(): deleting %d Effect(s)\n", pDevice->EffectCount);
1841 #endif
1842 ReleaseALEffects(pDevice);
1844 if(pDevice->FilterCount > 0)
1846 #ifdef _DEBUG
1847 AL_PRINT("alcCloseDevice(): deleting %d Filter(s)\n", pDevice->FilterCount);
1848 #endif
1849 ReleaseALFilters(pDevice);
1851 if(pDevice->DatabufferCount > 0)
1853 #ifdef _DEBUG
1854 AL_PRINT("alcCloseDevice(): deleting %d Databuffer(s)\n", pDevice->DatabufferCount);
1855 #endif
1856 ReleaseALDatabuffers(pDevice);
1859 free(pDevice->Bs2b);
1860 pDevice->Bs2b = NULL;
1862 free(pDevice->szDeviceName);
1863 pDevice->szDeviceName = NULL;
1865 free(pDevice->Contexts);
1866 pDevice->Contexts = NULL;
1868 //Release device structure
1869 memset(pDevice, 0, sizeof(ALCdevice));
1870 free(pDevice);
1872 bReturn = ALC_TRUE;
1874 else
1875 alcSetError(pDevice, ALC_INVALID_DEVICE);
1877 return bReturn;
1881 ALCvoid ReleaseALC(ALCvoid)
1883 free(alcDeviceList); alcDeviceList = NULL;
1884 alcDeviceListSize = 0;
1885 free(alcAllDeviceList); alcAllDeviceList = NULL;
1886 alcAllDeviceListSize = 0;
1887 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
1888 alcCaptureDeviceListSize = 0;
1890 free(alcDefaultDeviceSpecifier);
1891 alcDefaultDeviceSpecifier = NULL;
1892 free(alcDefaultAllDeviceSpecifier);
1893 alcDefaultAllDeviceSpecifier = NULL;
1894 free(alcCaptureDefaultDeviceSpecifier);
1895 alcCaptureDefaultDeviceSpecifier = NULL;
1897 #ifdef _DEBUG
1898 if(g_ulDeviceCount > 0)
1899 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
1900 #endif
1902 while(g_pDeviceList)
1904 if(g_pDeviceList->IsCaptureDevice)
1905 alcCaptureCloseDevice(g_pDeviceList);
1906 else
1907 alcCloseDevice(g_pDeviceList);
1911 ///////////////////////////////////////////////////////