Remove some more hungarian notation
[openal-soft/openal-hmr.git] / Alc / ALc.c
blobd8211d030ec9bdf284aa152f2b5972dbbf786642
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <math.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <memory.h>
27 #include <ctype.h>
28 #include <signal.h>
30 #include "alMain.h"
31 #include "alSource.h"
32 #include "AL/al.h"
33 #include "AL/alc.h"
34 #include "alThunk.h"
35 #include "alSource.h"
36 #include "alBuffer.h"
37 #include "alAuxEffectSlot.h"
38 #include "alError.h"
39 #include "bs2b.h"
40 #include "alu.h"
43 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
44 static struct BackendInfo BackendList[] = {
45 #ifdef HAVE_PULSEAUDIO
46 { "pulse", alc_pulse_init, alc_pulse_deinit, alc_pulse_probe, EmptyFuncs },
47 #endif
48 #ifdef HAVE_ALSA
49 { "alsa", alc_alsa_init, alc_alsa_deinit, alc_alsa_probe, EmptyFuncs },
50 #endif
51 #ifdef HAVE_COREAUDIO
52 { "core", alc_ca_init, alc_ca_deinit, alc_ca_probe, EmptyFuncs },
53 #endif
54 #ifdef HAVE_OSS
55 { "oss", alc_oss_init, alc_oss_deinit, alc_oss_probe, EmptyFuncs },
56 #endif
57 #ifdef HAVE_SOLARIS
58 { "solaris", alc_solaris_init, alc_solaris_deinit, alc_solaris_probe, EmptyFuncs },
59 #endif
60 #ifdef HAVE_SNDIO
61 { "sndio", alc_sndio_init, alc_sndio_deinit, alc_sndio_probe, EmptyFuncs },
62 #endif
63 #ifdef HAVE_MMDEVAPI
64 { "mmdevapi", alcMMDevApiInit, alcMMDevApiDeinit, alcMMDevApiProbe, EmptyFuncs },
65 #endif
66 #ifdef HAVE_DSOUND
67 { "dsound", alcDSoundInit, alcDSoundDeinit, alcDSoundProbe, EmptyFuncs },
68 #endif
69 #ifdef HAVE_WINMM
70 { "winmm", alcWinMMInit, alcWinMMDeinit, alcWinMMProbe, EmptyFuncs },
71 #endif
72 #ifdef HAVE_PORTAUDIO
73 { "port", alc_pa_init, alc_pa_deinit, alc_pa_probe, EmptyFuncs },
74 #endif
75 #ifdef HAVE_OPENSL
76 { "opensl", alc_opensl_init, alc_opensl_deinit, alc_opensl_probe, EmptyFuncs },
77 #endif
79 { "null", alc_null_init, alc_null_deinit, alc_null_probe, EmptyFuncs },
80 #ifdef HAVE_WAVE
81 { "wave", alc_wave_init, alc_wave_deinit, alc_wave_probe, EmptyFuncs },
82 #endif
84 { NULL, NULL, NULL, NULL, EmptyFuncs }
86 static struct BackendInfo BackendLoopback = {
87 "loopback", alc_loopback_init, alc_loopback_deinit, alc_loopback_probe, EmptyFuncs
89 #undef EmptyFuncs
91 static struct BackendInfo PlaybackBackend;
92 static struct BackendInfo CaptureBackend;
94 ///////////////////////////////////////////////////////
95 // STRING and EXTENSIONS
97 typedef struct ALCfunction {
98 const ALCchar *funcName;
99 ALCvoid *address;
100 } ALCfunction;
102 typedef struct ALCenums {
103 const ALCchar *enumName;
104 ALCenum value;
105 } ALCenums;
108 static const ALCfunction alcFunctions[] = {
109 { "alcCreateContext", (ALCvoid *) alcCreateContext },
110 { "alcMakeContextCurrent", (ALCvoid *) alcMakeContextCurrent },
111 { "alcProcessContext", (ALCvoid *) alcProcessContext },
112 { "alcSuspendContext", (ALCvoid *) alcSuspendContext },
113 { "alcDestroyContext", (ALCvoid *) alcDestroyContext },
114 { "alcGetCurrentContext", (ALCvoid *) alcGetCurrentContext },
115 { "alcGetContextsDevice", (ALCvoid *) alcGetContextsDevice },
116 { "alcOpenDevice", (ALCvoid *) alcOpenDevice },
117 { "alcCloseDevice", (ALCvoid *) alcCloseDevice },
118 { "alcGetError", (ALCvoid *) alcGetError },
119 { "alcIsExtensionPresent", (ALCvoid *) alcIsExtensionPresent },
120 { "alcGetProcAddress", (ALCvoid *) alcGetProcAddress },
121 { "alcGetEnumValue", (ALCvoid *) alcGetEnumValue },
122 { "alcGetString", (ALCvoid *) alcGetString },
123 { "alcGetIntegerv", (ALCvoid *) alcGetIntegerv },
124 { "alcCaptureOpenDevice", (ALCvoid *) alcCaptureOpenDevice },
125 { "alcCaptureCloseDevice", (ALCvoid *) alcCaptureCloseDevice },
126 { "alcCaptureStart", (ALCvoid *) alcCaptureStart },
127 { "alcCaptureStop", (ALCvoid *) alcCaptureStop },
128 { "alcCaptureSamples", (ALCvoid *) alcCaptureSamples },
130 { "alcSetThreadContext", (ALCvoid *) alcSetThreadContext },
131 { "alcGetThreadContext", (ALCvoid *) alcGetThreadContext },
133 { "alcLoopbackOpenDeviceSOFT", (ALCvoid *) alcLoopbackOpenDeviceSOFT},
134 { "alcIsRenderFormatSupportedSOFT",(ALCvoid *) alcIsRenderFormatSupportedSOFT},
135 { "alcRenderSamplesSOFT", (ALCvoid *) alcRenderSamplesSOFT },
137 { "alEnable", (ALCvoid *) alEnable },
138 { "alDisable", (ALCvoid *) alDisable },
139 { "alIsEnabled", (ALCvoid *) alIsEnabled },
140 { "alGetString", (ALCvoid *) alGetString },
141 { "alGetBooleanv", (ALCvoid *) alGetBooleanv },
142 { "alGetIntegerv", (ALCvoid *) alGetIntegerv },
143 { "alGetFloatv", (ALCvoid *) alGetFloatv },
144 { "alGetDoublev", (ALCvoid *) alGetDoublev },
145 { "alGetBoolean", (ALCvoid *) alGetBoolean },
146 { "alGetInteger", (ALCvoid *) alGetInteger },
147 { "alGetFloat", (ALCvoid *) alGetFloat },
148 { "alGetDouble", (ALCvoid *) alGetDouble },
149 { "alGetError", (ALCvoid *) alGetError },
150 { "alIsExtensionPresent", (ALCvoid *) alIsExtensionPresent },
151 { "alGetProcAddress", (ALCvoid *) alGetProcAddress },
152 { "alGetEnumValue", (ALCvoid *) alGetEnumValue },
153 { "alListenerf", (ALCvoid *) alListenerf },
154 { "alListener3f", (ALCvoid *) alListener3f },
155 { "alListenerfv", (ALCvoid *) alListenerfv },
156 { "alListeneri", (ALCvoid *) alListeneri },
157 { "alListener3i", (ALCvoid *) alListener3i },
158 { "alListeneriv", (ALCvoid *) alListeneriv },
159 { "alGetListenerf", (ALCvoid *) alGetListenerf },
160 { "alGetListener3f", (ALCvoid *) alGetListener3f },
161 { "alGetListenerfv", (ALCvoid *) alGetListenerfv },
162 { "alGetListeneri", (ALCvoid *) alGetListeneri },
163 { "alGetListener3i", (ALCvoid *) alGetListener3i },
164 { "alGetListeneriv", (ALCvoid *) alGetListeneriv },
165 { "alGenSources", (ALCvoid *) alGenSources },
166 { "alDeleteSources", (ALCvoid *) alDeleteSources },
167 { "alIsSource", (ALCvoid *) alIsSource },
168 { "alSourcef", (ALCvoid *) alSourcef },
169 { "alSource3f", (ALCvoid *) alSource3f },
170 { "alSourcefv", (ALCvoid *) alSourcefv },
171 { "alSourcei", (ALCvoid *) alSourcei },
172 { "alSource3i", (ALCvoid *) alSource3i },
173 { "alSourceiv", (ALCvoid *) alSourceiv },
174 { "alGetSourcef", (ALCvoid *) alGetSourcef },
175 { "alGetSource3f", (ALCvoid *) alGetSource3f },
176 { "alGetSourcefv", (ALCvoid *) alGetSourcefv },
177 { "alGetSourcei", (ALCvoid *) alGetSourcei },
178 { "alGetSource3i", (ALCvoid *) alGetSource3i },
179 { "alGetSourceiv", (ALCvoid *) alGetSourceiv },
180 { "alSourcePlayv", (ALCvoid *) alSourcePlayv },
181 { "alSourceStopv", (ALCvoid *) alSourceStopv },
182 { "alSourceRewindv", (ALCvoid *) alSourceRewindv },
183 { "alSourcePausev", (ALCvoid *) alSourcePausev },
184 { "alSourcePlay", (ALCvoid *) alSourcePlay },
185 { "alSourceStop", (ALCvoid *) alSourceStop },
186 { "alSourceRewind", (ALCvoid *) alSourceRewind },
187 { "alSourcePause", (ALCvoid *) alSourcePause },
188 { "alSourceQueueBuffers", (ALCvoid *) alSourceQueueBuffers },
189 { "alSourceUnqueueBuffers", (ALCvoid *) alSourceUnqueueBuffers },
190 { "alGenBuffers", (ALCvoid *) alGenBuffers },
191 { "alDeleteBuffers", (ALCvoid *) alDeleteBuffers },
192 { "alIsBuffer", (ALCvoid *) alIsBuffer },
193 { "alBufferData", (ALCvoid *) alBufferData },
194 { "alBufferf", (ALCvoid *) alBufferf },
195 { "alBuffer3f", (ALCvoid *) alBuffer3f },
196 { "alBufferfv", (ALCvoid *) alBufferfv },
197 { "alBufferi", (ALCvoid *) alBufferi },
198 { "alBuffer3i", (ALCvoid *) alBuffer3i },
199 { "alBufferiv", (ALCvoid *) alBufferiv },
200 { "alGetBufferf", (ALCvoid *) alGetBufferf },
201 { "alGetBuffer3f", (ALCvoid *) alGetBuffer3f },
202 { "alGetBufferfv", (ALCvoid *) alGetBufferfv },
203 { "alGetBufferi", (ALCvoid *) alGetBufferi },
204 { "alGetBuffer3i", (ALCvoid *) alGetBuffer3i },
205 { "alGetBufferiv", (ALCvoid *) alGetBufferiv },
206 { "alDopplerFactor", (ALCvoid *) alDopplerFactor },
207 { "alDopplerVelocity", (ALCvoid *) alDopplerVelocity },
208 { "alSpeedOfSound", (ALCvoid *) alSpeedOfSound },
209 { "alDistanceModel", (ALCvoid *) alDistanceModel },
211 { "alGenFilters", (ALCvoid *) alGenFilters },
212 { "alDeleteFilters", (ALCvoid *) alDeleteFilters },
213 { "alIsFilter", (ALCvoid *) alIsFilter },
214 { "alFilteri", (ALCvoid *) alFilteri },
215 { "alFilteriv", (ALCvoid *) alFilteriv },
216 { "alFilterf", (ALCvoid *) alFilterf },
217 { "alFilterfv", (ALCvoid *) alFilterfv },
218 { "alGetFilteri", (ALCvoid *) alGetFilteri },
219 { "alGetFilteriv", (ALCvoid *) alGetFilteriv },
220 { "alGetFilterf", (ALCvoid *) alGetFilterf },
221 { "alGetFilterfv", (ALCvoid *) alGetFilterfv },
222 { "alGenEffects", (ALCvoid *) alGenEffects },
223 { "alDeleteEffects", (ALCvoid *) alDeleteEffects },
224 { "alIsEffect", (ALCvoid *) alIsEffect },
225 { "alEffecti", (ALCvoid *) alEffecti },
226 { "alEffectiv", (ALCvoid *) alEffectiv },
227 { "alEffectf", (ALCvoid *) alEffectf },
228 { "alEffectfv", (ALCvoid *) alEffectfv },
229 { "alGetEffecti", (ALCvoid *) alGetEffecti },
230 { "alGetEffectiv", (ALCvoid *) alGetEffectiv },
231 { "alGetEffectf", (ALCvoid *) alGetEffectf },
232 { "alGetEffectfv", (ALCvoid *) alGetEffectfv },
233 { "alGenAuxiliaryEffectSlots", (ALCvoid *) alGenAuxiliaryEffectSlots},
234 { "alDeleteAuxiliaryEffectSlots",(ALCvoid *) alDeleteAuxiliaryEffectSlots},
235 { "alIsAuxiliaryEffectSlot", (ALCvoid *) alIsAuxiliaryEffectSlot },
236 { "alAuxiliaryEffectSloti", (ALCvoid *) alAuxiliaryEffectSloti },
237 { "alAuxiliaryEffectSlotiv", (ALCvoid *) alAuxiliaryEffectSlotiv },
238 { "alAuxiliaryEffectSlotf", (ALCvoid *) alAuxiliaryEffectSlotf },
239 { "alAuxiliaryEffectSlotfv", (ALCvoid *) alAuxiliaryEffectSlotfv },
240 { "alGetAuxiliaryEffectSloti", (ALCvoid *) alGetAuxiliaryEffectSloti},
241 { "alGetAuxiliaryEffectSlotiv", (ALCvoid *) alGetAuxiliaryEffectSlotiv},
242 { "alGetAuxiliaryEffectSlotf", (ALCvoid *) alGetAuxiliaryEffectSlotf},
243 { "alGetAuxiliaryEffectSlotfv", (ALCvoid *) alGetAuxiliaryEffectSlotfv},
245 { "alBufferSubDataSOFT", (ALCvoid *) alBufferSubDataSOFT },
247 { "alBufferSamplesSOFT", (ALCvoid *) alBufferSamplesSOFT },
248 { "alBufferSubSamplesSOFT", (ALCvoid *) alBufferSubSamplesSOFT },
249 { "alGetBufferSamplesSOFT", (ALCvoid *) alGetBufferSamplesSOFT },
250 { "alIsBufferFormatSupportedSOFT",(ALCvoid *) alIsBufferFormatSupportedSOFT},
252 { "alDeferUpdatesSOFT", (ALCvoid *) alDeferUpdatesSOFT },
253 { "alProcessUpdatesSOFT", (ALCvoid *) alProcessUpdatesSOFT },
255 { NULL, (ALCvoid *) NULL }
258 static const ALCenums enumeration[] = {
259 // Types
260 { "ALC_INVALID", ALC_INVALID },
261 { "ALC_FALSE", ALC_FALSE },
262 { "ALC_TRUE", ALC_TRUE },
264 // ALC Properties
265 { "ALC_MAJOR_VERSION", ALC_MAJOR_VERSION },
266 { "ALC_MINOR_VERSION", ALC_MINOR_VERSION },
267 { "ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE },
268 { "ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES },
269 { "ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER },
270 { "ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER },
271 { "ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER },
272 { "ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER },
273 { "ALC_EXTENSIONS", ALC_EXTENSIONS },
274 { "ALC_FREQUENCY", ALC_FREQUENCY },
275 { "ALC_REFRESH", ALC_REFRESH },
276 { "ALC_SYNC", ALC_SYNC },
277 { "ALC_MONO_SOURCES", ALC_MONO_SOURCES },
278 { "ALC_STEREO_SOURCES", ALC_STEREO_SOURCES },
279 { "ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER },
280 { "ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER},
281 { "ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES },
282 { "ALC_CONNECTED", ALC_CONNECTED },
284 // EFX Properties
285 { "ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION },
286 { "ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION },
287 { "ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS },
289 // Loopback device Properties
290 { "ALC_FORMAT_CHANNELS_SOFT", ALC_FORMAT_CHANNELS_SOFT },
291 { "ALC_FORMAT_TYPE_SOFT", ALC_FORMAT_TYPE_SOFT },
293 // Buffer Channel Configurations
294 { "ALC_MONO_SOFT", ALC_MONO_SOFT },
295 { "ALC_STEREO_SOFT", ALC_STEREO_SOFT },
296 { "ALC_QUAD_SOFT", ALC_QUAD_SOFT },
297 { "ALC_5POINT1_SOFT", ALC_5POINT1_SOFT },
298 { "ALC_6POINT1_SOFT", ALC_6POINT1_SOFT },
299 { "ALC_7POINT1_SOFT", ALC_7POINT1_SOFT },
301 // Buffer Sample Types
302 { "ALC_BYTE_SOFT", ALC_BYTE_SOFT },
303 { "ALC_UNSIGNED_BYTE_SOFT", ALC_UNSIGNED_BYTE_SOFT },
304 { "ALC_SHORT_SOFT", ALC_SHORT_SOFT },
305 { "ALC_UNSIGNED_SHORT_SOFT", ALC_UNSIGNED_SHORT_SOFT },
306 { "ALC_INT_SOFT", ALC_INT_SOFT },
307 { "ALC_UNSIGNED_INT_SOFT", ALC_UNSIGNED_INT_SOFT },
308 { "ALC_FLOAT_SOFT", ALC_FLOAT_SOFT },
310 // ALC Error Message
311 { "ALC_NO_ERROR", ALC_NO_ERROR },
312 { "ALC_INVALID_DEVICE", ALC_INVALID_DEVICE },
313 { "ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT },
314 { "ALC_INVALID_ENUM", ALC_INVALID_ENUM },
315 { "ALC_INVALID_VALUE", ALC_INVALID_VALUE },
316 { "ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY },
318 { NULL, (ALCenum)0 }
320 // Error strings
321 static const ALCchar alcNoError[] = "No Error";
322 static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
323 static const ALCchar alcErrInvalidContext[] = "Invalid Context";
324 static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
325 static const ALCchar alcErrInvalidValue[] = "Invalid Value";
326 static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
328 /* Device lists. Sizes only include the first ending null character, not the
329 * second */
330 static const ALCchar alcDefaultName[] = "OpenAL Soft\0";
331 static ALCchar *alcAllDeviceList;
332 static size_t alcAllDeviceListSize;
333 static ALCchar *alcCaptureDeviceList;
334 static size_t alcCaptureDeviceListSize;
335 /* Default is always the first in the list */
336 static ALCchar *alcDefaultAllDeviceSpecifier;
337 static ALCchar *alcCaptureDefaultDeviceSpecifier;
340 static const ALCchar alcNoDeviceExtList[] =
341 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
342 "ALC_EXT_thread_local_context ALC_SOFT_loopback";
343 static const ALCchar alcExtensionList[] =
344 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
345 "ALC_EXT_DEDICATED ALC_EXT_disconnect ALC_EXT_EFX "
346 "ALC_EXT_thread_local_context ALC_SOFT_loopback";
347 static const ALCint alcMajorVersion = 1;
348 static const ALCint alcMinorVersion = 1;
350 static const ALCint alcEFXMajorVersion = 1;
351 static const ALCint alcEFXMinorVersion = 0;
353 ///////////////////////////////////////////////////////
356 ///////////////////////////////////////////////////////
357 // Global Variables
359 static CRITICAL_SECTION ListLock;
361 /* Device List */
362 static ALCdevice *volatile DeviceList = NULL;
364 // Thread-local current context
365 static pthread_key_t LocalContext;
366 // Process-wide current context
367 static ALCcontext *volatile GlobalContext = NULL;
369 /* Device Error */
370 static volatile ALCenum LastNullDeviceError = ALC_NO_ERROR;
372 // Default context extensions
373 static const ALchar alExtList[] =
374 "AL_EXT_ALAW AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 "
375 "AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_MULAW "
376 "AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET AL_EXT_source_distance_model "
377 "AL_LOKI_quadriphonic AL_SOFT_buffer_samples AL_SOFT_buffer_sub_data "
378 "AL_SOFTX_deferred_updates AL_SOFT_direct_channels AL_SOFT_loop_points";
380 // Mixing Priority Level
381 ALint RTPrioLevel;
383 // Output Log File
384 FILE *LogFile;
386 // Output Log Level
387 #ifdef _DEBUG
388 enum LogLevel LogLevel = LogWarning;
389 #else
390 enum LogLevel LogLevel = LogError;
391 #endif
393 /* Flag to trap ALC device errors */
394 static ALCboolean TrapALCError = ALC_FALSE;
396 /* One-time configuration init control */
397 static pthread_once_t alc_config_once = PTHREAD_ONCE_INIT;
399 /* Forced effect that applies to sources that don't have an effect on send 0 */
400 static ALeffect DefaultEffect;
402 ///////////////////////////////////////////////////////
405 ///////////////////////////////////////////////////////
406 // ALC Related helper functions
407 static void ReleaseALC(void);
408 static void ReleaseThreadCtx(void *ptr);
410 static void alc_initconfig(void);
411 #define DO_INITCONFIG() pthread_once(&alc_config_once, alc_initconfig)
413 #if defined(_WIN32)
414 static void alc_init(void);
415 static void alc_deinit(void);
416 static void alc_deinit_safe(void);
418 UIntMap TlsDestructor;
420 #ifndef AL_LIBTYPE_STATIC
421 BOOL APIENTRY DllMain(HINSTANCE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
423 ALsizei i;
425 // Perform actions based on the reason for calling.
426 switch(ul_reason_for_call)
428 case DLL_PROCESS_ATTACH:
429 /* Pin the DLL so we won't get unloaded until the process terminates */
430 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
431 (WCHAR*)hModule, &hModule);
432 InitUIntMap(&TlsDestructor, ~0);
433 alc_init();
434 break;
436 case DLL_THREAD_DETACH:
437 LockUIntMapRead(&TlsDestructor);
438 for(i = 0;i < TlsDestructor.size;i++)
440 void *ptr = pthread_getspecific(TlsDestructor.array[i].key);
441 void (*callback)(void*) = (void(*)(void*))TlsDestructor.array[i].value;
442 if(ptr && callback)
443 callback(ptr);
445 UnlockUIntMapRead(&TlsDestructor);
446 break;
448 case DLL_PROCESS_DETACH:
449 if(!lpReserved)
450 alc_deinit();
451 else
452 alc_deinit_safe();
453 ResetUIntMap(&TlsDestructor);
454 break;
456 return TRUE;
458 #elif defined(_MSC_VER)
459 #pragma section(".CRT$XCU",read)
460 static void alc_constructor(void);
461 static void alc_destructor(void);
462 __declspec(allocate(".CRT$XCU")) void (__cdecl* alc_constructor_)(void) = alc_constructor;
464 static void alc_constructor(void)
466 atexit(alc_destructor);
467 alc_init();
470 static void alc_destructor(void)
472 alc_deinit();
474 #elif defined(HAVE_GCC_DESTRUCTOR)
475 static void alc_init(void) __attribute__((constructor));
476 static void alc_deinit(void) __attribute__((destructor));
477 #else
478 #error "No static initialization available on this platform!"
479 #endif
480 #elif defined(HAVE_GCC_DESTRUCTOR)
481 static void alc_init(void) __attribute__((constructor));
482 static void alc_deinit(void) __attribute__((destructor));
483 #else
484 #error "No global initialization available on this platform!"
485 #endif
487 static void alc_init(void)
489 const char *str;
491 LogFile = stderr;
493 str = getenv("__ALSOFT_HALF_ANGLE_CONES");
494 if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
495 ConeScale = 1.0f;
497 str = getenv("__ALSOFT_REVERSE_Z");
498 if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
499 ZScale = -1.0f;
501 pthread_key_create(&LocalContext, ReleaseThreadCtx);
502 InitializeCriticalSection(&ListLock);
503 ThunkInit();
506 static void alc_deinit_safe(void)
508 ReleaseALC();
510 FreeHrtf();
511 FreeALConfig();
513 ThunkExit();
514 DeleteCriticalSection(&ListLock);
515 pthread_key_delete(LocalContext);
517 if(LogFile != stderr)
518 fclose(LogFile);
519 LogFile = NULL;
522 static void alc_deinit(void)
524 int i;
526 ReleaseALC();
528 memset(&PlaybackBackend, 0, sizeof(PlaybackBackend));
529 memset(&CaptureBackend, 0, sizeof(CaptureBackend));
531 for(i = 0;BackendList[i].Deinit;i++)
532 BackendList[i].Deinit();
533 BackendLoopback.Deinit();
535 alc_deinit_safe();
538 static void alc_initconfig(void)
540 const char *devs, *str;
541 float valf;
542 int i, n;
544 str = getenv("ALSOFT_LOGLEVEL");
545 if(str)
547 long lvl = strtol(str, NULL, 0);
548 if(lvl >= NoLog && lvl <= LogRef)
549 LogLevel = lvl;
552 str = getenv("ALSOFT_LOGFILE");
553 if(str && str[0])
555 FILE *logfile = fopen(str, "wat");
556 if(logfile) LogFile = logfile;
557 else ERR("Failed to open log file '%s'\n", str);
560 ReadALConfig();
562 InitHrtf();
564 #ifdef _WIN32
565 RTPrioLevel = 1;
566 #else
567 RTPrioLevel = 0;
568 #endif
569 ConfigValueInt(NULL, "rt-prio", &RTPrioLevel);
571 if(ConfigValueStr(NULL, "resampler", &str))
573 if(strcasecmp(str, "point") == 0 || strcasecmp(str, "none") == 0)
574 DefaultResampler = PointResampler;
575 else if(strcasecmp(str, "linear") == 0)
576 DefaultResampler = LinearResampler;
577 else if(strcasecmp(str, "cubic") == 0)
578 DefaultResampler = CubicResampler;
579 else
581 char *end;
583 n = strtol(str, &end, 0);
584 if(*end == '\0' && (n == PointResampler || n == LinearResampler || n == CubicResampler))
585 DefaultResampler = n;
586 else
587 WARN("Invalid resampler: %s\n", str);
591 str = getenv("ALSOFT_TRAP_ERROR");
592 if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
594 TrapALError = AL_TRUE;
595 TrapALCError = AL_TRUE;
597 else
599 str = getenv("ALSOFT_TRAP_AL_ERROR");
600 if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
601 TrapALError = AL_TRUE;
602 TrapALError = GetConfigValueBool(NULL, "trap-al-error", TrapALError);
604 str = getenv("ALSOFT_TRAP_ALC_ERROR");
605 if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
606 TrapALCError = ALC_TRUE;
607 TrapALCError = GetConfigValueBool(NULL, "trap-alc-error", TrapALCError);
610 if(ConfigValueFloat("reverb", "boost", &valf))
611 ReverbBoost *= aluPow(10.0f, valf / 20.0f);
613 EmulateEAXReverb = GetConfigValueBool("reverb", "emulate-eax", AL_FALSE);
615 if(((devs=getenv("ALSOFT_DRIVERS")) && devs[0]) ||
616 ConfigValueStr(NULL, "drivers", &devs))
618 int n;
619 size_t len;
620 const char *next = devs;
621 int endlist, delitem;
623 i = 0;
624 do {
625 devs = next;
626 next = strchr(devs, ',');
628 delitem = (devs[0] == '-');
629 if(devs[0] == '-') devs++;
631 if(!devs[0] || devs[0] == ',')
633 endlist = 0;
634 continue;
636 endlist = 1;
638 len = (next ? ((size_t)(next-devs)) : strlen(devs));
639 for(n = i;BackendList[n].Init;n++)
641 if(len == strlen(BackendList[n].name) &&
642 strncmp(BackendList[n].name, devs, len) == 0)
644 if(delitem)
646 do {
647 BackendList[n] = BackendList[n+1];
648 ++n;
649 } while(BackendList[n].Init);
651 else
653 struct BackendInfo Bkp = BackendList[n];
654 while(n > i)
656 BackendList[n] = BackendList[n-1];
657 --n;
659 BackendList[n] = Bkp;
661 i++;
663 break;
666 } while(next++);
668 if(endlist)
670 BackendList[i].name = NULL;
671 BackendList[i].Init = NULL;
672 BackendList[i].Deinit = NULL;
673 BackendList[i].Probe = NULL;
677 for(i = 0;BackendList[i].Init && (!PlaybackBackend.name || !CaptureBackend.name);i++)
679 if(!BackendList[i].Init(&BackendList[i].Funcs))
681 WARN("Failed to initialize backend \"%s\"\n", BackendList[i].name);
682 continue;
685 TRACE("Initialized backend \"%s\"\n", BackendList[i].name);
686 if(BackendList[i].Funcs.OpenPlayback && !PlaybackBackend.name)
688 PlaybackBackend = BackendList[i];
689 TRACE("Added \"%s\" for playback\n", PlaybackBackend.name);
691 if(BackendList[i].Funcs.OpenCapture && !CaptureBackend.name)
693 CaptureBackend = BackendList[i];
694 TRACE("Added \"%s\" for capture\n", CaptureBackend.name);
697 BackendLoopback.Init(&BackendLoopback.Funcs);
699 if(ConfigValueStr(NULL, "excludefx", &str))
701 size_t len;
702 const char *next = str;
704 do {
705 str = next;
706 next = strchr(str, ',');
708 if(!str[0] || next == str)
709 continue;
711 len = (next ? ((size_t)(next-str)) : strlen(str));
712 for(n = 0;EffectList[n].name;n++)
714 if(len == strlen(EffectList[n].name) &&
715 strncmp(EffectList[n].name, str, len) == 0)
716 DisabledEffects[EffectList[n].type] = AL_TRUE;
718 } while(next++);
721 InitEffect(&DefaultEffect);
722 str = getenv("ALSOFT_DEFAULT_REVERB");
723 if((str && str[0]) || ConfigValueStr(NULL, "default-reverb", &str))
724 LoadReverbPreset(str, &DefaultEffect);
728 static void LockLists(void)
730 EnterCriticalSection(&ListLock);
733 static void UnlockLists(void)
735 LeaveCriticalSection(&ListLock);
739 static void ProbeList(ALCchar **list, size_t *listsize, enum DevProbe type)
741 DO_INITCONFIG();
743 LockLists();
744 free(*list);
745 *list = NULL;
746 *listsize = 0;
748 if(type == ALL_DEVICE_PROBE && PlaybackBackend.Probe)
749 PlaybackBackend.Probe(type);
750 else if(type == CAPTURE_DEVICE_PROBE && CaptureBackend.Probe)
751 CaptureBackend.Probe(type);
752 UnlockLists();
755 static void ProbeAllDeviceList(void)
756 { ProbeList(&alcAllDeviceList, &alcAllDeviceListSize, ALL_DEVICE_PROBE); }
757 static void ProbeCaptureDeviceList(void)
758 { ProbeList(&alcCaptureDeviceList, &alcCaptureDeviceListSize, CAPTURE_DEVICE_PROBE); }
761 static void AppendList(const ALCchar *name, ALCchar **List, size_t *ListSize)
763 size_t len = strlen(name);
764 void *temp;
766 if(len == 0)
767 return;
769 temp = realloc(*List, (*ListSize) + len + 2);
770 if(!temp)
772 ERR("Realloc failed to add %s!\n", name);
773 return;
775 *List = temp;
777 memcpy((*List)+(*ListSize), name, len+1);
778 *ListSize += len+1;
779 (*List)[*ListSize] = 0;
782 #define DECL_APPEND_LIST_FUNC(type) \
783 void Append##type##List(const ALCchar *name) \
784 { AppendList(name, &alc##type##List, &alc##type##ListSize); }
786 DECL_APPEND_LIST_FUNC(AllDevice)
787 DECL_APPEND_LIST_FUNC(CaptureDevice)
789 #undef DECL_APPEND_LIST_FUNC
792 /* Sets the default channel order used by most non-WaveFormatEx-based APIs */
793 void SetDefaultChannelOrder(ALCdevice *device)
795 switch(device->FmtChans)
797 case DevFmtX51: device->DevChannels[0] = FRONT_LEFT;
798 device->DevChannels[1] = FRONT_RIGHT;
799 device->DevChannels[2] = BACK_LEFT;
800 device->DevChannels[3] = BACK_RIGHT;
801 device->DevChannels[4] = FRONT_CENTER;
802 device->DevChannels[5] = LFE;
803 return;
805 case DevFmtX71: device->DevChannels[0] = FRONT_LEFT;
806 device->DevChannels[1] = FRONT_RIGHT;
807 device->DevChannels[2] = BACK_LEFT;
808 device->DevChannels[3] = BACK_RIGHT;
809 device->DevChannels[4] = FRONT_CENTER;
810 device->DevChannels[5] = LFE;
811 device->DevChannels[6] = SIDE_LEFT;
812 device->DevChannels[7] = SIDE_RIGHT;
813 return;
815 /* Same as WFX order */
816 case DevFmtMono:
817 case DevFmtStereo:
818 case DevFmtQuad:
819 case DevFmtX51Side:
820 case DevFmtX61:
821 break;
823 SetDefaultWFXChannelOrder(device);
825 /* Sets the default order used by WaveFormatEx */
826 void SetDefaultWFXChannelOrder(ALCdevice *device)
828 switch(device->FmtChans)
830 case DevFmtMono: device->DevChannels[0] = FRONT_CENTER; break;
832 case DevFmtStereo: device->DevChannels[0] = FRONT_LEFT;
833 device->DevChannels[1] = FRONT_RIGHT; break;
835 case DevFmtQuad: device->DevChannels[0] = FRONT_LEFT;
836 device->DevChannels[1] = FRONT_RIGHT;
837 device->DevChannels[2] = BACK_LEFT;
838 device->DevChannels[3] = BACK_RIGHT; break;
840 case DevFmtX51: device->DevChannels[0] = FRONT_LEFT;
841 device->DevChannels[1] = FRONT_RIGHT;
842 device->DevChannels[2] = FRONT_CENTER;
843 device->DevChannels[3] = LFE;
844 device->DevChannels[4] = BACK_LEFT;
845 device->DevChannels[5] = BACK_RIGHT; break;
847 case DevFmtX51Side: device->DevChannels[0] = FRONT_LEFT;
848 device->DevChannels[1] = FRONT_RIGHT;
849 device->DevChannels[2] = FRONT_CENTER;
850 device->DevChannels[3] = LFE;
851 device->DevChannels[4] = SIDE_LEFT;
852 device->DevChannels[5] = SIDE_RIGHT; break;
854 case DevFmtX61: device->DevChannels[0] = FRONT_LEFT;
855 device->DevChannels[1] = FRONT_RIGHT;
856 device->DevChannels[2] = FRONT_CENTER;
857 device->DevChannels[3] = LFE;
858 device->DevChannels[4] = BACK_CENTER;
859 device->DevChannels[5] = SIDE_LEFT;
860 device->DevChannels[6] = SIDE_RIGHT; break;
862 case DevFmtX71: device->DevChannels[0] = FRONT_LEFT;
863 device->DevChannels[1] = FRONT_RIGHT;
864 device->DevChannels[2] = FRONT_CENTER;
865 device->DevChannels[3] = LFE;
866 device->DevChannels[4] = BACK_LEFT;
867 device->DevChannels[5] = BACK_RIGHT;
868 device->DevChannels[6] = SIDE_LEFT;
869 device->DevChannels[7] = SIDE_RIGHT; break;
874 const ALCchar *DevFmtTypeString(enum DevFmtType type)
876 switch(type)
878 case DevFmtByte: return "Signed Byte";
879 case DevFmtUByte: return "Unsigned Byte";
880 case DevFmtShort: return "Signed Short";
881 case DevFmtUShort: return "Unsigned Short";
882 case DevFmtInt: return "Signed Int";
883 case DevFmtUInt: return "Unsigned Int";
884 case DevFmtFloat: return "Float";
886 return "(unknown type)";
888 const ALCchar *DevFmtChannelsString(enum DevFmtChannels chans)
890 switch(chans)
892 case DevFmtMono: return "Mono";
893 case DevFmtStereo: return "Stereo";
894 case DevFmtQuad: return "Quadraphonic";
895 case DevFmtX51: return "5.1 Surround";
896 case DevFmtX51Side: return "5.1 Side";
897 case DevFmtX61: return "6.1 Surround";
898 case DevFmtX71: return "7.1 Surround";
900 return "(unknown channels)";
903 ALuint BytesFromDevFmt(enum DevFmtType type)
905 switch(type)
907 case DevFmtByte: return sizeof(ALbyte);
908 case DevFmtUByte: return sizeof(ALubyte);
909 case DevFmtShort: return sizeof(ALshort);
910 case DevFmtUShort: return sizeof(ALushort);
911 case DevFmtInt: return sizeof(ALint);
912 case DevFmtUInt: return sizeof(ALuint);
913 case DevFmtFloat: return sizeof(ALfloat);
915 return 0;
917 ALuint ChannelsFromDevFmt(enum DevFmtChannels chans)
919 switch(chans)
921 case DevFmtMono: return 1;
922 case DevFmtStereo: return 2;
923 case DevFmtQuad: return 4;
924 case DevFmtX51: return 6;
925 case DevFmtX51Side: return 6;
926 case DevFmtX61: return 7;
927 case DevFmtX71: return 8;
929 return 0;
931 static ALboolean DecomposeDevFormat(ALenum format, enum DevFmtChannels *chans,
932 enum DevFmtType *type)
934 static const struct {
935 ALenum format;
936 enum DevFmtChannels channels;
937 enum DevFmtType type;
938 } list[] = {
939 { AL_FORMAT_MONO8, DevFmtMono, DevFmtUByte },
940 { AL_FORMAT_MONO16, DevFmtMono, DevFmtShort },
941 { AL_FORMAT_MONO_FLOAT32, DevFmtMono, DevFmtFloat },
943 { AL_FORMAT_STEREO8, DevFmtStereo, DevFmtUByte },
944 { AL_FORMAT_STEREO16, DevFmtStereo, DevFmtShort },
945 { AL_FORMAT_STEREO_FLOAT32, DevFmtStereo, DevFmtFloat },
947 { AL_FORMAT_QUAD8, DevFmtQuad, DevFmtUByte },
948 { AL_FORMAT_QUAD16, DevFmtQuad, DevFmtShort },
949 { AL_FORMAT_QUAD32, DevFmtQuad, DevFmtFloat },
951 { AL_FORMAT_51CHN8, DevFmtX51, DevFmtUByte },
952 { AL_FORMAT_51CHN16, DevFmtX51, DevFmtShort },
953 { AL_FORMAT_51CHN32, DevFmtX51, DevFmtFloat },
955 { AL_FORMAT_61CHN8, DevFmtX61, DevFmtUByte },
956 { AL_FORMAT_61CHN16, DevFmtX61, DevFmtShort },
957 { AL_FORMAT_61CHN32, DevFmtX61, DevFmtFloat },
959 { AL_FORMAT_71CHN8, DevFmtX71, DevFmtUByte },
960 { AL_FORMAT_71CHN16, DevFmtX71, DevFmtShort },
961 { AL_FORMAT_71CHN32, DevFmtX71, DevFmtFloat },
963 ALuint i;
965 for(i = 0;i < COUNTOF(list);i++)
967 if(list[i].format == format)
969 *chans = list[i].channels;
970 *type = list[i].type;
971 return AL_TRUE;
975 return AL_FALSE;
978 static ALCboolean IsValidALCType(ALCenum type)
980 switch(type)
982 case ALC_BYTE_SOFT:
983 case ALC_UNSIGNED_BYTE_SOFT:
984 case ALC_SHORT_SOFT:
985 case ALC_UNSIGNED_SHORT_SOFT:
986 case ALC_INT_SOFT:
987 case ALC_UNSIGNED_INT_SOFT:
988 case ALC_FLOAT_SOFT:
989 return ALC_TRUE;
991 return ALC_FALSE;
994 static ALCboolean IsValidALCChannels(ALCenum channels)
996 switch(channels)
998 case ALC_MONO_SOFT:
999 case ALC_STEREO_SOFT:
1000 case ALC_QUAD_SOFT:
1001 case ALC_5POINT1_SOFT:
1002 case ALC_6POINT1_SOFT:
1003 case ALC_7POINT1_SOFT:
1004 return ALC_TRUE;
1006 return ALC_FALSE;
1010 /* alcSetError
1012 * Stores the latest ALC Error
1014 static void alcSetError(ALCdevice *device, ALCenum errorCode)
1016 if(TrapALCError)
1018 #ifdef _WIN32
1019 /* DebugBreak() will cause an exception if there is no debugger */
1020 if(IsDebuggerPresent())
1021 DebugBreak();
1022 #elif defined(SIGTRAP)
1023 raise(SIGTRAP);
1024 #endif
1027 if(device)
1028 device->LastError = errorCode;
1029 else
1030 LastNullDeviceError = errorCode;
1034 /* UpdateDeviceParams
1036 * Updates device parameters according to the attribute list (caller is
1037 * responsible for holding the list lock).
1039 static ALCenum UpdateDeviceParams(ALCdevice *device, const ALCint *attrList)
1041 ALCcontext *context;
1042 enum DevFmtChannels oldChans;
1043 enum DevFmtType oldType;
1044 ALCuint oldFreq;
1045 int oldMode;
1046 ALuint i;
1048 // Check for attributes
1049 if(device->Type == Loopback)
1051 enum {
1052 GotFreq = 1<<0,
1053 GotChans = 1<<1,
1054 GotType = 1<<2,
1055 GotAll = GotFreq|GotChans|GotType
1057 ALCuint freq, numMono, numStereo, numSends;
1058 enum DevFmtChannels schans;
1059 enum DevFmtType stype;
1060 ALCuint attrIdx = 0;
1061 ALCint gotFmt = 0;
1063 if(!attrList)
1065 WARN("Missing attributes for loopback device\n");
1066 return ALC_INVALID_VALUE;
1069 numMono = device->NumMonoSources;
1070 numStereo = device->NumStereoSources;
1071 numSends = device->NumAuxSends;
1072 schans = device->FmtChans;
1073 stype = device->FmtType;
1074 freq = device->Frequency;
1076 while(attrList[attrIdx])
1078 if(attrList[attrIdx] == ALC_FORMAT_CHANNELS_SOFT)
1080 ALCint val = attrList[attrIdx + 1];
1081 if(!IsValidALCChannels(val) || !ChannelsFromDevFmt(val))
1082 return ALC_INVALID_VALUE;
1083 schans = val;
1084 gotFmt |= GotChans;
1087 if(attrList[attrIdx] == ALC_FORMAT_TYPE_SOFT)
1089 ALCint val = attrList[attrIdx + 1];
1090 if(!IsValidALCType(val) || !BytesFromDevFmt(val))
1091 return ALC_INVALID_VALUE;
1092 stype = val;
1093 gotFmt |= GotType;
1096 if(attrList[attrIdx] == ALC_FREQUENCY)
1098 freq = attrList[attrIdx + 1];
1099 if(freq < MIN_OUTPUT_RATE)
1100 return ALC_INVALID_VALUE;
1101 gotFmt |= GotFreq;
1104 if(attrList[attrIdx] == ALC_STEREO_SOURCES)
1106 numStereo = attrList[attrIdx + 1];
1107 if(numStereo > device->MaxNoOfSources)
1108 numStereo = device->MaxNoOfSources;
1110 numMono = device->MaxNoOfSources - numStereo;
1113 if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS)
1114 numSends = attrList[attrIdx + 1];
1116 attrIdx += 2;
1119 if(gotFmt != GotAll)
1121 WARN("Missing format for loopback device\n");
1122 return ALC_INVALID_VALUE;
1125 ConfigValueUInt(NULL, "sends", &numSends);
1126 numSends = minu(MAX_SENDS, numSends);
1128 if((device->Flags&DEVICE_RUNNING))
1129 ALCdevice_StopPlayback(device);
1130 device->Flags &= ~DEVICE_RUNNING;
1132 device->Frequency = freq;
1133 device->FmtChans = schans;
1134 device->FmtType = stype;
1135 device->NumMonoSources = numMono;
1136 device->NumStereoSources = numStereo;
1137 device->NumAuxSends = numSends;
1139 else if(attrList && attrList[0])
1141 ALCuint freq, numMono, numStereo, numSends;
1142 ALCuint attrIdx = 0;
1144 /* If a context is already running on the device, stop playback so the
1145 * device attributes can be updated. */
1146 if((device->Flags&DEVICE_RUNNING))
1147 ALCdevice_StopPlayback(device);
1148 device->Flags &= ~DEVICE_RUNNING;
1150 freq = device->Frequency;
1151 numMono = device->NumMonoSources;
1152 numStereo = device->NumStereoSources;
1153 numSends = device->NumAuxSends;
1155 while(attrList[attrIdx])
1157 if(attrList[attrIdx] == ALC_FREQUENCY)
1159 freq = attrList[attrIdx + 1];
1160 device->Flags |= DEVICE_FREQUENCY_REQUEST;
1163 if(attrList[attrIdx] == ALC_STEREO_SOURCES)
1165 numStereo = attrList[attrIdx + 1];
1166 if(numStereo > device->MaxNoOfSources)
1167 numStereo = device->MaxNoOfSources;
1169 numMono = device->MaxNoOfSources - numStereo;
1172 if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS)
1173 numSends = attrList[attrIdx + 1];
1175 attrIdx += 2;
1178 ConfigValueUInt(NULL, "frequency", &freq);
1179 freq = maxu(freq, MIN_OUTPUT_RATE);
1181 ConfigValueUInt(NULL, "sends", &numSends);
1182 numSends = minu(MAX_SENDS, numSends);
1184 device->UpdateSize = (ALuint64)device->UpdateSize * freq /
1185 device->Frequency;
1187 device->Frequency = freq;
1188 device->NumMonoSources = numMono;
1189 device->NumStereoSources = numStereo;
1190 device->NumAuxSends = numSends;
1193 if((device->Flags&DEVICE_RUNNING))
1194 return ALC_NO_ERROR;
1196 oldFreq = device->Frequency;
1197 oldChans = device->FmtChans;
1198 oldType = device->FmtType;
1200 TRACE("Format pre-setup: %s%s, %s%s, %uhz%s, %u update size x%d\n",
1201 DevFmtChannelsString(device->FmtChans),
1202 (device->Flags&DEVICE_CHANNELS_REQUEST)?" (requested)":"",
1203 DevFmtTypeString(device->FmtType),
1204 (device->Flags&DEVICE_SAMPLE_TYPE_REQUEST)?" (requested)":"",
1205 device->Frequency,
1206 (device->Flags&DEVICE_FREQUENCY_REQUEST)?" (requested)":"",
1207 device->UpdateSize, device->NumUpdates);
1209 if(ALCdevice_ResetPlayback(device) == ALC_FALSE)
1210 return ALC_INVALID_DEVICE;
1212 if(device->FmtChans != oldChans && (device->Flags&DEVICE_CHANNELS_REQUEST))
1214 ERR("Failed to set %s, got %s instead\n", DevFmtChannelsString(oldChans),
1215 DevFmtChannelsString(device->FmtChans));
1216 device->Flags &= ~DEVICE_CHANNELS_REQUEST;
1218 if(device->FmtType != oldType && (device->Flags&DEVICE_SAMPLE_TYPE_REQUEST))
1220 ERR("Failed to set %s, got %s instead\n", DevFmtTypeString(oldType),
1221 DevFmtTypeString(device->FmtType));
1222 device->Flags &= ~DEVICE_SAMPLE_TYPE_REQUEST;
1224 if(device->Frequency != oldFreq && (device->Flags&DEVICE_FREQUENCY_REQUEST))
1226 ERR("Failed to set %uhz, got %uhz instead\n", oldFreq, device->Frequency);
1227 device->Flags &= ~DEVICE_FREQUENCY_REQUEST;
1230 TRACE("Format post-setup: %s, %s, %uhz, %u update size x%d\n",
1231 DevFmtChannelsString(device->FmtChans),
1232 DevFmtTypeString(device->FmtType), device->Frequency,
1233 device->UpdateSize, device->NumUpdates);
1235 aluInitPanning(device);
1237 for(i = 0;i < MAXCHANNELS;i++)
1239 device->ClickRemoval[i] = 0.0f;
1240 device->PendingClicks[i] = 0.0f;
1243 device->Hrtf = NULL;
1244 if(device->Type != Loopback && GetConfigValueBool(NULL, "hrtf", AL_FALSE))
1245 device->Hrtf = GetHrtf(device);
1246 TRACE("HRTF %s\n", device->Hrtf?"enabled":"disabled");
1248 if(!device->Hrtf && device->Bs2bLevel > 0 && device->Bs2bLevel <= 6)
1250 if(!device->Bs2b)
1252 device->Bs2b = calloc(1, sizeof(*device->Bs2b));
1253 bs2b_clear(device->Bs2b);
1255 bs2b_set_srate(device->Bs2b, device->Frequency);
1256 bs2b_set_level(device->Bs2b, device->Bs2bLevel);
1257 TRACE("BS2B level %d\n", device->Bs2bLevel);
1259 else
1261 free(device->Bs2b);
1262 device->Bs2b = NULL;
1263 TRACE("BS2B disabled\n");
1266 device->Flags &= ~DEVICE_DUPLICATE_STEREO;
1267 switch(device->FmtChans)
1269 case DevFmtMono:
1270 case DevFmtStereo:
1271 break;
1272 case DevFmtQuad:
1273 case DevFmtX51:
1274 case DevFmtX51Side:
1275 case DevFmtX61:
1276 case DevFmtX71:
1277 if(GetConfigValueBool(NULL, "stereodup", AL_TRUE))
1278 device->Flags |= DEVICE_DUPLICATE_STEREO;
1279 break;
1281 TRACE("Stereo duplication %s\n", (device->Flags&DEVICE_DUPLICATE_STEREO)?"enabled":"disabled");
1283 oldMode = SetMixerFPUMode();
1284 LockDevice(device);
1285 context = device->ContextList;
1286 while(context)
1288 ALsizei pos;
1290 context->UpdateSources = AL_FALSE;
1291 LockUIntMapRead(&context->EffectSlotMap);
1292 for(pos = 0;pos < context->EffectSlotMap.size;pos++)
1294 ALeffectslot *slot = context->EffectSlotMap.array[pos].value;
1296 if(ALeffectState_DeviceUpdate(slot->EffectState, device) == AL_FALSE)
1298 UnlockUIntMapRead(&context->EffectSlotMap);
1299 UnlockDevice(device);
1300 RestoreFPUMode(oldMode);
1301 return ALC_INVALID_DEVICE;
1303 slot->NeedsUpdate = AL_FALSE;
1304 ALeffectState_Update(slot->EffectState, device, slot);
1306 UnlockUIntMapRead(&context->EffectSlotMap);
1308 LockUIntMapRead(&context->SourceMap);
1309 for(pos = 0;pos < context->SourceMap.size;pos++)
1311 ALsource *source = context->SourceMap.array[pos].value;
1312 ALuint s = device->NumAuxSends;
1313 while(s < MAX_SENDS)
1315 if(source->Send[s].Slot)
1316 DecrementRef(&source->Send[s].Slot->ref);
1317 source->Send[s].Slot = NULL;
1318 source->Send[s].WetGain = 1.0f;
1319 source->Send[s].WetGainHF = 1.0f;
1320 s++;
1322 source->NeedsUpdate = AL_FALSE;
1323 ALsource_Update(source, context);
1325 UnlockUIntMapRead(&context->SourceMap);
1327 context = context->next;
1329 if(device->DefaultSlot)
1331 ALeffectslot *slot = device->DefaultSlot;
1333 if(ALeffectState_DeviceUpdate(slot->EffectState, device) == AL_FALSE)
1335 UnlockDevice(device);
1336 RestoreFPUMode(oldMode);
1337 return ALC_INVALID_DEVICE;
1339 slot->NeedsUpdate = AL_FALSE;
1340 ALeffectState_Update(slot->EffectState, device, slot);
1342 UnlockDevice(device);
1343 RestoreFPUMode(oldMode);
1345 if(ALCdevice_StartPlayback(device) == ALC_FALSE)
1346 return ALC_INVALID_DEVICE;
1347 device->Flags |= DEVICE_RUNNING;
1349 return ALC_NO_ERROR;
1352 /* FreeDevice
1354 * Frees the device structure, and destroys any objects the app failed to
1355 * delete. Called once there's no more references on the device.
1357 static ALCvoid FreeDevice(ALCdevice *device)
1359 TRACE("%p\n", device);
1361 if(device->DefaultSlot)
1363 ALeffectState_Destroy(device->DefaultSlot->EffectState);
1364 device->DefaultSlot->EffectState = NULL;
1367 if(device->BufferMap.size > 0)
1369 WARN("(%p) Deleting %d Buffer(s)\n", device, device->BufferMap.size);
1370 ReleaseALBuffers(device);
1372 ResetUIntMap(&device->BufferMap);
1374 if(device->EffectMap.size > 0)
1376 WARN("(%p) Deleting %d Effect(s)\n", device, device->EffectMap.size);
1377 ReleaseALEffects(device);
1379 ResetUIntMap(&device->EffectMap);
1381 if(device->FilterMap.size > 0)
1383 WARN("(%p) Deleting %d Filter(s)\n", device, device->FilterMap.size);
1384 ReleaseALFilters(device);
1386 ResetUIntMap(&device->FilterMap);
1388 free(device->Bs2b);
1389 device->Bs2b = NULL;
1391 free(device->DeviceName);
1392 device->DeviceName = NULL;
1394 DeleteCriticalSection(&device->Mutex);
1396 free(device);
1400 void ALCdevice_IncRef(ALCdevice *device)
1402 RefCount ref;
1403 ref = IncrementRef(&device->ref);
1404 TRACEREF("%p increasing refcount to %u\n", device, ref);
1407 void ALCdevice_DecRef(ALCdevice *device)
1409 RefCount ref;
1410 ref = DecrementRef(&device->ref);
1411 TRACEREF("%p decreasing refcount to %u\n", device, ref);
1412 if(ref == 0) FreeDevice(device);
1415 /* VerifyDevice
1417 * Checks if the device handle is valid, and increments its ref count if so.
1419 static ALCdevice *VerifyDevice(ALCdevice *device)
1421 ALCdevice *tmpDevice;
1423 if(!device)
1424 return NULL;
1426 LockLists();
1427 tmpDevice = DeviceList;
1428 while(tmpDevice && tmpDevice != device)
1429 tmpDevice = tmpDevice->next;
1431 if(tmpDevice)
1432 ALCdevice_IncRef(tmpDevice);
1433 UnlockLists();
1434 return tmpDevice;
1438 /* InitContext
1440 * Initializes context variables
1442 static ALvoid InitContext(ALCcontext *Context)
1444 ALint i, j;
1446 //Initialise listener
1447 Context->Listener.Gain = 1.0f;
1448 Context->Listener.MetersPerUnit = 1.0f;
1449 Context->Listener.Position[0] = 0.0f;
1450 Context->Listener.Position[1] = 0.0f;
1451 Context->Listener.Position[2] = 0.0f;
1452 Context->Listener.Velocity[0] = 0.0f;
1453 Context->Listener.Velocity[1] = 0.0f;
1454 Context->Listener.Velocity[2] = 0.0f;
1455 Context->Listener.Forward[0] = 0.0f;
1456 Context->Listener.Forward[1] = 0.0f;
1457 Context->Listener.Forward[2] = -1.0f;
1458 Context->Listener.Up[0] = 0.0f;
1459 Context->Listener.Up[1] = 1.0f;
1460 Context->Listener.Up[2] = 0.0f;
1461 for(i = 0;i < 4;i++)
1463 for(j = 0;j < 4;j++)
1464 Context->Listener.Matrix[i][j] = ((i==j) ? 1.0f : 0.0f);
1467 //Validate Context
1468 Context->LastError = AL_NO_ERROR;
1469 Context->UpdateSources = AL_FALSE;
1470 Context->ActiveSourceCount = 0;
1471 InitUIntMap(&Context->SourceMap, Context->Device->MaxNoOfSources);
1472 InitUIntMap(&Context->EffectSlotMap, Context->Device->AuxiliaryEffectSlotMax);
1474 //Set globals
1475 Context->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
1476 Context->SourceDistanceModel = AL_FALSE;
1477 Context->DopplerFactor = 1.0f;
1478 Context->DopplerVelocity = 1.0f;
1479 Context->SpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
1480 Context->DeferUpdates = AL_FALSE;
1482 Context->ExtensionList = alExtList;
1486 /* FreeContext
1488 * Cleans up the context, and destroys any remaining objects the app failed to
1489 * delete. Called once there's no more references on the context.
1491 static ALCvoid FreeContext(ALCcontext *context)
1493 TRACE("%p\n", context);
1495 if(context->SourceMap.size > 0)
1497 ERR("(%p) Deleting %d Source(s)\n", context, context->SourceMap.size);
1498 ReleaseALSources(context);
1500 ResetUIntMap(&context->SourceMap);
1502 if(context->EffectSlotMap.size > 0)
1504 ERR("(%p) Deleting %d AuxiliaryEffectSlot(s)\n", context, context->EffectSlotMap.size);
1505 ReleaseALAuxiliaryEffectSlots(context);
1507 ResetUIntMap(&context->EffectSlotMap);
1509 context->ActiveSourceCount = 0;
1510 free(context->ActiveSources);
1511 context->ActiveSources = NULL;
1512 context->MaxActiveSources = 0;
1514 context->ActiveEffectSlotCount = 0;
1515 free(context->ActiveEffectSlots);
1516 context->ActiveEffectSlots = NULL;
1517 context->MaxActiveEffectSlots = 0;
1519 ALCdevice_DecRef(context->Device);
1520 context->Device = NULL;
1522 //Invalidate context
1523 memset(context, 0, sizeof(ALCcontext));
1524 free(context);
1527 /* ReleaseContext
1529 * Removes the context reference from the given device and removes it from
1530 * being current on the running thread or globally.
1532 static void ReleaseContext(ALCcontext *context, ALCdevice *device)
1534 ALCcontext *volatile*tmp_ctx;
1536 if(pthread_getspecific(LocalContext) == context)
1538 WARN("%p released while current on thread\n", context);
1539 pthread_setspecific(LocalContext, NULL);
1540 ALCcontext_DecRef(context);
1543 if(CompExchangePtr((XchgPtr*)&GlobalContext, context, NULL))
1544 ALCcontext_DecRef(context);
1546 LockDevice(device);
1547 tmp_ctx = &device->ContextList;
1548 while(*tmp_ctx)
1550 if(CompExchangePtr((XchgPtr*)tmp_ctx, context, context->next))
1551 break;
1552 tmp_ctx = &(*tmp_ctx)->next;
1554 UnlockDevice(device);
1556 ALCcontext_DecRef(context);
1559 void ALCcontext_IncRef(ALCcontext *context)
1561 RefCount ref;
1562 ref = IncrementRef(&context->ref);
1563 TRACEREF("%p increasing refcount to %u\n", context, ref);
1566 void ALCcontext_DecRef(ALCcontext *context)
1568 RefCount ref;
1569 ref = DecrementRef(&context->ref);
1570 TRACEREF("%p decreasing refcount to %u\n", context, ref);
1571 if(ref == 0) FreeContext(context);
1574 static void ReleaseThreadCtx(void *ptr)
1576 WARN("%p current for thread being destroyed\n", ptr);
1577 ALCcontext_DecRef(ptr);
1580 /* VerifyContext
1582 * Checks that the given context is valid, and increments its reference count.
1584 static ALCcontext *VerifyContext(ALCcontext *context)
1586 ALCdevice *dev;
1588 LockLists();
1589 dev = DeviceList;
1590 while(dev)
1592 ALCcontext *tmp_ctx = dev->ContextList;
1593 while(tmp_ctx)
1595 if(tmp_ctx == context)
1597 ALCcontext_IncRef(tmp_ctx);
1598 UnlockLists();
1599 return tmp_ctx;
1601 tmp_ctx = tmp_ctx->next;
1603 dev = dev->next;
1605 UnlockLists();
1607 return NULL;
1611 /* GetContextRef
1613 * Returns the currently active context, and adds a reference without locking
1614 * it.
1616 ALCcontext *GetContextRef(void)
1618 ALCcontext *context;
1620 context = pthread_getspecific(LocalContext);
1621 if(context)
1622 ALCcontext_IncRef(context);
1623 else
1625 LockLists();
1626 context = GlobalContext;
1627 if(context)
1628 ALCcontext_IncRef(context);
1629 UnlockLists();
1632 return context;
1635 ///////////////////////////////////////////////////////
1638 ///////////////////////////////////////////////////////
1639 // ALC Functions calls
1642 // This should probably move to another c file but for now ...
1643 ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei samples)
1645 ALCdevice *device = NULL;
1646 ALCenum err;
1648 DO_INITCONFIG();
1650 if(!CaptureBackend.name)
1652 alcSetError(NULL, ALC_INVALID_VALUE);
1653 return NULL;
1656 if(samples <= 0)
1658 alcSetError(NULL, ALC_INVALID_VALUE);
1659 return NULL;
1662 if(deviceName && (!deviceName[0] || strcasecmp(deviceName, alcDefaultName) == 0 || strcasecmp(deviceName, "openal-soft") == 0))
1663 deviceName = NULL;
1665 device = calloc(1, sizeof(ALCdevice));
1666 if(!device)
1668 alcSetError(NULL, ALC_OUT_OF_MEMORY);
1669 return NULL;
1672 //Validate device
1673 device->Funcs = &CaptureBackend.Funcs;
1674 device->ref = 1;
1675 device->Connected = ALC_TRUE;
1676 device->Type = Capture;
1677 InitializeCriticalSection(&device->Mutex);
1679 InitUIntMap(&device->BufferMap, ~0);
1680 InitUIntMap(&device->EffectMap, ~0);
1681 InitUIntMap(&device->FilterMap, ~0);
1683 device->DeviceName = NULL;
1685 device->Flags |= DEVICE_FREQUENCY_REQUEST;
1686 device->Frequency = frequency;
1688 device->Flags |= DEVICE_CHANNELS_REQUEST | DEVICE_SAMPLE_TYPE_REQUEST;
1689 if(DecomposeDevFormat(format, &device->FmtChans, &device->FmtType) == AL_FALSE)
1691 DeleteCriticalSection(&device->Mutex);
1692 free(device);
1693 alcSetError(NULL, ALC_INVALID_ENUM);
1694 return NULL;
1697 device->UpdateSize = samples;
1698 device->NumUpdates = 1;
1700 LockLists();
1701 if((err=ALCdevice_OpenCapture(device, deviceName)) != ALC_NO_ERROR)
1703 UnlockLists();
1704 DeleteCriticalSection(&device->Mutex);
1705 free(device);
1706 alcSetError(NULL, err);
1707 return NULL;
1709 UnlockLists();
1711 do {
1712 device->next = DeviceList;
1713 } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
1715 TRACE("Created device %p\n", device);
1716 return device;
1719 ALC_API ALCboolean ALC_APIENTRY alcCaptureCloseDevice(ALCdevice *Device)
1721 ALCdevice *volatile*list;
1723 LockLists();
1724 list = &DeviceList;
1725 while(*list && *list != Device)
1726 list = &(*list)->next;
1728 if(!*list || (*list)->Type != Capture)
1730 alcSetError(*list, ALC_INVALID_DEVICE);
1731 UnlockLists();
1732 return ALC_FALSE;
1735 *list = (*list)->next;
1736 UnlockLists();
1738 ALCdevice_CloseCapture(Device);
1740 ALCdevice_DecRef(Device);
1742 return ALC_TRUE;
1745 ALC_API void ALC_APIENTRY alcCaptureStart(ALCdevice *device)
1747 LockLists();
1748 if(!(device=VerifyDevice(device)) || device->Type != Capture)
1750 UnlockLists();
1751 alcSetError(device, ALC_INVALID_DEVICE);
1752 if(device) ALCdevice_DecRef(device);
1753 return;
1755 if(device->Connected)
1757 if(!(device->Flags&DEVICE_RUNNING))
1758 ALCdevice_StartCapture(device);
1759 device->Flags |= DEVICE_RUNNING;
1761 UnlockLists();
1763 ALCdevice_DecRef(device);
1766 ALC_API void ALC_APIENTRY alcCaptureStop(ALCdevice *device)
1768 LockLists();
1769 if(!(device=VerifyDevice(device)) || device->Type != Capture)
1771 UnlockLists();
1772 alcSetError(device, ALC_INVALID_DEVICE);
1773 if(device) ALCdevice_DecRef(device);
1774 return;
1776 if((device->Flags&DEVICE_RUNNING))
1777 ALCdevice_StopCapture(device);
1778 device->Flags &= ~DEVICE_RUNNING;
1779 UnlockLists();
1781 ALCdevice_DecRef(device);
1784 ALC_API void ALC_APIENTRY alcCaptureSamples(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
1786 ALCenum err = ALC_INVALID_DEVICE;
1787 LockLists();
1788 if((device=VerifyDevice(device)) != NULL && device->Type == Capture)
1790 err = ALC_INVALID_VALUE;
1791 if(samples >= 0 && ALCdevice_AvailableSamples(device) >= (ALCuint)samples)
1792 err = ALCdevice_CaptureSamples(device, buffer, samples);
1794 UnlockLists();
1795 if(err != ALC_NO_ERROR)
1796 alcSetError(device, err);
1797 if(device) ALCdevice_DecRef(device);
1801 alcGetError
1803 Return last ALC generated error code
1805 ALC_API ALCenum ALC_APIENTRY alcGetError(ALCdevice *device)
1807 ALCenum errorCode;
1809 if(VerifyDevice(device))
1811 errorCode = ExchangeInt(&device->LastError, ALC_NO_ERROR);
1812 ALCdevice_DecRef(device);
1814 else
1815 errorCode = ExchangeInt(&LastNullDeviceError, ALC_NO_ERROR);
1817 return errorCode;
1821 /* alcSuspendContext
1823 * Not functional
1825 ALC_API ALCvoid ALC_APIENTRY alcSuspendContext(ALCcontext *Context)
1827 (void)Context;
1830 /* alcProcessContext
1832 * Not functional
1834 ALC_API ALCvoid ALC_APIENTRY alcProcessContext(ALCcontext *Context)
1836 (void)Context;
1840 /* alcGetString
1842 * Returns information about the device, and error strings
1844 ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *Device, ALCenum param)
1846 const ALCchar *value = NULL;
1848 switch(param)
1850 case ALC_NO_ERROR:
1851 value = alcNoError;
1852 break;
1854 case ALC_INVALID_ENUM:
1855 value = alcErrInvalidEnum;
1856 break;
1858 case ALC_INVALID_VALUE:
1859 value = alcErrInvalidValue;
1860 break;
1862 case ALC_INVALID_DEVICE:
1863 value = alcErrInvalidDevice;
1864 break;
1866 case ALC_INVALID_CONTEXT:
1867 value = alcErrInvalidContext;
1868 break;
1870 case ALC_OUT_OF_MEMORY:
1871 value = alcErrOutOfMemory;
1872 break;
1874 case ALC_DEVICE_SPECIFIER:
1875 value = alcDefaultName;
1876 break;
1878 case ALC_ALL_DEVICES_SPECIFIER:
1879 if(VerifyDevice(Device))
1881 value = Device->DeviceName;
1882 ALCdevice_DecRef(Device);
1884 else
1886 ProbeAllDeviceList();
1887 value = alcAllDeviceList;
1889 break;
1891 case ALC_CAPTURE_DEVICE_SPECIFIER:
1892 if(VerifyDevice(Device))
1894 value = Device->DeviceName;
1895 ALCdevice_DecRef(Device);
1897 else
1899 ProbeCaptureDeviceList();
1900 value = alcCaptureDeviceList;
1902 break;
1904 /* Default devices are always first in the list */
1905 case ALC_DEFAULT_DEVICE_SPECIFIER:
1906 value = alcDefaultName;
1907 break;
1909 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
1910 if(!alcAllDeviceList)
1911 ProbeAllDeviceList();
1913 Device = VerifyDevice(Device);
1915 free(alcDefaultAllDeviceSpecifier);
1916 alcDefaultAllDeviceSpecifier = strdup(alcAllDeviceList ?
1917 alcAllDeviceList : "");
1918 if(!alcDefaultAllDeviceSpecifier)
1919 alcSetError(Device, ALC_OUT_OF_MEMORY);
1921 value = alcDefaultAllDeviceSpecifier;
1922 if(Device) ALCdevice_DecRef(Device);
1923 break;
1925 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
1926 if(!alcCaptureDeviceList)
1927 ProbeCaptureDeviceList();
1929 Device = VerifyDevice(Device);
1931 free(alcCaptureDefaultDeviceSpecifier);
1932 alcCaptureDefaultDeviceSpecifier = strdup(alcCaptureDeviceList ?
1933 alcCaptureDeviceList : "");
1934 if(!alcCaptureDefaultDeviceSpecifier)
1935 alcSetError(Device, ALC_OUT_OF_MEMORY);
1937 value = alcCaptureDefaultDeviceSpecifier;
1938 if(Device) ALCdevice_DecRef(Device);
1939 break;
1941 case ALC_EXTENSIONS:
1942 if(!VerifyDevice(Device))
1943 value = alcNoDeviceExtList;
1944 else
1946 value = alcExtensionList;
1947 ALCdevice_DecRef(Device);
1949 break;
1951 default:
1952 Device = VerifyDevice(Device);
1953 alcSetError(Device, ALC_INVALID_ENUM);
1954 if(Device) ALCdevice_DecRef(Device);
1955 break;
1958 return value;
1962 /* alcGetIntegerv
1964 * Returns information about the Device and the version of Open AL
1966 ALC_API ALCvoid ALC_APIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
1968 device = VerifyDevice(device);
1970 if(size == 0 || data == NULL)
1972 alcSetError(device, ALC_INVALID_VALUE);
1973 if(device) ALCdevice_DecRef(device);
1974 return;
1977 if(!device)
1979 switch(param)
1981 case ALC_MAJOR_VERSION:
1982 *data = alcMajorVersion;
1983 break;
1984 case ALC_MINOR_VERSION:
1985 *data = alcMinorVersion;
1986 break;
1988 case ALC_ATTRIBUTES_SIZE:
1989 case ALC_ALL_ATTRIBUTES:
1990 case ALC_FREQUENCY:
1991 case ALC_REFRESH:
1992 case ALC_SYNC:
1993 case ALC_MONO_SOURCES:
1994 case ALC_STEREO_SOURCES:
1995 case ALC_CAPTURE_SAMPLES:
1996 case ALC_FORMAT_CHANNELS_SOFT:
1997 case ALC_FORMAT_TYPE_SOFT:
1998 alcSetError(NULL, ALC_INVALID_DEVICE);
1999 break;
2001 default:
2002 alcSetError(NULL, ALC_INVALID_ENUM);
2003 break;
2006 else if(device->Type == Capture)
2008 switch(param)
2010 case ALC_CAPTURE_SAMPLES:
2011 LockLists();
2012 /* Re-validate the device since it may have been closed */
2013 ALCdevice_DecRef(device);
2014 if((device=VerifyDevice(device)) != NULL)
2015 *data = ALCdevice_AvailableSamples(device);
2016 else
2017 alcSetError(NULL, ALC_INVALID_DEVICE);
2018 UnlockLists();
2019 break;
2021 case ALC_CONNECTED:
2022 *data = device->Connected;
2023 break;
2025 default:
2026 alcSetError(device, ALC_INVALID_ENUM);
2027 break;
2030 else /* render device */
2032 switch(param)
2034 case ALC_MAJOR_VERSION:
2035 *data = alcMajorVersion;
2036 break;
2038 case ALC_MINOR_VERSION:
2039 *data = alcMinorVersion;
2040 break;
2042 case ALC_EFX_MAJOR_VERSION:
2043 *data = alcEFXMajorVersion;
2044 break;
2046 case ALC_EFX_MINOR_VERSION:
2047 *data = alcEFXMinorVersion;
2048 break;
2050 case ALC_ATTRIBUTES_SIZE:
2051 *data = 13;
2052 break;
2054 case ALC_ALL_ATTRIBUTES:
2055 if(size < 13)
2056 alcSetError(device, ALC_INVALID_VALUE);
2057 else
2059 int i = 0;
2061 data[i++] = ALC_FREQUENCY;
2062 data[i++] = device->Frequency;
2064 if(device->Type != Loopback)
2066 data[i++] = ALC_REFRESH;
2067 data[i++] = device->Frequency / device->UpdateSize;
2069 data[i++] = ALC_SYNC;
2070 data[i++] = ALC_FALSE;
2072 else
2074 data[i++] = ALC_FORMAT_CHANNELS_SOFT;
2075 data[i++] = device->FmtChans;
2077 data[i++] = ALC_FORMAT_TYPE_SOFT;
2078 data[i++] = device->FmtType;
2081 data[i++] = ALC_MONO_SOURCES;
2082 data[i++] = device->NumMonoSources;
2084 data[i++] = ALC_STEREO_SOURCES;
2085 data[i++] = device->NumStereoSources;
2087 data[i++] = ALC_MAX_AUXILIARY_SENDS;
2088 data[i++] = device->NumAuxSends;
2090 data[i++] = 0;
2092 break;
2094 case ALC_FREQUENCY:
2095 *data = device->Frequency;
2096 break;
2098 case ALC_REFRESH:
2099 if(device->Type == Loopback)
2100 alcSetError(device, ALC_INVALID_DEVICE);
2101 else
2102 *data = device->Frequency / device->UpdateSize;
2103 break;
2105 case ALC_SYNC:
2106 if(device->Type == Loopback)
2107 alcSetError(device, ALC_INVALID_DEVICE);
2108 else
2109 *data = ALC_FALSE;
2110 break;
2112 case ALC_FORMAT_CHANNELS_SOFT:
2113 if(device->Type != Loopback)
2114 alcSetError(device, ALC_INVALID_DEVICE);
2115 else
2116 *data = device->FmtChans;
2117 break;
2119 case ALC_FORMAT_TYPE_SOFT:
2120 if(device->Type != Loopback)
2121 alcSetError(device, ALC_INVALID_DEVICE);
2122 else
2123 *data = device->FmtType;
2124 break;
2126 case ALC_MONO_SOURCES:
2127 *data = device->NumMonoSources;
2128 break;
2130 case ALC_STEREO_SOURCES:
2131 *data = device->NumStereoSources;
2132 break;
2134 case ALC_MAX_AUXILIARY_SENDS:
2135 *data = device->NumAuxSends;
2136 break;
2138 case ALC_CONNECTED:
2139 *data = device->Connected;
2140 break;
2142 default:
2143 alcSetError(device, ALC_INVALID_ENUM);
2144 break;
2147 if(device)
2148 ALCdevice_DecRef(device);
2152 /* alcIsExtensionPresent
2154 * Determines if there is support for a particular extension
2156 ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
2158 ALCboolean bResult = ALC_FALSE;
2160 device = VerifyDevice(device);
2162 if(!extName)
2163 alcSetError(device, ALC_INVALID_VALUE);
2164 else
2166 size_t len = strlen(extName);
2167 const char *ptr = (device ? alcExtensionList : alcNoDeviceExtList);
2168 while(ptr && *ptr)
2170 if(strncasecmp(ptr, extName, len) == 0 &&
2171 (ptr[len] == '\0' || isspace(ptr[len])))
2173 bResult = ALC_TRUE;
2174 break;
2176 if((ptr=strchr(ptr, ' ')) != NULL)
2178 do {
2179 ++ptr;
2180 } while(isspace(*ptr));
2184 if(device)
2185 ALCdevice_DecRef(device);
2186 return bResult;
2190 /* alcGetProcAddress
2192 * Retrieves the function address for a particular extension function
2194 ALC_API ALCvoid* ALC_APIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
2196 ALCvoid *ptr = NULL;
2198 device = VerifyDevice(device);
2200 if(!funcName)
2201 alcSetError(device, ALC_INVALID_VALUE);
2202 else
2204 ALsizei i = 0;
2205 while(alcFunctions[i].funcName && strcmp(alcFunctions[i].funcName,funcName) != 0)
2206 i++;
2207 ptr = alcFunctions[i].address;
2209 if(device)
2210 ALCdevice_DecRef(device);
2211 return ptr;
2215 /* alcGetEnumValue
2217 * Get the value for a particular ALC Enumerated Value
2219 ALC_API ALCenum ALC_APIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
2221 ALCenum val = 0;
2223 device = VerifyDevice(device);
2225 if(!enumName)
2226 alcSetError(device, ALC_INVALID_VALUE);
2227 else
2229 ALsizei i = 0;
2230 while(enumeration[i].enumName && strcmp(enumeration[i].enumName,enumName) != 0)
2231 i++;
2232 val = enumeration[i].value;
2234 if(device)
2235 ALCdevice_DecRef(device);
2236 return val;
2240 /* alcCreateContext
2242 * Create and attach a Context to a particular Device.
2244 ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
2246 ALCcontext *ALContext;
2247 ALCenum err;
2249 LockLists();
2250 if(!(device=VerifyDevice(device)) || device->Type == Capture || !device->Connected)
2252 UnlockLists();
2253 alcSetError(device, ALC_INVALID_DEVICE);
2254 if(device) ALCdevice_DecRef(device);
2255 return NULL;
2258 /* Reset Context Last Error code */
2259 device->LastError = ALC_NO_ERROR;
2261 if((err=UpdateDeviceParams(device, attrList)) != ALC_NO_ERROR)
2263 UnlockLists();
2264 alcSetError(device, err);
2265 if(err == ALC_INVALID_DEVICE)
2266 aluHandleDisconnect(device);
2267 ALCdevice_DecRef(device);
2268 return NULL;
2271 ALContext = calloc(1, sizeof(ALCcontext));
2272 if(ALContext)
2274 ALContext->ref = 1;
2276 ALContext->MaxActiveSources = 256;
2277 ALContext->ActiveSources = malloc(sizeof(ALContext->ActiveSources[0]) *
2278 ALContext->MaxActiveSources);
2280 if(!ALContext || !ALContext->ActiveSources)
2282 if(!device->ContextList)
2284 ALCdevice_StopPlayback(device);
2285 device->Flags &= ~DEVICE_RUNNING;
2287 UnlockLists();
2289 free(ALContext);
2290 ALContext = NULL;
2292 alcSetError(device, ALC_OUT_OF_MEMORY);
2293 ALCdevice_DecRef(device);
2294 return NULL;
2297 ALContext->Device = device;
2298 ALCdevice_IncRef(device);
2299 InitContext(ALContext);
2301 do {
2302 ALContext->next = device->ContextList;
2303 } while(!CompExchangePtr((XchgPtr*)&device->ContextList, ALContext->next, ALContext));
2304 UnlockLists();
2306 ALCdevice_DecRef(device);
2308 TRACE("Created context %p\n", ALContext);
2309 return ALContext;
2312 /* alcDestroyContext
2314 * Remove a Context
2316 ALC_API ALCvoid ALC_APIENTRY alcDestroyContext(ALCcontext *context)
2318 ALCdevice *Device;
2320 LockLists();
2321 /* alcGetContextsDevice sets an error for invalid contexts */
2322 Device = alcGetContextsDevice(context);
2323 if(Device)
2325 ReleaseContext(context, Device);
2326 if(!Device->ContextList)
2328 ALCdevice_StopPlayback(Device);
2329 Device->Flags &= ~DEVICE_RUNNING;
2332 UnlockLists();
2336 /* alcGetCurrentContext
2338 * Returns the currently active Context
2340 ALC_API ALCcontext* ALC_APIENTRY alcGetCurrentContext(ALCvoid)
2342 ALCcontext *Context;
2344 Context = pthread_getspecific(LocalContext);
2345 if(!Context) Context = GlobalContext;
2347 return Context;
2350 /* alcGetThreadContext
2352 * Returns the currently active thread-local Context
2354 ALC_API ALCcontext* ALC_APIENTRY alcGetThreadContext(void)
2356 ALCcontext *Context;
2357 Context = pthread_getspecific(LocalContext);
2358 return Context;
2362 /* alcMakeContextCurrent
2364 * Makes the given Context the active Context
2366 ALC_API ALCboolean ALC_APIENTRY alcMakeContextCurrent(ALCcontext *context)
2368 /* context must be a valid Context or NULL */
2369 if(context && !(context=VerifyContext(context)))
2371 alcSetError(NULL, ALC_INVALID_CONTEXT);
2372 return ALC_FALSE;
2374 /* context's reference count is already incremented */
2375 context = ExchangePtr((XchgPtr*)&GlobalContext, context);
2376 if(context) ALCcontext_DecRef(context);
2378 if((context=pthread_getspecific(LocalContext)) != NULL)
2380 pthread_setspecific(LocalContext, NULL);
2381 ALCcontext_DecRef(context);
2384 return ALC_TRUE;
2387 /* alcSetThreadContext
2389 * Makes the given Context the active Context for the current thread
2391 ALC_API ALCboolean ALC_APIENTRY alcSetThreadContext(ALCcontext *context)
2393 ALCcontext *old;
2395 /* context must be a valid Context or NULL */
2396 if(context && !(context=VerifyContext(context)))
2398 alcSetError(NULL, ALC_INVALID_CONTEXT);
2399 return ALC_FALSE;
2401 /* context's reference count is already incremented */
2402 old = pthread_getspecific(LocalContext);
2403 pthread_setspecific(LocalContext, context);
2404 if(old) ALCcontext_DecRef(old);
2406 return ALC_TRUE;
2410 /* alcGetContextsDevice
2412 * Returns the Device that a particular Context is attached to
2414 ALC_API ALCdevice* ALC_APIENTRY alcGetContextsDevice(ALCcontext *Context)
2416 ALCdevice *Device;
2418 if(!(Context=VerifyContext(Context)))
2420 alcSetError(NULL, ALC_INVALID_CONTEXT);
2421 return NULL;
2423 Device = Context->Device;
2424 ALCcontext_DecRef(Context);
2426 return Device;
2430 /* alcOpenDevice
2432 * Open the Device specified.
2434 ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName)
2436 const ALCchar *fmt;
2437 ALCdevice *device;
2438 ALCenum err;
2440 DO_INITCONFIG();
2442 if(!PlaybackBackend.name)
2444 alcSetError(NULL, ALC_INVALID_VALUE);
2445 return NULL;
2448 if(deviceName && (!deviceName[0] || strcasecmp(deviceName, alcDefaultName) == 0 || strcasecmp(deviceName, "openal-soft") == 0))
2449 deviceName = NULL;
2451 device = calloc(1, sizeof(ALCdevice)+sizeof(ALeffectslot));
2452 if(!device)
2454 alcSetError(NULL, ALC_OUT_OF_MEMORY);
2455 return NULL;
2458 //Validate device
2459 device->Funcs = &PlaybackBackend.Funcs;
2460 device->ref = 1;
2461 device->Connected = ALC_TRUE;
2462 device->Type = Playback;
2463 InitializeCriticalSection(&device->Mutex);
2464 device->LastError = ALC_NO_ERROR;
2466 device->Flags = 0;
2467 device->Bs2b = NULL;
2468 device->Bs2bLevel = 0;
2469 device->DeviceName = NULL;
2471 device->ContextList = NULL;
2473 device->MaxNoOfSources = 256;
2474 device->AuxiliaryEffectSlotMax = 4;
2475 device->NumAuxSends = MAX_SENDS;
2477 InitUIntMap(&device->BufferMap, ~0);
2478 InitUIntMap(&device->EffectMap, ~0);
2479 InitUIntMap(&device->FilterMap, ~0);
2481 //Set output format
2482 device->FmtChans = DevFmtChannelsDefault;
2483 device->FmtType = DevFmtTypeDefault;
2484 device->Frequency = DEFAULT_OUTPUT_RATE;
2485 device->NumUpdates = 4;
2486 device->UpdateSize = 1024;
2488 if(ConfigValueStr(NULL, "channels", &fmt))
2490 static const struct {
2491 const char name[16];
2492 enum DevFmtChannels chans;
2493 } chanlist[] = {
2494 { "mono", DevFmtMono },
2495 { "stereo", DevFmtStereo },
2496 { "quad", DevFmtQuad },
2497 { "surround51", DevFmtX51 },
2498 { "surround61", DevFmtX61 },
2499 { "surround71", DevFmtX71 },
2501 size_t i;
2503 for(i = 0;i < COUNTOF(chanlist);i++)
2505 if(strcasecmp(chanlist[i].name, fmt) == 0)
2507 device->FmtChans = chanlist[i].chans;
2508 device->Flags |= DEVICE_CHANNELS_REQUEST;
2509 break;
2512 if(i == COUNTOF(chanlist))
2513 ERR("Unsupported channels: %s\n", fmt);
2515 if(ConfigValueStr(NULL, "sample-type", &fmt))
2517 static const struct {
2518 const char name[16];
2519 enum DevFmtType type;
2520 } typelist[] = {
2521 { "int8", DevFmtByte },
2522 { "uint8", DevFmtUByte },
2523 { "int16", DevFmtShort },
2524 { "uint16", DevFmtUShort },
2525 { "int32", DevFmtInt },
2526 { "uint32", DevFmtUInt },
2527 { "float32", DevFmtFloat },
2529 size_t i;
2531 for(i = 0;i < COUNTOF(typelist);i++)
2533 if(strcasecmp(typelist[i].name, fmt) == 0)
2535 device->FmtType = typelist[i].type;
2536 device->Flags |= DEVICE_SAMPLE_TYPE_REQUEST;
2537 break;
2540 if(i == COUNTOF(typelist))
2541 ERR("Unsupported sample-type: %s\n", fmt);
2543 #define DEVICE_FORMAT_REQUEST (DEVICE_CHANNELS_REQUEST|DEVICE_SAMPLE_TYPE_REQUEST)
2544 if((device->Flags&DEVICE_FORMAT_REQUEST) != DEVICE_FORMAT_REQUEST &&
2545 ConfigValueStr(NULL, "format", &fmt))
2547 static const struct {
2548 const char name[32];
2549 enum DevFmtChannels channels;
2550 enum DevFmtType type;
2551 } formats[] = {
2552 { "AL_FORMAT_MONO32", DevFmtMono, DevFmtFloat },
2553 { "AL_FORMAT_STEREO32", DevFmtStereo, DevFmtFloat },
2554 { "AL_FORMAT_QUAD32", DevFmtQuad, DevFmtFloat },
2555 { "AL_FORMAT_51CHN32", DevFmtX51, DevFmtFloat },
2556 { "AL_FORMAT_61CHN32", DevFmtX61, DevFmtFloat },
2557 { "AL_FORMAT_71CHN32", DevFmtX71, DevFmtFloat },
2559 { "AL_FORMAT_MONO16", DevFmtMono, DevFmtShort },
2560 { "AL_FORMAT_STEREO16", DevFmtStereo, DevFmtShort },
2561 { "AL_FORMAT_QUAD16", DevFmtQuad, DevFmtShort },
2562 { "AL_FORMAT_51CHN16", DevFmtX51, DevFmtShort },
2563 { "AL_FORMAT_61CHN16", DevFmtX61, DevFmtShort },
2564 { "AL_FORMAT_71CHN16", DevFmtX71, DevFmtShort },
2566 { "AL_FORMAT_MONO8", DevFmtMono, DevFmtByte },
2567 { "AL_FORMAT_STEREO8", DevFmtStereo, DevFmtByte },
2568 { "AL_FORMAT_QUAD8", DevFmtQuad, DevFmtByte },
2569 { "AL_FORMAT_51CHN8", DevFmtX51, DevFmtByte },
2570 { "AL_FORMAT_61CHN8", DevFmtX61, DevFmtByte },
2571 { "AL_FORMAT_71CHN8", DevFmtX71, DevFmtByte }
2573 size_t i;
2575 ERR("Option 'format' is deprecated, please use 'channels' and 'sample-type'\n");
2576 for(i = 0;i < COUNTOF(formats);i++)
2578 if(strcasecmp(fmt, formats[i].name) == 0)
2580 if(!(device->Flags&DEVICE_CHANNELS_REQUEST))
2581 device->FmtChans = formats[i].channels;
2582 if(!(device->Flags&DEVICE_SAMPLE_TYPE_REQUEST))
2583 device->FmtType = formats[i].type;
2584 device->Flags |= DEVICE_FORMAT_REQUEST;
2585 break;
2588 if(i == COUNTOF(formats))
2589 ERR("Unsupported format: %s\n", fmt);
2591 #undef DEVICE_FORMAT_REQUEST
2593 if(ConfigValueUInt(NULL, "frequency", &device->Frequency))
2595 device->Flags |= DEVICE_FREQUENCY_REQUEST;
2596 if(device->Frequency < MIN_OUTPUT_RATE)
2597 ERR("%uhz request clamped to %uhz minimum\n", device->Frequency, MIN_OUTPUT_RATE);
2598 device->Frequency = maxu(device->Frequency, MIN_OUTPUT_RATE);
2601 ConfigValueUInt(NULL, "periods", &device->NumUpdates);
2602 device->NumUpdates = clampu(device->NumUpdates, 2, 16);
2604 ConfigValueUInt(NULL, "period_size", &device->UpdateSize);
2605 device->UpdateSize = clampu(device->UpdateSize, 64, 8192);
2607 ConfigValueUInt(NULL, "sources", &device->MaxNoOfSources);
2608 if(device->MaxNoOfSources == 0) device->MaxNoOfSources = 256;
2610 ConfigValueUInt(NULL, "slots", &device->AuxiliaryEffectSlotMax);
2611 if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
2613 ConfigValueUInt(NULL, "sends", &device->NumAuxSends);
2614 if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
2616 ConfigValueInt(NULL, "cf_level", &device->Bs2bLevel);
2618 device->NumStereoSources = 1;
2619 device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
2621 // Find a playback device to open
2622 LockLists();
2623 if((err=ALCdevice_OpenPlayback(device, deviceName)) != ALC_NO_ERROR)
2625 UnlockLists();
2626 DeleteCriticalSection(&device->Mutex);
2627 free(device);
2628 alcSetError(NULL, err);
2629 return NULL;
2631 UnlockLists();
2633 if(DefaultEffect.type != AL_EFFECT_NULL)
2635 device->DefaultSlot = (ALeffectslot*)(device+1);
2636 if(InitEffectSlot(device->DefaultSlot) != AL_NO_ERROR)
2638 device->DefaultSlot = NULL;
2639 ERR("Failed to initialize the default effect slot\n");
2641 else if(InitializeEffect(device, device->DefaultSlot, &DefaultEffect) != AL_NO_ERROR)
2643 ALeffectState_Destroy(device->DefaultSlot->EffectState);
2644 device->DefaultSlot = NULL;
2645 ERR("Failed to initialize the default effect\n");
2649 do {
2650 device->next = DeviceList;
2651 } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
2653 TRACE("Created device %p, \"%s\"\n", device, device->DeviceName);
2654 return device;
2657 /* alcCloseDevice
2659 * Close the specified Device
2661 ALC_API ALCboolean ALC_APIENTRY alcCloseDevice(ALCdevice *Device)
2663 ALCdevice *volatile*list;
2664 ALCcontext *ctx;
2666 LockLists();
2667 list = &DeviceList;
2668 while(*list && *list != Device)
2669 list = &(*list)->next;
2671 if(!*list || (*list)->Type == Capture)
2673 alcSetError(*list, ALC_INVALID_DEVICE);
2674 UnlockLists();
2675 return ALC_FALSE;
2678 *list = (*list)->next;
2679 UnlockLists();
2681 while((ctx=Device->ContextList) != NULL)
2683 WARN("Releasing context %p\n", ctx);
2684 ReleaseContext(ctx, Device);
2686 if((Device->Flags&DEVICE_RUNNING))
2687 ALCdevice_StopPlayback(Device);
2688 Device->Flags &= ~DEVICE_RUNNING;
2690 ALCdevice_ClosePlayback(Device);
2692 ALCdevice_DecRef(Device);
2694 return ALC_TRUE;
2698 /* alcLoopbackOpenDeviceSOFT
2700 * Open a loopback device, for manual rendering.
2702 ALC_API ALCdevice* ALC_APIENTRY alcLoopbackOpenDeviceSOFT(const ALCchar *deviceName)
2704 ALCdevice *device;
2706 DO_INITCONFIG();
2708 /* Make sure the device name, if specified, is us. */
2709 if(deviceName && strcmp(deviceName, alcDefaultName) != 0)
2711 alcSetError(NULL, ALC_INVALID_VALUE);
2712 return NULL;
2715 device = calloc(1, sizeof(ALCdevice));
2716 if(!device)
2718 alcSetError(NULL, ALC_OUT_OF_MEMORY);
2719 return NULL;
2722 //Validate device
2723 device->Funcs = &BackendLoopback.Funcs;
2724 device->ref = 1;
2725 device->Connected = ALC_TRUE;
2726 device->Type = Loopback;
2727 InitializeCriticalSection(&device->Mutex);
2728 device->LastError = ALC_NO_ERROR;
2730 device->Flags = 0;
2731 device->Bs2b = NULL;
2732 device->Bs2bLevel = 0;
2733 device->DeviceName = NULL;
2735 device->ContextList = NULL;
2737 device->MaxNoOfSources = 256;
2738 device->AuxiliaryEffectSlotMax = 4;
2739 device->NumAuxSends = MAX_SENDS;
2741 InitUIntMap(&device->BufferMap, ~0);
2742 InitUIntMap(&device->EffectMap, ~0);
2743 InitUIntMap(&device->FilterMap, ~0);
2745 //Set output format
2746 device->NumUpdates = 0;
2747 device->UpdateSize = 0;
2749 device->Frequency = DEFAULT_OUTPUT_RATE;
2750 device->FmtChans = DevFmtChannelsDefault;
2751 device->FmtType = DevFmtTypeDefault;
2753 ConfigValueUInt(NULL, "sources", &device->MaxNoOfSources);
2754 if(device->MaxNoOfSources == 0) device->MaxNoOfSources = 256;
2756 ConfigValueUInt(NULL, "slots", &device->AuxiliaryEffectSlotMax);
2757 if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
2759 ConfigValueUInt(NULL, "sends", &device->NumAuxSends);
2760 if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
2762 device->NumStereoSources = 1;
2763 device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
2765 // Open the "backend"
2766 ALCdevice_OpenPlayback(device, "Loopback");
2767 do {
2768 device->next = DeviceList;
2769 } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
2771 TRACE("Created device %p\n", device);
2772 return device;
2775 /* alcIsRenderFormatSupportedSOFT
2777 * Determines if the loopback device supports the given format for rendering.
2779 ALC_API ALCboolean ALC_APIENTRY alcIsRenderFormatSupportedSOFT(ALCdevice *device, ALCsizei freq, ALCenum channels, ALCenum type)
2781 ALCboolean ret = ALC_FALSE;
2783 if(!(device=VerifyDevice(device)) || device->Type != Loopback)
2784 alcSetError(device, ALC_INVALID_DEVICE);
2785 else if(freq <= 0)
2786 alcSetError(device, ALC_INVALID_VALUE);
2787 else
2789 if(IsValidALCType(type) && BytesFromDevFmt(type) > 0 &&
2790 IsValidALCChannels(channels) && ChannelsFromDevFmt(channels) > 0 &&
2791 freq >= MIN_OUTPUT_RATE)
2792 ret = ALC_TRUE;
2794 if(device) ALCdevice_DecRef(device);
2796 return ret;
2799 /* alcRenderSamplesSOFT
2801 * Renders some samples into a buffer, using the format last set by the
2802 * attributes given to alcCreateContext.
2804 ALC_API void ALC_APIENTRY alcRenderSamplesSOFT(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
2806 if(!(device=VerifyDevice(device)) || device->Type != Loopback)
2807 alcSetError(device, ALC_INVALID_DEVICE);
2808 else if(samples < 0 || (samples > 0 && buffer == NULL))
2809 alcSetError(device, ALC_INVALID_VALUE);
2810 else
2811 aluMixData(device, buffer, samples);
2812 if(device) ALCdevice_DecRef(device);
2816 static void ReleaseALC(void)
2818 ALCdevice *dev;
2820 free(alcAllDeviceList); alcAllDeviceList = NULL;
2821 alcAllDeviceListSize = 0;
2822 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
2823 alcCaptureDeviceListSize = 0;
2825 free(alcDefaultAllDeviceSpecifier);
2826 alcDefaultAllDeviceSpecifier = NULL;
2827 free(alcCaptureDefaultDeviceSpecifier);
2828 alcCaptureDefaultDeviceSpecifier = NULL;
2830 if((dev=ExchangePtr((XchgPtr*)&DeviceList, NULL)) != NULL)
2832 ALCuint num = 0;
2833 do {
2834 num++;
2835 } while((dev=dev->next) != NULL);
2836 ERR("%u device%s not closed\n", num, (num>1)?"s":"");
2840 ///////////////////////////////////////////////////////