Make sure the device gets stopped when closing even if there's no contexts
[openal-soft.git] / Alc / ALc.c
blob35c5607d4153f84983b9697ee0dd350b1daeef6c
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 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;
1077 while(attrList[attrIdx])
1079 if(attrList[attrIdx] == ALC_FORMAT_CHANNELS_SOFT)
1081 ALCint val = attrList[attrIdx + 1];
1082 if(!IsValidALCChannels(val) || !ChannelsFromDevFmt(val))
1083 return ALC_INVALID_VALUE;
1084 schans = val;
1085 gotFmt |= GotChans;
1088 if(attrList[attrIdx] == ALC_FORMAT_TYPE_SOFT)
1090 ALCint val = attrList[attrIdx + 1];
1091 if(!IsValidALCType(val) || !BytesFromDevFmt(val))
1092 return ALC_INVALID_VALUE;
1093 stype = val;
1094 gotFmt |= GotType;
1097 if(attrList[attrIdx] == ALC_FREQUENCY)
1099 freq = attrList[attrIdx + 1];
1100 if(freq < MIN_OUTPUT_RATE)
1101 return ALC_INVALID_VALUE;
1102 gotFmt |= GotFreq;
1105 if(attrList[attrIdx] == ALC_STEREO_SOURCES)
1107 numStereo = attrList[attrIdx + 1];
1108 if(numStereo > device->MaxNoOfSources)
1109 numStereo = device->MaxNoOfSources;
1111 numMono = device->MaxNoOfSources - numStereo;
1114 if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS)
1115 numSends = attrList[attrIdx + 1];
1117 attrIdx += 2;
1120 if(gotFmt != GotAll)
1122 WARN("Missing format for loopback device\n");
1123 return ALC_INVALID_VALUE;
1126 ConfigValueUInt(NULL, "sends", &numSends);
1127 numSends = minu(MAX_SENDS, numSends);
1129 if((device->Flags&DEVICE_RUNNING))
1130 ALCdevice_StopPlayback(device);
1131 device->Flags &= ~DEVICE_RUNNING;
1133 device->Frequency = freq;
1134 device->FmtChans = schans;
1135 device->FmtType = stype;
1136 device->NumMonoSources = numMono;
1137 device->NumStereoSources = numStereo;
1138 device->NumAuxSends = numSends;
1140 else if(attrList && attrList[0])
1142 ALCuint freq, numMono, numStereo, numSends;
1143 ALCuint attrIdx = 0;
1145 /* If a context is already running on the device, stop playback so the
1146 * device attributes can be updated. */
1147 if((device->Flags&DEVICE_RUNNING))
1148 ALCdevice_StopPlayback(device);
1149 device->Flags &= ~DEVICE_RUNNING;
1151 freq = device->Frequency;
1152 numMono = device->NumMonoSources;
1153 numStereo = device->NumStereoSources;
1154 numSends = device->NumAuxSends;
1156 while(attrList[attrIdx])
1158 if(attrList[attrIdx] == ALC_FREQUENCY)
1160 freq = attrList[attrIdx + 1];
1161 device->Flags |= DEVICE_FREQUENCY_REQUEST;
1164 if(attrList[attrIdx] == ALC_STEREO_SOURCES)
1166 numStereo = attrList[attrIdx + 1];
1167 if(numStereo > device->MaxNoOfSources)
1168 numStereo = device->MaxNoOfSources;
1170 numMono = device->MaxNoOfSources - numStereo;
1173 if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS)
1174 numSends = attrList[attrIdx + 1];
1176 attrIdx += 2;
1179 ConfigValueUInt(NULL, "frequency", &freq);
1180 freq = maxu(freq, MIN_OUTPUT_RATE);
1182 ConfigValueUInt(NULL, "sends", &numSends);
1183 numSends = minu(MAX_SENDS, numSends);
1185 device->UpdateSize = (ALuint64)device->UpdateSize * freq /
1186 device->Frequency;
1188 device->Frequency = freq;
1189 device->NumMonoSources = numMono;
1190 device->NumStereoSources = numStereo;
1191 device->NumAuxSends = numSends;
1194 if((device->Flags&DEVICE_RUNNING))
1195 return ALC_NO_ERROR;
1197 LockDevice(device);
1199 oldFreq = device->Frequency;
1200 oldChans = device->FmtChans;
1201 oldType = device->FmtType;
1203 TRACE("Format pre-setup: %s%s, %s%s, %uhz%s, %u update size x%d\n",
1204 DevFmtChannelsString(device->FmtChans),
1205 (device->Flags&DEVICE_CHANNELS_REQUEST)?" (requested)":"",
1206 DevFmtTypeString(device->FmtType),
1207 (device->Flags&DEVICE_SAMPLE_TYPE_REQUEST)?" (requested)":"",
1208 device->Frequency,
1209 (device->Flags&DEVICE_FREQUENCY_REQUEST)?" (requested)":"",
1210 device->UpdateSize, device->NumUpdates);
1211 if(ALCdevice_ResetPlayback(device) == ALC_FALSE)
1213 UnlockDevice(device);
1214 return ALC_INVALID_DEVICE;
1216 device->Flags |= DEVICE_RUNNING;
1218 if(device->FmtChans != oldChans && (device->Flags&DEVICE_CHANNELS_REQUEST))
1220 ERR("Failed to set %s, got %s instead\n", DevFmtChannelsString(oldChans),
1221 DevFmtChannelsString(device->FmtChans));
1222 device->Flags &= ~DEVICE_CHANNELS_REQUEST;
1224 if(device->FmtType != oldType && (device->Flags&DEVICE_SAMPLE_TYPE_REQUEST))
1226 ERR("Failed to set %s, got %s instead\n", DevFmtTypeString(oldType),
1227 DevFmtTypeString(device->FmtType));
1228 device->Flags &= ~DEVICE_SAMPLE_TYPE_REQUEST;
1230 if(device->Frequency != oldFreq && (device->Flags&DEVICE_FREQUENCY_REQUEST))
1232 ERR("Failed to set %uhz, got %uhz instead\n", oldFreq, device->Frequency);
1233 device->Flags &= ~DEVICE_FREQUENCY_REQUEST;
1236 TRACE("Format post-setup: %s, %s, %uhz, %u update size x%d\n",
1237 DevFmtChannelsString(device->FmtChans),
1238 DevFmtTypeString(device->FmtType), device->Frequency,
1239 device->UpdateSize, device->NumUpdates);
1241 aluInitPanning(device);
1243 for(i = 0;i < MAXCHANNELS;i++)
1245 device->ClickRemoval[i] = 0.0f;
1246 device->PendingClicks[i] = 0.0f;
1249 device->Hrtf = NULL;
1250 if(device->Type != Loopback && GetConfigValueBool(NULL, "hrtf", AL_FALSE))
1251 device->Hrtf = GetHrtf(device);
1252 TRACE("HRTF %s\n", device->Hrtf?"enabled":"disabled");
1254 if(!device->Hrtf && device->Bs2bLevel > 0 && device->Bs2bLevel <= 6)
1256 if(!device->Bs2b)
1258 device->Bs2b = calloc(1, sizeof(*device->Bs2b));
1259 bs2b_clear(device->Bs2b);
1261 bs2b_set_srate(device->Bs2b, device->Frequency);
1262 bs2b_set_level(device->Bs2b, device->Bs2bLevel);
1263 TRACE("BS2B level %d\n", device->Bs2bLevel);
1265 else
1267 free(device->Bs2b);
1268 device->Bs2b = NULL;
1269 TRACE("BS2B disabled\n");
1272 device->Flags &= ~DEVICE_DUPLICATE_STEREO;
1273 switch(device->FmtChans)
1275 case DevFmtMono:
1276 case DevFmtStereo:
1277 break;
1278 case DevFmtQuad:
1279 case DevFmtX51:
1280 case DevFmtX51Side:
1281 case DevFmtX61:
1282 case DevFmtX71:
1283 if(GetConfigValueBool(NULL, "stereodup", AL_TRUE))
1284 device->Flags |= DEVICE_DUPLICATE_STEREO;
1285 break;
1287 TRACE("Stereo duplication %s\n", (device->Flags&DEVICE_DUPLICATE_STEREO)?"enabled":"disabled");
1289 oldMode = SetMixerFPUMode();
1290 context = device->ContextList;
1291 while(context)
1293 ALsizei pos;
1295 context->UpdateSources = AL_FALSE;
1296 LockUIntMapRead(&context->EffectSlotMap);
1297 for(pos = 0;pos < context->EffectSlotMap.size;pos++)
1299 ALeffectslot *slot = context->EffectSlotMap.array[pos].value;
1301 if(ALeffectState_DeviceUpdate(slot->EffectState, device) == AL_FALSE)
1303 UnlockUIntMapRead(&context->EffectSlotMap);
1304 RestoreFPUMode(oldMode);
1305 UnlockDevice(device);
1306 ALCdevice_StopPlayback(device);
1307 device->Flags &= ~DEVICE_RUNNING;
1308 return ALC_INVALID_DEVICE;
1310 slot->NeedsUpdate = AL_FALSE;
1311 ALeffectState_Update(slot->EffectState, context, slot);
1313 UnlockUIntMapRead(&context->EffectSlotMap);
1315 LockUIntMapRead(&context->SourceMap);
1316 for(pos = 0;pos < context->SourceMap.size;pos++)
1318 ALsource *source = context->SourceMap.array[pos].value;
1319 ALuint s = device->NumAuxSends;
1320 while(s < MAX_SENDS)
1322 if(source->Send[s].Slot)
1323 DecrementRef(&source->Send[s].Slot->ref);
1324 source->Send[s].Slot = NULL;
1325 source->Send[s].WetGain = 1.0f;
1326 source->Send[s].WetGainHF = 1.0f;
1327 s++;
1329 source->NeedsUpdate = AL_FALSE;
1330 ALsource_Update(source, context);
1332 UnlockUIntMapRead(&context->SourceMap);
1334 context = context->next;
1336 RestoreFPUMode(oldMode);
1337 UnlockDevice(device);
1339 return ALC_NO_ERROR;
1342 /* FreeDevice
1344 * Frees the device structure, and destroys any objects the app failed to
1345 * delete. Called once there's no more references on the device.
1347 static ALCvoid FreeDevice(ALCdevice *device)
1349 TRACE("%p\n", device);
1351 if(device->DefaultSlot)
1353 ALeffectState_Destroy(device->DefaultSlot->EffectState);
1354 device->DefaultSlot->EffectState = NULL;
1357 if(device->BufferMap.size > 0)
1359 WARN("(%p) Deleting %d Buffer(s)\n", device, device->BufferMap.size);
1360 ReleaseALBuffers(device);
1362 ResetUIntMap(&device->BufferMap);
1364 if(device->EffectMap.size > 0)
1366 WARN("(%p) Deleting %d Effect(s)\n", device, device->EffectMap.size);
1367 ReleaseALEffects(device);
1369 ResetUIntMap(&device->EffectMap);
1371 if(device->FilterMap.size > 0)
1373 WARN("(%p) Deleting %d Filter(s)\n", device, device->FilterMap.size);
1374 ReleaseALFilters(device);
1376 ResetUIntMap(&device->FilterMap);
1378 free(device->Bs2b);
1379 device->Bs2b = NULL;
1381 free(device->szDeviceName);
1382 device->szDeviceName = NULL;
1384 DeleteCriticalSection(&device->Mutex);
1386 free(device);
1390 void ALCdevice_IncRef(ALCdevice *device)
1392 RefCount ref;
1393 ref = IncrementRef(&device->ref);
1394 TRACEREF("%p increasing refcount to %u\n", device, ref);
1397 void ALCdevice_DecRef(ALCdevice *device)
1399 RefCount ref;
1400 ref = DecrementRef(&device->ref);
1401 TRACEREF("%p decreasing refcount to %u\n", device, ref);
1402 if(ref == 0) FreeDevice(device);
1405 /* VerifyDevice
1407 * Checks if the device handle is valid, and increments its ref count if so.
1409 static ALCdevice *VerifyDevice(ALCdevice *device)
1411 ALCdevice *tmpDevice;
1413 if(!device)
1414 return NULL;
1416 LockLists();
1417 tmpDevice = DeviceList;
1418 while(tmpDevice && tmpDevice != device)
1419 tmpDevice = tmpDevice->next;
1421 if(tmpDevice)
1422 ALCdevice_IncRef(tmpDevice);
1423 UnlockLists();
1424 return tmpDevice;
1428 /* InitContext
1430 * Initializes context variables
1432 static ALvoid InitContext(ALCcontext *pContext)
1434 ALint i, j;
1436 //Initialise listener
1437 pContext->Listener.Gain = 1.0f;
1438 pContext->Listener.MetersPerUnit = 1.0f;
1439 pContext->Listener.Position[0] = 0.0f;
1440 pContext->Listener.Position[1] = 0.0f;
1441 pContext->Listener.Position[2] = 0.0f;
1442 pContext->Listener.Velocity[0] = 0.0f;
1443 pContext->Listener.Velocity[1] = 0.0f;
1444 pContext->Listener.Velocity[2] = 0.0f;
1445 pContext->Listener.Forward[0] = 0.0f;
1446 pContext->Listener.Forward[1] = 0.0f;
1447 pContext->Listener.Forward[2] = -1.0f;
1448 pContext->Listener.Up[0] = 0.0f;
1449 pContext->Listener.Up[1] = 1.0f;
1450 pContext->Listener.Up[2] = 0.0f;
1451 for(i = 0;i < 4;i++)
1453 for(j = 0;j < 4;j++)
1454 pContext->Listener.Matrix[i][j] = ((i==j) ? 1.0f : 0.0f);
1457 //Validate pContext
1458 pContext->LastError = AL_NO_ERROR;
1459 pContext->UpdateSources = AL_FALSE;
1460 pContext->ActiveSourceCount = 0;
1461 InitUIntMap(&pContext->SourceMap, pContext->Device->MaxNoOfSources);
1462 InitUIntMap(&pContext->EffectSlotMap, pContext->Device->AuxiliaryEffectSlotMax);
1464 //Set globals
1465 pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
1466 pContext->SourceDistanceModel = AL_FALSE;
1467 pContext->DopplerFactor = 1.0f;
1468 pContext->DopplerVelocity = 1.0f;
1469 pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
1470 pContext->DeferUpdates = AL_FALSE;
1472 pContext->ExtensionList = alExtList;
1476 /* FreeContext
1478 * Cleans up the context, and destroys any remaining objects the app failed to
1479 * delete. Called once there's no more references on the context.
1481 static ALCvoid FreeContext(ALCcontext *context)
1483 TRACE("%p\n", context);
1485 if(context->SourceMap.size > 0)
1487 ERR("(%p) Deleting %d Source(s)\n", context, context->SourceMap.size);
1488 ReleaseALSources(context);
1490 ResetUIntMap(&context->SourceMap);
1492 if(context->EffectSlotMap.size > 0)
1494 ERR("(%p) Deleting %d AuxiliaryEffectSlot(s)\n", context, context->EffectSlotMap.size);
1495 ReleaseALAuxiliaryEffectSlots(context);
1497 ResetUIntMap(&context->EffectSlotMap);
1499 context->ActiveSourceCount = 0;
1500 free(context->ActiveSources);
1501 context->ActiveSources = NULL;
1502 context->MaxActiveSources = 0;
1504 context->ActiveEffectSlotCount = 0;
1505 free(context->ActiveEffectSlots);
1506 context->ActiveEffectSlots = NULL;
1507 context->MaxActiveEffectSlots = 0;
1509 ALCdevice_DecRef(context->Device);
1510 context->Device = NULL;
1512 //Invalidate context
1513 memset(context, 0, sizeof(ALCcontext));
1514 free(context);
1517 /* ReleaseContext
1519 * Removes the context reference from the given device and removes it from
1520 * being current on the running thread or globally.
1522 static void ReleaseContext(ALCcontext *context, ALCdevice *device)
1524 ALCcontext *volatile*tmp_ctx;
1526 if(pthread_getspecific(LocalContext) == context)
1528 WARN("%p released while current on thread\n", context);
1529 pthread_setspecific(LocalContext, NULL);
1530 ALCcontext_DecRef(context);
1533 if(CompExchangePtr((XchgPtr*)&GlobalContext, context, NULL))
1534 ALCcontext_DecRef(context);
1536 LockDevice(device);
1537 tmp_ctx = &device->ContextList;
1538 while(*tmp_ctx)
1540 if(CompExchangePtr((XchgPtr*)tmp_ctx, context, context->next))
1541 break;
1542 tmp_ctx = &(*tmp_ctx)->next;
1544 UnlockDevice(device);
1546 ALCcontext_DecRef(context);
1549 void ALCcontext_IncRef(ALCcontext *context)
1551 RefCount ref;
1552 ref = IncrementRef(&context->ref);
1553 TRACEREF("%p increasing refcount to %u\n", context, ref);
1556 void ALCcontext_DecRef(ALCcontext *context)
1558 RefCount ref;
1559 ref = DecrementRef(&context->ref);
1560 TRACEREF("%p decreasing refcount to %u\n", context, ref);
1561 if(ref == 0) FreeContext(context);
1564 static void ReleaseThreadCtx(void *ptr)
1566 WARN("%p current for thread being destroyed\n", ptr);
1567 ALCcontext_DecRef(ptr);
1570 /* VerifyContext
1572 * Checks that the given context is valid, and increments its reference count.
1574 static ALCcontext *VerifyContext(ALCcontext *context)
1576 ALCdevice *dev;
1578 LockLists();
1579 dev = DeviceList;
1580 while(dev)
1582 ALCcontext *tmp_ctx = dev->ContextList;
1583 while(tmp_ctx)
1585 if(tmp_ctx == context)
1587 ALCcontext_IncRef(tmp_ctx);
1588 UnlockLists();
1589 return tmp_ctx;
1591 tmp_ctx = tmp_ctx->next;
1593 dev = dev->next;
1595 UnlockLists();
1597 return NULL;
1601 /* GetContextRef
1603 * Returns the currently active context, and adds a reference without locking
1604 * it.
1606 ALCcontext *GetContextRef(void)
1608 ALCcontext *context;
1610 context = pthread_getspecific(LocalContext);
1611 if(context)
1612 ALCcontext_IncRef(context);
1613 else
1615 LockLists();
1616 context = GlobalContext;
1617 if(context)
1618 ALCcontext_IncRef(context);
1619 UnlockLists();
1622 return context;
1625 ///////////////////////////////////////////////////////
1628 ///////////////////////////////////////////////////////
1629 // ALC Functions calls
1632 // This should probably move to another c file but for now ...
1633 ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
1635 ALCdevice *device = NULL;
1636 ALCenum err;
1638 DO_INITCONFIG();
1640 if(!CaptureBackend.name)
1642 alcSetError(NULL, ALC_INVALID_VALUE);
1643 return NULL;
1646 if(SampleSize <= 0)
1648 alcSetError(NULL, ALC_INVALID_VALUE);
1649 return NULL;
1652 if(deviceName && (!deviceName[0] || strcasecmp(deviceName, alcDefaultName) == 0 || strcasecmp(deviceName, "openal-soft") == 0))
1653 deviceName = NULL;
1655 device = calloc(1, sizeof(ALCdevice));
1656 if(!device)
1658 alcSetError(NULL, ALC_OUT_OF_MEMORY);
1659 return NULL;
1662 //Validate device
1663 device->Funcs = &CaptureBackend.Funcs;
1664 device->ref = 1;
1665 device->Connected = ALC_TRUE;
1666 device->Type = Capture;
1667 InitializeCriticalSection(&device->Mutex);
1669 InitUIntMap(&device->BufferMap, ~0);
1670 InitUIntMap(&device->EffectMap, ~0);
1671 InitUIntMap(&device->FilterMap, ~0);
1673 device->szDeviceName = NULL;
1675 device->Flags |= DEVICE_FREQUENCY_REQUEST;
1676 device->Frequency = frequency;
1678 device->Flags |= DEVICE_CHANNELS_REQUEST | DEVICE_SAMPLE_TYPE_REQUEST;
1679 if(DecomposeDevFormat(format, &device->FmtChans, &device->FmtType) == AL_FALSE)
1681 DeleteCriticalSection(&device->Mutex);
1682 free(device);
1683 alcSetError(NULL, ALC_INVALID_ENUM);
1684 return NULL;
1687 device->UpdateSize = SampleSize;
1688 device->NumUpdates = 1;
1690 LockLists();
1691 if((err=ALCdevice_OpenCapture(device, deviceName)) != ALC_NO_ERROR)
1693 UnlockLists();
1694 DeleteCriticalSection(&device->Mutex);
1695 free(device);
1696 alcSetError(NULL, err);
1697 return NULL;
1699 UnlockLists();
1701 do {
1702 device->next = DeviceList;
1703 } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
1705 TRACE("Created device %p\n", device);
1706 return device;
1709 ALC_API ALCboolean ALC_APIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
1711 ALCdevice *volatile*list;
1713 LockLists();
1714 list = &DeviceList;
1715 while(*list && *list != pDevice)
1716 list = &(*list)->next;
1718 if(!*list || (*list)->Type != Capture)
1720 alcSetError(*list, ALC_INVALID_DEVICE);
1721 UnlockLists();
1722 return ALC_FALSE;
1725 *list = (*list)->next;
1726 UnlockLists();
1728 LockDevice(pDevice);
1729 ALCdevice_CloseCapture(pDevice);
1730 UnlockDevice(pDevice);
1732 ALCdevice_DecRef(pDevice);
1734 return ALC_TRUE;
1737 ALC_API void ALC_APIENTRY alcCaptureStart(ALCdevice *device)
1739 if(!(device=VerifyDevice(device)) || device->Type != Capture)
1741 alcSetError(device, ALC_INVALID_DEVICE);
1742 if(device) ALCdevice_DecRef(device);
1743 return;
1745 LockDevice(device);
1746 if(device->Connected)
1747 ALCdevice_StartCapture(device);
1748 UnlockDevice(device);
1750 ALCdevice_DecRef(device);
1753 ALC_API void ALC_APIENTRY alcCaptureStop(ALCdevice *device)
1755 if(!(device=VerifyDevice(device)) || device->Type != Capture)
1757 alcSetError(device, ALC_INVALID_DEVICE);
1758 if(device) ALCdevice_DecRef(device);
1759 return;
1761 LockDevice(device);
1762 if(device->Connected)
1763 ALCdevice_StopCapture(device);
1764 UnlockDevice(device);
1766 ALCdevice_DecRef(device);
1769 ALC_API void ALC_APIENTRY alcCaptureSamples(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
1771 ALCenum err = ALC_INVALID_DEVICE;
1772 if((device=VerifyDevice(device)) != NULL && device->Type == Capture)
1774 err = ALC_INVALID_VALUE;
1775 LockDevice(device);
1776 if(samples >= 0 && ALCdevice_AvailableSamples(device) >= (ALCuint)samples)
1777 err = ALCdevice_CaptureSamples(device, buffer, samples);
1778 UnlockDevice(device);
1780 if(err != ALC_NO_ERROR)
1781 alcSetError(device, err);
1782 if(device) ALCdevice_DecRef(device);
1786 alcGetError
1788 Return last ALC generated error code
1790 ALC_API ALCenum ALC_APIENTRY alcGetError(ALCdevice *device)
1792 ALCenum errorCode;
1794 if(VerifyDevice(device))
1796 errorCode = ExchangeInt(&device->LastError, ALC_NO_ERROR);
1797 ALCdevice_DecRef(device);
1799 else
1800 errorCode = ExchangeInt(&g_eLastNullDeviceError, ALC_NO_ERROR);
1802 return errorCode;
1806 /* alcSuspendContext
1808 * Not functional
1810 ALC_API ALCvoid ALC_APIENTRY alcSuspendContext(ALCcontext *Context)
1812 (void)Context;
1815 /* alcProcessContext
1817 * Not functional
1819 ALC_API ALCvoid ALC_APIENTRY alcProcessContext(ALCcontext *Context)
1821 (void)Context;
1825 /* alcGetString
1827 * Returns information about the Device, and error strings
1829 ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
1831 const ALCchar *value = NULL;
1833 switch(param)
1835 case ALC_NO_ERROR:
1836 value = alcNoError;
1837 break;
1839 case ALC_INVALID_ENUM:
1840 value = alcErrInvalidEnum;
1841 break;
1843 case ALC_INVALID_VALUE:
1844 value = alcErrInvalidValue;
1845 break;
1847 case ALC_INVALID_DEVICE:
1848 value = alcErrInvalidDevice;
1849 break;
1851 case ALC_INVALID_CONTEXT:
1852 value = alcErrInvalidContext;
1853 break;
1855 case ALC_OUT_OF_MEMORY:
1856 value = alcErrOutOfMemory;
1857 break;
1859 case ALC_DEVICE_SPECIFIER:
1860 value = alcDefaultName;
1861 break;
1863 case ALC_ALL_DEVICES_SPECIFIER:
1864 if(VerifyDevice(pDevice))
1866 value = pDevice->szDeviceName;
1867 ALCdevice_DecRef(pDevice);
1869 else
1871 ProbeAllDeviceList();
1872 value = alcAllDeviceList;
1874 break;
1876 case ALC_CAPTURE_DEVICE_SPECIFIER:
1877 if(VerifyDevice(pDevice))
1879 value = pDevice->szDeviceName;
1880 ALCdevice_DecRef(pDevice);
1882 else
1884 ProbeCaptureDeviceList();
1885 value = alcCaptureDeviceList;
1887 break;
1889 /* Default devices are always first in the list */
1890 case ALC_DEFAULT_DEVICE_SPECIFIER:
1891 value = alcDefaultName;
1892 break;
1894 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
1895 if(!alcAllDeviceList)
1896 ProbeAllDeviceList();
1898 pDevice = VerifyDevice(pDevice);
1900 free(alcDefaultAllDeviceSpecifier);
1901 alcDefaultAllDeviceSpecifier = strdup(alcAllDeviceList ?
1902 alcAllDeviceList : "");
1903 if(!alcDefaultAllDeviceSpecifier)
1904 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
1906 value = alcDefaultAllDeviceSpecifier;
1907 if(pDevice) ALCdevice_DecRef(pDevice);
1908 break;
1910 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
1911 if(!alcCaptureDeviceList)
1912 ProbeCaptureDeviceList();
1914 pDevice = VerifyDevice(pDevice);
1916 free(alcCaptureDefaultDeviceSpecifier);
1917 alcCaptureDefaultDeviceSpecifier = strdup(alcCaptureDeviceList ?
1918 alcCaptureDeviceList : "");
1919 if(!alcCaptureDefaultDeviceSpecifier)
1920 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
1922 value = alcCaptureDefaultDeviceSpecifier;
1923 if(pDevice) ALCdevice_DecRef(pDevice);
1924 break;
1926 case ALC_EXTENSIONS:
1927 if(!VerifyDevice(pDevice))
1928 value = alcNoDeviceExtList;
1929 else
1931 value = alcExtensionList;
1932 ALCdevice_DecRef(pDevice);
1934 break;
1936 default:
1937 pDevice = VerifyDevice(pDevice);
1938 alcSetError(pDevice, ALC_INVALID_ENUM);
1939 if(pDevice) ALCdevice_DecRef(pDevice);
1940 break;
1943 return value;
1947 /* alcGetIntegerv
1949 * Returns information about the Device and the version of Open AL
1951 ALC_API ALCvoid ALC_APIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
1953 device = VerifyDevice(device);
1955 if(size == 0 || data == NULL)
1957 alcSetError(device, ALC_INVALID_VALUE);
1958 if(device) ALCdevice_DecRef(device);
1959 return;
1962 if(!device)
1964 switch(param)
1966 case ALC_MAJOR_VERSION:
1967 *data = alcMajorVersion;
1968 break;
1969 case ALC_MINOR_VERSION:
1970 *data = alcMinorVersion;
1971 break;
1973 case ALC_ATTRIBUTES_SIZE:
1974 case ALC_ALL_ATTRIBUTES:
1975 case ALC_FREQUENCY:
1976 case ALC_REFRESH:
1977 case ALC_SYNC:
1978 case ALC_MONO_SOURCES:
1979 case ALC_STEREO_SOURCES:
1980 case ALC_CAPTURE_SAMPLES:
1981 case ALC_FORMAT_CHANNELS_SOFT:
1982 case ALC_FORMAT_TYPE_SOFT:
1983 alcSetError(NULL, ALC_INVALID_DEVICE);
1984 break;
1986 default:
1987 alcSetError(NULL, ALC_INVALID_ENUM);
1988 break;
1991 else if(device->Type == Capture)
1993 switch(param)
1995 case ALC_CAPTURE_SAMPLES:
1996 LockDevice(device);
1997 *data = ALCdevice_AvailableSamples(device);
1998 UnlockDevice(device);
1999 break;
2001 case ALC_CONNECTED:
2002 *data = device->Connected;
2003 break;
2005 default:
2006 alcSetError(device, ALC_INVALID_ENUM);
2007 break;
2010 else /* render device */
2012 switch(param)
2014 case ALC_MAJOR_VERSION:
2015 *data = alcMajorVersion;
2016 break;
2018 case ALC_MINOR_VERSION:
2019 *data = alcMinorVersion;
2020 break;
2022 case ALC_EFX_MAJOR_VERSION:
2023 *data = alcEFXMajorVersion;
2024 break;
2026 case ALC_EFX_MINOR_VERSION:
2027 *data = alcEFXMinorVersion;
2028 break;
2030 case ALC_ATTRIBUTES_SIZE:
2031 *data = 13;
2032 break;
2034 case ALC_ALL_ATTRIBUTES:
2035 if(size < 13)
2036 alcSetError(device, ALC_INVALID_VALUE);
2037 else
2039 int i = 0;
2041 data[i++] = ALC_FREQUENCY;
2042 data[i++] = device->Frequency;
2044 if(device->Type != Loopback)
2046 data[i++] = ALC_REFRESH;
2047 data[i++] = device->Frequency / device->UpdateSize;
2049 data[i++] = ALC_SYNC;
2050 data[i++] = ALC_FALSE;
2052 else
2054 data[i++] = ALC_FORMAT_CHANNELS_SOFT;
2055 data[i++] = device->FmtChans;
2057 data[i++] = ALC_FORMAT_TYPE_SOFT;
2058 data[i++] = device->FmtType;
2061 data[i++] = ALC_MONO_SOURCES;
2062 data[i++] = device->NumMonoSources;
2064 data[i++] = ALC_STEREO_SOURCES;
2065 data[i++] = device->NumStereoSources;
2067 data[i++] = ALC_MAX_AUXILIARY_SENDS;
2068 data[i++] = device->NumAuxSends;
2070 data[i++] = 0;
2072 break;
2074 case ALC_FREQUENCY:
2075 *data = device->Frequency;
2076 break;
2078 case ALC_REFRESH:
2079 if(device->Type == Loopback)
2080 alcSetError(device, ALC_INVALID_DEVICE);
2081 else
2082 *data = device->Frequency / device->UpdateSize;
2083 break;
2085 case ALC_SYNC:
2086 if(device->Type == Loopback)
2087 alcSetError(device, ALC_INVALID_DEVICE);
2088 else
2089 *data = ALC_FALSE;
2090 break;
2092 case ALC_FORMAT_CHANNELS_SOFT:
2093 if(device->Type != Loopback)
2094 alcSetError(device, ALC_INVALID_DEVICE);
2095 else
2096 *data = device->FmtChans;
2097 break;
2099 case ALC_FORMAT_TYPE_SOFT:
2100 if(device->Type != Loopback)
2101 alcSetError(device, ALC_INVALID_DEVICE);
2102 else
2103 *data = device->FmtType;
2104 break;
2106 case ALC_MONO_SOURCES:
2107 *data = device->NumMonoSources;
2108 break;
2110 case ALC_STEREO_SOURCES:
2111 *data = device->NumStereoSources;
2112 break;
2114 case ALC_MAX_AUXILIARY_SENDS:
2115 *data = device->NumAuxSends;
2116 break;
2118 case ALC_CONNECTED:
2119 *data = device->Connected;
2120 break;
2122 default:
2123 alcSetError(device, ALC_INVALID_ENUM);
2124 break;
2127 if(device)
2128 ALCdevice_DecRef(device);
2132 /* alcIsExtensionPresent
2134 * Determines if there is support for a particular extension
2136 ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
2138 ALCboolean bResult = ALC_FALSE;
2140 device = VerifyDevice(device);
2142 if(!extName)
2143 alcSetError(device, ALC_INVALID_VALUE);
2144 else
2146 size_t len = strlen(extName);
2147 const char *ptr = (device ? alcExtensionList : alcNoDeviceExtList);
2148 while(ptr && *ptr)
2150 if(strncasecmp(ptr, extName, len) == 0 &&
2151 (ptr[len] == '\0' || isspace(ptr[len])))
2153 bResult = ALC_TRUE;
2154 break;
2156 if((ptr=strchr(ptr, ' ')) != NULL)
2158 do {
2159 ++ptr;
2160 } while(isspace(*ptr));
2164 if(device)
2165 ALCdevice_DecRef(device);
2166 return bResult;
2170 /* alcGetProcAddress
2172 * Retrieves the function address for a particular extension function
2174 ALC_API ALCvoid* ALC_APIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
2176 ALCvoid *ptr = NULL;
2178 device = VerifyDevice(device);
2180 if(!funcName)
2181 alcSetError(device, ALC_INVALID_VALUE);
2182 else
2184 ALsizei i = 0;
2185 while(alcFunctions[i].funcName && strcmp(alcFunctions[i].funcName,funcName) != 0)
2186 i++;
2187 ptr = alcFunctions[i].address;
2189 if(device)
2190 ALCdevice_DecRef(device);
2191 return ptr;
2195 /* alcGetEnumValue
2197 * Get the value for a particular ALC Enumerated Value
2199 ALC_API ALCenum ALC_APIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
2201 ALCenum val = 0;
2203 device = VerifyDevice(device);
2205 if(!enumName)
2206 alcSetError(device, ALC_INVALID_VALUE);
2207 else
2209 ALsizei i = 0;
2210 while(enumeration[i].enumName && strcmp(enumeration[i].enumName,enumName) != 0)
2211 i++;
2212 val = enumeration[i].value;
2214 if(device)
2215 ALCdevice_DecRef(device);
2216 return val;
2220 /* alcCreateContext
2222 * Create and attach a Context to a particular Device.
2224 ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
2226 ALCcontext *ALContext;
2227 ALCenum err;
2229 LockLists();
2230 if(!(device=VerifyDevice(device)) || device->Type == Capture || !device->Connected)
2232 UnlockLists();
2233 alcSetError(device, ALC_INVALID_DEVICE);
2234 if(device) ALCdevice_DecRef(device);
2235 return NULL;
2238 /* Reset Context Last Error code */
2239 device->LastError = ALC_NO_ERROR;
2241 if((err=UpdateDeviceParams(device, attrList)) != ALC_NO_ERROR)
2243 UnlockLists();
2244 alcSetError(device, err);
2245 if(err == ALC_INVALID_DEVICE)
2246 aluHandleDisconnect(device);
2247 ALCdevice_DecRef(device);
2248 return NULL;
2251 ALContext = calloc(1, sizeof(ALCcontext));
2252 if(ALContext)
2254 ALContext->ref = 1;
2256 ALContext->MaxActiveSources = 256;
2257 ALContext->ActiveSources = malloc(sizeof(ALContext->ActiveSources[0]) *
2258 ALContext->MaxActiveSources);
2260 if(!ALContext || !ALContext->ActiveSources)
2262 if(!device->ContextList)
2264 ALCdevice_StopPlayback(device);
2265 device->Flags &= ~DEVICE_RUNNING;
2267 UnlockLists();
2269 free(ALContext);
2270 ALContext = NULL;
2272 alcSetError(device, ALC_OUT_OF_MEMORY);
2273 ALCdevice_DecRef(device);
2274 return NULL;
2277 ALContext->Device = device;
2278 ALCdevice_IncRef(device);
2279 InitContext(ALContext);
2281 do {
2282 ALContext->next = device->ContextList;
2283 } while(!CompExchangePtr((XchgPtr*)&device->ContextList, ALContext->next, ALContext));
2284 UnlockLists();
2286 if(device->DefaultSlot)
2287 InitializeEffect(ALContext, device->DefaultSlot, &ForcedEffect);
2288 ALContext->LastError = AL_NO_ERROR;
2290 ALCdevice_DecRef(device);
2292 TRACE("Created context %p\n", ALContext);
2293 return ALContext;
2296 /* alcDestroyContext
2298 * Remove a Context
2300 ALC_API ALCvoid ALC_APIENTRY alcDestroyContext(ALCcontext *context)
2302 ALCdevice *Device;
2304 LockLists();
2305 /* alcGetContextsDevice sets an error for invalid contexts */
2306 Device = alcGetContextsDevice(context);
2307 if(Device)
2309 ReleaseContext(context, Device);
2310 if(!Device->ContextList)
2312 ALCdevice_StopPlayback(Device);
2313 Device->Flags &= ~DEVICE_RUNNING;
2316 UnlockLists();
2320 /* alcGetCurrentContext
2322 * Returns the currently active Context
2324 ALC_API ALCcontext* ALC_APIENTRY alcGetCurrentContext(ALCvoid)
2326 ALCcontext *Context;
2328 Context = pthread_getspecific(LocalContext);
2329 if(!Context) Context = GlobalContext;
2331 return Context;
2334 /* alcGetThreadContext
2336 * Returns the currently active thread-local Context
2338 ALC_API ALCcontext* ALC_APIENTRY alcGetThreadContext(void)
2340 ALCcontext *Context;
2341 Context = pthread_getspecific(LocalContext);
2342 return Context;
2346 /* alcMakeContextCurrent
2348 * Makes the given Context the active Context
2350 ALC_API ALCboolean ALC_APIENTRY alcMakeContextCurrent(ALCcontext *context)
2352 /* context must be a valid Context or NULL */
2353 if(context && !(context=VerifyContext(context)))
2355 alcSetError(NULL, ALC_INVALID_CONTEXT);
2356 return ALC_FALSE;
2358 /* context's reference count is already incremented */
2359 context = ExchangePtr((XchgPtr*)&GlobalContext, context);
2360 if(context) ALCcontext_DecRef(context);
2362 if((context=pthread_getspecific(LocalContext)) != NULL)
2364 pthread_setspecific(LocalContext, NULL);
2365 ALCcontext_DecRef(context);
2368 return ALC_TRUE;
2371 /* alcSetThreadContext
2373 * Makes the given Context the active Context for the current thread
2375 ALC_API ALCboolean ALC_APIENTRY alcSetThreadContext(ALCcontext *context)
2377 ALCcontext *old;
2379 /* context must be a valid Context or NULL */
2380 if(context && !(context=VerifyContext(context)))
2382 alcSetError(NULL, ALC_INVALID_CONTEXT);
2383 return ALC_FALSE;
2385 /* context's reference count is already incremented */
2386 old = pthread_getspecific(LocalContext);
2387 pthread_setspecific(LocalContext, context);
2388 if(old) ALCcontext_DecRef(old);
2390 return ALC_TRUE;
2394 /* alcGetContextsDevice
2396 * Returns the Device that a particular Context is attached to
2398 ALC_API ALCdevice* ALC_APIENTRY alcGetContextsDevice(ALCcontext *Context)
2400 ALCdevice *Device;
2402 if(!(Context=VerifyContext(Context)))
2404 alcSetError(NULL, ALC_INVALID_CONTEXT);
2405 return NULL;
2407 Device = Context->Device;
2408 ALCcontext_DecRef(Context);
2410 return Device;
2414 /* alcOpenDevice
2416 * Open the Device specified.
2418 ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName)
2420 const ALCchar *fmt;
2421 ALCdevice *device;
2422 ALCenum err;
2424 DO_INITCONFIG();
2426 if(!PlaybackBackend.name)
2428 alcSetError(NULL, ALC_INVALID_VALUE);
2429 return NULL;
2432 if(deviceName && (!deviceName[0] || strcasecmp(deviceName, alcDefaultName) == 0 || strcasecmp(deviceName, "openal-soft") == 0))
2433 deviceName = NULL;
2435 device = calloc(1, sizeof(ALCdevice)+sizeof(ALeffectslot));
2436 if(!device)
2438 alcSetError(NULL, ALC_OUT_OF_MEMORY);
2439 return NULL;
2442 //Validate device
2443 device->Funcs = &PlaybackBackend.Funcs;
2444 device->ref = 1;
2445 device->Connected = ALC_TRUE;
2446 device->Type = Playback;
2447 InitializeCriticalSection(&device->Mutex);
2448 device->LastError = ALC_NO_ERROR;
2450 device->Flags = 0;
2451 device->Bs2b = NULL;
2452 device->Bs2bLevel = 0;
2453 device->szDeviceName = NULL;
2455 device->ContextList = NULL;
2457 device->MaxNoOfSources = 256;
2458 device->AuxiliaryEffectSlotMax = 4;
2459 device->NumAuxSends = MAX_SENDS;
2461 InitUIntMap(&device->BufferMap, ~0);
2462 InitUIntMap(&device->EffectMap, ~0);
2463 InitUIntMap(&device->FilterMap, ~0);
2465 //Set output format
2466 device->FmtChans = DevFmtStereo;
2467 device->FmtType = DevFmtFloat;
2468 device->Frequency = DEFAULT_OUTPUT_RATE;
2469 device->NumUpdates = 4;
2470 device->UpdateSize = 1024;
2472 if(ConfigValueStr(NULL, "channels", &fmt))
2474 static const struct {
2475 const char name[16];
2476 enum DevFmtChannels chans;
2477 } chanlist[] = {
2478 { "mono", DevFmtMono },
2479 { "stereo", DevFmtStereo },
2480 { "quad", DevFmtQuad },
2481 { "surround51", DevFmtX51 },
2482 { "surround61", DevFmtX61 },
2483 { "surround71", DevFmtX71 },
2485 size_t i;
2487 for(i = 0;i < COUNTOF(chanlist);i++)
2489 if(strcasecmp(chanlist[i].name, fmt) == 0)
2491 device->FmtChans = chanlist[i].chans;
2492 device->Flags |= DEVICE_CHANNELS_REQUEST;
2493 break;
2496 if(i == COUNTOF(chanlist))
2497 ERR("Unsupported channels: %s\n", fmt);
2499 if(ConfigValueStr(NULL, "sample-type", &fmt))
2501 static const struct {
2502 const char name[16];
2503 enum DevFmtType type;
2504 } typelist[] = {
2505 { "int8", DevFmtByte },
2506 { "uint8", DevFmtUByte },
2507 { "int16", DevFmtShort },
2508 { "uint16", DevFmtUShort },
2509 { "int32", DevFmtInt },
2510 { "uint32", DevFmtUInt },
2511 { "float32", DevFmtFloat },
2513 size_t i;
2515 for(i = 0;i < COUNTOF(typelist);i++)
2517 if(strcasecmp(typelist[i].name, fmt) == 0)
2519 device->FmtType = typelist[i].type;
2520 device->Flags |= DEVICE_SAMPLE_TYPE_REQUEST;
2521 break;
2524 if(i == COUNTOF(typelist))
2525 ERR("Unsupported sample-type: %s\n", fmt);
2527 #define DEVICE_FORMAT_REQUEST (DEVICE_CHANNELS_REQUEST|DEVICE_SAMPLE_TYPE_REQUEST)
2528 if((device->Flags&DEVICE_FORMAT_REQUEST) != DEVICE_FORMAT_REQUEST &&
2529 ConfigValueStr(NULL, "format", &fmt))
2531 static const struct {
2532 const char name[32];
2533 enum DevFmtChannels channels;
2534 enum DevFmtType type;
2535 } formats[] = {
2536 { "AL_FORMAT_MONO32", DevFmtMono, DevFmtFloat },
2537 { "AL_FORMAT_STEREO32", DevFmtStereo, DevFmtFloat },
2538 { "AL_FORMAT_QUAD32", DevFmtQuad, DevFmtFloat },
2539 { "AL_FORMAT_51CHN32", DevFmtX51, DevFmtFloat },
2540 { "AL_FORMAT_61CHN32", DevFmtX61, DevFmtFloat },
2541 { "AL_FORMAT_71CHN32", DevFmtX71, DevFmtFloat },
2543 { "AL_FORMAT_MONO16", DevFmtMono, DevFmtShort },
2544 { "AL_FORMAT_STEREO16", DevFmtStereo, DevFmtShort },
2545 { "AL_FORMAT_QUAD16", DevFmtQuad, DevFmtShort },
2546 { "AL_FORMAT_51CHN16", DevFmtX51, DevFmtShort },
2547 { "AL_FORMAT_61CHN16", DevFmtX61, DevFmtShort },
2548 { "AL_FORMAT_71CHN16", DevFmtX71, DevFmtShort },
2550 { "AL_FORMAT_MONO8", DevFmtMono, DevFmtByte },
2551 { "AL_FORMAT_STEREO8", DevFmtStereo, DevFmtByte },
2552 { "AL_FORMAT_QUAD8", DevFmtQuad, DevFmtByte },
2553 { "AL_FORMAT_51CHN8", DevFmtX51, DevFmtByte },
2554 { "AL_FORMAT_61CHN8", DevFmtX61, DevFmtByte },
2555 { "AL_FORMAT_71CHN8", DevFmtX71, DevFmtByte }
2557 size_t i;
2559 ERR("Option 'format' is deprecated, please use 'channels' and 'sample-type'\n");
2560 for(i = 0;i < COUNTOF(formats);i++)
2562 if(strcasecmp(fmt, formats[i].name) == 0)
2564 if(!(device->Flags&DEVICE_CHANNELS_REQUEST))
2565 device->FmtChans = formats[i].channels;
2566 if(!(device->Flags&DEVICE_SAMPLE_TYPE_REQUEST))
2567 device->FmtType = formats[i].type;
2568 device->Flags |= DEVICE_FORMAT_REQUEST;
2569 break;
2572 if(i == COUNTOF(formats))
2573 ERR("Unsupported format: %s\n", fmt);
2575 #undef DEVICE_FORMAT_REQUEST
2577 if(ConfigValueUInt(NULL, "frequency", &device->Frequency))
2579 device->Flags |= DEVICE_FREQUENCY_REQUEST;
2580 if(device->Frequency < MIN_OUTPUT_RATE)
2581 ERR("%uhz request clamped to %uhz minimum\n", device->Frequency, MIN_OUTPUT_RATE);
2582 device->Frequency = maxu(device->Frequency, MIN_OUTPUT_RATE);
2585 ConfigValueUInt(NULL, "periods", &device->NumUpdates);
2586 if(device->NumUpdates < 2) device->NumUpdates = 4;
2588 ConfigValueUInt(NULL, "period_size", &device->UpdateSize);
2589 if(device->UpdateSize == 0) device->UpdateSize = 1024;
2591 ConfigValueUInt(NULL, "sources", &device->MaxNoOfSources);
2592 if(device->MaxNoOfSources == 0) device->MaxNoOfSources = 256;
2594 ConfigValueUInt(NULL, "slots", &device->AuxiliaryEffectSlotMax);
2595 if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
2597 ConfigValueUInt(NULL, "sends", &device->NumAuxSends);
2598 if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
2600 ConfigValueInt(NULL, "cf_level", &device->Bs2bLevel);
2602 device->NumStereoSources = 1;
2603 device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
2605 if(ForcedEffect.type != AL_EFFECT_NULL)
2607 device->DefaultSlot = (ALeffectslot*)(device+1);
2608 if(InitEffectSlot(device->DefaultSlot) != AL_NO_ERROR)
2609 device->DefaultSlot = NULL;
2612 // Find a playback device to open
2613 LockLists();
2614 if((err=ALCdevice_OpenPlayback(device, deviceName)) != ALC_NO_ERROR)
2616 UnlockLists();
2617 DeleteCriticalSection(&device->Mutex);
2618 free(device);
2619 alcSetError(NULL, err);
2620 return NULL;
2622 UnlockLists();
2624 do {
2625 device->next = DeviceList;
2626 } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
2628 TRACE("Created device %p, \"%s\"\n", device, device->szDeviceName);
2629 return device;
2632 /* alcCloseDevice
2634 * Close the specified Device
2636 ALC_API ALCboolean ALC_APIENTRY alcCloseDevice(ALCdevice *pDevice)
2638 ALCdevice *volatile*list;
2639 ALCcontext *ctx;
2641 LockLists();
2642 list = &DeviceList;
2643 while(*list && *list != pDevice)
2644 list = &(*list)->next;
2646 if(!*list || (*list)->Type == Capture)
2648 alcSetError(*list, ALC_INVALID_DEVICE);
2649 UnlockLists();
2650 return ALC_FALSE;
2653 *list = (*list)->next;
2654 UnlockLists();
2656 while((ctx=pDevice->ContextList) != NULL)
2658 WARN("Releasing context %p\n", ctx);
2659 ReleaseContext(ctx, pDevice);
2661 if((pDevice->Flags&DEVICE_RUNNING))
2662 ALCdevice_StopPlayback(pDevice);
2663 pDevice->Flags &= ~DEVICE_RUNNING;
2665 ALCdevice_ClosePlayback(pDevice);
2667 ALCdevice_DecRef(pDevice);
2669 return ALC_TRUE;
2673 /* alcLoopbackOpenDeviceSOFT
2675 * Open a loopback device, for manual rendering.
2677 ALC_API ALCdevice* ALC_APIENTRY alcLoopbackOpenDeviceSOFT(const ALCchar *deviceName)
2679 ALCdevice *device;
2681 DO_INITCONFIG();
2683 /* Make sure the device name, if specified, is us. */
2684 if(deviceName && strcmp(deviceName, alcDefaultName) != 0)
2686 alcSetError(NULL, ALC_INVALID_VALUE);
2687 return NULL;
2690 device = calloc(1, sizeof(ALCdevice));
2691 if(!device)
2693 alcSetError(NULL, ALC_OUT_OF_MEMORY);
2694 return NULL;
2697 //Validate device
2698 device->Funcs = &BackendLoopback.Funcs;
2699 device->ref = 1;
2700 device->Connected = ALC_TRUE;
2701 device->Type = Loopback;
2702 InitializeCriticalSection(&device->Mutex);
2703 device->LastError = ALC_NO_ERROR;
2705 device->Flags = 0;
2706 device->Bs2b = NULL;
2707 device->Bs2bLevel = 0;
2708 device->szDeviceName = NULL;
2710 device->ContextList = NULL;
2712 device->MaxNoOfSources = 256;
2713 device->AuxiliaryEffectSlotMax = 4;
2714 device->NumAuxSends = MAX_SENDS;
2716 InitUIntMap(&device->BufferMap, ~0);
2717 InitUIntMap(&device->EffectMap, ~0);
2718 InitUIntMap(&device->FilterMap, ~0);
2720 //Set output format
2721 device->NumUpdates = 0;
2722 device->UpdateSize = 0;
2724 device->Frequency = 44100;
2725 device->FmtChans = DevFmtStereo;
2726 device->FmtType = DevFmtShort;
2728 ConfigValueUInt(NULL, "sources", &device->MaxNoOfSources);
2729 if(device->MaxNoOfSources == 0) device->MaxNoOfSources = 256;
2731 ConfigValueUInt(NULL, "slots", &device->AuxiliaryEffectSlotMax);
2732 if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
2734 ConfigValueUInt(NULL, "sends", &device->NumAuxSends);
2735 if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
2737 device->NumStereoSources = 1;
2738 device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
2740 // Open the "backend"
2741 ALCdevice_OpenPlayback(device, "Loopback");
2742 do {
2743 device->next = DeviceList;
2744 } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
2746 TRACE("Created device %p\n", device);
2747 return device;
2750 /* alcIsRenderFormatSupportedSOFT
2752 * Determines if the loopback device supports the given format for rendering.
2754 ALC_API ALCboolean ALC_APIENTRY alcIsRenderFormatSupportedSOFT(ALCdevice *device, ALCsizei freq, ALCenum channels, ALCenum type)
2756 ALCboolean ret = ALC_FALSE;
2758 if(!(device=VerifyDevice(device)) || device->Type != Loopback)
2759 alcSetError(device, ALC_INVALID_DEVICE);
2760 else if(freq <= 0)
2761 alcSetError(device, ALC_INVALID_VALUE);
2762 else if(!IsValidALCType(type) || !IsValidALCChannels(channels))
2763 alcSetError(device, ALC_INVALID_ENUM);
2764 else
2766 if(BytesFromDevFmt(type) > 0 && ChannelsFromDevFmt(channels) > 0 &&
2767 freq >= MIN_OUTPUT_RATE)
2768 ret = ALC_TRUE;
2770 if(device) ALCdevice_DecRef(device);
2772 return ret;
2775 /* alcRenderSamplesSOFT
2777 * Renders some samples into a buffer, using the format last set by the
2778 * attributes given to alcCreateContext.
2780 ALC_API void ALC_APIENTRY alcRenderSamplesSOFT(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
2782 if(!(device=VerifyDevice(device)) || device->Type != Loopback)
2783 alcSetError(device, ALC_INVALID_DEVICE);
2784 else if(samples < 0 || (samples > 0 && buffer == NULL))
2785 alcSetError(device, ALC_INVALID_VALUE);
2786 else
2787 aluMixData(device, buffer, samples);
2788 if(device) ALCdevice_DecRef(device);
2792 static void ReleaseALC(void)
2794 ALCdevice *dev;
2796 free(alcAllDeviceList); alcAllDeviceList = NULL;
2797 alcAllDeviceListSize = 0;
2798 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
2799 alcCaptureDeviceListSize = 0;
2801 free(alcDefaultAllDeviceSpecifier);
2802 alcDefaultAllDeviceSpecifier = NULL;
2803 free(alcCaptureDefaultDeviceSpecifier);
2804 alcCaptureDefaultDeviceSpecifier = NULL;
2806 if((dev=ExchangePtr((XchgPtr*)&DeviceList, NULL)) != NULL)
2808 ALCuint num = 0;
2809 do {
2810 num++;
2811 } while((dev=dev->next) != NULL);
2812 ERR("%u device%s not closed\n", num, (num>1)?"s":"");
2816 ///////////////////////////////////////////////////////