Use a separate backend callback to start playback of the device
[openal-soft.git] / Alc / ALc.c
blob9ac841b3069ac7575e7a86f4d15eb6dc6795e9cf
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_SOFTX_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 ForcedEffect;
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(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
423 ALsizei i;
424 (void)hModule;
426 // Perform actions based on the reason for calling.
427 switch(ul_reason_for_call)
429 case DLL_PROCESS_ATTACH:
430 InitUIntMap(&TlsDestructor, ~0);
431 alc_init();
432 break;
434 case DLL_THREAD_DETACH:
435 LockUIntMapRead(&TlsDestructor);
436 for(i = 0;i < TlsDestructor.size;i++)
438 void *ptr = pthread_getspecific(TlsDestructor.array[i].key);
439 void (*callback)(void*) = (void(*)(void*))TlsDestructor.array[i].value;
440 if(ptr && callback)
441 callback(ptr);
443 UnlockUIntMapRead(&TlsDestructor);
444 break;
446 case DLL_PROCESS_DETACH:
447 if(!lpReserved)
448 alc_deinit();
449 else
450 alc_deinit_safe();
451 ResetUIntMap(&TlsDestructor);
452 break;
454 return TRUE;
456 #elif defined(_MSC_VER)
457 #pragma section(".CRT$XCU",read)
458 static void alc_constructor(void);
459 static void alc_destructor(void);
460 __declspec(allocate(".CRT$XCU")) void (__cdecl* alc_constructor_)(void) = alc_constructor;
462 static void alc_constructor(void)
464 atexit(alc_destructor);
465 alc_init();
468 static void alc_destructor(void)
470 alc_deinit();
472 #elif defined(HAVE_GCC_DESTRUCTOR)
473 static void alc_init(void) __attribute__((constructor));
474 static void alc_deinit(void) __attribute__((destructor));
475 #else
476 #error "No static initialization available on this platform!"
477 #endif
478 #elif defined(HAVE_GCC_DESTRUCTOR)
479 static void alc_init(void) __attribute__((constructor));
480 static void alc_deinit(void) __attribute__((destructor));
481 #else
482 #error "No global initialization available on this platform!"
483 #endif
485 static void alc_init(void)
487 const char *str;
489 LogFile = stderr;
491 str = getenv("__ALSOFT_HALF_ANGLE_CONES");
492 if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
493 ConeScale = 1.0f;
495 str = getenv("__ALSOFT_REVERSE_Z");
496 if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
497 ZScale = -1.0f;
499 str = getenv("ALSOFT_TRAP_ERROR");
500 if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
502 TrapALError = AL_TRUE;
503 TrapALCError = AL_TRUE;
505 else
507 str = getenv("ALSOFT_TRAP_AL_ERROR");
508 if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
509 TrapALError = AL_TRUE;
511 str = getenv("ALSOFT_TRAP_ALC_ERROR");
512 if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
513 TrapALCError = ALC_TRUE;
516 pthread_key_create(&LocalContext, ReleaseThreadCtx);
517 InitializeCriticalSection(&ListLock);
518 ThunkInit();
521 static void alc_deinit_safe(void)
523 ReleaseALC();
525 FreeHrtf();
526 FreeALConfig();
528 ThunkExit();
529 DeleteCriticalSection(&ListLock);
530 pthread_key_delete(LocalContext);
532 if(LogFile != stderr)
533 fclose(LogFile);
534 LogFile = NULL;
537 static void alc_deinit(void)
539 int i;
541 ReleaseALC();
543 memset(&PlaybackBackend, 0, sizeof(PlaybackBackend));
544 memset(&CaptureBackend, 0, sizeof(CaptureBackend));
546 for(i = 0;BackendList[i].Deinit;i++)
547 BackendList[i].Deinit();
548 BackendLoopback.Deinit();
550 alc_deinit_safe();
553 static void alc_initconfig(void)
555 const char *devs, *str;
556 float valf;
557 int i, n;
559 str = getenv("ALSOFT_LOGLEVEL");
560 if(str)
562 long lvl = strtol(str, NULL, 0);
563 if(lvl >= NoLog && lvl <= LogRef)
564 LogLevel = lvl;
567 str = getenv("ALSOFT_LOGFILE");
568 if(str && str[0])
570 FILE *logfile = fopen(str, "wat");
571 if(logfile) LogFile = logfile;
572 else ERR("Failed to open log file '%s'\n", str);
575 ReadALConfig();
577 InitHrtf();
579 #ifdef _WIN32
580 RTPrioLevel = 1;
581 #else
582 RTPrioLevel = 0;
583 #endif
584 ConfigValueInt(NULL, "rt-prio", &RTPrioLevel);
586 if(ConfigValueStr(NULL, "resampler", &str))
588 if(strcasecmp(str, "point") == 0 || strcasecmp(str, "none") == 0)
589 DefaultResampler = PointResampler;
590 else if(strcasecmp(str, "linear") == 0)
591 DefaultResampler = LinearResampler;
592 else if(strcasecmp(str, "cubic") == 0)
593 DefaultResampler = CubicResampler;
594 else
596 char *end;
598 n = strtol(str, &end, 0);
599 if(*end == '\0' && (n == PointResampler || n == LinearResampler || n == CubicResampler))
600 DefaultResampler = n;
601 else
602 WARN("Invalid resampler: %s\n", str);
606 if(!TrapALCError)
607 TrapALCError = GetConfigValueBool(NULL, "trap-alc-error", ALC_FALSE);
609 if(!TrapALError)
610 TrapALError = GetConfigValueBool(NULL, "trap-al-error", AL_FALSE);
612 if(ConfigValueFloat("reverb", "boost", &valf))
613 ReverbBoost *= aluPow(10.0f, valf / 20.0f);
615 EmulateEAXReverb = GetConfigValueBool("reverb", "emulate-eax", AL_FALSE);
617 if(((devs=getenv("ALSOFT_DRIVERS")) && devs[0]) ||
618 ConfigValueStr(NULL, "drivers", &devs))
620 int n;
621 size_t len;
622 const char *next = devs;
623 int endlist, delitem;
625 i = 0;
626 do {
627 devs = next;
628 next = strchr(devs, ',');
630 delitem = (devs[0] == '-');
631 if(devs[0] == '-') devs++;
633 if(!devs[0] || devs[0] == ',')
635 endlist = 0;
636 continue;
638 endlist = 1;
640 len = (next ? ((size_t)(next-devs)) : strlen(devs));
641 for(n = i;BackendList[n].Init;n++)
643 if(len == strlen(BackendList[n].name) &&
644 strncmp(BackendList[n].name, devs, len) == 0)
646 if(delitem)
648 do {
649 BackendList[n] = BackendList[n+1];
650 ++n;
651 } while(BackendList[n].Init);
653 else
655 struct BackendInfo Bkp = BackendList[n];
656 while(n > i)
658 BackendList[n] = BackendList[n-1];
659 --n;
661 BackendList[n] = Bkp;
663 i++;
665 break;
668 } while(next++);
670 if(endlist)
672 BackendList[i].name = NULL;
673 BackendList[i].Init = NULL;
674 BackendList[i].Deinit = NULL;
675 BackendList[i].Probe = NULL;
679 for(i = 0;BackendList[i].Init && (!PlaybackBackend.name || !CaptureBackend.name);i++)
681 if(!BackendList[i].Init(&BackendList[i].Funcs))
683 WARN("Failed to initialize backend \"%s\"\n", BackendList[i].name);
684 continue;
687 TRACE("Initialized backend \"%s\"\n", BackendList[i].name);
688 if(BackendList[i].Funcs.OpenPlayback && !PlaybackBackend.name)
690 PlaybackBackend = BackendList[i];
691 TRACE("Added \"%s\" for playback\n", PlaybackBackend.name);
693 if(BackendList[i].Funcs.OpenCapture && !CaptureBackend.name)
695 CaptureBackend = BackendList[i];
696 TRACE("Added \"%s\" for capture\n", CaptureBackend.name);
699 BackendLoopback.Init(&BackendLoopback.Funcs);
701 if(ConfigValueStr(NULL, "excludefx", &str))
703 size_t len;
704 const char *next = str;
706 do {
707 str = next;
708 next = strchr(str, ',');
710 if(!str[0] || next == str)
711 continue;
713 len = (next ? ((size_t)(next-str)) : strlen(str));
714 for(n = 0;EffectList[n].name;n++)
716 if(len == strlen(EffectList[n].name) &&
717 strncmp(EffectList[n].name, str, len) == 0)
718 DisabledEffects[EffectList[n].type] = AL_TRUE;
720 } while(next++);
723 InitEffect(&ForcedEffect);
724 str = getenv("ALSOFT_DEFAULT_REVERB");
725 if(str && str[0])
726 GetReverbEffect(str, &ForcedEffect);
727 else if(ConfigValueStr(NULL, "default-reverb", &str))
728 GetReverbEffect(str, &ForcedEffect);
732 static void LockLists(void)
734 EnterCriticalSection(&ListLock);
737 static void UnlockLists(void)
739 LeaveCriticalSection(&ListLock);
743 static void ProbeList(ALCchar **list, size_t *listsize, enum DevProbe type)
745 DO_INITCONFIG();
747 LockLists();
748 free(*list);
749 *list = NULL;
750 *listsize = 0;
752 if(type == ALL_DEVICE_PROBE && PlaybackBackend.Probe)
753 PlaybackBackend.Probe(type);
754 else if(type == CAPTURE_DEVICE_PROBE && CaptureBackend.Probe)
755 CaptureBackend.Probe(type);
756 UnlockLists();
759 static void ProbeAllDeviceList(void)
760 { ProbeList(&alcAllDeviceList, &alcAllDeviceListSize, ALL_DEVICE_PROBE); }
761 static void ProbeCaptureDeviceList(void)
762 { ProbeList(&alcCaptureDeviceList, &alcCaptureDeviceListSize, CAPTURE_DEVICE_PROBE); }
765 static void AppendList(const ALCchar *name, ALCchar **List, size_t *ListSize)
767 size_t len = strlen(name);
768 void *temp;
770 if(len == 0)
771 return;
773 temp = realloc(*List, (*ListSize) + len + 2);
774 if(!temp)
776 ERR("Realloc failed to add %s!\n", name);
777 return;
779 *List = temp;
781 memcpy((*List)+(*ListSize), name, len+1);
782 *ListSize += len+1;
783 (*List)[*ListSize] = 0;
786 #define DECL_APPEND_LIST_FUNC(type) \
787 void Append##type##List(const ALCchar *name) \
788 { AppendList(name, &alc##type##List, &alc##type##ListSize); }
790 DECL_APPEND_LIST_FUNC(AllDevice)
791 DECL_APPEND_LIST_FUNC(CaptureDevice)
793 #undef DECL_APPEND_LIST_FUNC
796 /* Sets the default channel order used by most non-WaveFormatEx-based APIs */
797 void SetDefaultChannelOrder(ALCdevice *device)
799 switch(device->FmtChans)
801 case DevFmtX51: device->DevChannels[0] = FRONT_LEFT;
802 device->DevChannels[1] = FRONT_RIGHT;
803 device->DevChannels[2] = BACK_LEFT;
804 device->DevChannels[3] = BACK_RIGHT;
805 device->DevChannels[4] = FRONT_CENTER;
806 device->DevChannels[5] = LFE;
807 return;
809 case DevFmtX71: device->DevChannels[0] = FRONT_LEFT;
810 device->DevChannels[1] = FRONT_RIGHT;
811 device->DevChannels[2] = BACK_LEFT;
812 device->DevChannels[3] = BACK_RIGHT;
813 device->DevChannels[4] = FRONT_CENTER;
814 device->DevChannels[5] = LFE;
815 device->DevChannels[6] = SIDE_LEFT;
816 device->DevChannels[7] = SIDE_RIGHT;
817 return;
819 /* Same as WFX order */
820 case DevFmtMono:
821 case DevFmtStereo:
822 case DevFmtQuad:
823 case DevFmtX51Side:
824 case DevFmtX61:
825 break;
827 SetDefaultWFXChannelOrder(device);
829 /* Sets the default order used by WaveFormatEx */
830 void SetDefaultWFXChannelOrder(ALCdevice *device)
832 switch(device->FmtChans)
834 case DevFmtMono: device->DevChannels[0] = FRONT_CENTER; break;
836 case DevFmtStereo: device->DevChannels[0] = FRONT_LEFT;
837 device->DevChannels[1] = FRONT_RIGHT; break;
839 case DevFmtQuad: device->DevChannels[0] = FRONT_LEFT;
840 device->DevChannels[1] = FRONT_RIGHT;
841 device->DevChannels[2] = BACK_LEFT;
842 device->DevChannels[3] = BACK_RIGHT; break;
844 case DevFmtX51: device->DevChannels[0] = FRONT_LEFT;
845 device->DevChannels[1] = FRONT_RIGHT;
846 device->DevChannels[2] = FRONT_CENTER;
847 device->DevChannels[3] = LFE;
848 device->DevChannels[4] = BACK_LEFT;
849 device->DevChannels[5] = BACK_RIGHT; break;
851 case DevFmtX51Side: device->DevChannels[0] = FRONT_LEFT;
852 device->DevChannels[1] = FRONT_RIGHT;
853 device->DevChannels[2] = FRONT_CENTER;
854 device->DevChannels[3] = LFE;
855 device->DevChannels[4] = SIDE_LEFT;
856 device->DevChannels[5] = SIDE_RIGHT; break;
858 case DevFmtX61: device->DevChannels[0] = FRONT_LEFT;
859 device->DevChannels[1] = FRONT_RIGHT;
860 device->DevChannels[2] = FRONT_CENTER;
861 device->DevChannels[3] = LFE;
862 device->DevChannels[4] = BACK_CENTER;
863 device->DevChannels[5] = SIDE_LEFT;
864 device->DevChannels[6] = SIDE_RIGHT; break;
866 case DevFmtX71: device->DevChannels[0] = FRONT_LEFT;
867 device->DevChannels[1] = FRONT_RIGHT;
868 device->DevChannels[2] = FRONT_CENTER;
869 device->DevChannels[3] = LFE;
870 device->DevChannels[4] = BACK_LEFT;
871 device->DevChannels[5] = BACK_RIGHT;
872 device->DevChannels[6] = SIDE_LEFT;
873 device->DevChannels[7] = SIDE_RIGHT; break;
878 const ALCchar *DevFmtTypeString(enum DevFmtType type)
880 switch(type)
882 case DevFmtByte: return "Signed Byte";
883 case DevFmtUByte: return "Unsigned Byte";
884 case DevFmtShort: return "Signed Short";
885 case DevFmtUShort: return "Unsigned Short";
886 case DevFmtInt: return "Signed Int";
887 case DevFmtUInt: return "Unsigned Int";
888 case DevFmtFloat: return "Float";
890 return "(unknown type)";
892 const ALCchar *DevFmtChannelsString(enum DevFmtChannels chans)
894 switch(chans)
896 case DevFmtMono: return "Mono";
897 case DevFmtStereo: return "Stereo";
898 case DevFmtQuad: return "Quadraphonic";
899 case DevFmtX51: return "5.1 Surround";
900 case DevFmtX51Side: return "5.1 Side";
901 case DevFmtX61: return "6.1 Surround";
902 case DevFmtX71: return "7.1 Surround";
904 return "(unknown channels)";
907 ALuint BytesFromDevFmt(enum DevFmtType type)
909 switch(type)
911 case DevFmtByte: return sizeof(ALbyte);
912 case DevFmtUByte: return sizeof(ALubyte);
913 case DevFmtShort: return sizeof(ALshort);
914 case DevFmtUShort: return sizeof(ALushort);
915 case DevFmtInt: return sizeof(ALint);
916 case DevFmtUInt: return sizeof(ALuint);
917 case DevFmtFloat: return sizeof(ALfloat);
919 return 0;
921 ALuint ChannelsFromDevFmt(enum DevFmtChannels chans)
923 switch(chans)
925 case DevFmtMono: return 1;
926 case DevFmtStereo: return 2;
927 case DevFmtQuad: return 4;
928 case DevFmtX51: return 6;
929 case DevFmtX51Side: return 6;
930 case DevFmtX61: return 7;
931 case DevFmtX71: return 8;
933 return 0;
935 static ALboolean DecomposeDevFormat(ALenum format, enum DevFmtChannels *chans,
936 enum DevFmtType *type)
938 static const struct {
939 ALenum format;
940 enum DevFmtChannels channels;
941 enum DevFmtType type;
942 } list[] = {
943 { AL_FORMAT_MONO8, DevFmtMono, DevFmtUByte },
944 { AL_FORMAT_MONO16, DevFmtMono, DevFmtShort },
945 { AL_FORMAT_MONO_FLOAT32, DevFmtMono, DevFmtFloat },
947 { AL_FORMAT_STEREO8, DevFmtStereo, DevFmtUByte },
948 { AL_FORMAT_STEREO16, DevFmtStereo, DevFmtShort },
949 { AL_FORMAT_STEREO_FLOAT32, DevFmtStereo, DevFmtFloat },
951 { AL_FORMAT_QUAD8, DevFmtQuad, DevFmtUByte },
952 { AL_FORMAT_QUAD16, DevFmtQuad, DevFmtShort },
953 { AL_FORMAT_QUAD32, DevFmtQuad, DevFmtFloat },
955 { AL_FORMAT_51CHN8, DevFmtX51, DevFmtUByte },
956 { AL_FORMAT_51CHN16, DevFmtX51, DevFmtShort },
957 { AL_FORMAT_51CHN32, DevFmtX51, DevFmtFloat },
959 { AL_FORMAT_61CHN8, DevFmtX61, DevFmtUByte },
960 { AL_FORMAT_61CHN16, DevFmtX61, DevFmtShort },
961 { AL_FORMAT_61CHN32, DevFmtX61, DevFmtFloat },
963 { AL_FORMAT_71CHN8, DevFmtX71, DevFmtUByte },
964 { AL_FORMAT_71CHN16, DevFmtX71, DevFmtShort },
965 { AL_FORMAT_71CHN32, DevFmtX71, DevFmtFloat },
967 ALuint i;
969 for(i = 0;i < COUNTOF(list);i++)
971 if(list[i].format == format)
973 *chans = list[i].channels;
974 *type = list[i].type;
975 return AL_TRUE;
979 return AL_FALSE;
982 static ALCboolean IsValidALCType(ALCenum type)
984 switch(type)
986 case ALC_BYTE_SOFT:
987 case ALC_UNSIGNED_BYTE_SOFT:
988 case ALC_SHORT_SOFT:
989 case ALC_UNSIGNED_SHORT_SOFT:
990 case ALC_INT_SOFT:
991 case ALC_UNSIGNED_INT_SOFT:
992 case ALC_FLOAT_SOFT:
993 return ALC_TRUE;
995 return ALC_FALSE;
998 static ALCboolean IsValidALCChannels(ALCenum channels)
1000 switch(channels)
1002 case ALC_MONO_SOFT:
1003 case ALC_STEREO_SOFT:
1004 case ALC_QUAD_SOFT:
1005 case ALC_5POINT1_SOFT:
1006 case ALC_6POINT1_SOFT:
1007 case ALC_7POINT1_SOFT:
1008 return ALC_TRUE;
1010 return ALC_FALSE;
1014 /* alcSetError
1016 * Stores the latest ALC Error
1018 static void alcSetError(ALCdevice *device, ALCenum errorCode)
1020 if(TrapALCError)
1022 #ifdef _WIN32
1023 /* DebugBreak() will cause an exception if there is no debugger */
1024 if(IsDebuggerPresent())
1025 DebugBreak();
1026 #elif defined(SIGTRAP)
1027 raise(SIGTRAP);
1028 #endif
1031 if(device)
1032 device->LastError = errorCode;
1033 else
1034 g_eLastNullDeviceError = errorCode;
1038 /* UpdateDeviceParams
1040 * Updates device parameters according to the attribute list (caller is
1041 * responsible for holding the list lock).
1043 static ALCenum UpdateDeviceParams(ALCdevice *device, const ALCint *attrList)
1045 ALCcontext *context;
1046 enum DevFmtChannels oldChans;
1047 enum DevFmtType oldType;
1048 ALCuint oldFreq;
1049 int oldMode;
1050 ALuint i;
1052 // Check for attributes
1053 if(device->Type == Loopback)
1055 enum {
1056 GotFreq = 1<<0,
1057 GotChans = 1<<1,
1058 GotType = 1<<2,
1059 GotAll = GotFreq|GotChans|GotType
1061 ALCuint freq, numMono, numStereo, numSends;
1062 enum DevFmtChannels schans;
1063 enum DevFmtType stype;
1064 ALCuint attrIdx = 0;
1065 ALCint gotFmt = 0;
1067 if(!attrList)
1069 WARN("Missing attributes for loopback device\n");
1070 return ALC_INVALID_VALUE;
1073 numMono = device->NumMonoSources;
1074 numStereo = device->NumStereoSources;
1075 numSends = device->NumAuxSends;
1076 schans = device->FmtChans;
1077 stype = device->FmtType;
1078 freq = device->Frequency;
1080 while(attrList[attrIdx])
1082 if(attrList[attrIdx] == ALC_FORMAT_CHANNELS_SOFT)
1084 ALCint val = attrList[attrIdx + 1];
1085 if(!IsValidALCChannels(val) || !ChannelsFromDevFmt(val))
1086 return ALC_INVALID_VALUE;
1087 schans = val;
1088 gotFmt |= GotChans;
1091 if(attrList[attrIdx] == ALC_FORMAT_TYPE_SOFT)
1093 ALCint val = attrList[attrIdx + 1];
1094 if(!IsValidALCType(val) || !BytesFromDevFmt(val))
1095 return ALC_INVALID_VALUE;
1096 stype = val;
1097 gotFmt |= GotType;
1100 if(attrList[attrIdx] == ALC_FREQUENCY)
1102 freq = attrList[attrIdx + 1];
1103 if(freq < MIN_OUTPUT_RATE)
1104 return ALC_INVALID_VALUE;
1105 gotFmt |= GotFreq;
1108 if(attrList[attrIdx] == ALC_STEREO_SOURCES)
1110 numStereo = attrList[attrIdx + 1];
1111 if(numStereo > device->MaxNoOfSources)
1112 numStereo = device->MaxNoOfSources;
1114 numMono = device->MaxNoOfSources - numStereo;
1117 if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS)
1118 numSends = attrList[attrIdx + 1];
1120 attrIdx += 2;
1123 if(gotFmt != GotAll)
1125 WARN("Missing format for loopback device\n");
1126 return ALC_INVALID_VALUE;
1129 ConfigValueUInt(NULL, "sends", &numSends);
1130 numSends = minu(MAX_SENDS, numSends);
1132 if((device->Flags&DEVICE_RUNNING))
1133 ALCdevice_StopPlayback(device);
1134 device->Flags &= ~DEVICE_RUNNING;
1136 device->Frequency = freq;
1137 device->FmtChans = schans;
1138 device->FmtType = stype;
1139 device->NumMonoSources = numMono;
1140 device->NumStereoSources = numStereo;
1141 device->NumAuxSends = numSends;
1143 else if(attrList && attrList[0])
1145 ALCuint freq, numMono, numStereo, numSends;
1146 ALCuint attrIdx = 0;
1148 /* If a context is already running on the device, stop playback so the
1149 * device attributes can be updated. */
1150 if((device->Flags&DEVICE_RUNNING))
1151 ALCdevice_StopPlayback(device);
1152 device->Flags &= ~DEVICE_RUNNING;
1154 freq = device->Frequency;
1155 numMono = device->NumMonoSources;
1156 numStereo = device->NumStereoSources;
1157 numSends = device->NumAuxSends;
1159 while(attrList[attrIdx])
1161 if(attrList[attrIdx] == ALC_FREQUENCY)
1163 freq = attrList[attrIdx + 1];
1164 device->Flags |= DEVICE_FREQUENCY_REQUEST;
1167 if(attrList[attrIdx] == ALC_STEREO_SOURCES)
1169 numStereo = attrList[attrIdx + 1];
1170 if(numStereo > device->MaxNoOfSources)
1171 numStereo = device->MaxNoOfSources;
1173 numMono = device->MaxNoOfSources - numStereo;
1176 if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS)
1177 numSends = attrList[attrIdx + 1];
1179 attrIdx += 2;
1182 ConfigValueUInt(NULL, "frequency", &freq);
1183 freq = maxu(freq, MIN_OUTPUT_RATE);
1185 ConfigValueUInt(NULL, "sends", &numSends);
1186 numSends = minu(MAX_SENDS, numSends);
1188 device->UpdateSize = (ALuint64)device->UpdateSize * freq /
1189 device->Frequency;
1191 device->Frequency = freq;
1192 device->NumMonoSources = numMono;
1193 device->NumStereoSources = numStereo;
1194 device->NumAuxSends = numSends;
1197 if((device->Flags&DEVICE_RUNNING))
1198 return ALC_NO_ERROR;
1200 oldFreq = device->Frequency;
1201 oldChans = device->FmtChans;
1202 oldType = device->FmtType;
1204 TRACE("Format pre-setup: %s%s, %s%s, %uhz%s, %u update size x%d\n",
1205 DevFmtChannelsString(device->FmtChans),
1206 (device->Flags&DEVICE_CHANNELS_REQUEST)?" (requested)":"",
1207 DevFmtTypeString(device->FmtType),
1208 (device->Flags&DEVICE_SAMPLE_TYPE_REQUEST)?" (requested)":"",
1209 device->Frequency,
1210 (device->Flags&DEVICE_FREQUENCY_REQUEST)?" (requested)":"",
1211 device->UpdateSize, device->NumUpdates);
1213 if(ALCdevice_ResetPlayback(device) == ALC_FALSE)
1214 return ALC_INVALID_DEVICE;
1216 if(device->FmtChans != oldChans && (device->Flags&DEVICE_CHANNELS_REQUEST))
1218 ERR("Failed to set %s, got %s instead\n", DevFmtChannelsString(oldChans),
1219 DevFmtChannelsString(device->FmtChans));
1220 device->Flags &= ~DEVICE_CHANNELS_REQUEST;
1222 if(device->FmtType != oldType && (device->Flags&DEVICE_SAMPLE_TYPE_REQUEST))
1224 ERR("Failed to set %s, got %s instead\n", DevFmtTypeString(oldType),
1225 DevFmtTypeString(device->FmtType));
1226 device->Flags &= ~DEVICE_SAMPLE_TYPE_REQUEST;
1228 if(device->Frequency != oldFreq && (device->Flags&DEVICE_FREQUENCY_REQUEST))
1230 ERR("Failed to set %uhz, got %uhz instead\n", oldFreq, device->Frequency);
1231 device->Flags &= ~DEVICE_FREQUENCY_REQUEST;
1234 TRACE("Format post-setup: %s, %s, %uhz, %u update size x%d\n",
1235 DevFmtChannelsString(device->FmtChans),
1236 DevFmtTypeString(device->FmtType), device->Frequency,
1237 device->UpdateSize, device->NumUpdates);
1239 aluInitPanning(device);
1241 for(i = 0;i < MAXCHANNELS;i++)
1243 device->ClickRemoval[i] = 0.0f;
1244 device->PendingClicks[i] = 0.0f;
1247 device->Hrtf = NULL;
1248 if(device->Type != Loopback && GetConfigValueBool(NULL, "hrtf", AL_FALSE))
1249 device->Hrtf = GetHrtf(device);
1250 TRACE("HRTF %s\n", device->Hrtf?"enabled":"disabled");
1252 if(!device->Hrtf && device->Bs2bLevel > 0 && device->Bs2bLevel <= 6)
1254 if(!device->Bs2b)
1256 device->Bs2b = calloc(1, sizeof(*device->Bs2b));
1257 bs2b_clear(device->Bs2b);
1259 bs2b_set_srate(device->Bs2b, device->Frequency);
1260 bs2b_set_level(device->Bs2b, device->Bs2bLevel);
1261 TRACE("BS2B level %d\n", device->Bs2bLevel);
1263 else
1265 free(device->Bs2b);
1266 device->Bs2b = NULL;
1267 TRACE("BS2B disabled\n");
1270 device->Flags &= ~DEVICE_DUPLICATE_STEREO;
1271 switch(device->FmtChans)
1273 case DevFmtMono:
1274 case DevFmtStereo:
1275 break;
1276 case DevFmtQuad:
1277 case DevFmtX51:
1278 case DevFmtX51Side:
1279 case DevFmtX61:
1280 case DevFmtX71:
1281 if(GetConfigValueBool(NULL, "stereodup", AL_TRUE))
1282 device->Flags |= DEVICE_DUPLICATE_STEREO;
1283 break;
1285 TRACE("Stereo duplication %s\n", (device->Flags&DEVICE_DUPLICATE_STEREO)?"enabled":"disabled");
1287 oldMode = SetMixerFPUMode();
1288 LockDevice(device);
1289 context = device->ContextList;
1290 while(context)
1292 ALsizei pos;
1294 context->UpdateSources = AL_FALSE;
1295 LockUIntMapRead(&context->EffectSlotMap);
1296 for(pos = 0;pos < context->EffectSlotMap.size;pos++)
1298 ALeffectslot *slot = context->EffectSlotMap.array[pos].value;
1300 if(ALeffectState_DeviceUpdate(slot->EffectState, device) == AL_FALSE)
1302 UnlockUIntMapRead(&context->EffectSlotMap);
1303 UnlockDevice(device);
1304 RestoreFPUMode(oldMode);
1305 return ALC_INVALID_DEVICE;
1307 slot->NeedsUpdate = AL_FALSE;
1308 ALeffectState_Update(slot->EffectState, context, slot);
1310 UnlockUIntMapRead(&context->EffectSlotMap);
1312 LockUIntMapRead(&context->SourceMap);
1313 for(pos = 0;pos < context->SourceMap.size;pos++)
1315 ALsource *source = context->SourceMap.array[pos].value;
1316 ALuint s = device->NumAuxSends;
1317 while(s < MAX_SENDS)
1319 if(source->Send[s].Slot)
1320 DecrementRef(&source->Send[s].Slot->ref);
1321 source->Send[s].Slot = NULL;
1322 source->Send[s].WetGain = 1.0f;
1323 source->Send[s].WetGainHF = 1.0f;
1324 s++;
1326 source->NeedsUpdate = AL_FALSE;
1327 ALsource_Update(source, context);
1329 UnlockUIntMapRead(&context->SourceMap);
1331 context = context->next;
1333 UnlockDevice(device);
1334 RestoreFPUMode(oldMode);
1336 if(ALCdevice_StartPlayback(device) == ALC_FALSE)
1337 return ALC_INVALID_DEVICE;
1338 device->Flags |= DEVICE_RUNNING;
1340 return ALC_NO_ERROR;
1343 /* FreeDevice
1345 * Frees the device structure, and destroys any objects the app failed to
1346 * delete. Called once there's no more references on the device.
1348 static ALCvoid FreeDevice(ALCdevice *device)
1350 TRACE("%p\n", device);
1352 if(device->DefaultSlot)
1354 ALeffectState_Destroy(device->DefaultSlot->EffectState);
1355 device->DefaultSlot->EffectState = NULL;
1358 if(device->BufferMap.size > 0)
1360 WARN("(%p) Deleting %d Buffer(s)\n", device, device->BufferMap.size);
1361 ReleaseALBuffers(device);
1363 ResetUIntMap(&device->BufferMap);
1365 if(device->EffectMap.size > 0)
1367 WARN("(%p) Deleting %d Effect(s)\n", device, device->EffectMap.size);
1368 ReleaseALEffects(device);
1370 ResetUIntMap(&device->EffectMap);
1372 if(device->FilterMap.size > 0)
1374 WARN("(%p) Deleting %d Filter(s)\n", device, device->FilterMap.size);
1375 ReleaseALFilters(device);
1377 ResetUIntMap(&device->FilterMap);
1379 free(device->Bs2b);
1380 device->Bs2b = NULL;
1382 free(device->szDeviceName);
1383 device->szDeviceName = NULL;
1385 DeleteCriticalSection(&device->Mutex);
1387 free(device);
1391 void ALCdevice_IncRef(ALCdevice *device)
1393 RefCount ref;
1394 ref = IncrementRef(&device->ref);
1395 TRACEREF("%p increasing refcount to %u\n", device, ref);
1398 void ALCdevice_DecRef(ALCdevice *device)
1400 RefCount ref;
1401 ref = DecrementRef(&device->ref);
1402 TRACEREF("%p decreasing refcount to %u\n", device, ref);
1403 if(ref == 0) FreeDevice(device);
1406 /* VerifyDevice
1408 * Checks if the device handle is valid, and increments its ref count if so.
1410 static ALCdevice *VerifyDevice(ALCdevice *device)
1412 ALCdevice *tmpDevice;
1414 if(!device)
1415 return NULL;
1417 LockLists();
1418 tmpDevice = DeviceList;
1419 while(tmpDevice && tmpDevice != device)
1420 tmpDevice = tmpDevice->next;
1422 if(tmpDevice)
1423 ALCdevice_IncRef(tmpDevice);
1424 UnlockLists();
1425 return tmpDevice;
1429 /* InitContext
1431 * Initializes context variables
1433 static ALvoid InitContext(ALCcontext *pContext)
1435 ALint i, j;
1437 //Initialise listener
1438 pContext->Listener.Gain = 1.0f;
1439 pContext->Listener.MetersPerUnit = 1.0f;
1440 pContext->Listener.Position[0] = 0.0f;
1441 pContext->Listener.Position[1] = 0.0f;
1442 pContext->Listener.Position[2] = 0.0f;
1443 pContext->Listener.Velocity[0] = 0.0f;
1444 pContext->Listener.Velocity[1] = 0.0f;
1445 pContext->Listener.Velocity[2] = 0.0f;
1446 pContext->Listener.Forward[0] = 0.0f;
1447 pContext->Listener.Forward[1] = 0.0f;
1448 pContext->Listener.Forward[2] = -1.0f;
1449 pContext->Listener.Up[0] = 0.0f;
1450 pContext->Listener.Up[1] = 1.0f;
1451 pContext->Listener.Up[2] = 0.0f;
1452 for(i = 0;i < 4;i++)
1454 for(j = 0;j < 4;j++)
1455 pContext->Listener.Matrix[i][j] = ((i==j) ? 1.0f : 0.0f);
1458 //Validate pContext
1459 pContext->LastError = AL_NO_ERROR;
1460 pContext->UpdateSources = AL_FALSE;
1461 pContext->ActiveSourceCount = 0;
1462 InitUIntMap(&pContext->SourceMap, pContext->Device->MaxNoOfSources);
1463 InitUIntMap(&pContext->EffectSlotMap, pContext->Device->AuxiliaryEffectSlotMax);
1465 //Set globals
1466 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
1467 pContext->SourceDistanceModel = AL_FALSE;
1468 pContext->DopplerFactor = 1.0f;
1469 pContext->DopplerVelocity = 1.0f;
1470 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
1471 pContext->DeferUpdates = AL_FALSE;
1473 pContext->ExtensionList = alExtList;
1477 /* FreeContext
1479 * Cleans up the context, and destroys any remaining objects the app failed to
1480 * delete. Called once there's no more references on the context.
1482 static ALCvoid FreeContext(ALCcontext *context)
1484 TRACE("%p\n", context);
1486 if(context->SourceMap.size > 0)
1488 ERR("(%p) Deleting %d Source(s)\n", context, context->SourceMap.size);
1489 ReleaseALSources(context);
1491 ResetUIntMap(&context->SourceMap);
1493 if(context->EffectSlotMap.size > 0)
1495 ERR("(%p) Deleting %d AuxiliaryEffectSlot(s)\n", context, context->EffectSlotMap.size);
1496 ReleaseALAuxiliaryEffectSlots(context);
1498 ResetUIntMap(&context->EffectSlotMap);
1500 context->ActiveSourceCount = 0;
1501 free(context->ActiveSources);
1502 context->ActiveSources = NULL;
1503 context->MaxActiveSources = 0;
1505 context->ActiveEffectSlotCount = 0;
1506 free(context->ActiveEffectSlots);
1507 context->ActiveEffectSlots = NULL;
1508 context->MaxActiveEffectSlots = 0;
1510 ALCdevice_DecRef(context->Device);
1511 context->Device = NULL;
1513 //Invalidate context
1514 memset(context, 0, sizeof(ALCcontext));
1515 free(context);
1518 /* ReleaseContext
1520 * Removes the context reference from the given device and removes it from
1521 * being current on the running thread or globally.
1523 static void ReleaseContext(ALCcontext *context, ALCdevice *device)
1525 ALCcontext *volatile*tmp_ctx;
1527 if(pthread_getspecific(LocalContext) == context)
1529 WARN("%p released while current on thread\n", context);
1530 pthread_setspecific(LocalContext, NULL);
1531 ALCcontext_DecRef(context);
1534 if(CompExchangePtr((XchgPtr*)&GlobalContext, context, NULL))
1535 ALCcontext_DecRef(context);
1537 LockDevice(device);
1538 tmp_ctx = &device->ContextList;
1539 while(*tmp_ctx)
1541 if(CompExchangePtr((XchgPtr*)tmp_ctx, context, context->next))
1542 break;
1543 tmp_ctx = &(*tmp_ctx)->next;
1545 UnlockDevice(device);
1547 ALCcontext_DecRef(context);
1550 void ALCcontext_IncRef(ALCcontext *context)
1552 RefCount ref;
1553 ref = IncrementRef(&context->ref);
1554 TRACEREF("%p increasing refcount to %u\n", context, ref);
1557 void ALCcontext_DecRef(ALCcontext *context)
1559 RefCount ref;
1560 ref = DecrementRef(&context->ref);
1561 TRACEREF("%p decreasing refcount to %u\n", context, ref);
1562 if(ref == 0) FreeContext(context);
1565 static void ReleaseThreadCtx(void *ptr)
1567 WARN("%p current for thread being destroyed\n", ptr);
1568 ALCcontext_DecRef(ptr);
1571 /* VerifyContext
1573 * Checks that the given context is valid, and increments its reference count.
1575 static ALCcontext *VerifyContext(ALCcontext *context)
1577 ALCdevice *dev;
1579 LockLists();
1580 dev = DeviceList;
1581 while(dev)
1583 ALCcontext *tmp_ctx = dev->ContextList;
1584 while(tmp_ctx)
1586 if(tmp_ctx == context)
1588 ALCcontext_IncRef(tmp_ctx);
1589 UnlockLists();
1590 return tmp_ctx;
1592 tmp_ctx = tmp_ctx->next;
1594 dev = dev->next;
1596 UnlockLists();
1598 return NULL;
1602 /* GetContextRef
1604 * Returns the currently active context, and adds a reference without locking
1605 * it.
1607 ALCcontext *GetContextRef(void)
1609 ALCcontext *context;
1611 context = pthread_getspecific(LocalContext);
1612 if(context)
1613 ALCcontext_IncRef(context);
1614 else
1616 LockLists();
1617 context = GlobalContext;
1618 if(context)
1619 ALCcontext_IncRef(context);
1620 UnlockLists();
1623 return context;
1626 ///////////////////////////////////////////////////////
1629 ///////////////////////////////////////////////////////
1630 // ALC Functions calls
1633 // This should probably move to another c file but for now ...
1634 ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
1636 ALCdevice *device = NULL;
1637 ALCenum err;
1639 DO_INITCONFIG();
1641 if(!CaptureBackend.name)
1643 alcSetError(NULL, ALC_INVALID_VALUE);
1644 return NULL;
1647 if(SampleSize <= 0)
1649 alcSetError(NULL, ALC_INVALID_VALUE);
1650 return NULL;
1653 if(deviceName && (!deviceName[0] || strcasecmp(deviceName, alcDefaultName) == 0 || strcasecmp(deviceName, "openal-soft") == 0))
1654 deviceName = NULL;
1656 device = calloc(1, sizeof(ALCdevice));
1657 if(!device)
1659 alcSetError(NULL, ALC_OUT_OF_MEMORY);
1660 return NULL;
1663 //Validate device
1664 device->Funcs = &CaptureBackend.Funcs;
1665 device->ref = 1;
1666 device->Connected = ALC_TRUE;
1667 device->Type = Capture;
1668 InitializeCriticalSection(&device->Mutex);
1670 InitUIntMap(&device->BufferMap, ~0);
1671 InitUIntMap(&device->EffectMap, ~0);
1672 InitUIntMap(&device->FilterMap, ~0);
1674 device->szDeviceName = NULL;
1676 device->Flags |= DEVICE_FREQUENCY_REQUEST;
1677 device->Frequency = frequency;
1679 device->Flags |= DEVICE_CHANNELS_REQUEST | DEVICE_SAMPLE_TYPE_REQUEST;
1680 if(DecomposeDevFormat(format, &device->FmtChans, &device->FmtType) == AL_FALSE)
1682 DeleteCriticalSection(&device->Mutex);
1683 free(device);
1684 alcSetError(NULL, ALC_INVALID_ENUM);
1685 return NULL;
1688 device->UpdateSize = SampleSize;
1689 device->NumUpdates = 1;
1691 LockLists();
1692 if((err=ALCdevice_OpenCapture(device, deviceName)) != ALC_NO_ERROR)
1694 UnlockLists();
1695 DeleteCriticalSection(&device->Mutex);
1696 free(device);
1697 alcSetError(NULL, err);
1698 return NULL;
1700 UnlockLists();
1702 do {
1703 device->next = DeviceList;
1704 } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
1706 TRACE("Created device %p\n", device);
1707 return device;
1710 ALC_API ALCboolean ALC_APIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
1712 ALCdevice *volatile*list;
1714 LockLists();
1715 list = &DeviceList;
1716 while(*list && *list != pDevice)
1717 list = &(*list)->next;
1719 if(!*list || (*list)->Type != Capture)
1721 alcSetError(*list, ALC_INVALID_DEVICE);
1722 UnlockLists();
1723 return ALC_FALSE;
1726 *list = (*list)->next;
1727 UnlockLists();
1729 LockDevice(pDevice);
1730 ALCdevice_CloseCapture(pDevice);
1731 UnlockDevice(pDevice);
1733 ALCdevice_DecRef(pDevice);
1735 return ALC_TRUE;
1738 ALC_API void ALC_APIENTRY alcCaptureStart(ALCdevice *device)
1740 if(!(device=VerifyDevice(device)) || device->Type != Capture)
1742 alcSetError(device, ALC_INVALID_DEVICE);
1743 if(device) ALCdevice_DecRef(device);
1744 return;
1746 LockDevice(device);
1747 if(device->Connected)
1748 ALCdevice_StartCapture(device);
1749 UnlockDevice(device);
1751 ALCdevice_DecRef(device);
1754 ALC_API void ALC_APIENTRY alcCaptureStop(ALCdevice *device)
1756 if(!(device=VerifyDevice(device)) || device->Type != Capture)
1758 alcSetError(device, ALC_INVALID_DEVICE);
1759 if(device) ALCdevice_DecRef(device);
1760 return;
1762 LockDevice(device);
1763 if(device->Connected)
1764 ALCdevice_StopCapture(device);
1765 UnlockDevice(device);
1767 ALCdevice_DecRef(device);
1770 ALC_API void ALC_APIENTRY alcCaptureSamples(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
1772 ALCenum err = ALC_INVALID_DEVICE;
1773 if((device=VerifyDevice(device)) != NULL && device->Type == Capture)
1775 err = ALC_INVALID_VALUE;
1776 LockDevice(device);
1777 if(samples >= 0 && ALCdevice_AvailableSamples(device) >= (ALCuint)samples)
1778 err = ALCdevice_CaptureSamples(device, buffer, samples);
1779 UnlockDevice(device);
1781 if(err != ALC_NO_ERROR)
1782 alcSetError(device, err);
1783 if(device) ALCdevice_DecRef(device);
1787 alcGetError
1789 Return last ALC generated error code
1791 ALC_API ALCenum ALC_APIENTRY alcGetError(ALCdevice *device)
1793 ALCenum errorCode;
1795 if(VerifyDevice(device))
1797 errorCode = ExchangeInt(&device->LastError, ALC_NO_ERROR);
1798 ALCdevice_DecRef(device);
1800 else
1801 errorCode = ExchangeInt(&g_eLastNullDeviceError, ALC_NO_ERROR);
1803 return errorCode;
1807 /* alcSuspendContext
1809 * Not functional
1811 ALC_API ALCvoid ALC_APIENTRY alcSuspendContext(ALCcontext *Context)
1813 (void)Context;
1816 /* alcProcessContext
1818 * Not functional
1820 ALC_API ALCvoid ALC_APIENTRY alcProcessContext(ALCcontext *Context)
1822 (void)Context;
1826 /* alcGetString
1828 * Returns information about the Device, and error strings
1830 ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
1832 const ALCchar *value = NULL;
1834 switch(param)
1836 case ALC_NO_ERROR:
1837 value = alcNoError;
1838 break;
1840 case ALC_INVALID_ENUM:
1841 value = alcErrInvalidEnum;
1842 break;
1844 case ALC_INVALID_VALUE:
1845 value = alcErrInvalidValue;
1846 break;
1848 case ALC_INVALID_DEVICE:
1849 value = alcErrInvalidDevice;
1850 break;
1852 case ALC_INVALID_CONTEXT:
1853 value = alcErrInvalidContext;
1854 break;
1856 case ALC_OUT_OF_MEMORY:
1857 value = alcErrOutOfMemory;
1858 break;
1860 case ALC_DEVICE_SPECIFIER:
1861 value = alcDefaultName;
1862 break;
1864 case ALC_ALL_DEVICES_SPECIFIER:
1865 if(VerifyDevice(pDevice))
1867 value = pDevice->szDeviceName;
1868 ALCdevice_DecRef(pDevice);
1870 else
1872 ProbeAllDeviceList();
1873 value = alcAllDeviceList;
1875 break;
1877 case ALC_CAPTURE_DEVICE_SPECIFIER:
1878 if(VerifyDevice(pDevice))
1880 value = pDevice->szDeviceName;
1881 ALCdevice_DecRef(pDevice);
1883 else
1885 ProbeCaptureDeviceList();
1886 value = alcCaptureDeviceList;
1888 break;
1890 /* Default devices are always first in the list */
1891 case ALC_DEFAULT_DEVICE_SPECIFIER:
1892 value = alcDefaultName;
1893 break;
1895 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
1896 if(!alcAllDeviceList)
1897 ProbeAllDeviceList();
1899 pDevice = VerifyDevice(pDevice);
1901 free(alcDefaultAllDeviceSpecifier);
1902 alcDefaultAllDeviceSpecifier = strdup(alcAllDeviceList ?
1903 alcAllDeviceList : "");
1904 if(!alcDefaultAllDeviceSpecifier)
1905 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
1907 value = alcDefaultAllDeviceSpecifier;
1908 if(pDevice) ALCdevice_DecRef(pDevice);
1909 break;
1911 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
1912 if(!alcCaptureDeviceList)
1913 ProbeCaptureDeviceList();
1915 pDevice = VerifyDevice(pDevice);
1917 free(alcCaptureDefaultDeviceSpecifier);
1918 alcCaptureDefaultDeviceSpecifier = strdup(alcCaptureDeviceList ?
1919 alcCaptureDeviceList : "");
1920 if(!alcCaptureDefaultDeviceSpecifier)
1921 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
1923 value = alcCaptureDefaultDeviceSpecifier;
1924 if(pDevice) ALCdevice_DecRef(pDevice);
1925 break;
1927 case ALC_EXTENSIONS:
1928 if(!VerifyDevice(pDevice))
1929 value = alcNoDeviceExtList;
1930 else
1932 value = alcExtensionList;
1933 ALCdevice_DecRef(pDevice);
1935 break;
1937 default:
1938 pDevice = VerifyDevice(pDevice);
1939 alcSetError(pDevice, ALC_INVALID_ENUM);
1940 if(pDevice) ALCdevice_DecRef(pDevice);
1941 break;
1944 return value;
1948 /* alcGetIntegerv
1950 * Returns information about the Device and the version of Open AL
1952 ALC_API ALCvoid ALC_APIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
1954 device = VerifyDevice(device);
1956 if(size == 0 || data == NULL)
1958 alcSetError(device, ALC_INVALID_VALUE);
1959 if(device) ALCdevice_DecRef(device);
1960 return;
1963 if(!device)
1965 switch(param)
1967 case ALC_MAJOR_VERSION:
1968 *data = alcMajorVersion;
1969 break;
1970 case ALC_MINOR_VERSION:
1971 *data = alcMinorVersion;
1972 break;
1974 case ALC_ATTRIBUTES_SIZE:
1975 case ALC_ALL_ATTRIBUTES:
1976 case ALC_FREQUENCY:
1977 case ALC_REFRESH:
1978 case ALC_SYNC:
1979 case ALC_MONO_SOURCES:
1980 case ALC_STEREO_SOURCES:
1981 case ALC_CAPTURE_SAMPLES:
1982 case ALC_FORMAT_CHANNELS_SOFT:
1983 case ALC_FORMAT_TYPE_SOFT:
1984 alcSetError(NULL, ALC_INVALID_DEVICE);
1985 break;
1987 default:
1988 alcSetError(NULL, ALC_INVALID_ENUM);
1989 break;
1992 else if(device->Type == Capture)
1994 switch(param)
1996 case ALC_CAPTURE_SAMPLES:
1997 LockDevice(device);
1998 *data = ALCdevice_AvailableSamples(device);
1999 UnlockDevice(device);
2000 break;
2002 case ALC_CONNECTED:
2003 *data = device->Connected;
2004 break;
2006 default:
2007 alcSetError(device, ALC_INVALID_ENUM);
2008 break;
2011 else /* render device */
2013 switch(param)
2015 case ALC_MAJOR_VERSION:
2016 *data = alcMajorVersion;
2017 break;
2019 case ALC_MINOR_VERSION:
2020 *data = alcMinorVersion;
2021 break;
2023 case ALC_EFX_MAJOR_VERSION:
2024 *data = alcEFXMajorVersion;
2025 break;
2027 case ALC_EFX_MINOR_VERSION:
2028 *data = alcEFXMinorVersion;
2029 break;
2031 case ALC_ATTRIBUTES_SIZE:
2032 *data = 13;
2033 break;
2035 case ALC_ALL_ATTRIBUTES:
2036 if(size < 13)
2037 alcSetError(device, ALC_INVALID_VALUE);
2038 else
2040 int i = 0;
2042 data[i++] = ALC_FREQUENCY;
2043 data[i++] = device->Frequency;
2045 if(device->Type != Loopback)
2047 data[i++] = ALC_REFRESH;
2048 data[i++] = device->Frequency / device->UpdateSize;
2050 data[i++] = ALC_SYNC;
2051 data[i++] = ALC_FALSE;
2053 else
2055 data[i++] = ALC_FORMAT_CHANNELS_SOFT;
2056 data[i++] = device->FmtChans;
2058 data[i++] = ALC_FORMAT_TYPE_SOFT;
2059 data[i++] = device->FmtType;
2062 data[i++] = ALC_MONO_SOURCES;
2063 data[i++] = device->NumMonoSources;
2065 data[i++] = ALC_STEREO_SOURCES;
2066 data[i++] = device->NumStereoSources;
2068 data[i++] = ALC_MAX_AUXILIARY_SENDS;
2069 data[i++] = device->NumAuxSends;
2071 data[i++] = 0;
2073 break;
2075 case ALC_FREQUENCY:
2076 *data = device->Frequency;
2077 break;
2079 case ALC_REFRESH:
2080 if(device->Type == Loopback)
2081 alcSetError(device, ALC_INVALID_DEVICE);
2082 else
2083 *data = device->Frequency / device->UpdateSize;
2084 break;
2086 case ALC_SYNC:
2087 if(device->Type == Loopback)
2088 alcSetError(device, ALC_INVALID_DEVICE);
2089 else
2090 *data = ALC_FALSE;
2091 break;
2093 case ALC_FORMAT_CHANNELS_SOFT:
2094 if(device->Type != Loopback)
2095 alcSetError(device, ALC_INVALID_DEVICE);
2096 else
2097 *data = device->FmtChans;
2098 break;
2100 case ALC_FORMAT_TYPE_SOFT:
2101 if(device->Type != Loopback)
2102 alcSetError(device, ALC_INVALID_DEVICE);
2103 else
2104 *data = device->FmtType;
2105 break;
2107 case ALC_MONO_SOURCES:
2108 *data = device->NumMonoSources;
2109 break;
2111 case ALC_STEREO_SOURCES:
2112 *data = device->NumStereoSources;
2113 break;
2115 case ALC_MAX_AUXILIARY_SENDS:
2116 *data = device->NumAuxSends;
2117 break;
2119 case ALC_CONNECTED:
2120 *data = device->Connected;
2121 break;
2123 default:
2124 alcSetError(device, ALC_INVALID_ENUM);
2125 break;
2128 if(device)
2129 ALCdevice_DecRef(device);
2133 /* alcIsExtensionPresent
2135 * Determines if there is support for a particular extension
2137 ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
2139 ALCboolean bResult = ALC_FALSE;
2141 device = VerifyDevice(device);
2143 if(!extName)
2144 alcSetError(device, ALC_INVALID_VALUE);
2145 else
2147 size_t len = strlen(extName);
2148 const char *ptr = (device ? alcExtensionList : alcNoDeviceExtList);
2149 while(ptr && *ptr)
2151 if(strncasecmp(ptr, extName, len) == 0 &&
2152 (ptr[len] == '\0' || isspace(ptr[len])))
2154 bResult = ALC_TRUE;
2155 break;
2157 if((ptr=strchr(ptr, ' ')) != NULL)
2159 do {
2160 ++ptr;
2161 } while(isspace(*ptr));
2165 if(device)
2166 ALCdevice_DecRef(device);
2167 return bResult;
2171 /* alcGetProcAddress
2173 * Retrieves the function address for a particular extension function
2175 ALC_API ALCvoid* ALC_APIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
2177 ALCvoid *ptr = NULL;
2179 device = VerifyDevice(device);
2181 if(!funcName)
2182 alcSetError(device, ALC_INVALID_VALUE);
2183 else
2185 ALsizei i = 0;
2186 while(alcFunctions[i].funcName && strcmp(alcFunctions[i].funcName,funcName) != 0)
2187 i++;
2188 ptr = alcFunctions[i].address;
2190 if(device)
2191 ALCdevice_DecRef(device);
2192 return ptr;
2196 /* alcGetEnumValue
2198 * Get the value for a particular ALC Enumerated Value
2200 ALC_API ALCenum ALC_APIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
2202 ALCenum val = 0;
2204 device = VerifyDevice(device);
2206 if(!enumName)
2207 alcSetError(device, ALC_INVALID_VALUE);
2208 else
2210 ALsizei i = 0;
2211 while(enumeration[i].enumName && strcmp(enumeration[i].enumName,enumName) != 0)
2212 i++;
2213 val = enumeration[i].value;
2215 if(device)
2216 ALCdevice_DecRef(device);
2217 return val;
2221 /* alcCreateContext
2223 * Create and attach a Context to a particular Device.
2225 ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
2227 ALCcontext *ALContext;
2228 ALCenum err;
2230 LockLists();
2231 if(!(device=VerifyDevice(device)) || device->Type == Capture || !device->Connected)
2233 UnlockLists();
2234 alcSetError(device, ALC_INVALID_DEVICE);
2235 if(device) ALCdevice_DecRef(device);
2236 return NULL;
2239 /* Reset Context Last Error code */
2240 device->LastError = ALC_NO_ERROR;
2242 if((err=UpdateDeviceParams(device, attrList)) != ALC_NO_ERROR)
2244 UnlockLists();
2245 alcSetError(device, err);
2246 if(err == ALC_INVALID_DEVICE)
2247 aluHandleDisconnect(device);
2248 ALCdevice_DecRef(device);
2249 return NULL;
2252 ALContext = calloc(1, sizeof(ALCcontext));
2253 if(ALContext)
2255 ALContext->ref = 1;
2257 ALContext->MaxActiveSources = 256;
2258 ALContext->ActiveSources = malloc(sizeof(ALContext->ActiveSources[0]) *
2259 ALContext->MaxActiveSources);
2261 if(!ALContext || !ALContext->ActiveSources)
2263 if(!device->ContextList)
2265 ALCdevice_StopPlayback(device);
2266 device->Flags &= ~DEVICE_RUNNING;
2268 UnlockLists();
2270 free(ALContext);
2271 ALContext = NULL;
2273 alcSetError(device, ALC_OUT_OF_MEMORY);
2274 ALCdevice_DecRef(device);
2275 return NULL;
2278 ALContext->Device = device;
2279 ALCdevice_IncRef(device);
2280 InitContext(ALContext);
2282 do {
2283 ALContext->next = device->ContextList;
2284 } while(!CompExchangePtr((XchgPtr*)&device->ContextList, ALContext->next, ALContext));
2285 UnlockLists();
2287 if(device->DefaultSlot)
2288 InitializeEffect(ALContext, device->DefaultSlot, &ForcedEffect);
2289 ALContext->LastError = AL_NO_ERROR;
2291 ALCdevice_DecRef(device);
2293 TRACE("Created context %p\n", ALContext);
2294 return ALContext;
2297 /* alcDestroyContext
2299 * Remove a Context
2301 ALC_API ALCvoid ALC_APIENTRY alcDestroyContext(ALCcontext *context)
2303 ALCdevice *Device;
2305 LockLists();
2306 /* alcGetContextsDevice sets an error for invalid contexts */
2307 Device = alcGetContextsDevice(context);
2308 if(Device)
2310 ReleaseContext(context, Device);
2311 if(!Device->ContextList)
2313 ALCdevice_StopPlayback(Device);
2314 Device->Flags &= ~DEVICE_RUNNING;
2317 UnlockLists();
2321 /* alcGetCurrentContext
2323 * Returns the currently active Context
2325 ALC_API ALCcontext* ALC_APIENTRY alcGetCurrentContext(ALCvoid)
2327 ALCcontext *Context;
2329 Context = pthread_getspecific(LocalContext);
2330 if(!Context) Context = GlobalContext;
2332 return Context;
2335 /* alcGetThreadContext
2337 * Returns the currently active thread-local Context
2339 ALC_API ALCcontext* ALC_APIENTRY alcGetThreadContext(void)
2341 ALCcontext *Context;
2342 Context = pthread_getspecific(LocalContext);
2343 return Context;
2347 /* alcMakeContextCurrent
2349 * Makes the given Context the active Context
2351 ALC_API ALCboolean ALC_APIENTRY alcMakeContextCurrent(ALCcontext *context)
2353 /* context must be a valid Context or NULL */
2354 if(context && !(context=VerifyContext(context)))
2356 alcSetError(NULL, ALC_INVALID_CONTEXT);
2357 return ALC_FALSE;
2359 /* context's reference count is already incremented */
2360 context = ExchangePtr((XchgPtr*)&GlobalContext, context);
2361 if(context) ALCcontext_DecRef(context);
2363 if((context=pthread_getspecific(LocalContext)) != NULL)
2365 pthread_setspecific(LocalContext, NULL);
2366 ALCcontext_DecRef(context);
2369 return ALC_TRUE;
2372 /* alcSetThreadContext
2374 * Makes the given Context the active Context for the current thread
2376 ALC_API ALCboolean ALC_APIENTRY alcSetThreadContext(ALCcontext *context)
2378 ALCcontext *old;
2380 /* context must be a valid Context or NULL */
2381 if(context && !(context=VerifyContext(context)))
2383 alcSetError(NULL, ALC_INVALID_CONTEXT);
2384 return ALC_FALSE;
2386 /* context's reference count is already incremented */
2387 old = pthread_getspecific(LocalContext);
2388 pthread_setspecific(LocalContext, context);
2389 if(old) ALCcontext_DecRef(old);
2391 return ALC_TRUE;
2395 /* alcGetContextsDevice
2397 * Returns the Device that a particular Context is attached to
2399 ALC_API ALCdevice* ALC_APIENTRY alcGetContextsDevice(ALCcontext *Context)
2401 ALCdevice *Device;
2403 if(!(Context=VerifyContext(Context)))
2405 alcSetError(NULL, ALC_INVALID_CONTEXT);
2406 return NULL;
2408 Device = Context->Device;
2409 ALCcontext_DecRef(Context);
2411 return Device;
2415 /* alcOpenDevice
2417 * Open the Device specified.
2419 ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName)
2421 const ALCchar *fmt;
2422 ALCdevice *device;
2423 ALCenum err;
2425 DO_INITCONFIG();
2427 if(!PlaybackBackend.name)
2429 alcSetError(NULL, ALC_INVALID_VALUE);
2430 return NULL;
2433 if(deviceName && (!deviceName[0] || strcasecmp(deviceName, alcDefaultName) == 0 || strcasecmp(deviceName, "openal-soft") == 0))
2434 deviceName = NULL;
2436 device = calloc(1, sizeof(ALCdevice)+sizeof(ALeffectslot));
2437 if(!device)
2439 alcSetError(NULL, ALC_OUT_OF_MEMORY);
2440 return NULL;
2443 //Validate device
2444 device->Funcs = &PlaybackBackend.Funcs;
2445 device->ref = 1;
2446 device->Connected = ALC_TRUE;
2447 device->Type = Playback;
2448 InitializeCriticalSection(&device->Mutex);
2449 device->LastError = ALC_NO_ERROR;
2451 device->Flags = 0;
2452 device->Bs2b = NULL;
2453 device->Bs2bLevel = 0;
2454 device->szDeviceName = NULL;
2456 device->ContextList = NULL;
2458 device->MaxNoOfSources = 256;
2459 device->AuxiliaryEffectSlotMax = 4;
2460 device->NumAuxSends = MAX_SENDS;
2462 InitUIntMap(&device->BufferMap, ~0);
2463 InitUIntMap(&device->EffectMap, ~0);
2464 InitUIntMap(&device->FilterMap, ~0);
2466 //Set output format
2467 device->FmtChans = DevFmtStereo;
2468 device->FmtType = DevFmtFloat;
2469 device->Frequency = DEFAULT_OUTPUT_RATE;
2470 device->NumUpdates = 4;
2471 device->UpdateSize = 1024;
2473 if(ConfigValueStr(NULL, "channels", &fmt))
2475 static const struct {
2476 const char name[16];
2477 enum DevFmtChannels chans;
2478 } chanlist[] = {
2479 { "mono", DevFmtMono },
2480 { "stereo", DevFmtStereo },
2481 { "quad", DevFmtQuad },
2482 { "surround51", DevFmtX51 },
2483 { "surround61", DevFmtX61 },
2484 { "surround71", DevFmtX71 },
2486 size_t i;
2488 for(i = 0;i < COUNTOF(chanlist);i++)
2490 if(strcasecmp(chanlist[i].name, fmt) == 0)
2492 device->FmtChans = chanlist[i].chans;
2493 device->Flags |= DEVICE_CHANNELS_REQUEST;
2494 break;
2497 if(i == COUNTOF(chanlist))
2498 ERR("Unsupported channels: %s\n", fmt);
2500 if(ConfigValueStr(NULL, "sample-type", &fmt))
2502 static const struct {
2503 const char name[16];
2504 enum DevFmtType type;
2505 } typelist[] = {
2506 { "int8", DevFmtByte },
2507 { "uint8", DevFmtUByte },
2508 { "int16", DevFmtShort },
2509 { "uint16", DevFmtUShort },
2510 { "int32", DevFmtInt },
2511 { "uint32", DevFmtUInt },
2512 { "float32", DevFmtFloat },
2514 size_t i;
2516 for(i = 0;i < COUNTOF(typelist);i++)
2518 if(strcasecmp(typelist[i].name, fmt) == 0)
2520 device->FmtType = typelist[i].type;
2521 device->Flags |= DEVICE_SAMPLE_TYPE_REQUEST;
2522 break;
2525 if(i == COUNTOF(typelist))
2526 ERR("Unsupported sample-type: %s\n", fmt);
2528 #define DEVICE_FORMAT_REQUEST (DEVICE_CHANNELS_REQUEST|DEVICE_SAMPLE_TYPE_REQUEST)
2529 if((device->Flags&DEVICE_FORMAT_REQUEST) != DEVICE_FORMAT_REQUEST &&
2530 ConfigValueStr(NULL, "format", &fmt))
2532 static const struct {
2533 const char name[32];
2534 enum DevFmtChannels channels;
2535 enum DevFmtType type;
2536 } formats[] = {
2537 { "AL_FORMAT_MONO32", DevFmtMono, DevFmtFloat },
2538 { "AL_FORMAT_STEREO32", DevFmtStereo, DevFmtFloat },
2539 { "AL_FORMAT_QUAD32", DevFmtQuad, DevFmtFloat },
2540 { "AL_FORMAT_51CHN32", DevFmtX51, DevFmtFloat },
2541 { "AL_FORMAT_61CHN32", DevFmtX61, DevFmtFloat },
2542 { "AL_FORMAT_71CHN32", DevFmtX71, DevFmtFloat },
2544 { "AL_FORMAT_MONO16", DevFmtMono, DevFmtShort },
2545 { "AL_FORMAT_STEREO16", DevFmtStereo, DevFmtShort },
2546 { "AL_FORMAT_QUAD16", DevFmtQuad, DevFmtShort },
2547 { "AL_FORMAT_51CHN16", DevFmtX51, DevFmtShort },
2548 { "AL_FORMAT_61CHN16", DevFmtX61, DevFmtShort },
2549 { "AL_FORMAT_71CHN16", DevFmtX71, DevFmtShort },
2551 { "AL_FORMAT_MONO8", DevFmtMono, DevFmtByte },
2552 { "AL_FORMAT_STEREO8", DevFmtStereo, DevFmtByte },
2553 { "AL_FORMAT_QUAD8", DevFmtQuad, DevFmtByte },
2554 { "AL_FORMAT_51CHN8", DevFmtX51, DevFmtByte },
2555 { "AL_FORMAT_61CHN8", DevFmtX61, DevFmtByte },
2556 { "AL_FORMAT_71CHN8", DevFmtX71, DevFmtByte }
2558 size_t i;
2560 ERR("Option 'format' is deprecated, please use 'channels' and 'sample-type'\n");
2561 for(i = 0;i < COUNTOF(formats);i++)
2563 if(strcasecmp(fmt, formats[i].name) == 0)
2565 if(!(device->Flags&DEVICE_CHANNELS_REQUEST))
2566 device->FmtChans = formats[i].channels;
2567 if(!(device->Flags&DEVICE_SAMPLE_TYPE_REQUEST))
2568 device->FmtType = formats[i].type;
2569 device->Flags |= DEVICE_FORMAT_REQUEST;
2570 break;
2573 if(i == COUNTOF(formats))
2574 ERR("Unsupported format: %s\n", fmt);
2576 #undef DEVICE_FORMAT_REQUEST
2578 if(ConfigValueUInt(NULL, "frequency", &device->Frequency))
2580 device->Flags |= DEVICE_FREQUENCY_REQUEST;
2581 if(device->Frequency < MIN_OUTPUT_RATE)
2582 ERR("%uhz request clamped to %uhz minimum\n", device->Frequency, MIN_OUTPUT_RATE);
2583 device->Frequency = maxu(device->Frequency, MIN_OUTPUT_RATE);
2586 ConfigValueUInt(NULL, "periods", &device->NumUpdates);
2587 device->NumUpdates = clampu(device->NumUpdates, 2, 16);
2589 ConfigValueUInt(NULL, "period_size", &device->UpdateSize);
2590 device->UpdateSize = clampu(device->UpdateSize, 64, 8192);
2592 ConfigValueUInt(NULL, "sources", &device->MaxNoOfSources);
2593 if(device->MaxNoOfSources == 0) device->MaxNoOfSources = 256;
2595 ConfigValueUInt(NULL, "slots", &device->AuxiliaryEffectSlotMax);
2596 if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
2598 ConfigValueUInt(NULL, "sends", &device->NumAuxSends);
2599 if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
2601 ConfigValueInt(NULL, "cf_level", &device->Bs2bLevel);
2603 device->NumStereoSources = 1;
2604 device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
2606 if(ForcedEffect.type != AL_EFFECT_NULL)
2608 device->DefaultSlot = (ALeffectslot*)(device+1);
2609 if(InitEffectSlot(device->DefaultSlot) != AL_NO_ERROR)
2610 device->DefaultSlot = NULL;
2613 // Find a playback device to open
2614 LockLists();
2615 if((err=ALCdevice_OpenPlayback(device, deviceName)) != ALC_NO_ERROR)
2617 UnlockLists();
2618 DeleteCriticalSection(&device->Mutex);
2619 free(device);
2620 alcSetError(NULL, err);
2621 return NULL;
2623 UnlockLists();
2625 do {
2626 device->next = DeviceList;
2627 } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
2629 TRACE("Created device %p, \"%s\"\n", device, device->szDeviceName);
2630 return device;
2633 /* alcCloseDevice
2635 * Close the specified Device
2637 ALC_API ALCboolean ALC_APIENTRY alcCloseDevice(ALCdevice *pDevice)
2639 ALCdevice *volatile*list;
2640 ALCcontext *ctx;
2642 LockLists();
2643 list = &DeviceList;
2644 while(*list && *list != pDevice)
2645 list = &(*list)->next;
2647 if(!*list || (*list)->Type == Capture)
2649 alcSetError(*list, ALC_INVALID_DEVICE);
2650 UnlockLists();
2651 return ALC_FALSE;
2654 *list = (*list)->next;
2655 UnlockLists();
2657 while((ctx=pDevice->ContextList) != NULL)
2659 WARN("Releasing context %p\n", ctx);
2660 ReleaseContext(ctx, pDevice);
2662 if((pDevice->Flags&DEVICE_RUNNING))
2663 ALCdevice_StopPlayback(pDevice);
2664 pDevice->Flags &= ~DEVICE_RUNNING;
2666 ALCdevice_ClosePlayback(pDevice);
2668 ALCdevice_DecRef(pDevice);
2670 return ALC_TRUE;
2674 /* alcLoopbackOpenDeviceSOFT
2676 * Open a loopback device, for manual rendering.
2678 ALC_API ALCdevice* ALC_APIENTRY alcLoopbackOpenDeviceSOFT(const ALCchar *deviceName)
2680 ALCdevice *device;
2682 DO_INITCONFIG();
2684 /* Make sure the device name, if specified, is us. */
2685 if(deviceName && strcmp(deviceName, alcDefaultName) != 0)
2687 alcSetError(NULL, ALC_INVALID_VALUE);
2688 return NULL;
2691 device = calloc(1, sizeof(ALCdevice));
2692 if(!device)
2694 alcSetError(NULL, ALC_OUT_OF_MEMORY);
2695 return NULL;
2698 //Validate device
2699 device->Funcs = &BackendLoopback.Funcs;
2700 device->ref = 1;
2701 device->Connected = ALC_TRUE;
2702 device->Type = Loopback;
2703 InitializeCriticalSection(&device->Mutex);
2704 device->LastError = ALC_NO_ERROR;
2706 device->Flags = 0;
2707 device->Bs2b = NULL;
2708 device->Bs2bLevel = 0;
2709 device->szDeviceName = NULL;
2711 device->ContextList = NULL;
2713 device->MaxNoOfSources = 256;
2714 device->AuxiliaryEffectSlotMax = 4;
2715 device->NumAuxSends = MAX_SENDS;
2717 InitUIntMap(&device->BufferMap, ~0);
2718 InitUIntMap(&device->EffectMap, ~0);
2719 InitUIntMap(&device->FilterMap, ~0);
2721 //Set output format
2722 device->NumUpdates = 0;
2723 device->UpdateSize = 0;
2725 device->Frequency = 44100;
2726 device->FmtChans = DevFmtStereo;
2727 device->FmtType = DevFmtShort;
2729 ConfigValueUInt(NULL, "sources", &device->MaxNoOfSources);
2730 if(device->MaxNoOfSources == 0) device->MaxNoOfSources = 256;
2732 ConfigValueUInt(NULL, "slots", &device->AuxiliaryEffectSlotMax);
2733 if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
2735 ConfigValueUInt(NULL, "sends", &device->NumAuxSends);
2736 if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
2738 device->NumStereoSources = 1;
2739 device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
2741 // Open the "backend"
2742 ALCdevice_OpenPlayback(device, "Loopback");
2743 do {
2744 device->next = DeviceList;
2745 } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
2747 TRACE("Created device %p\n", device);
2748 return device;
2751 /* alcIsRenderFormatSupportedSOFT
2753 * Determines if the loopback device supports the given format for rendering.
2755 ALC_API ALCboolean ALC_APIENTRY alcIsRenderFormatSupportedSOFT(ALCdevice *device, ALCsizei freq, ALCenum channels, ALCenum type)
2757 ALCboolean ret = ALC_FALSE;
2759 if(!(device=VerifyDevice(device)) || device->Type != Loopback)
2760 alcSetError(device, ALC_INVALID_DEVICE);
2761 else if(freq <= 0)
2762 alcSetError(device, ALC_INVALID_VALUE);
2763 else if(!IsValidALCType(type) || !IsValidALCChannels(channels))
2764 alcSetError(device, ALC_INVALID_ENUM);
2765 else
2767 if(BytesFromDevFmt(type) > 0 && ChannelsFromDevFmt(channels) > 0 &&
2768 freq >= MIN_OUTPUT_RATE)
2769 ret = ALC_TRUE;
2771 if(device) ALCdevice_DecRef(device);
2773 return ret;
2776 /* alcRenderSamplesSOFT
2778 * Renders some samples into a buffer, using the format last set by the
2779 * attributes given to alcCreateContext.
2781 ALC_API void ALC_APIENTRY alcRenderSamplesSOFT(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
2783 if(!(device=VerifyDevice(device)) || device->Type != Loopback)
2784 alcSetError(device, ALC_INVALID_DEVICE);
2785 else if(samples < 0 || (samples > 0 && buffer == NULL))
2786 alcSetError(device, ALC_INVALID_VALUE);
2787 else
2788 aluMixData(device, buffer, samples);
2789 if(device) ALCdevice_DecRef(device);
2793 static void ReleaseALC(void)
2795 ALCdevice *dev;
2797 free(alcAllDeviceList); alcAllDeviceList = NULL;
2798 alcAllDeviceListSize = 0;
2799 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
2800 alcCaptureDeviceListSize = 0;
2802 free(alcDefaultAllDeviceSpecifier);
2803 alcDefaultAllDeviceSpecifier = NULL;
2804 free(alcCaptureDefaultDeviceSpecifier);
2805 alcCaptureDefaultDeviceSpecifier = NULL;
2807 if((dev=ExchangePtr((XchgPtr*)&DeviceList, NULL)) != NULL)
2809 ALCuint num = 0;
2810 do {
2811 num++;
2812 } while((dev=dev->next) != NULL);
2813 ERR("%u device%s not closed\n", num, (num>1)?"s":"");
2817 ///////////////////////////////////////////////////////