Suppress connection refused errors
[openal-soft/openal-hmr.git] / Alc / ALc.c
blob1efaf0adb31c0d3f20f17df522bdc0a074cc1cf7
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_PULSEAUDIO
52 { "pulse", alc_pulse_init, alc_pulse_deinit, alc_pulse_probe, EmptyFuncs },
53 #endif
54 #ifdef HAVE_ALSA
55 { "alsa", alc_alsa_init, alc_alsa_deinit, alc_alsa_probe, EmptyFuncs },
56 #endif
57 #ifdef HAVE_OSS
58 { "oss", alc_oss_init, alc_oss_deinit, alc_oss_probe, EmptyFuncs },
59 #endif
60 #ifdef HAVE_SOLARIS
61 { "solaris", alc_solaris_init, alc_solaris_deinit, alc_solaris_probe, EmptyFuncs },
62 #endif
63 #ifdef HAVE_DSOUND
64 { "dsound", alcDSoundInit, alcDSoundDeinit, alcDSoundProbe, EmptyFuncs },
65 #endif
66 #ifdef HAVE_WINMM
67 { "winmm", alcWinMMInit, alcWinMMDeinit, alcWinMMProbe, EmptyFuncs },
68 #endif
69 #ifdef HAVE_PORTAUDIO
70 { "port", alc_pa_init, alc_pa_deinit, alc_pa_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(ALCdevice *device, ALenum errorCode)
513 if(IsDevice(device))
514 device->LastError = errorCode;
515 else
516 g_eLastContextError = errorCode;
521 SuspendContext
523 Thread-safe entry
525 ALCvoid SuspendContext(ALCcontext *pContext)
527 (void)pContext;
528 EnterCriticalSection(&g_csMutex);
533 ProcessContext
535 Thread-safe exit
537 ALCvoid ProcessContext(ALCcontext *pContext)
539 (void)pContext;
540 LeaveCriticalSection(&g_csMutex);
545 GetContextSuspended
547 Returns the currently active Context, in a locked state
549 ALCcontext *GetContextSuspended(void)
551 ALCcontext *pContext = NULL;
553 SuspendContext(NULL);
555 pContext = tls_get(LocalContext);
556 if(pContext && !IsContext(pContext))
558 tls_set(LocalContext, NULL);
559 pContext = NULL;
561 if(!pContext)
563 pContext = g_pContextList;
564 while(pContext && !pContext->InUse)
565 pContext = pContext->next;
567 if(pContext)
568 SuspendContext(pContext);
570 ProcessContext(NULL);
572 return pContext;
577 InitContext
579 Initialize Context variables
581 static ALvoid InitContext(ALCcontext *pContext)
583 //Initialise listener
584 pContext->Listener.Gain = 1.0f;
585 pContext->Listener.MetersPerUnit = 1.0f;
586 pContext->Listener.Position[0] = 0.0f;
587 pContext->Listener.Position[1] = 0.0f;
588 pContext->Listener.Position[2] = 0.0f;
589 pContext->Listener.Velocity[0] = 0.0f;
590 pContext->Listener.Velocity[1] = 0.0f;
591 pContext->Listener.Velocity[2] = 0.0f;
592 pContext->Listener.Forward[0] = 0.0f;
593 pContext->Listener.Forward[1] = 0.0f;
594 pContext->Listener.Forward[2] = -1.0f;
595 pContext->Listener.Up[0] = 0.0f;
596 pContext->Listener.Up[1] = 1.0f;
597 pContext->Listener.Up[2] = 0.0f;
599 //Validate pContext
600 pContext->LastError = AL_NO_ERROR;
601 pContext->InUse = AL_FALSE;
602 pContext->Suspended = AL_FALSE;
604 //Set globals
605 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
606 pContext->SourceDistanceModel = AL_FALSE;
607 pContext->DopplerFactor = 1.0f;
608 pContext->DopplerVelocity = 1.0f;
609 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
611 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";
616 ExitContext
618 Clean up Context, destroy any remaining Sources
620 static ALCvoid ExitContext(ALCcontext *pContext)
622 //Invalidate context
623 pContext->LastError = AL_NO_ERROR;
624 pContext->InUse = AL_FALSE;
627 ///////////////////////////////////////////////////////
630 ///////////////////////////////////////////////////////
631 // ALC Functions calls
634 // This should probably move to another c file but for now ...
635 ALCAPI ALCdevice* ALCAPIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
637 ALCboolean DeviceFound = ALC_FALSE;
638 ALCdevice *pDevice = NULL;
639 ALCint i;
641 if(SampleSize <= 0)
643 alcSetError(NULL, ALC_INVALID_VALUE);
644 return NULL;
647 if(deviceName && !deviceName[0])
648 deviceName = NULL;
650 pDevice = malloc(sizeof(ALCdevice));
651 if (pDevice)
653 //Initialise device structure
654 memset(pDevice, 0, sizeof(ALCdevice));
656 //Validate device
657 pDevice->Connected = ALC_TRUE;
658 pDevice->IsCaptureDevice = AL_TRUE;
660 pDevice->szDeviceName = NULL;
662 pDevice->Frequency = frequency;
663 pDevice->Format = format;
664 pDevice->UpdateSize = SampleSize;
665 pDevice->NumUpdates = 1;
667 SuspendContext(NULL);
668 for(i = 0;BackendList[i].Init;i++)
670 pDevice->Funcs = &BackendList[i].Funcs;
671 if(ALCdevice_OpenCapture(pDevice, deviceName))
673 pDevice->next = g_pDeviceList;
674 g_pDeviceList = pDevice;
675 g_ulDeviceCount++;
677 DeviceFound = ALC_TRUE;
678 break;
681 ProcessContext(NULL);
683 if(!DeviceFound)
685 alcSetError(NULL, ALC_INVALID_VALUE);
686 free(pDevice);
687 pDevice = NULL;
690 else
691 alcSetError(NULL, ALC_OUT_OF_MEMORY);
693 return pDevice;
696 ALCAPI ALCboolean ALCAPIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
698 ALCboolean bReturn = ALC_FALSE;
699 ALCdevice **list;
701 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
703 SuspendContext(NULL);
705 list = &g_pDeviceList;
706 while(*list != pDevice)
707 list = &(*list)->next;
709 *list = (*list)->next;
710 g_ulDeviceCount--;
712 ProcessContext(NULL);
714 ALCdevice_CloseCapture(pDevice);
716 free(pDevice->szDeviceName);
717 pDevice->szDeviceName = NULL;
719 free(pDevice);
721 bReturn = ALC_TRUE;
723 else
724 alcSetError(pDevice, ALC_INVALID_DEVICE);
726 return bReturn;
729 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
731 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
732 ALCdevice_StartCapture(pDevice);
733 else
734 alcSetError(pDevice, ALC_INVALID_DEVICE);
737 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
739 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
740 ALCdevice_StopCapture(pDevice);
741 else
742 alcSetError(pDevice, ALC_INVALID_DEVICE);
745 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
747 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
748 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
749 else
750 alcSetError(pDevice, ALC_INVALID_DEVICE);
754 alcGetError
756 Return last ALC generated error code
758 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
760 ALCenum errorCode = ALC_NO_ERROR;
762 if(device)
764 errorCode = device->LastError;
765 device->LastError = ALC_NO_ERROR;
767 else
769 errorCode = g_eLastContextError;
770 g_eLastContextError = ALC_NO_ERROR;
772 return errorCode;
777 alcSuspendContext
779 Not functional
781 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
783 SuspendContext(NULL);
784 if(IsContext(pContext))
785 pContext->Suspended = AL_TRUE;
786 ProcessContext(NULL);
791 alcProcessContext
793 Not functional
795 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
797 SuspendContext(NULL);
798 if(IsContext(pContext))
799 pContext->Suspended = AL_FALSE;
800 ProcessContext(NULL);
805 alcGetString
807 Returns information about the Device, and error strings
809 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
811 const ALCchar *value = NULL;
813 switch (param)
815 case ALC_NO_ERROR:
816 value = alcNoError;
817 break;
819 case ALC_INVALID_ENUM:
820 value = alcErrInvalidEnum;
821 break;
823 case ALC_INVALID_VALUE:
824 value = alcErrInvalidValue;
825 break;
827 case ALC_INVALID_DEVICE:
828 value = alcErrInvalidDevice;
829 break;
831 case ALC_INVALID_CONTEXT:
832 value = alcErrInvalidContext;
833 break;
835 case ALC_OUT_OF_MEMORY:
836 value = alcErrOutOfMemory;
837 break;
839 case ALC_DEVICE_SPECIFIER:
840 if(IsDevice(pDevice))
841 value = pDevice->szDeviceName;
842 else
844 ProbeDeviceList();
845 value = alcDeviceList;
847 break;
849 case ALC_ALL_DEVICES_SPECIFIER:
850 ProbeAllDeviceList();
851 value = alcAllDeviceList;
852 break;
854 case ALC_CAPTURE_DEVICE_SPECIFIER:
855 if(IsDevice(pDevice))
856 value = pDevice->szDeviceName;
857 else
859 ProbeCaptureDeviceList();
860 value = alcCaptureDeviceList;
862 break;
864 /* Default devices are always first in the list */
865 case ALC_DEFAULT_DEVICE_SPECIFIER:
866 free(alcDefaultDeviceSpecifier);
867 alcDefaultDeviceSpecifier = strdup(alcDeviceList ? alcDeviceList : "");
868 if(!alcDefaultDeviceSpecifier)
869 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
870 value = alcDefaultDeviceSpecifier;
871 break;
873 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
874 free(alcDefaultAllDeviceSpecifier);
875 alcDefaultAllDeviceSpecifier = strdup(alcAllDeviceList ?
876 alcAllDeviceList : "");
877 if(!alcDefaultAllDeviceSpecifier)
878 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
879 value = alcDefaultAllDeviceSpecifier;
880 break;
882 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
883 free(alcCaptureDefaultDeviceSpecifier);
884 alcCaptureDefaultDeviceSpecifier = strdup(alcCaptureDeviceList ?
885 alcCaptureDeviceList : "");
886 if(!alcCaptureDefaultDeviceSpecifier)
887 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
888 value = alcCaptureDefaultDeviceSpecifier;
889 break;
891 case ALC_EXTENSIONS:
892 value = alcExtensionList;
893 break;
895 default:
896 alcSetError(pDevice, ALC_INVALID_ENUM);
897 break;
900 return value;
905 alcGetIntegerv
907 Returns information about the Device and the version of Open AL
909 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
911 if(size == 0 || data == NULL)
913 alcSetError(device, ALC_INVALID_VALUE);
914 return;
917 if(IsDevice(device) && device->IsCaptureDevice)
919 SuspendContext(NULL);
921 // Capture device
922 switch (param)
924 case ALC_CAPTURE_SAMPLES:
925 *data = ALCdevice_AvailableSamples(device);
926 break;
928 case ALC_CONNECTED:
929 *data = device->Connected;
930 break;
932 default:
933 alcSetError(device, ALC_INVALID_ENUM);
934 break;
937 ProcessContext(NULL);
938 return;
941 // Playback Device
942 switch (param)
944 case ALC_MAJOR_VERSION:
945 *data = alcMajorVersion;
946 break;
948 case ALC_MINOR_VERSION:
949 *data = alcMinorVersion;
950 break;
952 case ALC_EFX_MAJOR_VERSION:
953 *data = alcEFXMajorVersion;
954 break;
956 case ALC_EFX_MINOR_VERSION:
957 *data = alcEFXMinorVersion;
958 break;
960 case ALC_MAX_AUXILIARY_SENDS:
961 if(!IsDevice(device))
962 alcSetError(device, ALC_INVALID_DEVICE);
963 else
964 *data = device->NumAuxSends;
965 break;
967 case ALC_ATTRIBUTES_SIZE:
968 if(!IsDevice(device))
969 alcSetError(device, ALC_INVALID_DEVICE);
970 else
971 *data = 13;
972 break;
974 case ALC_ALL_ATTRIBUTES:
975 if(!IsDevice(device))
976 alcSetError(device, ALC_INVALID_DEVICE);
977 else if (size < 13)
978 alcSetError(device, ALC_INVALID_VALUE);
979 else
981 int i = 0;
983 SuspendContext(NULL);
984 data[i++] = ALC_FREQUENCY;
985 data[i++] = device->Frequency;
987 data[i++] = ALC_REFRESH;
988 data[i++] = device->Frequency / device->UpdateSize;
990 data[i++] = ALC_SYNC;
991 data[i++] = ALC_FALSE;
993 data[i++] = ALC_MONO_SOURCES;
994 data[i++] = device->lNumMonoSources;
996 data[i++] = ALC_STEREO_SOURCES;
997 data[i++] = device->lNumStereoSources;
999 data[i++] = ALC_MAX_AUXILIARY_SENDS;
1000 data[i++] = device->NumAuxSends;
1002 data[i++] = 0;
1003 ProcessContext(NULL);
1005 break;
1007 case ALC_FREQUENCY:
1008 if(!IsDevice(device))
1009 alcSetError(device, ALC_INVALID_DEVICE);
1010 else
1011 *data = device->Frequency;
1012 break;
1014 case ALC_REFRESH:
1015 if(!IsDevice(device))
1016 alcSetError(device, ALC_INVALID_DEVICE);
1017 else
1018 *data = device->Frequency / device->UpdateSize;
1019 break;
1021 case ALC_SYNC:
1022 if(!IsDevice(device))
1023 alcSetError(device, ALC_INVALID_DEVICE);
1024 else
1025 *data = ALC_FALSE;
1026 break;
1028 case ALC_MONO_SOURCES:
1029 if(!IsDevice(device))
1030 alcSetError(device, ALC_INVALID_DEVICE);
1031 else
1032 *data = device->lNumMonoSources;
1033 break;
1035 case ALC_STEREO_SOURCES:
1036 if(!IsDevice(device))
1037 alcSetError(device, ALC_INVALID_DEVICE);
1038 else
1039 *data = device->lNumStereoSources;
1040 break;
1042 case ALC_CONNECTED:
1043 if(!IsDevice(device))
1044 alcSetError(device, ALC_INVALID_DEVICE);
1045 else
1046 *data = device->Connected;
1047 break;
1049 default:
1050 alcSetError(device, ALC_INVALID_ENUM);
1051 break;
1057 alcIsExtensionPresent
1059 Determines if there is support for a particular extension
1061 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
1063 ALCboolean bResult = ALC_FALSE;
1065 if (extName)
1067 const char *ptr;
1068 size_t len;
1070 len = strlen(extName);
1071 ptr = alcExtensionList;
1072 while(ptr && *ptr)
1074 if(strncasecmp(ptr, extName, len) == 0 &&
1075 (ptr[len] == '\0' || isspace(ptr[len])))
1077 bResult = ALC_TRUE;
1078 break;
1080 if((ptr=strchr(ptr, ' ')) != NULL)
1082 do {
1083 ++ptr;
1084 } while(isspace(*ptr));
1088 else
1089 alcSetError(device, ALC_INVALID_VALUE);
1091 return bResult;
1096 alcGetProcAddress
1098 Retrieves the function address for a particular extension function
1100 ALCAPI ALCvoid * ALCAPIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
1102 ALCvoid *pFunction = NULL;
1103 ALsizei i = 0;
1105 if (funcName)
1107 while(alcFunctions[i].funcName &&
1108 strcmp(alcFunctions[i].funcName,funcName) != 0)
1109 i++;
1110 pFunction = alcFunctions[i].address;
1112 else
1113 alcSetError(device, ALC_INVALID_VALUE);
1115 return pFunction;
1120 alcGetEnumValue
1122 Get the value for a particular ALC Enumerated Value
1124 ALCAPI ALCenum ALCAPIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
1126 ALsizei i = 0;
1127 ALCenum val;
1129 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
1130 i++;
1131 val = enumeration[i].value;
1133 if(!enumeration[i].enumName)
1134 alcSetError(device, ALC_INVALID_VALUE);
1136 return val;
1141 alcCreateContext
1143 Create and attach a Context to a particular Device.
1145 ALCAPI ALCcontext* ALCAPIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
1147 ALuint attrIdx, reqStereoSources;
1148 ALCcontext *ALContext;
1149 void *temp;
1150 ALuint i;
1152 SuspendContext(NULL);
1154 if(!IsDevice(device) || device->IsCaptureDevice || !device->Connected)
1156 alcSetError(device, ALC_INVALID_DEVICE);
1157 ProcessContext(NULL);
1158 return NULL;
1161 // Reset Context Last Error code
1162 device->LastError = ALC_NO_ERROR;
1164 // If a context is already running on the device, stop playback so the
1165 // device attributes can be updated
1166 if(device->NumContexts > 0)
1168 ProcessContext(NULL);
1169 ALCdevice_StopPlayback(device);
1170 SuspendContext(NULL);
1173 // Check for attributes
1174 if(attrList)
1176 ALCint level = device->Bs2bLevel;
1177 ALCuint freq = device->Frequency;
1178 ALCint numMono = device->lNumMonoSources;
1179 ALCint numStereo = device->lNumStereoSources;
1180 ALCuint numSends = device->NumAuxSends;
1182 attrIdx = 0;
1183 while(attrList[attrIdx])
1185 if(attrList[attrIdx] == ALC_FREQUENCY &&
1186 !ConfigValueExists(NULL, "frequency"))
1188 freq = attrList[attrIdx + 1];
1189 if(freq < 8000)
1190 freq = 8000;
1193 if(attrList[attrIdx] == ALC_STEREO_SOURCES)
1195 reqStereoSources = attrList[attrIdx + 1];
1196 if(reqStereoSources > device->MaxNoOfSources)
1197 reqStereoSources = device->MaxNoOfSources;
1199 numStereo = reqStereoSources;
1200 numMono = device->MaxNoOfSources - numStereo;
1203 if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS &&
1204 !ConfigValueExists(NULL, "sends"))
1206 numSends = attrList[attrIdx + 1];
1207 if(numSends > MAX_SENDS)
1208 numSends = MAX_SENDS;
1211 attrIdx += 2;
1214 device->Bs2bLevel = level;
1215 device->Frequency = freq;
1216 device->lNumMonoSources = numMono;
1217 device->lNumStereoSources = numStereo;
1218 device->NumAuxSends = numSends;
1221 if(ALCdevice_ResetPlayback(device) == ALC_FALSE)
1223 alcSetError(device, ALC_INVALID_DEVICE);
1224 aluHandleDisconnect(device);
1225 ProcessContext(NULL);
1226 return NULL;
1229 for(i = 0;i < device->NumContexts;i++)
1231 ALCcontext *context = device->Contexts[i];
1232 ALeffectslot *slot;
1233 ALsource *source;
1235 SuspendContext(context);
1236 for(slot = context->AuxiliaryEffectSlot;slot != NULL;slot = slot->next)
1238 if(!slot->EffectState)
1239 continue;
1241 if(ALEffect_DeviceUpdate(slot->EffectState, device) == AL_FALSE)
1243 alcSetError(device, ALC_INVALID_DEVICE);
1244 aluHandleDisconnect(device);
1245 ProcessContext(context);
1246 ProcessContext(NULL);
1247 ALCdevice_StopPlayback(device);
1248 return NULL;
1250 ALEffect_Update(slot->EffectState, context, &slot->effect);
1253 for(source = context->Source;source != NULL;source = source->next)
1255 ALuint s = device->NumAuxSends;
1256 while(s < MAX_SENDS)
1258 if(source->Send[s].Slot)
1259 source->Send[s].Slot->refcount--;
1260 source->Send[s].Slot = NULL;
1261 source->Send[s].WetFilter.type = 0;
1262 source->Send[s].WetFilter.filter = 0;
1263 s++;
1265 source->NeedsUpdate = AL_TRUE;
1267 aluInitPanning(context);
1268 ProcessContext(context);
1271 if(device->Bs2bLevel > 0 && device->Bs2bLevel <= 6)
1273 if(!device->Bs2b)
1275 device->Bs2b = calloc(1, sizeof(*device->Bs2b));
1276 bs2b_clear(device->Bs2b);
1278 bs2b_set_srate(device->Bs2b, device->Frequency);
1279 bs2b_set_level(device->Bs2b, device->Bs2bLevel);
1281 else
1283 free(device->Bs2b);
1284 device->Bs2b = NULL;
1287 temp = realloc(device->Contexts, (device->NumContexts+1) * sizeof(*device->Contexts));
1288 if(!temp)
1290 alcSetError(device, ALC_OUT_OF_MEMORY);
1291 ProcessContext(NULL);
1292 return NULL;
1294 device->Contexts = temp;
1296 ALContext = calloc(1, sizeof(ALCcontext));
1297 if(!ALContext)
1299 alcSetError(device, ALC_OUT_OF_MEMORY);
1300 ProcessContext(NULL);
1301 return NULL;
1304 device->Contexts[device->NumContexts++] = ALContext;
1305 ALContext->Device = device;
1307 InitContext(ALContext);
1308 aluInitPanning(ALContext);
1310 ALContext->next = g_pContextList;
1311 g_pContextList = ALContext;
1312 g_ulContextCount++;
1314 ProcessContext(NULL);
1316 return ALContext;
1321 alcDestroyContext
1323 Remove a Context
1325 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
1327 ALCcontext **list;
1328 ALuint i;
1330 if (IsContext(context))
1332 ALCdevice *Device = context->Device;
1334 if(Device->NumContexts == 1)
1335 ALCdevice_StopPlayback(Device);
1337 SuspendContext(NULL);
1339 for(i = 0;i < Device->NumContexts-1;i++)
1341 if(Device->Contexts[i] == context)
1343 memmove(&Device->Contexts[i], &Device->Contexts[i+1],
1344 (Device->NumContexts-i-1) * sizeof(*Device->Contexts));
1345 break;
1348 Device->NumContexts--;
1350 // Lock context
1351 SuspendContext(context);
1353 if(context->SourceCount > 0)
1355 #ifdef _DEBUG
1356 AL_PRINT("alcDestroyContext(): deleting %d Source(s)\n", context->SourceCount);
1357 #endif
1358 ReleaseALSources(context);
1360 if(context->AuxiliaryEffectSlotCount > 0)
1362 #ifdef _DEBUG
1363 AL_PRINT("alcDestroyContext(): deleting %d AuxiliaryEffectSlot(s)\n", context->AuxiliaryEffectSlotCount);
1364 #endif
1365 ReleaseALAuxiliaryEffectSlots(context);
1368 list = &g_pContextList;
1369 while(*list != context)
1370 list = &(*list)->next;
1372 *list = (*list)->next;
1373 g_ulContextCount--;
1375 // Unlock context
1376 ProcessContext(context);
1377 ProcessContext(NULL);
1379 ExitContext(context);
1381 // Free memory (MUST do this after ProcessContext)
1382 memset(context, 0, sizeof(ALCcontext));
1383 free(context);
1385 else
1386 alcSetError(NULL, ALC_INVALID_CONTEXT);
1391 alcGetCurrentContext
1393 Returns the currently active Context
1395 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1397 ALCcontext *pContext;
1399 if((pContext=GetContextSuspended()) != NULL)
1400 ProcessContext(pContext);
1402 return pContext;
1406 alcGetThreadContext
1408 Returns the currently active thread-local Context
1410 ALCcontext * ALCAPIENTRY alcGetThreadContext(void)
1412 ALCcontext *pContext = NULL;
1414 SuspendContext(NULL);
1416 pContext = tls_get(LocalContext);
1417 if(pContext && !IsContext(pContext))
1419 tls_set(LocalContext, NULL);
1420 pContext = NULL;
1423 ProcessContext(NULL);
1425 return pContext;
1430 alcGetContextsDevice
1432 Returns the Device that a particular Context is attached to
1434 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1436 ALCdevice *pDevice = NULL;
1438 SuspendContext(NULL);
1439 if (IsContext(pContext))
1440 pDevice = pContext->Device;
1441 else
1442 alcSetError(NULL, ALC_INVALID_CONTEXT);
1443 ProcessContext(NULL);
1445 return pDevice;
1450 alcMakeContextCurrent
1452 Makes the given Context the active Context
1454 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1456 ALCcontext *ALContext;
1457 ALboolean bReturn = AL_TRUE;
1459 SuspendContext(NULL);
1461 // context must be a valid Context or NULL
1462 if(context == NULL || IsContext(context))
1464 if((ALContext=GetContextSuspended()) != NULL)
1466 ALContext->InUse=AL_FALSE;
1467 ProcessContext(ALContext);
1470 if((ALContext=context) != NULL && ALContext->Device)
1472 SuspendContext(ALContext);
1473 ALContext->InUse=AL_TRUE;
1474 ProcessContext(ALContext);
1477 tls_set(LocalContext, NULL);
1479 else
1481 alcSetError(NULL, ALC_INVALID_CONTEXT);
1482 bReturn = AL_FALSE;
1485 ProcessContext(NULL);
1487 return bReturn;
1491 alcMakeCurrent
1493 Makes the given Context the active Context for the current thread
1495 ALCboolean ALCAPIENTRY alcMakeCurrent(ALCcontext *context)
1497 ALboolean bReturn = AL_TRUE;
1499 SuspendContext(NULL);
1501 // context must be a valid Context or NULL
1502 if(context == NULL || IsContext(context))
1503 tls_set(LocalContext, context);
1504 else
1506 alcSetError(NULL, ALC_INVALID_CONTEXT);
1507 bReturn = AL_FALSE;
1510 ProcessContext(NULL);
1512 return bReturn;
1516 // Sets the default channel order used by most non-WaveFormatEx-based APIs
1517 void SetDefaultChannelOrder(ALCdevice *device)
1519 switch(aluChannelsFromFormat(device->Format))
1521 case 1: device->DevChannels[0] = FRONT_CENTER; break;
1523 case 2: device->DevChannels[0] = FRONT_LEFT;
1524 device->DevChannels[1] = FRONT_RIGHT; break;
1526 case 4: device->DevChannels[0] = FRONT_LEFT;
1527 device->DevChannels[1] = FRONT_RIGHT;
1528 device->DevChannels[2] = BACK_LEFT;
1529 device->DevChannels[3] = BACK_RIGHT; break;
1531 case 6: device->DevChannels[0] = FRONT_LEFT;
1532 device->DevChannels[1] = FRONT_RIGHT;
1533 device->DevChannels[2] = BACK_LEFT;
1534 device->DevChannels[3] = BACK_RIGHT;
1535 device->DevChannels[4] = FRONT_CENTER;
1536 device->DevChannels[5] = LFE; break;
1538 case 7: device->DevChannels[0] = FRONT_LEFT;
1539 device->DevChannels[1] = FRONT_RIGHT;
1540 device->DevChannels[2] = FRONT_CENTER;
1541 device->DevChannels[3] = LFE;
1542 device->DevChannels[4] = BACK_CENTER;
1543 device->DevChannels[5] = SIDE_LEFT;
1544 device->DevChannels[6] = SIDE_RIGHT; break;
1546 case 8: device->DevChannels[0] = FRONT_LEFT;
1547 device->DevChannels[1] = FRONT_RIGHT;
1548 device->DevChannels[2] = BACK_LEFT;
1549 device->DevChannels[3] = BACK_RIGHT;
1550 device->DevChannels[4] = FRONT_CENTER;
1551 device->DevChannels[5] = LFE;
1552 device->DevChannels[6] = SIDE_LEFT;
1553 device->DevChannels[7] = SIDE_RIGHT; break;
1556 // Sets the default order used by WaveFormatEx
1557 void SetDefaultWFXChannelOrder(ALCdevice *device)
1559 switch(aluChannelsFromFormat(device->Format))
1561 case 1: device->DevChannels[0] = FRONT_CENTER; break;
1563 case 2: device->DevChannels[0] = FRONT_LEFT;
1564 device->DevChannels[1] = FRONT_RIGHT; break;
1566 case 4: device->DevChannels[0] = FRONT_LEFT;
1567 device->DevChannels[1] = FRONT_RIGHT;
1568 device->DevChannels[2] = BACK_LEFT;
1569 device->DevChannels[3] = BACK_RIGHT; break;
1571 case 6: device->DevChannels[0] = FRONT_LEFT;
1572 device->DevChannels[1] = FRONT_RIGHT;
1573 device->DevChannels[2] = FRONT_CENTER;
1574 device->DevChannels[3] = LFE;
1575 device->DevChannels[4] = BACK_LEFT;
1576 device->DevChannels[5] = BACK_RIGHT; break;
1578 case 7: device->DevChannels[0] = FRONT_LEFT;
1579 device->DevChannels[1] = FRONT_RIGHT;
1580 device->DevChannels[2] = FRONT_CENTER;
1581 device->DevChannels[3] = LFE;
1582 device->DevChannels[4] = BACK_CENTER;
1583 device->DevChannels[5] = SIDE_LEFT;
1584 device->DevChannels[6] = SIDE_RIGHT; break;
1586 case 8: device->DevChannels[0] = FRONT_LEFT;
1587 device->DevChannels[1] = FRONT_RIGHT;
1588 device->DevChannels[2] = FRONT_CENTER;
1589 device->DevChannels[3] = LFE;
1590 device->DevChannels[4] = BACK_LEFT;
1591 device->DevChannels[5] = BACK_RIGHT;
1592 device->DevChannels[6] = SIDE_LEFT;
1593 device->DevChannels[7] = SIDE_RIGHT; break;
1597 static ALenum GetFormatFromString(const char *str)
1599 if(strcasecmp(str, "AL_FORMAT_MONO32") == 0) return AL_FORMAT_MONO_FLOAT32;
1600 if(strcasecmp(str, "AL_FORMAT_STEREO32") == 0) return AL_FORMAT_STEREO_FLOAT32;
1601 if(strcasecmp(str, "AL_FORMAT_QUAD32") == 0) return AL_FORMAT_QUAD32;
1602 if(strcasecmp(str, "AL_FORMAT_51CHN32") == 0) return AL_FORMAT_51CHN32;
1603 if(strcasecmp(str, "AL_FORMAT_61CHN32") == 0) return AL_FORMAT_61CHN32;
1604 if(strcasecmp(str, "AL_FORMAT_71CHN32") == 0) return AL_FORMAT_71CHN32;
1606 if(strcasecmp(str, "AL_FORMAT_MONO16") == 0) return AL_FORMAT_MONO16;
1607 if(strcasecmp(str, "AL_FORMAT_STEREO16") == 0) return AL_FORMAT_STEREO16;
1608 if(strcasecmp(str, "AL_FORMAT_QUAD16") == 0) return AL_FORMAT_QUAD16;
1609 if(strcasecmp(str, "AL_FORMAT_51CHN16") == 0) return AL_FORMAT_51CHN16;
1610 if(strcasecmp(str, "AL_FORMAT_61CHN16") == 0) return AL_FORMAT_61CHN16;
1611 if(strcasecmp(str, "AL_FORMAT_71CHN16") == 0) return AL_FORMAT_71CHN16;
1613 if(strcasecmp(str, "AL_FORMAT_MONO8") == 0) return AL_FORMAT_MONO8;
1614 if(strcasecmp(str, "AL_FORMAT_STEREO8") == 0) return AL_FORMAT_STEREO8;
1615 if(strcasecmp(str, "AL_FORMAT_QUAD8") == 0) return AL_FORMAT_QUAD8;
1616 if(strcasecmp(str, "AL_FORMAT_51CHN8") == 0) return AL_FORMAT_51CHN8;
1617 if(strcasecmp(str, "AL_FORMAT_61CHN8") == 0) return AL_FORMAT_61CHN8;
1618 if(strcasecmp(str, "AL_FORMAT_71CHN8") == 0) return AL_FORMAT_71CHN8;
1620 AL_PRINT("Unknown format: \"%s\"\n", str);
1621 return AL_FORMAT_STEREO16;
1625 alcOpenDevice
1627 Open the Device specified.
1629 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1631 ALboolean bDeviceFound = AL_FALSE;
1632 ALCdevice *device;
1633 ALint i;
1635 if(deviceName && !deviceName[0])
1636 deviceName = NULL;
1638 device = malloc(sizeof(ALCdevice));
1639 if (device)
1641 const char *fmt;
1643 //Initialise device structure
1644 memset(device, 0, sizeof(ALCdevice));
1646 //Validate device
1647 device->Connected = ALC_TRUE;
1648 device->IsCaptureDevice = AL_FALSE;
1649 device->LastError = ALC_NO_ERROR;
1651 device->Bs2b = NULL;
1652 device->szDeviceName = NULL;
1654 device->Contexts = NULL;
1655 device->NumContexts = 0;
1657 //Set output format
1658 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1659 if(device->Frequency < 8000)
1660 device->Frequency = 8000;
1662 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1663 device->Format = GetFormatFromString(fmt);
1665 device->NumUpdates = GetConfigValueInt(NULL, "periods", 4);
1666 if(device->NumUpdates < 2)
1667 device->NumUpdates = 4;
1669 i = GetConfigValueInt(NULL, "refresh", 4096);
1670 if(i <= 0) i = 4096;
1672 device->UpdateSize = GetConfigValueInt(NULL, "period_size", i/device->NumUpdates);
1673 if(device->UpdateSize <= 0)
1674 device->UpdateSize = i/device->NumUpdates;
1676 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1677 if((ALint)device->MaxNoOfSources <= 0)
1678 device->MaxNoOfSources = 256;
1680 device->AuxiliaryEffectSlotMax = GetConfigValueInt(NULL, "slots", 4);
1681 if((ALint)device->AuxiliaryEffectSlotMax <= 0)
1682 device->AuxiliaryEffectSlotMax = 4;
1684 device->lNumStereoSources = 1;
1685 device->lNumMonoSources = device->MaxNoOfSources - device->lNumStereoSources;
1687 device->NumAuxSends = GetConfigValueInt(NULL, "sends", MAX_SENDS);
1688 if(device->NumAuxSends > MAX_SENDS)
1689 device->NumAuxSends = MAX_SENDS;
1691 device->Bs2bLevel = GetConfigValueInt(NULL, "cf_level", 0);
1693 if(aluChannelsFromFormat(device->Format) <= 2)
1695 device->HeadDampen = GetConfigValueFloat(NULL, "head_dampen", DEFAULT_HEAD_DAMPEN);
1696 device->HeadDampen = __min(device->HeadDampen, 1.0f);
1697 device->HeadDampen = __max(device->HeadDampen, 0.0f);
1699 else
1700 device->HeadDampen = 0.0f;
1702 // Find a playback device to open
1703 SuspendContext(NULL);
1704 for(i = 0;BackendList[i].Init;i++)
1706 device->Funcs = &BackendList[i].Funcs;
1707 if(ALCdevice_OpenPlayback(device, deviceName))
1709 device->next = g_pDeviceList;
1710 g_pDeviceList = device;
1711 g_ulDeviceCount++;
1713 bDeviceFound = AL_TRUE;
1714 break;
1717 ProcessContext(NULL);
1719 if (!bDeviceFound)
1721 // No suitable output device found
1722 alcSetError(NULL, ALC_INVALID_VALUE);
1723 free(device);
1724 device = NULL;
1727 else
1728 alcSetError(NULL, ALC_OUT_OF_MEMORY);
1730 return device;
1735 alcCloseDevice
1737 Close the specified Device
1739 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1741 ALCboolean bReturn = ALC_FALSE;
1742 ALCdevice **list;
1744 if(IsDevice(pDevice) && !pDevice->IsCaptureDevice)
1746 SuspendContext(NULL);
1748 list = &g_pDeviceList;
1749 while(*list != pDevice)
1750 list = &(*list)->next;
1752 *list = (*list)->next;
1753 g_ulDeviceCount--;
1755 ProcessContext(NULL);
1757 if(pDevice->NumContexts > 0)
1759 #ifdef _DEBUG
1760 AL_PRINT("alcCloseDevice(): destroying %u Context(s)\n", pDevice->NumContexts);
1761 #endif
1762 while(pDevice->NumContexts > 0)
1763 alcDestroyContext(pDevice->Contexts[0]);
1765 ALCdevice_ClosePlayback(pDevice);
1767 if(pDevice->BufferCount > 0)
1769 #ifdef _DEBUG
1770 AL_PRINT("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice->BufferCount);
1771 #endif
1772 ReleaseALBuffers(pDevice);
1774 if(pDevice->EffectCount > 0)
1776 #ifdef _DEBUG
1777 AL_PRINT("alcCloseDevice(): deleting %d Effect(s)\n", pDevice->EffectCount);
1778 #endif
1779 ReleaseALEffects(pDevice);
1781 if(pDevice->FilterCount > 0)
1783 #ifdef _DEBUG
1784 AL_PRINT("alcCloseDevice(): deleting %d Filter(s)\n", pDevice->FilterCount);
1785 #endif
1786 ReleaseALFilters(pDevice);
1788 if(pDevice->DatabufferCount > 0)
1790 #ifdef _DEBUG
1791 AL_PRINT("alcCloseDevice(): deleting %d Databuffer(s)\n", pDevice->DatabufferCount);
1792 #endif
1793 ReleaseALDatabuffers(pDevice);
1796 free(pDevice->Bs2b);
1797 pDevice->Bs2b = NULL;
1799 free(pDevice->szDeviceName);
1800 pDevice->szDeviceName = NULL;
1802 free(pDevice->Contexts);
1803 pDevice->Contexts = NULL;
1805 //Release device structure
1806 memset(pDevice, 0, sizeof(ALCdevice));
1807 free(pDevice);
1809 bReturn = ALC_TRUE;
1811 else
1812 alcSetError(pDevice, ALC_INVALID_DEVICE);
1814 return bReturn;
1818 ALCvoid ReleaseALC(ALCvoid)
1820 free(alcDeviceList); alcDeviceList = NULL;
1821 alcDeviceListSize = 0;
1822 free(alcAllDeviceList); alcAllDeviceList = NULL;
1823 alcAllDeviceListSize = 0;
1824 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
1825 alcCaptureDeviceListSize = 0;
1827 free(alcDefaultDeviceSpecifier);
1828 alcDefaultDeviceSpecifier = NULL;
1829 free(alcDefaultAllDeviceSpecifier);
1830 alcDefaultAllDeviceSpecifier = NULL;
1831 free(alcCaptureDefaultDeviceSpecifier);
1832 alcCaptureDefaultDeviceSpecifier = NULL;
1834 #ifdef _DEBUG
1835 if(g_ulDeviceCount > 0)
1836 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
1837 #endif
1839 while(g_pDeviceList)
1841 if(g_pDeviceList->IsCaptureDevice)
1842 alcCaptureCloseDevice(g_pDeviceList);
1843 else
1844 alcCloseDevice(g_pDeviceList);
1848 ///////////////////////////////////////////////////////