Don't use deprecated macros
[openal-soft.git] / Alc / ALc.c
blob8434d4295df9b44f28e9f20a97e7ca91f856156a
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 size_t alcDeviceListSize;
173 static ALCchar *alcAllDeviceList;
174 static size_t alcAllDeviceListSize;
175 static ALCchar *alcCaptureDeviceList;
176 static size_t alcCaptureDeviceListSize;
177 // Default is always the first in the list
178 static ALCchar *alcDefaultDeviceSpecifier;
179 static ALCchar *alcDefaultAllDeviceSpecifier;
180 static ALCchar *alcCaptureDefaultDeviceSpecifier;
183 static const ALCchar alcNoDeviceExtList[] =
184 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
185 "ALC_EXTX_thread_local_context";
186 static const ALCchar alcExtensionList[] =
187 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
188 "ALC_EXT_disconnect ALC_EXT_EFX ALC_EXTX_thread_local_context";
189 static const ALCint alcMajorVersion = 1;
190 static const ALCint alcMinorVersion = 1;
192 static const ALCint alcEFXMajorVersion = 1;
193 static const ALCint alcEFXMinorVersion = 0;
195 ///////////////////////////////////////////////////////
198 ///////////////////////////////////////////////////////
199 // Global Variables
201 static ALCdevice *g_pDeviceList = NULL;
202 static ALCuint g_ulDeviceCount = 0;
204 static CRITICAL_SECTION g_csMutex;
206 // Context List
207 static ALCcontext *g_pContextList = NULL;
208 static ALCuint g_ulContextCount = 0;
210 // Thread-local current context
211 static tls_type LocalContext;
213 // Context Error
214 static ALCenum g_eLastContextError = ALC_NO_ERROR;
216 // Default context extensions
217 static const ALchar alExtList[] =
218 "AL_EXTX_buffer_sub_data AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE "
219 "AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS "
220 "AL_EXT_MULAW AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET "
221 "AL_EXTX_sample_buffer_object AL_EXT_source_distance_model "
222 "AL_LOKI_quadriphonic";
224 // Mixing Priority Level
225 ALint RTPrioLevel;
227 // Resampler Quality
228 resampler_t DefaultResampler;
230 ///////////////////////////////////////////////////////
233 ///////////////////////////////////////////////////////
234 // ALC Related helper functions
235 #ifdef _WIN32
236 static void alc_init(void);
237 static void alc_deinit(void);
239 BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
241 (void)lpReserved;
243 // Perform actions based on the reason for calling.
244 switch(ul_reason_for_call)
246 case DLL_PROCESS_ATTACH:
247 DisableThreadLibraryCalls(hModule);
248 alc_init();
249 break;
251 case DLL_PROCESS_DETACH:
252 alc_deinit();
253 break;
255 return TRUE;
257 #else
258 #ifdef HAVE_GCC_DESTRUCTOR
259 static void alc_init(void) __attribute__((constructor));
260 static void alc_deinit(void) __attribute__((destructor));
261 #endif
262 #endif
264 static void alc_init(void)
266 int i;
267 const char *devs, *str;
269 InitializeCriticalSection(&g_csMutex);
270 ALTHUNK_INIT();
271 ReadALConfig();
273 tls_create(&LocalContext);
275 RTPrioLevel = GetConfigValueInt(NULL, "rt-prio", 0);
277 DefaultResampler = GetConfigValueInt(NULL, "resampler", RESAMPLER_DEFAULT);
278 if(DefaultResampler >= RESAMPLER_MAX || DefaultResampler <= RESAMPLER_MIN)
279 DefaultResampler = RESAMPLER_DEFAULT;
281 devs = GetConfigValue(NULL, "drivers", "");
282 if(devs[0])
284 int n;
285 size_t len;
286 const char *next = devs;
287 int endlist;
289 i = 0;
290 do {
291 devs = next;
292 next = strchr(devs, ',');
294 if(!devs[0] || devs[0] == ',')
296 endlist = 0;
297 continue;
299 endlist = 1;
301 len = (next ? ((size_t)(next-devs)) : strlen(devs));
302 for(n = i;BackendList[n].Init;n++)
304 if(len == strlen(BackendList[n].name) &&
305 strncmp(BackendList[n].name, devs, len) == 0)
307 BackendInfo Bkp = BackendList[n];
308 while(n > i)
310 BackendList[n] = BackendList[n-1];
311 --n;
313 BackendList[n] = Bkp;
315 i++;
316 break;
319 } while(next++);
321 if(endlist)
323 BackendList[i].name = NULL;
324 BackendList[i].Init = NULL;
325 BackendList[i].Deinit = NULL;
326 BackendList[i].Probe = NULL;
330 for(i = 0;BackendList[i].Init;i++)
331 BackendList[i].Init(&BackendList[i].Funcs);
333 DuplicateStereo = GetConfigValueBool(NULL, "stereodup", 0);
335 str = GetConfigValue(NULL, "excludefx", "");
336 if(str[0])
338 const struct {
339 const char *name;
340 int type;
341 } EffectList[] = {
342 { "eaxreverb", EAXREVERB },
343 { "reverb", REVERB },
344 { "echo", ECHO },
345 { NULL, 0 }
347 int n;
348 size_t len;
349 const char *next = str;
351 do {
352 str = next;
353 next = strchr(str, ',');
355 if(!str[0] || next == str)
356 continue;
358 len = (next ? ((size_t)(next-str)) : strlen(str));
359 for(n = 0;EffectList[n].name;n++)
361 if(len == strlen(EffectList[n].name) &&
362 strncmp(EffectList[n].name, str, len) == 0)
363 DisabledEffects[EffectList[n].type] = AL_TRUE;
365 } while(next++);
369 static void alc_deinit(void)
371 int i;
373 ReleaseALC();
375 for(i = 0;BackendList[i].Deinit;i++)
376 BackendList[i].Deinit();
378 tls_delete(LocalContext);
380 FreeALConfig();
381 ALTHUNK_EXIT();
382 DeleteCriticalSection(&g_csMutex);
386 static void ProbeDeviceList()
388 ALint i;
390 free(alcDeviceList); alcDeviceList = NULL;
391 alcDeviceListSize = 0;
393 for(i = 0;BackendList[i].Probe;i++)
394 BackendList[i].Probe(DEVICE_PROBE);
397 static void ProbeAllDeviceList()
399 ALint i;
401 free(alcAllDeviceList); alcAllDeviceList = NULL;
402 alcAllDeviceListSize = 0;
404 for(i = 0;BackendList[i].Probe;i++)
405 BackendList[i].Probe(ALL_DEVICE_PROBE);
408 static void ProbeCaptureDeviceList()
410 ALint i;
412 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
413 alcCaptureDeviceListSize = 0;
415 for(i = 0;BackendList[i].Probe;i++)
416 BackendList[i].Probe(CAPTURE_DEVICE_PROBE);
420 #define DECL_APPEND_LIST_FUNC(type) \
421 void Append##type##List(const ALCchar *name) \
423 size_t len = strlen(name); \
424 void *temp; \
426 if(len == 0) \
427 return; \
429 temp = realloc(alc##type##List, alc##type##ListSize + len + 2); \
430 if(!temp) \
432 AL_PRINT("Realloc failed to add %s!\n", name); \
433 return; \
435 alc##type##List = temp; \
436 sprintf(alc##type##List+alc##type##ListSize, "%s", name); \
437 alc##type##ListSize += len+1; \
438 alc##type##List[alc##type##ListSize] = 0; \
441 DECL_APPEND_LIST_FUNC(Device)
442 DECL_APPEND_LIST_FUNC(AllDevice)
443 DECL_APPEND_LIST_FUNC(CaptureDevice)
446 void al_print(const char *fname, unsigned int line, const char *fmt, ...)
448 const char *fn;
449 char str[256];
450 int i;
452 fn = strrchr(fname, '/');
453 if(!fn) fn = strrchr(fname, '\\');;
454 if(!fn) fn = fname;
455 else fn += 1;
457 i = snprintf(str, sizeof(str), "AL lib: %s:%d: ", fn, line);
458 if(i < (int)sizeof(str) && i > 0)
460 va_list ap;
461 va_start(ap, fmt);
462 vsnprintf(str+i, sizeof(str)-i, fmt, ap);
463 va_end(ap);
465 str[sizeof(str)-1] = 0;
467 fprintf(stderr, "%s", str);
470 void EnableRTPrio(ALint level)
472 ALboolean failed;
474 #ifdef _WIN32
475 if(level > 0)
476 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
477 else
478 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
479 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM)
480 struct sched_param param;
482 if(level > 0)
484 /* Use the minimum real-time priority possible for now (on Linux this
485 * should be 1 for SCHED_RR) */
486 param.sched_priority = sched_get_priority_min(SCHED_RR);
487 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
489 else
491 param.sched_priority = 0;
492 failed = !!pthread_setschedparam(pthread_self(), SCHED_OTHER, &param);
494 #else
495 /* Real-time priority not available */
496 failed = !!level;
497 #endif
498 if(failed)
499 AL_PRINT("Failed to set priority level for thread\n");
504 IsDevice
506 Check pDevice is a valid Device pointer
508 static ALCboolean IsDevice(ALCdevice *pDevice)
510 ALCdevice *pTempDevice;
512 SuspendContext(NULL);
514 pTempDevice = g_pDeviceList;
515 while(pTempDevice && pTempDevice != pDevice)
516 pTempDevice = pTempDevice->next;
518 ProcessContext(NULL);
520 return (pTempDevice ? ALC_TRUE : ALC_FALSE);
524 IsContext
526 Check pContext is a valid Context pointer
528 static ALCboolean IsContext(ALCcontext *pContext)
530 ALCcontext *pTempContext;
532 SuspendContext(NULL);
534 pTempContext = g_pContextList;
535 while (pTempContext && pTempContext != pContext)
536 pTempContext = pTempContext->next;
538 ProcessContext(NULL);
540 return (pTempContext ? ALC_TRUE : ALC_FALSE);
545 alcSetError
547 Store latest ALC Error
549 ALCvoid alcSetError(ALCdevice *device, ALenum errorCode)
551 if(IsDevice(device))
552 device->LastError = errorCode;
553 else
554 g_eLastContextError = errorCode;
559 SuspendContext
561 Thread-safe entry
563 ALCvoid SuspendContext(ALCcontext *pContext)
565 (void)pContext;
566 EnterCriticalSection(&g_csMutex);
571 ProcessContext
573 Thread-safe exit
575 ALCvoid ProcessContext(ALCcontext *pContext)
577 (void)pContext;
578 LeaveCriticalSection(&g_csMutex);
583 GetContextSuspended
585 Returns the currently active Context, in a locked state
587 ALCcontext *GetContextSuspended(void)
589 ALCcontext *pContext = NULL;
591 SuspendContext(NULL);
593 pContext = tls_get(LocalContext);
594 if(pContext && !IsContext(pContext))
596 tls_set(LocalContext, NULL);
597 pContext = NULL;
599 if(!pContext)
601 pContext = g_pContextList;
602 while(pContext && !pContext->InUse)
603 pContext = pContext->next;
605 if(pContext)
606 SuspendContext(pContext);
608 ProcessContext(NULL);
610 return pContext;
615 InitContext
617 Initialize Context variables
619 static ALvoid InitContext(ALCcontext *pContext)
621 //Initialise listener
622 pContext->Listener.Gain = 1.0f;
623 pContext->Listener.MetersPerUnit = 1.0f;
624 pContext->Listener.Position[0] = 0.0f;
625 pContext->Listener.Position[1] = 0.0f;
626 pContext->Listener.Position[2] = 0.0f;
627 pContext->Listener.Velocity[0] = 0.0f;
628 pContext->Listener.Velocity[1] = 0.0f;
629 pContext->Listener.Velocity[2] = 0.0f;
630 pContext->Listener.Forward[0] = 0.0f;
631 pContext->Listener.Forward[1] = 0.0f;
632 pContext->Listener.Forward[2] = -1.0f;
633 pContext->Listener.Up[0] = 0.0f;
634 pContext->Listener.Up[1] = 1.0f;
635 pContext->Listener.Up[2] = 0.0f;
637 //Validate pContext
638 pContext->LastError = AL_NO_ERROR;
639 pContext->InUse = AL_FALSE;
640 pContext->Suspended = AL_FALSE;
642 //Set globals
643 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
644 pContext->SourceDistanceModel = AL_FALSE;
645 pContext->DopplerFactor = 1.0f;
646 pContext->DopplerVelocity = 1.0f;
647 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
649 pContext->ExtensionList = alExtList;
654 ExitContext
656 Clean up Context, destroy any remaining Sources
658 static ALCvoid ExitContext(ALCcontext *pContext)
660 //Invalidate context
661 pContext->LastError = AL_NO_ERROR;
662 pContext->InUse = AL_FALSE;
665 ///////////////////////////////////////////////////////
668 ///////////////////////////////////////////////////////
669 // ALC Functions calls
672 // This should probably move to another c file but for now ...
673 ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
675 ALCboolean DeviceFound = ALC_FALSE;
676 ALCdevice *pDevice = NULL;
677 ALCint i;
679 if(SampleSize <= 0)
681 alcSetError(NULL, ALC_INVALID_VALUE);
682 return NULL;
685 if(deviceName && !deviceName[0])
686 deviceName = NULL;
688 pDevice = malloc(sizeof(ALCdevice));
689 if (pDevice)
691 //Initialise device structure
692 memset(pDevice, 0, sizeof(ALCdevice));
694 //Validate device
695 pDevice->Connected = ALC_TRUE;
696 pDevice->IsCaptureDevice = AL_TRUE;
698 pDevice->szDeviceName = NULL;
700 pDevice->Frequency = frequency;
701 pDevice->Format = format;
702 pDevice->UpdateSize = SampleSize;
703 pDevice->NumUpdates = 1;
705 SuspendContext(NULL);
706 for(i = 0;BackendList[i].Init;i++)
708 pDevice->Funcs = &BackendList[i].Funcs;
709 if(ALCdevice_OpenCapture(pDevice, deviceName))
711 pDevice->next = g_pDeviceList;
712 g_pDeviceList = pDevice;
713 g_ulDeviceCount++;
715 DeviceFound = ALC_TRUE;
716 break;
719 ProcessContext(NULL);
721 if(!DeviceFound)
723 alcSetError(NULL, ALC_INVALID_VALUE);
724 free(pDevice);
725 pDevice = NULL;
728 else
729 alcSetError(NULL, ALC_OUT_OF_MEMORY);
731 return pDevice;
734 ALC_API ALCboolean ALC_APIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
736 ALCboolean bReturn = ALC_FALSE;
737 ALCdevice **list;
739 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
741 SuspendContext(NULL);
743 list = &g_pDeviceList;
744 while(*list != pDevice)
745 list = &(*list)->next;
747 *list = (*list)->next;
748 g_ulDeviceCount--;
750 ProcessContext(NULL);
752 ALCdevice_CloseCapture(pDevice);
754 free(pDevice->szDeviceName);
755 pDevice->szDeviceName = NULL;
757 free(pDevice);
759 bReturn = ALC_TRUE;
761 else
762 alcSetError(pDevice, ALC_INVALID_DEVICE);
764 return bReturn;
767 ALC_API void ALC_APIENTRY alcCaptureStart(ALCdevice *pDevice)
769 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
771 SuspendContext(NULL);
772 ALCdevice_StartCapture(pDevice);
773 ProcessContext(NULL);
775 else
776 alcSetError(pDevice, ALC_INVALID_DEVICE);
779 ALC_API void ALC_APIENTRY alcCaptureStop(ALCdevice *pDevice)
781 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
783 SuspendContext(NULL);
784 ALCdevice_StopCapture(pDevice);
785 ProcessContext(NULL);
787 else
788 alcSetError(pDevice, ALC_INVALID_DEVICE);
791 ALC_API void ALC_APIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
793 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
795 SuspendContext(NULL);
796 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
797 ProcessContext(NULL);
799 else
800 alcSetError(pDevice, ALC_INVALID_DEVICE);
804 alcGetError
806 Return last ALC generated error code
808 ALC_API ALCenum ALC_APIENTRY alcGetError(ALCdevice *device)
810 ALCenum errorCode = ALC_NO_ERROR;
812 if(IsDevice(device))
814 errorCode = device->LastError;
815 device->LastError = ALC_NO_ERROR;
817 else
819 errorCode = g_eLastContextError;
820 g_eLastContextError = ALC_NO_ERROR;
822 return errorCode;
827 alcSuspendContext
829 Not functional
831 ALC_API ALCvoid ALC_APIENTRY alcSuspendContext(ALCcontext *pContext)
833 SuspendContext(NULL);
834 if(IsContext(pContext))
835 pContext->Suspended = AL_TRUE;
836 ProcessContext(NULL);
841 alcProcessContext
843 Not functional
845 ALC_API ALCvoid ALC_APIENTRY alcProcessContext(ALCcontext *pContext)
847 SuspendContext(NULL);
848 if(IsContext(pContext))
849 pContext->Suspended = AL_FALSE;
850 ProcessContext(NULL);
855 alcGetString
857 Returns information about the Device, and error strings
859 ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
861 const ALCchar *value = NULL;
863 switch (param)
865 case ALC_NO_ERROR:
866 value = alcNoError;
867 break;
869 case ALC_INVALID_ENUM:
870 value = alcErrInvalidEnum;
871 break;
873 case ALC_INVALID_VALUE:
874 value = alcErrInvalidValue;
875 break;
877 case ALC_INVALID_DEVICE:
878 value = alcErrInvalidDevice;
879 break;
881 case ALC_INVALID_CONTEXT:
882 value = alcErrInvalidContext;
883 break;
885 case ALC_OUT_OF_MEMORY:
886 value = alcErrOutOfMemory;
887 break;
889 case ALC_DEVICE_SPECIFIER:
890 if(IsDevice(pDevice))
891 value = pDevice->szDeviceName;
892 else
894 ProbeDeviceList();
895 value = alcDeviceList;
897 break;
899 case ALC_ALL_DEVICES_SPECIFIER:
900 ProbeAllDeviceList();
901 value = alcAllDeviceList;
902 break;
904 case ALC_CAPTURE_DEVICE_SPECIFIER:
905 if(IsDevice(pDevice))
906 value = pDevice->szDeviceName;
907 else
909 ProbeCaptureDeviceList();
910 value = alcCaptureDeviceList;
912 break;
914 /* Default devices are always first in the list */
915 case ALC_DEFAULT_DEVICE_SPECIFIER:
916 if(!alcDeviceList)
917 ProbeDeviceList();
919 free(alcDefaultDeviceSpecifier);
920 alcDefaultDeviceSpecifier = strdup(alcDeviceList ? alcDeviceList : "");
921 if(!alcDefaultDeviceSpecifier)
922 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
923 value = alcDefaultDeviceSpecifier;
924 break;
926 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
927 if(!alcAllDeviceList)
928 ProbeAllDeviceList();
930 free(alcDefaultAllDeviceSpecifier);
931 alcDefaultAllDeviceSpecifier = strdup(alcAllDeviceList ?
932 alcAllDeviceList : "");
933 if(!alcDefaultAllDeviceSpecifier)
934 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
935 value = alcDefaultAllDeviceSpecifier;
936 break;
938 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
939 if(!alcCaptureDeviceList)
940 ProbeCaptureDeviceList();
942 free(alcCaptureDefaultDeviceSpecifier);
943 alcCaptureDefaultDeviceSpecifier = strdup(alcCaptureDeviceList ?
944 alcCaptureDeviceList : "");
945 if(!alcCaptureDefaultDeviceSpecifier)
946 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
947 value = alcCaptureDefaultDeviceSpecifier;
948 break;
950 case ALC_EXTENSIONS:
951 if(IsDevice(pDevice))
952 value = alcExtensionList;
953 else
954 value = alcNoDeviceExtList;
955 break;
957 default:
958 alcSetError(pDevice, ALC_INVALID_ENUM);
959 break;
962 return value;
967 alcGetIntegerv
969 Returns information about the Device and the version of Open AL
971 ALC_API ALCvoid ALC_APIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
973 if(size == 0 || data == NULL)
975 alcSetError(device, ALC_INVALID_VALUE);
976 return;
979 if(IsDevice(device) && device->IsCaptureDevice)
981 SuspendContext(NULL);
983 // Capture device
984 switch (param)
986 case ALC_CAPTURE_SAMPLES:
987 *data = ALCdevice_AvailableSamples(device);
988 break;
990 case ALC_CONNECTED:
991 *data = device->Connected;
992 break;
994 default:
995 alcSetError(device, ALC_INVALID_ENUM);
996 break;
999 ProcessContext(NULL);
1000 return;
1003 // Playback Device
1004 switch (param)
1006 case ALC_MAJOR_VERSION:
1007 *data = alcMajorVersion;
1008 break;
1010 case ALC_MINOR_VERSION:
1011 *data = alcMinorVersion;
1012 break;
1014 case ALC_EFX_MAJOR_VERSION:
1015 *data = alcEFXMajorVersion;
1016 break;
1018 case ALC_EFX_MINOR_VERSION:
1019 *data = alcEFXMinorVersion;
1020 break;
1022 case ALC_MAX_AUXILIARY_SENDS:
1023 if(!IsDevice(device))
1024 alcSetError(device, ALC_INVALID_DEVICE);
1025 else
1026 *data = device->NumAuxSends;
1027 break;
1029 case ALC_ATTRIBUTES_SIZE:
1030 if(!IsDevice(device))
1031 alcSetError(device, ALC_INVALID_DEVICE);
1032 else
1033 *data = 13;
1034 break;
1036 case ALC_ALL_ATTRIBUTES:
1037 if(!IsDevice(device))
1038 alcSetError(device, ALC_INVALID_DEVICE);
1039 else if (size < 13)
1040 alcSetError(device, ALC_INVALID_VALUE);
1041 else
1043 int i = 0;
1045 SuspendContext(NULL);
1046 data[i++] = ALC_FREQUENCY;
1047 data[i++] = device->Frequency;
1049 data[i++] = ALC_REFRESH;
1050 data[i++] = device->Frequency / device->UpdateSize;
1052 data[i++] = ALC_SYNC;
1053 data[i++] = ALC_FALSE;
1055 data[i++] = ALC_MONO_SOURCES;
1056 data[i++] = device->lNumMonoSources;
1058 data[i++] = ALC_STEREO_SOURCES;
1059 data[i++] = device->lNumStereoSources;
1061 data[i++] = ALC_MAX_AUXILIARY_SENDS;
1062 data[i++] = device->NumAuxSends;
1064 data[i++] = 0;
1065 ProcessContext(NULL);
1067 break;
1069 case ALC_FREQUENCY:
1070 if(!IsDevice(device))
1071 alcSetError(device, ALC_INVALID_DEVICE);
1072 else
1073 *data = device->Frequency;
1074 break;
1076 case ALC_REFRESH:
1077 if(!IsDevice(device))
1078 alcSetError(device, ALC_INVALID_DEVICE);
1079 else
1080 *data = device->Frequency / device->UpdateSize;
1081 break;
1083 case ALC_SYNC:
1084 if(!IsDevice(device))
1085 alcSetError(device, ALC_INVALID_DEVICE);
1086 else
1087 *data = ALC_FALSE;
1088 break;
1090 case ALC_MONO_SOURCES:
1091 if(!IsDevice(device))
1092 alcSetError(device, ALC_INVALID_DEVICE);
1093 else
1094 *data = device->lNumMonoSources;
1095 break;
1097 case ALC_STEREO_SOURCES:
1098 if(!IsDevice(device))
1099 alcSetError(device, ALC_INVALID_DEVICE);
1100 else
1101 *data = device->lNumStereoSources;
1102 break;
1104 case ALC_CONNECTED:
1105 if(!IsDevice(device))
1106 alcSetError(device, ALC_INVALID_DEVICE);
1107 else
1108 *data = device->Connected;
1109 break;
1111 default:
1112 alcSetError(device, ALC_INVALID_ENUM);
1113 break;
1119 alcIsExtensionPresent
1121 Determines if there is support for a particular extension
1123 ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
1125 ALCboolean bResult = ALC_FALSE;
1127 if (extName)
1129 const char *ptr;
1130 size_t len;
1132 len = strlen(extName);
1133 ptr = (IsDevice(device) ? alcExtensionList : alcNoDeviceExtList);
1134 while(ptr && *ptr)
1136 if(strncasecmp(ptr, extName, len) == 0 &&
1137 (ptr[len] == '\0' || isspace(ptr[len])))
1139 bResult = ALC_TRUE;
1140 break;
1142 if((ptr=strchr(ptr, ' ')) != NULL)
1144 do {
1145 ++ptr;
1146 } while(isspace(*ptr));
1150 else
1151 alcSetError(device, ALC_INVALID_VALUE);
1153 return bResult;
1158 alcGetProcAddress
1160 Retrieves the function address for a particular extension function
1162 ALC_API ALCvoid* ALC_APIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
1164 ALCvoid *pFunction = NULL;
1165 ALsizei i = 0;
1167 if (funcName)
1169 while(alcFunctions[i].funcName &&
1170 strcmp(alcFunctions[i].funcName,funcName) != 0)
1171 i++;
1172 pFunction = alcFunctions[i].address;
1174 else
1175 alcSetError(device, ALC_INVALID_VALUE);
1177 return pFunction;
1182 alcGetEnumValue
1184 Get the value for a particular ALC Enumerated Value
1186 ALC_API ALCenum ALC_APIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
1188 ALsizei i = 0;
1189 ALCenum val;
1191 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
1192 i++;
1193 val = enumeration[i].value;
1195 if(!enumeration[i].enumName)
1196 alcSetError(device, ALC_INVALID_VALUE);
1198 return val;
1203 alcCreateContext
1205 Create and attach a Context to a particular Device.
1207 ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
1209 ALuint attrIdx, reqStereoSources;
1210 ALCcontext *ALContext;
1211 void *temp;
1212 ALuint i;
1214 SuspendContext(NULL);
1216 if(!IsDevice(device) || device->IsCaptureDevice || !device->Connected)
1218 alcSetError(device, ALC_INVALID_DEVICE);
1219 ProcessContext(NULL);
1220 return NULL;
1223 // Reset Context Last Error code
1224 device->LastError = ALC_NO_ERROR;
1226 // If a context is already running on the device, stop playback so the
1227 // device attributes can be updated
1228 if(device->NumContexts > 0)
1230 ProcessContext(NULL);
1231 ALCdevice_StopPlayback(device);
1232 SuspendContext(NULL);
1235 // Check for attributes
1236 if(attrList)
1238 ALCint level = device->Bs2bLevel;
1239 ALCuint freq = device->Frequency;
1240 ALCint numMono = device->lNumMonoSources;
1241 ALCint numStereo = device->lNumStereoSources;
1242 ALCuint numSends = device->NumAuxSends;
1244 attrIdx = 0;
1245 while(attrList[attrIdx])
1247 if(attrList[attrIdx] == ALC_FREQUENCY &&
1248 !ConfigValueExists(NULL, "frequency"))
1250 freq = attrList[attrIdx + 1];
1251 if(freq < 8000)
1252 freq = 8000;
1255 if(attrList[attrIdx] == ALC_STEREO_SOURCES)
1257 reqStereoSources = attrList[attrIdx + 1];
1258 if(reqStereoSources > device->MaxNoOfSources)
1259 reqStereoSources = device->MaxNoOfSources;
1261 numStereo = reqStereoSources;
1262 numMono = device->MaxNoOfSources - numStereo;
1265 if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS &&
1266 !ConfigValueExists(NULL, "sends"))
1268 numSends = attrList[attrIdx + 1];
1269 if(numSends > MAX_SENDS)
1270 numSends = MAX_SENDS;
1273 attrIdx += 2;
1276 device->UpdateSize = (ALuint64)device->UpdateSize * freq /
1277 device->Frequency;
1279 device->Bs2bLevel = level;
1280 device->Frequency = freq;
1281 device->lNumMonoSources = numMono;
1282 device->lNumStereoSources = numStereo;
1283 device->NumAuxSends = numSends;
1286 if(ALCdevice_ResetPlayback(device) == ALC_FALSE)
1288 alcSetError(device, ALC_INVALID_DEVICE);
1289 aluHandleDisconnect(device);
1290 ProcessContext(NULL);
1291 return NULL;
1294 for(i = 0;i < device->NumContexts;i++)
1296 ALCcontext *context = device->Contexts[i];
1297 ALeffectslot *slot;
1298 ALsource *source;
1300 SuspendContext(context);
1301 for(slot = context->EffectSlotList;slot != NULL;slot = slot->next)
1303 if(!slot->EffectState)
1304 continue;
1306 if(ALEffect_DeviceUpdate(slot->EffectState, device) == AL_FALSE)
1308 alcSetError(device, ALC_INVALID_DEVICE);
1309 aluHandleDisconnect(device);
1310 ProcessContext(context);
1311 ProcessContext(NULL);
1312 ALCdevice_StopPlayback(device);
1313 return NULL;
1315 ALEffect_Update(slot->EffectState, context, &slot->effect);
1318 for(source = context->SourceList;source != NULL;source = source->next)
1320 ALuint s = device->NumAuxSends;
1321 while(s < MAX_SENDS)
1323 if(source->Send[s].Slot)
1324 source->Send[s].Slot->refcount--;
1325 source->Send[s].Slot = NULL;
1326 source->Send[s].WetFilter.type = 0;
1327 source->Send[s].WetFilter.filter = 0;
1328 s++;
1330 source->NeedsUpdate = AL_TRUE;
1332 aluInitPanning(context);
1333 ProcessContext(context);
1336 if(device->Bs2bLevel > 0 && device->Bs2bLevel <= 6)
1338 if(!device->Bs2b)
1340 device->Bs2b = calloc(1, sizeof(*device->Bs2b));
1341 bs2b_clear(device->Bs2b);
1343 bs2b_set_srate(device->Bs2b, device->Frequency);
1344 bs2b_set_level(device->Bs2b, device->Bs2bLevel);
1346 else
1348 free(device->Bs2b);
1349 device->Bs2b = NULL;
1352 temp = realloc(device->Contexts, (device->NumContexts+1) * sizeof(*device->Contexts));
1353 if(!temp)
1355 alcSetError(device, ALC_OUT_OF_MEMORY);
1356 ProcessContext(NULL);
1357 return NULL;
1359 device->Contexts = temp;
1361 ALContext = calloc(1, sizeof(ALCcontext));
1362 if(!ALContext)
1364 alcSetError(device, ALC_OUT_OF_MEMORY);
1365 ProcessContext(NULL);
1366 return NULL;
1369 device->Contexts[device->NumContexts++] = ALContext;
1370 ALContext->Device = device;
1372 InitContext(ALContext);
1373 aluInitPanning(ALContext);
1375 ALContext->next = g_pContextList;
1376 g_pContextList = ALContext;
1377 g_ulContextCount++;
1379 ProcessContext(NULL);
1381 return ALContext;
1386 alcDestroyContext
1388 Remove a Context
1390 ALC_API ALCvoid ALC_APIENTRY alcDestroyContext(ALCcontext *context)
1392 ALCcontext **list;
1393 ALuint i;
1395 if (IsContext(context))
1397 ALCdevice *Device = context->Device;
1399 if(Device->NumContexts == 1)
1400 ALCdevice_StopPlayback(Device);
1402 SuspendContext(NULL);
1404 for(i = 0;i < Device->NumContexts-1;i++)
1406 if(Device->Contexts[i] == context)
1408 memmove(&Device->Contexts[i], &Device->Contexts[i+1],
1409 (Device->NumContexts-i-1) * sizeof(*Device->Contexts));
1410 break;
1413 Device->NumContexts--;
1415 // Lock context
1416 SuspendContext(context);
1418 if(context->SourceCount > 0)
1420 #ifdef _DEBUG
1421 AL_PRINT("alcDestroyContext(): deleting %d Source(s)\n", context->SourceCount);
1422 #endif
1423 ReleaseALSources(context);
1425 if(context->EffectSlotCount > 0)
1427 #ifdef _DEBUG
1428 AL_PRINT("alcDestroyContext(): deleting %d AuxiliaryEffectSlot(s)\n", context->EffectSlotCount);
1429 #endif
1430 ReleaseALAuxiliaryEffectSlots(context);
1433 list = &g_pContextList;
1434 while(*list != context)
1435 list = &(*list)->next;
1437 *list = (*list)->next;
1438 g_ulContextCount--;
1440 // Unlock context
1441 ProcessContext(context);
1442 ProcessContext(NULL);
1444 ExitContext(context);
1446 // Free memory (MUST do this after ProcessContext)
1447 memset(context, 0, sizeof(ALCcontext));
1448 free(context);
1450 else
1451 alcSetError(NULL, ALC_INVALID_CONTEXT);
1456 alcGetCurrentContext
1458 Returns the currently active Context
1460 ALC_API ALCcontext* ALC_APIENTRY alcGetCurrentContext(ALCvoid)
1462 ALCcontext *pContext;
1464 if((pContext=GetContextSuspended()) != NULL)
1465 ProcessContext(pContext);
1467 return pContext;
1471 alcGetThreadContext
1473 Returns the currently active thread-local Context
1475 ALCcontext* ALC_APIENTRY alcGetThreadContext(void)
1477 ALCcontext *pContext = NULL;
1479 SuspendContext(NULL);
1481 pContext = tls_get(LocalContext);
1482 if(pContext && !IsContext(pContext))
1484 tls_set(LocalContext, NULL);
1485 pContext = NULL;
1488 ProcessContext(NULL);
1490 return pContext;
1495 alcGetContextsDevice
1497 Returns the Device that a particular Context is attached to
1499 ALC_API ALCdevice* ALC_APIENTRY alcGetContextsDevice(ALCcontext *pContext)
1501 ALCdevice *pDevice = NULL;
1503 SuspendContext(NULL);
1504 if (IsContext(pContext))
1505 pDevice = pContext->Device;
1506 else
1507 alcSetError(NULL, ALC_INVALID_CONTEXT);
1508 ProcessContext(NULL);
1510 return pDevice;
1515 alcMakeContextCurrent
1517 Makes the given Context the active Context
1519 ALC_API ALCboolean ALC_APIENTRY alcMakeContextCurrent(ALCcontext *context)
1521 ALCcontext *ALContext;
1522 ALboolean bReturn = AL_TRUE;
1524 SuspendContext(NULL);
1526 // context must be a valid Context or NULL
1527 if(context == NULL || IsContext(context))
1529 ALContext = g_pContextList;
1530 while(ALContext && !ALContext->InUse)
1531 ALContext = ALContext->next;
1533 if(ALContext != NULL)
1535 SuspendContext(ALContext);
1536 ALContext->InUse=AL_FALSE;
1537 ProcessContext(ALContext);
1540 if((ALContext=context) != NULL && ALContext->Device)
1542 SuspendContext(ALContext);
1543 ALContext->InUse=AL_TRUE;
1544 ProcessContext(ALContext);
1547 tls_set(LocalContext, NULL);
1549 else
1551 alcSetError(NULL, ALC_INVALID_CONTEXT);
1552 bReturn = AL_FALSE;
1555 ProcessContext(NULL);
1557 return bReturn;
1561 alcMakeCurrent
1563 Makes the given Context the active Context for the current thread
1565 ALCboolean ALC_APIENTRY alcMakeCurrent(ALCcontext *context)
1567 ALboolean bReturn = AL_TRUE;
1569 SuspendContext(NULL);
1571 // context must be a valid Context or NULL
1572 if(context == NULL || IsContext(context))
1573 tls_set(LocalContext, context);
1574 else
1576 alcSetError(NULL, ALC_INVALID_CONTEXT);
1577 bReturn = AL_FALSE;
1580 ProcessContext(NULL);
1582 return bReturn;
1586 // Sets the default channel order used by most non-WaveFormatEx-based APIs
1587 void SetDefaultChannelOrder(ALCdevice *device)
1589 switch(aluChannelsFromFormat(device->Format))
1591 case 1: device->DevChannels[0] = FRONT_CENTER; break;
1593 case 2: device->DevChannels[0] = FRONT_LEFT;
1594 device->DevChannels[1] = FRONT_RIGHT; break;
1596 case 4: device->DevChannels[0] = FRONT_LEFT;
1597 device->DevChannels[1] = FRONT_RIGHT;
1598 device->DevChannels[2] = BACK_LEFT;
1599 device->DevChannels[3] = BACK_RIGHT; break;
1601 case 6: device->DevChannels[0] = FRONT_LEFT;
1602 device->DevChannels[1] = FRONT_RIGHT;
1603 device->DevChannels[2] = BACK_LEFT;
1604 device->DevChannels[3] = BACK_RIGHT;
1605 device->DevChannels[4] = FRONT_CENTER;
1606 device->DevChannels[5] = LFE; break;
1608 case 7: device->DevChannels[0] = FRONT_LEFT;
1609 device->DevChannels[1] = FRONT_RIGHT;
1610 device->DevChannels[2] = FRONT_CENTER;
1611 device->DevChannels[3] = LFE;
1612 device->DevChannels[4] = BACK_CENTER;
1613 device->DevChannels[5] = SIDE_LEFT;
1614 device->DevChannels[6] = SIDE_RIGHT; break;
1616 case 8: device->DevChannels[0] = FRONT_LEFT;
1617 device->DevChannels[1] = FRONT_RIGHT;
1618 device->DevChannels[2] = BACK_LEFT;
1619 device->DevChannels[3] = BACK_RIGHT;
1620 device->DevChannels[4] = FRONT_CENTER;
1621 device->DevChannels[5] = LFE;
1622 device->DevChannels[6] = SIDE_LEFT;
1623 device->DevChannels[7] = SIDE_RIGHT; break;
1626 // Sets the default order used by WaveFormatEx
1627 void SetDefaultWFXChannelOrder(ALCdevice *device)
1629 switch(aluChannelsFromFormat(device->Format))
1631 case 1: device->DevChannels[0] = FRONT_CENTER; break;
1633 case 2: device->DevChannels[0] = FRONT_LEFT;
1634 device->DevChannels[1] = FRONT_RIGHT; break;
1636 case 4: device->DevChannels[0] = FRONT_LEFT;
1637 device->DevChannels[1] = FRONT_RIGHT;
1638 device->DevChannels[2] = BACK_LEFT;
1639 device->DevChannels[3] = BACK_RIGHT; break;
1641 case 6: device->DevChannels[0] = FRONT_LEFT;
1642 device->DevChannels[1] = FRONT_RIGHT;
1643 device->DevChannels[2] = FRONT_CENTER;
1644 device->DevChannels[3] = LFE;
1645 device->DevChannels[4] = BACK_LEFT;
1646 device->DevChannels[5] = BACK_RIGHT; break;
1648 case 7: device->DevChannels[0] = FRONT_LEFT;
1649 device->DevChannels[1] = FRONT_RIGHT;
1650 device->DevChannels[2] = FRONT_CENTER;
1651 device->DevChannels[3] = LFE;
1652 device->DevChannels[4] = BACK_CENTER;
1653 device->DevChannels[5] = SIDE_LEFT;
1654 device->DevChannels[6] = SIDE_RIGHT; break;
1656 case 8: device->DevChannels[0] = FRONT_LEFT;
1657 device->DevChannels[1] = FRONT_RIGHT;
1658 device->DevChannels[2] = FRONT_CENTER;
1659 device->DevChannels[3] = LFE;
1660 device->DevChannels[4] = BACK_LEFT;
1661 device->DevChannels[5] = BACK_RIGHT;
1662 device->DevChannels[6] = SIDE_LEFT;
1663 device->DevChannels[7] = SIDE_RIGHT; break;
1667 static ALenum GetFormatFromString(const char *str)
1669 if(strcasecmp(str, "AL_FORMAT_MONO32") == 0) return AL_FORMAT_MONO_FLOAT32;
1670 if(strcasecmp(str, "AL_FORMAT_STEREO32") == 0) return AL_FORMAT_STEREO_FLOAT32;
1671 if(strcasecmp(str, "AL_FORMAT_QUAD32") == 0) return AL_FORMAT_QUAD32;
1672 if(strcasecmp(str, "AL_FORMAT_51CHN32") == 0) return AL_FORMAT_51CHN32;
1673 if(strcasecmp(str, "AL_FORMAT_61CHN32") == 0) return AL_FORMAT_61CHN32;
1674 if(strcasecmp(str, "AL_FORMAT_71CHN32") == 0) return AL_FORMAT_71CHN32;
1676 if(strcasecmp(str, "AL_FORMAT_MONO16") == 0) return AL_FORMAT_MONO16;
1677 if(strcasecmp(str, "AL_FORMAT_STEREO16") == 0) return AL_FORMAT_STEREO16;
1678 if(strcasecmp(str, "AL_FORMAT_QUAD16") == 0) return AL_FORMAT_QUAD16;
1679 if(strcasecmp(str, "AL_FORMAT_51CHN16") == 0) return AL_FORMAT_51CHN16;
1680 if(strcasecmp(str, "AL_FORMAT_61CHN16") == 0) return AL_FORMAT_61CHN16;
1681 if(strcasecmp(str, "AL_FORMAT_71CHN16") == 0) return AL_FORMAT_71CHN16;
1683 if(strcasecmp(str, "AL_FORMAT_MONO8") == 0) return AL_FORMAT_MONO8;
1684 if(strcasecmp(str, "AL_FORMAT_STEREO8") == 0) return AL_FORMAT_STEREO8;
1685 if(strcasecmp(str, "AL_FORMAT_QUAD8") == 0) return AL_FORMAT_QUAD8;
1686 if(strcasecmp(str, "AL_FORMAT_51CHN8") == 0) return AL_FORMAT_51CHN8;
1687 if(strcasecmp(str, "AL_FORMAT_61CHN8") == 0) return AL_FORMAT_61CHN8;
1688 if(strcasecmp(str, "AL_FORMAT_71CHN8") == 0) return AL_FORMAT_71CHN8;
1690 AL_PRINT("Unknown format: \"%s\"\n", str);
1691 return AL_FORMAT_STEREO16;
1695 alcOpenDevice
1697 Open the Device specified.
1699 ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName)
1701 ALboolean bDeviceFound = AL_FALSE;
1702 ALCdevice *device;
1703 ALint i;
1705 if(deviceName && !deviceName[0])
1706 deviceName = NULL;
1708 device = malloc(sizeof(ALCdevice));
1709 if (device)
1711 const char *fmt;
1713 //Initialise device structure
1714 memset(device, 0, sizeof(ALCdevice));
1716 //Validate device
1717 device->Connected = ALC_TRUE;
1718 device->IsCaptureDevice = AL_FALSE;
1719 device->LastError = ALC_NO_ERROR;
1721 device->Bs2b = NULL;
1722 device->szDeviceName = NULL;
1724 device->Contexts = NULL;
1725 device->NumContexts = 0;
1727 //Set output format
1728 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1729 if(device->Frequency < 8000)
1730 device->Frequency = 8000;
1732 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1733 device->Format = GetFormatFromString(fmt);
1735 device->NumUpdates = GetConfigValueInt(NULL, "periods", 4);
1736 if(device->NumUpdates < 2)
1737 device->NumUpdates = 4;
1739 i = GetConfigValueInt(NULL, "refresh", 4096);
1740 if(i <= 0) i = 4096;
1742 device->UpdateSize = GetConfigValueInt(NULL, "period_size", i/device->NumUpdates);
1743 if(device->UpdateSize <= 0)
1744 device->UpdateSize = i/device->NumUpdates;
1746 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1747 if((ALint)device->MaxNoOfSources <= 0)
1748 device->MaxNoOfSources = 256;
1750 device->AuxiliaryEffectSlotMax = GetConfigValueInt(NULL, "slots", 4);
1751 if((ALint)device->AuxiliaryEffectSlotMax <= 0)
1752 device->AuxiliaryEffectSlotMax = 4;
1754 device->lNumStereoSources = 1;
1755 device->lNumMonoSources = device->MaxNoOfSources - device->lNumStereoSources;
1757 device->NumAuxSends = GetConfigValueInt(NULL, "sends", MAX_SENDS);
1758 if(device->NumAuxSends > MAX_SENDS)
1759 device->NumAuxSends = MAX_SENDS;
1761 device->Bs2bLevel = GetConfigValueInt(NULL, "cf_level", 0);
1763 if(aluChannelsFromFormat(device->Format) <= 2)
1765 device->HeadDampen = GetConfigValueFloat(NULL, "head_dampen", DEFAULT_HEAD_DAMPEN);
1766 device->HeadDampen = __min(device->HeadDampen, 1.0f);
1767 device->HeadDampen = __max(device->HeadDampen, 0.0f);
1769 else
1770 device->HeadDampen = 0.0f;
1772 // Find a playback device to open
1773 SuspendContext(NULL);
1774 for(i = 0;BackendList[i].Init;i++)
1776 device->Funcs = &BackendList[i].Funcs;
1777 if(ALCdevice_OpenPlayback(device, deviceName))
1779 device->next = g_pDeviceList;
1780 g_pDeviceList = device;
1781 g_ulDeviceCount++;
1783 bDeviceFound = AL_TRUE;
1784 break;
1787 ProcessContext(NULL);
1789 if (!bDeviceFound)
1791 // No suitable output device found
1792 alcSetError(NULL, ALC_INVALID_VALUE);
1793 free(device);
1794 device = NULL;
1797 else
1798 alcSetError(NULL, ALC_OUT_OF_MEMORY);
1800 return device;
1805 alcCloseDevice
1807 Close the specified Device
1809 ALC_API ALCboolean ALC_APIENTRY alcCloseDevice(ALCdevice *pDevice)
1811 ALCboolean bReturn = ALC_FALSE;
1812 ALCdevice **list;
1814 if(IsDevice(pDevice) && !pDevice->IsCaptureDevice)
1816 SuspendContext(NULL);
1818 list = &g_pDeviceList;
1819 while(*list != pDevice)
1820 list = &(*list)->next;
1822 *list = (*list)->next;
1823 g_ulDeviceCount--;
1825 ProcessContext(NULL);
1827 if(pDevice->NumContexts > 0)
1829 #ifdef _DEBUG
1830 AL_PRINT("alcCloseDevice(): destroying %u Context(s)\n", pDevice->NumContexts);
1831 #endif
1832 while(pDevice->NumContexts > 0)
1833 alcDestroyContext(pDevice->Contexts[0]);
1835 ALCdevice_ClosePlayback(pDevice);
1837 if(pDevice->BufferCount > 0)
1839 #ifdef _DEBUG
1840 AL_PRINT("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice->BufferCount);
1841 #endif
1842 ReleaseALBuffers(pDevice);
1844 if(pDevice->EffectCount > 0)
1846 #ifdef _DEBUG
1847 AL_PRINT("alcCloseDevice(): deleting %d Effect(s)\n", pDevice->EffectCount);
1848 #endif
1849 ReleaseALEffects(pDevice);
1851 if(pDevice->FilterCount > 0)
1853 #ifdef _DEBUG
1854 AL_PRINT("alcCloseDevice(): deleting %d Filter(s)\n", pDevice->FilterCount);
1855 #endif
1856 ReleaseALFilters(pDevice);
1858 if(pDevice->DatabufferCount > 0)
1860 #ifdef _DEBUG
1861 AL_PRINT("alcCloseDevice(): deleting %d Databuffer(s)\n", pDevice->DatabufferCount);
1862 #endif
1863 ReleaseALDatabuffers(pDevice);
1866 free(pDevice->Bs2b);
1867 pDevice->Bs2b = NULL;
1869 free(pDevice->szDeviceName);
1870 pDevice->szDeviceName = NULL;
1872 free(pDevice->Contexts);
1873 pDevice->Contexts = NULL;
1875 //Release device structure
1876 memset(pDevice, 0, sizeof(ALCdevice));
1877 free(pDevice);
1879 bReturn = ALC_TRUE;
1881 else
1882 alcSetError(pDevice, ALC_INVALID_DEVICE);
1884 return bReturn;
1888 ALCvoid ReleaseALC(ALCvoid)
1890 free(alcDeviceList); alcDeviceList = NULL;
1891 alcDeviceListSize = 0;
1892 free(alcAllDeviceList); alcAllDeviceList = NULL;
1893 alcAllDeviceListSize = 0;
1894 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
1895 alcCaptureDeviceListSize = 0;
1897 free(alcDefaultDeviceSpecifier);
1898 alcDefaultDeviceSpecifier = NULL;
1899 free(alcDefaultAllDeviceSpecifier);
1900 alcDefaultAllDeviceSpecifier = NULL;
1901 free(alcCaptureDefaultDeviceSpecifier);
1902 alcCaptureDefaultDeviceSpecifier = NULL;
1904 #ifdef _DEBUG
1905 if(g_ulDeviceCount > 0)
1906 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
1907 #endif
1909 while(g_pDeviceList)
1911 if(g_pDeviceList->IsCaptureDevice)
1912 alcCaptureCloseDevice(g_pDeviceList);
1913 else
1914 alcCloseDevice(g_pDeviceList);
1918 ///////////////////////////////////////////////////////