Verify the device pointer is a valid device
[openal-soft.git] / Alc / ALc.c
blob7143436fa26afdb4b07898b717b69b6662795d77
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 FreeALConfig();
220 ALTHUNK_EXIT();
221 DeleteCriticalSection(&g_csMutex);
222 break;
224 return TRUE;
226 #else
227 #ifdef HAVE_GCC_DESTRUCTOR
228 static void my_deinit() __attribute__((destructor));
229 static void my_deinit()
231 static ALenum once = AL_FALSE;
232 if(once || !init_done) return;
233 once = AL_TRUE;
235 ReleaseALC();
236 FreeALConfig();
237 ALTHUNK_EXIT();
238 DeleteCriticalSection(&g_csMutex);
240 #endif
241 #endif
243 static void InitAL(void)
245 if(!init_done)
247 int i;
248 const char *devs, *str;
250 init_done = AL_TRUE;
252 InitializeCriticalSection(&g_csMutex);
253 ALTHUNK_INIT();
254 ReadALConfig();
256 devs = GetConfigValue(NULL, "drivers", "");
257 if(devs[0])
259 int n;
260 size_t len;
261 const char *next = devs;
263 i = 0;
265 do {
266 devs = next;
267 next = strchr(devs, ',');
269 if(!devs[0] || devs[0] == ',')
270 continue;
272 len = (next ? ((size_t)(next-devs)) : strlen(devs));
273 for(n = i;BackendList[n].Init;n++)
275 if(len == strlen(BackendList[n].name) &&
276 strncmp(BackendList[n].name, devs, len) == 0)
278 const char *name = BackendList[i].name;
279 void (*Init)(BackendFuncs*) = BackendList[i].Init;
281 BackendList[i].name = BackendList[n].name;
282 BackendList[i].Init = BackendList[n].Init;
284 BackendList[n].name = name;
285 BackendList[n].Init = Init;
287 i++;
290 } while(next++);
292 BackendList[i].name = NULL;
293 BackendList[i].Init = NULL;
296 for(i = 0;BackendList[i].Init;i++)
297 BackendList[i].Init(&BackendList[i].Funcs);
299 str = GetConfigValue(NULL, "stereodup", "false");
300 DuplicateStereo = (strcasecmp(str, "true") == 0 ||
301 strcasecmp(str, "yes") == 0 ||
302 strcasecmp(str, "on") == 0 ||
303 atoi(str) != 0);
305 str = GetConfigValue(NULL, "excludefx", "");
306 if(str[0])
308 const struct {
309 const char *name;
310 int type;
311 } EffectList[] = {
312 { "eaxreverb", EAXREVERB },
313 { "reverb", REVERB },
314 { "echo", ECHO },
315 { NULL, 0 }
317 int n;
318 size_t len;
319 const char *next = str;
321 do {
322 str = next;
323 next = strchr(str, ',');
325 if(!str[0] || next == str)
326 continue;
328 len = (next ? ((size_t)(next-str)) : strlen(str));
329 for(n = 0;EffectList[n].name;n++)
331 if(len == strlen(EffectList[n].name) &&
332 strncmp(EffectList[n].name, str, len) == 0)
333 DisabledEffects[EffectList[n].type] = AL_TRUE;
335 } while(next++);
340 ALCchar *AppendDeviceList(char *name)
342 static size_t pos;
343 ALCchar *ret = alcDeviceList+pos;
344 if(pos >= sizeof(alcDeviceList))
346 AL_PRINT("Not enough room to add %s!\n", name);
347 return alcDeviceList + sizeof(alcDeviceList) - 1;
349 pos += snprintf(alcDeviceList+pos, sizeof(alcDeviceList)-pos-1, "%s", name) + 1;
350 return ret;
353 ALCchar *AppendAllDeviceList(char *name)
355 static size_t pos;
356 ALCchar *ret = alcAllDeviceList+pos;
357 if(pos >= sizeof(alcAllDeviceList))
359 AL_PRINT("Not enough room to add %s!\n", name);
360 return alcAllDeviceList + sizeof(alcAllDeviceList) - 1;
362 pos += snprintf(alcAllDeviceList+pos, sizeof(alcAllDeviceList)-pos-1, "%s", name) + 1;
363 return ret;
366 ALCchar *AppendCaptureDeviceList(char *name)
368 static size_t pos;
369 ALCchar *ret = alcCaptureDeviceList+pos;
370 if(pos >= sizeof(alcCaptureDeviceList))
372 AL_PRINT("Not enough room to add %s!\n", name);
373 return alcCaptureDeviceList + sizeof(alcCaptureDeviceList) - 1;
375 pos += snprintf(alcCaptureDeviceList+pos, sizeof(alcCaptureDeviceList)-pos-1, "%s", name) + 1;
376 return ret;
380 IsDevice
382 Check pDevice is a valid Device pointer
384 static ALCboolean IsDevice(ALCdevice *pDevice)
386 ALCdevice *pTempDevice;
388 SuspendContext(NULL);
390 pTempDevice = g_pDeviceList;
391 while(pTempDevice && pTempDevice != pDevice)
392 pTempDevice = pTempDevice->next;
394 ProcessContext(NULL);
396 return (pTempDevice ? ALC_TRUE : ALC_FALSE);
400 IsContext
402 Check pContext is a valid Context pointer
404 static ALCboolean IsContext(ALCcontext *pContext)
406 ALCcontext *pTempContext;
408 SuspendContext(NULL);
410 pTempContext = g_pContextList;
411 while (pTempContext && pTempContext != pContext)
412 pTempContext = pTempContext->next;
414 ProcessContext(NULL);
416 return (pTempContext ? ALC_TRUE : ALC_FALSE);
421 SetALCError
423 Store latest ALC Error
425 ALCvoid SetALCError(ALenum errorCode)
427 g_eLastContextError = errorCode;
432 SuspendContext
434 Thread-safe entry
436 ALCvoid SuspendContext(ALCcontext *pContext)
438 (void)pContext;
439 EnterCriticalSection(&g_csMutex);
444 ProcessContext
446 Thread-safe exit
448 ALCvoid ProcessContext(ALCcontext *pContext)
450 (void)pContext;
451 LeaveCriticalSection(&g_csMutex);
456 InitContext
458 Initialize Context variables
460 static ALvoid InitContext(ALCcontext *pContext)
462 int level;
464 //Initialise listener
465 pContext->Listener.Gain = 1.0f;
466 pContext->Listener.MetersPerUnit = 1.0f;
467 pContext->Listener.Position[0] = 0.0f;
468 pContext->Listener.Position[1] = 0.0f;
469 pContext->Listener.Position[2] = 0.0f;
470 pContext->Listener.Velocity[0] = 0.0f;
471 pContext->Listener.Velocity[1] = 0.0f;
472 pContext->Listener.Velocity[2] = 0.0f;
473 pContext->Listener.Forward[0] = 0.0f;
474 pContext->Listener.Forward[1] = 0.0f;
475 pContext->Listener.Forward[2] = -1.0f;
476 pContext->Listener.Up[0] = 0.0f;
477 pContext->Listener.Up[1] = 1.0f;
478 pContext->Listener.Up[2] = 0.0f;
480 //Validate pContext
481 pContext->LastError = AL_NO_ERROR;
482 pContext->InUse = AL_FALSE;
484 //Set output format
485 pContext->Frequency = pContext->Device->Frequency;
487 //Set globals
488 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
489 pContext->DopplerFactor = 1.0f;
490 pContext->DopplerVelocity = 1.0f;
491 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
493 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";
495 level = GetConfigValueInt(NULL, "cf_level", 0);
496 if(level > 0 && level <= 6)
498 pContext->bs2b = calloc(1, sizeof(*pContext->bs2b));
499 bs2b_set_srate(pContext->bs2b, pContext->Frequency);
500 bs2b_set_level(pContext->bs2b, level);
503 aluInitPanning(pContext);
508 ExitContext
510 Clean up Context, destroy any remaining Sources
512 static ALCvoid ExitContext(ALCcontext *pContext)
514 //Invalidate context
515 pContext->LastError = AL_NO_ERROR;
516 pContext->InUse = AL_FALSE;
518 free(pContext->bs2b);
519 pContext->bs2b = NULL;
522 ///////////////////////////////////////////////////////
525 ///////////////////////////////////////////////////////
526 // ALC Functions calls
529 // This should probably move to another c file but for now ...
530 ALCAPI ALCdevice* ALCAPIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
532 ALCboolean DeviceFound = ALC_FALSE;
533 ALCdevice *pDevice = NULL;
534 ALCint i;
536 InitAL();
538 if(SampleSize <= 0)
540 SetALCError(ALC_INVALID_VALUE);
541 return NULL;
544 if(deviceName && !deviceName[0])
545 deviceName = NULL;
547 pDevice = malloc(sizeof(ALCdevice));
548 if (pDevice)
550 //Initialise device structure
551 memset(pDevice, 0, sizeof(ALCdevice));
553 //Validate device
554 pDevice->IsCaptureDevice = AL_TRUE;
556 pDevice->Frequency = frequency;
557 pDevice->Format = format;
558 pDevice->BufferSize = SampleSize;
560 SuspendContext(NULL);
561 for(i = 0;BackendList[i].Init;i++)
563 pDevice->Funcs = &BackendList[i].Funcs;
564 if(ALCdevice_OpenCapture(pDevice, deviceName))
566 pDevice->next = g_pDeviceList;
567 g_pDeviceList = pDevice;
568 g_ulDeviceCount++;
570 DeviceFound = ALC_TRUE;
571 break;
574 ProcessContext(NULL);
576 if(!DeviceFound)
578 SetALCError(ALC_INVALID_VALUE);
579 free(pDevice);
580 pDevice = NULL;
583 else
584 SetALCError(ALC_OUT_OF_MEMORY);
586 return pDevice;
589 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
591 ALCboolean bReturn = ALC_FALSE;
592 ALCdevice **list;
594 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
596 SuspendContext(NULL);
598 list = &g_pDeviceList;
599 while(*list != pDevice)
600 list = &(*list)->next;
602 *list = (*list)->next;
603 g_ulDeviceCount--;
605 ProcessContext(NULL);
607 ALCdevice_CloseCapture(pDevice);
608 free(pDevice);
610 bReturn = ALC_TRUE;
612 else
613 SetALCError(ALC_INVALID_DEVICE);
615 return bReturn;
618 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
620 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
621 ALCdevice_StartCapture(pDevice);
622 else
623 SetALCError(ALC_INVALID_DEVICE);
626 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
628 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
629 ALCdevice_StopCapture(pDevice);
630 else
631 SetALCError(ALC_INVALID_DEVICE);
634 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
636 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
637 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
638 else
639 SetALCError(ALC_INVALID_DEVICE);
643 alcGetError
645 Return last ALC generated error code
647 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
649 ALCenum errorCode;
651 (void)device;
653 errorCode = g_eLastContextError;
654 g_eLastContextError = ALC_NO_ERROR;
655 return errorCode;
660 alcSuspendContext
662 Not functional
664 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
666 // Not a lot happens here !
667 (void)pContext;
672 alcProcessContext
674 Not functional
676 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
678 // Not a lot happens here !
679 (void)pContext;
684 alcGetString
686 Returns information about the Device, and error strings
688 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
690 const ALCchar *value = NULL;
692 InitAL();
694 switch (param)
696 case ALC_NO_ERROR:
697 value = alcNoError;
698 break;
700 case ALC_INVALID_ENUM:
701 value = alcErrInvalidEnum;
702 break;
704 case ALC_INVALID_VALUE:
705 value = alcErrInvalidValue;
706 break;
708 case ALC_INVALID_DEVICE:
709 value = alcErrInvalidDevice;
710 break;
712 case ALC_INVALID_CONTEXT:
713 value = alcErrInvalidContext;
714 break;
716 case ALC_OUT_OF_MEMORY:
717 value = alcErrOutOfMemory;
718 break;
720 case ALC_DEFAULT_DEVICE_SPECIFIER:
721 value = alcDefaultDeviceSpecifier;
722 break;
724 case ALC_DEVICE_SPECIFIER:
725 if(IsDevice(pDevice))
726 value = pDevice->szDeviceName;
727 else
728 value = alcDeviceList;
729 break;
731 case ALC_ALL_DEVICES_SPECIFIER:
732 value = alcAllDeviceList;
733 break;
735 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
736 value = alcDefaultAllDeviceSpecifier;
737 break;
739 case ALC_CAPTURE_DEVICE_SPECIFIER:
740 if(IsDevice(pDevice))
741 value = pDevice->szDeviceName;
742 else
743 value = alcCaptureDeviceList;
744 break;
746 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
747 value = alcCaptureDefaultDeviceSpecifier;
748 break;
750 case ALC_EXTENSIONS:
751 value = alcExtensionList;
752 break;
754 default:
755 SetALCError(ALC_INVALID_ENUM);
756 break;
759 return value;
764 alcGetIntegerv
766 Returns information about the Device and the version of Open AL
768 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
770 InitAL();
772 if(IsDevice(device) && device->IsCaptureDevice)
774 SuspendContext(NULL);
776 // Capture device
777 switch (param)
779 case ALC_CAPTURE_SAMPLES:
780 if ((size) && (data))
781 *data = ALCdevice_AvailableSamples(device);
782 else
783 SetALCError(ALC_INVALID_VALUE);
784 break;
786 default:
787 SetALCError(ALC_INVALID_ENUM);
788 break;
791 ProcessContext(NULL);
793 else
795 if(data)
797 // Playback Device
798 switch (param)
800 case ALC_MAJOR_VERSION:
801 if(!size)
802 SetALCError(ALC_INVALID_VALUE);
803 else
804 *data = alcMajorVersion;
805 break;
807 case ALC_MINOR_VERSION:
808 if(!size)
809 SetALCError(ALC_INVALID_VALUE);
810 else
811 *data = alcMinorVersion;
812 break;
814 case ALC_EFX_MAJOR_VERSION:
815 if(!size)
816 SetALCError(ALC_INVALID_VALUE);
817 else
818 *data = alcEFXMajorVersion;
819 break;
821 case ALC_EFX_MINOR_VERSION:
822 if(!size)
823 SetALCError(ALC_INVALID_VALUE);
824 else
825 *data = alcEFXMinorVersion;
826 break;
828 case ALC_MAX_AUXILIARY_SENDS:
829 if(!size)
830 SetALCError(ALC_INVALID_VALUE);
831 else
832 *data = (device?device->NumAuxSends:MAX_SENDS);
833 break;
835 case ALC_ATTRIBUTES_SIZE:
836 if(!IsDevice(device))
837 SetALCError(ALC_INVALID_DEVICE);
838 else if(!size)
839 SetALCError(ALC_INVALID_VALUE);
840 else
841 *data = 13;
842 break;
844 case ALC_ALL_ATTRIBUTES:
845 if(!IsDevice(device))
846 SetALCError(ALC_INVALID_DEVICE);
847 else if (size < 13)
848 SetALCError(ALC_INVALID_VALUE);
849 else
851 int i = 0;
853 SuspendContext(NULL);
854 data[i++] = ALC_FREQUENCY;
855 data[i++] = device->Frequency;
857 data[i++] = ALC_REFRESH;
858 data[i++] = device->Frequency / device->UpdateSize;
860 data[i++] = ALC_SYNC;
861 data[i++] = ALC_FALSE;
863 data[i++] = ALC_MONO_SOURCES;
864 data[i++] = device->lNumMonoSources;
866 data[i++] = ALC_STEREO_SOURCES;
867 data[i++] = device->lNumStereoSources;
869 data[i++] = ALC_MAX_AUXILIARY_SENDS;
870 data[i++] = device->NumAuxSends;
872 data[i++] = 0;
873 ProcessContext(NULL);
875 break;
877 case ALC_FREQUENCY:
878 if(!IsDevice(device))
879 SetALCError(ALC_INVALID_DEVICE);
880 else if(!size)
881 SetALCError(ALC_INVALID_VALUE);
882 else
883 *data = device->Frequency;
884 break;
886 case ALC_REFRESH:
887 if(!IsDevice(device))
888 SetALCError(ALC_INVALID_DEVICE);
889 else if(!size)
890 SetALCError(ALC_INVALID_VALUE);
891 else
892 *data = device->Frequency / device->UpdateSize;
893 break;
895 case ALC_SYNC:
896 if(!IsDevice(device))
897 SetALCError(ALC_INVALID_DEVICE);
898 else if(!size)
899 SetALCError(ALC_INVALID_VALUE);
900 else
901 *data = ALC_FALSE;
902 break;
904 case ALC_MONO_SOURCES:
905 if(!IsDevice(device))
906 SetALCError(ALC_INVALID_DEVICE);
907 else if (size != 1)
908 SetALCError(ALC_INVALID_VALUE);
909 else
910 *data = device->lNumMonoSources;
911 break;
913 case ALC_STEREO_SOURCES:
914 if(!IsDevice(device))
915 SetALCError(ALC_INVALID_DEVICE);
916 else if (size != 1)
917 SetALCError(ALC_INVALID_VALUE);
918 else
919 *data = device->lNumStereoSources;
920 break;
922 default:
923 SetALCError(ALC_INVALID_ENUM);
924 break;
927 else if(size)
928 SetALCError(ALC_INVALID_VALUE);
931 return;
936 alcIsExtensionPresent
938 Determines if there is support for a particular extension
940 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
942 ALCboolean bResult = ALC_FALSE;
944 (void)device;
946 if (extName)
948 const char *ptr;
949 size_t len;
951 len = strlen(extName);
952 ptr = alcExtensionList;
953 while(ptr && *ptr)
955 if(strncasecmp(ptr, extName, len) == 0 &&
956 (ptr[len] == '\0' || isspace(ptr[len])))
958 bResult = ALC_TRUE;
959 break;
961 if((ptr=strchr(ptr, ' ')) != NULL)
963 do {
964 ++ptr;
965 } while(isspace(*ptr));
969 else
970 SetALCError(ALC_INVALID_VALUE);
972 return bResult;
977 alcGetProcAddress
979 Retrieves the function address for a particular extension function
981 ALCAPI ALCvoid * ALCAPIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
983 ALCvoid *pFunction = NULL;
984 ALsizei i = 0;
986 (void)device;
988 if (funcName)
990 while(alcFunctions[i].funcName &&
991 strcmp(alcFunctions[i].funcName,funcName) != 0)
992 i++;
993 pFunction = alcFunctions[i].address;
995 else
996 SetALCError(ALC_INVALID_VALUE);
998 return pFunction;
1003 alcGetEnumValue
1005 Get the value for a particular ALC Enumerated Value
1007 ALCAPI ALCenum ALCAPIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
1009 ALsizei i = 0;
1010 ALCenum val;
1012 (void)device;
1014 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
1015 i++;
1016 val = enumeration[i].value;
1018 if(!enumeration[i].enumName)
1019 SetALCError(ALC_INVALID_VALUE);
1021 return val;
1026 alcCreateContext
1028 Create and attach a Context to a particular Device.
1030 ALCAPI ALCcontext* ALCAPIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
1032 ALCcontext *ALContext = NULL;
1033 ALuint ulAttributeIndex, ulRequestedStereoSources;
1034 ALuint RequestedSends;
1036 if(IsDevice(device) && !device->IsCaptureDevice)
1038 // Reset Context Last Error code
1039 g_eLastContextError = ALC_NO_ERROR;
1041 // Current implementation only allows one Context per Device
1042 if(!device->Context)
1044 ALContext = calloc(1, sizeof(ALCcontext));
1045 if(!ALContext)
1047 SetALCError(ALC_OUT_OF_MEMORY);
1048 return NULL;
1051 SuspendContext(NULL);
1053 ALContext->Device = device;
1054 InitContext(ALContext);
1056 device->Context = ALContext;
1058 ALContext->next = g_pContextList;
1059 g_pContextList = ALContext;
1060 g_ulContextCount++;
1062 // Check for attributes
1063 if (attrList)
1065 ALCuint freq = device->Frequency;
1066 ALCint numMono = device->lNumMonoSources;
1067 ALCint numStereo = device->lNumStereoSources;
1068 ALCuint numSends = device->NumAuxSends;
1070 ulAttributeIndex = 0;
1071 while ((ulAttributeIndex < 10) && (attrList[ulAttributeIndex]))
1073 if(attrList[ulAttributeIndex] == ALC_FREQUENCY)
1075 freq = attrList[ulAttributeIndex + 1];
1076 if(freq == 0)
1077 freq = device->Frequency;
1080 if(attrList[ulAttributeIndex] == ALC_STEREO_SOURCES)
1082 ulRequestedStereoSources = attrList[ulAttributeIndex + 1];
1084 if (ulRequestedStereoSources > device->MaxNoOfSources)
1085 ulRequestedStereoSources = device->MaxNoOfSources;
1087 numStereo = ulRequestedStereoSources;
1088 numMono = device->MaxNoOfSources - numStereo;
1091 if(attrList[ulAttributeIndex] == ALC_MAX_AUXILIARY_SENDS)
1093 RequestedSends = attrList[ulAttributeIndex + 1];
1095 if(RequestedSends > device->NumAuxSends)
1096 RequestedSends = device->NumAuxSends;
1098 numSends = RequestedSends;
1101 ulAttributeIndex += 2;
1104 device->Frequency = GetConfigValueInt(NULL, "frequency", freq);
1105 device->lNumMonoSources = numMono;
1106 device->lNumStereoSources = numStereo;
1107 device->NumAuxSends = numSends;
1110 if(ALCdevice_StartContext(device, ALContext) == ALC_FALSE)
1112 alcDestroyContext(ALContext);
1113 ALContext = NULL;
1114 SetALCError(ALC_INVALID_VALUE);
1116 else
1117 ALContext->Frequency = device->Frequency;
1119 ProcessContext(NULL);
1121 else
1123 SetALCError(ALC_INVALID_VALUE);
1124 ALContext = NULL;
1127 else
1128 SetALCError(ALC_INVALID_DEVICE);
1130 return ALContext;
1135 alcDestroyContext
1137 Remove a Context
1139 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
1141 ALCcontext **list;
1143 InitAL();
1145 if (IsContext(context))
1147 ALCdevice_StopContext(context->Device, context);
1149 // Lock context
1150 SuspendContext(context);
1152 ReleaseALSources(context);
1153 ReleaseALAuxiliaryEffectSlots(context);
1155 context->Device->Context = NULL;
1157 list = &g_pContextList;
1158 while(*list != context)
1159 list = &(*list)->next;
1161 *list = (*list)->next;
1162 g_ulContextCount--;
1164 // Unlock context
1165 ProcessContext(context);
1167 ExitContext(context);
1169 // Free memory (MUST do this after ProcessContext)
1170 memset(context, 0, sizeof(ALCcontext));
1171 free(context);
1173 else
1174 SetALCError(ALC_INVALID_CONTEXT);
1179 alcGetCurrentContext
1181 Returns the currently active Context
1183 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1185 ALCcontext *pContext = NULL;
1187 InitAL();
1189 SuspendContext(NULL);
1191 pContext = g_pContextList;
1192 while ((pContext) && (!pContext->InUse))
1193 pContext = pContext->next;
1195 ProcessContext(NULL);
1197 return pContext;
1202 alcGetContextsDevice
1204 Returns the Device that a particular Context is attached to
1206 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1208 ALCdevice *pDevice = NULL;
1210 InitAL();
1212 SuspendContext(NULL);
1213 if (IsContext(pContext))
1214 pDevice = pContext->Device;
1215 else
1216 SetALCError(ALC_INVALID_CONTEXT);
1217 ProcessContext(NULL);
1219 return pDevice;
1224 alcMakeContextCurrent
1226 Makes the given Context the active Context
1228 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1230 ALCcontext *ALContext;
1231 ALboolean bReturn = AL_TRUE;
1233 InitAL();
1235 SuspendContext(NULL);
1237 // context must be a valid Context or NULL
1238 if ((IsContext(context)) || (context == NULL))
1240 if ((ALContext=alcGetCurrentContext()))
1242 SuspendContext(ALContext);
1243 ALContext->InUse=AL_FALSE;
1244 ProcessContext(ALContext);
1247 if ((ALContext=context) && (ALContext->Device))
1249 SuspendContext(ALContext);
1250 ALContext->InUse=AL_TRUE;
1251 ProcessContext(ALContext);
1254 else
1256 SetALCError(ALC_INVALID_CONTEXT);
1257 bReturn = AL_FALSE;
1260 ProcessContext(NULL);
1262 return bReturn;
1267 alcOpenDevice
1269 Open the Device specified.
1271 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1273 ALboolean bDeviceFound = AL_FALSE;
1274 ALCdevice *device;
1275 ALint i;
1277 InitAL();
1279 if(deviceName && !deviceName[0])
1280 deviceName = NULL;
1282 device = malloc(sizeof(ALCdevice));
1283 if (device)
1285 const char *fmt;
1287 //Initialise device structure
1288 memset(device, 0, sizeof(ALCdevice));
1290 //Validate device
1291 device->IsCaptureDevice = AL_FALSE;
1293 //Set output format
1294 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1295 if(device->Frequency == 0)
1296 device->Frequency = SWMIXER_OUTPUT_RATE;
1298 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1299 if(fmt[0])
1300 device->Format = alGetEnumValue(fmt);
1302 if(!aluChannelsFromFormat(device->Format))
1303 device->Format = AL_FORMAT_STEREO16;
1305 device->BufferSize = GetConfigValueInt(NULL, "refresh", 4096);
1306 if((ALint)device->BufferSize <= 0)
1307 device->BufferSize = 4096;
1309 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1310 if((ALint)device->MaxNoOfSources <= 0)
1311 device->MaxNoOfSources = 256;
1313 device->AuxiliaryEffectSlotMax = GetConfigValueInt(NULL, "slots", 4);
1314 if((ALint)device->AuxiliaryEffectSlotMax <= 0)
1315 device->AuxiliaryEffectSlotMax = 4;
1317 device->lNumStereoSources = 1;
1318 device->lNumMonoSources = device->MaxNoOfSources - device->lNumStereoSources;
1320 device->NumAuxSends = GetConfigValueInt(NULL, "sends", MAX_SENDS);
1321 if(device->NumAuxSends > MAX_SENDS)
1322 device->NumAuxSends = MAX_SENDS;
1324 // Find a playback device to open
1325 SuspendContext(NULL);
1326 for(i = 0;BackendList[i].Init;i++)
1328 device->Funcs = &BackendList[i].Funcs;
1329 if(ALCdevice_OpenPlayback(device, deviceName))
1331 device->next = g_pDeviceList;
1332 g_pDeviceList = device;
1333 g_ulDeviceCount++;
1335 bDeviceFound = AL_TRUE;
1336 break;
1339 ProcessContext(NULL);
1341 if (!bDeviceFound)
1343 // No suitable output device found
1344 SetALCError(ALC_INVALID_VALUE);
1345 free(device);
1346 device = NULL;
1349 else
1350 SetALCError(ALC_OUT_OF_MEMORY);
1352 return device;
1357 alcCloseDevice
1359 Close the specified Device
1361 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1363 ALCboolean bReturn = ALC_FALSE;
1364 ALCdevice **list;
1366 if(IsDevice(pDevice) && !pDevice->IsCaptureDevice)
1368 SuspendContext(NULL);
1370 list = &g_pDeviceList;
1371 while(*list != pDevice)
1372 list = &(*list)->next;
1374 *list = (*list)->next;
1375 g_ulDeviceCount--;
1377 ProcessContext(NULL);
1379 if(pDevice->Context)
1381 #ifdef _DEBUG
1382 AL_PRINT("alcCloseDevice(): destroying 1 Context\n");
1383 #endif
1384 alcDestroyContext(pDevice->Context);
1386 ALCdevice_ClosePlayback(pDevice);
1388 if(pDevice->BufferCount > 0)
1390 #ifdef _DEBUG
1391 AL_PRINT("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice->BufferCount);
1392 #endif
1393 ReleaseALBuffers(pDevice);
1395 if(pDevice->EffectCount > 0)
1397 #ifdef _DEBUG
1398 AL_PRINT("alcCloseDevice(): deleting %d Effect(s)\n", pDevice->EffectCount);
1399 #endif
1400 ReleaseALEffects(pDevice);
1402 if(pDevice->FilterCount > 0)
1404 #ifdef _DEBUG
1405 AL_PRINT("alcCloseDevice(): deleting %d Filter(s)\n", pDevice->FilterCount);
1406 #endif
1407 ReleaseALFilters(pDevice);
1410 //Release device structure
1411 memset(pDevice, 0, sizeof(ALCdevice));
1412 free(pDevice);
1414 bReturn = ALC_TRUE;
1416 else
1417 SetALCError(ALC_INVALID_DEVICE);
1419 return bReturn;
1423 ALCvoid ReleaseALC(ALCvoid)
1425 #ifdef _DEBUG
1426 if(g_ulDeviceCount > 0)
1427 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
1428 #endif
1430 while(g_pDeviceList)
1432 if(g_pDeviceList->IsCaptureDevice)
1433 alcCaptureCloseDevice(g_pDeviceList);
1434 else
1435 alcCloseDevice(g_pDeviceList);
1439 ///////////////////////////////////////////////////////