Use a thread-safe static inline function for printing
[openal-soft.git] / Alc / ALc.c
blobbf7572182149e520adc93971bd8d5ff39b37037c
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 "bs2b.h"
38 #include "alu.h"
41 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
42 static struct {
43 const char *name;
44 void (*Init)(BackendFuncs*);
45 BackendFuncs Funcs;
46 } BackendList[] = {
47 #ifdef HAVE_ALSA
48 { "alsa", alc_alsa_init, EmptyFuncs },
49 #endif
50 #ifdef HAVE_OSS
51 { "oss", alc_oss_init, EmptyFuncs },
52 #endif
53 #ifdef HAVE_SOLARIS
54 { "solaris", alc_solaris_init, EmptyFuncs },
55 #endif
56 #ifdef HAVE_DSOUND
57 { "dsound", alcDSoundInit, EmptyFuncs },
58 #endif
59 #ifdef HAVE_WINMM
60 { "winmm", alcWinMMInit, EmptyFuncs },
61 #endif
62 #ifdef HAVE_PORTAUDIO
63 { "port", alc_pa_init, EmptyFuncs },
64 #endif
65 #ifdef HAVE_PULSEAUDIO
66 { "pulse", alc_pulse_init, EmptyFuncs },
67 #endif
69 { "wave", alc_wave_init, EmptyFuncs },
71 { NULL, NULL, EmptyFuncs }
73 #undef EmptyFuncs
75 ///////////////////////////////////////////////////////
77 #define ALC_EFX_MAJOR_VERSION 0x20001
78 #define ALC_EFX_MINOR_VERSION 0x20002
79 #define ALC_MAX_AUXILIARY_SENDS 0x20003
81 ///////////////////////////////////////////////////////
82 // STRING and EXTENSIONS
84 typedef struct ALCfunction_struct
86 ALCchar *funcName;
87 ALvoid *address;
88 } ALCfunction;
90 static ALCfunction alcFunctions[] = {
91 { "alcCreateContext", (ALvoid *) alcCreateContext },
92 { "alcMakeContextCurrent", (ALvoid *) alcMakeContextCurrent },
93 { "alcProcessContext", (ALvoid *) alcProcessContext },
94 { "alcSuspendContext", (ALvoid *) alcSuspendContext },
95 { "alcDestroyContext", (ALvoid *) alcDestroyContext },
96 { "alcGetCurrentContext", (ALvoid *) alcGetCurrentContext },
97 { "alcGetContextsDevice", (ALvoid *) alcGetContextsDevice },
98 { "alcOpenDevice", (ALvoid *) alcOpenDevice },
99 { "alcCloseDevice", (ALvoid *) alcCloseDevice },
100 { "alcGetError", (ALvoid *) alcGetError },
101 { "alcIsExtensionPresent", (ALvoid *) alcIsExtensionPresent },
102 { "alcGetProcAddress", (ALvoid *) alcGetProcAddress },
103 { "alcGetEnumValue", (ALvoid *) alcGetEnumValue },
104 { "alcGetString", (ALvoid *) alcGetString },
105 { "alcGetIntegerv", (ALvoid *) alcGetIntegerv },
106 { "alcCaptureOpenDevice", (ALvoid *) alcCaptureOpenDevice },
107 { "alcCaptureCloseDevice", (ALvoid *) alcCaptureCloseDevice },
108 { "alcCaptureStart", (ALvoid *) alcCaptureStart },
109 { "alcCaptureStop", (ALvoid *) alcCaptureStop },
110 { "alcCaptureSamples", (ALvoid *) alcCaptureSamples },
111 { NULL, (ALvoid *) NULL }
114 static ALenums enumeration[]={
115 // Types
116 { (ALchar *)"ALC_INVALID", ALC_INVALID },
117 { (ALchar *)"ALC_FALSE", ALC_FALSE },
118 { (ALchar *)"ALC_TRUE", ALC_TRUE },
120 // ALC Properties
121 { (ALchar *)"ALC_MAJOR_VERSION", ALC_MAJOR_VERSION },
122 { (ALchar *)"ALC_MINOR_VERSION", ALC_MINOR_VERSION },
123 { (ALchar *)"ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE },
124 { (ALchar *)"ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES },
125 { (ALchar *)"ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER },
126 { (ALchar *)"ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER },
127 { (ALchar *)"ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER },
128 { (ALchar *)"ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER },
129 { (ALchar *)"ALC_EXTENSIONS", ALC_EXTENSIONS },
130 { (ALchar *)"ALC_FREQUENCY", ALC_FREQUENCY },
131 { (ALchar *)"ALC_REFRESH", ALC_REFRESH },
132 { (ALchar *)"ALC_SYNC", ALC_SYNC },
133 { (ALchar *)"ALC_MONO_SOURCES", ALC_MONO_SOURCES },
134 { (ALchar *)"ALC_STEREO_SOURCES", ALC_STEREO_SOURCES },
135 { (ALchar *)"ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER },
136 { (ALchar *)"ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER},
137 { (ALchar *)"ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES },
139 // EFX Properties
140 { (ALchar *)"ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION },
141 { (ALchar *)"ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION },
142 { (ALchar *)"ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS },
144 // ALC Error Message
145 { (ALchar *)"ALC_NO_ERROR", ALC_NO_ERROR },
146 { (ALchar *)"ALC_INVALID_DEVICE", ALC_INVALID_DEVICE },
147 { (ALchar *)"ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT },
148 { (ALchar *)"ALC_INVALID_ENUM", ALC_INVALID_ENUM },
149 { (ALchar *)"ALC_INVALID_VALUE", ALC_INVALID_VALUE },
150 { (ALchar *)"ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY },
151 { (ALchar *)NULL, (ALenum)0 }
153 // Error strings
154 static const ALCchar alcNoError[] = "No Error";
155 static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
156 static const ALCchar alcErrInvalidContext[] = "Invalid Context";
157 static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
158 static const ALCchar alcErrInvalidValue[] = "Invalid Value";
159 static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
161 // Context strings
162 static ALCchar alcDeviceList[2048];
163 static ALCchar alcAllDeviceList[2048];
164 static ALCchar alcCaptureDeviceList[2048];
165 // Default is always the first in the list
166 static ALCchar *alcDefaultDeviceSpecifier = alcDeviceList;
167 static ALCchar *alcDefaultAllDeviceSpecifier = alcAllDeviceList;
168 static ALCchar *alcCaptureDefaultDeviceSpecifier = alcCaptureDeviceList;
171 static ALCchar alcExtensionList[] = "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_EFX";
172 static ALCint alcMajorVersion = 1;
173 static ALCint alcMinorVersion = 1;
175 static ALCint alcEFXMajorVersion = 1;
176 static ALCint alcEFXMinorVersion = 0;
178 ///////////////////////////////////////////////////////
181 ///////////////////////////////////////////////////////
182 // Global Variables
184 static ALCdevice *g_pDeviceList = NULL;
185 static ALCuint g_ulDeviceCount = 0;
187 static CRITICAL_SECTION g_csMutex;
189 // Context List
190 static ALCcontext *g_pContextList = NULL;
191 static ALCuint g_ulContextCount = 0;
193 // Context Error
194 static ALCenum g_eLastContextError = ALC_NO_ERROR;
196 static ALboolean init_done = AL_FALSE;
198 ///////////////////////////////////////////////////////
201 ///////////////////////////////////////////////////////
202 // ALC Related helper functions
203 #ifdef _WIN32
204 BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
206 (void)lpReserved;
208 // Perform actions based on the reason for calling.
209 switch(ul_reason_for_call)
211 case DLL_PROCESS_ATTACH:
212 DisableThreadLibraryCalls(hModule);
213 break;
215 case DLL_PROCESS_DETACH:
216 if(!init_done)
217 break;
218 ReleaseALC();
219 ReleaseALBuffers();
220 ReleaseALEffects();
221 ReleaseALFilters();
222 FreeALConfig();
223 ALTHUNK_EXIT();
224 DeleteCriticalSection(&g_csMutex);
225 break;
227 return TRUE;
229 #else
230 #ifdef HAVE_GCC_DESTRUCTOR
231 static void my_deinit() __attribute__((destructor));
232 static void my_deinit()
234 static ALenum once = AL_FALSE;
235 if(once || !init_done) return;
236 once = AL_TRUE;
238 ReleaseALC();
239 ReleaseALBuffers();
240 ReleaseALEffects();
241 ReleaseALFilters();
242 FreeALConfig();
243 ALTHUNK_EXIT();
244 DeleteCriticalSection(&g_csMutex);
246 #endif
247 #endif
249 static void InitAL(void)
251 if(!init_done)
253 int i;
254 const char *devs, *str;
256 init_done = AL_TRUE;
258 InitializeCriticalSection(&g_csMutex);
259 ALTHUNK_INIT();
260 ReadALConfig();
262 devs = GetConfigValue(NULL, "drivers", "");
263 if(devs[0])
265 int n;
266 size_t len;
267 const char *next = devs;
269 i = 0;
271 do {
272 devs = next;
273 next = strchr(devs, ',');
275 if(!devs[0] || devs[0] == ',')
276 continue;
278 len = (next ? ((size_t)(next-devs)) : strlen(devs));
279 for(n = i;BackendList[n].Init;n++)
281 if(len == strlen(BackendList[n].name) &&
282 strncmp(BackendList[n].name, devs, len) == 0)
284 const char *name = BackendList[i].name;
285 void (*Init)(BackendFuncs*) = BackendList[i].Init;
287 BackendList[i].name = BackendList[n].name;
288 BackendList[i].Init = BackendList[n].Init;
290 BackendList[n].name = name;
291 BackendList[n].Init = Init;
293 i++;
296 } while(next++);
298 BackendList[i].name = NULL;
299 BackendList[i].Init = NULL;
302 for(i = 0;BackendList[i].Init;i++)
303 BackendList[i].Init(&BackendList[i].Funcs);
305 str = GetConfigValue(NULL, "stereodup", "false");
306 DuplicateStereo = (strcasecmp(str, "true") == 0 ||
307 strcasecmp(str, "yes") == 0 ||
308 strcasecmp(str, "on") == 0 ||
309 atoi(str) != 0);
311 str = GetConfigValue(NULL, "excludefx", "");
312 if(str[0])
314 const struct {
315 const char *name;
316 int type;
317 } EffectList[] = {
318 { "eaxreverb", EAXREVERB },
319 { "reverb", REVERB },
320 { "echo", ECHO },
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->NumSends = GetConfigValueInt(NULL, "sends", MAX_SENDS);
479 if(pContext->NumSends > MAX_SENDS)
480 pContext->NumSends = MAX_SENDS;
482 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";
484 level = GetConfigValueInt(NULL, "cf_level", 0);
485 if(level > 0 && level <= 6)
487 pContext->bs2b = calloc(1, sizeof(*pContext->bs2b));
488 bs2b_set_srate(pContext->bs2b, pContext->Frequency);
489 bs2b_set_level(pContext->bs2b, level);
492 aluInitPanning(pContext);
497 ExitContext
499 Clean up Context, destroy any remaining Sources
501 static ALCvoid ExitContext(ALCcontext *pContext)
503 //Invalidate context
504 pContext->LastError = AL_NO_ERROR;
505 pContext->InUse = AL_FALSE;
507 free(pContext->bs2b);
508 pContext->bs2b = NULL;
511 ///////////////////////////////////////////////////////
514 ///////////////////////////////////////////////////////
515 // ALC Functions calls
518 // This should probably move to another c file but for now ...
519 ALCAPI ALCdevice* ALCAPIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
521 ALCboolean DeviceFound = ALC_FALSE;
522 ALCdevice *pDevice = NULL;
523 ALCint i;
525 InitAL();
527 if(SampleSize <= 0)
529 SetALCError(ALC_INVALID_VALUE);
530 return NULL;
533 if(deviceName && !deviceName[0])
534 deviceName = NULL;
536 pDevice = malloc(sizeof(ALCdevice));
537 if (pDevice)
539 //Initialise device structure
540 memset(pDevice, 0, sizeof(ALCdevice));
542 //Validate device
543 pDevice->IsCaptureDevice = AL_TRUE;
545 pDevice->Frequency = frequency;
546 pDevice->Format = format;
548 for(i = 0;BackendList[i].Init;i++)
550 pDevice->Funcs = &BackendList[i].Funcs;
551 if(ALCdevice_OpenCapture(pDevice, deviceName, frequency, format, SampleSize))
553 SuspendContext(NULL);
554 pDevice->next = g_pDeviceList;
555 g_pDeviceList = pDevice;
556 g_ulDeviceCount++;
557 ProcessContext(NULL);
559 DeviceFound = ALC_TRUE;
560 break;
564 if(!DeviceFound)
566 SetALCError(ALC_INVALID_VALUE);
567 free(pDevice);
568 pDevice = NULL;
571 else
572 SetALCError(ALC_OUT_OF_MEMORY);
574 return pDevice;
577 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
579 ALCboolean bReturn = ALC_FALSE;
580 ALCdevice **list;
582 if ((pDevice)&&(pDevice->IsCaptureDevice))
584 SuspendContext(NULL);
586 list = &g_pDeviceList;
587 while(*list != pDevice)
588 list = &(*list)->next;
590 *list = (*list)->next;
591 g_ulDeviceCount--;
593 ProcessContext(NULL);
595 ALCdevice_CloseCapture(pDevice);
596 free(pDevice);
598 bReturn = ALC_TRUE;
600 else
601 SetALCError(ALC_INVALID_DEVICE);
603 return bReturn;
606 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
608 if ((pDevice)&&(pDevice->IsCaptureDevice))
609 ALCdevice_StartCapture(pDevice);
610 else
611 SetALCError(ALC_INVALID_DEVICE);
614 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
616 if ((pDevice)&&(pDevice->IsCaptureDevice))
617 ALCdevice_StopCapture(pDevice);
618 else
619 SetALCError(ALC_INVALID_DEVICE);
622 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
624 if ((pDevice) && (pDevice->IsCaptureDevice))
625 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
626 else
627 SetALCError(ALC_INVALID_DEVICE);
631 alcGetError
633 Return last ALC generated error code
635 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
637 ALCenum errorCode;
639 (void)device;
641 errorCode = g_eLastContextError;
642 g_eLastContextError = ALC_NO_ERROR;
643 return errorCode;
648 alcSuspendContext
650 Not functional
652 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
654 // Not a lot happens here !
655 (void)pContext;
660 alcProcessContext
662 Not functional
664 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
666 // Not a lot happens here !
667 (void)pContext;
672 alcGetString
674 Returns information about the Device, and error strings
676 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
678 const ALCchar *value = NULL;
680 InitAL();
682 switch (param)
684 case ALC_NO_ERROR:
685 value = alcNoError;
686 break;
688 case ALC_INVALID_ENUM:
689 value = alcErrInvalidEnum;
690 break;
692 case ALC_INVALID_VALUE:
693 value = alcErrInvalidValue;
694 break;
696 case ALC_INVALID_DEVICE:
697 value = alcErrInvalidDevice;
698 break;
700 case ALC_INVALID_CONTEXT:
701 value = alcErrInvalidContext;
702 break;
704 case ALC_OUT_OF_MEMORY:
705 value = alcErrOutOfMemory;
706 break;
708 case ALC_DEFAULT_DEVICE_SPECIFIER:
709 value = alcDefaultDeviceSpecifier;
710 break;
712 case ALC_DEVICE_SPECIFIER:
713 if (pDevice)
714 value = pDevice->szDeviceName;
715 else
716 value = alcDeviceList;
717 break;
719 case ALC_ALL_DEVICES_SPECIFIER:
720 value = alcAllDeviceList;
721 break;
723 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
724 value = alcDefaultAllDeviceSpecifier;
725 break;
727 case ALC_CAPTURE_DEVICE_SPECIFIER:
728 if (pDevice)
729 value = pDevice->szDeviceName;
730 else
731 value = alcCaptureDeviceList;
732 break;
734 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
735 value = alcCaptureDefaultDeviceSpecifier;
736 break;
738 case ALC_EXTENSIONS:
739 value = alcExtensionList;
740 break;
742 default:
743 SetALCError(ALC_INVALID_ENUM);
744 break;
747 return value;
752 alcGetIntegerv
754 Returns information about the Device and the version of Open AL
756 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
758 InitAL();
760 if ((device)&&(device->IsCaptureDevice))
762 SuspendContext(NULL);
764 // Capture device
765 switch (param)
767 case ALC_CAPTURE_SAMPLES:
768 if ((size) && (data))
769 *data = ALCdevice_AvailableSamples(device);
770 else
771 SetALCError(ALC_INVALID_VALUE);
772 break;
774 default:
775 SetALCError(ALC_INVALID_ENUM);
776 break;
779 ProcessContext(NULL);
781 else
783 if(data)
785 // Playback Device
786 switch (param)
788 case ALC_MAJOR_VERSION:
789 if(!size)
790 SetALCError(ALC_INVALID_VALUE);
791 else
792 *data = alcMajorVersion;
793 break;
795 case ALC_MINOR_VERSION:
796 if(!size)
797 SetALCError(ALC_INVALID_VALUE);
798 else
799 *data = alcMinorVersion;
800 break;
802 case ALC_EFX_MAJOR_VERSION:
803 if(!size)
804 SetALCError(ALC_INVALID_VALUE);
805 else
806 *data = alcEFXMajorVersion;
807 break;
809 case ALC_EFX_MINOR_VERSION:
810 if(!size)
811 SetALCError(ALC_INVALID_VALUE);
812 else
813 *data = alcEFXMinorVersion;
814 break;
816 case ALC_MAX_AUXILIARY_SENDS:
817 if(!size)
818 SetALCError(ALC_INVALID_VALUE);
819 else if(device && device->Context)
820 *data = device->Context->NumSends;
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++] = device->Context->NumSends;
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;
1027 ALuint RequestedSends;
1029 if ((device)&&(!device->IsCaptureDevice))
1031 // Reset Context Last Error code
1032 g_eLastContextError = ALC_NO_ERROR;
1034 // Current implementation only allows one Context per Device
1035 if(!device->Context)
1037 ALContext = calloc(1, sizeof(ALCcontext));
1038 if(!ALContext)
1040 SetALCError(ALC_OUT_OF_MEMORY);
1041 return NULL;
1044 ALContext->Device = device;
1045 InitContext(ALContext);
1047 device->Context = ALContext;
1049 SuspendContext(NULL);
1051 ALContext->next = g_pContextList;
1052 g_pContextList = ALContext;
1053 g_ulContextCount++;
1055 ProcessContext(NULL);
1057 // Check for attributes
1058 if (attrList)
1060 ulAttributeIndex = 0;
1061 while ((ulAttributeIndex < 10) && (attrList[ulAttributeIndex]))
1063 if (attrList[ulAttributeIndex] == ALC_STEREO_SOURCES)
1065 ulRequestedStereoSources = attrList[ulAttributeIndex + 1];
1067 if (ulRequestedStereoSources > ALContext->Device->MaxNoOfSources)
1068 ulRequestedStereoSources = ALContext->Device->MaxNoOfSources;
1070 ALContext->lNumStereoSources = ulRequestedStereoSources;
1071 ALContext->lNumMonoSources = ALContext->Device->MaxNoOfSources - ALContext->lNumStereoSources;
1074 if(attrList[ulAttributeIndex] == ALC_MAX_AUXILIARY_SENDS)
1076 RequestedSends = attrList[ulAttributeIndex + 1];
1078 if(RequestedSends > ALContext->NumSends)
1079 RequestedSends = ALContext->NumSends;
1081 ALContext->NumSends = RequestedSends;
1084 ulAttributeIndex += 2;
1088 else
1090 SetALCError(ALC_INVALID_VALUE);
1091 ALContext = NULL;
1094 else
1095 SetALCError(ALC_INVALID_DEVICE);
1097 return ALContext;
1102 alcDestroyContext
1104 Remove a Context
1106 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
1108 ALCcontext **list;
1110 InitAL();
1112 // Lock context list
1113 SuspendContext(NULL);
1115 if (IsContext(context))
1117 // Lock context
1118 SuspendContext(context);
1120 ReleaseALSources(context);
1121 ReleaseALAuxiliaryEffectSlots(context);
1123 context->Device->Context = NULL;
1125 list = &g_pContextList;
1126 while(*list != context)
1127 list = &(*list)->next;
1129 *list = (*list)->next;
1130 g_ulContextCount--;
1132 // Unlock context
1133 ProcessContext(context);
1135 ExitContext(context);
1137 // Free memory (MUST do this after ProcessContext)
1138 memset(context, 0, sizeof(ALCcontext));
1139 free(context);
1141 else
1142 SetALCError(ALC_INVALID_CONTEXT);
1144 ProcessContext(NULL);
1149 alcGetCurrentContext
1151 Returns the currently active Context
1153 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1155 ALCcontext *pContext = NULL;
1157 InitAL();
1159 SuspendContext(NULL);
1161 pContext = g_pContextList;
1162 while ((pContext) && (!pContext->InUse))
1163 pContext = pContext->next;
1165 ProcessContext(NULL);
1167 return pContext;
1172 alcGetContextsDevice
1174 Returns the Device that a particular Context is attached to
1176 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1178 ALCdevice *pDevice = NULL;
1180 InitAL();
1182 SuspendContext(NULL);
1183 if (IsContext(pContext))
1184 pDevice = pContext->Device;
1185 else
1186 SetALCError(ALC_INVALID_CONTEXT);
1187 ProcessContext(NULL);
1189 return pDevice;
1194 alcMakeContextCurrent
1196 Makes the given Context the active Context
1198 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1200 ALCcontext *ALContext;
1201 ALboolean bReturn = AL_TRUE;
1203 InitAL();
1205 SuspendContext(NULL);
1207 // context must be a valid Context or NULL
1208 if ((IsContext(context)) || (context == NULL))
1210 if ((ALContext=alcGetCurrentContext()))
1212 SuspendContext(ALContext);
1213 ALContext->InUse=AL_FALSE;
1214 ProcessContext(ALContext);
1217 if ((ALContext=context) && (ALContext->Device))
1219 SuspendContext(ALContext);
1220 ALContext->InUse=AL_TRUE;
1221 ProcessContext(ALContext);
1224 else
1226 SetALCError(ALC_INVALID_CONTEXT);
1227 bReturn = AL_FALSE;
1230 ProcessContext(NULL);
1232 return bReturn;
1237 alcOpenDevice
1239 Open the Device specified.
1241 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1243 ALboolean bDeviceFound = AL_FALSE;
1244 ALCdevice *device;
1245 ALint i;
1247 InitAL();
1249 if(deviceName && !deviceName[0])
1250 deviceName = NULL;
1252 device = malloc(sizeof(ALCdevice));
1253 if (device)
1255 const char *fmt;
1257 //Initialise device structure
1258 memset(device, 0, sizeof(ALCdevice));
1260 //Validate device
1261 device->IsCaptureDevice = AL_FALSE;
1263 //Set output format
1264 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1265 if((ALint)device->Frequency <= 0)
1266 device->Frequency = SWMIXER_OUTPUT_RATE;
1268 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1269 if(fmt[0])
1270 device->Format = alGetEnumValue(fmt);
1272 if(!aluChannelsFromFormat(device->Format))
1273 device->Format = AL_FORMAT_STEREO16;
1275 device->UpdateSize = GetConfigValueInt(NULL, "refresh", 4096);
1276 if((ALint)device->UpdateSize <= 0)
1277 device->UpdateSize = 4096;
1279 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1280 if((ALint)device->MaxNoOfSources <= 0)
1281 device->MaxNoOfSources = 256;
1283 device->AuxiliaryEffectSlotMax = GetConfigValueInt(NULL, "slots", 4);
1284 if((ALint)device->AuxiliaryEffectSlotMax <= 0)
1285 device->AuxiliaryEffectSlotMax = 4;
1287 // Find a playback device to open
1288 SuspendContext(NULL);
1289 for(i = 0;BackendList[i].Init;i++)
1291 device->Funcs = &BackendList[i].Funcs;
1292 if(ALCdevice_OpenPlayback(device, deviceName))
1294 device->next = g_pDeviceList;
1295 g_pDeviceList = device;
1296 g_ulDeviceCount++;
1298 bDeviceFound = AL_TRUE;
1299 break;
1302 ProcessContext(NULL);
1304 if (!bDeviceFound)
1306 // No suitable output device found
1307 SetALCError(ALC_INVALID_VALUE);
1308 free(device);
1309 device = NULL;
1312 else
1313 SetALCError(ALC_OUT_OF_MEMORY);
1315 return device;
1320 alcCloseDevice
1322 Close the specified Device
1324 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1326 ALCboolean bReturn = ALC_FALSE;
1327 ALCdevice **list;
1329 if ((pDevice)&&(!pDevice->IsCaptureDevice))
1331 SuspendContext(NULL);
1333 list = &g_pDeviceList;
1334 while(*list != pDevice)
1335 list = &(*list)->next;
1337 *list = (*list)->next;
1338 g_ulDeviceCount--;
1340 ProcessContext(NULL);
1342 if(pDevice->Context)
1344 #ifdef _DEBUG
1345 AL_PRINT("alcCloseDevice(): destroying 1 Context\n");
1346 #endif
1347 alcDestroyContext(pDevice->Context);
1349 ALCdevice_ClosePlayback(pDevice);
1351 //Release device structure
1352 memset(pDevice, 0, sizeof(ALCdevice));
1353 free(pDevice);
1355 bReturn = ALC_TRUE;
1357 else
1358 SetALCError(ALC_INVALID_DEVICE);
1360 return bReturn;
1364 ALCvoid ReleaseALC(ALCvoid)
1366 #ifdef _DEBUG
1367 if(g_ulDeviceCount > 0)
1368 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
1369 #endif
1371 while(g_pDeviceList)
1373 if(g_pDeviceList->IsCaptureDevice)
1374 alcCaptureCloseDevice(g_pDeviceList);
1375 else
1376 alcCloseDevice(g_pDeviceList);
1380 ///////////////////////////////////////////////////////