Make auxiliary effect slot count configurable
[openal-soft.git] / Alc / ALc.c
blob943201f64240bf0fbdc408e7e57efef6851c0b86
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"
40 #include "alu.h"
42 ///////////////////////////////////////////////////////
43 // DEBUG INFORMATION
45 char _alDebug[256];
47 ///////////////////////////////////////////////////////
50 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
51 static struct {
52 const char *name;
53 void (*Init)(BackendFuncs*);
54 BackendFuncs Funcs;
55 } BackendList[] = {
56 #ifdef HAVE_ALSA
57 { "alsa", alc_alsa_init, EmptyFuncs },
58 #endif
59 #ifdef HAVE_OSS
60 { "oss", alc_oss_init, EmptyFuncs },
61 #endif
62 #ifdef HAVE_SOLARIS
63 { "solaris", alc_solaris_init, EmptyFuncs },
64 #endif
65 #ifdef HAVE_DSOUND
66 { "dsound", alcDSoundInit, EmptyFuncs },
67 #endif
68 #ifdef HAVE_WINMM
69 { "winmm", alcWinMMInit, EmptyFuncs },
70 #endif
71 #ifdef HAVE_PORTAUDIO
72 { "port", alc_pa_init, EmptyFuncs },
73 #endif
75 { "wave", alc_wave_init, EmptyFuncs },
77 { NULL, NULL, EmptyFuncs }
79 #undef EmptyFuncs
81 ///////////////////////////////////////////////////////
83 #define ALC_EFX_MAJOR_VERSION 0x20001
84 #define ALC_EFX_MINOR_VERSION 0x20002
85 #define ALC_MAX_AUXILIARY_SENDS 0x20003
87 ///////////////////////////////////////////////////////
88 // STRING and EXTENSIONS
90 typedef struct ALCfunction_struct
92 ALCchar *funcName;
93 ALvoid *address;
94 } ALCfunction;
96 static ALCfunction alcFunctions[] = {
97 { "alcCreateContext", (ALvoid *) alcCreateContext },
98 { "alcMakeContextCurrent", (ALvoid *) alcMakeContextCurrent },
99 { "alcProcessContext", (ALvoid *) alcProcessContext },
100 { "alcSuspendContext", (ALvoid *) alcSuspendContext },
101 { "alcDestroyContext", (ALvoid *) alcDestroyContext },
102 { "alcGetCurrentContext", (ALvoid *) alcGetCurrentContext },
103 { "alcGetContextsDevice", (ALvoid *) alcGetContextsDevice },
104 { "alcOpenDevice", (ALvoid *) alcOpenDevice },
105 { "alcCloseDevice", (ALvoid *) alcCloseDevice },
106 { "alcGetError", (ALvoid *) alcGetError },
107 { "alcIsExtensionPresent", (ALvoid *) alcIsExtensionPresent },
108 { "alcGetProcAddress", (ALvoid *) alcGetProcAddress },
109 { "alcGetEnumValue", (ALvoid *) alcGetEnumValue },
110 { "alcGetString", (ALvoid *) alcGetString },
111 { "alcGetIntegerv", (ALvoid *) alcGetIntegerv },
112 { "alcCaptureOpenDevice", (ALvoid *) alcCaptureOpenDevice },
113 { "alcCaptureCloseDevice", (ALvoid *) alcCaptureCloseDevice },
114 { "alcCaptureStart", (ALvoid *) alcCaptureStart },
115 { "alcCaptureStop", (ALvoid *) alcCaptureStop },
116 { "alcCaptureSamples", (ALvoid *) alcCaptureSamples },
117 { NULL, (ALvoid *) NULL }
120 static ALenums enumeration[]={
121 // Types
122 { (ALchar *)"ALC_INVALID", ALC_INVALID },
123 { (ALchar *)"ALC_FALSE", ALC_FALSE },
124 { (ALchar *)"ALC_TRUE", ALC_TRUE },
126 // ALC Properties
127 { (ALchar *)"ALC_MAJOR_VERSION", ALC_MAJOR_VERSION },
128 { (ALchar *)"ALC_MINOR_VERSION", ALC_MINOR_VERSION },
129 { (ALchar *)"ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE },
130 { (ALchar *)"ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES },
131 { (ALchar *)"ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER },
132 { (ALchar *)"ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER },
133 { (ALchar *)"ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER },
134 { (ALchar *)"ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER },
135 { (ALchar *)"ALC_EXTENSIONS", ALC_EXTENSIONS },
136 { (ALchar *)"ALC_FREQUENCY", ALC_FREQUENCY },
137 { (ALchar *)"ALC_REFRESH", ALC_REFRESH },
138 { (ALchar *)"ALC_SYNC", ALC_SYNC },
139 { (ALchar *)"ALC_MONO_SOURCES", ALC_MONO_SOURCES },
140 { (ALchar *)"ALC_STEREO_SOURCES", ALC_STEREO_SOURCES },
141 { (ALchar *)"ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER },
142 { (ALchar *)"ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER},
143 { (ALchar *)"ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES },
145 // EFX Properties
146 { (ALchar *)"ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION },
147 { (ALchar *)"ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION },
148 { (ALchar *)"ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS },
150 // ALC Error Message
151 { (ALchar *)"ALC_NO_ERROR", ALC_NO_ERROR },
152 { (ALchar *)"ALC_INVALID_DEVICE", ALC_INVALID_DEVICE },
153 { (ALchar *)"ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT },
154 { (ALchar *)"ALC_INVALID_ENUM", ALC_INVALID_ENUM },
155 { (ALchar *)"ALC_INVALID_VALUE", ALC_INVALID_VALUE },
156 { (ALchar *)"ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY },
157 { (ALchar *)NULL, (ALenum)0 }
159 // Error strings
160 static const ALCchar alcNoError[] = "No Error";
161 static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
162 static const ALCchar alcErrInvalidContext[] = "Invalid Context";
163 static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
164 static const ALCchar alcErrInvalidValue[] = "Invalid Value";
165 static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
167 // Context strings
168 static ALCchar alcDeviceList[2048];
169 static ALCchar alcAllDeviceList[2048];
170 static ALCchar alcCaptureDeviceList[2048];
171 // Default is always the first in the list
172 static ALCchar *alcDefaultDeviceSpecifier = alcDeviceList;
173 static ALCchar *alcDefaultAllDeviceSpecifier = alcAllDeviceList;
174 static ALCchar *alcCaptureDefaultDeviceSpecifier = alcCaptureDeviceList;
177 static ALCchar alcExtensionList[] = "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_EFX";
178 static ALCint alcMajorVersion = 1;
179 static ALCint alcMinorVersion = 1;
181 static ALCint alcEFXMajorVersion = 1;
182 static ALCint alcEFXMinorVersion = 0;
184 ///////////////////////////////////////////////////////
187 ///////////////////////////////////////////////////////
188 // Global Variables
190 static ALCdevice *g_pDeviceList = NULL;
191 static ALCuint g_ulDeviceCount = 0;
193 static CRITICAL_SECTION g_csMutex;
195 // Context List
196 static ALCcontext *g_pContextList = NULL;
197 static ALCuint g_ulContextCount = 0;
199 // Context Error
200 static ALCenum g_eLastContextError = ALC_NO_ERROR;
202 static ALboolean init_done = AL_FALSE;
204 ///////////////////////////////////////////////////////
207 ///////////////////////////////////////////////////////
208 // ALC Related helper functions
209 #ifdef _WIN32
210 BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
212 (void)lpReserved;
214 // Perform actions based on the reason for calling.
215 switch(ul_reason_for_call)
217 case DLL_PROCESS_ATTACH:
218 DisableThreadLibraryCalls(hModule);
219 break;
221 case DLL_PROCESS_DETACH:
222 if(!init_done)
223 break;
224 ReleaseALC();
225 ReleaseALBuffers();
226 ReleaseALEffects();
227 ReleaseALFilters();
228 FreeALConfig();
229 ALTHUNK_EXIT();
230 DeleteCriticalSection(&g_csMutex);
231 break;
233 return TRUE;
235 #else
236 #ifdef HAVE_GCC_DESTRUCTOR
237 static void my_deinit() __attribute__((destructor));
238 static void my_deinit()
240 static ALenum once = AL_FALSE;
241 if(once || !init_done) return;
242 once = AL_TRUE;
244 ReleaseALC();
245 ReleaseALBuffers();
246 ReleaseALEffects();
247 ReleaseALFilters();
248 FreeALConfig();
249 ALTHUNK_EXIT();
250 DeleteCriticalSection(&g_csMutex);
252 #endif
253 #endif
255 static void InitAL(void)
257 if(!init_done)
259 int i;
260 const char *devs, *str;
262 init_done = AL_TRUE;
264 InitializeCriticalSection(&g_csMutex);
265 ALTHUNK_INIT();
266 ReadALConfig();
268 devs = GetConfigValue(NULL, "drivers", "");
269 if(devs[0])
271 int n;
272 size_t len;
273 const char *next = devs;
275 i = 0;
277 do {
278 devs = next;
279 next = strchr(devs, ',');
281 if(!devs[0] || devs[0] == ',')
282 continue;
284 len = (next ? ((size_t)(next-devs)) : strlen(devs));
285 for(n = i;BackendList[n].Init;n++)
287 if(len == strlen(BackendList[n].name) &&
288 strncmp(BackendList[n].name, devs, len) == 0)
290 const char *name = BackendList[i].name;
291 void (*Init)(BackendFuncs*) = BackendList[i].Init;
293 BackendList[i].name = BackendList[n].name;
294 BackendList[i].Init = BackendList[n].Init;
296 BackendList[n].name = name;
297 BackendList[n].Init = Init;
299 i++;
302 } while(next++);
304 BackendList[i].name = NULL;
305 BackendList[i].Init = NULL;
308 for(i = 0;BackendList[i].Init;i++)
309 BackendList[i].Init(&BackendList[i].Funcs);
311 str = GetConfigValue(NULL, "stereodup", "false");
312 DuplicateStereo = (strcasecmp(str, "true") == 0 ||
313 strcasecmp(str, "yes") == 0 ||
314 strcasecmp(str, "on") == 0 ||
315 atoi(str) != 0);
317 str = GetConfigValue(NULL, "excludefx", "");
318 if(str[0])
320 const struct {
321 const char *name;
322 int type;
323 } EffectList[] = {
324 { "reverb", REVERB },
325 { NULL, 0 }
327 int n;
328 size_t len;
329 const char *next = str;
331 do {
332 str = next;
333 next = strchr(str, ',');
335 if(!str[0] || next == str)
336 continue;
338 len = (next ? ((size_t)(next-str)) : strlen(str));
339 for(n = 0;EffectList[n].name;n++)
341 if(len == strlen(EffectList[n].name) &&
342 strncmp(EffectList[n].name, str, len) == 0)
343 DisabledEffects[EffectList[n].type] = AL_TRUE;
345 } while(next++);
350 ALCchar *AppendDeviceList(char *name)
352 static size_t pos;
353 ALCchar *ret = alcDeviceList+pos;
354 if(pos >= sizeof(alcDeviceList))
356 AL_PRINT("Not enough room to add %s!\n", name);
357 return alcDeviceList + sizeof(alcDeviceList) - 1;
359 pos += snprintf(alcDeviceList+pos, sizeof(alcDeviceList)-pos-1, "%s", name) + 1;
360 return ret;
363 ALCchar *AppendAllDeviceList(char *name)
365 static size_t pos;
366 ALCchar *ret = alcAllDeviceList+pos;
367 if(pos >= sizeof(alcAllDeviceList))
369 AL_PRINT("Not enough room to add %s!\n", name);
370 return alcAllDeviceList + sizeof(alcAllDeviceList) - 1;
372 pos += snprintf(alcAllDeviceList+pos, sizeof(alcAllDeviceList)-pos-1, "%s", name) + 1;
373 return ret;
376 ALCchar *AppendCaptureDeviceList(char *name)
378 static size_t pos;
379 ALCchar *ret = alcCaptureDeviceList+pos;
380 if(pos >= sizeof(alcCaptureDeviceList))
382 AL_PRINT("Not enough room to add %s!\n", name);
383 return alcCaptureDeviceList + sizeof(alcCaptureDeviceList) - 1;
385 pos += snprintf(alcCaptureDeviceList+pos, sizeof(alcCaptureDeviceList)-pos-1, "%s", name) + 1;
386 return ret;
390 IsContext
392 Check pContext is a valid Context pointer
394 static ALCboolean IsContext(ALCcontext *pContext)
396 ALCcontext *pTempContext;
398 pTempContext = g_pContextList;
399 while (pTempContext && pTempContext != pContext)
400 pTempContext = pTempContext->next;
402 return (pTempContext ? ALC_TRUE : ALC_FALSE);
407 SetALCError
409 Store latest ALC Error
411 ALCvoid SetALCError(ALenum errorCode)
413 g_eLastContextError = errorCode;
418 SuspendContext
420 Thread-safe entry
422 ALCvoid SuspendContext(ALCcontext *pContext)
424 (void)pContext;
425 EnterCriticalSection(&g_csMutex);
430 ProcessContext
432 Thread-safe exit
434 ALCvoid ProcessContext(ALCcontext *pContext)
436 (void)pContext;
437 LeaveCriticalSection(&g_csMutex);
442 InitContext
444 Initialize Context variables
446 static ALvoid InitContext(ALCcontext *pContext)
448 int level;
450 //Initialise listener
451 pContext->Listener.Gain = 1.0f;
452 pContext->Listener.MetersPerUnit = 1.0f;
453 pContext->Listener.Position[0] = 0.0f;
454 pContext->Listener.Position[1] = 0.0f;
455 pContext->Listener.Position[2] = 0.0f;
456 pContext->Listener.Velocity[0] = 0.0f;
457 pContext->Listener.Velocity[1] = 0.0f;
458 pContext->Listener.Velocity[2] = 0.0f;
459 pContext->Listener.Forward[0] = 0.0f;
460 pContext->Listener.Forward[1] = 0.0f;
461 pContext->Listener.Forward[2] = -1.0f;
462 pContext->Listener.Up[0] = 0.0f;
463 pContext->Listener.Up[1] = 1.0f;
464 pContext->Listener.Up[2] = 0.0f;
466 //Validate pContext
467 pContext->LastError = AL_NO_ERROR;
468 pContext->InUse = AL_FALSE;
470 //Set output format
471 pContext->Frequency = pContext->Device->Frequency;
473 //Set globals
474 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
475 pContext->DopplerFactor = 1.0f;
476 pContext->DopplerVelocity = 1.0f;
477 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
479 pContext->lNumStereoSources = 1;
480 pContext->lNumMonoSources = pContext->Device->MaxNoOfSources - pContext->lNumStereoSources;
482 pContext->AuxiliaryEffectSlotMax = GetConfigValueInt(NULL, "slots", 4);
484 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_source_distance_model AL_LOKI_quadriphonic";
486 level = GetConfigValueInt(NULL, "cf_level", 0);
487 if(level > 0 && level <= 6)
489 pContext->bs2b = calloc(1, sizeof(*pContext->bs2b));
490 bs2b_set_srate(pContext->bs2b, pContext->Frequency);
491 bs2b_set_level(pContext->bs2b, level);
494 aluInitPanning(pContext);
499 ExitContext
501 Clean up Context, destroy any remaining Sources
503 static ALCvoid ExitContext(ALCcontext *pContext)
505 //Invalidate context
506 pContext->LastError = AL_NO_ERROR;
507 pContext->InUse = AL_FALSE;
509 free(pContext->bs2b);
510 pContext->bs2b = NULL;
513 ///////////////////////////////////////////////////////
516 ///////////////////////////////////////////////////////
517 // ALC Functions calls
520 // This should probably move to another c file but for now ...
521 ALCAPI ALCdevice* ALCAPIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
523 ALCboolean DeviceFound = ALC_FALSE;
524 ALCdevice *pDevice = NULL;
525 ALCint i;
527 InitAL();
529 if(SampleSize <= 0)
531 SetALCError(ALC_INVALID_VALUE);
532 return NULL;
535 if(deviceName && !deviceName[0])
536 deviceName = NULL;
538 pDevice = malloc(sizeof(ALCdevice));
539 if (pDevice)
541 //Initialise device structure
542 memset(pDevice, 0, sizeof(ALCdevice));
544 //Validate device
545 pDevice->IsCaptureDevice = AL_TRUE;
547 pDevice->Frequency = frequency;
548 pDevice->Format = format;
550 for(i = 0;BackendList[i].Init;i++)
552 pDevice->Funcs = &BackendList[i].Funcs;
553 if(ALCdevice_OpenCapture(pDevice, deviceName, frequency, format, SampleSize))
555 SuspendContext(NULL);
556 pDevice->next = g_pDeviceList;
557 g_pDeviceList = pDevice;
558 g_ulDeviceCount++;
559 ProcessContext(NULL);
561 DeviceFound = ALC_TRUE;
562 break;
566 if(!DeviceFound)
568 SetALCError(ALC_INVALID_VALUE);
569 free(pDevice);
570 pDevice = NULL;
573 else
574 SetALCError(ALC_OUT_OF_MEMORY);
576 return pDevice;
579 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
581 ALCboolean bReturn = ALC_FALSE;
582 ALCdevice **list;
584 if ((pDevice)&&(pDevice->IsCaptureDevice))
586 SuspendContext(NULL);
588 list = &g_pDeviceList;
589 while(*list != pDevice)
590 list = &(*list)->next;
592 *list = (*list)->next;
593 g_ulDeviceCount--;
595 ProcessContext(NULL);
597 ALCdevice_CloseCapture(pDevice);
598 free(pDevice);
600 bReturn = ALC_TRUE;
602 else
603 SetALCError(ALC_INVALID_DEVICE);
605 return bReturn;
608 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
610 if ((pDevice)&&(pDevice->IsCaptureDevice))
611 ALCdevice_StartCapture(pDevice);
612 else
613 SetALCError(ALC_INVALID_DEVICE);
616 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
618 if ((pDevice)&&(pDevice->IsCaptureDevice))
619 ALCdevice_StopCapture(pDevice);
620 else
621 SetALCError(ALC_INVALID_DEVICE);
624 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
626 if ((pDevice) && (pDevice->IsCaptureDevice))
627 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
628 else
629 SetALCError(ALC_INVALID_DEVICE);
633 alcGetError
635 Return last ALC generated error code
637 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
639 ALCenum errorCode;
641 (void)device;
643 errorCode = g_eLastContextError;
644 g_eLastContextError = ALC_NO_ERROR;
645 return errorCode;
650 alcSuspendContext
652 Not functional
654 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
656 // Not a lot happens here !
657 (void)pContext;
662 alcProcessContext
664 Not functional
666 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
668 // Not a lot happens here !
669 (void)pContext;
674 alcGetString
676 Returns information about the Device, and error strings
678 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
680 const ALCchar *value = NULL;
682 InitAL();
684 switch (param)
686 case ALC_NO_ERROR:
687 value = alcNoError;
688 break;
690 case ALC_INVALID_ENUM:
691 value = alcErrInvalidEnum;
692 break;
694 case ALC_INVALID_VALUE:
695 value = alcErrInvalidValue;
696 break;
698 case ALC_INVALID_DEVICE:
699 value = alcErrInvalidDevice;
700 break;
702 case ALC_INVALID_CONTEXT:
703 value = alcErrInvalidContext;
704 break;
706 case ALC_OUT_OF_MEMORY:
707 value = alcErrOutOfMemory;
708 break;
710 case ALC_DEFAULT_DEVICE_SPECIFIER:
711 value = alcDefaultDeviceSpecifier;
712 break;
714 case ALC_DEVICE_SPECIFIER:
715 if (pDevice)
716 value = pDevice->szDeviceName;
717 else
718 value = alcDeviceList;
719 break;
721 case ALC_ALL_DEVICES_SPECIFIER:
722 value = alcAllDeviceList;
723 break;
725 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
726 value = alcDefaultAllDeviceSpecifier;
727 break;
729 case ALC_CAPTURE_DEVICE_SPECIFIER:
730 if (pDevice)
731 value = pDevice->szDeviceName;
732 else
733 value = alcCaptureDeviceList;
734 break;
736 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
737 value = alcCaptureDefaultDeviceSpecifier;
738 break;
740 case ALC_EXTENSIONS:
741 value = alcExtensionList;
742 break;
744 default:
745 SetALCError(ALC_INVALID_ENUM);
746 break;
749 return value;
754 alcGetIntegerv
756 Returns information about the Device and the version of Open AL
758 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
760 InitAL();
762 if ((device)&&(device->IsCaptureDevice))
764 SuspendContext(NULL);
766 // Capture device
767 switch (param)
769 case ALC_CAPTURE_SAMPLES:
770 if ((size) && (data))
771 *data = ALCdevice_AvailableSamples(device);
772 else
773 SetALCError(ALC_INVALID_VALUE);
774 break;
776 default:
777 SetALCError(ALC_INVALID_ENUM);
778 break;
781 ProcessContext(NULL);
783 else
785 if(data)
787 // Playback Device
788 switch (param)
790 case ALC_MAJOR_VERSION:
791 if(!size)
792 SetALCError(ALC_INVALID_VALUE);
793 else
794 *data = alcMajorVersion;
795 break;
797 case ALC_MINOR_VERSION:
798 if(!size)
799 SetALCError(ALC_INVALID_VALUE);
800 else
801 *data = alcMinorVersion;
802 break;
804 case ALC_EFX_MAJOR_VERSION:
805 if(!size)
806 SetALCError(ALC_INVALID_VALUE);
807 else
808 *data = alcEFXMajorVersion;
809 break;
811 case ALC_EFX_MINOR_VERSION:
812 if(!size)
813 SetALCError(ALC_INVALID_VALUE);
814 else
815 *data = alcEFXMinorVersion;
816 break;
818 case ALC_MAX_AUXILIARY_SENDS:
819 if(!size)
820 SetALCError(ALC_INVALID_VALUE);
821 else
822 *data = MAX_SENDS;
823 break;
825 case ALC_ATTRIBUTES_SIZE:
826 if(!device)
827 SetALCError(ALC_INVALID_DEVICE);
828 else if(!size)
829 SetALCError(ALC_INVALID_VALUE);
830 else
831 *data = 12;
832 break;
834 case ALC_ALL_ATTRIBUTES:
835 if(!device)
836 SetALCError(ALC_INVALID_DEVICE);
837 else if (size < 7)
838 SetALCError(ALC_INVALID_VALUE);
839 else
841 int i = 0;
843 data[i++] = ALC_FREQUENCY;
844 data[i++] = device->Frequency;
846 data[i++] = ALC_REFRESH;
847 data[i++] = device->Frequency / device->UpdateSize;
849 data[i++] = ALC_SYNC;
850 data[i++] = ALC_FALSE;
852 SuspendContext(NULL);
853 if(device->Context && size >= 12)
855 data[i++] = ALC_MONO_SOURCES;
856 data[i++] = device->Context->lNumMonoSources;
858 data[i++] = ALC_STEREO_SOURCES;
859 data[i++] = device->Context->lNumStereoSources;
861 data[i++] = ALC_MAX_AUXILIARY_SENDS;
862 data[i++] = MAX_SENDS;
864 ProcessContext(NULL);
866 data[i++] = 0;
868 break;
870 case ALC_FREQUENCY:
871 if(!device)
872 SetALCError(ALC_INVALID_DEVICE);
873 else if(!size)
874 SetALCError(ALC_INVALID_VALUE);
875 else
876 *data = device->Frequency;
877 break;
879 case ALC_REFRESH:
880 if(!device)
881 SetALCError(ALC_INVALID_DEVICE);
882 else if(!size)
883 SetALCError(ALC_INVALID_VALUE);
884 else
885 *data = device->Frequency / device->UpdateSize;
886 break;
888 case ALC_SYNC:
889 if(!device)
890 SetALCError(ALC_INVALID_DEVICE);
891 else if(!size)
892 SetALCError(ALC_INVALID_VALUE);
893 else
894 *data = ALC_FALSE;
895 break;
897 case ALC_MONO_SOURCES:
898 if(!device || !device->Context)
899 SetALCError(ALC_INVALID_DEVICE);
900 else if (size != 1)
901 SetALCError(ALC_INVALID_VALUE);
902 else
903 *data = device->Context->lNumMonoSources;
904 break;
906 case ALC_STEREO_SOURCES:
907 if(!device || !device->Context)
908 SetALCError(ALC_INVALID_DEVICE);
909 else if (size != 1)
910 SetALCError(ALC_INVALID_VALUE);
911 else
912 *data = device->Context->lNumStereoSources;
913 break;
915 default:
916 SetALCError(ALC_INVALID_ENUM);
917 break;
920 else if(size)
921 SetALCError(ALC_INVALID_VALUE);
924 return;
929 alcIsExtensionPresent
931 Determines if there is support for a particular extension
933 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
935 ALCboolean bResult = ALC_FALSE;
937 (void)device;
939 if (extName)
941 const char *ptr;
942 size_t len;
944 len = strlen(extName);
945 ptr = alcExtensionList;
946 while(ptr && *ptr)
948 if(strncasecmp(ptr, extName, len) == 0 &&
949 (ptr[len] == '\0' || isspace(ptr[len])))
951 bResult = ALC_TRUE;
952 break;
954 if((ptr=strchr(ptr, ' ')) != NULL)
956 do {
957 ++ptr;
958 } while(isspace(*ptr));
962 else
963 SetALCError(ALC_INVALID_VALUE);
965 return bResult;
970 alcGetProcAddress
972 Retrieves the function address for a particular extension function
974 ALCAPI ALCvoid * ALCAPIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
976 ALCvoid *pFunction = NULL;
977 ALsizei i = 0;
979 (void)device;
981 if (funcName)
983 while(alcFunctions[i].funcName &&
984 strcmp(alcFunctions[i].funcName,funcName) != 0)
985 i++;
986 pFunction = alcFunctions[i].address;
988 else
989 SetALCError(ALC_INVALID_VALUE);
991 return pFunction;
996 alcGetEnumValue
998 Get the value for a particular ALC Enumerated Value
1000 ALCAPI ALCenum ALCAPIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
1002 ALsizei i = 0;
1003 ALCenum val;
1005 (void)device;
1007 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
1008 i++;
1009 val = enumeration[i].value;
1011 if(!enumeration[i].enumName)
1012 SetALCError(ALC_INVALID_VALUE);
1014 return val;
1019 alcCreateContext
1021 Create and attach a Context to a particular Device.
1023 ALCAPI ALCcontext* ALCAPIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
1025 ALCcontext *ALContext = NULL;
1026 ALuint ulAttributeIndex, ulRequestedStereoSources;
1028 if ((device)&&(!device->IsCaptureDevice))
1030 // Reset Context Last Error code
1031 g_eLastContextError = ALC_NO_ERROR;
1033 // Current implementation only allows one Context per Device
1034 if(!device->Context)
1036 ALContext = calloc(1, sizeof(ALCcontext));
1037 if(!ALContext)
1039 SetALCError(ALC_OUT_OF_MEMORY);
1040 return NULL;
1043 ALContext->Device = device;
1044 InitContext(ALContext);
1046 device->Context = ALContext;
1048 SuspendContext(NULL);
1050 ALContext->next = g_pContextList;
1051 g_pContextList = ALContext;
1052 g_ulContextCount++;
1054 ProcessContext(NULL);
1056 // Check for Voice Count attributes
1057 if (attrList)
1059 ulAttributeIndex = 0;
1060 while ((ulAttributeIndex < 10) && (attrList[ulAttributeIndex]))
1062 if (attrList[ulAttributeIndex] == ALC_STEREO_SOURCES)
1064 ulRequestedStereoSources = attrList[ulAttributeIndex + 1];
1066 if (ulRequestedStereoSources > ALContext->Device->MaxNoOfSources)
1067 ulRequestedStereoSources = ALContext->Device->MaxNoOfSources;
1069 ALContext->lNumStereoSources = ulRequestedStereoSources;
1070 ALContext->lNumMonoSources = ALContext->Device->MaxNoOfSources - ALContext->lNumStereoSources;
1071 break;
1074 ulAttributeIndex += 2;
1078 else
1080 SetALCError(ALC_INVALID_VALUE);
1081 ALContext = NULL;
1084 else
1085 SetALCError(ALC_INVALID_DEVICE);
1087 return ALContext;
1092 alcDestroyContext
1094 Remove a Context
1096 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
1098 ALCcontext **list;
1100 InitAL();
1102 // Lock context list
1103 SuspendContext(NULL);
1105 if (IsContext(context))
1107 // Lock context
1108 SuspendContext(context);
1110 ReleaseALSources(context);
1111 ReleaseALAuxiliaryEffectSlots(context);
1113 context->Device->Context = NULL;
1115 list = &g_pContextList;
1116 while(*list != context)
1117 list = &(*list)->next;
1119 *list = (*list)->next;
1120 g_ulContextCount--;
1122 // Unlock context
1123 ProcessContext(context);
1125 ExitContext(context);
1127 // Free memory (MUST do this after ProcessContext)
1128 memset(context, 0, sizeof(ALCcontext));
1129 free(context);
1131 else
1132 SetALCError(ALC_INVALID_CONTEXT);
1134 ProcessContext(NULL);
1139 alcGetCurrentContext
1141 Returns the currently active Context
1143 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1145 ALCcontext *pContext = NULL;
1147 InitAL();
1149 SuspendContext(NULL);
1151 pContext = g_pContextList;
1152 while ((pContext) && (!pContext->InUse))
1153 pContext = pContext->next;
1155 ProcessContext(NULL);
1157 return pContext;
1162 alcGetContextsDevice
1164 Returns the Device that a particular Context is attached to
1166 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1168 ALCdevice *pDevice = NULL;
1170 InitAL();
1172 SuspendContext(NULL);
1173 if (IsContext(pContext))
1174 pDevice = pContext->Device;
1175 else
1176 SetALCError(ALC_INVALID_CONTEXT);
1177 ProcessContext(NULL);
1179 return pDevice;
1184 alcMakeContextCurrent
1186 Makes the given Context the active Context
1188 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1190 ALCcontext *ALContext;
1191 ALboolean bReturn = AL_TRUE;
1193 InitAL();
1195 SuspendContext(NULL);
1197 // context must be a valid Context or NULL
1198 if ((IsContext(context)) || (context == NULL))
1200 if ((ALContext=alcGetCurrentContext()))
1202 SuspendContext(ALContext);
1203 ALContext->InUse=AL_FALSE;
1204 ProcessContext(ALContext);
1207 if ((ALContext=context) && (ALContext->Device))
1209 SuspendContext(ALContext);
1210 ALContext->InUse=AL_TRUE;
1211 ProcessContext(ALContext);
1214 else
1216 SetALCError(ALC_INVALID_CONTEXT);
1217 bReturn = AL_FALSE;
1220 ProcessContext(NULL);
1222 return bReturn;
1227 alcOpenDevice
1229 Open the Device specified.
1231 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1233 ALboolean bDeviceFound = AL_FALSE;
1234 ALCdevice *device;
1235 ALint i;
1237 InitAL();
1239 if(deviceName && !deviceName[0])
1240 deviceName = NULL;
1242 device = malloc(sizeof(ALCdevice));
1243 if (device)
1245 const char *fmt;
1247 //Initialise device structure
1248 memset(device, 0, sizeof(ALCdevice));
1250 //Validate device
1251 device->IsCaptureDevice = AL_FALSE;
1253 //Set output format
1254 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1255 if((ALint)device->Frequency <= 0)
1256 device->Frequency = SWMIXER_OUTPUT_RATE;
1258 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1259 if(fmt[0])
1260 device->Format = alGetEnumValue(fmt);
1262 if(!aluChannelsFromFormat(device->Format))
1263 device->Format = AL_FORMAT_STEREO16;
1265 device->UpdateSize = GetConfigValueInt(NULL, "refresh", 4096);
1266 if((ALint)device->UpdateSize <= 0)
1267 device->UpdateSize = 4096;
1269 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1270 if((ALint)device->MaxNoOfSources <= 0)
1271 device->MaxNoOfSources = 256;
1273 // Find a playback device to open
1274 SuspendContext(NULL);
1275 for(i = 0;BackendList[i].Init;i++)
1277 device->Funcs = &BackendList[i].Funcs;
1278 if(ALCdevice_OpenPlayback(device, deviceName))
1280 device->next = g_pDeviceList;
1281 g_pDeviceList = device;
1282 g_ulDeviceCount++;
1284 bDeviceFound = AL_TRUE;
1285 break;
1288 ProcessContext(NULL);
1290 if (!bDeviceFound)
1292 // No suitable output device found
1293 SetALCError(ALC_INVALID_VALUE);
1294 free(device);
1295 device = NULL;
1298 else
1299 SetALCError(ALC_OUT_OF_MEMORY);
1301 return device;
1306 alcCloseDevice
1308 Close the specified Device
1310 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1312 ALCboolean bReturn = ALC_FALSE;
1313 ALCdevice **list;
1315 if ((pDevice)&&(!pDevice->IsCaptureDevice))
1317 SuspendContext(NULL);
1319 list = &g_pDeviceList;
1320 while(*list != pDevice)
1321 list = &(*list)->next;
1323 *list = (*list)->next;
1324 g_ulDeviceCount--;
1326 ProcessContext(NULL);
1328 if(pDevice->Context)
1330 #ifdef _DEBUG
1331 AL_PRINT("alcCloseDevice(): destroying 1 Context\n");
1332 #endif
1333 alcDestroyContext(pDevice->Context);
1335 ALCdevice_ClosePlayback(pDevice);
1337 //Release device structure
1338 memset(pDevice, 0, sizeof(ALCdevice));
1339 free(pDevice);
1341 bReturn = ALC_TRUE;
1343 else
1344 SetALCError(ALC_INVALID_DEVICE);
1346 return bReturn;
1350 ALCvoid ReleaseALC(ALCvoid)
1352 #ifdef _DEBUG
1353 if(g_ulDeviceCount > 0)
1354 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
1355 #endif
1357 while(g_pDeviceList)
1359 if(g_pDeviceList->IsCaptureDevice)
1360 alcCaptureCloseDevice(g_pDeviceList);
1361 else
1362 alcCloseDevice(g_pDeviceList);
1366 ///////////////////////////////////////////////////////