Release 1.5.304
[openal-soft/openal-hmr.git] / Alc / ALc.c
blob971eb8270340037fb60baca01cc4a833e0dfe03b
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"
41 ///////////////////////////////////////////////////////
42 // DEBUG INFORMATION
44 char _alDebug[256];
46 ///////////////////////////////////////////////////////
49 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
50 static struct {
51 const char *name;
52 void (*Init)(BackendFuncs*);
53 BackendFuncs Funcs;
54 } BackendList[] = {
55 #ifdef HAVE_ALSA
56 { "alsa", alc_alsa_init, EmptyFuncs },
57 #endif
58 #ifdef HAVE_OSS
59 { "oss", alc_oss_init, EmptyFuncs },
60 #endif
61 #ifdef HAVE_DSOUND
62 { "dsound", alcDSoundInit, EmptyFuncs },
63 #endif
64 #ifdef HAVE_WINMM
65 { "winmm", alcWinMMInit, EmptyFuncs },
66 #endif
68 { "wave", alc_wave_init, EmptyFuncs },
70 { NULL, NULL, EmptyFuncs }
72 #undef EmptyFuncs
74 ///////////////////////////////////////////////////////
76 #define ALC_EFX_MAJOR_VERSION 0x20001
77 #define ALC_EFX_MINOR_VERSION 0x20002
78 #define ALC_MAX_AUXILIARY_SENDS 0x20003
80 ///////////////////////////////////////////////////////
81 // STRING and EXTENSIONS
83 typedef struct ALCfunction_struct
85 ALCchar *funcName;
86 ALvoid *address;
87 } ALCfunction;
89 static ALCfunction alcFunctions[] = {
90 { "alcCreateContext", (ALvoid *) alcCreateContext },
91 { "alcMakeContextCurrent", (ALvoid *) alcMakeContextCurrent },
92 { "alcProcessContext", (ALvoid *) alcProcessContext },
93 { "alcSuspendContext", (ALvoid *) alcSuspendContext },
94 { "alcDestroyContext", (ALvoid *) alcDestroyContext },
95 { "alcGetCurrentContext", (ALvoid *) alcGetCurrentContext },
96 { "alcGetContextsDevice", (ALvoid *) alcGetContextsDevice },
97 { "alcOpenDevice", (ALvoid *) alcOpenDevice },
98 { "alcCloseDevice", (ALvoid *) alcCloseDevice },
99 { "alcGetError", (ALvoid *) alcGetError },
100 { "alcIsExtensionPresent", (ALvoid *) alcIsExtensionPresent },
101 { "alcGetProcAddress", (ALvoid *) alcGetProcAddress },
102 { "alcGetEnumValue", (ALvoid *) alcGetEnumValue },
103 { "alcGetString", (ALvoid *) alcGetString },
104 { "alcGetIntegerv", (ALvoid *) alcGetIntegerv },
105 { "alcCaptureOpenDevice", (ALvoid *) alcCaptureOpenDevice },
106 { "alcCaptureCloseDevice", (ALvoid *) alcCaptureCloseDevice },
107 { "alcCaptureStart", (ALvoid *) alcCaptureStart },
108 { "alcCaptureStop", (ALvoid *) alcCaptureStop },
109 { "alcCaptureSamples", (ALvoid *) alcCaptureSamples },
110 { NULL, (ALvoid *) NULL }
113 static ALenums enumeration[]={
114 // Types
115 { (ALchar *)"ALC_INVALID", ALC_INVALID },
116 { (ALchar *)"ALC_FALSE", ALC_FALSE },
117 { (ALchar *)"ALC_TRUE", ALC_TRUE },
119 // ALC Properties
120 { (ALchar *)"ALC_MAJOR_VERSION", ALC_MAJOR_VERSION },
121 { (ALchar *)"ALC_MINOR_VERSION", ALC_MINOR_VERSION },
122 { (ALchar *)"ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE },
123 { (ALchar *)"ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES },
124 { (ALchar *)"ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER },
125 { (ALchar *)"ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER },
126 { (ALchar *)"ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER },
127 { (ALchar *)"ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER },
128 { (ALchar *)"ALC_EXTENSIONS", ALC_EXTENSIONS },
129 { (ALchar *)"ALC_FREQUENCY", ALC_FREQUENCY },
130 { (ALchar *)"ALC_REFRESH", ALC_REFRESH },
131 { (ALchar *)"ALC_SYNC", ALC_SYNC },
132 { (ALchar *)"ALC_MONO_SOURCES", ALC_MONO_SOURCES },
133 { (ALchar *)"ALC_STEREO_SOURCES", ALC_STEREO_SOURCES },
134 { (ALchar *)"ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER },
135 { (ALchar *)"ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER},
136 { (ALchar *)"ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES },
138 // EFX Properties
139 { (ALchar *)"ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION },
140 { (ALchar *)"ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION },
141 { (ALchar *)"ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS },
143 // ALC Error Message
144 { (ALchar *)"ALC_NO_ERROR", ALC_NO_ERROR },
145 { (ALchar *)"ALC_INVALID_DEVICE", ALC_INVALID_DEVICE },
146 { (ALchar *)"ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT },
147 { (ALchar *)"ALC_INVALID_ENUM", ALC_INVALID_ENUM },
148 { (ALchar *)"ALC_INVALID_VALUE", ALC_INVALID_VALUE },
149 { (ALchar *)"ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY },
150 { (ALchar *)NULL, (ALenum)0 }
152 // Error strings
153 static const ALCchar alcNoError[] = "No Error";
154 static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
155 static const ALCchar alcErrInvalidContext[] = "Invalid Context";
156 static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
157 static const ALCchar alcErrInvalidValue[] = "Invalid Value";
158 static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
160 // Context strings
161 static ALCchar alcDeviceList[2048];
162 static ALCchar alcAllDeviceList[2048];
163 static ALCchar alcCaptureDeviceList[2048];
164 // Default is always the first in the list
165 static ALCchar *alcDefaultDeviceSpecifier = alcDeviceList;
166 static ALCchar *alcDefaultAllDeviceSpecifier = alcAllDeviceList;
167 static ALCchar *alcCaptureDefaultDeviceSpecifier = alcCaptureDeviceList;
170 static ALCchar alcExtensionList[] = "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_EFX";
171 static ALCint alcMajorVersion = 1;
172 static ALCint alcMinorVersion = 1;
174 static ALCint alcEFXMajorVersion = 1;
175 static ALCint alcEFXMinorVersion = 0;
177 ///////////////////////////////////////////////////////
180 ///////////////////////////////////////////////////////
181 // Global Variables
183 static ALCdevice *g_pDeviceList = NULL;
184 static ALCuint g_ulDeviceCount = 0;
186 static CRITICAL_SECTION g_csMutex;
188 // Context List
189 static ALCcontext *g_pContextList = NULL;
190 static ALCuint g_ulContextCount = 0;
192 // Context Error
193 static ALCenum g_eLastContextError = ALC_NO_ERROR;
195 static ALboolean init_done = AL_FALSE;
197 ///////////////////////////////////////////////////////
200 ///////////////////////////////////////////////////////
201 // ALC Related helper functions
202 #ifdef _WIN32
203 BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
205 (void)lpReserved;
207 // Perform actions based on the reason for calling.
208 switch(ul_reason_for_call)
210 case DLL_PROCESS_ATTACH:
211 DisableThreadLibraryCalls(hModule);
212 break;
214 case DLL_PROCESS_DETACH:
215 if(!init_done)
216 break;
217 ReleaseALC();
218 ReleaseALBuffers();
219 ReleaseALEffects();
220 ReleaseALFilters();
221 FreeALConfig();
222 ALTHUNK_EXIT();
223 DeleteCriticalSection(&g_csMutex);
224 break;
226 return TRUE;
228 #else
229 #ifdef HAVE_GCC_DESTRUCTOR
230 static void my_deinit() __attribute__((destructor));
231 static void my_deinit()
233 static ALenum once = AL_FALSE;
234 if(once || !init_done) return;
235 once = AL_TRUE;
237 ReleaseALC();
238 ReleaseALBuffers();
239 ReleaseALEffects();
240 ReleaseALFilters();
241 FreeALConfig();
242 ALTHUNK_EXIT();
243 DeleteCriticalSection(&g_csMutex);
245 #endif
246 #endif
248 static void InitAL(void)
250 if(!init_done)
252 int i;
253 const char *devs, *str;
255 init_done = AL_TRUE;
257 InitializeCriticalSection(&g_csMutex);
258 ALTHUNK_INIT();
259 ReadALConfig();
261 devs = GetConfigValue(NULL, "drivers", "");
262 if(devs[0])
264 int n;
265 size_t len;
266 const char *next = devs;
268 i = 0;
270 do {
271 devs = next;
272 next = strchr(devs, ',');
274 if(!devs[0] || devs[0] == ',')
275 continue;
277 len = (next ? ((size_t)(next-devs)) : strlen(devs));
278 for(n = i;BackendList[n].Init;n++)
280 if(len == strlen(BackendList[n].name) &&
281 strncmp(BackendList[n].name, devs, len) == 0)
283 const char *name = BackendList[i].name;
284 void (*Init)(BackendFuncs*) = BackendList[i].Init;
286 BackendList[i].name = BackendList[n].name;
287 BackendList[i].Init = BackendList[n].Init;
289 BackendList[n].name = name;
290 BackendList[n].Init = Init;
292 i++;
295 } while(next++);
297 BackendList[i].name = NULL;
298 BackendList[i].Init = NULL;
301 for(i = 0;BackendList[i].Init;i++)
302 BackendList[i].Init(&BackendList[i].Funcs);
304 str = GetConfigValue(NULL, "stereodup", "false");
305 DuplicateStereo = (strcasecmp(str, "true") == 0 ||
306 strcasecmp(str, "yes") == 0 ||
307 strcasecmp(str, "on") == 0 ||
308 atoi(str) != 0);
312 ALCchar *AppendDeviceList(char *name)
314 static size_t pos;
315 ALCchar *ret = alcDeviceList+pos;
316 if(pos >= sizeof(alcDeviceList))
318 AL_PRINT("Not enough room to add %s!\n", name);
319 return alcDeviceList + sizeof(alcDeviceList) - 1;
321 pos += snprintf(alcDeviceList+pos, sizeof(alcDeviceList)-pos-1, "%s", name) + 1;
322 return ret;
325 ALCchar *AppendAllDeviceList(char *name)
327 static size_t pos;
328 ALCchar *ret = alcAllDeviceList+pos;
329 if(pos >= sizeof(alcAllDeviceList))
331 AL_PRINT("Not enough room to add %s!\n", name);
332 return alcAllDeviceList + sizeof(alcAllDeviceList) - 1;
334 pos += snprintf(alcAllDeviceList+pos, sizeof(alcAllDeviceList)-pos-1, "%s", name) + 1;
335 return ret;
338 ALCchar *AppendCaptureDeviceList(char *name)
340 static size_t pos;
341 ALCchar *ret = alcCaptureDeviceList+pos;
342 if(pos >= sizeof(alcCaptureDeviceList))
344 AL_PRINT("Not enough room to add %s!\n", name);
345 return alcCaptureDeviceList + sizeof(alcCaptureDeviceList) - 1;
347 pos += snprintf(alcCaptureDeviceList+pos, sizeof(alcCaptureDeviceList)-pos-1, "%s", name) + 1;
348 return ret;
352 IsContext
354 Check pContext is a valid Context pointer
356 static ALCboolean IsContext(ALCcontext *pContext)
358 ALCcontext *pTempContext;
360 pTempContext = g_pContextList;
361 while (pTempContext && pTempContext != pContext)
362 pTempContext = pTempContext->next;
364 return (pTempContext ? ALC_TRUE : ALC_FALSE);
369 SetALCError
371 Store latest ALC Error
373 ALCvoid SetALCError(ALenum errorCode)
375 g_eLastContextError = errorCode;
380 SuspendContext
382 Thread-safe entry
384 ALCvoid SuspendContext(ALCcontext *pContext)
386 (void)pContext;
387 EnterCriticalSection(&g_csMutex);
392 ProcessContext
394 Thread-safe exit
396 ALCvoid ProcessContext(ALCcontext *pContext)
398 (void)pContext;
399 LeaveCriticalSection(&g_csMutex);
404 InitContext
406 Initialize Context variables
408 static ALvoid InitContext(ALCcontext *pContext)
410 int level;
412 //Initialise listener
413 pContext->Listener.Gain = 1.0f;
414 pContext->Listener.MetersPerUnit = 1.0f;
415 pContext->Listener.Position[0] = 0.0f;
416 pContext->Listener.Position[1] = 0.0f;
417 pContext->Listener.Position[2] = 0.0f;
418 pContext->Listener.Velocity[0] = 0.0f;
419 pContext->Listener.Velocity[1] = 0.0f;
420 pContext->Listener.Velocity[2] = 0.0f;
421 pContext->Listener.Forward[0] = 0.0f;
422 pContext->Listener.Forward[1] = 0.0f;
423 pContext->Listener.Forward[2] = -1.0f;
424 pContext->Listener.Up[0] = 0.0f;
425 pContext->Listener.Up[1] = 1.0f;
426 pContext->Listener.Up[2] = 0.0f;
428 //Validate pContext
429 pContext->LastError = AL_NO_ERROR;
430 pContext->InUse = AL_FALSE;
432 //Set output format
433 pContext->Frequency = pContext->Device->Frequency;
435 //Set globals
436 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
437 pContext->DopplerFactor = 1.0f;
438 pContext->DopplerVelocity = 1.0f;
439 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
441 pContext->lNumStereoSources = 1;
442 pContext->lNumMonoSources = pContext->Device->MaxNoOfSources - pContext->lNumStereoSources;
444 pContext->ExtensionList = "AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_OFFSET AL_LOKI_quadriphonic";
446 level = GetConfigValueInt(NULL, "cf_level", 0);
447 if(level > 0 && level <= 6)
449 pContext->bs2b = calloc(1, sizeof(*pContext->bs2b));
450 bs2b_set_srate(pContext->bs2b, pContext->Frequency);
451 bs2b_set_level(pContext->bs2b, level);
457 ExitContext
459 Clean up Context, destroy any remaining Sources
461 static ALCvoid ExitContext(ALCcontext *pContext)
463 //Invalidate context
464 pContext->LastError = AL_NO_ERROR;
465 pContext->InUse = AL_FALSE;
467 free(pContext->bs2b);
468 pContext->bs2b = NULL;
471 ///////////////////////////////////////////////////////
474 ///////////////////////////////////////////////////////
475 // ALC Functions calls
478 // This should probably move to another c file but for now ...
479 ALCAPI ALCdevice* ALCAPIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
481 ALCboolean DeviceFound = ALC_FALSE;
482 ALCdevice *pDevice = NULL;
483 ALCint i;
485 InitAL();
487 if(deviceName && !deviceName[0])
488 deviceName = NULL;
490 pDevice = malloc(sizeof(ALCdevice));
491 if (pDevice)
493 if (SampleSize > 0)
495 //Initialise device structure
496 memset(pDevice, 0, sizeof(ALCdevice));
498 //Validate device
499 pDevice->IsCaptureDevice = AL_TRUE;
501 pDevice->Frequency = frequency;
502 pDevice->Format = format;
504 for(i = 0;BackendList[i].Init;i++)
506 pDevice->Funcs = &BackendList[i].Funcs;
507 if(ALCdevice_OpenCapture(pDevice, deviceName, frequency, format, SampleSize))
509 SuspendContext(NULL);
510 pDevice->next = g_pDeviceList;
511 g_pDeviceList = pDevice;
512 g_ulDeviceCount++;
513 ProcessContext(NULL);
515 DeviceFound = ALC_TRUE;
516 break;
520 else
521 SetALCError(ALC_INVALID_VALUE);
523 if(!DeviceFound)
525 free(pDevice);
526 pDevice = NULL;
529 else
530 SetALCError(ALC_OUT_OF_MEMORY);
532 return pDevice;
535 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
537 ALCboolean bReturn = ALC_FALSE;
538 ALCdevice **list;
540 if ((pDevice)&&(pDevice->IsCaptureDevice))
542 SuspendContext(NULL);
544 list = &g_pDeviceList;
545 while(*list != pDevice)
546 list = &(*list)->next;
548 *list = (*list)->next;
549 g_ulDeviceCount--;
551 ProcessContext(NULL);
553 ALCdevice_CloseCapture(pDevice);
554 free(pDevice);
556 bReturn = ALC_TRUE;
558 else
559 SetALCError(ALC_INVALID_DEVICE);
561 return bReturn;
564 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
566 if ((pDevice)&&(pDevice->IsCaptureDevice))
567 ALCdevice_StartCapture(pDevice);
568 else
569 SetALCError(ALC_INVALID_DEVICE);
572 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
574 if ((pDevice)&&(pDevice->IsCaptureDevice))
575 ALCdevice_StopCapture(pDevice);
576 else
577 SetALCError(ALC_INVALID_DEVICE);
580 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
582 if ((pDevice) && (pDevice->IsCaptureDevice))
583 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
584 else
585 SetALCError(ALC_INVALID_DEVICE);
589 alcGetError
591 Return last ALC generated error code
593 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
595 ALCenum errorCode;
597 (void)device;
599 errorCode = g_eLastContextError;
600 g_eLastContextError = ALC_NO_ERROR;
601 return errorCode;
606 alcSuspendContext
608 Not functional
610 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
612 // Not a lot happens here !
613 (void)pContext;
618 alcProcessContext
620 Not functional
622 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
624 // Not a lot happens here !
625 (void)pContext;
630 alcGetString
632 Returns information about the Device, and error strings
634 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
636 const ALCchar *value = NULL;
638 InitAL();
640 switch (param)
642 case ALC_NO_ERROR:
643 value = alcNoError;
644 break;
646 case ALC_INVALID_ENUM:
647 value = alcErrInvalidEnum;
648 break;
650 case ALC_INVALID_VALUE:
651 value = alcErrInvalidValue;
652 break;
654 case ALC_INVALID_DEVICE:
655 value = alcErrInvalidDevice;
656 break;
658 case ALC_INVALID_CONTEXT:
659 value = alcErrInvalidContext;
660 break;
662 case ALC_OUT_OF_MEMORY:
663 value = alcErrOutOfMemory;
664 break;
666 case ALC_DEFAULT_DEVICE_SPECIFIER:
667 value = alcDefaultDeviceSpecifier;
668 break;
670 case ALC_DEVICE_SPECIFIER:
671 if (pDevice)
672 value = pDevice->szDeviceName;
673 else
674 value = alcDeviceList;
675 break;
677 case ALC_ALL_DEVICES_SPECIFIER:
678 value = alcAllDeviceList;
679 break;
681 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
682 value = alcDefaultAllDeviceSpecifier;
683 break;
685 case ALC_CAPTURE_DEVICE_SPECIFIER:
686 if (pDevice)
687 value = pDevice->szDeviceName;
688 else
689 value = alcCaptureDeviceList;
690 break;
692 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
693 value = alcCaptureDefaultDeviceSpecifier;
694 break;
696 case ALC_EXTENSIONS:
697 value = alcExtensionList;
698 break;
700 default:
701 SetALCError(ALC_INVALID_ENUM);
702 break;
705 return value;
710 alcGetIntegerv
712 Returns information about the Device and the version of Open AL
714 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
716 InitAL();
718 if ((device)&&(device->IsCaptureDevice))
720 SuspendContext(NULL);
722 // Capture device
723 switch (param)
725 case ALC_CAPTURE_SAMPLES:
726 if ((size) && (data))
727 *data = ALCdevice_AvailableSamples(device);
728 else
729 SetALCError(ALC_INVALID_VALUE);
730 break;
732 default:
733 SetALCError(ALC_INVALID_ENUM);
734 break;
737 ProcessContext(NULL);
739 else
741 if(data)
743 // Playback Device
744 switch (param)
746 case ALC_MAJOR_VERSION:
747 if(!size)
748 SetALCError(ALC_INVALID_VALUE);
749 else
750 *data = alcMajorVersion;
751 break;
753 case ALC_MINOR_VERSION:
754 if(!size)
755 SetALCError(ALC_INVALID_VALUE);
756 else
757 *data = alcMinorVersion;
758 break;
760 case ALC_EFX_MAJOR_VERSION:
761 if(!size)
762 SetALCError(ALC_INVALID_VALUE);
763 else
764 *data = alcEFXMajorVersion;
765 break;
767 case ALC_EFX_MINOR_VERSION:
768 if(!size)
769 SetALCError(ALC_INVALID_VALUE);
770 else
771 *data = alcEFXMinorVersion;
772 break;
774 case ALC_MAX_AUXILIARY_SENDS:
775 if(!size)
776 SetALCError(ALC_INVALID_VALUE);
777 else
778 *data = MAX_SENDS;
779 break;
781 case ALC_ATTRIBUTES_SIZE:
782 if(!device)
783 SetALCError(ALC_INVALID_DEVICE);
784 else if(!size)
785 SetALCError(ALC_INVALID_VALUE);
786 else
787 *data = 12;
788 break;
790 case ALC_ALL_ATTRIBUTES:
791 if(!device)
792 SetALCError(ALC_INVALID_DEVICE);
793 else if (size < 7)
794 SetALCError(ALC_INVALID_VALUE);
795 else
797 int i = 0;
799 data[i++] = ALC_FREQUENCY;
800 data[i++] = device->Frequency;
802 data[i++] = ALC_REFRESH;
803 data[i++] = device->Frequency / device->UpdateSize;
805 data[i++] = ALC_SYNC;
806 data[i++] = ALC_FALSE;
808 SuspendContext(NULL);
809 if(device->Context && size >= 12)
811 data[i++] = ALC_MONO_SOURCES;
812 data[i++] = device->Context->lNumMonoSources;
814 data[i++] = ALC_STEREO_SOURCES;
815 data[i++] = device->Context->lNumStereoSources;
817 data[i++] = ALC_MAX_AUXILIARY_SENDS;
818 data[i++] = MAX_SENDS;
820 ProcessContext(NULL);
822 data[i++] = 0;
824 break;
826 case ALC_FREQUENCY:
827 if(!device)
828 SetALCError(ALC_INVALID_DEVICE);
829 else if(!size)
830 SetALCError(ALC_INVALID_VALUE);
831 else
832 *data = device->Frequency;
833 break;
835 case ALC_REFRESH:
836 if(!device)
837 SetALCError(ALC_INVALID_DEVICE);
838 else if(!size)
839 SetALCError(ALC_INVALID_VALUE);
840 else
841 *data = device->Frequency / device->UpdateSize;
842 break;
844 case ALC_SYNC:
845 if(!device)
846 SetALCError(ALC_INVALID_DEVICE);
847 else if(!size)
848 SetALCError(ALC_INVALID_VALUE);
849 else
850 *data = ALC_FALSE;
851 break;
853 case ALC_MONO_SOURCES:
854 if(!device || !device->Context)
855 SetALCError(ALC_INVALID_DEVICE);
856 else if (size != 1)
857 SetALCError(ALC_INVALID_VALUE);
858 else
859 *data = device->Context->lNumMonoSources;
860 break;
862 case ALC_STEREO_SOURCES:
863 if(!device || !device->Context)
864 SetALCError(ALC_INVALID_DEVICE);
865 else if (size != 1)
866 SetALCError(ALC_INVALID_VALUE);
867 else
868 *data = device->Context->lNumStereoSources;
869 break;
871 default:
872 SetALCError(ALC_INVALID_ENUM);
873 break;
876 else if(size)
877 SetALCError(ALC_INVALID_VALUE);
880 return;
885 alcIsExtensionPresent
887 Determines if there is support for a particular extension
889 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
891 ALCboolean bResult = ALC_FALSE;
893 (void)device;
895 if (extName)
897 const char *ptr;
898 size_t len;
900 len = strlen(extName);
901 ptr = alcExtensionList;
902 while(ptr && *ptr)
904 if(strncasecmp(ptr, extName, len) == 0 &&
905 (ptr[len] == '\0' || isspace(ptr[len])))
907 bResult = ALC_TRUE;
908 break;
910 if((ptr=strchr(ptr, ' ')) != NULL)
912 do {
913 ++ptr;
914 } while(isspace(*ptr));
918 else
919 SetALCError(ALC_INVALID_VALUE);
921 return bResult;
926 alcGetProcAddress
928 Retrieves the function address for a particular extension function
930 ALCAPI ALCvoid * ALCAPIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
932 ALCvoid *pFunction = NULL;
933 ALsizei i = 0;
935 (void)device;
937 if (funcName)
939 while(alcFunctions[i].funcName &&
940 strcmp(alcFunctions[i].funcName,funcName) != 0)
941 i++;
942 pFunction = alcFunctions[i].address;
944 else
945 SetALCError(ALC_INVALID_VALUE);
947 return pFunction;
952 alcGetEnumValue
954 Get the value for a particular ALC Enumerated Value
956 ALCAPI ALCenum ALCAPIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
958 ALsizei i = 0;
959 ALCenum val;
961 (void)device;
963 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
964 i++;
965 val = enumeration[i].value;
967 if(!enumeration[i].enumName)
968 SetALCError(ALC_INVALID_VALUE);
970 return val;
975 alcCreateContext
977 Create and attach a Context to a particular Device.
979 ALCAPI ALCcontext* ALCAPIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
981 ALCcontext *ALContext = NULL;
982 ALuint ulAttributeIndex, ulRequestedStereoSources;
984 if ((device)&&(!device->IsCaptureDevice))
986 // Reset Context Last Error code
987 g_eLastContextError = ALC_NO_ERROR;
989 // Current implementation only allows one Context per Device
990 if(!device->Context)
992 ALContext = calloc(1, sizeof(ALCcontext));
993 if(!ALContext)
995 SetALCError(ALC_OUT_OF_MEMORY);
996 return NULL;
999 ALContext->Device = device;
1000 InitContext(ALContext);
1002 device->Context = ALContext;
1004 SuspendContext(NULL);
1006 ALContext->next = g_pContextList;
1007 g_pContextList = ALContext;
1008 g_ulContextCount++;
1010 ProcessContext(NULL);
1012 // Check for Voice Count attributes
1013 if (attrList)
1015 ulAttributeIndex = 0;
1016 while ((ulAttributeIndex < 10) && (attrList[ulAttributeIndex]))
1018 if (attrList[ulAttributeIndex] == ALC_STEREO_SOURCES)
1020 ulRequestedStereoSources = attrList[ulAttributeIndex + 1];
1022 if (ulRequestedStereoSources > ALContext->Device->MaxNoOfSources)
1023 ulRequestedStereoSources = ALContext->Device->MaxNoOfSources;
1025 ALContext->lNumStereoSources = ulRequestedStereoSources;
1026 ALContext->lNumMonoSources = ALContext->Device->MaxNoOfSources - ALContext->lNumStereoSources;
1027 break;
1030 ulAttributeIndex += 2;
1034 else
1036 SetALCError(ALC_INVALID_VALUE);
1037 ALContext = NULL;
1040 else
1041 SetALCError(ALC_INVALID_DEVICE);
1043 return ALContext;
1048 alcDestroyContext
1050 Remove a Context
1052 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
1054 ALCcontext **list;
1056 InitAL();
1058 // Lock context list
1059 SuspendContext(NULL);
1061 if (IsContext(context))
1063 // Lock context
1064 SuspendContext(context);
1066 ReleaseALSources(context);
1067 ReleaseALAuxiliaryEffectSlots(context);
1069 context->Device->Context = NULL;
1071 list = &g_pContextList;
1072 while(*list != context)
1073 list = &(*list)->next;
1075 *list = (*list)->next;
1076 g_ulContextCount--;
1078 // Unlock context
1079 ProcessContext(context);
1081 ExitContext(context);
1083 // Free memory (MUST do this after ProcessContext)
1084 memset(context, 0, sizeof(ALCcontext));
1085 free(context);
1087 else
1088 SetALCError(ALC_INVALID_CONTEXT);
1090 ProcessContext(NULL);
1095 alcGetCurrentContext
1097 Returns the currently active Context
1099 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1101 ALCcontext *pContext = NULL;
1103 InitAL();
1105 SuspendContext(NULL);
1107 pContext = g_pContextList;
1108 while ((pContext) && (!pContext->InUse))
1109 pContext = pContext->next;
1111 ProcessContext(NULL);
1113 return pContext;
1118 alcGetContextsDevice
1120 Returns the Device that a particular Context is attached to
1122 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1124 ALCdevice *pDevice = NULL;
1126 InitAL();
1128 SuspendContext(NULL);
1129 if (IsContext(pContext))
1130 pDevice = pContext->Device;
1131 else
1132 SetALCError(ALC_INVALID_CONTEXT);
1133 ProcessContext(NULL);
1135 return pDevice;
1140 alcMakeContextCurrent
1142 Makes the given Context the active Context
1144 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1146 ALCcontext *ALContext;
1147 ALboolean bReturn = AL_TRUE;
1149 InitAL();
1151 SuspendContext(NULL);
1153 // context must be a valid Context or NULL
1154 if ((IsContext(context)) || (context == NULL))
1156 if ((ALContext=alcGetCurrentContext()))
1158 SuspendContext(ALContext);
1159 ALContext->InUse=AL_FALSE;
1160 ProcessContext(ALContext);
1163 if ((ALContext=context) && (ALContext->Device))
1165 SuspendContext(ALContext);
1166 ALContext->InUse=AL_TRUE;
1167 ProcessContext(ALContext);
1170 else
1172 SetALCError(ALC_INVALID_CONTEXT);
1173 bReturn = AL_FALSE;
1176 ProcessContext(NULL);
1178 return bReturn;
1183 alcOpenDevice
1185 Open the Device specified.
1187 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1189 ALboolean bDeviceFound = AL_FALSE;
1190 ALCdevice *device;
1191 ALint i;
1193 InitAL();
1195 if(deviceName && !deviceName[0])
1196 deviceName = NULL;
1198 device = malloc(sizeof(ALCdevice));
1199 if (device)
1201 const char *fmt;
1203 //Initialise device structure
1204 memset(device, 0, sizeof(ALCdevice));
1206 //Validate device
1207 device->IsCaptureDevice = AL_FALSE;
1209 //Set output format
1210 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1211 if((ALint)device->Frequency <= 0)
1212 device->Frequency = SWMIXER_OUTPUT_RATE;
1214 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1215 if(fmt[0])
1216 device->Format = alGetEnumValue(fmt);
1218 if(!aluChannelsFromFormat(device->Format))
1219 device->Format = AL_FORMAT_STEREO16;
1221 device->UpdateSize = GetConfigValueInt(NULL, "refresh", 4096);
1222 if((ALint)device->UpdateSize <= 0)
1223 device->UpdateSize = 4096;
1225 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1226 if((ALint)device->MaxNoOfSources <= 0)
1227 device->MaxNoOfSources = 256;
1229 // Find a playback device to open
1230 for(i = 0;BackendList[i].Init;i++)
1232 device->Funcs = &BackendList[i].Funcs;
1233 if(ALCdevice_OpenPlayback(device, deviceName))
1235 SuspendContext(NULL);
1236 device->next = g_pDeviceList;
1237 g_pDeviceList = device;
1238 g_ulDeviceCount++;
1239 ProcessContext(NULL);
1241 bDeviceFound = AL_TRUE;
1242 break;
1246 if (!bDeviceFound)
1248 // No suitable output device found
1249 free(device);
1250 device = NULL;
1254 return device;
1259 alcCloseDevice
1261 Close the specified Device
1263 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1265 ALCboolean bReturn = ALC_FALSE;
1266 ALCdevice **list;
1268 if ((pDevice)&&(!pDevice->IsCaptureDevice))
1270 SuspendContext(NULL);
1272 list = &g_pDeviceList;
1273 while(*list != pDevice)
1274 list = &(*list)->next;
1276 *list = (*list)->next;
1277 g_ulDeviceCount--;
1279 ProcessContext(NULL);
1281 if(pDevice->Context)
1283 #ifdef _DEBUG
1284 AL_PRINT("alcCloseDevice(): destroying 1 Context\n");
1285 #endif
1286 alcDestroyContext(pDevice->Context);
1288 ALCdevice_ClosePlayback(pDevice);
1290 //Release device structure
1291 memset(pDevice, 0, sizeof(ALCdevice));
1292 free(pDevice);
1294 bReturn = ALC_TRUE;
1296 else
1297 SetALCError(ALC_INVALID_DEVICE);
1299 return bReturn;
1303 ALCvoid ReleaseALC(ALCvoid)
1305 #ifdef _DEBUG
1306 if(g_ulDeviceCount > 0)
1307 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
1308 #endif
1310 while(g_pDeviceList)
1312 if(g_pDeviceList->IsCaptureDevice)
1313 alcCaptureCloseDevice(g_pDeviceList);
1314 else
1315 alcCloseDevice(g_pDeviceList);
1319 ///////////////////////////////////////////////////////