Disable fast float-to-int hack.
[openal-soft.git] / Alc / ALc.c
blob1a66dc2a515a96028bb1cf5d3ff0d15144990480
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 "AL/al.h"
31 #include "AL/alc.h"
32 #include "alThunk.h"
33 #include "alSource.h"
34 #include "alExtension.h"
35 #include "bs2b.h"
37 ///////////////////////////////////////////////////////
38 // DEBUG INFORMATION
40 char _alDebug[256];
42 ///////////////////////////////////////////////////////
45 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
46 struct {
47 const char *name;
48 void (*Init)(BackendFuncs*);
49 BackendFuncs Funcs;
50 } BackendList[] = {
51 #ifdef HAVE_ALSA
52 { "alsa", alc_alsa_init, EmptyFuncs },
53 #endif
54 #ifdef HAVE_OSS
55 { "oss", alc_oss_init, EmptyFuncs },
56 #endif
57 #ifdef HAVE_DSOUND
58 { "dsound", alcDSoundInit, EmptyFuncs },
59 #endif
60 #ifdef HAVE_WINMM
61 { "winmm", alcWinMMInit, EmptyFuncs },
62 #endif
64 { NULL, NULL, EmptyFuncs }
66 #undef EmptyFuncs
68 ///////////////////////////////////////////////////////
71 ///////////////////////////////////////////////////////
72 // STRING and EXTENSIONS
74 typedef struct ALCextension_struct
76 ALCchar *extName;
77 ALvoid *address;
78 } ALCextension;
80 typedef struct ALCfunction_struct
82 ALCchar *funcName;
83 ALvoid *address;
84 } ALCfunction;
86 static ALCextension alcExtensions[] = {
87 { "ALC_ENUMERATE_ALL_EXT", (ALvoid *) NULL },
88 { "ALC_ENUMERATION_EXT", (ALvoid *) NULL },
89 { "ALC_EXT_CAPTURE", (ALvoid *) NULL },
90 { NULL, (ALvoid *) NULL }
93 static ALCfunction alcFunctions[] = {
94 { "alcCreateContext", (ALvoid *) alcCreateContext },
95 { "alcMakeContextCurrent", (ALvoid *) alcMakeContextCurrent },
96 { "alcProcessContext", (ALvoid *) alcProcessContext },
97 { "alcSuspendContext", (ALvoid *) alcSuspendContext },
98 { "alcDestroyContext", (ALvoid *) alcDestroyContext },
99 { "alcGetCurrentContext", (ALvoid *) alcGetCurrentContext },
100 { "alcGetContextsDevice", (ALvoid *) alcGetContextsDevice },
101 { "alcOpenDevice", (ALvoid *) alcOpenDevice },
102 { "alcCloseDevice", (ALvoid *) alcCloseDevice },
103 { "alcGetError", (ALvoid *) alcGetError },
104 { "alcIsExtensionPresent", (ALvoid *) alcIsExtensionPresent },
105 { "alcGetProcAddress", (ALvoid *) alcGetProcAddress },
106 { "alcGetEnumValue", (ALvoid *) alcGetEnumValue },
107 { "alcGetString", (ALvoid *) alcGetString },
108 { "alcGetIntegerv", (ALvoid *) alcGetIntegerv },
109 { "alcCaptureOpenDevice", (ALvoid *) alcCaptureOpenDevice },
110 { "alcCaptureCloseDevice", (ALvoid *) alcCaptureCloseDevice },
111 { "alcCaptureStart", (ALvoid *) alcCaptureStart },
112 { "alcCaptureStop", (ALvoid *) alcCaptureStop },
113 { "alcCaptureSamples", (ALvoid *) alcCaptureSamples },
114 { NULL, (ALvoid *) NULL }
117 static ALenums enumeration[]={
118 // Types
119 { (ALchar *)"ALC_INVALID", ALC_INVALID },
120 { (ALchar *)"ALC_FALSE", ALC_FALSE },
121 { (ALchar *)"ALC_TRUE", ALC_TRUE },
123 // ALC Properties
124 { (ALchar *)"ALC_MAJOR_VERSION", ALC_MAJOR_VERSION },
125 { (ALchar *)"ALC_MINOR_VERSION", ALC_MINOR_VERSION },
126 { (ALchar *)"ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE },
127 { (ALchar *)"ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES },
128 { (ALchar *)"ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER },
129 { (ALchar *)"ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER },
130 { (ALchar *)"ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER },
131 { (ALchar *)"ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER },
132 { (ALchar *)"ALC_EXTENSIONS", ALC_EXTENSIONS },
133 { (ALchar *)"ALC_FREQUENCY", ALC_FREQUENCY },
134 { (ALchar *)"ALC_REFRESH", ALC_REFRESH },
135 { (ALchar *)"ALC_SYNC", ALC_SYNC },
136 { (ALchar *)"ALC_MONO_SOURCES", ALC_MONO_SOURCES },
137 { (ALchar *)"ALC_STEREO_SOURCES", ALC_STEREO_SOURCES },
138 { (ALchar *)"ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER },
139 { (ALchar *)"ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER },
140 { (ALchar *)"ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES },
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";
170 static ALCint alcMajorVersion = 1;
171 static ALCint alcMinorVersion = 1;
173 ///////////////////////////////////////////////////////
176 ///////////////////////////////////////////////////////
177 // Global Variables
179 // Context List
180 static ALCcontext *g_pContextList = NULL;
181 static ALCuint g_ulContextCount = 0;
183 // Context Error
184 static ALCenum g_eLastContextError = ALC_NO_ERROR;
186 ///////////////////////////////////////////////////////
189 ///////////////////////////////////////////////////////
190 // ALC Related helper functions
192 static void InitAL(void)
194 static int done = 0;
195 if(!done)
197 int i;
198 const char *devs;
200 InitializeCriticalSection(&_alMutex);
201 ALTHUNK_INIT();
202 ReadALConfig();
204 devs = GetConfigValue(NULL, "drivers", "");
205 if(devs[0])
207 int n;
208 size_t len;
209 const char *next = devs;
211 i = 0;
213 do {
214 devs = next;
215 next = strchr(devs, ',');
217 if(!devs[0] || devs[0] == ',')
218 continue;
220 len = (next ? ((size_t)(next-devs)) : strlen(devs));
221 for(n = i;BackendList[n].Init;n++)
223 if(len == strlen(BackendList[n].name) &&
224 strncmp(BackendList[n].name, devs, len) == 0)
226 const char *name = BackendList[i].name;
227 void (*Init)(BackendFuncs*) = BackendList[i].Init;
229 BackendList[i].name = BackendList[n].name;
230 BackendList[i].Init = BackendList[n].Init;
232 BackendList[n].name = name;
233 BackendList[n].Init = Init;
235 i++;
238 } while(next++);
240 BackendList[i].name = NULL;
241 BackendList[i].Init = NULL;
244 for(i = 0;BackendList[i].Init;i++)
245 BackendList[i].Init(&BackendList[i].Funcs);
246 done = 1;
250 ALCchar *AppendDeviceList(char *name)
252 static int pos;
253 ALCchar *ret = alcDeviceList+pos;
254 pos += snprintf(alcDeviceList+pos, sizeof(alcDeviceList)-pos, "%s", name) + 1;
255 return ret;
258 ALCchar *AppendAllDeviceList(char *name)
260 static int pos;
261 ALCchar *ret = alcAllDeviceList+pos;
262 pos += snprintf(alcAllDeviceList+pos, sizeof(alcAllDeviceList)-pos, "%s", name) + 1;
263 return ret;
266 ALCchar *AppendCaptureDeviceList(char *name)
268 static int pos;
269 ALCchar *ret = alcCaptureDeviceList+pos;
270 pos += snprintf(alcCaptureDeviceList+pos, sizeof(alcCaptureDeviceList)-pos, "%s", name) + 1;
271 return ret;
275 IsContext
277 Check pContext is a valid Context pointer
279 static ALCboolean IsContext(ALCcontext *pContext)
281 ALCcontext *pTempContext;
283 pTempContext = g_pContextList;
284 while (pTempContext && pTempContext != pContext)
285 pTempContext = pTempContext->next;
287 return (pTempContext ? ALC_TRUE : ALC_FALSE);
292 SetALCError
294 Store latest ALC Error
296 ALCvoid SetALCError(ALenum errorCode)
298 g_eLastContextError = errorCode;
303 SuspendContext
305 Thread-safe entry
307 ALCvoid SuspendContext(ALCcontext *pContext)
309 (void)pContext;
310 EnterCriticalSection(&_alMutex);
315 ProcessContext
317 Thread-safe exit
319 ALCvoid ProcessContext(ALCcontext *pContext)
321 (void)pContext;
322 LeaveCriticalSection(&_alMutex);
327 InitContext
329 Initialize Context variables
331 static ALvoid InitContext(ALCcontext *pContext)
333 int level;
335 //Initialise listener
336 pContext->Listener.Gain = 1.0f;
337 pContext->Listener.Position[0] = 0.0f;
338 pContext->Listener.Position[1] = 0.0f;
339 pContext->Listener.Position[2] = 0.0f;
340 pContext->Listener.Velocity[0] = 0.0f;
341 pContext->Listener.Velocity[1] = 0.0f;
342 pContext->Listener.Velocity[2] = 0.0f;
343 pContext->Listener.Forward[0] = 0.0f;
344 pContext->Listener.Forward[1] = 0.0f;
345 pContext->Listener.Forward[2] = -1.0f;
346 pContext->Listener.Up[0] = 0.0f;
347 pContext->Listener.Up[1] = 1.0f;
348 pContext->Listener.Up[2] = 0.0f;
350 //Validate pContext
351 pContext->LastError = AL_NO_ERROR;
352 pContext->InUse = AL_FALSE;
354 //Set output format
355 pContext->Frequency = pContext->Device->Frequency;
357 //Set globals
358 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
359 pContext->DopplerFactor = 1.0f;
360 pContext->DopplerVelocity = 1.0f;
361 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
363 pContext->lNumStereoSources = 1;
364 pContext->lNumMonoSources = pContext->Device->MaxNoOfSources - pContext->lNumStereoSources;
366 strcpy(pContext->ExtensionList, "AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_OFFSET");
368 level = GetConfigValueInt(NULL, "cf_level", 0);
369 if(level > 0 && level <= 6)
371 pContext->bs2b = calloc(1, sizeof(*pContext->bs2b));
372 bs2b_set_srate(pContext->bs2b, pContext->Frequency);
373 bs2b_set_level(pContext->bs2b, level);
379 ExitContext
381 Clean up Context, destroy any remaining Sources
383 static ALCvoid ExitContext(ALCcontext *pContext)
385 unsigned int i;
386 ALsource *ALSource;
387 ALsource *ALTempSource;
389 #ifdef _DEBUG
390 if (pContext->SourceCount>0)
391 AL_PRINT("alcDestroyContext() %d Source(s) NOT deleted\n", pContext->SourceCount);
392 #endif
394 // Free all the Sources still remaining
395 ALSource = pContext->Source;
396 for (i = 0; i < pContext->SourceCount; i++)
398 ALTempSource = ALSource->next;
399 ALTHUNK_REMOVEENTRY(ALSource->source);
400 memset(ALSource, 0, sizeof(ALsource));
401 free(ALSource);
402 ALSource = ALTempSource;
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 DeviceFound = ALC_TRUE;
456 break;
460 else
461 SetALCError(ALC_INVALID_VALUE);
463 if(!DeviceFound)
465 free(pDevice);
466 pDevice = NULL;
469 else
470 SetALCError(ALC_OUT_OF_MEMORY);
472 return pDevice;
475 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
477 ALCboolean bReturn = ALC_FALSE;
479 if ((pDevice)&&(pDevice->IsCaptureDevice))
481 ALCdevice_CloseCapture(pDevice);
482 free(pDevice);
484 bReturn = ALC_TRUE;
486 else
487 SetALCError(ALC_INVALID_DEVICE);
489 return bReturn;
492 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
494 if ((pDevice)&&(pDevice->IsCaptureDevice))
495 ALCdevice_StartCapture(pDevice);
496 else
497 SetALCError(ALC_INVALID_DEVICE);
500 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
502 if ((pDevice)&&(pDevice->IsCaptureDevice))
503 ALCdevice_StopCapture(pDevice);
504 else
505 SetALCError(ALC_INVALID_DEVICE);
508 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
510 if ((pDevice) && (pDevice->IsCaptureDevice))
511 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
512 else
513 SetALCError(ALC_INVALID_DEVICE);
517 alcGetError
519 Return last ALC generated error code
521 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
523 ALCenum errorCode;
525 (void)device;
527 errorCode = g_eLastContextError;
528 g_eLastContextError = ALC_NO_ERROR;
529 return errorCode;
534 alcSuspendContext
536 Not functional
538 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
540 // Not a lot happens here !
541 (void)pContext;
546 alcProcessContext
548 Not functional
550 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
552 // Not a lot happens here !
553 (void)pContext;
558 alcGetString
560 Returns information about the Device, and error strings
562 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
564 const ALCchar *value = NULL;
566 InitAL();
568 switch (param)
570 case ALC_NO_ERROR:
571 value = alcNoError;
572 break;
574 case ALC_INVALID_ENUM:
575 value = alcErrInvalidEnum;
576 break;
578 case ALC_INVALID_VALUE:
579 value = alcErrInvalidValue;
580 break;
582 case ALC_INVALID_DEVICE:
583 value = alcErrInvalidDevice;
584 break;
586 case ALC_INVALID_CONTEXT:
587 value = alcErrInvalidContext;
588 break;
590 case ALC_OUT_OF_MEMORY:
591 value = alcErrOutOfMemory;
592 break;
594 case ALC_DEFAULT_DEVICE_SPECIFIER:
595 value = alcDefaultDeviceSpecifier;
596 break;
598 case ALC_DEVICE_SPECIFIER:
599 if (pDevice)
600 value = pDevice->szDeviceName;
601 else
602 value = alcDeviceList;
603 break;
605 case ALC_ALL_DEVICES_SPECIFIER:
606 value = alcAllDeviceList;
607 break;
609 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
610 value = alcDefaultAllDeviceSpecifier;
611 break;
613 case ALC_CAPTURE_DEVICE_SPECIFIER:
614 if (pDevice)
615 value = pDevice->szDeviceName;
616 else
617 value = alcCaptureDeviceList;
618 break;
620 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
621 value = alcCaptureDefaultDeviceSpecifier;
622 break;
624 case ALC_EXTENSIONS:
625 value = alcExtensionList;
626 break;
628 default:
629 SetALCError(ALC_INVALID_ENUM);
630 break;
633 return value;
638 alcGetIntegerv
640 Returns information about the Device and the version of Open AL
642 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
644 InitAL();
646 if ((device)&&(device->IsCaptureDevice))
648 SuspendContext(NULL);
650 // Capture device
651 switch (param)
653 case ALC_CAPTURE_SAMPLES:
654 if ((size) && (data))
655 *data = ALCdevice_AvailableSamples(device);
656 else
657 SetALCError(ALC_INVALID_VALUE);
658 break;
660 default:
661 SetALCError(ALC_INVALID_ENUM);
662 break;
665 ProcessContext(NULL);
667 else
669 if(data)
671 // Playback Device
672 switch (param)
674 case ALC_MAJOR_VERSION:
675 if(!size)
676 SetALCError(ALC_INVALID_VALUE);
677 else
678 *data = alcMajorVersion;
679 break;
681 case ALC_MINOR_VERSION:
682 if(!size)
683 SetALCError(ALC_INVALID_VALUE);
684 else
685 *data = alcMinorVersion;
686 break;
688 case ALC_ATTRIBUTES_SIZE:
689 if(!device)
690 SetALCError(ALC_INVALID_DEVICE);
691 else if(!size)
692 SetALCError(ALC_INVALID_VALUE);
693 else
694 *data = 11;
695 break;
697 case ALC_ALL_ATTRIBUTES:
698 if(!device)
699 SetALCError(ALC_INVALID_DEVICE);
700 else if (size < 7)
701 SetALCError(ALC_INVALID_VALUE);
702 else
704 int i = 0;
706 data[i++] = ALC_FREQUENCY;
707 data[i++] = device->Frequency;
709 data[i++] = ALC_REFRESH;
710 data[i++] = device->UpdateFreq;
712 data[i++] = ALC_SYNC;
713 data[i++] = ALC_FALSE;
715 SuspendContext(NULL);
716 if(device->Context && size >= 11)
718 data[i++] = ALC_MONO_SOURCES;
719 data[i++] = device->Context->lNumMonoSources;
721 data[i++] = ALC_STEREO_SOURCES;
722 data[i++] = device->Context->lNumStereoSources;
724 ProcessContext(NULL);
726 data[i++] = 0;
728 break;
730 case ALC_FREQUENCY:
731 if(!device)
732 SetALCError(ALC_INVALID_DEVICE);
733 else if(!size)
734 SetALCError(ALC_INVALID_VALUE);
735 else
736 *data = device->Frequency;
737 break;
739 case ALC_REFRESH:
740 if(!device)
741 SetALCError(ALC_INVALID_DEVICE);
742 else if(!size)
743 SetALCError(ALC_INVALID_VALUE);
744 else
745 *data = device->UpdateFreq;
746 break;
748 case ALC_SYNC:
749 if(!device)
750 SetALCError(ALC_INVALID_DEVICE);
751 else if(!size)
752 SetALCError(ALC_INVALID_VALUE);
753 else
754 *data = ALC_FALSE;
755 break;
757 default:
758 SetALCError(ALC_INVALID_ENUM);
759 break;
762 else if(size)
763 SetALCError(ALC_INVALID_VALUE);
766 return;
771 alcIsExtensionPresent
773 Determines if there is support for a particular extension
775 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
777 ALCboolean bResult = ALC_FALSE;
778 ALsizei i = 0;
780 (void)device;
782 if (extName)
784 while(alcExtensions[i].extName &&
785 strcasecmp(alcExtensions[i].extName,extName) != 0)
786 i++;
788 if (alcExtensions[i].extName)
789 bResult = ALC_TRUE;
791 else
792 SetALCError(ALC_INVALID_VALUE);
794 return bResult;
799 alcGetProcAddress
801 Retrieves the function address for a particular extension function
803 ALCAPI ALCvoid * ALCAPIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
805 ALCvoid *pFunction = NULL;
806 ALsizei i = 0;
808 (void)device;
810 if (funcName)
812 while(alcFunctions[i].funcName &&
813 strcmp(alcFunctions[i].funcName,funcName) != 0)
814 i++;
815 pFunction = alcFunctions[i].address;
817 else
818 SetALCError(ALC_INVALID_VALUE);
820 return pFunction;
825 alcGetEnumValue
827 Get the value for a particular ALC Enumerated Value
829 ALCAPI ALCenum ALCAPIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
831 ALsizei i = 0;
832 ALCenum val;
834 (void)device;
836 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
837 i++;
838 val = enumeration[i].value;
840 if(!enumeration[i].enumName)
841 SetALCError(ALC_INVALID_VALUE);
843 return val;
848 alcCreateContext
850 Create and attach a Context to a particular Device.
852 ALCAPI ALCcontext* ALCAPIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
854 ALCcontext *ALContext = NULL;
855 ALuint ulAttributeIndex, ulRequestedStereoSources;
857 if ((device)&&(!device->IsCaptureDevice))
859 // Reset Context Last Error code
860 g_eLastContextError = ALC_NO_ERROR;
862 // Current implementation only allows one Context per Device
863 if(!device->Context)
865 ALContext = calloc(1, sizeof(ALCcontext));
866 if(!ALContext)
868 SetALCError(ALC_OUT_OF_MEMORY);
869 return NULL;
872 ALContext->Device = device;
873 InitContext(ALContext);
875 device->Context = ALContext;
877 SuspendContext(NULL);
879 ALContext->next = g_pContextList;
880 g_pContextList = ALContext;
881 g_ulContextCount++;
883 ProcessContext(NULL);
885 // Check for Voice Count attributes
886 if (attrList)
888 ulAttributeIndex = 0;
889 while ((ulAttributeIndex < 10) && (attrList[ulAttributeIndex]))
891 if (attrList[ulAttributeIndex] == ALC_STEREO_SOURCES)
893 ulRequestedStereoSources = attrList[ulAttributeIndex + 1];
895 if (ulRequestedStereoSources > ALContext->Device->MaxNoOfSources)
896 ulRequestedStereoSources = ALContext->Device->MaxNoOfSources;
898 ALContext->lNumStereoSources = ulRequestedStereoSources;
899 ALContext->lNumMonoSources = ALContext->Device->MaxNoOfSources - ALContext->lNumStereoSources;
900 break;
903 ulAttributeIndex += 2;
907 else
909 SetALCError(ALC_INVALID_VALUE);
910 ALContext = NULL;
913 else
914 SetALCError(ALC_INVALID_DEVICE);
916 return ALContext;
921 alcDestroyContext
923 Remove a Context
925 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
927 ALCcontext **list;
929 // Lock context list
930 SuspendContext(NULL);
932 if (IsContext(context))
934 // Lock context
935 SuspendContext(context);
937 context->Device->Context = NULL;
939 list = &g_pContextList;
940 while(*list != context)
941 list = &(*list)->next;
943 *list = (*list)->next;
944 g_ulContextCount--;
946 // Unlock context
947 ProcessContext(context);
949 ExitContext(context);
951 // Free memory (MUST do this after ProcessContext)
952 memset(context, 0, sizeof(ALCcontext));
953 free(context);
955 else
956 SetALCError(ALC_INVALID_CONTEXT);
958 ProcessContext(NULL);
963 alcGetCurrentContext
965 Returns the currently active Context
967 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
969 ALCcontext *pContext = NULL;
971 SuspendContext(NULL);
973 pContext = g_pContextList;
974 while ((pContext) && (!pContext->InUse))
975 pContext = pContext->next;
977 ProcessContext(NULL);
979 return pContext;
984 alcGetContextsDevice
986 Returns the Device that a particular Context is attached to
988 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
990 ALCdevice *pDevice = NULL;
992 SuspendContext(NULL);
993 if (IsContext(pContext))
994 pDevice = pContext->Device;
995 else
996 SetALCError(ALC_INVALID_CONTEXT);
997 ProcessContext(NULL);
999 return pDevice;
1004 alcMakeContextCurrent
1006 Makes the given Context the active Context
1008 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1010 ALCcontext *ALContext;
1011 ALboolean bReturn = AL_TRUE;
1013 SuspendContext(NULL);
1015 // context must be a valid Context or NULL
1016 if ((IsContext(context)) || (context == NULL))
1018 if ((ALContext=alcGetCurrentContext()))
1020 SuspendContext(ALContext);
1021 ALContext->InUse=AL_FALSE;
1022 ProcessContext(ALContext);
1025 if ((ALContext=context) && (ALContext->Device))
1027 SuspendContext(ALContext);
1028 ALContext->InUse=AL_TRUE;
1029 ProcessContext(ALContext);
1032 else
1034 SetALCError(ALC_INVALID_CONTEXT);
1035 bReturn = AL_FALSE;
1038 ProcessContext(NULL);
1040 return bReturn;
1045 alcOpenDevice
1047 Open the Device specified.
1049 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1051 ALboolean bDeviceFound = AL_FALSE;
1052 ALCdevice *device;
1053 ALint i;
1055 InitAL();
1057 if(deviceName && !deviceName[0])
1058 deviceName = NULL;
1060 device = malloc(sizeof(ALCdevice));
1061 if (device)
1063 const char *fmt;
1065 //Initialise device structure
1066 memset(device, 0, sizeof(ALCdevice));
1068 //Validate device
1069 device->InUse = AL_TRUE;
1070 device->IsCaptureDevice = AL_FALSE;
1072 //Set output format
1073 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1074 if((ALint)device->Frequency <= 0)
1075 device->Frequency = SWMIXER_OUTPUT_RATE;
1077 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1078 if(fmt[0])
1079 device->Format = alGetEnumValue(fmt);
1081 device->Channels = aluChannelsFromFormat(device->Format);
1082 if(!device->Channels)
1084 device->Format = AL_FORMAT_STEREO16;
1085 device->Channels = 2;
1086 device->FrameSize = 4;
1088 else
1089 device->FrameSize = aluBytesFromFormat(device->Format) *
1090 device->Channels;
1092 device->UpdateFreq = GetConfigValueInt(NULL, "refresh", 0);
1093 if((ALint)device->UpdateFreq <= 0)
1094 device->UpdateFreq = 8192 * device->Frequency / 22050;
1096 // Find a playback device to open
1097 for(i = 0;BackendList[i].Init;i++)
1099 device->Funcs = &BackendList[i].Funcs;
1100 if(ALCdevice_OpenPlayback(device, deviceName))
1102 bDeviceFound = AL_TRUE;
1103 break;
1107 if (!bDeviceFound)
1109 // No suitable output device found
1110 free(device);
1111 device = NULL;
1115 return device;
1120 alcCloseDevice
1122 Close the specified Device
1124 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1126 ALCboolean bReturn = ALC_FALSE;
1128 if ((pDevice)&&(!pDevice->IsCaptureDevice))
1130 ALCdevice_ClosePlayback(pDevice);
1132 //Release device structure
1133 memset(pDevice, 0, sizeof(ALCdevice));
1134 free(pDevice);
1136 bReturn = ALC_TRUE;
1138 else
1139 SetALCError(ALC_INVALID_DEVICE);
1141 return bReturn;
1143 ///////////////////////////////////////////////////////