Hold the default context extensions globally
[openal-soft.git] / Alc / ALc.c
bloba9708e5dea17e182739b51e47263a1cad106fed3
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <math.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <memory.h>
27 #include <ctype.h>
28 #include "alMain.h"
29 #include "alSource.h"
30 #include "AL/al.h"
31 #include "AL/alc.h"
32 #include "alThunk.h"
33 #include "alSource.h"
34 #include "alBuffer.h"
35 #include "alExtension.h"
36 #include "alAuxEffectSlot.h"
37 #include "alDatabuffer.h"
38 #include "bs2b.h"
39 #include "alu.h"
42 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
43 typedef struct BackendInfo {
44 const char *name;
45 void (*Init)(BackendFuncs*);
46 void (*Deinit)(void);
47 void (*Probe)(int);
48 BackendFuncs Funcs;
49 } BackendInfo;
50 static BackendInfo BackendList[] = {
51 #ifdef HAVE_PULSEAUDIO
52 { "pulse", alc_pulse_init, alc_pulse_deinit, alc_pulse_probe, EmptyFuncs },
53 #endif
54 #ifdef HAVE_ALSA
55 { "alsa", alc_alsa_init, alc_alsa_deinit, alc_alsa_probe, EmptyFuncs },
56 #endif
57 #ifdef HAVE_OSS
58 { "oss", alc_oss_init, alc_oss_deinit, alc_oss_probe, EmptyFuncs },
59 #endif
60 #ifdef HAVE_SOLARIS
61 { "solaris", alc_solaris_init, alc_solaris_deinit, alc_solaris_probe, EmptyFuncs },
62 #endif
63 #ifdef HAVE_DSOUND
64 { "dsound", alcDSoundInit, alcDSoundDeinit, alcDSoundProbe, EmptyFuncs },
65 #endif
66 #ifdef HAVE_WINMM
67 { "winmm", alcWinMMInit, alcWinMMDeinit, alcWinMMProbe, EmptyFuncs },
68 #endif
69 #ifdef HAVE_PORTAUDIO
70 { "port", alc_pa_init, alc_pa_deinit, alc_pa_probe, EmptyFuncs },
71 #endif
73 { "wave", alc_wave_init, alc_wave_deinit, alc_wave_probe, EmptyFuncs },
75 { NULL, NULL, NULL, NULL, EmptyFuncs }
77 #undef EmptyFuncs
79 ///////////////////////////////////////////////////////
81 #define ALC_EFX_MAJOR_VERSION 0x20001
82 #define ALC_EFX_MINOR_VERSION 0x20002
83 #define ALC_MAX_AUXILIARY_SENDS 0x20003
85 ///////////////////////////////////////////////////////
86 // STRING and EXTENSIONS
88 typedef struct ALCfunction_struct
90 ALCchar *funcName;
91 ALvoid *address;
92 } ALCfunction;
94 static ALCfunction alcFunctions[] = {
95 { "alcCreateContext", (ALvoid *) alcCreateContext },
96 { "alcMakeContextCurrent", (ALvoid *) alcMakeContextCurrent },
97 { "alcProcessContext", (ALvoid *) alcProcessContext },
98 { "alcSuspendContext", (ALvoid *) alcSuspendContext },
99 { "alcDestroyContext", (ALvoid *) alcDestroyContext },
100 { "alcGetCurrentContext", (ALvoid *) alcGetCurrentContext },
101 { "alcGetContextsDevice", (ALvoid *) alcGetContextsDevice },
102 { "alcOpenDevice", (ALvoid *) alcOpenDevice },
103 { "alcCloseDevice", (ALvoid *) alcCloseDevice },
104 { "alcGetError", (ALvoid *) alcGetError },
105 { "alcIsExtensionPresent", (ALvoid *) alcIsExtensionPresent },
106 { "alcGetProcAddress", (ALvoid *) alcGetProcAddress },
107 { "alcGetEnumValue", (ALvoid *) alcGetEnumValue },
108 { "alcGetString", (ALvoid *) alcGetString },
109 { "alcGetIntegerv", (ALvoid *) alcGetIntegerv },
110 { "alcCaptureOpenDevice", (ALvoid *) alcCaptureOpenDevice },
111 { "alcCaptureCloseDevice", (ALvoid *) alcCaptureCloseDevice },
112 { "alcCaptureStart", (ALvoid *) alcCaptureStart },
113 { "alcCaptureStop", (ALvoid *) alcCaptureStop },
114 { "alcCaptureSamples", (ALvoid *) alcCaptureSamples },
116 { "alcMakeCurrent", (ALvoid *) alcMakeCurrent },
117 { "alcGetThreadContext", (ALvoid *) alcGetThreadContext },
119 { NULL, (ALvoid *) NULL }
122 static ALenums enumeration[]={
123 // Types
124 { (ALchar *)"ALC_INVALID", ALC_INVALID },
125 { (ALchar *)"ALC_FALSE", ALC_FALSE },
126 { (ALchar *)"ALC_TRUE", ALC_TRUE },
128 // ALC Properties
129 { (ALchar *)"ALC_MAJOR_VERSION", ALC_MAJOR_VERSION },
130 { (ALchar *)"ALC_MINOR_VERSION", ALC_MINOR_VERSION },
131 { (ALchar *)"ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE },
132 { (ALchar *)"ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES },
133 { (ALchar *)"ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER },
134 { (ALchar *)"ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER },
135 { (ALchar *)"ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER },
136 { (ALchar *)"ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER },
137 { (ALchar *)"ALC_EXTENSIONS", ALC_EXTENSIONS },
138 { (ALchar *)"ALC_FREQUENCY", ALC_FREQUENCY },
139 { (ALchar *)"ALC_REFRESH", ALC_REFRESH },
140 { (ALchar *)"ALC_SYNC", ALC_SYNC },
141 { (ALchar *)"ALC_MONO_SOURCES", ALC_MONO_SOURCES },
142 { (ALchar *)"ALC_STEREO_SOURCES", ALC_STEREO_SOURCES },
143 { (ALchar *)"ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER },
144 { (ALchar *)"ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER},
145 { (ALchar *)"ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES },
147 // EFX Properties
148 { (ALchar *)"ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION },
149 { (ALchar *)"ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION },
150 { (ALchar *)"ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS },
152 // ALC Error Message
153 { (ALchar *)"ALC_NO_ERROR", ALC_NO_ERROR },
154 { (ALchar *)"ALC_INVALID_DEVICE", ALC_INVALID_DEVICE },
155 { (ALchar *)"ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT },
156 { (ALchar *)"ALC_INVALID_ENUM", ALC_INVALID_ENUM },
157 { (ALchar *)"ALC_INVALID_VALUE", ALC_INVALID_VALUE },
158 { (ALchar *)"ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY },
159 { (ALchar *)NULL, (ALenum)0 }
161 // Error strings
162 static const ALCchar alcNoError[] = "No Error";
163 static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
164 static const ALCchar alcErrInvalidContext[] = "Invalid Context";
165 static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
166 static const ALCchar alcErrInvalidValue[] = "Invalid Value";
167 static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
169 /* Device lists. Sizes only include the first ending null character, not the
170 * second */
171 static ALCchar *alcDeviceList;
172 static ALCuint alcDeviceListSize;
173 static ALCchar *alcAllDeviceList;
174 static ALCuint alcAllDeviceListSize;
175 static ALCchar *alcCaptureDeviceList;
176 static ALCuint alcCaptureDeviceListSize;
177 // Default is always the first in the list
178 static ALCchar *alcDefaultDeviceSpecifier;
179 static ALCchar *alcDefaultAllDeviceSpecifier;
180 static ALCchar *alcCaptureDefaultDeviceSpecifier;
183 static ALCchar alcExtensionList[] = "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_disconnect ALC_EXT_EFX ALC_EXTX_thread_local_context";
184 static ALCint alcMajorVersion = 1;
185 static ALCint alcMinorVersion = 1;
187 static ALCint alcEFXMajorVersion = 1;
188 static ALCint alcEFXMinorVersion = 0;
190 ///////////////////////////////////////////////////////
193 ///////////////////////////////////////////////////////
194 // Global Variables
196 static ALCdevice *g_pDeviceList = NULL;
197 static ALCuint g_ulDeviceCount = 0;
199 static CRITICAL_SECTION g_csMutex;
201 // Context List
202 static ALCcontext *g_pContextList = NULL;
203 static ALCuint g_ulContextCount = 0;
205 // Thread-local current context
206 static tls_type LocalContext;
208 // Context Error
209 static ALCenum g_eLastContextError = ALC_NO_ERROR;
211 // Default context extensions
212 static const ALchar alExtList[] =
213 "AL_EXTX_buffer_sub_data AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 "
214 "AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_OFFSET "
215 "AL_EXTX_sample_buffer_object AL_EXTX_source_distance_model "
216 "AL_LOKI_quadriphonic";
218 // Mixing Priority Level
219 ALint RTPrioLevel;
221 ///////////////////////////////////////////////////////
224 ///////////////////////////////////////////////////////
225 // ALC Related helper functions
226 #ifdef _WIN32
227 static void alc_init(void);
228 static void alc_deinit(void);
230 BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
232 (void)lpReserved;
234 // Perform actions based on the reason for calling.
235 switch(ul_reason_for_call)
237 case DLL_PROCESS_ATTACH:
238 DisableThreadLibraryCalls(hModule);
239 alc_init();
240 break;
242 case DLL_PROCESS_DETACH:
243 alc_deinit();
244 break;
246 return TRUE;
248 #else
249 #ifdef HAVE_GCC_DESTRUCTOR
250 static void alc_init(void) __attribute__((constructor));
251 static void alc_deinit(void) __attribute__((destructor));
252 #endif
253 #endif
255 static void alc_init(void)
257 int i;
258 const char *devs, *str;
260 InitializeCriticalSection(&g_csMutex);
261 ALTHUNK_INIT();
262 ReadALConfig();
264 tls_create(&LocalContext);
266 RTPrioLevel = GetConfigValueInt(NULL, "rt-prio", 0);
268 devs = GetConfigValue(NULL, "drivers", "");
269 if(devs[0])
271 int n;
272 size_t len;
273 const char *next = devs;
274 int endlist;
276 i = 0;
277 do {
278 devs = next;
279 next = strchr(devs, ',');
281 if(!devs[0] || devs[0] == ',')
283 endlist = 0;
284 continue;
286 endlist = 1;
288 len = (next ? ((size_t)(next-devs)) : strlen(devs));
289 for(n = i;BackendList[n].Init;n++)
291 if(len == strlen(BackendList[n].name) &&
292 strncmp(BackendList[n].name, devs, len) == 0)
294 BackendInfo Bkp = BackendList[n];
295 while(n > i)
297 BackendList[n] = BackendList[n-1];
298 --n;
300 BackendList[n] = Bkp;
302 i++;
303 break;
306 } while(next++);
308 if(endlist)
310 BackendList[i].name = NULL;
311 BackendList[i].Init = NULL;
312 BackendList[i].Deinit = NULL;
313 BackendList[i].Probe = NULL;
317 for(i = 0;BackendList[i].Init;i++)
319 BackendList[i].Init(&BackendList[i].Funcs);
321 BackendList[i].Probe(DEVICE_PROBE);
322 BackendList[i].Probe(ALL_DEVICE_PROBE);
323 BackendList[i].Probe(CAPTURE_DEVICE_PROBE);
326 DuplicateStereo = GetConfigValueBool(NULL, "stereodup", 0);
328 str = GetConfigValue(NULL, "excludefx", "");
329 if(str[0])
331 const struct {
332 const char *name;
333 int type;
334 } EffectList[] = {
335 { "eaxreverb", EAXREVERB },
336 { "reverb", REVERB },
337 { "echo", ECHO },
338 { NULL, 0 }
340 int n;
341 size_t len;
342 const char *next = str;
344 do {
345 str = next;
346 next = strchr(str, ',');
348 if(!str[0] || next == str)
349 continue;
351 len = (next ? ((size_t)(next-str)) : strlen(str));
352 for(n = 0;EffectList[n].name;n++)
354 if(len == strlen(EffectList[n].name) &&
355 strncmp(EffectList[n].name, str, len) == 0)
356 DisabledEffects[EffectList[n].type] = AL_TRUE;
358 } while(next++);
362 static void alc_deinit(void)
364 int i;
366 ReleaseALC();
368 for(i = 0;BackendList[i].Deinit;i++)
369 BackendList[i].Deinit();
371 tls_delete(LocalContext);
373 FreeALConfig();
374 ALTHUNK_EXIT();
375 DeleteCriticalSection(&g_csMutex);
379 static void ProbeDeviceList()
381 ALint i;
383 free(alcDeviceList); alcDeviceList = NULL;
384 alcDeviceListSize = 0;
386 for(i = 0;BackendList[i].Probe;i++)
387 BackendList[i].Probe(DEVICE_PROBE);
390 static void ProbeAllDeviceList()
392 ALint i;
394 free(alcAllDeviceList); alcAllDeviceList = NULL;
395 alcAllDeviceListSize = 0;
397 for(i = 0;BackendList[i].Probe;i++)
398 BackendList[i].Probe(ALL_DEVICE_PROBE);
401 static void ProbeCaptureDeviceList()
403 ALint i;
405 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
406 alcCaptureDeviceListSize = 0;
408 for(i = 0;BackendList[i].Probe;i++)
409 BackendList[i].Probe(CAPTURE_DEVICE_PROBE);
413 #define DECL_APPEND_LIST_FUNC(type) \
414 void Append##type##List(const ALCchar *name) \
416 ALCuint len = strlen(name); \
417 void *temp; \
419 if(len == 0) \
420 return; \
422 temp = realloc(alc##type##List, alc##type##ListSize + len + 2); \
423 if(!temp) \
425 AL_PRINT("Realloc failed to add %s!\n", name); \
426 return; \
428 alc##type##List = temp; \
429 sprintf(alc##type##List+alc##type##ListSize, "%s", name); \
430 alc##type##ListSize += len+1; \
431 alc##type##List[alc##type##ListSize] = 0; \
434 DECL_APPEND_LIST_FUNC(Device)
435 DECL_APPEND_LIST_FUNC(AllDevice)
436 DECL_APPEND_LIST_FUNC(CaptureDevice)
439 void EnableRTPrio(ALint level)
441 ALboolean failed;
443 #ifdef _WIN32
444 if(level > 0)
445 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
446 else
447 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
448 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM)
449 struct sched_param param;
451 if(level > 0)
453 /* Use the minimum real-time priority possible for now (on Linux this
454 * should be 1 for SCHED_RR) */
455 param.sched_priority = sched_get_priority_min(SCHED_RR);
456 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
458 else
460 param.sched_priority = 0;
461 failed = !!pthread_setschedparam(pthread_self(), SCHED_OTHER, &param);
463 #else
464 /* Real-time priority not available */
465 failed = !!level;
466 #endif
467 if(failed)
468 AL_PRINT("Failed to set priority level for thread\n");
473 IsDevice
475 Check pDevice is a valid Device pointer
477 static ALCboolean IsDevice(ALCdevice *pDevice)
479 ALCdevice *pTempDevice;
481 SuspendContext(NULL);
483 pTempDevice = g_pDeviceList;
484 while(pTempDevice && pTempDevice != pDevice)
485 pTempDevice = pTempDevice->next;
487 ProcessContext(NULL);
489 return (pTempDevice ? ALC_TRUE : ALC_FALSE);
493 IsContext
495 Check pContext is a valid Context pointer
497 static ALCboolean IsContext(ALCcontext *pContext)
499 ALCcontext *pTempContext;
501 SuspendContext(NULL);
503 pTempContext = g_pContextList;
504 while (pTempContext && pTempContext != pContext)
505 pTempContext = pTempContext->next;
507 ProcessContext(NULL);
509 return (pTempContext ? ALC_TRUE : ALC_FALSE);
514 alcSetError
516 Store latest ALC Error
518 ALCvoid alcSetError(ALCdevice *device, ALenum errorCode)
520 if(IsDevice(device))
521 device->LastError = errorCode;
522 else
523 g_eLastContextError = errorCode;
528 SuspendContext
530 Thread-safe entry
532 ALCvoid SuspendContext(ALCcontext *pContext)
534 (void)pContext;
535 EnterCriticalSection(&g_csMutex);
540 ProcessContext
542 Thread-safe exit
544 ALCvoid ProcessContext(ALCcontext *pContext)
546 (void)pContext;
547 LeaveCriticalSection(&g_csMutex);
552 GetContextSuspended
554 Returns the currently active Context, in a locked state
556 ALCcontext *GetContextSuspended(void)
558 ALCcontext *pContext = NULL;
560 SuspendContext(NULL);
562 pContext = tls_get(LocalContext);
563 if(pContext && !IsContext(pContext))
565 tls_set(LocalContext, NULL);
566 pContext = NULL;
568 if(!pContext)
570 pContext = g_pContextList;
571 while(pContext && !pContext->InUse)
572 pContext = pContext->next;
574 if(pContext)
575 SuspendContext(pContext);
577 ProcessContext(NULL);
579 return pContext;
584 InitContext
586 Initialize Context variables
588 static ALvoid InitContext(ALCcontext *pContext)
590 //Initialise listener
591 pContext->Listener.Gain = 1.0f;
592 pContext->Listener.MetersPerUnit = 1.0f;
593 pContext->Listener.Position[0] = 0.0f;
594 pContext->Listener.Position[1] = 0.0f;
595 pContext->Listener.Position[2] = 0.0f;
596 pContext->Listener.Velocity[0] = 0.0f;
597 pContext->Listener.Velocity[1] = 0.0f;
598 pContext->Listener.Velocity[2] = 0.0f;
599 pContext->Listener.Forward[0] = 0.0f;
600 pContext->Listener.Forward[1] = 0.0f;
601 pContext->Listener.Forward[2] = -1.0f;
602 pContext->Listener.Up[0] = 0.0f;
603 pContext->Listener.Up[1] = 1.0f;
604 pContext->Listener.Up[2] = 0.0f;
606 //Validate pContext
607 pContext->LastError = AL_NO_ERROR;
608 pContext->InUse = AL_FALSE;
609 pContext->Suspended = AL_FALSE;
611 //Set globals
612 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
613 pContext->SourceDistanceModel = AL_FALSE;
614 pContext->DopplerFactor = 1.0f;
615 pContext->DopplerVelocity = 1.0f;
616 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
618 pContext->ExtensionList = alExtList;
623 ExitContext
625 Clean up Context, destroy any remaining Sources
627 static ALCvoid ExitContext(ALCcontext *pContext)
629 //Invalidate context
630 pContext->LastError = AL_NO_ERROR;
631 pContext->InUse = AL_FALSE;
634 ///////////////////////////////////////////////////////
637 ///////////////////////////////////////////////////////
638 // ALC Functions calls
641 // This should probably move to another c file but for now ...
642 ALCAPI ALCdevice* ALCAPIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
644 ALCboolean DeviceFound = ALC_FALSE;
645 ALCdevice *pDevice = NULL;
646 ALCint i;
648 if(SampleSize <= 0)
650 alcSetError(NULL, ALC_INVALID_VALUE);
651 return NULL;
654 if(deviceName && !deviceName[0])
655 deviceName = NULL;
657 pDevice = malloc(sizeof(ALCdevice));
658 if (pDevice)
660 //Initialise device structure
661 memset(pDevice, 0, sizeof(ALCdevice));
663 //Validate device
664 pDevice->Connected = ALC_TRUE;
665 pDevice->IsCaptureDevice = AL_TRUE;
667 pDevice->szDeviceName = NULL;
669 pDevice->Frequency = frequency;
670 pDevice->Format = format;
671 pDevice->UpdateSize = SampleSize;
672 pDevice->NumUpdates = 1;
674 SuspendContext(NULL);
675 for(i = 0;BackendList[i].Init;i++)
677 pDevice->Funcs = &BackendList[i].Funcs;
678 if(ALCdevice_OpenCapture(pDevice, deviceName))
680 pDevice->next = g_pDeviceList;
681 g_pDeviceList = pDevice;
682 g_ulDeviceCount++;
684 DeviceFound = ALC_TRUE;
685 break;
688 ProcessContext(NULL);
690 if(!DeviceFound)
692 alcSetError(NULL, ALC_INVALID_VALUE);
693 free(pDevice);
694 pDevice = NULL;
697 else
698 alcSetError(NULL, ALC_OUT_OF_MEMORY);
700 return pDevice;
703 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
705 ALCboolean bReturn = ALC_FALSE;
706 ALCdevice **list;
708 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
710 SuspendContext(NULL);
712 list = &g_pDeviceList;
713 while(*list != pDevice)
714 list = &(*list)->next;
716 *list = (*list)->next;
717 g_ulDeviceCount--;
719 ProcessContext(NULL);
721 ALCdevice_CloseCapture(pDevice);
723 free(pDevice->szDeviceName);
724 pDevice->szDeviceName = NULL;
726 free(pDevice);
728 bReturn = ALC_TRUE;
730 else
731 alcSetError(pDevice, ALC_INVALID_DEVICE);
733 return bReturn;
736 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
738 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
739 ALCdevice_StartCapture(pDevice);
740 else
741 alcSetError(pDevice, ALC_INVALID_DEVICE);
744 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
746 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
747 ALCdevice_StopCapture(pDevice);
748 else
749 alcSetError(pDevice, ALC_INVALID_DEVICE);
752 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
754 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
755 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
756 else
757 alcSetError(pDevice, ALC_INVALID_DEVICE);
761 alcGetError
763 Return last ALC generated error code
765 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
767 ALCenum errorCode = ALC_NO_ERROR;
769 if(device)
771 errorCode = device->LastError;
772 device->LastError = ALC_NO_ERROR;
774 else
776 errorCode = g_eLastContextError;
777 g_eLastContextError = ALC_NO_ERROR;
779 return errorCode;
784 alcSuspendContext
786 Not functional
788 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
790 SuspendContext(NULL);
791 if(IsContext(pContext))
792 pContext->Suspended = AL_TRUE;
793 ProcessContext(NULL);
798 alcProcessContext
800 Not functional
802 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
804 SuspendContext(NULL);
805 if(IsContext(pContext))
806 pContext->Suspended = AL_FALSE;
807 ProcessContext(NULL);
812 alcGetString
814 Returns information about the Device, and error strings
816 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
818 const ALCchar *value = NULL;
820 switch (param)
822 case ALC_NO_ERROR:
823 value = alcNoError;
824 break;
826 case ALC_INVALID_ENUM:
827 value = alcErrInvalidEnum;
828 break;
830 case ALC_INVALID_VALUE:
831 value = alcErrInvalidValue;
832 break;
834 case ALC_INVALID_DEVICE:
835 value = alcErrInvalidDevice;
836 break;
838 case ALC_INVALID_CONTEXT:
839 value = alcErrInvalidContext;
840 break;
842 case ALC_OUT_OF_MEMORY:
843 value = alcErrOutOfMemory;
844 break;
846 case ALC_DEVICE_SPECIFIER:
847 if(IsDevice(pDevice))
848 value = pDevice->szDeviceName;
849 else
851 ProbeDeviceList();
852 value = alcDeviceList;
854 break;
856 case ALC_ALL_DEVICES_SPECIFIER:
857 ProbeAllDeviceList();
858 value = alcAllDeviceList;
859 break;
861 case ALC_CAPTURE_DEVICE_SPECIFIER:
862 if(IsDevice(pDevice))
863 value = pDevice->szDeviceName;
864 else
866 ProbeCaptureDeviceList();
867 value = alcCaptureDeviceList;
869 break;
871 /* Default devices are always first in the list */
872 case ALC_DEFAULT_DEVICE_SPECIFIER:
873 free(alcDefaultDeviceSpecifier);
874 alcDefaultDeviceSpecifier = strdup(alcDeviceList ? alcDeviceList : "");
875 if(!alcDefaultDeviceSpecifier)
876 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
877 value = alcDefaultDeviceSpecifier;
878 break;
880 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
881 free(alcDefaultAllDeviceSpecifier);
882 alcDefaultAllDeviceSpecifier = strdup(alcAllDeviceList ?
883 alcAllDeviceList : "");
884 if(!alcDefaultAllDeviceSpecifier)
885 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
886 value = alcDefaultAllDeviceSpecifier;
887 break;
889 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
890 free(alcCaptureDefaultDeviceSpecifier);
891 alcCaptureDefaultDeviceSpecifier = strdup(alcCaptureDeviceList ?
892 alcCaptureDeviceList : "");
893 if(!alcCaptureDefaultDeviceSpecifier)
894 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
895 value = alcCaptureDefaultDeviceSpecifier;
896 break;
898 case ALC_EXTENSIONS:
899 value = alcExtensionList;
900 break;
902 default:
903 alcSetError(pDevice, ALC_INVALID_ENUM);
904 break;
907 return value;
912 alcGetIntegerv
914 Returns information about the Device and the version of Open AL
916 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
918 if(size == 0 || data == NULL)
920 alcSetError(device, ALC_INVALID_VALUE);
921 return;
924 if(IsDevice(device) && device->IsCaptureDevice)
926 SuspendContext(NULL);
928 // Capture device
929 switch (param)
931 case ALC_CAPTURE_SAMPLES:
932 *data = ALCdevice_AvailableSamples(device);
933 break;
935 case ALC_CONNECTED:
936 *data = device->Connected;
937 break;
939 default:
940 alcSetError(device, ALC_INVALID_ENUM);
941 break;
944 ProcessContext(NULL);
945 return;
948 // Playback Device
949 switch (param)
951 case ALC_MAJOR_VERSION:
952 *data = alcMajorVersion;
953 break;
955 case ALC_MINOR_VERSION:
956 *data = alcMinorVersion;
957 break;
959 case ALC_EFX_MAJOR_VERSION:
960 *data = alcEFXMajorVersion;
961 break;
963 case ALC_EFX_MINOR_VERSION:
964 *data = alcEFXMinorVersion;
965 break;
967 case ALC_MAX_AUXILIARY_SENDS:
968 if(!IsDevice(device))
969 alcSetError(device, ALC_INVALID_DEVICE);
970 else
971 *data = device->NumAuxSends;
972 break;
974 case ALC_ATTRIBUTES_SIZE:
975 if(!IsDevice(device))
976 alcSetError(device, ALC_INVALID_DEVICE);
977 else
978 *data = 13;
979 break;
981 case ALC_ALL_ATTRIBUTES:
982 if(!IsDevice(device))
983 alcSetError(device, ALC_INVALID_DEVICE);
984 else if (size < 13)
985 alcSetError(device, ALC_INVALID_VALUE);
986 else
988 int i = 0;
990 SuspendContext(NULL);
991 data[i++] = ALC_FREQUENCY;
992 data[i++] = device->Frequency;
994 data[i++] = ALC_REFRESH;
995 data[i++] = device->Frequency / device->UpdateSize;
997 data[i++] = ALC_SYNC;
998 data[i++] = ALC_FALSE;
1000 data[i++] = ALC_MONO_SOURCES;
1001 data[i++] = device->lNumMonoSources;
1003 data[i++] = ALC_STEREO_SOURCES;
1004 data[i++] = device->lNumStereoSources;
1006 data[i++] = ALC_MAX_AUXILIARY_SENDS;
1007 data[i++] = device->NumAuxSends;
1009 data[i++] = 0;
1010 ProcessContext(NULL);
1012 break;
1014 case ALC_FREQUENCY:
1015 if(!IsDevice(device))
1016 alcSetError(device, ALC_INVALID_DEVICE);
1017 else
1018 *data = device->Frequency;
1019 break;
1021 case ALC_REFRESH:
1022 if(!IsDevice(device))
1023 alcSetError(device, ALC_INVALID_DEVICE);
1024 else
1025 *data = device->Frequency / device->UpdateSize;
1026 break;
1028 case ALC_SYNC:
1029 if(!IsDevice(device))
1030 alcSetError(device, ALC_INVALID_DEVICE);
1031 else
1032 *data = ALC_FALSE;
1033 break;
1035 case ALC_MONO_SOURCES:
1036 if(!IsDevice(device))
1037 alcSetError(device, ALC_INVALID_DEVICE);
1038 else
1039 *data = device->lNumMonoSources;
1040 break;
1042 case ALC_STEREO_SOURCES:
1043 if(!IsDevice(device))
1044 alcSetError(device, ALC_INVALID_DEVICE);
1045 else
1046 *data = device->lNumStereoSources;
1047 break;
1049 case ALC_CONNECTED:
1050 if(!IsDevice(device))
1051 alcSetError(device, ALC_INVALID_DEVICE);
1052 else
1053 *data = device->Connected;
1054 break;
1056 default:
1057 alcSetError(device, ALC_INVALID_ENUM);
1058 break;
1064 alcIsExtensionPresent
1066 Determines if there is support for a particular extension
1068 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
1070 ALCboolean bResult = ALC_FALSE;
1072 if (extName)
1074 const char *ptr;
1075 size_t len;
1077 len = strlen(extName);
1078 ptr = alcExtensionList;
1079 while(ptr && *ptr)
1081 if(strncasecmp(ptr, extName, len) == 0 &&
1082 (ptr[len] == '\0' || isspace(ptr[len])))
1084 bResult = ALC_TRUE;
1085 break;
1087 if((ptr=strchr(ptr, ' ')) != NULL)
1089 do {
1090 ++ptr;
1091 } while(isspace(*ptr));
1095 else
1096 alcSetError(device, ALC_INVALID_VALUE);
1098 return bResult;
1103 alcGetProcAddress
1105 Retrieves the function address for a particular extension function
1107 ALCAPI ALCvoid * ALCAPIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
1109 ALCvoid *pFunction = NULL;
1110 ALsizei i = 0;
1112 if (funcName)
1114 while(alcFunctions[i].funcName &&
1115 strcmp(alcFunctions[i].funcName,funcName) != 0)
1116 i++;
1117 pFunction = alcFunctions[i].address;
1119 else
1120 alcSetError(device, ALC_INVALID_VALUE);
1122 return pFunction;
1127 alcGetEnumValue
1129 Get the value for a particular ALC Enumerated Value
1131 ALCAPI ALCenum ALCAPIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
1133 ALsizei i = 0;
1134 ALCenum val;
1136 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
1137 i++;
1138 val = enumeration[i].value;
1140 if(!enumeration[i].enumName)
1141 alcSetError(device, ALC_INVALID_VALUE);
1143 return val;
1148 alcCreateContext
1150 Create and attach a Context to a particular Device.
1152 ALCAPI ALCcontext* ALCAPIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
1154 ALuint attrIdx, reqStereoSources;
1155 ALCcontext *ALContext;
1156 void *temp;
1157 ALuint i;
1159 SuspendContext(NULL);
1161 if(!IsDevice(device) || device->IsCaptureDevice || !device->Connected)
1163 alcSetError(device, ALC_INVALID_DEVICE);
1164 ProcessContext(NULL);
1165 return NULL;
1168 // Reset Context Last Error code
1169 device->LastError = ALC_NO_ERROR;
1171 // If a context is already running on the device, stop playback so the
1172 // device attributes can be updated
1173 if(device->NumContexts > 0)
1175 ProcessContext(NULL);
1176 ALCdevice_StopPlayback(device);
1177 SuspendContext(NULL);
1180 // Check for attributes
1181 if(attrList)
1183 ALCint level = device->Bs2bLevel;
1184 ALCuint freq = device->Frequency;
1185 ALCint numMono = device->lNumMonoSources;
1186 ALCint numStereo = device->lNumStereoSources;
1187 ALCuint numSends = device->NumAuxSends;
1189 attrIdx = 0;
1190 while(attrList[attrIdx])
1192 if(attrList[attrIdx] == ALC_FREQUENCY &&
1193 !ConfigValueExists(NULL, "frequency"))
1195 freq = attrList[attrIdx + 1];
1196 if(freq < 8000)
1197 freq = 8000;
1200 if(attrList[attrIdx] == ALC_STEREO_SOURCES)
1202 reqStereoSources = attrList[attrIdx + 1];
1203 if(reqStereoSources > device->MaxNoOfSources)
1204 reqStereoSources = device->MaxNoOfSources;
1206 numStereo = reqStereoSources;
1207 numMono = device->MaxNoOfSources - numStereo;
1210 if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS &&
1211 !ConfigValueExists(NULL, "sends"))
1213 numSends = attrList[attrIdx + 1];
1214 if(numSends > MAX_SENDS)
1215 numSends = MAX_SENDS;
1218 attrIdx += 2;
1221 device->Bs2bLevel = level;
1222 device->Frequency = freq;
1223 device->lNumMonoSources = numMono;
1224 device->lNumStereoSources = numStereo;
1225 device->NumAuxSends = numSends;
1228 if(ALCdevice_ResetPlayback(device) == ALC_FALSE)
1230 alcSetError(device, ALC_INVALID_DEVICE);
1231 aluHandleDisconnect(device);
1232 ProcessContext(NULL);
1233 return NULL;
1236 for(i = 0;i < device->NumContexts;i++)
1238 ALCcontext *context = device->Contexts[i];
1239 ALeffectslot *slot;
1240 ALsource *source;
1242 SuspendContext(context);
1243 for(slot = context->AuxiliaryEffectSlot;slot != NULL;slot = slot->next)
1245 if(!slot->EffectState)
1246 continue;
1248 if(ALEffect_DeviceUpdate(slot->EffectState, device) == AL_FALSE)
1250 alcSetError(device, ALC_INVALID_DEVICE);
1251 aluHandleDisconnect(device);
1252 ProcessContext(context);
1253 ProcessContext(NULL);
1254 ALCdevice_StopPlayback(device);
1255 return NULL;
1257 ALEffect_Update(slot->EffectState, context, &slot->effect);
1260 for(source = context->Source;source != NULL;source = source->next)
1262 ALuint s = device->NumAuxSends;
1263 while(s < MAX_SENDS)
1265 if(source->Send[s].Slot)
1266 source->Send[s].Slot->refcount--;
1267 source->Send[s].Slot = NULL;
1268 source->Send[s].WetFilter.type = 0;
1269 source->Send[s].WetFilter.filter = 0;
1270 s++;
1272 source->NeedsUpdate = AL_TRUE;
1274 aluInitPanning(context);
1275 ProcessContext(context);
1278 if(device->Bs2bLevel > 0 && device->Bs2bLevel <= 6)
1280 if(!device->Bs2b)
1282 device->Bs2b = calloc(1, sizeof(*device->Bs2b));
1283 bs2b_clear(device->Bs2b);
1285 bs2b_set_srate(device->Bs2b, device->Frequency);
1286 bs2b_set_level(device->Bs2b, device->Bs2bLevel);
1288 else
1290 free(device->Bs2b);
1291 device->Bs2b = NULL;
1294 temp = realloc(device->Contexts, (device->NumContexts+1) * sizeof(*device->Contexts));
1295 if(!temp)
1297 alcSetError(device, ALC_OUT_OF_MEMORY);
1298 ProcessContext(NULL);
1299 return NULL;
1301 device->Contexts = temp;
1303 ALContext = calloc(1, sizeof(ALCcontext));
1304 if(!ALContext)
1306 alcSetError(device, ALC_OUT_OF_MEMORY);
1307 ProcessContext(NULL);
1308 return NULL;
1311 device->Contexts[device->NumContexts++] = ALContext;
1312 ALContext->Device = device;
1314 InitContext(ALContext);
1315 aluInitPanning(ALContext);
1317 ALContext->next = g_pContextList;
1318 g_pContextList = ALContext;
1319 g_ulContextCount++;
1321 ProcessContext(NULL);
1323 return ALContext;
1328 alcDestroyContext
1330 Remove a Context
1332 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
1334 ALCcontext **list;
1335 ALuint i;
1337 if (IsContext(context))
1339 ALCdevice *Device = context->Device;
1341 if(Device->NumContexts == 1)
1342 ALCdevice_StopPlayback(Device);
1344 SuspendContext(NULL);
1346 for(i = 0;i < Device->NumContexts-1;i++)
1348 if(Device->Contexts[i] == context)
1350 memmove(&Device->Contexts[i], &Device->Contexts[i+1],
1351 (Device->NumContexts-i-1) * sizeof(*Device->Contexts));
1352 break;
1355 Device->NumContexts--;
1357 // Lock context
1358 SuspendContext(context);
1360 if(context->SourceCount > 0)
1362 #ifdef _DEBUG
1363 AL_PRINT("alcDestroyContext(): deleting %d Source(s)\n", context->SourceCount);
1364 #endif
1365 ReleaseALSources(context);
1367 if(context->AuxiliaryEffectSlotCount > 0)
1369 #ifdef _DEBUG
1370 AL_PRINT("alcDestroyContext(): deleting %d AuxiliaryEffectSlot(s)\n", context->AuxiliaryEffectSlotCount);
1371 #endif
1372 ReleaseALAuxiliaryEffectSlots(context);
1375 list = &g_pContextList;
1376 while(*list != context)
1377 list = &(*list)->next;
1379 *list = (*list)->next;
1380 g_ulContextCount--;
1382 // Unlock context
1383 ProcessContext(context);
1384 ProcessContext(NULL);
1386 ExitContext(context);
1388 // Free memory (MUST do this after ProcessContext)
1389 memset(context, 0, sizeof(ALCcontext));
1390 free(context);
1392 else
1393 alcSetError(NULL, ALC_INVALID_CONTEXT);
1398 alcGetCurrentContext
1400 Returns the currently active Context
1402 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1404 ALCcontext *pContext;
1406 if((pContext=GetContextSuspended()) != NULL)
1407 ProcessContext(pContext);
1409 return pContext;
1413 alcGetThreadContext
1415 Returns the currently active thread-local Context
1417 ALCcontext * ALCAPIENTRY alcGetThreadContext(void)
1419 ALCcontext *pContext = NULL;
1421 SuspendContext(NULL);
1423 pContext = tls_get(LocalContext);
1424 if(pContext && !IsContext(pContext))
1426 tls_set(LocalContext, NULL);
1427 pContext = NULL;
1430 ProcessContext(NULL);
1432 return pContext;
1437 alcGetContextsDevice
1439 Returns the Device that a particular Context is attached to
1441 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1443 ALCdevice *pDevice = NULL;
1445 SuspendContext(NULL);
1446 if (IsContext(pContext))
1447 pDevice = pContext->Device;
1448 else
1449 alcSetError(NULL, ALC_INVALID_CONTEXT);
1450 ProcessContext(NULL);
1452 return pDevice;
1457 alcMakeContextCurrent
1459 Makes the given Context the active Context
1461 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1463 ALCcontext *ALContext;
1464 ALboolean bReturn = AL_TRUE;
1466 SuspendContext(NULL);
1468 // context must be a valid Context or NULL
1469 if(context == NULL || IsContext(context))
1471 if((ALContext=GetContextSuspended()) != NULL)
1473 ALContext->InUse=AL_FALSE;
1474 ProcessContext(ALContext);
1477 if((ALContext=context) != NULL && ALContext->Device)
1479 SuspendContext(ALContext);
1480 ALContext->InUse=AL_TRUE;
1481 ProcessContext(ALContext);
1484 tls_set(LocalContext, NULL);
1486 else
1488 alcSetError(NULL, ALC_INVALID_CONTEXT);
1489 bReturn = AL_FALSE;
1492 ProcessContext(NULL);
1494 return bReturn;
1498 alcMakeCurrent
1500 Makes the given Context the active Context for the current thread
1502 ALCboolean ALCAPIENTRY alcMakeCurrent(ALCcontext *context)
1504 ALboolean bReturn = AL_TRUE;
1506 SuspendContext(NULL);
1508 // context must be a valid Context or NULL
1509 if(context == NULL || IsContext(context))
1510 tls_set(LocalContext, context);
1511 else
1513 alcSetError(NULL, ALC_INVALID_CONTEXT);
1514 bReturn = AL_FALSE;
1517 ProcessContext(NULL);
1519 return bReturn;
1523 // Sets the default channel order used by most non-WaveFormatEx-based APIs
1524 void SetDefaultChannelOrder(ALCdevice *device)
1526 switch(aluChannelsFromFormat(device->Format))
1528 case 1: device->DevChannels[0] = FRONT_CENTER; break;
1530 case 2: device->DevChannels[0] = FRONT_LEFT;
1531 device->DevChannels[1] = FRONT_RIGHT; break;
1533 case 4: device->DevChannels[0] = FRONT_LEFT;
1534 device->DevChannels[1] = FRONT_RIGHT;
1535 device->DevChannels[2] = BACK_LEFT;
1536 device->DevChannels[3] = BACK_RIGHT; break;
1538 case 6: device->DevChannels[0] = FRONT_LEFT;
1539 device->DevChannels[1] = FRONT_RIGHT;
1540 device->DevChannels[2] = BACK_LEFT;
1541 device->DevChannels[3] = BACK_RIGHT;
1542 device->DevChannels[4] = FRONT_CENTER;
1543 device->DevChannels[5] = LFE; break;
1545 case 7: device->DevChannels[0] = FRONT_LEFT;
1546 device->DevChannels[1] = FRONT_RIGHT;
1547 device->DevChannels[2] = FRONT_CENTER;
1548 device->DevChannels[3] = LFE;
1549 device->DevChannels[4] = BACK_CENTER;
1550 device->DevChannels[5] = SIDE_LEFT;
1551 device->DevChannels[6] = SIDE_RIGHT; break;
1553 case 8: device->DevChannels[0] = FRONT_LEFT;
1554 device->DevChannels[1] = FRONT_RIGHT;
1555 device->DevChannels[2] = BACK_LEFT;
1556 device->DevChannels[3] = BACK_RIGHT;
1557 device->DevChannels[4] = FRONT_CENTER;
1558 device->DevChannels[5] = LFE;
1559 device->DevChannels[6] = SIDE_LEFT;
1560 device->DevChannels[7] = SIDE_RIGHT; break;
1563 // Sets the default order used by WaveFormatEx
1564 void SetDefaultWFXChannelOrder(ALCdevice *device)
1566 switch(aluChannelsFromFormat(device->Format))
1568 case 1: device->DevChannels[0] = FRONT_CENTER; break;
1570 case 2: device->DevChannels[0] = FRONT_LEFT;
1571 device->DevChannels[1] = FRONT_RIGHT; break;
1573 case 4: device->DevChannels[0] = FRONT_LEFT;
1574 device->DevChannels[1] = FRONT_RIGHT;
1575 device->DevChannels[2] = BACK_LEFT;
1576 device->DevChannels[3] = BACK_RIGHT; break;
1578 case 6: device->DevChannels[0] = FRONT_LEFT;
1579 device->DevChannels[1] = FRONT_RIGHT;
1580 device->DevChannels[2] = FRONT_CENTER;
1581 device->DevChannels[3] = LFE;
1582 device->DevChannels[4] = BACK_LEFT;
1583 device->DevChannels[5] = BACK_RIGHT; break;
1585 case 7: device->DevChannels[0] = FRONT_LEFT;
1586 device->DevChannels[1] = FRONT_RIGHT;
1587 device->DevChannels[2] = FRONT_CENTER;
1588 device->DevChannels[3] = LFE;
1589 device->DevChannels[4] = BACK_CENTER;
1590 device->DevChannels[5] = SIDE_LEFT;
1591 device->DevChannels[6] = SIDE_RIGHT; break;
1593 case 8: device->DevChannels[0] = FRONT_LEFT;
1594 device->DevChannels[1] = FRONT_RIGHT;
1595 device->DevChannels[2] = FRONT_CENTER;
1596 device->DevChannels[3] = LFE;
1597 device->DevChannels[4] = BACK_LEFT;
1598 device->DevChannels[5] = BACK_RIGHT;
1599 device->DevChannels[6] = SIDE_LEFT;
1600 device->DevChannels[7] = SIDE_RIGHT; break;
1604 static ALenum GetFormatFromString(const char *str)
1606 if(strcasecmp(str, "AL_FORMAT_MONO32") == 0) return AL_FORMAT_MONO_FLOAT32;
1607 if(strcasecmp(str, "AL_FORMAT_STEREO32") == 0) return AL_FORMAT_STEREO_FLOAT32;
1608 if(strcasecmp(str, "AL_FORMAT_QUAD32") == 0) return AL_FORMAT_QUAD32;
1609 if(strcasecmp(str, "AL_FORMAT_51CHN32") == 0) return AL_FORMAT_51CHN32;
1610 if(strcasecmp(str, "AL_FORMAT_61CHN32") == 0) return AL_FORMAT_61CHN32;
1611 if(strcasecmp(str, "AL_FORMAT_71CHN32") == 0) return AL_FORMAT_71CHN32;
1613 if(strcasecmp(str, "AL_FORMAT_MONO16") == 0) return AL_FORMAT_MONO16;
1614 if(strcasecmp(str, "AL_FORMAT_STEREO16") == 0) return AL_FORMAT_STEREO16;
1615 if(strcasecmp(str, "AL_FORMAT_QUAD16") == 0) return AL_FORMAT_QUAD16;
1616 if(strcasecmp(str, "AL_FORMAT_51CHN16") == 0) return AL_FORMAT_51CHN16;
1617 if(strcasecmp(str, "AL_FORMAT_61CHN16") == 0) return AL_FORMAT_61CHN16;
1618 if(strcasecmp(str, "AL_FORMAT_71CHN16") == 0) return AL_FORMAT_71CHN16;
1620 if(strcasecmp(str, "AL_FORMAT_MONO8") == 0) return AL_FORMAT_MONO8;
1621 if(strcasecmp(str, "AL_FORMAT_STEREO8") == 0) return AL_FORMAT_STEREO8;
1622 if(strcasecmp(str, "AL_FORMAT_QUAD8") == 0) return AL_FORMAT_QUAD8;
1623 if(strcasecmp(str, "AL_FORMAT_51CHN8") == 0) return AL_FORMAT_51CHN8;
1624 if(strcasecmp(str, "AL_FORMAT_61CHN8") == 0) return AL_FORMAT_61CHN8;
1625 if(strcasecmp(str, "AL_FORMAT_71CHN8") == 0) return AL_FORMAT_71CHN8;
1627 AL_PRINT("Unknown format: \"%s\"\n", str);
1628 return AL_FORMAT_STEREO16;
1632 alcOpenDevice
1634 Open the Device specified.
1636 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1638 ALboolean bDeviceFound = AL_FALSE;
1639 ALCdevice *device;
1640 ALint i;
1642 if(deviceName && !deviceName[0])
1643 deviceName = NULL;
1645 device = malloc(sizeof(ALCdevice));
1646 if (device)
1648 const char *fmt;
1650 //Initialise device structure
1651 memset(device, 0, sizeof(ALCdevice));
1653 //Validate device
1654 device->Connected = ALC_TRUE;
1655 device->IsCaptureDevice = AL_FALSE;
1656 device->LastError = ALC_NO_ERROR;
1658 device->Bs2b = NULL;
1659 device->szDeviceName = NULL;
1661 device->Contexts = NULL;
1662 device->NumContexts = 0;
1664 //Set output format
1665 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1666 if(device->Frequency < 8000)
1667 device->Frequency = 8000;
1669 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1670 device->Format = GetFormatFromString(fmt);
1672 device->NumUpdates = GetConfigValueInt(NULL, "periods", 4);
1673 if(device->NumUpdates < 2)
1674 device->NumUpdates = 4;
1676 i = GetConfigValueInt(NULL, "refresh", 4096);
1677 if(i <= 0) i = 4096;
1679 device->UpdateSize = GetConfigValueInt(NULL, "period_size", i/device->NumUpdates);
1680 if(device->UpdateSize <= 0)
1681 device->UpdateSize = i/device->NumUpdates;
1683 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1684 if((ALint)device->MaxNoOfSources <= 0)
1685 device->MaxNoOfSources = 256;
1687 device->AuxiliaryEffectSlotMax = GetConfigValueInt(NULL, "slots", 4);
1688 if((ALint)device->AuxiliaryEffectSlotMax <= 0)
1689 device->AuxiliaryEffectSlotMax = 4;
1691 device->lNumStereoSources = 1;
1692 device->lNumMonoSources = device->MaxNoOfSources - device->lNumStereoSources;
1694 device->NumAuxSends = GetConfigValueInt(NULL, "sends", MAX_SENDS);
1695 if(device->NumAuxSends > MAX_SENDS)
1696 device->NumAuxSends = MAX_SENDS;
1698 device->Bs2bLevel = GetConfigValueInt(NULL, "cf_level", 0);
1700 if(aluChannelsFromFormat(device->Format) <= 2)
1702 device->HeadDampen = GetConfigValueFloat(NULL, "head_dampen", DEFAULT_HEAD_DAMPEN);
1703 device->HeadDampen = __min(device->HeadDampen, 1.0f);
1704 device->HeadDampen = __max(device->HeadDampen, 0.0f);
1706 else
1707 device->HeadDampen = 0.0f;
1709 // Find a playback device to open
1710 SuspendContext(NULL);
1711 for(i = 0;BackendList[i].Init;i++)
1713 device->Funcs = &BackendList[i].Funcs;
1714 if(ALCdevice_OpenPlayback(device, deviceName))
1716 device->next = g_pDeviceList;
1717 g_pDeviceList = device;
1718 g_ulDeviceCount++;
1720 bDeviceFound = AL_TRUE;
1721 break;
1724 ProcessContext(NULL);
1726 if (!bDeviceFound)
1728 // No suitable output device found
1729 alcSetError(NULL, ALC_INVALID_VALUE);
1730 free(device);
1731 device = NULL;
1734 else
1735 alcSetError(NULL, ALC_OUT_OF_MEMORY);
1737 return device;
1742 alcCloseDevice
1744 Close the specified Device
1746 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1748 ALCboolean bReturn = ALC_FALSE;
1749 ALCdevice **list;
1751 if(IsDevice(pDevice) && !pDevice->IsCaptureDevice)
1753 SuspendContext(NULL);
1755 list = &g_pDeviceList;
1756 while(*list != pDevice)
1757 list = &(*list)->next;
1759 *list = (*list)->next;
1760 g_ulDeviceCount--;
1762 ProcessContext(NULL);
1764 if(pDevice->NumContexts > 0)
1766 #ifdef _DEBUG
1767 AL_PRINT("alcCloseDevice(): destroying %u Context(s)\n", pDevice->NumContexts);
1768 #endif
1769 while(pDevice->NumContexts > 0)
1770 alcDestroyContext(pDevice->Contexts[0]);
1772 ALCdevice_ClosePlayback(pDevice);
1774 if(pDevice->BufferCount > 0)
1776 #ifdef _DEBUG
1777 AL_PRINT("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice->BufferCount);
1778 #endif
1779 ReleaseALBuffers(pDevice);
1781 if(pDevice->EffectCount > 0)
1783 #ifdef _DEBUG
1784 AL_PRINT("alcCloseDevice(): deleting %d Effect(s)\n", pDevice->EffectCount);
1785 #endif
1786 ReleaseALEffects(pDevice);
1788 if(pDevice->FilterCount > 0)
1790 #ifdef _DEBUG
1791 AL_PRINT("alcCloseDevice(): deleting %d Filter(s)\n", pDevice->FilterCount);
1792 #endif
1793 ReleaseALFilters(pDevice);
1795 if(pDevice->DatabufferCount > 0)
1797 #ifdef _DEBUG
1798 AL_PRINT("alcCloseDevice(): deleting %d Databuffer(s)\n", pDevice->DatabufferCount);
1799 #endif
1800 ReleaseALDatabuffers(pDevice);
1803 free(pDevice->Bs2b);
1804 pDevice->Bs2b = NULL;
1806 free(pDevice->szDeviceName);
1807 pDevice->szDeviceName = NULL;
1809 free(pDevice->Contexts);
1810 pDevice->Contexts = NULL;
1812 //Release device structure
1813 memset(pDevice, 0, sizeof(ALCdevice));
1814 free(pDevice);
1816 bReturn = ALC_TRUE;
1818 else
1819 alcSetError(pDevice, ALC_INVALID_DEVICE);
1821 return bReturn;
1825 ALCvoid ReleaseALC(ALCvoid)
1827 free(alcDeviceList); alcDeviceList = NULL;
1828 alcDeviceListSize = 0;
1829 free(alcAllDeviceList); alcAllDeviceList = NULL;
1830 alcAllDeviceListSize = 0;
1831 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
1832 alcCaptureDeviceListSize = 0;
1834 free(alcDefaultDeviceSpecifier);
1835 alcDefaultDeviceSpecifier = NULL;
1836 free(alcDefaultAllDeviceSpecifier);
1837 alcDefaultAllDeviceSpecifier = NULL;
1838 free(alcCaptureDefaultDeviceSpecifier);
1839 alcCaptureDefaultDeviceSpecifier = NULL;
1841 #ifdef _DEBUG
1842 if(g_ulDeviceCount > 0)
1843 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
1844 #endif
1846 while(g_pDeviceList)
1848 if(g_pDeviceList->IsCaptureDevice)
1849 alcCaptureCloseDevice(g_pDeviceList);
1850 else
1851 alcCloseDevice(g_pDeviceList);
1855 ///////////////////////////////////////////////////////