Get the mmdevapi device period after initialization
[openal-soft.git] / Alc / ALc.c
blobb399b6920d9fc225635e1565610c3178720c55ca
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 }
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 ALCchar *alcDeviceList;
331 static size_t alcDeviceListSize;
332 static ALCchar *alcAllDeviceList;
333 static size_t alcAllDeviceListSize;
334 static ALCchar *alcCaptureDeviceList;
335 static size_t alcCaptureDeviceListSize;
336 /* Default is always the first in the list */
337 static ALCchar *alcDefaultDeviceSpecifier;
338 static ALCchar *alcDefaultAllDeviceSpecifier;
339 static ALCchar *alcCaptureDefaultDeviceSpecifier;
342 static const ALCchar alcNoDeviceExtList[] =
343 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
344 "ALC_EXT_thread_local_context ALC_SOFTX_loopback_device";
345 static const ALCchar alcExtensionList[] =
346 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
347 "ALC_EXT_DEDICATED ALC_EXT_disconnect ALC_EXT_EFX "
348 "ALC_EXT_thread_local_context ALC_SOFTX_loopback_device";
349 static const ALCint alcMajorVersion = 1;
350 static const ALCint alcMinorVersion = 1;
352 static const ALCint alcEFXMajorVersion = 1;
353 static const ALCint alcEFXMinorVersion = 0;
355 ///////////////////////////////////////////////////////
358 ///////////////////////////////////////////////////////
359 // Global Variables
361 static CRITICAL_SECTION ListLock;
363 /* Device List */
364 static ALCdevice *volatile DeviceList = NULL;
366 // Thread-local current context
367 static pthread_key_t LocalContext;
368 // Process-wide current context
369 static ALCcontext *GlobalContext;
371 /* Device Error */
372 static volatile ALCenum g_eLastNullDeviceError = ALC_NO_ERROR;
374 // Default context extensions
375 static const ALchar alExtList[] =
376 "AL_EXT_ALAW AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 "
377 "AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_MULAW "
378 "AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET AL_EXT_source_distance_model "
379 "AL_LOKI_quadriphonic AL_SOFT_buffer_samples AL_SOFT_buffer_sub_data "
380 "AL_SOFTX_deferred_updates AL_SOFTX_direct_channels AL_SOFT_loop_points";
382 // Mixing Priority Level
383 ALint RTPrioLevel;
385 // Output Log File
386 FILE *LogFile;
388 // Output Log Level
389 #ifdef _DEBUG
390 enum LogLevel LogLevel = LogWarning;
391 #else
392 enum LogLevel LogLevel = LogError;
393 #endif
395 /* Flag to trap ALC device errors */
396 static ALCboolean TrapALCError = ALC_FALSE;
398 /* One-time configuration init control */
399 static pthread_once_t alc_config_once = PTHREAD_ONCE_INIT;
401 /* Forced effect that applies to sources that don't have an effect on send 0 */
402 static ALeffect ForcedEffect;
404 ///////////////////////////////////////////////////////
407 ///////////////////////////////////////////////////////
408 // ALC Related helper functions
409 static void ReleaseALC(void);
410 static void ReleaseThreadCtx(void *ptr);
412 static void alc_initconfig(void);
413 #define DO_INITCONFIG() pthread_once(&alc_config_once, alc_initconfig)
415 #if defined(_WIN32)
416 static void alc_init(void);
417 static void alc_deinit(void);
418 static void alc_deinit_safe(void);
420 UIntMap TlsDestructor;
422 #ifndef AL_LIBTYPE_STATIC
423 BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
425 ALsizei i;
426 (void)hModule;
428 // Perform actions based on the reason for calling.
429 switch(ul_reason_for_call)
431 case DLL_PROCESS_ATTACH:
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(ConfigValueStr(NULL, "drivers", &devs))
621 int n;
622 size_t len;
623 const char *next = devs;
624 int endlist, delitem;
626 i = 0;
627 do {
628 devs = next;
629 next = strchr(devs, ',');
631 delitem = (devs[0] == '-');
632 if(devs[0] == '-') devs++;
634 if(!devs[0] || devs[0] == ',')
636 endlist = 0;
637 continue;
639 endlist = 1;
641 len = (next ? ((size_t)(next-devs)) : strlen(devs));
642 for(n = i;BackendList[n].Init;n++)
644 if(len == strlen(BackendList[n].name) &&
645 strncmp(BackendList[n].name, devs, len) == 0)
647 if(delitem)
649 do {
650 BackendList[n] = BackendList[n+1];
651 ++n;
652 } while(BackendList[n].Init);
654 else
656 struct BackendInfo Bkp = BackendList[n];
657 while(n > i)
659 BackendList[n] = BackendList[n-1];
660 --n;
662 BackendList[n] = Bkp;
664 i++;
666 break;
669 } while(next++);
671 if(endlist)
673 BackendList[i].name = NULL;
674 BackendList[i].Init = NULL;
675 BackendList[i].Deinit = NULL;
676 BackendList[i].Probe = NULL;
680 for(i = 0;BackendList[i].Init && (!PlaybackBackend.name || !CaptureBackend.name);i++)
682 if(!BackendList[i].Init(&BackendList[i].Funcs))
684 WARN("Failed to initialize backend \"%s\"\n", BackendList[i].name);
685 continue;
688 TRACE("Initialized backend \"%s\"\n", BackendList[i].name);
689 if(BackendList[i].Funcs.OpenPlayback && !PlaybackBackend.name)
691 PlaybackBackend = BackendList[i];
692 TRACE("Added \"%s\" for playback\n", PlaybackBackend.name);
694 if(BackendList[i].Funcs.OpenCapture && !CaptureBackend.name)
696 CaptureBackend = BackendList[i];
697 TRACE("Added \"%s\" for capture\n", CaptureBackend.name);
700 BackendLoopback.Init(&BackendLoopback.Funcs);
702 if(ConfigValueStr(NULL, "excludefx", &str))
704 size_t len;
705 const char *next = str;
707 do {
708 str = next;
709 next = strchr(str, ',');
711 if(!str[0] || next == str)
712 continue;
714 len = (next ? ((size_t)(next-str)) : strlen(str));
715 for(n = 0;EffectList[n].name;n++)
717 if(len == strlen(EffectList[n].name) &&
718 strncmp(EffectList[n].name, str, len) == 0)
719 DisabledEffects[EffectList[n].type] = AL_TRUE;
721 } while(next++);
724 InitEffect(&ForcedEffect);
725 str = getenv("__ALSOFT_DEFAULT_REVERB");
726 if(str && str[0])
727 GetReverbEffect(str, &ForcedEffect);
728 else if(ConfigValueStr(NULL, "default-reverb", &str))
729 GetReverbEffect(str, &ForcedEffect);
733 static void LockLists(void)
735 EnterCriticalSection(&ListLock);
738 static void UnlockLists(void)
740 LeaveCriticalSection(&ListLock);
744 static void ProbeList(ALCchar **list, size_t *listsize, enum DevProbe type)
746 DO_INITCONFIG();
748 LockLists();
749 free(*list);
750 *list = NULL;
751 *listsize = 0;
753 if(type == CAPTURE_DEVICE_PROBE)
754 CaptureBackend.Probe(type);
755 else
756 PlaybackBackend.Probe(type);
757 UnlockLists();
760 static void ProbeDeviceList(void)
761 { ProbeList(&alcDeviceList, &alcDeviceListSize, DEVICE_PROBE); }
762 static void ProbeAllDeviceList(void)
763 { ProbeList(&alcAllDeviceList, &alcAllDeviceListSize, ALL_DEVICE_PROBE); }
764 static void ProbeCaptureDeviceList(void)
765 { ProbeList(&alcCaptureDeviceList, &alcCaptureDeviceListSize, CAPTURE_DEVICE_PROBE); }
768 static void AppendList(const ALCchar *name, ALCchar **List, size_t *ListSize)
770 size_t len = strlen(name);
771 void *temp;
773 if(len == 0)
774 return;
776 temp = realloc(*List, (*ListSize) + len + 2);
777 if(!temp)
779 ERR("Realloc failed to add %s!\n", name);
780 return;
782 *List = temp;
784 memcpy((*List)+(*ListSize), name, len+1);
785 *ListSize += len+1;
786 (*List)[*ListSize] = 0;
789 #define DECL_APPEND_LIST_FUNC(type) \
790 void Append##type##List(const ALCchar *name) \
791 { AppendList(name, &alc##type##List, &alc##type##ListSize); }
793 DECL_APPEND_LIST_FUNC(Device)
794 DECL_APPEND_LIST_FUNC(AllDevice)
795 DECL_APPEND_LIST_FUNC(CaptureDevice)
797 #undef DECL_APPEND_LIST_FUNC
800 /* Sets the default channel order used by most non-WaveFormatEx-based APIs */
801 void SetDefaultChannelOrder(ALCdevice *device)
803 switch(device->FmtChans)
805 case DevFmtX51: device->DevChannels[0] = FRONT_LEFT;
806 device->DevChannels[1] = FRONT_RIGHT;
807 device->DevChannels[2] = BACK_LEFT;
808 device->DevChannels[3] = BACK_RIGHT;
809 device->DevChannels[4] = FRONT_CENTER;
810 device->DevChannels[5] = LFE;
811 return;
813 case DevFmtX71: device->DevChannels[0] = FRONT_LEFT;
814 device->DevChannels[1] = FRONT_RIGHT;
815 device->DevChannels[2] = BACK_LEFT;
816 device->DevChannels[3] = BACK_RIGHT;
817 device->DevChannels[4] = FRONT_CENTER;
818 device->DevChannels[5] = LFE;
819 device->DevChannels[6] = SIDE_LEFT;
820 device->DevChannels[7] = SIDE_RIGHT;
821 return;
823 /* Same as WFX order */
824 case DevFmtMono:
825 case DevFmtStereo:
826 case DevFmtQuad:
827 case DevFmtX51Side:
828 case DevFmtX61:
829 break;
831 SetDefaultWFXChannelOrder(device);
833 /* Sets the default order used by WaveFormatEx */
834 void SetDefaultWFXChannelOrder(ALCdevice *device)
836 switch(device->FmtChans)
838 case DevFmtMono: device->DevChannels[0] = FRONT_CENTER; break;
840 case DevFmtStereo: device->DevChannels[0] = FRONT_LEFT;
841 device->DevChannels[1] = FRONT_RIGHT; break;
843 case DevFmtQuad: device->DevChannels[0] = FRONT_LEFT;
844 device->DevChannels[1] = FRONT_RIGHT;
845 device->DevChannels[2] = BACK_LEFT;
846 device->DevChannels[3] = BACK_RIGHT; break;
848 case DevFmtX51: device->DevChannels[0] = FRONT_LEFT;
849 device->DevChannels[1] = FRONT_RIGHT;
850 device->DevChannels[2] = FRONT_CENTER;
851 device->DevChannels[3] = LFE;
852 device->DevChannels[4] = BACK_LEFT;
853 device->DevChannels[5] = BACK_RIGHT; break;
855 case DevFmtX51Side: device->DevChannels[0] = FRONT_LEFT;
856 device->DevChannels[1] = FRONT_RIGHT;
857 device->DevChannels[2] = FRONT_CENTER;
858 device->DevChannels[3] = LFE;
859 device->DevChannels[4] = SIDE_LEFT;
860 device->DevChannels[5] = SIDE_RIGHT; break;
862 case DevFmtX61: device->DevChannels[0] = FRONT_LEFT;
863 device->DevChannels[1] = FRONT_RIGHT;
864 device->DevChannels[2] = FRONT_CENTER;
865 device->DevChannels[3] = LFE;
866 device->DevChannels[4] = BACK_CENTER;
867 device->DevChannels[5] = SIDE_LEFT;
868 device->DevChannels[6] = SIDE_RIGHT; break;
870 case DevFmtX71: device->DevChannels[0] = FRONT_LEFT;
871 device->DevChannels[1] = FRONT_RIGHT;
872 device->DevChannels[2] = FRONT_CENTER;
873 device->DevChannels[3] = LFE;
874 device->DevChannels[4] = BACK_LEFT;
875 device->DevChannels[5] = BACK_RIGHT;
876 device->DevChannels[6] = SIDE_LEFT;
877 device->DevChannels[7] = SIDE_RIGHT; break;
882 const ALCchar *DevFmtTypeString(enum DevFmtType type)
884 switch(type)
886 case DevFmtByte: return "Signed Byte";
887 case DevFmtUByte: return "Unsigned Byte";
888 case DevFmtShort: return "Signed Short";
889 case DevFmtUShort: return "Unsigned Short";
890 case DevFmtInt: return "Signed Int";
891 case DevFmtUInt: return "Unsigned Int";
892 case DevFmtFloat: return "Float";
894 return "(unknown type)";
896 const ALCchar *DevFmtChannelsString(enum DevFmtChannels chans)
898 switch(chans)
900 case DevFmtMono: return "Mono";
901 case DevFmtStereo: return "Stereo";
902 case DevFmtQuad: return "Quadraphonic";
903 case DevFmtX51: return "5.1 Surround";
904 case DevFmtX51Side: return "5.1 Side";
905 case DevFmtX61: return "6.1 Surround";
906 case DevFmtX71: return "7.1 Surround";
908 return "(unknown channels)";
911 ALuint BytesFromDevFmt(enum DevFmtType type)
913 switch(type)
915 case DevFmtByte: return sizeof(ALbyte);
916 case DevFmtUByte: return sizeof(ALubyte);
917 case DevFmtShort: return sizeof(ALshort);
918 case DevFmtUShort: return sizeof(ALushort);
919 case DevFmtInt: return sizeof(ALint);
920 case DevFmtUInt: return sizeof(ALuint);
921 case DevFmtFloat: return sizeof(ALfloat);
923 return 0;
925 ALuint ChannelsFromDevFmt(enum DevFmtChannels chans)
927 switch(chans)
929 case DevFmtMono: return 1;
930 case DevFmtStereo: return 2;
931 case DevFmtQuad: return 4;
932 case DevFmtX51: return 6;
933 case DevFmtX51Side: return 6;
934 case DevFmtX61: return 7;
935 case DevFmtX71: return 8;
937 return 0;
939 static ALboolean DecomposeDevFormat(ALenum format, enum DevFmtChannels *chans,
940 enum DevFmtType *type)
942 static const struct {
943 ALenum format;
944 enum DevFmtChannels channels;
945 enum DevFmtType type;
946 } list[] = {
947 { AL_FORMAT_MONO8, DevFmtMono, DevFmtUByte },
948 { AL_FORMAT_MONO16, DevFmtMono, DevFmtShort },
949 { AL_FORMAT_MONO_FLOAT32, DevFmtMono, DevFmtFloat },
951 { AL_FORMAT_STEREO8, DevFmtStereo, DevFmtUByte },
952 { AL_FORMAT_STEREO16, DevFmtStereo, DevFmtShort },
953 { AL_FORMAT_STEREO_FLOAT32, DevFmtStereo, DevFmtFloat },
955 { AL_FORMAT_QUAD8, DevFmtQuad, DevFmtUByte },
956 { AL_FORMAT_QUAD16, DevFmtQuad, DevFmtShort },
957 { AL_FORMAT_QUAD32, DevFmtQuad, DevFmtFloat },
959 { AL_FORMAT_51CHN8, DevFmtX51, DevFmtUByte },
960 { AL_FORMAT_51CHN16, DevFmtX51, DevFmtShort },
961 { AL_FORMAT_51CHN32, DevFmtX51, DevFmtFloat },
963 { AL_FORMAT_61CHN8, DevFmtX61, DevFmtUByte },
964 { AL_FORMAT_61CHN16, DevFmtX61, DevFmtShort },
965 { AL_FORMAT_61CHN32, DevFmtX61, DevFmtFloat },
967 { AL_FORMAT_71CHN8, DevFmtX71, DevFmtUByte },
968 { AL_FORMAT_71CHN16, DevFmtX71, DevFmtShort },
969 { AL_FORMAT_71CHN32, DevFmtX71, DevFmtFloat },
971 ALuint i;
973 for(i = 0;i < sizeof(list)/sizeof(list[0]);i++)
975 if(list[i].format == format)
977 *chans = list[i].channels;
978 *type = list[i].type;
979 return AL_TRUE;
983 return AL_FALSE;
986 static ALCboolean IsValidALCType(ALCenum type)
988 switch(type)
990 case ALC_BYTE_SOFT:
991 case ALC_UNSIGNED_BYTE_SOFT:
992 case ALC_SHORT_SOFT:
993 case ALC_UNSIGNED_SHORT_SOFT:
994 case ALC_INT_SOFT:
995 case ALC_UNSIGNED_INT_SOFT:
996 case ALC_FLOAT_SOFT:
997 return ALC_TRUE;
999 return ALC_FALSE;
1002 static ALCboolean IsValidALCChannels(ALCenum channels)
1004 switch(channels)
1006 case ALC_MONO_SOFT:
1007 case ALC_STEREO_SOFT:
1008 case ALC_QUAD_SOFT:
1009 case ALC_5POINT1_SOFT:
1010 case ALC_6POINT1_SOFT:
1011 case ALC_7POINT1_SOFT:
1012 return ALC_TRUE;
1014 return ALC_FALSE;
1018 /* alcSetError
1020 * Stores the latest ALC Error
1022 static void alcSetError(ALCdevice *device, ALCenum errorCode)
1024 if(TrapALCError)
1026 #ifdef _WIN32
1027 /* DebugBreak() will cause an exception if there is no debugger */
1028 if(IsDebuggerPresent())
1029 DebugBreak();
1030 #elif defined(SIGTRAP)
1031 raise(SIGTRAP);
1032 #endif
1035 if(device)
1036 device->LastError = errorCode;
1037 else
1038 g_eLastNullDeviceError = errorCode;
1042 /* UpdateDeviceParams
1044 * Updates device parameters according to the attribute list (caller is
1045 * responsible for holding the list lock).
1047 static ALCenum UpdateDeviceParams(ALCdevice *device, const ALCint *attrList)
1049 ALCcontext *context;
1050 enum DevFmtChannels oldChans;
1051 enum DevFmtType oldType;
1052 ALCuint oldFreq;
1053 int oldMode;
1054 ALuint i;
1056 if(device->IsLoopbackDevice && !(attrList && attrList[0]))
1058 WARN("Missing attributes for loopback device\n");
1059 return ALC_INVALID_VALUE;
1062 // Check for attributes
1063 if(attrList && attrList[0])
1065 enum {
1066 GotFreq = 1<<0,
1067 GotChans = 1<<1,
1068 GotType = 1<<2,
1069 GotAll = GotFreq|GotChans|GotType
1071 ALCuint freq, numMono, numStereo, numSends;
1072 enum DevFmtChannels schans;
1073 enum DevFmtType stype;
1074 ALCuint attrIdx = 0;
1075 ALCint gotFmt = 0;
1077 // If a context is already running on the device, stop playback so the
1078 // device attributes can be updated
1079 if((device->Flags&DEVICE_RUNNING))
1080 ALCdevice_StopPlayback(device);
1081 device->Flags &= ~DEVICE_RUNNING;
1083 freq = device->Frequency;
1084 schans = device->FmtChans;
1085 stype = device->FmtType;
1086 numMono = device->NumMonoSources;
1087 numStereo = device->NumStereoSources;
1088 numSends = device->NumAuxSends;
1090 while(attrList[attrIdx])
1092 if(attrList[attrIdx] == ALC_FORMAT_CHANNELS_SOFT &&
1093 device->IsLoopbackDevice)
1095 ALCint val = attrList[attrIdx + 1];
1096 if(!IsValidALCChannels(val) || !ChannelsFromDevFmt(val))
1097 return ALC_INVALID_VALUE;
1098 schans = val;
1099 gotFmt |= GotChans;
1102 if(attrList[attrIdx] == ALC_FORMAT_TYPE_SOFT &&
1103 device->IsLoopbackDevice)
1105 ALCint val = attrList[attrIdx + 1];
1106 if(!IsValidALCType(val) || !BytesFromDevFmt(val))
1107 return ALC_INVALID_VALUE;
1108 stype = val;
1109 gotFmt |= GotType;
1112 if(attrList[attrIdx] == ALC_FREQUENCY)
1114 if(device->IsLoopbackDevice)
1116 freq = attrList[attrIdx + 1];
1117 if(freq < MIN_OUTPUT_RATE)
1118 return ALC_INVALID_VALUE;
1119 gotFmt |= GotFreq;
1121 else
1123 freq = attrList[attrIdx + 1];
1124 device->Flags |= DEVICE_FREQUENCY_REQUEST;
1128 if(attrList[attrIdx] == ALC_STEREO_SOURCES)
1130 numStereo = attrList[attrIdx + 1];
1131 if(numStereo > device->MaxNoOfSources)
1132 numStereo = device->MaxNoOfSources;
1134 numMono = device->MaxNoOfSources - numStereo;
1137 if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS)
1138 numSends = attrList[attrIdx + 1];
1140 attrIdx += 2;
1143 if(device->IsLoopbackDevice)
1145 if(gotFmt != GotAll)
1147 WARN("Missing format for loopback device\n");
1148 return ALC_INVALID_VALUE;
1151 else
1153 ConfigValueUInt(NULL, "frequency", &freq);
1154 freq = maxu(freq, MIN_OUTPUT_RATE);
1156 ConfigValueUInt(NULL, "sends", &numSends);
1157 numSends = minu(MAX_SENDS, numSends);
1159 device->UpdateSize = (ALuint64)device->UpdateSize * freq /
1160 device->Frequency;
1162 device->Frequency = freq;
1163 device->FmtChans = schans;
1164 device->FmtType = stype;
1165 device->NumMonoSources = numMono;
1166 device->NumStereoSources = numStereo;
1167 device->NumAuxSends = numSends;
1170 if((device->Flags&DEVICE_RUNNING))
1171 return ALC_NO_ERROR;
1173 LockDevice(device);
1175 oldFreq = device->Frequency;
1176 oldChans = device->FmtChans;
1177 oldType = device->FmtType;
1179 TRACE("Format pre-setup: %s%s, %s%s, %uhz%s, %u update size x%d\n",
1180 DevFmtChannelsString(device->FmtChans),
1181 (device->Flags&DEVICE_CHANNELS_REQUEST)?" (requested)":"",
1182 DevFmtTypeString(device->FmtType),
1183 (device->Flags&DEVICE_SAMPLE_TYPE_REQUEST)?" (requested)":"",
1184 device->Frequency,
1185 (device->Flags&DEVICE_FREQUENCY_REQUEST)?" (requested)":"",
1186 device->UpdateSize, device->NumUpdates);
1187 if(ALCdevice_ResetPlayback(device) == ALC_FALSE)
1189 UnlockDevice(device);
1190 return ALC_INVALID_DEVICE;
1192 device->Flags |= DEVICE_RUNNING;
1194 if(device->FmtChans != oldChans && (device->Flags&DEVICE_CHANNELS_REQUEST))
1196 ERR("Failed to set %s, got %s instead\n", DevFmtChannelsString(oldChans),
1197 DevFmtChannelsString(device->FmtChans));
1198 device->Flags &= ~DEVICE_CHANNELS_REQUEST;
1200 if(device->FmtType != oldType && (device->Flags&DEVICE_SAMPLE_TYPE_REQUEST))
1202 ERR("Failed to set %s, got %s instead\n", DevFmtTypeString(oldType),
1203 DevFmtTypeString(device->FmtType));
1204 device->Flags &= ~DEVICE_SAMPLE_TYPE_REQUEST;
1206 if(device->Frequency != oldFreq && (device->Flags&DEVICE_FREQUENCY_REQUEST))
1208 ERR("Failed to set %uhz, got %uhz instead\n", oldFreq, device->Frequency);
1209 device->Flags &= ~DEVICE_FREQUENCY_REQUEST;
1212 TRACE("Format post-setup: %s, %s, %uhz, %u update size x%d\n",
1213 DevFmtChannelsString(device->FmtChans),
1214 DevFmtTypeString(device->FmtType), device->Frequency,
1215 device->UpdateSize, device->NumUpdates);
1217 aluInitPanning(device);
1219 for(i = 0;i < MAXCHANNELS;i++)
1221 device->ClickRemoval[i] = 0.0f;
1222 device->PendingClicks[i] = 0.0f;
1225 device->Hrtf = NULL;
1226 if(!device->IsLoopbackDevice && GetConfigValueBool(NULL, "hrtf", AL_FALSE))
1227 device->Hrtf = GetHrtf(device);
1228 TRACE("HRTF %s\n", device->Hrtf?"enabled":"disabled");
1230 if(!device->Hrtf && device->Bs2bLevel > 0 && device->Bs2bLevel <= 6)
1232 if(!device->Bs2b)
1234 device->Bs2b = calloc(1, sizeof(*device->Bs2b));
1235 bs2b_clear(device->Bs2b);
1237 bs2b_set_srate(device->Bs2b, device->Frequency);
1238 bs2b_set_level(device->Bs2b, device->Bs2bLevel);
1239 TRACE("BS2B level %d\n", device->Bs2bLevel);
1241 else
1243 free(device->Bs2b);
1244 device->Bs2b = NULL;
1245 TRACE("BS2B disabled\n");
1248 device->Flags &= ~DEVICE_DUPLICATE_STEREO;
1249 switch(device->FmtChans)
1251 case DevFmtMono:
1252 case DevFmtStereo:
1253 break;
1254 case DevFmtQuad:
1255 case DevFmtX51:
1256 case DevFmtX51Side:
1257 case DevFmtX61:
1258 case DevFmtX71:
1259 if(GetConfigValueBool(NULL, "stereodup", AL_TRUE))
1260 device->Flags |= DEVICE_DUPLICATE_STEREO;
1261 break;
1263 TRACE("Stereo duplication %s\n", (device->Flags&DEVICE_DUPLICATE_STEREO)?"enabled":"disabled");
1265 oldMode = SetMixerFPUMode();
1266 context = device->ContextList;
1267 while(context)
1269 ALsizei pos;
1271 context->UpdateSources = AL_FALSE;
1272 LockUIntMapRead(&context->EffectSlotMap);
1273 for(pos = 0;pos < context->EffectSlotMap.size;pos++)
1275 ALeffectslot *slot = context->EffectSlotMap.array[pos].value;
1277 if(ALeffectState_DeviceUpdate(slot->EffectState, device) == AL_FALSE)
1279 UnlockUIntMapRead(&context->EffectSlotMap);
1280 RestoreFPUMode(oldMode);
1281 UnlockDevice(device);
1282 ALCdevice_StopPlayback(device);
1283 device->Flags &= ~DEVICE_RUNNING;
1284 return ALC_INVALID_DEVICE;
1286 slot->NeedsUpdate = AL_FALSE;
1287 ALeffectState_Update(slot->EffectState, context, slot);
1289 UnlockUIntMapRead(&context->EffectSlotMap);
1291 LockUIntMapRead(&context->SourceMap);
1292 for(pos = 0;pos < context->SourceMap.size;pos++)
1294 ALsource *source = context->SourceMap.array[pos].value;
1295 ALuint s = device->NumAuxSends;
1296 while(s < MAX_SENDS)
1298 if(source->Send[s].Slot)
1299 DecrementRef(&source->Send[s].Slot->ref);
1300 source->Send[s].Slot = NULL;
1301 source->Send[s].WetGain = 1.0f;
1302 source->Send[s].WetGainHF = 1.0f;
1303 s++;
1305 source->NeedsUpdate = AL_FALSE;
1306 ALsource_Update(source, context);
1308 UnlockUIntMapRead(&context->SourceMap);
1310 context = context->next;
1312 RestoreFPUMode(oldMode);
1313 UnlockDevice(device);
1315 return ALC_NO_ERROR;
1318 /* FreeDevice
1320 * Frees the device structure, and destroys any objects the app failed to
1321 * delete. Called once there's no more references on the device.
1323 static ALCvoid FreeDevice(ALCdevice *device)
1325 TRACE("%p\n", device);
1327 if(device->DefaultSlot)
1329 ALeffectState_Destroy(device->DefaultSlot->EffectState);
1330 device->DefaultSlot->EffectState = NULL;
1333 if(device->BufferMap.size > 0)
1335 WARN("(%p) Deleting %d Buffer(s)\n", device, device->BufferMap.size);
1336 ReleaseALBuffers(device);
1338 ResetUIntMap(&device->BufferMap);
1340 if(device->EffectMap.size > 0)
1342 WARN("(%p) Deleting %d Effect(s)\n", device, device->EffectMap.size);
1343 ReleaseALEffects(device);
1345 ResetUIntMap(&device->EffectMap);
1347 if(device->FilterMap.size > 0)
1349 WARN("(%p) Deleting %d Filter(s)\n", device, device->FilterMap.size);
1350 ReleaseALFilters(device);
1352 ResetUIntMap(&device->FilterMap);
1354 free(device->Bs2b);
1355 device->Bs2b = NULL;
1357 free(device->szDeviceName);
1358 device->szDeviceName = NULL;
1360 DeleteCriticalSection(&device->Mutex);
1362 free(device);
1366 void ALCdevice_IncRef(ALCdevice *device)
1368 RefCount ref;
1369 ref = IncrementRef(&device->ref);
1370 TRACEREF("%p increasing refcount to %u\n", device, ref);
1373 void ALCdevice_DecRef(ALCdevice *device)
1375 RefCount ref;
1376 ref = DecrementRef(&device->ref);
1377 TRACEREF("%p decreasing refcount to %u\n", device, ref);
1378 if(ref == 0) FreeDevice(device);
1381 /* VerifyDevice
1383 * Checks if the device handle is valid, and increments its ref count if so.
1385 static ALCdevice *VerifyDevice(ALCdevice *device)
1387 ALCdevice *tmpDevice;
1389 if(!device)
1390 return NULL;
1392 LockLists();
1393 tmpDevice = DeviceList;
1394 while(tmpDevice && tmpDevice != device)
1395 tmpDevice = tmpDevice->next;
1397 if(tmpDevice)
1398 ALCdevice_IncRef(tmpDevice);
1399 UnlockLists();
1400 return tmpDevice;
1404 /* InitContext
1406 * Initializes context variables
1408 static ALvoid InitContext(ALCcontext *pContext)
1410 ALint i, j;
1412 //Initialise listener
1413 pContext->Listener.Gain = 1.0f;
1414 pContext->Listener.MetersPerUnit = 1.0f;
1415 pContext->Listener.Position[0] = 0.0f;
1416 pContext->Listener.Position[1] = 0.0f;
1417 pContext->Listener.Position[2] = 0.0f;
1418 pContext->Listener.Velocity[0] = 0.0f;
1419 pContext->Listener.Velocity[1] = 0.0f;
1420 pContext->Listener.Velocity[2] = 0.0f;
1421 pContext->Listener.Forward[0] = 0.0f;
1422 pContext->Listener.Forward[1] = 0.0f;
1423 pContext->Listener.Forward[2] = -1.0f;
1424 pContext->Listener.Up[0] = 0.0f;
1425 pContext->Listener.Up[1] = 1.0f;
1426 pContext->Listener.Up[2] = 0.0f;
1427 for(i = 0;i < 4;i++)
1429 for(j = 0;j < 4;j++)
1430 pContext->Listener.Matrix[i][j] = ((i==j) ? 1.0f : 0.0f);
1433 //Validate pContext
1434 pContext->LastError = AL_NO_ERROR;
1435 pContext->UpdateSources = AL_FALSE;
1436 pContext->ActiveSourceCount = 0;
1437 InitUIntMap(&pContext->SourceMap, pContext->Device->MaxNoOfSources);
1438 InitUIntMap(&pContext->EffectSlotMap, pContext->Device->AuxiliaryEffectSlotMax);
1440 //Set globals
1441 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
1442 pContext->SourceDistanceModel = AL_FALSE;
1443 pContext->DopplerFactor = 1.0f;
1444 pContext->DopplerVelocity = 1.0f;
1445 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
1446 pContext->DeferUpdates = AL_FALSE;
1448 pContext->ExtensionList = alExtList;
1452 /* FreeContext
1454 * Cleans up the context, and destroys any remaining objects the app failed to
1455 * delete. Called once there's no more references on the context.
1457 static ALCvoid FreeContext(ALCcontext *context)
1459 TRACE("%p\n", context);
1461 if(context->SourceMap.size > 0)
1463 ERR("(%p) Deleting %d Source(s)\n", context, context->SourceMap.size);
1464 ReleaseALSources(context);
1466 ResetUIntMap(&context->SourceMap);
1468 if(context->EffectSlotMap.size > 0)
1470 ERR("(%p) Deleting %d AuxiliaryEffectSlot(s)\n", context, context->EffectSlotMap.size);
1471 ReleaseALAuxiliaryEffectSlots(context);
1473 ResetUIntMap(&context->EffectSlotMap);
1475 context->ActiveSourceCount = 0;
1476 free(context->ActiveSources);
1477 context->ActiveSources = NULL;
1478 context->MaxActiveSources = 0;
1480 context->ActiveEffectSlotCount = 0;
1481 free(context->ActiveEffectSlots);
1482 context->ActiveEffectSlots = NULL;
1483 context->MaxActiveEffectSlots = 0;
1485 ALCdevice_DecRef(context->Device);
1486 context->Device = NULL;
1488 //Invalidate context
1489 memset(context, 0, sizeof(ALCcontext));
1490 free(context);
1493 /* ReleaseContext
1495 * Removes the context reference from the given device and removes it from
1496 * being current on the running thread or globally.
1498 static void ReleaseContext(ALCcontext *context, ALCdevice *device)
1500 ALCcontext *volatile*tmp_ctx;
1502 if(pthread_getspecific(LocalContext) == context)
1504 WARN("%p released while current on thread\n", context);
1505 pthread_setspecific(LocalContext, NULL);
1506 ALCcontext_DecRef(context);
1509 if(CompExchangePtr((XchgPtr*)&GlobalContext, context, NULL))
1510 ALCcontext_DecRef(context);
1512 LockDevice(device);
1513 tmp_ctx = &device->ContextList;
1514 while(*tmp_ctx)
1516 if(CompExchangePtr((XchgPtr*)tmp_ctx, context, context->next))
1517 break;
1518 tmp_ctx = &(*tmp_ctx)->next;
1520 UnlockDevice(device);
1522 ALCcontext_DecRef(context);
1525 void ALCcontext_IncRef(ALCcontext *context)
1527 RefCount ref;
1528 ref = IncrementRef(&context->ref);
1529 TRACEREF("%p increasing refcount to %u\n", context, ref);
1532 void ALCcontext_DecRef(ALCcontext *context)
1534 RefCount ref;
1535 ref = DecrementRef(&context->ref);
1536 TRACEREF("%p decreasing refcount to %u\n", context, ref);
1537 if(ref == 0) FreeContext(context);
1540 static void ReleaseThreadCtx(void *ptr)
1542 WARN("%p current for thread being destroyed\n", ptr);
1543 ALCcontext_DecRef(ptr);
1546 /* VerifyContext
1548 * Checks that the given context is valid, and increments its reference count.
1550 static ALCcontext *VerifyContext(ALCcontext *context)
1552 ALCdevice *dev;
1554 LockLists();
1555 dev = DeviceList;
1556 while(dev)
1558 ALCcontext *tmp_ctx = dev->ContextList;
1559 while(tmp_ctx)
1561 if(tmp_ctx == context)
1563 ALCcontext_IncRef(tmp_ctx);
1564 UnlockLists();
1565 return tmp_ctx;
1567 tmp_ctx = tmp_ctx->next;
1569 dev = dev->next;
1571 UnlockLists();
1573 return NULL;
1577 /* GetContextRef
1579 * Returns the currently active context, and adds a reference without locking
1580 * it.
1582 ALCcontext *GetContextRef(void)
1584 ALCcontext *context;
1586 context = pthread_getspecific(LocalContext);
1587 if(context)
1588 ALCcontext_IncRef(context);
1589 else
1591 LockLists();
1592 context = GlobalContext;
1593 if(context)
1594 ALCcontext_IncRef(context);
1595 UnlockLists();
1598 return context;
1601 ///////////////////////////////////////////////////////
1604 ///////////////////////////////////////////////////////
1605 // ALC Functions calls
1608 // This should probably move to another c file but for now ...
1609 ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
1611 ALCdevice *device = NULL;
1612 ALCenum err;
1614 DO_INITCONFIG();
1616 if(!CaptureBackend.name)
1618 alcSetError(NULL, ALC_INVALID_VALUE);
1619 return NULL;
1622 if(SampleSize <= 0)
1624 alcSetError(NULL, ALC_INVALID_VALUE);
1625 return NULL;
1628 if(deviceName && (!deviceName[0] || strcasecmp(deviceName, "openal soft") == 0 || strcasecmp(deviceName, "openal-soft") == 0))
1629 deviceName = NULL;
1631 device = calloc(1, sizeof(ALCdevice));
1632 if(!device)
1634 alcSetError(NULL, ALC_OUT_OF_MEMORY);
1635 return NULL;
1638 //Validate device
1639 device->Funcs = &CaptureBackend.Funcs;
1640 device->ref = 1;
1641 device->Connected = ALC_TRUE;
1642 device->IsCaptureDevice = AL_TRUE;
1643 device->IsLoopbackDevice = AL_FALSE;
1644 InitializeCriticalSection(&device->Mutex);
1646 InitUIntMap(&device->BufferMap, ~0);
1647 InitUIntMap(&device->EffectMap, ~0);
1648 InitUIntMap(&device->FilterMap, ~0);
1650 device->szDeviceName = NULL;
1652 device->Flags |= DEVICE_FREQUENCY_REQUEST;
1653 device->Frequency = frequency;
1655 device->Flags |= DEVICE_CHANNELS_REQUEST | DEVICE_SAMPLE_TYPE_REQUEST;
1656 if(DecomposeDevFormat(format, &device->FmtChans, &device->FmtType) == AL_FALSE)
1658 DeleteCriticalSection(&device->Mutex);
1659 free(device);
1660 alcSetError(NULL, ALC_INVALID_ENUM);
1661 return NULL;
1664 device->UpdateSize = SampleSize;
1665 device->NumUpdates = 1;
1667 LockLists();
1668 if((err=ALCdevice_OpenCapture(device, deviceName)) != ALC_NO_ERROR)
1670 UnlockLists();
1671 DeleteCriticalSection(&device->Mutex);
1672 free(device);
1673 alcSetError(NULL, err);
1674 return NULL;
1676 UnlockLists();
1678 do {
1679 device->next = DeviceList;
1680 } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
1682 TRACE("Created device %p\n", device);
1683 return device;
1686 ALC_API ALCboolean ALC_APIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
1688 ALCdevice *volatile*list;
1690 LockLists();
1691 list = &DeviceList;
1692 while(*list && *list != pDevice)
1693 list = &(*list)->next;
1695 if(!*list || !(*list)->IsCaptureDevice)
1697 alcSetError(*list, ALC_INVALID_DEVICE);
1698 UnlockLists();
1699 return ALC_FALSE;
1702 *list = (*list)->next;
1703 UnlockLists();
1705 LockDevice(pDevice);
1706 ALCdevice_CloseCapture(pDevice);
1707 UnlockDevice(pDevice);
1709 ALCdevice_DecRef(pDevice);
1711 return ALC_TRUE;
1714 ALC_API void ALC_APIENTRY alcCaptureStart(ALCdevice *device)
1716 if(!(device=VerifyDevice(device)) || !device->IsCaptureDevice)
1718 alcSetError(device, ALC_INVALID_DEVICE);
1719 if(device) ALCdevice_DecRef(device);
1720 return;
1722 LockDevice(device);
1723 if(device->Connected)
1724 ALCdevice_StartCapture(device);
1725 UnlockDevice(device);
1727 ALCdevice_DecRef(device);
1730 ALC_API void ALC_APIENTRY alcCaptureStop(ALCdevice *device)
1732 if(!(device=VerifyDevice(device)) || !device->IsCaptureDevice)
1734 alcSetError(device, ALC_INVALID_DEVICE);
1735 if(device) ALCdevice_DecRef(device);
1736 return;
1738 LockDevice(device);
1739 if(device->Connected)
1740 ALCdevice_StopCapture(device);
1741 UnlockDevice(device);
1743 ALCdevice_DecRef(device);
1746 ALC_API void ALC_APIENTRY alcCaptureSamples(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
1748 ALCenum err = ALC_INVALID_DEVICE;
1749 if((device=VerifyDevice(device)) != NULL && device->IsCaptureDevice)
1751 err = ALC_INVALID_VALUE;
1752 LockDevice(device);
1753 if(samples >= 0 && ALCdevice_AvailableSamples(device) >= (ALCuint)samples)
1754 err = ALCdevice_CaptureSamples(device, buffer, samples);
1755 UnlockDevice(device);
1757 if(err != ALC_NO_ERROR)
1758 alcSetError(device, err);
1759 if(device) ALCdevice_DecRef(device);
1763 alcGetError
1765 Return last ALC generated error code
1767 ALC_API ALCenum ALC_APIENTRY alcGetError(ALCdevice *device)
1769 ALCenum errorCode;
1771 if(VerifyDevice(device))
1773 errorCode = ExchangeInt(&device->LastError, ALC_NO_ERROR);
1774 ALCdevice_DecRef(device);
1776 else
1777 errorCode = ExchangeInt(&g_eLastNullDeviceError, ALC_NO_ERROR);
1779 return errorCode;
1783 /* alcSuspendContext
1785 * Not functional
1787 ALC_API ALCvoid ALC_APIENTRY alcSuspendContext(ALCcontext *Context)
1789 (void)Context;
1792 /* alcProcessContext
1794 * Not functional
1796 ALC_API ALCvoid ALC_APIENTRY alcProcessContext(ALCcontext *Context)
1798 (void)Context;
1802 /* alcGetString
1804 * Returns information about the Device, and error strings
1806 ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
1808 const ALCchar *value = NULL;
1810 switch(param)
1812 case ALC_NO_ERROR:
1813 value = alcNoError;
1814 break;
1816 case ALC_INVALID_ENUM:
1817 value = alcErrInvalidEnum;
1818 break;
1820 case ALC_INVALID_VALUE:
1821 value = alcErrInvalidValue;
1822 break;
1824 case ALC_INVALID_DEVICE:
1825 value = alcErrInvalidDevice;
1826 break;
1828 case ALC_INVALID_CONTEXT:
1829 value = alcErrInvalidContext;
1830 break;
1832 case ALC_OUT_OF_MEMORY:
1833 value = alcErrOutOfMemory;
1834 break;
1836 case ALC_DEVICE_SPECIFIER:
1837 if(VerifyDevice(pDevice))
1839 value = pDevice->szDeviceName;
1840 ALCdevice_DecRef(pDevice);
1842 else
1844 ProbeDeviceList();
1845 value = alcDeviceList;
1847 break;
1849 case ALC_ALL_DEVICES_SPECIFIER:
1850 ProbeAllDeviceList();
1851 value = alcAllDeviceList;
1852 break;
1854 case ALC_CAPTURE_DEVICE_SPECIFIER:
1855 if(VerifyDevice(pDevice))
1857 value = pDevice->szDeviceName;
1858 ALCdevice_DecRef(pDevice);
1860 else
1862 ProbeCaptureDeviceList();
1863 value = alcCaptureDeviceList;
1865 break;
1867 /* Default devices are always first in the list */
1868 case ALC_DEFAULT_DEVICE_SPECIFIER:
1869 if(!alcDeviceList)
1870 ProbeDeviceList();
1872 pDevice = VerifyDevice(pDevice);
1874 free(alcDefaultDeviceSpecifier);
1875 alcDefaultDeviceSpecifier = strdup(alcDeviceList ? alcDeviceList : "");
1876 if(!alcDefaultDeviceSpecifier)
1877 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
1879 value = alcDefaultDeviceSpecifier;
1880 if(pDevice) ALCdevice_DecRef(pDevice);
1881 break;
1883 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
1884 if(!alcAllDeviceList)
1885 ProbeAllDeviceList();
1887 pDevice = VerifyDevice(pDevice);
1889 free(alcDefaultAllDeviceSpecifier);
1890 alcDefaultAllDeviceSpecifier = strdup(alcAllDeviceList ?
1891 alcAllDeviceList : "");
1892 if(!alcDefaultAllDeviceSpecifier)
1893 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
1895 value = alcDefaultAllDeviceSpecifier;
1896 if(pDevice) ALCdevice_DecRef(pDevice);
1897 break;
1899 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
1900 if(!alcCaptureDeviceList)
1901 ProbeCaptureDeviceList();
1903 pDevice = VerifyDevice(pDevice);
1905 free(alcCaptureDefaultDeviceSpecifier);
1906 alcCaptureDefaultDeviceSpecifier = strdup(alcCaptureDeviceList ?
1907 alcCaptureDeviceList : "");
1908 if(!alcCaptureDefaultDeviceSpecifier)
1909 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
1911 value = alcCaptureDefaultDeviceSpecifier;
1912 if(pDevice) ALCdevice_DecRef(pDevice);
1913 break;
1915 case ALC_EXTENSIONS:
1916 if(!VerifyDevice(pDevice))
1917 value = alcNoDeviceExtList;
1918 else
1920 value = alcExtensionList;
1921 ALCdevice_DecRef(pDevice);
1923 break;
1925 default:
1926 pDevice = VerifyDevice(pDevice);
1927 alcSetError(pDevice, ALC_INVALID_ENUM);
1928 if(pDevice) ALCdevice_DecRef(pDevice);
1929 break;
1932 return value;
1936 /* alcGetIntegerv
1938 * Returns information about the Device and the version of Open AL
1940 ALC_API ALCvoid ALC_APIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
1942 device = VerifyDevice(device);
1944 if(size == 0 || data == NULL)
1946 alcSetError(device, ALC_INVALID_VALUE);
1947 if(device) ALCdevice_DecRef(device);
1948 return;
1951 if(!device)
1953 switch(param)
1955 case ALC_MAJOR_VERSION:
1956 *data = alcMajorVersion;
1957 break;
1958 case ALC_MINOR_VERSION:
1959 *data = alcMinorVersion;
1960 break;
1962 case ALC_ATTRIBUTES_SIZE:
1963 case ALC_ALL_ATTRIBUTES:
1964 case ALC_FREQUENCY:
1965 case ALC_REFRESH:
1966 case ALC_SYNC:
1967 case ALC_MONO_SOURCES:
1968 case ALC_STEREO_SOURCES:
1969 case ALC_CAPTURE_SAMPLES:
1970 case ALC_FORMAT_CHANNELS_SOFT:
1971 case ALC_FORMAT_TYPE_SOFT:
1972 alcSetError(NULL, ALC_INVALID_DEVICE);
1973 break;
1975 default:
1976 alcSetError(NULL, ALC_INVALID_ENUM);
1977 break;
1980 else if(device->IsCaptureDevice)
1982 switch(param)
1984 case ALC_CAPTURE_SAMPLES:
1985 LockDevice(device);
1986 *data = ALCdevice_AvailableSamples(device);
1987 UnlockDevice(device);
1988 break;
1990 case ALC_CONNECTED:
1991 *data = device->Connected;
1992 break;
1994 default:
1995 alcSetError(device, ALC_INVALID_ENUM);
1996 break;
1999 else /* render device */
2001 switch(param)
2003 case ALC_MAJOR_VERSION:
2004 *data = alcMajorVersion;
2005 break;
2007 case ALC_MINOR_VERSION:
2008 *data = alcMinorVersion;
2009 break;
2011 case ALC_EFX_MAJOR_VERSION:
2012 *data = alcEFXMajorVersion;
2013 break;
2015 case ALC_EFX_MINOR_VERSION:
2016 *data = alcEFXMinorVersion;
2017 break;
2019 case ALC_ATTRIBUTES_SIZE:
2020 *data = 13;
2021 break;
2023 case ALC_ALL_ATTRIBUTES:
2024 if(size < 13)
2025 alcSetError(device, ALC_INVALID_VALUE);
2026 else
2028 int i = 0;
2030 data[i++] = ALC_FREQUENCY;
2031 data[i++] = device->Frequency;
2033 if(!device->IsLoopbackDevice)
2035 data[i++] = ALC_REFRESH;
2036 data[i++] = device->Frequency / device->UpdateSize;
2038 data[i++] = ALC_SYNC;
2039 data[i++] = ALC_FALSE;
2041 else
2043 data[i++] = ALC_FORMAT_CHANNELS_SOFT;
2044 data[i++] = device->FmtChans;
2046 data[i++] = ALC_FORMAT_TYPE_SOFT;
2047 data[i++] = device->FmtType;
2050 data[i++] = ALC_MONO_SOURCES;
2051 data[i++] = device->NumMonoSources;
2053 data[i++] = ALC_STEREO_SOURCES;
2054 data[i++] = device->NumStereoSources;
2056 data[i++] = ALC_MAX_AUXILIARY_SENDS;
2057 data[i++] = device->NumAuxSends;
2059 data[i++] = 0;
2061 break;
2063 case ALC_FREQUENCY:
2064 *data = device->Frequency;
2065 break;
2067 case ALC_REFRESH:
2068 if(device->IsLoopbackDevice)
2069 alcSetError(device, ALC_INVALID_DEVICE);
2070 else
2071 *data = device->Frequency / device->UpdateSize;
2072 break;
2074 case ALC_SYNC:
2075 if(device->IsLoopbackDevice)
2076 alcSetError(device, ALC_INVALID_DEVICE);
2077 else
2078 *data = ALC_FALSE;
2079 break;
2081 case ALC_FORMAT_CHANNELS_SOFT:
2082 if(!device->IsLoopbackDevice)
2083 alcSetError(device, ALC_INVALID_DEVICE);
2084 else
2085 *data = device->FmtChans;
2086 break;
2088 case ALC_FORMAT_TYPE_SOFT:
2089 if(!device->IsLoopbackDevice)
2090 alcSetError(device, ALC_INVALID_DEVICE);
2091 else
2092 *data = device->FmtType;
2093 break;
2095 case ALC_MONO_SOURCES:
2096 *data = device->NumMonoSources;
2097 break;
2099 case ALC_STEREO_SOURCES:
2100 *data = device->NumStereoSources;
2101 break;
2103 case ALC_MAX_AUXILIARY_SENDS:
2104 *data = device->NumAuxSends;
2105 break;
2107 case ALC_CONNECTED:
2108 *data = device->Connected;
2109 break;
2111 default:
2112 alcSetError(device, ALC_INVALID_ENUM);
2113 break;
2116 if(device)
2117 ALCdevice_DecRef(device);
2121 /* alcIsExtensionPresent
2123 * Determines if there is support for a particular extension
2125 ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
2127 ALCboolean bResult = ALC_FALSE;
2129 device = VerifyDevice(device);
2131 if(!extName)
2132 alcSetError(device, ALC_INVALID_VALUE);
2133 else
2135 size_t len = strlen(extName);
2136 const char *ptr = (device ? alcExtensionList : alcNoDeviceExtList);
2137 while(ptr && *ptr)
2139 if(strncasecmp(ptr, extName, len) == 0 &&
2140 (ptr[len] == '\0' || isspace(ptr[len])))
2142 bResult = ALC_TRUE;
2143 break;
2145 if((ptr=strchr(ptr, ' ')) != NULL)
2147 do {
2148 ++ptr;
2149 } while(isspace(*ptr));
2153 if(device)
2154 ALCdevice_DecRef(device);
2155 return bResult;
2159 /* alcGetProcAddress
2161 * Retrieves the function address for a particular extension function
2163 ALC_API ALCvoid* ALC_APIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
2165 ALCvoid *ptr = NULL;
2167 device = VerifyDevice(device);
2169 if(!funcName)
2170 alcSetError(device, ALC_INVALID_VALUE);
2171 else
2173 ALsizei i = 0;
2174 while(alcFunctions[i].funcName && strcmp(alcFunctions[i].funcName,funcName) != 0)
2175 i++;
2176 ptr = alcFunctions[i].address;
2178 if(device)
2179 ALCdevice_DecRef(device);
2180 return ptr;
2184 /* alcGetEnumValue
2186 * Get the value for a particular ALC Enumerated Value
2188 ALC_API ALCenum ALC_APIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
2190 ALCenum val = 0;
2192 device = VerifyDevice(device);
2194 if(!enumName)
2195 alcSetError(device, ALC_INVALID_VALUE);
2196 else
2198 ALsizei i = 0;
2199 while(enumeration[i].enumName && strcmp(enumeration[i].enumName,enumName) != 0)
2200 i++;
2201 val = enumeration[i].value;
2203 if(device)
2204 ALCdevice_DecRef(device);
2205 return val;
2209 /* alcCreateContext
2211 * Create and attach a Context to a particular Device.
2213 ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
2215 ALCcontext *ALContext;
2216 ALCenum err;
2218 LockLists();
2219 if(!(device=VerifyDevice(device)) || device->IsCaptureDevice || !device->Connected)
2221 UnlockLists();
2222 alcSetError(device, ALC_INVALID_DEVICE);
2223 if(device) ALCdevice_DecRef(device);
2224 return NULL;
2227 /* Reset Context Last Error code */
2228 device->LastError = ALC_NO_ERROR;
2230 if((err=UpdateDeviceParams(device, attrList)) != ALC_NO_ERROR)
2232 UnlockLists();
2233 alcSetError(device, err);
2234 if(err == ALC_INVALID_DEVICE)
2235 aluHandleDisconnect(device);
2236 ALCdevice_DecRef(device);
2237 return NULL;
2240 ALContext = calloc(1, sizeof(ALCcontext));
2241 if(ALContext)
2243 ALContext->ref = 1;
2245 ALContext->MaxActiveSources = 256;
2246 ALContext->ActiveSources = malloc(sizeof(ALContext->ActiveSources[0]) *
2247 ALContext->MaxActiveSources);
2249 if(!ALContext || !ALContext->ActiveSources)
2251 if(!device->ContextList)
2253 ALCdevice_StopPlayback(device);
2254 device->Flags &= ~DEVICE_RUNNING;
2256 UnlockLists();
2258 free(ALContext);
2259 ALContext = NULL;
2261 alcSetError(device, ALC_OUT_OF_MEMORY);
2262 ALCdevice_DecRef(device);
2263 return NULL;
2266 ALContext->Device = device;
2267 ALCdevice_IncRef(device);
2268 InitContext(ALContext);
2270 do {
2271 ALContext->next = device->ContextList;
2272 } while(!CompExchangePtr((XchgPtr*)&device->ContextList, ALContext->next, ALContext));
2273 UnlockLists();
2275 if(device->DefaultSlot)
2276 InitializeEffect(ALContext, device->DefaultSlot, &ForcedEffect);
2277 ALContext->LastError = AL_NO_ERROR;
2279 ALCdevice_DecRef(device);
2281 TRACE("Created context %p\n", ALContext);
2282 return ALContext;
2285 /* alcDestroyContext
2287 * Remove a Context
2289 ALC_API ALCvoid ALC_APIENTRY alcDestroyContext(ALCcontext *context)
2291 ALCdevice *Device;
2293 LockLists();
2294 /* alcGetContextsDevice sets an error for invalid contexts */
2295 Device = alcGetContextsDevice(context);
2296 if(Device)
2298 ReleaseContext(context, Device);
2299 if(!Device->ContextList)
2301 ALCdevice_StopPlayback(Device);
2302 Device->Flags &= ~DEVICE_RUNNING;
2305 UnlockLists();
2309 /* alcGetCurrentContext
2311 * Returns the currently active Context
2313 ALC_API ALCcontext* ALC_APIENTRY alcGetCurrentContext(ALCvoid)
2315 ALCcontext *Context;
2317 Context = pthread_getspecific(LocalContext);
2318 if(!Context) Context = GlobalContext;
2320 return Context;
2323 /* alcGetThreadContext
2325 * Returns the currently active thread-local Context
2327 ALC_API ALCcontext* ALC_APIENTRY alcGetThreadContext(void)
2329 ALCcontext *Context;
2330 Context = pthread_getspecific(LocalContext);
2331 return Context;
2335 /* alcMakeContextCurrent
2337 * Makes the given Context the active Context
2339 ALC_API ALCboolean ALC_APIENTRY alcMakeContextCurrent(ALCcontext *context)
2341 /* context must be a valid Context or NULL */
2342 if(context && !(context=VerifyContext(context)))
2344 alcSetError(NULL, ALC_INVALID_CONTEXT);
2345 return ALC_FALSE;
2347 /* context's reference count is already incremented */
2348 context = ExchangePtr((XchgPtr*)&GlobalContext, context);
2349 if(context) ALCcontext_DecRef(context);
2351 if((context=pthread_getspecific(LocalContext)) != NULL)
2353 pthread_setspecific(LocalContext, NULL);
2354 ALCcontext_DecRef(context);
2357 return ALC_TRUE;
2360 /* alcSetThreadContext
2362 * Makes the given Context the active Context for the current thread
2364 ALC_API ALCboolean ALC_APIENTRY alcSetThreadContext(ALCcontext *context)
2366 ALCcontext *old;
2368 /* context must be a valid Context or NULL */
2369 if(context && !(context=VerifyContext(context)))
2371 alcSetError(NULL, ALC_INVALID_CONTEXT);
2372 return ALC_FALSE;
2374 /* context's reference count is already incremented */
2375 old = pthread_getspecific(LocalContext);
2376 pthread_setspecific(LocalContext, context);
2377 if(old) ALCcontext_DecRef(old);
2379 return ALC_TRUE;
2383 /* alcGetContextsDevice
2385 * Returns the Device that a particular Context is attached to
2387 ALC_API ALCdevice* ALC_APIENTRY alcGetContextsDevice(ALCcontext *Context)
2389 ALCdevice *Device;
2391 if(!(Context=VerifyContext(Context)))
2393 alcSetError(NULL, ALC_INVALID_CONTEXT);
2394 return NULL;
2396 Device = Context->Device;
2397 ALCcontext_DecRef(Context);
2399 return Device;
2403 /* alcOpenDevice
2405 * Open the Device specified.
2407 ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName)
2409 const ALCchar *fmt;
2410 ALCdevice *device;
2411 ALCenum err;
2413 DO_INITCONFIG();
2415 if(!PlaybackBackend.name)
2417 alcSetError(NULL, ALC_INVALID_VALUE);
2418 return NULL;
2421 if(deviceName && (!deviceName[0] || strcasecmp(deviceName, "openal soft") == 0 || strcasecmp(deviceName, "openal-soft") == 0))
2422 deviceName = NULL;
2424 device = calloc(1, sizeof(ALCdevice)+sizeof(ALeffectslot));
2425 if(!device)
2427 alcSetError(NULL, ALC_OUT_OF_MEMORY);
2428 return NULL;
2431 //Validate device
2432 device->Funcs = &PlaybackBackend.Funcs;
2433 device->ref = 1;
2434 device->Connected = ALC_TRUE;
2435 device->IsCaptureDevice = AL_FALSE;
2436 device->IsLoopbackDevice = AL_FALSE;
2437 InitializeCriticalSection(&device->Mutex);
2438 device->LastError = ALC_NO_ERROR;
2440 device->Flags = 0;
2441 device->Bs2b = NULL;
2442 device->Bs2bLevel = 0;
2443 device->szDeviceName = NULL;
2445 device->ContextList = NULL;
2447 device->MaxNoOfSources = 256;
2448 device->AuxiliaryEffectSlotMax = 4;
2449 device->NumAuxSends = MAX_SENDS;
2451 InitUIntMap(&device->BufferMap, ~0);
2452 InitUIntMap(&device->EffectMap, ~0);
2453 InitUIntMap(&device->FilterMap, ~0);
2455 //Set output format
2456 device->FmtChans = DevFmtStereo;
2457 device->FmtType = DevFmtFloat;
2458 device->Frequency = DEFAULT_OUTPUT_RATE;
2459 device->NumUpdates = 4;
2460 device->UpdateSize = 1024;
2462 if(ConfigValueStr(NULL, "format", &fmt))
2464 static const struct {
2465 const char name[32];
2466 enum DevFmtChannels channels;
2467 enum DevFmtType type;
2468 } formats[] = {
2469 { "AL_FORMAT_MONO32", DevFmtMono, DevFmtFloat },
2470 { "AL_FORMAT_STEREO32", DevFmtStereo, DevFmtFloat },
2471 { "AL_FORMAT_QUAD32", DevFmtQuad, DevFmtFloat },
2472 { "AL_FORMAT_51CHN32", DevFmtX51, DevFmtFloat },
2473 { "AL_FORMAT_61CHN32", DevFmtX61, DevFmtFloat },
2474 { "AL_FORMAT_71CHN32", DevFmtX71, DevFmtFloat },
2476 { "AL_FORMAT_MONO16", DevFmtMono, DevFmtShort },
2477 { "AL_FORMAT_STEREO16", DevFmtStereo, DevFmtShort },
2478 { "AL_FORMAT_QUAD16", DevFmtQuad, DevFmtShort },
2479 { "AL_FORMAT_51CHN16", DevFmtX51, DevFmtShort },
2480 { "AL_FORMAT_61CHN16", DevFmtX61, DevFmtShort },
2481 { "AL_FORMAT_71CHN16", DevFmtX71, DevFmtShort },
2483 { "AL_FORMAT_MONO8", DevFmtMono, DevFmtByte },
2484 { "AL_FORMAT_STEREO8", DevFmtStereo, DevFmtByte },
2485 { "AL_FORMAT_QUAD8", DevFmtQuad, DevFmtByte },
2486 { "AL_FORMAT_51CHN8", DevFmtX51, DevFmtByte },
2487 { "AL_FORMAT_61CHN8", DevFmtX61, DevFmtByte },
2488 { "AL_FORMAT_71CHN8", DevFmtX71, DevFmtByte }
2490 size_t i;
2492 for(i = 0;i < sizeof(formats)/sizeof(formats[0]);i++)
2494 if(strcasecmp(fmt, formats[i].name) == 0)
2496 if(!(device->Flags&DEVICE_CHANNELS_REQUEST))
2497 device->FmtChans = formats[i].channels;
2498 if(!(device->Flags&DEVICE_SAMPLE_TYPE_REQUEST))
2499 device->FmtType = formats[i].type;
2500 device->Flags |= DEVICE_CHANNELS_REQUEST |
2501 DEVICE_SAMPLE_TYPE_REQUEST;
2502 break;
2505 if(i == sizeof(formats)/sizeof(formats[0]))
2506 ERR("Unsupported format: %s\n", fmt);
2508 if(ConfigValueStr(NULL, "channels", &fmt))
2510 static const struct {
2511 const char name[16];
2512 enum DevFmtChannels chans;
2513 } chanlist[] = {
2514 { "mono", DevFmtMono },
2515 { "stereo", DevFmtStereo },
2516 { "quad", DevFmtQuad },
2517 { "surround51", DevFmtX51 },
2518 { "surround61", DevFmtX61 },
2519 { "surround71", DevFmtX71 },
2521 size_t i;
2523 for(i = 0;i < sizeof(chanlist)/sizeof(chanlist[0]);i++)
2525 if(strcasecmp(chanlist[i].name, fmt) == 0)
2527 device->FmtChans = chanlist[i].chans;
2528 device->Flags |= DEVICE_CHANNELS_REQUEST;
2529 break;
2532 if(i == sizeof(chanlist)/sizeof(chanlist[0]))
2533 ERR("Unsupported channels: %s\n", fmt);
2535 if(ConfigValueStr(NULL, "sample-type", &fmt))
2537 static const struct {
2538 const char name[16];
2539 enum DevFmtType type;
2540 } chanlist[] = {
2541 { "int8", DevFmtByte },
2542 { "uint8", DevFmtUByte },
2543 { "int16", DevFmtShort },
2544 { "uint16", DevFmtUShort },
2545 { "int32", DevFmtInt },
2546 { "uint32", DevFmtUInt },
2547 { "float32", DevFmtFloat },
2549 size_t i;
2551 for(i = 0;i < sizeof(chanlist)/sizeof(chanlist[0]);i++)
2553 if(strcasecmp(chanlist[i].name, fmt) == 0)
2555 device->FmtType = chanlist[i].type;
2556 device->Flags |= DEVICE_SAMPLE_TYPE_REQUEST;
2557 break;
2560 if(i == sizeof(chanlist)/sizeof(chanlist[0]))
2561 ERR("Unsupported sample-type: %s\n", fmt);
2564 if(ConfigValueUInt(NULL, "frequency", &device->Frequency))
2566 device->Flags |= DEVICE_FREQUENCY_REQUEST;
2567 if(device->Frequency < MIN_OUTPUT_RATE)
2568 ERR("%uhz request clamped to %uhz minimum\n", device->Frequency, MIN_OUTPUT_RATE);
2569 device->Frequency = maxu(device->Frequency, MIN_OUTPUT_RATE);
2572 ConfigValueUInt(NULL, "periods", &device->NumUpdates);
2573 if(device->NumUpdates < 2) device->NumUpdates = 4;
2575 ConfigValueUInt(NULL, "period_size", &device->UpdateSize);
2576 if(device->UpdateSize == 0) device->UpdateSize = 1024;
2578 ConfigValueUInt(NULL, "sources", &device->MaxNoOfSources);
2579 if(device->MaxNoOfSources == 0) device->MaxNoOfSources = 256;
2581 ConfigValueUInt(NULL, "slots", &device->AuxiliaryEffectSlotMax);
2582 if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
2584 ConfigValueUInt(NULL, "sends", &device->NumAuxSends);
2585 if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
2587 ConfigValueInt(NULL, "cf_level", &device->Bs2bLevel);
2589 device->NumStereoSources = 1;
2590 device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
2592 device->DefaultSlot = (ALeffectslot*)(device+1);
2593 if(InitEffectSlot(device->DefaultSlot) != AL_NO_ERROR)
2594 device->DefaultSlot = NULL;
2596 // Find a playback device to open
2597 LockLists();
2598 if((err=ALCdevice_OpenPlayback(device, deviceName)) != ALC_NO_ERROR)
2600 UnlockLists();
2601 DeleteCriticalSection(&device->Mutex);
2602 free(device);
2603 alcSetError(NULL, err);
2604 return NULL;
2606 UnlockLists();
2608 do {
2609 device->next = DeviceList;
2610 } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
2612 TRACE("Created device %p\n", device);
2613 return device;
2616 /* alcCloseDevice
2618 * Close the specified Device
2620 ALC_API ALCboolean ALC_APIENTRY alcCloseDevice(ALCdevice *pDevice)
2622 ALCdevice *volatile*list;
2623 ALCcontext *ctx;
2625 LockLists();
2626 list = &DeviceList;
2627 while(*list && *list != pDevice)
2628 list = &(*list)->next;
2630 if(!*list || (*list)->IsCaptureDevice)
2632 alcSetError(*list, ALC_INVALID_DEVICE);
2633 UnlockLists();
2634 return ALC_FALSE;
2637 *list = (*list)->next;
2638 UnlockLists();
2640 if((ctx=pDevice->ContextList) != NULL)
2642 do {
2643 WARN("Releasing context %p\n", ctx);
2644 ReleaseContext(ctx, pDevice);
2645 } while((ctx=pDevice->ContextList) != NULL);
2646 ALCdevice_StopPlayback(pDevice);
2647 pDevice->Flags &= ~DEVICE_RUNNING;
2649 ALCdevice_ClosePlayback(pDevice);
2651 ALCdevice_DecRef(pDevice);
2653 return ALC_TRUE;
2657 /* alcLoopbackOpenDeviceSOFT
2659 * Open a loopback device, for manual rendering.
2661 ALC_API ALCdevice* ALC_APIENTRY alcLoopbackOpenDeviceSOFT(ALCdevice *device)
2663 DO_INITCONFIG();
2665 /* Make sure the device, if specified, belongs to us. */
2666 if(device && !(device=VerifyDevice(device)))
2668 alcSetError(device, ALC_INVALID_DEVICE);
2669 return NULL;
2671 if(device) ALCdevice_DecRef(device);
2673 device = calloc(1, sizeof(ALCdevice));
2674 if(!device)
2676 alcSetError(NULL, ALC_OUT_OF_MEMORY);
2677 return NULL;
2680 //Validate device
2681 device->Funcs = &BackendLoopback.Funcs;
2682 device->ref = 1;
2683 device->Connected = ALC_TRUE;
2684 device->IsCaptureDevice = AL_FALSE;
2685 device->IsLoopbackDevice = AL_TRUE;
2686 InitializeCriticalSection(&device->Mutex);
2687 device->LastError = ALC_NO_ERROR;
2689 device->Flags = 0;
2690 device->Bs2b = NULL;
2691 device->Bs2bLevel = 0;
2692 device->szDeviceName = NULL;
2694 device->ContextList = NULL;
2696 device->MaxNoOfSources = 256;
2697 device->AuxiliaryEffectSlotMax = 4;
2698 device->NumAuxSends = MAX_SENDS;
2700 InitUIntMap(&device->BufferMap, ~0);
2701 InitUIntMap(&device->EffectMap, ~0);
2702 InitUIntMap(&device->FilterMap, ~0);
2704 //Set output format
2705 device->NumUpdates = 0;
2706 device->UpdateSize = 0;
2708 device->Frequency = 44100;
2709 device->FmtChans = DevFmtStereo;
2710 device->FmtType = DevFmtShort;
2712 ConfigValueUInt(NULL, "sources", &device->MaxNoOfSources);
2713 if(device->MaxNoOfSources == 0) device->MaxNoOfSources = 256;
2715 ConfigValueUInt(NULL, "slots", &device->AuxiliaryEffectSlotMax);
2716 if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
2718 ConfigValueUInt(NULL, "sends", &device->NumAuxSends);
2719 if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
2721 device->NumStereoSources = 1;
2722 device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
2724 // Open the "backend"
2725 ALCdevice_OpenPlayback(device, "Loopback");
2726 do {
2727 device->next = DeviceList;
2728 } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
2730 TRACE("Created device %p\n", device);
2731 return device;
2734 /* alcIsRenderFormatSupportedSOFT
2736 * Determines if the loopback device supports the given format for rendering.
2738 ALC_API ALCboolean ALC_APIENTRY alcIsRenderFormatSupportedSOFT(ALCdevice *device, ALCsizei freq, ALCenum channels, ALCenum type)
2740 ALCboolean ret = ALC_FALSE;
2742 if(!(device=VerifyDevice(device)) || !device->IsLoopbackDevice)
2743 alcSetError(device, ALC_INVALID_DEVICE);
2744 else if(freq <= 0)
2745 alcSetError(device, ALC_INVALID_VALUE);
2746 else if(!IsValidALCType(type) || !IsValidALCChannels(channels))
2747 alcSetError(device, ALC_INVALID_ENUM);
2748 else
2750 if(BytesFromDevFmt(type) > 0 && ChannelsFromDevFmt(channels) > 0 &&
2751 freq >= MIN_OUTPUT_RATE)
2752 ret = ALC_TRUE;
2754 if(device) ALCdevice_DecRef(device);
2756 return ret;
2759 /* alcRenderSamplesSOFT
2761 * Renders some samples into a buffer, using the format last set by the
2762 * attributes given to alcCreateContext.
2764 ALC_API void ALC_APIENTRY alcRenderSamplesSOFT(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
2766 if(!(device=VerifyDevice(device)) || !device->IsLoopbackDevice)
2767 alcSetError(device, ALC_INVALID_DEVICE);
2768 else if(samples < 0 || (samples > 0 && buffer == NULL))
2769 alcSetError(device, ALC_INVALID_VALUE);
2770 else
2771 aluMixData(device, buffer, samples);
2772 if(device) ALCdevice_DecRef(device);
2776 static void ReleaseALC(void)
2778 ALCdevice *dev;
2780 free(alcDeviceList); alcDeviceList = NULL;
2781 alcDeviceListSize = 0;
2782 free(alcAllDeviceList); alcAllDeviceList = NULL;
2783 alcAllDeviceListSize = 0;
2784 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
2785 alcCaptureDeviceListSize = 0;
2787 free(alcDefaultDeviceSpecifier);
2788 alcDefaultDeviceSpecifier = NULL;
2789 free(alcDefaultAllDeviceSpecifier);
2790 alcDefaultAllDeviceSpecifier = NULL;
2791 free(alcCaptureDefaultDeviceSpecifier);
2792 alcCaptureDefaultDeviceSpecifier = NULL;
2794 if((dev=ExchangePtr((XchgPtr*)&DeviceList, NULL)) != NULL)
2796 ALCuint num = 0;
2797 do {
2798 num++;
2799 } while((dev=dev->next) != NULL);
2800 ERR("%u device%s not closed\n", num, (num>1)?"s":"");
2804 ///////////////////////////////////////////////////////