Use enums to specify the default channel configuration and sample type
[openal-soft.git] / Alc / ALc.c
blob0c47818e4c4278438d42ca8edea3fcd32e31970e
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 if(device->DefaultSlot)
1337 ALeffectslot *slot = device->DefaultSlot;
1339 if(ALeffectState_DeviceUpdate(slot->EffectState, device) == AL_FALSE)
1341 UnlockDevice(device);
1342 RestoreFPUMode(oldMode);
1343 return ALC_INVALID_DEVICE;
1345 slot->NeedsUpdate = AL_FALSE;
1346 ALeffectState_Update(slot->EffectState, device, slot);
1348 UnlockDevice(device);
1349 RestoreFPUMode(oldMode);
1351 if(ALCdevice_StartPlayback(device) == ALC_FALSE)
1352 return ALC_INVALID_DEVICE;
1353 device->Flags |= DEVICE_RUNNING;
1355 return ALC_NO_ERROR;
1358 /* FreeDevice
1360 * Frees the device structure, and destroys any objects the app failed to
1361 * delete. Called once there's no more references on the device.
1363 static ALCvoid FreeDevice(ALCdevice *device)
1365 TRACE("%p\n", device);
1367 if(device->DefaultSlot)
1369 ALeffectState_Destroy(device->DefaultSlot->EffectState);
1370 device->DefaultSlot->EffectState = NULL;
1373 if(device->BufferMap.size > 0)
1375 WARN("(%p) Deleting %d Buffer(s)\n", device, device->BufferMap.size);
1376 ReleaseALBuffers(device);
1378 ResetUIntMap(&device->BufferMap);
1380 if(device->EffectMap.size > 0)
1382 WARN("(%p) Deleting %d Effect(s)\n", device, device->EffectMap.size);
1383 ReleaseALEffects(device);
1385 ResetUIntMap(&device->EffectMap);
1387 if(device->FilterMap.size > 0)
1389 WARN("(%p) Deleting %d Filter(s)\n", device, device->FilterMap.size);
1390 ReleaseALFilters(device);
1392 ResetUIntMap(&device->FilterMap);
1394 free(device->Bs2b);
1395 device->Bs2b = NULL;
1397 free(device->szDeviceName);
1398 device->szDeviceName = NULL;
1400 DeleteCriticalSection(&device->Mutex);
1402 free(device);
1406 void ALCdevice_IncRef(ALCdevice *device)
1408 RefCount ref;
1409 ref = IncrementRef(&device->ref);
1410 TRACEREF("%p increasing refcount to %u\n", device, ref);
1413 void ALCdevice_DecRef(ALCdevice *device)
1415 RefCount ref;
1416 ref = DecrementRef(&device->ref);
1417 TRACEREF("%p decreasing refcount to %u\n", device, ref);
1418 if(ref == 0) FreeDevice(device);
1421 /* VerifyDevice
1423 * Checks if the device handle is valid, and increments its ref count if so.
1425 static ALCdevice *VerifyDevice(ALCdevice *device)
1427 ALCdevice *tmpDevice;
1429 if(!device)
1430 return NULL;
1432 LockLists();
1433 tmpDevice = DeviceList;
1434 while(tmpDevice && tmpDevice != device)
1435 tmpDevice = tmpDevice->next;
1437 if(tmpDevice)
1438 ALCdevice_IncRef(tmpDevice);
1439 UnlockLists();
1440 return tmpDevice;
1444 /* InitContext
1446 * Initializes context variables
1448 static ALvoid InitContext(ALCcontext *pContext)
1450 ALint i, j;
1452 //Initialise listener
1453 pContext->Listener.Gain = 1.0f;
1454 pContext->Listener.MetersPerUnit = 1.0f;
1455 pContext->Listener.Position[0] = 0.0f;
1456 pContext->Listener.Position[1] = 0.0f;
1457 pContext->Listener.Position[2] = 0.0f;
1458 pContext->Listener.Velocity[0] = 0.0f;
1459 pContext->Listener.Velocity[1] = 0.0f;
1460 pContext->Listener.Velocity[2] = 0.0f;
1461 pContext->Listener.Forward[0] = 0.0f;
1462 pContext->Listener.Forward[1] = 0.0f;
1463 pContext->Listener.Forward[2] = -1.0f;
1464 pContext->Listener.Up[0] = 0.0f;
1465 pContext->Listener.Up[1] = 1.0f;
1466 pContext->Listener.Up[2] = 0.0f;
1467 for(i = 0;i < 4;i++)
1469 for(j = 0;j < 4;j++)
1470 pContext->Listener.Matrix[i][j] = ((i==j) ? 1.0f : 0.0f);
1473 //Validate pContext
1474 pContext->LastError = AL_NO_ERROR;
1475 pContext->UpdateSources = AL_FALSE;
1476 pContext->ActiveSourceCount = 0;
1477 InitUIntMap(&pContext->SourceMap, pContext->Device->MaxNoOfSources);
1478 InitUIntMap(&pContext->EffectSlotMap, pContext->Device->AuxiliaryEffectSlotMax);
1480 //Set globals
1481 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
1482 pContext->SourceDistanceModel = AL_FALSE;
1483 pContext->DopplerFactor = 1.0f;
1484 pContext->DopplerVelocity = 1.0f;
1485 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
1486 pContext->DeferUpdates = AL_FALSE;
1488 pContext->ExtensionList = alExtList;
1492 /* FreeContext
1494 * Cleans up the context, and destroys any remaining objects the app failed to
1495 * delete. Called once there's no more references on the context.
1497 static ALCvoid FreeContext(ALCcontext *context)
1499 TRACE("%p\n", context);
1501 if(context->SourceMap.size > 0)
1503 ERR("(%p) Deleting %d Source(s)\n", context, context->SourceMap.size);
1504 ReleaseALSources(context);
1506 ResetUIntMap(&context->SourceMap);
1508 if(context->EffectSlotMap.size > 0)
1510 ERR("(%p) Deleting %d AuxiliaryEffectSlot(s)\n", context, context->EffectSlotMap.size);
1511 ReleaseALAuxiliaryEffectSlots(context);
1513 ResetUIntMap(&context->EffectSlotMap);
1515 context->ActiveSourceCount = 0;
1516 free(context->ActiveSources);
1517 context->ActiveSources = NULL;
1518 context->MaxActiveSources = 0;
1520 context->ActiveEffectSlotCount = 0;
1521 free(context->ActiveEffectSlots);
1522 context->ActiveEffectSlots = NULL;
1523 context->MaxActiveEffectSlots = 0;
1525 ALCdevice_DecRef(context->Device);
1526 context->Device = NULL;
1528 //Invalidate context
1529 memset(context, 0, sizeof(ALCcontext));
1530 free(context);
1533 /* ReleaseContext
1535 * Removes the context reference from the given device and removes it from
1536 * being current on the running thread or globally.
1538 static void ReleaseContext(ALCcontext *context, ALCdevice *device)
1540 ALCcontext *volatile*tmp_ctx;
1542 if(pthread_getspecific(LocalContext) == context)
1544 WARN("%p released while current on thread\n", context);
1545 pthread_setspecific(LocalContext, NULL);
1546 ALCcontext_DecRef(context);
1549 if(CompExchangePtr((XchgPtr*)&GlobalContext, context, NULL))
1550 ALCcontext_DecRef(context);
1552 LockDevice(device);
1553 tmp_ctx = &device->ContextList;
1554 while(*tmp_ctx)
1556 if(CompExchangePtr((XchgPtr*)tmp_ctx, context, context->next))
1557 break;
1558 tmp_ctx = &(*tmp_ctx)->next;
1560 UnlockDevice(device);
1562 ALCcontext_DecRef(context);
1565 void ALCcontext_IncRef(ALCcontext *context)
1567 RefCount ref;
1568 ref = IncrementRef(&context->ref);
1569 TRACEREF("%p increasing refcount to %u\n", context, ref);
1572 void ALCcontext_DecRef(ALCcontext *context)
1574 RefCount ref;
1575 ref = DecrementRef(&context->ref);
1576 TRACEREF("%p decreasing refcount to %u\n", context, ref);
1577 if(ref == 0) FreeContext(context);
1580 static void ReleaseThreadCtx(void *ptr)
1582 WARN("%p current for thread being destroyed\n", ptr);
1583 ALCcontext_DecRef(ptr);
1586 /* VerifyContext
1588 * Checks that the given context is valid, and increments its reference count.
1590 static ALCcontext *VerifyContext(ALCcontext *context)
1592 ALCdevice *dev;
1594 LockLists();
1595 dev = DeviceList;
1596 while(dev)
1598 ALCcontext *tmp_ctx = dev->ContextList;
1599 while(tmp_ctx)
1601 if(tmp_ctx == context)
1603 ALCcontext_IncRef(tmp_ctx);
1604 UnlockLists();
1605 return tmp_ctx;
1607 tmp_ctx = tmp_ctx->next;
1609 dev = dev->next;
1611 UnlockLists();
1613 return NULL;
1617 /* GetContextRef
1619 * Returns the currently active context, and adds a reference without locking
1620 * it.
1622 ALCcontext *GetContextRef(void)
1624 ALCcontext *context;
1626 context = pthread_getspecific(LocalContext);
1627 if(context)
1628 ALCcontext_IncRef(context);
1629 else
1631 LockLists();
1632 context = GlobalContext;
1633 if(context)
1634 ALCcontext_IncRef(context);
1635 UnlockLists();
1638 return context;
1641 ///////////////////////////////////////////////////////
1644 ///////////////////////////////////////////////////////
1645 // ALC Functions calls
1648 // This should probably move to another c file but for now ...
1649 ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
1651 ALCdevice *device = NULL;
1652 ALCenum err;
1654 DO_INITCONFIG();
1656 if(!CaptureBackend.name)
1658 alcSetError(NULL, ALC_INVALID_VALUE);
1659 return NULL;
1662 if(SampleSize <= 0)
1664 alcSetError(NULL, ALC_INVALID_VALUE);
1665 return NULL;
1668 if(deviceName && (!deviceName[0] || strcasecmp(deviceName, alcDefaultName) == 0 || strcasecmp(deviceName, "openal-soft") == 0))
1669 deviceName = NULL;
1671 device = calloc(1, sizeof(ALCdevice));
1672 if(!device)
1674 alcSetError(NULL, ALC_OUT_OF_MEMORY);
1675 return NULL;
1678 //Validate device
1679 device->Funcs = &CaptureBackend.Funcs;
1680 device->ref = 1;
1681 device->Connected = ALC_TRUE;
1682 device->Type = Capture;
1683 InitializeCriticalSection(&device->Mutex);
1685 InitUIntMap(&device->BufferMap, ~0);
1686 InitUIntMap(&device->EffectMap, ~0);
1687 InitUIntMap(&device->FilterMap, ~0);
1689 device->szDeviceName = NULL;
1691 device->Flags |= DEVICE_FREQUENCY_REQUEST;
1692 device->Frequency = frequency;
1694 device->Flags |= DEVICE_CHANNELS_REQUEST | DEVICE_SAMPLE_TYPE_REQUEST;
1695 if(DecomposeDevFormat(format, &device->FmtChans, &device->FmtType) == AL_FALSE)
1697 DeleteCriticalSection(&device->Mutex);
1698 free(device);
1699 alcSetError(NULL, ALC_INVALID_ENUM);
1700 return NULL;
1703 device->UpdateSize = SampleSize;
1704 device->NumUpdates = 1;
1706 LockLists();
1707 if((err=ALCdevice_OpenCapture(device, deviceName)) != ALC_NO_ERROR)
1709 UnlockLists();
1710 DeleteCriticalSection(&device->Mutex);
1711 free(device);
1712 alcSetError(NULL, err);
1713 return NULL;
1715 UnlockLists();
1717 do {
1718 device->next = DeviceList;
1719 } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
1721 TRACE("Created device %p\n", device);
1722 return device;
1725 ALC_API ALCboolean ALC_APIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
1727 ALCdevice *volatile*list;
1729 LockLists();
1730 list = &DeviceList;
1731 while(*list && *list != pDevice)
1732 list = &(*list)->next;
1734 if(!*list || (*list)->Type != Capture)
1736 alcSetError(*list, ALC_INVALID_DEVICE);
1737 UnlockLists();
1738 return ALC_FALSE;
1741 *list = (*list)->next;
1742 UnlockLists();
1744 ALCdevice_CloseCapture(pDevice);
1746 ALCdevice_DecRef(pDevice);
1748 return ALC_TRUE;
1751 ALC_API void ALC_APIENTRY alcCaptureStart(ALCdevice *device)
1753 LockLists();
1754 if(!(device=VerifyDevice(device)) || device->Type != Capture)
1756 UnlockLists();
1757 alcSetError(device, ALC_INVALID_DEVICE);
1758 if(device) ALCdevice_DecRef(device);
1759 return;
1761 if(device->Connected)
1763 if(!(device->Flags&DEVICE_RUNNING))
1764 ALCdevice_StartCapture(device);
1765 device->Flags |= DEVICE_RUNNING;
1767 UnlockLists();
1769 ALCdevice_DecRef(device);
1772 ALC_API void ALC_APIENTRY alcCaptureStop(ALCdevice *device)
1774 LockLists();
1775 if(!(device=VerifyDevice(device)) || device->Type != Capture)
1777 UnlockLists();
1778 alcSetError(device, ALC_INVALID_DEVICE);
1779 if(device) ALCdevice_DecRef(device);
1780 return;
1782 if((device->Flags&DEVICE_RUNNING))
1783 ALCdevice_StopCapture(device);
1784 device->Flags &= ~DEVICE_RUNNING;
1785 UnlockLists();
1787 ALCdevice_DecRef(device);
1790 ALC_API void ALC_APIENTRY alcCaptureSamples(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
1792 ALCenum err = ALC_INVALID_DEVICE;
1793 LockLists();
1794 if((device=VerifyDevice(device)) != NULL && device->Type == Capture)
1796 err = ALC_INVALID_VALUE;
1797 if(samples >= 0 && ALCdevice_AvailableSamples(device) >= (ALCuint)samples)
1798 err = ALCdevice_CaptureSamples(device, buffer, samples);
1800 UnlockLists();
1801 if(err != ALC_NO_ERROR)
1802 alcSetError(device, err);
1803 if(device) ALCdevice_DecRef(device);
1807 alcGetError
1809 Return last ALC generated error code
1811 ALC_API ALCenum ALC_APIENTRY alcGetError(ALCdevice *device)
1813 ALCenum errorCode;
1815 if(VerifyDevice(device))
1817 errorCode = ExchangeInt(&device->LastError, ALC_NO_ERROR);
1818 ALCdevice_DecRef(device);
1820 else
1821 errorCode = ExchangeInt(&g_eLastNullDeviceError, ALC_NO_ERROR);
1823 return errorCode;
1827 /* alcSuspendContext
1829 * Not functional
1831 ALC_API ALCvoid ALC_APIENTRY alcSuspendContext(ALCcontext *Context)
1833 (void)Context;
1836 /* alcProcessContext
1838 * Not functional
1840 ALC_API ALCvoid ALC_APIENTRY alcProcessContext(ALCcontext *Context)
1842 (void)Context;
1846 /* alcGetString
1848 * Returns information about the Device, and error strings
1850 ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
1852 const ALCchar *value = NULL;
1854 switch(param)
1856 case ALC_NO_ERROR:
1857 value = alcNoError;
1858 break;
1860 case ALC_INVALID_ENUM:
1861 value = alcErrInvalidEnum;
1862 break;
1864 case ALC_INVALID_VALUE:
1865 value = alcErrInvalidValue;
1866 break;
1868 case ALC_INVALID_DEVICE:
1869 value = alcErrInvalidDevice;
1870 break;
1872 case ALC_INVALID_CONTEXT:
1873 value = alcErrInvalidContext;
1874 break;
1876 case ALC_OUT_OF_MEMORY:
1877 value = alcErrOutOfMemory;
1878 break;
1880 case ALC_DEVICE_SPECIFIER:
1881 value = alcDefaultName;
1882 break;
1884 case ALC_ALL_DEVICES_SPECIFIER:
1885 if(VerifyDevice(pDevice))
1887 value = pDevice->szDeviceName;
1888 ALCdevice_DecRef(pDevice);
1890 else
1892 ProbeAllDeviceList();
1893 value = alcAllDeviceList;
1895 break;
1897 case ALC_CAPTURE_DEVICE_SPECIFIER:
1898 if(VerifyDevice(pDevice))
1900 value = pDevice->szDeviceName;
1901 ALCdevice_DecRef(pDevice);
1903 else
1905 ProbeCaptureDeviceList();
1906 value = alcCaptureDeviceList;
1908 break;
1910 /* Default devices are always first in the list */
1911 case ALC_DEFAULT_DEVICE_SPECIFIER:
1912 value = alcDefaultName;
1913 break;
1915 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
1916 if(!alcAllDeviceList)
1917 ProbeAllDeviceList();
1919 pDevice = VerifyDevice(pDevice);
1921 free(alcDefaultAllDeviceSpecifier);
1922 alcDefaultAllDeviceSpecifier = strdup(alcAllDeviceList ?
1923 alcAllDeviceList : "");
1924 if(!alcDefaultAllDeviceSpecifier)
1925 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
1927 value = alcDefaultAllDeviceSpecifier;
1928 if(pDevice) ALCdevice_DecRef(pDevice);
1929 break;
1931 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
1932 if(!alcCaptureDeviceList)
1933 ProbeCaptureDeviceList();
1935 pDevice = VerifyDevice(pDevice);
1937 free(alcCaptureDefaultDeviceSpecifier);
1938 alcCaptureDefaultDeviceSpecifier = strdup(alcCaptureDeviceList ?
1939 alcCaptureDeviceList : "");
1940 if(!alcCaptureDefaultDeviceSpecifier)
1941 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
1943 value = alcCaptureDefaultDeviceSpecifier;
1944 if(pDevice) ALCdevice_DecRef(pDevice);
1945 break;
1947 case ALC_EXTENSIONS:
1948 if(!VerifyDevice(pDevice))
1949 value = alcNoDeviceExtList;
1950 else
1952 value = alcExtensionList;
1953 ALCdevice_DecRef(pDevice);
1955 break;
1957 default:
1958 pDevice = VerifyDevice(pDevice);
1959 alcSetError(pDevice, ALC_INVALID_ENUM);
1960 if(pDevice) ALCdevice_DecRef(pDevice);
1961 break;
1964 return value;
1968 /* alcGetIntegerv
1970 * Returns information about the Device and the version of Open AL
1972 ALC_API ALCvoid ALC_APIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
1974 device = VerifyDevice(device);
1976 if(size == 0 || data == NULL)
1978 alcSetError(device, ALC_INVALID_VALUE);
1979 if(device) ALCdevice_DecRef(device);
1980 return;
1983 if(!device)
1985 switch(param)
1987 case ALC_MAJOR_VERSION:
1988 *data = alcMajorVersion;
1989 break;
1990 case ALC_MINOR_VERSION:
1991 *data = alcMinorVersion;
1992 break;
1994 case ALC_ATTRIBUTES_SIZE:
1995 case ALC_ALL_ATTRIBUTES:
1996 case ALC_FREQUENCY:
1997 case ALC_REFRESH:
1998 case ALC_SYNC:
1999 case ALC_MONO_SOURCES:
2000 case ALC_STEREO_SOURCES:
2001 case ALC_CAPTURE_SAMPLES:
2002 case ALC_FORMAT_CHANNELS_SOFT:
2003 case ALC_FORMAT_TYPE_SOFT:
2004 alcSetError(NULL, ALC_INVALID_DEVICE);
2005 break;
2007 default:
2008 alcSetError(NULL, ALC_INVALID_ENUM);
2009 break;
2012 else if(device->Type == Capture)
2014 switch(param)
2016 case ALC_CAPTURE_SAMPLES:
2017 LockLists();
2018 /* Re-validate the device since it may have been closed */
2019 ALCdevice_DecRef(device);
2020 if((device=VerifyDevice(device)) != NULL)
2021 *data = ALCdevice_AvailableSamples(device);
2022 else
2023 alcSetError(NULL, ALC_INVALID_DEVICE);
2024 UnlockLists();
2025 break;
2027 case ALC_CONNECTED:
2028 *data = device->Connected;
2029 break;
2031 default:
2032 alcSetError(device, ALC_INVALID_ENUM);
2033 break;
2036 else /* render device */
2038 switch(param)
2040 case ALC_MAJOR_VERSION:
2041 *data = alcMajorVersion;
2042 break;
2044 case ALC_MINOR_VERSION:
2045 *data = alcMinorVersion;
2046 break;
2048 case ALC_EFX_MAJOR_VERSION:
2049 *data = alcEFXMajorVersion;
2050 break;
2052 case ALC_EFX_MINOR_VERSION:
2053 *data = alcEFXMinorVersion;
2054 break;
2056 case ALC_ATTRIBUTES_SIZE:
2057 *data = 13;
2058 break;
2060 case ALC_ALL_ATTRIBUTES:
2061 if(size < 13)
2062 alcSetError(device, ALC_INVALID_VALUE);
2063 else
2065 int i = 0;
2067 data[i++] = ALC_FREQUENCY;
2068 data[i++] = device->Frequency;
2070 if(device->Type != Loopback)
2072 data[i++] = ALC_REFRESH;
2073 data[i++] = device->Frequency / device->UpdateSize;
2075 data[i++] = ALC_SYNC;
2076 data[i++] = ALC_FALSE;
2078 else
2080 data[i++] = ALC_FORMAT_CHANNELS_SOFT;
2081 data[i++] = device->FmtChans;
2083 data[i++] = ALC_FORMAT_TYPE_SOFT;
2084 data[i++] = device->FmtType;
2087 data[i++] = ALC_MONO_SOURCES;
2088 data[i++] = device->NumMonoSources;
2090 data[i++] = ALC_STEREO_SOURCES;
2091 data[i++] = device->NumStereoSources;
2093 data[i++] = ALC_MAX_AUXILIARY_SENDS;
2094 data[i++] = device->NumAuxSends;
2096 data[i++] = 0;
2098 break;
2100 case ALC_FREQUENCY:
2101 *data = device->Frequency;
2102 break;
2104 case ALC_REFRESH:
2105 if(device->Type == Loopback)
2106 alcSetError(device, ALC_INVALID_DEVICE);
2107 else
2108 *data = device->Frequency / device->UpdateSize;
2109 break;
2111 case ALC_SYNC:
2112 if(device->Type == Loopback)
2113 alcSetError(device, ALC_INVALID_DEVICE);
2114 else
2115 *data = ALC_FALSE;
2116 break;
2118 case ALC_FORMAT_CHANNELS_SOFT:
2119 if(device->Type != Loopback)
2120 alcSetError(device, ALC_INVALID_DEVICE);
2121 else
2122 *data = device->FmtChans;
2123 break;
2125 case ALC_FORMAT_TYPE_SOFT:
2126 if(device->Type != Loopback)
2127 alcSetError(device, ALC_INVALID_DEVICE);
2128 else
2129 *data = device->FmtType;
2130 break;
2132 case ALC_MONO_SOURCES:
2133 *data = device->NumMonoSources;
2134 break;
2136 case ALC_STEREO_SOURCES:
2137 *data = device->NumStereoSources;
2138 break;
2140 case ALC_MAX_AUXILIARY_SENDS:
2141 *data = device->NumAuxSends;
2142 break;
2144 case ALC_CONNECTED:
2145 *data = device->Connected;
2146 break;
2148 default:
2149 alcSetError(device, ALC_INVALID_ENUM);
2150 break;
2153 if(device)
2154 ALCdevice_DecRef(device);
2158 /* alcIsExtensionPresent
2160 * Determines if there is support for a particular extension
2162 ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
2164 ALCboolean bResult = ALC_FALSE;
2166 device = VerifyDevice(device);
2168 if(!extName)
2169 alcSetError(device, ALC_INVALID_VALUE);
2170 else
2172 size_t len = strlen(extName);
2173 const char *ptr = (device ? alcExtensionList : alcNoDeviceExtList);
2174 while(ptr && *ptr)
2176 if(strncasecmp(ptr, extName, len) == 0 &&
2177 (ptr[len] == '\0' || isspace(ptr[len])))
2179 bResult = ALC_TRUE;
2180 break;
2182 if((ptr=strchr(ptr, ' ')) != NULL)
2184 do {
2185 ++ptr;
2186 } while(isspace(*ptr));
2190 if(device)
2191 ALCdevice_DecRef(device);
2192 return bResult;
2196 /* alcGetProcAddress
2198 * Retrieves the function address for a particular extension function
2200 ALC_API ALCvoid* ALC_APIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
2202 ALCvoid *ptr = NULL;
2204 device = VerifyDevice(device);
2206 if(!funcName)
2207 alcSetError(device, ALC_INVALID_VALUE);
2208 else
2210 ALsizei i = 0;
2211 while(alcFunctions[i].funcName && strcmp(alcFunctions[i].funcName,funcName) != 0)
2212 i++;
2213 ptr = alcFunctions[i].address;
2215 if(device)
2216 ALCdevice_DecRef(device);
2217 return ptr;
2221 /* alcGetEnumValue
2223 * Get the value for a particular ALC Enumerated Value
2225 ALC_API ALCenum ALC_APIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
2227 ALCenum val = 0;
2229 device = VerifyDevice(device);
2231 if(!enumName)
2232 alcSetError(device, ALC_INVALID_VALUE);
2233 else
2235 ALsizei i = 0;
2236 while(enumeration[i].enumName && strcmp(enumeration[i].enumName,enumName) != 0)
2237 i++;
2238 val = enumeration[i].value;
2240 if(device)
2241 ALCdevice_DecRef(device);
2242 return val;
2246 /* alcCreateContext
2248 * Create and attach a Context to a particular Device.
2250 ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
2252 ALCcontext *ALContext;
2253 ALCenum err;
2255 LockLists();
2256 if(!(device=VerifyDevice(device)) || device->Type == Capture || !device->Connected)
2258 UnlockLists();
2259 alcSetError(device, ALC_INVALID_DEVICE);
2260 if(device) ALCdevice_DecRef(device);
2261 return NULL;
2264 /* Reset Context Last Error code */
2265 device->LastError = ALC_NO_ERROR;
2267 if((err=UpdateDeviceParams(device, attrList)) != ALC_NO_ERROR)
2269 UnlockLists();
2270 alcSetError(device, err);
2271 if(err == ALC_INVALID_DEVICE)
2272 aluHandleDisconnect(device);
2273 ALCdevice_DecRef(device);
2274 return NULL;
2277 ALContext = calloc(1, sizeof(ALCcontext));
2278 if(ALContext)
2280 ALContext->ref = 1;
2282 ALContext->MaxActiveSources = 256;
2283 ALContext->ActiveSources = malloc(sizeof(ALContext->ActiveSources[0]) *
2284 ALContext->MaxActiveSources);
2286 if(!ALContext || !ALContext->ActiveSources)
2288 if(!device->ContextList)
2290 ALCdevice_StopPlayback(device);
2291 device->Flags &= ~DEVICE_RUNNING;
2293 UnlockLists();
2295 free(ALContext);
2296 ALContext = NULL;
2298 alcSetError(device, ALC_OUT_OF_MEMORY);
2299 ALCdevice_DecRef(device);
2300 return NULL;
2303 ALContext->Device = device;
2304 ALCdevice_IncRef(device);
2305 InitContext(ALContext);
2307 do {
2308 ALContext->next = device->ContextList;
2309 } while(!CompExchangePtr((XchgPtr*)&device->ContextList, ALContext->next, ALContext));
2310 UnlockLists();
2312 ALCdevice_DecRef(device);
2314 TRACE("Created context %p\n", ALContext);
2315 return ALContext;
2318 /* alcDestroyContext
2320 * Remove a Context
2322 ALC_API ALCvoid ALC_APIENTRY alcDestroyContext(ALCcontext *context)
2324 ALCdevice *Device;
2326 LockLists();
2327 /* alcGetContextsDevice sets an error for invalid contexts */
2328 Device = alcGetContextsDevice(context);
2329 if(Device)
2331 ReleaseContext(context, Device);
2332 if(!Device->ContextList)
2334 ALCdevice_StopPlayback(Device);
2335 Device->Flags &= ~DEVICE_RUNNING;
2338 UnlockLists();
2342 /* alcGetCurrentContext
2344 * Returns the currently active Context
2346 ALC_API ALCcontext* ALC_APIENTRY alcGetCurrentContext(ALCvoid)
2348 ALCcontext *Context;
2350 Context = pthread_getspecific(LocalContext);
2351 if(!Context) Context = GlobalContext;
2353 return Context;
2356 /* alcGetThreadContext
2358 * Returns the currently active thread-local Context
2360 ALC_API ALCcontext* ALC_APIENTRY alcGetThreadContext(void)
2362 ALCcontext *Context;
2363 Context = pthread_getspecific(LocalContext);
2364 return Context;
2368 /* alcMakeContextCurrent
2370 * Makes the given Context the active Context
2372 ALC_API ALCboolean ALC_APIENTRY alcMakeContextCurrent(ALCcontext *context)
2374 /* context must be a valid Context or NULL */
2375 if(context && !(context=VerifyContext(context)))
2377 alcSetError(NULL, ALC_INVALID_CONTEXT);
2378 return ALC_FALSE;
2380 /* context's reference count is already incremented */
2381 context = ExchangePtr((XchgPtr*)&GlobalContext, context);
2382 if(context) ALCcontext_DecRef(context);
2384 if((context=pthread_getspecific(LocalContext)) != NULL)
2386 pthread_setspecific(LocalContext, NULL);
2387 ALCcontext_DecRef(context);
2390 return ALC_TRUE;
2393 /* alcSetThreadContext
2395 * Makes the given Context the active Context for the current thread
2397 ALC_API ALCboolean ALC_APIENTRY alcSetThreadContext(ALCcontext *context)
2399 ALCcontext *old;
2401 /* context must be a valid Context or NULL */
2402 if(context && !(context=VerifyContext(context)))
2404 alcSetError(NULL, ALC_INVALID_CONTEXT);
2405 return ALC_FALSE;
2407 /* context's reference count is already incremented */
2408 old = pthread_getspecific(LocalContext);
2409 pthread_setspecific(LocalContext, context);
2410 if(old) ALCcontext_DecRef(old);
2412 return ALC_TRUE;
2416 /* alcGetContextsDevice
2418 * Returns the Device that a particular Context is attached to
2420 ALC_API ALCdevice* ALC_APIENTRY alcGetContextsDevice(ALCcontext *Context)
2422 ALCdevice *Device;
2424 if(!(Context=VerifyContext(Context)))
2426 alcSetError(NULL, ALC_INVALID_CONTEXT);
2427 return NULL;
2429 Device = Context->Device;
2430 ALCcontext_DecRef(Context);
2432 return Device;
2436 /* alcOpenDevice
2438 * Open the Device specified.
2440 ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName)
2442 const ALCchar *fmt;
2443 ALCdevice *device;
2444 ALCenum err;
2446 DO_INITCONFIG();
2448 if(!PlaybackBackend.name)
2450 alcSetError(NULL, ALC_INVALID_VALUE);
2451 return NULL;
2454 if(deviceName && (!deviceName[0] || strcasecmp(deviceName, alcDefaultName) == 0 || strcasecmp(deviceName, "openal-soft") == 0))
2455 deviceName = NULL;
2457 device = calloc(1, sizeof(ALCdevice)+sizeof(ALeffectslot));
2458 if(!device)
2460 alcSetError(NULL, ALC_OUT_OF_MEMORY);
2461 return NULL;
2464 //Validate device
2465 device->Funcs = &PlaybackBackend.Funcs;
2466 device->ref = 1;
2467 device->Connected = ALC_TRUE;
2468 device->Type = Playback;
2469 InitializeCriticalSection(&device->Mutex);
2470 device->LastError = ALC_NO_ERROR;
2472 device->Flags = 0;
2473 device->Bs2b = NULL;
2474 device->Bs2bLevel = 0;
2475 device->szDeviceName = NULL;
2477 device->ContextList = NULL;
2479 device->MaxNoOfSources = 256;
2480 device->AuxiliaryEffectSlotMax = 4;
2481 device->NumAuxSends = MAX_SENDS;
2483 InitUIntMap(&device->BufferMap, ~0);
2484 InitUIntMap(&device->EffectMap, ~0);
2485 InitUIntMap(&device->FilterMap, ~0);
2487 //Set output format
2488 device->FmtChans = DevFmtChannelsDefault;
2489 device->FmtType = DevFmtTypeDefault;
2490 device->Frequency = DEFAULT_OUTPUT_RATE;
2491 device->NumUpdates = 4;
2492 device->UpdateSize = 1024;
2494 if(ConfigValueStr(NULL, "channels", &fmt))
2496 static const struct {
2497 const char name[16];
2498 enum DevFmtChannels chans;
2499 } chanlist[] = {
2500 { "mono", DevFmtMono },
2501 { "stereo", DevFmtStereo },
2502 { "quad", DevFmtQuad },
2503 { "surround51", DevFmtX51 },
2504 { "surround61", DevFmtX61 },
2505 { "surround71", DevFmtX71 },
2507 size_t i;
2509 for(i = 0;i < COUNTOF(chanlist);i++)
2511 if(strcasecmp(chanlist[i].name, fmt) == 0)
2513 device->FmtChans = chanlist[i].chans;
2514 device->Flags |= DEVICE_CHANNELS_REQUEST;
2515 break;
2518 if(i == COUNTOF(chanlist))
2519 ERR("Unsupported channels: %s\n", fmt);
2521 if(ConfigValueStr(NULL, "sample-type", &fmt))
2523 static const struct {
2524 const char name[16];
2525 enum DevFmtType type;
2526 } typelist[] = {
2527 { "int8", DevFmtByte },
2528 { "uint8", DevFmtUByte },
2529 { "int16", DevFmtShort },
2530 { "uint16", DevFmtUShort },
2531 { "int32", DevFmtInt },
2532 { "uint32", DevFmtUInt },
2533 { "float32", DevFmtFloat },
2535 size_t i;
2537 for(i = 0;i < COUNTOF(typelist);i++)
2539 if(strcasecmp(typelist[i].name, fmt) == 0)
2541 device->FmtType = typelist[i].type;
2542 device->Flags |= DEVICE_SAMPLE_TYPE_REQUEST;
2543 break;
2546 if(i == COUNTOF(typelist))
2547 ERR("Unsupported sample-type: %s\n", fmt);
2549 #define DEVICE_FORMAT_REQUEST (DEVICE_CHANNELS_REQUEST|DEVICE_SAMPLE_TYPE_REQUEST)
2550 if((device->Flags&DEVICE_FORMAT_REQUEST) != DEVICE_FORMAT_REQUEST &&
2551 ConfigValueStr(NULL, "format", &fmt))
2553 static const struct {
2554 const char name[32];
2555 enum DevFmtChannels channels;
2556 enum DevFmtType type;
2557 } formats[] = {
2558 { "AL_FORMAT_MONO32", DevFmtMono, DevFmtFloat },
2559 { "AL_FORMAT_STEREO32", DevFmtStereo, DevFmtFloat },
2560 { "AL_FORMAT_QUAD32", DevFmtQuad, DevFmtFloat },
2561 { "AL_FORMAT_51CHN32", DevFmtX51, DevFmtFloat },
2562 { "AL_FORMAT_61CHN32", DevFmtX61, DevFmtFloat },
2563 { "AL_FORMAT_71CHN32", DevFmtX71, DevFmtFloat },
2565 { "AL_FORMAT_MONO16", DevFmtMono, DevFmtShort },
2566 { "AL_FORMAT_STEREO16", DevFmtStereo, DevFmtShort },
2567 { "AL_FORMAT_QUAD16", DevFmtQuad, DevFmtShort },
2568 { "AL_FORMAT_51CHN16", DevFmtX51, DevFmtShort },
2569 { "AL_FORMAT_61CHN16", DevFmtX61, DevFmtShort },
2570 { "AL_FORMAT_71CHN16", DevFmtX71, DevFmtShort },
2572 { "AL_FORMAT_MONO8", DevFmtMono, DevFmtByte },
2573 { "AL_FORMAT_STEREO8", DevFmtStereo, DevFmtByte },
2574 { "AL_FORMAT_QUAD8", DevFmtQuad, DevFmtByte },
2575 { "AL_FORMAT_51CHN8", DevFmtX51, DevFmtByte },
2576 { "AL_FORMAT_61CHN8", DevFmtX61, DevFmtByte },
2577 { "AL_FORMAT_71CHN8", DevFmtX71, DevFmtByte }
2579 size_t i;
2581 ERR("Option 'format' is deprecated, please use 'channels' and 'sample-type'\n");
2582 for(i = 0;i < COUNTOF(formats);i++)
2584 if(strcasecmp(fmt, formats[i].name) == 0)
2586 if(!(device->Flags&DEVICE_CHANNELS_REQUEST))
2587 device->FmtChans = formats[i].channels;
2588 if(!(device->Flags&DEVICE_SAMPLE_TYPE_REQUEST))
2589 device->FmtType = formats[i].type;
2590 device->Flags |= DEVICE_FORMAT_REQUEST;
2591 break;
2594 if(i == COUNTOF(formats))
2595 ERR("Unsupported format: %s\n", fmt);
2597 #undef DEVICE_FORMAT_REQUEST
2599 if(ConfigValueUInt(NULL, "frequency", &device->Frequency))
2601 device->Flags |= DEVICE_FREQUENCY_REQUEST;
2602 if(device->Frequency < MIN_OUTPUT_RATE)
2603 ERR("%uhz request clamped to %uhz minimum\n", device->Frequency, MIN_OUTPUT_RATE);
2604 device->Frequency = maxu(device->Frequency, MIN_OUTPUT_RATE);
2607 ConfigValueUInt(NULL, "periods", &device->NumUpdates);
2608 device->NumUpdates = clampu(device->NumUpdates, 2, 16);
2610 ConfigValueUInt(NULL, "period_size", &device->UpdateSize);
2611 device->UpdateSize = clampu(device->UpdateSize, 64, 8192);
2613 ConfigValueUInt(NULL, "sources", &device->MaxNoOfSources);
2614 if(device->MaxNoOfSources == 0) device->MaxNoOfSources = 256;
2616 ConfigValueUInt(NULL, "slots", &device->AuxiliaryEffectSlotMax);
2617 if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
2619 ConfigValueUInt(NULL, "sends", &device->NumAuxSends);
2620 if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
2622 ConfigValueInt(NULL, "cf_level", &device->Bs2bLevel);
2624 device->NumStereoSources = 1;
2625 device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
2627 if(DefaultEffect.type != AL_EFFECT_NULL)
2629 device->DefaultSlot = (ALeffectslot*)(device+1);
2630 if(InitEffectSlot(device->DefaultSlot) != AL_NO_ERROR ||
2631 InitializeEffect(device, device->DefaultSlot, &DefaultEffect) != AL_NO_ERROR)
2633 device->DefaultSlot = NULL;
2634 ERR("Failed to initialize the default effect\n");
2638 // Find a playback device to open
2639 LockLists();
2640 if((err=ALCdevice_OpenPlayback(device, deviceName)) != ALC_NO_ERROR)
2642 UnlockLists();
2643 DeleteCriticalSection(&device->Mutex);
2644 free(device);
2645 alcSetError(NULL, err);
2646 return NULL;
2648 UnlockLists();
2650 do {
2651 device->next = DeviceList;
2652 } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
2654 TRACE("Created device %p, \"%s\"\n", device, device->szDeviceName);
2655 return device;
2658 /* alcCloseDevice
2660 * Close the specified Device
2662 ALC_API ALCboolean ALC_APIENTRY alcCloseDevice(ALCdevice *pDevice)
2664 ALCdevice *volatile*list;
2665 ALCcontext *ctx;
2667 LockLists();
2668 list = &DeviceList;
2669 while(*list && *list != pDevice)
2670 list = &(*list)->next;
2672 if(!*list || (*list)->Type == Capture)
2674 alcSetError(*list, ALC_INVALID_DEVICE);
2675 UnlockLists();
2676 return ALC_FALSE;
2679 *list = (*list)->next;
2680 UnlockLists();
2682 while((ctx=pDevice->ContextList) != NULL)
2684 WARN("Releasing context %p\n", ctx);
2685 ReleaseContext(ctx, pDevice);
2687 if((pDevice->Flags&DEVICE_RUNNING))
2688 ALCdevice_StopPlayback(pDevice);
2689 pDevice->Flags &= ~DEVICE_RUNNING;
2691 ALCdevice_ClosePlayback(pDevice);
2693 ALCdevice_DecRef(pDevice);
2695 return ALC_TRUE;
2699 /* alcLoopbackOpenDeviceSOFT
2701 * Open a loopback device, for manual rendering.
2703 ALC_API ALCdevice* ALC_APIENTRY alcLoopbackOpenDeviceSOFT(const ALCchar *deviceName)
2705 ALCdevice *device;
2707 DO_INITCONFIG();
2709 /* Make sure the device name, if specified, is us. */
2710 if(deviceName && strcmp(deviceName, alcDefaultName) != 0)
2712 alcSetError(NULL, ALC_INVALID_VALUE);
2713 return NULL;
2716 device = calloc(1, sizeof(ALCdevice));
2717 if(!device)
2719 alcSetError(NULL, ALC_OUT_OF_MEMORY);
2720 return NULL;
2723 //Validate device
2724 device->Funcs = &BackendLoopback.Funcs;
2725 device->ref = 1;
2726 device->Connected = ALC_TRUE;
2727 device->Type = Loopback;
2728 InitializeCriticalSection(&device->Mutex);
2729 device->LastError = ALC_NO_ERROR;
2731 device->Flags = 0;
2732 device->Bs2b = NULL;
2733 device->Bs2bLevel = 0;
2734 device->szDeviceName = NULL;
2736 device->ContextList = NULL;
2738 device->MaxNoOfSources = 256;
2739 device->AuxiliaryEffectSlotMax = 4;
2740 device->NumAuxSends = MAX_SENDS;
2742 InitUIntMap(&device->BufferMap, ~0);
2743 InitUIntMap(&device->EffectMap, ~0);
2744 InitUIntMap(&device->FilterMap, ~0);
2746 //Set output format
2747 device->NumUpdates = 0;
2748 device->UpdateSize = 0;
2750 device->Frequency = DEFAULT_OUTPUT_RATE;
2751 device->FmtChans = DevFmtChannelsDefault;
2752 device->FmtType = DevFmtTypeDefault;
2754 ConfigValueUInt(NULL, "sources", &device->MaxNoOfSources);
2755 if(device->MaxNoOfSources == 0) device->MaxNoOfSources = 256;
2757 ConfigValueUInt(NULL, "slots", &device->AuxiliaryEffectSlotMax);
2758 if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
2760 ConfigValueUInt(NULL, "sends", &device->NumAuxSends);
2761 if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
2763 device->NumStereoSources = 1;
2764 device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
2766 // Open the "backend"
2767 ALCdevice_OpenPlayback(device, "Loopback");
2768 do {
2769 device->next = DeviceList;
2770 } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
2772 TRACE("Created device %p\n", device);
2773 return device;
2776 /* alcIsRenderFormatSupportedSOFT
2778 * Determines if the loopback device supports the given format for rendering.
2780 ALC_API ALCboolean ALC_APIENTRY alcIsRenderFormatSupportedSOFT(ALCdevice *device, ALCsizei freq, ALCenum channels, ALCenum type)
2782 ALCboolean ret = ALC_FALSE;
2784 if(!(device=VerifyDevice(device)) || device->Type != Loopback)
2785 alcSetError(device, ALC_INVALID_DEVICE);
2786 else if(freq <= 0)
2787 alcSetError(device, ALC_INVALID_VALUE);
2788 else if(!IsValidALCType(type) || !IsValidALCChannels(channels))
2789 alcSetError(device, ALC_INVALID_ENUM);
2790 else
2792 if(BytesFromDevFmt(type) > 0 && ChannelsFromDevFmt(channels) > 0 &&
2793 freq >= MIN_OUTPUT_RATE)
2794 ret = ALC_TRUE;
2796 if(device) ALCdevice_DecRef(device);
2798 return ret;
2801 /* alcRenderSamplesSOFT
2803 * Renders some samples into a buffer, using the format last set by the
2804 * attributes given to alcCreateContext.
2806 ALC_API void ALC_APIENTRY alcRenderSamplesSOFT(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
2808 if(!(device=VerifyDevice(device)) || device->Type != Loopback)
2809 alcSetError(device, ALC_INVALID_DEVICE);
2810 else if(samples < 0 || (samples > 0 && buffer == NULL))
2811 alcSetError(device, ALC_INVALID_VALUE);
2812 else
2813 aluMixData(device, buffer, samples);
2814 if(device) ALCdevice_DecRef(device);
2818 static void ReleaseALC(void)
2820 ALCdevice *dev;
2822 free(alcAllDeviceList); alcAllDeviceList = NULL;
2823 alcAllDeviceListSize = 0;
2824 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
2825 alcCaptureDeviceListSize = 0;
2827 free(alcDefaultAllDeviceSpecifier);
2828 alcDefaultAllDeviceSpecifier = NULL;
2829 free(alcCaptureDefaultDeviceSpecifier);
2830 alcCaptureDefaultDeviceSpecifier = NULL;
2832 if((dev=ExchangePtr((XchgPtr*)&DeviceList, NULL)) != NULL)
2834 ALCuint num = 0;
2835 do {
2836 num++;
2837 } while((dev=dev->next) != NULL);
2838 ERR("%u device%s not closed\n", num, (num>1)?"s":"");
2842 ///////////////////////////////////////////////////////