Don't remove the devices from the device list on unexpected shutdown
[openal-soft.git] / Alc / ALc.c
blob714a142fc622726cc7ea2da309237f1605bbac40
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 "alExtension.h"
37 #include "alAuxEffectSlot.h"
38 #include "bs2b.h"
40 ///////////////////////////////////////////////////////
41 // DEBUG INFORMATION
43 char _alDebug[256];
45 ///////////////////////////////////////////////////////
48 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
49 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_DSOUND
61 { "dsound", alcDSoundInit, EmptyFuncs },
62 #endif
63 #ifdef HAVE_WINMM
64 { "winmm", alcWinMMInit, EmptyFuncs },
65 #endif
67 { "wave", alc_wave_init, EmptyFuncs },
69 { NULL, NULL, EmptyFuncs }
71 #undef EmptyFuncs
73 ///////////////////////////////////////////////////////
75 #define ALC_EFX_MAJOR_VERSION 0x20001
76 #define ALC_EFX_MINOR_VERSION 0x20002
77 #define ALC_MAX_AUXILIARY_SENDS 0x20003
79 ///////////////////////////////////////////////////////
80 // STRING and EXTENSIONS
82 typedef struct ALCfunction_struct
84 ALCchar *funcName;
85 ALvoid *address;
86 } ALCfunction;
88 static ALCfunction alcFunctions[] = {
89 { "alcCreateContext", (ALvoid *) alcCreateContext },
90 { "alcMakeContextCurrent", (ALvoid *) alcMakeContextCurrent },
91 { "alcProcessContext", (ALvoid *) alcProcessContext },
92 { "alcSuspendContext", (ALvoid *) alcSuspendContext },
93 { "alcDestroyContext", (ALvoid *) alcDestroyContext },
94 { "alcGetCurrentContext", (ALvoid *) alcGetCurrentContext },
95 { "alcGetContextsDevice", (ALvoid *) alcGetContextsDevice },
96 { "alcOpenDevice", (ALvoid *) alcOpenDevice },
97 { "alcCloseDevice", (ALvoid *) alcCloseDevice },
98 { "alcGetError", (ALvoid *) alcGetError },
99 { "alcIsExtensionPresent", (ALvoid *) alcIsExtensionPresent },
100 { "alcGetProcAddress", (ALvoid *) alcGetProcAddress },
101 { "alcGetEnumValue", (ALvoid *) alcGetEnumValue },
102 { "alcGetString", (ALvoid *) alcGetString },
103 { "alcGetIntegerv", (ALvoid *) alcGetIntegerv },
104 { "alcCaptureOpenDevice", (ALvoid *) alcCaptureOpenDevice },
105 { "alcCaptureCloseDevice", (ALvoid *) alcCaptureCloseDevice },
106 { "alcCaptureStart", (ALvoid *) alcCaptureStart },
107 { "alcCaptureStop", (ALvoid *) alcCaptureStop },
108 { "alcCaptureSamples", (ALvoid *) alcCaptureSamples },
109 { NULL, (ALvoid *) NULL }
112 static ALenums enumeration[]={
113 // Types
114 { (ALchar *)"ALC_INVALID", ALC_INVALID },
115 { (ALchar *)"ALC_FALSE", ALC_FALSE },
116 { (ALchar *)"ALC_TRUE", ALC_TRUE },
118 // ALC Properties
119 { (ALchar *)"ALC_MAJOR_VERSION", ALC_MAJOR_VERSION },
120 { (ALchar *)"ALC_MINOR_VERSION", ALC_MINOR_VERSION },
121 { (ALchar *)"ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE },
122 { (ALchar *)"ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES },
123 { (ALchar *)"ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER },
124 { (ALchar *)"ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER },
125 { (ALchar *)"ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER },
126 { (ALchar *)"ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER },
127 { (ALchar *)"ALC_EXTENSIONS", ALC_EXTENSIONS },
128 { (ALchar *)"ALC_FREQUENCY", ALC_FREQUENCY },
129 { (ALchar *)"ALC_REFRESH", ALC_REFRESH },
130 { (ALchar *)"ALC_SYNC", ALC_SYNC },
131 { (ALchar *)"ALC_MONO_SOURCES", ALC_MONO_SOURCES },
132 { (ALchar *)"ALC_STEREO_SOURCES", ALC_STEREO_SOURCES },
133 { (ALchar *)"ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER },
134 { (ALchar *)"ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER},
135 { (ALchar *)"ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES },
137 // EFX Properties
138 { (ALchar *)"ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION },
139 { (ALchar *)"ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION },
140 { (ALchar *)"ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS },
142 // ALC Error Message
143 { (ALchar *)"ALC_NO_ERROR", ALC_NO_ERROR },
144 { (ALchar *)"ALC_INVALID_DEVICE", ALC_INVALID_DEVICE },
145 { (ALchar *)"ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT },
146 { (ALchar *)"ALC_INVALID_ENUM", ALC_INVALID_ENUM },
147 { (ALchar *)"ALC_INVALID_VALUE", ALC_INVALID_VALUE },
148 { (ALchar *)"ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY },
149 { (ALchar *)NULL, (ALenum)0 }
151 // Error strings
152 static const ALCchar alcNoError[] = "No Error";
153 static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
154 static const ALCchar alcErrInvalidContext[] = "Invalid Context";
155 static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
156 static const ALCchar alcErrInvalidValue[] = "Invalid Value";
157 static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
159 // Context strings
160 static ALCchar alcDeviceList[2048];
161 static ALCchar alcAllDeviceList[2048];
162 static ALCchar alcCaptureDeviceList[2048];
163 // Default is always the first in the list
164 static ALCchar *alcDefaultDeviceSpecifier = alcDeviceList;
165 static ALCchar *alcDefaultAllDeviceSpecifier = alcAllDeviceList;
166 static ALCchar *alcCaptureDefaultDeviceSpecifier = alcCaptureDeviceList;
169 static ALCchar alcExtensionList[] = "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_EFX";
170 static ALCint alcMajorVersion = 1;
171 static ALCint alcMinorVersion = 1;
173 static ALCint alcEFXMajorVersion = 1;
174 static ALCint alcEFXMinorVersion = 0;
176 ///////////////////////////////////////////////////////
179 ///////////////////////////////////////////////////////
180 // Global Variables
182 static ALCdevice *g_pDeviceList = NULL;
183 static ALCuint g_ulDeviceCount = 0;
185 // Context List
186 static ALCcontext *g_pContextList = NULL;
187 static ALCuint g_ulContextCount = 0;
189 // Context Error
190 static ALCenum g_eLastContextError = ALC_NO_ERROR;
192 ///////////////////////////////////////////////////////
195 ///////////////////////////////////////////////////////
196 // ALC Related helper functions
198 static void InitAL(void)
200 static int done = 0;
201 if(!done)
203 int i;
204 const char *devs, *str;
206 done = 1;
208 InitializeCriticalSection(&_alMutex);
209 ALTHUNK_INIT();
210 ReadALConfig();
212 devs = GetConfigValue(NULL, "drivers", "");
213 if(devs[0])
215 int n;
216 size_t len;
217 const char *next = devs;
219 i = 0;
221 do {
222 devs = next;
223 next = strchr(devs, ',');
225 if(!devs[0] || devs[0] == ',')
226 continue;
228 len = (next ? ((size_t)(next-devs)) : strlen(devs));
229 for(n = i;BackendList[n].Init;n++)
231 if(len == strlen(BackendList[n].name) &&
232 strncmp(BackendList[n].name, devs, len) == 0)
234 const char *name = BackendList[i].name;
235 void (*Init)(BackendFuncs*) = BackendList[i].Init;
237 BackendList[i].name = BackendList[n].name;
238 BackendList[i].Init = BackendList[n].Init;
240 BackendList[n].name = name;
241 BackendList[n].Init = Init;
243 i++;
246 } while(next++);
248 BackendList[i].name = NULL;
249 BackendList[i].Init = NULL;
252 for(i = 0;BackendList[i].Init;i++)
253 BackendList[i].Init(&BackendList[i].Funcs);
255 str = GetConfigValue(NULL, "stereodup", "false");
256 DuplicateStereo = (strcasecmp(str, "true") == 0 ||
257 strcasecmp(str, "yes") == 0 ||
258 strcasecmp(str, "on") == 0 ||
259 atoi(str) != 0);
263 ALCchar *AppendDeviceList(char *name)
265 static int pos;
266 ALCchar *ret = alcDeviceList+pos;
267 pos += snprintf(alcDeviceList+pos, sizeof(alcDeviceList)-pos, "%s", name) + 1;
268 return ret;
271 ALCchar *AppendAllDeviceList(char *name)
273 static int pos;
274 ALCchar *ret = alcAllDeviceList+pos;
275 pos += snprintf(alcAllDeviceList+pos, sizeof(alcAllDeviceList)-pos, "%s", name) + 1;
276 return ret;
279 ALCchar *AppendCaptureDeviceList(char *name)
281 static int pos;
282 ALCchar *ret = alcCaptureDeviceList+pos;
283 pos += snprintf(alcCaptureDeviceList+pos, sizeof(alcCaptureDeviceList)-pos, "%s", name) + 1;
284 return ret;
288 IsContext
290 Check pContext is a valid Context pointer
292 static ALCboolean IsContext(ALCcontext *pContext)
294 ALCcontext *pTempContext;
296 pTempContext = g_pContextList;
297 while (pTempContext && pTempContext != pContext)
298 pTempContext = pTempContext->next;
300 return (pTempContext ? ALC_TRUE : ALC_FALSE);
305 SetALCError
307 Store latest ALC Error
309 ALCvoid SetALCError(ALenum errorCode)
311 g_eLastContextError = errorCode;
316 SuspendContext
318 Thread-safe entry
320 ALCvoid SuspendContext(ALCcontext *pContext)
322 (void)pContext;
323 EnterCriticalSection(&_alMutex);
328 ProcessContext
330 Thread-safe exit
332 ALCvoid ProcessContext(ALCcontext *pContext)
334 (void)pContext;
335 LeaveCriticalSection(&_alMutex);
340 InitContext
342 Initialize Context variables
344 static ALvoid InitContext(ALCcontext *pContext)
346 int level;
348 //Initialise listener
349 pContext->Listener.Gain = 1.0f;
350 pContext->Listener.MetersPerUnit = 1.0f;
351 pContext->Listener.Position[0] = 0.0f;
352 pContext->Listener.Position[1] = 0.0f;
353 pContext->Listener.Position[2] = 0.0f;
354 pContext->Listener.Velocity[0] = 0.0f;
355 pContext->Listener.Velocity[1] = 0.0f;
356 pContext->Listener.Velocity[2] = 0.0f;
357 pContext->Listener.Forward[0] = 0.0f;
358 pContext->Listener.Forward[1] = 0.0f;
359 pContext->Listener.Forward[2] = -1.0f;
360 pContext->Listener.Up[0] = 0.0f;
361 pContext->Listener.Up[1] = 1.0f;
362 pContext->Listener.Up[2] = 0.0f;
364 //Validate pContext
365 pContext->LastError = AL_NO_ERROR;
366 pContext->InUse = AL_FALSE;
368 //Set output format
369 pContext->Frequency = pContext->Device->Frequency;
371 //Set globals
372 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
373 pContext->DopplerFactor = 1.0f;
374 pContext->DopplerVelocity = 1.0f;
375 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
377 pContext->lNumStereoSources = 1;
378 pContext->lNumMonoSources = pContext->Device->MaxNoOfSources - pContext->lNumStereoSources;
380 strcpy(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");
382 level = GetConfigValueInt(NULL, "cf_level", 0);
383 if(level > 0 && level <= 6)
385 pContext->bs2b = calloc(1, sizeof(*pContext->bs2b));
386 bs2b_set_srate(pContext->bs2b, pContext->Frequency);
387 bs2b_set_level(pContext->bs2b, level);
393 ExitContext
395 Clean up Context, destroy any remaining Sources
397 static ALCvoid ExitContext(ALCcontext *pContext)
399 //Invalidate context
400 pContext->LastError = AL_NO_ERROR;
401 pContext->InUse = AL_FALSE;
403 free(pContext->bs2b);
404 pContext->bs2b = NULL;
407 ///////////////////////////////////////////////////////
410 ///////////////////////////////////////////////////////
411 // ALC Functions calls
414 // This should probably move to another c file but for now ...
415 ALCAPI ALCdevice* ALCAPIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
417 ALCboolean DeviceFound = ALC_FALSE;
418 ALCdevice *pDevice = NULL;
419 ALCint i;
421 InitAL();
423 if(deviceName && !deviceName[0])
424 deviceName = NULL;
426 pDevice = malloc(sizeof(ALCdevice));
427 if (pDevice)
429 if (SampleSize > 0)
431 //Initialise device structure
432 memset(pDevice, 0, sizeof(ALCdevice));
434 //Validate device
435 pDevice->InUse = AL_TRUE;
436 pDevice->IsCaptureDevice = AL_TRUE;
438 pDevice->Frequency = frequency;
439 pDevice->Format = format;
440 pDevice->FrameSize = aluBytesFromFormat(format) *
441 aluChannelsFromFormat(format);
443 for(i = 0;BackendList[i].Init;i++)
445 pDevice->Funcs = &BackendList[i].Funcs;
446 if(ALCdevice_OpenCapture(pDevice, deviceName, frequency, format, SampleSize))
448 SuspendContext(NULL);
449 pDevice->next = g_pDeviceList;
450 g_pDeviceList = pDevice;
451 g_ulDeviceCount++;
452 ProcessContext(NULL);
454 DeviceFound = ALC_TRUE;
455 break;
459 else
460 SetALCError(ALC_INVALID_VALUE);
462 if(!DeviceFound)
464 free(pDevice);
465 pDevice = NULL;
468 else
469 SetALCError(ALC_OUT_OF_MEMORY);
471 return pDevice;
474 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
476 ALCboolean bReturn = ALC_FALSE;
477 ALCdevice **list;
479 if ((pDevice)&&(pDevice->IsCaptureDevice))
481 SuspendContext(NULL);
483 list = &g_pDeviceList;
484 while(*list != pDevice)
485 list = &(*list)->next;
487 *list = (*list)->next;
488 g_ulDeviceCount--;
490 ProcessContext(NULL);
492 ALCdevice_CloseCapture(pDevice);
493 free(pDevice);
495 bReturn = ALC_TRUE;
497 else
498 SetALCError(ALC_INVALID_DEVICE);
500 return bReturn;
503 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
505 if ((pDevice)&&(pDevice->IsCaptureDevice))
506 ALCdevice_StartCapture(pDevice);
507 else
508 SetALCError(ALC_INVALID_DEVICE);
511 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
513 if ((pDevice)&&(pDevice->IsCaptureDevice))
514 ALCdevice_StopCapture(pDevice);
515 else
516 SetALCError(ALC_INVALID_DEVICE);
519 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
521 if ((pDevice) && (pDevice->IsCaptureDevice))
522 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
523 else
524 SetALCError(ALC_INVALID_DEVICE);
528 alcGetError
530 Return last ALC generated error code
532 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
534 ALCenum errorCode;
536 (void)device;
538 errorCode = g_eLastContextError;
539 g_eLastContextError = ALC_NO_ERROR;
540 return errorCode;
545 alcSuspendContext
547 Not functional
549 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
551 // Not a lot happens here !
552 (void)pContext;
557 alcProcessContext
559 Not functional
561 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
563 // Not a lot happens here !
564 (void)pContext;
569 alcGetString
571 Returns information about the Device, and error strings
573 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
575 const ALCchar *value = NULL;
577 InitAL();
579 switch (param)
581 case ALC_NO_ERROR:
582 value = alcNoError;
583 break;
585 case ALC_INVALID_ENUM:
586 value = alcErrInvalidEnum;
587 break;
589 case ALC_INVALID_VALUE:
590 value = alcErrInvalidValue;
591 break;
593 case ALC_INVALID_DEVICE:
594 value = alcErrInvalidDevice;
595 break;
597 case ALC_INVALID_CONTEXT:
598 value = alcErrInvalidContext;
599 break;
601 case ALC_OUT_OF_MEMORY:
602 value = alcErrOutOfMemory;
603 break;
605 case ALC_DEFAULT_DEVICE_SPECIFIER:
606 value = alcDefaultDeviceSpecifier;
607 break;
609 case ALC_DEVICE_SPECIFIER:
610 if (pDevice)
611 value = pDevice->szDeviceName;
612 else
613 value = alcDeviceList;
614 break;
616 case ALC_ALL_DEVICES_SPECIFIER:
617 value = alcAllDeviceList;
618 break;
620 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
621 value = alcDefaultAllDeviceSpecifier;
622 break;
624 case ALC_CAPTURE_DEVICE_SPECIFIER:
625 if (pDevice)
626 value = pDevice->szDeviceName;
627 else
628 value = alcCaptureDeviceList;
629 break;
631 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
632 value = alcCaptureDefaultDeviceSpecifier;
633 break;
635 case ALC_EXTENSIONS:
636 value = alcExtensionList;
637 break;
639 default:
640 SetALCError(ALC_INVALID_ENUM);
641 break;
644 return value;
649 alcGetIntegerv
651 Returns information about the Device and the version of Open AL
653 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
655 InitAL();
657 if ((device)&&(device->IsCaptureDevice))
659 SuspendContext(NULL);
661 // Capture device
662 switch (param)
664 case ALC_CAPTURE_SAMPLES:
665 if ((size) && (data))
666 *data = ALCdevice_AvailableSamples(device);
667 else
668 SetALCError(ALC_INVALID_VALUE);
669 break;
671 default:
672 SetALCError(ALC_INVALID_ENUM);
673 break;
676 ProcessContext(NULL);
678 else
680 if(data)
682 // Playback Device
683 switch (param)
685 case ALC_MAJOR_VERSION:
686 if(!size)
687 SetALCError(ALC_INVALID_VALUE);
688 else
689 *data = alcMajorVersion;
690 break;
692 case ALC_MINOR_VERSION:
693 if(!size)
694 SetALCError(ALC_INVALID_VALUE);
695 else
696 *data = alcMinorVersion;
697 break;
699 case ALC_EFX_MAJOR_VERSION:
700 if(!size)
701 SetALCError(ALC_INVALID_VALUE);
702 else
703 *data = alcEFXMajorVersion;
704 break;
706 case ALC_EFX_MINOR_VERSION:
707 if(!size)
708 SetALCError(ALC_INVALID_VALUE);
709 else
710 *data = alcEFXMinorVersion;
711 break;
713 case ALC_MAX_AUXILIARY_SENDS:
714 if(!size)
715 SetALCError(ALC_INVALID_VALUE);
716 else
717 *data = MAX_SENDS;
718 break;
720 case ALC_ATTRIBUTES_SIZE:
721 if(!device)
722 SetALCError(ALC_INVALID_DEVICE);
723 else if(!size)
724 SetALCError(ALC_INVALID_VALUE);
725 else
726 *data = 12;
727 break;
729 case ALC_ALL_ATTRIBUTES:
730 if(!device)
731 SetALCError(ALC_INVALID_DEVICE);
732 else if (size < 7)
733 SetALCError(ALC_INVALID_VALUE);
734 else
736 int i = 0;
738 data[i++] = ALC_FREQUENCY;
739 data[i++] = device->Frequency;
741 data[i++] = ALC_REFRESH;
742 data[i++] = device->Frequency / device->UpdateFreq;
744 data[i++] = ALC_SYNC;
745 data[i++] = ALC_FALSE;
747 SuspendContext(NULL);
748 if(device->Context && size >= 12)
750 data[i++] = ALC_MONO_SOURCES;
751 data[i++] = device->Context->lNumMonoSources;
753 data[i++] = ALC_STEREO_SOURCES;
754 data[i++] = device->Context->lNumStereoSources;
756 data[i++] = ALC_MAX_AUXILIARY_SENDS;
757 data[i++] = MAX_SENDS;
759 ProcessContext(NULL);
761 data[i++] = 0;
763 break;
765 case ALC_FREQUENCY:
766 if(!device)
767 SetALCError(ALC_INVALID_DEVICE);
768 else if(!size)
769 SetALCError(ALC_INVALID_VALUE);
770 else
771 *data = device->Frequency;
772 break;
774 case ALC_REFRESH:
775 if(!device)
776 SetALCError(ALC_INVALID_DEVICE);
777 else if(!size)
778 SetALCError(ALC_INVALID_VALUE);
779 else
780 *data = device->Frequency / device->UpdateFreq;
781 break;
783 case ALC_SYNC:
784 if(!device)
785 SetALCError(ALC_INVALID_DEVICE);
786 else if(!size)
787 SetALCError(ALC_INVALID_VALUE);
788 else
789 *data = ALC_FALSE;
790 break;
792 case ALC_MONO_SOURCES:
793 if(!device || !device->Context)
794 SetALCError(ALC_INVALID_DEVICE);
795 else if (size != 1)
796 SetALCError(ALC_INVALID_VALUE);
797 else
798 *data = device->Context->lNumMonoSources;
799 break;
801 case ALC_STEREO_SOURCES:
802 if(!device || !device->Context)
803 SetALCError(ALC_INVALID_DEVICE);
804 else if (size != 1)
805 SetALCError(ALC_INVALID_VALUE);
806 else
807 *data = device->Context->lNumStereoSources;
808 break;
810 default:
811 SetALCError(ALC_INVALID_ENUM);
812 break;
815 else if(size)
816 SetALCError(ALC_INVALID_VALUE);
819 return;
824 alcIsExtensionPresent
826 Determines if there is support for a particular extension
828 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
830 ALCboolean bResult = ALC_FALSE;
832 (void)device;
834 if (extName)
836 const char *ptr;
837 size_t len;
839 len = strlen(extName);
840 ptr = alcExtensionList;
841 while(ptr && *ptr)
843 if(strncasecmp(ptr, extName, len) == 0 &&
844 (ptr[len] == '\0' || isspace(ptr[len])))
846 bResult = ALC_TRUE;
847 break;
849 if((ptr=strchr(ptr, ' ')) != NULL)
851 do {
852 ++ptr;
853 } while(isspace(*ptr));
857 else
858 SetALCError(ALC_INVALID_VALUE);
860 return bResult;
865 alcGetProcAddress
867 Retrieves the function address for a particular extension function
869 ALCAPI ALCvoid * ALCAPIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
871 ALCvoid *pFunction = NULL;
872 ALsizei i = 0;
874 (void)device;
876 if (funcName)
878 while(alcFunctions[i].funcName &&
879 strcmp(alcFunctions[i].funcName,funcName) != 0)
880 i++;
881 pFunction = alcFunctions[i].address;
883 else
884 SetALCError(ALC_INVALID_VALUE);
886 return pFunction;
891 alcGetEnumValue
893 Get the value for a particular ALC Enumerated Value
895 ALCAPI ALCenum ALCAPIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
897 ALsizei i = 0;
898 ALCenum val;
900 (void)device;
902 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
903 i++;
904 val = enumeration[i].value;
906 if(!enumeration[i].enumName)
907 SetALCError(ALC_INVALID_VALUE);
909 return val;
914 alcCreateContext
916 Create and attach a Context to a particular Device.
918 ALCAPI ALCcontext* ALCAPIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
920 ALCcontext *ALContext = NULL;
921 ALuint ulAttributeIndex, ulRequestedStereoSources;
923 if ((device)&&(!device->IsCaptureDevice))
925 // Reset Context Last Error code
926 g_eLastContextError = ALC_NO_ERROR;
928 // Current implementation only allows one Context per Device
929 if(!device->Context)
931 ALContext = calloc(1, sizeof(ALCcontext));
932 if(!ALContext)
934 SetALCError(ALC_OUT_OF_MEMORY);
935 return NULL;
938 ALContext->Device = device;
939 InitContext(ALContext);
941 device->Context = ALContext;
943 SuspendContext(NULL);
945 ALContext->next = g_pContextList;
946 g_pContextList = ALContext;
947 g_ulContextCount++;
949 ProcessContext(NULL);
951 // Check for Voice Count attributes
952 if (attrList)
954 ulAttributeIndex = 0;
955 while ((ulAttributeIndex < 10) && (attrList[ulAttributeIndex]))
957 if (attrList[ulAttributeIndex] == ALC_STEREO_SOURCES)
959 ulRequestedStereoSources = attrList[ulAttributeIndex + 1];
961 if (ulRequestedStereoSources > ALContext->Device->MaxNoOfSources)
962 ulRequestedStereoSources = ALContext->Device->MaxNoOfSources;
964 ALContext->lNumStereoSources = ulRequestedStereoSources;
965 ALContext->lNumMonoSources = ALContext->Device->MaxNoOfSources - ALContext->lNumStereoSources;
966 break;
969 ulAttributeIndex += 2;
973 else
975 SetALCError(ALC_INVALID_VALUE);
976 ALContext = NULL;
979 else
980 SetALCError(ALC_INVALID_DEVICE);
982 return ALContext;
987 alcDestroyContext
989 Remove a Context
991 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
993 ALCcontext **list;
995 // Lock context list
996 SuspendContext(NULL);
998 if (IsContext(context))
1000 // Lock context
1001 SuspendContext(context);
1003 ReleaseALSources(context);
1004 ReleaseALAuxiliaryEffectSlots(context);
1006 context->Device->Context = NULL;
1008 list = &g_pContextList;
1009 while(*list != context)
1010 list = &(*list)->next;
1012 *list = (*list)->next;
1013 g_ulContextCount--;
1015 // Unlock context
1016 ProcessContext(context);
1018 ExitContext(context);
1020 // Free memory (MUST do this after ProcessContext)
1021 memset(context, 0, sizeof(ALCcontext));
1022 free(context);
1024 else
1025 SetALCError(ALC_INVALID_CONTEXT);
1027 ProcessContext(NULL);
1032 alcGetCurrentContext
1034 Returns the currently active Context
1036 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1038 ALCcontext *pContext = NULL;
1040 SuspendContext(NULL);
1042 pContext = g_pContextList;
1043 while ((pContext) && (!pContext->InUse))
1044 pContext = pContext->next;
1046 ProcessContext(NULL);
1048 return pContext;
1053 alcGetContextsDevice
1055 Returns the Device that a particular Context is attached to
1057 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1059 ALCdevice *pDevice = NULL;
1061 SuspendContext(NULL);
1062 if (IsContext(pContext))
1063 pDevice = pContext->Device;
1064 else
1065 SetALCError(ALC_INVALID_CONTEXT);
1066 ProcessContext(NULL);
1068 return pDevice;
1073 alcMakeContextCurrent
1075 Makes the given Context the active Context
1077 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1079 ALCcontext *ALContext;
1080 ALboolean bReturn = AL_TRUE;
1082 SuspendContext(NULL);
1084 // context must be a valid Context or NULL
1085 if ((IsContext(context)) || (context == NULL))
1087 if ((ALContext=alcGetCurrentContext()))
1089 SuspendContext(ALContext);
1090 ALContext->InUse=AL_FALSE;
1091 ProcessContext(ALContext);
1094 if ((ALContext=context) && (ALContext->Device))
1096 SuspendContext(ALContext);
1097 ALContext->InUse=AL_TRUE;
1098 ProcessContext(ALContext);
1101 else
1103 SetALCError(ALC_INVALID_CONTEXT);
1104 bReturn = AL_FALSE;
1107 ProcessContext(NULL);
1109 return bReturn;
1114 alcOpenDevice
1116 Open the Device specified.
1118 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1120 ALboolean bDeviceFound = AL_FALSE;
1121 ALCdevice *device;
1122 ALint i;
1124 InitAL();
1126 if(deviceName && !deviceName[0])
1127 deviceName = NULL;
1129 device = malloc(sizeof(ALCdevice));
1130 if (device)
1132 const char *fmt;
1134 //Initialise device structure
1135 memset(device, 0, sizeof(ALCdevice));
1137 //Validate device
1138 device->InUse = AL_TRUE;
1139 device->IsCaptureDevice = AL_FALSE;
1141 //Set output format
1142 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1143 if((ALint)device->Frequency <= 0)
1144 device->Frequency = SWMIXER_OUTPUT_RATE;
1146 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1147 if(fmt[0])
1148 device->Format = alGetEnumValue(fmt);
1150 if(!aluChannelsFromFormat(device->Format))
1152 device->Format = AL_FORMAT_STEREO16;
1153 device->FrameSize = 4;
1155 else
1156 device->FrameSize = aluBytesFromFormat(device->Format) *
1157 aluChannelsFromFormat(device->Format);
1159 device->UpdateFreq = GetConfigValueInt(NULL, "refresh", 8192);
1160 if((ALint)device->UpdateFreq <= 0)
1161 device->UpdateFreq = 8192;
1163 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1164 if((ALint)device->MaxNoOfSources <= 0)
1165 device->MaxNoOfSources = 256;
1167 // Find a playback device to open
1168 for(i = 0;BackendList[i].Init;i++)
1170 device->Funcs = &BackendList[i].Funcs;
1171 if(ALCdevice_OpenPlayback(device, deviceName))
1173 SuspendContext(NULL);
1174 device->next = g_pDeviceList;
1175 g_pDeviceList = device;
1176 g_ulDeviceCount++;
1177 ProcessContext(NULL);
1179 bDeviceFound = AL_TRUE;
1180 break;
1184 if (!bDeviceFound)
1186 // No suitable output device found
1187 free(device);
1188 device = NULL;
1192 return device;
1197 alcCloseDevice
1199 Close the specified Device
1201 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1203 ALCboolean bReturn = ALC_FALSE;
1204 ALCdevice **list;
1206 if ((pDevice)&&(!pDevice->IsCaptureDevice))
1208 SuspendContext(NULL);
1210 list = &g_pDeviceList;
1211 while(*list != pDevice)
1212 list = &(*list)->next;
1214 *list = (*list)->next;
1215 g_ulDeviceCount--;
1217 ProcessContext(NULL);
1219 if(pDevice->Context)
1220 alcDestroyContext(pDevice->Context);
1221 ALCdevice_ClosePlayback(pDevice);
1223 //Release device structure
1224 memset(pDevice, 0, sizeof(ALCdevice));
1225 free(pDevice);
1227 bReturn = ALC_TRUE;
1229 else
1230 SetALCError(ALC_INVALID_DEVICE);
1232 return bReturn;
1236 ALCvoid ReleaseALC(ALCvoid)
1238 #ifdef _DEBUG
1239 if(g_ulContextCount > 0)
1240 AL_PRINT("exit() %u device(s) and %u context(s) NOT deleted\n", g_ulDeviceCount, g_ulContextCount);
1241 #endif
1243 while(g_pDeviceList)
1245 if(g_pDeviceList->IsCaptureDevice)
1246 alcCaptureCloseDevice(g_pDeviceList);
1247 else
1248 alcCloseDevice(g_pDeviceList);
1252 ///////////////////////////////////////////////////////