Add a method to deinitialize backends
[openal-soft.git] / Alc / ALc.c
bloba47a03d2bb024d58bc02a57130cc8c4dadf08eeb
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <math.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <memory.h>
27 #include <ctype.h>
28 #include "alMain.h"
29 #include "alSource.h"
30 #include "AL/al.h"
31 #include "AL/alc.h"
32 #include "alThunk.h"
33 #include "alSource.h"
34 #include "alBuffer.h"
35 #include "alExtension.h"
36 #include "alAuxEffectSlot.h"
37 #include "alDatabuffer.h"
38 #include "bs2b.h"
39 #include "alu.h"
42 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
43 static struct {
44 const char *name;
45 void (*Init)(BackendFuncs*);
46 void (*Deinit)(void);
47 BackendFuncs Funcs;
48 } BackendList[] = {
49 #ifdef HAVE_ALSA
50 { "alsa", alc_alsa_init, alc_alsa_deinit, EmptyFuncs },
51 #endif
52 #ifdef HAVE_OSS
53 { "oss", alc_oss_init, alc_oss_deinit, EmptyFuncs },
54 #endif
55 #ifdef HAVE_SOLARIS
56 { "solaris", alc_solaris_init, alc_solaris_deinit, EmptyFuncs },
57 #endif
58 #ifdef HAVE_DSOUND
59 { "dsound", alcDSoundInit, alcDSoundDeinit, EmptyFuncs },
60 #endif
61 #ifdef HAVE_WINMM
62 { "winmm", alcWinMMInit, alcWinMMDeinit, EmptyFuncs },
63 #endif
64 #ifdef HAVE_PORTAUDIO
65 { "port", alc_pa_init, alc_pa_deinit, EmptyFuncs },
66 #endif
67 #ifdef HAVE_PULSEAUDIO
68 { "pulse", alc_pulse_init, alc_pulse_deinit, EmptyFuncs },
69 #endif
71 { "wave", alc_wave_init, alc_wave_deinit, EmptyFuncs },
73 { NULL, 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_disconnect 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 int i;
210 (void)lpReserved;
212 // Perform actions based on the reason for calling.
213 switch(ul_reason_for_call)
215 case DLL_PROCESS_ATTACH:
216 DisableThreadLibraryCalls(hModule);
217 break;
219 case DLL_PROCESS_DETACH:
220 if(!init_done)
221 break;
222 ReleaseALC();
224 for(i = 0;BackendList[i].Deinit;i++)
225 BackendList[i].Deinit();
227 FreeALConfig();
228 ALTHUNK_EXIT();
229 DeleteCriticalSection(&g_csMutex);
230 break;
232 return TRUE;
234 #else
235 #ifdef HAVE_GCC_DESTRUCTOR
236 static void my_deinit() __attribute__((destructor));
237 static void my_deinit()
239 static ALenum once = AL_FALSE;
240 int i;
242 if(once || !init_done) return;
243 once = AL_TRUE;
245 ReleaseALC();
247 for(i = 0;BackendList[i].Deinit;i++)
248 BackendList[i].Deinit();
250 FreeALConfig();
251 ALTHUNK_EXIT();
252 DeleteCriticalSection(&g_csMutex);
254 #endif
255 #endif
257 static void InitAL(void)
259 if(!init_done)
261 int i;
262 const char *devs, *str;
264 init_done = AL_TRUE;
266 InitializeCriticalSection(&g_csMutex);
267 ALTHUNK_INIT();
268 ReadALConfig();
270 devs = GetConfigValue(NULL, "drivers", "");
271 if(devs[0])
273 int n;
274 size_t len;
275 const char *next = devs;
277 i = 0;
279 do {
280 devs = next;
281 next = strchr(devs, ',');
283 if(!devs[0] || devs[0] == ',')
284 continue;
286 len = (next ? ((size_t)(next-devs)) : strlen(devs));
287 for(n = i;BackendList[n].Init;n++)
289 if(len == strlen(BackendList[n].name) &&
290 strncmp(BackendList[n].name, devs, len) == 0)
292 const char *name = BackendList[i].name;
293 void (*Init)(BackendFuncs*) = BackendList[i].Init;
295 BackendList[i].name = BackendList[n].name;
296 BackendList[i].Init = BackendList[n].Init;
298 BackendList[n].name = name;
299 BackendList[n].Init = Init;
301 i++;
304 } while(next++);
306 BackendList[i].name = NULL;
307 BackendList[i].Init = NULL;
310 for(i = 0;BackendList[i].Init;i++)
311 BackendList[i].Init(&BackendList[i].Funcs);
313 str = GetConfigValue(NULL, "stereodup", "false");
314 DuplicateStereo = (strcasecmp(str, "true") == 0 ||
315 strcasecmp(str, "yes") == 0 ||
316 strcasecmp(str, "on") == 0 ||
317 atoi(str) != 0);
319 str = GetConfigValue(NULL, "excludefx", "");
320 if(str[0])
322 const struct {
323 const char *name;
324 int type;
325 } EffectList[] = {
326 { "eaxreverb", EAXREVERB },
327 { "reverb", REVERB },
328 { "echo", ECHO },
329 { NULL, 0 }
331 int n;
332 size_t len;
333 const char *next = str;
335 do {
336 str = next;
337 next = strchr(str, ',');
339 if(!str[0] || next == str)
340 continue;
342 len = (next ? ((size_t)(next-str)) : strlen(str));
343 for(n = 0;EffectList[n].name;n++)
345 if(len == strlen(EffectList[n].name) &&
346 strncmp(EffectList[n].name, str, len) == 0)
347 DisabledEffects[EffectList[n].type] = AL_TRUE;
349 } while(next++);
354 ALCchar *AppendDeviceList(char *name)
356 static size_t pos;
357 ALCchar *ret = alcDeviceList+pos;
358 if(pos >= sizeof(alcDeviceList))
360 AL_PRINT("Not enough room to add %s!\n", name);
361 return alcDeviceList + sizeof(alcDeviceList) - 1;
363 pos += snprintf(alcDeviceList+pos, sizeof(alcDeviceList)-pos-1, "%s", name) + 1;
364 return ret;
367 ALCchar *AppendAllDeviceList(char *name)
369 static size_t pos;
370 ALCchar *ret = alcAllDeviceList+pos;
371 if(pos >= sizeof(alcAllDeviceList))
373 AL_PRINT("Not enough room to add %s!\n", name);
374 return alcAllDeviceList + sizeof(alcAllDeviceList) - 1;
376 pos += snprintf(alcAllDeviceList+pos, sizeof(alcAllDeviceList)-pos-1, "%s", name) + 1;
377 return ret;
380 ALCchar *AppendCaptureDeviceList(char *name)
382 static size_t pos;
383 ALCchar *ret = alcCaptureDeviceList+pos;
384 if(pos >= sizeof(alcCaptureDeviceList))
386 AL_PRINT("Not enough room to add %s!\n", name);
387 return alcCaptureDeviceList + sizeof(alcCaptureDeviceList) - 1;
389 pos += snprintf(alcCaptureDeviceList+pos, sizeof(alcCaptureDeviceList)-pos-1, "%s", name) + 1;
390 return ret;
394 IsDevice
396 Check pDevice is a valid Device pointer
398 static ALCboolean IsDevice(ALCdevice *pDevice)
400 ALCdevice *pTempDevice;
402 SuspendContext(NULL);
404 pTempDevice = g_pDeviceList;
405 while(pTempDevice && pTempDevice != pDevice)
406 pTempDevice = pTempDevice->next;
408 ProcessContext(NULL);
410 return (pTempDevice ? ALC_TRUE : ALC_FALSE);
414 IsContext
416 Check pContext is a valid Context pointer
418 static ALCboolean IsContext(ALCcontext *pContext)
420 ALCcontext *pTempContext;
422 SuspendContext(NULL);
424 pTempContext = g_pContextList;
425 while (pTempContext && pTempContext != pContext)
426 pTempContext = pTempContext->next;
428 ProcessContext(NULL);
430 return (pTempContext ? ALC_TRUE : ALC_FALSE);
435 SetALCError
437 Store latest ALC Error
439 ALCvoid SetALCError(ALenum errorCode)
441 g_eLastContextError = errorCode;
446 SuspendContext
448 Thread-safe entry
450 ALCvoid SuspendContext(ALCcontext *pContext)
452 (void)pContext;
453 EnterCriticalSection(&g_csMutex);
458 ProcessContext
460 Thread-safe exit
462 ALCvoid ProcessContext(ALCcontext *pContext)
464 (void)pContext;
465 LeaveCriticalSection(&g_csMutex);
470 GetContextSuspended
472 Returns the currently active Context, in a locked state
474 ALCcontext *GetContextSuspended(void)
476 ALCcontext *pContext = NULL;
478 SuspendContext(NULL);
480 pContext = g_pContextList;
481 while(pContext && !pContext->InUse)
482 pContext = pContext->next;
484 if(pContext)
485 SuspendContext(pContext);
487 ProcessContext(NULL);
489 return pContext;
494 InitContext
496 Initialize Context variables
498 static ALvoid InitContext(ALCcontext *pContext)
500 int level;
502 //Initialise listener
503 pContext->Listener.Gain = 1.0f;
504 pContext->Listener.MetersPerUnit = 1.0f;
505 pContext->Listener.Position[0] = 0.0f;
506 pContext->Listener.Position[1] = 0.0f;
507 pContext->Listener.Position[2] = 0.0f;
508 pContext->Listener.Velocity[0] = 0.0f;
509 pContext->Listener.Velocity[1] = 0.0f;
510 pContext->Listener.Velocity[2] = 0.0f;
511 pContext->Listener.Forward[0] = 0.0f;
512 pContext->Listener.Forward[1] = 0.0f;
513 pContext->Listener.Forward[2] = -1.0f;
514 pContext->Listener.Up[0] = 0.0f;
515 pContext->Listener.Up[1] = 1.0f;
516 pContext->Listener.Up[2] = 0.0f;
518 //Validate pContext
519 pContext->LastError = AL_NO_ERROR;
520 pContext->InUse = AL_FALSE;
522 //Set output format
523 pContext->Frequency = pContext->Device->Frequency;
525 //Set globals
526 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
527 pContext->DopplerFactor = 1.0f;
528 pContext->DopplerVelocity = 1.0f;
529 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
531 pContext->ExtensionList = "AL_EXTX_buffer_sub_data AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_OFFSET AL_EXTX_sample_buffer_object AL_EXTX_source_distance_model AL_LOKI_quadriphonic";
533 level = GetConfigValueInt(NULL, "cf_level", 0);
534 if(level > 0 && level <= 6)
536 pContext->bs2b = calloc(1, sizeof(*pContext->bs2b));
537 bs2b_set_srate(pContext->bs2b, pContext->Frequency);
538 bs2b_set_level(pContext->bs2b, level);
541 aluInitPanning(pContext);
546 ExitContext
548 Clean up Context, destroy any remaining Sources
550 static ALCvoid ExitContext(ALCcontext *pContext)
552 //Invalidate context
553 pContext->LastError = AL_NO_ERROR;
554 pContext->InUse = AL_FALSE;
556 free(pContext->bs2b);
557 pContext->bs2b = NULL;
560 ///////////////////////////////////////////////////////
563 ///////////////////////////////////////////////////////
564 // ALC Functions calls
567 // This should probably move to another c file but for now ...
568 ALCAPI ALCdevice* ALCAPIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
570 ALCboolean DeviceFound = ALC_FALSE;
571 ALCdevice *pDevice = NULL;
572 ALCint i;
574 InitAL();
576 if(SampleSize <= 0)
578 SetALCError(ALC_INVALID_VALUE);
579 return NULL;
582 if(deviceName && !deviceName[0])
583 deviceName = NULL;
585 pDevice = malloc(sizeof(ALCdevice));
586 if (pDevice)
588 //Initialise device structure
589 memset(pDevice, 0, sizeof(ALCdevice));
591 //Validate device
592 pDevice->Connected = ALC_TRUE;
593 pDevice->IsCaptureDevice = AL_TRUE;
595 pDevice->Frequency = frequency;
596 pDevice->Format = format;
597 pDevice->BufferSize = SampleSize;
599 SuspendContext(NULL);
600 for(i = 0;BackendList[i].Init;i++)
602 pDevice->Funcs = &BackendList[i].Funcs;
603 if(ALCdevice_OpenCapture(pDevice, deviceName))
605 pDevice->next = g_pDeviceList;
606 g_pDeviceList = pDevice;
607 g_ulDeviceCount++;
609 DeviceFound = ALC_TRUE;
610 break;
613 ProcessContext(NULL);
615 if(!DeviceFound)
617 SetALCError(ALC_INVALID_VALUE);
618 free(pDevice);
619 pDevice = NULL;
622 else
623 SetALCError(ALC_OUT_OF_MEMORY);
625 return pDevice;
628 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
630 ALCboolean bReturn = ALC_FALSE;
631 ALCdevice **list;
633 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
635 SuspendContext(NULL);
637 list = &g_pDeviceList;
638 while(*list != pDevice)
639 list = &(*list)->next;
641 *list = (*list)->next;
642 g_ulDeviceCount--;
644 ProcessContext(NULL);
646 ALCdevice_CloseCapture(pDevice);
647 free(pDevice);
649 bReturn = ALC_TRUE;
651 else
652 SetALCError(ALC_INVALID_DEVICE);
654 return bReturn;
657 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
659 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
660 ALCdevice_StartCapture(pDevice);
661 else
662 SetALCError(ALC_INVALID_DEVICE);
665 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
667 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
668 ALCdevice_StopCapture(pDevice);
669 else
670 SetALCError(ALC_INVALID_DEVICE);
673 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
675 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
676 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
677 else
678 SetALCError(ALC_INVALID_DEVICE);
682 alcGetError
684 Return last ALC generated error code
686 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
688 ALCenum errorCode;
690 (void)device;
692 errorCode = g_eLastContextError;
693 g_eLastContextError = ALC_NO_ERROR;
694 return errorCode;
699 alcSuspendContext
701 Not functional
703 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
705 // Not a lot happens here !
706 (void)pContext;
711 alcProcessContext
713 Not functional
715 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
717 // Not a lot happens here !
718 (void)pContext;
723 alcGetString
725 Returns information about the Device, and error strings
727 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
729 const ALCchar *value = NULL;
731 InitAL();
733 switch (param)
735 case ALC_NO_ERROR:
736 value = alcNoError;
737 break;
739 case ALC_INVALID_ENUM:
740 value = alcErrInvalidEnum;
741 break;
743 case ALC_INVALID_VALUE:
744 value = alcErrInvalidValue;
745 break;
747 case ALC_INVALID_DEVICE:
748 value = alcErrInvalidDevice;
749 break;
751 case ALC_INVALID_CONTEXT:
752 value = alcErrInvalidContext;
753 break;
755 case ALC_OUT_OF_MEMORY:
756 value = alcErrOutOfMemory;
757 break;
759 case ALC_DEFAULT_DEVICE_SPECIFIER:
760 value = alcDefaultDeviceSpecifier;
761 break;
763 case ALC_DEVICE_SPECIFIER:
764 if(IsDevice(pDevice))
765 value = pDevice->szDeviceName;
766 else
767 value = alcDeviceList;
768 break;
770 case ALC_ALL_DEVICES_SPECIFIER:
771 value = alcAllDeviceList;
772 break;
774 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
775 value = alcDefaultAllDeviceSpecifier;
776 break;
778 case ALC_CAPTURE_DEVICE_SPECIFIER:
779 if(IsDevice(pDevice))
780 value = pDevice->szDeviceName;
781 else
782 value = alcCaptureDeviceList;
783 break;
785 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
786 value = alcCaptureDefaultDeviceSpecifier;
787 break;
789 case ALC_EXTENSIONS:
790 value = alcExtensionList;
791 break;
793 default:
794 SetALCError(ALC_INVALID_ENUM);
795 break;
798 return value;
803 alcGetIntegerv
805 Returns information about the Device and the version of Open AL
807 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
809 InitAL();
811 if(IsDevice(device) && device->IsCaptureDevice)
813 SuspendContext(NULL);
815 // Capture device
816 switch (param)
818 case ALC_CAPTURE_SAMPLES:
819 if ((size) && (data))
820 *data = ALCdevice_AvailableSamples(device);
821 else
822 SetALCError(ALC_INVALID_VALUE);
823 break;
825 case ALC_CONNECTED:
826 if(size <= 0)
827 SetALCError(ALC_INVALID_VALUE);
828 else
829 *data = device->Connected;
830 break;
832 default:
833 SetALCError(ALC_INVALID_ENUM);
834 break;
837 ProcessContext(NULL);
839 else
841 if(data)
843 // Playback Device
844 switch (param)
846 case ALC_MAJOR_VERSION:
847 if(size <= 0)
848 SetALCError(ALC_INVALID_VALUE);
849 else
850 *data = alcMajorVersion;
851 break;
853 case ALC_MINOR_VERSION:
854 if(size <= 0)
855 SetALCError(ALC_INVALID_VALUE);
856 else
857 *data = alcMinorVersion;
858 break;
860 case ALC_EFX_MAJOR_VERSION:
861 if(size <= 0)
862 SetALCError(ALC_INVALID_VALUE);
863 else
864 *data = alcEFXMajorVersion;
865 break;
867 case ALC_EFX_MINOR_VERSION:
868 if(size <= 0)
869 SetALCError(ALC_INVALID_VALUE);
870 else
871 *data = alcEFXMinorVersion;
872 break;
874 case ALC_MAX_AUXILIARY_SENDS:
875 if(size <= 0)
876 SetALCError(ALC_INVALID_VALUE);
877 else
878 *data = (device?device->NumAuxSends:MAX_SENDS);
879 break;
881 case ALC_ATTRIBUTES_SIZE:
882 if(!IsDevice(device))
883 SetALCError(ALC_INVALID_DEVICE);
884 else if(size <= 0)
885 SetALCError(ALC_INVALID_VALUE);
886 else
887 *data = 13;
888 break;
890 case ALC_ALL_ATTRIBUTES:
891 if(!IsDevice(device))
892 SetALCError(ALC_INVALID_DEVICE);
893 else if (size < 13)
894 SetALCError(ALC_INVALID_VALUE);
895 else
897 int i = 0;
899 SuspendContext(NULL);
900 data[i++] = ALC_FREQUENCY;
901 data[i++] = device->Frequency;
903 data[i++] = ALC_REFRESH;
904 data[i++] = device->Frequency / device->UpdateSize;
906 data[i++] = ALC_SYNC;
907 data[i++] = ALC_FALSE;
909 data[i++] = ALC_MONO_SOURCES;
910 data[i++] = device->lNumMonoSources;
912 data[i++] = ALC_STEREO_SOURCES;
913 data[i++] = device->lNumStereoSources;
915 data[i++] = ALC_MAX_AUXILIARY_SENDS;
916 data[i++] = device->NumAuxSends;
918 data[i++] = 0;
919 ProcessContext(NULL);
921 break;
923 case ALC_FREQUENCY:
924 if(!IsDevice(device))
925 SetALCError(ALC_INVALID_DEVICE);
926 else if(size <= 0)
927 SetALCError(ALC_INVALID_VALUE);
928 else
929 *data = device->Frequency;
930 break;
932 case ALC_REFRESH:
933 if(!IsDevice(device))
934 SetALCError(ALC_INVALID_DEVICE);
935 else if(size <= 0)
936 SetALCError(ALC_INVALID_VALUE);
937 else
938 *data = device->Frequency / device->UpdateSize;
939 break;
941 case ALC_SYNC:
942 if(!IsDevice(device))
943 SetALCError(ALC_INVALID_DEVICE);
944 else if(size <= 0)
945 SetALCError(ALC_INVALID_VALUE);
946 else
947 *data = ALC_FALSE;
948 break;
950 case ALC_MONO_SOURCES:
951 if(!IsDevice(device))
952 SetALCError(ALC_INVALID_DEVICE);
953 else if(size <= 0)
954 SetALCError(ALC_INVALID_VALUE);
955 else
956 *data = device->lNumMonoSources;
957 break;
959 case ALC_STEREO_SOURCES:
960 if(!IsDevice(device))
961 SetALCError(ALC_INVALID_DEVICE);
962 else if(size <= 0)
963 SetALCError(ALC_INVALID_VALUE);
964 else
965 *data = device->lNumStereoSources;
966 break;
968 case ALC_CONNECTED:
969 if(!IsDevice(device))
970 SetALCError(ALC_INVALID_DEVICE);
971 else if(size <= 0)
972 SetALCError(ALC_INVALID_VALUE);
973 else
974 *data = device->Connected;
975 break;
977 default:
978 SetALCError(ALC_INVALID_ENUM);
979 break;
982 else if(size)
983 SetALCError(ALC_INVALID_VALUE);
986 return;
991 alcIsExtensionPresent
993 Determines if there is support for a particular extension
995 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
997 ALCboolean bResult = ALC_FALSE;
999 (void)device;
1001 if (extName)
1003 const char *ptr;
1004 size_t len;
1006 len = strlen(extName);
1007 ptr = alcExtensionList;
1008 while(ptr && *ptr)
1010 if(strncasecmp(ptr, extName, len) == 0 &&
1011 (ptr[len] == '\0' || isspace(ptr[len])))
1013 bResult = ALC_TRUE;
1014 break;
1016 if((ptr=strchr(ptr, ' ')) != NULL)
1018 do {
1019 ++ptr;
1020 } while(isspace(*ptr));
1024 else
1025 SetALCError(ALC_INVALID_VALUE);
1027 return bResult;
1032 alcGetProcAddress
1034 Retrieves the function address for a particular extension function
1036 ALCAPI ALCvoid * ALCAPIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
1038 ALCvoid *pFunction = NULL;
1039 ALsizei i = 0;
1041 (void)device;
1043 if (funcName)
1045 while(alcFunctions[i].funcName &&
1046 strcmp(alcFunctions[i].funcName,funcName) != 0)
1047 i++;
1048 pFunction = alcFunctions[i].address;
1050 else
1051 SetALCError(ALC_INVALID_VALUE);
1053 return pFunction;
1058 alcGetEnumValue
1060 Get the value for a particular ALC Enumerated Value
1062 ALCAPI ALCenum ALCAPIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
1064 ALsizei i = 0;
1065 ALCenum val;
1067 (void)device;
1069 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
1070 i++;
1071 val = enumeration[i].value;
1073 if(!enumeration[i].enumName)
1074 SetALCError(ALC_INVALID_VALUE);
1076 return val;
1081 alcCreateContext
1083 Create and attach a Context to a particular Device.
1085 ALCAPI ALCcontext* ALCAPIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
1087 ALCcontext *ALContext = NULL;
1088 ALuint ulAttributeIndex, ulRequestedStereoSources;
1089 ALuint RequestedSends;
1091 if(IsDevice(device) && !device->IsCaptureDevice && device->Connected)
1093 // Reset Context Last Error code
1094 g_eLastContextError = ALC_NO_ERROR;
1096 // Current implementation only allows one Context per Device
1097 if(!device->Context)
1099 ALContext = calloc(1, sizeof(ALCcontext));
1100 if(!ALContext)
1102 SetALCError(ALC_OUT_OF_MEMORY);
1103 return NULL;
1106 SuspendContext(NULL);
1108 ALContext->Device = device;
1109 InitContext(ALContext);
1111 device->Context = ALContext;
1113 ALContext->next = g_pContextList;
1114 g_pContextList = ALContext;
1115 g_ulContextCount++;
1117 // Check for attributes
1118 if (attrList)
1120 ALCuint freq = device->Frequency;
1121 ALCint numMono = device->lNumMonoSources;
1122 ALCint numStereo = device->lNumStereoSources;
1123 ALCuint numSends = device->NumAuxSends;
1125 ulAttributeIndex = 0;
1126 while ((ulAttributeIndex < 10) && (attrList[ulAttributeIndex]))
1128 if(attrList[ulAttributeIndex] == ALC_FREQUENCY)
1130 freq = attrList[ulAttributeIndex + 1];
1131 if(freq == 0)
1132 freq = device->Frequency;
1135 if(attrList[ulAttributeIndex] == ALC_STEREO_SOURCES)
1137 ulRequestedStereoSources = attrList[ulAttributeIndex + 1];
1139 if (ulRequestedStereoSources > device->MaxNoOfSources)
1140 ulRequestedStereoSources = device->MaxNoOfSources;
1142 numStereo = ulRequestedStereoSources;
1143 numMono = device->MaxNoOfSources - numStereo;
1146 if(attrList[ulAttributeIndex] == ALC_MAX_AUXILIARY_SENDS)
1148 RequestedSends = attrList[ulAttributeIndex + 1];
1150 if(RequestedSends > device->NumAuxSends)
1151 RequestedSends = device->NumAuxSends;
1153 numSends = RequestedSends;
1156 ulAttributeIndex += 2;
1159 device->Frequency = GetConfigValueInt(NULL, "frequency", freq);
1160 device->lNumMonoSources = numMono;
1161 device->lNumStereoSources = numStereo;
1162 device->NumAuxSends = numSends;
1165 if(ALCdevice_StartContext(device, ALContext) == ALC_FALSE)
1167 alcDestroyContext(ALContext);
1168 ALContext = NULL;
1169 SetALCError(ALC_INVALID_VALUE);
1171 else
1172 ALContext->Frequency = device->Frequency;
1174 ProcessContext(NULL);
1176 else
1178 SetALCError(ALC_INVALID_VALUE);
1179 ALContext = NULL;
1182 else
1183 SetALCError(ALC_INVALID_DEVICE);
1185 return ALContext;
1190 alcDestroyContext
1192 Remove a Context
1194 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
1196 ALCcontext **list;
1198 InitAL();
1200 if (IsContext(context))
1202 ALCdevice_StopContext(context->Device, context);
1204 // Lock context
1205 SuspendContext(context);
1207 if(context->SourceCount > 0)
1209 #ifdef _DEBUG
1210 AL_PRINT("alcDestroyContext(): deleting %d Source(s)\n", context->SourceCount);
1211 #endif
1212 ReleaseALSources(context);
1214 if(context->AuxiliaryEffectSlotCount > 0)
1216 #ifdef _DEBUG
1217 AL_PRINT("alcDestroyContext(): deleting %d AuxiliaryEffectSlot(s)\n", context->AuxiliaryEffectSlotCount);
1218 #endif
1219 ReleaseALAuxiliaryEffectSlots(context);
1222 context->Device->Context = NULL;
1224 list = &g_pContextList;
1225 while(*list != context)
1226 list = &(*list)->next;
1228 *list = (*list)->next;
1229 g_ulContextCount--;
1231 // Unlock context
1232 ProcessContext(context);
1234 ExitContext(context);
1236 // Free memory (MUST do this after ProcessContext)
1237 memset(context, 0, sizeof(ALCcontext));
1238 free(context);
1240 else
1241 SetALCError(ALC_INVALID_CONTEXT);
1246 alcGetCurrentContext
1248 Returns the currently active Context
1250 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1252 ALCcontext *pContext = NULL;
1254 InitAL();
1256 SuspendContext(NULL);
1258 pContext = g_pContextList;
1259 while ((pContext) && (!pContext->InUse))
1260 pContext = pContext->next;
1262 ProcessContext(NULL);
1264 return pContext;
1269 alcGetContextsDevice
1271 Returns the Device that a particular Context is attached to
1273 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1275 ALCdevice *pDevice = NULL;
1277 InitAL();
1279 SuspendContext(NULL);
1280 if (IsContext(pContext))
1281 pDevice = pContext->Device;
1282 else
1283 SetALCError(ALC_INVALID_CONTEXT);
1284 ProcessContext(NULL);
1286 return pDevice;
1291 alcMakeContextCurrent
1293 Makes the given Context the active Context
1295 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1297 ALCcontext *ALContext;
1298 ALboolean bReturn = AL_TRUE;
1300 InitAL();
1302 SuspendContext(NULL);
1304 // context must be a valid Context or NULL
1305 if(context == NULL || IsContext(context))
1307 if((ALContext=GetContextSuspended()) != NULL)
1309 ALContext->InUse=AL_FALSE;
1310 ProcessContext(ALContext);
1313 if((ALContext=context) != NULL && ALContext->Device)
1315 SuspendContext(ALContext);
1316 ALContext->InUse=AL_TRUE;
1317 ProcessContext(ALContext);
1320 else
1322 SetALCError(ALC_INVALID_CONTEXT);
1323 bReturn = AL_FALSE;
1326 ProcessContext(NULL);
1328 return bReturn;
1332 static ALenum GetFormatFromString(const char *str)
1334 if(strcasecmp(str, "AL_FORMAT_MONO32") == 0) return AL_FORMAT_MONO_FLOAT32;
1335 if(strcasecmp(str, "AL_FORMAT_STEREO32") == 0) return AL_FORMAT_STEREO_FLOAT32;
1336 if(strcasecmp(str, "AL_FORMAT_QUAD32") == 0) return AL_FORMAT_QUAD32;
1337 if(strcasecmp(str, "AL_FORMAT_51CHN32") == 0) return AL_FORMAT_51CHN32;
1338 if(strcasecmp(str, "AL_FORMAT_61CHN32") == 0) return AL_FORMAT_61CHN32;
1339 if(strcasecmp(str, "AL_FORMAT_71CHN32") == 0) return AL_FORMAT_71CHN32;
1341 if(strcasecmp(str, "AL_FORMAT_MONO16") == 0) return AL_FORMAT_MONO16;
1342 if(strcasecmp(str, "AL_FORMAT_STEREO16") == 0) return AL_FORMAT_STEREO16;
1343 if(strcasecmp(str, "AL_FORMAT_QUAD16") == 0) return AL_FORMAT_QUAD16;
1344 if(strcasecmp(str, "AL_FORMAT_51CHN16") == 0) return AL_FORMAT_51CHN16;
1345 if(strcasecmp(str, "AL_FORMAT_61CHN16") == 0) return AL_FORMAT_61CHN16;
1346 if(strcasecmp(str, "AL_FORMAT_71CHN16") == 0) return AL_FORMAT_71CHN16;
1348 if(strcasecmp(str, "AL_FORMAT_MONO8") == 0) return AL_FORMAT_MONO8;
1349 if(strcasecmp(str, "AL_FORMAT_STEREO8") == 0) return AL_FORMAT_STEREO8;
1350 if(strcasecmp(str, "AL_FORMAT_QUAD8") == 0) return AL_FORMAT_QUAD8;
1351 if(strcasecmp(str, "AL_FORMAT_51CHN8") == 0) return AL_FORMAT_51CHN8;
1352 if(strcasecmp(str, "AL_FORMAT_61CHN8") == 0) return AL_FORMAT_61CHN8;
1353 if(strcasecmp(str, "AL_FORMAT_71CHN8") == 0) return AL_FORMAT_71CHN8;
1355 AL_PRINT("Unknown format: \"%s\"\n", str);
1356 return AL_FORMAT_STEREO16;
1360 alcOpenDevice
1362 Open the Device specified.
1364 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1366 ALboolean bDeviceFound = AL_FALSE;
1367 ALCdevice *device;
1368 ALint i;
1370 InitAL();
1372 if(deviceName && !deviceName[0])
1373 deviceName = NULL;
1375 device = malloc(sizeof(ALCdevice));
1376 if (device)
1378 const char *fmt;
1380 //Initialise device structure
1381 memset(device, 0, sizeof(ALCdevice));
1383 //Validate device
1384 device->Connected = ALC_TRUE;
1385 device->IsCaptureDevice = AL_FALSE;
1387 //Set output format
1388 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1389 if(device->Frequency == 0)
1390 device->Frequency = SWMIXER_OUTPUT_RATE;
1392 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1393 device->Format = GetFormatFromString(fmt);
1395 device->BufferSize = GetConfigValueInt(NULL, "refresh", 4096);
1396 if((ALint)device->BufferSize <= 0)
1397 device->BufferSize = 4096;
1399 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1400 if((ALint)device->MaxNoOfSources <= 0)
1401 device->MaxNoOfSources = 256;
1403 device->AuxiliaryEffectSlotMax = GetConfigValueInt(NULL, "slots", 4);
1404 if((ALint)device->AuxiliaryEffectSlotMax <= 0)
1405 device->AuxiliaryEffectSlotMax = 4;
1407 device->lNumStereoSources = 1;
1408 device->lNumMonoSources = device->MaxNoOfSources - device->lNumStereoSources;
1410 device->NumAuxSends = GetConfigValueInt(NULL, "sends", MAX_SENDS);
1411 if(device->NumAuxSends > MAX_SENDS)
1412 device->NumAuxSends = MAX_SENDS;
1414 // Find a playback device to open
1415 SuspendContext(NULL);
1416 for(i = 0;BackendList[i].Init;i++)
1418 device->Funcs = &BackendList[i].Funcs;
1419 if(ALCdevice_OpenPlayback(device, deviceName))
1421 device->next = g_pDeviceList;
1422 g_pDeviceList = device;
1423 g_ulDeviceCount++;
1425 bDeviceFound = AL_TRUE;
1426 break;
1429 ProcessContext(NULL);
1431 if (!bDeviceFound)
1433 // No suitable output device found
1434 SetALCError(ALC_INVALID_VALUE);
1435 free(device);
1436 device = NULL;
1439 else
1440 SetALCError(ALC_OUT_OF_MEMORY);
1442 return device;
1447 alcCloseDevice
1449 Close the specified Device
1451 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1453 ALCboolean bReturn = ALC_FALSE;
1454 ALCdevice **list;
1456 if(IsDevice(pDevice) && !pDevice->IsCaptureDevice)
1458 SuspendContext(NULL);
1460 list = &g_pDeviceList;
1461 while(*list != pDevice)
1462 list = &(*list)->next;
1464 *list = (*list)->next;
1465 g_ulDeviceCount--;
1467 ProcessContext(NULL);
1469 if(pDevice->Context)
1471 #ifdef _DEBUG
1472 AL_PRINT("alcCloseDevice(): destroying 1 Context\n");
1473 #endif
1474 alcDestroyContext(pDevice->Context);
1476 ALCdevice_ClosePlayback(pDevice);
1478 if(pDevice->BufferCount > 0)
1480 #ifdef _DEBUG
1481 AL_PRINT("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice->BufferCount);
1482 #endif
1483 ReleaseALBuffers(pDevice);
1485 if(pDevice->EffectCount > 0)
1487 #ifdef _DEBUG
1488 AL_PRINT("alcCloseDevice(): deleting %d Effect(s)\n", pDevice->EffectCount);
1489 #endif
1490 ReleaseALEffects(pDevice);
1492 if(pDevice->FilterCount > 0)
1494 #ifdef _DEBUG
1495 AL_PRINT("alcCloseDevice(): deleting %d Filter(s)\n", pDevice->FilterCount);
1496 #endif
1497 ReleaseALFilters(pDevice);
1499 if(pDevice->DatabufferCount > 0)
1501 #ifdef _DEBUG
1502 AL_PRINT("alcCloseDevice(): deleting %d Databuffer(s)\n", pDevice->DatabufferCount);
1503 #endif
1504 ReleaseALDatabuffers(pDevice);
1507 //Release device structure
1508 memset(pDevice, 0, sizeof(ALCdevice));
1509 free(pDevice);
1511 bReturn = ALC_TRUE;
1513 else
1514 SetALCError(ALC_INVALID_DEVICE);
1516 return bReturn;
1520 ALCvoid ReleaseALC(ALCvoid)
1522 #ifdef _DEBUG
1523 if(g_ulDeviceCount > 0)
1524 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
1525 #endif
1527 while(g_pDeviceList)
1529 if(g_pDeviceList->IsCaptureDevice)
1530 alcCaptureCloseDevice(g_pDeviceList);
1531 else
1532 alcCloseDevice(g_pDeviceList);
1536 ///////////////////////////////////////////////////////