Add an option to disable specific EFX effect types
[openal-soft/openal-hmr.git] / Alc / ALc.c
blob3913fcae61c076ab0acdfffdf5d9e972c4de3819
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 #define _CRT_SECURE_NO_DEPRECATE // get rid of sprintf security warnings on VS2005
23 #include "config.h"
25 #include <math.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <memory.h>
29 #include <ctype.h>
30 #include "alMain.h"
31 #include "alSource.h"
32 #include "AL/al.h"
33 #include "AL/alc.h"
34 #include "alThunk.h"
35 #include "alSource.h"
36 #include "alBuffer.h"
37 #include "alExtension.h"
38 #include "alAuxEffectSlot.h"
39 #include "bs2b.h"
41 ///////////////////////////////////////////////////////
42 // DEBUG INFORMATION
44 char _alDebug[256];
46 ///////////////////////////////////////////////////////
49 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
50 static struct {
51 const char *name;
52 void (*Init)(BackendFuncs*);
53 BackendFuncs Funcs;
54 } BackendList[] = {
55 #ifdef HAVE_ALSA
56 { "alsa", alc_alsa_init, EmptyFuncs },
57 #endif
58 #ifdef HAVE_OSS
59 { "oss", alc_oss_init, EmptyFuncs },
60 #endif
61 #ifdef HAVE_SOLARIS
62 { "solaris", alc_solaris_init, EmptyFuncs },
63 #endif
64 #ifdef HAVE_DSOUND
65 { "dsound", alcDSoundInit, EmptyFuncs },
66 #endif
67 #ifdef HAVE_WINMM
68 { "winmm", alcWinMMInit, EmptyFuncs },
69 #endif
71 { "wave", alc_wave_init, EmptyFuncs },
73 { NULL, NULL, EmptyFuncs }
75 #undef EmptyFuncs
77 ///////////////////////////////////////////////////////
79 #define ALC_EFX_MAJOR_VERSION 0x20001
80 #define ALC_EFX_MINOR_VERSION 0x20002
81 #define ALC_MAX_AUXILIARY_SENDS 0x20003
83 ///////////////////////////////////////////////////////
84 // STRING and EXTENSIONS
86 typedef struct ALCfunction_struct
88 ALCchar *funcName;
89 ALvoid *address;
90 } ALCfunction;
92 static ALCfunction alcFunctions[] = {
93 { "alcCreateContext", (ALvoid *) alcCreateContext },
94 { "alcMakeContextCurrent", (ALvoid *) alcMakeContextCurrent },
95 { "alcProcessContext", (ALvoid *) alcProcessContext },
96 { "alcSuspendContext", (ALvoid *) alcSuspendContext },
97 { "alcDestroyContext", (ALvoid *) alcDestroyContext },
98 { "alcGetCurrentContext", (ALvoid *) alcGetCurrentContext },
99 { "alcGetContextsDevice", (ALvoid *) alcGetContextsDevice },
100 { "alcOpenDevice", (ALvoid *) alcOpenDevice },
101 { "alcCloseDevice", (ALvoid *) alcCloseDevice },
102 { "alcGetError", (ALvoid *) alcGetError },
103 { "alcIsExtensionPresent", (ALvoid *) alcIsExtensionPresent },
104 { "alcGetProcAddress", (ALvoid *) alcGetProcAddress },
105 { "alcGetEnumValue", (ALvoid *) alcGetEnumValue },
106 { "alcGetString", (ALvoid *) alcGetString },
107 { "alcGetIntegerv", (ALvoid *) alcGetIntegerv },
108 { "alcCaptureOpenDevice", (ALvoid *) alcCaptureOpenDevice },
109 { "alcCaptureCloseDevice", (ALvoid *) alcCaptureCloseDevice },
110 { "alcCaptureStart", (ALvoid *) alcCaptureStart },
111 { "alcCaptureStop", (ALvoid *) alcCaptureStop },
112 { "alcCaptureSamples", (ALvoid *) alcCaptureSamples },
113 { NULL, (ALvoid *) NULL }
116 static ALenums enumeration[]={
117 // Types
118 { (ALchar *)"ALC_INVALID", ALC_INVALID },
119 { (ALchar *)"ALC_FALSE", ALC_FALSE },
120 { (ALchar *)"ALC_TRUE", ALC_TRUE },
122 // ALC Properties
123 { (ALchar *)"ALC_MAJOR_VERSION", ALC_MAJOR_VERSION },
124 { (ALchar *)"ALC_MINOR_VERSION", ALC_MINOR_VERSION },
125 { (ALchar *)"ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE },
126 { (ALchar *)"ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES },
127 { (ALchar *)"ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER },
128 { (ALchar *)"ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER },
129 { (ALchar *)"ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER },
130 { (ALchar *)"ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER },
131 { (ALchar *)"ALC_EXTENSIONS", ALC_EXTENSIONS },
132 { (ALchar *)"ALC_FREQUENCY", ALC_FREQUENCY },
133 { (ALchar *)"ALC_REFRESH", ALC_REFRESH },
134 { (ALchar *)"ALC_SYNC", ALC_SYNC },
135 { (ALchar *)"ALC_MONO_SOURCES", ALC_MONO_SOURCES },
136 { (ALchar *)"ALC_STEREO_SOURCES", ALC_STEREO_SOURCES },
137 { (ALchar *)"ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER },
138 { (ALchar *)"ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER},
139 { (ALchar *)"ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES },
141 // EFX Properties
142 { (ALchar *)"ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION },
143 { (ALchar *)"ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION },
144 { (ALchar *)"ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS },
146 // ALC Error Message
147 { (ALchar *)"ALC_NO_ERROR", ALC_NO_ERROR },
148 { (ALchar *)"ALC_INVALID_DEVICE", ALC_INVALID_DEVICE },
149 { (ALchar *)"ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT },
150 { (ALchar *)"ALC_INVALID_ENUM", ALC_INVALID_ENUM },
151 { (ALchar *)"ALC_INVALID_VALUE", ALC_INVALID_VALUE },
152 { (ALchar *)"ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY },
153 { (ALchar *)NULL, (ALenum)0 }
155 // Error strings
156 static const ALCchar alcNoError[] = "No Error";
157 static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
158 static const ALCchar alcErrInvalidContext[] = "Invalid Context";
159 static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
160 static const ALCchar alcErrInvalidValue[] = "Invalid Value";
161 static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
163 // Context strings
164 static ALCchar alcDeviceList[2048];
165 static ALCchar alcAllDeviceList[2048];
166 static ALCchar alcCaptureDeviceList[2048];
167 // Default is always the first in the list
168 static ALCchar *alcDefaultDeviceSpecifier = alcDeviceList;
169 static ALCchar *alcDefaultAllDeviceSpecifier = alcAllDeviceList;
170 static ALCchar *alcCaptureDefaultDeviceSpecifier = alcCaptureDeviceList;
173 static ALCchar alcExtensionList[] = "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_EFX";
174 static ALCint alcMajorVersion = 1;
175 static ALCint alcMinorVersion = 1;
177 static ALCint alcEFXMajorVersion = 1;
178 static ALCint alcEFXMinorVersion = 0;
180 ///////////////////////////////////////////////////////
183 ///////////////////////////////////////////////////////
184 // Global Variables
186 static ALCdevice *g_pDeviceList = NULL;
187 static ALCuint g_ulDeviceCount = 0;
189 static CRITICAL_SECTION g_csMutex;
191 // Context List
192 static ALCcontext *g_pContextList = NULL;
193 static ALCuint g_ulContextCount = 0;
195 // Context Error
196 static ALCenum g_eLastContextError = ALC_NO_ERROR;
198 static ALboolean init_done = AL_FALSE;
200 ///////////////////////////////////////////////////////
203 ///////////////////////////////////////////////////////
204 // ALC Related helper functions
205 #ifdef _WIN32
206 BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
208 (void)lpReserved;
210 // Perform actions based on the reason for calling.
211 switch(ul_reason_for_call)
213 case DLL_PROCESS_ATTACH:
214 DisableThreadLibraryCalls(hModule);
215 break;
217 case DLL_PROCESS_DETACH:
218 if(!init_done)
219 break;
220 ReleaseALC();
221 ReleaseALBuffers();
222 ReleaseALEffects();
223 ReleaseALFilters();
224 FreeALConfig();
225 ALTHUNK_EXIT();
226 DeleteCriticalSection(&g_csMutex);
227 break;
229 return TRUE;
231 #else
232 #ifdef HAVE_GCC_DESTRUCTOR
233 static void my_deinit() __attribute__((destructor));
234 static void my_deinit()
236 static ALenum once = AL_FALSE;
237 if(once || !init_done) return;
238 once = AL_TRUE;
240 ReleaseALC();
241 ReleaseALBuffers();
242 ReleaseALEffects();
243 ReleaseALFilters();
244 FreeALConfig();
245 ALTHUNK_EXIT();
246 DeleteCriticalSection(&g_csMutex);
248 #endif
249 #endif
251 static void InitAL(void)
253 if(!init_done)
255 int i;
256 const char *devs, *str;
258 init_done = AL_TRUE;
260 InitializeCriticalSection(&g_csMutex);
261 ALTHUNK_INIT();
262 ReadALConfig();
264 devs = GetConfigValue(NULL, "drivers", "");
265 if(devs[0])
267 int n;
268 size_t len;
269 const char *next = devs;
271 i = 0;
273 do {
274 devs = next;
275 next = strchr(devs, ',');
277 if(!devs[0] || devs[0] == ',')
278 continue;
280 len = (next ? ((size_t)(next-devs)) : strlen(devs));
281 for(n = i;BackendList[n].Init;n++)
283 if(len == strlen(BackendList[n].name) &&
284 strncmp(BackendList[n].name, devs, len) == 0)
286 const char *name = BackendList[i].name;
287 void (*Init)(BackendFuncs*) = BackendList[i].Init;
289 BackendList[i].name = BackendList[n].name;
290 BackendList[i].Init = BackendList[n].Init;
292 BackendList[n].name = name;
293 BackendList[n].Init = Init;
295 i++;
298 } while(next++);
300 BackendList[i].name = NULL;
301 BackendList[i].Init = NULL;
304 for(i = 0;BackendList[i].Init;i++)
305 BackendList[i].Init(&BackendList[i].Funcs);
307 str = GetConfigValue(NULL, "stereodup", "false");
308 DuplicateStereo = (strcasecmp(str, "true") == 0 ||
309 strcasecmp(str, "yes") == 0 ||
310 strcasecmp(str, "on") == 0 ||
311 atoi(str) != 0);
313 str = GetConfigValue(NULL, "excludefx", "");
314 if(str[0])
316 const struct {
317 const char *name;
318 int type;
319 } EffectList[] = {
320 { "reverb", REVERB },
321 { NULL, 0 }
323 int n;
324 size_t len;
325 const char *next = str;
327 do {
328 str = next;
329 next = strchr(str, ',');
331 if(!str[0] || next == str)
332 continue;
334 len = (next ? ((size_t)(next-str)) : strlen(str));
335 for(n = 0;EffectList[n].name;n++)
337 if(len == strlen(EffectList[n].name) &&
338 strncmp(EffectList[n].name, str, len) == 0)
339 DisabledEffects[EffectList[n].type] = AL_TRUE;
341 } while(next++);
346 ALCchar *AppendDeviceList(char *name)
348 static size_t pos;
349 ALCchar *ret = alcDeviceList+pos;
350 if(pos >= sizeof(alcDeviceList))
352 AL_PRINT("Not enough room to add %s!\n", name);
353 return alcDeviceList + sizeof(alcDeviceList) - 1;
355 pos += snprintf(alcDeviceList+pos, sizeof(alcDeviceList)-pos-1, "%s", name) + 1;
356 return ret;
359 ALCchar *AppendAllDeviceList(char *name)
361 static size_t pos;
362 ALCchar *ret = alcAllDeviceList+pos;
363 if(pos >= sizeof(alcAllDeviceList))
365 AL_PRINT("Not enough room to add %s!\n", name);
366 return alcAllDeviceList + sizeof(alcAllDeviceList) - 1;
368 pos += snprintf(alcAllDeviceList+pos, sizeof(alcAllDeviceList)-pos-1, "%s", name) + 1;
369 return ret;
372 ALCchar *AppendCaptureDeviceList(char *name)
374 static size_t pos;
375 ALCchar *ret = alcCaptureDeviceList+pos;
376 if(pos >= sizeof(alcCaptureDeviceList))
378 AL_PRINT("Not enough room to add %s!\n", name);
379 return alcCaptureDeviceList + sizeof(alcCaptureDeviceList) - 1;
381 pos += snprintf(alcCaptureDeviceList+pos, sizeof(alcCaptureDeviceList)-pos-1, "%s", name) + 1;
382 return ret;
386 IsContext
388 Check pContext is a valid Context pointer
390 static ALCboolean IsContext(ALCcontext *pContext)
392 ALCcontext *pTempContext;
394 pTempContext = g_pContextList;
395 while (pTempContext && pTempContext != pContext)
396 pTempContext = pTempContext->next;
398 return (pTempContext ? ALC_TRUE : ALC_FALSE);
403 SetALCError
405 Store latest ALC Error
407 ALCvoid SetALCError(ALenum errorCode)
409 g_eLastContextError = errorCode;
414 SuspendContext
416 Thread-safe entry
418 ALCvoid SuspendContext(ALCcontext *pContext)
420 (void)pContext;
421 EnterCriticalSection(&g_csMutex);
426 ProcessContext
428 Thread-safe exit
430 ALCvoid ProcessContext(ALCcontext *pContext)
432 (void)pContext;
433 LeaveCriticalSection(&g_csMutex);
438 InitContext
440 Initialize Context variables
442 static ALvoid InitContext(ALCcontext *pContext)
444 int level;
446 //Initialise listener
447 pContext->Listener.Gain = 1.0f;
448 pContext->Listener.MetersPerUnit = 1.0f;
449 pContext->Listener.Position[0] = 0.0f;
450 pContext->Listener.Position[1] = 0.0f;
451 pContext->Listener.Position[2] = 0.0f;
452 pContext->Listener.Velocity[0] = 0.0f;
453 pContext->Listener.Velocity[1] = 0.0f;
454 pContext->Listener.Velocity[2] = 0.0f;
455 pContext->Listener.Forward[0] = 0.0f;
456 pContext->Listener.Forward[1] = 0.0f;
457 pContext->Listener.Forward[2] = -1.0f;
458 pContext->Listener.Up[0] = 0.0f;
459 pContext->Listener.Up[1] = 1.0f;
460 pContext->Listener.Up[2] = 0.0f;
462 //Validate pContext
463 pContext->LastError = AL_NO_ERROR;
464 pContext->InUse = AL_FALSE;
466 //Set output format
467 pContext->Frequency = pContext->Device->Frequency;
469 //Set globals
470 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
471 pContext->DopplerFactor = 1.0f;
472 pContext->DopplerVelocity = 1.0f;
473 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
475 pContext->lNumStereoSources = 1;
476 pContext->lNumMonoSources = pContext->Device->MaxNoOfSources - pContext->lNumStereoSources;
478 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_LOKI_quadriphonic";
480 level = GetConfigValueInt(NULL, "cf_level", 0);
481 if(level > 0 && level <= 6)
483 pContext->bs2b = calloc(1, sizeof(*pContext->bs2b));
484 bs2b_set_srate(pContext->bs2b, pContext->Frequency);
485 bs2b_set_level(pContext->bs2b, level);
491 ExitContext
493 Clean up Context, destroy any remaining Sources
495 static ALCvoid ExitContext(ALCcontext *pContext)
497 //Invalidate context
498 pContext->LastError = AL_NO_ERROR;
499 pContext->InUse = AL_FALSE;
501 free(pContext->bs2b);
502 pContext->bs2b = NULL;
505 ///////////////////////////////////////////////////////
508 ///////////////////////////////////////////////////////
509 // ALC Functions calls
512 // This should probably move to another c file but for now ...
513 ALCAPI ALCdevice* ALCAPIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
515 ALCboolean DeviceFound = ALC_FALSE;
516 ALCdevice *pDevice = NULL;
517 ALCint i;
519 InitAL();
521 if(SampleSize <= 0)
523 SetALCError(ALC_INVALID_VALUE);
524 return NULL;
527 if(deviceName && !deviceName[0])
528 deviceName = NULL;
530 pDevice = malloc(sizeof(ALCdevice));
531 if (pDevice)
533 //Initialise device structure
534 memset(pDevice, 0, sizeof(ALCdevice));
536 //Validate device
537 pDevice->IsCaptureDevice = AL_TRUE;
539 pDevice->Frequency = frequency;
540 pDevice->Format = format;
542 for(i = 0;BackendList[i].Init;i++)
544 pDevice->Funcs = &BackendList[i].Funcs;
545 if(ALCdevice_OpenCapture(pDevice, deviceName, frequency, format, SampleSize))
547 SuspendContext(NULL);
548 pDevice->next = g_pDeviceList;
549 g_pDeviceList = pDevice;
550 g_ulDeviceCount++;
551 ProcessContext(NULL);
553 DeviceFound = ALC_TRUE;
554 break;
558 if(!DeviceFound)
560 SetALCError(ALC_INVALID_VALUE);
561 free(pDevice);
562 pDevice = NULL;
565 else
566 SetALCError(ALC_OUT_OF_MEMORY);
568 return pDevice;
571 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
573 ALCboolean bReturn = ALC_FALSE;
574 ALCdevice **list;
576 if ((pDevice)&&(pDevice->IsCaptureDevice))
578 SuspendContext(NULL);
580 list = &g_pDeviceList;
581 while(*list != pDevice)
582 list = &(*list)->next;
584 *list = (*list)->next;
585 g_ulDeviceCount--;
587 ProcessContext(NULL);
589 ALCdevice_CloseCapture(pDevice);
590 free(pDevice);
592 bReturn = ALC_TRUE;
594 else
595 SetALCError(ALC_INVALID_DEVICE);
597 return bReturn;
600 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
602 if ((pDevice)&&(pDevice->IsCaptureDevice))
603 ALCdevice_StartCapture(pDevice);
604 else
605 SetALCError(ALC_INVALID_DEVICE);
608 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
610 if ((pDevice)&&(pDevice->IsCaptureDevice))
611 ALCdevice_StopCapture(pDevice);
612 else
613 SetALCError(ALC_INVALID_DEVICE);
616 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
618 if ((pDevice) && (pDevice->IsCaptureDevice))
619 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
620 else
621 SetALCError(ALC_INVALID_DEVICE);
625 alcGetError
627 Return last ALC generated error code
629 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
631 ALCenum errorCode;
633 (void)device;
635 errorCode = g_eLastContextError;
636 g_eLastContextError = ALC_NO_ERROR;
637 return errorCode;
642 alcSuspendContext
644 Not functional
646 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
648 // Not a lot happens here !
649 (void)pContext;
654 alcProcessContext
656 Not functional
658 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
660 // Not a lot happens here !
661 (void)pContext;
666 alcGetString
668 Returns information about the Device, and error strings
670 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
672 const ALCchar *value = NULL;
674 InitAL();
676 switch (param)
678 case ALC_NO_ERROR:
679 value = alcNoError;
680 break;
682 case ALC_INVALID_ENUM:
683 value = alcErrInvalidEnum;
684 break;
686 case ALC_INVALID_VALUE:
687 value = alcErrInvalidValue;
688 break;
690 case ALC_INVALID_DEVICE:
691 value = alcErrInvalidDevice;
692 break;
694 case ALC_INVALID_CONTEXT:
695 value = alcErrInvalidContext;
696 break;
698 case ALC_OUT_OF_MEMORY:
699 value = alcErrOutOfMemory;
700 break;
702 case ALC_DEFAULT_DEVICE_SPECIFIER:
703 value = alcDefaultDeviceSpecifier;
704 break;
706 case ALC_DEVICE_SPECIFIER:
707 if (pDevice)
708 value = pDevice->szDeviceName;
709 else
710 value = alcDeviceList;
711 break;
713 case ALC_ALL_DEVICES_SPECIFIER:
714 value = alcAllDeviceList;
715 break;
717 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
718 value = alcDefaultAllDeviceSpecifier;
719 break;
721 case ALC_CAPTURE_DEVICE_SPECIFIER:
722 if (pDevice)
723 value = pDevice->szDeviceName;
724 else
725 value = alcCaptureDeviceList;
726 break;
728 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
729 value = alcCaptureDefaultDeviceSpecifier;
730 break;
732 case ALC_EXTENSIONS:
733 value = alcExtensionList;
734 break;
736 default:
737 SetALCError(ALC_INVALID_ENUM);
738 break;
741 return value;
746 alcGetIntegerv
748 Returns information about the Device and the version of Open AL
750 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
752 InitAL();
754 if ((device)&&(device->IsCaptureDevice))
756 SuspendContext(NULL);
758 // Capture device
759 switch (param)
761 case ALC_CAPTURE_SAMPLES:
762 if ((size) && (data))
763 *data = ALCdevice_AvailableSamples(device);
764 else
765 SetALCError(ALC_INVALID_VALUE);
766 break;
768 default:
769 SetALCError(ALC_INVALID_ENUM);
770 break;
773 ProcessContext(NULL);
775 else
777 if(data)
779 // Playback Device
780 switch (param)
782 case ALC_MAJOR_VERSION:
783 if(!size)
784 SetALCError(ALC_INVALID_VALUE);
785 else
786 *data = alcMajorVersion;
787 break;
789 case ALC_MINOR_VERSION:
790 if(!size)
791 SetALCError(ALC_INVALID_VALUE);
792 else
793 *data = alcMinorVersion;
794 break;
796 case ALC_EFX_MAJOR_VERSION:
797 if(!size)
798 SetALCError(ALC_INVALID_VALUE);
799 else
800 *data = alcEFXMajorVersion;
801 break;
803 case ALC_EFX_MINOR_VERSION:
804 if(!size)
805 SetALCError(ALC_INVALID_VALUE);
806 else
807 *data = alcEFXMinorVersion;
808 break;
810 case ALC_MAX_AUXILIARY_SENDS:
811 if(!size)
812 SetALCError(ALC_INVALID_VALUE);
813 else
814 *data = MAX_SENDS;
815 break;
817 case ALC_ATTRIBUTES_SIZE:
818 if(!device)
819 SetALCError(ALC_INVALID_DEVICE);
820 else if(!size)
821 SetALCError(ALC_INVALID_VALUE);
822 else
823 *data = 12;
824 break;
826 case ALC_ALL_ATTRIBUTES:
827 if(!device)
828 SetALCError(ALC_INVALID_DEVICE);
829 else if (size < 7)
830 SetALCError(ALC_INVALID_VALUE);
831 else
833 int i = 0;
835 data[i++] = ALC_FREQUENCY;
836 data[i++] = device->Frequency;
838 data[i++] = ALC_REFRESH;
839 data[i++] = device->Frequency / device->UpdateSize;
841 data[i++] = ALC_SYNC;
842 data[i++] = ALC_FALSE;
844 SuspendContext(NULL);
845 if(device->Context && size >= 12)
847 data[i++] = ALC_MONO_SOURCES;
848 data[i++] = device->Context->lNumMonoSources;
850 data[i++] = ALC_STEREO_SOURCES;
851 data[i++] = device->Context->lNumStereoSources;
853 data[i++] = ALC_MAX_AUXILIARY_SENDS;
854 data[i++] = MAX_SENDS;
856 ProcessContext(NULL);
858 data[i++] = 0;
860 break;
862 case ALC_FREQUENCY:
863 if(!device)
864 SetALCError(ALC_INVALID_DEVICE);
865 else if(!size)
866 SetALCError(ALC_INVALID_VALUE);
867 else
868 *data = device->Frequency;
869 break;
871 case ALC_REFRESH:
872 if(!device)
873 SetALCError(ALC_INVALID_DEVICE);
874 else if(!size)
875 SetALCError(ALC_INVALID_VALUE);
876 else
877 *data = device->Frequency / device->UpdateSize;
878 break;
880 case ALC_SYNC:
881 if(!device)
882 SetALCError(ALC_INVALID_DEVICE);
883 else if(!size)
884 SetALCError(ALC_INVALID_VALUE);
885 else
886 *data = ALC_FALSE;
887 break;
889 case ALC_MONO_SOURCES:
890 if(!device || !device->Context)
891 SetALCError(ALC_INVALID_DEVICE);
892 else if (size != 1)
893 SetALCError(ALC_INVALID_VALUE);
894 else
895 *data = device->Context->lNumMonoSources;
896 break;
898 case ALC_STEREO_SOURCES:
899 if(!device || !device->Context)
900 SetALCError(ALC_INVALID_DEVICE);
901 else if (size != 1)
902 SetALCError(ALC_INVALID_VALUE);
903 else
904 *data = device->Context->lNumStereoSources;
905 break;
907 default:
908 SetALCError(ALC_INVALID_ENUM);
909 break;
912 else if(size)
913 SetALCError(ALC_INVALID_VALUE);
916 return;
921 alcIsExtensionPresent
923 Determines if there is support for a particular extension
925 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
927 ALCboolean bResult = ALC_FALSE;
929 (void)device;
931 if (extName)
933 const char *ptr;
934 size_t len;
936 len = strlen(extName);
937 ptr = alcExtensionList;
938 while(ptr && *ptr)
940 if(strncasecmp(ptr, extName, len) == 0 &&
941 (ptr[len] == '\0' || isspace(ptr[len])))
943 bResult = ALC_TRUE;
944 break;
946 if((ptr=strchr(ptr, ' ')) != NULL)
948 do {
949 ++ptr;
950 } while(isspace(*ptr));
954 else
955 SetALCError(ALC_INVALID_VALUE);
957 return bResult;
962 alcGetProcAddress
964 Retrieves the function address for a particular extension function
966 ALCAPI ALCvoid * ALCAPIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
968 ALCvoid *pFunction = NULL;
969 ALsizei i = 0;
971 (void)device;
973 if (funcName)
975 while(alcFunctions[i].funcName &&
976 strcmp(alcFunctions[i].funcName,funcName) != 0)
977 i++;
978 pFunction = alcFunctions[i].address;
980 else
981 SetALCError(ALC_INVALID_VALUE);
983 return pFunction;
988 alcGetEnumValue
990 Get the value for a particular ALC Enumerated Value
992 ALCAPI ALCenum ALCAPIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
994 ALsizei i = 0;
995 ALCenum val;
997 (void)device;
999 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
1000 i++;
1001 val = enumeration[i].value;
1003 if(!enumeration[i].enumName)
1004 SetALCError(ALC_INVALID_VALUE);
1006 return val;
1011 alcCreateContext
1013 Create and attach a Context to a particular Device.
1015 ALCAPI ALCcontext* ALCAPIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
1017 ALCcontext *ALContext = NULL;
1018 ALuint ulAttributeIndex, ulRequestedStereoSources;
1020 if ((device)&&(!device->IsCaptureDevice))
1022 // Reset Context Last Error code
1023 g_eLastContextError = ALC_NO_ERROR;
1025 // Current implementation only allows one Context per Device
1026 if(!device->Context)
1028 ALContext = calloc(1, sizeof(ALCcontext));
1029 if(!ALContext)
1031 SetALCError(ALC_OUT_OF_MEMORY);
1032 return NULL;
1035 ALContext->Device = device;
1036 InitContext(ALContext);
1038 device->Context = ALContext;
1040 SuspendContext(NULL);
1042 ALContext->next = g_pContextList;
1043 g_pContextList = ALContext;
1044 g_ulContextCount++;
1046 ProcessContext(NULL);
1048 // Check for Voice Count attributes
1049 if (attrList)
1051 ulAttributeIndex = 0;
1052 while ((ulAttributeIndex < 10) && (attrList[ulAttributeIndex]))
1054 if (attrList[ulAttributeIndex] == ALC_STEREO_SOURCES)
1056 ulRequestedStereoSources = attrList[ulAttributeIndex + 1];
1058 if (ulRequestedStereoSources > ALContext->Device->MaxNoOfSources)
1059 ulRequestedStereoSources = ALContext->Device->MaxNoOfSources;
1061 ALContext->lNumStereoSources = ulRequestedStereoSources;
1062 ALContext->lNumMonoSources = ALContext->Device->MaxNoOfSources - ALContext->lNumStereoSources;
1063 break;
1066 ulAttributeIndex += 2;
1070 else
1072 SetALCError(ALC_INVALID_VALUE);
1073 ALContext = NULL;
1076 else
1077 SetALCError(ALC_INVALID_DEVICE);
1079 return ALContext;
1084 alcDestroyContext
1086 Remove a Context
1088 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
1090 ALCcontext **list;
1092 InitAL();
1094 // Lock context list
1095 SuspendContext(NULL);
1097 if (IsContext(context))
1099 // Lock context
1100 SuspendContext(context);
1102 ReleaseALSources(context);
1103 ReleaseALAuxiliaryEffectSlots(context);
1105 context->Device->Context = NULL;
1107 list = &g_pContextList;
1108 while(*list != context)
1109 list = &(*list)->next;
1111 *list = (*list)->next;
1112 g_ulContextCount--;
1114 // Unlock context
1115 ProcessContext(context);
1117 ExitContext(context);
1119 // Free memory (MUST do this after ProcessContext)
1120 memset(context, 0, sizeof(ALCcontext));
1121 free(context);
1123 else
1124 SetALCError(ALC_INVALID_CONTEXT);
1126 ProcessContext(NULL);
1131 alcGetCurrentContext
1133 Returns the currently active Context
1135 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1137 ALCcontext *pContext = NULL;
1139 InitAL();
1141 SuspendContext(NULL);
1143 pContext = g_pContextList;
1144 while ((pContext) && (!pContext->InUse))
1145 pContext = pContext->next;
1147 ProcessContext(NULL);
1149 return pContext;
1154 alcGetContextsDevice
1156 Returns the Device that a particular Context is attached to
1158 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1160 ALCdevice *pDevice = NULL;
1162 InitAL();
1164 SuspendContext(NULL);
1165 if (IsContext(pContext))
1166 pDevice = pContext->Device;
1167 else
1168 SetALCError(ALC_INVALID_CONTEXT);
1169 ProcessContext(NULL);
1171 return pDevice;
1176 alcMakeContextCurrent
1178 Makes the given Context the active Context
1180 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1182 ALCcontext *ALContext;
1183 ALboolean bReturn = AL_TRUE;
1185 InitAL();
1187 SuspendContext(NULL);
1189 // context must be a valid Context or NULL
1190 if ((IsContext(context)) || (context == NULL))
1192 if ((ALContext=alcGetCurrentContext()))
1194 SuspendContext(ALContext);
1195 ALContext->InUse=AL_FALSE;
1196 ProcessContext(ALContext);
1199 if ((ALContext=context) && (ALContext->Device))
1201 SuspendContext(ALContext);
1202 ALContext->InUse=AL_TRUE;
1203 ProcessContext(ALContext);
1206 else
1208 SetALCError(ALC_INVALID_CONTEXT);
1209 bReturn = AL_FALSE;
1212 ProcessContext(NULL);
1214 return bReturn;
1219 alcOpenDevice
1221 Open the Device specified.
1223 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1225 ALboolean bDeviceFound = AL_FALSE;
1226 ALCdevice *device;
1227 ALint i;
1229 InitAL();
1231 if(deviceName && !deviceName[0])
1232 deviceName = NULL;
1234 device = malloc(sizeof(ALCdevice));
1235 if (device)
1237 const char *fmt;
1239 //Initialise device structure
1240 memset(device, 0, sizeof(ALCdevice));
1242 //Validate device
1243 device->IsCaptureDevice = AL_FALSE;
1245 //Set output format
1246 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1247 if((ALint)device->Frequency <= 0)
1248 device->Frequency = SWMIXER_OUTPUT_RATE;
1250 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1251 if(fmt[0])
1252 device->Format = alGetEnumValue(fmt);
1254 if(!aluChannelsFromFormat(device->Format))
1255 device->Format = AL_FORMAT_STEREO16;
1257 device->UpdateSize = GetConfigValueInt(NULL, "refresh", 4096);
1258 if((ALint)device->UpdateSize <= 0)
1259 device->UpdateSize = 4096;
1261 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1262 if((ALint)device->MaxNoOfSources <= 0)
1263 device->MaxNoOfSources = 256;
1265 // Find a playback device to open
1266 for(i = 0;BackendList[i].Init;i++)
1268 device->Funcs = &BackendList[i].Funcs;
1269 if(ALCdevice_OpenPlayback(device, deviceName))
1271 SuspendContext(NULL);
1272 device->next = g_pDeviceList;
1273 g_pDeviceList = device;
1274 g_ulDeviceCount++;
1275 ProcessContext(NULL);
1277 bDeviceFound = AL_TRUE;
1278 break;
1282 if (!bDeviceFound)
1284 // No suitable output device found
1285 SetALCError(ALC_INVALID_VALUE);
1286 free(device);
1287 device = NULL;
1290 else
1291 SetALCError(ALC_OUT_OF_MEMORY);
1293 return device;
1298 alcCloseDevice
1300 Close the specified Device
1302 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1304 ALCboolean bReturn = ALC_FALSE;
1305 ALCdevice **list;
1307 if ((pDevice)&&(!pDevice->IsCaptureDevice))
1309 SuspendContext(NULL);
1311 list = &g_pDeviceList;
1312 while(*list != pDevice)
1313 list = &(*list)->next;
1315 *list = (*list)->next;
1316 g_ulDeviceCount--;
1318 ProcessContext(NULL);
1320 if(pDevice->Context)
1322 #ifdef _DEBUG
1323 AL_PRINT("alcCloseDevice(): destroying 1 Context\n");
1324 #endif
1325 alcDestroyContext(pDevice->Context);
1327 ALCdevice_ClosePlayback(pDevice);
1329 //Release device structure
1330 memset(pDevice, 0, sizeof(ALCdevice));
1331 free(pDevice);
1333 bReturn = ALC_TRUE;
1335 else
1336 SetALCError(ALC_INVALID_DEVICE);
1338 return bReturn;
1342 ALCvoid ReleaseALC(ALCvoid)
1344 #ifdef _DEBUG
1345 if(g_ulDeviceCount > 0)
1346 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
1347 #endif
1349 while(g_pDeviceList)
1351 if(g_pDeviceList->IsCaptureDevice)
1352 alcCaptureCloseDevice(g_pDeviceList);
1353 else
1354 alcCloseDevice(g_pDeviceList);
1358 ///////////////////////////////////////////////////////