Seperate data converters into reusable functions
[openal-soft.git] / Alc / ALc.c
blob4a254f8860cc4f143d0ddea3a4868d279d134062
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_SOLARIS
62 { "solaris", alc_solaris_init, EmptyFuncs },
63 #endif
64 #ifdef HAVE_DSOUND
65 { "dsound", alcDSoundInit, EmptyFuncs },
66 #endif
67 #ifdef HAVE_WINMM
68 { "winmm", alcWinMMInit, EmptyFuncs },
69 #endif
71 { "wave", alc_wave_init, EmptyFuncs },
73 { NULL, NULL, EmptyFuncs }
75 #undef EmptyFuncs
77 ///////////////////////////////////////////////////////
79 #define ALC_EFX_MAJOR_VERSION 0x20001
80 #define ALC_EFX_MINOR_VERSION 0x20002
81 #define ALC_MAX_AUXILIARY_SENDS 0x20003
83 ///////////////////////////////////////////////////////
84 // STRING and EXTENSIONS
86 typedef struct ALCfunction_struct
88 ALCchar *funcName;
89 ALvoid *address;
90 } ALCfunction;
92 static ALCfunction alcFunctions[] = {
93 { "alcCreateContext", (ALvoid *) alcCreateContext },
94 { "alcMakeContextCurrent", (ALvoid *) alcMakeContextCurrent },
95 { "alcProcessContext", (ALvoid *) alcProcessContext },
96 { "alcSuspendContext", (ALvoid *) alcSuspendContext },
97 { "alcDestroyContext", (ALvoid *) alcDestroyContext },
98 { "alcGetCurrentContext", (ALvoid *) alcGetCurrentContext },
99 { "alcGetContextsDevice", (ALvoid *) alcGetContextsDevice },
100 { "alcOpenDevice", (ALvoid *) alcOpenDevice },
101 { "alcCloseDevice", (ALvoid *) alcCloseDevice },
102 { "alcGetError", (ALvoid *) alcGetError },
103 { "alcIsExtensionPresent", (ALvoid *) alcIsExtensionPresent },
104 { "alcGetProcAddress", (ALvoid *) alcGetProcAddress },
105 { "alcGetEnumValue", (ALvoid *) alcGetEnumValue },
106 { "alcGetString", (ALvoid *) alcGetString },
107 { "alcGetIntegerv", (ALvoid *) alcGetIntegerv },
108 { "alcCaptureOpenDevice", (ALvoid *) alcCaptureOpenDevice },
109 { "alcCaptureCloseDevice", (ALvoid *) alcCaptureCloseDevice },
110 { "alcCaptureStart", (ALvoid *) alcCaptureStart },
111 { "alcCaptureStop", (ALvoid *) alcCaptureStop },
112 { "alcCaptureSamples", (ALvoid *) alcCaptureSamples },
113 { NULL, (ALvoid *) NULL }
116 static ALenums enumeration[]={
117 // Types
118 { (ALchar *)"ALC_INVALID", ALC_INVALID },
119 { (ALchar *)"ALC_FALSE", ALC_FALSE },
120 { (ALchar *)"ALC_TRUE", ALC_TRUE },
122 // ALC Properties
123 { (ALchar *)"ALC_MAJOR_VERSION", ALC_MAJOR_VERSION },
124 { (ALchar *)"ALC_MINOR_VERSION", ALC_MINOR_VERSION },
125 { (ALchar *)"ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE },
126 { (ALchar *)"ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES },
127 { (ALchar *)"ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER },
128 { (ALchar *)"ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER },
129 { (ALchar *)"ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER },
130 { (ALchar *)"ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER },
131 { (ALchar *)"ALC_EXTENSIONS", ALC_EXTENSIONS },
132 { (ALchar *)"ALC_FREQUENCY", ALC_FREQUENCY },
133 { (ALchar *)"ALC_REFRESH", ALC_REFRESH },
134 { (ALchar *)"ALC_SYNC", ALC_SYNC },
135 { (ALchar *)"ALC_MONO_SOURCES", ALC_MONO_SOURCES },
136 { (ALchar *)"ALC_STEREO_SOURCES", ALC_STEREO_SOURCES },
137 { (ALchar *)"ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER },
138 { (ALchar *)"ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER},
139 { (ALchar *)"ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES },
141 // EFX Properties
142 { (ALchar *)"ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION },
143 { (ALchar *)"ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION },
144 { (ALchar *)"ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS },
146 // ALC Error Message
147 { (ALchar *)"ALC_NO_ERROR", ALC_NO_ERROR },
148 { (ALchar *)"ALC_INVALID_DEVICE", ALC_INVALID_DEVICE },
149 { (ALchar *)"ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT },
150 { (ALchar *)"ALC_INVALID_ENUM", ALC_INVALID_ENUM },
151 { (ALchar *)"ALC_INVALID_VALUE", ALC_INVALID_VALUE },
152 { (ALchar *)"ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY },
153 { (ALchar *)NULL, (ALenum)0 }
155 // Error strings
156 static const ALCchar alcNoError[] = "No Error";
157 static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
158 static const ALCchar alcErrInvalidContext[] = "Invalid Context";
159 static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
160 static const ALCchar alcErrInvalidValue[] = "Invalid Value";
161 static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
163 // Context strings
164 static ALCchar alcDeviceList[2048];
165 static ALCchar alcAllDeviceList[2048];
166 static ALCchar alcCaptureDeviceList[2048];
167 // Default is always the first in the list
168 static ALCchar *alcDefaultDeviceSpecifier = alcDeviceList;
169 static ALCchar *alcDefaultAllDeviceSpecifier = alcAllDeviceList;
170 static ALCchar *alcCaptureDefaultDeviceSpecifier = alcCaptureDeviceList;
173 static ALCchar alcExtensionList[] = "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_EFX";
174 static ALCint alcMajorVersion = 1;
175 static ALCint alcMinorVersion = 1;
177 static ALCint alcEFXMajorVersion = 1;
178 static ALCint alcEFXMinorVersion = 0;
180 ///////////////////////////////////////////////////////
183 ///////////////////////////////////////////////////////
184 // Global Variables
186 static ALCdevice *g_pDeviceList = NULL;
187 static ALCuint g_ulDeviceCount = 0;
189 static CRITICAL_SECTION g_csMutex;
191 // Context List
192 static ALCcontext *g_pContextList = NULL;
193 static ALCuint g_ulContextCount = 0;
195 // Context Error
196 static ALCenum g_eLastContextError = ALC_NO_ERROR;
198 static ALboolean init_done = AL_FALSE;
200 ///////////////////////////////////////////////////////
203 ///////////////////////////////////////////////////////
204 // ALC Related helper functions
205 #ifdef _WIN32
206 BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
208 (void)lpReserved;
210 // Perform actions based on the reason for calling.
211 switch(ul_reason_for_call)
213 case DLL_PROCESS_ATTACH:
214 DisableThreadLibraryCalls(hModule);
215 break;
217 case DLL_PROCESS_DETACH:
218 if(!init_done)
219 break;
220 ReleaseALC();
221 ReleaseALBuffers();
222 ReleaseALEffects();
223 ReleaseALFilters();
224 FreeALConfig();
225 ALTHUNK_EXIT();
226 DeleteCriticalSection(&g_csMutex);
227 break;
229 return TRUE;
231 #else
232 #ifdef HAVE_GCC_DESTRUCTOR
233 static void my_deinit() __attribute__((destructor));
234 static void my_deinit()
236 static ALenum once = AL_FALSE;
237 if(once || !init_done) return;
238 once = AL_TRUE;
240 ReleaseALC();
241 ReleaseALBuffers();
242 ReleaseALEffects();
243 ReleaseALFilters();
244 FreeALConfig();
245 ALTHUNK_EXIT();
246 DeleteCriticalSection(&g_csMutex);
248 #endif
249 #endif
251 static void InitAL(void)
253 if(!init_done)
255 int i;
256 const char *devs, *str;
258 init_done = AL_TRUE;
260 InitializeCriticalSection(&g_csMutex);
261 ALTHUNK_INIT();
262 ReadALConfig();
264 devs = GetConfigValue(NULL, "drivers", "");
265 if(devs[0])
267 int n;
268 size_t len;
269 const char *next = devs;
271 i = 0;
273 do {
274 devs = next;
275 next = strchr(devs, ',');
277 if(!devs[0] || devs[0] == ',')
278 continue;
280 len = (next ? ((size_t)(next-devs)) : strlen(devs));
281 for(n = i;BackendList[n].Init;n++)
283 if(len == strlen(BackendList[n].name) &&
284 strncmp(BackendList[n].name, devs, len) == 0)
286 const char *name = BackendList[i].name;
287 void (*Init)(BackendFuncs*) = BackendList[i].Init;
289 BackendList[i].name = BackendList[n].name;
290 BackendList[i].Init = BackendList[n].Init;
292 BackendList[n].name = name;
293 BackendList[n].Init = Init;
295 i++;
298 } while(next++);
300 BackendList[i].name = NULL;
301 BackendList[i].Init = NULL;
304 for(i = 0;BackendList[i].Init;i++)
305 BackendList[i].Init(&BackendList[i].Funcs);
307 str = GetConfigValue(NULL, "stereodup", "false");
308 DuplicateStereo = (strcasecmp(str, "true") == 0 ||
309 strcasecmp(str, "yes") == 0 ||
310 strcasecmp(str, "on") == 0 ||
311 atoi(str) != 0);
315 ALCchar *AppendDeviceList(char *name)
317 static size_t pos;
318 ALCchar *ret = alcDeviceList+pos;
319 if(pos >= sizeof(alcDeviceList))
321 AL_PRINT("Not enough room to add %s!\n", name);
322 return alcDeviceList + sizeof(alcDeviceList) - 1;
324 pos += snprintf(alcDeviceList+pos, sizeof(alcDeviceList)-pos-1, "%s", name) + 1;
325 return ret;
328 ALCchar *AppendAllDeviceList(char *name)
330 static size_t pos;
331 ALCchar *ret = alcAllDeviceList+pos;
332 if(pos >= sizeof(alcAllDeviceList))
334 AL_PRINT("Not enough room to add %s!\n", name);
335 return alcAllDeviceList + sizeof(alcAllDeviceList) - 1;
337 pos += snprintf(alcAllDeviceList+pos, sizeof(alcAllDeviceList)-pos-1, "%s", name) + 1;
338 return ret;
341 ALCchar *AppendCaptureDeviceList(char *name)
343 static size_t pos;
344 ALCchar *ret = alcCaptureDeviceList+pos;
345 if(pos >= sizeof(alcCaptureDeviceList))
347 AL_PRINT("Not enough room to add %s!\n", name);
348 return alcCaptureDeviceList + sizeof(alcCaptureDeviceList) - 1;
350 pos += snprintf(alcCaptureDeviceList+pos, sizeof(alcCaptureDeviceList)-pos-1, "%s", name) + 1;
351 return ret;
355 IsContext
357 Check pContext is a valid Context pointer
359 static ALCboolean IsContext(ALCcontext *pContext)
361 ALCcontext *pTempContext;
363 pTempContext = g_pContextList;
364 while (pTempContext && pTempContext != pContext)
365 pTempContext = pTempContext->next;
367 return (pTempContext ? ALC_TRUE : ALC_FALSE);
372 SetALCError
374 Store latest ALC Error
376 ALCvoid SetALCError(ALenum errorCode)
378 g_eLastContextError = errorCode;
383 SuspendContext
385 Thread-safe entry
387 ALCvoid SuspendContext(ALCcontext *pContext)
389 (void)pContext;
390 EnterCriticalSection(&g_csMutex);
395 ProcessContext
397 Thread-safe exit
399 ALCvoid ProcessContext(ALCcontext *pContext)
401 (void)pContext;
402 LeaveCriticalSection(&g_csMutex);
407 InitContext
409 Initialize Context variables
411 static ALvoid InitContext(ALCcontext *pContext)
413 int level;
415 //Initialise listener
416 pContext->Listener.Gain = 1.0f;
417 pContext->Listener.MetersPerUnit = 1.0f;
418 pContext->Listener.Position[0] = 0.0f;
419 pContext->Listener.Position[1] = 0.0f;
420 pContext->Listener.Position[2] = 0.0f;
421 pContext->Listener.Velocity[0] = 0.0f;
422 pContext->Listener.Velocity[1] = 0.0f;
423 pContext->Listener.Velocity[2] = 0.0f;
424 pContext->Listener.Forward[0] = 0.0f;
425 pContext->Listener.Forward[1] = 0.0f;
426 pContext->Listener.Forward[2] = -1.0f;
427 pContext->Listener.Up[0] = 0.0f;
428 pContext->Listener.Up[1] = 1.0f;
429 pContext->Listener.Up[2] = 0.0f;
431 //Validate pContext
432 pContext->LastError = AL_NO_ERROR;
433 pContext->InUse = AL_FALSE;
435 //Set output format
436 pContext->Frequency = pContext->Device->Frequency;
438 //Set globals
439 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
440 pContext->DopplerFactor = 1.0f;
441 pContext->DopplerVelocity = 1.0f;
442 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
444 pContext->lNumStereoSources = 1;
445 pContext->lNumMonoSources = pContext->Device->MaxNoOfSources - pContext->lNumStereoSources;
447 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";
449 level = GetConfigValueInt(NULL, "cf_level", 0);
450 if(level > 0 && level <= 6)
452 pContext->bs2b = calloc(1, sizeof(*pContext->bs2b));
453 bs2b_set_srate(pContext->bs2b, pContext->Frequency);
454 bs2b_set_level(pContext->bs2b, level);
460 ExitContext
462 Clean up Context, destroy any remaining Sources
464 static ALCvoid ExitContext(ALCcontext *pContext)
466 //Invalidate context
467 pContext->LastError = AL_NO_ERROR;
468 pContext->InUse = AL_FALSE;
470 free(pContext->bs2b);
471 pContext->bs2b = NULL;
474 ///////////////////////////////////////////////////////
477 ///////////////////////////////////////////////////////
478 // ALC Functions calls
481 // This should probably move to another c file but for now ...
482 ALCAPI ALCdevice* ALCAPIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
484 ALCboolean DeviceFound = ALC_FALSE;
485 ALCdevice *pDevice = NULL;
486 ALCint i;
488 InitAL();
490 if(SampleSize <= 0)
492 SetALCError(ALC_INVALID_VALUE);
493 return NULL;
496 if(deviceName && !deviceName[0])
497 deviceName = NULL;
499 pDevice = malloc(sizeof(ALCdevice));
500 if (pDevice)
502 //Initialise device structure
503 memset(pDevice, 0, sizeof(ALCdevice));
505 //Validate device
506 pDevice->IsCaptureDevice = AL_TRUE;
508 pDevice->Frequency = frequency;
509 pDevice->Format = format;
511 for(i = 0;BackendList[i].Init;i++)
513 pDevice->Funcs = &BackendList[i].Funcs;
514 if(ALCdevice_OpenCapture(pDevice, deviceName, frequency, format, SampleSize))
516 SuspendContext(NULL);
517 pDevice->next = g_pDeviceList;
518 g_pDeviceList = pDevice;
519 g_ulDeviceCount++;
520 ProcessContext(NULL);
522 DeviceFound = ALC_TRUE;
523 break;
527 if(!DeviceFound)
529 SetALCError(ALC_INVALID_VALUE);
530 free(pDevice);
531 pDevice = NULL;
534 else
535 SetALCError(ALC_OUT_OF_MEMORY);
537 return pDevice;
540 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
542 ALCboolean bReturn = ALC_FALSE;
543 ALCdevice **list;
545 if ((pDevice)&&(pDevice->IsCaptureDevice))
547 SuspendContext(NULL);
549 list = &g_pDeviceList;
550 while(*list != pDevice)
551 list = &(*list)->next;
553 *list = (*list)->next;
554 g_ulDeviceCount--;
556 ProcessContext(NULL);
558 ALCdevice_CloseCapture(pDevice);
559 free(pDevice);
561 bReturn = ALC_TRUE;
563 else
564 SetALCError(ALC_INVALID_DEVICE);
566 return bReturn;
569 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
571 if ((pDevice)&&(pDevice->IsCaptureDevice))
572 ALCdevice_StartCapture(pDevice);
573 else
574 SetALCError(ALC_INVALID_DEVICE);
577 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
579 if ((pDevice)&&(pDevice->IsCaptureDevice))
580 ALCdevice_StopCapture(pDevice);
581 else
582 SetALCError(ALC_INVALID_DEVICE);
585 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
587 if ((pDevice) && (pDevice->IsCaptureDevice))
588 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
589 else
590 SetALCError(ALC_INVALID_DEVICE);
594 alcGetError
596 Return last ALC generated error code
598 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
600 ALCenum errorCode;
602 (void)device;
604 errorCode = g_eLastContextError;
605 g_eLastContextError = ALC_NO_ERROR;
606 return errorCode;
611 alcSuspendContext
613 Not functional
615 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
617 // Not a lot happens here !
618 (void)pContext;
623 alcProcessContext
625 Not functional
627 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
629 // Not a lot happens here !
630 (void)pContext;
635 alcGetString
637 Returns information about the Device, and error strings
639 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
641 const ALCchar *value = NULL;
643 InitAL();
645 switch (param)
647 case ALC_NO_ERROR:
648 value = alcNoError;
649 break;
651 case ALC_INVALID_ENUM:
652 value = alcErrInvalidEnum;
653 break;
655 case ALC_INVALID_VALUE:
656 value = alcErrInvalidValue;
657 break;
659 case ALC_INVALID_DEVICE:
660 value = alcErrInvalidDevice;
661 break;
663 case ALC_INVALID_CONTEXT:
664 value = alcErrInvalidContext;
665 break;
667 case ALC_OUT_OF_MEMORY:
668 value = alcErrOutOfMemory;
669 break;
671 case ALC_DEFAULT_DEVICE_SPECIFIER:
672 value = alcDefaultDeviceSpecifier;
673 break;
675 case ALC_DEVICE_SPECIFIER:
676 if (pDevice)
677 value = pDevice->szDeviceName;
678 else
679 value = alcDeviceList;
680 break;
682 case ALC_ALL_DEVICES_SPECIFIER:
683 value = alcAllDeviceList;
684 break;
686 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
687 value = alcDefaultAllDeviceSpecifier;
688 break;
690 case ALC_CAPTURE_DEVICE_SPECIFIER:
691 if (pDevice)
692 value = pDevice->szDeviceName;
693 else
694 value = alcCaptureDeviceList;
695 break;
697 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
698 value = alcCaptureDefaultDeviceSpecifier;
699 break;
701 case ALC_EXTENSIONS:
702 value = alcExtensionList;
703 break;
705 default:
706 SetALCError(ALC_INVALID_ENUM);
707 break;
710 return value;
715 alcGetIntegerv
717 Returns information about the Device and the version of Open AL
719 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
721 InitAL();
723 if ((device)&&(device->IsCaptureDevice))
725 SuspendContext(NULL);
727 // Capture device
728 switch (param)
730 case ALC_CAPTURE_SAMPLES:
731 if ((size) && (data))
732 *data = ALCdevice_AvailableSamples(device);
733 else
734 SetALCError(ALC_INVALID_VALUE);
735 break;
737 default:
738 SetALCError(ALC_INVALID_ENUM);
739 break;
742 ProcessContext(NULL);
744 else
746 if(data)
748 // Playback Device
749 switch (param)
751 case ALC_MAJOR_VERSION:
752 if(!size)
753 SetALCError(ALC_INVALID_VALUE);
754 else
755 *data = alcMajorVersion;
756 break;
758 case ALC_MINOR_VERSION:
759 if(!size)
760 SetALCError(ALC_INVALID_VALUE);
761 else
762 *data = alcMinorVersion;
763 break;
765 case ALC_EFX_MAJOR_VERSION:
766 if(!size)
767 SetALCError(ALC_INVALID_VALUE);
768 else
769 *data = alcEFXMajorVersion;
770 break;
772 case ALC_EFX_MINOR_VERSION:
773 if(!size)
774 SetALCError(ALC_INVALID_VALUE);
775 else
776 *data = alcEFXMinorVersion;
777 break;
779 case ALC_MAX_AUXILIARY_SENDS:
780 if(!size)
781 SetALCError(ALC_INVALID_VALUE);
782 else
783 *data = MAX_SENDS;
784 break;
786 case ALC_ATTRIBUTES_SIZE:
787 if(!device)
788 SetALCError(ALC_INVALID_DEVICE);
789 else if(!size)
790 SetALCError(ALC_INVALID_VALUE);
791 else
792 *data = 12;
793 break;
795 case ALC_ALL_ATTRIBUTES:
796 if(!device)
797 SetALCError(ALC_INVALID_DEVICE);
798 else if (size < 7)
799 SetALCError(ALC_INVALID_VALUE);
800 else
802 int i = 0;
804 data[i++] = ALC_FREQUENCY;
805 data[i++] = device->Frequency;
807 data[i++] = ALC_REFRESH;
808 data[i++] = device->Frequency / device->UpdateSize;
810 data[i++] = ALC_SYNC;
811 data[i++] = ALC_FALSE;
813 SuspendContext(NULL);
814 if(device->Context && size >= 12)
816 data[i++] = ALC_MONO_SOURCES;
817 data[i++] = device->Context->lNumMonoSources;
819 data[i++] = ALC_STEREO_SOURCES;
820 data[i++] = device->Context->lNumStereoSources;
822 data[i++] = ALC_MAX_AUXILIARY_SENDS;
823 data[i++] = MAX_SENDS;
825 ProcessContext(NULL);
827 data[i++] = 0;
829 break;
831 case ALC_FREQUENCY:
832 if(!device)
833 SetALCError(ALC_INVALID_DEVICE);
834 else if(!size)
835 SetALCError(ALC_INVALID_VALUE);
836 else
837 *data = device->Frequency;
838 break;
840 case ALC_REFRESH:
841 if(!device)
842 SetALCError(ALC_INVALID_DEVICE);
843 else if(!size)
844 SetALCError(ALC_INVALID_VALUE);
845 else
846 *data = device->Frequency / device->UpdateSize;
847 break;
849 case ALC_SYNC:
850 if(!device)
851 SetALCError(ALC_INVALID_DEVICE);
852 else if(!size)
853 SetALCError(ALC_INVALID_VALUE);
854 else
855 *data = ALC_FALSE;
856 break;
858 case ALC_MONO_SOURCES:
859 if(!device || !device->Context)
860 SetALCError(ALC_INVALID_DEVICE);
861 else if (size != 1)
862 SetALCError(ALC_INVALID_VALUE);
863 else
864 *data = device->Context->lNumMonoSources;
865 break;
867 case ALC_STEREO_SOURCES:
868 if(!device || !device->Context)
869 SetALCError(ALC_INVALID_DEVICE);
870 else if (size != 1)
871 SetALCError(ALC_INVALID_VALUE);
872 else
873 *data = device->Context->lNumStereoSources;
874 break;
876 default:
877 SetALCError(ALC_INVALID_ENUM);
878 break;
881 else if(size)
882 SetALCError(ALC_INVALID_VALUE);
885 return;
890 alcIsExtensionPresent
892 Determines if there is support for a particular extension
894 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
896 ALCboolean bResult = ALC_FALSE;
898 (void)device;
900 if (extName)
902 const char *ptr;
903 size_t len;
905 len = strlen(extName);
906 ptr = alcExtensionList;
907 while(ptr && *ptr)
909 if(strncasecmp(ptr, extName, len) == 0 &&
910 (ptr[len] == '\0' || isspace(ptr[len])))
912 bResult = ALC_TRUE;
913 break;
915 if((ptr=strchr(ptr, ' ')) != NULL)
917 do {
918 ++ptr;
919 } while(isspace(*ptr));
923 else
924 SetALCError(ALC_INVALID_VALUE);
926 return bResult;
931 alcGetProcAddress
933 Retrieves the function address for a particular extension function
935 ALCAPI ALCvoid * ALCAPIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
937 ALCvoid *pFunction = NULL;
938 ALsizei i = 0;
940 (void)device;
942 if (funcName)
944 while(alcFunctions[i].funcName &&
945 strcmp(alcFunctions[i].funcName,funcName) != 0)
946 i++;
947 pFunction = alcFunctions[i].address;
949 else
950 SetALCError(ALC_INVALID_VALUE);
952 return pFunction;
957 alcGetEnumValue
959 Get the value for a particular ALC Enumerated Value
961 ALCAPI ALCenum ALCAPIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
963 ALsizei i = 0;
964 ALCenum val;
966 (void)device;
968 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
969 i++;
970 val = enumeration[i].value;
972 if(!enumeration[i].enumName)
973 SetALCError(ALC_INVALID_VALUE);
975 return val;
980 alcCreateContext
982 Create and attach a Context to a particular Device.
984 ALCAPI ALCcontext* ALCAPIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
986 ALCcontext *ALContext = NULL;
987 ALuint ulAttributeIndex, ulRequestedStereoSources;
989 if ((device)&&(!device->IsCaptureDevice))
991 // Reset Context Last Error code
992 g_eLastContextError = ALC_NO_ERROR;
994 // Current implementation only allows one Context per Device
995 if(!device->Context)
997 ALContext = calloc(1, sizeof(ALCcontext));
998 if(!ALContext)
1000 SetALCError(ALC_OUT_OF_MEMORY);
1001 return NULL;
1004 ALContext->Device = device;
1005 InitContext(ALContext);
1007 device->Context = ALContext;
1009 SuspendContext(NULL);
1011 ALContext->next = g_pContextList;
1012 g_pContextList = ALContext;
1013 g_ulContextCount++;
1015 ProcessContext(NULL);
1017 // Check for Voice Count attributes
1018 if (attrList)
1020 ulAttributeIndex = 0;
1021 while ((ulAttributeIndex < 10) && (attrList[ulAttributeIndex]))
1023 if (attrList[ulAttributeIndex] == ALC_STEREO_SOURCES)
1025 ulRequestedStereoSources = attrList[ulAttributeIndex + 1];
1027 if (ulRequestedStereoSources > ALContext->Device->MaxNoOfSources)
1028 ulRequestedStereoSources = ALContext->Device->MaxNoOfSources;
1030 ALContext->lNumStereoSources = ulRequestedStereoSources;
1031 ALContext->lNumMonoSources = ALContext->Device->MaxNoOfSources - ALContext->lNumStereoSources;
1032 break;
1035 ulAttributeIndex += 2;
1039 else
1041 SetALCError(ALC_INVALID_VALUE);
1042 ALContext = NULL;
1045 else
1046 SetALCError(ALC_INVALID_DEVICE);
1048 return ALContext;
1053 alcDestroyContext
1055 Remove a Context
1057 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
1059 ALCcontext **list;
1061 InitAL();
1063 // Lock context list
1064 SuspendContext(NULL);
1066 if (IsContext(context))
1068 // Lock context
1069 SuspendContext(context);
1071 ReleaseALSources(context);
1072 ReleaseALAuxiliaryEffectSlots(context);
1074 context->Device->Context = NULL;
1076 list = &g_pContextList;
1077 while(*list != context)
1078 list = &(*list)->next;
1080 *list = (*list)->next;
1081 g_ulContextCount--;
1083 // Unlock context
1084 ProcessContext(context);
1086 ExitContext(context);
1088 // Free memory (MUST do this after ProcessContext)
1089 memset(context, 0, sizeof(ALCcontext));
1090 free(context);
1092 else
1093 SetALCError(ALC_INVALID_CONTEXT);
1095 ProcessContext(NULL);
1100 alcGetCurrentContext
1102 Returns the currently active Context
1104 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1106 ALCcontext *pContext = NULL;
1108 InitAL();
1110 SuspendContext(NULL);
1112 pContext = g_pContextList;
1113 while ((pContext) && (!pContext->InUse))
1114 pContext = pContext->next;
1116 ProcessContext(NULL);
1118 return pContext;
1123 alcGetContextsDevice
1125 Returns the Device that a particular Context is attached to
1127 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1129 ALCdevice *pDevice = NULL;
1131 InitAL();
1133 SuspendContext(NULL);
1134 if (IsContext(pContext))
1135 pDevice = pContext->Device;
1136 else
1137 SetALCError(ALC_INVALID_CONTEXT);
1138 ProcessContext(NULL);
1140 return pDevice;
1145 alcMakeContextCurrent
1147 Makes the given Context the active Context
1149 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1151 ALCcontext *ALContext;
1152 ALboolean bReturn = AL_TRUE;
1154 InitAL();
1156 SuspendContext(NULL);
1158 // context must be a valid Context or NULL
1159 if ((IsContext(context)) || (context == NULL))
1161 if ((ALContext=alcGetCurrentContext()))
1163 SuspendContext(ALContext);
1164 ALContext->InUse=AL_FALSE;
1165 ProcessContext(ALContext);
1168 if ((ALContext=context) && (ALContext->Device))
1170 SuspendContext(ALContext);
1171 ALContext->InUse=AL_TRUE;
1172 ProcessContext(ALContext);
1175 else
1177 SetALCError(ALC_INVALID_CONTEXT);
1178 bReturn = AL_FALSE;
1181 ProcessContext(NULL);
1183 return bReturn;
1188 alcOpenDevice
1190 Open the Device specified.
1192 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1194 ALboolean bDeviceFound = AL_FALSE;
1195 ALCdevice *device;
1196 ALint i;
1198 InitAL();
1200 if(deviceName && !deviceName[0])
1201 deviceName = NULL;
1203 device = malloc(sizeof(ALCdevice));
1204 if (device)
1206 const char *fmt;
1208 //Initialise device structure
1209 memset(device, 0, sizeof(ALCdevice));
1211 //Validate device
1212 device->IsCaptureDevice = AL_FALSE;
1214 //Set output format
1215 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1216 if((ALint)device->Frequency <= 0)
1217 device->Frequency = SWMIXER_OUTPUT_RATE;
1219 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1220 if(fmt[0])
1221 device->Format = alGetEnumValue(fmt);
1223 if(!aluChannelsFromFormat(device->Format))
1224 device->Format = AL_FORMAT_STEREO16;
1226 device->UpdateSize = GetConfigValueInt(NULL, "refresh", 4096);
1227 if((ALint)device->UpdateSize <= 0)
1228 device->UpdateSize = 4096;
1230 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1231 if((ALint)device->MaxNoOfSources <= 0)
1232 device->MaxNoOfSources = 256;
1234 // Find a playback device to open
1235 for(i = 0;BackendList[i].Init;i++)
1237 device->Funcs = &BackendList[i].Funcs;
1238 if(ALCdevice_OpenPlayback(device, deviceName))
1240 SuspendContext(NULL);
1241 device->next = g_pDeviceList;
1242 g_pDeviceList = device;
1243 g_ulDeviceCount++;
1244 ProcessContext(NULL);
1246 bDeviceFound = AL_TRUE;
1247 break;
1251 if (!bDeviceFound)
1253 // No suitable output device found
1254 SetALCError(ALC_INVALID_VALUE);
1255 free(device);
1256 device = NULL;
1259 else
1260 SetALCError(ALC_OUT_OF_MEMORY);
1262 return device;
1267 alcCloseDevice
1269 Close the specified Device
1271 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1273 ALCboolean bReturn = ALC_FALSE;
1274 ALCdevice **list;
1276 if ((pDevice)&&(!pDevice->IsCaptureDevice))
1278 SuspendContext(NULL);
1280 list = &g_pDeviceList;
1281 while(*list != pDevice)
1282 list = &(*list)->next;
1284 *list = (*list)->next;
1285 g_ulDeviceCount--;
1287 ProcessContext(NULL);
1289 if(pDevice->Context)
1291 #ifdef _DEBUG
1292 AL_PRINT("alcCloseDevice(): destroying 1 Context\n");
1293 #endif
1294 alcDestroyContext(pDevice->Context);
1296 ALCdevice_ClosePlayback(pDevice);
1298 //Release device structure
1299 memset(pDevice, 0, sizeof(ALCdevice));
1300 free(pDevice);
1302 bReturn = ALC_TRUE;
1304 else
1305 SetALCError(ALC_INVALID_DEVICE);
1307 return bReturn;
1311 ALCvoid ReleaseALC(ALCvoid)
1313 #ifdef _DEBUG
1314 if(g_ulDeviceCount > 0)
1315 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
1316 #endif
1318 while(g_pDeviceList)
1320 if(g_pDeviceList->IsCaptureDevice)
1321 alcCaptureCloseDevice(g_pDeviceList);
1322 else
1323 alcCloseDevice(g_pDeviceList);
1327 ///////////////////////////////////////////////////////