Simplify ALSA PCM parameter retrieval
[openal-soft.git] / Alc / ALc.c
blobd04b0a05c83ebaefd0636dc22ffa3745e9a6fb31
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"
40 ///////////////////////////////////////////////////////
41 // DEBUG INFORMATION
43 char _alDebug[256];
45 ///////////////////////////////////////////////////////
48 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
49 static struct {
50 const char *name;
51 void (*Init)(BackendFuncs*);
52 BackendFuncs Funcs;
53 } BackendList[] = {
54 #ifdef HAVE_ALSA
55 { "alsa", alc_alsa_init, EmptyFuncs },
56 #endif
57 #ifdef HAVE_OSS
58 { "oss", alc_oss_init, EmptyFuncs },
59 #endif
60 #ifdef HAVE_SOLARIS
61 { "solaris", alc_solaris_init, EmptyFuncs },
62 #endif
63 #ifdef HAVE_DSOUND
64 { "dsound", alcDSoundInit, EmptyFuncs },
65 #endif
66 #ifdef HAVE_WINMM
67 { "winmm", alcWinMMInit, EmptyFuncs },
68 #endif
69 #ifdef HAVE_PORTAUDIO
70 { "port", alc_pa_init, EmptyFuncs },
71 #endif
72 #ifdef HAVE_PULSEAUDIO
73 { "pulse", alc_pulse_init, EmptyFuncs },
74 #endif
76 { "wave", alc_wave_init, EmptyFuncs },
78 { NULL, NULL, EmptyFuncs }
80 #undef EmptyFuncs
82 ///////////////////////////////////////////////////////
84 #define ALC_EFX_MAJOR_VERSION 0x20001
85 #define ALC_EFX_MINOR_VERSION 0x20002
86 #define ALC_MAX_AUXILIARY_SENDS 0x20003
88 ///////////////////////////////////////////////////////
89 // STRING and EXTENSIONS
91 typedef struct ALCfunction_struct
93 ALCchar *funcName;
94 ALvoid *address;
95 } ALCfunction;
97 static ALCfunction alcFunctions[] = {
98 { "alcCreateContext", (ALvoid *) alcCreateContext },
99 { "alcMakeContextCurrent", (ALvoid *) alcMakeContextCurrent },
100 { "alcProcessContext", (ALvoid *) alcProcessContext },
101 { "alcSuspendContext", (ALvoid *) alcSuspendContext },
102 { "alcDestroyContext", (ALvoid *) alcDestroyContext },
103 { "alcGetCurrentContext", (ALvoid *) alcGetCurrentContext },
104 { "alcGetContextsDevice", (ALvoid *) alcGetContextsDevice },
105 { "alcOpenDevice", (ALvoid *) alcOpenDevice },
106 { "alcCloseDevice", (ALvoid *) alcCloseDevice },
107 { "alcGetError", (ALvoid *) alcGetError },
108 { "alcIsExtensionPresent", (ALvoid *) alcIsExtensionPresent },
109 { "alcGetProcAddress", (ALvoid *) alcGetProcAddress },
110 { "alcGetEnumValue", (ALvoid *) alcGetEnumValue },
111 { "alcGetString", (ALvoid *) alcGetString },
112 { "alcGetIntegerv", (ALvoid *) alcGetIntegerv },
113 { "alcCaptureOpenDevice", (ALvoid *) alcCaptureOpenDevice },
114 { "alcCaptureCloseDevice", (ALvoid *) alcCaptureCloseDevice },
115 { "alcCaptureStart", (ALvoid *) alcCaptureStart },
116 { "alcCaptureStop", (ALvoid *) alcCaptureStop },
117 { "alcCaptureSamples", (ALvoid *) alcCaptureSamples },
118 { NULL, (ALvoid *) NULL }
121 static ALenums enumeration[]={
122 // Types
123 { (ALchar *)"ALC_INVALID", ALC_INVALID },
124 { (ALchar *)"ALC_FALSE", ALC_FALSE },
125 { (ALchar *)"ALC_TRUE", ALC_TRUE },
127 // ALC Properties
128 { (ALchar *)"ALC_MAJOR_VERSION", ALC_MAJOR_VERSION },
129 { (ALchar *)"ALC_MINOR_VERSION", ALC_MINOR_VERSION },
130 { (ALchar *)"ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE },
131 { (ALchar *)"ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES },
132 { (ALchar *)"ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER },
133 { (ALchar *)"ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER },
134 { (ALchar *)"ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER },
135 { (ALchar *)"ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER },
136 { (ALchar *)"ALC_EXTENSIONS", ALC_EXTENSIONS },
137 { (ALchar *)"ALC_FREQUENCY", ALC_FREQUENCY },
138 { (ALchar *)"ALC_REFRESH", ALC_REFRESH },
139 { (ALchar *)"ALC_SYNC", ALC_SYNC },
140 { (ALchar *)"ALC_MONO_SOURCES", ALC_MONO_SOURCES },
141 { (ALchar *)"ALC_STEREO_SOURCES", ALC_STEREO_SOURCES },
142 { (ALchar *)"ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER },
143 { (ALchar *)"ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER},
144 { (ALchar *)"ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES },
146 // EFX Properties
147 { (ALchar *)"ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION },
148 { (ALchar *)"ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION },
149 { (ALchar *)"ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS },
151 // ALC Error Message
152 { (ALchar *)"ALC_NO_ERROR", ALC_NO_ERROR },
153 { (ALchar *)"ALC_INVALID_DEVICE", ALC_INVALID_DEVICE },
154 { (ALchar *)"ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT },
155 { (ALchar *)"ALC_INVALID_ENUM", ALC_INVALID_ENUM },
156 { (ALchar *)"ALC_INVALID_VALUE", ALC_INVALID_VALUE },
157 { (ALchar *)"ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY },
158 { (ALchar *)NULL, (ALenum)0 }
160 // Error strings
161 static const ALCchar alcNoError[] = "No Error";
162 static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
163 static const ALCchar alcErrInvalidContext[] = "Invalid Context";
164 static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
165 static const ALCchar alcErrInvalidValue[] = "Invalid Value";
166 static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
168 // Context strings
169 static ALCchar alcDeviceList[2048];
170 static ALCchar alcAllDeviceList[2048];
171 static ALCchar alcCaptureDeviceList[2048];
172 // Default is always the first in the list
173 static ALCchar *alcDefaultDeviceSpecifier = alcDeviceList;
174 static ALCchar *alcDefaultAllDeviceSpecifier = alcAllDeviceList;
175 static ALCchar *alcCaptureDefaultDeviceSpecifier = alcCaptureDeviceList;
178 static ALCchar alcExtensionList[] = "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_EFX";
179 static ALCint alcMajorVersion = 1;
180 static ALCint alcMinorVersion = 1;
182 static ALCint alcEFXMajorVersion = 1;
183 static ALCint alcEFXMinorVersion = 0;
185 ///////////////////////////////////////////////////////
188 ///////////////////////////////////////////////////////
189 // Global Variables
191 static ALCdevice *g_pDeviceList = NULL;
192 static ALCuint g_ulDeviceCount = 0;
194 static CRITICAL_SECTION g_csMutex;
196 // Context List
197 static ALCcontext *g_pContextList = NULL;
198 static ALCuint g_ulContextCount = 0;
200 // Context Error
201 static ALCenum g_eLastContextError = ALC_NO_ERROR;
203 static ALboolean init_done = AL_FALSE;
205 ///////////////////////////////////////////////////////
208 ///////////////////////////////////////////////////////
209 // ALC Related helper functions
210 #ifdef _WIN32
211 BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
213 (void)lpReserved;
215 // Perform actions based on the reason for calling.
216 switch(ul_reason_for_call)
218 case DLL_PROCESS_ATTACH:
219 DisableThreadLibraryCalls(hModule);
220 break;
222 case DLL_PROCESS_DETACH:
223 if(!init_done)
224 break;
225 ReleaseALC();
226 ReleaseALBuffers();
227 ReleaseALEffects();
228 ReleaseALFilters();
229 FreeALConfig();
230 ALTHUNK_EXIT();
231 DeleteCriticalSection(&g_csMutex);
232 break;
234 return TRUE;
236 #else
237 #ifdef HAVE_GCC_DESTRUCTOR
238 static void my_deinit() __attribute__((destructor));
239 static void my_deinit()
241 static ALenum once = AL_FALSE;
242 if(once || !init_done) return;
243 once = AL_TRUE;
245 ReleaseALC();
246 ReleaseALBuffers();
247 ReleaseALEffects();
248 ReleaseALFilters();
249 FreeALConfig();
250 ALTHUNK_EXIT();
251 DeleteCriticalSection(&g_csMutex);
253 #endif
254 #endif
256 static void InitAL(void)
258 if(!init_done)
260 int i;
261 const char *devs, *str;
263 init_done = AL_TRUE;
265 InitializeCriticalSection(&g_csMutex);
266 ALTHUNK_INIT();
267 ReadALConfig();
269 devs = GetConfigValue(NULL, "drivers", "");
270 if(devs[0])
272 int n;
273 size_t len;
274 const char *next = devs;
276 i = 0;
278 do {
279 devs = next;
280 next = strchr(devs, ',');
282 if(!devs[0] || devs[0] == ',')
283 continue;
285 len = (next ? ((size_t)(next-devs)) : strlen(devs));
286 for(n = i;BackendList[n].Init;n++)
288 if(len == strlen(BackendList[n].name) &&
289 strncmp(BackendList[n].name, devs, len) == 0)
291 const char *name = BackendList[i].name;
292 void (*Init)(BackendFuncs*) = BackendList[i].Init;
294 BackendList[i].name = BackendList[n].name;
295 BackendList[i].Init = BackendList[n].Init;
297 BackendList[n].name = name;
298 BackendList[n].Init = Init;
300 i++;
303 } while(next++);
305 BackendList[i].name = NULL;
306 BackendList[i].Init = NULL;
309 for(i = 0;BackendList[i].Init;i++)
310 BackendList[i].Init(&BackendList[i].Funcs);
312 str = GetConfigValue(NULL, "stereodup", "false");
313 DuplicateStereo = (strcasecmp(str, "true") == 0 ||
314 strcasecmp(str, "yes") == 0 ||
315 strcasecmp(str, "on") == 0 ||
316 atoi(str) != 0);
318 str = GetConfigValue(NULL, "excludefx", "");
319 if(str[0])
321 const struct {
322 const char *name;
323 int type;
324 } EffectList[] = {
325 { "reverb", REVERB },
326 { "echo", ECHO },
327 { NULL, 0 }
329 int n;
330 size_t len;
331 const char *next = str;
333 do {
334 str = next;
335 next = strchr(str, ',');
337 if(!str[0] || next == str)
338 continue;
340 len = (next ? ((size_t)(next-str)) : strlen(str));
341 for(n = 0;EffectList[n].name;n++)
343 if(len == strlen(EffectList[n].name) &&
344 strncmp(EffectList[n].name, str, len) == 0)
345 DisabledEffects[EffectList[n].type] = AL_TRUE;
347 } while(next++);
352 ALCchar *AppendDeviceList(char *name)
354 static size_t pos;
355 ALCchar *ret = alcDeviceList+pos;
356 if(pos >= sizeof(alcDeviceList))
358 AL_PRINT("Not enough room to add %s!\n", name);
359 return alcDeviceList + sizeof(alcDeviceList) - 1;
361 pos += snprintf(alcDeviceList+pos, sizeof(alcDeviceList)-pos-1, "%s", name) + 1;
362 return ret;
365 ALCchar *AppendAllDeviceList(char *name)
367 static size_t pos;
368 ALCchar *ret = alcAllDeviceList+pos;
369 if(pos >= sizeof(alcAllDeviceList))
371 AL_PRINT("Not enough room to add %s!\n", name);
372 return alcAllDeviceList + sizeof(alcAllDeviceList) - 1;
374 pos += snprintf(alcAllDeviceList+pos, sizeof(alcAllDeviceList)-pos-1, "%s", name) + 1;
375 return ret;
378 ALCchar *AppendCaptureDeviceList(char *name)
380 static size_t pos;
381 ALCchar *ret = alcCaptureDeviceList+pos;
382 if(pos >= sizeof(alcCaptureDeviceList))
384 AL_PRINT("Not enough room to add %s!\n", name);
385 return alcCaptureDeviceList + sizeof(alcCaptureDeviceList) - 1;
387 pos += snprintf(alcCaptureDeviceList+pos, sizeof(alcCaptureDeviceList)-pos-1, "%s", name) + 1;
388 return ret;
392 IsContext
394 Check pContext is a valid Context pointer
396 static ALCboolean IsContext(ALCcontext *pContext)
398 ALCcontext *pTempContext;
400 pTempContext = g_pContextList;
401 while (pTempContext && pTempContext != pContext)
402 pTempContext = pTempContext->next;
404 return (pTempContext ? ALC_TRUE : ALC_FALSE);
409 SetALCError
411 Store latest ALC Error
413 ALCvoid SetALCError(ALenum errorCode)
415 g_eLastContextError = errorCode;
420 SuspendContext
422 Thread-safe entry
424 ALCvoid SuspendContext(ALCcontext *pContext)
426 (void)pContext;
427 EnterCriticalSection(&g_csMutex);
432 ProcessContext
434 Thread-safe exit
436 ALCvoid ProcessContext(ALCcontext *pContext)
438 (void)pContext;
439 LeaveCriticalSection(&g_csMutex);
444 InitContext
446 Initialize Context variables
448 static ALvoid InitContext(ALCcontext *pContext)
450 int level;
452 //Initialise listener
453 pContext->Listener.Gain = 1.0f;
454 pContext->Listener.MetersPerUnit = 1.0f;
455 pContext->Listener.Position[0] = 0.0f;
456 pContext->Listener.Position[1] = 0.0f;
457 pContext->Listener.Position[2] = 0.0f;
458 pContext->Listener.Velocity[0] = 0.0f;
459 pContext->Listener.Velocity[1] = 0.0f;
460 pContext->Listener.Velocity[2] = 0.0f;
461 pContext->Listener.Forward[0] = 0.0f;
462 pContext->Listener.Forward[1] = 0.0f;
463 pContext->Listener.Forward[2] = -1.0f;
464 pContext->Listener.Up[0] = 0.0f;
465 pContext->Listener.Up[1] = 1.0f;
466 pContext->Listener.Up[2] = 0.0f;
468 //Validate pContext
469 pContext->LastError = AL_NO_ERROR;
470 pContext->InUse = AL_FALSE;
472 //Set output format
473 pContext->Frequency = pContext->Device->Frequency;
475 //Set globals
476 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
477 pContext->DopplerFactor = 1.0f;
478 pContext->DopplerVelocity = 1.0f;
479 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
481 pContext->lNumStereoSources = 1;
482 pContext->lNumMonoSources = pContext->Device->MaxNoOfSources - pContext->lNumStereoSources;
484 pContext->AuxiliaryEffectSlotMax = GetConfigValueInt(NULL, "slots", 4);
485 pContext->NumSends = GetConfigValueInt(NULL, "sends", MAX_SENDS);
486 if(pContext->NumSends > MAX_SENDS)
487 pContext->NumSends = MAX_SENDS;
489 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";
491 level = GetConfigValueInt(NULL, "cf_level", 0);
492 if(level > 0 && level <= 6)
494 pContext->bs2b = calloc(1, sizeof(*pContext->bs2b));
495 bs2b_set_srate(pContext->bs2b, pContext->Frequency);
496 bs2b_set_level(pContext->bs2b, level);
499 aluInitPanning(pContext);
504 ExitContext
506 Clean up Context, destroy any remaining Sources
508 static ALCvoid ExitContext(ALCcontext *pContext)
510 //Invalidate context
511 pContext->LastError = AL_NO_ERROR;
512 pContext->InUse = AL_FALSE;
514 free(pContext->bs2b);
515 pContext->bs2b = NULL;
518 ///////////////////////////////////////////////////////
521 ///////////////////////////////////////////////////////
522 // ALC Functions calls
525 // This should probably move to another c file but for now ...
526 ALCAPI ALCdevice* ALCAPIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
528 ALCboolean DeviceFound = ALC_FALSE;
529 ALCdevice *pDevice = NULL;
530 ALCint i;
532 InitAL();
534 if(SampleSize <= 0)
536 SetALCError(ALC_INVALID_VALUE);
537 return NULL;
540 if(deviceName && !deviceName[0])
541 deviceName = NULL;
543 pDevice = malloc(sizeof(ALCdevice));
544 if (pDevice)
546 //Initialise device structure
547 memset(pDevice, 0, sizeof(ALCdevice));
549 //Validate device
550 pDevice->IsCaptureDevice = AL_TRUE;
552 pDevice->Frequency = frequency;
553 pDevice->Format = format;
555 for(i = 0;BackendList[i].Init;i++)
557 pDevice->Funcs = &BackendList[i].Funcs;
558 if(ALCdevice_OpenCapture(pDevice, deviceName, frequency, format, SampleSize))
560 SuspendContext(NULL);
561 pDevice->next = g_pDeviceList;
562 g_pDeviceList = pDevice;
563 g_ulDeviceCount++;
564 ProcessContext(NULL);
566 DeviceFound = ALC_TRUE;
567 break;
571 if(!DeviceFound)
573 SetALCError(ALC_INVALID_VALUE);
574 free(pDevice);
575 pDevice = NULL;
578 else
579 SetALCError(ALC_OUT_OF_MEMORY);
581 return pDevice;
584 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
586 ALCboolean bReturn = ALC_FALSE;
587 ALCdevice **list;
589 if ((pDevice)&&(pDevice->IsCaptureDevice))
591 SuspendContext(NULL);
593 list = &g_pDeviceList;
594 while(*list != pDevice)
595 list = &(*list)->next;
597 *list = (*list)->next;
598 g_ulDeviceCount--;
600 ProcessContext(NULL);
602 ALCdevice_CloseCapture(pDevice);
603 free(pDevice);
605 bReturn = ALC_TRUE;
607 else
608 SetALCError(ALC_INVALID_DEVICE);
610 return bReturn;
613 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
615 if ((pDevice)&&(pDevice->IsCaptureDevice))
616 ALCdevice_StartCapture(pDevice);
617 else
618 SetALCError(ALC_INVALID_DEVICE);
621 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
623 if ((pDevice)&&(pDevice->IsCaptureDevice))
624 ALCdevice_StopCapture(pDevice);
625 else
626 SetALCError(ALC_INVALID_DEVICE);
629 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
631 if ((pDevice) && (pDevice->IsCaptureDevice))
632 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
633 else
634 SetALCError(ALC_INVALID_DEVICE);
638 alcGetError
640 Return last ALC generated error code
642 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
644 ALCenum errorCode;
646 (void)device;
648 errorCode = g_eLastContextError;
649 g_eLastContextError = ALC_NO_ERROR;
650 return errorCode;
655 alcSuspendContext
657 Not functional
659 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
661 // Not a lot happens here !
662 (void)pContext;
667 alcProcessContext
669 Not functional
671 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
673 // Not a lot happens here !
674 (void)pContext;
679 alcGetString
681 Returns information about the Device, and error strings
683 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
685 const ALCchar *value = NULL;
687 InitAL();
689 switch (param)
691 case ALC_NO_ERROR:
692 value = alcNoError;
693 break;
695 case ALC_INVALID_ENUM:
696 value = alcErrInvalidEnum;
697 break;
699 case ALC_INVALID_VALUE:
700 value = alcErrInvalidValue;
701 break;
703 case ALC_INVALID_DEVICE:
704 value = alcErrInvalidDevice;
705 break;
707 case ALC_INVALID_CONTEXT:
708 value = alcErrInvalidContext;
709 break;
711 case ALC_OUT_OF_MEMORY:
712 value = alcErrOutOfMemory;
713 break;
715 case ALC_DEFAULT_DEVICE_SPECIFIER:
716 value = alcDefaultDeviceSpecifier;
717 break;
719 case ALC_DEVICE_SPECIFIER:
720 if (pDevice)
721 value = pDevice->szDeviceName;
722 else
723 value = alcDeviceList;
724 break;
726 case ALC_ALL_DEVICES_SPECIFIER:
727 value = alcAllDeviceList;
728 break;
730 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
731 value = alcDefaultAllDeviceSpecifier;
732 break;
734 case ALC_CAPTURE_DEVICE_SPECIFIER:
735 if (pDevice)
736 value = pDevice->szDeviceName;
737 else
738 value = alcCaptureDeviceList;
739 break;
741 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
742 value = alcCaptureDefaultDeviceSpecifier;
743 break;
745 case ALC_EXTENSIONS:
746 value = alcExtensionList;
747 break;
749 default:
750 SetALCError(ALC_INVALID_ENUM);
751 break;
754 return value;
759 alcGetIntegerv
761 Returns information about the Device and the version of Open AL
763 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
765 InitAL();
767 if ((device)&&(device->IsCaptureDevice))
769 SuspendContext(NULL);
771 // Capture device
772 switch (param)
774 case ALC_CAPTURE_SAMPLES:
775 if ((size) && (data))
776 *data = ALCdevice_AvailableSamples(device);
777 else
778 SetALCError(ALC_INVALID_VALUE);
779 break;
781 default:
782 SetALCError(ALC_INVALID_ENUM);
783 break;
786 ProcessContext(NULL);
788 else
790 if(data)
792 // Playback Device
793 switch (param)
795 case ALC_MAJOR_VERSION:
796 if(!size)
797 SetALCError(ALC_INVALID_VALUE);
798 else
799 *data = alcMajorVersion;
800 break;
802 case ALC_MINOR_VERSION:
803 if(!size)
804 SetALCError(ALC_INVALID_VALUE);
805 else
806 *data = alcMinorVersion;
807 break;
809 case ALC_EFX_MAJOR_VERSION:
810 if(!size)
811 SetALCError(ALC_INVALID_VALUE);
812 else
813 *data = alcEFXMajorVersion;
814 break;
816 case ALC_EFX_MINOR_VERSION:
817 if(!size)
818 SetALCError(ALC_INVALID_VALUE);
819 else
820 *data = alcEFXMinorVersion;
821 break;
823 case ALC_MAX_AUXILIARY_SENDS:
824 if(!size)
825 SetALCError(ALC_INVALID_VALUE);
826 else if(device && device->Context)
827 *data = device->Context->NumSends;
828 else
829 *data = MAX_SENDS;
830 break;
832 case ALC_ATTRIBUTES_SIZE:
833 if(!device)
834 SetALCError(ALC_INVALID_DEVICE);
835 else if(!size)
836 SetALCError(ALC_INVALID_VALUE);
837 else
838 *data = 12;
839 break;
841 case ALC_ALL_ATTRIBUTES:
842 if(!device)
843 SetALCError(ALC_INVALID_DEVICE);
844 else if (size < 7)
845 SetALCError(ALC_INVALID_VALUE);
846 else
848 int i = 0;
850 data[i++] = ALC_FREQUENCY;
851 data[i++] = device->Frequency;
853 data[i++] = ALC_REFRESH;
854 data[i++] = device->Frequency / device->UpdateSize;
856 data[i++] = ALC_SYNC;
857 data[i++] = ALC_FALSE;
859 SuspendContext(NULL);
860 if(device->Context && size >= 12)
862 data[i++] = ALC_MONO_SOURCES;
863 data[i++] = device->Context->lNumMonoSources;
865 data[i++] = ALC_STEREO_SOURCES;
866 data[i++] = device->Context->lNumStereoSources;
868 data[i++] = ALC_MAX_AUXILIARY_SENDS;
869 data[i++] = device->Context->NumSends;
871 ProcessContext(NULL);
873 data[i++] = 0;
875 break;
877 case ALC_FREQUENCY:
878 if(!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(!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(!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(!device || !device->Context)
906 SetALCError(ALC_INVALID_DEVICE);
907 else if (size != 1)
908 SetALCError(ALC_INVALID_VALUE);
909 else
910 *data = device->Context->lNumMonoSources;
911 break;
913 case ALC_STEREO_SOURCES:
914 if(!device || !device->Context)
915 SetALCError(ALC_INVALID_DEVICE);
916 else if (size != 1)
917 SetALCError(ALC_INVALID_VALUE);
918 else
919 *data = device->Context->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 ((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 ALContext->Device = device;
1052 InitContext(ALContext);
1054 device->Context = ALContext;
1056 SuspendContext(NULL);
1058 ALContext->next = g_pContextList;
1059 g_pContextList = ALContext;
1060 g_ulContextCount++;
1062 ProcessContext(NULL);
1064 // Check for attributes
1065 if (attrList)
1067 ulAttributeIndex = 0;
1068 while ((ulAttributeIndex < 10) && (attrList[ulAttributeIndex]))
1070 if (attrList[ulAttributeIndex] == ALC_STEREO_SOURCES)
1072 ulRequestedStereoSources = attrList[ulAttributeIndex + 1];
1074 if (ulRequestedStereoSources > ALContext->Device->MaxNoOfSources)
1075 ulRequestedStereoSources = ALContext->Device->MaxNoOfSources;
1077 ALContext->lNumStereoSources = ulRequestedStereoSources;
1078 ALContext->lNumMonoSources = ALContext->Device->MaxNoOfSources - ALContext->lNumStereoSources;
1081 if(attrList[ulAttributeIndex] == ALC_MAX_AUXILIARY_SENDS)
1083 RequestedSends = attrList[ulAttributeIndex + 1];
1085 if(RequestedSends > ALContext->NumSends)
1086 RequestedSends = ALContext->NumSends;
1088 ALContext->NumSends = RequestedSends;
1091 ulAttributeIndex += 2;
1095 else
1097 SetALCError(ALC_INVALID_VALUE);
1098 ALContext = NULL;
1101 else
1102 SetALCError(ALC_INVALID_DEVICE);
1104 return ALContext;
1109 alcDestroyContext
1111 Remove a Context
1113 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
1115 ALCcontext **list;
1117 InitAL();
1119 // Lock context list
1120 SuspendContext(NULL);
1122 if (IsContext(context))
1124 // Lock context
1125 SuspendContext(context);
1127 ReleaseALSources(context);
1128 ReleaseALAuxiliaryEffectSlots(context);
1130 context->Device->Context = NULL;
1132 list = &g_pContextList;
1133 while(*list != context)
1134 list = &(*list)->next;
1136 *list = (*list)->next;
1137 g_ulContextCount--;
1139 // Unlock context
1140 ProcessContext(context);
1142 ExitContext(context);
1144 // Free memory (MUST do this after ProcessContext)
1145 memset(context, 0, sizeof(ALCcontext));
1146 free(context);
1148 else
1149 SetALCError(ALC_INVALID_CONTEXT);
1151 ProcessContext(NULL);
1156 alcGetCurrentContext
1158 Returns the currently active Context
1160 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1162 ALCcontext *pContext = NULL;
1164 InitAL();
1166 SuspendContext(NULL);
1168 pContext = g_pContextList;
1169 while ((pContext) && (!pContext->InUse))
1170 pContext = pContext->next;
1172 ProcessContext(NULL);
1174 return pContext;
1179 alcGetContextsDevice
1181 Returns the Device that a particular Context is attached to
1183 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1185 ALCdevice *pDevice = NULL;
1187 InitAL();
1189 SuspendContext(NULL);
1190 if (IsContext(pContext))
1191 pDevice = pContext->Device;
1192 else
1193 SetALCError(ALC_INVALID_CONTEXT);
1194 ProcessContext(NULL);
1196 return pDevice;
1201 alcMakeContextCurrent
1203 Makes the given Context the active Context
1205 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1207 ALCcontext *ALContext;
1208 ALboolean bReturn = AL_TRUE;
1210 InitAL();
1212 SuspendContext(NULL);
1214 // context must be a valid Context or NULL
1215 if ((IsContext(context)) || (context == NULL))
1217 if ((ALContext=alcGetCurrentContext()))
1219 SuspendContext(ALContext);
1220 ALContext->InUse=AL_FALSE;
1221 ProcessContext(ALContext);
1224 if ((ALContext=context) && (ALContext->Device))
1226 SuspendContext(ALContext);
1227 ALContext->InUse=AL_TRUE;
1228 ProcessContext(ALContext);
1231 else
1233 SetALCError(ALC_INVALID_CONTEXT);
1234 bReturn = AL_FALSE;
1237 ProcessContext(NULL);
1239 return bReturn;
1244 alcOpenDevice
1246 Open the Device specified.
1248 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1250 ALboolean bDeviceFound = AL_FALSE;
1251 ALCdevice *device;
1252 ALint i;
1254 InitAL();
1256 if(deviceName && !deviceName[0])
1257 deviceName = NULL;
1259 device = malloc(sizeof(ALCdevice));
1260 if (device)
1262 const char *fmt;
1264 //Initialise device structure
1265 memset(device, 0, sizeof(ALCdevice));
1267 //Validate device
1268 device->IsCaptureDevice = AL_FALSE;
1270 //Set output format
1271 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1272 if((ALint)device->Frequency <= 0)
1273 device->Frequency = SWMIXER_OUTPUT_RATE;
1275 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1276 if(fmt[0])
1277 device->Format = alGetEnumValue(fmt);
1279 if(!aluChannelsFromFormat(device->Format))
1280 device->Format = AL_FORMAT_STEREO16;
1282 device->UpdateSize = GetConfigValueInt(NULL, "refresh", 4096);
1283 if((ALint)device->UpdateSize <= 0)
1284 device->UpdateSize = 4096;
1286 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1287 if((ALint)device->MaxNoOfSources <= 0)
1288 device->MaxNoOfSources = 256;
1290 // Find a playback device to open
1291 SuspendContext(NULL);
1292 for(i = 0;BackendList[i].Init;i++)
1294 device->Funcs = &BackendList[i].Funcs;
1295 if(ALCdevice_OpenPlayback(device, deviceName))
1297 device->next = g_pDeviceList;
1298 g_pDeviceList = device;
1299 g_ulDeviceCount++;
1301 bDeviceFound = AL_TRUE;
1302 break;
1305 ProcessContext(NULL);
1307 if (!bDeviceFound)
1309 // No suitable output device found
1310 SetALCError(ALC_INVALID_VALUE);
1311 free(device);
1312 device = NULL;
1315 else
1316 SetALCError(ALC_OUT_OF_MEMORY);
1318 return device;
1323 alcCloseDevice
1325 Close the specified Device
1327 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1329 ALCboolean bReturn = ALC_FALSE;
1330 ALCdevice **list;
1332 if ((pDevice)&&(!pDevice->IsCaptureDevice))
1334 SuspendContext(NULL);
1336 list = &g_pDeviceList;
1337 while(*list != pDevice)
1338 list = &(*list)->next;
1340 *list = (*list)->next;
1341 g_ulDeviceCount--;
1343 ProcessContext(NULL);
1345 if(pDevice->Context)
1347 #ifdef _DEBUG
1348 AL_PRINT("alcCloseDevice(): destroying 1 Context\n");
1349 #endif
1350 alcDestroyContext(pDevice->Context);
1352 ALCdevice_ClosePlayback(pDevice);
1354 //Release device structure
1355 memset(pDevice, 0, sizeof(ALCdevice));
1356 free(pDevice);
1358 bReturn = ALC_TRUE;
1360 else
1361 SetALCError(ALC_INVALID_DEVICE);
1363 return bReturn;
1367 ALCvoid ReleaseALC(ALCvoid)
1369 #ifdef _DEBUG
1370 if(g_ulDeviceCount > 0)
1371 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
1372 #endif
1374 while(g_pDeviceList)
1376 if(g_pDeviceList->IsCaptureDevice)
1377 alcCaptureCloseDevice(g_pDeviceList);
1378 else
1379 alcCloseDevice(g_pDeviceList);
1383 ///////////////////////////////////////////////////////