Use both write pointers from the directsound buffer lock
[openal-soft.git] / Alc / ALc.c
blobbca519e277ae017cbf293e36b04483375becbc9a
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->Channels = aluChannelsFromFormat(format);
434 pDevice->FrameSize = aluBytesFromFormat(format) *
435 pDevice->Channels;
437 for(i = 0;BackendList[i].Init;i++)
439 pDevice->Funcs = &BackendList[i].Funcs;
440 if(ALCdevice_OpenCapture(pDevice, deviceName, frequency, format, SampleSize))
442 SuspendContext(NULL);
443 pDevice->next = g_pDeviceList;
444 g_pDeviceList = pDevice;
445 g_ulDeviceCount++;
446 ProcessContext(NULL);
448 DeviceFound = ALC_TRUE;
449 break;
453 else
454 SetALCError(ALC_INVALID_VALUE);
456 if(!DeviceFound)
458 free(pDevice);
459 pDevice = NULL;
462 else
463 SetALCError(ALC_OUT_OF_MEMORY);
465 return pDevice;
468 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
470 ALCboolean bReturn = ALC_FALSE;
471 ALCdevice **list;
473 if ((pDevice)&&(pDevice->IsCaptureDevice))
475 SuspendContext(NULL);
477 list = &g_pDeviceList;
478 while(*list != pDevice)
479 list = &(*list)->next;
481 *list = (*list)->next;
482 g_ulDeviceCount--;
484 ProcessContext(NULL);
486 ALCdevice_CloseCapture(pDevice);
487 free(pDevice);
489 bReturn = ALC_TRUE;
491 else
492 SetALCError(ALC_INVALID_DEVICE);
494 return bReturn;
497 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
499 if ((pDevice)&&(pDevice->IsCaptureDevice))
500 ALCdevice_StartCapture(pDevice);
501 else
502 SetALCError(ALC_INVALID_DEVICE);
505 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
507 if ((pDevice)&&(pDevice->IsCaptureDevice))
508 ALCdevice_StopCapture(pDevice);
509 else
510 SetALCError(ALC_INVALID_DEVICE);
513 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
515 if ((pDevice) && (pDevice->IsCaptureDevice))
516 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
517 else
518 SetALCError(ALC_INVALID_DEVICE);
522 alcGetError
524 Return last ALC generated error code
526 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
528 ALCenum errorCode;
530 (void)device;
532 errorCode = g_eLastContextError;
533 g_eLastContextError = ALC_NO_ERROR;
534 return errorCode;
539 alcSuspendContext
541 Not functional
543 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
545 // Not a lot happens here !
546 (void)pContext;
551 alcProcessContext
553 Not functional
555 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
557 // Not a lot happens here !
558 (void)pContext;
563 alcGetString
565 Returns information about the Device, and error strings
567 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
569 const ALCchar *value = NULL;
571 InitAL();
573 switch (param)
575 case ALC_NO_ERROR:
576 value = alcNoError;
577 break;
579 case ALC_INVALID_ENUM:
580 value = alcErrInvalidEnum;
581 break;
583 case ALC_INVALID_VALUE:
584 value = alcErrInvalidValue;
585 break;
587 case ALC_INVALID_DEVICE:
588 value = alcErrInvalidDevice;
589 break;
591 case ALC_INVALID_CONTEXT:
592 value = alcErrInvalidContext;
593 break;
595 case ALC_OUT_OF_MEMORY:
596 value = alcErrOutOfMemory;
597 break;
599 case ALC_DEFAULT_DEVICE_SPECIFIER:
600 value = alcDefaultDeviceSpecifier;
601 break;
603 case ALC_DEVICE_SPECIFIER:
604 if (pDevice)
605 value = pDevice->szDeviceName;
606 else
607 value = alcDeviceList;
608 break;
610 case ALC_ALL_DEVICES_SPECIFIER:
611 value = alcAllDeviceList;
612 break;
614 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
615 value = alcDefaultAllDeviceSpecifier;
616 break;
618 case ALC_CAPTURE_DEVICE_SPECIFIER:
619 if (pDevice)
620 value = pDevice->szDeviceName;
621 else
622 value = alcCaptureDeviceList;
623 break;
625 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
626 value = alcCaptureDefaultDeviceSpecifier;
627 break;
629 case ALC_EXTENSIONS:
630 value = alcExtensionList;
631 break;
633 default:
634 SetALCError(ALC_INVALID_ENUM);
635 break;
638 return value;
643 alcGetIntegerv
645 Returns information about the Device and the version of Open AL
647 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
649 InitAL();
651 if ((device)&&(device->IsCaptureDevice))
653 SuspendContext(NULL);
655 // Capture device
656 switch (param)
658 case ALC_CAPTURE_SAMPLES:
659 if ((size) && (data))
660 *data = ALCdevice_AvailableSamples(device);
661 else
662 SetALCError(ALC_INVALID_VALUE);
663 break;
665 default:
666 SetALCError(ALC_INVALID_ENUM);
667 break;
670 ProcessContext(NULL);
672 else
674 if(data)
676 // Playback Device
677 switch (param)
679 case ALC_MAJOR_VERSION:
680 if(!size)
681 SetALCError(ALC_INVALID_VALUE);
682 else
683 *data = alcMajorVersion;
684 break;
686 case ALC_MINOR_VERSION:
687 if(!size)
688 SetALCError(ALC_INVALID_VALUE);
689 else
690 *data = alcMinorVersion;
691 break;
693 case ALC_EFX_MAJOR_VERSION:
694 if(!size)
695 SetALCError(ALC_INVALID_VALUE);
696 else
697 *data = alcEFXMajorVersion;
698 break;
700 case ALC_EFX_MINOR_VERSION:
701 if(!size)
702 SetALCError(ALC_INVALID_VALUE);
703 else
704 *data = alcEFXMinorVersion;
705 break;
707 case ALC_MAX_AUXILIARY_SENDS:
708 if(!size)
709 SetALCError(ALC_INVALID_VALUE);
710 else
711 *data = MAX_SENDS;
712 break;
714 case ALC_ATTRIBUTES_SIZE:
715 if(!device)
716 SetALCError(ALC_INVALID_DEVICE);
717 else if(!size)
718 SetALCError(ALC_INVALID_VALUE);
719 else
720 *data = 12;
721 break;
723 case ALC_ALL_ATTRIBUTES:
724 if(!device)
725 SetALCError(ALC_INVALID_DEVICE);
726 else if (size < 7)
727 SetALCError(ALC_INVALID_VALUE);
728 else
730 int i = 0;
732 data[i++] = ALC_FREQUENCY;
733 data[i++] = device->Frequency;
735 data[i++] = ALC_REFRESH;
736 data[i++] = device->Frequency / device->UpdateFreq;
738 data[i++] = ALC_SYNC;
739 data[i++] = ALC_FALSE;
741 SuspendContext(NULL);
742 if(device->Context && size >= 12)
744 data[i++] = ALC_MONO_SOURCES;
745 data[i++] = device->Context->lNumMonoSources;
747 data[i++] = ALC_STEREO_SOURCES;
748 data[i++] = device->Context->lNumStereoSources;
750 data[i++] = ALC_MAX_AUXILIARY_SENDS;
751 data[i++] = MAX_SENDS;
753 ProcessContext(NULL);
755 data[i++] = 0;
757 break;
759 case ALC_FREQUENCY:
760 if(!device)
761 SetALCError(ALC_INVALID_DEVICE);
762 else if(!size)
763 SetALCError(ALC_INVALID_VALUE);
764 else
765 *data = device->Frequency;
766 break;
768 case ALC_REFRESH:
769 if(!device)
770 SetALCError(ALC_INVALID_DEVICE);
771 else if(!size)
772 SetALCError(ALC_INVALID_VALUE);
773 else
774 *data = device->Frequency / device->UpdateFreq;
775 break;
777 case ALC_SYNC:
778 if(!device)
779 SetALCError(ALC_INVALID_DEVICE);
780 else if(!size)
781 SetALCError(ALC_INVALID_VALUE);
782 else
783 *data = ALC_FALSE;
784 break;
786 case ALC_MONO_SOURCES:
787 if(!device || !device->Context)
788 SetALCError(ALC_INVALID_DEVICE);
789 else if (size != 1)
790 SetALCError(ALC_INVALID_VALUE);
791 else
792 *data = device->Context->lNumMonoSources;
793 break;
795 case ALC_STEREO_SOURCES:
796 if(!device || !device->Context)
797 SetALCError(ALC_INVALID_DEVICE);
798 else if (size != 1)
799 SetALCError(ALC_INVALID_VALUE);
800 else
801 *data = device->Context->lNumStereoSources;
802 break;
804 default:
805 SetALCError(ALC_INVALID_ENUM);
806 break;
809 else if(size)
810 SetALCError(ALC_INVALID_VALUE);
813 return;
818 alcIsExtensionPresent
820 Determines if there is support for a particular extension
822 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
824 ALCboolean bResult = ALC_FALSE;
826 (void)device;
828 if (extName)
830 const char *ptr;
831 size_t len;
833 len = strlen(extName);
834 ptr = alcExtensionList;
835 while(ptr && *ptr)
837 if(strncasecmp(ptr, extName, len) == 0 &&
838 (ptr[len] == '\0' || isspace(ptr[len])))
840 bResult = ALC_TRUE;
841 break;
843 if((ptr=strchr(ptr, ' ')) != NULL)
845 do {
846 ++ptr;
847 } while(isspace(*ptr));
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 ///////////////////////////////////////////////////////