Update queued buffer states when source looping is toggled
[openal-soft.git] / Alc / ALc.c
blobd9273cffcfbf7c822facd3e5e3a79fcbc7a46e45
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 #include "config.h"
23 #include <math.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <memory.h>
27 #include <ctype.h>
28 #include "alMain.h"
29 #include "alSource.h"
30 #include "AL/al.h"
31 #include "AL/alc.h"
32 #include "alThunk.h"
33 #include "alSource.h"
34 #include "alBuffer.h"
35 #include "alExtension.h"
36 #include "alAuxEffectSlot.h"
37 #include "bs2b.h"
38 #include "alu.h"
41 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
42 static struct {
43 const char *name;
44 void (*Init)(BackendFuncs*);
45 BackendFuncs Funcs;
46 } BackendList[] = {
47 #ifdef HAVE_ALSA
48 { "alsa", alc_alsa_init, EmptyFuncs },
49 #endif
50 #ifdef HAVE_OSS
51 { "oss", alc_oss_init, EmptyFuncs },
52 #endif
53 #ifdef HAVE_SOLARIS
54 { "solaris", alc_solaris_init, EmptyFuncs },
55 #endif
56 #ifdef HAVE_DSOUND
57 { "dsound", alcDSoundInit, EmptyFuncs },
58 #endif
59 #ifdef HAVE_WINMM
60 { "winmm", alcWinMMInit, EmptyFuncs },
61 #endif
62 #ifdef HAVE_PORTAUDIO
63 { "port", alc_pa_init, EmptyFuncs },
64 #endif
65 #ifdef HAVE_PULSEAUDIO
66 { "pulse", alc_pulse_init, EmptyFuncs },
67 #endif
69 { "wave", alc_wave_init, EmptyFuncs },
71 { NULL, NULL, EmptyFuncs }
73 #undef EmptyFuncs
75 ///////////////////////////////////////////////////////
77 #define ALC_EFX_MAJOR_VERSION 0x20001
78 #define ALC_EFX_MINOR_VERSION 0x20002
79 #define ALC_MAX_AUXILIARY_SENDS 0x20003
81 ///////////////////////////////////////////////////////
82 // STRING and EXTENSIONS
84 typedef struct ALCfunction_struct
86 ALCchar *funcName;
87 ALvoid *address;
88 } ALCfunction;
90 static ALCfunction alcFunctions[] = {
91 { "alcCreateContext", (ALvoid *) alcCreateContext },
92 { "alcMakeContextCurrent", (ALvoid *) alcMakeContextCurrent },
93 { "alcProcessContext", (ALvoid *) alcProcessContext },
94 { "alcSuspendContext", (ALvoid *) alcSuspendContext },
95 { "alcDestroyContext", (ALvoid *) alcDestroyContext },
96 { "alcGetCurrentContext", (ALvoid *) alcGetCurrentContext },
97 { "alcGetContextsDevice", (ALvoid *) alcGetContextsDevice },
98 { "alcOpenDevice", (ALvoid *) alcOpenDevice },
99 { "alcCloseDevice", (ALvoid *) alcCloseDevice },
100 { "alcGetError", (ALvoid *) alcGetError },
101 { "alcIsExtensionPresent", (ALvoid *) alcIsExtensionPresent },
102 { "alcGetProcAddress", (ALvoid *) alcGetProcAddress },
103 { "alcGetEnumValue", (ALvoid *) alcGetEnumValue },
104 { "alcGetString", (ALvoid *) alcGetString },
105 { "alcGetIntegerv", (ALvoid *) alcGetIntegerv },
106 { "alcCaptureOpenDevice", (ALvoid *) alcCaptureOpenDevice },
107 { "alcCaptureCloseDevice", (ALvoid *) alcCaptureCloseDevice },
108 { "alcCaptureStart", (ALvoid *) alcCaptureStart },
109 { "alcCaptureStop", (ALvoid *) alcCaptureStop },
110 { "alcCaptureSamples", (ALvoid *) alcCaptureSamples },
111 { NULL, (ALvoid *) NULL }
114 static ALenums enumeration[]={
115 // Types
116 { (ALchar *)"ALC_INVALID", ALC_INVALID },
117 { (ALchar *)"ALC_FALSE", ALC_FALSE },
118 { (ALchar *)"ALC_TRUE", ALC_TRUE },
120 // ALC Properties
121 { (ALchar *)"ALC_MAJOR_VERSION", ALC_MAJOR_VERSION },
122 { (ALchar *)"ALC_MINOR_VERSION", ALC_MINOR_VERSION },
123 { (ALchar *)"ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE },
124 { (ALchar *)"ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES },
125 { (ALchar *)"ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER },
126 { (ALchar *)"ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER },
127 { (ALchar *)"ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER },
128 { (ALchar *)"ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER },
129 { (ALchar *)"ALC_EXTENSIONS", ALC_EXTENSIONS },
130 { (ALchar *)"ALC_FREQUENCY", ALC_FREQUENCY },
131 { (ALchar *)"ALC_REFRESH", ALC_REFRESH },
132 { (ALchar *)"ALC_SYNC", ALC_SYNC },
133 { (ALchar *)"ALC_MONO_SOURCES", ALC_MONO_SOURCES },
134 { (ALchar *)"ALC_STEREO_SOURCES", ALC_STEREO_SOURCES },
135 { (ALchar *)"ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER },
136 { (ALchar *)"ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER},
137 { (ALchar *)"ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES },
139 // EFX Properties
140 { (ALchar *)"ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION },
141 { (ALchar *)"ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION },
142 { (ALchar *)"ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS },
144 // ALC Error Message
145 { (ALchar *)"ALC_NO_ERROR", ALC_NO_ERROR },
146 { (ALchar *)"ALC_INVALID_DEVICE", ALC_INVALID_DEVICE },
147 { (ALchar *)"ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT },
148 { (ALchar *)"ALC_INVALID_ENUM", ALC_INVALID_ENUM },
149 { (ALchar *)"ALC_INVALID_VALUE", ALC_INVALID_VALUE },
150 { (ALchar *)"ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY },
151 { (ALchar *)NULL, (ALenum)0 }
153 // Error strings
154 static const ALCchar alcNoError[] = "No Error";
155 static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
156 static const ALCchar alcErrInvalidContext[] = "Invalid Context";
157 static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
158 static const ALCchar alcErrInvalidValue[] = "Invalid Value";
159 static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
161 // Context strings
162 static ALCchar alcDeviceList[2048];
163 static ALCchar alcAllDeviceList[2048];
164 static ALCchar alcCaptureDeviceList[2048];
165 // Default is always the first in the list
166 static ALCchar *alcDefaultDeviceSpecifier = alcDeviceList;
167 static ALCchar *alcDefaultAllDeviceSpecifier = alcAllDeviceList;
168 static ALCchar *alcCaptureDefaultDeviceSpecifier = alcCaptureDeviceList;
171 static ALCchar alcExtensionList[] = "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_EFX";
172 static ALCint alcMajorVersion = 1;
173 static ALCint alcMinorVersion = 1;
175 static ALCint alcEFXMajorVersion = 1;
176 static ALCint alcEFXMinorVersion = 0;
178 ///////////////////////////////////////////////////////
181 ///////////////////////////////////////////////////////
182 // Global Variables
184 static ALCdevice *g_pDeviceList = NULL;
185 static ALCuint g_ulDeviceCount = 0;
187 static CRITICAL_SECTION g_csMutex;
189 // Context List
190 static ALCcontext *g_pContextList = NULL;
191 static ALCuint g_ulContextCount = 0;
193 // Context Error
194 static ALCenum g_eLastContextError = ALC_NO_ERROR;
196 static ALboolean init_done = AL_FALSE;
198 ///////////////////////////////////////////////////////
201 ///////////////////////////////////////////////////////
202 // ALC Related helper functions
203 #ifdef _WIN32
204 BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
206 (void)lpReserved;
208 // Perform actions based on the reason for calling.
209 switch(ul_reason_for_call)
211 case DLL_PROCESS_ATTACH:
212 DisableThreadLibraryCalls(hModule);
213 break;
215 case DLL_PROCESS_DETACH:
216 if(!init_done)
217 break;
218 ReleaseALC();
219 ReleaseALBuffers();
220 ReleaseALEffects();
221 ReleaseALFilters();
222 FreeALConfig();
223 ALTHUNK_EXIT();
224 DeleteCriticalSection(&g_csMutex);
225 break;
227 return TRUE;
229 #else
230 #ifdef HAVE_GCC_DESTRUCTOR
231 static void my_deinit() __attribute__((destructor));
232 static void my_deinit()
234 static ALenum once = AL_FALSE;
235 if(once || !init_done) return;
236 once = AL_TRUE;
238 ReleaseALC();
239 ReleaseALBuffers();
240 ReleaseALEffects();
241 ReleaseALFilters();
242 FreeALConfig();
243 ALTHUNK_EXIT();
244 DeleteCriticalSection(&g_csMutex);
246 #endif
247 #endif
249 static void InitAL(void)
251 if(!init_done)
253 int i;
254 const char *devs, *str;
256 init_done = AL_TRUE;
258 InitializeCriticalSection(&g_csMutex);
259 ALTHUNK_INIT();
260 ReadALConfig();
262 devs = GetConfigValue(NULL, "drivers", "");
263 if(devs[0])
265 int n;
266 size_t len;
267 const char *next = devs;
269 i = 0;
271 do {
272 devs = next;
273 next = strchr(devs, ',');
275 if(!devs[0] || devs[0] == ',')
276 continue;
278 len = (next ? ((size_t)(next-devs)) : strlen(devs));
279 for(n = i;BackendList[n].Init;n++)
281 if(len == strlen(BackendList[n].name) &&
282 strncmp(BackendList[n].name, devs, len) == 0)
284 const char *name = BackendList[i].name;
285 void (*Init)(BackendFuncs*) = BackendList[i].Init;
287 BackendList[i].name = BackendList[n].name;
288 BackendList[i].Init = BackendList[n].Init;
290 BackendList[n].name = name;
291 BackendList[n].Init = Init;
293 i++;
296 } while(next++);
298 BackendList[i].name = NULL;
299 BackendList[i].Init = NULL;
302 for(i = 0;BackendList[i].Init;i++)
303 BackendList[i].Init(&BackendList[i].Funcs);
305 str = GetConfigValue(NULL, "stereodup", "false");
306 DuplicateStereo = (strcasecmp(str, "true") == 0 ||
307 strcasecmp(str, "yes") == 0 ||
308 strcasecmp(str, "on") == 0 ||
309 atoi(str) != 0);
311 str = GetConfigValue(NULL, "excludefx", "");
312 if(str[0])
314 const struct {
315 const char *name;
316 int type;
317 } EffectList[] = {
318 { "eaxreverb", EAXREVERB },
319 { "reverb", REVERB },
320 { "echo", ECHO },
321 { NULL, 0 }
323 int n;
324 size_t len;
325 const char *next = str;
327 do {
328 str = next;
329 next = strchr(str, ',');
331 if(!str[0] || next == str)
332 continue;
334 len = (next ? ((size_t)(next-str)) : strlen(str));
335 for(n = 0;EffectList[n].name;n++)
337 if(len == strlen(EffectList[n].name) &&
338 strncmp(EffectList[n].name, str, len) == 0)
339 DisabledEffects[EffectList[n].type] = AL_TRUE;
341 } while(next++);
346 ALCchar *AppendDeviceList(char *name)
348 static size_t pos;
349 ALCchar *ret = alcDeviceList+pos;
350 if(pos >= sizeof(alcDeviceList))
352 AL_PRINT("Not enough room to add %s!\n", name);
353 return alcDeviceList + sizeof(alcDeviceList) - 1;
355 pos += snprintf(alcDeviceList+pos, sizeof(alcDeviceList)-pos-1, "%s", name) + 1;
356 return ret;
359 ALCchar *AppendAllDeviceList(char *name)
361 static size_t pos;
362 ALCchar *ret = alcAllDeviceList+pos;
363 if(pos >= sizeof(alcAllDeviceList))
365 AL_PRINT("Not enough room to add %s!\n", name);
366 return alcAllDeviceList + sizeof(alcAllDeviceList) - 1;
368 pos += snprintf(alcAllDeviceList+pos, sizeof(alcAllDeviceList)-pos-1, "%s", name) + 1;
369 return ret;
372 ALCchar *AppendCaptureDeviceList(char *name)
374 static size_t pos;
375 ALCchar *ret = alcCaptureDeviceList+pos;
376 if(pos >= sizeof(alcCaptureDeviceList))
378 AL_PRINT("Not enough room to add %s!\n", name);
379 return alcCaptureDeviceList + sizeof(alcCaptureDeviceList) - 1;
381 pos += snprintf(alcCaptureDeviceList+pos, sizeof(alcCaptureDeviceList)-pos-1, "%s", name) + 1;
382 return ret;
386 IsContext
388 Check pContext is a valid Context pointer
390 static ALCboolean IsContext(ALCcontext *pContext)
392 ALCcontext *pTempContext;
394 pTempContext = g_pContextList;
395 while (pTempContext && pTempContext != pContext)
396 pTempContext = pTempContext->next;
398 return (pTempContext ? ALC_TRUE : ALC_FALSE);
403 SetALCError
405 Store latest ALC Error
407 ALCvoid SetALCError(ALenum errorCode)
409 g_eLastContextError = errorCode;
414 SuspendContext
416 Thread-safe entry
418 ALCvoid SuspendContext(ALCcontext *pContext)
420 (void)pContext;
421 EnterCriticalSection(&g_csMutex);
426 ProcessContext
428 Thread-safe exit
430 ALCvoid ProcessContext(ALCcontext *pContext)
432 (void)pContext;
433 LeaveCriticalSection(&g_csMutex);
438 InitContext
440 Initialize Context variables
442 static ALvoid InitContext(ALCcontext *pContext)
444 int level;
446 //Initialise listener
447 pContext->Listener.Gain = 1.0f;
448 pContext->Listener.MetersPerUnit = 1.0f;
449 pContext->Listener.Position[0] = 0.0f;
450 pContext->Listener.Position[1] = 0.0f;
451 pContext->Listener.Position[2] = 0.0f;
452 pContext->Listener.Velocity[0] = 0.0f;
453 pContext->Listener.Velocity[1] = 0.0f;
454 pContext->Listener.Velocity[2] = 0.0f;
455 pContext->Listener.Forward[0] = 0.0f;
456 pContext->Listener.Forward[1] = 0.0f;
457 pContext->Listener.Forward[2] = -1.0f;
458 pContext->Listener.Up[0] = 0.0f;
459 pContext->Listener.Up[1] = 1.0f;
460 pContext->Listener.Up[2] = 0.0f;
462 //Validate pContext
463 pContext->LastError = AL_NO_ERROR;
464 pContext->InUse = AL_FALSE;
466 //Set output format
467 pContext->Frequency = pContext->Device->Frequency;
469 //Set globals
470 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
471 pContext->DopplerFactor = 1.0f;
472 pContext->DopplerVelocity = 1.0f;
473 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
475 pContext->ExtensionList = "AL_EXTX_buffer_sub_data AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_OFFSET AL_EXTX_source_distance_model AL_LOKI_quadriphonic";
477 level = GetConfigValueInt(NULL, "cf_level", 0);
478 if(level > 0 && level <= 6)
480 pContext->bs2b = calloc(1, sizeof(*pContext->bs2b));
481 bs2b_set_srate(pContext->bs2b, pContext->Frequency);
482 bs2b_set_level(pContext->bs2b, level);
485 aluInitPanning(pContext);
490 ExitContext
492 Clean up Context, destroy any remaining Sources
494 static ALCvoid ExitContext(ALCcontext *pContext)
496 //Invalidate context
497 pContext->LastError = AL_NO_ERROR;
498 pContext->InUse = AL_FALSE;
500 free(pContext->bs2b);
501 pContext->bs2b = NULL;
504 ///////////////////////////////////////////////////////
507 ///////////////////////////////////////////////////////
508 // ALC Functions calls
511 // This should probably move to another c file but for now ...
512 ALCAPI ALCdevice* ALCAPIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
514 ALCboolean DeviceFound = ALC_FALSE;
515 ALCdevice *pDevice = NULL;
516 ALCint i;
518 InitAL();
520 if(SampleSize <= 0)
522 SetALCError(ALC_INVALID_VALUE);
523 return NULL;
526 if(deviceName && !deviceName[0])
527 deviceName = NULL;
529 pDevice = malloc(sizeof(ALCdevice));
530 if (pDevice)
532 //Initialise device structure
533 memset(pDevice, 0, sizeof(ALCdevice));
535 //Validate device
536 pDevice->IsCaptureDevice = AL_TRUE;
538 pDevice->Frequency = frequency;
539 pDevice->Format = format;
541 for(i = 0;BackendList[i].Init;i++)
543 pDevice->Funcs = &BackendList[i].Funcs;
544 if(ALCdevice_OpenCapture(pDevice, deviceName, frequency, format, SampleSize))
546 SuspendContext(NULL);
547 pDevice->next = g_pDeviceList;
548 g_pDeviceList = pDevice;
549 g_ulDeviceCount++;
550 ProcessContext(NULL);
552 DeviceFound = ALC_TRUE;
553 break;
557 if(!DeviceFound)
559 SetALCError(ALC_INVALID_VALUE);
560 free(pDevice);
561 pDevice = NULL;
564 else
565 SetALCError(ALC_OUT_OF_MEMORY);
567 return pDevice;
570 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
572 ALCboolean bReturn = ALC_FALSE;
573 ALCdevice **list;
575 if ((pDevice)&&(pDevice->IsCaptureDevice))
577 SuspendContext(NULL);
579 list = &g_pDeviceList;
580 while(*list != pDevice)
581 list = &(*list)->next;
583 *list = (*list)->next;
584 g_ulDeviceCount--;
586 ProcessContext(NULL);
588 ALCdevice_CloseCapture(pDevice);
589 free(pDevice);
591 bReturn = ALC_TRUE;
593 else
594 SetALCError(ALC_INVALID_DEVICE);
596 return bReturn;
599 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
601 if ((pDevice)&&(pDevice->IsCaptureDevice))
602 ALCdevice_StartCapture(pDevice);
603 else
604 SetALCError(ALC_INVALID_DEVICE);
607 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
609 if ((pDevice)&&(pDevice->IsCaptureDevice))
610 ALCdevice_StopCapture(pDevice);
611 else
612 SetALCError(ALC_INVALID_DEVICE);
615 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
617 if ((pDevice) && (pDevice->IsCaptureDevice))
618 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
619 else
620 SetALCError(ALC_INVALID_DEVICE);
624 alcGetError
626 Return last ALC generated error code
628 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
630 ALCenum errorCode;
632 (void)device;
634 errorCode = g_eLastContextError;
635 g_eLastContextError = ALC_NO_ERROR;
636 return errorCode;
641 alcSuspendContext
643 Not functional
645 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
647 // Not a lot happens here !
648 (void)pContext;
653 alcProcessContext
655 Not functional
657 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
659 // Not a lot happens here !
660 (void)pContext;
665 alcGetString
667 Returns information about the Device, and error strings
669 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
671 const ALCchar *value = NULL;
673 InitAL();
675 switch (param)
677 case ALC_NO_ERROR:
678 value = alcNoError;
679 break;
681 case ALC_INVALID_ENUM:
682 value = alcErrInvalidEnum;
683 break;
685 case ALC_INVALID_VALUE:
686 value = alcErrInvalidValue;
687 break;
689 case ALC_INVALID_DEVICE:
690 value = alcErrInvalidDevice;
691 break;
693 case ALC_INVALID_CONTEXT:
694 value = alcErrInvalidContext;
695 break;
697 case ALC_OUT_OF_MEMORY:
698 value = alcErrOutOfMemory;
699 break;
701 case ALC_DEFAULT_DEVICE_SPECIFIER:
702 value = alcDefaultDeviceSpecifier;
703 break;
705 case ALC_DEVICE_SPECIFIER:
706 if (pDevice)
707 value = pDevice->szDeviceName;
708 else
709 value = alcDeviceList;
710 break;
712 case ALC_ALL_DEVICES_SPECIFIER:
713 value = alcAllDeviceList;
714 break;
716 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
717 value = alcDefaultAllDeviceSpecifier;
718 break;
720 case ALC_CAPTURE_DEVICE_SPECIFIER:
721 if (pDevice)
722 value = pDevice->szDeviceName;
723 else
724 value = alcCaptureDeviceList;
725 break;
727 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
728 value = alcCaptureDefaultDeviceSpecifier;
729 break;
731 case ALC_EXTENSIONS:
732 value = alcExtensionList;
733 break;
735 default:
736 SetALCError(ALC_INVALID_ENUM);
737 break;
740 return value;
745 alcGetIntegerv
747 Returns information about the Device and the version of Open AL
749 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
751 InitAL();
753 if ((device)&&(device->IsCaptureDevice))
755 SuspendContext(NULL);
757 // Capture device
758 switch (param)
760 case ALC_CAPTURE_SAMPLES:
761 if ((size) && (data))
762 *data = ALCdevice_AvailableSamples(device);
763 else
764 SetALCError(ALC_INVALID_VALUE);
765 break;
767 default:
768 SetALCError(ALC_INVALID_ENUM);
769 break;
772 ProcessContext(NULL);
774 else
776 if(data)
778 // Playback Device
779 switch (param)
781 case ALC_MAJOR_VERSION:
782 if(!size)
783 SetALCError(ALC_INVALID_VALUE);
784 else
785 *data = alcMajorVersion;
786 break;
788 case ALC_MINOR_VERSION:
789 if(!size)
790 SetALCError(ALC_INVALID_VALUE);
791 else
792 *data = alcMinorVersion;
793 break;
795 case ALC_EFX_MAJOR_VERSION:
796 if(!size)
797 SetALCError(ALC_INVALID_VALUE);
798 else
799 *data = alcEFXMajorVersion;
800 break;
802 case ALC_EFX_MINOR_VERSION:
803 if(!size)
804 SetALCError(ALC_INVALID_VALUE);
805 else
806 *data = alcEFXMinorVersion;
807 break;
809 case ALC_MAX_AUXILIARY_SENDS:
810 if(!size)
811 SetALCError(ALC_INVALID_VALUE);
812 else
813 *data = (device?device->NumAuxSends:MAX_SENDS);
814 break;
816 case ALC_ATTRIBUTES_SIZE:
817 if(!device)
818 SetALCError(ALC_INVALID_DEVICE);
819 else if(!size)
820 SetALCError(ALC_INVALID_VALUE);
821 else
822 *data = 13;
823 break;
825 case ALC_ALL_ATTRIBUTES:
826 if(!device)
827 SetALCError(ALC_INVALID_DEVICE);
828 else if (size < 13)
829 SetALCError(ALC_INVALID_VALUE);
830 else
832 int i = 0;
834 SuspendContext(NULL);
835 data[i++] = ALC_FREQUENCY;
836 data[i++] = device->Frequency;
838 data[i++] = ALC_REFRESH;
839 data[i++] = device->Frequency / device->UpdateSize;
841 data[i++] = ALC_SYNC;
842 data[i++] = ALC_FALSE;
844 data[i++] = ALC_MONO_SOURCES;
845 data[i++] = device->lNumMonoSources;
847 data[i++] = ALC_STEREO_SOURCES;
848 data[i++] = device->lNumStereoSources;
850 data[i++] = ALC_MAX_AUXILIARY_SENDS;
851 data[i++] = device->NumAuxSends;
853 data[i++] = 0;
854 ProcessContext(NULL);
856 break;
858 case ALC_FREQUENCY:
859 if(!device)
860 SetALCError(ALC_INVALID_DEVICE);
861 else if(!size)
862 SetALCError(ALC_INVALID_VALUE);
863 else
864 *data = device->Frequency;
865 break;
867 case ALC_REFRESH:
868 if(!device)
869 SetALCError(ALC_INVALID_DEVICE);
870 else if(!size)
871 SetALCError(ALC_INVALID_VALUE);
872 else
873 *data = device->Frequency / device->UpdateSize;
874 break;
876 case ALC_SYNC:
877 if(!device)
878 SetALCError(ALC_INVALID_DEVICE);
879 else if(!size)
880 SetALCError(ALC_INVALID_VALUE);
881 else
882 *data = ALC_FALSE;
883 break;
885 case ALC_MONO_SOURCES:
886 if(!device || !device->Context)
887 SetALCError(ALC_INVALID_DEVICE);
888 else if (size != 1)
889 SetALCError(ALC_INVALID_VALUE);
890 else
891 *data = device->lNumMonoSources;
892 break;
894 case ALC_STEREO_SOURCES:
895 if(!device || !device->Context)
896 SetALCError(ALC_INVALID_DEVICE);
897 else if (size != 1)
898 SetALCError(ALC_INVALID_VALUE);
899 else
900 *data = device->lNumStereoSources;
901 break;
903 default:
904 SetALCError(ALC_INVALID_ENUM);
905 break;
908 else if(size)
909 SetALCError(ALC_INVALID_VALUE);
912 return;
917 alcIsExtensionPresent
919 Determines if there is support for a particular extension
921 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
923 ALCboolean bResult = ALC_FALSE;
925 (void)device;
927 if (extName)
929 const char *ptr;
930 size_t len;
932 len = strlen(extName);
933 ptr = alcExtensionList;
934 while(ptr && *ptr)
936 if(strncasecmp(ptr, extName, len) == 0 &&
937 (ptr[len] == '\0' || isspace(ptr[len])))
939 bResult = ALC_TRUE;
940 break;
942 if((ptr=strchr(ptr, ' ')) != NULL)
944 do {
945 ++ptr;
946 } while(isspace(*ptr));
950 else
951 SetALCError(ALC_INVALID_VALUE);
953 return bResult;
958 alcGetProcAddress
960 Retrieves the function address for a particular extension function
962 ALCAPI ALCvoid * ALCAPIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
964 ALCvoid *pFunction = NULL;
965 ALsizei i = 0;
967 (void)device;
969 if (funcName)
971 while(alcFunctions[i].funcName &&
972 strcmp(alcFunctions[i].funcName,funcName) != 0)
973 i++;
974 pFunction = alcFunctions[i].address;
976 else
977 SetALCError(ALC_INVALID_VALUE);
979 return pFunction;
984 alcGetEnumValue
986 Get the value for a particular ALC Enumerated Value
988 ALCAPI ALCenum ALCAPIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
990 ALsizei i = 0;
991 ALCenum val;
993 (void)device;
995 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
996 i++;
997 val = enumeration[i].value;
999 if(!enumeration[i].enumName)
1000 SetALCError(ALC_INVALID_VALUE);
1002 return val;
1007 alcCreateContext
1009 Create and attach a Context to a particular Device.
1011 ALCAPI ALCcontext* ALCAPIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
1013 ALCcontext *ALContext = NULL;
1014 ALuint ulAttributeIndex, ulRequestedStereoSources;
1015 ALuint RequestedSends;
1017 if ((device)&&(!device->IsCaptureDevice))
1019 // Reset Context Last Error code
1020 g_eLastContextError = ALC_NO_ERROR;
1022 // Current implementation only allows one Context per Device
1023 if(!device->Context)
1025 ALContext = calloc(1, sizeof(ALCcontext));
1026 if(!ALContext)
1028 SetALCError(ALC_OUT_OF_MEMORY);
1029 return NULL;
1032 ALContext->Device = device;
1033 InitContext(ALContext);
1035 device->Context = ALContext;
1037 SuspendContext(NULL);
1039 ALContext->next = g_pContextList;
1040 g_pContextList = ALContext;
1041 g_ulContextCount++;
1043 ProcessContext(NULL);
1045 // Check for attributes
1046 if (attrList)
1048 ALCint numMono = ALContext->Device->lNumMonoSources;
1049 ALCint numStereo = ALContext->Device->lNumStereoSources;
1050 ALCuint numSends = ALContext->Device->NumAuxSends;
1052 ulAttributeIndex = 0;
1053 while ((ulAttributeIndex < 10) && (attrList[ulAttributeIndex]))
1055 if (attrList[ulAttributeIndex] == ALC_STEREO_SOURCES)
1057 ulRequestedStereoSources = attrList[ulAttributeIndex + 1];
1059 if (ulRequestedStereoSources > ALContext->Device->MaxNoOfSources)
1060 ulRequestedStereoSources = ALContext->Device->MaxNoOfSources;
1062 numStereo = ulRequestedStereoSources;
1063 numMono = ALContext->Device->MaxNoOfSources - numStereo;
1066 if(attrList[ulAttributeIndex] == ALC_MAX_AUXILIARY_SENDS)
1068 RequestedSends = attrList[ulAttributeIndex + 1];
1070 if(RequestedSends > ALContext->Device->NumAuxSends)
1071 RequestedSends = ALContext->Device->NumAuxSends;
1073 numSends = RequestedSends;
1076 ulAttributeIndex += 2;
1079 ALContext->Device->lNumMonoSources = numMono;
1080 ALContext->Device->lNumStereoSources = numStereo;
1081 ALContext->Device->NumAuxSends = numSends;
1084 else
1086 SetALCError(ALC_INVALID_VALUE);
1087 ALContext = NULL;
1090 else
1091 SetALCError(ALC_INVALID_DEVICE);
1093 return ALContext;
1098 alcDestroyContext
1100 Remove a Context
1102 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
1104 ALCcontext **list;
1106 InitAL();
1108 // Lock context list
1109 SuspendContext(NULL);
1111 if (IsContext(context))
1113 // Lock context
1114 SuspendContext(context);
1116 ReleaseALSources(context);
1117 ReleaseALAuxiliaryEffectSlots(context);
1119 context->Device->Context = NULL;
1121 list = &g_pContextList;
1122 while(*list != context)
1123 list = &(*list)->next;
1125 *list = (*list)->next;
1126 g_ulContextCount--;
1128 // Unlock context
1129 ProcessContext(context);
1131 ExitContext(context);
1133 // Free memory (MUST do this after ProcessContext)
1134 memset(context, 0, sizeof(ALCcontext));
1135 free(context);
1137 else
1138 SetALCError(ALC_INVALID_CONTEXT);
1140 ProcessContext(NULL);
1145 alcGetCurrentContext
1147 Returns the currently active Context
1149 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1151 ALCcontext *pContext = NULL;
1153 InitAL();
1155 SuspendContext(NULL);
1157 pContext = g_pContextList;
1158 while ((pContext) && (!pContext->InUse))
1159 pContext = pContext->next;
1161 ProcessContext(NULL);
1163 return pContext;
1168 alcGetContextsDevice
1170 Returns the Device that a particular Context is attached to
1172 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1174 ALCdevice *pDevice = NULL;
1176 InitAL();
1178 SuspendContext(NULL);
1179 if (IsContext(pContext))
1180 pDevice = pContext->Device;
1181 else
1182 SetALCError(ALC_INVALID_CONTEXT);
1183 ProcessContext(NULL);
1185 return pDevice;
1190 alcMakeContextCurrent
1192 Makes the given Context the active Context
1194 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1196 ALCcontext *ALContext;
1197 ALboolean bReturn = AL_TRUE;
1199 InitAL();
1201 SuspendContext(NULL);
1203 // context must be a valid Context or NULL
1204 if ((IsContext(context)) || (context == NULL))
1206 if ((ALContext=alcGetCurrentContext()))
1208 SuspendContext(ALContext);
1209 ALContext->InUse=AL_FALSE;
1210 ProcessContext(ALContext);
1213 if ((ALContext=context) && (ALContext->Device))
1215 SuspendContext(ALContext);
1216 ALContext->InUse=AL_TRUE;
1217 ProcessContext(ALContext);
1220 else
1222 SetALCError(ALC_INVALID_CONTEXT);
1223 bReturn = AL_FALSE;
1226 ProcessContext(NULL);
1228 return bReturn;
1233 alcOpenDevice
1235 Open the Device specified.
1237 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1239 ALboolean bDeviceFound = AL_FALSE;
1240 ALCdevice *device;
1241 ALint i;
1243 InitAL();
1245 if(deviceName && !deviceName[0])
1246 deviceName = NULL;
1248 device = malloc(sizeof(ALCdevice));
1249 if (device)
1251 const char *fmt;
1253 //Initialise device structure
1254 memset(device, 0, sizeof(ALCdevice));
1256 //Validate device
1257 device->IsCaptureDevice = AL_FALSE;
1259 //Set output format
1260 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1261 if((ALint)device->Frequency <= 0)
1262 device->Frequency = SWMIXER_OUTPUT_RATE;
1264 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1265 if(fmt[0])
1266 device->Format = alGetEnumValue(fmt);
1268 if(!aluChannelsFromFormat(device->Format))
1269 device->Format = AL_FORMAT_STEREO16;
1271 device->UpdateSize = GetConfigValueInt(NULL, "refresh", 4096);
1272 if((ALint)device->UpdateSize <= 0)
1273 device->UpdateSize = 4096;
1275 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1276 if((ALint)device->MaxNoOfSources <= 0)
1277 device->MaxNoOfSources = 256;
1279 device->AuxiliaryEffectSlotMax = GetConfigValueInt(NULL, "slots", 4);
1280 if((ALint)device->AuxiliaryEffectSlotMax <= 0)
1281 device->AuxiliaryEffectSlotMax = 4;
1283 device->lNumStereoSources = 1;
1284 device->lNumMonoSources = device->MaxNoOfSources - device->lNumStereoSources;
1286 device->NumAuxSends = GetConfigValueInt(NULL, "sends", MAX_SENDS);
1287 if(device->NumAuxSends > MAX_SENDS)
1288 device->NumAuxSends = MAX_SENDS;
1290 // Find a playback device to open
1291 SuspendContext(NULL);
1292 for(i = 0;BackendList[i].Init;i++)
1294 device->Funcs = &BackendList[i].Funcs;
1295 if(ALCdevice_OpenPlayback(device, deviceName))
1297 device->next = g_pDeviceList;
1298 g_pDeviceList = device;
1299 g_ulDeviceCount++;
1301 bDeviceFound = AL_TRUE;
1302 break;
1305 ProcessContext(NULL);
1307 if (!bDeviceFound)
1309 // No suitable output device found
1310 SetALCError(ALC_INVALID_VALUE);
1311 free(device);
1312 device = NULL;
1315 else
1316 SetALCError(ALC_OUT_OF_MEMORY);
1318 return device;
1323 alcCloseDevice
1325 Close the specified Device
1327 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1329 ALCboolean bReturn = ALC_FALSE;
1330 ALCdevice **list;
1332 if ((pDevice)&&(!pDevice->IsCaptureDevice))
1334 SuspendContext(NULL);
1336 list = &g_pDeviceList;
1337 while(*list != pDevice)
1338 list = &(*list)->next;
1340 *list = (*list)->next;
1341 g_ulDeviceCount--;
1343 ProcessContext(NULL);
1345 if(pDevice->Context)
1347 #ifdef _DEBUG
1348 AL_PRINT("alcCloseDevice(): destroying 1 Context\n");
1349 #endif
1350 alcDestroyContext(pDevice->Context);
1352 ALCdevice_ClosePlayback(pDevice);
1354 //Release device structure
1355 memset(pDevice, 0, sizeof(ALCdevice));
1356 free(pDevice);
1358 bReturn = ALC_TRUE;
1360 else
1361 SetALCError(ALC_INVALID_DEVICE);
1363 return bReturn;
1367 ALCvoid ReleaseALC(ALCvoid)
1369 #ifdef _DEBUG
1370 if(g_ulDeviceCount > 0)
1371 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
1372 #endif
1374 while(g_pDeviceList)
1376 if(g_pDeviceList->IsCaptureDevice)
1377 alcCaptureCloseDevice(g_pDeviceList);
1378 else
1379 alcCloseDevice(g_pDeviceList);
1383 ///////////////////////////////////////////////////////