Always disable ALSA's resampler for playback
[openal-soft.git] / Alc / ALc.c
blob290e9593eaf6395dc9733b26a757d69372f1d0ff
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 // Default context extensions
212 static const ALchar alExtList[] =
213 "AL_EXTX_buffer_sub_data AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 "
214 "AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_OFFSET "
215 "AL_EXTX_sample_buffer_object AL_EXT_source_distance_model "
216 "AL_LOKI_quadriphonic";
218 // Mixing Priority Level
219 ALint RTPrioLevel;
221 // Resampler Quality
222 resampler_t DefaultResampler;
224 ///////////////////////////////////////////////////////
227 ///////////////////////////////////////////////////////
228 // ALC Related helper functions
229 #ifdef _WIN32
230 static void alc_init(void);
231 static void alc_deinit(void);
233 BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
235 (void)lpReserved;
237 // Perform actions based on the reason for calling.
238 switch(ul_reason_for_call)
240 case DLL_PROCESS_ATTACH:
241 DisableThreadLibraryCalls(hModule);
242 alc_init();
243 break;
245 case DLL_PROCESS_DETACH:
246 alc_deinit();
247 break;
249 return TRUE;
251 #else
252 #ifdef HAVE_GCC_DESTRUCTOR
253 static void alc_init(void) __attribute__((constructor));
254 static void alc_deinit(void) __attribute__((destructor));
255 #endif
256 #endif
258 static void alc_init(void)
260 int i;
261 const char *devs, *str;
263 InitializeCriticalSection(&g_csMutex);
264 ALTHUNK_INIT();
265 ReadALConfig();
267 tls_create(&LocalContext);
269 RTPrioLevel = GetConfigValueInt(NULL, "rt-prio", 0);
271 DefaultResampler = GetConfigValueInt(NULL, "resampler", RESAMPLER_DEFAULT);
272 if(DefaultResampler >= RESAMPLER_MAX || DefaultResampler <= RESAMPLER_MIN)
273 DefaultResampler = RESAMPLER_DEFAULT;
275 devs = GetConfigValue(NULL, "drivers", "");
276 if(devs[0])
278 int n;
279 size_t len;
280 const char *next = devs;
281 int endlist;
283 i = 0;
284 do {
285 devs = next;
286 next = strchr(devs, ',');
288 if(!devs[0] || devs[0] == ',')
290 endlist = 0;
291 continue;
293 endlist = 1;
295 len = (next ? ((size_t)(next-devs)) : strlen(devs));
296 for(n = i;BackendList[n].Init;n++)
298 if(len == strlen(BackendList[n].name) &&
299 strncmp(BackendList[n].name, devs, len) == 0)
301 BackendInfo Bkp = BackendList[n];
302 while(n > i)
304 BackendList[n] = BackendList[n-1];
305 --n;
307 BackendList[n] = Bkp;
309 i++;
310 break;
313 } while(next++);
315 if(endlist)
317 BackendList[i].name = NULL;
318 BackendList[i].Init = NULL;
319 BackendList[i].Deinit = NULL;
320 BackendList[i].Probe = NULL;
324 for(i = 0;BackendList[i].Init;i++)
326 BackendList[i].Init(&BackendList[i].Funcs);
328 BackendList[i].Probe(DEVICE_PROBE);
329 BackendList[i].Probe(ALL_DEVICE_PROBE);
330 BackendList[i].Probe(CAPTURE_DEVICE_PROBE);
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 ALCuint 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 ALCAPI ALCdevice* ALCAPIENTRY 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 ALCAPI ALCboolean ALCAPIENTRY 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 ALCAPI void ALCAPIENTRY alcCaptureStart(ALCdevice *pDevice)
769 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
770 ALCdevice_StartCapture(pDevice);
771 else
772 alcSetError(pDevice, ALC_INVALID_DEVICE);
775 ALCAPI void ALCAPIENTRY alcCaptureStop(ALCdevice *pDevice)
777 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
778 ALCdevice_StopCapture(pDevice);
779 else
780 alcSetError(pDevice, ALC_INVALID_DEVICE);
783 ALCAPI void ALCAPIENTRY alcCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCsizei lSamples)
785 if(IsDevice(pDevice) && pDevice->IsCaptureDevice)
786 ALCdevice_CaptureSamples(pDevice, pBuffer, lSamples);
787 else
788 alcSetError(pDevice, ALC_INVALID_DEVICE);
792 alcGetError
794 Return last ALC generated error code
796 ALCAPI ALCenum ALCAPIENTRY alcGetError(ALCdevice *device)
798 ALCenum errorCode = ALC_NO_ERROR;
800 if(IsDevice(device))
802 errorCode = device->LastError;
803 device->LastError = ALC_NO_ERROR;
805 else
807 errorCode = g_eLastContextError;
808 g_eLastContextError = ALC_NO_ERROR;
810 return errorCode;
815 alcSuspendContext
817 Not functional
819 ALCAPI ALCvoid ALCAPIENTRY alcSuspendContext(ALCcontext *pContext)
821 SuspendContext(NULL);
822 if(IsContext(pContext))
823 pContext->Suspended = AL_TRUE;
824 ProcessContext(NULL);
829 alcProcessContext
831 Not functional
833 ALCAPI ALCvoid ALCAPIENTRY alcProcessContext(ALCcontext *pContext)
835 SuspendContext(NULL);
836 if(IsContext(pContext))
837 pContext->Suspended = AL_FALSE;
838 ProcessContext(NULL);
843 alcGetString
845 Returns information about the Device, and error strings
847 ALCAPI const ALCchar* ALCAPIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
849 const ALCchar *value = NULL;
851 switch (param)
853 case ALC_NO_ERROR:
854 value = alcNoError;
855 break;
857 case ALC_INVALID_ENUM:
858 value = alcErrInvalidEnum;
859 break;
861 case ALC_INVALID_VALUE:
862 value = alcErrInvalidValue;
863 break;
865 case ALC_INVALID_DEVICE:
866 value = alcErrInvalidDevice;
867 break;
869 case ALC_INVALID_CONTEXT:
870 value = alcErrInvalidContext;
871 break;
873 case ALC_OUT_OF_MEMORY:
874 value = alcErrOutOfMemory;
875 break;
877 case ALC_DEVICE_SPECIFIER:
878 if(IsDevice(pDevice))
879 value = pDevice->szDeviceName;
880 else
882 ProbeDeviceList();
883 value = alcDeviceList;
885 break;
887 case ALC_ALL_DEVICES_SPECIFIER:
888 ProbeAllDeviceList();
889 value = alcAllDeviceList;
890 break;
892 case ALC_CAPTURE_DEVICE_SPECIFIER:
893 if(IsDevice(pDevice))
894 value = pDevice->szDeviceName;
895 else
897 ProbeCaptureDeviceList();
898 value = alcCaptureDeviceList;
900 break;
902 /* Default devices are always first in the list */
903 case ALC_DEFAULT_DEVICE_SPECIFIER:
904 free(alcDefaultDeviceSpecifier);
905 alcDefaultDeviceSpecifier = strdup(alcDeviceList ? alcDeviceList : "");
906 if(!alcDefaultDeviceSpecifier)
907 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
908 value = alcDefaultDeviceSpecifier;
909 break;
911 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
912 free(alcDefaultAllDeviceSpecifier);
913 alcDefaultAllDeviceSpecifier = strdup(alcAllDeviceList ?
914 alcAllDeviceList : "");
915 if(!alcDefaultAllDeviceSpecifier)
916 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
917 value = alcDefaultAllDeviceSpecifier;
918 break;
920 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
921 free(alcCaptureDefaultDeviceSpecifier);
922 alcCaptureDefaultDeviceSpecifier = strdup(alcCaptureDeviceList ?
923 alcCaptureDeviceList : "");
924 if(!alcCaptureDefaultDeviceSpecifier)
925 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
926 value = alcCaptureDefaultDeviceSpecifier;
927 break;
929 case ALC_EXTENSIONS:
930 value = alcExtensionList;
931 break;
933 default:
934 alcSetError(pDevice, ALC_INVALID_ENUM);
935 break;
938 return value;
943 alcGetIntegerv
945 Returns information about the Device and the version of Open AL
947 ALCAPI ALCvoid ALCAPIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
949 if(size == 0 || data == NULL)
951 alcSetError(device, ALC_INVALID_VALUE);
952 return;
955 if(IsDevice(device) && device->IsCaptureDevice)
957 SuspendContext(NULL);
959 // Capture device
960 switch (param)
962 case ALC_CAPTURE_SAMPLES:
963 *data = ALCdevice_AvailableSamples(device);
964 break;
966 case ALC_CONNECTED:
967 *data = device->Connected;
968 break;
970 default:
971 alcSetError(device, ALC_INVALID_ENUM);
972 break;
975 ProcessContext(NULL);
976 return;
979 // Playback Device
980 switch (param)
982 case ALC_MAJOR_VERSION:
983 *data = alcMajorVersion;
984 break;
986 case ALC_MINOR_VERSION:
987 *data = alcMinorVersion;
988 break;
990 case ALC_EFX_MAJOR_VERSION:
991 *data = alcEFXMajorVersion;
992 break;
994 case ALC_EFX_MINOR_VERSION:
995 *data = alcEFXMinorVersion;
996 break;
998 case ALC_MAX_AUXILIARY_SENDS:
999 if(!IsDevice(device))
1000 alcSetError(device, ALC_INVALID_DEVICE);
1001 else
1002 *data = device->NumAuxSends;
1003 break;
1005 case ALC_ATTRIBUTES_SIZE:
1006 if(!IsDevice(device))
1007 alcSetError(device, ALC_INVALID_DEVICE);
1008 else
1009 *data = 13;
1010 break;
1012 case ALC_ALL_ATTRIBUTES:
1013 if(!IsDevice(device))
1014 alcSetError(device, ALC_INVALID_DEVICE);
1015 else if (size < 13)
1016 alcSetError(device, ALC_INVALID_VALUE);
1017 else
1019 int i = 0;
1021 SuspendContext(NULL);
1022 data[i++] = ALC_FREQUENCY;
1023 data[i++] = device->Frequency;
1025 data[i++] = ALC_REFRESH;
1026 data[i++] = device->Frequency / device->UpdateSize;
1028 data[i++] = ALC_SYNC;
1029 data[i++] = ALC_FALSE;
1031 data[i++] = ALC_MONO_SOURCES;
1032 data[i++] = device->lNumMonoSources;
1034 data[i++] = ALC_STEREO_SOURCES;
1035 data[i++] = device->lNumStereoSources;
1037 data[i++] = ALC_MAX_AUXILIARY_SENDS;
1038 data[i++] = device->NumAuxSends;
1040 data[i++] = 0;
1041 ProcessContext(NULL);
1043 break;
1045 case ALC_FREQUENCY:
1046 if(!IsDevice(device))
1047 alcSetError(device, ALC_INVALID_DEVICE);
1048 else
1049 *data = device->Frequency;
1050 break;
1052 case ALC_REFRESH:
1053 if(!IsDevice(device))
1054 alcSetError(device, ALC_INVALID_DEVICE);
1055 else
1056 *data = device->Frequency / device->UpdateSize;
1057 break;
1059 case ALC_SYNC:
1060 if(!IsDevice(device))
1061 alcSetError(device, ALC_INVALID_DEVICE);
1062 else
1063 *data = ALC_FALSE;
1064 break;
1066 case ALC_MONO_SOURCES:
1067 if(!IsDevice(device))
1068 alcSetError(device, ALC_INVALID_DEVICE);
1069 else
1070 *data = device->lNumMonoSources;
1071 break;
1073 case ALC_STEREO_SOURCES:
1074 if(!IsDevice(device))
1075 alcSetError(device, ALC_INVALID_DEVICE);
1076 else
1077 *data = device->lNumStereoSources;
1078 break;
1080 case ALC_CONNECTED:
1081 if(!IsDevice(device))
1082 alcSetError(device, ALC_INVALID_DEVICE);
1083 else
1084 *data = device->Connected;
1085 break;
1087 default:
1088 alcSetError(device, ALC_INVALID_ENUM);
1089 break;
1095 alcIsExtensionPresent
1097 Determines if there is support for a particular extension
1099 ALCAPI ALCboolean ALCAPIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
1101 ALCboolean bResult = ALC_FALSE;
1103 if (extName)
1105 const char *ptr;
1106 size_t len;
1108 len = strlen(extName);
1109 ptr = alcExtensionList;
1110 while(ptr && *ptr)
1112 if(strncasecmp(ptr, extName, len) == 0 &&
1113 (ptr[len] == '\0' || isspace(ptr[len])))
1115 bResult = ALC_TRUE;
1116 break;
1118 if((ptr=strchr(ptr, ' ')) != NULL)
1120 do {
1121 ++ptr;
1122 } while(isspace(*ptr));
1126 else
1127 alcSetError(device, ALC_INVALID_VALUE);
1129 return bResult;
1134 alcGetProcAddress
1136 Retrieves the function address for a particular extension function
1138 ALCAPI ALCvoid * ALCAPIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
1140 ALCvoid *pFunction = NULL;
1141 ALsizei i = 0;
1143 if (funcName)
1145 while(alcFunctions[i].funcName &&
1146 strcmp(alcFunctions[i].funcName,funcName) != 0)
1147 i++;
1148 pFunction = alcFunctions[i].address;
1150 else
1151 alcSetError(device, ALC_INVALID_VALUE);
1153 return pFunction;
1158 alcGetEnumValue
1160 Get the value for a particular ALC Enumerated Value
1162 ALCAPI ALCenum ALCAPIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
1164 ALsizei i = 0;
1165 ALCenum val;
1167 while ((enumeration[i].enumName)&&(strcmp(enumeration[i].enumName,enumName)))
1168 i++;
1169 val = enumeration[i].value;
1171 if(!enumeration[i].enumName)
1172 alcSetError(device, ALC_INVALID_VALUE);
1174 return val;
1179 alcCreateContext
1181 Create and attach a Context to a particular Device.
1183 ALCAPI ALCcontext* ALCAPIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
1185 ALuint attrIdx, reqStereoSources;
1186 ALCcontext *ALContext;
1187 void *temp;
1188 ALuint i;
1190 SuspendContext(NULL);
1192 if(!IsDevice(device) || device->IsCaptureDevice || !device->Connected)
1194 alcSetError(device, ALC_INVALID_DEVICE);
1195 ProcessContext(NULL);
1196 return NULL;
1199 // Reset Context Last Error code
1200 device->LastError = ALC_NO_ERROR;
1202 // If a context is already running on the device, stop playback so the
1203 // device attributes can be updated
1204 if(device->NumContexts > 0)
1206 ProcessContext(NULL);
1207 ALCdevice_StopPlayback(device);
1208 SuspendContext(NULL);
1211 // Check for attributes
1212 if(attrList)
1214 ALCint level = device->Bs2bLevel;
1215 ALCuint freq = device->Frequency;
1216 ALCint numMono = device->lNumMonoSources;
1217 ALCint numStereo = device->lNumStereoSources;
1218 ALCuint numSends = device->NumAuxSends;
1220 attrIdx = 0;
1221 while(attrList[attrIdx])
1223 if(attrList[attrIdx] == ALC_FREQUENCY &&
1224 !ConfigValueExists(NULL, "frequency"))
1226 freq = attrList[attrIdx + 1];
1227 if(freq < 8000)
1228 freq = 8000;
1231 if(attrList[attrIdx] == ALC_STEREO_SOURCES)
1233 reqStereoSources = attrList[attrIdx + 1];
1234 if(reqStereoSources > device->MaxNoOfSources)
1235 reqStereoSources = device->MaxNoOfSources;
1237 numStereo = reqStereoSources;
1238 numMono = device->MaxNoOfSources - numStereo;
1241 if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS &&
1242 !ConfigValueExists(NULL, "sends"))
1244 numSends = attrList[attrIdx + 1];
1245 if(numSends > MAX_SENDS)
1246 numSends = MAX_SENDS;
1249 attrIdx += 2;
1252 device->Bs2bLevel = level;
1253 device->Frequency = freq;
1254 device->lNumMonoSources = numMono;
1255 device->lNumStereoSources = numStereo;
1256 device->NumAuxSends = numSends;
1259 if(ALCdevice_ResetPlayback(device) == ALC_FALSE)
1261 alcSetError(device, ALC_INVALID_DEVICE);
1262 aluHandleDisconnect(device);
1263 ProcessContext(NULL);
1264 return NULL;
1267 for(i = 0;i < device->NumContexts;i++)
1269 ALCcontext *context = device->Contexts[i];
1270 ALeffectslot *slot;
1271 ALsource *source;
1273 SuspendContext(context);
1274 for(slot = context->AuxiliaryEffectSlot;slot != NULL;slot = slot->next)
1276 if(!slot->EffectState)
1277 continue;
1279 if(ALEffect_DeviceUpdate(slot->EffectState, device) == AL_FALSE)
1281 alcSetError(device, ALC_INVALID_DEVICE);
1282 aluHandleDisconnect(device);
1283 ProcessContext(context);
1284 ProcessContext(NULL);
1285 ALCdevice_StopPlayback(device);
1286 return NULL;
1288 ALEffect_Update(slot->EffectState, context, &slot->effect);
1291 for(source = context->Source;source != NULL;source = source->next)
1293 ALuint s = device->NumAuxSends;
1294 while(s < MAX_SENDS)
1296 if(source->Send[s].Slot)
1297 source->Send[s].Slot->refcount--;
1298 source->Send[s].Slot = NULL;
1299 source->Send[s].WetFilter.type = 0;
1300 source->Send[s].WetFilter.filter = 0;
1301 s++;
1303 source->NeedsUpdate = AL_TRUE;
1305 aluInitPanning(context);
1306 ProcessContext(context);
1309 if(device->Bs2bLevel > 0 && device->Bs2bLevel <= 6)
1311 if(!device->Bs2b)
1313 device->Bs2b = calloc(1, sizeof(*device->Bs2b));
1314 bs2b_clear(device->Bs2b);
1316 bs2b_set_srate(device->Bs2b, device->Frequency);
1317 bs2b_set_level(device->Bs2b, device->Bs2bLevel);
1319 else
1321 free(device->Bs2b);
1322 device->Bs2b = NULL;
1325 temp = realloc(device->Contexts, (device->NumContexts+1) * sizeof(*device->Contexts));
1326 if(!temp)
1328 alcSetError(device, ALC_OUT_OF_MEMORY);
1329 ProcessContext(NULL);
1330 return NULL;
1332 device->Contexts = temp;
1334 ALContext = calloc(1, sizeof(ALCcontext));
1335 if(!ALContext)
1337 alcSetError(device, ALC_OUT_OF_MEMORY);
1338 ProcessContext(NULL);
1339 return NULL;
1342 device->Contexts[device->NumContexts++] = ALContext;
1343 ALContext->Device = device;
1345 InitContext(ALContext);
1346 aluInitPanning(ALContext);
1348 ALContext->next = g_pContextList;
1349 g_pContextList = ALContext;
1350 g_ulContextCount++;
1352 ProcessContext(NULL);
1354 return ALContext;
1359 alcDestroyContext
1361 Remove a Context
1363 ALCAPI ALCvoid ALCAPIENTRY alcDestroyContext(ALCcontext *context)
1365 ALCcontext **list;
1366 ALuint i;
1368 if (IsContext(context))
1370 ALCdevice *Device = context->Device;
1372 if(Device->NumContexts == 1)
1373 ALCdevice_StopPlayback(Device);
1375 SuspendContext(NULL);
1377 for(i = 0;i < Device->NumContexts-1;i++)
1379 if(Device->Contexts[i] == context)
1381 memmove(&Device->Contexts[i], &Device->Contexts[i+1],
1382 (Device->NumContexts-i-1) * sizeof(*Device->Contexts));
1383 break;
1386 Device->NumContexts--;
1388 // Lock context
1389 SuspendContext(context);
1391 if(context->SourceCount > 0)
1393 #ifdef _DEBUG
1394 AL_PRINT("alcDestroyContext(): deleting %d Source(s)\n", context->SourceCount);
1395 #endif
1396 ReleaseALSources(context);
1398 if(context->AuxiliaryEffectSlotCount > 0)
1400 #ifdef _DEBUG
1401 AL_PRINT("alcDestroyContext(): deleting %d AuxiliaryEffectSlot(s)\n", context->AuxiliaryEffectSlotCount);
1402 #endif
1403 ReleaseALAuxiliaryEffectSlots(context);
1406 list = &g_pContextList;
1407 while(*list != context)
1408 list = &(*list)->next;
1410 *list = (*list)->next;
1411 g_ulContextCount--;
1413 // Unlock context
1414 ProcessContext(context);
1415 ProcessContext(NULL);
1417 ExitContext(context);
1419 // Free memory (MUST do this after ProcessContext)
1420 memset(context, 0, sizeof(ALCcontext));
1421 free(context);
1423 else
1424 alcSetError(NULL, ALC_INVALID_CONTEXT);
1429 alcGetCurrentContext
1431 Returns the currently active Context
1433 ALCAPI ALCcontext * ALCAPIENTRY alcGetCurrentContext(ALCvoid)
1435 ALCcontext *pContext;
1437 if((pContext=GetContextSuspended()) != NULL)
1438 ProcessContext(pContext);
1440 return pContext;
1444 alcGetThreadContext
1446 Returns the currently active thread-local Context
1448 ALCcontext * ALCAPIENTRY alcGetThreadContext(void)
1450 ALCcontext *pContext = NULL;
1452 SuspendContext(NULL);
1454 pContext = tls_get(LocalContext);
1455 if(pContext && !IsContext(pContext))
1457 tls_set(LocalContext, NULL);
1458 pContext = NULL;
1461 ProcessContext(NULL);
1463 return pContext;
1468 alcGetContextsDevice
1470 Returns the Device that a particular Context is attached to
1472 ALCAPI ALCdevice* ALCAPIENTRY alcGetContextsDevice(ALCcontext *pContext)
1474 ALCdevice *pDevice = NULL;
1476 SuspendContext(NULL);
1477 if (IsContext(pContext))
1478 pDevice = pContext->Device;
1479 else
1480 alcSetError(NULL, ALC_INVALID_CONTEXT);
1481 ProcessContext(NULL);
1483 return pDevice;
1488 alcMakeContextCurrent
1490 Makes the given Context the active Context
1492 ALCAPI ALCboolean ALCAPIENTRY alcMakeContextCurrent(ALCcontext *context)
1494 ALCcontext *ALContext;
1495 ALboolean bReturn = AL_TRUE;
1497 SuspendContext(NULL);
1499 // context must be a valid Context or NULL
1500 if(context == NULL || IsContext(context))
1502 if((ALContext=GetContextSuspended()) != NULL)
1504 ALContext->InUse=AL_FALSE;
1505 ProcessContext(ALContext);
1508 if((ALContext=context) != NULL && ALContext->Device)
1510 SuspendContext(ALContext);
1511 ALContext->InUse=AL_TRUE;
1512 ProcessContext(ALContext);
1515 tls_set(LocalContext, NULL);
1517 else
1519 alcSetError(NULL, ALC_INVALID_CONTEXT);
1520 bReturn = AL_FALSE;
1523 ProcessContext(NULL);
1525 return bReturn;
1529 alcMakeCurrent
1531 Makes the given Context the active Context for the current thread
1533 ALCboolean ALCAPIENTRY alcMakeCurrent(ALCcontext *context)
1535 ALboolean bReturn = AL_TRUE;
1537 SuspendContext(NULL);
1539 // context must be a valid Context or NULL
1540 if(context == NULL || IsContext(context))
1541 tls_set(LocalContext, context);
1542 else
1544 alcSetError(NULL, ALC_INVALID_CONTEXT);
1545 bReturn = AL_FALSE;
1548 ProcessContext(NULL);
1550 return bReturn;
1554 // Sets the default channel order used by most non-WaveFormatEx-based APIs
1555 void SetDefaultChannelOrder(ALCdevice *device)
1557 switch(aluChannelsFromFormat(device->Format))
1559 case 1: device->DevChannels[0] = FRONT_CENTER; break;
1561 case 2: device->DevChannels[0] = FRONT_LEFT;
1562 device->DevChannels[1] = FRONT_RIGHT; break;
1564 case 4: device->DevChannels[0] = FRONT_LEFT;
1565 device->DevChannels[1] = FRONT_RIGHT;
1566 device->DevChannels[2] = BACK_LEFT;
1567 device->DevChannels[3] = BACK_RIGHT; break;
1569 case 6: device->DevChannels[0] = FRONT_LEFT;
1570 device->DevChannels[1] = FRONT_RIGHT;
1571 device->DevChannels[2] = BACK_LEFT;
1572 device->DevChannels[3] = BACK_RIGHT;
1573 device->DevChannels[4] = FRONT_CENTER;
1574 device->DevChannels[5] = LFE; break;
1576 case 7: device->DevChannels[0] = FRONT_LEFT;
1577 device->DevChannels[1] = FRONT_RIGHT;
1578 device->DevChannels[2] = FRONT_CENTER;
1579 device->DevChannels[3] = LFE;
1580 device->DevChannels[4] = BACK_CENTER;
1581 device->DevChannels[5] = SIDE_LEFT;
1582 device->DevChannels[6] = SIDE_RIGHT; break;
1584 case 8: device->DevChannels[0] = FRONT_LEFT;
1585 device->DevChannels[1] = FRONT_RIGHT;
1586 device->DevChannels[2] = BACK_LEFT;
1587 device->DevChannels[3] = BACK_RIGHT;
1588 device->DevChannels[4] = FRONT_CENTER;
1589 device->DevChannels[5] = LFE;
1590 device->DevChannels[6] = SIDE_LEFT;
1591 device->DevChannels[7] = SIDE_RIGHT; break;
1594 // Sets the default order used by WaveFormatEx
1595 void SetDefaultWFXChannelOrder(ALCdevice *device)
1597 switch(aluChannelsFromFormat(device->Format))
1599 case 1: device->DevChannels[0] = FRONT_CENTER; break;
1601 case 2: device->DevChannels[0] = FRONT_LEFT;
1602 device->DevChannels[1] = FRONT_RIGHT; break;
1604 case 4: device->DevChannels[0] = FRONT_LEFT;
1605 device->DevChannels[1] = FRONT_RIGHT;
1606 device->DevChannels[2] = BACK_LEFT;
1607 device->DevChannels[3] = BACK_RIGHT; break;
1609 case 6: device->DevChannels[0] = FRONT_LEFT;
1610 device->DevChannels[1] = FRONT_RIGHT;
1611 device->DevChannels[2] = FRONT_CENTER;
1612 device->DevChannels[3] = LFE;
1613 device->DevChannels[4] = BACK_LEFT;
1614 device->DevChannels[5] = BACK_RIGHT; break;
1616 case 7: device->DevChannels[0] = FRONT_LEFT;
1617 device->DevChannels[1] = FRONT_RIGHT;
1618 device->DevChannels[2] = FRONT_CENTER;
1619 device->DevChannels[3] = LFE;
1620 device->DevChannels[4] = BACK_CENTER;
1621 device->DevChannels[5] = SIDE_LEFT;
1622 device->DevChannels[6] = SIDE_RIGHT; break;
1624 case 8: device->DevChannels[0] = FRONT_LEFT;
1625 device->DevChannels[1] = FRONT_RIGHT;
1626 device->DevChannels[2] = FRONT_CENTER;
1627 device->DevChannels[3] = LFE;
1628 device->DevChannels[4] = BACK_LEFT;
1629 device->DevChannels[5] = BACK_RIGHT;
1630 device->DevChannels[6] = SIDE_LEFT;
1631 device->DevChannels[7] = SIDE_RIGHT; break;
1635 static ALenum GetFormatFromString(const char *str)
1637 if(strcasecmp(str, "AL_FORMAT_MONO32") == 0) return AL_FORMAT_MONO_FLOAT32;
1638 if(strcasecmp(str, "AL_FORMAT_STEREO32") == 0) return AL_FORMAT_STEREO_FLOAT32;
1639 if(strcasecmp(str, "AL_FORMAT_QUAD32") == 0) return AL_FORMAT_QUAD32;
1640 if(strcasecmp(str, "AL_FORMAT_51CHN32") == 0) return AL_FORMAT_51CHN32;
1641 if(strcasecmp(str, "AL_FORMAT_61CHN32") == 0) return AL_FORMAT_61CHN32;
1642 if(strcasecmp(str, "AL_FORMAT_71CHN32") == 0) return AL_FORMAT_71CHN32;
1644 if(strcasecmp(str, "AL_FORMAT_MONO16") == 0) return AL_FORMAT_MONO16;
1645 if(strcasecmp(str, "AL_FORMAT_STEREO16") == 0) return AL_FORMAT_STEREO16;
1646 if(strcasecmp(str, "AL_FORMAT_QUAD16") == 0) return AL_FORMAT_QUAD16;
1647 if(strcasecmp(str, "AL_FORMAT_51CHN16") == 0) return AL_FORMAT_51CHN16;
1648 if(strcasecmp(str, "AL_FORMAT_61CHN16") == 0) return AL_FORMAT_61CHN16;
1649 if(strcasecmp(str, "AL_FORMAT_71CHN16") == 0) return AL_FORMAT_71CHN16;
1651 if(strcasecmp(str, "AL_FORMAT_MONO8") == 0) return AL_FORMAT_MONO8;
1652 if(strcasecmp(str, "AL_FORMAT_STEREO8") == 0) return AL_FORMAT_STEREO8;
1653 if(strcasecmp(str, "AL_FORMAT_QUAD8") == 0) return AL_FORMAT_QUAD8;
1654 if(strcasecmp(str, "AL_FORMAT_51CHN8") == 0) return AL_FORMAT_51CHN8;
1655 if(strcasecmp(str, "AL_FORMAT_61CHN8") == 0) return AL_FORMAT_61CHN8;
1656 if(strcasecmp(str, "AL_FORMAT_71CHN8") == 0) return AL_FORMAT_71CHN8;
1658 AL_PRINT("Unknown format: \"%s\"\n", str);
1659 return AL_FORMAT_STEREO16;
1663 alcOpenDevice
1665 Open the Device specified.
1667 ALCAPI ALCdevice* ALCAPIENTRY alcOpenDevice(const ALCchar *deviceName)
1669 ALboolean bDeviceFound = AL_FALSE;
1670 ALCdevice *device;
1671 ALint i;
1673 if(deviceName && !deviceName[0])
1674 deviceName = NULL;
1676 device = malloc(sizeof(ALCdevice));
1677 if (device)
1679 const char *fmt;
1681 //Initialise device structure
1682 memset(device, 0, sizeof(ALCdevice));
1684 //Validate device
1685 device->Connected = ALC_TRUE;
1686 device->IsCaptureDevice = AL_FALSE;
1687 device->LastError = ALC_NO_ERROR;
1689 device->Bs2b = NULL;
1690 device->szDeviceName = NULL;
1692 device->Contexts = NULL;
1693 device->NumContexts = 0;
1695 //Set output format
1696 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1697 if(device->Frequency < 8000)
1698 device->Frequency = 8000;
1700 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1701 device->Format = GetFormatFromString(fmt);
1703 device->NumUpdates = GetConfigValueInt(NULL, "periods", 4);
1704 if(device->NumUpdates < 2)
1705 device->NumUpdates = 4;
1707 i = GetConfigValueInt(NULL, "refresh", 4096);
1708 if(i <= 0) i = 4096;
1710 device->UpdateSize = GetConfigValueInt(NULL, "period_size", i/device->NumUpdates);
1711 if(device->UpdateSize <= 0)
1712 device->UpdateSize = i/device->NumUpdates;
1714 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1715 if((ALint)device->MaxNoOfSources <= 0)
1716 device->MaxNoOfSources = 256;
1718 device->AuxiliaryEffectSlotMax = GetConfigValueInt(NULL, "slots", 4);
1719 if((ALint)device->AuxiliaryEffectSlotMax <= 0)
1720 device->AuxiliaryEffectSlotMax = 4;
1722 device->lNumStereoSources = 1;
1723 device->lNumMonoSources = device->MaxNoOfSources - device->lNumStereoSources;
1725 device->NumAuxSends = GetConfigValueInt(NULL, "sends", MAX_SENDS);
1726 if(device->NumAuxSends > MAX_SENDS)
1727 device->NumAuxSends = MAX_SENDS;
1729 device->Bs2bLevel = GetConfigValueInt(NULL, "cf_level", 0);
1731 if(aluChannelsFromFormat(device->Format) <= 2)
1733 device->HeadDampen = GetConfigValueFloat(NULL, "head_dampen", DEFAULT_HEAD_DAMPEN);
1734 device->HeadDampen = __min(device->HeadDampen, 1.0f);
1735 device->HeadDampen = __max(device->HeadDampen, 0.0f);
1737 else
1738 device->HeadDampen = 0.0f;
1740 // Find a playback device to open
1741 SuspendContext(NULL);
1742 for(i = 0;BackendList[i].Init;i++)
1744 device->Funcs = &BackendList[i].Funcs;
1745 if(ALCdevice_OpenPlayback(device, deviceName))
1747 device->next = g_pDeviceList;
1748 g_pDeviceList = device;
1749 g_ulDeviceCount++;
1751 bDeviceFound = AL_TRUE;
1752 break;
1755 ProcessContext(NULL);
1757 if (!bDeviceFound)
1759 // No suitable output device found
1760 alcSetError(NULL, ALC_INVALID_VALUE);
1761 free(device);
1762 device = NULL;
1765 else
1766 alcSetError(NULL, ALC_OUT_OF_MEMORY);
1768 return device;
1773 alcCloseDevice
1775 Close the specified Device
1777 ALCAPI ALCboolean ALCAPIENTRY alcCloseDevice(ALCdevice *pDevice)
1779 ALCboolean bReturn = ALC_FALSE;
1780 ALCdevice **list;
1782 if(IsDevice(pDevice) && !pDevice->IsCaptureDevice)
1784 SuspendContext(NULL);
1786 list = &g_pDeviceList;
1787 while(*list != pDevice)
1788 list = &(*list)->next;
1790 *list = (*list)->next;
1791 g_ulDeviceCount--;
1793 ProcessContext(NULL);
1795 if(pDevice->NumContexts > 0)
1797 #ifdef _DEBUG
1798 AL_PRINT("alcCloseDevice(): destroying %u Context(s)\n", pDevice->NumContexts);
1799 #endif
1800 while(pDevice->NumContexts > 0)
1801 alcDestroyContext(pDevice->Contexts[0]);
1803 ALCdevice_ClosePlayback(pDevice);
1805 if(pDevice->BufferCount > 0)
1807 #ifdef _DEBUG
1808 AL_PRINT("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice->BufferCount);
1809 #endif
1810 ReleaseALBuffers(pDevice);
1812 if(pDevice->EffectCount > 0)
1814 #ifdef _DEBUG
1815 AL_PRINT("alcCloseDevice(): deleting %d Effect(s)\n", pDevice->EffectCount);
1816 #endif
1817 ReleaseALEffects(pDevice);
1819 if(pDevice->FilterCount > 0)
1821 #ifdef _DEBUG
1822 AL_PRINT("alcCloseDevice(): deleting %d Filter(s)\n", pDevice->FilterCount);
1823 #endif
1824 ReleaseALFilters(pDevice);
1826 if(pDevice->DatabufferCount > 0)
1828 #ifdef _DEBUG
1829 AL_PRINT("alcCloseDevice(): deleting %d Databuffer(s)\n", pDevice->DatabufferCount);
1830 #endif
1831 ReleaseALDatabuffers(pDevice);
1834 free(pDevice->Bs2b);
1835 pDevice->Bs2b = NULL;
1837 free(pDevice->szDeviceName);
1838 pDevice->szDeviceName = NULL;
1840 free(pDevice->Contexts);
1841 pDevice->Contexts = NULL;
1843 //Release device structure
1844 memset(pDevice, 0, sizeof(ALCdevice));
1845 free(pDevice);
1847 bReturn = ALC_TRUE;
1849 else
1850 alcSetError(pDevice, ALC_INVALID_DEVICE);
1852 return bReturn;
1856 ALCvoid ReleaseALC(ALCvoid)
1858 free(alcDeviceList); alcDeviceList = NULL;
1859 alcDeviceListSize = 0;
1860 free(alcAllDeviceList); alcAllDeviceList = NULL;
1861 alcAllDeviceListSize = 0;
1862 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
1863 alcCaptureDeviceListSize = 0;
1865 free(alcDefaultDeviceSpecifier);
1866 alcDefaultDeviceSpecifier = NULL;
1867 free(alcDefaultAllDeviceSpecifier);
1868 alcDefaultAllDeviceSpecifier = NULL;
1869 free(alcCaptureDefaultDeviceSpecifier);
1870 alcCaptureDefaultDeviceSpecifier = NULL;
1872 #ifdef _DEBUG
1873 if(g_ulDeviceCount > 0)
1874 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
1875 #endif
1877 while(g_pDeviceList)
1879 if(g_pDeviceList->IsCaptureDevice)
1880 alcCaptureCloseDevice(g_pDeviceList);
1881 else
1882 alcCloseDevice(g_pDeviceList);
1886 ///////////////////////////////////////////////////////