Pass the device to InitializeEffect and return the error enum from it
[openal-soft.git] / Alc / ALc.c
blob21d1ac9ab09849ff99435c2940835e0294ed0bd5
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_SOFTX_loopback_device";
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_SOFTX_loopback_device";
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 *GlobalContext;
369 /* Device Error */
370 static volatile ALCenum g_eLastNullDeviceError = 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 str = getenv("ALSOFT_TRAP_ERROR");
502 if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
504 TrapALError = AL_TRUE;
505 TrapALCError = AL_TRUE;
507 else
509 str = getenv("ALSOFT_TRAP_AL_ERROR");
510 if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
511 TrapALError = AL_TRUE;
513 str = getenv("ALSOFT_TRAP_ALC_ERROR");
514 if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
515 TrapALCError = ALC_TRUE;
518 pthread_key_create(&LocalContext, ReleaseThreadCtx);
519 InitializeCriticalSection(&ListLock);
520 ThunkInit();
523 static void alc_deinit_safe(void)
525 ReleaseALC();
527 FreeHrtf();
528 FreeALConfig();
530 ThunkExit();
531 DeleteCriticalSection(&ListLock);
532 pthread_key_delete(LocalContext);
534 if(LogFile != stderr)
535 fclose(LogFile);
536 LogFile = NULL;
539 static void alc_deinit(void)
541 int i;
543 ReleaseALC();
545 memset(&PlaybackBackend, 0, sizeof(PlaybackBackend));
546 memset(&CaptureBackend, 0, sizeof(CaptureBackend));
548 for(i = 0;BackendList[i].Deinit;i++)
549 BackendList[i].Deinit();
550 BackendLoopback.Deinit();
552 alc_deinit_safe();
555 static void alc_initconfig(void)
557 const char *devs, *str;
558 float valf;
559 int i, n;
561 str = getenv("ALSOFT_LOGLEVEL");
562 if(str)
564 long lvl = strtol(str, NULL, 0);
565 if(lvl >= NoLog && lvl <= LogRef)
566 LogLevel = lvl;
569 str = getenv("ALSOFT_LOGFILE");
570 if(str && str[0])
572 FILE *logfile = fopen(str, "wat");
573 if(logfile) LogFile = logfile;
574 else ERR("Failed to open log file '%s'\n", str);
577 ReadALConfig();
579 InitHrtf();
581 #ifdef _WIN32
582 RTPrioLevel = 1;
583 #else
584 RTPrioLevel = 0;
585 #endif
586 ConfigValueInt(NULL, "rt-prio", &RTPrioLevel);
588 if(ConfigValueStr(NULL, "resampler", &str))
590 if(strcasecmp(str, "point") == 0 || strcasecmp(str, "none") == 0)
591 DefaultResampler = PointResampler;
592 else if(strcasecmp(str, "linear") == 0)
593 DefaultResampler = LinearResampler;
594 else if(strcasecmp(str, "cubic") == 0)
595 DefaultResampler = CubicResampler;
596 else
598 char *end;
600 n = strtol(str, &end, 0);
601 if(*end == '\0' && (n == PointResampler || n == LinearResampler || n == CubicResampler))
602 DefaultResampler = n;
603 else
604 WARN("Invalid resampler: %s\n", str);
608 if(!TrapALCError)
609 TrapALCError = GetConfigValueBool(NULL, "trap-alc-error", ALC_FALSE);
611 if(!TrapALError)
612 TrapALError = GetConfigValueBool(NULL, "trap-al-error", AL_FALSE);
614 if(ConfigValueFloat("reverb", "boost", &valf))
615 ReverbBoost *= aluPow(10.0f, valf / 20.0f);
617 EmulateEAXReverb = GetConfigValueBool("reverb", "emulate-eax", AL_FALSE);
619 if(((devs=getenv("ALSOFT_DRIVERS")) && devs[0]) ||
620 ConfigValueStr(NULL, "drivers", &devs))
622 int n;
623 size_t len;
624 const char *next = devs;
625 int endlist, delitem;
627 i = 0;
628 do {
629 devs = next;
630 next = strchr(devs, ',');
632 delitem = (devs[0] == '-');
633 if(devs[0] == '-') devs++;
635 if(!devs[0] || devs[0] == ',')
637 endlist = 0;
638 continue;
640 endlist = 1;
642 len = (next ? ((size_t)(next-devs)) : strlen(devs));
643 for(n = i;BackendList[n].Init;n++)
645 if(len == strlen(BackendList[n].name) &&
646 strncmp(BackendList[n].name, devs, len) == 0)
648 if(delitem)
650 do {
651 BackendList[n] = BackendList[n+1];
652 ++n;
653 } while(BackendList[n].Init);
655 else
657 struct BackendInfo Bkp = BackendList[n];
658 while(n > i)
660 BackendList[n] = BackendList[n-1];
661 --n;
663 BackendList[n] = Bkp;
665 i++;
667 break;
670 } while(next++);
672 if(endlist)
674 BackendList[i].name = NULL;
675 BackendList[i].Init = NULL;
676 BackendList[i].Deinit = NULL;
677 BackendList[i].Probe = NULL;
681 for(i = 0;BackendList[i].Init && (!PlaybackBackend.name || !CaptureBackend.name);i++)
683 if(!BackendList[i].Init(&BackendList[i].Funcs))
685 WARN("Failed to initialize backend \"%s\"\n", BackendList[i].name);
686 continue;
689 TRACE("Initialized backend \"%s\"\n", BackendList[i].name);
690 if(BackendList[i].Funcs.OpenPlayback && !PlaybackBackend.name)
692 PlaybackBackend = BackendList[i];
693 TRACE("Added \"%s\" for playback\n", PlaybackBackend.name);
695 if(BackendList[i].Funcs.OpenCapture && !CaptureBackend.name)
697 CaptureBackend = BackendList[i];
698 TRACE("Added \"%s\" for capture\n", CaptureBackend.name);
701 BackendLoopback.Init(&BackendLoopback.Funcs);
703 if(ConfigValueStr(NULL, "excludefx", &str))
705 size_t len;
706 const char *next = str;
708 do {
709 str = next;
710 next = strchr(str, ',');
712 if(!str[0] || next == str)
713 continue;
715 len = (next ? ((size_t)(next-str)) : strlen(str));
716 for(n = 0;EffectList[n].name;n++)
718 if(len == strlen(EffectList[n].name) &&
719 strncmp(EffectList[n].name, str, len) == 0)
720 DisabledEffects[EffectList[n].type] = AL_TRUE;
722 } while(next++);
725 InitEffect(&DefaultEffect);
726 str = getenv("ALSOFT_DEFAULT_REVERB");
727 if(str && str[0])
728 GetReverbEffect(str, &DefaultEffect);
729 else if(ConfigValueStr(NULL, "default-reverb", &str))
730 GetReverbEffect(str, &DefaultEffect);
734 static void LockLists(void)
736 EnterCriticalSection(&ListLock);
739 static void UnlockLists(void)
741 LeaveCriticalSection(&ListLock);
745 static void ProbeList(ALCchar **list, size_t *listsize, enum DevProbe type)
747 DO_INITCONFIG();
749 LockLists();
750 free(*list);
751 *list = NULL;
752 *listsize = 0;
754 if(type == ALL_DEVICE_PROBE && PlaybackBackend.Probe)
755 PlaybackBackend.Probe(type);
756 else if(type == CAPTURE_DEVICE_PROBE && CaptureBackend.Probe)
757 CaptureBackend.Probe(type);
758 UnlockLists();
761 static void ProbeAllDeviceList(void)
762 { ProbeList(&alcAllDeviceList, &alcAllDeviceListSize, ALL_DEVICE_PROBE); }
763 static void ProbeCaptureDeviceList(void)
764 { ProbeList(&alcCaptureDeviceList, &alcCaptureDeviceListSize, CAPTURE_DEVICE_PROBE); }
767 static void AppendList(const ALCchar *name, ALCchar **List, size_t *ListSize)
769 size_t len = strlen(name);
770 void *temp;
772 if(len == 0)
773 return;
775 temp = realloc(*List, (*ListSize) + len + 2);
776 if(!temp)
778 ERR("Realloc failed to add %s!\n", name);
779 return;
781 *List = temp;
783 memcpy((*List)+(*ListSize), name, len+1);
784 *ListSize += len+1;
785 (*List)[*ListSize] = 0;
788 #define DECL_APPEND_LIST_FUNC(type) \
789 void Append##type##List(const ALCchar *name) \
790 { AppendList(name, &alc##type##List, &alc##type##ListSize); }
792 DECL_APPEND_LIST_FUNC(AllDevice)
793 DECL_APPEND_LIST_FUNC(CaptureDevice)
795 #undef DECL_APPEND_LIST_FUNC
798 /* Sets the default channel order used by most non-WaveFormatEx-based APIs */
799 void SetDefaultChannelOrder(ALCdevice *device)
801 switch(device->FmtChans)
803 case DevFmtX51: device->DevChannels[0] = FRONT_LEFT;
804 device->DevChannels[1] = FRONT_RIGHT;
805 device->DevChannels[2] = BACK_LEFT;
806 device->DevChannels[3] = BACK_RIGHT;
807 device->DevChannels[4] = FRONT_CENTER;
808 device->DevChannels[5] = LFE;
809 return;
811 case DevFmtX71: device->DevChannels[0] = FRONT_LEFT;
812 device->DevChannels[1] = FRONT_RIGHT;
813 device->DevChannels[2] = BACK_LEFT;
814 device->DevChannels[3] = BACK_RIGHT;
815 device->DevChannels[4] = FRONT_CENTER;
816 device->DevChannels[5] = LFE;
817 device->DevChannels[6] = SIDE_LEFT;
818 device->DevChannels[7] = SIDE_RIGHT;
819 return;
821 /* Same as WFX order */
822 case DevFmtMono:
823 case DevFmtStereo:
824 case DevFmtQuad:
825 case DevFmtX51Side:
826 case DevFmtX61:
827 break;
829 SetDefaultWFXChannelOrder(device);
831 /* Sets the default order used by WaveFormatEx */
832 void SetDefaultWFXChannelOrder(ALCdevice *device)
834 switch(device->FmtChans)
836 case DevFmtMono: device->DevChannels[0] = FRONT_CENTER; break;
838 case DevFmtStereo: device->DevChannels[0] = FRONT_LEFT;
839 device->DevChannels[1] = FRONT_RIGHT; break;
841 case DevFmtQuad: device->DevChannels[0] = FRONT_LEFT;
842 device->DevChannels[1] = FRONT_RIGHT;
843 device->DevChannels[2] = BACK_LEFT;
844 device->DevChannels[3] = BACK_RIGHT; break;
846 case DevFmtX51: device->DevChannels[0] = FRONT_LEFT;
847 device->DevChannels[1] = FRONT_RIGHT;
848 device->DevChannels[2] = FRONT_CENTER;
849 device->DevChannels[3] = LFE;
850 device->DevChannels[4] = BACK_LEFT;
851 device->DevChannels[5] = BACK_RIGHT; break;
853 case DevFmtX51Side: device->DevChannels[0] = FRONT_LEFT;
854 device->DevChannels[1] = FRONT_RIGHT;
855 device->DevChannels[2] = FRONT_CENTER;
856 device->DevChannels[3] = LFE;
857 device->DevChannels[4] = SIDE_LEFT;
858 device->DevChannels[5] = SIDE_RIGHT; break;
860 case DevFmtX61: device->DevChannels[0] = FRONT_LEFT;
861 device->DevChannels[1] = FRONT_RIGHT;
862 device->DevChannels[2] = FRONT_CENTER;
863 device->DevChannels[3] = LFE;
864 device->DevChannels[4] = BACK_CENTER;
865 device->DevChannels[5] = SIDE_LEFT;
866 device->DevChannels[6] = SIDE_RIGHT; break;
868 case DevFmtX71: device->DevChannels[0] = FRONT_LEFT;
869 device->DevChannels[1] = FRONT_RIGHT;
870 device->DevChannels[2] = FRONT_CENTER;
871 device->DevChannels[3] = LFE;
872 device->DevChannels[4] = BACK_LEFT;
873 device->DevChannels[5] = BACK_RIGHT;
874 device->DevChannels[6] = SIDE_LEFT;
875 device->DevChannels[7] = SIDE_RIGHT; break;
880 const ALCchar *DevFmtTypeString(enum DevFmtType type)
882 switch(type)
884 case DevFmtByte: return "Signed Byte";
885 case DevFmtUByte: return "Unsigned Byte";
886 case DevFmtShort: return "Signed Short";
887 case DevFmtUShort: return "Unsigned Short";
888 case DevFmtInt: return "Signed Int";
889 case DevFmtUInt: return "Unsigned Int";
890 case DevFmtFloat: return "Float";
892 return "(unknown type)";
894 const ALCchar *DevFmtChannelsString(enum DevFmtChannels chans)
896 switch(chans)
898 case DevFmtMono: return "Mono";
899 case DevFmtStereo: return "Stereo";
900 case DevFmtQuad: return "Quadraphonic";
901 case DevFmtX51: return "5.1 Surround";
902 case DevFmtX51Side: return "5.1 Side";
903 case DevFmtX61: return "6.1 Surround";
904 case DevFmtX71: return "7.1 Surround";
906 return "(unknown channels)";
909 ALuint BytesFromDevFmt(enum DevFmtType type)
911 switch(type)
913 case DevFmtByte: return sizeof(ALbyte);
914 case DevFmtUByte: return sizeof(ALubyte);
915 case DevFmtShort: return sizeof(ALshort);
916 case DevFmtUShort: return sizeof(ALushort);
917 case DevFmtInt: return sizeof(ALint);
918 case DevFmtUInt: return sizeof(ALuint);
919 case DevFmtFloat: return sizeof(ALfloat);
921 return 0;
923 ALuint ChannelsFromDevFmt(enum DevFmtChannels chans)
925 switch(chans)
927 case DevFmtMono: return 1;
928 case DevFmtStereo: return 2;
929 case DevFmtQuad: return 4;
930 case DevFmtX51: return 6;
931 case DevFmtX51Side: return 6;
932 case DevFmtX61: return 7;
933 case DevFmtX71: return 8;
935 return 0;
937 static ALboolean DecomposeDevFormat(ALenum format, enum DevFmtChannels *chans,
938 enum DevFmtType *type)
940 static const struct {
941 ALenum format;
942 enum DevFmtChannels channels;
943 enum DevFmtType type;
944 } list[] = {
945 { AL_FORMAT_MONO8, DevFmtMono, DevFmtUByte },
946 { AL_FORMAT_MONO16, DevFmtMono, DevFmtShort },
947 { AL_FORMAT_MONO_FLOAT32, DevFmtMono, DevFmtFloat },
949 { AL_FORMAT_STEREO8, DevFmtStereo, DevFmtUByte },
950 { AL_FORMAT_STEREO16, DevFmtStereo, DevFmtShort },
951 { AL_FORMAT_STEREO_FLOAT32, DevFmtStereo, DevFmtFloat },
953 { AL_FORMAT_QUAD8, DevFmtQuad, DevFmtUByte },
954 { AL_FORMAT_QUAD16, DevFmtQuad, DevFmtShort },
955 { AL_FORMAT_QUAD32, DevFmtQuad, DevFmtFloat },
957 { AL_FORMAT_51CHN8, DevFmtX51, DevFmtUByte },
958 { AL_FORMAT_51CHN16, DevFmtX51, DevFmtShort },
959 { AL_FORMAT_51CHN32, DevFmtX51, DevFmtFloat },
961 { AL_FORMAT_61CHN8, DevFmtX61, DevFmtUByte },
962 { AL_FORMAT_61CHN16, DevFmtX61, DevFmtShort },
963 { AL_FORMAT_61CHN32, DevFmtX61, DevFmtFloat },
965 { AL_FORMAT_71CHN8, DevFmtX71, DevFmtUByte },
966 { AL_FORMAT_71CHN16, DevFmtX71, DevFmtShort },
967 { AL_FORMAT_71CHN32, DevFmtX71, DevFmtFloat },
969 ALuint i;
971 for(i = 0;i < COUNTOF(list);i++)
973 if(list[i].format == format)
975 *chans = list[i].channels;
976 *type = list[i].type;
977 return AL_TRUE;
981 return AL_FALSE;
984 static ALCboolean IsValidALCType(ALCenum type)
986 switch(type)
988 case ALC_BYTE_SOFT:
989 case ALC_UNSIGNED_BYTE_SOFT:
990 case ALC_SHORT_SOFT:
991 case ALC_UNSIGNED_SHORT_SOFT:
992 case ALC_INT_SOFT:
993 case ALC_UNSIGNED_INT_SOFT:
994 case ALC_FLOAT_SOFT:
995 return ALC_TRUE;
997 return ALC_FALSE;
1000 static ALCboolean IsValidALCChannels(ALCenum channels)
1002 switch(channels)
1004 case ALC_MONO_SOFT:
1005 case ALC_STEREO_SOFT:
1006 case ALC_QUAD_SOFT:
1007 case ALC_5POINT1_SOFT:
1008 case ALC_6POINT1_SOFT:
1009 case ALC_7POINT1_SOFT:
1010 return ALC_TRUE;
1012 return ALC_FALSE;
1016 /* alcSetError
1018 * Stores the latest ALC Error
1020 static void alcSetError(ALCdevice *device, ALCenum errorCode)
1022 if(TrapALCError)
1024 #ifdef _WIN32
1025 /* DebugBreak() will cause an exception if there is no debugger */
1026 if(IsDebuggerPresent())
1027 DebugBreak();
1028 #elif defined(SIGTRAP)
1029 raise(SIGTRAP);
1030 #endif
1033 if(device)
1034 device->LastError = errorCode;
1035 else
1036 g_eLastNullDeviceError = errorCode;
1040 /* UpdateDeviceParams
1042 * Updates device parameters according to the attribute list (caller is
1043 * responsible for holding the list lock).
1045 static ALCenum UpdateDeviceParams(ALCdevice *device, const ALCint *attrList)
1047 ALCcontext *context;
1048 enum DevFmtChannels oldChans;
1049 enum DevFmtType oldType;
1050 ALCuint oldFreq;
1051 int oldMode;
1052 ALuint i;
1054 // Check for attributes
1055 if(device->Type == Loopback)
1057 enum {
1058 GotFreq = 1<<0,
1059 GotChans = 1<<1,
1060 GotType = 1<<2,
1061 GotAll = GotFreq|GotChans|GotType
1063 ALCuint freq, numMono, numStereo, numSends;
1064 enum DevFmtChannels schans;
1065 enum DevFmtType stype;
1066 ALCuint attrIdx = 0;
1067 ALCint gotFmt = 0;
1069 if(!attrList)
1071 WARN("Missing attributes for loopback device\n");
1072 return ALC_INVALID_VALUE;
1075 numMono = device->NumMonoSources;
1076 numStereo = device->NumStereoSources;
1077 numSends = device->NumAuxSends;
1078 schans = device->FmtChans;
1079 stype = device->FmtType;
1080 freq = device->Frequency;
1082 while(attrList[attrIdx])
1084 if(attrList[attrIdx] == ALC_FORMAT_CHANNELS_SOFT)
1086 ALCint val = attrList[attrIdx + 1];
1087 if(!IsValidALCChannels(val) || !ChannelsFromDevFmt(val))
1088 return ALC_INVALID_VALUE;
1089 schans = val;
1090 gotFmt |= GotChans;
1093 if(attrList[attrIdx] == ALC_FORMAT_TYPE_SOFT)
1095 ALCint val = attrList[attrIdx + 1];
1096 if(!IsValidALCType(val) || !BytesFromDevFmt(val))
1097 return ALC_INVALID_VALUE;
1098 stype = val;
1099 gotFmt |= GotType;
1102 if(attrList[attrIdx] == ALC_FREQUENCY)
1104 freq = attrList[attrIdx + 1];
1105 if(freq < MIN_OUTPUT_RATE)
1106 return ALC_INVALID_VALUE;
1107 gotFmt |= GotFreq;
1110 if(attrList[attrIdx] == ALC_STEREO_SOURCES)
1112 numStereo = attrList[attrIdx + 1];
1113 if(numStereo > device->MaxNoOfSources)
1114 numStereo = device->MaxNoOfSources;
1116 numMono = device->MaxNoOfSources - numStereo;
1119 if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS)
1120 numSends = attrList[attrIdx + 1];
1122 attrIdx += 2;
1125 if(gotFmt != GotAll)
1127 WARN("Missing format for loopback device\n");
1128 return ALC_INVALID_VALUE;
1131 ConfigValueUInt(NULL, "sends", &numSends);
1132 numSends = minu(MAX_SENDS, numSends);
1134 if((device->Flags&DEVICE_RUNNING))
1135 ALCdevice_StopPlayback(device);
1136 device->Flags &= ~DEVICE_RUNNING;
1138 device->Frequency = freq;
1139 device->FmtChans = schans;
1140 device->FmtType = stype;
1141 device->NumMonoSources = numMono;
1142 device->NumStereoSources = numStereo;
1143 device->NumAuxSends = numSends;
1145 else if(attrList && attrList[0])
1147 ALCuint freq, numMono, numStereo, numSends;
1148 ALCuint attrIdx = 0;
1150 /* If a context is already running on the device, stop playback so the
1151 * device attributes can be updated. */
1152 if((device->Flags&DEVICE_RUNNING))
1153 ALCdevice_StopPlayback(device);
1154 device->Flags &= ~DEVICE_RUNNING;
1156 freq = device->Frequency;
1157 numMono = device->NumMonoSources;
1158 numStereo = device->NumStereoSources;
1159 numSends = device->NumAuxSends;
1161 while(attrList[attrIdx])
1163 if(attrList[attrIdx] == ALC_FREQUENCY)
1165 freq = attrList[attrIdx + 1];
1166 device->Flags |= DEVICE_FREQUENCY_REQUEST;
1169 if(attrList[attrIdx] == ALC_STEREO_SOURCES)
1171 numStereo = attrList[attrIdx + 1];
1172 if(numStereo > device->MaxNoOfSources)
1173 numStereo = device->MaxNoOfSources;
1175 numMono = device->MaxNoOfSources - numStereo;
1178 if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS)
1179 numSends = attrList[attrIdx + 1];
1181 attrIdx += 2;
1184 ConfigValueUInt(NULL, "frequency", &freq);
1185 freq = maxu(freq, MIN_OUTPUT_RATE);
1187 ConfigValueUInt(NULL, "sends", &numSends);
1188 numSends = minu(MAX_SENDS, numSends);
1190 device->UpdateSize = (ALuint64)device->UpdateSize * freq /
1191 device->Frequency;
1193 device->Frequency = freq;
1194 device->NumMonoSources = numMono;
1195 device->NumStereoSources = numStereo;
1196 device->NumAuxSends = numSends;
1199 if((device->Flags&DEVICE_RUNNING))
1200 return ALC_NO_ERROR;
1202 oldFreq = device->Frequency;
1203 oldChans = device->FmtChans;
1204 oldType = device->FmtType;
1206 TRACE("Format pre-setup: %s%s, %s%s, %uhz%s, %u update size x%d\n",
1207 DevFmtChannelsString(device->FmtChans),
1208 (device->Flags&DEVICE_CHANNELS_REQUEST)?" (requested)":"",
1209 DevFmtTypeString(device->FmtType),
1210 (device->Flags&DEVICE_SAMPLE_TYPE_REQUEST)?" (requested)":"",
1211 device->Frequency,
1212 (device->Flags&DEVICE_FREQUENCY_REQUEST)?" (requested)":"",
1213 device->UpdateSize, device->NumUpdates);
1215 if(ALCdevice_ResetPlayback(device) == ALC_FALSE)
1216 return ALC_INVALID_DEVICE;
1218 if(device->FmtChans != oldChans && (device->Flags&DEVICE_CHANNELS_REQUEST))
1220 ERR("Failed to set %s, got %s instead\n", DevFmtChannelsString(oldChans),
1221 DevFmtChannelsString(device->FmtChans));
1222 device->Flags &= ~DEVICE_CHANNELS_REQUEST;
1224 if(device->FmtType != oldType && (device->Flags&DEVICE_SAMPLE_TYPE_REQUEST))
1226 ERR("Failed to set %s, got %s instead\n", DevFmtTypeString(oldType),
1227 DevFmtTypeString(device->FmtType));
1228 device->Flags &= ~DEVICE_SAMPLE_TYPE_REQUEST;
1230 if(device->Frequency != oldFreq && (device->Flags&DEVICE_FREQUENCY_REQUEST))
1232 ERR("Failed to set %uhz, got %uhz instead\n", oldFreq, device->Frequency);
1233 device->Flags &= ~DEVICE_FREQUENCY_REQUEST;
1236 TRACE("Format post-setup: %s, %s, %uhz, %u update size x%d\n",
1237 DevFmtChannelsString(device->FmtChans),
1238 DevFmtTypeString(device->FmtType), device->Frequency,
1239 device->UpdateSize, device->NumUpdates);
1241 aluInitPanning(device);
1243 for(i = 0;i < MAXCHANNELS;i++)
1245 device->ClickRemoval[i] = 0.0f;
1246 device->PendingClicks[i] = 0.0f;
1249 device->Hrtf = NULL;
1250 if(device->Type != Loopback && GetConfigValueBool(NULL, "hrtf", AL_FALSE))
1251 device->Hrtf = GetHrtf(device);
1252 TRACE("HRTF %s\n", device->Hrtf?"enabled":"disabled");
1254 if(!device->Hrtf && device->Bs2bLevel > 0 && device->Bs2bLevel <= 6)
1256 if(!device->Bs2b)
1258 device->Bs2b = calloc(1, sizeof(*device->Bs2b));
1259 bs2b_clear(device->Bs2b);
1261 bs2b_set_srate(device->Bs2b, device->Frequency);
1262 bs2b_set_level(device->Bs2b, device->Bs2bLevel);
1263 TRACE("BS2B level %d\n", device->Bs2bLevel);
1265 else
1267 free(device->Bs2b);
1268 device->Bs2b = NULL;
1269 TRACE("BS2B disabled\n");
1272 device->Flags &= ~DEVICE_DUPLICATE_STEREO;
1273 switch(device->FmtChans)
1275 case DevFmtMono:
1276 case DevFmtStereo:
1277 break;
1278 case DevFmtQuad:
1279 case DevFmtX51:
1280 case DevFmtX51Side:
1281 case DevFmtX61:
1282 case DevFmtX71:
1283 if(GetConfigValueBool(NULL, "stereodup", AL_TRUE))
1284 device->Flags |= DEVICE_DUPLICATE_STEREO;
1285 break;
1287 TRACE("Stereo duplication %s\n", (device->Flags&DEVICE_DUPLICATE_STEREO)?"enabled":"disabled");
1289 oldMode = SetMixerFPUMode();
1290 LockDevice(device);
1291 context = device->ContextList;
1292 while(context)
1294 ALsizei pos;
1296 context->UpdateSources = AL_FALSE;
1297 LockUIntMapRead(&context->EffectSlotMap);
1298 for(pos = 0;pos < context->EffectSlotMap.size;pos++)
1300 ALeffectslot *slot = context->EffectSlotMap.array[pos].value;
1302 if(ALeffectState_DeviceUpdate(slot->EffectState, device) == AL_FALSE)
1304 UnlockUIntMapRead(&context->EffectSlotMap);
1305 UnlockDevice(device);
1306 RestoreFPUMode(oldMode);
1307 return ALC_INVALID_DEVICE;
1309 slot->NeedsUpdate = AL_FALSE;
1310 ALeffectState_Update(slot->EffectState, device, slot);
1312 UnlockUIntMapRead(&context->EffectSlotMap);
1314 LockUIntMapRead(&context->SourceMap);
1315 for(pos = 0;pos < context->SourceMap.size;pos++)
1317 ALsource *source = context->SourceMap.array[pos].value;
1318 ALuint s = device->NumAuxSends;
1319 while(s < MAX_SENDS)
1321 if(source->Send[s].Slot)
1322 DecrementRef(&source->Send[s].Slot->ref);
1323 source->Send[s].Slot = NULL;
1324 source->Send[s].WetGain = 1.0f;
1325 source->Send[s].WetGainHF = 1.0f;
1326 s++;
1328 source->NeedsUpdate = AL_FALSE;
1329 ALsource_Update(source, context);
1331 UnlockUIntMapRead(&context->SourceMap);
1333 context = context->next;
1335 UnlockDevice(device);
1336 RestoreFPUMode(oldMode);
1338 if(ALCdevice_StartPlayback(device) == ALC_FALSE)
1339 return ALC_INVALID_DEVICE;
1340 device->Flags |= DEVICE_RUNNING;
1342 return ALC_NO_ERROR;
1345 /* FreeDevice
1347 * Frees the device structure, and destroys any objects the app failed to
1348 * delete. Called once there's no more references on the device.
1350 static ALCvoid FreeDevice(ALCdevice *device)
1352 TRACE("%p\n", device);
1354 if(device->DefaultSlot)
1356 ALeffectState_Destroy(device->DefaultSlot->EffectState);
1357 device->DefaultSlot->EffectState = NULL;
1360 if(device->BufferMap.size > 0)
1362 WARN("(%p) Deleting %d Buffer(s)\n", device, device->BufferMap.size);
1363 ReleaseALBuffers(device);
1365 ResetUIntMap(&device->BufferMap);
1367 if(device->EffectMap.size > 0)
1369 WARN("(%p) Deleting %d Effect(s)\n", device, device->EffectMap.size);
1370 ReleaseALEffects(device);
1372 ResetUIntMap(&device->EffectMap);
1374 if(device->FilterMap.size > 0)
1376 WARN("(%p) Deleting %d Filter(s)\n", device, device->FilterMap.size);
1377 ReleaseALFilters(device);
1379 ResetUIntMap(&device->FilterMap);
1381 free(device->Bs2b);
1382 device->Bs2b = NULL;
1384 free(device->szDeviceName);
1385 device->szDeviceName = NULL;
1387 DeleteCriticalSection(&device->Mutex);
1389 free(device);
1393 void ALCdevice_IncRef(ALCdevice *device)
1395 RefCount ref;
1396 ref = IncrementRef(&device->ref);
1397 TRACEREF("%p increasing refcount to %u\n", device, ref);
1400 void ALCdevice_DecRef(ALCdevice *device)
1402 RefCount ref;
1403 ref = DecrementRef(&device->ref);
1404 TRACEREF("%p decreasing refcount to %u\n", device, ref);
1405 if(ref == 0) FreeDevice(device);
1408 /* VerifyDevice
1410 * Checks if the device handle is valid, and increments its ref count if so.
1412 static ALCdevice *VerifyDevice(ALCdevice *device)
1414 ALCdevice *tmpDevice;
1416 if(!device)
1417 return NULL;
1419 LockLists();
1420 tmpDevice = DeviceList;
1421 while(tmpDevice && tmpDevice != device)
1422 tmpDevice = tmpDevice->next;
1424 if(tmpDevice)
1425 ALCdevice_IncRef(tmpDevice);
1426 UnlockLists();
1427 return tmpDevice;
1431 /* InitContext
1433 * Initializes context variables
1435 static ALvoid InitContext(ALCcontext *pContext)
1437 ALint i, j;
1439 //Initialise listener
1440 pContext->Listener.Gain = 1.0f;
1441 pContext->Listener.MetersPerUnit = 1.0f;
1442 pContext->Listener.Position[0] = 0.0f;
1443 pContext->Listener.Position[1] = 0.0f;
1444 pContext->Listener.Position[2] = 0.0f;
1445 pContext->Listener.Velocity[0] = 0.0f;
1446 pContext->Listener.Velocity[1] = 0.0f;
1447 pContext->Listener.Velocity[2] = 0.0f;
1448 pContext->Listener.Forward[0] = 0.0f;
1449 pContext->Listener.Forward[1] = 0.0f;
1450 pContext->Listener.Forward[2] = -1.0f;
1451 pContext->Listener.Up[0] = 0.0f;
1452 pContext->Listener.Up[1] = 1.0f;
1453 pContext->Listener.Up[2] = 0.0f;
1454 for(i = 0;i < 4;i++)
1456 for(j = 0;j < 4;j++)
1457 pContext->Listener.Matrix[i][j] = ((i==j) ? 1.0f : 0.0f);
1460 //Validate pContext
1461 pContext->LastError = AL_NO_ERROR;
1462 pContext->UpdateSources = AL_FALSE;
1463 pContext->ActiveSourceCount = 0;
1464 InitUIntMap(&pContext->SourceMap, pContext->Device->MaxNoOfSources);
1465 InitUIntMap(&pContext->EffectSlotMap, pContext->Device->AuxiliaryEffectSlotMax);
1467 //Set globals
1468 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
1469 pContext->SourceDistanceModel = AL_FALSE;
1470 pContext->DopplerFactor = 1.0f;
1471 pContext->DopplerVelocity = 1.0f;
1472 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
1473 pContext->DeferUpdates = AL_FALSE;
1475 pContext->ExtensionList = alExtList;
1479 /* FreeContext
1481 * Cleans up the context, and destroys any remaining objects the app failed to
1482 * delete. Called once there's no more references on the context.
1484 static ALCvoid FreeContext(ALCcontext *context)
1486 TRACE("%p\n", context);
1488 if(context->SourceMap.size > 0)
1490 ERR("(%p) Deleting %d Source(s)\n", context, context->SourceMap.size);
1491 ReleaseALSources(context);
1493 ResetUIntMap(&context->SourceMap);
1495 if(context->EffectSlotMap.size > 0)
1497 ERR("(%p) Deleting %d AuxiliaryEffectSlot(s)\n", context, context->EffectSlotMap.size);
1498 ReleaseALAuxiliaryEffectSlots(context);
1500 ResetUIntMap(&context->EffectSlotMap);
1502 context->ActiveSourceCount = 0;
1503 free(context->ActiveSources);
1504 context->ActiveSources = NULL;
1505 context->MaxActiveSources = 0;
1507 context->ActiveEffectSlotCount = 0;
1508 free(context->ActiveEffectSlots);
1509 context->ActiveEffectSlots = NULL;
1510 context->MaxActiveEffectSlots = 0;
1512 ALCdevice_DecRef(context->Device);
1513 context->Device = NULL;
1515 //Invalidate context
1516 memset(context, 0, sizeof(ALCcontext));
1517 free(context);
1520 /* ReleaseContext
1522 * Removes the context reference from the given device and removes it from
1523 * being current on the running thread or globally.
1525 static void ReleaseContext(ALCcontext *context, ALCdevice *device)
1527 ALCcontext *volatile*tmp_ctx;
1529 if(pthread_getspecific(LocalContext) == context)
1531 WARN("%p released while current on thread\n", context);
1532 pthread_setspecific(LocalContext, NULL);
1533 ALCcontext_DecRef(context);
1536 if(CompExchangePtr((XchgPtr*)&GlobalContext, context, NULL))
1537 ALCcontext_DecRef(context);
1539 LockDevice(device);
1540 tmp_ctx = &device->ContextList;
1541 while(*tmp_ctx)
1543 if(CompExchangePtr((XchgPtr*)tmp_ctx, context, context->next))
1544 break;
1545 tmp_ctx = &(*tmp_ctx)->next;
1547 UnlockDevice(device);
1549 ALCcontext_DecRef(context);
1552 void ALCcontext_IncRef(ALCcontext *context)
1554 RefCount ref;
1555 ref = IncrementRef(&context->ref);
1556 TRACEREF("%p increasing refcount to %u\n", context, ref);
1559 void ALCcontext_DecRef(ALCcontext *context)
1561 RefCount ref;
1562 ref = DecrementRef(&context->ref);
1563 TRACEREF("%p decreasing refcount to %u\n", context, ref);
1564 if(ref == 0) FreeContext(context);
1567 static void ReleaseThreadCtx(void *ptr)
1569 WARN("%p current for thread being destroyed\n", ptr);
1570 ALCcontext_DecRef(ptr);
1573 /* VerifyContext
1575 * Checks that the given context is valid, and increments its reference count.
1577 static ALCcontext *VerifyContext(ALCcontext *context)
1579 ALCdevice *dev;
1581 LockLists();
1582 dev = DeviceList;
1583 while(dev)
1585 ALCcontext *tmp_ctx = dev->ContextList;
1586 while(tmp_ctx)
1588 if(tmp_ctx == context)
1590 ALCcontext_IncRef(tmp_ctx);
1591 UnlockLists();
1592 return tmp_ctx;
1594 tmp_ctx = tmp_ctx->next;
1596 dev = dev->next;
1598 UnlockLists();
1600 return NULL;
1604 /* GetContextRef
1606 * Returns the currently active context, and adds a reference without locking
1607 * it.
1609 ALCcontext *GetContextRef(void)
1611 ALCcontext *context;
1613 context = pthread_getspecific(LocalContext);
1614 if(context)
1615 ALCcontext_IncRef(context);
1616 else
1618 LockLists();
1619 context = GlobalContext;
1620 if(context)
1621 ALCcontext_IncRef(context);
1622 UnlockLists();
1625 return context;
1628 ///////////////////////////////////////////////////////
1631 ///////////////////////////////////////////////////////
1632 // ALC Functions calls
1635 // This should probably move to another c file but for now ...
1636 ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
1638 ALCdevice *device = NULL;
1639 ALCenum err;
1641 DO_INITCONFIG();
1643 if(!CaptureBackend.name)
1645 alcSetError(NULL, ALC_INVALID_VALUE);
1646 return NULL;
1649 if(SampleSize <= 0)
1651 alcSetError(NULL, ALC_INVALID_VALUE);
1652 return NULL;
1655 if(deviceName && (!deviceName[0] || strcasecmp(deviceName, alcDefaultName) == 0 || strcasecmp(deviceName, "openal-soft") == 0))
1656 deviceName = NULL;
1658 device = calloc(1, sizeof(ALCdevice));
1659 if(!device)
1661 alcSetError(NULL, ALC_OUT_OF_MEMORY);
1662 return NULL;
1665 //Validate device
1666 device->Funcs = &CaptureBackend.Funcs;
1667 device->ref = 1;
1668 device->Connected = ALC_TRUE;
1669 device->Type = Capture;
1670 InitializeCriticalSection(&device->Mutex);
1672 InitUIntMap(&device->BufferMap, ~0);
1673 InitUIntMap(&device->EffectMap, ~0);
1674 InitUIntMap(&device->FilterMap, ~0);
1676 device->szDeviceName = NULL;
1678 device->Flags |= DEVICE_FREQUENCY_REQUEST;
1679 device->Frequency = frequency;
1681 device->Flags |= DEVICE_CHANNELS_REQUEST | DEVICE_SAMPLE_TYPE_REQUEST;
1682 if(DecomposeDevFormat(format, &device->FmtChans, &device->FmtType) == AL_FALSE)
1684 DeleteCriticalSection(&device->Mutex);
1685 free(device);
1686 alcSetError(NULL, ALC_INVALID_ENUM);
1687 return NULL;
1690 device->UpdateSize = SampleSize;
1691 device->NumUpdates = 1;
1693 LockLists();
1694 if((err=ALCdevice_OpenCapture(device, deviceName)) != ALC_NO_ERROR)
1696 UnlockLists();
1697 DeleteCriticalSection(&device->Mutex);
1698 free(device);
1699 alcSetError(NULL, err);
1700 return NULL;
1702 UnlockLists();
1704 do {
1705 device->next = DeviceList;
1706 } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
1708 TRACE("Created device %p\n", device);
1709 return device;
1712 ALC_API ALCboolean ALC_APIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
1714 ALCdevice *volatile*list;
1716 LockLists();
1717 list = &DeviceList;
1718 while(*list && *list != pDevice)
1719 list = &(*list)->next;
1721 if(!*list || (*list)->Type != Capture)
1723 alcSetError(*list, ALC_INVALID_DEVICE);
1724 UnlockLists();
1725 return ALC_FALSE;
1728 *list = (*list)->next;
1729 UnlockLists();
1731 ALCdevice_CloseCapture(pDevice);
1733 ALCdevice_DecRef(pDevice);
1735 return ALC_TRUE;
1738 ALC_API void ALC_APIENTRY alcCaptureStart(ALCdevice *device)
1740 LockLists();
1741 if(!(device=VerifyDevice(device)) || device->Type != Capture)
1743 UnlockLists();
1744 alcSetError(device, ALC_INVALID_DEVICE);
1745 if(device) ALCdevice_DecRef(device);
1746 return;
1748 if(device->Connected)
1750 if(!(device->Flags&DEVICE_RUNNING))
1751 ALCdevice_StartCapture(device);
1752 device->Flags |= DEVICE_RUNNING;
1754 UnlockLists();
1756 ALCdevice_DecRef(device);
1759 ALC_API void ALC_APIENTRY alcCaptureStop(ALCdevice *device)
1761 LockLists();
1762 if(!(device=VerifyDevice(device)) || device->Type != Capture)
1764 UnlockLists();
1765 alcSetError(device, ALC_INVALID_DEVICE);
1766 if(device) ALCdevice_DecRef(device);
1767 return;
1769 if((device->Flags&DEVICE_RUNNING))
1770 ALCdevice_StopCapture(device);
1771 device->Flags &= ~DEVICE_RUNNING;
1772 UnlockLists();
1774 ALCdevice_DecRef(device);
1777 ALC_API void ALC_APIENTRY alcCaptureSamples(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
1779 ALCenum err = ALC_INVALID_DEVICE;
1780 LockLists();
1781 if((device=VerifyDevice(device)) != NULL && device->Type == Capture)
1783 err = ALC_INVALID_VALUE;
1784 if(samples >= 0 && ALCdevice_AvailableSamples(device) >= (ALCuint)samples)
1785 err = ALCdevice_CaptureSamples(device, buffer, samples);
1787 UnlockLists();
1788 if(err != ALC_NO_ERROR)
1789 alcSetError(device, err);
1790 if(device) ALCdevice_DecRef(device);
1794 alcGetError
1796 Return last ALC generated error code
1798 ALC_API ALCenum ALC_APIENTRY alcGetError(ALCdevice *device)
1800 ALCenum errorCode;
1802 if(VerifyDevice(device))
1804 errorCode = ExchangeInt(&device->LastError, ALC_NO_ERROR);
1805 ALCdevice_DecRef(device);
1807 else
1808 errorCode = ExchangeInt(&g_eLastNullDeviceError, ALC_NO_ERROR);
1810 return errorCode;
1814 /* alcSuspendContext
1816 * Not functional
1818 ALC_API ALCvoid ALC_APIENTRY alcSuspendContext(ALCcontext *Context)
1820 (void)Context;
1823 /* alcProcessContext
1825 * Not functional
1827 ALC_API ALCvoid ALC_APIENTRY alcProcessContext(ALCcontext *Context)
1829 (void)Context;
1833 /* alcGetString
1835 * Returns information about the Device, and error strings
1837 ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
1839 const ALCchar *value = NULL;
1841 switch(param)
1843 case ALC_NO_ERROR:
1844 value = alcNoError;
1845 break;
1847 case ALC_INVALID_ENUM:
1848 value = alcErrInvalidEnum;
1849 break;
1851 case ALC_INVALID_VALUE:
1852 value = alcErrInvalidValue;
1853 break;
1855 case ALC_INVALID_DEVICE:
1856 value = alcErrInvalidDevice;
1857 break;
1859 case ALC_INVALID_CONTEXT:
1860 value = alcErrInvalidContext;
1861 break;
1863 case ALC_OUT_OF_MEMORY:
1864 value = alcErrOutOfMemory;
1865 break;
1867 case ALC_DEVICE_SPECIFIER:
1868 value = alcDefaultName;
1869 break;
1871 case ALC_ALL_DEVICES_SPECIFIER:
1872 if(VerifyDevice(pDevice))
1874 value = pDevice->szDeviceName;
1875 ALCdevice_DecRef(pDevice);
1877 else
1879 ProbeAllDeviceList();
1880 value = alcAllDeviceList;
1882 break;
1884 case ALC_CAPTURE_DEVICE_SPECIFIER:
1885 if(VerifyDevice(pDevice))
1887 value = pDevice->szDeviceName;
1888 ALCdevice_DecRef(pDevice);
1890 else
1892 ProbeCaptureDeviceList();
1893 value = alcCaptureDeviceList;
1895 break;
1897 /* Default devices are always first in the list */
1898 case ALC_DEFAULT_DEVICE_SPECIFIER:
1899 value = alcDefaultName;
1900 break;
1902 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
1903 if(!alcAllDeviceList)
1904 ProbeAllDeviceList();
1906 pDevice = VerifyDevice(pDevice);
1908 free(alcDefaultAllDeviceSpecifier);
1909 alcDefaultAllDeviceSpecifier = strdup(alcAllDeviceList ?
1910 alcAllDeviceList : "");
1911 if(!alcDefaultAllDeviceSpecifier)
1912 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
1914 value = alcDefaultAllDeviceSpecifier;
1915 if(pDevice) ALCdevice_DecRef(pDevice);
1916 break;
1918 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
1919 if(!alcCaptureDeviceList)
1920 ProbeCaptureDeviceList();
1922 pDevice = VerifyDevice(pDevice);
1924 free(alcCaptureDefaultDeviceSpecifier);
1925 alcCaptureDefaultDeviceSpecifier = strdup(alcCaptureDeviceList ?
1926 alcCaptureDeviceList : "");
1927 if(!alcCaptureDefaultDeviceSpecifier)
1928 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
1930 value = alcCaptureDefaultDeviceSpecifier;
1931 if(pDevice) ALCdevice_DecRef(pDevice);
1932 break;
1934 case ALC_EXTENSIONS:
1935 if(!VerifyDevice(pDevice))
1936 value = alcNoDeviceExtList;
1937 else
1939 value = alcExtensionList;
1940 ALCdevice_DecRef(pDevice);
1942 break;
1944 default:
1945 pDevice = VerifyDevice(pDevice);
1946 alcSetError(pDevice, ALC_INVALID_ENUM);
1947 if(pDevice) ALCdevice_DecRef(pDevice);
1948 break;
1951 return value;
1955 /* alcGetIntegerv
1957 * Returns information about the Device and the version of Open AL
1959 ALC_API ALCvoid ALC_APIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
1961 device = VerifyDevice(device);
1963 if(size == 0 || data == NULL)
1965 alcSetError(device, ALC_INVALID_VALUE);
1966 if(device) ALCdevice_DecRef(device);
1967 return;
1970 if(!device)
1972 switch(param)
1974 case ALC_MAJOR_VERSION:
1975 *data = alcMajorVersion;
1976 break;
1977 case ALC_MINOR_VERSION:
1978 *data = alcMinorVersion;
1979 break;
1981 case ALC_ATTRIBUTES_SIZE:
1982 case ALC_ALL_ATTRIBUTES:
1983 case ALC_FREQUENCY:
1984 case ALC_REFRESH:
1985 case ALC_SYNC:
1986 case ALC_MONO_SOURCES:
1987 case ALC_STEREO_SOURCES:
1988 case ALC_CAPTURE_SAMPLES:
1989 case ALC_FORMAT_CHANNELS_SOFT:
1990 case ALC_FORMAT_TYPE_SOFT:
1991 alcSetError(NULL, ALC_INVALID_DEVICE);
1992 break;
1994 default:
1995 alcSetError(NULL, ALC_INVALID_ENUM);
1996 break;
1999 else if(device->Type == Capture)
2001 switch(param)
2003 case ALC_CAPTURE_SAMPLES:
2004 LockLists();
2005 /* Re-validate the device since it may have been closed */
2006 ALCdevice_DecRef(device);
2007 if((device=VerifyDevice(device)) != NULL)
2008 *data = ALCdevice_AvailableSamples(device);
2009 else
2010 alcSetError(NULL, ALC_INVALID_DEVICE);
2011 UnlockLists();
2012 break;
2014 case ALC_CONNECTED:
2015 *data = device->Connected;
2016 break;
2018 default:
2019 alcSetError(device, ALC_INVALID_ENUM);
2020 break;
2023 else /* render device */
2025 switch(param)
2027 case ALC_MAJOR_VERSION:
2028 *data = alcMajorVersion;
2029 break;
2031 case ALC_MINOR_VERSION:
2032 *data = alcMinorVersion;
2033 break;
2035 case ALC_EFX_MAJOR_VERSION:
2036 *data = alcEFXMajorVersion;
2037 break;
2039 case ALC_EFX_MINOR_VERSION:
2040 *data = alcEFXMinorVersion;
2041 break;
2043 case ALC_ATTRIBUTES_SIZE:
2044 *data = 13;
2045 break;
2047 case ALC_ALL_ATTRIBUTES:
2048 if(size < 13)
2049 alcSetError(device, ALC_INVALID_VALUE);
2050 else
2052 int i = 0;
2054 data[i++] = ALC_FREQUENCY;
2055 data[i++] = device->Frequency;
2057 if(device->Type != Loopback)
2059 data[i++] = ALC_REFRESH;
2060 data[i++] = device->Frequency / device->UpdateSize;
2062 data[i++] = ALC_SYNC;
2063 data[i++] = ALC_FALSE;
2065 else
2067 data[i++] = ALC_FORMAT_CHANNELS_SOFT;
2068 data[i++] = device->FmtChans;
2070 data[i++] = ALC_FORMAT_TYPE_SOFT;
2071 data[i++] = device->FmtType;
2074 data[i++] = ALC_MONO_SOURCES;
2075 data[i++] = device->NumMonoSources;
2077 data[i++] = ALC_STEREO_SOURCES;
2078 data[i++] = device->NumStereoSources;
2080 data[i++] = ALC_MAX_AUXILIARY_SENDS;
2081 data[i++] = device->NumAuxSends;
2083 data[i++] = 0;
2085 break;
2087 case ALC_FREQUENCY:
2088 *data = device->Frequency;
2089 break;
2091 case ALC_REFRESH:
2092 if(device->Type == Loopback)
2093 alcSetError(device, ALC_INVALID_DEVICE);
2094 else
2095 *data = device->Frequency / device->UpdateSize;
2096 break;
2098 case ALC_SYNC:
2099 if(device->Type == Loopback)
2100 alcSetError(device, ALC_INVALID_DEVICE);
2101 else
2102 *data = ALC_FALSE;
2103 break;
2105 case ALC_FORMAT_CHANNELS_SOFT:
2106 if(device->Type != Loopback)
2107 alcSetError(device, ALC_INVALID_DEVICE);
2108 else
2109 *data = device->FmtChans;
2110 break;
2112 case ALC_FORMAT_TYPE_SOFT:
2113 if(device->Type != Loopback)
2114 alcSetError(device, ALC_INVALID_DEVICE);
2115 else
2116 *data = device->FmtType;
2117 break;
2119 case ALC_MONO_SOURCES:
2120 *data = device->NumMonoSources;
2121 break;
2123 case ALC_STEREO_SOURCES:
2124 *data = device->NumStereoSources;
2125 break;
2127 case ALC_MAX_AUXILIARY_SENDS:
2128 *data = device->NumAuxSends;
2129 break;
2131 case ALC_CONNECTED:
2132 *data = device->Connected;
2133 break;
2135 default:
2136 alcSetError(device, ALC_INVALID_ENUM);
2137 break;
2140 if(device)
2141 ALCdevice_DecRef(device);
2145 /* alcIsExtensionPresent
2147 * Determines if there is support for a particular extension
2149 ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
2151 ALCboolean bResult = ALC_FALSE;
2153 device = VerifyDevice(device);
2155 if(!extName)
2156 alcSetError(device, ALC_INVALID_VALUE);
2157 else
2159 size_t len = strlen(extName);
2160 const char *ptr = (device ? alcExtensionList : alcNoDeviceExtList);
2161 while(ptr && *ptr)
2163 if(strncasecmp(ptr, extName, len) == 0 &&
2164 (ptr[len] == '\0' || isspace(ptr[len])))
2166 bResult = ALC_TRUE;
2167 break;
2169 if((ptr=strchr(ptr, ' ')) != NULL)
2171 do {
2172 ++ptr;
2173 } while(isspace(*ptr));
2177 if(device)
2178 ALCdevice_DecRef(device);
2179 return bResult;
2183 /* alcGetProcAddress
2185 * Retrieves the function address for a particular extension function
2187 ALC_API ALCvoid* ALC_APIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
2189 ALCvoid *ptr = NULL;
2191 device = VerifyDevice(device);
2193 if(!funcName)
2194 alcSetError(device, ALC_INVALID_VALUE);
2195 else
2197 ALsizei i = 0;
2198 while(alcFunctions[i].funcName && strcmp(alcFunctions[i].funcName,funcName) != 0)
2199 i++;
2200 ptr = alcFunctions[i].address;
2202 if(device)
2203 ALCdevice_DecRef(device);
2204 return ptr;
2208 /* alcGetEnumValue
2210 * Get the value for a particular ALC Enumerated Value
2212 ALC_API ALCenum ALC_APIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
2214 ALCenum val = 0;
2216 device = VerifyDevice(device);
2218 if(!enumName)
2219 alcSetError(device, ALC_INVALID_VALUE);
2220 else
2222 ALsizei i = 0;
2223 while(enumeration[i].enumName && strcmp(enumeration[i].enumName,enumName) != 0)
2224 i++;
2225 val = enumeration[i].value;
2227 if(device)
2228 ALCdevice_DecRef(device);
2229 return val;
2233 /* alcCreateContext
2235 * Create and attach a Context to a particular Device.
2237 ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
2239 ALCcontext *ALContext;
2240 ALCenum err;
2242 LockLists();
2243 if(!(device=VerifyDevice(device)) || device->Type == Capture || !device->Connected)
2245 UnlockLists();
2246 alcSetError(device, ALC_INVALID_DEVICE);
2247 if(device) ALCdevice_DecRef(device);
2248 return NULL;
2251 /* Reset Context Last Error code */
2252 device->LastError = ALC_NO_ERROR;
2254 if((err=UpdateDeviceParams(device, attrList)) != ALC_NO_ERROR)
2256 UnlockLists();
2257 alcSetError(device, err);
2258 if(err == ALC_INVALID_DEVICE)
2259 aluHandleDisconnect(device);
2260 ALCdevice_DecRef(device);
2261 return NULL;
2264 ALContext = calloc(1, sizeof(ALCcontext));
2265 if(ALContext)
2267 ALContext->ref = 1;
2269 ALContext->MaxActiveSources = 256;
2270 ALContext->ActiveSources = malloc(sizeof(ALContext->ActiveSources[0]) *
2271 ALContext->MaxActiveSources);
2273 if(!ALContext || !ALContext->ActiveSources)
2275 if(!device->ContextList)
2277 ALCdevice_StopPlayback(device);
2278 device->Flags &= ~DEVICE_RUNNING;
2280 UnlockLists();
2282 free(ALContext);
2283 ALContext = NULL;
2285 alcSetError(device, ALC_OUT_OF_MEMORY);
2286 ALCdevice_DecRef(device);
2287 return NULL;
2290 ALContext->Device = device;
2291 ALCdevice_IncRef(device);
2292 InitContext(ALContext);
2294 do {
2295 ALContext->next = device->ContextList;
2296 } while(!CompExchangePtr((XchgPtr*)&device->ContextList, ALContext->next, ALContext));
2297 UnlockLists();
2299 if(device->DefaultSlot)
2300 InitializeEffect(device, device->DefaultSlot, &DefaultEffect);
2301 ALContext->LastError = AL_NO_ERROR;
2303 ALCdevice_DecRef(device);
2305 TRACE("Created context %p\n", ALContext);
2306 return ALContext;
2309 /* alcDestroyContext
2311 * Remove a Context
2313 ALC_API ALCvoid ALC_APIENTRY alcDestroyContext(ALCcontext *context)
2315 ALCdevice *Device;
2317 LockLists();
2318 /* alcGetContextsDevice sets an error for invalid contexts */
2319 Device = alcGetContextsDevice(context);
2320 if(Device)
2322 ReleaseContext(context, Device);
2323 if(!Device->ContextList)
2325 ALCdevice_StopPlayback(Device);
2326 Device->Flags &= ~DEVICE_RUNNING;
2329 UnlockLists();
2333 /* alcGetCurrentContext
2335 * Returns the currently active Context
2337 ALC_API ALCcontext* ALC_APIENTRY alcGetCurrentContext(ALCvoid)
2339 ALCcontext *Context;
2341 Context = pthread_getspecific(LocalContext);
2342 if(!Context) Context = GlobalContext;
2344 return Context;
2347 /* alcGetThreadContext
2349 * Returns the currently active thread-local Context
2351 ALC_API ALCcontext* ALC_APIENTRY alcGetThreadContext(void)
2353 ALCcontext *Context;
2354 Context = pthread_getspecific(LocalContext);
2355 return Context;
2359 /* alcMakeContextCurrent
2361 * Makes the given Context the active Context
2363 ALC_API ALCboolean ALC_APIENTRY alcMakeContextCurrent(ALCcontext *context)
2365 /* context must be a valid Context or NULL */
2366 if(context && !(context=VerifyContext(context)))
2368 alcSetError(NULL, ALC_INVALID_CONTEXT);
2369 return ALC_FALSE;
2371 /* context's reference count is already incremented */
2372 context = ExchangePtr((XchgPtr*)&GlobalContext, context);
2373 if(context) ALCcontext_DecRef(context);
2375 if((context=pthread_getspecific(LocalContext)) != NULL)
2377 pthread_setspecific(LocalContext, NULL);
2378 ALCcontext_DecRef(context);
2381 return ALC_TRUE;
2384 /* alcSetThreadContext
2386 * Makes the given Context the active Context for the current thread
2388 ALC_API ALCboolean ALC_APIENTRY alcSetThreadContext(ALCcontext *context)
2390 ALCcontext *old;
2392 /* context must be a valid Context or NULL */
2393 if(context && !(context=VerifyContext(context)))
2395 alcSetError(NULL, ALC_INVALID_CONTEXT);
2396 return ALC_FALSE;
2398 /* context's reference count is already incremented */
2399 old = pthread_getspecific(LocalContext);
2400 pthread_setspecific(LocalContext, context);
2401 if(old) ALCcontext_DecRef(old);
2403 return ALC_TRUE;
2407 /* alcGetContextsDevice
2409 * Returns the Device that a particular Context is attached to
2411 ALC_API ALCdevice* ALC_APIENTRY alcGetContextsDevice(ALCcontext *Context)
2413 ALCdevice *Device;
2415 if(!(Context=VerifyContext(Context)))
2417 alcSetError(NULL, ALC_INVALID_CONTEXT);
2418 return NULL;
2420 Device = Context->Device;
2421 ALCcontext_DecRef(Context);
2423 return Device;
2427 /* alcOpenDevice
2429 * Open the Device specified.
2431 ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName)
2433 const ALCchar *fmt;
2434 ALCdevice *device;
2435 ALCenum err;
2437 DO_INITCONFIG();
2439 if(!PlaybackBackend.name)
2441 alcSetError(NULL, ALC_INVALID_VALUE);
2442 return NULL;
2445 if(deviceName && (!deviceName[0] || strcasecmp(deviceName, alcDefaultName) == 0 || strcasecmp(deviceName, "openal-soft") == 0))
2446 deviceName = NULL;
2448 device = calloc(1, sizeof(ALCdevice)+sizeof(ALeffectslot));
2449 if(!device)
2451 alcSetError(NULL, ALC_OUT_OF_MEMORY);
2452 return NULL;
2455 //Validate device
2456 device->Funcs = &PlaybackBackend.Funcs;
2457 device->ref = 1;
2458 device->Connected = ALC_TRUE;
2459 device->Type = Playback;
2460 InitializeCriticalSection(&device->Mutex);
2461 device->LastError = ALC_NO_ERROR;
2463 device->Flags = 0;
2464 device->Bs2b = NULL;
2465 device->Bs2bLevel = 0;
2466 device->szDeviceName = NULL;
2468 device->ContextList = NULL;
2470 device->MaxNoOfSources = 256;
2471 device->AuxiliaryEffectSlotMax = 4;
2472 device->NumAuxSends = MAX_SENDS;
2474 InitUIntMap(&device->BufferMap, ~0);
2475 InitUIntMap(&device->EffectMap, ~0);
2476 InitUIntMap(&device->FilterMap, ~0);
2478 //Set output format
2479 device->FmtChans = DevFmtStereo;
2480 device->FmtType = DevFmtFloat;
2481 device->Frequency = DEFAULT_OUTPUT_RATE;
2482 device->NumUpdates = 4;
2483 device->UpdateSize = 1024;
2485 if(ConfigValueStr(NULL, "channels", &fmt))
2487 static const struct {
2488 const char name[16];
2489 enum DevFmtChannels chans;
2490 } chanlist[] = {
2491 { "mono", DevFmtMono },
2492 { "stereo", DevFmtStereo },
2493 { "quad", DevFmtQuad },
2494 { "surround51", DevFmtX51 },
2495 { "surround61", DevFmtX61 },
2496 { "surround71", DevFmtX71 },
2498 size_t i;
2500 for(i = 0;i < COUNTOF(chanlist);i++)
2502 if(strcasecmp(chanlist[i].name, fmt) == 0)
2504 device->FmtChans = chanlist[i].chans;
2505 device->Flags |= DEVICE_CHANNELS_REQUEST;
2506 break;
2509 if(i == COUNTOF(chanlist))
2510 ERR("Unsupported channels: %s\n", fmt);
2512 if(ConfigValueStr(NULL, "sample-type", &fmt))
2514 static const struct {
2515 const char name[16];
2516 enum DevFmtType type;
2517 } typelist[] = {
2518 { "int8", DevFmtByte },
2519 { "uint8", DevFmtUByte },
2520 { "int16", DevFmtShort },
2521 { "uint16", DevFmtUShort },
2522 { "int32", DevFmtInt },
2523 { "uint32", DevFmtUInt },
2524 { "float32", DevFmtFloat },
2526 size_t i;
2528 for(i = 0;i < COUNTOF(typelist);i++)
2530 if(strcasecmp(typelist[i].name, fmt) == 0)
2532 device->FmtType = typelist[i].type;
2533 device->Flags |= DEVICE_SAMPLE_TYPE_REQUEST;
2534 break;
2537 if(i == COUNTOF(typelist))
2538 ERR("Unsupported sample-type: %s\n", fmt);
2540 #define DEVICE_FORMAT_REQUEST (DEVICE_CHANNELS_REQUEST|DEVICE_SAMPLE_TYPE_REQUEST)
2541 if((device->Flags&DEVICE_FORMAT_REQUEST) != DEVICE_FORMAT_REQUEST &&
2542 ConfigValueStr(NULL, "format", &fmt))
2544 static const struct {
2545 const char name[32];
2546 enum DevFmtChannels channels;
2547 enum DevFmtType type;
2548 } formats[] = {
2549 { "AL_FORMAT_MONO32", DevFmtMono, DevFmtFloat },
2550 { "AL_FORMAT_STEREO32", DevFmtStereo, DevFmtFloat },
2551 { "AL_FORMAT_QUAD32", DevFmtQuad, DevFmtFloat },
2552 { "AL_FORMAT_51CHN32", DevFmtX51, DevFmtFloat },
2553 { "AL_FORMAT_61CHN32", DevFmtX61, DevFmtFloat },
2554 { "AL_FORMAT_71CHN32", DevFmtX71, DevFmtFloat },
2556 { "AL_FORMAT_MONO16", DevFmtMono, DevFmtShort },
2557 { "AL_FORMAT_STEREO16", DevFmtStereo, DevFmtShort },
2558 { "AL_FORMAT_QUAD16", DevFmtQuad, DevFmtShort },
2559 { "AL_FORMAT_51CHN16", DevFmtX51, DevFmtShort },
2560 { "AL_FORMAT_61CHN16", DevFmtX61, DevFmtShort },
2561 { "AL_FORMAT_71CHN16", DevFmtX71, DevFmtShort },
2563 { "AL_FORMAT_MONO8", DevFmtMono, DevFmtByte },
2564 { "AL_FORMAT_STEREO8", DevFmtStereo, DevFmtByte },
2565 { "AL_FORMAT_QUAD8", DevFmtQuad, DevFmtByte },
2566 { "AL_FORMAT_51CHN8", DevFmtX51, DevFmtByte },
2567 { "AL_FORMAT_61CHN8", DevFmtX61, DevFmtByte },
2568 { "AL_FORMAT_71CHN8", DevFmtX71, DevFmtByte }
2570 size_t i;
2572 ERR("Option 'format' is deprecated, please use 'channels' and 'sample-type'\n");
2573 for(i = 0;i < COUNTOF(formats);i++)
2575 if(strcasecmp(fmt, formats[i].name) == 0)
2577 if(!(device->Flags&DEVICE_CHANNELS_REQUEST))
2578 device->FmtChans = formats[i].channels;
2579 if(!(device->Flags&DEVICE_SAMPLE_TYPE_REQUEST))
2580 device->FmtType = formats[i].type;
2581 device->Flags |= DEVICE_FORMAT_REQUEST;
2582 break;
2585 if(i == COUNTOF(formats))
2586 ERR("Unsupported format: %s\n", fmt);
2588 #undef DEVICE_FORMAT_REQUEST
2590 if(ConfigValueUInt(NULL, "frequency", &device->Frequency))
2592 device->Flags |= DEVICE_FREQUENCY_REQUEST;
2593 if(device->Frequency < MIN_OUTPUT_RATE)
2594 ERR("%uhz request clamped to %uhz minimum\n", device->Frequency, MIN_OUTPUT_RATE);
2595 device->Frequency = maxu(device->Frequency, MIN_OUTPUT_RATE);
2598 ConfigValueUInt(NULL, "periods", &device->NumUpdates);
2599 device->NumUpdates = clampu(device->NumUpdates, 2, 16);
2601 ConfigValueUInt(NULL, "period_size", &device->UpdateSize);
2602 device->UpdateSize = clampu(device->UpdateSize, 64, 8192);
2604 ConfigValueUInt(NULL, "sources", &device->MaxNoOfSources);
2605 if(device->MaxNoOfSources == 0) device->MaxNoOfSources = 256;
2607 ConfigValueUInt(NULL, "slots", &device->AuxiliaryEffectSlotMax);
2608 if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
2610 ConfigValueUInt(NULL, "sends", &device->NumAuxSends);
2611 if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
2613 ConfigValueInt(NULL, "cf_level", &device->Bs2bLevel);
2615 device->NumStereoSources = 1;
2616 device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
2618 if(DefaultEffect.type != AL_EFFECT_NULL)
2620 device->DefaultSlot = (ALeffectslot*)(device+1);
2621 if(InitEffectSlot(device->DefaultSlot) != AL_NO_ERROR)
2622 device->DefaultSlot = NULL;
2625 // Find a playback device to open
2626 LockLists();
2627 if((err=ALCdevice_OpenPlayback(device, deviceName)) != ALC_NO_ERROR)
2629 UnlockLists();
2630 DeleteCriticalSection(&device->Mutex);
2631 free(device);
2632 alcSetError(NULL, err);
2633 return NULL;
2635 UnlockLists();
2637 do {
2638 device->next = DeviceList;
2639 } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
2641 TRACE("Created device %p, \"%s\"\n", device, device->szDeviceName);
2642 return device;
2645 /* alcCloseDevice
2647 * Close the specified Device
2649 ALC_API ALCboolean ALC_APIENTRY alcCloseDevice(ALCdevice *pDevice)
2651 ALCdevice *volatile*list;
2652 ALCcontext *ctx;
2654 LockLists();
2655 list = &DeviceList;
2656 while(*list && *list != pDevice)
2657 list = &(*list)->next;
2659 if(!*list || (*list)->Type == Capture)
2661 alcSetError(*list, ALC_INVALID_DEVICE);
2662 UnlockLists();
2663 return ALC_FALSE;
2666 *list = (*list)->next;
2667 UnlockLists();
2669 while((ctx=pDevice->ContextList) != NULL)
2671 WARN("Releasing context %p\n", ctx);
2672 ReleaseContext(ctx, pDevice);
2674 if((pDevice->Flags&DEVICE_RUNNING))
2675 ALCdevice_StopPlayback(pDevice);
2676 pDevice->Flags &= ~DEVICE_RUNNING;
2678 ALCdevice_ClosePlayback(pDevice);
2680 ALCdevice_DecRef(pDevice);
2682 return ALC_TRUE;
2686 /* alcLoopbackOpenDeviceSOFT
2688 * Open a loopback device, for manual rendering.
2690 ALC_API ALCdevice* ALC_APIENTRY alcLoopbackOpenDeviceSOFT(const ALCchar *deviceName)
2692 ALCdevice *device;
2694 DO_INITCONFIG();
2696 /* Make sure the device name, if specified, is us. */
2697 if(deviceName && strcmp(deviceName, alcDefaultName) != 0)
2699 alcSetError(NULL, ALC_INVALID_VALUE);
2700 return NULL;
2703 device = calloc(1, sizeof(ALCdevice));
2704 if(!device)
2706 alcSetError(NULL, ALC_OUT_OF_MEMORY);
2707 return NULL;
2710 //Validate device
2711 device->Funcs = &BackendLoopback.Funcs;
2712 device->ref = 1;
2713 device->Connected = ALC_TRUE;
2714 device->Type = Loopback;
2715 InitializeCriticalSection(&device->Mutex);
2716 device->LastError = ALC_NO_ERROR;
2718 device->Flags = 0;
2719 device->Bs2b = NULL;
2720 device->Bs2bLevel = 0;
2721 device->szDeviceName = NULL;
2723 device->ContextList = NULL;
2725 device->MaxNoOfSources = 256;
2726 device->AuxiliaryEffectSlotMax = 4;
2727 device->NumAuxSends = MAX_SENDS;
2729 InitUIntMap(&device->BufferMap, ~0);
2730 InitUIntMap(&device->EffectMap, ~0);
2731 InitUIntMap(&device->FilterMap, ~0);
2733 //Set output format
2734 device->NumUpdates = 0;
2735 device->UpdateSize = 0;
2737 device->Frequency = 44100;
2738 device->FmtChans = DevFmtStereo;
2739 device->FmtType = DevFmtShort;
2741 ConfigValueUInt(NULL, "sources", &device->MaxNoOfSources);
2742 if(device->MaxNoOfSources == 0) device->MaxNoOfSources = 256;
2744 ConfigValueUInt(NULL, "slots", &device->AuxiliaryEffectSlotMax);
2745 if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
2747 ConfigValueUInt(NULL, "sends", &device->NumAuxSends);
2748 if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
2750 device->NumStereoSources = 1;
2751 device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
2753 // Open the "backend"
2754 ALCdevice_OpenPlayback(device, "Loopback");
2755 do {
2756 device->next = DeviceList;
2757 } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
2759 TRACE("Created device %p\n", device);
2760 return device;
2763 /* alcIsRenderFormatSupportedSOFT
2765 * Determines if the loopback device supports the given format for rendering.
2767 ALC_API ALCboolean ALC_APIENTRY alcIsRenderFormatSupportedSOFT(ALCdevice *device, ALCsizei freq, ALCenum channels, ALCenum type)
2769 ALCboolean ret = ALC_FALSE;
2771 if(!(device=VerifyDevice(device)) || device->Type != Loopback)
2772 alcSetError(device, ALC_INVALID_DEVICE);
2773 else if(freq <= 0)
2774 alcSetError(device, ALC_INVALID_VALUE);
2775 else if(!IsValidALCType(type) || !IsValidALCChannels(channels))
2776 alcSetError(device, ALC_INVALID_ENUM);
2777 else
2779 if(BytesFromDevFmt(type) > 0 && ChannelsFromDevFmt(channels) > 0 &&
2780 freq >= MIN_OUTPUT_RATE)
2781 ret = ALC_TRUE;
2783 if(device) ALCdevice_DecRef(device);
2785 return ret;
2788 /* alcRenderSamplesSOFT
2790 * Renders some samples into a buffer, using the format last set by the
2791 * attributes given to alcCreateContext.
2793 ALC_API void ALC_APIENTRY alcRenderSamplesSOFT(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
2795 if(!(device=VerifyDevice(device)) || device->Type != Loopback)
2796 alcSetError(device, ALC_INVALID_DEVICE);
2797 else if(samples < 0 || (samples > 0 && buffer == NULL))
2798 alcSetError(device, ALC_INVALID_VALUE);
2799 else
2800 aluMixData(device, buffer, samples);
2801 if(device) ALCdevice_DecRef(device);
2805 static void ReleaseALC(void)
2807 ALCdevice *dev;
2809 free(alcAllDeviceList); alcAllDeviceList = NULL;
2810 alcAllDeviceListSize = 0;
2811 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
2812 alcCaptureDeviceListSize = 0;
2814 free(alcDefaultAllDeviceSpecifier);
2815 alcDefaultAllDeviceSpecifier = NULL;
2816 free(alcCaptureDefaultDeviceSpecifier);
2817 alcCaptureDefaultDeviceSpecifier = NULL;
2819 if((dev=ExchangePtr((XchgPtr*)&DeviceList, NULL)) != NULL)
2821 ALCuint num = 0;
2822 do {
2823 num++;
2824 } while((dev=dev->next) != NULL);
2825 ERR("%u device%s not closed\n", num, (num>1)?"s":"");
2829 ///////////////////////////////////////////////////////