Remove unnecessary Channels field
[openal-soft.git] / Alc / ALc.c
blobb485ec2e8cf9250985b41910bafaa39724071267
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;
206 InitializeCriticalSection(&_alMutex);
207 ALTHUNK_INIT();
208 ReadALConfig();
210 devs = GetConfigValue(NULL, "drivers", "");
211 if(devs[0])
213 int n;
214 size_t len;
215 const char *next = devs;
217 i = 0;
219 do {
220 devs = next;
221 next = strchr(devs, ',');
223 if(!devs[0] || devs[0] == ',')
224 continue;
226 len = (next ? ((size_t)(next-devs)) : strlen(devs));
227 for(n = i;BackendList[n].Init;n++)
229 if(len == strlen(BackendList[n].name) &&
230 strncmp(BackendList[n].name, devs, len) == 0)
232 const char *name = BackendList[i].name;
233 void (*Init)(BackendFuncs*) = BackendList[i].Init;
235 BackendList[i].name = BackendList[n].name;
236 BackendList[i].Init = BackendList[n].Init;
238 BackendList[n].name = name;
239 BackendList[n].Init = Init;
241 i++;
244 } while(next++);
246 BackendList[i].name = NULL;
247 BackendList[i].Init = NULL;
250 for(i = 0;BackendList[i].Init;i++)
251 BackendList[i].Init(&BackendList[i].Funcs);
252 done = 1;
256 ALCchar *AppendDeviceList(char *name)
258 static int pos;
259 ALCchar *ret = alcDeviceList+pos;
260 pos += snprintf(alcDeviceList+pos, sizeof(alcDeviceList)-pos, "%s", name) + 1;
261 return ret;
264 ALCchar *AppendAllDeviceList(char *name)
266 static int pos;
267 ALCchar *ret = alcAllDeviceList+pos;
268 pos += snprintf(alcAllDeviceList+pos, sizeof(alcAllDeviceList)-pos, "%s", name) + 1;
269 return ret;
272 ALCchar *AppendCaptureDeviceList(char *name)
274 static int pos;
275 ALCchar *ret = alcCaptureDeviceList+pos;
276 pos += snprintf(alcCaptureDeviceList+pos, sizeof(alcCaptureDeviceList)-pos, "%s", name) + 1;
277 return ret;
281 IsContext
283 Check pContext is a valid Context pointer
285 static ALCboolean IsContext(ALCcontext *pContext)
287 ALCcontext *pTempContext;
289 pTempContext = g_pContextList;
290 while (pTempContext && pTempContext != pContext)
291 pTempContext = pTempContext->next;
293 return (pTempContext ? ALC_TRUE : ALC_FALSE);
298 SetALCError
300 Store latest ALC Error
302 ALCvoid SetALCError(ALenum errorCode)
304 g_eLastContextError = errorCode;
309 SuspendContext
311 Thread-safe entry
313 ALCvoid SuspendContext(ALCcontext *pContext)
315 (void)pContext;
316 EnterCriticalSection(&_alMutex);
321 ProcessContext
323 Thread-safe exit
325 ALCvoid ProcessContext(ALCcontext *pContext)
327 (void)pContext;
328 LeaveCriticalSection(&_alMutex);
333 InitContext
335 Initialize Context variables
337 static ALvoid InitContext(ALCcontext *pContext)
339 int level;
341 //Initialise listener
342 pContext->Listener.Gain = 1.0f;
343 pContext->Listener.MetersPerUnit = 1.0f;
344 pContext->Listener.Position[0] = 0.0f;
345 pContext->Listener.Position[1] = 0.0f;
346 pContext->Listener.Position[2] = 0.0f;
347 pContext->Listener.Velocity[0] = 0.0f;
348 pContext->Listener.Velocity[1] = 0.0f;
349 pContext->Listener.Velocity[2] = 0.0f;
350 pContext->Listener.Forward[0] = 0.0f;
351 pContext->Listener.Forward[1] = 0.0f;
352 pContext->Listener.Forward[2] = -1.0f;
353 pContext->Listener.Up[0] = 0.0f;
354 pContext->Listener.Up[1] = 1.0f;
355 pContext->Listener.Up[2] = 0.0f;
357 //Validate pContext
358 pContext->LastError = AL_NO_ERROR;
359 pContext->InUse = AL_FALSE;
361 //Set output format
362 pContext->Frequency = pContext->Device->Frequency;
364 //Set globals
365 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
366 pContext->DopplerFactor = 1.0f;
367 pContext->DopplerVelocity = 1.0f;
368 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
370 pContext->lNumStereoSources = 1;
371 pContext->lNumMonoSources = pContext->Device->MaxNoOfSources - pContext->lNumStereoSources;
373 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");
375 level = GetConfigValueInt(NULL, "cf_level", 0);
376 if(level > 0 && level <= 6)
378 pContext->bs2b = calloc(1, sizeof(*pContext->bs2b));
379 bs2b_set_srate(pContext->bs2b, pContext->Frequency);
380 bs2b_set_level(pContext->bs2b, level);
386 ExitContext
388 Clean up Context, destroy any remaining Sources
390 static ALCvoid ExitContext(ALCcontext *pContext)
392 //Invalidate context
393 pContext->LastError = AL_NO_ERROR;
394 pContext->InUse = AL_FALSE;
396 free(pContext->bs2b);
397 pContext->bs2b = NULL;
400 ///////////////////////////////////////////////////////
403 ///////////////////////////////////////////////////////
404 // ALC Functions calls
407 // This should probably move to another c file but for now ...
408 ALCAPI ALCdevice* ALCAPIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
410 ALCboolean DeviceFound = ALC_FALSE;
411 ALCdevice *pDevice = NULL;
412 ALCint i;
414 InitAL();
416 if(deviceName && !deviceName[0])
417 deviceName = NULL;
419 pDevice = malloc(sizeof(ALCdevice));
420 if (pDevice)
422 if (SampleSize > 0)
424 //Initialise device structure
425 memset(pDevice, 0, sizeof(ALCdevice));
427 //Validate device
428 pDevice->InUse = AL_TRUE;
429 pDevice->IsCaptureDevice = AL_TRUE;
431 pDevice->Frequency = frequency;
432 pDevice->Format = format;
433 pDevice->FrameSize = aluBytesFromFormat(format) *
434 aluChannelsFromFormat(format);
436 for(i = 0;BackendList[i].Init;i++)
438 pDevice->Funcs = &BackendList[i].Funcs;
439 if(ALCdevice_OpenCapture(pDevice, deviceName, frequency, format, SampleSize))
441 SuspendContext(NULL);
442 pDevice->next = g_pDeviceList;
443 g_pDeviceList = pDevice;
444 g_ulDeviceCount++;
445 ProcessContext(NULL);
447 DeviceFound = ALC_TRUE;
448 break;
452 else
453 SetALCError(ALC_INVALID_VALUE);
455 if(!DeviceFound)
457 free(pDevice);
458 pDevice = NULL;
461 else
462 SetALCError(ALC_OUT_OF_MEMORY);
464 return pDevice;
467 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
469 ALCboolean bReturn = ALC_FALSE;
470 ALCdevice **list;
472 if ((pDevice)&&(pDevice->IsCaptureDevice))
474 SuspendContext(NULL);
476 list = &g_pDeviceList;
477 while(*list != pDevice)
478 list = &(*list)->next;
480 *list = (*list)->next;
481 g_ulDeviceCount--;
483 ProcessContext(NULL);
485 ALCdevice_CloseCapture(pDevice);
486 free(pDevice);
488 bReturn = ALC_TRUE;
490 else
491 SetALCError(ALC_INVALID_DEVICE);
493 return bReturn;
496 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
498 if ((pDevice)&&(pDevice->IsCaptureDevice))
499 ALCdevice_StartCapture(pDevice);
500 else
501 SetALCError(ALC_INVALID_DEVICE);
504 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
506 if ((pDevice)&&(pDevice->IsCaptureDevice))
507 ALCdevice_StopCapture(pDevice);
508 else
509 SetALCError(ALC_INVALID_DEVICE);
512 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
514 if ((pDevice) && (pDevice->IsCaptureDevice))
515 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
516 else
517 SetALCError(ALC_INVALID_DEVICE);
521 alcGetError
523 Return last ALC generated error code
525 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
527 ALCenum errorCode;
529 (void)device;
531 errorCode = g_eLastContextError;
532 g_eLastContextError = ALC_NO_ERROR;
533 return errorCode;
538 alcSuspendContext
540 Not functional
542 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
544 // Not a lot happens here !
545 (void)pContext;
550 alcProcessContext
552 Not functional
554 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
556 // Not a lot happens here !
557 (void)pContext;
562 alcGetString
564 Returns information about the Device, and error strings
566 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
568 const ALCchar *value = NULL;
570 InitAL();
572 switch (param)
574 case ALC_NO_ERROR:
575 value = alcNoError;
576 break;
578 case ALC_INVALID_ENUM:
579 value = alcErrInvalidEnum;
580 break;
582 case ALC_INVALID_VALUE:
583 value = alcErrInvalidValue;
584 break;
586 case ALC_INVALID_DEVICE:
587 value = alcErrInvalidDevice;
588 break;
590 case ALC_INVALID_CONTEXT:
591 value = alcErrInvalidContext;
592 break;
594 case ALC_OUT_OF_MEMORY:
595 value = alcErrOutOfMemory;
596 break;
598 case ALC_DEFAULT_DEVICE_SPECIFIER:
599 value = alcDefaultDeviceSpecifier;
600 break;
602 case ALC_DEVICE_SPECIFIER:
603 if (pDevice)
604 value = pDevice->szDeviceName;
605 else
606 value = alcDeviceList;
607 break;
609 case ALC_ALL_DEVICES_SPECIFIER:
610 value = alcAllDeviceList;
611 break;
613 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
614 value = alcDefaultAllDeviceSpecifier;
615 break;
617 case ALC_CAPTURE_DEVICE_SPECIFIER:
618 if (pDevice)
619 value = pDevice->szDeviceName;
620 else
621 value = alcCaptureDeviceList;
622 break;
624 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
625 value = alcCaptureDefaultDeviceSpecifier;
626 break;
628 case ALC_EXTENSIONS:
629 value = alcExtensionList;
630 break;
632 default:
633 SetALCError(ALC_INVALID_ENUM);
634 break;
637 return value;
642 alcGetIntegerv
644 Returns information about the Device and the version of Open AL
646 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
648 InitAL();
650 if ((device)&&(device->IsCaptureDevice))
652 SuspendContext(NULL);
654 // Capture device
655 switch (param)
657 case ALC_CAPTURE_SAMPLES:
658 if ((size) && (data))
659 *data = ALCdevice_AvailableSamples(device);
660 else
661 SetALCError(ALC_INVALID_VALUE);
662 break;
664 default:
665 SetALCError(ALC_INVALID_ENUM);
666 break;
669 ProcessContext(NULL);
671 else
673 if(data)
675 // Playback Device
676 switch (param)
678 case ALC_MAJOR_VERSION:
679 if(!size)
680 SetALCError(ALC_INVALID_VALUE);
681 else
682 *data = alcMajorVersion;
683 break;
685 case ALC_MINOR_VERSION:
686 if(!size)
687 SetALCError(ALC_INVALID_VALUE);
688 else
689 *data = alcMinorVersion;
690 break;
692 case ALC_EFX_MAJOR_VERSION:
693 if(!size)
694 SetALCError(ALC_INVALID_VALUE);
695 else
696 *data = alcEFXMajorVersion;
697 break;
699 case ALC_EFX_MINOR_VERSION:
700 if(!size)
701 SetALCError(ALC_INVALID_VALUE);
702 else
703 *data = alcEFXMinorVersion;
704 break;
706 case ALC_MAX_AUXILIARY_SENDS:
707 if(!size)
708 SetALCError(ALC_INVALID_VALUE);
709 else
710 *data = MAX_SENDS;
711 break;
713 case ALC_ATTRIBUTES_SIZE:
714 if(!device)
715 SetALCError(ALC_INVALID_DEVICE);
716 else if(!size)
717 SetALCError(ALC_INVALID_VALUE);
718 else
719 *data = 12;
720 break;
722 case ALC_ALL_ATTRIBUTES:
723 if(!device)
724 SetALCError(ALC_INVALID_DEVICE);
725 else if (size < 7)
726 SetALCError(ALC_INVALID_VALUE);
727 else
729 int i = 0;
731 data[i++] = ALC_FREQUENCY;
732 data[i++] = device->Frequency;
734 data[i++] = ALC_REFRESH;
735 data[i++] = device->Frequency / device->UpdateFreq;
737 data[i++] = ALC_SYNC;
738 data[i++] = ALC_FALSE;
740 SuspendContext(NULL);
741 if(device->Context && size >= 12)
743 data[i++] = ALC_MONO_SOURCES;
744 data[i++] = device->Context->lNumMonoSources;
746 data[i++] = ALC_STEREO_SOURCES;
747 data[i++] = device->Context->lNumStereoSources;
749 data[i++] = ALC_MAX_AUXILIARY_SENDS;
750 data[i++] = MAX_SENDS;
752 ProcessContext(NULL);
754 data[i++] = 0;
756 break;
758 case ALC_FREQUENCY:
759 if(!device)
760 SetALCError(ALC_INVALID_DEVICE);
761 else if(!size)
762 SetALCError(ALC_INVALID_VALUE);
763 else
764 *data = device->Frequency;
765 break;
767 case ALC_REFRESH:
768 if(!device)
769 SetALCError(ALC_INVALID_DEVICE);
770 else if(!size)
771 SetALCError(ALC_INVALID_VALUE);
772 else
773 *data = device->Frequency / device->UpdateFreq;
774 break;
776 case ALC_SYNC:
777 if(!device)
778 SetALCError(ALC_INVALID_DEVICE);
779 else if(!size)
780 SetALCError(ALC_INVALID_VALUE);
781 else
782 *data = ALC_FALSE;
783 break;
785 case ALC_MONO_SOURCES:
786 if(!device || !device->Context)
787 SetALCError(ALC_INVALID_DEVICE);
788 else if (size != 1)
789 SetALCError(ALC_INVALID_VALUE);
790 else
791 *data = device->Context->lNumMonoSources;
792 break;
794 case ALC_STEREO_SOURCES:
795 if(!device || !device->Context)
796 SetALCError(ALC_INVALID_DEVICE);
797 else if (size != 1)
798 SetALCError(ALC_INVALID_VALUE);
799 else
800 *data = device->Context->lNumStereoSources;
801 break;
803 default:
804 SetALCError(ALC_INVALID_ENUM);
805 break;
808 else if(size)
809 SetALCError(ALC_INVALID_VALUE);
812 return;
817 alcIsExtensionPresent
819 Determines if there is support for a particular extension
821 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
823 ALCboolean bResult = ALC_FALSE;
825 (void)device;
827 if (extName)
829 const char *ptr;
830 size_t len;
832 len = strlen(extName);
833 ptr = alcExtensionList;
834 while(ptr && *ptr)
836 if(strncasecmp(ptr, extName, len) == 0 &&
837 (ptr[len] == '\0' || isspace(ptr[len])))
839 bResult = ALC_TRUE;
840 break;
842 if((ptr=strchr(ptr, ' ')) != NULL)
844 do {
845 ++ptr;
846 } while(isspace(*ptr));
850 else
851 SetALCError(ALC_INVALID_VALUE);
853 return bResult;
858 alcGetProcAddress
860 Retrieves the function address for a particular extension function
862 ALCAPI ALCvoid * ALCAPIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
864 ALCvoid *pFunction = NULL;
865 ALsizei i = 0;
867 (void)device;
869 if (funcName)
871 while(alcFunctions[i].funcName &&
872 strcmp(alcFunctions[i].funcName,funcName) != 0)
873 i++;
874 pFunction = alcFunctions[i].address;
876 else
877 SetALCError(ALC_INVALID_VALUE);
879 return pFunction;
884 alcGetEnumValue
886 Get the value for a particular ALC Enumerated Value
888 ALCAPI ALCenum ALCAPIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
890 ALsizei i = 0;
891 ALCenum val;
893 (void)device;
895 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
896 i++;
897 val = enumeration[i].value;
899 if(!enumeration[i].enumName)
900 SetALCError(ALC_INVALID_VALUE);
902 return val;
907 alcCreateContext
909 Create and attach a Context to a particular Device.
911 ALCAPI ALCcontext* ALCAPIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
913 ALCcontext *ALContext = NULL;
914 ALuint ulAttributeIndex, ulRequestedStereoSources;
916 if ((device)&&(!device->IsCaptureDevice))
918 // Reset Context Last Error code
919 g_eLastContextError = ALC_NO_ERROR;
921 // Current implementation only allows one Context per Device
922 if(!device->Context)
924 ALContext = calloc(1, sizeof(ALCcontext));
925 if(!ALContext)
927 SetALCError(ALC_OUT_OF_MEMORY);
928 return NULL;
931 ALContext->Device = device;
932 InitContext(ALContext);
934 device->Context = ALContext;
936 SuspendContext(NULL);
938 ALContext->next = g_pContextList;
939 g_pContextList = ALContext;
940 g_ulContextCount++;
942 ProcessContext(NULL);
944 // Check for Voice Count attributes
945 if (attrList)
947 ulAttributeIndex = 0;
948 while ((ulAttributeIndex < 10) && (attrList[ulAttributeIndex]))
950 if (attrList[ulAttributeIndex] == ALC_STEREO_SOURCES)
952 ulRequestedStereoSources = attrList[ulAttributeIndex + 1];
954 if (ulRequestedStereoSources > ALContext->Device->MaxNoOfSources)
955 ulRequestedStereoSources = ALContext->Device->MaxNoOfSources;
957 ALContext->lNumStereoSources = ulRequestedStereoSources;
958 ALContext->lNumMonoSources = ALContext->Device->MaxNoOfSources - ALContext->lNumStereoSources;
959 break;
962 ulAttributeIndex += 2;
966 else
968 SetALCError(ALC_INVALID_VALUE);
969 ALContext = NULL;
972 else
973 SetALCError(ALC_INVALID_DEVICE);
975 return ALContext;
980 alcDestroyContext
982 Remove a Context
984 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
986 ALCcontext **list;
988 // Lock context list
989 SuspendContext(NULL);
991 if (IsContext(context))
993 // Lock context
994 SuspendContext(context);
996 ReleaseALSources(context);
997 ReleaseALAuxiliaryEffectSlots(context);
999 context->Device->Context = NULL;
1001 list = &g_pContextList;
1002 while(*list != context)
1003 list = &(*list)->next;
1005 *list = (*list)->next;
1006 g_ulContextCount--;
1008 // Unlock context
1009 ProcessContext(context);
1011 ExitContext(context);
1013 // Free memory (MUST do this after ProcessContext)
1014 memset(context, 0, sizeof(ALCcontext));
1015 free(context);
1017 else
1018 SetALCError(ALC_INVALID_CONTEXT);
1020 ProcessContext(NULL);
1025 alcGetCurrentContext
1027 Returns the currently active Context
1029 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1031 ALCcontext *pContext = NULL;
1033 SuspendContext(NULL);
1035 pContext = g_pContextList;
1036 while ((pContext) && (!pContext->InUse))
1037 pContext = pContext->next;
1039 ProcessContext(NULL);
1041 return pContext;
1046 alcGetContextsDevice
1048 Returns the Device that a particular Context is attached to
1050 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1052 ALCdevice *pDevice = NULL;
1054 SuspendContext(NULL);
1055 if (IsContext(pContext))
1056 pDevice = pContext->Device;
1057 else
1058 SetALCError(ALC_INVALID_CONTEXT);
1059 ProcessContext(NULL);
1061 return pDevice;
1066 alcMakeContextCurrent
1068 Makes the given Context the active Context
1070 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1072 ALCcontext *ALContext;
1073 ALboolean bReturn = AL_TRUE;
1075 SuspendContext(NULL);
1077 // context must be a valid Context or NULL
1078 if ((IsContext(context)) || (context == NULL))
1080 if ((ALContext=alcGetCurrentContext()))
1082 SuspendContext(ALContext);
1083 ALContext->InUse=AL_FALSE;
1084 ProcessContext(ALContext);
1087 if ((ALContext=context) && (ALContext->Device))
1089 SuspendContext(ALContext);
1090 ALContext->InUse=AL_TRUE;
1091 ProcessContext(ALContext);
1094 else
1096 SetALCError(ALC_INVALID_CONTEXT);
1097 bReturn = AL_FALSE;
1100 ProcessContext(NULL);
1102 return bReturn;
1107 alcOpenDevice
1109 Open the Device specified.
1111 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1113 ALboolean bDeviceFound = AL_FALSE;
1114 ALCdevice *device;
1115 ALint i;
1117 InitAL();
1119 if(deviceName && !deviceName[0])
1120 deviceName = NULL;
1122 device = malloc(sizeof(ALCdevice));
1123 if (device)
1125 const char *fmt;
1127 //Initialise device structure
1128 memset(device, 0, sizeof(ALCdevice));
1130 //Validate device
1131 device->InUse = AL_TRUE;
1132 device->IsCaptureDevice = AL_FALSE;
1134 //Set output format
1135 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1136 if((ALint)device->Frequency <= 0)
1137 device->Frequency = SWMIXER_OUTPUT_RATE;
1139 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1140 if(fmt[0])
1141 device->Format = alGetEnumValue(fmt);
1143 if(!aluChannelsFromFormat(device->Format))
1145 device->Format = AL_FORMAT_STEREO16;
1146 device->FrameSize = 4;
1148 else
1149 device->FrameSize = aluBytesFromFormat(device->Format) *
1150 aluChannelsFromFormat(device->Format);
1152 device->UpdateFreq = GetConfigValueInt(NULL, "refresh", 8192);
1153 if((ALint)device->UpdateFreq <= 0)
1154 device->UpdateFreq = 8192;
1156 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1157 if((ALint)device->MaxNoOfSources <= 0)
1158 device->MaxNoOfSources = 256;
1160 // Find a playback device to open
1161 for(i = 0;BackendList[i].Init;i++)
1163 device->Funcs = &BackendList[i].Funcs;
1164 if(ALCdevice_OpenPlayback(device, deviceName))
1166 SuspendContext(NULL);
1167 device->next = g_pDeviceList;
1168 g_pDeviceList = device;
1169 g_ulDeviceCount++;
1170 ProcessContext(NULL);
1172 bDeviceFound = AL_TRUE;
1173 break;
1177 if (!bDeviceFound)
1179 // No suitable output device found
1180 free(device);
1181 device = NULL;
1185 return device;
1190 alcCloseDevice
1192 Close the specified Device
1194 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1196 ALCboolean bReturn = ALC_FALSE;
1197 ALCdevice **list;
1199 if ((pDevice)&&(!pDevice->IsCaptureDevice))
1201 SuspendContext(NULL);
1203 list = &g_pDeviceList;
1204 while(*list != pDevice)
1205 list = &(*list)->next;
1207 *list = (*list)->next;
1208 g_ulDeviceCount--;
1210 ProcessContext(NULL);
1212 if(pDevice->Context)
1213 alcDestroyContext(pDevice->Context);
1214 ALCdevice_ClosePlayback(pDevice);
1216 //Release device structure
1217 memset(pDevice, 0, sizeof(ALCdevice));
1218 free(pDevice);
1220 bReturn = ALC_TRUE;
1222 else
1223 SetALCError(ALC_INVALID_DEVICE);
1225 return bReturn;
1229 ALCvoid ReleaseALC(ALCvoid)
1231 ALCdevice *Dev;
1233 #ifdef _DEBUG
1234 if(g_ulContextCount > 0)
1235 AL_PRINT("exit() %u device(s) and %u context(s) NOT deleted\n", g_ulDeviceCount, g_ulContextCount);
1236 #endif
1238 while(g_pDeviceList)
1240 Dev = g_pDeviceList;
1241 g_pDeviceList = g_pDeviceList->next;
1242 if(Dev->IsCaptureDevice)
1243 alcCaptureCloseDevice(Dev);
1244 else
1245 alcCloseDevice(Dev);
1249 ///////////////////////////////////////////////////////