Use a map for the effects list
[openal-soft/android/lowlatency.git] / Alc / ALc.c
blob3f9c6006c9170e4c2edc14894b424f40a1dfac60
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>
29 #include "alMain.h"
30 #include "alSource.h"
31 #include "AL/al.h"
32 #include "AL/alc.h"
33 #include "alThunk.h"
34 #include "alSource.h"
35 #include "alBuffer.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 {
89 const ALCchar *funcName;
90 ALCvoid *address;
91 } ALCfunction;
93 typedef struct ALCenums {
94 const ALCchar *enumName;
95 ALCenum value;
96 } ALCenums;
99 static const ALCfunction alcFunctions[] = {
100 { "alcCreateContext", (ALCvoid *) alcCreateContext },
101 { "alcMakeContextCurrent", (ALCvoid *) alcMakeContextCurrent },
102 { "alcProcessContext", (ALCvoid *) alcProcessContext },
103 { "alcSuspendContext", (ALCvoid *) alcSuspendContext },
104 { "alcDestroyContext", (ALCvoid *) alcDestroyContext },
105 { "alcGetCurrentContext", (ALCvoid *) alcGetCurrentContext },
106 { "alcGetContextsDevice", (ALCvoid *) alcGetContextsDevice },
107 { "alcOpenDevice", (ALCvoid *) alcOpenDevice },
108 { "alcCloseDevice", (ALCvoid *) alcCloseDevice },
109 { "alcGetError", (ALCvoid *) alcGetError },
110 { "alcIsExtensionPresent", (ALCvoid *) alcIsExtensionPresent },
111 { "alcGetProcAddress", (ALCvoid *) alcGetProcAddress },
112 { "alcGetEnumValue", (ALCvoid *) alcGetEnumValue },
113 { "alcGetString", (ALCvoid *) alcGetString },
114 { "alcGetIntegerv", (ALCvoid *) alcGetIntegerv },
115 { "alcCaptureOpenDevice", (ALCvoid *) alcCaptureOpenDevice },
116 { "alcCaptureCloseDevice", (ALCvoid *) alcCaptureCloseDevice },
117 { "alcCaptureStart", (ALCvoid *) alcCaptureStart },
118 { "alcCaptureStop", (ALCvoid *) alcCaptureStop },
119 { "alcCaptureSamples", (ALCvoid *) alcCaptureSamples },
121 { "alcSetThreadContext", (ALCvoid *) alcSetThreadContext },
122 { "alcGetThreadContext", (ALCvoid *) alcGetThreadContext },
124 { "alEnable", (ALCvoid *) alEnable },
125 { "alDisable", (ALCvoid *) alDisable },
126 { "alIsEnabled", (ALCvoid *) alIsEnabled },
127 { "alGetString", (ALCvoid *) alGetString },
128 { "alGetBooleanv", (ALCvoid *) alGetBooleanv },
129 { "alGetIntegerv", (ALCvoid *) alGetIntegerv },
130 { "alGetFloatv", (ALCvoid *) alGetFloatv },
131 { "alGetDoublev", (ALCvoid *) alGetDoublev },
132 { "alGetBoolean", (ALCvoid *) alGetBoolean },
133 { "alGetInteger", (ALCvoid *) alGetInteger },
134 { "alGetFloat", (ALCvoid *) alGetFloat },
135 { "alGetDouble", (ALCvoid *) alGetDouble },
136 { "alGetError", (ALCvoid *) alGetError },
137 { "alIsExtensionPresent", (ALCvoid *) alIsExtensionPresent },
138 { "alGetProcAddress", (ALCvoid *) alGetProcAddress },
139 { "alGetEnumValue", (ALCvoid *) alGetEnumValue },
140 { "alListenerf", (ALCvoid *) alListenerf },
141 { "alListener3f", (ALCvoid *) alListener3f },
142 { "alListenerfv", (ALCvoid *) alListenerfv },
143 { "alListeneri", (ALCvoid *) alListeneri },
144 { "alListener3i", (ALCvoid *) alListener3i },
145 { "alListeneriv", (ALCvoid *) alListeneriv },
146 { "alGetListenerf", (ALCvoid *) alGetListenerf },
147 { "alGetListener3f", (ALCvoid *) alGetListener3f },
148 { "alGetListenerfv", (ALCvoid *) alGetListenerfv },
149 { "alGetListeneri", (ALCvoid *) alGetListeneri },
150 { "alGetListener3i", (ALCvoid *) alGetListener3i },
151 { "alGetListeneriv", (ALCvoid *) alGetListeneriv },
152 { "alGenSources", (ALCvoid *) alGenSources },
153 { "alDeleteSources", (ALCvoid *) alDeleteSources },
154 { "alIsSource", (ALCvoid *) alIsSource },
155 { "alSourcef", (ALCvoid *) alSourcef },
156 { "alSource3f", (ALCvoid *) alSource3f },
157 { "alSourcefv", (ALCvoid *) alSourcefv },
158 { "alSourcei", (ALCvoid *) alSourcei },
159 { "alSource3i", (ALCvoid *) alSource3i },
160 { "alSourceiv", (ALCvoid *) alSourceiv },
161 { "alGetSourcef", (ALCvoid *) alGetSourcef },
162 { "alGetSource3f", (ALCvoid *) alGetSource3f },
163 { "alGetSourcefv", (ALCvoid *) alGetSourcefv },
164 { "alGetSourcei", (ALCvoid *) alGetSourcei },
165 { "alGetSource3i", (ALCvoid *) alGetSource3i },
166 { "alGetSourceiv", (ALCvoid *) alGetSourceiv },
167 { "alSourcePlayv", (ALCvoid *) alSourcePlayv },
168 { "alSourceStopv", (ALCvoid *) alSourceStopv },
169 { "alSourceRewindv", (ALCvoid *) alSourceRewindv },
170 { "alSourcePausev", (ALCvoid *) alSourcePausev },
171 { "alSourcePlay", (ALCvoid *) alSourcePlay },
172 { "alSourceStop", (ALCvoid *) alSourceStop },
173 { "alSourceRewind", (ALCvoid *) alSourceRewind },
174 { "alSourcePause", (ALCvoid *) alSourcePause },
175 { "alSourceQueueBuffers", (ALCvoid *) alSourceQueueBuffers },
176 { "alSourceUnqueueBuffers", (ALCvoid *) alSourceUnqueueBuffers },
177 { "alGenBuffers", (ALCvoid *) alGenBuffers },
178 { "alDeleteBuffers", (ALCvoid *) alDeleteBuffers },
179 { "alIsBuffer", (ALCvoid *) alIsBuffer },
180 { "alBufferData", (ALCvoid *) alBufferData },
181 { "alBufferf", (ALCvoid *) alBufferf },
182 { "alBuffer3f", (ALCvoid *) alBuffer3f },
183 { "alBufferfv", (ALCvoid *) alBufferfv },
184 { "alBufferi", (ALCvoid *) alBufferi },
185 { "alBuffer3i", (ALCvoid *) alBuffer3i },
186 { "alBufferiv", (ALCvoid *) alBufferiv },
187 { "alGetBufferf", (ALCvoid *) alGetBufferf },
188 { "alGetBuffer3f", (ALCvoid *) alGetBuffer3f },
189 { "alGetBufferfv", (ALCvoid *) alGetBufferfv },
190 { "alGetBufferi", (ALCvoid *) alGetBufferi },
191 { "alGetBuffer3i", (ALCvoid *) alGetBuffer3i },
192 { "alGetBufferiv", (ALCvoid *) alGetBufferiv },
193 { "alDopplerFactor", (ALCvoid *) alDopplerFactor },
194 { "alDopplerVelocity", (ALCvoid *) alDopplerVelocity },
195 { "alSpeedOfSound", (ALCvoid *) alSpeedOfSound },
196 { "alDistanceModel", (ALCvoid *) alDistanceModel },
198 { "alGenFilters", (ALCvoid *) alGenFilters },
199 { "alDeleteFilters", (ALCvoid *) alDeleteFilters },
200 { "alIsFilter", (ALCvoid *) alIsFilter },
201 { "alFilteri", (ALCvoid *) alFilteri },
202 { "alFilteriv", (ALCvoid *) alFilteriv },
203 { "alFilterf", (ALCvoid *) alFilterf },
204 { "alFilterfv", (ALCvoid *) alFilterfv },
205 { "alGetFilteri", (ALCvoid *) alGetFilteri },
206 { "alGetFilteriv", (ALCvoid *) alGetFilteriv },
207 { "alGetFilterf", (ALCvoid *) alGetFilterf },
208 { "alGetFilterfv", (ALCvoid *) alGetFilterfv },
210 { "alGenEffects", (ALCvoid *) alGenEffects },
211 { "alDeleteEffects", (ALCvoid *) alDeleteEffects },
212 { "alIsEffect", (ALCvoid *) alIsEffect },
213 { "alEffecti", (ALCvoid *) alEffecti },
214 { "alEffectiv", (ALCvoid *) alEffectiv },
215 { "alEffectf", (ALCvoid *) alEffectf },
216 { "alEffectfv", (ALCvoid *) alEffectfv },
217 { "alGetEffecti", (ALCvoid *) alGetEffecti },
218 { "alGetEffectiv", (ALCvoid *) alGetEffectiv },
219 { "alGetEffectf", (ALCvoid *) alGetEffectf },
220 { "alGetEffectfv", (ALCvoid *) alGetEffectfv },
222 { "alGenAuxiliaryEffectSlots", (ALCvoid *) alGenAuxiliaryEffectSlots},
223 { "alDeleteAuxiliaryEffectSlots",(ALCvoid *) alDeleteAuxiliaryEffectSlots},
224 { "alIsAuxiliaryEffectSlot", (ALCvoid *) alIsAuxiliaryEffectSlot },
225 { "alAuxiliaryEffectSloti", (ALCvoid *) alAuxiliaryEffectSloti },
226 { "alAuxiliaryEffectSlotiv", (ALCvoid *) alAuxiliaryEffectSlotiv },
227 { "alAuxiliaryEffectSlotf", (ALCvoid *) alAuxiliaryEffectSlotf },
228 { "alAuxiliaryEffectSlotfv", (ALCvoid *) alAuxiliaryEffectSlotfv },
229 { "alGetAuxiliaryEffectSloti", (ALCvoid *) alGetAuxiliaryEffectSloti},
230 { "alGetAuxiliaryEffectSlotiv", (ALCvoid *) alGetAuxiliaryEffectSlotiv},
231 { "alGetAuxiliaryEffectSlotf", (ALCvoid *) alGetAuxiliaryEffectSlotf},
232 { "alGetAuxiliaryEffectSlotfv", (ALCvoid *) alGetAuxiliaryEffectSlotfv},
234 { "alBufferSubDataEXT", (ALCvoid *) alBufferSubDataEXT },
236 { "alGenDatabuffersEXT", (ALCvoid *) alGenDatabuffersEXT },
237 { "alDeleteDatabuffersEXT", (ALCvoid *) alDeleteDatabuffersEXT },
238 { "alIsDatabufferEXT", (ALCvoid *) alIsDatabufferEXT },
239 { "alDatabufferDataEXT", (ALCvoid *) alDatabufferDataEXT },
240 { "alDatabufferSubDataEXT", (ALCvoid *) alDatabufferSubDataEXT },
241 { "alGetDatabufferSubDataEXT", (ALCvoid *) alGetDatabufferSubDataEXT},
242 { "alDatabufferfEXT", (ALCvoid *) alDatabufferfEXT },
243 { "alDatabufferfvEXT", (ALCvoid *) alDatabufferfvEXT },
244 { "alDatabufferiEXT", (ALCvoid *) alDatabufferiEXT },
245 { "alDatabufferivEXT", (ALCvoid *) alDatabufferivEXT },
246 { "alGetDatabufferfEXT", (ALCvoid *) alGetDatabufferfEXT },
247 { "alGetDatabufferfvEXT", (ALCvoid *) alGetDatabufferfvEXT },
248 { "alGetDatabufferiEXT", (ALCvoid *) alGetDatabufferiEXT },
249 { "alGetDatabufferivEXT", (ALCvoid *) alGetDatabufferivEXT },
250 { "alSelectDatabufferEXT", (ALCvoid *) alSelectDatabufferEXT },
251 { "alMapDatabufferEXT", (ALCvoid *) alMapDatabufferEXT },
252 { "alUnmapDatabufferEXT", (ALCvoid *) alUnmapDatabufferEXT },
254 { NULL, (ALCvoid *) NULL }
257 static const ALCenums enumeration[] = {
258 // Types
259 { "ALC_INVALID", ALC_INVALID },
260 { "ALC_FALSE", ALC_FALSE },
261 { "ALC_TRUE", ALC_TRUE },
263 // ALC Properties
264 { "ALC_MAJOR_VERSION", ALC_MAJOR_VERSION },
265 { "ALC_MINOR_VERSION", ALC_MINOR_VERSION },
266 { "ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE },
267 { "ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES },
268 { "ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER },
269 { "ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER },
270 { "ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER },
271 { "ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER },
272 { "ALC_EXTENSIONS", ALC_EXTENSIONS },
273 { "ALC_FREQUENCY", ALC_FREQUENCY },
274 { "ALC_REFRESH", ALC_REFRESH },
275 { "ALC_SYNC", ALC_SYNC },
276 { "ALC_MONO_SOURCES", ALC_MONO_SOURCES },
277 { "ALC_STEREO_SOURCES", ALC_STEREO_SOURCES },
278 { "ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER },
279 { "ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER},
280 { "ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES },
281 { "ALC_CONNECTED", ALC_CONNECTED },
283 // EFX Properties
284 { "ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION },
285 { "ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION },
286 { "ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS },
288 // ALC Error Message
289 { "ALC_NO_ERROR", ALC_NO_ERROR },
290 { "ALC_INVALID_DEVICE", ALC_INVALID_DEVICE },
291 { "ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT },
292 { "ALC_INVALID_ENUM", ALC_INVALID_ENUM },
293 { "ALC_INVALID_VALUE", ALC_INVALID_VALUE },
294 { "ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY },
295 { NULL, (ALCenum)0 }
297 // Error strings
298 static const ALCchar alcNoError[] = "No Error";
299 static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
300 static const ALCchar alcErrInvalidContext[] = "Invalid Context";
301 static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
302 static const ALCchar alcErrInvalidValue[] = "Invalid Value";
303 static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
305 /* Device lists. Sizes only include the first ending null character, not the
306 * second */
307 static ALCchar *alcDeviceList;
308 static size_t alcDeviceListSize;
309 static ALCchar *alcAllDeviceList;
310 static size_t alcAllDeviceListSize;
311 static ALCchar *alcCaptureDeviceList;
312 static size_t alcCaptureDeviceListSize;
313 // Default is always the first in the list
314 static ALCchar *alcDefaultDeviceSpecifier;
315 static ALCchar *alcDefaultAllDeviceSpecifier;
316 static ALCchar *alcCaptureDefaultDeviceSpecifier;
319 static const ALCchar alcNoDeviceExtList[] =
320 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
321 "ALC_EXT_thread_local_context";
322 static const ALCchar alcExtensionList[] =
323 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
324 "ALC_EXT_disconnect ALC_EXT_EFX ALC_EXT_thread_local_context";
325 static const ALCint alcMajorVersion = 1;
326 static const ALCint alcMinorVersion = 1;
328 static const ALCint alcEFXMajorVersion = 1;
329 static const ALCint alcEFXMinorVersion = 0;
331 ///////////////////////////////////////////////////////
334 ///////////////////////////////////////////////////////
335 // Global Variables
337 static ALCdevice *g_pDeviceList = NULL;
338 static ALCuint g_ulDeviceCount = 0;
340 static CRITICAL_SECTION g_csMutex;
342 // Context List
343 static ALCcontext *g_pContextList = NULL;
344 static ALCuint g_ulContextCount = 0;
346 // Thread-local current context
347 static tls_type LocalContext;
348 // Process-wide current context
349 static ALCcontext *GlobalContext;
351 // Context Error
352 static ALCenum g_eLastNullDeviceError = ALC_NO_ERROR;
354 // Default context extensions
355 static const ALchar alExtList[] =
356 "AL_EXTX_buffer_sub_data AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE "
357 "AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXTX_loop_points "
358 "AL_EXT_MCFORMATS AL_EXT_MULAW AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET "
359 "AL_EXTX_sample_buffer_object AL_EXT_source_distance_model "
360 "AL_LOKI_quadriphonic";
362 // Mixing Priority Level
363 static ALint RTPrioLevel;
365 // Resampler Quality
366 resampler_t DefaultResampler;
368 ///////////////////////////////////////////////////////
371 ///////////////////////////////////////////////////////
372 // ALC Related helper functions
373 #ifdef _WIN32
374 static void alc_init(void);
375 static void alc_deinit(void);
377 BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
379 (void)lpReserved;
381 // Perform actions based on the reason for calling.
382 switch(ul_reason_for_call)
384 case DLL_PROCESS_ATTACH:
385 DisableThreadLibraryCalls(hModule);
386 alc_init();
387 break;
389 case DLL_PROCESS_DETACH:
390 alc_deinit();
391 break;
393 return TRUE;
395 #else
396 #ifdef HAVE_GCC_DESTRUCTOR
397 static void alc_init(void) __attribute__((constructor));
398 static void alc_deinit(void) __attribute__((destructor));
399 #endif
400 #endif
402 static void alc_init(void)
404 int i;
405 const char *devs, *str;
407 InitializeCriticalSection(&g_csMutex);
408 ALTHUNK_INIT();
409 ReadALConfig();
411 tls_create(&LocalContext);
413 RTPrioLevel = GetConfigValueInt(NULL, "rt-prio", 0);
415 DefaultResampler = GetConfigValueInt(NULL, "resampler", RESAMPLER_DEFAULT);
416 if(DefaultResampler >= RESAMPLER_MAX || DefaultResampler <= RESAMPLER_MIN)
417 DefaultResampler = RESAMPLER_DEFAULT;
419 devs = GetConfigValue(NULL, "drivers", "");
420 if(devs[0])
422 int n;
423 size_t len;
424 const char *next = devs;
425 int endlist;
427 i = 0;
428 do {
429 devs = next;
430 next = strchr(devs, ',');
432 if(!devs[0] || devs[0] == ',')
434 endlist = 0;
435 continue;
437 endlist = 1;
439 len = (next ? ((size_t)(next-devs)) : strlen(devs));
440 for(n = i;BackendList[n].Init;n++)
442 if(len == strlen(BackendList[n].name) &&
443 strncmp(BackendList[n].name, devs, len) == 0)
445 BackendInfo Bkp = BackendList[n];
446 while(n > i)
448 BackendList[n] = BackendList[n-1];
449 --n;
451 BackendList[n] = Bkp;
453 i++;
454 break;
457 } while(next++);
459 if(endlist)
461 BackendList[i].name = NULL;
462 BackendList[i].Init = NULL;
463 BackendList[i].Deinit = NULL;
464 BackendList[i].Probe = NULL;
468 for(i = 0;BackendList[i].Init;i++)
469 BackendList[i].Init(&BackendList[i].Funcs);
471 str = GetConfigValue(NULL, "excludefx", "");
472 if(str[0])
474 const struct {
475 const char *name;
476 int type;
477 } EffectList[] = {
478 { "eaxreverb", EAXREVERB },
479 { "reverb", REVERB },
480 { "echo", ECHO },
481 { "modulator", MODULATOR },
482 { NULL, 0 }
484 int n;
485 size_t len;
486 const char *next = str;
488 do {
489 str = next;
490 next = strchr(str, ',');
492 if(!str[0] || next == str)
493 continue;
495 len = (next ? ((size_t)(next-str)) : strlen(str));
496 for(n = 0;EffectList[n].name;n++)
498 if(len == strlen(EffectList[n].name) &&
499 strncmp(EffectList[n].name, str, len) == 0)
500 DisabledEffects[EffectList[n].type] = AL_TRUE;
502 } while(next++);
506 static void alc_deinit(void)
508 int i;
510 ReleaseALC();
512 for(i = 0;BackendList[i].Deinit;i++)
513 BackendList[i].Deinit();
515 tls_delete(LocalContext);
517 FreeALConfig();
518 ALTHUNK_EXIT();
519 DeleteCriticalSection(&g_csMutex);
523 static void ProbeDeviceList()
525 ALint i;
527 free(alcDeviceList); alcDeviceList = NULL;
528 alcDeviceListSize = 0;
530 for(i = 0;BackendList[i].Probe;i++)
531 BackendList[i].Probe(DEVICE_PROBE);
534 static void ProbeAllDeviceList()
536 ALint i;
538 free(alcAllDeviceList); alcAllDeviceList = NULL;
539 alcAllDeviceListSize = 0;
541 for(i = 0;BackendList[i].Probe;i++)
542 BackendList[i].Probe(ALL_DEVICE_PROBE);
545 static void ProbeCaptureDeviceList()
547 ALint i;
549 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
550 alcCaptureDeviceListSize = 0;
552 for(i = 0;BackendList[i].Probe;i++)
553 BackendList[i].Probe(CAPTURE_DEVICE_PROBE);
557 #define DECL_APPEND_LIST_FUNC(type) \
558 void Append##type##List(const ALCchar *name) \
560 size_t len = strlen(name); \
561 void *temp; \
563 if(len == 0) \
564 return; \
566 temp = realloc(alc##type##List, alc##type##ListSize + len + 2); \
567 if(!temp) \
569 AL_PRINT("Realloc failed to add %s!\n", name); \
570 return; \
572 alc##type##List = temp; \
573 sprintf(alc##type##List+alc##type##ListSize, "%s", name); \
574 alc##type##ListSize += len+1; \
575 alc##type##List[alc##type##ListSize] = 0; \
578 DECL_APPEND_LIST_FUNC(Device)
579 DECL_APPEND_LIST_FUNC(AllDevice)
580 DECL_APPEND_LIST_FUNC(CaptureDevice)
583 void al_print(const char *fname, unsigned int line, const char *fmt, ...)
585 const char *fn;
586 char str[256];
587 int i;
589 fn = strrchr(fname, '/');
590 if(!fn) fn = strrchr(fname, '\\');;
591 if(!fn) fn = fname;
592 else fn += 1;
594 i = snprintf(str, sizeof(str), "AL lib: %s:%d: ", fn, line);
595 if(i < (int)sizeof(str) && i > 0)
597 va_list ap;
598 va_start(ap, fmt);
599 vsnprintf(str+i, sizeof(str)-i, fmt, ap);
600 va_end(ap);
602 str[sizeof(str)-1] = 0;
604 fprintf(stderr, "%s", str);
607 void SetRTPriority(void)
609 ALboolean failed;
611 #ifdef _WIN32
612 if(RTPrioLevel > 0)
613 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
614 else
615 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
616 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM)
617 struct sched_param param;
619 if(RTPrioLevel > 0)
621 /* Use the minimum real-time priority possible for now (on Linux this
622 * should be 1 for SCHED_RR) */
623 param.sched_priority = sched_get_priority_min(SCHED_RR);
624 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
626 else
628 param.sched_priority = 0;
629 failed = !!pthread_setschedparam(pthread_self(), SCHED_OTHER, &param);
631 #else
632 /* Real-time priority not available */
633 failed = (RTPrioLevel>0);
634 #endif
635 if(failed)
636 AL_PRINT("Failed to set priority level for thread\n");
640 void InitUIntMap(UIntMap *map)
642 map->array = NULL;
643 map->size = 0;
644 map->maxsize = 0;
647 void ResetUIntMap(UIntMap *map)
649 free(map->array);
650 map->array = NULL;
651 map->size = 0;
652 map->maxsize = 0;
655 ALenum InsertUIntMapEntry(UIntMap *map, ALuint key, ALvoid *value)
657 ALsizei pos = 0;
659 if(map->size > 0)
661 ALsizei low = 0;
662 ALsizei high = map->size - 1;
663 while(low < high)
665 ALsizei mid = low + (high-low)/2;
666 if(map->array[mid].key < key)
667 low = mid + 1;
668 else
669 high = mid;
671 if(map->array[low].key < key)
672 low++;
673 pos = low;
676 if(pos == map->size || map->array[pos].key != key)
678 if(map->size == map->maxsize)
680 ALvoid *temp;
681 ALsizei newsize;
683 newsize = (map->maxsize ? (map->maxsize<<1) : 4);
684 if(newsize < map->maxsize)
685 return AL_OUT_OF_MEMORY;
687 temp = realloc(map->array, newsize*sizeof(map->array[0]));
688 if(!temp) return AL_OUT_OF_MEMORY;
689 map->array = temp;
690 map->maxsize = newsize;
693 map->size++;
694 if(pos < map->size-1)
695 memmove(&map->array[pos+1], &map->array[pos],
696 (map->size-1-pos)*sizeof(map->array[0]));
698 map->array[pos].key = key;
699 map->array[pos].value = value;
701 return AL_NO_ERROR;
704 void RemoveUIntMapKey(UIntMap *map, ALuint key)
706 if(map->size > 0)
708 ALsizei low = 0;
709 ALsizei high = map->size - 1;
710 while(low < high)
712 ALsizei mid = low + (high-low)/2;
713 if(map->array[mid].key < key)
714 low = mid + 1;
715 else
716 high = mid;
718 if(map->array[low].key == key)
720 if(low < map->size-1)
721 memmove(&map->array[low], &map->array[low+1],
722 (map->size-1-low)*sizeof(map->array[0]));
723 map->size--;
730 IsDevice
732 Check pDevice is a valid Device pointer
734 static ALCboolean IsDevice(ALCdevice *pDevice)
736 ALCdevice *pTempDevice;
738 SuspendContext(NULL);
740 pTempDevice = g_pDeviceList;
741 while(pTempDevice && pTempDevice != pDevice)
742 pTempDevice = pTempDevice->next;
744 ProcessContext(NULL);
746 return (pTempDevice ? ALC_TRUE : ALC_FALSE);
750 IsContext
752 Check pContext is a valid Context pointer
754 static ALCboolean IsContext(ALCcontext *pContext)
756 ALCcontext *pTempContext;
758 SuspendContext(NULL);
760 pTempContext = g_pContextList;
761 while (pTempContext && pTempContext != pContext)
762 pTempContext = pTempContext->next;
764 ProcessContext(NULL);
766 return (pTempContext ? ALC_TRUE : ALC_FALSE);
771 alcSetError
773 Store latest ALC Error
775 ALCvoid alcSetError(ALCdevice *device, ALenum errorCode)
777 if(IsDevice(device))
778 device->LastError = errorCode;
779 else
780 g_eLastNullDeviceError = errorCode;
785 SuspendContext
787 Thread-safe entry
789 ALCvoid SuspendContext(ALCcontext *pContext)
791 (void)pContext;
792 EnterCriticalSection(&g_csMutex);
797 ProcessContext
799 Thread-safe exit
801 ALCvoid ProcessContext(ALCcontext *pContext)
803 (void)pContext;
804 LeaveCriticalSection(&g_csMutex);
809 GetContextSuspended
811 Returns the currently active Context, in a locked state
813 ALCcontext *GetContextSuspended(void)
815 ALCcontext *pContext = NULL;
817 SuspendContext(NULL);
819 pContext = tls_get(LocalContext);
820 if(pContext && !IsContext(pContext))
822 tls_set(LocalContext, NULL);
823 pContext = NULL;
825 if(!pContext)
826 pContext = GlobalContext;
828 if(pContext)
829 SuspendContext(pContext);
831 ProcessContext(NULL);
833 return pContext;
838 InitContext
840 Initialize Context variables
842 static ALvoid InitContext(ALCcontext *pContext)
844 //Initialise listener
845 pContext->Listener.Gain = 1.0f;
846 pContext->Listener.MetersPerUnit = 1.0f;
847 pContext->Listener.Position[0] = 0.0f;
848 pContext->Listener.Position[1] = 0.0f;
849 pContext->Listener.Position[2] = 0.0f;
850 pContext->Listener.Velocity[0] = 0.0f;
851 pContext->Listener.Velocity[1] = 0.0f;
852 pContext->Listener.Velocity[2] = 0.0f;
853 pContext->Listener.Forward[0] = 0.0f;
854 pContext->Listener.Forward[1] = 0.0f;
855 pContext->Listener.Forward[2] = -1.0f;
856 pContext->Listener.Up[0] = 0.0f;
857 pContext->Listener.Up[1] = 1.0f;
858 pContext->Listener.Up[2] = 0.0f;
860 //Validate pContext
861 pContext->LastError = AL_NO_ERROR;
862 pContext->Suspended = AL_FALSE;
863 InitUIntMap(&pContext->SourceMap);
865 //Set globals
866 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
867 pContext->SourceDistanceModel = AL_FALSE;
868 pContext->DopplerFactor = 1.0f;
869 pContext->DopplerVelocity = 1.0f;
870 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
872 pContext->ExtensionList = alExtList;
877 ExitContext
879 Clean up Context, destroy any remaining Sources
881 static ALCvoid ExitContext(ALCcontext *pContext)
883 //Invalidate context
884 pContext->LastError = AL_NO_ERROR;
887 ///////////////////////////////////////////////////////
890 ///////////////////////////////////////////////////////
891 // ALC Functions calls
894 // This should probably move to another c file but for now ...
895 ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
897 ALCboolean DeviceFound = ALC_FALSE;
898 ALCdevice *device = NULL;
899 ALCint i;
901 if(SampleSize <= 0)
903 alcSetError(NULL, ALC_INVALID_VALUE);
904 return NULL;
907 if(deviceName && !deviceName[0])
908 deviceName = NULL;
910 device = calloc(1, sizeof(ALCdevice));
911 if(!device)
913 alcSetError(NULL, ALC_OUT_OF_MEMORY);
914 return NULL;
917 //Validate device
918 device->Connected = ALC_TRUE;
919 device->IsCaptureDevice = AL_TRUE;
921 device->szDeviceName = NULL;
923 device->Frequency = frequency;
924 device->Format = format;
925 device->UpdateSize = SampleSize;
926 device->NumUpdates = 1;
928 SuspendContext(NULL);
929 for(i = 0;BackendList[i].Init;i++)
931 device->Funcs = &BackendList[i].Funcs;
932 if(ALCdevice_OpenCapture(device, deviceName))
934 device->next = g_pDeviceList;
935 g_pDeviceList = device;
936 g_ulDeviceCount++;
938 DeviceFound = ALC_TRUE;
939 break;
942 ProcessContext(NULL);
944 if(!DeviceFound)
946 alcSetError(NULL, ALC_INVALID_VALUE);
947 free(device);
948 device = NULL;
951 return device;
954 ALC_API ALCboolean ALC_APIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
956 ALCdevice **list;
958 if(!IsDevice(pDevice) || !pDevice->IsCaptureDevice)
960 alcSetError(pDevice, ALC_INVALID_DEVICE);
961 return ALC_FALSE;
964 SuspendContext(NULL);
966 list = &g_pDeviceList;
967 while(*list != pDevice)
968 list = &(*list)->next;
970 *list = (*list)->next;
971 g_ulDeviceCount--;
973 ProcessContext(NULL);
975 ALCdevice_CloseCapture(pDevice);
977 free(pDevice->szDeviceName);
978 pDevice->szDeviceName = NULL;
980 free(pDevice);
982 return ALC_TRUE;
985 ALC_API void ALC_APIENTRY alcCaptureStart(ALCdevice *device)
987 if(!IsDevice(device) || !device->IsCaptureDevice)
989 alcSetError(device, ALC_INVALID_DEVICE);
990 return;
992 SuspendContext(NULL);
993 ALCdevice_StartCapture(device);
994 ProcessContext(NULL);
997 ALC_API void ALC_APIENTRY alcCaptureStop(ALCdevice *device)
999 if(!IsDevice(device) || !device->IsCaptureDevice)
1001 alcSetError(device, ALC_INVALID_DEVICE);
1002 return;
1004 SuspendContext(NULL);
1005 ALCdevice_StopCapture(device);
1006 ProcessContext(NULL);
1009 ALC_API void ALC_APIENTRY alcCaptureSamples(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
1011 if(!IsDevice(device) || !device->IsCaptureDevice)
1013 alcSetError(device, ALC_INVALID_DEVICE);
1014 return;
1016 SuspendContext(NULL);
1017 ALCdevice_CaptureSamples(device, buffer, samples);
1018 ProcessContext(NULL);
1022 alcGetError
1024 Return last ALC generated error code
1026 ALC_API ALCenum ALC_APIENTRY alcGetError(ALCdevice *device)
1028 ALCenum errorCode;
1030 if(IsDevice(device))
1032 errorCode = device->LastError;
1033 device->LastError = ALC_NO_ERROR;
1035 else
1037 errorCode = g_eLastNullDeviceError;
1038 g_eLastNullDeviceError = ALC_NO_ERROR;
1040 return errorCode;
1045 alcSuspendContext
1047 Not functional
1049 ALC_API ALCvoid ALC_APIENTRY alcSuspendContext(ALCcontext *pContext)
1051 SuspendContext(NULL);
1052 if(IsContext(pContext))
1053 pContext->Suspended = AL_TRUE;
1054 ProcessContext(NULL);
1059 alcProcessContext
1061 Not functional
1063 ALC_API ALCvoid ALC_APIENTRY alcProcessContext(ALCcontext *pContext)
1065 SuspendContext(NULL);
1066 if(IsContext(pContext))
1067 pContext->Suspended = AL_FALSE;
1068 ProcessContext(NULL);
1073 alcGetString
1075 Returns information about the Device, and error strings
1077 ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
1079 const ALCchar *value = NULL;
1081 switch (param)
1083 case ALC_NO_ERROR:
1084 value = alcNoError;
1085 break;
1087 case ALC_INVALID_ENUM:
1088 value = alcErrInvalidEnum;
1089 break;
1091 case ALC_INVALID_VALUE:
1092 value = alcErrInvalidValue;
1093 break;
1095 case ALC_INVALID_DEVICE:
1096 value = alcErrInvalidDevice;
1097 break;
1099 case ALC_INVALID_CONTEXT:
1100 value = alcErrInvalidContext;
1101 break;
1103 case ALC_OUT_OF_MEMORY:
1104 value = alcErrOutOfMemory;
1105 break;
1107 case ALC_DEVICE_SPECIFIER:
1108 if(IsDevice(pDevice))
1109 value = pDevice->szDeviceName;
1110 else
1112 ProbeDeviceList();
1113 value = alcDeviceList;
1115 break;
1117 case ALC_ALL_DEVICES_SPECIFIER:
1118 ProbeAllDeviceList();
1119 value = alcAllDeviceList;
1120 break;
1122 case ALC_CAPTURE_DEVICE_SPECIFIER:
1123 if(IsDevice(pDevice))
1124 value = pDevice->szDeviceName;
1125 else
1127 ProbeCaptureDeviceList();
1128 value = alcCaptureDeviceList;
1130 break;
1132 /* Default devices are always first in the list */
1133 case ALC_DEFAULT_DEVICE_SPECIFIER:
1134 if(!alcDeviceList)
1135 ProbeDeviceList();
1137 free(alcDefaultDeviceSpecifier);
1138 alcDefaultDeviceSpecifier = strdup(alcDeviceList ? alcDeviceList : "");
1139 if(!alcDefaultDeviceSpecifier)
1140 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
1141 value = alcDefaultDeviceSpecifier;
1142 break;
1144 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
1145 if(!alcAllDeviceList)
1146 ProbeAllDeviceList();
1148 free(alcDefaultAllDeviceSpecifier);
1149 alcDefaultAllDeviceSpecifier = strdup(alcAllDeviceList ?
1150 alcAllDeviceList : "");
1151 if(!alcDefaultAllDeviceSpecifier)
1152 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
1153 value = alcDefaultAllDeviceSpecifier;
1154 break;
1156 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
1157 if(!alcCaptureDeviceList)
1158 ProbeCaptureDeviceList();
1160 free(alcCaptureDefaultDeviceSpecifier);
1161 alcCaptureDefaultDeviceSpecifier = strdup(alcCaptureDeviceList ?
1162 alcCaptureDeviceList : "");
1163 if(!alcCaptureDefaultDeviceSpecifier)
1164 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
1165 value = alcCaptureDefaultDeviceSpecifier;
1166 break;
1168 case ALC_EXTENSIONS:
1169 if(IsDevice(pDevice))
1170 value = alcExtensionList;
1171 else
1172 value = alcNoDeviceExtList;
1173 break;
1175 default:
1176 alcSetError(pDevice, ALC_INVALID_ENUM);
1177 break;
1180 return value;
1185 alcGetIntegerv
1187 Returns information about the Device and the version of Open AL
1189 ALC_API ALCvoid ALC_APIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
1191 if(size == 0 || data == NULL)
1193 alcSetError(device, ALC_INVALID_VALUE);
1194 return;
1197 if(IsDevice(device) && device->IsCaptureDevice)
1199 SuspendContext(NULL);
1201 // Capture device
1202 switch (param)
1204 case ALC_CAPTURE_SAMPLES:
1205 *data = ALCdevice_AvailableSamples(device);
1206 break;
1208 case ALC_CONNECTED:
1209 *data = device->Connected;
1210 break;
1212 default:
1213 alcSetError(device, ALC_INVALID_ENUM);
1214 break;
1217 ProcessContext(NULL);
1218 return;
1221 // Playback Device
1222 switch (param)
1224 case ALC_MAJOR_VERSION:
1225 *data = alcMajorVersion;
1226 break;
1228 case ALC_MINOR_VERSION:
1229 *data = alcMinorVersion;
1230 break;
1232 case ALC_EFX_MAJOR_VERSION:
1233 *data = alcEFXMajorVersion;
1234 break;
1236 case ALC_EFX_MINOR_VERSION:
1237 *data = alcEFXMinorVersion;
1238 break;
1240 case ALC_MAX_AUXILIARY_SENDS:
1241 if(!IsDevice(device))
1242 alcSetError(device, ALC_INVALID_DEVICE);
1243 else
1244 *data = device->NumAuxSends;
1245 break;
1247 case ALC_ATTRIBUTES_SIZE:
1248 if(!IsDevice(device))
1249 alcSetError(device, ALC_INVALID_DEVICE);
1250 else
1251 *data = 13;
1252 break;
1254 case ALC_ALL_ATTRIBUTES:
1255 if(!IsDevice(device))
1256 alcSetError(device, ALC_INVALID_DEVICE);
1257 else if (size < 13)
1258 alcSetError(device, ALC_INVALID_VALUE);
1259 else
1261 int i = 0;
1263 SuspendContext(NULL);
1264 data[i++] = ALC_FREQUENCY;
1265 data[i++] = device->Frequency;
1267 data[i++] = ALC_REFRESH;
1268 data[i++] = device->Frequency / device->UpdateSize;
1270 data[i++] = ALC_SYNC;
1271 data[i++] = ALC_FALSE;
1273 data[i++] = ALC_MONO_SOURCES;
1274 data[i++] = device->NumMonoSources;
1276 data[i++] = ALC_STEREO_SOURCES;
1277 data[i++] = device->NumStereoSources;
1279 data[i++] = ALC_MAX_AUXILIARY_SENDS;
1280 data[i++] = device->NumAuxSends;
1282 data[i++] = 0;
1283 ProcessContext(NULL);
1285 break;
1287 case ALC_FREQUENCY:
1288 if(!IsDevice(device))
1289 alcSetError(device, ALC_INVALID_DEVICE);
1290 else
1291 *data = device->Frequency;
1292 break;
1294 case ALC_REFRESH:
1295 if(!IsDevice(device))
1296 alcSetError(device, ALC_INVALID_DEVICE);
1297 else
1298 *data = device->Frequency / device->UpdateSize;
1299 break;
1301 case ALC_SYNC:
1302 if(!IsDevice(device))
1303 alcSetError(device, ALC_INVALID_DEVICE);
1304 else
1305 *data = ALC_FALSE;
1306 break;
1308 case ALC_MONO_SOURCES:
1309 if(!IsDevice(device))
1310 alcSetError(device, ALC_INVALID_DEVICE);
1311 else
1312 *data = device->NumMonoSources;
1313 break;
1315 case ALC_STEREO_SOURCES:
1316 if(!IsDevice(device))
1317 alcSetError(device, ALC_INVALID_DEVICE);
1318 else
1319 *data = device->NumStereoSources;
1320 break;
1322 case ALC_CONNECTED:
1323 if(!IsDevice(device))
1324 alcSetError(device, ALC_INVALID_DEVICE);
1325 else
1326 *data = device->Connected;
1327 break;
1329 default:
1330 alcSetError(device, ALC_INVALID_ENUM);
1331 break;
1337 alcIsExtensionPresent
1339 Determines if there is support for a particular extension
1341 ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
1343 ALCboolean bResult = ALC_FALSE;
1344 const char *ptr;
1345 size_t len;
1347 if(!extName)
1349 alcSetError(device, ALC_INVALID_VALUE);
1350 return ALC_FALSE;
1353 len = strlen(extName);
1354 ptr = (IsDevice(device) ? alcExtensionList : alcNoDeviceExtList);
1355 while(ptr && *ptr)
1357 if(strncasecmp(ptr, extName, len) == 0 &&
1358 (ptr[len] == '\0' || isspace(ptr[len])))
1360 bResult = ALC_TRUE;
1361 break;
1363 if((ptr=strchr(ptr, ' ')) != NULL)
1365 do {
1366 ++ptr;
1367 } while(isspace(*ptr));
1371 return bResult;
1376 alcGetProcAddress
1378 Retrieves the function address for a particular extension function
1380 ALC_API ALCvoid* ALC_APIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
1382 ALsizei i = 0;
1384 if(!funcName)
1386 alcSetError(device, ALC_INVALID_VALUE);
1387 return NULL;
1390 while(alcFunctions[i].funcName && strcmp(alcFunctions[i].funcName,funcName) != 0)
1391 i++;
1392 return alcFunctions[i].address;
1397 alcGetEnumValue
1399 Get the value for a particular ALC Enumerated Value
1401 ALC_API ALCenum ALC_APIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
1403 ALsizei i = 0;
1405 if(!enumName)
1407 alcSetError(device, ALC_INVALID_VALUE);
1408 return (ALCenum)0;
1411 while(enumeration[i].enumName && strcmp(enumeration[i].enumName,enumName) != 0)
1412 i++;
1413 return enumeration[i].value;
1418 alcCreateContext
1420 Create and attach a Context to a particular Device.
1422 ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
1424 ALCcontext *ALContext;
1425 ALboolean running;
1426 ALuint attrIdx;
1427 void *temp;
1428 ALuint i;
1430 SuspendContext(NULL);
1432 if(!IsDevice(device) || device->IsCaptureDevice || !device->Connected)
1434 alcSetError(device, ALC_INVALID_DEVICE);
1435 ProcessContext(NULL);
1436 return NULL;
1439 running = ((device->NumContexts > 0) ? AL_TRUE : AL_FALSE);
1441 // Reset Context Last Error code
1442 device->LastError = ALC_NO_ERROR;
1444 // Check for attributes
1445 if(attrList && attrList[0])
1447 ALCuint freq, numMono, numStereo, numSends;
1449 // If a context is already running on the device, stop playback so the
1450 // device attributes can be updated
1451 if(running)
1453 ProcessContext(NULL);
1454 ALCdevice_StopPlayback(device);
1455 SuspendContext(NULL);
1456 running = AL_FALSE;
1459 freq = device->Frequency;
1460 numMono = device->NumMonoSources;
1461 numStereo = device->NumStereoSources;
1462 numSends = device->NumAuxSends;
1464 attrIdx = 0;
1465 while(attrList[attrIdx])
1467 if(attrList[attrIdx] == ALC_FREQUENCY &&
1468 !ConfigValueExists(NULL, "frequency"))
1470 freq = attrList[attrIdx + 1];
1471 if(freq < 8000)
1472 freq = 8000;
1475 if(attrList[attrIdx] == ALC_STEREO_SOURCES)
1477 numStereo = attrList[attrIdx + 1];
1478 if(numStereo > device->MaxNoOfSources)
1479 numStereo = device->MaxNoOfSources;
1481 numMono = device->MaxNoOfSources - numStereo;
1484 if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS &&
1485 !ConfigValueExists(NULL, "sends"))
1487 numSends = attrList[attrIdx + 1];
1488 if(numSends > MAX_SENDS)
1489 numSends = MAX_SENDS;
1492 attrIdx += 2;
1495 device->UpdateSize = (ALuint64)device->UpdateSize * freq /
1496 device->Frequency;
1498 device->Frequency = freq;
1499 device->NumMonoSources = numMono;
1500 device->NumStereoSources = numStereo;
1501 device->NumAuxSends = numSends;
1504 if(running == AL_FALSE && ALCdevice_ResetPlayback(device) == ALC_FALSE)
1506 alcSetError(device, ALC_INVALID_DEVICE);
1507 aluHandleDisconnect(device);
1508 ProcessContext(NULL);
1509 return NULL;
1511 aluInitPanning(device);
1513 for(i = 0;i < device->NumContexts;i++)
1515 ALCcontext *context = device->Contexts[i];
1516 ALsizei pos;
1518 SuspendContext(context);
1519 for(pos = 0;pos < context->EffectSlotMap.size;pos++)
1521 ALeffectslot *slot = context->EffectSlotMap.array[pos].value;
1522 if(!slot->EffectState)
1523 continue;
1525 if(ALEffect_DeviceUpdate(slot->EffectState, device) == AL_FALSE)
1527 alcSetError(device, ALC_INVALID_DEVICE);
1528 aluHandleDisconnect(device);
1529 ProcessContext(context);
1530 ProcessContext(NULL);
1531 ALCdevice_StopPlayback(device);
1532 return NULL;
1534 ALEffect_Update(slot->EffectState, context, &slot->effect);
1537 for(pos = 0;pos < context->SourceMap.size;pos++)
1539 ALsource *source = context->SourceMap.array[pos].value;
1540 ALuint s = device->NumAuxSends;
1541 while(s < MAX_SENDS)
1543 if(source->Send[s].Slot)
1544 source->Send[s].Slot->refcount--;
1545 source->Send[s].Slot = NULL;
1546 source->Send[s].WetFilter.type = 0;
1547 source->Send[s].WetFilter.filter = 0;
1548 s++;
1550 source->NeedsUpdate = AL_TRUE;
1552 ProcessContext(context);
1555 if(device->Bs2bLevel > 0 && device->Bs2bLevel <= 6)
1557 if(!device->Bs2b)
1559 device->Bs2b = calloc(1, sizeof(*device->Bs2b));
1560 bs2b_clear(device->Bs2b);
1562 bs2b_set_srate(device->Bs2b, device->Frequency);
1563 bs2b_set_level(device->Bs2b, device->Bs2bLevel);
1565 else
1567 free(device->Bs2b);
1568 device->Bs2b = NULL;
1571 temp = realloc(device->Contexts, (device->NumContexts+1) * sizeof(*device->Contexts));
1572 if(!temp)
1574 alcSetError(device, ALC_OUT_OF_MEMORY);
1575 ProcessContext(NULL);
1576 return NULL;
1578 device->Contexts = temp;
1580 ALContext = calloc(1, sizeof(ALCcontext));
1581 if(!ALContext)
1583 alcSetError(device, ALC_OUT_OF_MEMORY);
1584 ProcessContext(NULL);
1585 return NULL;
1588 device->Contexts[device->NumContexts++] = ALContext;
1589 ALContext->Device = device;
1591 InitContext(ALContext);
1593 ALContext->next = g_pContextList;
1594 g_pContextList = ALContext;
1595 g_ulContextCount++;
1597 ProcessContext(NULL);
1599 return ALContext;
1604 alcDestroyContext
1606 Remove a Context
1608 ALC_API ALCvoid ALC_APIENTRY alcDestroyContext(ALCcontext *context)
1610 ALCdevice *Device;
1611 ALCcontext **list;
1612 ALuint i;
1614 if(!IsContext(context))
1616 alcSetError(NULL, ALC_INVALID_CONTEXT);
1617 return;
1620 Device = context->Device;
1622 if(Device->NumContexts == 1)
1623 ALCdevice_StopPlayback(Device);
1625 SuspendContext(NULL);
1627 if(context == GlobalContext)
1628 GlobalContext = NULL;
1630 for(i = 0;i < Device->NumContexts-1;i++)
1632 if(Device->Contexts[i] == context)
1634 Device->Contexts[i] = Device->Contexts[Device->NumContexts-1];
1635 break;
1638 Device->NumContexts--;
1640 // Lock context
1641 SuspendContext(context);
1643 if(context->SourceMap.size > 0)
1645 #ifdef _DEBUG
1646 AL_PRINT("alcDestroyContext(): deleting %d Source(s)\n", context->SourceMap.size);
1647 #endif
1648 ReleaseALSources(context);
1650 ResetUIntMap(&context->SourceMap);
1652 if(context->EffectSlotMap.size > 0)
1654 #ifdef _DEBUG
1655 AL_PRINT("alcDestroyContext(): deleting %d AuxiliaryEffectSlot(s)\n", context->EffectSlotMap.size);
1656 #endif
1657 ReleaseALAuxiliaryEffectSlots(context);
1659 ResetUIntMap(&context->EffectSlotMap);
1661 list = &g_pContextList;
1662 while(*list != context)
1663 list = &(*list)->next;
1665 *list = (*list)->next;
1666 g_ulContextCount--;
1668 // Unlock context
1669 ProcessContext(context);
1670 ProcessContext(NULL);
1672 ExitContext(context);
1674 // Free memory (MUST do this after ProcessContext)
1675 memset(context, 0, sizeof(ALCcontext));
1676 free(context);
1681 alcGetCurrentContext
1683 Returns the currently active Context
1685 ALC_API ALCcontext* ALC_APIENTRY alcGetCurrentContext(ALCvoid)
1687 ALCcontext *pContext;
1689 if((pContext=GetContextSuspended()) != NULL)
1690 ProcessContext(pContext);
1692 return pContext;
1696 alcGetThreadContext
1698 Returns the currently active thread-local Context
1700 ALC_API ALCcontext* ALC_APIENTRY alcGetThreadContext(void)
1702 ALCcontext *pContext = NULL;
1704 SuspendContext(NULL);
1706 pContext = tls_get(LocalContext);
1707 if(pContext && !IsContext(pContext))
1709 tls_set(LocalContext, NULL);
1710 pContext = NULL;
1713 ProcessContext(NULL);
1715 return pContext;
1720 alcGetContextsDevice
1722 Returns the Device that a particular Context is attached to
1724 ALC_API ALCdevice* ALC_APIENTRY alcGetContextsDevice(ALCcontext *pContext)
1726 ALCdevice *pDevice = NULL;
1728 SuspendContext(NULL);
1729 if(IsContext(pContext))
1730 pDevice = pContext->Device;
1731 else
1732 alcSetError(NULL, ALC_INVALID_CONTEXT);
1733 ProcessContext(NULL);
1735 return pDevice;
1740 alcMakeContextCurrent
1742 Makes the given Context the active Context
1744 ALC_API ALCboolean ALC_APIENTRY alcMakeContextCurrent(ALCcontext *context)
1746 ALboolean bReturn = AL_TRUE;
1748 SuspendContext(NULL);
1750 // context must be a valid Context or NULL
1751 if(context == NULL || IsContext(context))
1753 GlobalContext = context;
1754 tls_set(LocalContext, NULL);
1756 else
1758 alcSetError(NULL, ALC_INVALID_CONTEXT);
1759 bReturn = AL_FALSE;
1762 ProcessContext(NULL);
1764 return bReturn;
1768 alcSetThreadContext
1770 Makes the given Context the active Context for the current thread
1772 ALC_API ALCboolean ALC_APIENTRY alcSetThreadContext(ALCcontext *context)
1774 ALboolean bReturn = AL_TRUE;
1776 SuspendContext(NULL);
1778 // context must be a valid Context or NULL
1779 if(context == NULL || IsContext(context))
1780 tls_set(LocalContext, context);
1781 else
1783 alcSetError(NULL, ALC_INVALID_CONTEXT);
1784 bReturn = AL_FALSE;
1787 ProcessContext(NULL);
1789 return bReturn;
1793 // Sets the default channel order used by most non-WaveFormatEx-based APIs
1794 void SetDefaultChannelOrder(ALCdevice *device)
1796 switch(aluChannelsFromFormat(device->Format))
1798 case 1: device->DevChannels[FRONT_CENTER] = 0; break;
1800 case 2: device->DevChannels[FRONT_LEFT] = 0;
1801 device->DevChannels[FRONT_RIGHT] = 1; break;
1803 case 4: device->DevChannels[FRONT_LEFT] = 0;
1804 device->DevChannels[FRONT_RIGHT] = 1;
1805 device->DevChannels[BACK_LEFT] = 2;
1806 device->DevChannels[BACK_RIGHT] = 3; break;
1808 case 6: device->DevChannels[FRONT_LEFT] = 0;
1809 device->DevChannels[FRONT_RIGHT] = 1;
1810 device->DevChannels[BACK_LEFT] = 2;
1811 device->DevChannels[BACK_RIGHT] = 3;
1812 device->DevChannels[FRONT_CENTER] = 4;
1813 device->DevChannels[LFE] = 5; break;
1815 case 7: device->DevChannels[FRONT_LEFT] = 0;
1816 device->DevChannels[FRONT_RIGHT] = 1;
1817 device->DevChannels[FRONT_CENTER] = 2;
1818 device->DevChannels[LFE] = 3;
1819 device->DevChannels[BACK_CENTER] = 4;
1820 device->DevChannels[SIDE_LEFT] = 5;
1821 device->DevChannels[SIDE_RIGHT] = 6; break;
1823 case 8: device->DevChannels[FRONT_LEFT] = 0;
1824 device->DevChannels[FRONT_RIGHT] = 1;
1825 device->DevChannels[BACK_LEFT] = 2;
1826 device->DevChannels[BACK_RIGHT] = 3;
1827 device->DevChannels[FRONT_CENTER] = 4;
1828 device->DevChannels[LFE] = 5;
1829 device->DevChannels[SIDE_LEFT] = 6;
1830 device->DevChannels[SIDE_RIGHT] = 7; break;
1833 // Sets the default order used by WaveFormatEx
1834 void SetDefaultWFXChannelOrder(ALCdevice *device)
1836 switch(aluChannelsFromFormat(device->Format))
1838 case 1: device->DevChannels[FRONT_CENTER] = 0; break;
1840 case 2: device->DevChannels[FRONT_LEFT] = 0;
1841 device->DevChannels[FRONT_RIGHT] = 1; break;
1843 case 4: device->DevChannels[FRONT_LEFT] = 0;
1844 device->DevChannels[FRONT_RIGHT] = 1;
1845 device->DevChannels[BACK_LEFT] = 2;
1846 device->DevChannels[BACK_RIGHT] = 3; break;
1848 case 6: device->DevChannels[FRONT_LEFT] = 0;
1849 device->DevChannels[FRONT_RIGHT] = 1;
1850 device->DevChannels[FRONT_CENTER] = 2;
1851 device->DevChannels[LFE] = 3;
1852 device->DevChannels[BACK_LEFT] = 4;
1853 device->DevChannels[BACK_RIGHT] = 5; break;
1855 case 7: device->DevChannels[FRONT_LEFT] = 0;
1856 device->DevChannels[FRONT_RIGHT] = 1;
1857 device->DevChannels[FRONT_CENTER] = 2;
1858 device->DevChannels[LFE] = 3;
1859 device->DevChannels[BACK_CENTER] = 4;
1860 device->DevChannels[SIDE_LEFT] = 5;
1861 device->DevChannels[SIDE_RIGHT] = 6; break;
1863 case 8: device->DevChannels[FRONT_LEFT] = 0;
1864 device->DevChannels[FRONT_RIGHT] = 1;
1865 device->DevChannels[FRONT_CENTER] = 2;
1866 device->DevChannels[LFE] = 3;
1867 device->DevChannels[BACK_LEFT] = 4;
1868 device->DevChannels[BACK_RIGHT] = 5;
1869 device->DevChannels[SIDE_LEFT] = 6;
1870 device->DevChannels[SIDE_RIGHT] = 7; break;
1874 static ALenum GetFormatFromString(const char *str)
1876 if(strcasecmp(str, "AL_FORMAT_MONO32") == 0) return AL_FORMAT_MONO_FLOAT32;
1877 if(strcasecmp(str, "AL_FORMAT_STEREO32") == 0) return AL_FORMAT_STEREO_FLOAT32;
1878 if(strcasecmp(str, "AL_FORMAT_QUAD32") == 0) return AL_FORMAT_QUAD32;
1879 if(strcasecmp(str, "AL_FORMAT_51CHN32") == 0) return AL_FORMAT_51CHN32;
1880 if(strcasecmp(str, "AL_FORMAT_61CHN32") == 0) return AL_FORMAT_61CHN32;
1881 if(strcasecmp(str, "AL_FORMAT_71CHN32") == 0) return AL_FORMAT_71CHN32;
1883 if(strcasecmp(str, "AL_FORMAT_MONO16") == 0) return AL_FORMAT_MONO16;
1884 if(strcasecmp(str, "AL_FORMAT_STEREO16") == 0) return AL_FORMAT_STEREO16;
1885 if(strcasecmp(str, "AL_FORMAT_QUAD16") == 0) return AL_FORMAT_QUAD16;
1886 if(strcasecmp(str, "AL_FORMAT_51CHN16") == 0) return AL_FORMAT_51CHN16;
1887 if(strcasecmp(str, "AL_FORMAT_61CHN16") == 0) return AL_FORMAT_61CHN16;
1888 if(strcasecmp(str, "AL_FORMAT_71CHN16") == 0) return AL_FORMAT_71CHN16;
1890 if(strcasecmp(str, "AL_FORMAT_MONO8") == 0) return AL_FORMAT_MONO8;
1891 if(strcasecmp(str, "AL_FORMAT_STEREO8") == 0) return AL_FORMAT_STEREO8;
1892 if(strcasecmp(str, "AL_FORMAT_QUAD8") == 0) return AL_FORMAT_QUAD8;
1893 if(strcasecmp(str, "AL_FORMAT_51CHN8") == 0) return AL_FORMAT_51CHN8;
1894 if(strcasecmp(str, "AL_FORMAT_61CHN8") == 0) return AL_FORMAT_61CHN8;
1895 if(strcasecmp(str, "AL_FORMAT_71CHN8") == 0) return AL_FORMAT_71CHN8;
1897 AL_PRINT("Unknown format: \"%s\"\n", str);
1898 return AL_FORMAT_STEREO16;
1902 alcOpenDevice
1904 Open the Device specified.
1906 ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName)
1908 ALboolean bDeviceFound = AL_FALSE;
1909 const ALCchar *fmt;
1910 ALCdevice *device;
1911 ALint i;
1913 if(deviceName && !deviceName[0])
1914 deviceName = NULL;
1916 device = calloc(1, sizeof(ALCdevice));
1917 if(!device)
1919 alcSetError(NULL, ALC_OUT_OF_MEMORY);
1920 return NULL;
1923 //Validate device
1924 device->Connected = ALC_TRUE;
1925 device->IsCaptureDevice = AL_FALSE;
1926 device->LastError = ALC_NO_ERROR;
1928 device->Bs2b = NULL;
1929 device->szDeviceName = NULL;
1931 device->Contexts = NULL;
1932 device->NumContexts = 0;
1934 InitUIntMap(&device->BufferMap);
1935 InitUIntMap(&device->EffectMap);
1937 //Set output format
1938 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
1939 if(device->Frequency < 8000)
1940 device->Frequency = 8000;
1942 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
1943 device->Format = GetFormatFromString(fmt);
1945 device->NumUpdates = GetConfigValueInt(NULL, "periods", 4);
1946 if(device->NumUpdates < 2)
1947 device->NumUpdates = 4;
1949 i = GetConfigValueInt(NULL, "refresh", 4096);
1950 if(i <= 0) i = 4096;
1952 device->UpdateSize = GetConfigValueInt(NULL, "period_size", i/device->NumUpdates);
1953 if(device->UpdateSize <= 0)
1954 device->UpdateSize = i/device->NumUpdates;
1956 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
1957 if((ALint)device->MaxNoOfSources <= 0)
1958 device->MaxNoOfSources = 256;
1960 device->AuxiliaryEffectSlotMax = GetConfigValueInt(NULL, "slots", 4);
1961 if((ALint)device->AuxiliaryEffectSlotMax <= 0)
1962 device->AuxiliaryEffectSlotMax = 4;
1964 device->NumStereoSources = 1;
1965 device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
1967 device->NumAuxSends = GetConfigValueInt(NULL, "sends", MAX_SENDS);
1968 if(device->NumAuxSends > MAX_SENDS)
1969 device->NumAuxSends = MAX_SENDS;
1971 device->Bs2bLevel = GetConfigValueInt(NULL, "cf_level", 0);
1973 if(aluChannelsFromFormat(device->Format) <= 2)
1975 device->HeadDampen = GetConfigValueFloat(NULL, "head_dampen", DEFAULT_HEAD_DAMPEN);
1976 device->HeadDampen = __min(device->HeadDampen, 1.0f);
1977 device->HeadDampen = __max(device->HeadDampen, 0.0f);
1979 else
1980 device->HeadDampen = 0.0f;
1982 // Find a playback device to open
1983 SuspendContext(NULL);
1984 for(i = 0;BackendList[i].Init;i++)
1986 device->Funcs = &BackendList[i].Funcs;
1987 if(ALCdevice_OpenPlayback(device, deviceName))
1989 device->next = g_pDeviceList;
1990 g_pDeviceList = device;
1991 g_ulDeviceCount++;
1993 bDeviceFound = AL_TRUE;
1994 break;
1997 ProcessContext(NULL);
1999 if(!bDeviceFound)
2001 // No suitable output device found
2002 alcSetError(NULL, ALC_INVALID_VALUE);
2003 free(device);
2004 device = NULL;
2007 return device;
2012 alcCloseDevice
2014 Close the specified Device
2016 ALC_API ALCboolean ALC_APIENTRY alcCloseDevice(ALCdevice *pDevice)
2018 ALCdevice **list;
2020 if(!IsDevice(pDevice) || pDevice->IsCaptureDevice)
2022 alcSetError(pDevice, ALC_INVALID_DEVICE);
2023 return ALC_FALSE;
2026 SuspendContext(NULL);
2028 list = &g_pDeviceList;
2029 while(*list != pDevice)
2030 list = &(*list)->next;
2032 *list = (*list)->next;
2033 g_ulDeviceCount--;
2035 ProcessContext(NULL);
2037 if(pDevice->NumContexts > 0)
2039 #ifdef _DEBUG
2040 AL_PRINT("alcCloseDevice(): destroying %u Context(s)\n", pDevice->NumContexts);
2041 #endif
2042 while(pDevice->NumContexts > 0)
2043 alcDestroyContext(pDevice->Contexts[0]);
2045 ALCdevice_ClosePlayback(pDevice);
2047 if(pDevice->BufferMap.size > 0)
2049 #ifdef _DEBUG
2050 AL_PRINT("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice->BufferMap.size);
2051 #endif
2052 ReleaseALBuffers(pDevice);
2054 ResetUIntMap(&pDevice->BufferMap);
2056 if(pDevice->EffectMap.size > 0)
2058 #ifdef _DEBUG
2059 AL_PRINT("alcCloseDevice(): deleting %d Effect(s)\n", pDevice->EffectMap.size);
2060 #endif
2061 ReleaseALEffects(pDevice);
2063 ResetUIntMap(&pDevice->EffectMap);
2065 if(pDevice->FilterCount > 0)
2067 #ifdef _DEBUG
2068 AL_PRINT("alcCloseDevice(): deleting %d Filter(s)\n", pDevice->FilterCount);
2069 #endif
2070 ReleaseALFilters(pDevice);
2072 if(pDevice->DatabufferCount > 0)
2074 #ifdef _DEBUG
2075 AL_PRINT("alcCloseDevice(): deleting %d Databuffer(s)\n", pDevice->DatabufferCount);
2076 #endif
2077 ReleaseALDatabuffers(pDevice);
2080 free(pDevice->Bs2b);
2081 pDevice->Bs2b = NULL;
2083 free(pDevice->szDeviceName);
2084 pDevice->szDeviceName = NULL;
2086 free(pDevice->Contexts);
2087 pDevice->Contexts = NULL;
2089 //Release device structure
2090 memset(pDevice, 0, sizeof(ALCdevice));
2091 free(pDevice);
2093 return ALC_TRUE;
2097 ALCvoid ReleaseALC(ALCvoid)
2099 free(alcDeviceList); alcDeviceList = NULL;
2100 alcDeviceListSize = 0;
2101 free(alcAllDeviceList); alcAllDeviceList = NULL;
2102 alcAllDeviceListSize = 0;
2103 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
2104 alcCaptureDeviceListSize = 0;
2106 free(alcDefaultDeviceSpecifier);
2107 alcDefaultDeviceSpecifier = NULL;
2108 free(alcDefaultAllDeviceSpecifier);
2109 alcDefaultAllDeviceSpecifier = NULL;
2110 free(alcCaptureDefaultDeviceSpecifier);
2111 alcCaptureDefaultDeviceSpecifier = NULL;
2113 #ifdef _DEBUG
2114 if(g_ulDeviceCount > 0)
2115 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
2116 #endif
2118 while(g_pDeviceList)
2120 if(g_pDeviceList->IsCaptureDevice)
2121 alcCaptureCloseDevice(g_pDeviceList);
2122 else
2123 alcCloseDevice(g_pDeviceList);
2127 ///////////////////////////////////////////////////////