Do not open pulseaudio if it didn't load
[openal-soft.git] / Alc / ALc.c
blob749e1d4946e0f351697e2deaf0afaebf1779a5a1
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
74 #ifdef HAVE_PULSEAUDIO
75 { "pulse", alc_pulse_init, EmptyFuncs },
76 #endif
78 { "wave", alc_wave_init, EmptyFuncs },
80 { NULL, NULL, EmptyFuncs }
82 #undef EmptyFuncs
84 ///////////////////////////////////////////////////////
86 #define ALC_EFX_MAJOR_VERSION 0x20001
87 #define ALC_EFX_MINOR_VERSION 0x20002
88 #define ALC_MAX_AUXILIARY_SENDS 0x20003
90 ///////////////////////////////////////////////////////
91 // STRING and EXTENSIONS
93 typedef struct ALCfunction_struct
95 ALCchar *funcName;
96 ALvoid *address;
97 } ALCfunction;
99 static ALCfunction alcFunctions[] = {
100 { "alcCreateContext", (ALvoid *) alcCreateContext },
101 { "alcMakeContextCurrent", (ALvoid *) alcMakeContextCurrent },
102 { "alcProcessContext", (ALvoid *) alcProcessContext },
103 { "alcSuspendContext", (ALvoid *) alcSuspendContext },
104 { "alcDestroyContext", (ALvoid *) alcDestroyContext },
105 { "alcGetCurrentContext", (ALvoid *) alcGetCurrentContext },
106 { "alcGetContextsDevice", (ALvoid *) alcGetContextsDevice },
107 { "alcOpenDevice", (ALvoid *) alcOpenDevice },
108 { "alcCloseDevice", (ALvoid *) alcCloseDevice },
109 { "alcGetError", (ALvoid *) alcGetError },
110 { "alcIsExtensionPresent", (ALvoid *) alcIsExtensionPresent },
111 { "alcGetProcAddress", (ALvoid *) alcGetProcAddress },
112 { "alcGetEnumValue", (ALvoid *) alcGetEnumValue },
113 { "alcGetString", (ALvoid *) alcGetString },
114 { "alcGetIntegerv", (ALvoid *) alcGetIntegerv },
115 { "alcCaptureOpenDevice", (ALvoid *) alcCaptureOpenDevice },
116 { "alcCaptureCloseDevice", (ALvoid *) alcCaptureCloseDevice },
117 { "alcCaptureStart", (ALvoid *) alcCaptureStart },
118 { "alcCaptureStop", (ALvoid *) alcCaptureStop },
119 { "alcCaptureSamples", (ALvoid *) alcCaptureSamples },
120 { NULL, (ALvoid *) NULL }
123 static ALenums enumeration[]={
124 // Types
125 { (ALchar *)"ALC_INVALID", ALC_INVALID },
126 { (ALchar *)"ALC_FALSE", ALC_FALSE },
127 { (ALchar *)"ALC_TRUE", ALC_TRUE },
129 // ALC Properties
130 { (ALchar *)"ALC_MAJOR_VERSION", ALC_MAJOR_VERSION },
131 { (ALchar *)"ALC_MINOR_VERSION", ALC_MINOR_VERSION },
132 { (ALchar *)"ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE },
133 { (ALchar *)"ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES },
134 { (ALchar *)"ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER },
135 { (ALchar *)"ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER },
136 { (ALchar *)"ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER },
137 { (ALchar *)"ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER },
138 { (ALchar *)"ALC_EXTENSIONS", ALC_EXTENSIONS },
139 { (ALchar *)"ALC_FREQUENCY", ALC_FREQUENCY },
140 { (ALchar *)"ALC_REFRESH", ALC_REFRESH },
141 { (ALchar *)"ALC_SYNC", ALC_SYNC },
142 { (ALchar *)"ALC_MONO_SOURCES", ALC_MONO_SOURCES },
143 { (ALchar *)"ALC_STEREO_SOURCES", ALC_STEREO_SOURCES },
144 { (ALchar *)"ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER },
145 { (ALchar *)"ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER},
146 { (ALchar *)"ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES },
148 // EFX Properties
149 { (ALchar *)"ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION },
150 { (ALchar *)"ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION },
151 { (ALchar *)"ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS },
153 // ALC Error Message
154 { (ALchar *)"ALC_NO_ERROR", ALC_NO_ERROR },
155 { (ALchar *)"ALC_INVALID_DEVICE", ALC_INVALID_DEVICE },
156 { (ALchar *)"ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT },
157 { (ALchar *)"ALC_INVALID_ENUM", ALC_INVALID_ENUM },
158 { (ALchar *)"ALC_INVALID_VALUE", ALC_INVALID_VALUE },
159 { (ALchar *)"ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY },
160 { (ALchar *)NULL, (ALenum)0 }
162 // Error strings
163 static const ALCchar alcNoError[] = "No Error";
164 static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
165 static const ALCchar alcErrInvalidContext[] = "Invalid Context";
166 static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
167 static const ALCchar alcErrInvalidValue[] = "Invalid Value";
168 static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
170 // Context strings
171 static ALCchar alcDeviceList[2048];
172 static ALCchar alcAllDeviceList[2048];
173 static ALCchar alcCaptureDeviceList[2048];
174 // Default is always the first in the list
175 static ALCchar *alcDefaultDeviceSpecifier = alcDeviceList;
176 static ALCchar *alcDefaultAllDeviceSpecifier = alcAllDeviceList;
177 static ALCchar *alcCaptureDefaultDeviceSpecifier = alcCaptureDeviceList;
180 static ALCchar alcExtensionList[] = "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_EFX";
181 static ALCint alcMajorVersion = 1;
182 static ALCint alcMinorVersion = 1;
184 static ALCint alcEFXMajorVersion = 1;
185 static ALCint alcEFXMinorVersion = 0;
187 ///////////////////////////////////////////////////////
190 ///////////////////////////////////////////////////////
191 // Global Variables
193 static ALCdevice *g_pDeviceList = NULL;
194 static ALCuint g_ulDeviceCount = 0;
196 static CRITICAL_SECTION g_csMutex;
198 // Context List
199 static ALCcontext *g_pContextList = NULL;
200 static ALCuint g_ulContextCount = 0;
202 // Context Error
203 static ALCenum g_eLastContextError = ALC_NO_ERROR;
205 static ALboolean init_done = AL_FALSE;
207 ///////////////////////////////////////////////////////
210 ///////////////////////////////////////////////////////
211 // ALC Related helper functions
212 #ifdef _WIN32
213 BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
215 (void)lpReserved;
217 // Perform actions based on the reason for calling.
218 switch(ul_reason_for_call)
220 case DLL_PROCESS_ATTACH:
221 DisableThreadLibraryCalls(hModule);
222 break;
224 case DLL_PROCESS_DETACH:
225 if(!init_done)
226 break;
227 ReleaseALC();
228 ReleaseALBuffers();
229 ReleaseALEffects();
230 ReleaseALFilters();
231 FreeALConfig();
232 ALTHUNK_EXIT();
233 DeleteCriticalSection(&g_csMutex);
234 break;
236 return TRUE;
238 #else
239 #ifdef HAVE_GCC_DESTRUCTOR
240 static void my_deinit() __attribute__((destructor));
241 static void my_deinit()
243 static ALenum once = AL_FALSE;
244 if(once || !init_done) return;
245 once = AL_TRUE;
247 ReleaseALC();
248 ReleaseALBuffers();
249 ReleaseALEffects();
250 ReleaseALFilters();
251 FreeALConfig();
252 ALTHUNK_EXIT();
253 DeleteCriticalSection(&g_csMutex);
255 #endif
256 #endif
258 static void InitAL(void)
260 if(!init_done)
262 int i;
263 const char *devs, *str;
265 init_done = AL_TRUE;
267 InitializeCriticalSection(&g_csMutex);
268 ALTHUNK_INIT();
269 ReadALConfig();
271 devs = GetConfigValue(NULL, "drivers", "");
272 if(devs[0])
274 int n;
275 size_t len;
276 const char *next = devs;
278 i = 0;
280 do {
281 devs = next;
282 next = strchr(devs, ',');
284 if(!devs[0] || devs[0] == ',')
285 continue;
287 len = (next ? ((size_t)(next-devs)) : strlen(devs));
288 for(n = i;BackendList[n].Init;n++)
290 if(len == strlen(BackendList[n].name) &&
291 strncmp(BackendList[n].name, devs, len) == 0)
293 const char *name = BackendList[i].name;
294 void (*Init)(BackendFuncs*) = BackendList[i].Init;
296 BackendList[i].name = BackendList[n].name;
297 BackendList[i].Init = BackendList[n].Init;
299 BackendList[n].name = name;
300 BackendList[n].Init = Init;
302 i++;
305 } while(next++);
307 BackendList[i].name = NULL;
308 BackendList[i].Init = NULL;
311 for(i = 0;BackendList[i].Init;i++)
312 BackendList[i].Init(&BackendList[i].Funcs);
314 str = GetConfigValue(NULL, "stereodup", "false");
315 DuplicateStereo = (strcasecmp(str, "true") == 0 ||
316 strcasecmp(str, "yes") == 0 ||
317 strcasecmp(str, "on") == 0 ||
318 atoi(str) != 0);
320 str = GetConfigValue(NULL, "excludefx", "");
321 if(str[0])
323 const struct {
324 const char *name;
325 int type;
326 } EffectList[] = {
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 IsContext
396 Check pContext is a valid Context pointer
398 static ALCboolean IsContext(ALCcontext *pContext)
400 ALCcontext *pTempContext;
402 pTempContext = g_pContextList;
403 while (pTempContext && pTempContext != pContext)
404 pTempContext = pTempContext->next;
406 return (pTempContext ? ALC_TRUE : ALC_FALSE);
411 SetALCError
413 Store latest ALC Error
415 ALCvoid SetALCError(ALenum errorCode)
417 g_eLastContextError = errorCode;
422 SuspendContext
424 Thread-safe entry
426 ALCvoid SuspendContext(ALCcontext *pContext)
428 (void)pContext;
429 EnterCriticalSection(&g_csMutex);
434 ProcessContext
436 Thread-safe exit
438 ALCvoid ProcessContext(ALCcontext *pContext)
440 (void)pContext;
441 LeaveCriticalSection(&g_csMutex);
446 InitContext
448 Initialize Context variables
450 static ALvoid InitContext(ALCcontext *pContext)
452 int level;
454 //Initialise listener
455 pContext->Listener.Gain = 1.0f;
456 pContext->Listener.MetersPerUnit = 1.0f;
457 pContext->Listener.Position[0] = 0.0f;
458 pContext->Listener.Position[1] = 0.0f;
459 pContext->Listener.Position[2] = 0.0f;
460 pContext->Listener.Velocity[0] = 0.0f;
461 pContext->Listener.Velocity[1] = 0.0f;
462 pContext->Listener.Velocity[2] = 0.0f;
463 pContext->Listener.Forward[0] = 0.0f;
464 pContext->Listener.Forward[1] = 0.0f;
465 pContext->Listener.Forward[2] = -1.0f;
466 pContext->Listener.Up[0] = 0.0f;
467 pContext->Listener.Up[1] = 1.0f;
468 pContext->Listener.Up[2] = 0.0f;
470 //Validate pContext
471 pContext->LastError = AL_NO_ERROR;
472 pContext->InUse = AL_FALSE;
474 //Set output format
475 pContext->Frequency = pContext->Device->Frequency;
477 //Set globals
478 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
479 pContext->DopplerFactor = 1.0f;
480 pContext->DopplerVelocity = 1.0f;
481 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
483 pContext->lNumStereoSources = 1;
484 pContext->lNumMonoSources = pContext->Device->MaxNoOfSources - pContext->lNumStereoSources;
486 pContext->AuxiliaryEffectSlotMax = GetConfigValueInt(NULL, "slots", 4);
487 pContext->NumSends = GetConfigValueInt(NULL, "sends", MAX_SENDS);
488 if(pContext->NumSends > MAX_SENDS)
489 pContext->NumSends = MAX_SENDS;
491 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";
493 level = GetConfigValueInt(NULL, "cf_level", 0);
494 if(level > 0 && level <= 6)
496 pContext->bs2b = calloc(1, sizeof(*pContext->bs2b));
497 bs2b_set_srate(pContext->bs2b, pContext->Frequency);
498 bs2b_set_level(pContext->bs2b, level);
501 aluInitPanning(pContext);
506 ExitContext
508 Clean up Context, destroy any remaining Sources
510 static ALCvoid ExitContext(ALCcontext *pContext)
512 //Invalidate context
513 pContext->LastError = AL_NO_ERROR;
514 pContext->InUse = AL_FALSE;
516 free(pContext->bs2b);
517 pContext->bs2b = NULL;
520 ///////////////////////////////////////////////////////
523 ///////////////////////////////////////////////////////
524 // ALC Functions calls
527 // This should probably move to another c file but for now ...
528 ALCAPI ALCdevice* ALCAPIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
530 ALCboolean DeviceFound = ALC_FALSE;
531 ALCdevice *pDevice = NULL;
532 ALCint i;
534 InitAL();
536 if(SampleSize <= 0)
538 SetALCError(ALC_INVALID_VALUE);
539 return NULL;
542 if(deviceName && !deviceName[0])
543 deviceName = NULL;
545 pDevice = malloc(sizeof(ALCdevice));
546 if (pDevice)
548 //Initialise device structure
549 memset(pDevice, 0, sizeof(ALCdevice));
551 //Validate device
552 pDevice->IsCaptureDevice = AL_TRUE;
554 pDevice->Frequency = frequency;
555 pDevice->Format = format;
557 for(i = 0;BackendList[i].Init;i++)
559 pDevice->Funcs = &BackendList[i].Funcs;
560 if(ALCdevice_OpenCapture(pDevice, deviceName, frequency, format, SampleSize))
562 SuspendContext(NULL);
563 pDevice->next = g_pDeviceList;
564 g_pDeviceList = pDevice;
565 g_ulDeviceCount++;
566 ProcessContext(NULL);
568 DeviceFound = ALC_TRUE;
569 break;
573 if(!DeviceFound)
575 SetALCError(ALC_INVALID_VALUE);
576 free(pDevice);
577 pDevice = NULL;
580 else
581 SetALCError(ALC_OUT_OF_MEMORY);
583 return pDevice;
586 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
588 ALCboolean bReturn = ALC_FALSE;
589 ALCdevice **list;
591 if ((pDevice)&&(pDevice->IsCaptureDevice))
593 SuspendContext(NULL);
595 list = &g_pDeviceList;
596 while(*list != pDevice)
597 list = &(*list)->next;
599 *list = (*list)->next;
600 g_ulDeviceCount--;
602 ProcessContext(NULL);
604 ALCdevice_CloseCapture(pDevice);
605 free(pDevice);
607 bReturn = ALC_TRUE;
609 else
610 SetALCError(ALC_INVALID_DEVICE);
612 return bReturn;
615 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
617 if ((pDevice)&&(pDevice->IsCaptureDevice))
618 ALCdevice_StartCapture(pDevice);
619 else
620 SetALCError(ALC_INVALID_DEVICE);
623 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
625 if ((pDevice)&&(pDevice->IsCaptureDevice))
626 ALCdevice_StopCapture(pDevice);
627 else
628 SetALCError(ALC_INVALID_DEVICE);
631 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
633 if ((pDevice) && (pDevice->IsCaptureDevice))
634 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
635 else
636 SetALCError(ALC_INVALID_DEVICE);
640 alcGetError
642 Return last ALC generated error code
644 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
646 ALCenum errorCode;
648 (void)device;
650 errorCode = g_eLastContextError;
651 g_eLastContextError = ALC_NO_ERROR;
652 return errorCode;
657 alcSuspendContext
659 Not functional
661 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
663 // Not a lot happens here !
664 (void)pContext;
669 alcProcessContext
671 Not functional
673 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
675 // Not a lot happens here !
676 (void)pContext;
681 alcGetString
683 Returns information about the Device, and error strings
685 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
687 const ALCchar *value = NULL;
689 InitAL();
691 switch (param)
693 case ALC_NO_ERROR:
694 value = alcNoError;
695 break;
697 case ALC_INVALID_ENUM:
698 value = alcErrInvalidEnum;
699 break;
701 case ALC_INVALID_VALUE:
702 value = alcErrInvalidValue;
703 break;
705 case ALC_INVALID_DEVICE:
706 value = alcErrInvalidDevice;
707 break;
709 case ALC_INVALID_CONTEXT:
710 value = alcErrInvalidContext;
711 break;
713 case ALC_OUT_OF_MEMORY:
714 value = alcErrOutOfMemory;
715 break;
717 case ALC_DEFAULT_DEVICE_SPECIFIER:
718 value = alcDefaultDeviceSpecifier;
719 break;
721 case ALC_DEVICE_SPECIFIER:
722 if (pDevice)
723 value = pDevice->szDeviceName;
724 else
725 value = alcDeviceList;
726 break;
728 case ALC_ALL_DEVICES_SPECIFIER:
729 value = alcAllDeviceList;
730 break;
732 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
733 value = alcDefaultAllDeviceSpecifier;
734 break;
736 case ALC_CAPTURE_DEVICE_SPECIFIER:
737 if (pDevice)
738 value = pDevice->szDeviceName;
739 else
740 value = alcCaptureDeviceList;
741 break;
743 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
744 value = alcCaptureDefaultDeviceSpecifier;
745 break;
747 case ALC_EXTENSIONS:
748 value = alcExtensionList;
749 break;
751 default:
752 SetALCError(ALC_INVALID_ENUM);
753 break;
756 return value;
761 alcGetIntegerv
763 Returns information about the Device and the version of Open AL
765 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
767 InitAL();
769 if ((device)&&(device->IsCaptureDevice))
771 SuspendContext(NULL);
773 // Capture device
774 switch (param)
776 case ALC_CAPTURE_SAMPLES:
777 if ((size) && (data))
778 *data = ALCdevice_AvailableSamples(device);
779 else
780 SetALCError(ALC_INVALID_VALUE);
781 break;
783 default:
784 SetALCError(ALC_INVALID_ENUM);
785 break;
788 ProcessContext(NULL);
790 else
792 if(data)
794 // Playback Device
795 switch (param)
797 case ALC_MAJOR_VERSION:
798 if(!size)
799 SetALCError(ALC_INVALID_VALUE);
800 else
801 *data = alcMajorVersion;
802 break;
804 case ALC_MINOR_VERSION:
805 if(!size)
806 SetALCError(ALC_INVALID_VALUE);
807 else
808 *data = alcMinorVersion;
809 break;
811 case ALC_EFX_MAJOR_VERSION:
812 if(!size)
813 SetALCError(ALC_INVALID_VALUE);
814 else
815 *data = alcEFXMajorVersion;
816 break;
818 case ALC_EFX_MINOR_VERSION:
819 if(!size)
820 SetALCError(ALC_INVALID_VALUE);
821 else
822 *data = alcEFXMinorVersion;
823 break;
825 case ALC_MAX_AUXILIARY_SENDS:
826 if(!size)
827 SetALCError(ALC_INVALID_VALUE);
828 else if(device && device->Context)
829 *data = device->Context->NumSends;
830 else
831 *data = MAX_SENDS;
832 break;
834 case ALC_ATTRIBUTES_SIZE:
835 if(!device)
836 SetALCError(ALC_INVALID_DEVICE);
837 else if(!size)
838 SetALCError(ALC_INVALID_VALUE);
839 else
840 *data = 12;
841 break;
843 case ALC_ALL_ATTRIBUTES:
844 if(!device)
845 SetALCError(ALC_INVALID_DEVICE);
846 else if (size < 7)
847 SetALCError(ALC_INVALID_VALUE);
848 else
850 int i = 0;
852 data[i++] = ALC_FREQUENCY;
853 data[i++] = device->Frequency;
855 data[i++] = ALC_REFRESH;
856 data[i++] = device->Frequency / device->UpdateSize;
858 data[i++] = ALC_SYNC;
859 data[i++] = ALC_FALSE;
861 SuspendContext(NULL);
862 if(device->Context && size >= 12)
864 data[i++] = ALC_MONO_SOURCES;
865 data[i++] = device->Context->lNumMonoSources;
867 data[i++] = ALC_STEREO_SOURCES;
868 data[i++] = device->Context->lNumStereoSources;
870 data[i++] = ALC_MAX_AUXILIARY_SENDS;
871 data[i++] = device->Context->NumSends;
873 ProcessContext(NULL);
875 data[i++] = 0;
877 break;
879 case ALC_FREQUENCY:
880 if(!device)
881 SetALCError(ALC_INVALID_DEVICE);
882 else if(!size)
883 SetALCError(ALC_INVALID_VALUE);
884 else
885 *data = device->Frequency;
886 break;
888 case ALC_REFRESH:
889 if(!device)
890 SetALCError(ALC_INVALID_DEVICE);
891 else if(!size)
892 SetALCError(ALC_INVALID_VALUE);
893 else
894 *data = device->Frequency / device->UpdateSize;
895 break;
897 case ALC_SYNC:
898 if(!device)
899 SetALCError(ALC_INVALID_DEVICE);
900 else if(!size)
901 SetALCError(ALC_INVALID_VALUE);
902 else
903 *data = ALC_FALSE;
904 break;
906 case ALC_MONO_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->lNumMonoSources;
913 break;
915 case ALC_STEREO_SOURCES:
916 if(!device || !device->Context)
917 SetALCError(ALC_INVALID_DEVICE);
918 else if (size != 1)
919 SetALCError(ALC_INVALID_VALUE);
920 else
921 *data = device->Context->lNumStereoSources;
922 break;
924 default:
925 SetALCError(ALC_INVALID_ENUM);
926 break;
929 else if(size)
930 SetALCError(ALC_INVALID_VALUE);
933 return;
938 alcIsExtensionPresent
940 Determines if there is support for a particular extension
942 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
944 ALCboolean bResult = ALC_FALSE;
946 (void)device;
948 if (extName)
950 const char *ptr;
951 size_t len;
953 len = strlen(extName);
954 ptr = alcExtensionList;
955 while(ptr && *ptr)
957 if(strncasecmp(ptr, extName, len) == 0 &&
958 (ptr[len] == '\0' || isspace(ptr[len])))
960 bResult = ALC_TRUE;
961 break;
963 if((ptr=strchr(ptr, ' ')) != NULL)
965 do {
966 ++ptr;
967 } while(isspace(*ptr));
971 else
972 SetALCError(ALC_INVALID_VALUE);
974 return bResult;
979 alcGetProcAddress
981 Retrieves the function address for a particular extension function
983 ALCAPI ALCvoid * ALCAPIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
985 ALCvoid *pFunction = NULL;
986 ALsizei i = 0;
988 (void)device;
990 if (funcName)
992 while(alcFunctions[i].funcName &&
993 strcmp(alcFunctions[i].funcName,funcName) != 0)
994 i++;
995 pFunction = alcFunctions[i].address;
997 else
998 SetALCError(ALC_INVALID_VALUE);
1000 return pFunction;
1005 alcGetEnumValue
1007 Get the value for a particular ALC Enumerated Value
1009 ALCAPI ALCenum ALCAPIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
1011 ALsizei i = 0;
1012 ALCenum val;
1014 (void)device;
1016 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
1017 i++;
1018 val = enumeration[i].value;
1020 if(!enumeration[i].enumName)
1021 SetALCError(ALC_INVALID_VALUE);
1023 return val;
1028 alcCreateContext
1030 Create and attach a Context to a particular Device.
1032 ALCAPI ALCcontext* ALCAPIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
1034 ALCcontext *ALContext = NULL;
1035 ALuint ulAttributeIndex, ulRequestedStereoSources;
1036 ALuint RequestedSends;
1038 if ((device)&&(!device->IsCaptureDevice))
1040 // Reset Context Last Error code
1041 g_eLastContextError = ALC_NO_ERROR;
1043 // Current implementation only allows one Context per Device
1044 if(!device->Context)
1046 ALContext = calloc(1, sizeof(ALCcontext));
1047 if(!ALContext)
1049 SetALCError(ALC_OUT_OF_MEMORY);
1050 return NULL;
1053 ALContext->Device = device;
1054 InitContext(ALContext);
1056 device->Context = ALContext;
1058 SuspendContext(NULL);
1060 ALContext->next = g_pContextList;
1061 g_pContextList = ALContext;
1062 g_ulContextCount++;
1064 ProcessContext(NULL);
1066 // Check for attributes
1067 if (attrList)
1069 ulAttributeIndex = 0;
1070 while ((ulAttributeIndex < 10) && (attrList[ulAttributeIndex]))
1072 if (attrList[ulAttributeIndex] == ALC_STEREO_SOURCES)
1074 ulRequestedStereoSources = attrList[ulAttributeIndex + 1];
1076 if (ulRequestedStereoSources > ALContext->Device->MaxNoOfSources)
1077 ulRequestedStereoSources = ALContext->Device->MaxNoOfSources;
1079 ALContext->lNumStereoSources = ulRequestedStereoSources;
1080 ALContext->lNumMonoSources = ALContext->Device->MaxNoOfSources - ALContext->lNumStereoSources;
1083 if(attrList[ulAttributeIndex] == ALC_MAX_AUXILIARY_SENDS)
1085 RequestedSends = attrList[ulAttributeIndex + 1];
1087 if(RequestedSends > ALContext->NumSends)
1088 RequestedSends = ALContext->NumSends;
1090 ALContext->NumSends = RequestedSends;
1093 ulAttributeIndex += 2;
1097 else
1099 SetALCError(ALC_INVALID_VALUE);
1100 ALContext = NULL;
1103 else
1104 SetALCError(ALC_INVALID_DEVICE);
1106 return ALContext;
1111 alcDestroyContext
1113 Remove a Context
1115 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
1117 ALCcontext **list;
1119 InitAL();
1121 // Lock context list
1122 SuspendContext(NULL);
1124 if (IsContext(context))
1126 // Lock context
1127 SuspendContext(context);
1129 ReleaseALSources(context);
1130 ReleaseALAuxiliaryEffectSlots(context);
1132 context->Device->Context = NULL;
1134 list = &g_pContextList;
1135 while(*list != context)
1136 list = &(*list)->next;
1138 *list = (*list)->next;
1139 g_ulContextCount--;
1141 // Unlock context
1142 ProcessContext(context);
1144 ExitContext(context);
1146 // Free memory (MUST do this after ProcessContext)
1147 memset(context, 0, sizeof(ALCcontext));
1148 free(context);
1150 else
1151 SetALCError(ALC_INVALID_CONTEXT);
1153 ProcessContext(NULL);
1158 alcGetCurrentContext
1160 Returns the currently active Context
1162 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1164 ALCcontext *pContext = NULL;
1166 InitAL();
1168 SuspendContext(NULL);
1170 pContext = g_pContextList;
1171 while ((pContext) && (!pContext->InUse))
1172 pContext = pContext->next;
1174 ProcessContext(NULL);
1176 return pContext;
1181 alcGetContextsDevice
1183 Returns the Device that a particular Context is attached to
1185 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1187 ALCdevice *pDevice = NULL;
1189 InitAL();
1191 SuspendContext(NULL);
1192 if (IsContext(pContext))
1193 pDevice = pContext->Device;
1194 else
1195 SetALCError(ALC_INVALID_CONTEXT);
1196 ProcessContext(NULL);
1198 return pDevice;
1203 alcMakeContextCurrent
1205 Makes the given Context the active Context
1207 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1209 ALCcontext *ALContext;
1210 ALboolean bReturn = AL_TRUE;
1212 InitAL();
1214 SuspendContext(NULL);
1216 // context must be a valid Context or NULL
1217 if ((IsContext(context)) || (context == NULL))
1219 if ((ALContext=alcGetCurrentContext()))
1221 SuspendContext(ALContext);
1222 ALContext->InUse=AL_FALSE;
1223 ProcessContext(ALContext);
1226 if ((ALContext=context) && (ALContext->Device))
1228 SuspendContext(ALContext);
1229 ALContext->InUse=AL_TRUE;
1230 ProcessContext(ALContext);
1233 else
1235 SetALCError(ALC_INVALID_CONTEXT);
1236 bReturn = AL_FALSE;
1239 ProcessContext(NULL);
1241 return bReturn;
1246 alcOpenDevice
1248 Open the Device specified.
1250 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1252 ALboolean bDeviceFound = AL_FALSE;
1253 ALCdevice *device;
1254 ALint i;
1256 InitAL();
1258 if(deviceName && !deviceName[0])
1259 deviceName = NULL;
1261 device = malloc(sizeof(ALCdevice));
1262 if (device)
1264 const char *fmt;
1266 //Initialise device structure
1267 memset(device, 0, sizeof(ALCdevice));
1269 //Validate device
1270 device->IsCaptureDevice = AL_FALSE;
1272 //Set output format
1273 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1274 if((ALint)device->Frequency <= 0)
1275 device->Frequency = SWMIXER_OUTPUT_RATE;
1277 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1278 if(fmt[0])
1279 device->Format = alGetEnumValue(fmt);
1281 if(!aluChannelsFromFormat(device->Format))
1282 device->Format = AL_FORMAT_STEREO16;
1284 device->UpdateSize = GetConfigValueInt(NULL, "refresh", 4096);
1285 if((ALint)device->UpdateSize <= 0)
1286 device->UpdateSize = 4096;
1288 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1289 if((ALint)device->MaxNoOfSources <= 0)
1290 device->MaxNoOfSources = 256;
1292 // Find a playback device to open
1293 SuspendContext(NULL);
1294 for(i = 0;BackendList[i].Init;i++)
1296 device->Funcs = &BackendList[i].Funcs;
1297 if(ALCdevice_OpenPlayback(device, deviceName))
1299 device->next = g_pDeviceList;
1300 g_pDeviceList = device;
1301 g_ulDeviceCount++;
1303 bDeviceFound = AL_TRUE;
1304 break;
1307 ProcessContext(NULL);
1309 if (!bDeviceFound)
1311 // No suitable output device found
1312 SetALCError(ALC_INVALID_VALUE);
1313 free(device);
1314 device = NULL;
1317 else
1318 SetALCError(ALC_OUT_OF_MEMORY);
1320 return device;
1325 alcCloseDevice
1327 Close the specified Device
1329 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1331 ALCboolean bReturn = ALC_FALSE;
1332 ALCdevice **list;
1334 if ((pDevice)&&(!pDevice->IsCaptureDevice))
1336 SuspendContext(NULL);
1338 list = &g_pDeviceList;
1339 while(*list != pDevice)
1340 list = &(*list)->next;
1342 *list = (*list)->next;
1343 g_ulDeviceCount--;
1345 ProcessContext(NULL);
1347 if(pDevice->Context)
1349 #ifdef _DEBUG
1350 AL_PRINT("alcCloseDevice(): destroying 1 Context\n");
1351 #endif
1352 alcDestroyContext(pDevice->Context);
1354 ALCdevice_ClosePlayback(pDevice);
1356 //Release device structure
1357 memset(pDevice, 0, sizeof(ALCdevice));
1358 free(pDevice);
1360 bReturn = ALC_TRUE;
1362 else
1363 SetALCError(ALC_INVALID_DEVICE);
1365 return bReturn;
1369 ALCvoid ReleaseALC(ALCvoid)
1371 #ifdef _DEBUG
1372 if(g_ulDeviceCount > 0)
1373 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
1374 #endif
1376 while(g_pDeviceList)
1378 if(g_pDeviceList->IsCaptureDevice)
1379 alcCaptureCloseDevice(g_pDeviceList);
1380 else
1381 alcCloseDevice(g_pDeviceList);
1385 ///////////////////////////////////////////////////////