Don't force latency adjustment with PulseAudio
[openal-soft.git] / Alc / ALc.c
blob13acaa3a9a603598c50a555d67b8a9f7f480544f
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 "alDatabuffer.h"
38 #include "bs2b.h"
39 #include "alu.h"
42 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
43 typedef struct BackendInfo {
44 const char *name;
45 void (*Init)(BackendFuncs*);
46 void (*Deinit)(void);
47 void (*Probe)(int);
48 BackendFuncs Funcs;
49 } BackendInfo;
50 static BackendInfo BackendList[] = {
51 #ifdef HAVE_ALSA
52 { "alsa", alc_alsa_init, alc_alsa_deinit, alc_alsa_probe, EmptyFuncs },
53 #endif
54 #ifdef HAVE_OSS
55 { "oss", alc_oss_init, alc_oss_deinit, alc_oss_probe, EmptyFuncs },
56 #endif
57 #ifdef HAVE_SOLARIS
58 { "solaris", alc_solaris_init, alc_solaris_deinit, alc_solaris_probe, EmptyFuncs },
59 #endif
60 #ifdef HAVE_DSOUND
61 { "dsound", alcDSoundInit, alcDSoundDeinit, alcDSoundProbe, EmptyFuncs },
62 #endif
63 #ifdef HAVE_WINMM
64 { "winmm", alcWinMMInit, alcWinMMDeinit, alcWinMMProbe, EmptyFuncs },
65 #endif
66 #ifdef HAVE_PORTAUDIO
67 { "port", alc_pa_init, alc_pa_deinit, alc_pa_probe, EmptyFuncs },
68 #endif
69 #ifdef HAVE_PULSEAUDIO
70 { "pulse", alc_pulse_init, alc_pulse_deinit, alc_pulse_probe, EmptyFuncs },
71 #endif
73 { "wave", alc_wave_init, alc_wave_deinit, alc_wave_probe, EmptyFuncs },
75 { NULL, NULL, NULL, NULL, EmptyFuncs }
77 #undef EmptyFuncs
79 ///////////////////////////////////////////////////////
81 #define ALC_EFX_MAJOR_VERSION 0x20001
82 #define ALC_EFX_MINOR_VERSION 0x20002
83 #define ALC_MAX_AUXILIARY_SENDS 0x20003
85 ///////////////////////////////////////////////////////
86 // STRING and EXTENSIONS
88 typedef struct ALCfunction_struct
90 ALCchar *funcName;
91 ALvoid *address;
92 } ALCfunction;
94 static ALCfunction alcFunctions[] = {
95 { "alcCreateContext", (ALvoid *) alcCreateContext },
96 { "alcMakeContextCurrent", (ALvoid *) alcMakeContextCurrent },
97 { "alcProcessContext", (ALvoid *) alcProcessContext },
98 { "alcSuspendContext", (ALvoid *) alcSuspendContext },
99 { "alcDestroyContext", (ALvoid *) alcDestroyContext },
100 { "alcGetCurrentContext", (ALvoid *) alcGetCurrentContext },
101 { "alcGetContextsDevice", (ALvoid *) alcGetContextsDevice },
102 { "alcOpenDevice", (ALvoid *) alcOpenDevice },
103 { "alcCloseDevice", (ALvoid *) alcCloseDevice },
104 { "alcGetError", (ALvoid *) alcGetError },
105 { "alcIsExtensionPresent", (ALvoid *) alcIsExtensionPresent },
106 { "alcGetProcAddress", (ALvoid *) alcGetProcAddress },
107 { "alcGetEnumValue", (ALvoid *) alcGetEnumValue },
108 { "alcGetString", (ALvoid *) alcGetString },
109 { "alcGetIntegerv", (ALvoid *) alcGetIntegerv },
110 { "alcCaptureOpenDevice", (ALvoid *) alcCaptureOpenDevice },
111 { "alcCaptureCloseDevice", (ALvoid *) alcCaptureCloseDevice },
112 { "alcCaptureStart", (ALvoid *) alcCaptureStart },
113 { "alcCaptureStop", (ALvoid *) alcCaptureStop },
114 { "alcCaptureSamples", (ALvoid *) alcCaptureSamples },
116 { "alcMakeCurrent", (ALvoid *) alcMakeCurrent },
117 { "alcGetThreadContext", (ALvoid *) alcGetThreadContext },
119 { NULL, (ALvoid *) NULL }
122 static ALenums enumeration[]={
123 // Types
124 { (ALchar *)"ALC_INVALID", ALC_INVALID },
125 { (ALchar *)"ALC_FALSE", ALC_FALSE },
126 { (ALchar *)"ALC_TRUE", ALC_TRUE },
128 // ALC Properties
129 { (ALchar *)"ALC_MAJOR_VERSION", ALC_MAJOR_VERSION },
130 { (ALchar *)"ALC_MINOR_VERSION", ALC_MINOR_VERSION },
131 { (ALchar *)"ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE },
132 { (ALchar *)"ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES },
133 { (ALchar *)"ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER },
134 { (ALchar *)"ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER },
135 { (ALchar *)"ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER },
136 { (ALchar *)"ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER },
137 { (ALchar *)"ALC_EXTENSIONS", ALC_EXTENSIONS },
138 { (ALchar *)"ALC_FREQUENCY", ALC_FREQUENCY },
139 { (ALchar *)"ALC_REFRESH", ALC_REFRESH },
140 { (ALchar *)"ALC_SYNC", ALC_SYNC },
141 { (ALchar *)"ALC_MONO_SOURCES", ALC_MONO_SOURCES },
142 { (ALchar *)"ALC_STEREO_SOURCES", ALC_STEREO_SOURCES },
143 { (ALchar *)"ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER },
144 { (ALchar *)"ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER},
145 { (ALchar *)"ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES },
147 // EFX Properties
148 { (ALchar *)"ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION },
149 { (ALchar *)"ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION },
150 { (ALchar *)"ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS },
152 // ALC Error Message
153 { (ALchar *)"ALC_NO_ERROR", ALC_NO_ERROR },
154 { (ALchar *)"ALC_INVALID_DEVICE", ALC_INVALID_DEVICE },
155 { (ALchar *)"ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT },
156 { (ALchar *)"ALC_INVALID_ENUM", ALC_INVALID_ENUM },
157 { (ALchar *)"ALC_INVALID_VALUE", ALC_INVALID_VALUE },
158 { (ALchar *)"ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY },
159 { (ALchar *)NULL, (ALenum)0 }
161 // Error strings
162 static const ALCchar alcNoError[] = "No Error";
163 static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
164 static const ALCchar alcErrInvalidContext[] = "Invalid Context";
165 static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
166 static const ALCchar alcErrInvalidValue[] = "Invalid Value";
167 static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
169 /* Device lists. Sizes only include the first ending null character, not the
170 * second */
171 static ALCchar *alcDeviceList;
172 static ALCuint alcDeviceListSize;
173 static ALCchar *alcAllDeviceList;
174 static ALCuint alcAllDeviceListSize;
175 static ALCchar *alcCaptureDeviceList;
176 static ALCuint alcCaptureDeviceListSize;
177 // Default is always the first in the list
178 static ALCchar *alcDefaultDeviceSpecifier;
179 static ALCchar *alcDefaultAllDeviceSpecifier;
180 static ALCchar *alcCaptureDefaultDeviceSpecifier;
183 static ALCchar alcExtensionList[] = "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_disconnect ALC_EXT_EFX ALC_EXTX_thread_local_context";
184 static ALCint alcMajorVersion = 1;
185 static ALCint alcMinorVersion = 1;
187 static ALCint alcEFXMajorVersion = 1;
188 static ALCint alcEFXMinorVersion = 0;
190 ///////////////////////////////////////////////////////
193 ///////////////////////////////////////////////////////
194 // Global Variables
196 static ALCdevice *g_pDeviceList = NULL;
197 static ALCuint g_ulDeviceCount = 0;
199 static CRITICAL_SECTION g_csMutex;
201 // Context List
202 static ALCcontext *g_pContextList = NULL;
203 static ALCuint g_ulContextCount = 0;
205 // Thread-local current context
206 static tls_type LocalContext;
208 // Context Error
209 static ALCenum g_eLastContextError = ALC_NO_ERROR;
211 // Mixing Priority Level
212 ALint RTPrioLevel;
214 ///////////////////////////////////////////////////////
217 ///////////////////////////////////////////////////////
218 // ALC Related helper functions
219 #ifdef _WIN32
220 static void alc_init(void);
221 static void alc_deinit(void);
223 BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
225 (void)lpReserved;
227 // Perform actions based on the reason for calling.
228 switch(ul_reason_for_call)
230 case DLL_PROCESS_ATTACH:
231 DisableThreadLibraryCalls(hModule);
232 alc_init();
233 break;
235 case DLL_PROCESS_DETACH:
236 alc_deinit();
237 break;
239 return TRUE;
241 #else
242 #ifdef HAVE_GCC_DESTRUCTOR
243 static void alc_init(void) __attribute__((constructor));
244 static void alc_deinit(void) __attribute__((destructor));
245 #endif
246 #endif
248 static void alc_init(void)
250 int i;
251 const char *devs, *str;
253 InitializeCriticalSection(&g_csMutex);
254 ALTHUNK_INIT();
255 ReadALConfig();
257 tls_create(&LocalContext);
259 RTPrioLevel = GetConfigValueInt(NULL, "rt-prio", 0);
261 devs = GetConfigValue(NULL, "drivers", "");
262 if(devs[0])
264 int n;
265 size_t len;
266 const char *next = devs;
267 int endlist;
269 i = 0;
270 do {
271 devs = next;
272 next = strchr(devs, ',');
274 if(!devs[0] || devs[0] == ',')
276 endlist = 0;
277 continue;
279 endlist = 1;
281 len = (next ? ((size_t)(next-devs)) : strlen(devs));
282 for(n = i;BackendList[n].Init;n++)
284 if(len == strlen(BackendList[n].name) &&
285 strncmp(BackendList[n].name, devs, len) == 0)
287 BackendInfo Bkp = BackendList[n];
288 while(n > i)
290 BackendList[n] = BackendList[n-1];
291 --n;
293 BackendList[n] = Bkp;
295 i++;
296 break;
299 } while(next++);
301 if(endlist)
303 BackendList[i].name = NULL;
304 BackendList[i].Init = NULL;
305 BackendList[i].Deinit = NULL;
306 BackendList[i].Probe = NULL;
310 for(i = 0;BackendList[i].Init;i++)
312 BackendList[i].Init(&BackendList[i].Funcs);
314 BackendList[i].Probe(DEVICE_PROBE);
315 BackendList[i].Probe(ALL_DEVICE_PROBE);
316 BackendList[i].Probe(CAPTURE_DEVICE_PROBE);
319 DuplicateStereo = GetConfigValueBool(NULL, "stereodup", 0);
321 str = GetConfigValue(NULL, "excludefx", "");
322 if(str[0])
324 const struct {
325 const char *name;
326 int type;
327 } EffectList[] = {
328 { "eaxreverb", EAXREVERB },
329 { "reverb", REVERB },
330 { "echo", ECHO },
331 { NULL, 0 }
333 int n;
334 size_t len;
335 const char *next = str;
337 do {
338 str = next;
339 next = strchr(str, ',');
341 if(!str[0] || next == str)
342 continue;
344 len = (next ? ((size_t)(next-str)) : strlen(str));
345 for(n = 0;EffectList[n].name;n++)
347 if(len == strlen(EffectList[n].name) &&
348 strncmp(EffectList[n].name, str, len) == 0)
349 DisabledEffects[EffectList[n].type] = AL_TRUE;
351 } while(next++);
355 static void alc_deinit(void)
357 int i;
359 ReleaseALC();
361 for(i = 0;BackendList[i].Deinit;i++)
362 BackendList[i].Deinit();
364 tls_delete(LocalContext);
366 FreeALConfig();
367 ALTHUNK_EXIT();
368 DeleteCriticalSection(&g_csMutex);
372 static void ProbeDeviceList()
374 ALint i;
376 free(alcDeviceList); alcDeviceList = NULL;
377 alcDeviceListSize = 0;
379 for(i = 0;BackendList[i].Probe;i++)
380 BackendList[i].Probe(DEVICE_PROBE);
383 static void ProbeAllDeviceList()
385 ALint i;
387 free(alcAllDeviceList); alcAllDeviceList = NULL;
388 alcAllDeviceListSize = 0;
390 for(i = 0;BackendList[i].Probe;i++)
391 BackendList[i].Probe(ALL_DEVICE_PROBE);
394 static void ProbeCaptureDeviceList()
396 ALint i;
398 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
399 alcCaptureDeviceListSize = 0;
401 for(i = 0;BackendList[i].Probe;i++)
402 BackendList[i].Probe(CAPTURE_DEVICE_PROBE);
406 #define DECL_APPEND_LIST_FUNC(type) \
407 void Append##type##List(const ALCchar *name) \
409 ALCuint len = strlen(name); \
410 void *temp; \
412 if(len == 0) \
413 return; \
415 temp = realloc(alc##type##List, alc##type##ListSize + len + 2); \
416 if(!temp) \
418 AL_PRINT("Realloc failed to add %s!\n", name); \
419 return; \
421 alc##type##List = temp; \
422 sprintf(alc##type##List+alc##type##ListSize, "%s", name); \
423 alc##type##ListSize += len+1; \
424 alc##type##List[alc##type##ListSize] = 0; \
427 DECL_APPEND_LIST_FUNC(Device)
428 DECL_APPEND_LIST_FUNC(AllDevice)
429 DECL_APPEND_LIST_FUNC(CaptureDevice)
432 void EnableRTPrio(ALint level)
434 ALboolean failed;
436 #ifdef _WIN32
437 if(level > 0)
438 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
439 else
440 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
441 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM)
442 struct sched_param param;
444 if(level > 0)
446 /* Use the minimum real-time priority possible for now (on Linux this
447 * should be 1 for SCHED_RR) */
448 param.sched_priority = sched_get_priority_min(SCHED_RR);
449 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
451 else
453 param.sched_priority = 0;
454 failed = !!pthread_setschedparam(pthread_self(), SCHED_OTHER, &param);
456 #else
457 /* Real-time priority not available */
458 failed = !!level;
459 #endif
460 if(failed)
461 AL_PRINT("Failed to set priority level for thread\n");
466 IsDevice
468 Check pDevice is a valid Device pointer
470 static ALCboolean IsDevice(ALCdevice *pDevice)
472 ALCdevice *pTempDevice;
474 SuspendContext(NULL);
476 pTempDevice = g_pDeviceList;
477 while(pTempDevice && pTempDevice != pDevice)
478 pTempDevice = pTempDevice->next;
480 ProcessContext(NULL);
482 return (pTempDevice ? ALC_TRUE : ALC_FALSE);
486 IsContext
488 Check pContext is a valid Context pointer
490 static ALCboolean IsContext(ALCcontext *pContext)
492 ALCcontext *pTempContext;
494 SuspendContext(NULL);
496 pTempContext = g_pContextList;
497 while (pTempContext && pTempContext != pContext)
498 pTempContext = pTempContext->next;
500 ProcessContext(NULL);
502 return (pTempContext ? ALC_TRUE : ALC_FALSE);
507 alcSetError
509 Store latest ALC Error
511 ALCvoid alcSetError(ALenum errorCode)
513 g_eLastContextError = errorCode;
518 SuspendContext
520 Thread-safe entry
522 ALCvoid SuspendContext(ALCcontext *pContext)
524 (void)pContext;
525 EnterCriticalSection(&g_csMutex);
530 ProcessContext
532 Thread-safe exit
534 ALCvoid ProcessContext(ALCcontext *pContext)
536 (void)pContext;
537 LeaveCriticalSection(&g_csMutex);
542 GetContextSuspended
544 Returns the currently active Context, in a locked state
546 ALCcontext *GetContextSuspended(void)
548 ALCcontext *pContext = NULL;
550 SuspendContext(NULL);
552 pContext = tls_get(LocalContext);
553 if(pContext && !IsContext(pContext))
555 tls_set(LocalContext, NULL);
556 pContext = NULL;
558 if(!pContext)
560 pContext = g_pContextList;
561 while(pContext && !pContext->InUse)
562 pContext = pContext->next;
564 if(pContext)
565 SuspendContext(pContext);
567 ProcessContext(NULL);
569 return pContext;
574 InitContext
576 Initialize Context variables
578 static ALvoid InitContext(ALCcontext *pContext)
580 //Initialise listener
581 pContext->Listener.Gain = 1.0f;
582 pContext->Listener.MetersPerUnit = 1.0f;
583 pContext->Listener.Position[0] = 0.0f;
584 pContext->Listener.Position[1] = 0.0f;
585 pContext->Listener.Position[2] = 0.0f;
586 pContext->Listener.Velocity[0] = 0.0f;
587 pContext->Listener.Velocity[1] = 0.0f;
588 pContext->Listener.Velocity[2] = 0.0f;
589 pContext->Listener.Forward[0] = 0.0f;
590 pContext->Listener.Forward[1] = 0.0f;
591 pContext->Listener.Forward[2] = -1.0f;
592 pContext->Listener.Up[0] = 0.0f;
593 pContext->Listener.Up[1] = 1.0f;
594 pContext->Listener.Up[2] = 0.0f;
596 //Validate pContext
597 pContext->LastError = AL_NO_ERROR;
598 pContext->InUse = AL_FALSE;
599 pContext->Suspended = AL_FALSE;
601 //Set globals
602 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
603 pContext->SourceDistanceModel = AL_FALSE;
604 pContext->DopplerFactor = 1.0f;
605 pContext->DopplerVelocity = 1.0f;
606 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
608 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_sample_buffer_object AL_EXTX_source_distance_model AL_LOKI_quadriphonic";
610 aluInitPanning(pContext);
615 ExitContext
617 Clean up Context, destroy any remaining Sources
619 static ALCvoid ExitContext(ALCcontext *pContext)
621 //Invalidate context
622 pContext->LastError = AL_NO_ERROR;
623 pContext->InUse = AL_FALSE;
626 ///////////////////////////////////////////////////////
629 ///////////////////////////////////////////////////////
630 // ALC Functions calls
633 // This should probably move to another c file but for now ...
634 ALCAPI ALCdevice* ALCAPIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
636 ALCboolean DeviceFound = ALC_FALSE;
637 ALCdevice *pDevice = NULL;
638 ALCint i;
640 if(SampleSize <= 0)
642 alcSetError(ALC_INVALID_VALUE);
643 return NULL;
646 if(deviceName && !deviceName[0])
647 deviceName = NULL;
649 pDevice = malloc(sizeof(ALCdevice));
650 if (pDevice)
652 //Initialise device structure
653 memset(pDevice, 0, sizeof(ALCdevice));
655 //Validate device
656 pDevice->Connected = ALC_TRUE;
657 pDevice->IsCaptureDevice = AL_TRUE;
659 pDevice->szDeviceName = NULL;
661 pDevice->Frequency = frequency;
662 pDevice->Format = format;
663 pDevice->UpdateSize = SampleSize;
664 pDevice->NumUpdates = 1;
666 SuspendContext(NULL);
667 for(i = 0;BackendList[i].Init;i++)
669 pDevice->Funcs = &BackendList[i].Funcs;
670 if(ALCdevice_OpenCapture(pDevice, deviceName))
672 pDevice->next = g_pDeviceList;
673 g_pDeviceList = pDevice;
674 g_ulDeviceCount++;
676 DeviceFound = ALC_TRUE;
677 break;
680 ProcessContext(NULL);
682 if(!DeviceFound)
684 alcSetError(ALC_INVALID_VALUE);
685 free(pDevice);
686 pDevice = NULL;
689 else
690 alcSetError(ALC_OUT_OF_MEMORY);
692 return pDevice;
695 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
697 ALCboolean bReturn = ALC_FALSE;
698 ALCdevice **list;
700 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
702 SuspendContext(NULL);
704 list = &g_pDeviceList;
705 while(*list != pDevice)
706 list = &(*list)->next;
708 *list = (*list)->next;
709 g_ulDeviceCount--;
711 ProcessContext(NULL);
713 ALCdevice_CloseCapture(pDevice);
715 free(pDevice->szDeviceName);
716 pDevice->szDeviceName = NULL;
718 free(pDevice);
720 bReturn = ALC_TRUE;
722 else
723 alcSetError(ALC_INVALID_DEVICE);
725 return bReturn;
728 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
730 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
731 ALCdevice_StartCapture(pDevice);
732 else
733 alcSetError(ALC_INVALID_DEVICE);
736 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
738 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
739 ALCdevice_StopCapture(pDevice);
740 else
741 alcSetError(ALC_INVALID_DEVICE);
744 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
746 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
747 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
748 else
749 alcSetError(ALC_INVALID_DEVICE);
753 alcGetError
755 Return last ALC generated error code
757 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
759 ALCenum errorCode;
761 (void)device;
763 errorCode = g_eLastContextError;
764 g_eLastContextError = ALC_NO_ERROR;
765 return errorCode;
770 alcSuspendContext
772 Not functional
774 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
776 SuspendContext(NULL);
777 if(IsContext(pContext))
778 pContext->Suspended = AL_TRUE;
779 ProcessContext(NULL);
784 alcProcessContext
786 Not functional
788 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
790 SuspendContext(NULL);
791 if(IsContext(pContext))
792 pContext->Suspended = AL_FALSE;
793 ProcessContext(NULL);
798 alcGetString
800 Returns information about the Device, and error strings
802 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
804 const ALCchar *value = NULL;
806 switch (param)
808 case ALC_NO_ERROR:
809 value = alcNoError;
810 break;
812 case ALC_INVALID_ENUM:
813 value = alcErrInvalidEnum;
814 break;
816 case ALC_INVALID_VALUE:
817 value = alcErrInvalidValue;
818 break;
820 case ALC_INVALID_DEVICE:
821 value = alcErrInvalidDevice;
822 break;
824 case ALC_INVALID_CONTEXT:
825 value = alcErrInvalidContext;
826 break;
828 case ALC_OUT_OF_MEMORY:
829 value = alcErrOutOfMemory;
830 break;
832 case ALC_DEVICE_SPECIFIER:
833 if(IsDevice(pDevice))
834 value = pDevice->szDeviceName;
835 else
837 ProbeDeviceList();
838 value = alcDeviceList;
840 break;
842 case ALC_ALL_DEVICES_SPECIFIER:
843 ProbeAllDeviceList();
844 value = alcAllDeviceList;
845 break;
847 case ALC_CAPTURE_DEVICE_SPECIFIER:
848 if(IsDevice(pDevice))
849 value = pDevice->szDeviceName;
850 else
852 ProbeCaptureDeviceList();
853 value = alcCaptureDeviceList;
855 break;
857 /* Default devices are always first in the list */
858 case ALC_DEFAULT_DEVICE_SPECIFIER:
859 free(alcDefaultDeviceSpecifier);
860 alcDefaultDeviceSpecifier = strdup(alcDeviceList ? alcDeviceList : "");
861 if(!alcDefaultDeviceSpecifier)
862 alcSetError(ALC_OUT_OF_MEMORY);
863 value = alcDefaultDeviceSpecifier;
864 break;
866 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
867 free(alcDefaultAllDeviceSpecifier);
868 alcDefaultAllDeviceSpecifier = strdup(alcAllDeviceList ?
869 alcAllDeviceList : "");
870 if(!alcDefaultAllDeviceSpecifier)
871 alcSetError(ALC_OUT_OF_MEMORY);
872 value = alcDefaultAllDeviceSpecifier;
873 break;
875 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
876 free(alcCaptureDefaultDeviceSpecifier);
877 alcCaptureDefaultDeviceSpecifier = strdup(alcCaptureDeviceList ?
878 alcCaptureDeviceList : "");
879 if(!alcCaptureDefaultDeviceSpecifier)
880 alcSetError(ALC_OUT_OF_MEMORY);
881 value = alcCaptureDefaultDeviceSpecifier;
882 break;
884 case ALC_EXTENSIONS:
885 value = alcExtensionList;
886 break;
888 default:
889 alcSetError(ALC_INVALID_ENUM);
890 break;
893 return value;
898 alcGetIntegerv
900 Returns information about the Device and the version of Open AL
902 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
904 if(IsDevice(device) && device->IsCaptureDevice)
906 SuspendContext(NULL);
908 // Capture device
909 switch (param)
911 case ALC_CAPTURE_SAMPLES:
912 if ((size) && (data))
913 *data = ALCdevice_AvailableSamples(device);
914 else
915 alcSetError(ALC_INVALID_VALUE);
916 break;
918 case ALC_CONNECTED:
919 if(size <= 0)
920 alcSetError(ALC_INVALID_VALUE);
921 else
922 *data = device->Connected;
923 break;
925 default:
926 alcSetError(ALC_INVALID_ENUM);
927 break;
930 ProcessContext(NULL);
932 else
934 if(data)
936 // Playback Device
937 switch (param)
939 case ALC_MAJOR_VERSION:
940 if(size <= 0)
941 alcSetError(ALC_INVALID_VALUE);
942 else
943 *data = alcMajorVersion;
944 break;
946 case ALC_MINOR_VERSION:
947 if(size <= 0)
948 alcSetError(ALC_INVALID_VALUE);
949 else
950 *data = alcMinorVersion;
951 break;
953 case ALC_EFX_MAJOR_VERSION:
954 if(size <= 0)
955 alcSetError(ALC_INVALID_VALUE);
956 else
957 *data = alcEFXMajorVersion;
958 break;
960 case ALC_EFX_MINOR_VERSION:
961 if(size <= 0)
962 alcSetError(ALC_INVALID_VALUE);
963 else
964 *data = alcEFXMinorVersion;
965 break;
967 case ALC_MAX_AUXILIARY_SENDS:
968 if(size <= 0)
969 alcSetError(ALC_INVALID_VALUE);
970 else
971 *data = (device?device->NumAuxSends:MAX_SENDS);
972 break;
974 case ALC_ATTRIBUTES_SIZE:
975 if(!IsDevice(device))
976 alcSetError(ALC_INVALID_DEVICE);
977 else if(size <= 0)
978 alcSetError(ALC_INVALID_VALUE);
979 else
980 *data = 13;
981 break;
983 case ALC_ALL_ATTRIBUTES:
984 if(!IsDevice(device))
985 alcSetError(ALC_INVALID_DEVICE);
986 else if (size < 13)
987 alcSetError(ALC_INVALID_VALUE);
988 else
990 int i = 0;
992 SuspendContext(NULL);
993 data[i++] = ALC_FREQUENCY;
994 data[i++] = device->Frequency;
996 data[i++] = ALC_REFRESH;
997 data[i++] = device->Frequency / device->UpdateSize;
999 data[i++] = ALC_SYNC;
1000 data[i++] = ALC_FALSE;
1002 data[i++] = ALC_MONO_SOURCES;
1003 data[i++] = device->lNumMonoSources;
1005 data[i++] = ALC_STEREO_SOURCES;
1006 data[i++] = device->lNumStereoSources;
1008 data[i++] = ALC_MAX_AUXILIARY_SENDS;
1009 data[i++] = device->NumAuxSends;
1011 data[i++] = 0;
1012 ProcessContext(NULL);
1014 break;
1016 case ALC_FREQUENCY:
1017 if(!IsDevice(device))
1018 alcSetError(ALC_INVALID_DEVICE);
1019 else if(size <= 0)
1020 alcSetError(ALC_INVALID_VALUE);
1021 else
1022 *data = device->Frequency;
1023 break;
1025 case ALC_REFRESH:
1026 if(!IsDevice(device))
1027 alcSetError(ALC_INVALID_DEVICE);
1028 else if(size <= 0)
1029 alcSetError(ALC_INVALID_VALUE);
1030 else
1031 *data = device->Frequency / device->UpdateSize;
1032 break;
1034 case ALC_SYNC:
1035 if(!IsDevice(device))
1036 alcSetError(ALC_INVALID_DEVICE);
1037 else if(size <= 0)
1038 alcSetError(ALC_INVALID_VALUE);
1039 else
1040 *data = ALC_FALSE;
1041 break;
1043 case ALC_MONO_SOURCES:
1044 if(!IsDevice(device))
1045 alcSetError(ALC_INVALID_DEVICE);
1046 else if(size <= 0)
1047 alcSetError(ALC_INVALID_VALUE);
1048 else
1049 *data = device->lNumMonoSources;
1050 break;
1052 case ALC_STEREO_SOURCES:
1053 if(!IsDevice(device))
1054 alcSetError(ALC_INVALID_DEVICE);
1055 else if(size <= 0)
1056 alcSetError(ALC_INVALID_VALUE);
1057 else
1058 *data = device->lNumStereoSources;
1059 break;
1061 case ALC_CONNECTED:
1062 if(!IsDevice(device))
1063 alcSetError(ALC_INVALID_DEVICE);
1064 else if(size <= 0)
1065 alcSetError(ALC_INVALID_VALUE);
1066 else
1067 *data = device->Connected;
1068 break;
1070 default:
1071 alcSetError(ALC_INVALID_ENUM);
1072 break;
1075 else if(size)
1076 alcSetError(ALC_INVALID_VALUE);
1079 return;
1084 alcIsExtensionPresent
1086 Determines if there is support for a particular extension
1088 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
1090 ALCboolean bResult = ALC_FALSE;
1092 (void)device;
1094 if (extName)
1096 const char *ptr;
1097 size_t len;
1099 len = strlen(extName);
1100 ptr = alcExtensionList;
1101 while(ptr && *ptr)
1103 if(strncasecmp(ptr, extName, len) == 0 &&
1104 (ptr[len] == '\0' || isspace(ptr[len])))
1106 bResult = ALC_TRUE;
1107 break;
1109 if((ptr=strchr(ptr, ' ')) != NULL)
1111 do {
1112 ++ptr;
1113 } while(isspace(*ptr));
1117 else
1118 alcSetError(ALC_INVALID_VALUE);
1120 return bResult;
1125 alcGetProcAddress
1127 Retrieves the function address for a particular extension function
1129 ALCAPI ALCvoid * ALCAPIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
1131 ALCvoid *pFunction = NULL;
1132 ALsizei i = 0;
1134 (void)device;
1136 if (funcName)
1138 while(alcFunctions[i].funcName &&
1139 strcmp(alcFunctions[i].funcName,funcName) != 0)
1140 i++;
1141 pFunction = alcFunctions[i].address;
1143 else
1144 alcSetError(ALC_INVALID_VALUE);
1146 return pFunction;
1151 alcGetEnumValue
1153 Get the value for a particular ALC Enumerated Value
1155 ALCAPI ALCenum ALCAPIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
1157 ALsizei i = 0;
1158 ALCenum val;
1160 (void)device;
1162 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
1163 i++;
1164 val = enumeration[i].value;
1166 if(!enumeration[i].enumName)
1167 alcSetError(ALC_INVALID_VALUE);
1169 return val;
1174 alcCreateContext
1176 Create and attach a Context to a particular Device.
1178 ALCAPI ALCcontext* ALCAPIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
1180 ALuint attrIdx, reqStereoSources;
1181 ALCcontext *ALContext;
1182 void *temp;
1183 ALuint i;
1185 SuspendContext(NULL);
1187 if(!IsDevice(device) || device->IsCaptureDevice || !device->Connected)
1189 alcSetError(ALC_INVALID_DEVICE);
1190 ProcessContext(NULL);
1191 return NULL;
1194 // Reset Context Last Error code
1195 g_eLastContextError = ALC_NO_ERROR;
1197 // If a context is already running on the device, stop playback so the
1198 // device attributes can be updated
1199 if(device->NumContexts > 0)
1201 ProcessContext(NULL);
1202 ALCdevice_StopPlayback(device);
1203 SuspendContext(NULL);
1206 // Check for attributes
1207 if(attrList)
1209 ALCint level = device->Bs2bLevel;
1210 ALCuint freq = device->Frequency;
1211 ALCint numMono = device->lNumMonoSources;
1212 ALCint numStereo = device->lNumStereoSources;
1213 ALCuint numSends = device->NumAuxSends;
1215 attrIdx = 0;
1216 while(attrList[attrIdx])
1218 if(attrList[attrIdx] == ALC_FREQUENCY)
1220 freq = attrList[attrIdx + 1];
1221 if(freq == 0)
1222 freq = device->Frequency;
1225 if(attrList[attrIdx] == ALC_STEREO_SOURCES)
1227 reqStereoSources = attrList[attrIdx + 1];
1228 if(reqStereoSources > device->MaxNoOfSources)
1229 reqStereoSources = device->MaxNoOfSources;
1231 numStereo = reqStereoSources;
1232 numMono = device->MaxNoOfSources - numStereo;
1235 if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS)
1237 numSends = attrList[attrIdx + 1];
1238 if(numSends > MAX_SENDS)
1239 numSends = MAX_SENDS;
1242 attrIdx += 2;
1245 device->Bs2bLevel = GetConfigValueInt(NULL, "cf_level", level);
1246 device->Frequency = GetConfigValueInt(NULL, "frequency", freq);
1247 device->lNumMonoSources = numMono;
1248 device->lNumStereoSources = numStereo;
1249 device->NumAuxSends = GetConfigValueInt(NULL, "sends", numSends);
1252 if(ALCdevice_ResetPlayback(device) == ALC_FALSE)
1254 alcSetError(ALC_INVALID_DEVICE);
1255 aluHandleDisconnect(device);
1256 ProcessContext(NULL);
1257 return NULL;
1260 for(i = 0;i < device->NumContexts;i++)
1262 ALCcontext *context = device->Contexts[i];
1263 ALeffectslot *slot;
1264 ALsource *source;
1266 SuspendContext(context);
1267 for(slot = context->AuxiliaryEffectSlot;slot != NULL;slot = slot->next)
1269 if(!slot->EffectState)
1270 continue;
1272 if(ALEffect_DeviceUpdate(slot->EffectState, device) == AL_FALSE)
1274 alcSetError(ALC_INVALID_DEVICE);
1275 aluHandleDisconnect(device);
1276 ProcessContext(context);
1277 ProcessContext(NULL);
1278 ALCdevice_StopPlayback(device);
1279 return NULL;
1281 ALEffect_Update(slot->EffectState, context, &slot->effect);
1284 for(source = context->Source;source != NULL;source = source->next)
1286 ALuint s = device->NumAuxSends;
1287 while(s < MAX_SENDS)
1289 if(source->Send[s].Slot)
1290 source->Send[s].Slot->refcount--;
1291 source->Send[s].Slot = NULL;
1292 source->Send[s].WetFilter.type = 0;
1293 source->Send[s].WetFilter.filter = 0;
1294 s++;
1296 source->NeedsUpdate = AL_TRUE;
1298 ProcessContext(context);
1301 if(device->Bs2bLevel > 0 && device->Bs2bLevel <= 6)
1303 if(!device->Bs2b)
1305 device->Bs2b = calloc(1, sizeof(*device->Bs2b));
1306 bs2b_clear(device->Bs2b);
1308 bs2b_set_srate(device->Bs2b, device->Frequency);
1309 bs2b_set_level(device->Bs2b, device->Bs2bLevel);
1311 else
1313 free(device->Bs2b);
1314 device->Bs2b = NULL;
1317 temp = realloc(device->Contexts, (device->NumContexts+1) * sizeof(*device->Contexts));
1318 if(!temp)
1320 alcSetError(ALC_OUT_OF_MEMORY);
1321 ProcessContext(NULL);
1322 return NULL;
1324 device->Contexts = temp;
1326 ALContext = calloc(1, sizeof(ALCcontext));
1327 if(!ALContext)
1329 alcSetError(ALC_OUT_OF_MEMORY);
1330 ProcessContext(NULL);
1331 return NULL;
1334 device->Contexts[device->NumContexts++] = ALContext;
1335 ALContext->Device = device;
1337 InitContext(ALContext);
1339 ALContext->next = g_pContextList;
1340 g_pContextList = ALContext;
1341 g_ulContextCount++;
1343 ProcessContext(NULL);
1345 return ALContext;
1350 alcDestroyContext
1352 Remove a Context
1354 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
1356 ALCcontext **list;
1357 ALuint i;
1359 if (IsContext(context))
1361 ALCdevice *Device = context->Device;
1363 if(Device->NumContexts == 1)
1364 ALCdevice_StopPlayback(Device);
1366 SuspendContext(NULL);
1368 for(i = 0;i < Device->NumContexts-1;i++)
1370 if(Device->Contexts[i] == context)
1372 memmove(&Device->Contexts[i], &Device->Contexts[i+1],
1373 (Device->NumContexts-i-1) * sizeof(*Device->Contexts));
1374 break;
1377 Device->NumContexts--;
1379 // Lock context
1380 SuspendContext(context);
1382 if(context->SourceCount > 0)
1384 #ifdef _DEBUG
1385 AL_PRINT("alcDestroyContext(): deleting %d Source(s)\n", context->SourceCount);
1386 #endif
1387 ReleaseALSources(context);
1389 if(context->AuxiliaryEffectSlotCount > 0)
1391 #ifdef _DEBUG
1392 AL_PRINT("alcDestroyContext(): deleting %d AuxiliaryEffectSlot(s)\n", context->AuxiliaryEffectSlotCount);
1393 #endif
1394 ReleaseALAuxiliaryEffectSlots(context);
1397 list = &g_pContextList;
1398 while(*list != context)
1399 list = &(*list)->next;
1401 *list = (*list)->next;
1402 g_ulContextCount--;
1404 // Unlock context
1405 ProcessContext(context);
1406 ProcessContext(NULL);
1408 ExitContext(context);
1410 // Free memory (MUST do this after ProcessContext)
1411 memset(context, 0, sizeof(ALCcontext));
1412 free(context);
1414 else
1415 alcSetError(ALC_INVALID_CONTEXT);
1420 alcGetCurrentContext
1422 Returns the currently active Context
1424 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1426 ALCcontext *pContext;
1428 if((pContext=GetContextSuspended()) != NULL)
1429 ProcessContext(pContext);
1431 return pContext;
1435 alcGetThreadContext
1437 Returns the currently active thread-local Context
1439 ALCcontext * ALCAPIENTRY alcGetThreadContext(void)
1441 ALCcontext *pContext = NULL;
1443 SuspendContext(NULL);
1445 pContext = tls_get(LocalContext);
1446 if(pContext && !IsContext(pContext))
1448 tls_set(LocalContext, NULL);
1449 pContext = NULL;
1452 ProcessContext(NULL);
1454 return pContext;
1459 alcGetContextsDevice
1461 Returns the Device that a particular Context is attached to
1463 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1465 ALCdevice *pDevice = NULL;
1467 SuspendContext(NULL);
1468 if (IsContext(pContext))
1469 pDevice = pContext->Device;
1470 else
1471 alcSetError(ALC_INVALID_CONTEXT);
1472 ProcessContext(NULL);
1474 return pDevice;
1479 alcMakeContextCurrent
1481 Makes the given Context the active Context
1483 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1485 ALCcontext *ALContext;
1486 ALboolean bReturn = AL_TRUE;
1488 SuspendContext(NULL);
1490 // context must be a valid Context or NULL
1491 if(context == NULL || IsContext(context))
1493 if((ALContext=GetContextSuspended()) != NULL)
1495 ALContext->InUse=AL_FALSE;
1496 ProcessContext(ALContext);
1499 if((ALContext=context) != NULL && ALContext->Device)
1501 SuspendContext(ALContext);
1502 ALContext->InUse=AL_TRUE;
1503 ProcessContext(ALContext);
1506 tls_set(LocalContext, NULL);
1508 else
1510 alcSetError(ALC_INVALID_CONTEXT);
1511 bReturn = AL_FALSE;
1514 ProcessContext(NULL);
1516 return bReturn;
1520 alcMakeCurrent
1522 Makes the given Context the active Context for the current thread
1524 ALCboolean ALCAPIENTRY alcMakeCurrent(ALCcontext *context)
1526 ALboolean bReturn = AL_TRUE;
1528 SuspendContext(NULL);
1530 // context must be a valid Context or NULL
1531 if(context == NULL || IsContext(context))
1532 tls_set(LocalContext, context);
1533 else
1535 alcSetError(ALC_INVALID_CONTEXT);
1536 bReturn = AL_FALSE;
1539 ProcessContext(NULL);
1541 return bReturn;
1545 // Sets the default channel order used by most non-WaveFormatEx-based APIs
1546 void SetDefaultChannelOrder(ALCdevice *device)
1548 switch(aluChannelsFromFormat(device->Format))
1550 case 1: device->DevChannels[0] = FRONT_CENTER; break;
1552 case 2: device->DevChannels[0] = FRONT_LEFT;
1553 device->DevChannels[1] = FRONT_RIGHT; break;
1555 case 4: device->DevChannels[0] = FRONT_LEFT;
1556 device->DevChannels[1] = FRONT_RIGHT;
1557 device->DevChannels[2] = BACK_LEFT;
1558 device->DevChannels[3] = BACK_RIGHT; break;
1560 case 6: device->DevChannels[0] = FRONT_LEFT;
1561 device->DevChannels[1] = FRONT_RIGHT;
1562 device->DevChannels[2] = BACK_LEFT;
1563 device->DevChannels[3] = BACK_RIGHT;
1564 device->DevChannels[4] = FRONT_CENTER;
1565 device->DevChannels[5] = LFE; break;
1567 case 7: device->DevChannels[0] = FRONT_LEFT;
1568 device->DevChannels[1] = FRONT_RIGHT;
1569 device->DevChannels[2] = FRONT_CENTER;
1570 device->DevChannels[3] = LFE;
1571 device->DevChannels[4] = BACK_CENTER;
1572 device->DevChannels[5] = SIDE_LEFT;
1573 device->DevChannels[6] = SIDE_RIGHT; break;
1575 case 8: device->DevChannels[0] = FRONT_LEFT;
1576 device->DevChannels[1] = FRONT_RIGHT;
1577 device->DevChannels[2] = BACK_LEFT;
1578 device->DevChannels[3] = BACK_RIGHT;
1579 device->DevChannels[4] = FRONT_CENTER;
1580 device->DevChannels[5] = LFE;
1581 device->DevChannels[6] = SIDE_LEFT;
1582 device->DevChannels[7] = SIDE_RIGHT; break;
1585 // Sets the default order used by WaveFormatEx
1586 void SetDefaultWFXChannelOrder(ALCdevice *device)
1588 switch(aluChannelsFromFormat(device->Format))
1590 case 1: device->DevChannels[0] = FRONT_CENTER; break;
1592 case 2: device->DevChannels[0] = FRONT_LEFT;
1593 device->DevChannels[1] = FRONT_RIGHT; break;
1595 case 4: device->DevChannels[0] = FRONT_LEFT;
1596 device->DevChannels[1] = FRONT_RIGHT;
1597 device->DevChannels[2] = BACK_LEFT;
1598 device->DevChannels[3] = BACK_RIGHT; break;
1600 case 6: device->DevChannels[0] = FRONT_LEFT;
1601 device->DevChannels[1] = FRONT_RIGHT;
1602 device->DevChannels[2] = FRONT_CENTER;
1603 device->DevChannels[3] = LFE;
1604 device->DevChannels[4] = BACK_LEFT;
1605 device->DevChannels[5] = BACK_RIGHT; break;
1607 case 7: device->DevChannels[0] = FRONT_LEFT;
1608 device->DevChannels[1] = FRONT_RIGHT;
1609 device->DevChannels[2] = FRONT_CENTER;
1610 device->DevChannels[3] = LFE;
1611 device->DevChannels[4] = BACK_CENTER;
1612 device->DevChannels[5] = SIDE_LEFT;
1613 device->DevChannels[6] = SIDE_RIGHT; break;
1615 case 8: device->DevChannels[0] = FRONT_LEFT;
1616 device->DevChannels[1] = FRONT_RIGHT;
1617 device->DevChannels[2] = FRONT_CENTER;
1618 device->DevChannels[3] = LFE;
1619 device->DevChannels[4] = BACK_LEFT;
1620 device->DevChannels[5] = BACK_RIGHT;
1621 device->DevChannels[6] = SIDE_LEFT;
1622 device->DevChannels[7] = SIDE_RIGHT; break;
1626 static ALenum GetFormatFromString(const char *str)
1628 if(strcasecmp(str, "AL_FORMAT_MONO32") == 0) return AL_FORMAT_MONO_FLOAT32;
1629 if(strcasecmp(str, "AL_FORMAT_STEREO32") == 0) return AL_FORMAT_STEREO_FLOAT32;
1630 if(strcasecmp(str, "AL_FORMAT_QUAD32") == 0) return AL_FORMAT_QUAD32;
1631 if(strcasecmp(str, "AL_FORMAT_51CHN32") == 0) return AL_FORMAT_51CHN32;
1632 if(strcasecmp(str, "AL_FORMAT_61CHN32") == 0) return AL_FORMAT_61CHN32;
1633 if(strcasecmp(str, "AL_FORMAT_71CHN32") == 0) return AL_FORMAT_71CHN32;
1635 if(strcasecmp(str, "AL_FORMAT_MONO16") == 0) return AL_FORMAT_MONO16;
1636 if(strcasecmp(str, "AL_FORMAT_STEREO16") == 0) return AL_FORMAT_STEREO16;
1637 if(strcasecmp(str, "AL_FORMAT_QUAD16") == 0) return AL_FORMAT_QUAD16;
1638 if(strcasecmp(str, "AL_FORMAT_51CHN16") == 0) return AL_FORMAT_51CHN16;
1639 if(strcasecmp(str, "AL_FORMAT_61CHN16") == 0) return AL_FORMAT_61CHN16;
1640 if(strcasecmp(str, "AL_FORMAT_71CHN16") == 0) return AL_FORMAT_71CHN16;
1642 if(strcasecmp(str, "AL_FORMAT_MONO8") == 0) return AL_FORMAT_MONO8;
1643 if(strcasecmp(str, "AL_FORMAT_STEREO8") == 0) return AL_FORMAT_STEREO8;
1644 if(strcasecmp(str, "AL_FORMAT_QUAD8") == 0) return AL_FORMAT_QUAD8;
1645 if(strcasecmp(str, "AL_FORMAT_51CHN8") == 0) return AL_FORMAT_51CHN8;
1646 if(strcasecmp(str, "AL_FORMAT_61CHN8") == 0) return AL_FORMAT_61CHN8;
1647 if(strcasecmp(str, "AL_FORMAT_71CHN8") == 0) return AL_FORMAT_71CHN8;
1649 AL_PRINT("Unknown format: \"%s\"\n", str);
1650 return AL_FORMAT_STEREO16;
1654 alcOpenDevice
1656 Open the Device specified.
1658 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1660 ALboolean bDeviceFound = AL_FALSE;
1661 ALCdevice *device;
1662 ALint i;
1664 if(deviceName && !deviceName[0])
1665 deviceName = NULL;
1667 device = malloc(sizeof(ALCdevice));
1668 if (device)
1670 const char *fmt;
1672 //Initialise device structure
1673 memset(device, 0, sizeof(ALCdevice));
1675 //Validate device
1676 device->Connected = ALC_TRUE;
1677 device->IsCaptureDevice = AL_FALSE;
1679 device->Bs2b = NULL;
1680 device->szDeviceName = NULL;
1682 device->Contexts = NULL;
1683 device->NumContexts = 0;
1685 //Set output format
1686 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1687 if(device->Frequency == 0)
1688 device->Frequency = SWMIXER_OUTPUT_RATE;
1690 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1691 device->Format = GetFormatFromString(fmt);
1693 device->NumUpdates = GetConfigValueInt(NULL, "periods", 4);
1694 if(device->NumUpdates < 2)
1695 device->NumUpdates = 4;
1697 i = GetConfigValueInt(NULL, "refresh", 4096);
1698 if(i <= 0) i = 4096;
1700 device->UpdateSize = GetConfigValueInt(NULL, "period_size", i/device->NumUpdates);
1701 if(device->UpdateSize <= 0)
1702 device->UpdateSize = i/device->NumUpdates;
1704 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1705 if((ALint)device->MaxNoOfSources <= 0)
1706 device->MaxNoOfSources = 256;
1708 device->AuxiliaryEffectSlotMax = GetConfigValueInt(NULL, "slots", 4);
1709 if((ALint)device->AuxiliaryEffectSlotMax <= 0)
1710 device->AuxiliaryEffectSlotMax = 4;
1712 device->lNumStereoSources = 1;
1713 device->lNumMonoSources = device->MaxNoOfSources - device->lNumStereoSources;
1715 device->NumAuxSends = GetConfigValueInt(NULL, "sends", MAX_SENDS);
1716 if(device->NumAuxSends > MAX_SENDS)
1717 device->NumAuxSends = MAX_SENDS;
1719 device->Bs2bLevel = GetConfigValueInt(NULL, "cf_level", 0);
1721 if(aluChannelsFromFormat(device->Format) <= 2)
1723 device->HeadDampen = GetConfigValueFloat(NULL, "head_dampen", DEFAULT_HEAD_DAMPEN);
1724 device->HeadDampen = __min(device->HeadDampen, 1.0f);
1725 device->HeadDampen = __max(device->HeadDampen, 0.0f);
1727 else
1728 device->HeadDampen = 0.0f;
1730 // Find a playback device to open
1731 SuspendContext(NULL);
1732 for(i = 0;BackendList[i].Init;i++)
1734 device->Funcs = &BackendList[i].Funcs;
1735 if(ALCdevice_OpenPlayback(device, deviceName))
1737 device->next = g_pDeviceList;
1738 g_pDeviceList = device;
1739 g_ulDeviceCount++;
1741 bDeviceFound = AL_TRUE;
1742 break;
1745 ProcessContext(NULL);
1747 if (!bDeviceFound)
1749 // No suitable output device found
1750 alcSetError(ALC_INVALID_VALUE);
1751 free(device);
1752 device = NULL;
1755 else
1756 alcSetError(ALC_OUT_OF_MEMORY);
1758 return device;
1763 alcCloseDevice
1765 Close the specified Device
1767 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1769 ALCboolean bReturn = ALC_FALSE;
1770 ALCdevice **list;
1772 if(IsDevice(pDevice) && !pDevice->IsCaptureDevice)
1774 SuspendContext(NULL);
1776 list = &g_pDeviceList;
1777 while(*list != pDevice)
1778 list = &(*list)->next;
1780 *list = (*list)->next;
1781 g_ulDeviceCount--;
1783 ProcessContext(NULL);
1785 if(pDevice->NumContexts > 0)
1787 #ifdef _DEBUG
1788 AL_PRINT("alcCloseDevice(): destroying %u Context(s)\n", pDevice->NumContexts);
1789 #endif
1790 while(pDevice->NumContexts > 0)
1791 alcDestroyContext(pDevice->Contexts[0]);
1793 ALCdevice_ClosePlayback(pDevice);
1795 if(pDevice->BufferCount > 0)
1797 #ifdef _DEBUG
1798 AL_PRINT("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice->BufferCount);
1799 #endif
1800 ReleaseALBuffers(pDevice);
1802 if(pDevice->EffectCount > 0)
1804 #ifdef _DEBUG
1805 AL_PRINT("alcCloseDevice(): deleting %d Effect(s)\n", pDevice->EffectCount);
1806 #endif
1807 ReleaseALEffects(pDevice);
1809 if(pDevice->FilterCount > 0)
1811 #ifdef _DEBUG
1812 AL_PRINT("alcCloseDevice(): deleting %d Filter(s)\n", pDevice->FilterCount);
1813 #endif
1814 ReleaseALFilters(pDevice);
1816 if(pDevice->DatabufferCount > 0)
1818 #ifdef _DEBUG
1819 AL_PRINT("alcCloseDevice(): deleting %d Databuffer(s)\n", pDevice->DatabufferCount);
1820 #endif
1821 ReleaseALDatabuffers(pDevice);
1824 free(pDevice->Bs2b);
1825 pDevice->Bs2b = NULL;
1827 free(pDevice->szDeviceName);
1828 pDevice->szDeviceName = NULL;
1830 free(pDevice->Contexts);
1831 pDevice->Contexts = NULL;
1833 //Release device structure
1834 memset(pDevice, 0, sizeof(ALCdevice));
1835 free(pDevice);
1837 bReturn = ALC_TRUE;
1839 else
1840 alcSetError(ALC_INVALID_DEVICE);
1842 return bReturn;
1846 ALCvoid ReleaseALC(ALCvoid)
1848 free(alcDeviceList); alcDeviceList = NULL;
1849 alcDeviceListSize = 0;
1850 free(alcAllDeviceList); alcAllDeviceList = NULL;
1851 alcAllDeviceListSize = 0;
1852 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
1853 alcCaptureDeviceListSize = 0;
1855 free(alcDefaultDeviceSpecifier);
1856 alcDefaultDeviceSpecifier = NULL;
1857 free(alcDefaultAllDeviceSpecifier);
1858 alcDefaultAllDeviceSpecifier = NULL;
1859 free(alcCaptureDefaultDeviceSpecifier);
1860 alcCaptureDefaultDeviceSpecifier = NULL;
1862 #ifdef _DEBUG
1863 if(g_ulDeviceCount > 0)
1864 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
1865 #endif
1867 while(g_pDeviceList)
1869 if(g_pDeviceList->IsCaptureDevice)
1870 alcCaptureCloseDevice(g_pDeviceList);
1871 else
1872 alcCloseDevice(g_pDeviceList);
1876 ///////////////////////////////////////////////////////