Remove the SDL backend
[openal-soft.git] / Alc / ALc.c
blob574de7698cf94458b4e53deb44f764c9cfe6c3eb
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #define _CRT_SECURE_NO_DEPRECATE // get rid of sprintf security warnings on VS2005
23 #include "config.h"
25 #include <math.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <memory.h>
29 #include <ctype.h>
30 #include "alMain.h"
31 #include "alSource.h"
32 #include "AL/al.h"
33 #include "AL/alc.h"
34 #include "alThunk.h"
35 #include "alSource.h"
36 #include "alBuffer.h"
37 #include "alExtension.h"
38 #include "alAuxEffectSlot.h"
39 #include "bs2b.h"
40 #include "alu.h"
42 ///////////////////////////////////////////////////////
43 // DEBUG INFORMATION
45 char _alDebug[256];
47 ///////////////////////////////////////////////////////
50 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
51 static struct {
52 const char *name;
53 void (*Init)(BackendFuncs*);
54 BackendFuncs Funcs;
55 } BackendList[] = {
56 #ifdef HAVE_ALSA
57 { "alsa", alc_alsa_init, EmptyFuncs },
58 #endif
59 #ifdef HAVE_OSS
60 { "oss", alc_oss_init, EmptyFuncs },
61 #endif
62 #ifdef HAVE_SOLARIS
63 { "solaris", alc_solaris_init, EmptyFuncs },
64 #endif
65 #ifdef HAVE_DSOUND
66 { "dsound", alcDSoundInit, EmptyFuncs },
67 #endif
68 #ifdef HAVE_WINMM
69 { "winmm", alcWinMMInit, EmptyFuncs },
70 #endif
71 #ifdef HAVE_PORTAUDIO
72 { "port", alc_pa_init, EmptyFuncs },
73 #endif
75 { "wave", alc_wave_init, EmptyFuncs },
77 { NULL, NULL, EmptyFuncs }
79 #undef EmptyFuncs
81 ///////////////////////////////////////////////////////
83 #define ALC_EFX_MAJOR_VERSION 0x20001
84 #define ALC_EFX_MINOR_VERSION 0x20002
85 #define ALC_MAX_AUXILIARY_SENDS 0x20003
87 ///////////////////////////////////////////////////////
88 // STRING and EXTENSIONS
90 typedef struct ALCfunction_struct
92 ALCchar *funcName;
93 ALvoid *address;
94 } ALCfunction;
96 static ALCfunction alcFunctions[] = {
97 { "alcCreateContext", (ALvoid *) alcCreateContext },
98 { "alcMakeContextCurrent", (ALvoid *) alcMakeContextCurrent },
99 { "alcProcessContext", (ALvoid *) alcProcessContext },
100 { "alcSuspendContext", (ALvoid *) alcSuspendContext },
101 { "alcDestroyContext", (ALvoid *) alcDestroyContext },
102 { "alcGetCurrentContext", (ALvoid *) alcGetCurrentContext },
103 { "alcGetContextsDevice", (ALvoid *) alcGetContextsDevice },
104 { "alcOpenDevice", (ALvoid *) alcOpenDevice },
105 { "alcCloseDevice", (ALvoid *) alcCloseDevice },
106 { "alcGetError", (ALvoid *) alcGetError },
107 { "alcIsExtensionPresent", (ALvoid *) alcIsExtensionPresent },
108 { "alcGetProcAddress", (ALvoid *) alcGetProcAddress },
109 { "alcGetEnumValue", (ALvoid *) alcGetEnumValue },
110 { "alcGetString", (ALvoid *) alcGetString },
111 { "alcGetIntegerv", (ALvoid *) alcGetIntegerv },
112 { "alcCaptureOpenDevice", (ALvoid *) alcCaptureOpenDevice },
113 { "alcCaptureCloseDevice", (ALvoid *) alcCaptureCloseDevice },
114 { "alcCaptureStart", (ALvoid *) alcCaptureStart },
115 { "alcCaptureStop", (ALvoid *) alcCaptureStop },
116 { "alcCaptureSamples", (ALvoid *) alcCaptureSamples },
117 { NULL, (ALvoid *) NULL }
120 static ALenums enumeration[]={
121 // Types
122 { (ALchar *)"ALC_INVALID", ALC_INVALID },
123 { (ALchar *)"ALC_FALSE", ALC_FALSE },
124 { (ALchar *)"ALC_TRUE", ALC_TRUE },
126 // ALC Properties
127 { (ALchar *)"ALC_MAJOR_VERSION", ALC_MAJOR_VERSION },
128 { (ALchar *)"ALC_MINOR_VERSION", ALC_MINOR_VERSION },
129 { (ALchar *)"ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE },
130 { (ALchar *)"ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES },
131 { (ALchar *)"ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER },
132 { (ALchar *)"ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER },
133 { (ALchar *)"ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER },
134 { (ALchar *)"ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER },
135 { (ALchar *)"ALC_EXTENSIONS", ALC_EXTENSIONS },
136 { (ALchar *)"ALC_FREQUENCY", ALC_FREQUENCY },
137 { (ALchar *)"ALC_REFRESH", ALC_REFRESH },
138 { (ALchar *)"ALC_SYNC", ALC_SYNC },
139 { (ALchar *)"ALC_MONO_SOURCES", ALC_MONO_SOURCES },
140 { (ALchar *)"ALC_STEREO_SOURCES", ALC_STEREO_SOURCES },
141 { (ALchar *)"ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER },
142 { (ALchar *)"ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER},
143 { (ALchar *)"ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES },
145 // EFX Properties
146 { (ALchar *)"ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION },
147 { (ALchar *)"ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION },
148 { (ALchar *)"ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS },
150 // ALC Error Message
151 { (ALchar *)"ALC_NO_ERROR", ALC_NO_ERROR },
152 { (ALchar *)"ALC_INVALID_DEVICE", ALC_INVALID_DEVICE },
153 { (ALchar *)"ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT },
154 { (ALchar *)"ALC_INVALID_ENUM", ALC_INVALID_ENUM },
155 { (ALchar *)"ALC_INVALID_VALUE", ALC_INVALID_VALUE },
156 { (ALchar *)"ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY },
157 { (ALchar *)NULL, (ALenum)0 }
159 // Error strings
160 static const ALCchar alcNoError[] = "No Error";
161 static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
162 static const ALCchar alcErrInvalidContext[] = "Invalid Context";
163 static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
164 static const ALCchar alcErrInvalidValue[] = "Invalid Value";
165 static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
167 // Context strings
168 static ALCchar alcDeviceList[2048];
169 static ALCchar alcAllDeviceList[2048];
170 static ALCchar alcCaptureDeviceList[2048];
171 // Default is always the first in the list
172 static ALCchar *alcDefaultDeviceSpecifier = alcDeviceList;
173 static ALCchar *alcDefaultAllDeviceSpecifier = alcAllDeviceList;
174 static ALCchar *alcCaptureDefaultDeviceSpecifier = alcCaptureDeviceList;
177 static ALCchar alcExtensionList[] = "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_EFX";
178 static ALCint alcMajorVersion = 1;
179 static ALCint alcMinorVersion = 1;
181 static ALCint alcEFXMajorVersion = 1;
182 static ALCint alcEFXMinorVersion = 0;
184 ///////////////////////////////////////////////////////
187 ///////////////////////////////////////////////////////
188 // Global Variables
190 static ALCdevice *g_pDeviceList = NULL;
191 static ALCuint g_ulDeviceCount = 0;
193 static CRITICAL_SECTION g_csMutex;
195 // Context List
196 static ALCcontext *g_pContextList = NULL;
197 static ALCuint g_ulContextCount = 0;
199 // Context Error
200 static ALCenum g_eLastContextError = ALC_NO_ERROR;
202 static ALboolean init_done = AL_FALSE;
204 ///////////////////////////////////////////////////////
207 ///////////////////////////////////////////////////////
208 // ALC Related helper functions
209 #ifdef _WIN32
210 BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
212 (void)lpReserved;
214 // Perform actions based on the reason for calling.
215 switch(ul_reason_for_call)
217 case DLL_PROCESS_ATTACH:
218 DisableThreadLibraryCalls(hModule);
219 break;
221 case DLL_PROCESS_DETACH:
222 if(!init_done)
223 break;
224 ReleaseALC();
225 ReleaseALBuffers();
226 ReleaseALEffects();
227 ReleaseALFilters();
228 FreeALConfig();
229 ALTHUNK_EXIT();
230 DeleteCriticalSection(&g_csMutex);
231 break;
233 return TRUE;
235 #else
236 #ifdef HAVE_GCC_DESTRUCTOR
237 static void my_deinit() __attribute__((destructor));
238 static void my_deinit()
240 static ALenum once = AL_FALSE;
241 if(once || !init_done) return;
242 once = AL_TRUE;
244 ReleaseALC();
245 ReleaseALBuffers();
246 ReleaseALEffects();
247 ReleaseALFilters();
248 FreeALConfig();
249 ALTHUNK_EXIT();
250 DeleteCriticalSection(&g_csMutex);
252 #endif
253 #endif
255 static void InitAL(void)
257 if(!init_done)
259 int i;
260 const char *devs, *str;
262 init_done = AL_TRUE;
264 InitializeCriticalSection(&g_csMutex);
265 ALTHUNK_INIT();
266 ReadALConfig();
268 devs = GetConfigValue(NULL, "drivers", "");
269 if(devs[0])
271 int n;
272 size_t len;
273 const char *next = devs;
275 i = 0;
277 do {
278 devs = next;
279 next = strchr(devs, ',');
281 if(!devs[0] || devs[0] == ',')
282 continue;
284 len = (next ? ((size_t)(next-devs)) : strlen(devs));
285 for(n = i;BackendList[n].Init;n++)
287 if(len == strlen(BackendList[n].name) &&
288 strncmp(BackendList[n].name, devs, len) == 0)
290 const char *name = BackendList[i].name;
291 void (*Init)(BackendFuncs*) = BackendList[i].Init;
293 BackendList[i].name = BackendList[n].name;
294 BackendList[i].Init = BackendList[n].Init;
296 BackendList[n].name = name;
297 BackendList[n].Init = Init;
299 i++;
302 } while(next++);
304 BackendList[i].name = NULL;
305 BackendList[i].Init = NULL;
308 for(i = 0;BackendList[i].Init;i++)
309 BackendList[i].Init(&BackendList[i].Funcs);
311 str = GetConfigValue(NULL, "stereodup", "false");
312 DuplicateStereo = (strcasecmp(str, "true") == 0 ||
313 strcasecmp(str, "yes") == 0 ||
314 strcasecmp(str, "on") == 0 ||
315 atoi(str) != 0);
317 str = GetConfigValue(NULL, "excludefx", "");
318 if(str[0])
320 const struct {
321 const char *name;
322 int type;
323 } EffectList[] = {
324 { "reverb", REVERB },
325 { NULL, 0 }
327 int n;
328 size_t len;
329 const char *next = str;
331 do {
332 str = next;
333 next = strchr(str, ',');
335 if(!str[0] || next == str)
336 continue;
338 len = (next ? ((size_t)(next-str)) : strlen(str));
339 for(n = 0;EffectList[n].name;n++)
341 if(len == strlen(EffectList[n].name) &&
342 strncmp(EffectList[n].name, str, len) == 0)
343 DisabledEffects[EffectList[n].type] = AL_TRUE;
345 } while(next++);
350 ALCchar *AppendDeviceList(char *name)
352 static size_t pos;
353 ALCchar *ret = alcDeviceList+pos;
354 if(pos >= sizeof(alcDeviceList))
356 AL_PRINT("Not enough room to add %s!\n", name);
357 return alcDeviceList + sizeof(alcDeviceList) - 1;
359 pos += snprintf(alcDeviceList+pos, sizeof(alcDeviceList)-pos-1, "%s", name) + 1;
360 return ret;
363 ALCchar *AppendAllDeviceList(char *name)
365 static size_t pos;
366 ALCchar *ret = alcAllDeviceList+pos;
367 if(pos >= sizeof(alcAllDeviceList))
369 AL_PRINT("Not enough room to add %s!\n", name);
370 return alcAllDeviceList + sizeof(alcAllDeviceList) - 1;
372 pos += snprintf(alcAllDeviceList+pos, sizeof(alcAllDeviceList)-pos-1, "%s", name) + 1;
373 return ret;
376 ALCchar *AppendCaptureDeviceList(char *name)
378 static size_t pos;
379 ALCchar *ret = alcCaptureDeviceList+pos;
380 if(pos >= sizeof(alcCaptureDeviceList))
382 AL_PRINT("Not enough room to add %s!\n", name);
383 return alcCaptureDeviceList + sizeof(alcCaptureDeviceList) - 1;
385 pos += snprintf(alcCaptureDeviceList+pos, sizeof(alcCaptureDeviceList)-pos-1, "%s", name) + 1;
386 return ret;
390 IsContext
392 Check pContext is a valid Context pointer
394 static ALCboolean IsContext(ALCcontext *pContext)
396 ALCcontext *pTempContext;
398 pTempContext = g_pContextList;
399 while (pTempContext && pTempContext != pContext)
400 pTempContext = pTempContext->next;
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->lNumStereoSources = 1;
480 pContext->lNumMonoSources = pContext->Device->MaxNoOfSources - pContext->lNumStereoSources;
482 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";
484 level = GetConfigValueInt(NULL, "cf_level", 0);
485 if(level > 0 && level <= 6)
487 pContext->bs2b = calloc(1, sizeof(*pContext->bs2b));
488 bs2b_set_srate(pContext->bs2b, pContext->Frequency);
489 bs2b_set_level(pContext->bs2b, level);
492 aluInitPanning(pContext);
497 ExitContext
499 Clean up Context, destroy any remaining Sources
501 static ALCvoid ExitContext(ALCcontext *pContext)
503 //Invalidate context
504 pContext->LastError = AL_NO_ERROR;
505 pContext->InUse = AL_FALSE;
507 free(pContext->bs2b);
508 pContext->bs2b = NULL;
511 ///////////////////////////////////////////////////////
514 ///////////////////////////////////////////////////////
515 // ALC Functions calls
518 // This should probably move to another c file but for now ...
519 ALCAPI ALCdevice* ALCAPIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
521 ALCboolean DeviceFound = ALC_FALSE;
522 ALCdevice *pDevice = NULL;
523 ALCint i;
525 InitAL();
527 if(SampleSize <= 0)
529 SetALCError(ALC_INVALID_VALUE);
530 return NULL;
533 if(deviceName && !deviceName[0])
534 deviceName = NULL;
536 pDevice = malloc(sizeof(ALCdevice));
537 if (pDevice)
539 //Initialise device structure
540 memset(pDevice, 0, sizeof(ALCdevice));
542 //Validate device
543 pDevice->IsCaptureDevice = AL_TRUE;
545 pDevice->Frequency = frequency;
546 pDevice->Format = format;
548 for(i = 0;BackendList[i].Init;i++)
550 pDevice->Funcs = &BackendList[i].Funcs;
551 if(ALCdevice_OpenCapture(pDevice, deviceName, frequency, format, SampleSize))
553 SuspendContext(NULL);
554 pDevice->next = g_pDeviceList;
555 g_pDeviceList = pDevice;
556 g_ulDeviceCount++;
557 ProcessContext(NULL);
559 DeviceFound = ALC_TRUE;
560 break;
564 if(!DeviceFound)
566 SetALCError(ALC_INVALID_VALUE);
567 free(pDevice);
568 pDevice = NULL;
571 else
572 SetALCError(ALC_OUT_OF_MEMORY);
574 return pDevice;
577 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
579 ALCboolean bReturn = ALC_FALSE;
580 ALCdevice **list;
582 if ((pDevice)&&(pDevice->IsCaptureDevice))
584 SuspendContext(NULL);
586 list = &g_pDeviceList;
587 while(*list != pDevice)
588 list = &(*list)->next;
590 *list = (*list)->next;
591 g_ulDeviceCount--;
593 ProcessContext(NULL);
595 ALCdevice_CloseCapture(pDevice);
596 free(pDevice);
598 bReturn = ALC_TRUE;
600 else
601 SetALCError(ALC_INVALID_DEVICE);
603 return bReturn;
606 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
608 if ((pDevice)&&(pDevice->IsCaptureDevice))
609 ALCdevice_StartCapture(pDevice);
610 else
611 SetALCError(ALC_INVALID_DEVICE);
614 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
616 if ((pDevice)&&(pDevice->IsCaptureDevice))
617 ALCdevice_StopCapture(pDevice);
618 else
619 SetALCError(ALC_INVALID_DEVICE);
622 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
624 if ((pDevice) && (pDevice->IsCaptureDevice))
625 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
626 else
627 SetALCError(ALC_INVALID_DEVICE);
631 alcGetError
633 Return last ALC generated error code
635 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
637 ALCenum errorCode;
639 (void)device;
641 errorCode = g_eLastContextError;
642 g_eLastContextError = ALC_NO_ERROR;
643 return errorCode;
648 alcSuspendContext
650 Not functional
652 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
654 // Not a lot happens here !
655 (void)pContext;
660 alcProcessContext
662 Not functional
664 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
666 // Not a lot happens here !
667 (void)pContext;
672 alcGetString
674 Returns information about the Device, and error strings
676 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
678 const ALCchar *value = NULL;
680 InitAL();
682 switch (param)
684 case ALC_NO_ERROR:
685 value = alcNoError;
686 break;
688 case ALC_INVALID_ENUM:
689 value = alcErrInvalidEnum;
690 break;
692 case ALC_INVALID_VALUE:
693 value = alcErrInvalidValue;
694 break;
696 case ALC_INVALID_DEVICE:
697 value = alcErrInvalidDevice;
698 break;
700 case ALC_INVALID_CONTEXT:
701 value = alcErrInvalidContext;
702 break;
704 case ALC_OUT_OF_MEMORY:
705 value = alcErrOutOfMemory;
706 break;
708 case ALC_DEFAULT_DEVICE_SPECIFIER:
709 value = alcDefaultDeviceSpecifier;
710 break;
712 case ALC_DEVICE_SPECIFIER:
713 if (pDevice)
714 value = pDevice->szDeviceName;
715 else
716 value = alcDeviceList;
717 break;
719 case ALC_ALL_DEVICES_SPECIFIER:
720 value = alcAllDeviceList;
721 break;
723 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
724 value = alcDefaultAllDeviceSpecifier;
725 break;
727 case ALC_CAPTURE_DEVICE_SPECIFIER:
728 if (pDevice)
729 value = pDevice->szDeviceName;
730 else
731 value = alcCaptureDeviceList;
732 break;
734 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
735 value = alcCaptureDefaultDeviceSpecifier;
736 break;
738 case ALC_EXTENSIONS:
739 value = alcExtensionList;
740 break;
742 default:
743 SetALCError(ALC_INVALID_ENUM);
744 break;
747 return value;
752 alcGetIntegerv
754 Returns information about the Device and the version of Open AL
756 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
758 InitAL();
760 if ((device)&&(device->IsCaptureDevice))
762 SuspendContext(NULL);
764 // Capture device
765 switch (param)
767 case ALC_CAPTURE_SAMPLES:
768 if ((size) && (data))
769 *data = ALCdevice_AvailableSamples(device);
770 else
771 SetALCError(ALC_INVALID_VALUE);
772 break;
774 default:
775 SetALCError(ALC_INVALID_ENUM);
776 break;
779 ProcessContext(NULL);
781 else
783 if(data)
785 // Playback Device
786 switch (param)
788 case ALC_MAJOR_VERSION:
789 if(!size)
790 SetALCError(ALC_INVALID_VALUE);
791 else
792 *data = alcMajorVersion;
793 break;
795 case ALC_MINOR_VERSION:
796 if(!size)
797 SetALCError(ALC_INVALID_VALUE);
798 else
799 *data = alcMinorVersion;
800 break;
802 case ALC_EFX_MAJOR_VERSION:
803 if(!size)
804 SetALCError(ALC_INVALID_VALUE);
805 else
806 *data = alcEFXMajorVersion;
807 break;
809 case ALC_EFX_MINOR_VERSION:
810 if(!size)
811 SetALCError(ALC_INVALID_VALUE);
812 else
813 *data = alcEFXMinorVersion;
814 break;
816 case ALC_MAX_AUXILIARY_SENDS:
817 if(!size)
818 SetALCError(ALC_INVALID_VALUE);
819 else
820 *data = MAX_SENDS;
821 break;
823 case ALC_ATTRIBUTES_SIZE:
824 if(!device)
825 SetALCError(ALC_INVALID_DEVICE);
826 else if(!size)
827 SetALCError(ALC_INVALID_VALUE);
828 else
829 *data = 12;
830 break;
832 case ALC_ALL_ATTRIBUTES:
833 if(!device)
834 SetALCError(ALC_INVALID_DEVICE);
835 else if (size < 7)
836 SetALCError(ALC_INVALID_VALUE);
837 else
839 int i = 0;
841 data[i++] = ALC_FREQUENCY;
842 data[i++] = device->Frequency;
844 data[i++] = ALC_REFRESH;
845 data[i++] = device->Frequency / device->UpdateSize;
847 data[i++] = ALC_SYNC;
848 data[i++] = ALC_FALSE;
850 SuspendContext(NULL);
851 if(device->Context && size >= 12)
853 data[i++] = ALC_MONO_SOURCES;
854 data[i++] = device->Context->lNumMonoSources;
856 data[i++] = ALC_STEREO_SOURCES;
857 data[i++] = device->Context->lNumStereoSources;
859 data[i++] = ALC_MAX_AUXILIARY_SENDS;
860 data[i++] = MAX_SENDS;
862 ProcessContext(NULL);
864 data[i++] = 0;
866 break;
868 case ALC_FREQUENCY:
869 if(!device)
870 SetALCError(ALC_INVALID_DEVICE);
871 else if(!size)
872 SetALCError(ALC_INVALID_VALUE);
873 else
874 *data = device->Frequency;
875 break;
877 case ALC_REFRESH:
878 if(!device)
879 SetALCError(ALC_INVALID_DEVICE);
880 else if(!size)
881 SetALCError(ALC_INVALID_VALUE);
882 else
883 *data = device->Frequency / device->UpdateSize;
884 break;
886 case ALC_SYNC:
887 if(!device)
888 SetALCError(ALC_INVALID_DEVICE);
889 else if(!size)
890 SetALCError(ALC_INVALID_VALUE);
891 else
892 *data = ALC_FALSE;
893 break;
895 case ALC_MONO_SOURCES:
896 if(!device || !device->Context)
897 SetALCError(ALC_INVALID_DEVICE);
898 else if (size != 1)
899 SetALCError(ALC_INVALID_VALUE);
900 else
901 *data = device->Context->lNumMonoSources;
902 break;
904 case ALC_STEREO_SOURCES:
905 if(!device || !device->Context)
906 SetALCError(ALC_INVALID_DEVICE);
907 else if (size != 1)
908 SetALCError(ALC_INVALID_VALUE);
909 else
910 *data = device->Context->lNumStereoSources;
911 break;
913 default:
914 SetALCError(ALC_INVALID_ENUM);
915 break;
918 else if(size)
919 SetALCError(ALC_INVALID_VALUE);
922 return;
927 alcIsExtensionPresent
929 Determines if there is support for a particular extension
931 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
933 ALCboolean bResult = ALC_FALSE;
935 (void)device;
937 if (extName)
939 const char *ptr;
940 size_t len;
942 len = strlen(extName);
943 ptr = alcExtensionList;
944 while(ptr && *ptr)
946 if(strncasecmp(ptr, extName, len) == 0 &&
947 (ptr[len] == '\0' || isspace(ptr[len])))
949 bResult = ALC_TRUE;
950 break;
952 if((ptr=strchr(ptr, ' ')) != NULL)
954 do {
955 ++ptr;
956 } while(isspace(*ptr));
960 else
961 SetALCError(ALC_INVALID_VALUE);
963 return bResult;
968 alcGetProcAddress
970 Retrieves the function address for a particular extension function
972 ALCAPI ALCvoid * ALCAPIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
974 ALCvoid *pFunction = NULL;
975 ALsizei i = 0;
977 (void)device;
979 if (funcName)
981 while(alcFunctions[i].funcName &&
982 strcmp(alcFunctions[i].funcName,funcName) != 0)
983 i++;
984 pFunction = alcFunctions[i].address;
986 else
987 SetALCError(ALC_INVALID_VALUE);
989 return pFunction;
994 alcGetEnumValue
996 Get the value for a particular ALC Enumerated Value
998 ALCAPI ALCenum ALCAPIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
1000 ALsizei i = 0;
1001 ALCenum val;
1003 (void)device;
1005 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
1006 i++;
1007 val = enumeration[i].value;
1009 if(!enumeration[i].enumName)
1010 SetALCError(ALC_INVALID_VALUE);
1012 return val;
1017 alcCreateContext
1019 Create and attach a Context to a particular Device.
1021 ALCAPI ALCcontext* ALCAPIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
1023 ALCcontext *ALContext = NULL;
1024 ALuint ulAttributeIndex, ulRequestedStereoSources;
1026 if ((device)&&(!device->IsCaptureDevice))
1028 // Reset Context Last Error code
1029 g_eLastContextError = ALC_NO_ERROR;
1031 // Current implementation only allows one Context per Device
1032 if(!device->Context)
1034 ALContext = calloc(1, sizeof(ALCcontext));
1035 if(!ALContext)
1037 SetALCError(ALC_OUT_OF_MEMORY);
1038 return NULL;
1041 ALContext->Device = device;
1042 InitContext(ALContext);
1044 device->Context = ALContext;
1046 SuspendContext(NULL);
1048 ALContext->next = g_pContextList;
1049 g_pContextList = ALContext;
1050 g_ulContextCount++;
1052 ProcessContext(NULL);
1054 // Check for Voice Count attributes
1055 if (attrList)
1057 ulAttributeIndex = 0;
1058 while ((ulAttributeIndex < 10) && (attrList[ulAttributeIndex]))
1060 if (attrList[ulAttributeIndex] == ALC_STEREO_SOURCES)
1062 ulRequestedStereoSources = attrList[ulAttributeIndex + 1];
1064 if (ulRequestedStereoSources > ALContext->Device->MaxNoOfSources)
1065 ulRequestedStereoSources = ALContext->Device->MaxNoOfSources;
1067 ALContext->lNumStereoSources = ulRequestedStereoSources;
1068 ALContext->lNumMonoSources = ALContext->Device->MaxNoOfSources - ALContext->lNumStereoSources;
1069 break;
1072 ulAttributeIndex += 2;
1076 else
1078 SetALCError(ALC_INVALID_VALUE);
1079 ALContext = NULL;
1082 else
1083 SetALCError(ALC_INVALID_DEVICE);
1085 return ALContext;
1090 alcDestroyContext
1092 Remove a Context
1094 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
1096 ALCcontext **list;
1098 InitAL();
1100 // Lock context list
1101 SuspendContext(NULL);
1103 if (IsContext(context))
1105 // Lock context
1106 SuspendContext(context);
1108 ReleaseALSources(context);
1109 ReleaseALAuxiliaryEffectSlots(context);
1111 context->Device->Context = NULL;
1113 list = &g_pContextList;
1114 while(*list != context)
1115 list = &(*list)->next;
1117 *list = (*list)->next;
1118 g_ulContextCount--;
1120 // Unlock context
1121 ProcessContext(context);
1123 ExitContext(context);
1125 // Free memory (MUST do this after ProcessContext)
1126 memset(context, 0, sizeof(ALCcontext));
1127 free(context);
1129 else
1130 SetALCError(ALC_INVALID_CONTEXT);
1132 ProcessContext(NULL);
1137 alcGetCurrentContext
1139 Returns the currently active Context
1141 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1143 ALCcontext *pContext = NULL;
1145 InitAL();
1147 SuspendContext(NULL);
1149 pContext = g_pContextList;
1150 while ((pContext) && (!pContext->InUse))
1151 pContext = pContext->next;
1153 ProcessContext(NULL);
1155 return pContext;
1160 alcGetContextsDevice
1162 Returns the Device that a particular Context is attached to
1164 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1166 ALCdevice *pDevice = NULL;
1168 InitAL();
1170 SuspendContext(NULL);
1171 if (IsContext(pContext))
1172 pDevice = pContext->Device;
1173 else
1174 SetALCError(ALC_INVALID_CONTEXT);
1175 ProcessContext(NULL);
1177 return pDevice;
1182 alcMakeContextCurrent
1184 Makes the given Context the active Context
1186 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1188 ALCcontext *ALContext;
1189 ALboolean bReturn = AL_TRUE;
1191 InitAL();
1193 SuspendContext(NULL);
1195 // context must be a valid Context or NULL
1196 if ((IsContext(context)) || (context == NULL))
1198 if ((ALContext=alcGetCurrentContext()))
1200 SuspendContext(ALContext);
1201 ALContext->InUse=AL_FALSE;
1202 ProcessContext(ALContext);
1205 if ((ALContext=context) && (ALContext->Device))
1207 SuspendContext(ALContext);
1208 ALContext->InUse=AL_TRUE;
1209 ProcessContext(ALContext);
1212 else
1214 SetALCError(ALC_INVALID_CONTEXT);
1215 bReturn = AL_FALSE;
1218 ProcessContext(NULL);
1220 return bReturn;
1225 alcOpenDevice
1227 Open the Device specified.
1229 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1231 ALboolean bDeviceFound = AL_FALSE;
1232 ALCdevice *device;
1233 ALint i;
1235 InitAL();
1237 if(deviceName && !deviceName[0])
1238 deviceName = NULL;
1240 device = malloc(sizeof(ALCdevice));
1241 if (device)
1243 const char *fmt;
1245 //Initialise device structure
1246 memset(device, 0, sizeof(ALCdevice));
1248 //Validate device
1249 device->IsCaptureDevice = AL_FALSE;
1251 //Set output format
1252 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1253 if((ALint)device->Frequency <= 0)
1254 device->Frequency = SWMIXER_OUTPUT_RATE;
1256 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1257 if(fmt[0])
1258 device->Format = alGetEnumValue(fmt);
1260 if(!aluChannelsFromFormat(device->Format))
1261 device->Format = AL_FORMAT_STEREO16;
1263 device->UpdateSize = GetConfigValueInt(NULL, "refresh", 4096);
1264 if((ALint)device->UpdateSize <= 0)
1265 device->UpdateSize = 4096;
1267 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1268 if((ALint)device->MaxNoOfSources <= 0)
1269 device->MaxNoOfSources = 256;
1271 // Find a playback device to open
1272 SuspendContext(NULL);
1273 for(i = 0;BackendList[i].Init;i++)
1275 device->Funcs = &BackendList[i].Funcs;
1276 if(ALCdevice_OpenPlayback(device, deviceName))
1278 device->next = g_pDeviceList;
1279 g_pDeviceList = device;
1280 g_ulDeviceCount++;
1282 bDeviceFound = AL_TRUE;
1283 break;
1286 ProcessContext(NULL);
1288 if (!bDeviceFound)
1290 // No suitable output device found
1291 SetALCError(ALC_INVALID_VALUE);
1292 free(device);
1293 device = NULL;
1296 else
1297 SetALCError(ALC_OUT_OF_MEMORY);
1299 return device;
1304 alcCloseDevice
1306 Close the specified Device
1308 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1310 ALCboolean bReturn = ALC_FALSE;
1311 ALCdevice **list;
1313 if ((pDevice)&&(!pDevice->IsCaptureDevice))
1315 SuspendContext(NULL);
1317 list = &g_pDeviceList;
1318 while(*list != pDevice)
1319 list = &(*list)->next;
1321 *list = (*list)->next;
1322 g_ulDeviceCount--;
1324 ProcessContext(NULL);
1326 if(pDevice->Context)
1328 #ifdef _DEBUG
1329 AL_PRINT("alcCloseDevice(): destroying 1 Context\n");
1330 #endif
1331 alcDestroyContext(pDevice->Context);
1333 ALCdevice_ClosePlayback(pDevice);
1335 //Release device structure
1336 memset(pDevice, 0, sizeof(ALCdevice));
1337 free(pDevice);
1339 bReturn = ALC_TRUE;
1341 else
1342 SetALCError(ALC_INVALID_DEVICE);
1344 return bReturn;
1348 ALCvoid ReleaseALC(ALCvoid)
1350 #ifdef _DEBUG
1351 if(g_ulDeviceCount > 0)
1352 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
1353 #endif
1355 while(g_pDeviceList)
1357 if(g_pDeviceList->IsCaptureDevice)
1358 alcCaptureCloseDevice(g_pDeviceList);
1359 else
1360 alcCloseDevice(g_pDeviceList);
1364 ///////////////////////////////////////////////////////