Close dangling devices when exiting
[openal-soft.git] / Alc / ALc.c
blob3ae5c902de9926d3da8d95b210fa443c7c4d2960
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 "bs2b.h"
38 ///////////////////////////////////////////////////////
39 // DEBUG INFORMATION
41 char _alDebug[256];
43 ///////////////////////////////////////////////////////
46 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
47 struct {
48 const char *name;
49 void (*Init)(BackendFuncs*);
50 BackendFuncs Funcs;
51 } BackendList[] = {
52 #ifdef HAVE_ALSA
53 { "alsa", alc_alsa_init, EmptyFuncs },
54 #endif
55 #ifdef HAVE_OSS
56 { "oss", alc_oss_init, EmptyFuncs },
57 #endif
58 #ifdef HAVE_DSOUND
59 { "dsound", alcDSoundInit, EmptyFuncs },
60 #endif
61 #ifdef HAVE_WINMM
62 { "winmm", alcWinMMInit, EmptyFuncs },
63 #endif
65 { "wave", alc_wave_init, EmptyFuncs },
67 { NULL, NULL, EmptyFuncs }
69 #undef EmptyFuncs
71 ///////////////////////////////////////////////////////
73 #define ALC_EFX_MAJOR_VERSION 0x20001
74 #define ALC_EFX_MINOR_VERSION 0x20002
75 #define ALC_MAX_AUXILIARY_SENDS 0x20003
77 ///////////////////////////////////////////////////////
78 // STRING and EXTENSIONS
80 typedef struct ALCextension_struct
82 ALCchar *extName;
83 ALvoid *address;
84 } ALCextension;
86 typedef struct ALCfunction_struct
88 ALCchar *funcName;
89 ALvoid *address;
90 } ALCfunction;
92 static ALCextension alcExtensions[] = {
93 { "ALC_ENUMERATE_ALL_EXT", (ALvoid *) NULL },
94 { "ALC_ENUMERATION_EXT", (ALvoid *) NULL },
95 { "ALC_EXT_CAPTURE", (ALvoid *) NULL },
96 { "ALC_EXT_EFX", (ALvoid *) NULL },
97 { NULL, (ALvoid *) NULL }
100 static ALCfunction alcFunctions[] = {
101 { "alcCreateContext", (ALvoid *) alcCreateContext },
102 { "alcMakeContextCurrent", (ALvoid *) alcMakeContextCurrent },
103 { "alcProcessContext", (ALvoid *) alcProcessContext },
104 { "alcSuspendContext", (ALvoid *) alcSuspendContext },
105 { "alcDestroyContext", (ALvoid *) alcDestroyContext },
106 { "alcGetCurrentContext", (ALvoid *) alcGetCurrentContext },
107 { "alcGetContextsDevice", (ALvoid *) alcGetContextsDevice },
108 { "alcOpenDevice", (ALvoid *) alcOpenDevice },
109 { "alcCloseDevice", (ALvoid *) alcCloseDevice },
110 { "alcGetError", (ALvoid *) alcGetError },
111 { "alcIsExtensionPresent", (ALvoid *) alcIsExtensionPresent },
112 { "alcGetProcAddress", (ALvoid *) alcGetProcAddress },
113 { "alcGetEnumValue", (ALvoid *) alcGetEnumValue },
114 { "alcGetString", (ALvoid *) alcGetString },
115 { "alcGetIntegerv", (ALvoid *) alcGetIntegerv },
116 { "alcCaptureOpenDevice", (ALvoid *) alcCaptureOpenDevice },
117 { "alcCaptureCloseDevice", (ALvoid *) alcCaptureCloseDevice },
118 { "alcCaptureStart", (ALvoid *) alcCaptureStart },
119 { "alcCaptureStop", (ALvoid *) alcCaptureStop },
120 { "alcCaptureSamples", (ALvoid *) alcCaptureSamples },
121 { NULL, (ALvoid *) NULL }
124 static ALenums enumeration[]={
125 // Types
126 { (ALchar *)"ALC_INVALID", ALC_INVALID },
127 { (ALchar *)"ALC_FALSE", ALC_FALSE },
128 { (ALchar *)"ALC_TRUE", ALC_TRUE },
130 // ALC Properties
131 { (ALchar *)"ALC_MAJOR_VERSION", ALC_MAJOR_VERSION },
132 { (ALchar *)"ALC_MINOR_VERSION", ALC_MINOR_VERSION },
133 { (ALchar *)"ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE },
134 { (ALchar *)"ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES },
135 { (ALchar *)"ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER },
136 { (ALchar *)"ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER },
137 { (ALchar *)"ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER },
138 { (ALchar *)"ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER },
139 { (ALchar *)"ALC_EXTENSIONS", ALC_EXTENSIONS },
140 { (ALchar *)"ALC_FREQUENCY", ALC_FREQUENCY },
141 { (ALchar *)"ALC_REFRESH", ALC_REFRESH },
142 { (ALchar *)"ALC_SYNC", ALC_SYNC },
143 { (ALchar *)"ALC_MONO_SOURCES", ALC_MONO_SOURCES },
144 { (ALchar *)"ALC_STEREO_SOURCES", ALC_STEREO_SOURCES },
145 { (ALchar *)"ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER },
146 { (ALchar *)"ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER},
147 { (ALchar *)"ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES },
149 // EFX Properties
150 { (ALchar *)"ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION },
151 { (ALchar *)"ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION },
152 { (ALchar *)"ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS },
154 // ALC Error Message
155 { (ALchar *)"ALC_NO_ERROR", ALC_NO_ERROR },
156 { (ALchar *)"ALC_INVALID_DEVICE", ALC_INVALID_DEVICE },
157 { (ALchar *)"ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT },
158 { (ALchar *)"ALC_INVALID_ENUM", ALC_INVALID_ENUM },
159 { (ALchar *)"ALC_INVALID_VALUE", ALC_INVALID_VALUE },
160 { (ALchar *)"ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY },
161 { (ALchar *)NULL, (ALenum)0 }
163 // Error strings
164 static const ALCchar alcNoError[] = "No Error";
165 static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
166 static const ALCchar alcErrInvalidContext[] = "Invalid Context";
167 static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
168 static const ALCchar alcErrInvalidValue[] = "Invalid Value";
169 static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
171 // Context strings
172 static ALCchar alcDeviceList[2048];
173 static ALCchar alcAllDeviceList[2048];
174 static ALCchar alcCaptureDeviceList[2048];
175 // Default is always the first in the list
176 static ALCchar *alcDefaultDeviceSpecifier = alcDeviceList;
177 static ALCchar *alcDefaultAllDeviceSpecifier = alcAllDeviceList;
178 static ALCchar *alcCaptureDefaultDeviceSpecifier = alcCaptureDeviceList;
181 static ALCchar alcExtensionList[] = "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_EFX";
182 static ALCint alcMajorVersion = 1;
183 static ALCint alcMinorVersion = 1;
185 static ALCint alcEFXMajorVersion = 1;
186 static ALCint alcEFXMinorVersion = 0;
188 ///////////////////////////////////////////////////////
191 ///////////////////////////////////////////////////////
192 // Global Variables
194 static ALCdevice *g_pDeviceList = NULL;
195 static ALCuint g_ulDeviceCount = 0;
197 // Context List
198 static ALCcontext *g_pContextList = NULL;
199 static ALCuint g_ulContextCount = 0;
201 // Context Error
202 static ALCenum g_eLastContextError = ALC_NO_ERROR;
204 ///////////////////////////////////////////////////////
207 ///////////////////////////////////////////////////////
208 // ALC Related helper functions
210 static void InitAL(void)
212 static int done = 0;
213 if(!done)
215 int i;
216 const char *devs;
218 InitializeCriticalSection(&_alMutex);
219 ALTHUNK_INIT();
220 ReadALConfig();
222 devs = GetConfigValue(NULL, "drivers", "");
223 if(devs[0])
225 int n;
226 size_t len;
227 const char *next = devs;
229 i = 0;
231 do {
232 devs = next;
233 next = strchr(devs, ',');
235 if(!devs[0] || devs[0] == ',')
236 continue;
238 len = (next ? ((size_t)(next-devs)) : strlen(devs));
239 for(n = i;BackendList[n].Init;n++)
241 if(len == strlen(BackendList[n].name) &&
242 strncmp(BackendList[n].name, devs, len) == 0)
244 const char *name = BackendList[i].name;
245 void (*Init)(BackendFuncs*) = BackendList[i].Init;
247 BackendList[i].name = BackendList[n].name;
248 BackendList[i].Init = BackendList[n].Init;
250 BackendList[n].name = name;
251 BackendList[n].Init = Init;
253 i++;
256 } while(next++);
258 BackendList[i].name = NULL;
259 BackendList[i].Init = NULL;
262 for(i = 0;BackendList[i].Init;i++)
263 BackendList[i].Init(&BackendList[i].Funcs);
264 done = 1;
268 ALCchar *AppendDeviceList(char *name)
270 static int pos;
271 ALCchar *ret = alcDeviceList+pos;
272 pos += snprintf(alcDeviceList+pos, sizeof(alcDeviceList)-pos, "%s", name) + 1;
273 return ret;
276 ALCchar *AppendAllDeviceList(char *name)
278 static int pos;
279 ALCchar *ret = alcAllDeviceList+pos;
280 pos += snprintf(alcAllDeviceList+pos, sizeof(alcAllDeviceList)-pos, "%s", name) + 1;
281 return ret;
284 ALCchar *AppendCaptureDeviceList(char *name)
286 static int pos;
287 ALCchar *ret = alcCaptureDeviceList+pos;
288 pos += snprintf(alcCaptureDeviceList+pos, sizeof(alcCaptureDeviceList)-pos, "%s", name) + 1;
289 return ret;
293 IsContext
295 Check pContext is a valid Context pointer
297 static ALCboolean IsContext(ALCcontext *pContext)
299 ALCcontext *pTempContext;
301 pTempContext = g_pContextList;
302 while (pTempContext && pTempContext != pContext)
303 pTempContext = pTempContext->next;
305 return (pTempContext ? ALC_TRUE : ALC_FALSE);
310 SetALCError
312 Store latest ALC Error
314 ALCvoid SetALCError(ALenum errorCode)
316 g_eLastContextError = errorCode;
321 SuspendContext
323 Thread-safe entry
325 ALCvoid SuspendContext(ALCcontext *pContext)
327 (void)pContext;
328 EnterCriticalSection(&_alMutex);
333 ProcessContext
335 Thread-safe exit
337 ALCvoid ProcessContext(ALCcontext *pContext)
339 (void)pContext;
340 LeaveCriticalSection(&_alMutex);
345 InitContext
347 Initialize Context variables
349 static ALvoid InitContext(ALCcontext *pContext)
351 int level;
353 //Initialise listener
354 pContext->Listener.Gain = 1.0f;
355 pContext->Listener.MetersPerUnit = 1.0f;
356 pContext->Listener.Position[0] = 0.0f;
357 pContext->Listener.Position[1] = 0.0f;
358 pContext->Listener.Position[2] = 0.0f;
359 pContext->Listener.Velocity[0] = 0.0f;
360 pContext->Listener.Velocity[1] = 0.0f;
361 pContext->Listener.Velocity[2] = 0.0f;
362 pContext->Listener.Forward[0] = 0.0f;
363 pContext->Listener.Forward[1] = 0.0f;
364 pContext->Listener.Forward[2] = -1.0f;
365 pContext->Listener.Up[0] = 0.0f;
366 pContext->Listener.Up[1] = 1.0f;
367 pContext->Listener.Up[2] = 0.0f;
369 //Validate pContext
370 pContext->LastError = AL_NO_ERROR;
371 pContext->InUse = AL_FALSE;
373 //Set output format
374 pContext->Frequency = pContext->Device->Frequency;
376 //Set globals
377 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
378 pContext->DopplerFactor = 1.0f;
379 pContext->DopplerVelocity = 1.0f;
380 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
382 pContext->lNumStereoSources = 1;
383 pContext->lNumMonoSources = pContext->Device->MaxNoOfSources - pContext->lNumStereoSources;
385 strcpy(pContext->ExtensionList, "AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_OFFSET");
387 level = GetConfigValueInt(NULL, "cf_level", 0);
388 if(level > 0 && level <= 6)
390 pContext->bs2b = calloc(1, sizeof(*pContext->bs2b));
391 bs2b_set_srate(pContext->bs2b, pContext->Frequency);
392 bs2b_set_level(pContext->bs2b, level);
398 ExitContext
400 Clean up Context, destroy any remaining Sources
402 static ALCvoid ExitContext(ALCcontext *pContext)
404 unsigned int i;
405 ALsource *ALSource;
406 ALsource *ALTempSource;
408 #ifdef _DEBUG
409 if (pContext->SourceCount>0)
410 AL_PRINT("alcDestroyContext() %d Source(s) NOT deleted\n", pContext->SourceCount);
411 #endif
413 // Free all the Sources still remaining
414 ALSource = pContext->Source;
415 for (i = 0; i < pContext->SourceCount; i++)
417 ALTempSource = ALSource->next;
418 ALTHUNK_REMOVEENTRY(ALSource->source);
419 memset(ALSource, 0, sizeof(ALsource));
420 free(ALSource);
421 ALSource = ALTempSource;
424 //Invalidate context
425 pContext->LastError = AL_NO_ERROR;
426 pContext->InUse = AL_FALSE;
428 free(pContext->bs2b);
429 pContext->bs2b = NULL;
432 ///////////////////////////////////////////////////////
435 ///////////////////////////////////////////////////////
436 // ALC Functions calls
439 // This should probably move to another c file but for now ...
440 ALCAPI ALCdevice* ALCAPIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
442 ALCboolean DeviceFound = ALC_FALSE;
443 ALCdevice *pDevice = NULL;
444 ALCint i;
446 InitAL();
448 if(deviceName && !deviceName[0])
449 deviceName = NULL;
451 pDevice = malloc(sizeof(ALCdevice));
452 if (pDevice)
454 if (SampleSize > 0)
456 //Initialise device structure
457 memset(pDevice, 0, sizeof(ALCdevice));
459 //Validate device
460 pDevice->InUse = AL_TRUE;
461 pDevice->IsCaptureDevice = AL_TRUE;
463 pDevice->Frequency = frequency;
464 pDevice->Format = format;
465 pDevice->Channels = aluChannelsFromFormat(format);
466 pDevice->FrameSize = aluBytesFromFormat(format) *
467 pDevice->Channels;
469 for(i = 0;BackendList[i].Init;i++)
471 pDevice->Funcs = &BackendList[i].Funcs;
472 if(ALCdevice_OpenCapture(pDevice, deviceName, frequency, format, SampleSize))
474 SuspendContext(NULL);
475 pDevice->next = g_pDeviceList;
476 g_pDeviceList = pDevice;
477 g_ulDeviceCount++;
478 ProcessContext(NULL);
480 DeviceFound = ALC_TRUE;
481 break;
485 else
486 SetALCError(ALC_INVALID_VALUE);
488 if(!DeviceFound)
490 free(pDevice);
491 pDevice = NULL;
494 else
495 SetALCError(ALC_OUT_OF_MEMORY);
497 return pDevice;
500 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
502 ALCboolean bReturn = ALC_FALSE;
503 ALCdevice **list;
505 if ((pDevice)&&(pDevice->IsCaptureDevice))
507 SuspendContext(NULL);
509 list = &g_pDeviceList;
510 while(*list != pDevice)
511 list = &(*list)->next;
513 *list = (*list)->next;
514 g_ulDeviceCount--;
516 ProcessContext(NULL);
518 ALCdevice_CloseCapture(pDevice);
519 free(pDevice);
521 bReturn = ALC_TRUE;
523 else
524 SetALCError(ALC_INVALID_DEVICE);
526 return bReturn;
529 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
531 if ((pDevice)&&(pDevice->IsCaptureDevice))
532 ALCdevice_StartCapture(pDevice);
533 else
534 SetALCError(ALC_INVALID_DEVICE);
537 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
539 if ((pDevice)&&(pDevice->IsCaptureDevice))
540 ALCdevice_StopCapture(pDevice);
541 else
542 SetALCError(ALC_INVALID_DEVICE);
545 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
547 if ((pDevice) && (pDevice->IsCaptureDevice))
548 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
549 else
550 SetALCError(ALC_INVALID_DEVICE);
554 alcGetError
556 Return last ALC generated error code
558 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
560 ALCenum errorCode;
562 (void)device;
564 errorCode = g_eLastContextError;
565 g_eLastContextError = ALC_NO_ERROR;
566 return errorCode;
571 alcSuspendContext
573 Not functional
575 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
577 // Not a lot happens here !
578 (void)pContext;
583 alcProcessContext
585 Not functional
587 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
589 // Not a lot happens here !
590 (void)pContext;
595 alcGetString
597 Returns information about the Device, and error strings
599 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
601 const ALCchar *value = NULL;
603 InitAL();
605 switch (param)
607 case ALC_NO_ERROR:
608 value = alcNoError;
609 break;
611 case ALC_INVALID_ENUM:
612 value = alcErrInvalidEnum;
613 break;
615 case ALC_INVALID_VALUE:
616 value = alcErrInvalidValue;
617 break;
619 case ALC_INVALID_DEVICE:
620 value = alcErrInvalidDevice;
621 break;
623 case ALC_INVALID_CONTEXT:
624 value = alcErrInvalidContext;
625 break;
627 case ALC_OUT_OF_MEMORY:
628 value = alcErrOutOfMemory;
629 break;
631 case ALC_DEFAULT_DEVICE_SPECIFIER:
632 value = alcDefaultDeviceSpecifier;
633 break;
635 case ALC_DEVICE_SPECIFIER:
636 if (pDevice)
637 value = pDevice->szDeviceName;
638 else
639 value = alcDeviceList;
640 break;
642 case ALC_ALL_DEVICES_SPECIFIER:
643 value = alcAllDeviceList;
644 break;
646 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
647 value = alcDefaultAllDeviceSpecifier;
648 break;
650 case ALC_CAPTURE_DEVICE_SPECIFIER:
651 if (pDevice)
652 value = pDevice->szDeviceName;
653 else
654 value = alcCaptureDeviceList;
655 break;
657 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
658 value = alcCaptureDefaultDeviceSpecifier;
659 break;
661 case ALC_EXTENSIONS:
662 value = alcExtensionList;
663 break;
665 default:
666 SetALCError(ALC_INVALID_ENUM);
667 break;
670 return value;
675 alcGetIntegerv
677 Returns information about the Device and the version of Open AL
679 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
681 InitAL();
683 if ((device)&&(device->IsCaptureDevice))
685 SuspendContext(NULL);
687 // Capture device
688 switch (param)
690 case ALC_CAPTURE_SAMPLES:
691 if ((size) && (data))
692 *data = ALCdevice_AvailableSamples(device);
693 else
694 SetALCError(ALC_INVALID_VALUE);
695 break;
697 default:
698 SetALCError(ALC_INVALID_ENUM);
699 break;
702 ProcessContext(NULL);
704 else
706 if(data)
708 // Playback Device
709 switch (param)
711 case ALC_MAJOR_VERSION:
712 if(!size)
713 SetALCError(ALC_INVALID_VALUE);
714 else
715 *data = alcMajorVersion;
716 break;
718 case ALC_MINOR_VERSION:
719 if(!size)
720 SetALCError(ALC_INVALID_VALUE);
721 else
722 *data = alcMinorVersion;
723 break;
725 case ALC_EFX_MAJOR_VERSION:
726 if(!size)
727 SetALCError(ALC_INVALID_VALUE);
728 else
729 *data = alcEFXMajorVersion;
730 break;
732 case ALC_EFX_MINOR_VERSION:
733 if(!size)
734 SetALCError(ALC_INVALID_VALUE);
735 else
736 *data = alcEFXMinorVersion;
737 break;
739 case ALC_MAX_AUXILIARY_SENDS:
740 if(!size)
741 SetALCError(ALC_INVALID_VALUE);
742 else
743 *data = MAX_SENDS;
744 break;
746 case ALC_ATTRIBUTES_SIZE:
747 if(!device)
748 SetALCError(ALC_INVALID_DEVICE);
749 else if(!size)
750 SetALCError(ALC_INVALID_VALUE);
751 else
752 *data = 12;
753 break;
755 case ALC_ALL_ATTRIBUTES:
756 if(!device)
757 SetALCError(ALC_INVALID_DEVICE);
758 else if (size < 7)
759 SetALCError(ALC_INVALID_VALUE);
760 else
762 int i = 0;
764 data[i++] = ALC_FREQUENCY;
765 data[i++] = device->Frequency;
767 data[i++] = ALC_REFRESH;
768 data[i++] = device->Frequency / device->UpdateFreq;
770 data[i++] = ALC_SYNC;
771 data[i++] = ALC_FALSE;
773 SuspendContext(NULL);
774 if(device->Context && size >= 12)
776 data[i++] = ALC_MONO_SOURCES;
777 data[i++] = device->Context->lNumMonoSources;
779 data[i++] = ALC_STEREO_SOURCES;
780 data[i++] = device->Context->lNumStereoSources;
782 data[i++] = ALC_MAX_AUXILIARY_SENDS;
783 data[i++] = MAX_SENDS;
785 ProcessContext(NULL);
787 data[i++] = 0;
789 break;
791 case ALC_FREQUENCY:
792 if(!device)
793 SetALCError(ALC_INVALID_DEVICE);
794 else if(!size)
795 SetALCError(ALC_INVALID_VALUE);
796 else
797 *data = device->Frequency;
798 break;
800 case ALC_REFRESH:
801 if(!device)
802 SetALCError(ALC_INVALID_DEVICE);
803 else if(!size)
804 SetALCError(ALC_INVALID_VALUE);
805 else
806 *data = device->Frequency / device->UpdateFreq;
807 break;
809 case ALC_SYNC:
810 if(!device)
811 SetALCError(ALC_INVALID_DEVICE);
812 else if(!size)
813 SetALCError(ALC_INVALID_VALUE);
814 else
815 *data = ALC_FALSE;
816 break;
818 case ALC_MONO_SOURCES:
819 if(!device || !device->Context)
820 SetALCError(ALC_INVALID_DEVICE);
821 else if (size != 1)
822 SetALCError(ALC_INVALID_VALUE);
823 else
824 *data = device->Context->lNumMonoSources;
825 break;
827 case ALC_STEREO_SOURCES:
828 if(!device || !device->Context)
829 SetALCError(ALC_INVALID_DEVICE);
830 else if (size != 1)
831 SetALCError(ALC_INVALID_VALUE);
832 else
833 *data = device->Context->lNumStereoSources;
834 break;
836 default:
837 SetALCError(ALC_INVALID_ENUM);
838 break;
841 else if(size)
842 SetALCError(ALC_INVALID_VALUE);
845 return;
850 alcIsExtensionPresent
852 Determines if there is support for a particular extension
854 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
856 ALCboolean bResult = ALC_FALSE;
857 ALsizei i = 0;
859 (void)device;
861 if (extName)
863 while(alcExtensions[i].extName &&
864 strcasecmp(alcExtensions[i].extName,extName) != 0)
865 i++;
867 if (alcExtensions[i].extName)
868 bResult = ALC_TRUE;
870 else
871 SetALCError(ALC_INVALID_VALUE);
873 return bResult;
878 alcGetProcAddress
880 Retrieves the function address for a particular extension function
882 ALCAPI ALCvoid * ALCAPIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
884 ALCvoid *pFunction = NULL;
885 ALsizei i = 0;
887 (void)device;
889 if (funcName)
891 while(alcFunctions[i].funcName &&
892 strcmp(alcFunctions[i].funcName,funcName) != 0)
893 i++;
894 pFunction = alcFunctions[i].address;
896 else
897 SetALCError(ALC_INVALID_VALUE);
899 return pFunction;
904 alcGetEnumValue
906 Get the value for a particular ALC Enumerated Value
908 ALCAPI ALCenum ALCAPIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
910 ALsizei i = 0;
911 ALCenum val;
913 (void)device;
915 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
916 i++;
917 val = enumeration[i].value;
919 if(!enumeration[i].enumName)
920 SetALCError(ALC_INVALID_VALUE);
922 return val;
927 alcCreateContext
929 Create and attach a Context to a particular Device.
931 ALCAPI ALCcontext* ALCAPIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
933 ALCcontext *ALContext = NULL;
934 ALuint ulAttributeIndex, ulRequestedStereoSources;
936 if ((device)&&(!device->IsCaptureDevice))
938 // Reset Context Last Error code
939 g_eLastContextError = ALC_NO_ERROR;
941 // Current implementation only allows one Context per Device
942 if(!device->Context)
944 ALContext = calloc(1, sizeof(ALCcontext));
945 if(!ALContext)
947 SetALCError(ALC_OUT_OF_MEMORY);
948 return NULL;
951 ALContext->Device = device;
952 InitContext(ALContext);
954 device->Context = ALContext;
956 SuspendContext(NULL);
958 ALContext->next = g_pContextList;
959 g_pContextList = ALContext;
960 g_ulContextCount++;
962 ProcessContext(NULL);
964 // Check for Voice Count attributes
965 if (attrList)
967 ulAttributeIndex = 0;
968 while ((ulAttributeIndex < 10) && (attrList[ulAttributeIndex]))
970 if (attrList[ulAttributeIndex] == ALC_STEREO_SOURCES)
972 ulRequestedStereoSources = attrList[ulAttributeIndex + 1];
974 if (ulRequestedStereoSources > ALContext->Device->MaxNoOfSources)
975 ulRequestedStereoSources = ALContext->Device->MaxNoOfSources;
977 ALContext->lNumStereoSources = ulRequestedStereoSources;
978 ALContext->lNumMonoSources = ALContext->Device->MaxNoOfSources - ALContext->lNumStereoSources;
979 break;
982 ulAttributeIndex += 2;
986 else
988 SetALCError(ALC_INVALID_VALUE);
989 ALContext = NULL;
992 else
993 SetALCError(ALC_INVALID_DEVICE);
995 return ALContext;
1000 alcDestroyContext
1002 Remove a Context
1004 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
1006 ALCcontext **list;
1008 // Lock context list
1009 SuspendContext(NULL);
1011 if (IsContext(context))
1013 // Lock context
1014 SuspendContext(context);
1016 context->Device->Context = NULL;
1018 list = &g_pContextList;
1019 while(*list != context)
1020 list = &(*list)->next;
1022 *list = (*list)->next;
1023 g_ulContextCount--;
1025 // Unlock context
1026 ProcessContext(context);
1028 ExitContext(context);
1030 // Free memory (MUST do this after ProcessContext)
1031 memset(context, 0, sizeof(ALCcontext));
1032 free(context);
1034 else
1035 SetALCError(ALC_INVALID_CONTEXT);
1037 ProcessContext(NULL);
1042 alcGetCurrentContext
1044 Returns the currently active Context
1046 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1048 ALCcontext *pContext = NULL;
1050 SuspendContext(NULL);
1052 pContext = g_pContextList;
1053 while ((pContext) && (!pContext->InUse))
1054 pContext = pContext->next;
1056 ProcessContext(NULL);
1058 return pContext;
1063 alcGetContextsDevice
1065 Returns the Device that a particular Context is attached to
1067 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1069 ALCdevice *pDevice = NULL;
1071 SuspendContext(NULL);
1072 if (IsContext(pContext))
1073 pDevice = pContext->Device;
1074 else
1075 SetALCError(ALC_INVALID_CONTEXT);
1076 ProcessContext(NULL);
1078 return pDevice;
1083 alcMakeContextCurrent
1085 Makes the given Context the active Context
1087 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1089 ALCcontext *ALContext;
1090 ALboolean bReturn = AL_TRUE;
1092 SuspendContext(NULL);
1094 // context must be a valid Context or NULL
1095 if ((IsContext(context)) || (context == NULL))
1097 if ((ALContext=alcGetCurrentContext()))
1099 SuspendContext(ALContext);
1100 ALContext->InUse=AL_FALSE;
1101 ProcessContext(ALContext);
1104 if ((ALContext=context) && (ALContext->Device))
1106 SuspendContext(ALContext);
1107 ALContext->InUse=AL_TRUE;
1108 ProcessContext(ALContext);
1111 else
1113 SetALCError(ALC_INVALID_CONTEXT);
1114 bReturn = AL_FALSE;
1117 ProcessContext(NULL);
1119 return bReturn;
1124 alcOpenDevice
1126 Open the Device specified.
1128 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1130 ALboolean bDeviceFound = AL_FALSE;
1131 ALCdevice *device;
1132 ALint i;
1134 InitAL();
1136 if(deviceName && !deviceName[0])
1137 deviceName = NULL;
1139 device = malloc(sizeof(ALCdevice));
1140 if (device)
1142 const char *fmt;
1144 //Initialise device structure
1145 memset(device, 0, sizeof(ALCdevice));
1147 //Validate device
1148 device->InUse = AL_TRUE;
1149 device->IsCaptureDevice = AL_FALSE;
1151 //Set output format
1152 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1153 if((ALint)device->Frequency <= 0)
1154 device->Frequency = SWMIXER_OUTPUT_RATE;
1156 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1157 if(fmt[0])
1158 device->Format = alGetEnumValue(fmt);
1160 device->Channels = aluChannelsFromFormat(device->Format);
1161 if(!device->Channels)
1163 device->Format = AL_FORMAT_STEREO16;
1164 device->Channels = 2;
1165 device->FrameSize = 4;
1167 else
1168 device->FrameSize = aluBytesFromFormat(device->Format) *
1169 device->Channels;
1171 device->UpdateFreq = GetConfigValueInt(NULL, "refresh", 0);
1172 if((ALint)device->UpdateFreq <= 0)
1173 device->UpdateFreq = 8192 * device->Frequency / 22050;
1175 // Find a playback device to open
1176 for(i = 0;BackendList[i].Init;i++)
1178 device->Funcs = &BackendList[i].Funcs;
1179 if(ALCdevice_OpenPlayback(device, deviceName))
1181 SuspendContext(NULL);
1182 device->next = g_pDeviceList;
1183 g_pDeviceList = device;
1184 g_ulDeviceCount++;
1185 ProcessContext(NULL);
1187 bDeviceFound = AL_TRUE;
1188 break;
1192 if (!bDeviceFound)
1194 // No suitable output device found
1195 free(device);
1196 device = NULL;
1200 return device;
1205 alcCloseDevice
1207 Close the specified Device
1209 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1211 ALCboolean bReturn = ALC_FALSE;
1212 ALCdevice **list;
1214 if ((pDevice)&&(!pDevice->IsCaptureDevice))
1216 SuspendContext(NULL);
1218 list = &g_pDeviceList;
1219 while(*list != pDevice)
1220 list = &(*list)->next;
1222 *list = (*list)->next;
1223 g_ulDeviceCount--;
1225 ProcessContext(NULL);
1227 if(pDevice->Context)
1228 alcDestroyContext(pDevice->Context);
1229 ALCdevice_ClosePlayback(pDevice);
1231 //Release device structure
1232 memset(pDevice, 0, sizeof(ALCdevice));
1233 free(pDevice);
1235 bReturn = ALC_TRUE;
1237 else
1238 SetALCError(ALC_INVALID_DEVICE);
1240 return bReturn;
1244 ALCvoid ReleaseALC(ALCvoid)
1246 ALCdevice *Dev;
1248 #ifdef _DEBUG
1249 if(g_ulContextCount > 0)
1250 AL_PRINT("exit() %u device(s) and %u context(s) NOT deleted\n", g_ulDeviceCount, g_ulContextCount);
1251 #endif
1253 while(g_pDeviceList)
1255 Dev = g_pDeviceList;
1256 g_pDeviceList = g_pDeviceList->next;
1257 if(Dev->IsCaptureDevice)
1258 alcCaptureCloseDevice(Dev);
1259 else
1260 alcCloseDevice(Dev);
1264 ///////////////////////////////////////////////////////