Add an option for setting the max number of sources
[openal-soft.git] / Alc / ALc.c
blob83ff7ba4656afff29c104c7b299ee5794586291b
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 //Invalidate context
406 pContext->LastError = AL_NO_ERROR;
407 pContext->InUse = AL_FALSE;
409 free(pContext->bs2b);
410 pContext->bs2b = NULL;
413 ///////////////////////////////////////////////////////
416 ///////////////////////////////////////////////////////
417 // ALC Functions calls
420 // This should probably move to another c file but for now ...
421 ALCAPI ALCdevice* ALCAPIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
423 ALCboolean DeviceFound = ALC_FALSE;
424 ALCdevice *pDevice = NULL;
425 ALCint i;
427 InitAL();
429 if(deviceName && !deviceName[0])
430 deviceName = NULL;
432 pDevice = malloc(sizeof(ALCdevice));
433 if (pDevice)
435 if (SampleSize > 0)
437 //Initialise device structure
438 memset(pDevice, 0, sizeof(ALCdevice));
440 //Validate device
441 pDevice->InUse = AL_TRUE;
442 pDevice->IsCaptureDevice = AL_TRUE;
444 pDevice->Frequency = frequency;
445 pDevice->Format = format;
446 pDevice->Channels = aluChannelsFromFormat(format);
447 pDevice->FrameSize = aluBytesFromFormat(format) *
448 pDevice->Channels;
450 for(i = 0;BackendList[i].Init;i++)
452 pDevice->Funcs = &BackendList[i].Funcs;
453 if(ALCdevice_OpenCapture(pDevice, deviceName, frequency, format, SampleSize))
455 SuspendContext(NULL);
456 pDevice->next = g_pDeviceList;
457 g_pDeviceList = pDevice;
458 g_ulDeviceCount++;
459 ProcessContext(NULL);
461 DeviceFound = ALC_TRUE;
462 break;
466 else
467 SetALCError(ALC_INVALID_VALUE);
469 if(!DeviceFound)
471 free(pDevice);
472 pDevice = NULL;
475 else
476 SetALCError(ALC_OUT_OF_MEMORY);
478 return pDevice;
481 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
483 ALCboolean bReturn = ALC_FALSE;
484 ALCdevice **list;
486 if ((pDevice)&&(pDevice->IsCaptureDevice))
488 SuspendContext(NULL);
490 list = &g_pDeviceList;
491 while(*list != pDevice)
492 list = &(*list)->next;
494 *list = (*list)->next;
495 g_ulDeviceCount--;
497 ProcessContext(NULL);
499 ALCdevice_CloseCapture(pDevice);
500 free(pDevice);
502 bReturn = ALC_TRUE;
504 else
505 SetALCError(ALC_INVALID_DEVICE);
507 return bReturn;
510 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
512 if ((pDevice)&&(pDevice->IsCaptureDevice))
513 ALCdevice_StartCapture(pDevice);
514 else
515 SetALCError(ALC_INVALID_DEVICE);
518 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
520 if ((pDevice)&&(pDevice->IsCaptureDevice))
521 ALCdevice_StopCapture(pDevice);
522 else
523 SetALCError(ALC_INVALID_DEVICE);
526 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
528 if ((pDevice) && (pDevice->IsCaptureDevice))
529 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
530 else
531 SetALCError(ALC_INVALID_DEVICE);
535 alcGetError
537 Return last ALC generated error code
539 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
541 ALCenum errorCode;
543 (void)device;
545 errorCode = g_eLastContextError;
546 g_eLastContextError = ALC_NO_ERROR;
547 return errorCode;
552 alcSuspendContext
554 Not functional
556 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
558 // Not a lot happens here !
559 (void)pContext;
564 alcProcessContext
566 Not functional
568 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
570 // Not a lot happens here !
571 (void)pContext;
576 alcGetString
578 Returns information about the Device, and error strings
580 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
582 const ALCchar *value = NULL;
584 InitAL();
586 switch (param)
588 case ALC_NO_ERROR:
589 value = alcNoError;
590 break;
592 case ALC_INVALID_ENUM:
593 value = alcErrInvalidEnum;
594 break;
596 case ALC_INVALID_VALUE:
597 value = alcErrInvalidValue;
598 break;
600 case ALC_INVALID_DEVICE:
601 value = alcErrInvalidDevice;
602 break;
604 case ALC_INVALID_CONTEXT:
605 value = alcErrInvalidContext;
606 break;
608 case ALC_OUT_OF_MEMORY:
609 value = alcErrOutOfMemory;
610 break;
612 case ALC_DEFAULT_DEVICE_SPECIFIER:
613 value = alcDefaultDeviceSpecifier;
614 break;
616 case ALC_DEVICE_SPECIFIER:
617 if (pDevice)
618 value = pDevice->szDeviceName;
619 else
620 value = alcDeviceList;
621 break;
623 case ALC_ALL_DEVICES_SPECIFIER:
624 value = alcAllDeviceList;
625 break;
627 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
628 value = alcDefaultAllDeviceSpecifier;
629 break;
631 case ALC_CAPTURE_DEVICE_SPECIFIER:
632 if (pDevice)
633 value = pDevice->szDeviceName;
634 else
635 value = alcCaptureDeviceList;
636 break;
638 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
639 value = alcCaptureDefaultDeviceSpecifier;
640 break;
642 case ALC_EXTENSIONS:
643 value = alcExtensionList;
644 break;
646 default:
647 SetALCError(ALC_INVALID_ENUM);
648 break;
651 return value;
656 alcGetIntegerv
658 Returns information about the Device and the version of Open AL
660 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
662 InitAL();
664 if ((device)&&(device->IsCaptureDevice))
666 SuspendContext(NULL);
668 // Capture device
669 switch (param)
671 case ALC_CAPTURE_SAMPLES:
672 if ((size) && (data))
673 *data = ALCdevice_AvailableSamples(device);
674 else
675 SetALCError(ALC_INVALID_VALUE);
676 break;
678 default:
679 SetALCError(ALC_INVALID_ENUM);
680 break;
683 ProcessContext(NULL);
685 else
687 if(data)
689 // Playback Device
690 switch (param)
692 case ALC_MAJOR_VERSION:
693 if(!size)
694 SetALCError(ALC_INVALID_VALUE);
695 else
696 *data = alcMajorVersion;
697 break;
699 case ALC_MINOR_VERSION:
700 if(!size)
701 SetALCError(ALC_INVALID_VALUE);
702 else
703 *data = alcMinorVersion;
704 break;
706 case ALC_EFX_MAJOR_VERSION:
707 if(!size)
708 SetALCError(ALC_INVALID_VALUE);
709 else
710 *data = alcEFXMajorVersion;
711 break;
713 case ALC_EFX_MINOR_VERSION:
714 if(!size)
715 SetALCError(ALC_INVALID_VALUE);
716 else
717 *data = alcEFXMinorVersion;
718 break;
720 case ALC_MAX_AUXILIARY_SENDS:
721 if(!size)
722 SetALCError(ALC_INVALID_VALUE);
723 else
724 *data = MAX_SENDS;
725 break;
727 case ALC_ATTRIBUTES_SIZE:
728 if(!device)
729 SetALCError(ALC_INVALID_DEVICE);
730 else if(!size)
731 SetALCError(ALC_INVALID_VALUE);
732 else
733 *data = 12;
734 break;
736 case ALC_ALL_ATTRIBUTES:
737 if(!device)
738 SetALCError(ALC_INVALID_DEVICE);
739 else if (size < 7)
740 SetALCError(ALC_INVALID_VALUE);
741 else
743 int i = 0;
745 data[i++] = ALC_FREQUENCY;
746 data[i++] = device->Frequency;
748 data[i++] = ALC_REFRESH;
749 data[i++] = device->Frequency / device->UpdateFreq;
751 data[i++] = ALC_SYNC;
752 data[i++] = ALC_FALSE;
754 SuspendContext(NULL);
755 if(device->Context && size >= 12)
757 data[i++] = ALC_MONO_SOURCES;
758 data[i++] = device->Context->lNumMonoSources;
760 data[i++] = ALC_STEREO_SOURCES;
761 data[i++] = device->Context->lNumStereoSources;
763 data[i++] = ALC_MAX_AUXILIARY_SENDS;
764 data[i++] = MAX_SENDS;
766 ProcessContext(NULL);
768 data[i++] = 0;
770 break;
772 case ALC_FREQUENCY:
773 if(!device)
774 SetALCError(ALC_INVALID_DEVICE);
775 else if(!size)
776 SetALCError(ALC_INVALID_VALUE);
777 else
778 *data = device->Frequency;
779 break;
781 case ALC_REFRESH:
782 if(!device)
783 SetALCError(ALC_INVALID_DEVICE);
784 else if(!size)
785 SetALCError(ALC_INVALID_VALUE);
786 else
787 *data = device->Frequency / device->UpdateFreq;
788 break;
790 case ALC_SYNC:
791 if(!device)
792 SetALCError(ALC_INVALID_DEVICE);
793 else if(!size)
794 SetALCError(ALC_INVALID_VALUE);
795 else
796 *data = ALC_FALSE;
797 break;
799 case ALC_MONO_SOURCES:
800 if(!device || !device->Context)
801 SetALCError(ALC_INVALID_DEVICE);
802 else if (size != 1)
803 SetALCError(ALC_INVALID_VALUE);
804 else
805 *data = device->Context->lNumMonoSources;
806 break;
808 case ALC_STEREO_SOURCES:
809 if(!device || !device->Context)
810 SetALCError(ALC_INVALID_DEVICE);
811 else if (size != 1)
812 SetALCError(ALC_INVALID_VALUE);
813 else
814 *data = device->Context->lNumStereoSources;
815 break;
817 default:
818 SetALCError(ALC_INVALID_ENUM);
819 break;
822 else if(size)
823 SetALCError(ALC_INVALID_VALUE);
826 return;
831 alcIsExtensionPresent
833 Determines if there is support for a particular extension
835 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
837 ALCboolean bResult = ALC_FALSE;
838 ALsizei i = 0;
840 (void)device;
842 if (extName)
844 while(alcExtensions[i].extName &&
845 strcasecmp(alcExtensions[i].extName,extName) != 0)
846 i++;
848 if (alcExtensions[i].extName)
849 bResult = ALC_TRUE;
851 else
852 SetALCError(ALC_INVALID_VALUE);
854 return bResult;
859 alcGetProcAddress
861 Retrieves the function address for a particular extension function
863 ALCAPI ALCvoid * ALCAPIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
865 ALCvoid *pFunction = NULL;
866 ALsizei i = 0;
868 (void)device;
870 if (funcName)
872 while(alcFunctions[i].funcName &&
873 strcmp(alcFunctions[i].funcName,funcName) != 0)
874 i++;
875 pFunction = alcFunctions[i].address;
877 else
878 SetALCError(ALC_INVALID_VALUE);
880 return pFunction;
885 alcGetEnumValue
887 Get the value for a particular ALC Enumerated Value
889 ALCAPI ALCenum ALCAPIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
891 ALsizei i = 0;
892 ALCenum val;
894 (void)device;
896 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
897 i++;
898 val = enumeration[i].value;
900 if(!enumeration[i].enumName)
901 SetALCError(ALC_INVALID_VALUE);
903 return val;
908 alcCreateContext
910 Create and attach a Context to a particular Device.
912 ALCAPI ALCcontext* ALCAPIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
914 ALCcontext *ALContext = NULL;
915 ALuint ulAttributeIndex, ulRequestedStereoSources;
917 if ((device)&&(!device->IsCaptureDevice))
919 // Reset Context Last Error code
920 g_eLastContextError = ALC_NO_ERROR;
922 // Current implementation only allows one Context per Device
923 if(!device->Context)
925 ALContext = calloc(1, sizeof(ALCcontext));
926 if(!ALContext)
928 SetALCError(ALC_OUT_OF_MEMORY);
929 return NULL;
932 ALContext->Device = device;
933 InitContext(ALContext);
935 device->Context = ALContext;
937 SuspendContext(NULL);
939 ALContext->next = g_pContextList;
940 g_pContextList = ALContext;
941 g_ulContextCount++;
943 ProcessContext(NULL);
945 // Check for Voice Count attributes
946 if (attrList)
948 ulAttributeIndex = 0;
949 while ((ulAttributeIndex < 10) && (attrList[ulAttributeIndex]))
951 if (attrList[ulAttributeIndex] == ALC_STEREO_SOURCES)
953 ulRequestedStereoSources = attrList[ulAttributeIndex + 1];
955 if (ulRequestedStereoSources > ALContext->Device->MaxNoOfSources)
956 ulRequestedStereoSources = ALContext->Device->MaxNoOfSources;
958 ALContext->lNumStereoSources = ulRequestedStereoSources;
959 ALContext->lNumMonoSources = ALContext->Device->MaxNoOfSources - ALContext->lNumStereoSources;
960 break;
963 ulAttributeIndex += 2;
967 else
969 SetALCError(ALC_INVALID_VALUE);
970 ALContext = NULL;
973 else
974 SetALCError(ALC_INVALID_DEVICE);
976 return ALContext;
981 alcDestroyContext
983 Remove a Context
985 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
987 ALCcontext **list;
989 // Lock context list
990 SuspendContext(NULL);
992 if (IsContext(context))
994 // Lock context
995 SuspendContext(context);
997 ReleaseALSources(context);
998 ReleaseALAuxiliaryEffectSlots(context);
1000 context->Device->Context = NULL;
1002 list = &g_pContextList;
1003 while(*list != context)
1004 list = &(*list)->next;
1006 *list = (*list)->next;
1007 g_ulContextCount--;
1009 // Unlock context
1010 ProcessContext(context);
1012 ExitContext(context);
1014 // Free memory (MUST do this after ProcessContext)
1015 memset(context, 0, sizeof(ALCcontext));
1016 free(context);
1018 else
1019 SetALCError(ALC_INVALID_CONTEXT);
1021 ProcessContext(NULL);
1026 alcGetCurrentContext
1028 Returns the currently active Context
1030 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1032 ALCcontext *pContext = NULL;
1034 SuspendContext(NULL);
1036 pContext = g_pContextList;
1037 while ((pContext) && (!pContext->InUse))
1038 pContext = pContext->next;
1040 ProcessContext(NULL);
1042 return pContext;
1047 alcGetContextsDevice
1049 Returns the Device that a particular Context is attached to
1051 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1053 ALCdevice *pDevice = NULL;
1055 SuspendContext(NULL);
1056 if (IsContext(pContext))
1057 pDevice = pContext->Device;
1058 else
1059 SetALCError(ALC_INVALID_CONTEXT);
1060 ProcessContext(NULL);
1062 return pDevice;
1067 alcMakeContextCurrent
1069 Makes the given Context the active Context
1071 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1073 ALCcontext *ALContext;
1074 ALboolean bReturn = AL_TRUE;
1076 SuspendContext(NULL);
1078 // context must be a valid Context or NULL
1079 if ((IsContext(context)) || (context == NULL))
1081 if ((ALContext=alcGetCurrentContext()))
1083 SuspendContext(ALContext);
1084 ALContext->InUse=AL_FALSE;
1085 ProcessContext(ALContext);
1088 if ((ALContext=context) && (ALContext->Device))
1090 SuspendContext(ALContext);
1091 ALContext->InUse=AL_TRUE;
1092 ProcessContext(ALContext);
1095 else
1097 SetALCError(ALC_INVALID_CONTEXT);
1098 bReturn = AL_FALSE;
1101 ProcessContext(NULL);
1103 return bReturn;
1108 alcOpenDevice
1110 Open the Device specified.
1112 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1114 ALboolean bDeviceFound = AL_FALSE;
1115 ALCdevice *device;
1116 ALint i;
1118 InitAL();
1120 if(deviceName && !deviceName[0])
1121 deviceName = NULL;
1123 device = malloc(sizeof(ALCdevice));
1124 if (device)
1126 const char *fmt;
1128 //Initialise device structure
1129 memset(device, 0, sizeof(ALCdevice));
1131 //Validate device
1132 device->InUse = AL_TRUE;
1133 device->IsCaptureDevice = AL_FALSE;
1135 //Set output format
1136 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1137 if((ALint)device->Frequency <= 0)
1138 device->Frequency = SWMIXER_OUTPUT_RATE;
1140 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1141 if(fmt[0])
1142 device->Format = alGetEnumValue(fmt);
1144 device->Channels = aluChannelsFromFormat(device->Format);
1145 if(!device->Channels)
1147 device->Format = AL_FORMAT_STEREO16;
1148 device->Channels = 2;
1149 device->FrameSize = 4;
1151 else
1152 device->FrameSize = aluBytesFromFormat(device->Format) *
1153 device->Channels;
1155 device->UpdateFreq = GetConfigValueInt(NULL, "refresh", 8192);
1156 if((ALint)device->UpdateFreq <= 0)
1157 device->UpdateFreq = 8192;
1159 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1160 if((ALint)device->MaxNoOfSources <= 0)
1161 device->MaxNoOfSources = 256;
1163 // Find a playback device to open
1164 for(i = 0;BackendList[i].Init;i++)
1166 device->Funcs = &BackendList[i].Funcs;
1167 if(ALCdevice_OpenPlayback(device, deviceName))
1169 SuspendContext(NULL);
1170 device->next = g_pDeviceList;
1171 g_pDeviceList = device;
1172 g_ulDeviceCount++;
1173 ProcessContext(NULL);
1175 bDeviceFound = AL_TRUE;
1176 break;
1180 if (!bDeviceFound)
1182 // No suitable output device found
1183 free(device);
1184 device = NULL;
1188 return device;
1193 alcCloseDevice
1195 Close the specified Device
1197 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1199 ALCboolean bReturn = ALC_FALSE;
1200 ALCdevice **list;
1202 if ((pDevice)&&(!pDevice->IsCaptureDevice))
1204 SuspendContext(NULL);
1206 list = &g_pDeviceList;
1207 while(*list != pDevice)
1208 list = &(*list)->next;
1210 *list = (*list)->next;
1211 g_ulDeviceCount--;
1213 ProcessContext(NULL);
1215 if(pDevice->Context)
1216 alcDestroyContext(pDevice->Context);
1217 ALCdevice_ClosePlayback(pDevice);
1219 //Release device structure
1220 memset(pDevice, 0, sizeof(ALCdevice));
1221 free(pDevice);
1223 bReturn = ALC_TRUE;
1225 else
1226 SetALCError(ALC_INVALID_DEVICE);
1228 return bReturn;
1232 ALCvoid ReleaseALC(ALCvoid)
1234 ALCdevice *Dev;
1236 #ifdef _DEBUG
1237 if(g_ulContextCount > 0)
1238 AL_PRINT("exit() %u device(s) and %u context(s) NOT deleted\n", g_ulDeviceCount, g_ulContextCount);
1239 #endif
1241 while(g_pDeviceList)
1243 Dev = g_pDeviceList;
1244 g_pDeviceList = g_pDeviceList->next;
1245 if(Dev->IsCaptureDevice)
1246 alcCaptureCloseDevice(Dev);
1247 else
1248 alcCloseDevice(Dev);
1252 ///////////////////////////////////////////////////////