Hold the lock while finding a capture device to open
[openal-soft.git] / Alc / ALc.c
blob042724eb4c477b82c1f92c221450e60e52a94fb0
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, 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 SuspendContext(NULL);
396 pTempContext = g_pContextList;
397 while (pTempContext && pTempContext != pContext)
398 pTempContext = pTempContext->next;
400 ProcessContext(NULL);
402 return (pTempContext ? ALC_TRUE : ALC_FALSE);
407 SetALCError
409 Store latest ALC Error
411 ALCvoid SetALCError(ALenum errorCode)
413 g_eLastContextError = errorCode;
418 SuspendContext
420 Thread-safe entry
422 ALCvoid SuspendContext(ALCcontext *pContext)
424 (void)pContext;
425 EnterCriticalSection(&g_csMutex);
430 ProcessContext
432 Thread-safe exit
434 ALCvoid ProcessContext(ALCcontext *pContext)
436 (void)pContext;
437 LeaveCriticalSection(&g_csMutex);
442 InitContext
444 Initialize Context variables
446 static ALvoid InitContext(ALCcontext *pContext)
448 int level;
450 //Initialise listener
451 pContext->Listener.Gain = 1.0f;
452 pContext->Listener.MetersPerUnit = 1.0f;
453 pContext->Listener.Position[0] = 0.0f;
454 pContext->Listener.Position[1] = 0.0f;
455 pContext->Listener.Position[2] = 0.0f;
456 pContext->Listener.Velocity[0] = 0.0f;
457 pContext->Listener.Velocity[1] = 0.0f;
458 pContext->Listener.Velocity[2] = 0.0f;
459 pContext->Listener.Forward[0] = 0.0f;
460 pContext->Listener.Forward[1] = 0.0f;
461 pContext->Listener.Forward[2] = -1.0f;
462 pContext->Listener.Up[0] = 0.0f;
463 pContext->Listener.Up[1] = 1.0f;
464 pContext->Listener.Up[2] = 0.0f;
466 //Validate pContext
467 pContext->LastError = AL_NO_ERROR;
468 pContext->InUse = AL_FALSE;
470 //Set output format
471 pContext->Frequency = pContext->Device->Frequency;
473 //Set globals
474 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
475 pContext->DopplerFactor = 1.0f;
476 pContext->DopplerVelocity = 1.0f;
477 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
479 pContext->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";
481 level = GetConfigValueInt(NULL, "cf_level", 0);
482 if(level > 0 && level <= 6)
484 pContext->bs2b = calloc(1, sizeof(*pContext->bs2b));
485 bs2b_set_srate(pContext->bs2b, pContext->Frequency);
486 bs2b_set_level(pContext->bs2b, level);
489 aluInitPanning(pContext);
494 ExitContext
496 Clean up Context, destroy any remaining Sources
498 static ALCvoid ExitContext(ALCcontext *pContext)
500 //Invalidate context
501 pContext->LastError = AL_NO_ERROR;
502 pContext->InUse = AL_FALSE;
504 free(pContext->bs2b);
505 pContext->bs2b = NULL;
508 ///////////////////////////////////////////////////////
511 ///////////////////////////////////////////////////////
512 // ALC Functions calls
515 // This should probably move to another c file but for now ...
516 ALCAPI ALCdevice* ALCAPIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
518 ALCboolean DeviceFound = ALC_FALSE;
519 ALCdevice *pDevice = NULL;
520 ALCint i;
522 InitAL();
524 if(SampleSize <= 0)
526 SetALCError(ALC_INVALID_VALUE);
527 return NULL;
530 if(deviceName && !deviceName[0])
531 deviceName = NULL;
533 pDevice = malloc(sizeof(ALCdevice));
534 if (pDevice)
536 //Initialise device structure
537 memset(pDevice, 0, sizeof(ALCdevice));
539 //Validate device
540 pDevice->IsCaptureDevice = AL_TRUE;
542 pDevice->Frequency = frequency;
543 pDevice->Format = format;
545 SuspendContext(NULL);
546 for(i = 0;BackendList[i].Init;i++)
548 pDevice->Funcs = &BackendList[i].Funcs;
549 if(ALCdevice_OpenCapture(pDevice, deviceName, frequency, format, SampleSize))
551 pDevice->next = g_pDeviceList;
552 g_pDeviceList = pDevice;
553 g_ulDeviceCount++;
555 DeviceFound = ALC_TRUE;
556 break;
559 ProcessContext(NULL);
561 if(!DeviceFound)
563 SetALCError(ALC_INVALID_VALUE);
564 free(pDevice);
565 pDevice = NULL;
568 else
569 SetALCError(ALC_OUT_OF_MEMORY);
571 return pDevice;
574 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
576 ALCboolean bReturn = ALC_FALSE;
577 ALCdevice **list;
579 if ((pDevice)&&(pDevice->IsCaptureDevice))
581 SuspendContext(NULL);
583 list = &g_pDeviceList;
584 while(*list != pDevice)
585 list = &(*list)->next;
587 *list = (*list)->next;
588 g_ulDeviceCount--;
590 ProcessContext(NULL);
592 ALCdevice_CloseCapture(pDevice);
593 free(pDevice);
595 bReturn = ALC_TRUE;
597 else
598 SetALCError(ALC_INVALID_DEVICE);
600 return bReturn;
603 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
605 if ((pDevice)&&(pDevice->IsCaptureDevice))
606 ALCdevice_StartCapture(pDevice);
607 else
608 SetALCError(ALC_INVALID_DEVICE);
611 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
613 if ((pDevice)&&(pDevice->IsCaptureDevice))
614 ALCdevice_StopCapture(pDevice);
615 else
616 SetALCError(ALC_INVALID_DEVICE);
619 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
621 if ((pDevice) && (pDevice->IsCaptureDevice))
622 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
623 else
624 SetALCError(ALC_INVALID_DEVICE);
628 alcGetError
630 Return last ALC generated error code
632 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
634 ALCenum errorCode;
636 (void)device;
638 errorCode = g_eLastContextError;
639 g_eLastContextError = ALC_NO_ERROR;
640 return errorCode;
645 alcSuspendContext
647 Not functional
649 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
651 // Not a lot happens here !
652 (void)pContext;
657 alcProcessContext
659 Not functional
661 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
663 // Not a lot happens here !
664 (void)pContext;
669 alcGetString
671 Returns information about the Device, and error strings
673 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
675 const ALCchar *value = NULL;
677 InitAL();
679 switch (param)
681 case ALC_NO_ERROR:
682 value = alcNoError;
683 break;
685 case ALC_INVALID_ENUM:
686 value = alcErrInvalidEnum;
687 break;
689 case ALC_INVALID_VALUE:
690 value = alcErrInvalidValue;
691 break;
693 case ALC_INVALID_DEVICE:
694 value = alcErrInvalidDevice;
695 break;
697 case ALC_INVALID_CONTEXT:
698 value = alcErrInvalidContext;
699 break;
701 case ALC_OUT_OF_MEMORY:
702 value = alcErrOutOfMemory;
703 break;
705 case ALC_DEFAULT_DEVICE_SPECIFIER:
706 value = alcDefaultDeviceSpecifier;
707 break;
709 case ALC_DEVICE_SPECIFIER:
710 if (pDevice)
711 value = pDevice->szDeviceName;
712 else
713 value = alcDeviceList;
714 break;
716 case ALC_ALL_DEVICES_SPECIFIER:
717 value = alcAllDeviceList;
718 break;
720 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
721 value = alcDefaultAllDeviceSpecifier;
722 break;
724 case ALC_CAPTURE_DEVICE_SPECIFIER:
725 if (pDevice)
726 value = pDevice->szDeviceName;
727 else
728 value = alcCaptureDeviceList;
729 break;
731 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
732 value = alcCaptureDefaultDeviceSpecifier;
733 break;
735 case ALC_EXTENSIONS:
736 value = alcExtensionList;
737 break;
739 default:
740 SetALCError(ALC_INVALID_ENUM);
741 break;
744 return value;
749 alcGetIntegerv
751 Returns information about the Device and the version of Open AL
753 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
755 InitAL();
757 if ((device)&&(device->IsCaptureDevice))
759 SuspendContext(NULL);
761 // Capture device
762 switch (param)
764 case ALC_CAPTURE_SAMPLES:
765 if ((size) && (data))
766 *data = ALCdevice_AvailableSamples(device);
767 else
768 SetALCError(ALC_INVALID_VALUE);
769 break;
771 default:
772 SetALCError(ALC_INVALID_ENUM);
773 break;
776 ProcessContext(NULL);
778 else
780 if(data)
782 // Playback Device
783 switch (param)
785 case ALC_MAJOR_VERSION:
786 if(!size)
787 SetALCError(ALC_INVALID_VALUE);
788 else
789 *data = alcMajorVersion;
790 break;
792 case ALC_MINOR_VERSION:
793 if(!size)
794 SetALCError(ALC_INVALID_VALUE);
795 else
796 *data = alcMinorVersion;
797 break;
799 case ALC_EFX_MAJOR_VERSION:
800 if(!size)
801 SetALCError(ALC_INVALID_VALUE);
802 else
803 *data = alcEFXMajorVersion;
804 break;
806 case ALC_EFX_MINOR_VERSION:
807 if(!size)
808 SetALCError(ALC_INVALID_VALUE);
809 else
810 *data = alcEFXMinorVersion;
811 break;
813 case ALC_MAX_AUXILIARY_SENDS:
814 if(!size)
815 SetALCError(ALC_INVALID_VALUE);
816 else
817 *data = (device?device->NumAuxSends:MAX_SENDS);
818 break;
820 case ALC_ATTRIBUTES_SIZE:
821 if(!device)
822 SetALCError(ALC_INVALID_DEVICE);
823 else if(!size)
824 SetALCError(ALC_INVALID_VALUE);
825 else
826 *data = 13;
827 break;
829 case ALC_ALL_ATTRIBUTES:
830 if(!device)
831 SetALCError(ALC_INVALID_DEVICE);
832 else if (size < 13)
833 SetALCError(ALC_INVALID_VALUE);
834 else
836 int i = 0;
838 SuspendContext(NULL);
839 data[i++] = ALC_FREQUENCY;
840 data[i++] = device->Frequency;
842 data[i++] = ALC_REFRESH;
843 data[i++] = device->Frequency / device->UpdateSize;
845 data[i++] = ALC_SYNC;
846 data[i++] = ALC_FALSE;
848 data[i++] = ALC_MONO_SOURCES;
849 data[i++] = device->lNumMonoSources;
851 data[i++] = ALC_STEREO_SOURCES;
852 data[i++] = device->lNumStereoSources;
854 data[i++] = ALC_MAX_AUXILIARY_SENDS;
855 data[i++] = device->NumAuxSends;
857 data[i++] = 0;
858 ProcessContext(NULL);
860 break;
862 case ALC_FREQUENCY:
863 if(!device)
864 SetALCError(ALC_INVALID_DEVICE);
865 else if(!size)
866 SetALCError(ALC_INVALID_VALUE);
867 else
868 *data = device->Frequency;
869 break;
871 case ALC_REFRESH:
872 if(!device)
873 SetALCError(ALC_INVALID_DEVICE);
874 else if(!size)
875 SetALCError(ALC_INVALID_VALUE);
876 else
877 *data = device->Frequency / device->UpdateSize;
878 break;
880 case ALC_SYNC:
881 if(!device)
882 SetALCError(ALC_INVALID_DEVICE);
883 else if(!size)
884 SetALCError(ALC_INVALID_VALUE);
885 else
886 *data = ALC_FALSE;
887 break;
889 case ALC_MONO_SOURCES:
890 if(!device || !device->Context)
891 SetALCError(ALC_INVALID_DEVICE);
892 else if (size != 1)
893 SetALCError(ALC_INVALID_VALUE);
894 else
895 *data = device->lNumMonoSources;
896 break;
898 case ALC_STEREO_SOURCES:
899 if(!device || !device->Context)
900 SetALCError(ALC_INVALID_DEVICE);
901 else if (size != 1)
902 SetALCError(ALC_INVALID_VALUE);
903 else
904 *data = device->lNumStereoSources;
905 break;
907 default:
908 SetALCError(ALC_INVALID_ENUM);
909 break;
912 else if(size)
913 SetALCError(ALC_INVALID_VALUE);
916 return;
921 alcIsExtensionPresent
923 Determines if there is support for a particular extension
925 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
927 ALCboolean bResult = ALC_FALSE;
929 (void)device;
931 if (extName)
933 const char *ptr;
934 size_t len;
936 len = strlen(extName);
937 ptr = alcExtensionList;
938 while(ptr && *ptr)
940 if(strncasecmp(ptr, extName, len) == 0 &&
941 (ptr[len] == '\0' || isspace(ptr[len])))
943 bResult = ALC_TRUE;
944 break;
946 if((ptr=strchr(ptr, ' ')) != NULL)
948 do {
949 ++ptr;
950 } while(isspace(*ptr));
954 else
955 SetALCError(ALC_INVALID_VALUE);
957 return bResult;
962 alcGetProcAddress
964 Retrieves the function address for a particular extension function
966 ALCAPI ALCvoid * ALCAPIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
968 ALCvoid *pFunction = NULL;
969 ALsizei i = 0;
971 (void)device;
973 if (funcName)
975 while(alcFunctions[i].funcName &&
976 strcmp(alcFunctions[i].funcName,funcName) != 0)
977 i++;
978 pFunction = alcFunctions[i].address;
980 else
981 SetALCError(ALC_INVALID_VALUE);
983 return pFunction;
988 alcGetEnumValue
990 Get the value for a particular ALC Enumerated Value
992 ALCAPI ALCenum ALCAPIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
994 ALsizei i = 0;
995 ALCenum val;
997 (void)device;
999 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
1000 i++;
1001 val = enumeration[i].value;
1003 if(!enumeration[i].enumName)
1004 SetALCError(ALC_INVALID_VALUE);
1006 return val;
1011 alcCreateContext
1013 Create and attach a Context to a particular Device.
1015 ALCAPI ALCcontext* ALCAPIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
1017 ALCcontext *ALContext = NULL;
1018 ALuint ulAttributeIndex, ulRequestedStereoSources;
1019 ALuint RequestedSends;
1021 if ((device)&&(!device->IsCaptureDevice))
1023 // Reset Context Last Error code
1024 g_eLastContextError = ALC_NO_ERROR;
1026 // Current implementation only allows one Context per Device
1027 if(!device->Context)
1029 ALContext = calloc(1, sizeof(ALCcontext));
1030 if(!ALContext)
1032 SetALCError(ALC_OUT_OF_MEMORY);
1033 return NULL;
1036 SuspendContext(NULL);
1038 ALContext->Device = device;
1039 InitContext(ALContext);
1041 device->Context = ALContext;
1043 ALContext->next = g_pContextList;
1044 g_pContextList = ALContext;
1045 g_ulContextCount++;
1047 // Check for attributes
1048 if (attrList)
1050 ALCuint freq = device->Frequency;
1051 ALCint numMono = device->lNumMonoSources;
1052 ALCint numStereo = device->lNumStereoSources;
1053 ALCuint numSends = device->NumAuxSends;
1055 ulAttributeIndex = 0;
1056 while ((ulAttributeIndex < 10) && (attrList[ulAttributeIndex]))
1058 if(attrList[ulAttributeIndex] == ALC_FREQUENCY)
1060 freq = attrList[ulAttributeIndex + 1];
1061 if(freq == 0)
1062 freq = device->Frequency;
1065 if(attrList[ulAttributeIndex] == ALC_STEREO_SOURCES)
1067 ulRequestedStereoSources = attrList[ulAttributeIndex + 1];
1069 if (ulRequestedStereoSources > device->MaxNoOfSources)
1070 ulRequestedStereoSources = device->MaxNoOfSources;
1072 numStereo = ulRequestedStereoSources;
1073 numMono = device->MaxNoOfSources - numStereo;
1076 if(attrList[ulAttributeIndex] == ALC_MAX_AUXILIARY_SENDS)
1078 RequestedSends = attrList[ulAttributeIndex + 1];
1080 if(RequestedSends > device->NumAuxSends)
1081 RequestedSends = device->NumAuxSends;
1083 numSends = RequestedSends;
1086 ulAttributeIndex += 2;
1089 device->Frequency = GetConfigValueInt(NULL, "frequency", freq);
1090 device->lNumMonoSources = numMono;
1091 device->lNumStereoSources = numStereo;
1092 device->NumAuxSends = numSends;
1095 if(ALCdevice_StartContext(device, ALContext) == ALC_FALSE)
1097 alcDestroyContext(ALContext);
1098 ALContext = NULL;
1099 SetALCError(ALC_INVALID_VALUE);
1101 else
1102 ALContext->Frequency = device->Frequency;
1104 ProcessContext(NULL);
1106 else
1108 SetALCError(ALC_INVALID_VALUE);
1109 ALContext = NULL;
1112 else
1113 SetALCError(ALC_INVALID_DEVICE);
1115 return ALContext;
1120 alcDestroyContext
1122 Remove a Context
1124 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
1126 ALCcontext **list;
1128 InitAL();
1130 if (IsContext(context))
1132 ALCdevice_StopContext(context->Device, context);
1134 // Lock context
1135 SuspendContext(context);
1137 ReleaseALSources(context);
1138 ReleaseALAuxiliaryEffectSlots(context);
1140 context->Device->Context = NULL;
1142 list = &g_pContextList;
1143 while(*list != context)
1144 list = &(*list)->next;
1146 *list = (*list)->next;
1147 g_ulContextCount--;
1149 // Unlock context
1150 ProcessContext(context);
1152 ExitContext(context);
1154 // Free memory (MUST do this after ProcessContext)
1155 memset(context, 0, sizeof(ALCcontext));
1156 free(context);
1158 else
1159 SetALCError(ALC_INVALID_CONTEXT);
1164 alcGetCurrentContext
1166 Returns the currently active Context
1168 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1170 ALCcontext *pContext = NULL;
1172 InitAL();
1174 SuspendContext(NULL);
1176 pContext = g_pContextList;
1177 while ((pContext) && (!pContext->InUse))
1178 pContext = pContext->next;
1180 ProcessContext(NULL);
1182 return pContext;
1187 alcGetContextsDevice
1189 Returns the Device that a particular Context is attached to
1191 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1193 ALCdevice *pDevice = NULL;
1195 InitAL();
1197 SuspendContext(NULL);
1198 if (IsContext(pContext))
1199 pDevice = pContext->Device;
1200 else
1201 SetALCError(ALC_INVALID_CONTEXT);
1202 ProcessContext(NULL);
1204 return pDevice;
1209 alcMakeContextCurrent
1211 Makes the given Context the active Context
1213 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1215 ALCcontext *ALContext;
1216 ALboolean bReturn = AL_TRUE;
1218 InitAL();
1220 SuspendContext(NULL);
1222 // context must be a valid Context or NULL
1223 if ((IsContext(context)) || (context == NULL))
1225 if ((ALContext=alcGetCurrentContext()))
1227 SuspendContext(ALContext);
1228 ALContext->InUse=AL_FALSE;
1229 ProcessContext(ALContext);
1232 if ((ALContext=context) && (ALContext->Device))
1234 SuspendContext(ALContext);
1235 ALContext->InUse=AL_TRUE;
1236 ProcessContext(ALContext);
1239 else
1241 SetALCError(ALC_INVALID_CONTEXT);
1242 bReturn = AL_FALSE;
1245 ProcessContext(NULL);
1247 return bReturn;
1252 alcOpenDevice
1254 Open the Device specified.
1256 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1258 ALboolean bDeviceFound = AL_FALSE;
1259 ALCdevice *device;
1260 ALint i;
1262 InitAL();
1264 if(deviceName && !deviceName[0])
1265 deviceName = NULL;
1267 device = malloc(sizeof(ALCdevice));
1268 if (device)
1270 const char *fmt;
1272 //Initialise device structure
1273 memset(device, 0, sizeof(ALCdevice));
1275 //Validate device
1276 device->IsCaptureDevice = AL_FALSE;
1278 //Set output format
1279 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1280 if(device->Frequency == 0)
1281 device->Frequency = SWMIXER_OUTPUT_RATE;
1283 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1284 if(fmt[0])
1285 device->Format = alGetEnumValue(fmt);
1287 if(!aluChannelsFromFormat(device->Format))
1288 device->Format = AL_FORMAT_STEREO16;
1290 device->BufferSize = GetConfigValueInt(NULL, "refresh", 4096);
1291 if((ALint)device->BufferSize <= 0)
1292 device->BufferSize = 4096;
1294 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1295 if((ALint)device->MaxNoOfSources <= 0)
1296 device->MaxNoOfSources = 256;
1298 device->AuxiliaryEffectSlotMax = GetConfigValueInt(NULL, "slots", 4);
1299 if((ALint)device->AuxiliaryEffectSlotMax <= 0)
1300 device->AuxiliaryEffectSlotMax = 4;
1302 device->lNumStereoSources = 1;
1303 device->lNumMonoSources = device->MaxNoOfSources - device->lNumStereoSources;
1305 device->NumAuxSends = GetConfigValueInt(NULL, "sends", MAX_SENDS);
1306 if(device->NumAuxSends > MAX_SENDS)
1307 device->NumAuxSends = MAX_SENDS;
1309 // Find a playback device to open
1310 SuspendContext(NULL);
1311 for(i = 0;BackendList[i].Init;i++)
1313 device->Funcs = &BackendList[i].Funcs;
1314 if(ALCdevice_OpenPlayback(device, deviceName))
1316 device->next = g_pDeviceList;
1317 g_pDeviceList = device;
1318 g_ulDeviceCount++;
1320 bDeviceFound = AL_TRUE;
1321 break;
1324 ProcessContext(NULL);
1326 if (!bDeviceFound)
1328 // No suitable output device found
1329 SetALCError(ALC_INVALID_VALUE);
1330 free(device);
1331 device = NULL;
1334 else
1335 SetALCError(ALC_OUT_OF_MEMORY);
1337 return device;
1342 alcCloseDevice
1344 Close the specified Device
1346 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1348 ALCboolean bReturn = ALC_FALSE;
1349 ALCdevice **list;
1351 if ((pDevice)&&(!pDevice->IsCaptureDevice))
1353 SuspendContext(NULL);
1355 list = &g_pDeviceList;
1356 while(*list != pDevice)
1357 list = &(*list)->next;
1359 *list = (*list)->next;
1360 g_ulDeviceCount--;
1362 ProcessContext(NULL);
1364 if(pDevice->Context)
1366 #ifdef _DEBUG
1367 AL_PRINT("alcCloseDevice(): destroying 1 Context\n");
1368 #endif
1369 alcDestroyContext(pDevice->Context);
1371 ALCdevice_ClosePlayback(pDevice);
1373 //Release device structure
1374 memset(pDevice, 0, sizeof(ALCdevice));
1375 free(pDevice);
1377 bReturn = ALC_TRUE;
1379 else
1380 SetALCError(ALC_INVALID_DEVICE);
1382 return bReturn;
1386 ALCvoid ReleaseALC(ALCvoid)
1388 #ifdef _DEBUG
1389 if(g_ulDeviceCount > 0)
1390 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
1391 #endif
1393 while(g_pDeviceList)
1395 if(g_pDeviceList->IsCaptureDevice)
1396 alcCaptureCloseDevice(g_pDeviceList);
1397 else
1398 alcCloseDevice(g_pDeviceList);
1402 ///////////////////////////////////////////////////////