Update panning for existing AL contexts when creating a new one
[openal-soft.git] / Alc / ALc.c
blob7cdf2049fc6d15c5da6f59268fa818c96816998a
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_ALSA
52 { "alsa", alc_alsa_init, alc_alsa_deinit, alc_alsa_probe, EmptyFuncs },
53 #endif
54 #ifdef HAVE_OSS
55 { "oss", alc_oss_init, alc_oss_deinit, alc_oss_probe, EmptyFuncs },
56 #endif
57 #ifdef HAVE_SOLARIS
58 { "solaris", alc_solaris_init, alc_solaris_deinit, alc_solaris_probe, EmptyFuncs },
59 #endif
60 #ifdef HAVE_DSOUND
61 { "dsound", alcDSoundInit, alcDSoundDeinit, alcDSoundProbe, EmptyFuncs },
62 #endif
63 #ifdef HAVE_WINMM
64 { "winmm", alcWinMMInit, alcWinMMDeinit, alcWinMMProbe, EmptyFuncs },
65 #endif
66 #ifdef HAVE_PORTAUDIO
67 { "port", alc_pa_init, alc_pa_deinit, alc_pa_probe, EmptyFuncs },
68 #endif
69 #ifdef HAVE_PULSEAUDIO
70 { "pulse", alc_pulse_init, alc_pulse_deinit, alc_pulse_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 // Mixing Priority Level
212 ALint RTPrioLevel;
214 ///////////////////////////////////////////////////////
217 ///////////////////////////////////////////////////////
218 // ALC Related helper functions
219 #ifdef _WIN32
220 static void alc_init(void);
221 static void alc_deinit(void);
223 BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
225 (void)lpReserved;
227 // Perform actions based on the reason for calling.
228 switch(ul_reason_for_call)
230 case DLL_PROCESS_ATTACH:
231 DisableThreadLibraryCalls(hModule);
232 alc_init();
233 break;
235 case DLL_PROCESS_DETACH:
236 alc_deinit();
237 break;
239 return TRUE;
241 #else
242 #ifdef HAVE_GCC_DESTRUCTOR
243 static void alc_init(void) __attribute__((constructor));
244 static void alc_deinit(void) __attribute__((destructor));
245 #endif
246 #endif
248 static void alc_init(void)
250 int i;
251 const char *devs, *str;
253 InitializeCriticalSection(&g_csMutex);
254 ALTHUNK_INIT();
255 ReadALConfig();
257 tls_create(&LocalContext);
259 RTPrioLevel = GetConfigValueInt(NULL, "rt-prio", 0);
261 devs = GetConfigValue(NULL, "drivers", "");
262 if(devs[0])
264 int n;
265 size_t len;
266 const char *next = devs;
267 int endlist;
269 i = 0;
270 do {
271 devs = next;
272 next = strchr(devs, ',');
274 if(!devs[0] || devs[0] == ',')
276 endlist = 0;
277 continue;
279 endlist = 1;
281 len = (next ? ((size_t)(next-devs)) : strlen(devs));
282 for(n = i;BackendList[n].Init;n++)
284 if(len == strlen(BackendList[n].name) &&
285 strncmp(BackendList[n].name, devs, len) == 0)
287 BackendInfo Bkp = BackendList[n];
288 while(n > i)
290 BackendList[n] = BackendList[n-1];
291 --n;
293 BackendList[n] = Bkp;
295 i++;
296 break;
299 } while(next++);
301 if(endlist)
303 BackendList[i].name = NULL;
304 BackendList[i].Init = NULL;
305 BackendList[i].Deinit = NULL;
306 BackendList[i].Probe = NULL;
310 for(i = 0;BackendList[i].Init;i++)
312 BackendList[i].Init(&BackendList[i].Funcs);
314 BackendList[i].Probe(DEVICE_PROBE);
315 BackendList[i].Probe(ALL_DEVICE_PROBE);
316 BackendList[i].Probe(CAPTURE_DEVICE_PROBE);
319 DuplicateStereo = GetConfigValueBool(NULL, "stereodup", 0);
321 str = GetConfigValue(NULL, "excludefx", "");
322 if(str[0])
324 const struct {
325 const char *name;
326 int type;
327 } EffectList[] = {
328 { "eaxreverb", EAXREVERB },
329 { "reverb", REVERB },
330 { "echo", ECHO },
331 { NULL, 0 }
333 int n;
334 size_t len;
335 const char *next = str;
337 do {
338 str = next;
339 next = strchr(str, ',');
341 if(!str[0] || next == str)
342 continue;
344 len = (next ? ((size_t)(next-str)) : strlen(str));
345 for(n = 0;EffectList[n].name;n++)
347 if(len == strlen(EffectList[n].name) &&
348 strncmp(EffectList[n].name, str, len) == 0)
349 DisabledEffects[EffectList[n].type] = AL_TRUE;
351 } while(next++);
355 static void alc_deinit(void)
357 int i;
359 ReleaseALC();
361 for(i = 0;BackendList[i].Deinit;i++)
362 BackendList[i].Deinit();
364 tls_delete(LocalContext);
366 FreeALConfig();
367 ALTHUNK_EXIT();
368 DeleteCriticalSection(&g_csMutex);
372 static void ProbeDeviceList()
374 ALint i;
376 free(alcDeviceList); alcDeviceList = NULL;
377 alcDeviceListSize = 0;
379 for(i = 0;BackendList[i].Probe;i++)
380 BackendList[i].Probe(DEVICE_PROBE);
383 static void ProbeAllDeviceList()
385 ALint i;
387 free(alcAllDeviceList); alcAllDeviceList = NULL;
388 alcAllDeviceListSize = 0;
390 for(i = 0;BackendList[i].Probe;i++)
391 BackendList[i].Probe(ALL_DEVICE_PROBE);
394 static void ProbeCaptureDeviceList()
396 ALint i;
398 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
399 alcCaptureDeviceListSize = 0;
401 for(i = 0;BackendList[i].Probe;i++)
402 BackendList[i].Probe(CAPTURE_DEVICE_PROBE);
406 #define DECL_APPEND_LIST_FUNC(type) \
407 void Append##type##List(const ALCchar *name) \
409 ALCuint len = strlen(name); \
410 void *temp; \
412 if(len == 0) \
413 return; \
415 temp = realloc(alc##type##List, alc##type##ListSize + len + 2); \
416 if(!temp) \
418 AL_PRINT("Realloc failed to add %s!\n", name); \
419 return; \
421 alc##type##List = temp; \
422 sprintf(alc##type##List+alc##type##ListSize, "%s", name); \
423 alc##type##ListSize += len+1; \
424 alc##type##List[alc##type##ListSize] = 0; \
427 DECL_APPEND_LIST_FUNC(Device)
428 DECL_APPEND_LIST_FUNC(AllDevice)
429 DECL_APPEND_LIST_FUNC(CaptureDevice)
432 void EnableRTPrio(ALint level)
434 ALboolean failed;
436 #ifdef _WIN32
437 if(level > 0)
438 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
439 else
440 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
441 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM)
442 struct sched_param param;
444 if(level > 0)
446 /* Use the minimum real-time priority possible for now (on Linux this
447 * should be 1 for SCHED_RR) */
448 param.sched_priority = sched_get_priority_min(SCHED_RR);
449 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
451 else
453 param.sched_priority = 0;
454 failed = !!pthread_setschedparam(pthread_self(), SCHED_OTHER, &param);
456 #else
457 /* Real-time priority not available */
458 failed = !!level;
459 #endif
460 if(failed)
461 AL_PRINT("Failed to set priority level for thread\n");
466 IsDevice
468 Check pDevice is a valid Device pointer
470 static ALCboolean IsDevice(ALCdevice *pDevice)
472 ALCdevice *pTempDevice;
474 SuspendContext(NULL);
476 pTempDevice = g_pDeviceList;
477 while(pTempDevice && pTempDevice != pDevice)
478 pTempDevice = pTempDevice->next;
480 ProcessContext(NULL);
482 return (pTempDevice ? ALC_TRUE : ALC_FALSE);
486 IsContext
488 Check pContext is a valid Context pointer
490 static ALCboolean IsContext(ALCcontext *pContext)
492 ALCcontext *pTempContext;
494 SuspendContext(NULL);
496 pTempContext = g_pContextList;
497 while (pTempContext && pTempContext != pContext)
498 pTempContext = pTempContext->next;
500 ProcessContext(NULL);
502 return (pTempContext ? ALC_TRUE : ALC_FALSE);
507 alcSetError
509 Store latest ALC Error
511 ALCvoid alcSetError(ALenum errorCode)
513 g_eLastContextError = errorCode;
518 SuspendContext
520 Thread-safe entry
522 ALCvoid SuspendContext(ALCcontext *pContext)
524 (void)pContext;
525 EnterCriticalSection(&g_csMutex);
530 ProcessContext
532 Thread-safe exit
534 ALCvoid ProcessContext(ALCcontext *pContext)
536 (void)pContext;
537 LeaveCriticalSection(&g_csMutex);
542 GetContextSuspended
544 Returns the currently active Context, in a locked state
546 ALCcontext *GetContextSuspended(void)
548 ALCcontext *pContext = NULL;
550 SuspendContext(NULL);
552 pContext = tls_get(LocalContext);
553 if(pContext && !IsContext(pContext))
555 tls_set(LocalContext, NULL);
556 pContext = NULL;
558 if(!pContext)
560 pContext = g_pContextList;
561 while(pContext && !pContext->InUse)
562 pContext = pContext->next;
564 if(pContext)
565 SuspendContext(pContext);
567 ProcessContext(NULL);
569 return pContext;
574 InitContext
576 Initialize Context variables
578 static ALvoid InitContext(ALCcontext *pContext)
580 //Initialise listener
581 pContext->Listener.Gain = 1.0f;
582 pContext->Listener.MetersPerUnit = 1.0f;
583 pContext->Listener.Position[0] = 0.0f;
584 pContext->Listener.Position[1] = 0.0f;
585 pContext->Listener.Position[2] = 0.0f;
586 pContext->Listener.Velocity[0] = 0.0f;
587 pContext->Listener.Velocity[1] = 0.0f;
588 pContext->Listener.Velocity[2] = 0.0f;
589 pContext->Listener.Forward[0] = 0.0f;
590 pContext->Listener.Forward[1] = 0.0f;
591 pContext->Listener.Forward[2] = -1.0f;
592 pContext->Listener.Up[0] = 0.0f;
593 pContext->Listener.Up[1] = 1.0f;
594 pContext->Listener.Up[2] = 0.0f;
596 //Validate pContext
597 pContext->LastError = AL_NO_ERROR;
598 pContext->InUse = AL_FALSE;
599 pContext->Suspended = AL_FALSE;
601 //Set globals
602 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
603 pContext->SourceDistanceModel = AL_FALSE;
604 pContext->DopplerFactor = 1.0f;
605 pContext->DopplerVelocity = 1.0f;
606 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
608 pContext->ExtensionList = "AL_EXTX_buffer_sub_data AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_OFFSET AL_EXTX_sample_buffer_object AL_EXTX_source_distance_model AL_LOKI_quadriphonic";
613 ExitContext
615 Clean up Context, destroy any remaining Sources
617 static ALCvoid ExitContext(ALCcontext *pContext)
619 //Invalidate context
620 pContext->LastError = AL_NO_ERROR;
621 pContext->InUse = AL_FALSE;
624 ///////////////////////////////////////////////////////
627 ///////////////////////////////////////////////////////
628 // ALC Functions calls
631 // This should probably move to another c file but for now ...
632 ALCAPI ALCdevice* ALCAPIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
634 ALCboolean DeviceFound = ALC_FALSE;
635 ALCdevice *pDevice = NULL;
636 ALCint i;
638 if(SampleSize <= 0)
640 alcSetError(ALC_INVALID_VALUE);
641 return NULL;
644 if(deviceName && !deviceName[0])
645 deviceName = NULL;
647 pDevice = malloc(sizeof(ALCdevice));
648 if (pDevice)
650 //Initialise device structure
651 memset(pDevice, 0, sizeof(ALCdevice));
653 //Validate device
654 pDevice->Connected = ALC_TRUE;
655 pDevice->IsCaptureDevice = AL_TRUE;
657 pDevice->szDeviceName = NULL;
659 pDevice->Frequency = frequency;
660 pDevice->Format = format;
661 pDevice->UpdateSize = SampleSize;
662 pDevice->NumUpdates = 1;
664 SuspendContext(NULL);
665 for(i = 0;BackendList[i].Init;i++)
667 pDevice->Funcs = &BackendList[i].Funcs;
668 if(ALCdevice_OpenCapture(pDevice, deviceName))
670 pDevice->next = g_pDeviceList;
671 g_pDeviceList = pDevice;
672 g_ulDeviceCount++;
674 DeviceFound = ALC_TRUE;
675 break;
678 ProcessContext(NULL);
680 if(!DeviceFound)
682 alcSetError(ALC_INVALID_VALUE);
683 free(pDevice);
684 pDevice = NULL;
687 else
688 alcSetError(ALC_OUT_OF_MEMORY);
690 return pDevice;
693 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
695 ALCboolean bReturn = ALC_FALSE;
696 ALCdevice **list;
698 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
700 SuspendContext(NULL);
702 list = &g_pDeviceList;
703 while(*list != pDevice)
704 list = &(*list)->next;
706 *list = (*list)->next;
707 g_ulDeviceCount--;
709 ProcessContext(NULL);
711 ALCdevice_CloseCapture(pDevice);
713 free(pDevice->szDeviceName);
714 pDevice->szDeviceName = NULL;
716 free(pDevice);
718 bReturn = ALC_TRUE;
720 else
721 alcSetError(ALC_INVALID_DEVICE);
723 return bReturn;
726 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
728 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
729 ALCdevice_StartCapture(pDevice);
730 else
731 alcSetError(ALC_INVALID_DEVICE);
734 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
736 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
737 ALCdevice_StopCapture(pDevice);
738 else
739 alcSetError(ALC_INVALID_DEVICE);
742 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
744 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
745 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
746 else
747 alcSetError(ALC_INVALID_DEVICE);
751 alcGetError
753 Return last ALC generated error code
755 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
757 ALCenum errorCode;
759 (void)device;
761 errorCode = g_eLastContextError;
762 g_eLastContextError = ALC_NO_ERROR;
763 return errorCode;
768 alcSuspendContext
770 Not functional
772 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
774 SuspendContext(NULL);
775 if(IsContext(pContext))
776 pContext->Suspended = AL_TRUE;
777 ProcessContext(NULL);
782 alcProcessContext
784 Not functional
786 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
788 SuspendContext(NULL);
789 if(IsContext(pContext))
790 pContext->Suspended = AL_FALSE;
791 ProcessContext(NULL);
796 alcGetString
798 Returns information about the Device, and error strings
800 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
802 const ALCchar *value = NULL;
804 switch (param)
806 case ALC_NO_ERROR:
807 value = alcNoError;
808 break;
810 case ALC_INVALID_ENUM:
811 value = alcErrInvalidEnum;
812 break;
814 case ALC_INVALID_VALUE:
815 value = alcErrInvalidValue;
816 break;
818 case ALC_INVALID_DEVICE:
819 value = alcErrInvalidDevice;
820 break;
822 case ALC_INVALID_CONTEXT:
823 value = alcErrInvalidContext;
824 break;
826 case ALC_OUT_OF_MEMORY:
827 value = alcErrOutOfMemory;
828 break;
830 case ALC_DEVICE_SPECIFIER:
831 if(IsDevice(pDevice))
832 value = pDevice->szDeviceName;
833 else
835 ProbeDeviceList();
836 value = alcDeviceList;
838 break;
840 case ALC_ALL_DEVICES_SPECIFIER:
841 ProbeAllDeviceList();
842 value = alcAllDeviceList;
843 break;
845 case ALC_CAPTURE_DEVICE_SPECIFIER:
846 if(IsDevice(pDevice))
847 value = pDevice->szDeviceName;
848 else
850 ProbeCaptureDeviceList();
851 value = alcCaptureDeviceList;
853 break;
855 /* Default devices are always first in the list */
856 case ALC_DEFAULT_DEVICE_SPECIFIER:
857 free(alcDefaultDeviceSpecifier);
858 alcDefaultDeviceSpecifier = strdup(alcDeviceList ? alcDeviceList : "");
859 if(!alcDefaultDeviceSpecifier)
860 alcSetError(ALC_OUT_OF_MEMORY);
861 value = alcDefaultDeviceSpecifier;
862 break;
864 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
865 free(alcDefaultAllDeviceSpecifier);
866 alcDefaultAllDeviceSpecifier = strdup(alcAllDeviceList ?
867 alcAllDeviceList : "");
868 if(!alcDefaultAllDeviceSpecifier)
869 alcSetError(ALC_OUT_OF_MEMORY);
870 value = alcDefaultAllDeviceSpecifier;
871 break;
873 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
874 free(alcCaptureDefaultDeviceSpecifier);
875 alcCaptureDefaultDeviceSpecifier = strdup(alcCaptureDeviceList ?
876 alcCaptureDeviceList : "");
877 if(!alcCaptureDefaultDeviceSpecifier)
878 alcSetError(ALC_OUT_OF_MEMORY);
879 value = alcCaptureDefaultDeviceSpecifier;
880 break;
882 case ALC_EXTENSIONS:
883 value = alcExtensionList;
884 break;
886 default:
887 alcSetError(ALC_INVALID_ENUM);
888 break;
891 return value;
896 alcGetIntegerv
898 Returns information about the Device and the version of Open AL
900 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
902 if(IsDevice(device) && device->IsCaptureDevice)
904 SuspendContext(NULL);
906 // Capture device
907 switch (param)
909 case ALC_CAPTURE_SAMPLES:
910 if ((size) && (data))
911 *data = ALCdevice_AvailableSamples(device);
912 else
913 alcSetError(ALC_INVALID_VALUE);
914 break;
916 case ALC_CONNECTED:
917 if(size <= 0)
918 alcSetError(ALC_INVALID_VALUE);
919 else
920 *data = device->Connected;
921 break;
923 default:
924 alcSetError(ALC_INVALID_ENUM);
925 break;
928 ProcessContext(NULL);
930 else
932 if(data)
934 // Playback Device
935 switch (param)
937 case ALC_MAJOR_VERSION:
938 if(size <= 0)
939 alcSetError(ALC_INVALID_VALUE);
940 else
941 *data = alcMajorVersion;
942 break;
944 case ALC_MINOR_VERSION:
945 if(size <= 0)
946 alcSetError(ALC_INVALID_VALUE);
947 else
948 *data = alcMinorVersion;
949 break;
951 case ALC_EFX_MAJOR_VERSION:
952 if(size <= 0)
953 alcSetError(ALC_INVALID_VALUE);
954 else
955 *data = alcEFXMajorVersion;
956 break;
958 case ALC_EFX_MINOR_VERSION:
959 if(size <= 0)
960 alcSetError(ALC_INVALID_VALUE);
961 else
962 *data = alcEFXMinorVersion;
963 break;
965 case ALC_MAX_AUXILIARY_SENDS:
966 if(size <= 0)
967 alcSetError(ALC_INVALID_VALUE);
968 else
969 *data = (device?device->NumAuxSends:MAX_SENDS);
970 break;
972 case ALC_ATTRIBUTES_SIZE:
973 if(!IsDevice(device))
974 alcSetError(ALC_INVALID_DEVICE);
975 else if(size <= 0)
976 alcSetError(ALC_INVALID_VALUE);
977 else
978 *data = 13;
979 break;
981 case ALC_ALL_ATTRIBUTES:
982 if(!IsDevice(device))
983 alcSetError(ALC_INVALID_DEVICE);
984 else if (size < 13)
985 alcSetError(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(ALC_INVALID_DEVICE);
1017 else if(size <= 0)
1018 alcSetError(ALC_INVALID_VALUE);
1019 else
1020 *data = device->Frequency;
1021 break;
1023 case ALC_REFRESH:
1024 if(!IsDevice(device))
1025 alcSetError(ALC_INVALID_DEVICE);
1026 else if(size <= 0)
1027 alcSetError(ALC_INVALID_VALUE);
1028 else
1029 *data = device->Frequency / device->UpdateSize;
1030 break;
1032 case ALC_SYNC:
1033 if(!IsDevice(device))
1034 alcSetError(ALC_INVALID_DEVICE);
1035 else if(size <= 0)
1036 alcSetError(ALC_INVALID_VALUE);
1037 else
1038 *data = ALC_FALSE;
1039 break;
1041 case ALC_MONO_SOURCES:
1042 if(!IsDevice(device))
1043 alcSetError(ALC_INVALID_DEVICE);
1044 else if(size <= 0)
1045 alcSetError(ALC_INVALID_VALUE);
1046 else
1047 *data = device->lNumMonoSources;
1048 break;
1050 case ALC_STEREO_SOURCES:
1051 if(!IsDevice(device))
1052 alcSetError(ALC_INVALID_DEVICE);
1053 else if(size <= 0)
1054 alcSetError(ALC_INVALID_VALUE);
1055 else
1056 *data = device->lNumStereoSources;
1057 break;
1059 case ALC_CONNECTED:
1060 if(!IsDevice(device))
1061 alcSetError(ALC_INVALID_DEVICE);
1062 else if(size <= 0)
1063 alcSetError(ALC_INVALID_VALUE);
1064 else
1065 *data = device->Connected;
1066 break;
1068 default:
1069 alcSetError(ALC_INVALID_ENUM);
1070 break;
1073 else if(size)
1074 alcSetError(ALC_INVALID_VALUE);
1077 return;
1082 alcIsExtensionPresent
1084 Determines if there is support for a particular extension
1086 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
1088 ALCboolean bResult = ALC_FALSE;
1090 (void)device;
1092 if (extName)
1094 const char *ptr;
1095 size_t len;
1097 len = strlen(extName);
1098 ptr = alcExtensionList;
1099 while(ptr && *ptr)
1101 if(strncasecmp(ptr, extName, len) == 0 &&
1102 (ptr[len] == '\0' || isspace(ptr[len])))
1104 bResult = ALC_TRUE;
1105 break;
1107 if((ptr=strchr(ptr, ' ')) != NULL)
1109 do {
1110 ++ptr;
1111 } while(isspace(*ptr));
1115 else
1116 alcSetError(ALC_INVALID_VALUE);
1118 return bResult;
1123 alcGetProcAddress
1125 Retrieves the function address for a particular extension function
1127 ALCAPI ALCvoid * ALCAPIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
1129 ALCvoid *pFunction = NULL;
1130 ALsizei i = 0;
1132 (void)device;
1134 if (funcName)
1136 while(alcFunctions[i].funcName &&
1137 strcmp(alcFunctions[i].funcName,funcName) != 0)
1138 i++;
1139 pFunction = alcFunctions[i].address;
1141 else
1142 alcSetError(ALC_INVALID_VALUE);
1144 return pFunction;
1149 alcGetEnumValue
1151 Get the value for a particular ALC Enumerated Value
1153 ALCAPI ALCenum ALCAPIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
1155 ALsizei i = 0;
1156 ALCenum val;
1158 (void)device;
1160 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
1161 i++;
1162 val = enumeration[i].value;
1164 if(!enumeration[i].enumName)
1165 alcSetError(ALC_INVALID_VALUE);
1167 return val;
1172 alcCreateContext
1174 Create and attach a Context to a particular Device.
1176 ALCAPI ALCcontext* ALCAPIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
1178 ALuint attrIdx, reqStereoSources;
1179 ALCcontext *ALContext;
1180 void *temp;
1181 ALuint i;
1183 SuspendContext(NULL);
1185 if(!IsDevice(device) || device->IsCaptureDevice || !device->Connected)
1187 alcSetError(ALC_INVALID_DEVICE);
1188 ProcessContext(NULL);
1189 return NULL;
1192 // Reset Context Last Error code
1193 g_eLastContextError = ALC_NO_ERROR;
1195 // If a context is already running on the device, stop playback so the
1196 // device attributes can be updated
1197 if(device->NumContexts > 0)
1199 ProcessContext(NULL);
1200 ALCdevice_StopPlayback(device);
1201 SuspendContext(NULL);
1204 // Check for attributes
1205 if(attrList)
1207 ALCint level = device->Bs2bLevel;
1208 ALCuint freq = device->Frequency;
1209 ALCint numMono = device->lNumMonoSources;
1210 ALCint numStereo = device->lNumStereoSources;
1211 ALCuint numSends = device->NumAuxSends;
1213 attrIdx = 0;
1214 while(attrList[attrIdx])
1216 if(attrList[attrIdx] == ALC_FREQUENCY)
1218 freq = attrList[attrIdx + 1];
1219 if(freq == 0)
1220 freq = device->Frequency;
1223 if(attrList[attrIdx] == ALC_STEREO_SOURCES)
1225 reqStereoSources = attrList[attrIdx + 1];
1226 if(reqStereoSources > device->MaxNoOfSources)
1227 reqStereoSources = device->MaxNoOfSources;
1229 numStereo = reqStereoSources;
1230 numMono = device->MaxNoOfSources - numStereo;
1233 if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS)
1235 numSends = attrList[attrIdx + 1];
1236 if(numSends > MAX_SENDS)
1237 numSends = MAX_SENDS;
1240 attrIdx += 2;
1243 device->Bs2bLevel = GetConfigValueInt(NULL, "cf_level", level);
1244 device->Frequency = GetConfigValueInt(NULL, "frequency", freq);
1245 device->lNumMonoSources = numMono;
1246 device->lNumStereoSources = numStereo;
1247 device->NumAuxSends = GetConfigValueInt(NULL, "sends", numSends);
1250 if(ALCdevice_ResetPlayback(device) == ALC_FALSE)
1252 alcSetError(ALC_INVALID_DEVICE);
1253 aluHandleDisconnect(device);
1254 ProcessContext(NULL);
1255 return NULL;
1258 for(i = 0;i < device->NumContexts;i++)
1260 ALCcontext *context = device->Contexts[i];
1261 ALeffectslot *slot;
1262 ALsource *source;
1264 SuspendContext(context);
1265 for(slot = context->AuxiliaryEffectSlot;slot != NULL;slot = slot->next)
1267 if(!slot->EffectState)
1268 continue;
1270 if(ALEffect_DeviceUpdate(slot->EffectState, device) == AL_FALSE)
1272 alcSetError(ALC_INVALID_DEVICE);
1273 aluHandleDisconnect(device);
1274 ProcessContext(context);
1275 ProcessContext(NULL);
1276 ALCdevice_StopPlayback(device);
1277 return NULL;
1279 ALEffect_Update(slot->EffectState, context, &slot->effect);
1282 for(source = context->Source;source != NULL;source = source->next)
1284 ALuint s = device->NumAuxSends;
1285 while(s < MAX_SENDS)
1287 if(source->Send[s].Slot)
1288 source->Send[s].Slot->refcount--;
1289 source->Send[s].Slot = NULL;
1290 source->Send[s].WetFilter.type = 0;
1291 source->Send[s].WetFilter.filter = 0;
1292 s++;
1294 source->NeedsUpdate = AL_TRUE;
1296 aluInitPanning(context);
1297 ProcessContext(context);
1300 if(device->Bs2bLevel > 0 && device->Bs2bLevel <= 6)
1302 if(!device->Bs2b)
1304 device->Bs2b = calloc(1, sizeof(*device->Bs2b));
1305 bs2b_clear(device->Bs2b);
1307 bs2b_set_srate(device->Bs2b, device->Frequency);
1308 bs2b_set_level(device->Bs2b, device->Bs2bLevel);
1310 else
1312 free(device->Bs2b);
1313 device->Bs2b = NULL;
1316 temp = realloc(device->Contexts, (device->NumContexts+1) * sizeof(*device->Contexts));
1317 if(!temp)
1319 alcSetError(ALC_OUT_OF_MEMORY);
1320 ProcessContext(NULL);
1321 return NULL;
1323 device->Contexts = temp;
1325 ALContext = calloc(1, sizeof(ALCcontext));
1326 if(!ALContext)
1328 alcSetError(ALC_OUT_OF_MEMORY);
1329 ProcessContext(NULL);
1330 return NULL;
1333 device->Contexts[device->NumContexts++] = ALContext;
1334 ALContext->Device = device;
1336 InitContext(ALContext);
1337 aluInitPanning(ALContext);
1339 ALContext->next = g_pContextList;
1340 g_pContextList = ALContext;
1341 g_ulContextCount++;
1343 ProcessContext(NULL);
1345 return ALContext;
1350 alcDestroyContext
1352 Remove a Context
1354 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
1356 ALCcontext **list;
1357 ALuint i;
1359 if (IsContext(context))
1361 ALCdevice *Device = context->Device;
1363 if(Device->NumContexts == 1)
1364 ALCdevice_StopPlayback(Device);
1366 SuspendContext(NULL);
1368 for(i = 0;i < Device->NumContexts-1;i++)
1370 if(Device->Contexts[i] == context)
1372 memmove(&Device->Contexts[i], &Device->Contexts[i+1],
1373 (Device->NumContexts-i-1) * sizeof(*Device->Contexts));
1374 break;
1377 Device->NumContexts--;
1379 // Lock context
1380 SuspendContext(context);
1382 if(context->SourceCount > 0)
1384 #ifdef _DEBUG
1385 AL_PRINT("alcDestroyContext(): deleting %d Source(s)\n", context->SourceCount);
1386 #endif
1387 ReleaseALSources(context);
1389 if(context->AuxiliaryEffectSlotCount > 0)
1391 #ifdef _DEBUG
1392 AL_PRINT("alcDestroyContext(): deleting %d AuxiliaryEffectSlot(s)\n", context->AuxiliaryEffectSlotCount);
1393 #endif
1394 ReleaseALAuxiliaryEffectSlots(context);
1397 list = &g_pContextList;
1398 while(*list != context)
1399 list = &(*list)->next;
1401 *list = (*list)->next;
1402 g_ulContextCount--;
1404 // Unlock context
1405 ProcessContext(context);
1406 ProcessContext(NULL);
1408 ExitContext(context);
1410 // Free memory (MUST do this after ProcessContext)
1411 memset(context, 0, sizeof(ALCcontext));
1412 free(context);
1414 else
1415 alcSetError(ALC_INVALID_CONTEXT);
1420 alcGetCurrentContext
1422 Returns the currently active Context
1424 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1426 ALCcontext *pContext;
1428 if((pContext=GetContextSuspended()) != NULL)
1429 ProcessContext(pContext);
1431 return pContext;
1435 alcGetThreadContext
1437 Returns the currently active thread-local Context
1439 ALCcontext * ALCAPIENTRY alcGetThreadContext(void)
1441 ALCcontext *pContext = NULL;
1443 SuspendContext(NULL);
1445 pContext = tls_get(LocalContext);
1446 if(pContext && !IsContext(pContext))
1448 tls_set(LocalContext, NULL);
1449 pContext = NULL;
1452 ProcessContext(NULL);
1454 return pContext;
1459 alcGetContextsDevice
1461 Returns the Device that a particular Context is attached to
1463 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1465 ALCdevice *pDevice = NULL;
1467 SuspendContext(NULL);
1468 if (IsContext(pContext))
1469 pDevice = pContext->Device;
1470 else
1471 alcSetError(ALC_INVALID_CONTEXT);
1472 ProcessContext(NULL);
1474 return pDevice;
1479 alcMakeContextCurrent
1481 Makes the given Context the active Context
1483 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1485 ALCcontext *ALContext;
1486 ALboolean bReturn = AL_TRUE;
1488 SuspendContext(NULL);
1490 // context must be a valid Context or NULL
1491 if(context == NULL || IsContext(context))
1493 if((ALContext=GetContextSuspended()) != NULL)
1495 ALContext->InUse=AL_FALSE;
1496 ProcessContext(ALContext);
1499 if((ALContext=context) != NULL && ALContext->Device)
1501 SuspendContext(ALContext);
1502 ALContext->InUse=AL_TRUE;
1503 ProcessContext(ALContext);
1506 tls_set(LocalContext, NULL);
1508 else
1510 alcSetError(ALC_INVALID_CONTEXT);
1511 bReturn = AL_FALSE;
1514 ProcessContext(NULL);
1516 return bReturn;
1520 alcMakeCurrent
1522 Makes the given Context the active Context for the current thread
1524 ALCboolean ALCAPIENTRY alcMakeCurrent(ALCcontext *context)
1526 ALboolean bReturn = AL_TRUE;
1528 SuspendContext(NULL);
1530 // context must be a valid Context or NULL
1531 if(context == NULL || IsContext(context))
1532 tls_set(LocalContext, context);
1533 else
1535 alcSetError(ALC_INVALID_CONTEXT);
1536 bReturn = AL_FALSE;
1539 ProcessContext(NULL);
1541 return bReturn;
1545 // Sets the default channel order used by most non-WaveFormatEx-based APIs
1546 void SetDefaultChannelOrder(ALCdevice *device)
1548 switch(aluChannelsFromFormat(device->Format))
1550 case 1: device->DevChannels[0] = FRONT_CENTER; break;
1552 case 2: device->DevChannels[0] = FRONT_LEFT;
1553 device->DevChannels[1] = FRONT_RIGHT; break;
1555 case 4: device->DevChannels[0] = FRONT_LEFT;
1556 device->DevChannels[1] = FRONT_RIGHT;
1557 device->DevChannels[2] = BACK_LEFT;
1558 device->DevChannels[3] = BACK_RIGHT; break;
1560 case 6: device->DevChannels[0] = FRONT_LEFT;
1561 device->DevChannels[1] = FRONT_RIGHT;
1562 device->DevChannels[2] = BACK_LEFT;
1563 device->DevChannels[3] = BACK_RIGHT;
1564 device->DevChannels[4] = FRONT_CENTER;
1565 device->DevChannels[5] = LFE; break;
1567 case 7: device->DevChannels[0] = FRONT_LEFT;
1568 device->DevChannels[1] = FRONT_RIGHT;
1569 device->DevChannels[2] = FRONT_CENTER;
1570 device->DevChannels[3] = LFE;
1571 device->DevChannels[4] = BACK_CENTER;
1572 device->DevChannels[5] = SIDE_LEFT;
1573 device->DevChannels[6] = SIDE_RIGHT; break;
1575 case 8: device->DevChannels[0] = FRONT_LEFT;
1576 device->DevChannels[1] = FRONT_RIGHT;
1577 device->DevChannels[2] = BACK_LEFT;
1578 device->DevChannels[3] = BACK_RIGHT;
1579 device->DevChannels[4] = FRONT_CENTER;
1580 device->DevChannels[5] = LFE;
1581 device->DevChannels[6] = SIDE_LEFT;
1582 device->DevChannels[7] = SIDE_RIGHT; break;
1585 // Sets the default order used by WaveFormatEx
1586 void SetDefaultWFXChannelOrder(ALCdevice *device)
1588 switch(aluChannelsFromFormat(device->Format))
1590 case 1: device->DevChannels[0] = FRONT_CENTER; break;
1592 case 2: device->DevChannels[0] = FRONT_LEFT;
1593 device->DevChannels[1] = FRONT_RIGHT; break;
1595 case 4: device->DevChannels[0] = FRONT_LEFT;
1596 device->DevChannels[1] = FRONT_RIGHT;
1597 device->DevChannels[2] = BACK_LEFT;
1598 device->DevChannels[3] = BACK_RIGHT; break;
1600 case 6: device->DevChannels[0] = FRONT_LEFT;
1601 device->DevChannels[1] = FRONT_RIGHT;
1602 device->DevChannels[2] = FRONT_CENTER;
1603 device->DevChannels[3] = LFE;
1604 device->DevChannels[4] = BACK_LEFT;
1605 device->DevChannels[5] = BACK_RIGHT; break;
1607 case 7: device->DevChannels[0] = FRONT_LEFT;
1608 device->DevChannels[1] = FRONT_RIGHT;
1609 device->DevChannels[2] = FRONT_CENTER;
1610 device->DevChannels[3] = LFE;
1611 device->DevChannels[4] = BACK_CENTER;
1612 device->DevChannels[5] = SIDE_LEFT;
1613 device->DevChannels[6] = SIDE_RIGHT; break;
1615 case 8: device->DevChannels[0] = FRONT_LEFT;
1616 device->DevChannels[1] = FRONT_RIGHT;
1617 device->DevChannels[2] = FRONT_CENTER;
1618 device->DevChannels[3] = LFE;
1619 device->DevChannels[4] = BACK_LEFT;
1620 device->DevChannels[5] = BACK_RIGHT;
1621 device->DevChannels[6] = SIDE_LEFT;
1622 device->DevChannels[7] = SIDE_RIGHT; break;
1626 static ALenum GetFormatFromString(const char *str)
1628 if(strcasecmp(str, "AL_FORMAT_MONO32") == 0) return AL_FORMAT_MONO_FLOAT32;
1629 if(strcasecmp(str, "AL_FORMAT_STEREO32") == 0) return AL_FORMAT_STEREO_FLOAT32;
1630 if(strcasecmp(str, "AL_FORMAT_QUAD32") == 0) return AL_FORMAT_QUAD32;
1631 if(strcasecmp(str, "AL_FORMAT_51CHN32") == 0) return AL_FORMAT_51CHN32;
1632 if(strcasecmp(str, "AL_FORMAT_61CHN32") == 0) return AL_FORMAT_61CHN32;
1633 if(strcasecmp(str, "AL_FORMAT_71CHN32") == 0) return AL_FORMAT_71CHN32;
1635 if(strcasecmp(str, "AL_FORMAT_MONO16") == 0) return AL_FORMAT_MONO16;
1636 if(strcasecmp(str, "AL_FORMAT_STEREO16") == 0) return AL_FORMAT_STEREO16;
1637 if(strcasecmp(str, "AL_FORMAT_QUAD16") == 0) return AL_FORMAT_QUAD16;
1638 if(strcasecmp(str, "AL_FORMAT_51CHN16") == 0) return AL_FORMAT_51CHN16;
1639 if(strcasecmp(str, "AL_FORMAT_61CHN16") == 0) return AL_FORMAT_61CHN16;
1640 if(strcasecmp(str, "AL_FORMAT_71CHN16") == 0) return AL_FORMAT_71CHN16;
1642 if(strcasecmp(str, "AL_FORMAT_MONO8") == 0) return AL_FORMAT_MONO8;
1643 if(strcasecmp(str, "AL_FORMAT_STEREO8") == 0) return AL_FORMAT_STEREO8;
1644 if(strcasecmp(str, "AL_FORMAT_QUAD8") == 0) return AL_FORMAT_QUAD8;
1645 if(strcasecmp(str, "AL_FORMAT_51CHN8") == 0) return AL_FORMAT_51CHN8;
1646 if(strcasecmp(str, "AL_FORMAT_61CHN8") == 0) return AL_FORMAT_61CHN8;
1647 if(strcasecmp(str, "AL_FORMAT_71CHN8") == 0) return AL_FORMAT_71CHN8;
1649 AL_PRINT("Unknown format: \"%s\"\n", str);
1650 return AL_FORMAT_STEREO16;
1654 alcOpenDevice
1656 Open the Device specified.
1658 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1660 ALboolean bDeviceFound = AL_FALSE;
1661 ALCdevice *device;
1662 ALint i;
1664 if(deviceName && !deviceName[0])
1665 deviceName = NULL;
1667 device = malloc(sizeof(ALCdevice));
1668 if (device)
1670 const char *fmt;
1672 //Initialise device structure
1673 memset(device, 0, sizeof(ALCdevice));
1675 //Validate device
1676 device->Connected = ALC_TRUE;
1677 device->IsCaptureDevice = AL_FALSE;
1679 device->Bs2b = NULL;
1680 device->szDeviceName = NULL;
1682 device->Contexts = NULL;
1683 device->NumContexts = 0;
1685 //Set output format
1686 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1687 if(device->Frequency == 0)
1688 device->Frequency = SWMIXER_OUTPUT_RATE;
1690 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1691 device->Format = GetFormatFromString(fmt);
1693 device->NumUpdates = GetConfigValueInt(NULL, "periods", 4);
1694 if(device->NumUpdates < 2)
1695 device->NumUpdates = 4;
1697 i = GetConfigValueInt(NULL, "refresh", 4096);
1698 if(i <= 0) i = 4096;
1700 device->UpdateSize = GetConfigValueInt(NULL, "period_size", i/device->NumUpdates);
1701 if(device->UpdateSize <= 0)
1702 device->UpdateSize = i/device->NumUpdates;
1704 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1705 if((ALint)device->MaxNoOfSources <= 0)
1706 device->MaxNoOfSources = 256;
1708 device->AuxiliaryEffectSlotMax = GetConfigValueInt(NULL, "slots", 4);
1709 if((ALint)device->AuxiliaryEffectSlotMax <= 0)
1710 device->AuxiliaryEffectSlotMax = 4;
1712 device->lNumStereoSources = 1;
1713 device->lNumMonoSources = device->MaxNoOfSources - device->lNumStereoSources;
1715 device->NumAuxSends = GetConfigValueInt(NULL, "sends", MAX_SENDS);
1716 if(device->NumAuxSends > MAX_SENDS)
1717 device->NumAuxSends = MAX_SENDS;
1719 device->Bs2bLevel = GetConfigValueInt(NULL, "cf_level", 0);
1721 if(aluChannelsFromFormat(device->Format) <= 2)
1723 device->HeadDampen = GetConfigValueFloat(NULL, "head_dampen", DEFAULT_HEAD_DAMPEN);
1724 device->HeadDampen = __min(device->HeadDampen, 1.0f);
1725 device->HeadDampen = __max(device->HeadDampen, 0.0f);
1727 else
1728 device->HeadDampen = 0.0f;
1730 // Find a playback device to open
1731 SuspendContext(NULL);
1732 for(i = 0;BackendList[i].Init;i++)
1734 device->Funcs = &BackendList[i].Funcs;
1735 if(ALCdevice_OpenPlayback(device, deviceName))
1737 device->next = g_pDeviceList;
1738 g_pDeviceList = device;
1739 g_ulDeviceCount++;
1741 bDeviceFound = AL_TRUE;
1742 break;
1745 ProcessContext(NULL);
1747 if (!bDeviceFound)
1749 // No suitable output device found
1750 alcSetError(ALC_INVALID_VALUE);
1751 free(device);
1752 device = NULL;
1755 else
1756 alcSetError(ALC_OUT_OF_MEMORY);
1758 return device;
1763 alcCloseDevice
1765 Close the specified Device
1767 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1769 ALCboolean bReturn = ALC_FALSE;
1770 ALCdevice **list;
1772 if(IsDevice(pDevice) && !pDevice->IsCaptureDevice)
1774 SuspendContext(NULL);
1776 list = &g_pDeviceList;
1777 while(*list != pDevice)
1778 list = &(*list)->next;
1780 *list = (*list)->next;
1781 g_ulDeviceCount--;
1783 ProcessContext(NULL);
1785 if(pDevice->NumContexts > 0)
1787 #ifdef _DEBUG
1788 AL_PRINT("alcCloseDevice(): destroying %u Context(s)\n", pDevice->NumContexts);
1789 #endif
1790 while(pDevice->NumContexts > 0)
1791 alcDestroyContext(pDevice->Contexts[0]);
1793 ALCdevice_ClosePlayback(pDevice);
1795 if(pDevice->BufferCount > 0)
1797 #ifdef _DEBUG
1798 AL_PRINT("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice->BufferCount);
1799 #endif
1800 ReleaseALBuffers(pDevice);
1802 if(pDevice->EffectCount > 0)
1804 #ifdef _DEBUG
1805 AL_PRINT("alcCloseDevice(): deleting %d Effect(s)\n", pDevice->EffectCount);
1806 #endif
1807 ReleaseALEffects(pDevice);
1809 if(pDevice->FilterCount > 0)
1811 #ifdef _DEBUG
1812 AL_PRINT("alcCloseDevice(): deleting %d Filter(s)\n", pDevice->FilterCount);
1813 #endif
1814 ReleaseALFilters(pDevice);
1816 if(pDevice->DatabufferCount > 0)
1818 #ifdef _DEBUG
1819 AL_PRINT("alcCloseDevice(): deleting %d Databuffer(s)\n", pDevice->DatabufferCount);
1820 #endif
1821 ReleaseALDatabuffers(pDevice);
1824 free(pDevice->Bs2b);
1825 pDevice->Bs2b = NULL;
1827 free(pDevice->szDeviceName);
1828 pDevice->szDeviceName = NULL;
1830 free(pDevice->Contexts);
1831 pDevice->Contexts = NULL;
1833 //Release device structure
1834 memset(pDevice, 0, sizeof(ALCdevice));
1835 free(pDevice);
1837 bReturn = ALC_TRUE;
1839 else
1840 alcSetError(ALC_INVALID_DEVICE);
1842 return bReturn;
1846 ALCvoid ReleaseALC(ALCvoid)
1848 free(alcDeviceList); alcDeviceList = NULL;
1849 alcDeviceListSize = 0;
1850 free(alcAllDeviceList); alcAllDeviceList = NULL;
1851 alcAllDeviceListSize = 0;
1852 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
1853 alcCaptureDeviceListSize = 0;
1855 free(alcDefaultDeviceSpecifier);
1856 alcDefaultDeviceSpecifier = NULL;
1857 free(alcDefaultAllDeviceSpecifier);
1858 alcDefaultAllDeviceSpecifier = NULL;
1859 free(alcCaptureDefaultDeviceSpecifier);
1860 alcCaptureDefaultDeviceSpecifier = NULL;
1862 #ifdef _DEBUG
1863 if(g_ulDeviceCount > 0)
1864 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
1865 #endif
1867 while(g_pDeviceList)
1869 if(g_pDeviceList->IsCaptureDevice)
1870 alcCaptureCloseDevice(g_pDeviceList);
1871 else
1872 alcCloseDevice(g_pDeviceList);
1876 ///////////////////////////////////////////////////////