Cleanup some ok() macros
[openal-soft.git] / Alc / ALc.c
bloba2b74644ff90c4b50634ec647bc0df0cb84c756f
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 { "null", alc_null_init, alc_null_deinit, alc_null_probe, EmptyFuncs },
74 #ifdef HAVE_WAVE
75 { "wave", alc_wave_init, alc_wave_deinit, alc_wave_probe, EmptyFuncs },
76 #endif
78 { NULL, NULL, NULL, NULL, EmptyFuncs }
80 #undef EmptyFuncs
82 ///////////////////////////////////////////////////////
84 #define ALC_EFX_MAJOR_VERSION 0x20001
85 #define ALC_EFX_MINOR_VERSION 0x20002
86 #define ALC_MAX_AUXILIARY_SENDS 0x20003
88 ///////////////////////////////////////////////////////
89 // STRING and EXTENSIONS
91 typedef struct ALCfunction {
92 const ALCchar *funcName;
93 ALCvoid *address;
94 } ALCfunction;
96 typedef struct ALCenums {
97 const ALCchar *enumName;
98 ALCenum value;
99 } ALCenums;
102 static const ALCfunction alcFunctions[] = {
103 { "alcCreateContext", (ALCvoid *) alcCreateContext },
104 { "alcMakeContextCurrent", (ALCvoid *) alcMakeContextCurrent },
105 { "alcProcessContext", (ALCvoid *) alcProcessContext },
106 { "alcSuspendContext", (ALCvoid *) alcSuspendContext },
107 { "alcDestroyContext", (ALCvoid *) alcDestroyContext },
108 { "alcGetCurrentContext", (ALCvoid *) alcGetCurrentContext },
109 { "alcGetContextsDevice", (ALCvoid *) alcGetContextsDevice },
110 { "alcOpenDevice", (ALCvoid *) alcOpenDevice },
111 { "alcCloseDevice", (ALCvoid *) alcCloseDevice },
112 { "alcGetError", (ALCvoid *) alcGetError },
113 { "alcIsExtensionPresent", (ALCvoid *) alcIsExtensionPresent },
114 { "alcGetProcAddress", (ALCvoid *) alcGetProcAddress },
115 { "alcGetEnumValue", (ALCvoid *) alcGetEnumValue },
116 { "alcGetString", (ALCvoid *) alcGetString },
117 { "alcGetIntegerv", (ALCvoid *) alcGetIntegerv },
118 { "alcCaptureOpenDevice", (ALCvoid *) alcCaptureOpenDevice },
119 { "alcCaptureCloseDevice", (ALCvoid *) alcCaptureCloseDevice },
120 { "alcCaptureStart", (ALCvoid *) alcCaptureStart },
121 { "alcCaptureStop", (ALCvoid *) alcCaptureStop },
122 { "alcCaptureSamples", (ALCvoid *) alcCaptureSamples },
124 { "alcSetThreadContext", (ALCvoid *) alcSetThreadContext },
125 { "alcGetThreadContext", (ALCvoid *) alcGetThreadContext },
127 { "alEnable", (ALCvoid *) alEnable },
128 { "alDisable", (ALCvoid *) alDisable },
129 { "alIsEnabled", (ALCvoid *) alIsEnabled },
130 { "alGetString", (ALCvoid *) alGetString },
131 { "alGetBooleanv", (ALCvoid *) alGetBooleanv },
132 { "alGetIntegerv", (ALCvoid *) alGetIntegerv },
133 { "alGetFloatv", (ALCvoid *) alGetFloatv },
134 { "alGetDoublev", (ALCvoid *) alGetDoublev },
135 { "alGetBoolean", (ALCvoid *) alGetBoolean },
136 { "alGetInteger", (ALCvoid *) alGetInteger },
137 { "alGetFloat", (ALCvoid *) alGetFloat },
138 { "alGetDouble", (ALCvoid *) alGetDouble },
139 { "alGetError", (ALCvoid *) alGetError },
140 { "alIsExtensionPresent", (ALCvoid *) alIsExtensionPresent },
141 { "alGetProcAddress", (ALCvoid *) alGetProcAddress },
142 { "alGetEnumValue", (ALCvoid *) alGetEnumValue },
143 { "alListenerf", (ALCvoid *) alListenerf },
144 { "alListener3f", (ALCvoid *) alListener3f },
145 { "alListenerfv", (ALCvoid *) alListenerfv },
146 { "alListeneri", (ALCvoid *) alListeneri },
147 { "alListener3i", (ALCvoid *) alListener3i },
148 { "alListeneriv", (ALCvoid *) alListeneriv },
149 { "alGetListenerf", (ALCvoid *) alGetListenerf },
150 { "alGetListener3f", (ALCvoid *) alGetListener3f },
151 { "alGetListenerfv", (ALCvoid *) alGetListenerfv },
152 { "alGetListeneri", (ALCvoid *) alGetListeneri },
153 { "alGetListener3i", (ALCvoid *) alGetListener3i },
154 { "alGetListeneriv", (ALCvoid *) alGetListeneriv },
155 { "alGenSources", (ALCvoid *) alGenSources },
156 { "alDeleteSources", (ALCvoid *) alDeleteSources },
157 { "alIsSource", (ALCvoid *) alIsSource },
158 { "alSourcef", (ALCvoid *) alSourcef },
159 { "alSource3f", (ALCvoid *) alSource3f },
160 { "alSourcefv", (ALCvoid *) alSourcefv },
161 { "alSourcei", (ALCvoid *) alSourcei },
162 { "alSource3i", (ALCvoid *) alSource3i },
163 { "alSourceiv", (ALCvoid *) alSourceiv },
164 { "alGetSourcef", (ALCvoid *) alGetSourcef },
165 { "alGetSource3f", (ALCvoid *) alGetSource3f },
166 { "alGetSourcefv", (ALCvoid *) alGetSourcefv },
167 { "alGetSourcei", (ALCvoid *) alGetSourcei },
168 { "alGetSource3i", (ALCvoid *) alGetSource3i },
169 { "alGetSourceiv", (ALCvoid *) alGetSourceiv },
170 { "alSourcePlayv", (ALCvoid *) alSourcePlayv },
171 { "alSourceStopv", (ALCvoid *) alSourceStopv },
172 { "alSourceRewindv", (ALCvoid *) alSourceRewindv },
173 { "alSourcePausev", (ALCvoid *) alSourcePausev },
174 { "alSourcePlay", (ALCvoid *) alSourcePlay },
175 { "alSourceStop", (ALCvoid *) alSourceStop },
176 { "alSourceRewind", (ALCvoid *) alSourceRewind },
177 { "alSourcePause", (ALCvoid *) alSourcePause },
178 { "alSourceQueueBuffers", (ALCvoid *) alSourceQueueBuffers },
179 { "alSourceUnqueueBuffers", (ALCvoid *) alSourceUnqueueBuffers },
180 { "alGenBuffers", (ALCvoid *) alGenBuffers },
181 { "alDeleteBuffers", (ALCvoid *) alDeleteBuffers },
182 { "alIsBuffer", (ALCvoid *) alIsBuffer },
183 { "alBufferData", (ALCvoid *) alBufferData },
184 { "alBufferf", (ALCvoid *) alBufferf },
185 { "alBuffer3f", (ALCvoid *) alBuffer3f },
186 { "alBufferfv", (ALCvoid *) alBufferfv },
187 { "alBufferi", (ALCvoid *) alBufferi },
188 { "alBuffer3i", (ALCvoid *) alBuffer3i },
189 { "alBufferiv", (ALCvoid *) alBufferiv },
190 { "alGetBufferf", (ALCvoid *) alGetBufferf },
191 { "alGetBuffer3f", (ALCvoid *) alGetBuffer3f },
192 { "alGetBufferfv", (ALCvoid *) alGetBufferfv },
193 { "alGetBufferi", (ALCvoid *) alGetBufferi },
194 { "alGetBuffer3i", (ALCvoid *) alGetBuffer3i },
195 { "alGetBufferiv", (ALCvoid *) alGetBufferiv },
196 { "alDopplerFactor", (ALCvoid *) alDopplerFactor },
197 { "alDopplerVelocity", (ALCvoid *) alDopplerVelocity },
198 { "alSpeedOfSound", (ALCvoid *) alSpeedOfSound },
199 { "alDistanceModel", (ALCvoid *) alDistanceModel },
201 { "alGenFilters", (ALCvoid *) alGenFilters },
202 { "alDeleteFilters", (ALCvoid *) alDeleteFilters },
203 { "alIsFilter", (ALCvoid *) alIsFilter },
204 { "alFilteri", (ALCvoid *) alFilteri },
205 { "alFilteriv", (ALCvoid *) alFilteriv },
206 { "alFilterf", (ALCvoid *) alFilterf },
207 { "alFilterfv", (ALCvoid *) alFilterfv },
208 { "alGetFilteri", (ALCvoid *) alGetFilteri },
209 { "alGetFilteriv", (ALCvoid *) alGetFilteriv },
210 { "alGetFilterf", (ALCvoid *) alGetFilterf },
211 { "alGetFilterfv", (ALCvoid *) alGetFilterfv },
213 { "alGenEffects", (ALCvoid *) alGenEffects },
214 { "alDeleteEffects", (ALCvoid *) alDeleteEffects },
215 { "alIsEffect", (ALCvoid *) alIsEffect },
216 { "alEffecti", (ALCvoid *) alEffecti },
217 { "alEffectiv", (ALCvoid *) alEffectiv },
218 { "alEffectf", (ALCvoid *) alEffectf },
219 { "alEffectfv", (ALCvoid *) alEffectfv },
220 { "alGetEffecti", (ALCvoid *) alGetEffecti },
221 { "alGetEffectiv", (ALCvoid *) alGetEffectiv },
222 { "alGetEffectf", (ALCvoid *) alGetEffectf },
223 { "alGetEffectfv", (ALCvoid *) alGetEffectfv },
225 { "alGenAuxiliaryEffectSlots", (ALCvoid *) alGenAuxiliaryEffectSlots},
226 { "alDeleteAuxiliaryEffectSlots",(ALCvoid *) alDeleteAuxiliaryEffectSlots},
227 { "alIsAuxiliaryEffectSlot", (ALCvoid *) alIsAuxiliaryEffectSlot },
228 { "alAuxiliaryEffectSloti", (ALCvoid *) alAuxiliaryEffectSloti },
229 { "alAuxiliaryEffectSlotiv", (ALCvoid *) alAuxiliaryEffectSlotiv },
230 { "alAuxiliaryEffectSlotf", (ALCvoid *) alAuxiliaryEffectSlotf },
231 { "alAuxiliaryEffectSlotfv", (ALCvoid *) alAuxiliaryEffectSlotfv },
232 { "alGetAuxiliaryEffectSloti", (ALCvoid *) alGetAuxiliaryEffectSloti},
233 { "alGetAuxiliaryEffectSlotiv", (ALCvoid *) alGetAuxiliaryEffectSlotiv},
234 { "alGetAuxiliaryEffectSlotf", (ALCvoid *) alGetAuxiliaryEffectSlotf},
235 { "alGetAuxiliaryEffectSlotfv", (ALCvoid *) alGetAuxiliaryEffectSlotfv},
237 { "alBufferSubDataSOFT", (ALCvoid *) alBufferSubDataSOFT },
238 #if 0
239 { "alGenDatabuffersEXT", (ALCvoid *) alGenDatabuffersEXT },
240 { "alDeleteDatabuffersEXT", (ALCvoid *) alDeleteDatabuffersEXT },
241 { "alIsDatabufferEXT", (ALCvoid *) alIsDatabufferEXT },
242 { "alDatabufferDataEXT", (ALCvoid *) alDatabufferDataEXT },
243 { "alDatabufferSubDataEXT", (ALCvoid *) alDatabufferSubDataEXT },
244 { "alGetDatabufferSubDataEXT", (ALCvoid *) alGetDatabufferSubDataEXT},
245 { "alDatabufferfEXT", (ALCvoid *) alDatabufferfEXT },
246 { "alDatabufferfvEXT", (ALCvoid *) alDatabufferfvEXT },
247 { "alDatabufferiEXT", (ALCvoid *) alDatabufferiEXT },
248 { "alDatabufferivEXT", (ALCvoid *) alDatabufferivEXT },
249 { "alGetDatabufferfEXT", (ALCvoid *) alGetDatabufferfEXT },
250 { "alGetDatabufferfvEXT", (ALCvoid *) alGetDatabufferfvEXT },
251 { "alGetDatabufferiEXT", (ALCvoid *) alGetDatabufferiEXT },
252 { "alGetDatabufferivEXT", (ALCvoid *) alGetDatabufferivEXT },
253 { "alSelectDatabufferEXT", (ALCvoid *) alSelectDatabufferEXT },
254 { "alMapDatabufferEXT", (ALCvoid *) alMapDatabufferEXT },
255 { "alUnmapDatabufferEXT", (ALCvoid *) alUnmapDatabufferEXT },
256 #endif
257 { NULL, (ALCvoid *) NULL }
260 static const ALCenums enumeration[] = {
261 // Types
262 { "ALC_INVALID", ALC_INVALID },
263 { "ALC_FALSE", ALC_FALSE },
264 { "ALC_TRUE", ALC_TRUE },
266 // ALC Properties
267 { "ALC_MAJOR_VERSION", ALC_MAJOR_VERSION },
268 { "ALC_MINOR_VERSION", ALC_MINOR_VERSION },
269 { "ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE },
270 { "ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES },
271 { "ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER },
272 { "ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER },
273 { "ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER },
274 { "ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER },
275 { "ALC_EXTENSIONS", ALC_EXTENSIONS },
276 { "ALC_FREQUENCY", ALC_FREQUENCY },
277 { "ALC_REFRESH", ALC_REFRESH },
278 { "ALC_SYNC", ALC_SYNC },
279 { "ALC_MONO_SOURCES", ALC_MONO_SOURCES },
280 { "ALC_STEREO_SOURCES", ALC_STEREO_SOURCES },
281 { "ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER },
282 { "ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER},
283 { "ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES },
284 { "ALC_CONNECTED", ALC_CONNECTED },
286 // EFX Properties
287 { "ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION },
288 { "ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION },
289 { "ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS },
291 // ALC Error Message
292 { "ALC_NO_ERROR", ALC_NO_ERROR },
293 { "ALC_INVALID_DEVICE", ALC_INVALID_DEVICE },
294 { "ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT },
295 { "ALC_INVALID_ENUM", ALC_INVALID_ENUM },
296 { "ALC_INVALID_VALUE", ALC_INVALID_VALUE },
297 { "ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY },
298 { NULL, (ALCenum)0 }
300 // Error strings
301 static const ALCchar alcNoError[] = "No Error";
302 static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
303 static const ALCchar alcErrInvalidContext[] = "Invalid Context";
304 static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
305 static const ALCchar alcErrInvalidValue[] = "Invalid Value";
306 static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
308 /* Device lists. Sizes only include the first ending null character, not the
309 * second */
310 static ALCchar *alcDeviceList;
311 static size_t alcDeviceListSize;
312 static ALCchar *alcAllDeviceList;
313 static size_t alcAllDeviceListSize;
314 static ALCchar *alcCaptureDeviceList;
315 static size_t alcCaptureDeviceListSize;
316 // Default is always the first in the list
317 static ALCchar *alcDefaultDeviceSpecifier;
318 static ALCchar *alcDefaultAllDeviceSpecifier;
319 static ALCchar *alcCaptureDefaultDeviceSpecifier;
322 static const ALCchar alcNoDeviceExtList[] =
323 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
324 "ALC_EXT_thread_local_context";
325 static const ALCchar alcExtensionList[] =
326 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
327 "ALC_EXT_disconnect ALC_EXT_EFX ALC_EXT_thread_local_context";
328 static const ALCint alcMajorVersion = 1;
329 static const ALCint alcMinorVersion = 1;
331 static const ALCint alcEFXMajorVersion = 1;
332 static const ALCint alcEFXMinorVersion = 0;
334 ///////////////////////////////////////////////////////
337 ///////////////////////////////////////////////////////
338 // Global Variables
340 static ALCdevice *g_pDeviceList = NULL;
341 static ALCuint g_ulDeviceCount = 0;
343 static CRITICAL_SECTION g_csMutex;
345 // Context List
346 static ALCcontext *g_pContextList = NULL;
347 static ALCuint g_ulContextCount = 0;
349 // Thread-local current context
350 static tls_type LocalContext;
351 // Process-wide current context
352 static ALCcontext *GlobalContext;
354 // Context Error
355 static ALCenum g_eLastNullDeviceError = ALC_NO_ERROR;
357 // Default context extensions
358 static const ALchar alExtList[] =
359 "AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 AL_EXT_IMA4 "
360 "AL_EXT_LINEAR_DISTANCE AL_EXTX_loop_points AL_EXT_MCFORMATS AL_EXT_MULAW "
361 "AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET AL_EXT_source_distance_model "
362 "AL_LOKI_quadriphonic AL_SOFT_buffer_sub_data";
364 // Mixing Priority Level
365 static ALint RTPrioLevel;
367 // Output Log File
368 static FILE *LogFile;
370 ///////////////////////////////////////////////////////
373 ///////////////////////////////////////////////////////
374 // ALC Related helper functions
375 static void ReleaseALC(void);
377 #ifdef HAVE_GCC_DESTRUCTOR
378 static void alc_init(void) __attribute__((constructor));
379 static void alc_deinit(void) __attribute__((destructor));
380 #else
381 #ifdef _WIN32
382 static void alc_init(void);
383 static void alc_deinit(void);
385 BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
387 (void)lpReserved;
389 // Perform actions based on the reason for calling.
390 switch(ul_reason_for_call)
392 case DLL_PROCESS_ATTACH:
393 DisableThreadLibraryCalls(hModule);
394 alc_init();
395 break;
397 case DLL_PROCESS_DETACH:
398 alc_deinit();
399 break;
401 return TRUE;
403 #endif
404 #endif
406 static void alc_init(void)
408 int i;
409 const char *devs, *str;
411 str = getenv("ALSOFT_LOGFILE");
412 if(str && str[0])
414 LogFile = fopen(str, "w");
415 if(!LogFile)
416 fprintf(stderr, "AL lib: Failed to open log file '%s'\n", str);
418 if(!LogFile)
419 LogFile = stderr;
421 InitializeCriticalSection(&g_csMutex);
422 ALTHUNK_INIT();
423 ReadALConfig();
425 tls_create(&LocalContext);
427 RTPrioLevel = GetConfigValueInt(NULL, "rt-prio", 0);
429 DefaultResampler = GetConfigValueInt(NULL, "resampler", RESAMPLER_DEFAULT);
430 if(DefaultResampler >= RESAMPLER_MAX || DefaultResampler <= RESAMPLER_MIN)
431 DefaultResampler = RESAMPLER_DEFAULT;
433 devs = GetConfigValue(NULL, "drivers", "");
434 if(devs[0])
436 int n;
437 size_t len;
438 const char *next = devs;
439 int endlist, delitem;
441 i = 0;
442 do {
443 devs = next;
444 next = strchr(devs, ',');
446 delitem = (devs[0] == '-');
447 if(devs[0] == '-') devs++;
449 if(!devs[0] || devs[0] == ',')
451 endlist = 0;
452 continue;
454 endlist = 1;
456 len = (next ? ((size_t)(next-devs)) : strlen(devs));
457 for(n = i;BackendList[n].Init;n++)
459 if(len == strlen(BackendList[n].name) &&
460 strncmp(BackendList[n].name, devs, len) == 0)
462 if(delitem)
464 do {
465 BackendList[n] = BackendList[n+1];
466 ++n;
467 } while(BackendList[n].Init);
469 else
471 BackendInfo Bkp = BackendList[n];
472 while(n > i)
474 BackendList[n] = BackendList[n-1];
475 --n;
477 BackendList[n] = Bkp;
479 i++;
481 break;
484 } while(next++);
486 if(endlist)
488 BackendList[i].name = NULL;
489 BackendList[i].Init = NULL;
490 BackendList[i].Deinit = NULL;
491 BackendList[i].Probe = NULL;
495 for(i = 0;BackendList[i].Init;i++)
496 BackendList[i].Init(&BackendList[i].Funcs);
498 str = GetConfigValue(NULL, "excludefx", "");
499 if(str[0])
501 const struct {
502 const char *name;
503 int type;
504 } EffectList[] = {
505 { "eaxreverb", EAXREVERB },
506 { "reverb", REVERB },
507 { "echo", ECHO },
508 { "modulator", MODULATOR },
509 { NULL, 0 }
511 int n;
512 size_t len;
513 const char *next = str;
515 do {
516 str = next;
517 next = strchr(str, ',');
519 if(!str[0] || next == str)
520 continue;
522 len = (next ? ((size_t)(next-str)) : strlen(str));
523 for(n = 0;EffectList[n].name;n++)
525 if(len == strlen(EffectList[n].name) &&
526 strncmp(EffectList[n].name, str, len) == 0)
527 DisabledEffects[EffectList[n].type] = AL_TRUE;
529 } while(next++);
533 static void alc_deinit(void)
535 int i;
537 ReleaseALC();
539 for(i = 0;BackendList[i].Deinit;i++)
540 BackendList[i].Deinit();
542 tls_delete(LocalContext);
544 FreeALConfig();
545 ALTHUNK_EXIT();
546 DeleteCriticalSection(&g_csMutex);
548 if(LogFile != stderr)
549 fclose(LogFile);
550 LogFile = NULL;
554 static void ProbeDeviceList()
556 ALint i;
558 free(alcDeviceList); alcDeviceList = NULL;
559 alcDeviceListSize = 0;
561 for(i = 0;BackendList[i].Probe;i++)
562 BackendList[i].Probe(DEVICE_PROBE);
565 static void ProbeAllDeviceList()
567 ALint i;
569 free(alcAllDeviceList); alcAllDeviceList = NULL;
570 alcAllDeviceListSize = 0;
572 for(i = 0;BackendList[i].Probe;i++)
573 BackendList[i].Probe(ALL_DEVICE_PROBE);
576 static void ProbeCaptureDeviceList()
578 ALint i;
580 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
581 alcCaptureDeviceListSize = 0;
583 for(i = 0;BackendList[i].Probe;i++)
584 BackendList[i].Probe(CAPTURE_DEVICE_PROBE);
588 #define DECL_APPEND_LIST_FUNC(type) \
589 void Append##type##List(const ALCchar *name) \
591 size_t len = strlen(name); \
592 void *temp; \
594 if(len == 0) \
595 return; \
597 temp = realloc(alc##type##List, alc##type##ListSize + len + 2); \
598 if(!temp) \
600 AL_PRINT("Realloc failed to add %s!\n", name); \
601 return; \
603 alc##type##List = temp; \
604 memcpy(alc##type##List+alc##type##ListSize, name, len+1); \
605 alc##type##ListSize += len+1; \
606 alc##type##List[alc##type##ListSize] = 0; \
609 DECL_APPEND_LIST_FUNC(Device)
610 DECL_APPEND_LIST_FUNC(AllDevice)
611 DECL_APPEND_LIST_FUNC(CaptureDevice)
614 void al_print(const char *fname, unsigned int line, const char *fmt, ...)
616 const char *fn;
617 char str[256];
618 int i;
620 fn = strrchr(fname, '/');
621 if(!fn) fn = strrchr(fname, '\\');;
622 if(!fn) fn = fname;
623 else fn += 1;
625 i = snprintf(str, sizeof(str), "AL lib: %s:%d: ", fn, line);
626 if(i < (int)sizeof(str) && i > 0)
628 va_list ap;
629 va_start(ap, fmt);
630 vsnprintf(str+i, sizeof(str)-i, fmt, ap);
631 va_end(ap);
633 str[sizeof(str)-1] = 0;
635 fprintf(LogFile, "%s", str);
636 fflush(LogFile);
639 void SetRTPriority(void)
641 ALboolean failed;
643 #ifdef _WIN32
644 if(RTPrioLevel > 0)
645 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
646 else
647 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
648 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM)
649 struct sched_param param;
651 if(RTPrioLevel > 0)
653 /* Use the minimum real-time priority possible for now (on Linux this
654 * should be 1 for SCHED_RR) */
655 param.sched_priority = sched_get_priority_min(SCHED_RR);
656 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
658 else
660 param.sched_priority = 0;
661 failed = !!pthread_setschedparam(pthread_self(), SCHED_OTHER, &param);
663 #else
664 /* Real-time priority not available */
665 failed = (RTPrioLevel>0);
666 #endif
667 if(failed)
668 AL_PRINT("Failed to set priority level for thread\n");
672 void InitUIntMap(UIntMap *map)
674 map->array = NULL;
675 map->size = 0;
676 map->maxsize = 0;
679 void ResetUIntMap(UIntMap *map)
681 free(map->array);
682 map->array = NULL;
683 map->size = 0;
684 map->maxsize = 0;
687 ALenum InsertUIntMapEntry(UIntMap *map, ALuint key, ALvoid *value)
689 ALsizei pos = 0;
691 if(map->size > 0)
693 ALsizei low = 0;
694 ALsizei high = map->size - 1;
695 while(low < high)
697 ALsizei mid = low + (high-low)/2;
698 if(map->array[mid].key < key)
699 low = mid + 1;
700 else
701 high = mid;
703 if(map->array[low].key < key)
704 low++;
705 pos = low;
708 if(pos == map->size || map->array[pos].key != key)
710 if(map->size == map->maxsize)
712 ALvoid *temp;
713 ALsizei newsize;
715 newsize = (map->maxsize ? (map->maxsize<<1) : 4);
716 if(newsize < map->maxsize)
717 return AL_OUT_OF_MEMORY;
719 temp = realloc(map->array, newsize*sizeof(map->array[0]));
720 if(!temp) return AL_OUT_OF_MEMORY;
721 map->array = temp;
722 map->maxsize = newsize;
725 map->size++;
726 if(pos < map->size-1)
727 memmove(&map->array[pos+1], &map->array[pos],
728 (map->size-1-pos)*sizeof(map->array[0]));
730 map->array[pos].key = key;
731 map->array[pos].value = value;
733 return AL_NO_ERROR;
736 void RemoveUIntMapKey(UIntMap *map, ALuint key)
738 if(map->size > 0)
740 ALsizei low = 0;
741 ALsizei high = map->size - 1;
742 while(low < high)
744 ALsizei mid = low + (high-low)/2;
745 if(map->array[mid].key < key)
746 low = mid + 1;
747 else
748 high = mid;
750 if(map->array[low].key == key)
752 if(low < map->size-1)
753 memmove(&map->array[low], &map->array[low+1],
754 (map->size-1-low)*sizeof(map->array[0]));
755 map->size--;
760 ALvoid *LookupUIntMapKey(UIntMap *map, ALuint key)
762 if(map->size > 0)
764 ALsizei low = 0;
765 ALsizei high = map->size - 1;
766 while(low < high)
768 ALsizei mid = low + (high-low)/2;
769 if(map->array[mid].key < key)
770 low = mid + 1;
771 else
772 high = mid;
774 if(map->array[low].key == key)
775 return map->array[low].value;
777 return NULL;
781 ALuint BytesFromDevFmt(enum DevFmtType type)
783 switch(type)
785 case DevFmtByte: return sizeof(ALbyte);
786 case DevFmtUByte: return sizeof(ALubyte);
787 case DevFmtShort: return sizeof(ALshort);
788 case DevFmtUShort: return sizeof(ALushort);
789 case DevFmtFloat: return sizeof(ALfloat);
791 return 0;
793 ALuint ChannelsFromDevFmt(enum DevFmtChannels chans)
795 switch(chans)
797 case DevFmtMono: return 1;
798 case DevFmtStereo: return 2;
799 case DevFmtQuad: return 4;
800 case DevFmtX51: return 6;
801 case DevFmtX61: return 7;
802 case DevFmtX71: return 8;
804 return 0;
806 ALboolean DecomposeDevFormat(ALenum format, enum DevFmtChannels *chans,
807 enum DevFmtType *type)
809 switch(format)
811 case AL_FORMAT_MONO8:
812 *chans = DevFmtMono;
813 *type = DevFmtUByte;
814 return AL_TRUE;
815 case AL_FORMAT_MONO16:
816 *chans = DevFmtMono;
817 *type = DevFmtShort;
818 return AL_TRUE;
819 case AL_FORMAT_MONO_FLOAT32:
820 *chans = DevFmtMono;
821 *type = DevFmtFloat;
822 return AL_TRUE;
823 case AL_FORMAT_STEREO8:
824 *chans = DevFmtStereo;
825 *type = DevFmtUByte;
826 return AL_TRUE;
827 case AL_FORMAT_STEREO16:
828 *chans = DevFmtStereo;
829 *type = DevFmtShort;
830 return AL_TRUE;
831 case AL_FORMAT_STEREO_FLOAT32:
832 *chans = DevFmtStereo;
833 *type = DevFmtFloat;
834 return AL_TRUE;
835 case AL_FORMAT_QUAD8:
836 *chans = DevFmtQuad;
837 *type = DevFmtUByte;
838 return AL_TRUE;
839 case AL_FORMAT_QUAD16:
840 *chans = DevFmtQuad;
841 *type = DevFmtShort;
842 return AL_TRUE;
843 case AL_FORMAT_QUAD32:
844 *chans = DevFmtQuad;
845 *type = DevFmtFloat;
846 return AL_TRUE;
847 case AL_FORMAT_51CHN8:
848 *chans = DevFmtX51;
849 *type = DevFmtUByte;
850 return AL_TRUE;
851 case AL_FORMAT_51CHN16:
852 *chans = DevFmtX51;
853 *type = DevFmtShort;
854 return AL_TRUE;
855 case AL_FORMAT_51CHN32:
856 *chans = DevFmtX51;
857 *type = DevFmtFloat;
858 return AL_TRUE;
859 case AL_FORMAT_61CHN8:
860 *chans = DevFmtX61;
861 *type = DevFmtUByte;
862 return AL_TRUE;
863 case AL_FORMAT_61CHN16:
864 *chans = DevFmtX61;
865 *type = DevFmtShort;
866 return AL_TRUE;
867 case AL_FORMAT_61CHN32:
868 *chans = DevFmtX61;
869 *type = DevFmtFloat;
870 return AL_TRUE;
871 case AL_FORMAT_71CHN8:
872 *chans = DevFmtX71;
873 *type = DevFmtUByte;
874 return AL_TRUE;
875 case AL_FORMAT_71CHN16:
876 *chans = DevFmtX71;
877 *type = DevFmtShort;
878 return AL_TRUE;
879 case AL_FORMAT_71CHN32:
880 *chans = DevFmtX71;
881 *type = DevFmtFloat;
882 return AL_TRUE;
884 return AL_FALSE;
888 IsDevice
890 Check pDevice is a valid Device pointer
892 static ALCboolean IsDevice(ALCdevice *pDevice)
894 ALCdevice *pTempDevice;
896 SuspendContext(NULL);
898 pTempDevice = g_pDeviceList;
899 while(pTempDevice && pTempDevice != pDevice)
900 pTempDevice = pTempDevice->next;
902 ProcessContext(NULL);
904 return (pTempDevice ? ALC_TRUE : ALC_FALSE);
908 IsContext
910 Check pContext is a valid Context pointer
912 static ALCboolean IsContext(ALCcontext *pContext)
914 ALCcontext *pTempContext;
916 SuspendContext(NULL);
918 pTempContext = g_pContextList;
919 while (pTempContext && pTempContext != pContext)
920 pTempContext = pTempContext->next;
922 ProcessContext(NULL);
924 return (pTempContext ? ALC_TRUE : ALC_FALSE);
929 alcSetError
931 Store latest ALC Error
933 ALCvoid alcSetError(ALCdevice *device, ALenum errorCode)
935 if(IsDevice(device))
936 device->LastError = errorCode;
937 else
938 g_eLastNullDeviceError = errorCode;
942 /* UpdateDeviceParams:
944 * Updates device parameters according to the attribute list.
946 static ALCboolean UpdateDeviceParams(ALCdevice *device, const ALCint *attrList)
948 ALCuint freq, numMono, numStereo, numSends;
949 ALboolean running;
950 ALuint oldRate;
951 ALuint attrIdx;
952 ALuint i;
954 running = ((device->NumContexts > 0) ? AL_TRUE : AL_FALSE);
955 oldRate = device->Frequency;
957 // Check for attributes
958 if(attrList && attrList[0])
960 // If a context is already running on the device, stop playback so the
961 // device attributes can be updated
962 if(running)
964 ProcessContext(NULL);
965 ALCdevice_StopPlayback(device);
966 SuspendContext(NULL);
967 running = AL_FALSE;
970 freq = device->Frequency;
971 numMono = device->NumMonoSources;
972 numStereo = device->NumStereoSources;
973 numSends = device->NumAuxSends;
975 attrIdx = 0;
976 while(attrList[attrIdx])
978 if(attrList[attrIdx] == ALC_FREQUENCY &&
979 !ConfigValueExists(NULL, "frequency"))
981 freq = attrList[attrIdx + 1];
982 if(freq < 8000)
983 freq = 8000;
986 if(attrList[attrIdx] == ALC_STEREO_SOURCES)
988 numStereo = attrList[attrIdx + 1];
989 if(numStereo > device->MaxNoOfSources)
990 numStereo = device->MaxNoOfSources;
992 numMono = device->MaxNoOfSources - numStereo;
995 if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS &&
996 !ConfigValueExists(NULL, "sends"))
998 numSends = attrList[attrIdx + 1];
999 if(numSends > MAX_SENDS)
1000 numSends = MAX_SENDS;
1003 attrIdx += 2;
1006 device->UpdateSize = (ALuint64)device->UpdateSize * freq /
1007 device->Frequency;
1009 device->Frequency = freq;
1010 device->NumMonoSources = numMono;
1011 device->NumStereoSources = numStereo;
1012 device->NumAuxSends = numSends;
1015 if(running)
1016 return ALC_TRUE;
1018 if(ALCdevice_ResetPlayback(device) == ALC_FALSE)
1019 return ALC_FALSE;
1021 aluInitPanning(device);
1023 for(i = 0;i < MAXCHANNELS;i++)
1025 device->ClickRemoval[i] = 0.0f;
1026 device->PendingClicks[i] = 0.0f;
1029 for(i = 0;i < device->NumContexts;i++)
1031 ALCcontext *context = device->Contexts[i];
1032 ALsizei pos;
1034 SuspendContext(context);
1035 for(pos = 0;pos < context->EffectSlotMap.size;pos++)
1037 ALeffectslot *slot = context->EffectSlotMap.array[pos].value;
1039 if(ALEffect_DeviceUpdate(slot->EffectState, device) == AL_FALSE)
1041 ProcessContext(context);
1042 return ALC_FALSE;
1044 ALEffect_Update(slot->EffectState, context, &slot->effect);
1047 for(pos = 0;pos < context->SourceMap.size;pos++)
1049 ALsource *source = context->SourceMap.array[pos].value;
1050 ALuint s = device->NumAuxSends;
1051 while(s < MAX_SENDS)
1053 if(source->Send[s].Slot)
1054 source->Send[s].Slot->refcount--;
1055 source->Send[s].Slot = NULL;
1056 source->Send[s].WetFilter.type = 0;
1057 source->Send[s].WetFilter.filter = 0;
1058 s++;
1060 source->NeedsUpdate = AL_TRUE;
1062 ProcessContext(context);
1065 if(device->Bs2bLevel > 0 && device->Bs2bLevel <= 6)
1067 if(!device->Bs2b)
1069 device->Bs2b = calloc(1, sizeof(*device->Bs2b));
1070 bs2b_clear(device->Bs2b);
1072 bs2b_set_srate(device->Bs2b, device->Frequency);
1073 bs2b_set_level(device->Bs2b, device->Bs2bLevel);
1075 else
1077 free(device->Bs2b);
1078 device->Bs2b = NULL;
1081 if(ChannelsFromDevFmt(device->FmtChans) <= 2)
1083 device->HeadDampen = GetConfigValueFloat(NULL, "head_dampen", DEFAULT_HEAD_DAMPEN);
1084 device->HeadDampen = __min(device->HeadDampen, 1.0f);
1085 device->HeadDampen = __max(device->HeadDampen, 0.0f);
1087 else
1088 device->HeadDampen = 0.0f;
1090 return ALC_TRUE;
1095 SuspendContext
1097 Thread-safe entry
1099 ALCvoid SuspendContext(ALCcontext *pContext)
1101 (void)pContext;
1102 EnterCriticalSection(&g_csMutex);
1107 ProcessContext
1109 Thread-safe exit
1111 ALCvoid ProcessContext(ALCcontext *pContext)
1113 (void)pContext;
1114 LeaveCriticalSection(&g_csMutex);
1119 GetContextSuspended
1121 Returns the currently active Context, in a locked state
1123 ALCcontext *GetContextSuspended(void)
1125 ALCcontext *pContext = NULL;
1127 SuspendContext(NULL);
1129 pContext = tls_get(LocalContext);
1130 if(pContext && !IsContext(pContext))
1132 tls_set(LocalContext, NULL);
1133 pContext = NULL;
1135 if(!pContext)
1136 pContext = GlobalContext;
1138 if(pContext)
1139 SuspendContext(pContext);
1141 ProcessContext(NULL);
1143 return pContext;
1148 InitContext
1150 Initialize Context variables
1152 static ALvoid InitContext(ALCcontext *pContext)
1154 //Initialise listener
1155 pContext->Listener.Gain = 1.0f;
1156 pContext->Listener.MetersPerUnit = 1.0f;
1157 pContext->Listener.Position[0] = 0.0f;
1158 pContext->Listener.Position[1] = 0.0f;
1159 pContext->Listener.Position[2] = 0.0f;
1160 pContext->Listener.Velocity[0] = 0.0f;
1161 pContext->Listener.Velocity[1] = 0.0f;
1162 pContext->Listener.Velocity[2] = 0.0f;
1163 pContext->Listener.Forward[0] = 0.0f;
1164 pContext->Listener.Forward[1] = 0.0f;
1165 pContext->Listener.Forward[2] = -1.0f;
1166 pContext->Listener.Up[0] = 0.0f;
1167 pContext->Listener.Up[1] = 1.0f;
1168 pContext->Listener.Up[2] = 0.0f;
1170 //Validate pContext
1171 pContext->LastError = AL_NO_ERROR;
1172 pContext->Suspended = AL_FALSE;
1173 pContext->ActiveSourceCount = 0;
1174 InitUIntMap(&pContext->SourceMap);
1175 InitUIntMap(&pContext->EffectSlotMap);
1177 //Set globals
1178 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
1179 pContext->SourceDistanceModel = AL_FALSE;
1180 pContext->DopplerFactor = 1.0f;
1181 pContext->DopplerVelocity = 1.0f;
1182 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
1184 pContext->ExtensionList = alExtList;
1189 ExitContext
1191 Clean up Context, destroy any remaining Sources
1193 static ALCvoid ExitContext(ALCcontext *pContext)
1195 //Invalidate context
1196 pContext->LastError = AL_NO_ERROR;
1199 ///////////////////////////////////////////////////////
1202 ///////////////////////////////////////////////////////
1203 // ALC Functions calls
1206 // This should probably move to another c file but for now ...
1207 ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
1209 ALCboolean DeviceFound = ALC_FALSE;
1210 ALCdevice *device = NULL;
1211 ALCint i;
1213 if(SampleSize <= 0)
1215 alcSetError(NULL, ALC_INVALID_VALUE);
1216 return NULL;
1219 if(deviceName && !deviceName[0])
1220 deviceName = NULL;
1222 device = calloc(1, sizeof(ALCdevice));
1223 if(!device)
1225 alcSetError(NULL, ALC_OUT_OF_MEMORY);
1226 return NULL;
1229 //Validate device
1230 device->Connected = ALC_TRUE;
1231 device->IsCaptureDevice = AL_TRUE;
1233 device->szDeviceName = NULL;
1235 device->Frequency = frequency;
1236 if(DecomposeDevFormat(format, &device->FmtChans, &device->FmtType) == AL_FALSE)
1238 free(device);
1239 alcSetError(NULL, ALC_INVALID_ENUM);
1240 return NULL;
1243 device->UpdateSize = SampleSize;
1244 device->NumUpdates = 1;
1246 SuspendContext(NULL);
1247 for(i = 0;BackendList[i].Init;i++)
1249 device->Funcs = &BackendList[i].Funcs;
1250 if(ALCdevice_OpenCapture(device, deviceName))
1252 device->next = g_pDeviceList;
1253 g_pDeviceList = device;
1254 g_ulDeviceCount++;
1256 DeviceFound = ALC_TRUE;
1257 break;
1260 ProcessContext(NULL);
1262 if(!DeviceFound)
1264 alcSetError(NULL, ALC_INVALID_VALUE);
1265 free(device);
1266 device = NULL;
1269 return device;
1272 ALC_API ALCboolean ALC_APIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
1274 ALCdevice **list;
1276 if(!IsDevice(pDevice) || !pDevice->IsCaptureDevice)
1278 alcSetError(pDevice, ALC_INVALID_DEVICE);
1279 return ALC_FALSE;
1282 SuspendContext(NULL);
1284 list = &g_pDeviceList;
1285 while(*list != pDevice)
1286 list = &(*list)->next;
1288 *list = (*list)->next;
1289 g_ulDeviceCount--;
1291 ProcessContext(NULL);
1293 ALCdevice_CloseCapture(pDevice);
1295 free(pDevice->szDeviceName);
1296 pDevice->szDeviceName = NULL;
1298 free(pDevice);
1300 return ALC_TRUE;
1303 ALC_API void ALC_APIENTRY alcCaptureStart(ALCdevice *device)
1305 SuspendContext(NULL);
1306 if(!IsDevice(device) || !device->IsCaptureDevice)
1307 alcSetError(device, ALC_INVALID_DEVICE);
1308 else if(device->Connected)
1309 ALCdevice_StartCapture(device);
1310 ProcessContext(NULL);
1313 ALC_API void ALC_APIENTRY alcCaptureStop(ALCdevice *device)
1315 SuspendContext(NULL);
1316 if(!IsDevice(device) || !device->IsCaptureDevice)
1317 alcSetError(device, ALC_INVALID_DEVICE);
1318 else
1319 ALCdevice_StopCapture(device);
1320 ProcessContext(NULL);
1323 ALC_API void ALC_APIENTRY alcCaptureSamples(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
1325 SuspendContext(NULL);
1326 if(!IsDevice(device) || !device->IsCaptureDevice)
1327 alcSetError(device, ALC_INVALID_DEVICE);
1328 else
1329 ALCdevice_CaptureSamples(device, buffer, samples);
1330 ProcessContext(NULL);
1334 alcGetError
1336 Return last ALC generated error code
1338 ALC_API ALCenum ALC_APIENTRY alcGetError(ALCdevice *device)
1340 ALCenum errorCode;
1342 if(IsDevice(device))
1344 errorCode = device->LastError;
1345 device->LastError = ALC_NO_ERROR;
1347 else
1349 errorCode = g_eLastNullDeviceError;
1350 g_eLastNullDeviceError = ALC_NO_ERROR;
1352 return errorCode;
1357 alcSuspendContext
1359 Not functional
1361 ALC_API ALCvoid ALC_APIENTRY alcSuspendContext(ALCcontext *pContext)
1363 SuspendContext(NULL);
1364 if(IsContext(pContext))
1365 pContext->Suspended = AL_TRUE;
1366 ProcessContext(NULL);
1371 alcProcessContext
1373 Not functional
1375 ALC_API ALCvoid ALC_APIENTRY alcProcessContext(ALCcontext *pContext)
1377 SuspendContext(NULL);
1378 if(IsContext(pContext))
1379 pContext->Suspended = AL_FALSE;
1380 ProcessContext(NULL);
1385 alcGetString
1387 Returns information about the Device, and error strings
1389 ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
1391 const ALCchar *value = NULL;
1393 switch (param)
1395 case ALC_NO_ERROR:
1396 value = alcNoError;
1397 break;
1399 case ALC_INVALID_ENUM:
1400 value = alcErrInvalidEnum;
1401 break;
1403 case ALC_INVALID_VALUE:
1404 value = alcErrInvalidValue;
1405 break;
1407 case ALC_INVALID_DEVICE:
1408 value = alcErrInvalidDevice;
1409 break;
1411 case ALC_INVALID_CONTEXT:
1412 value = alcErrInvalidContext;
1413 break;
1415 case ALC_OUT_OF_MEMORY:
1416 value = alcErrOutOfMemory;
1417 break;
1419 case ALC_DEVICE_SPECIFIER:
1420 if(IsDevice(pDevice))
1421 value = pDevice->szDeviceName;
1422 else
1424 ProbeDeviceList();
1425 value = alcDeviceList;
1427 break;
1429 case ALC_ALL_DEVICES_SPECIFIER:
1430 ProbeAllDeviceList();
1431 value = alcAllDeviceList;
1432 break;
1434 case ALC_CAPTURE_DEVICE_SPECIFIER:
1435 if(IsDevice(pDevice))
1436 value = pDevice->szDeviceName;
1437 else
1439 ProbeCaptureDeviceList();
1440 value = alcCaptureDeviceList;
1442 break;
1444 /* Default devices are always first in the list */
1445 case ALC_DEFAULT_DEVICE_SPECIFIER:
1446 if(!alcDeviceList)
1447 ProbeDeviceList();
1449 free(alcDefaultDeviceSpecifier);
1450 alcDefaultDeviceSpecifier = strdup(alcDeviceList ? alcDeviceList : "");
1451 if(!alcDefaultDeviceSpecifier)
1452 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
1453 value = alcDefaultDeviceSpecifier;
1454 break;
1456 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
1457 if(!alcAllDeviceList)
1458 ProbeAllDeviceList();
1460 free(alcDefaultAllDeviceSpecifier);
1461 alcDefaultAllDeviceSpecifier = strdup(alcAllDeviceList ?
1462 alcAllDeviceList : "");
1463 if(!alcDefaultAllDeviceSpecifier)
1464 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
1465 value = alcDefaultAllDeviceSpecifier;
1466 break;
1468 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
1469 if(!alcCaptureDeviceList)
1470 ProbeCaptureDeviceList();
1472 free(alcCaptureDefaultDeviceSpecifier);
1473 alcCaptureDefaultDeviceSpecifier = strdup(alcCaptureDeviceList ?
1474 alcCaptureDeviceList : "");
1475 if(!alcCaptureDefaultDeviceSpecifier)
1476 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
1477 value = alcCaptureDefaultDeviceSpecifier;
1478 break;
1480 case ALC_EXTENSIONS:
1481 if(IsDevice(pDevice))
1482 value = alcExtensionList;
1483 else
1484 value = alcNoDeviceExtList;
1485 break;
1487 default:
1488 alcSetError(pDevice, ALC_INVALID_ENUM);
1489 break;
1492 return value;
1497 alcGetIntegerv
1499 Returns information about the Device and the version of Open AL
1501 ALC_API ALCvoid ALC_APIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
1503 if(size == 0 || data == NULL)
1505 alcSetError(device, ALC_INVALID_VALUE);
1506 return;
1509 if(IsDevice(device) && device->IsCaptureDevice)
1511 SuspendContext(NULL);
1513 // Capture device
1514 switch (param)
1516 case ALC_CAPTURE_SAMPLES:
1517 *data = ALCdevice_AvailableSamples(device);
1518 break;
1520 case ALC_CONNECTED:
1521 *data = device->Connected;
1522 break;
1524 default:
1525 alcSetError(device, ALC_INVALID_ENUM);
1526 break;
1529 ProcessContext(NULL);
1530 return;
1533 // Playback Device
1534 switch (param)
1536 case ALC_MAJOR_VERSION:
1537 *data = alcMajorVersion;
1538 break;
1540 case ALC_MINOR_VERSION:
1541 *data = alcMinorVersion;
1542 break;
1544 case ALC_EFX_MAJOR_VERSION:
1545 *data = alcEFXMajorVersion;
1546 break;
1548 case ALC_EFX_MINOR_VERSION:
1549 *data = alcEFXMinorVersion;
1550 break;
1552 case ALC_MAX_AUXILIARY_SENDS:
1553 if(!IsDevice(device))
1554 alcSetError(device, ALC_INVALID_DEVICE);
1555 else
1556 *data = device->NumAuxSends;
1557 break;
1559 case ALC_ATTRIBUTES_SIZE:
1560 if(!IsDevice(device))
1561 alcSetError(device, ALC_INVALID_DEVICE);
1562 else
1563 *data = 13;
1564 break;
1566 case ALC_ALL_ATTRIBUTES:
1567 if(!IsDevice(device))
1568 alcSetError(device, ALC_INVALID_DEVICE);
1569 else if (size < 13)
1570 alcSetError(device, ALC_INVALID_VALUE);
1571 else
1573 int i = 0;
1575 SuspendContext(NULL);
1576 data[i++] = ALC_FREQUENCY;
1577 data[i++] = device->Frequency;
1579 data[i++] = ALC_REFRESH;
1580 data[i++] = device->Frequency / device->UpdateSize;
1582 data[i++] = ALC_SYNC;
1583 data[i++] = ALC_FALSE;
1585 data[i++] = ALC_MONO_SOURCES;
1586 data[i++] = device->NumMonoSources;
1588 data[i++] = ALC_STEREO_SOURCES;
1589 data[i++] = device->NumStereoSources;
1591 data[i++] = ALC_MAX_AUXILIARY_SENDS;
1592 data[i++] = device->NumAuxSends;
1594 data[i++] = 0;
1595 ProcessContext(NULL);
1597 break;
1599 case ALC_FREQUENCY:
1600 if(!IsDevice(device))
1601 alcSetError(device, ALC_INVALID_DEVICE);
1602 else
1603 *data = device->Frequency;
1604 break;
1606 case ALC_REFRESH:
1607 if(!IsDevice(device))
1608 alcSetError(device, ALC_INVALID_DEVICE);
1609 else
1610 *data = device->Frequency / device->UpdateSize;
1611 break;
1613 case ALC_SYNC:
1614 if(!IsDevice(device))
1615 alcSetError(device, ALC_INVALID_DEVICE);
1616 else
1617 *data = ALC_FALSE;
1618 break;
1620 case ALC_MONO_SOURCES:
1621 if(!IsDevice(device))
1622 alcSetError(device, ALC_INVALID_DEVICE);
1623 else
1624 *data = device->NumMonoSources;
1625 break;
1627 case ALC_STEREO_SOURCES:
1628 if(!IsDevice(device))
1629 alcSetError(device, ALC_INVALID_DEVICE);
1630 else
1631 *data = device->NumStereoSources;
1632 break;
1634 case ALC_CONNECTED:
1635 if(!IsDevice(device))
1636 alcSetError(device, ALC_INVALID_DEVICE);
1637 else
1638 *data = device->Connected;
1639 break;
1641 default:
1642 alcSetError(device, ALC_INVALID_ENUM);
1643 break;
1649 alcIsExtensionPresent
1651 Determines if there is support for a particular extension
1653 ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
1655 ALCboolean bResult = ALC_FALSE;
1656 const char *ptr;
1657 size_t len;
1659 if(!extName)
1661 alcSetError(device, ALC_INVALID_VALUE);
1662 return ALC_FALSE;
1665 len = strlen(extName);
1666 ptr = (IsDevice(device) ? alcExtensionList : alcNoDeviceExtList);
1667 while(ptr && *ptr)
1669 if(strncasecmp(ptr, extName, len) == 0 &&
1670 (ptr[len] == '\0' || isspace(ptr[len])))
1672 bResult = ALC_TRUE;
1673 break;
1675 if((ptr=strchr(ptr, ' ')) != NULL)
1677 do {
1678 ++ptr;
1679 } while(isspace(*ptr));
1683 return bResult;
1688 alcGetProcAddress
1690 Retrieves the function address for a particular extension function
1692 ALC_API ALCvoid* ALC_APIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
1694 ALsizei i = 0;
1696 if(!funcName)
1698 alcSetError(device, ALC_INVALID_VALUE);
1699 return NULL;
1702 while(alcFunctions[i].funcName && strcmp(alcFunctions[i].funcName,funcName) != 0)
1703 i++;
1704 return alcFunctions[i].address;
1709 alcGetEnumValue
1711 Get the value for a particular ALC Enumerated Value
1713 ALC_API ALCenum ALC_APIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
1715 ALsizei i = 0;
1717 if(!enumName)
1719 alcSetError(device, ALC_INVALID_VALUE);
1720 return (ALCenum)0;
1723 while(enumeration[i].enumName && strcmp(enumeration[i].enumName,enumName) != 0)
1724 i++;
1725 return enumeration[i].value;
1730 alcCreateContext
1732 Create and attach a Context to a particular Device.
1734 ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
1736 ALCcontext *ALContext;
1737 void *temp;
1739 SuspendContext(NULL);
1741 if(!IsDevice(device) || device->IsCaptureDevice || !device->Connected)
1743 alcSetError(device, ALC_INVALID_DEVICE);
1744 ProcessContext(NULL);
1745 return NULL;
1748 // Reset Context Last Error code
1749 device->LastError = ALC_NO_ERROR;
1751 if(UpdateDeviceParams(device, attrList) == ALC_FALSE)
1753 alcSetError(device, ALC_INVALID_DEVICE);
1754 aluHandleDisconnect(device);
1755 ProcessContext(NULL);
1756 ALCdevice_StopPlayback(device);
1757 return NULL;
1760 ALContext = NULL;
1761 temp = realloc(device->Contexts, (device->NumContexts+1) * sizeof(*device->Contexts));
1762 if(temp)
1764 device->Contexts = temp;
1766 ALContext = calloc(1, sizeof(ALCcontext));
1767 if(ALContext)
1769 ALContext->MaxActiveSources = 256;
1770 ALContext->ActiveSources = malloc(sizeof(ALContext->ActiveSources[0]) *
1771 ALContext->MaxActiveSources);
1774 if(!ALContext || !ALContext->ActiveSources)
1776 free(ALContext);
1777 alcSetError(device, ALC_OUT_OF_MEMORY);
1778 ProcessContext(NULL);
1779 if(device->NumContexts == 0)
1780 ALCdevice_StopPlayback(device);
1781 return NULL;
1784 device->Contexts[device->NumContexts++] = ALContext;
1785 ALContext->Device = device;
1787 InitContext(ALContext);
1789 ALContext->next = g_pContextList;
1790 g_pContextList = ALContext;
1791 g_ulContextCount++;
1793 ProcessContext(NULL);
1795 return ALContext;
1800 alcDestroyContext
1802 Remove a Context
1804 ALC_API ALCvoid ALC_APIENTRY alcDestroyContext(ALCcontext *context)
1806 ALCdevice *Device;
1807 ALCcontext **list;
1808 ALuint i;
1810 if(!IsContext(context))
1812 alcSetError(NULL, ALC_INVALID_CONTEXT);
1813 return;
1816 Device = context->Device;
1818 if(Device->NumContexts == 1)
1819 ALCdevice_StopPlayback(Device);
1821 SuspendContext(NULL);
1823 if(context == GlobalContext)
1824 GlobalContext = NULL;
1826 for(i = 0;i < Device->NumContexts;i++)
1828 if(Device->Contexts[i] == context)
1830 Device->Contexts[i] = Device->Contexts[Device->NumContexts-1];
1831 Device->NumContexts--;
1832 break;
1836 // Lock context
1837 SuspendContext(context);
1839 if(context->SourceMap.size > 0)
1841 #ifdef _DEBUG
1842 AL_PRINT("alcDestroyContext(): deleting %d Source(s)\n", context->SourceMap.size);
1843 #endif
1844 ReleaseALSources(context);
1846 ResetUIntMap(&context->SourceMap);
1848 if(context->EffectSlotMap.size > 0)
1850 #ifdef _DEBUG
1851 AL_PRINT("alcDestroyContext(): deleting %d AuxiliaryEffectSlot(s)\n", context->EffectSlotMap.size);
1852 #endif
1853 ReleaseALAuxiliaryEffectSlots(context);
1855 ResetUIntMap(&context->EffectSlotMap);
1857 free(context->ActiveSources);
1858 context->ActiveSources = NULL;
1859 context->MaxActiveSources = 0;
1860 context->ActiveSourceCount = 0;
1862 list = &g_pContextList;
1863 while(*list != context)
1864 list = &(*list)->next;
1866 *list = (*list)->next;
1867 g_ulContextCount--;
1869 // Unlock context
1870 ProcessContext(context);
1871 ProcessContext(NULL);
1873 ExitContext(context);
1875 // Free memory (MUST do this after ProcessContext)
1876 memset(context, 0, sizeof(ALCcontext));
1877 free(context);
1882 alcGetCurrentContext
1884 Returns the currently active Context
1886 ALC_API ALCcontext* ALC_APIENTRY alcGetCurrentContext(ALCvoid)
1888 ALCcontext *pContext;
1890 if((pContext=GetContextSuspended()) != NULL)
1891 ProcessContext(pContext);
1893 return pContext;
1897 alcGetThreadContext
1899 Returns the currently active thread-local Context
1901 ALC_API ALCcontext* ALC_APIENTRY alcGetThreadContext(void)
1903 ALCcontext *pContext = NULL;
1905 SuspendContext(NULL);
1907 pContext = tls_get(LocalContext);
1908 if(pContext && !IsContext(pContext))
1910 tls_set(LocalContext, NULL);
1911 pContext = NULL;
1914 ProcessContext(NULL);
1916 return pContext;
1921 alcGetContextsDevice
1923 Returns the Device that a particular Context is attached to
1925 ALC_API ALCdevice* ALC_APIENTRY alcGetContextsDevice(ALCcontext *pContext)
1927 ALCdevice *pDevice = NULL;
1929 SuspendContext(NULL);
1930 if(IsContext(pContext))
1931 pDevice = pContext->Device;
1932 else
1933 alcSetError(NULL, ALC_INVALID_CONTEXT);
1934 ProcessContext(NULL);
1936 return pDevice;
1941 alcMakeContextCurrent
1943 Makes the given Context the active Context
1945 ALC_API ALCboolean ALC_APIENTRY alcMakeContextCurrent(ALCcontext *context)
1947 ALboolean bReturn = AL_TRUE;
1949 SuspendContext(NULL);
1951 // context must be a valid Context or NULL
1952 if(context == NULL || IsContext(context))
1954 GlobalContext = context;
1955 tls_set(LocalContext, NULL);
1957 else
1959 alcSetError(NULL, ALC_INVALID_CONTEXT);
1960 bReturn = AL_FALSE;
1963 ProcessContext(NULL);
1965 return bReturn;
1969 alcSetThreadContext
1971 Makes the given Context the active Context for the current thread
1973 ALC_API ALCboolean ALC_APIENTRY alcSetThreadContext(ALCcontext *context)
1975 ALboolean bReturn = AL_TRUE;
1977 SuspendContext(NULL);
1979 // context must be a valid Context or NULL
1980 if(context == NULL || IsContext(context))
1981 tls_set(LocalContext, context);
1982 else
1984 alcSetError(NULL, ALC_INVALID_CONTEXT);
1985 bReturn = AL_FALSE;
1988 ProcessContext(NULL);
1990 return bReturn;
1994 // Sets the default channel order used by most non-WaveFormatEx-based APIs
1995 void SetDefaultChannelOrder(ALCdevice *device)
1997 switch(device->FmtChans)
1999 case DevFmtMono: device->DevChannels[FRONT_CENTER] = 0; break;
2001 case DevFmtStereo: device->DevChannels[FRONT_LEFT] = 0;
2002 device->DevChannels[FRONT_RIGHT] = 1; break;
2004 case DevFmtQuad: device->DevChannels[FRONT_LEFT] = 0;
2005 device->DevChannels[FRONT_RIGHT] = 1;
2006 device->DevChannels[BACK_LEFT] = 2;
2007 device->DevChannels[BACK_RIGHT] = 3; break;
2009 case DevFmtX51: device->DevChannels[FRONT_LEFT] = 0;
2010 device->DevChannels[FRONT_RIGHT] = 1;
2011 device->DevChannels[BACK_LEFT] = 2;
2012 device->DevChannels[BACK_RIGHT] = 3;
2013 device->DevChannels[FRONT_CENTER] = 4;
2014 device->DevChannels[LFE] = 5; break;
2016 case DevFmtX61: device->DevChannels[FRONT_LEFT] = 0;
2017 device->DevChannels[FRONT_RIGHT] = 1;
2018 device->DevChannels[FRONT_CENTER] = 2;
2019 device->DevChannels[LFE] = 3;
2020 device->DevChannels[BACK_CENTER] = 4;
2021 device->DevChannels[SIDE_LEFT] = 5;
2022 device->DevChannels[SIDE_RIGHT] = 6; break;
2024 case DevFmtX71: device->DevChannels[FRONT_LEFT] = 0;
2025 device->DevChannels[FRONT_RIGHT] = 1;
2026 device->DevChannels[BACK_LEFT] = 2;
2027 device->DevChannels[BACK_RIGHT] = 3;
2028 device->DevChannels[FRONT_CENTER] = 4;
2029 device->DevChannels[LFE] = 5;
2030 device->DevChannels[SIDE_LEFT] = 6;
2031 device->DevChannels[SIDE_RIGHT] = 7; break;
2034 // Sets the default order used by WaveFormatEx
2035 void SetDefaultWFXChannelOrder(ALCdevice *device)
2037 switch(device->FmtChans)
2039 case DevFmtMono: device->DevChannels[FRONT_CENTER] = 0; break;
2041 case DevFmtStereo: device->DevChannels[FRONT_LEFT] = 0;
2042 device->DevChannels[FRONT_RIGHT] = 1; break;
2044 case DevFmtQuad: device->DevChannels[FRONT_LEFT] = 0;
2045 device->DevChannels[FRONT_RIGHT] = 1;
2046 device->DevChannels[BACK_LEFT] = 2;
2047 device->DevChannels[BACK_RIGHT] = 3; break;
2049 case DevFmtX51: device->DevChannels[FRONT_LEFT] = 0;
2050 device->DevChannels[FRONT_RIGHT] = 1;
2051 device->DevChannels[FRONT_CENTER] = 2;
2052 device->DevChannels[LFE] = 3;
2053 device->DevChannels[BACK_LEFT] = 4;
2054 device->DevChannels[BACK_RIGHT] = 5; break;
2056 case DevFmtX61: device->DevChannels[FRONT_LEFT] = 0;
2057 device->DevChannels[FRONT_RIGHT] = 1;
2058 device->DevChannels[FRONT_CENTER] = 2;
2059 device->DevChannels[LFE] = 3;
2060 device->DevChannels[BACK_CENTER] = 4;
2061 device->DevChannels[SIDE_LEFT] = 5;
2062 device->DevChannels[SIDE_RIGHT] = 6; break;
2064 case DevFmtX71: device->DevChannels[FRONT_LEFT] = 0;
2065 device->DevChannels[FRONT_RIGHT] = 1;
2066 device->DevChannels[FRONT_CENTER] = 2;
2067 device->DevChannels[LFE] = 3;
2068 device->DevChannels[BACK_LEFT] = 4;
2069 device->DevChannels[BACK_RIGHT] = 5;
2070 device->DevChannels[SIDE_LEFT] = 6;
2071 device->DevChannels[SIDE_RIGHT] = 7; break;
2075 static ALenum GetFormatFromString(const char *str)
2077 if(strcasecmp(str, "AL_FORMAT_MONO32") == 0) return AL_FORMAT_MONO_FLOAT32;
2078 if(strcasecmp(str, "AL_FORMAT_STEREO32") == 0) return AL_FORMAT_STEREO_FLOAT32;
2079 if(strcasecmp(str, "AL_FORMAT_QUAD32") == 0) return AL_FORMAT_QUAD32;
2080 if(strcasecmp(str, "AL_FORMAT_51CHN32") == 0) return AL_FORMAT_51CHN32;
2081 if(strcasecmp(str, "AL_FORMAT_61CHN32") == 0) return AL_FORMAT_61CHN32;
2082 if(strcasecmp(str, "AL_FORMAT_71CHN32") == 0) return AL_FORMAT_71CHN32;
2084 if(strcasecmp(str, "AL_FORMAT_MONO16") == 0) return AL_FORMAT_MONO16;
2085 if(strcasecmp(str, "AL_FORMAT_STEREO16") == 0) return AL_FORMAT_STEREO16;
2086 if(strcasecmp(str, "AL_FORMAT_QUAD16") == 0) return AL_FORMAT_QUAD16;
2087 if(strcasecmp(str, "AL_FORMAT_51CHN16") == 0) return AL_FORMAT_51CHN16;
2088 if(strcasecmp(str, "AL_FORMAT_61CHN16") == 0) return AL_FORMAT_61CHN16;
2089 if(strcasecmp(str, "AL_FORMAT_71CHN16") == 0) return AL_FORMAT_71CHN16;
2091 if(strcasecmp(str, "AL_FORMAT_MONO8") == 0) return AL_FORMAT_MONO8;
2092 if(strcasecmp(str, "AL_FORMAT_STEREO8") == 0) return AL_FORMAT_STEREO8;
2093 if(strcasecmp(str, "AL_FORMAT_QUAD8") == 0) return AL_FORMAT_QUAD8;
2094 if(strcasecmp(str, "AL_FORMAT_51CHN8") == 0) return AL_FORMAT_51CHN8;
2095 if(strcasecmp(str, "AL_FORMAT_61CHN8") == 0) return AL_FORMAT_61CHN8;
2096 if(strcasecmp(str, "AL_FORMAT_71CHN8") == 0) return AL_FORMAT_71CHN8;
2098 AL_PRINT("Unknown format: \"%s\"\n", str);
2099 return AL_FORMAT_STEREO16;
2103 alcOpenDevice
2105 Open the Device specified.
2107 ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName)
2109 ALboolean bDeviceFound = AL_FALSE;
2110 const ALCchar *fmt;
2111 ALCdevice *device;
2112 ALint i;
2114 if(deviceName && !deviceName[0])
2115 deviceName = NULL;
2117 device = calloc(1, sizeof(ALCdevice));
2118 if(!device)
2120 alcSetError(NULL, ALC_OUT_OF_MEMORY);
2121 return NULL;
2124 //Validate device
2125 device->Connected = ALC_TRUE;
2126 device->IsCaptureDevice = AL_FALSE;
2127 device->LastError = ALC_NO_ERROR;
2129 device->Bs2b = NULL;
2130 device->szDeviceName = NULL;
2132 device->Contexts = NULL;
2133 device->NumContexts = 0;
2135 InitUIntMap(&device->BufferMap);
2136 InitUIntMap(&device->EffectMap);
2137 InitUIntMap(&device->FilterMap);
2138 InitUIntMap(&device->DatabufferMap);
2140 //Set output format
2141 device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
2142 if(device->Frequency < 8000)
2143 device->Frequency = 8000;
2145 fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
2146 if(DecomposeDevFormat(GetFormatFromString(fmt),
2147 &device->FmtChans, &device->FmtType) == AL_FALSE)
2149 /* Should never happen... */
2150 device->FmtChans = DevFmtStereo;
2151 device->FmtType = DevFmtShort;
2154 device->NumUpdates = GetConfigValueInt(NULL, "periods", 4);
2155 if(device->NumUpdates < 2)
2156 device->NumUpdates = 4;
2158 device->UpdateSize = GetConfigValueInt(NULL, "period_size", 1024);
2159 if(device->UpdateSize <= 0)
2160 device->UpdateSize = 1024;
2162 device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
2163 if((ALint)device->MaxNoOfSources <= 0)
2164 device->MaxNoOfSources = 256;
2166 device->AuxiliaryEffectSlotMax = GetConfigValueInt(NULL, "slots", 4);
2167 if((ALint)device->AuxiliaryEffectSlotMax <= 0)
2168 device->AuxiliaryEffectSlotMax = 4;
2170 device->NumStereoSources = 1;
2171 device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
2173 device->NumAuxSends = GetConfigValueInt(NULL, "sends", 1);
2174 if(device->NumAuxSends > MAX_SENDS)
2175 device->NumAuxSends = MAX_SENDS;
2177 device->Bs2bLevel = GetConfigValueInt(NULL, "cf_level", 0);
2179 device->DuplicateStereo = GetConfigValueBool(NULL, "stereodup", 1);
2181 device->HeadDampen = 0.0f;
2183 // Find a playback device to open
2184 SuspendContext(NULL);
2185 for(i = 0;BackendList[i].Init;i++)
2187 device->Funcs = &BackendList[i].Funcs;
2188 if(ALCdevice_OpenPlayback(device, deviceName))
2190 device->next = g_pDeviceList;
2191 g_pDeviceList = device;
2192 g_ulDeviceCount++;
2194 bDeviceFound = AL_TRUE;
2195 break;
2198 ProcessContext(NULL);
2200 if(!bDeviceFound)
2202 // No suitable output device found
2203 alcSetError(NULL, ALC_INVALID_VALUE);
2204 free(device);
2205 device = NULL;
2208 return device;
2213 alcCloseDevice
2215 Close the specified Device
2217 ALC_API ALCboolean ALC_APIENTRY alcCloseDevice(ALCdevice *pDevice)
2219 ALCdevice **list;
2221 if(!IsDevice(pDevice) || pDevice->IsCaptureDevice)
2223 alcSetError(pDevice, ALC_INVALID_DEVICE);
2224 return ALC_FALSE;
2227 SuspendContext(NULL);
2229 list = &g_pDeviceList;
2230 while(*list != pDevice)
2231 list = &(*list)->next;
2233 *list = (*list)->next;
2234 g_ulDeviceCount--;
2236 ProcessContext(NULL);
2238 if(pDevice->NumContexts > 0)
2240 #ifdef _DEBUG
2241 AL_PRINT("alcCloseDevice(): destroying %u Context(s)\n", pDevice->NumContexts);
2242 #endif
2243 while(pDevice->NumContexts > 0)
2244 alcDestroyContext(pDevice->Contexts[0]);
2246 ALCdevice_ClosePlayback(pDevice);
2248 if(pDevice->BufferMap.size > 0)
2250 #ifdef _DEBUG
2251 AL_PRINT("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice->BufferMap.size);
2252 #endif
2253 ReleaseALBuffers(pDevice);
2255 ResetUIntMap(&pDevice->BufferMap);
2257 if(pDevice->EffectMap.size > 0)
2259 #ifdef _DEBUG
2260 AL_PRINT("alcCloseDevice(): deleting %d Effect(s)\n", pDevice->EffectMap.size);
2261 #endif
2262 ReleaseALEffects(pDevice);
2264 ResetUIntMap(&pDevice->EffectMap);
2266 if(pDevice->FilterMap.size > 0)
2268 #ifdef _DEBUG
2269 AL_PRINT("alcCloseDevice(): deleting %d Filter(s)\n", pDevice->FilterMap.size);
2270 #endif
2271 ReleaseALFilters(pDevice);
2273 ResetUIntMap(&pDevice->FilterMap);
2275 if(pDevice->DatabufferMap.size > 0)
2277 #ifdef _DEBUG
2278 AL_PRINT("alcCloseDevice(): deleting %d Databuffer(s)\n", pDevice->DatabufferMap.size);
2279 #endif
2280 ReleaseALDatabuffers(pDevice);
2282 ResetUIntMap(&pDevice->DatabufferMap);
2284 free(pDevice->Bs2b);
2285 pDevice->Bs2b = NULL;
2287 free(pDevice->szDeviceName);
2288 pDevice->szDeviceName = NULL;
2290 free(pDevice->Contexts);
2291 pDevice->Contexts = NULL;
2293 //Release device structure
2294 memset(pDevice, 0, sizeof(ALCdevice));
2295 free(pDevice);
2297 return ALC_TRUE;
2301 static void ReleaseALC(void)
2303 free(alcDeviceList); alcDeviceList = NULL;
2304 alcDeviceListSize = 0;
2305 free(alcAllDeviceList); alcAllDeviceList = NULL;
2306 alcAllDeviceListSize = 0;
2307 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
2308 alcCaptureDeviceListSize = 0;
2310 free(alcDefaultDeviceSpecifier);
2311 alcDefaultDeviceSpecifier = NULL;
2312 free(alcDefaultAllDeviceSpecifier);
2313 alcDefaultAllDeviceSpecifier = NULL;
2314 free(alcCaptureDefaultDeviceSpecifier);
2315 alcCaptureDefaultDeviceSpecifier = NULL;
2317 #ifdef _DEBUG
2318 if(g_ulDeviceCount > 0)
2319 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
2320 #endif
2322 while(g_pDeviceList)
2324 if(g_pDeviceList->IsCaptureDevice)
2325 alcCaptureCloseDevice(g_pDeviceList);
2326 else
2327 alcCloseDevice(g_pDeviceList);
2331 ///////////////////////////////////////////////////////