Don't include alAuxEffectSlot.h in alSource.h
[openal-soft.git] / Alc / ALc.c
blobdfd6b4b95590229772371ad73a0ec13f332b32bd
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 "alMain.h"
30 #include "alSource.h"
31 #include "AL/al.h"
32 #include "AL/alc.h"
33 #include "alThunk.h"
34 #include "alSource.h"
35 #include "alExtension.h"
36 #include "alAuxEffectSlot.h"
37 #include "bs2b.h"
39 ///////////////////////////////////////////////////////
40 // DEBUG INFORMATION
42 char _alDebug[256];
44 ///////////////////////////////////////////////////////
47 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
48 struct {
49 const char *name;
50 void (*Init)(BackendFuncs*);
51 BackendFuncs Funcs;
52 } BackendList[] = {
53 #ifdef HAVE_ALSA
54 { "alsa", alc_alsa_init, EmptyFuncs },
55 #endif
56 #ifdef HAVE_OSS
57 { "oss", alc_oss_init, EmptyFuncs },
58 #endif
59 #ifdef HAVE_DSOUND
60 { "dsound", alcDSoundInit, EmptyFuncs },
61 #endif
62 #ifdef HAVE_WINMM
63 { "winmm", alcWinMMInit, EmptyFuncs },
64 #endif
66 { "wave", alc_wave_init, EmptyFuncs },
68 { NULL, NULL, EmptyFuncs }
70 #undef EmptyFuncs
72 ///////////////////////////////////////////////////////
74 #define ALC_EFX_MAJOR_VERSION 0x20001
75 #define ALC_EFX_MINOR_VERSION 0x20002
76 #define ALC_MAX_AUXILIARY_SENDS 0x20003
78 ///////////////////////////////////////////////////////
79 // STRING and EXTENSIONS
81 typedef struct ALCextension_struct
83 ALCchar *extName;
84 ALvoid *address;
85 } ALCextension;
87 typedef struct ALCfunction_struct
89 ALCchar *funcName;
90 ALvoid *address;
91 } ALCfunction;
93 static ALCextension alcExtensions[] = {
94 { "ALC_ENUMERATE_ALL_EXT", (ALvoid *) NULL },
95 { "ALC_ENUMERATION_EXT", (ALvoid *) NULL },
96 { "ALC_EXT_CAPTURE", (ALvoid *) NULL },
97 { "ALC_EXT_EFX", (ALvoid *) NULL },
98 { NULL, (ALvoid *) NULL }
101 static ALCfunction alcFunctions[] = {
102 { "alcCreateContext", (ALvoid *) alcCreateContext },
103 { "alcMakeContextCurrent", (ALvoid *) alcMakeContextCurrent },
104 { "alcProcessContext", (ALvoid *) alcProcessContext },
105 { "alcSuspendContext", (ALvoid *) alcSuspendContext },
106 { "alcDestroyContext", (ALvoid *) alcDestroyContext },
107 { "alcGetCurrentContext", (ALvoid *) alcGetCurrentContext },
108 { "alcGetContextsDevice", (ALvoid *) alcGetContextsDevice },
109 { "alcOpenDevice", (ALvoid *) alcOpenDevice },
110 { "alcCloseDevice", (ALvoid *) alcCloseDevice },
111 { "alcGetError", (ALvoid *) alcGetError },
112 { "alcIsExtensionPresent", (ALvoid *) alcIsExtensionPresent },
113 { "alcGetProcAddress", (ALvoid *) alcGetProcAddress },
114 { "alcGetEnumValue", (ALvoid *) alcGetEnumValue },
115 { "alcGetString", (ALvoid *) alcGetString },
116 { "alcGetIntegerv", (ALvoid *) alcGetIntegerv },
117 { "alcCaptureOpenDevice", (ALvoid *) alcCaptureOpenDevice },
118 { "alcCaptureCloseDevice", (ALvoid *) alcCaptureCloseDevice },
119 { "alcCaptureStart", (ALvoid *) alcCaptureStart },
120 { "alcCaptureStop", (ALvoid *) alcCaptureStop },
121 { "alcCaptureSamples", (ALvoid *) alcCaptureSamples },
122 { NULL, (ALvoid *) NULL }
125 static ALenums enumeration[]={
126 // Types
127 { (ALchar *)"ALC_INVALID", ALC_INVALID },
128 { (ALchar *)"ALC_FALSE", ALC_FALSE },
129 { (ALchar *)"ALC_TRUE", ALC_TRUE },
131 // ALC Properties
132 { (ALchar *)"ALC_MAJOR_VERSION", ALC_MAJOR_VERSION },
133 { (ALchar *)"ALC_MINOR_VERSION", ALC_MINOR_VERSION },
134 { (ALchar *)"ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE },
135 { (ALchar *)"ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES },
136 { (ALchar *)"ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER },
137 { (ALchar *)"ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER },
138 { (ALchar *)"ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER },
139 { (ALchar *)"ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER },
140 { (ALchar *)"ALC_EXTENSIONS", ALC_EXTENSIONS },
141 { (ALchar *)"ALC_FREQUENCY", ALC_FREQUENCY },
142 { (ALchar *)"ALC_REFRESH", ALC_REFRESH },
143 { (ALchar *)"ALC_SYNC", ALC_SYNC },
144 { (ALchar *)"ALC_MONO_SOURCES", ALC_MONO_SOURCES },
145 { (ALchar *)"ALC_STEREO_SOURCES", ALC_STEREO_SOURCES },
146 { (ALchar *)"ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER },
147 { (ALchar *)"ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER},
148 { (ALchar *)"ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES },
150 // EFX Properties
151 { (ALchar *)"ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION },
152 { (ALchar *)"ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION },
153 { (ALchar *)"ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS },
155 // ALC Error Message
156 { (ALchar *)"ALC_NO_ERROR", ALC_NO_ERROR },
157 { (ALchar *)"ALC_INVALID_DEVICE", ALC_INVALID_DEVICE },
158 { (ALchar *)"ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT },
159 { (ALchar *)"ALC_INVALID_ENUM", ALC_INVALID_ENUM },
160 { (ALchar *)"ALC_INVALID_VALUE", ALC_INVALID_VALUE },
161 { (ALchar *)"ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY },
162 { (ALchar *)NULL, (ALenum)0 }
164 // Error strings
165 static const ALCchar alcNoError[] = "No Error";
166 static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
167 static const ALCchar alcErrInvalidContext[] = "Invalid Context";
168 static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
169 static const ALCchar alcErrInvalidValue[] = "Invalid Value";
170 static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
172 // Context strings
173 static ALCchar alcDeviceList[2048];
174 static ALCchar alcAllDeviceList[2048];
175 static ALCchar alcCaptureDeviceList[2048];
176 // Default is always the first in the list
177 static ALCchar *alcDefaultDeviceSpecifier = alcDeviceList;
178 static ALCchar *alcDefaultAllDeviceSpecifier = alcAllDeviceList;
179 static ALCchar *alcCaptureDefaultDeviceSpecifier = alcCaptureDeviceList;
182 static ALCchar alcExtensionList[] = "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_EFX";
183 static ALCint alcMajorVersion = 1;
184 static ALCint alcMinorVersion = 1;
186 static ALCint alcEFXMajorVersion = 1;
187 static ALCint alcEFXMinorVersion = 0;
189 ///////////////////////////////////////////////////////
192 ///////////////////////////////////////////////////////
193 // Global Variables
195 static ALCdevice *g_pDeviceList = NULL;
196 static ALCuint g_ulDeviceCount = 0;
198 // Context List
199 static ALCcontext *g_pContextList = NULL;
200 static ALCuint g_ulContextCount = 0;
202 // Context Error
203 static ALCenum g_eLastContextError = ALC_NO_ERROR;
205 ///////////////////////////////////////////////////////
208 ///////////////////////////////////////////////////////
209 // ALC Related helper functions
211 static void InitAL(void)
213 static int done = 0;
214 if(!done)
216 int i;
217 const char *devs;
219 InitializeCriticalSection(&_alMutex);
220 ALTHUNK_INIT();
221 ReadALConfig();
223 devs = GetConfigValue(NULL, "drivers", "");
224 if(devs[0])
226 int n;
227 size_t len;
228 const char *next = devs;
230 i = 0;
232 do {
233 devs = next;
234 next = strchr(devs, ',');
236 if(!devs[0] || devs[0] == ',')
237 continue;
239 len = (next ? ((size_t)(next-devs)) : strlen(devs));
240 for(n = i;BackendList[n].Init;n++)
242 if(len == strlen(BackendList[n].name) &&
243 strncmp(BackendList[n].name, devs, len) == 0)
245 const char *name = BackendList[i].name;
246 void (*Init)(BackendFuncs*) = BackendList[i].Init;
248 BackendList[i].name = BackendList[n].name;
249 BackendList[i].Init = BackendList[n].Init;
251 BackendList[n].name = name;
252 BackendList[n].Init = Init;
254 i++;
257 } while(next++);
259 BackendList[i].name = NULL;
260 BackendList[i].Init = NULL;
263 for(i = 0;BackendList[i].Init;i++)
264 BackendList[i].Init(&BackendList[i].Funcs);
265 done = 1;
269 ALCchar *AppendDeviceList(char *name)
271 static int pos;
272 ALCchar *ret = alcDeviceList+pos;
273 pos += snprintf(alcDeviceList+pos, sizeof(alcDeviceList)-pos, "%s", name) + 1;
274 return ret;
277 ALCchar *AppendAllDeviceList(char *name)
279 static int pos;
280 ALCchar *ret = alcAllDeviceList+pos;
281 pos += snprintf(alcAllDeviceList+pos, sizeof(alcAllDeviceList)-pos, "%s", name) + 1;
282 return ret;
285 ALCchar *AppendCaptureDeviceList(char *name)
287 static int pos;
288 ALCchar *ret = alcCaptureDeviceList+pos;
289 pos += snprintf(alcCaptureDeviceList+pos, sizeof(alcCaptureDeviceList)-pos, "%s", name) + 1;
290 return ret;
294 IsContext
296 Check pContext is a valid Context pointer
298 static ALCboolean IsContext(ALCcontext *pContext)
300 ALCcontext *pTempContext;
302 pTempContext = g_pContextList;
303 while (pTempContext && pTempContext != pContext)
304 pTempContext = pTempContext->next;
306 return (pTempContext ? ALC_TRUE : ALC_FALSE);
311 SetALCError
313 Store latest ALC Error
315 ALCvoid SetALCError(ALenum errorCode)
317 g_eLastContextError = errorCode;
322 SuspendContext
324 Thread-safe entry
326 ALCvoid SuspendContext(ALCcontext *pContext)
328 (void)pContext;
329 EnterCriticalSection(&_alMutex);
334 ProcessContext
336 Thread-safe exit
338 ALCvoid ProcessContext(ALCcontext *pContext)
340 (void)pContext;
341 LeaveCriticalSection(&_alMutex);
346 InitContext
348 Initialize Context variables
350 static ALvoid InitContext(ALCcontext *pContext)
352 int level;
354 //Initialise listener
355 pContext->Listener.Gain = 1.0f;
356 pContext->Listener.MetersPerUnit = 1.0f;
357 pContext->Listener.Position[0] = 0.0f;
358 pContext->Listener.Position[1] = 0.0f;
359 pContext->Listener.Position[2] = 0.0f;
360 pContext->Listener.Velocity[0] = 0.0f;
361 pContext->Listener.Velocity[1] = 0.0f;
362 pContext->Listener.Velocity[2] = 0.0f;
363 pContext->Listener.Forward[0] = 0.0f;
364 pContext->Listener.Forward[1] = 0.0f;
365 pContext->Listener.Forward[2] = -1.0f;
366 pContext->Listener.Up[0] = 0.0f;
367 pContext->Listener.Up[1] = 1.0f;
368 pContext->Listener.Up[2] = 0.0f;
370 //Validate pContext
371 pContext->LastError = AL_NO_ERROR;
372 pContext->InUse = AL_FALSE;
374 //Set output format
375 pContext->Frequency = pContext->Device->Frequency;
377 //Set globals
378 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
379 pContext->DopplerFactor = 1.0f;
380 pContext->DopplerVelocity = 1.0f;
381 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
383 pContext->lNumStereoSources = 1;
384 pContext->lNumMonoSources = pContext->Device->MaxNoOfSources - pContext->lNumStereoSources;
386 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");
388 level = GetConfigValueInt(NULL, "cf_level", 0);
389 if(level > 0 && level <= 6)
391 pContext->bs2b = calloc(1, sizeof(*pContext->bs2b));
392 bs2b_set_srate(pContext->bs2b, pContext->Frequency);
393 bs2b_set_level(pContext->bs2b, level);
399 ExitContext
401 Clean up Context, destroy any remaining Sources
403 static ALCvoid ExitContext(ALCcontext *pContext)
405 unsigned int i;
406 ALsource *ALSource;
407 ALsource *ALTempSource;
409 #ifdef _DEBUG
410 if (pContext->SourceCount>0)
411 AL_PRINT("alcDestroyContext() %d Source(s) NOT deleted\n", pContext->SourceCount);
412 #endif
414 // Free all the Sources still remaining
415 ALSource = pContext->Source;
416 for (i = 0; i < pContext->SourceCount; i++)
418 ALTempSource = ALSource->next;
419 ALTHUNK_REMOVEENTRY(ALSource->source);
420 memset(ALSource, 0, sizeof(ALsource));
421 free(ALSource);
422 ALSource = ALTempSource;
425 //Invalidate context
426 pContext->LastError = AL_NO_ERROR;
427 pContext->InUse = AL_FALSE;
429 free(pContext->bs2b);
430 pContext->bs2b = NULL;
433 ///////////////////////////////////////////////////////
436 ///////////////////////////////////////////////////////
437 // ALC Functions calls
440 // This should probably move to another c file but for now ...
441 ALCAPI ALCdevice* ALCAPIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
443 ALCboolean DeviceFound = ALC_FALSE;
444 ALCdevice *pDevice = NULL;
445 ALCint i;
447 InitAL();
449 if(deviceName && !deviceName[0])
450 deviceName = NULL;
452 pDevice = malloc(sizeof(ALCdevice));
453 if (pDevice)
455 if (SampleSize > 0)
457 //Initialise device structure
458 memset(pDevice, 0, sizeof(ALCdevice));
460 //Validate device
461 pDevice->InUse = AL_TRUE;
462 pDevice->IsCaptureDevice = AL_TRUE;
464 pDevice->Frequency = frequency;
465 pDevice->Format = format;
466 pDevice->Channels = aluChannelsFromFormat(format);
467 pDevice->FrameSize = aluBytesFromFormat(format) *
468 pDevice->Channels;
470 for(i = 0;BackendList[i].Init;i++)
472 pDevice->Funcs = &BackendList[i].Funcs;
473 if(ALCdevice_OpenCapture(pDevice, deviceName, frequency, format, SampleSize))
475 SuspendContext(NULL);
476 pDevice->next = g_pDeviceList;
477 g_pDeviceList = pDevice;
478 g_ulDeviceCount++;
479 ProcessContext(NULL);
481 DeviceFound = ALC_TRUE;
482 break;
486 else
487 SetALCError(ALC_INVALID_VALUE);
489 if(!DeviceFound)
491 free(pDevice);
492 pDevice = NULL;
495 else
496 SetALCError(ALC_OUT_OF_MEMORY);
498 return pDevice;
501 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
503 ALCboolean bReturn = ALC_FALSE;
504 ALCdevice **list;
506 if ((pDevice)&&(pDevice->IsCaptureDevice))
508 SuspendContext(NULL);
510 list = &g_pDeviceList;
511 while(*list != pDevice)
512 list = &(*list)->next;
514 *list = (*list)->next;
515 g_ulDeviceCount--;
517 ProcessContext(NULL);
519 ALCdevice_CloseCapture(pDevice);
520 free(pDevice);
522 bReturn = ALC_TRUE;
524 else
525 SetALCError(ALC_INVALID_DEVICE);
527 return bReturn;
530 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
532 if ((pDevice)&&(pDevice->IsCaptureDevice))
533 ALCdevice_StartCapture(pDevice);
534 else
535 SetALCError(ALC_INVALID_DEVICE);
538 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
540 if ((pDevice)&&(pDevice->IsCaptureDevice))
541 ALCdevice_StopCapture(pDevice);
542 else
543 SetALCError(ALC_INVALID_DEVICE);
546 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
548 if ((pDevice) && (pDevice->IsCaptureDevice))
549 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
550 else
551 SetALCError(ALC_INVALID_DEVICE);
555 alcGetError
557 Return last ALC generated error code
559 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
561 ALCenum errorCode;
563 (void)device;
565 errorCode = g_eLastContextError;
566 g_eLastContextError = ALC_NO_ERROR;
567 return errorCode;
572 alcSuspendContext
574 Not functional
576 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
578 // Not a lot happens here !
579 (void)pContext;
584 alcProcessContext
586 Not functional
588 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
590 // Not a lot happens here !
591 (void)pContext;
596 alcGetString
598 Returns information about the Device, and error strings
600 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
602 const ALCchar *value = NULL;
604 InitAL();
606 switch (param)
608 case ALC_NO_ERROR:
609 value = alcNoError;
610 break;
612 case ALC_INVALID_ENUM:
613 value = alcErrInvalidEnum;
614 break;
616 case ALC_INVALID_VALUE:
617 value = alcErrInvalidValue;
618 break;
620 case ALC_INVALID_DEVICE:
621 value = alcErrInvalidDevice;
622 break;
624 case ALC_INVALID_CONTEXT:
625 value = alcErrInvalidContext;
626 break;
628 case ALC_OUT_OF_MEMORY:
629 value = alcErrOutOfMemory;
630 break;
632 case ALC_DEFAULT_DEVICE_SPECIFIER:
633 value = alcDefaultDeviceSpecifier;
634 break;
636 case ALC_DEVICE_SPECIFIER:
637 if (pDevice)
638 value = pDevice->szDeviceName;
639 else
640 value = alcDeviceList;
641 break;
643 case ALC_ALL_DEVICES_SPECIFIER:
644 value = alcAllDeviceList;
645 break;
647 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
648 value = alcDefaultAllDeviceSpecifier;
649 break;
651 case ALC_CAPTURE_DEVICE_SPECIFIER:
652 if (pDevice)
653 value = pDevice->szDeviceName;
654 else
655 value = alcCaptureDeviceList;
656 break;
658 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
659 value = alcCaptureDefaultDeviceSpecifier;
660 break;
662 case ALC_EXTENSIONS:
663 value = alcExtensionList;
664 break;
666 default:
667 SetALCError(ALC_INVALID_ENUM);
668 break;
671 return value;
676 alcGetIntegerv
678 Returns information about the Device and the version of Open AL
680 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
682 InitAL();
684 if ((device)&&(device->IsCaptureDevice))
686 SuspendContext(NULL);
688 // Capture device
689 switch (param)
691 case ALC_CAPTURE_SAMPLES:
692 if ((size) && (data))
693 *data = ALCdevice_AvailableSamples(device);
694 else
695 SetALCError(ALC_INVALID_VALUE);
696 break;
698 default:
699 SetALCError(ALC_INVALID_ENUM);
700 break;
703 ProcessContext(NULL);
705 else
707 if(data)
709 // Playback Device
710 switch (param)
712 case ALC_MAJOR_VERSION:
713 if(!size)
714 SetALCError(ALC_INVALID_VALUE);
715 else
716 *data = alcMajorVersion;
717 break;
719 case ALC_MINOR_VERSION:
720 if(!size)
721 SetALCError(ALC_INVALID_VALUE);
722 else
723 *data = alcMinorVersion;
724 break;
726 case ALC_EFX_MAJOR_VERSION:
727 if(!size)
728 SetALCError(ALC_INVALID_VALUE);
729 else
730 *data = alcEFXMajorVersion;
731 break;
733 case ALC_EFX_MINOR_VERSION:
734 if(!size)
735 SetALCError(ALC_INVALID_VALUE);
736 else
737 *data = alcEFXMinorVersion;
738 break;
740 case ALC_MAX_AUXILIARY_SENDS:
741 if(!size)
742 SetALCError(ALC_INVALID_VALUE);
743 else
744 *data = MAX_SENDS;
745 break;
747 case ALC_ATTRIBUTES_SIZE:
748 if(!device)
749 SetALCError(ALC_INVALID_DEVICE);
750 else if(!size)
751 SetALCError(ALC_INVALID_VALUE);
752 else
753 *data = 12;
754 break;
756 case ALC_ALL_ATTRIBUTES:
757 if(!device)
758 SetALCError(ALC_INVALID_DEVICE);
759 else if (size < 7)
760 SetALCError(ALC_INVALID_VALUE);
761 else
763 int i = 0;
765 data[i++] = ALC_FREQUENCY;
766 data[i++] = device->Frequency;
768 data[i++] = ALC_REFRESH;
769 data[i++] = device->Frequency / device->UpdateFreq;
771 data[i++] = ALC_SYNC;
772 data[i++] = ALC_FALSE;
774 SuspendContext(NULL);
775 if(device->Context && size >= 12)
777 data[i++] = ALC_MONO_SOURCES;
778 data[i++] = device->Context->lNumMonoSources;
780 data[i++] = ALC_STEREO_SOURCES;
781 data[i++] = device->Context->lNumStereoSources;
783 data[i++] = ALC_MAX_AUXILIARY_SENDS;
784 data[i++] = MAX_SENDS;
786 ProcessContext(NULL);
788 data[i++] = 0;
790 break;
792 case ALC_FREQUENCY:
793 if(!device)
794 SetALCError(ALC_INVALID_DEVICE);
795 else if(!size)
796 SetALCError(ALC_INVALID_VALUE);
797 else
798 *data = device->Frequency;
799 break;
801 case ALC_REFRESH:
802 if(!device)
803 SetALCError(ALC_INVALID_DEVICE);
804 else if(!size)
805 SetALCError(ALC_INVALID_VALUE);
806 else
807 *data = device->Frequency / device->UpdateFreq;
808 break;
810 case ALC_SYNC:
811 if(!device)
812 SetALCError(ALC_INVALID_DEVICE);
813 else if(!size)
814 SetALCError(ALC_INVALID_VALUE);
815 else
816 *data = ALC_FALSE;
817 break;
819 case ALC_MONO_SOURCES:
820 if(!device || !device->Context)
821 SetALCError(ALC_INVALID_DEVICE);
822 else if (size != 1)
823 SetALCError(ALC_INVALID_VALUE);
824 else
825 *data = device->Context->lNumMonoSources;
826 break;
828 case ALC_STEREO_SOURCES:
829 if(!device || !device->Context)
830 SetALCError(ALC_INVALID_DEVICE);
831 else if (size != 1)
832 SetALCError(ALC_INVALID_VALUE);
833 else
834 *data = device->Context->lNumStereoSources;
835 break;
837 default:
838 SetALCError(ALC_INVALID_ENUM);
839 break;
842 else if(size)
843 SetALCError(ALC_INVALID_VALUE);
846 return;
851 alcIsExtensionPresent
853 Determines if there is support for a particular extension
855 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
857 ALCboolean bResult = ALC_FALSE;
858 ALsizei i = 0;
860 (void)device;
862 if (extName)
864 while(alcExtensions[i].extName &&
865 strcasecmp(alcExtensions[i].extName,extName) != 0)
866 i++;
868 if (alcExtensions[i].extName)
869 bResult = ALC_TRUE;
871 else
872 SetALCError(ALC_INVALID_VALUE);
874 return bResult;
879 alcGetProcAddress
881 Retrieves the function address for a particular extension function
883 ALCAPI ALCvoid * ALCAPIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
885 ALCvoid *pFunction = NULL;
886 ALsizei i = 0;
888 (void)device;
890 if (funcName)
892 while(alcFunctions[i].funcName &&
893 strcmp(alcFunctions[i].funcName,funcName) != 0)
894 i++;
895 pFunction = alcFunctions[i].address;
897 else
898 SetALCError(ALC_INVALID_VALUE);
900 return pFunction;
905 alcGetEnumValue
907 Get the value for a particular ALC Enumerated Value
909 ALCAPI ALCenum ALCAPIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
911 ALsizei i = 0;
912 ALCenum val;
914 (void)device;
916 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
917 i++;
918 val = enumeration[i].value;
920 if(!enumeration[i].enumName)
921 SetALCError(ALC_INVALID_VALUE);
923 return val;
928 alcCreateContext
930 Create and attach a Context to a particular Device.
932 ALCAPI ALCcontext* ALCAPIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
934 ALCcontext *ALContext = NULL;
935 ALuint ulAttributeIndex, ulRequestedStereoSources;
937 if ((device)&&(!device->IsCaptureDevice))
939 // Reset Context Last Error code
940 g_eLastContextError = ALC_NO_ERROR;
942 // Current implementation only allows one Context per Device
943 if(!device->Context)
945 ALContext = calloc(1, sizeof(ALCcontext));
946 if(!ALContext)
948 SetALCError(ALC_OUT_OF_MEMORY);
949 return NULL;
952 ALContext->Device = device;
953 InitContext(ALContext);
955 device->Context = ALContext;
957 SuspendContext(NULL);
959 ALContext->next = g_pContextList;
960 g_pContextList = ALContext;
961 g_ulContextCount++;
963 ProcessContext(NULL);
965 // Check for Voice Count attributes
966 if (attrList)
968 ulAttributeIndex = 0;
969 while ((ulAttributeIndex < 10) && (attrList[ulAttributeIndex]))
971 if (attrList[ulAttributeIndex] == ALC_STEREO_SOURCES)
973 ulRequestedStereoSources = attrList[ulAttributeIndex + 1];
975 if (ulRequestedStereoSources > ALContext->Device->MaxNoOfSources)
976 ulRequestedStereoSources = ALContext->Device->MaxNoOfSources;
978 ALContext->lNumStereoSources = ulRequestedStereoSources;
979 ALContext->lNumMonoSources = ALContext->Device->MaxNoOfSources - ALContext->lNumStereoSources;
980 break;
983 ulAttributeIndex += 2;
987 else
989 SetALCError(ALC_INVALID_VALUE);
990 ALContext = NULL;
993 else
994 SetALCError(ALC_INVALID_DEVICE);
996 return ALContext;
1001 alcDestroyContext
1003 Remove a Context
1005 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
1007 ALCcontext **list;
1009 // Lock context list
1010 SuspendContext(NULL);
1012 if (IsContext(context))
1014 // Lock context
1015 SuspendContext(context);
1017 ReleaseALSources(context);
1018 ReleaseALAuxiliaryEffectSlots(context);
1020 context->Device->Context = NULL;
1022 list = &g_pContextList;
1023 while(*list != context)
1024 list = &(*list)->next;
1026 *list = (*list)->next;
1027 g_ulContextCount--;
1029 // Unlock context
1030 ProcessContext(context);
1032 ExitContext(context);
1034 // Free memory (MUST do this after ProcessContext)
1035 memset(context, 0, sizeof(ALCcontext));
1036 free(context);
1038 else
1039 SetALCError(ALC_INVALID_CONTEXT);
1041 ProcessContext(NULL);
1046 alcGetCurrentContext
1048 Returns the currently active Context
1050 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1052 ALCcontext *pContext = NULL;
1054 SuspendContext(NULL);
1056 pContext = g_pContextList;
1057 while ((pContext) && (!pContext->InUse))
1058 pContext = pContext->next;
1060 ProcessContext(NULL);
1062 return pContext;
1067 alcGetContextsDevice
1069 Returns the Device that a particular Context is attached to
1071 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1073 ALCdevice *pDevice = NULL;
1075 SuspendContext(NULL);
1076 if (IsContext(pContext))
1077 pDevice = pContext->Device;
1078 else
1079 SetALCError(ALC_INVALID_CONTEXT);
1080 ProcessContext(NULL);
1082 return pDevice;
1087 alcMakeContextCurrent
1089 Makes the given Context the active Context
1091 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1093 ALCcontext *ALContext;
1094 ALboolean bReturn = AL_TRUE;
1096 SuspendContext(NULL);
1098 // context must be a valid Context or NULL
1099 if ((IsContext(context)) || (context == NULL))
1101 if ((ALContext=alcGetCurrentContext()))
1103 SuspendContext(ALContext);
1104 ALContext->InUse=AL_FALSE;
1105 ProcessContext(ALContext);
1108 if ((ALContext=context) && (ALContext->Device))
1110 SuspendContext(ALContext);
1111 ALContext->InUse=AL_TRUE;
1112 ProcessContext(ALContext);
1115 else
1117 SetALCError(ALC_INVALID_CONTEXT);
1118 bReturn = AL_FALSE;
1121 ProcessContext(NULL);
1123 return bReturn;
1128 alcOpenDevice
1130 Open the Device specified.
1132 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1134 ALboolean bDeviceFound = AL_FALSE;
1135 ALCdevice *device;
1136 ALint i;
1138 InitAL();
1140 if(deviceName && !deviceName[0])
1141 deviceName = NULL;
1143 device = malloc(sizeof(ALCdevice));
1144 if (device)
1146 const char *fmt;
1148 //Initialise device structure
1149 memset(device, 0, sizeof(ALCdevice));
1151 //Validate device
1152 device->InUse = AL_TRUE;
1153 device->IsCaptureDevice = AL_FALSE;
1155 //Set output format
1156 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1157 if((ALint)device->Frequency <= 0)
1158 device->Frequency = SWMIXER_OUTPUT_RATE;
1160 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1161 if(fmt[0])
1162 device->Format = alGetEnumValue(fmt);
1164 device->Channels = aluChannelsFromFormat(device->Format);
1165 if(!device->Channels)
1167 device->Format = AL_FORMAT_STEREO16;
1168 device->Channels = 2;
1169 device->FrameSize = 4;
1171 else
1172 device->FrameSize = aluBytesFromFormat(device->Format) *
1173 device->Channels;
1175 device->UpdateFreq = GetConfigValueInt(NULL, "refresh", 0);
1176 if((ALint)device->UpdateFreq <= 0)
1177 device->UpdateFreq = 8192 * device->Frequency / 22050;
1179 // Find a playback device to open
1180 for(i = 0;BackendList[i].Init;i++)
1182 device->Funcs = &BackendList[i].Funcs;
1183 if(ALCdevice_OpenPlayback(device, deviceName))
1185 SuspendContext(NULL);
1186 device->next = g_pDeviceList;
1187 g_pDeviceList = device;
1188 g_ulDeviceCount++;
1189 ProcessContext(NULL);
1191 bDeviceFound = AL_TRUE;
1192 break;
1196 if (!bDeviceFound)
1198 // No suitable output device found
1199 free(device);
1200 device = NULL;
1204 return device;
1209 alcCloseDevice
1211 Close the specified Device
1213 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1215 ALCboolean bReturn = ALC_FALSE;
1216 ALCdevice **list;
1218 if ((pDevice)&&(!pDevice->IsCaptureDevice))
1220 SuspendContext(NULL);
1222 list = &g_pDeviceList;
1223 while(*list != pDevice)
1224 list = &(*list)->next;
1226 *list = (*list)->next;
1227 g_ulDeviceCount--;
1229 ProcessContext(NULL);
1231 if(pDevice->Context)
1232 alcDestroyContext(pDevice->Context);
1233 ALCdevice_ClosePlayback(pDevice);
1235 //Release device structure
1236 memset(pDevice, 0, sizeof(ALCdevice));
1237 free(pDevice);
1239 bReturn = ALC_TRUE;
1241 else
1242 SetALCError(ALC_INVALID_DEVICE);
1244 return bReturn;
1248 ALCvoid ReleaseALC(ALCvoid)
1250 ALCdevice *Dev;
1252 #ifdef _DEBUG
1253 if(g_ulContextCount > 0)
1254 AL_PRINT("exit() %u device(s) and %u context(s) NOT deleted\n", g_ulDeviceCount, g_ulContextCount);
1255 #endif
1257 while(g_pDeviceList)
1259 Dev = g_pDeviceList;
1260 g_pDeviceList = g_pDeviceList->next;
1261 if(Dev->IsCaptureDevice)
1262 alcCaptureCloseDevice(Dev);
1263 else
1264 alcCloseDevice(Dev);
1268 ///////////////////////////////////////////////////////