Fix comparison check
[openal-soft.git] / Alc / ALc.c
blob2c87809ddeb982cb32aee634facfb2125e615429
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>
29 #include "alMain.h"
30 #include "alSource.h"
31 #include "AL/al.h"
32 #include "AL/alc.h"
33 #include "alThunk.h"
34 #include "alSource.h"
35 #include "alBuffer.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 {
89 const ALCchar *funcName;
90 ALCvoid *address;
91 } ALCfunction;
93 typedef struct ALCenums {
94 const ALCchar *enumName;
95 ALCenum value;
96 } ALCenums;
99 static const ALCfunction alcFunctions[] = {
100 { "alcCreateContext", (ALCvoid *) alcCreateContext },
101 { "alcMakeContextCurrent", (ALCvoid *) alcMakeContextCurrent },
102 { "alcProcessContext", (ALCvoid *) alcProcessContext },
103 { "alcSuspendContext", (ALCvoid *) alcSuspendContext },
104 { "alcDestroyContext", (ALCvoid *) alcDestroyContext },
105 { "alcGetCurrentContext", (ALCvoid *) alcGetCurrentContext },
106 { "alcGetContextsDevice", (ALCvoid *) alcGetContextsDevice },
107 { "alcOpenDevice", (ALCvoid *) alcOpenDevice },
108 { "alcCloseDevice", (ALCvoid *) alcCloseDevice },
109 { "alcGetError", (ALCvoid *) alcGetError },
110 { "alcIsExtensionPresent", (ALCvoid *) alcIsExtensionPresent },
111 { "alcGetProcAddress", (ALCvoid *) alcGetProcAddress },
112 { "alcGetEnumValue", (ALCvoid *) alcGetEnumValue },
113 { "alcGetString", (ALCvoid *) alcGetString },
114 { "alcGetIntegerv", (ALCvoid *) alcGetIntegerv },
115 { "alcCaptureOpenDevice", (ALCvoid *) alcCaptureOpenDevice },
116 { "alcCaptureCloseDevice", (ALCvoid *) alcCaptureCloseDevice },
117 { "alcCaptureStart", (ALCvoid *) alcCaptureStart },
118 { "alcCaptureStop", (ALCvoid *) alcCaptureStop },
119 { "alcCaptureSamples", (ALCvoid *) alcCaptureSamples },
121 { "alcSetThreadContext", (ALCvoid *) alcSetThreadContext },
122 { "alcGetThreadContext", (ALCvoid *) alcGetThreadContext },
124 { NULL, (ALCvoid *) NULL }
127 static const ALCenums enumeration[] = {
128 // Types
129 { "ALC_INVALID", ALC_INVALID },
130 { "ALC_FALSE", ALC_FALSE },
131 { "ALC_TRUE", ALC_TRUE },
133 // ALC Properties
134 { "ALC_MAJOR_VERSION", ALC_MAJOR_VERSION },
135 { "ALC_MINOR_VERSION", ALC_MINOR_VERSION },
136 { "ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE },
137 { "ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES },
138 { "ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER },
139 { "ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER },
140 { "ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER },
141 { "ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER },
142 { "ALC_EXTENSIONS", ALC_EXTENSIONS },
143 { "ALC_FREQUENCY", ALC_FREQUENCY },
144 { "ALC_REFRESH", ALC_REFRESH },
145 { "ALC_SYNC", ALC_SYNC },
146 { "ALC_MONO_SOURCES", ALC_MONO_SOURCES },
147 { "ALC_STEREO_SOURCES", ALC_STEREO_SOURCES },
148 { "ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER },
149 { "ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER},
150 { "ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES },
152 // EFX Properties
153 { "ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION },
154 { "ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION },
155 { "ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS },
157 // ALC Error Message
158 { "ALC_NO_ERROR", ALC_NO_ERROR },
159 { "ALC_INVALID_DEVICE", ALC_INVALID_DEVICE },
160 { "ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT },
161 { "ALC_INVALID_ENUM", ALC_INVALID_ENUM },
162 { "ALC_INVALID_VALUE", ALC_INVALID_VALUE },
163 { "ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY },
164 { NULL, (ALCenum)0 }
166 // Error strings
167 static const ALCchar alcNoError[] = "No Error";
168 static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
169 static const ALCchar alcErrInvalidContext[] = "Invalid Context";
170 static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
171 static const ALCchar alcErrInvalidValue[] = "Invalid Value";
172 static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
174 /* Device lists. Sizes only include the first ending null character, not the
175 * second */
176 static ALCchar *alcDeviceList;
177 static size_t alcDeviceListSize;
178 static ALCchar *alcAllDeviceList;
179 static size_t alcAllDeviceListSize;
180 static ALCchar *alcCaptureDeviceList;
181 static size_t alcCaptureDeviceListSize;
182 // Default is always the first in the list
183 static ALCchar *alcDefaultDeviceSpecifier;
184 static ALCchar *alcDefaultAllDeviceSpecifier;
185 static ALCchar *alcCaptureDefaultDeviceSpecifier;
188 static const ALCchar alcNoDeviceExtList[] =
189 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
190 "ALC_EXTX_thread_local_context";
191 static const ALCchar alcExtensionList[] =
192 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
193 "ALC_EXT_disconnect ALC_EXT_EFX ALC_EXTX_thread_local_context";
194 static const ALCint alcMajorVersion = 1;
195 static const ALCint alcMinorVersion = 1;
197 static const ALCint alcEFXMajorVersion = 1;
198 static const ALCint alcEFXMinorVersion = 0;
200 ///////////////////////////////////////////////////////
203 ///////////////////////////////////////////////////////
204 // Global Variables
206 static ALCdevice *g_pDeviceList = NULL;
207 static ALCuint g_ulDeviceCount = 0;
209 static CRITICAL_SECTION g_csMutex;
211 // Context List
212 static ALCcontext *g_pContextList = NULL;
213 static ALCuint g_ulContextCount = 0;
215 // Thread-local current context
216 static tls_type LocalContext;
217 // Process-wide current context
218 static ALCcontext *GlobalContext;
220 // Context Error
221 static ALCenum g_eLastContextError = ALC_NO_ERROR;
223 // Default context extensions
224 static const ALchar alExtList[] =
225 "AL_EXTX_buffer_sub_data AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE "
226 "AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS "
227 "AL_EXT_MULAW AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET "
228 "AL_EXTX_sample_buffer_object AL_EXT_source_distance_model "
229 "AL_LOKI_quadriphonic";
231 // Mixing Priority Level
232 ALint RTPrioLevel;
234 // Resampler Quality
235 resampler_t DefaultResampler;
237 ///////////////////////////////////////////////////////
240 ///////////////////////////////////////////////////////
241 // ALC Related helper functions
242 #ifdef _WIN32
243 static void alc_init(void);
244 static void alc_deinit(void);
246 BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
248 (void)lpReserved;
250 // Perform actions based on the reason for calling.
251 switch(ul_reason_for_call)
253 case DLL_PROCESS_ATTACH:
254 DisableThreadLibraryCalls(hModule);
255 alc_init();
256 break;
258 case DLL_PROCESS_DETACH:
259 alc_deinit();
260 break;
262 return TRUE;
264 #else
265 #ifdef HAVE_GCC_DESTRUCTOR
266 static void alc_init(void) __attribute__((constructor));
267 static void alc_deinit(void) __attribute__((destructor));
268 #endif
269 #endif
271 static void alc_init(void)
273 int i;
274 const char *devs, *str;
276 InitializeCriticalSection(&g_csMutex);
277 ALTHUNK_INIT();
278 ReadALConfig();
280 tls_create(&LocalContext);
282 RTPrioLevel = GetConfigValueInt(NULL, "rt-prio", 0);
284 DefaultResampler = GetConfigValueInt(NULL, "resampler", RESAMPLER_DEFAULT);
285 if(DefaultResampler >= RESAMPLER_MAX || DefaultResampler <= RESAMPLER_MIN)
286 DefaultResampler = RESAMPLER_DEFAULT;
288 devs = GetConfigValue(NULL, "drivers", "");
289 if(devs[0])
291 int n;
292 size_t len;
293 const char *next = devs;
294 int endlist;
296 i = 0;
297 do {
298 devs = next;
299 next = strchr(devs, ',');
301 if(!devs[0] || devs[0] == ',')
303 endlist = 0;
304 continue;
306 endlist = 1;
308 len = (next ? ((size_t)(next-devs)) : strlen(devs));
309 for(n = i;BackendList[n].Init;n++)
311 if(len == strlen(BackendList[n].name) &&
312 strncmp(BackendList[n].name, devs, len) == 0)
314 BackendInfo Bkp = BackendList[n];
315 while(n > i)
317 BackendList[n] = BackendList[n-1];
318 --n;
320 BackendList[n] = Bkp;
322 i++;
323 break;
326 } while(next++);
328 if(endlist)
330 BackendList[i].name = NULL;
331 BackendList[i].Init = NULL;
332 BackendList[i].Deinit = NULL;
333 BackendList[i].Probe = NULL;
337 for(i = 0;BackendList[i].Init;i++)
338 BackendList[i].Init(&BackendList[i].Funcs);
340 DuplicateStereo = GetConfigValueBool(NULL, "stereodup", 0);
342 str = GetConfigValue(NULL, "excludefx", "");
343 if(str[0])
345 const struct {
346 const char *name;
347 int type;
348 } EffectList[] = {
349 { "eaxreverb", EAXREVERB },
350 { "reverb", REVERB },
351 { "echo", ECHO },
352 { NULL, 0 }
354 int n;
355 size_t len;
356 const char *next = str;
358 do {
359 str = next;
360 next = strchr(str, ',');
362 if(!str[0] || next == str)
363 continue;
365 len = (next ? ((size_t)(next-str)) : strlen(str));
366 for(n = 0;EffectList[n].name;n++)
368 if(len == strlen(EffectList[n].name) &&
369 strncmp(EffectList[n].name, str, len) == 0)
370 DisabledEffects[EffectList[n].type] = AL_TRUE;
372 } while(next++);
376 static void alc_deinit(void)
378 int i;
380 ReleaseALC();
382 for(i = 0;BackendList[i].Deinit;i++)
383 BackendList[i].Deinit();
385 tls_delete(LocalContext);
387 FreeALConfig();
388 ALTHUNK_EXIT();
389 DeleteCriticalSection(&g_csMutex);
393 static void ProbeDeviceList()
395 ALint i;
397 free(alcDeviceList); alcDeviceList = NULL;
398 alcDeviceListSize = 0;
400 for(i = 0;BackendList[i].Probe;i++)
401 BackendList[i].Probe(DEVICE_PROBE);
404 static void ProbeAllDeviceList()
406 ALint i;
408 free(alcAllDeviceList); alcAllDeviceList = NULL;
409 alcAllDeviceListSize = 0;
411 for(i = 0;BackendList[i].Probe;i++)
412 BackendList[i].Probe(ALL_DEVICE_PROBE);
415 static void ProbeCaptureDeviceList()
417 ALint i;
419 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
420 alcCaptureDeviceListSize = 0;
422 for(i = 0;BackendList[i].Probe;i++)
423 BackendList[i].Probe(CAPTURE_DEVICE_PROBE);
427 #define DECL_APPEND_LIST_FUNC(type) \
428 void Append##type##List(const ALCchar *name) \
430 size_t len = strlen(name); \
431 void *temp; \
433 if(len == 0) \
434 return; \
436 temp = realloc(alc##type##List, alc##type##ListSize + len + 2); \
437 if(!temp) \
439 AL_PRINT("Realloc failed to add %s!\n", name); \
440 return; \
442 alc##type##List = temp; \
443 sprintf(alc##type##List+alc##type##ListSize, "%s", name); \
444 alc##type##ListSize += len+1; \
445 alc##type##List[alc##type##ListSize] = 0; \
448 DECL_APPEND_LIST_FUNC(Device)
449 DECL_APPEND_LIST_FUNC(AllDevice)
450 DECL_APPEND_LIST_FUNC(CaptureDevice)
453 void al_print(const char *fname, unsigned int line, const char *fmt, ...)
455 const char *fn;
456 char str[256];
457 int i;
459 fn = strrchr(fname, '/');
460 if(!fn) fn = strrchr(fname, '\\');;
461 if(!fn) fn = fname;
462 else fn += 1;
464 i = snprintf(str, sizeof(str), "AL lib: %s:%d: ", fn, line);
465 if(i < (int)sizeof(str) && i > 0)
467 va_list ap;
468 va_start(ap, fmt);
469 vsnprintf(str+i, sizeof(str)-i, fmt, ap);
470 va_end(ap);
472 str[sizeof(str)-1] = 0;
474 fprintf(stderr, "%s", str);
477 void EnableRTPrio(ALint level)
479 ALboolean failed;
481 #ifdef _WIN32
482 if(level > 0)
483 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
484 else
485 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
486 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM)
487 struct sched_param param;
489 if(level > 0)
491 /* Use the minimum real-time priority possible for now (on Linux this
492 * should be 1 for SCHED_RR) */
493 param.sched_priority = sched_get_priority_min(SCHED_RR);
494 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
496 else
498 param.sched_priority = 0;
499 failed = !!pthread_setschedparam(pthread_self(), SCHED_OTHER, &param);
501 #else
502 /* Real-time priority not available */
503 failed = !!level;
504 #endif
505 if(failed)
506 AL_PRINT("Failed to set priority level for thread\n");
511 IsDevice
513 Check pDevice is a valid Device pointer
515 static ALCboolean IsDevice(ALCdevice *pDevice)
517 ALCdevice *pTempDevice;
519 SuspendContext(NULL);
521 pTempDevice = g_pDeviceList;
522 while(pTempDevice && pTempDevice != pDevice)
523 pTempDevice = pTempDevice->next;
525 ProcessContext(NULL);
527 return (pTempDevice ? ALC_TRUE : ALC_FALSE);
531 IsContext
533 Check pContext is a valid Context pointer
535 static ALCboolean IsContext(ALCcontext *pContext)
537 ALCcontext *pTempContext;
539 SuspendContext(NULL);
541 pTempContext = g_pContextList;
542 while (pTempContext && pTempContext != pContext)
543 pTempContext = pTempContext->next;
545 ProcessContext(NULL);
547 return (pTempContext ? ALC_TRUE : ALC_FALSE);
552 alcSetError
554 Store latest ALC Error
556 ALCvoid alcSetError(ALCdevice *device, ALenum errorCode)
558 if(IsDevice(device))
559 device->LastError = errorCode;
560 else
561 g_eLastContextError = errorCode;
566 SuspendContext
568 Thread-safe entry
570 ALCvoid SuspendContext(ALCcontext *pContext)
572 (void)pContext;
573 EnterCriticalSection(&g_csMutex);
578 ProcessContext
580 Thread-safe exit
582 ALCvoid ProcessContext(ALCcontext *pContext)
584 (void)pContext;
585 LeaveCriticalSection(&g_csMutex);
590 GetContextSuspended
592 Returns the currently active Context, in a locked state
594 ALCcontext *GetContextSuspended(void)
596 ALCcontext *pContext = NULL;
598 SuspendContext(NULL);
600 pContext = tls_get(LocalContext);
601 if(pContext && !IsContext(pContext))
603 tls_set(LocalContext, NULL);
604 pContext = NULL;
606 if(!pContext)
607 pContext = GlobalContext;
609 if(pContext)
610 SuspendContext(pContext);
612 ProcessContext(NULL);
614 return pContext;
619 InitContext
621 Initialize Context variables
623 static ALvoid InitContext(ALCcontext *pContext)
625 //Initialise listener
626 pContext->Listener.Gain = 1.0f;
627 pContext->Listener.MetersPerUnit = 1.0f;
628 pContext->Listener.Position[0] = 0.0f;
629 pContext->Listener.Position[1] = 0.0f;
630 pContext->Listener.Position[2] = 0.0f;
631 pContext->Listener.Velocity[0] = 0.0f;
632 pContext->Listener.Velocity[1] = 0.0f;
633 pContext->Listener.Velocity[2] = 0.0f;
634 pContext->Listener.Forward[0] = 0.0f;
635 pContext->Listener.Forward[1] = 0.0f;
636 pContext->Listener.Forward[2] = -1.0f;
637 pContext->Listener.Up[0] = 0.0f;
638 pContext->Listener.Up[1] = 1.0f;
639 pContext->Listener.Up[2] = 0.0f;
641 //Validate pContext
642 pContext->LastError = AL_NO_ERROR;
643 pContext->Suspended = AL_FALSE;
645 //Set globals
646 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
647 pContext->SourceDistanceModel = AL_FALSE;
648 pContext->DopplerFactor = 1.0f;
649 pContext->DopplerVelocity = 1.0f;
650 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
652 pContext->ExtensionList = alExtList;
657 ExitContext
659 Clean up Context, destroy any remaining Sources
661 static ALCvoid ExitContext(ALCcontext *pContext)
663 //Invalidate context
664 pContext->LastError = AL_NO_ERROR;
667 ///////////////////////////////////////////////////////
670 ///////////////////////////////////////////////////////
671 // ALC Functions calls
674 // This should probably move to another c file but for now ...
675 ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
677 ALCboolean DeviceFound = ALC_FALSE;
678 ALCdevice *device = NULL;
679 ALCint i;
681 if(SampleSize <= 0)
683 alcSetError(NULL, ALC_INVALID_VALUE);
684 return NULL;
687 if(deviceName && !deviceName[0])
688 deviceName = NULL;
690 device = calloc(1, sizeof(ALCdevice));
691 if(!device)
693 alcSetError(NULL, ALC_OUT_OF_MEMORY);
694 return NULL;
697 //Validate device
698 device->Connected = ALC_TRUE;
699 device->IsCaptureDevice = AL_TRUE;
701 device->szDeviceName = NULL;
703 device->Frequency = frequency;
704 device->Format = format;
705 device->UpdateSize = SampleSize;
706 device->NumUpdates = 1;
708 SuspendContext(NULL);
709 for(i = 0;BackendList[i].Init;i++)
711 device->Funcs = &BackendList[i].Funcs;
712 if(ALCdevice_OpenCapture(device, deviceName))
714 device->next = g_pDeviceList;
715 g_pDeviceList = device;
716 g_ulDeviceCount++;
718 DeviceFound = ALC_TRUE;
719 break;
722 ProcessContext(NULL);
724 if(!DeviceFound)
726 alcSetError(NULL, ALC_INVALID_VALUE);
727 free(device);
728 device = NULL;
731 return device;
734 ALC_API ALCboolean ALC_APIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
736 ALCdevice **list;
738 if(!IsDevice(pDevice) || !pDevice->IsCaptureDevice)
740 alcSetError(pDevice, ALC_INVALID_DEVICE);
741 return ALC_FALSE;
744 SuspendContext(NULL);
746 list = &g_pDeviceList;
747 while(*list != pDevice)
748 list = &(*list)->next;
750 *list = (*list)->next;
751 g_ulDeviceCount--;
753 ProcessContext(NULL);
755 ALCdevice_CloseCapture(pDevice);
757 free(pDevice->szDeviceName);
758 pDevice->szDeviceName = NULL;
760 free(pDevice);
762 return ALC_TRUE;
765 ALC_API void ALC_APIENTRY alcCaptureStart(ALCdevice *device)
767 if(!IsDevice(device) || !device->IsCaptureDevice)
769 alcSetError(device, ALC_INVALID_DEVICE);
770 return;
772 SuspendContext(NULL);
773 ALCdevice_StartCapture(device);
774 ProcessContext(NULL);
777 ALC_API void ALC_APIENTRY alcCaptureStop(ALCdevice *device)
779 if(!IsDevice(device) || !device->IsCaptureDevice)
781 alcSetError(device, ALC_INVALID_DEVICE);
782 return;
784 SuspendContext(NULL);
785 ALCdevice_StopCapture(device);
786 ProcessContext(NULL);
789 ALC_API void ALC_APIENTRY alcCaptureSamples(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
791 if(!IsDevice(device) || !device->IsCaptureDevice)
793 alcSetError(device, ALC_INVALID_DEVICE);
794 return;
796 SuspendContext(NULL);
797 ALCdevice_CaptureSamples(device, buffer, samples);
798 ProcessContext(NULL);
802 alcGetError
804 Return last ALC generated error code
806 ALC_API ALCenum ALC_APIENTRY alcGetError(ALCdevice *device)
808 ALCenum errorCode = ALC_NO_ERROR;
810 if(IsDevice(device))
812 errorCode = device->LastError;
813 device->LastError = ALC_NO_ERROR;
815 else
817 errorCode = g_eLastContextError;
818 g_eLastContextError = ALC_NO_ERROR;
820 return errorCode;
825 alcSuspendContext
827 Not functional
829 ALC_API ALCvoid ALC_APIENTRY alcSuspendContext(ALCcontext *pContext)
831 SuspendContext(NULL);
832 if(IsContext(pContext))
833 pContext->Suspended = AL_TRUE;
834 ProcessContext(NULL);
839 alcProcessContext
841 Not functional
843 ALC_API ALCvoid ALC_APIENTRY alcProcessContext(ALCcontext *pContext)
845 SuspendContext(NULL);
846 if(IsContext(pContext))
847 pContext->Suspended = AL_FALSE;
848 ProcessContext(NULL);
853 alcGetString
855 Returns information about the Device, and error strings
857 ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
859 const ALCchar *value = NULL;
861 switch (param)
863 case ALC_NO_ERROR:
864 value = alcNoError;
865 break;
867 case ALC_INVALID_ENUM:
868 value = alcErrInvalidEnum;
869 break;
871 case ALC_INVALID_VALUE:
872 value = alcErrInvalidValue;
873 break;
875 case ALC_INVALID_DEVICE:
876 value = alcErrInvalidDevice;
877 break;
879 case ALC_INVALID_CONTEXT:
880 value = alcErrInvalidContext;
881 break;
883 case ALC_OUT_OF_MEMORY:
884 value = alcErrOutOfMemory;
885 break;
887 case ALC_DEVICE_SPECIFIER:
888 if(IsDevice(pDevice))
889 value = pDevice->szDeviceName;
890 else
892 ProbeDeviceList();
893 value = alcDeviceList;
895 break;
897 case ALC_ALL_DEVICES_SPECIFIER:
898 ProbeAllDeviceList();
899 value = alcAllDeviceList;
900 break;
902 case ALC_CAPTURE_DEVICE_SPECIFIER:
903 if(IsDevice(pDevice))
904 value = pDevice->szDeviceName;
905 else
907 ProbeCaptureDeviceList();
908 value = alcCaptureDeviceList;
910 break;
912 /* Default devices are always first in the list */
913 case ALC_DEFAULT_DEVICE_SPECIFIER:
914 if(!alcDeviceList)
915 ProbeDeviceList();
917 free(alcDefaultDeviceSpecifier);
918 alcDefaultDeviceSpecifier = strdup(alcDeviceList ? alcDeviceList : "");
919 if(!alcDefaultDeviceSpecifier)
920 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
921 value = alcDefaultDeviceSpecifier;
922 break;
924 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
925 if(!alcAllDeviceList)
926 ProbeAllDeviceList();
928 free(alcDefaultAllDeviceSpecifier);
929 alcDefaultAllDeviceSpecifier = strdup(alcAllDeviceList ?
930 alcAllDeviceList : "");
931 if(!alcDefaultAllDeviceSpecifier)
932 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
933 value = alcDefaultAllDeviceSpecifier;
934 break;
936 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
937 if(!alcCaptureDeviceList)
938 ProbeCaptureDeviceList();
940 free(alcCaptureDefaultDeviceSpecifier);
941 alcCaptureDefaultDeviceSpecifier = strdup(alcCaptureDeviceList ?
942 alcCaptureDeviceList : "");
943 if(!alcCaptureDefaultDeviceSpecifier)
944 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
945 value = alcCaptureDefaultDeviceSpecifier;
946 break;
948 case ALC_EXTENSIONS:
949 if(IsDevice(pDevice))
950 value = alcExtensionList;
951 else
952 value = alcNoDeviceExtList;
953 break;
955 default:
956 alcSetError(pDevice, ALC_INVALID_ENUM);
957 break;
960 return value;
965 alcGetIntegerv
967 Returns information about the Device and the version of Open AL
969 ALC_API ALCvoid ALC_APIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
971 if(size == 0 || data == NULL)
973 alcSetError(device, ALC_INVALID_VALUE);
974 return;
977 if(IsDevice(device) && device->IsCaptureDevice)
979 SuspendContext(NULL);
981 // Capture device
982 switch (param)
984 case ALC_CAPTURE_SAMPLES:
985 *data = ALCdevice_AvailableSamples(device);
986 break;
988 case ALC_CONNECTED:
989 *data = device->Connected;
990 break;
992 default:
993 alcSetError(device, ALC_INVALID_ENUM);
994 break;
997 ProcessContext(NULL);
998 return;
1001 // Playback Device
1002 switch (param)
1004 case ALC_MAJOR_VERSION:
1005 *data = alcMajorVersion;
1006 break;
1008 case ALC_MINOR_VERSION:
1009 *data = alcMinorVersion;
1010 break;
1012 case ALC_EFX_MAJOR_VERSION:
1013 *data = alcEFXMajorVersion;
1014 break;
1016 case ALC_EFX_MINOR_VERSION:
1017 *data = alcEFXMinorVersion;
1018 break;
1020 case ALC_MAX_AUXILIARY_SENDS:
1021 if(!IsDevice(device))
1022 alcSetError(device, ALC_INVALID_DEVICE);
1023 else
1024 *data = device->NumAuxSends;
1025 break;
1027 case ALC_ATTRIBUTES_SIZE:
1028 if(!IsDevice(device))
1029 alcSetError(device, ALC_INVALID_DEVICE);
1030 else
1031 *data = 13;
1032 break;
1034 case ALC_ALL_ATTRIBUTES:
1035 if(!IsDevice(device))
1036 alcSetError(device, ALC_INVALID_DEVICE);
1037 else if (size < 13)
1038 alcSetError(device, ALC_INVALID_VALUE);
1039 else
1041 int i = 0;
1043 SuspendContext(NULL);
1044 data[i++] = ALC_FREQUENCY;
1045 data[i++] = device->Frequency;
1047 data[i++] = ALC_REFRESH;
1048 data[i++] = device->Frequency / device->UpdateSize;
1050 data[i++] = ALC_SYNC;
1051 data[i++] = ALC_FALSE;
1053 data[i++] = ALC_MONO_SOURCES;
1054 data[i++] = device->lNumMonoSources;
1056 data[i++] = ALC_STEREO_SOURCES;
1057 data[i++] = device->lNumStereoSources;
1059 data[i++] = ALC_MAX_AUXILIARY_SENDS;
1060 data[i++] = device->NumAuxSends;
1062 data[i++] = 0;
1063 ProcessContext(NULL);
1065 break;
1067 case ALC_FREQUENCY:
1068 if(!IsDevice(device))
1069 alcSetError(device, ALC_INVALID_DEVICE);
1070 else
1071 *data = device->Frequency;
1072 break;
1074 case ALC_REFRESH:
1075 if(!IsDevice(device))
1076 alcSetError(device, ALC_INVALID_DEVICE);
1077 else
1078 *data = device->Frequency / device->UpdateSize;
1079 break;
1081 case ALC_SYNC:
1082 if(!IsDevice(device))
1083 alcSetError(device, ALC_INVALID_DEVICE);
1084 else
1085 *data = ALC_FALSE;
1086 break;
1088 case ALC_MONO_SOURCES:
1089 if(!IsDevice(device))
1090 alcSetError(device, ALC_INVALID_DEVICE);
1091 else
1092 *data = device->lNumMonoSources;
1093 break;
1095 case ALC_STEREO_SOURCES:
1096 if(!IsDevice(device))
1097 alcSetError(device, ALC_INVALID_DEVICE);
1098 else
1099 *data = device->lNumStereoSources;
1100 break;
1102 case ALC_CONNECTED:
1103 if(!IsDevice(device))
1104 alcSetError(device, ALC_INVALID_DEVICE);
1105 else
1106 *data = device->Connected;
1107 break;
1109 default:
1110 alcSetError(device, ALC_INVALID_ENUM);
1111 break;
1117 alcIsExtensionPresent
1119 Determines if there is support for a particular extension
1121 ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
1123 ALCboolean bResult = ALC_FALSE;
1124 const char *ptr;
1125 size_t len;
1127 if(!extName)
1129 alcSetError(device, ALC_INVALID_VALUE);
1130 return ALC_FALSE;
1133 len = strlen(extName);
1134 ptr = (IsDevice(device) ? alcExtensionList : alcNoDeviceExtList);
1135 while(ptr && *ptr)
1137 if(strncasecmp(ptr, extName, len) == 0 &&
1138 (ptr[len] == '\0' || isspace(ptr[len])))
1140 bResult = ALC_TRUE;
1141 break;
1143 if((ptr=strchr(ptr, ' ')) != NULL)
1145 do {
1146 ++ptr;
1147 } while(isspace(*ptr));
1151 return bResult;
1156 alcGetProcAddress
1158 Retrieves the function address for a particular extension function
1160 ALC_API ALCvoid* ALC_APIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
1162 ALsizei i = 0;
1164 if(!funcName)
1166 alcSetError(device, ALC_INVALID_VALUE);
1167 return NULL;
1170 while(alcFunctions[i].funcName && strcmp(alcFunctions[i].funcName,funcName) != 0)
1171 i++;
1172 return alcFunctions[i].address;
1177 alcGetEnumValue
1179 Get the value for a particular ALC Enumerated Value
1181 ALC_API ALCenum ALC_APIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
1183 ALsizei i = 0;
1185 if(!enumName)
1187 alcSetError(device, ALC_INVALID_VALUE);
1188 return (ALCenum)0;
1191 while(enumeration[i].enumName && strcmp(enumeration[i].enumName,enumName) != 0)
1192 i++;
1193 return enumeration[i].value;
1198 alcCreateContext
1200 Create and attach a Context to a particular Device.
1202 ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
1204 ALuint attrIdx, reqStereoSources;
1205 ALCcontext *ALContext;
1206 void *temp;
1207 ALuint i;
1209 SuspendContext(NULL);
1211 if(!IsDevice(device) || device->IsCaptureDevice || !device->Connected)
1213 alcSetError(device, ALC_INVALID_DEVICE);
1214 ProcessContext(NULL);
1215 return NULL;
1218 // Reset Context Last Error code
1219 device->LastError = ALC_NO_ERROR;
1221 // If a context is already running on the device, stop playback so the
1222 // device attributes can be updated
1223 if(device->NumContexts > 0)
1225 ProcessContext(NULL);
1226 ALCdevice_StopPlayback(device);
1227 SuspendContext(NULL);
1230 // Check for attributes
1231 if(attrList)
1233 ALCint level = device->Bs2bLevel;
1234 ALCuint freq = device->Frequency;
1235 ALCint numMono = device->lNumMonoSources;
1236 ALCint numStereo = device->lNumStereoSources;
1237 ALCuint numSends = device->NumAuxSends;
1239 attrIdx = 0;
1240 while(attrList[attrIdx])
1242 if(attrList[attrIdx] == ALC_FREQUENCY &&
1243 !ConfigValueExists(NULL, "frequency"))
1245 freq = attrList[attrIdx + 1];
1246 if(freq < 8000)
1247 freq = 8000;
1250 if(attrList[attrIdx] == ALC_STEREO_SOURCES)
1252 reqStereoSources = attrList[attrIdx + 1];
1253 if(reqStereoSources > device->MaxNoOfSources)
1254 reqStereoSources = device->MaxNoOfSources;
1256 numStereo = reqStereoSources;
1257 numMono = device->MaxNoOfSources - numStereo;
1260 if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS &&
1261 !ConfigValueExists(NULL, "sends"))
1263 numSends = attrList[attrIdx + 1];
1264 if(numSends > MAX_SENDS)
1265 numSends = MAX_SENDS;
1268 attrIdx += 2;
1271 device->UpdateSize = (ALuint64)device->UpdateSize * freq /
1272 device->Frequency;
1274 device->Bs2bLevel = level;
1275 device->Frequency = freq;
1276 device->lNumMonoSources = numMono;
1277 device->lNumStereoSources = numStereo;
1278 device->NumAuxSends = numSends;
1281 if(ALCdevice_ResetPlayback(device) == ALC_FALSE)
1283 alcSetError(device, ALC_INVALID_DEVICE);
1284 aluHandleDisconnect(device);
1285 ProcessContext(NULL);
1286 return NULL;
1289 for(i = 0;i < device->NumContexts;i++)
1291 ALCcontext *context = device->Contexts[i];
1292 ALeffectslot *slot;
1293 ALsource *source;
1295 SuspendContext(context);
1296 for(slot = context->EffectSlotList;slot != NULL;slot = slot->next)
1298 if(!slot->EffectState)
1299 continue;
1301 if(ALEffect_DeviceUpdate(slot->EffectState, device) == AL_FALSE)
1303 alcSetError(device, ALC_INVALID_DEVICE);
1304 aluHandleDisconnect(device);
1305 ProcessContext(context);
1306 ProcessContext(NULL);
1307 ALCdevice_StopPlayback(device);
1308 return NULL;
1310 ALEffect_Update(slot->EffectState, context, &slot->effect);
1313 for(source = context->SourceList;source != NULL;source = source->next)
1315 ALuint s = device->NumAuxSends;
1316 while(s < MAX_SENDS)
1318 if(source->Send[s].Slot)
1319 source->Send[s].Slot->refcount--;
1320 source->Send[s].Slot = NULL;
1321 source->Send[s].WetFilter.type = 0;
1322 source->Send[s].WetFilter.filter = 0;
1323 s++;
1325 source->NeedsUpdate = AL_TRUE;
1327 aluInitPanning(context);
1328 ProcessContext(context);
1331 if(device->Bs2bLevel > 0 && device->Bs2bLevel <= 6)
1333 if(!device->Bs2b)
1335 device->Bs2b = calloc(1, sizeof(*device->Bs2b));
1336 bs2b_clear(device->Bs2b);
1338 bs2b_set_srate(device->Bs2b, device->Frequency);
1339 bs2b_set_level(device->Bs2b, device->Bs2bLevel);
1341 else
1343 free(device->Bs2b);
1344 device->Bs2b = NULL;
1347 temp = realloc(device->Contexts, (device->NumContexts+1) * sizeof(*device->Contexts));
1348 if(!temp)
1350 alcSetError(device, ALC_OUT_OF_MEMORY);
1351 ProcessContext(NULL);
1352 return NULL;
1354 device->Contexts = temp;
1356 ALContext = calloc(1, sizeof(ALCcontext));
1357 if(!ALContext)
1359 alcSetError(device, ALC_OUT_OF_MEMORY);
1360 ProcessContext(NULL);
1361 return NULL;
1364 device->Contexts[device->NumContexts++] = ALContext;
1365 ALContext->Device = device;
1367 InitContext(ALContext);
1368 aluInitPanning(ALContext);
1370 ALContext->next = g_pContextList;
1371 g_pContextList = ALContext;
1372 g_ulContextCount++;
1374 ProcessContext(NULL);
1376 return ALContext;
1381 alcDestroyContext
1383 Remove a Context
1385 ALC_API ALCvoid ALC_APIENTRY alcDestroyContext(ALCcontext *context)
1387 ALCdevice *Device;
1388 ALCcontext **list;
1389 ALuint i;
1391 if(!IsContext(context))
1393 alcSetError(NULL, ALC_INVALID_CONTEXT);
1394 return;
1397 Device = context->Device;
1399 if(Device->NumContexts == 1)
1400 ALCdevice_StopPlayback(Device);
1402 SuspendContext(NULL);
1404 if(context == GlobalContext)
1405 GlobalContext = NULL;
1407 for(i = 0;i < Device->NumContexts-1;i++)
1409 if(Device->Contexts[i] == context)
1411 Device->Contexts[i] = Device->Contexts[Device->NumContexts-1];
1412 break;
1415 Device->NumContexts--;
1417 // Lock context
1418 SuspendContext(context);
1420 if(context->SourceCount > 0)
1422 #ifdef _DEBUG
1423 AL_PRINT("alcDestroyContext(): deleting %d Source(s)\n", context->SourceCount);
1424 #endif
1425 ReleaseALSources(context);
1427 if(context->EffectSlotCount > 0)
1429 #ifdef _DEBUG
1430 AL_PRINT("alcDestroyContext(): deleting %d AuxiliaryEffectSlot(s)\n", context->EffectSlotCount);
1431 #endif
1432 ReleaseALAuxiliaryEffectSlots(context);
1435 list = &g_pContextList;
1436 while(*list != context)
1437 list = &(*list)->next;
1439 *list = (*list)->next;
1440 g_ulContextCount--;
1442 // Unlock context
1443 ProcessContext(context);
1444 ProcessContext(NULL);
1446 ExitContext(context);
1448 // Free memory (MUST do this after ProcessContext)
1449 memset(context, 0, sizeof(ALCcontext));
1450 free(context);
1455 alcGetCurrentContext
1457 Returns the currently active Context
1459 ALC_API ALCcontext* ALC_APIENTRY alcGetCurrentContext(ALCvoid)
1461 ALCcontext *pContext;
1463 if((pContext=GetContextSuspended()) != NULL)
1464 ProcessContext(pContext);
1466 return pContext;
1470 alcGetThreadContext
1472 Returns the currently active thread-local Context
1474 ALC_API ALCcontext* ALC_APIENTRY alcGetThreadContext(void)
1476 ALCcontext *pContext = NULL;
1478 SuspendContext(NULL);
1480 pContext = tls_get(LocalContext);
1481 if(pContext && !IsContext(pContext))
1483 tls_set(LocalContext, NULL);
1484 pContext = NULL;
1487 ProcessContext(NULL);
1489 return pContext;
1494 alcGetContextsDevice
1496 Returns the Device that a particular Context is attached to
1498 ALC_API ALCdevice* ALC_APIENTRY alcGetContextsDevice(ALCcontext *pContext)
1500 ALCdevice *pDevice = NULL;
1502 SuspendContext(NULL);
1503 if(IsContext(pContext))
1504 pDevice = pContext->Device;
1505 else
1506 alcSetError(NULL, ALC_INVALID_CONTEXT);
1507 ProcessContext(NULL);
1509 return pDevice;
1514 alcMakeContextCurrent
1516 Makes the given Context the active Context
1518 ALC_API ALCboolean ALC_APIENTRY alcMakeContextCurrent(ALCcontext *context)
1520 ALboolean bReturn = AL_TRUE;
1522 SuspendContext(NULL);
1524 // context must be a valid Context or NULL
1525 if(context == NULL || IsContext(context))
1527 GlobalContext = context;
1528 tls_set(LocalContext, NULL);
1530 else
1532 alcSetError(NULL, ALC_INVALID_CONTEXT);
1533 bReturn = AL_FALSE;
1536 ProcessContext(NULL);
1538 return bReturn;
1542 alcSetThreadContext
1544 Makes the given Context the active Context for the current thread
1546 ALC_API ALCboolean ALC_APIENTRY alcSetThreadContext(ALCcontext *context)
1548 ALboolean bReturn = AL_TRUE;
1550 SuspendContext(NULL);
1552 // context must be a valid Context or NULL
1553 if(context == NULL || IsContext(context))
1554 tls_set(LocalContext, context);
1555 else
1557 alcSetError(NULL, ALC_INVALID_CONTEXT);
1558 bReturn = AL_FALSE;
1561 ProcessContext(NULL);
1563 return bReturn;
1567 // Sets the default channel order used by most non-WaveFormatEx-based APIs
1568 void SetDefaultChannelOrder(ALCdevice *device)
1570 switch(aluChannelsFromFormat(device->Format))
1572 case 1: device->DevChannels[0] = FRONT_CENTER; break;
1574 case 2: device->DevChannels[0] = FRONT_LEFT;
1575 device->DevChannels[1] = FRONT_RIGHT; break;
1577 case 4: device->DevChannels[0] = FRONT_LEFT;
1578 device->DevChannels[1] = FRONT_RIGHT;
1579 device->DevChannels[2] = BACK_LEFT;
1580 device->DevChannels[3] = BACK_RIGHT; break;
1582 case 6: device->DevChannels[0] = FRONT_LEFT;
1583 device->DevChannels[1] = FRONT_RIGHT;
1584 device->DevChannels[2] = BACK_LEFT;
1585 device->DevChannels[3] = BACK_RIGHT;
1586 device->DevChannels[4] = FRONT_CENTER;
1587 device->DevChannels[5] = LFE; break;
1589 case 7: device->DevChannels[0] = FRONT_LEFT;
1590 device->DevChannels[1] = FRONT_RIGHT;
1591 device->DevChannels[2] = FRONT_CENTER;
1592 device->DevChannels[3] = LFE;
1593 device->DevChannels[4] = BACK_CENTER;
1594 device->DevChannels[5] = SIDE_LEFT;
1595 device->DevChannels[6] = SIDE_RIGHT; break;
1597 case 8: device->DevChannels[0] = FRONT_LEFT;
1598 device->DevChannels[1] = FRONT_RIGHT;
1599 device->DevChannels[2] = BACK_LEFT;
1600 device->DevChannels[3] = BACK_RIGHT;
1601 device->DevChannels[4] = FRONT_CENTER;
1602 device->DevChannels[5] = LFE;
1603 device->DevChannels[6] = SIDE_LEFT;
1604 device->DevChannels[7] = SIDE_RIGHT; break;
1607 // Sets the default order used by WaveFormatEx
1608 void SetDefaultWFXChannelOrder(ALCdevice *device)
1610 switch(aluChannelsFromFormat(device->Format))
1612 case 1: device->DevChannels[0] = FRONT_CENTER; break;
1614 case 2: device->DevChannels[0] = FRONT_LEFT;
1615 device->DevChannels[1] = FRONT_RIGHT; break;
1617 case 4: device->DevChannels[0] = FRONT_LEFT;
1618 device->DevChannels[1] = FRONT_RIGHT;
1619 device->DevChannels[2] = BACK_LEFT;
1620 device->DevChannels[3] = BACK_RIGHT; break;
1622 case 6: device->DevChannels[0] = FRONT_LEFT;
1623 device->DevChannels[1] = FRONT_RIGHT;
1624 device->DevChannels[2] = FRONT_CENTER;
1625 device->DevChannels[3] = LFE;
1626 device->DevChannels[4] = BACK_LEFT;
1627 device->DevChannels[5] = BACK_RIGHT; break;
1629 case 7: device->DevChannels[0] = FRONT_LEFT;
1630 device->DevChannels[1] = FRONT_RIGHT;
1631 device->DevChannels[2] = FRONT_CENTER;
1632 device->DevChannels[3] = LFE;
1633 device->DevChannels[4] = BACK_CENTER;
1634 device->DevChannels[5] = SIDE_LEFT;
1635 device->DevChannels[6] = SIDE_RIGHT; break;
1637 case 8: device->DevChannels[0] = FRONT_LEFT;
1638 device->DevChannels[1] = FRONT_RIGHT;
1639 device->DevChannels[2] = FRONT_CENTER;
1640 device->DevChannels[3] = LFE;
1641 device->DevChannels[4] = BACK_LEFT;
1642 device->DevChannels[5] = BACK_RIGHT;
1643 device->DevChannels[6] = SIDE_LEFT;
1644 device->DevChannels[7] = SIDE_RIGHT; break;
1648 static ALenum GetFormatFromString(const char *str)
1650 if(strcasecmp(str, "AL_FORMAT_MONO32") == 0) return AL_FORMAT_MONO_FLOAT32;
1651 if(strcasecmp(str, "AL_FORMAT_STEREO32") == 0) return AL_FORMAT_STEREO_FLOAT32;
1652 if(strcasecmp(str, "AL_FORMAT_QUAD32") == 0) return AL_FORMAT_QUAD32;
1653 if(strcasecmp(str, "AL_FORMAT_51CHN32") == 0) return AL_FORMAT_51CHN32;
1654 if(strcasecmp(str, "AL_FORMAT_61CHN32") == 0) return AL_FORMAT_61CHN32;
1655 if(strcasecmp(str, "AL_FORMAT_71CHN32") == 0) return AL_FORMAT_71CHN32;
1657 if(strcasecmp(str, "AL_FORMAT_MONO16") == 0) return AL_FORMAT_MONO16;
1658 if(strcasecmp(str, "AL_FORMAT_STEREO16") == 0) return AL_FORMAT_STEREO16;
1659 if(strcasecmp(str, "AL_FORMAT_QUAD16") == 0) return AL_FORMAT_QUAD16;
1660 if(strcasecmp(str, "AL_FORMAT_51CHN16") == 0) return AL_FORMAT_51CHN16;
1661 if(strcasecmp(str, "AL_FORMAT_61CHN16") == 0) return AL_FORMAT_61CHN16;
1662 if(strcasecmp(str, "AL_FORMAT_71CHN16") == 0) return AL_FORMAT_71CHN16;
1664 if(strcasecmp(str, "AL_FORMAT_MONO8") == 0) return AL_FORMAT_MONO8;
1665 if(strcasecmp(str, "AL_FORMAT_STEREO8") == 0) return AL_FORMAT_STEREO8;
1666 if(strcasecmp(str, "AL_FORMAT_QUAD8") == 0) return AL_FORMAT_QUAD8;
1667 if(strcasecmp(str, "AL_FORMAT_51CHN8") == 0) return AL_FORMAT_51CHN8;
1668 if(strcasecmp(str, "AL_FORMAT_61CHN8") == 0) return AL_FORMAT_61CHN8;
1669 if(strcasecmp(str, "AL_FORMAT_71CHN8") == 0) return AL_FORMAT_71CHN8;
1671 AL_PRINT("Unknown format: \"%s\"\n", str);
1672 return AL_FORMAT_STEREO16;
1676 alcOpenDevice
1678 Open the Device specified.
1680 ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName)
1682 ALboolean bDeviceFound = AL_FALSE;
1683 const ALCchar *fmt;
1684 ALCdevice *device;
1685 ALint i;
1687 if(deviceName && !deviceName[0])
1688 deviceName = NULL;
1690 device = calloc(1, sizeof(ALCdevice));
1691 if(!device)
1693 alcSetError(NULL, ALC_OUT_OF_MEMORY);
1694 return NULL;
1697 //Validate device
1698 device->Connected = ALC_TRUE;
1699 device->IsCaptureDevice = AL_FALSE;
1700 device->LastError = ALC_NO_ERROR;
1702 device->Bs2b = NULL;
1703 device->szDeviceName = NULL;
1705 device->Contexts = NULL;
1706 device->NumContexts = 0;
1708 //Set output format
1709 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1710 if(device->Frequency < 8000)
1711 device->Frequency = 8000;
1713 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1714 device->Format = GetFormatFromString(fmt);
1716 device->NumUpdates = GetConfigValueInt(NULL, "periods", 4);
1717 if(device->NumUpdates < 2)
1718 device->NumUpdates = 4;
1720 i = GetConfigValueInt(NULL, "refresh", 4096);
1721 if(i <= 0) i = 4096;
1723 device->UpdateSize = GetConfigValueInt(NULL, "period_size", i/device->NumUpdates);
1724 if(device->UpdateSize <= 0)
1725 device->UpdateSize = i/device->NumUpdates;
1727 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1728 if((ALint)device->MaxNoOfSources <= 0)
1729 device->MaxNoOfSources = 256;
1731 device->AuxiliaryEffectSlotMax = GetConfigValueInt(NULL, "slots", 4);
1732 if((ALint)device->AuxiliaryEffectSlotMax <= 0)
1733 device->AuxiliaryEffectSlotMax = 4;
1735 device->lNumStereoSources = 1;
1736 device->lNumMonoSources = device->MaxNoOfSources - device->lNumStereoSources;
1738 device->NumAuxSends = GetConfigValueInt(NULL, "sends", MAX_SENDS);
1739 if(device->NumAuxSends > MAX_SENDS)
1740 device->NumAuxSends = MAX_SENDS;
1742 device->Bs2bLevel = GetConfigValueInt(NULL, "cf_level", 0);
1744 if(aluChannelsFromFormat(device->Format) <= 2)
1746 device->HeadDampen = GetConfigValueFloat(NULL, "head_dampen", DEFAULT_HEAD_DAMPEN);
1747 device->HeadDampen = __min(device->HeadDampen, 1.0f);
1748 device->HeadDampen = __max(device->HeadDampen, 0.0f);
1750 else
1751 device->HeadDampen = 0.0f;
1753 // Find a playback device to open
1754 SuspendContext(NULL);
1755 for(i = 0;BackendList[i].Init;i++)
1757 device->Funcs = &BackendList[i].Funcs;
1758 if(ALCdevice_OpenPlayback(device, deviceName))
1760 device->next = g_pDeviceList;
1761 g_pDeviceList = device;
1762 g_ulDeviceCount++;
1764 bDeviceFound = AL_TRUE;
1765 break;
1768 ProcessContext(NULL);
1770 if(!bDeviceFound)
1772 // No suitable output device found
1773 alcSetError(NULL, ALC_INVALID_VALUE);
1774 free(device);
1775 device = NULL;
1778 return device;
1783 alcCloseDevice
1785 Close the specified Device
1787 ALC_API ALCboolean ALC_APIENTRY alcCloseDevice(ALCdevice *pDevice)
1789 ALCdevice **list;
1791 if(!IsDevice(pDevice) || pDevice->IsCaptureDevice)
1793 alcSetError(pDevice, ALC_INVALID_DEVICE);
1794 return ALC_FALSE;
1797 SuspendContext(NULL);
1799 list = &g_pDeviceList;
1800 while(*list != pDevice)
1801 list = &(*list)->next;
1803 *list = (*list)->next;
1804 g_ulDeviceCount--;
1806 ProcessContext(NULL);
1808 if(pDevice->NumContexts > 0)
1810 #ifdef _DEBUG
1811 AL_PRINT("alcCloseDevice(): destroying %u Context(s)\n", pDevice->NumContexts);
1812 #endif
1813 while(pDevice->NumContexts > 0)
1814 alcDestroyContext(pDevice->Contexts[0]);
1816 ALCdevice_ClosePlayback(pDevice);
1818 if(pDevice->BufferCount > 0)
1820 #ifdef _DEBUG
1821 AL_PRINT("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice->BufferCount);
1822 #endif
1823 ReleaseALBuffers(pDevice);
1825 if(pDevice->EffectCount > 0)
1827 #ifdef _DEBUG
1828 AL_PRINT("alcCloseDevice(): deleting %d Effect(s)\n", pDevice->EffectCount);
1829 #endif
1830 ReleaseALEffects(pDevice);
1832 if(pDevice->FilterCount > 0)
1834 #ifdef _DEBUG
1835 AL_PRINT("alcCloseDevice(): deleting %d Filter(s)\n", pDevice->FilterCount);
1836 #endif
1837 ReleaseALFilters(pDevice);
1839 if(pDevice->DatabufferCount > 0)
1841 #ifdef _DEBUG
1842 AL_PRINT("alcCloseDevice(): deleting %d Databuffer(s)\n", pDevice->DatabufferCount);
1843 #endif
1844 ReleaseALDatabuffers(pDevice);
1847 free(pDevice->Bs2b);
1848 pDevice->Bs2b = NULL;
1850 free(pDevice->szDeviceName);
1851 pDevice->szDeviceName = NULL;
1853 free(pDevice->Contexts);
1854 pDevice->Contexts = NULL;
1856 //Release device structure
1857 memset(pDevice, 0, sizeof(ALCdevice));
1858 free(pDevice);
1860 return ALC_TRUE;
1864 ALCvoid ReleaseALC(ALCvoid)
1866 free(alcDeviceList); alcDeviceList = NULL;
1867 alcDeviceListSize = 0;
1868 free(alcAllDeviceList); alcAllDeviceList = NULL;
1869 alcAllDeviceListSize = 0;
1870 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
1871 alcCaptureDeviceListSize = 0;
1873 free(alcDefaultDeviceSpecifier);
1874 alcDefaultDeviceSpecifier = NULL;
1875 free(alcDefaultAllDeviceSpecifier);
1876 alcDefaultAllDeviceSpecifier = NULL;
1877 free(alcCaptureDefaultDeviceSpecifier);
1878 alcCaptureDefaultDeviceSpecifier = NULL;
1880 #ifdef _DEBUG
1881 if(g_ulDeviceCount > 0)
1882 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
1883 #endif
1885 while(g_pDeviceList)
1887 if(g_pDeviceList->IsCaptureDevice)
1888 alcCaptureCloseDevice(g_pDeviceList);
1889 else
1890 alcCloseDevice(g_pDeviceList);
1894 ///////////////////////////////////////////////////////