Print enumerated ALSA devices
[openal-soft.git] / Alc / ALc.c
blob7a5e3c546ec961406881a6f17518dc46250fe1a1
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 if(!VerifyDevice(pDevice))
1861 value = alcDefaultName;
1862 else
1864 value = pDevice->szDeviceName;
1865 ALCdevice_DecRef(pDevice);
1867 break;
1869 case ALC_ALL_DEVICES_SPECIFIER:
1870 ProbeAllDeviceList();
1871 value = alcAllDeviceList;
1872 break;
1874 case ALC_CAPTURE_DEVICE_SPECIFIER:
1875 if(VerifyDevice(pDevice))
1877 value = pDevice->szDeviceName;
1878 ALCdevice_DecRef(pDevice);
1880 else
1882 ProbeCaptureDeviceList();
1883 value = alcCaptureDeviceList;
1885 break;
1887 /* Default devices are always first in the list */
1888 case ALC_DEFAULT_DEVICE_SPECIFIER:
1889 value = alcDefaultName;
1890 break;
1892 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
1893 if(!alcAllDeviceList)
1894 ProbeAllDeviceList();
1896 pDevice = VerifyDevice(pDevice);
1898 free(alcDefaultAllDeviceSpecifier);
1899 alcDefaultAllDeviceSpecifier = strdup(alcAllDeviceList ?
1900 alcAllDeviceList : "");
1901 if(!alcDefaultAllDeviceSpecifier)
1902 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
1904 value = alcDefaultAllDeviceSpecifier;
1905 if(pDevice) ALCdevice_DecRef(pDevice);
1906 break;
1908 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
1909 if(!alcCaptureDeviceList)
1910 ProbeCaptureDeviceList();
1912 pDevice = VerifyDevice(pDevice);
1914 free(alcCaptureDefaultDeviceSpecifier);
1915 alcCaptureDefaultDeviceSpecifier = strdup(alcCaptureDeviceList ?
1916 alcCaptureDeviceList : "");
1917 if(!alcCaptureDefaultDeviceSpecifier)
1918 alcSetError(pDevice, ALC_OUT_OF_MEMORY);
1920 value = alcCaptureDefaultDeviceSpecifier;
1921 if(pDevice) ALCdevice_DecRef(pDevice);
1922 break;
1924 case ALC_EXTENSIONS:
1925 if(!VerifyDevice(pDevice))
1926 value = alcNoDeviceExtList;
1927 else
1929 value = alcExtensionList;
1930 ALCdevice_DecRef(pDevice);
1932 break;
1934 default:
1935 pDevice = VerifyDevice(pDevice);
1936 alcSetError(pDevice, ALC_INVALID_ENUM);
1937 if(pDevice) ALCdevice_DecRef(pDevice);
1938 break;
1941 return value;
1945 /* alcGetIntegerv
1947 * Returns information about the Device and the version of Open AL
1949 ALC_API ALCvoid ALC_APIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
1951 device = VerifyDevice(device);
1953 if(size == 0 || data == NULL)
1955 alcSetError(device, ALC_INVALID_VALUE);
1956 if(device) ALCdevice_DecRef(device);
1957 return;
1960 if(!device)
1962 switch(param)
1964 case ALC_MAJOR_VERSION:
1965 *data = alcMajorVersion;
1966 break;
1967 case ALC_MINOR_VERSION:
1968 *data = alcMinorVersion;
1969 break;
1971 case ALC_ATTRIBUTES_SIZE:
1972 case ALC_ALL_ATTRIBUTES:
1973 case ALC_FREQUENCY:
1974 case ALC_REFRESH:
1975 case ALC_SYNC:
1976 case ALC_MONO_SOURCES:
1977 case ALC_STEREO_SOURCES:
1978 case ALC_CAPTURE_SAMPLES:
1979 case ALC_FORMAT_CHANNELS_SOFT:
1980 case ALC_FORMAT_TYPE_SOFT:
1981 alcSetError(NULL, ALC_INVALID_DEVICE);
1982 break;
1984 default:
1985 alcSetError(NULL, ALC_INVALID_ENUM);
1986 break;
1989 else if(device->Type == Capture)
1991 switch(param)
1993 case ALC_CAPTURE_SAMPLES:
1994 LockDevice(device);
1995 *data = ALCdevice_AvailableSamples(device);
1996 UnlockDevice(device);
1997 break;
1999 case ALC_CONNECTED:
2000 *data = device->Connected;
2001 break;
2003 default:
2004 alcSetError(device, ALC_INVALID_ENUM);
2005 break;
2008 else /* render device */
2010 switch(param)
2012 case ALC_MAJOR_VERSION:
2013 *data = alcMajorVersion;
2014 break;
2016 case ALC_MINOR_VERSION:
2017 *data = alcMinorVersion;
2018 break;
2020 case ALC_EFX_MAJOR_VERSION:
2021 *data = alcEFXMajorVersion;
2022 break;
2024 case ALC_EFX_MINOR_VERSION:
2025 *data = alcEFXMinorVersion;
2026 break;
2028 case ALC_ATTRIBUTES_SIZE:
2029 *data = 13;
2030 break;
2032 case ALC_ALL_ATTRIBUTES:
2033 if(size < 13)
2034 alcSetError(device, ALC_INVALID_VALUE);
2035 else
2037 int i = 0;
2039 data[i++] = ALC_FREQUENCY;
2040 data[i++] = device->Frequency;
2042 if(device->Type != Loopback)
2044 data[i++] = ALC_REFRESH;
2045 data[i++] = device->Frequency / device->UpdateSize;
2047 data[i++] = ALC_SYNC;
2048 data[i++] = ALC_FALSE;
2050 else
2052 data[i++] = ALC_FORMAT_CHANNELS_SOFT;
2053 data[i++] = device->FmtChans;
2055 data[i++] = ALC_FORMAT_TYPE_SOFT;
2056 data[i++] = device->FmtType;
2059 data[i++] = ALC_MONO_SOURCES;
2060 data[i++] = device->NumMonoSources;
2062 data[i++] = ALC_STEREO_SOURCES;
2063 data[i++] = device->NumStereoSources;
2065 data[i++] = ALC_MAX_AUXILIARY_SENDS;
2066 data[i++] = device->NumAuxSends;
2068 data[i++] = 0;
2070 break;
2072 case ALC_FREQUENCY:
2073 *data = device->Frequency;
2074 break;
2076 case ALC_REFRESH:
2077 if(device->Type == Loopback)
2078 alcSetError(device, ALC_INVALID_DEVICE);
2079 else
2080 *data = device->Frequency / device->UpdateSize;
2081 break;
2083 case ALC_SYNC:
2084 if(device->Type == Loopback)
2085 alcSetError(device, ALC_INVALID_DEVICE);
2086 else
2087 *data = ALC_FALSE;
2088 break;
2090 case ALC_FORMAT_CHANNELS_SOFT:
2091 if(device->Type != Loopback)
2092 alcSetError(device, ALC_INVALID_DEVICE);
2093 else
2094 *data = device->FmtChans;
2095 break;
2097 case ALC_FORMAT_TYPE_SOFT:
2098 if(device->Type != Loopback)
2099 alcSetError(device, ALC_INVALID_DEVICE);
2100 else
2101 *data = device->FmtType;
2102 break;
2104 case ALC_MONO_SOURCES:
2105 *data = device->NumMonoSources;
2106 break;
2108 case ALC_STEREO_SOURCES:
2109 *data = device->NumStereoSources;
2110 break;
2112 case ALC_MAX_AUXILIARY_SENDS:
2113 *data = device->NumAuxSends;
2114 break;
2116 case ALC_CONNECTED:
2117 *data = device->Connected;
2118 break;
2120 default:
2121 alcSetError(device, ALC_INVALID_ENUM);
2122 break;
2125 if(device)
2126 ALCdevice_DecRef(device);
2130 /* alcIsExtensionPresent
2132 * Determines if there is support for a particular extension
2134 ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
2136 ALCboolean bResult = ALC_FALSE;
2138 device = VerifyDevice(device);
2140 if(!extName)
2141 alcSetError(device, ALC_INVALID_VALUE);
2142 else
2144 size_t len = strlen(extName);
2145 const char *ptr = (device ? alcExtensionList : alcNoDeviceExtList);
2146 while(ptr && *ptr)
2148 if(strncasecmp(ptr, extName, len) == 0 &&
2149 (ptr[len] == '\0' || isspace(ptr[len])))
2151 bResult = ALC_TRUE;
2152 break;
2154 if((ptr=strchr(ptr, ' ')) != NULL)
2156 do {
2157 ++ptr;
2158 } while(isspace(*ptr));
2162 if(device)
2163 ALCdevice_DecRef(device);
2164 return bResult;
2168 /* alcGetProcAddress
2170 * Retrieves the function address for a particular extension function
2172 ALC_API ALCvoid* ALC_APIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
2174 ALCvoid *ptr = NULL;
2176 device = VerifyDevice(device);
2178 if(!funcName)
2179 alcSetError(device, ALC_INVALID_VALUE);
2180 else
2182 ALsizei i = 0;
2183 while(alcFunctions[i].funcName && strcmp(alcFunctions[i].funcName,funcName) != 0)
2184 i++;
2185 ptr = alcFunctions[i].address;
2187 if(device)
2188 ALCdevice_DecRef(device);
2189 return ptr;
2193 /* alcGetEnumValue
2195 * Get the value for a particular ALC Enumerated Value
2197 ALC_API ALCenum ALC_APIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
2199 ALCenum val = 0;
2201 device = VerifyDevice(device);
2203 if(!enumName)
2204 alcSetError(device, ALC_INVALID_VALUE);
2205 else
2207 ALsizei i = 0;
2208 while(enumeration[i].enumName && strcmp(enumeration[i].enumName,enumName) != 0)
2209 i++;
2210 val = enumeration[i].value;
2212 if(device)
2213 ALCdevice_DecRef(device);
2214 return val;
2218 /* alcCreateContext
2220 * Create and attach a Context to a particular Device.
2222 ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
2224 ALCcontext *ALContext;
2225 ALCenum err;
2227 LockLists();
2228 if(!(device=VerifyDevice(device)) || device->Type == Capture || !device->Connected)
2230 UnlockLists();
2231 alcSetError(device, ALC_INVALID_DEVICE);
2232 if(device) ALCdevice_DecRef(device);
2233 return NULL;
2236 /* Reset Context Last Error code */
2237 device->LastError = ALC_NO_ERROR;
2239 if((err=UpdateDeviceParams(device, attrList)) != ALC_NO_ERROR)
2241 UnlockLists();
2242 alcSetError(device, err);
2243 if(err == ALC_INVALID_DEVICE)
2244 aluHandleDisconnect(device);
2245 ALCdevice_DecRef(device);
2246 return NULL;
2249 ALContext = calloc(1, sizeof(ALCcontext));
2250 if(ALContext)
2252 ALContext->ref = 1;
2254 ALContext->MaxActiveSources = 256;
2255 ALContext->ActiveSources = malloc(sizeof(ALContext->ActiveSources[0]) *
2256 ALContext->MaxActiveSources);
2258 if(!ALContext || !ALContext->ActiveSources)
2260 if(!device->ContextList)
2262 ALCdevice_StopPlayback(device);
2263 device->Flags &= ~DEVICE_RUNNING;
2265 UnlockLists();
2267 free(ALContext);
2268 ALContext = NULL;
2270 alcSetError(device, ALC_OUT_OF_MEMORY);
2271 ALCdevice_DecRef(device);
2272 return NULL;
2275 ALContext->Device = device;
2276 ALCdevice_IncRef(device);
2277 InitContext(ALContext);
2279 do {
2280 ALContext->next = device->ContextList;
2281 } while(!CompExchangePtr((XchgPtr*)&device->ContextList, ALContext->next, ALContext));
2282 UnlockLists();
2284 if(device->DefaultSlot)
2285 InitializeEffect(ALContext, device->DefaultSlot, &ForcedEffect);
2286 ALContext->LastError = AL_NO_ERROR;
2288 ALCdevice_DecRef(device);
2290 TRACE("Created context %p\n", ALContext);
2291 return ALContext;
2294 /* alcDestroyContext
2296 * Remove a Context
2298 ALC_API ALCvoid ALC_APIENTRY alcDestroyContext(ALCcontext *context)
2300 ALCdevice *Device;
2302 LockLists();
2303 /* alcGetContextsDevice sets an error for invalid contexts */
2304 Device = alcGetContextsDevice(context);
2305 if(Device)
2307 ReleaseContext(context, Device);
2308 if(!Device->ContextList)
2310 ALCdevice_StopPlayback(Device);
2311 Device->Flags &= ~DEVICE_RUNNING;
2314 UnlockLists();
2318 /* alcGetCurrentContext
2320 * Returns the currently active Context
2322 ALC_API ALCcontext* ALC_APIENTRY alcGetCurrentContext(ALCvoid)
2324 ALCcontext *Context;
2326 Context = pthread_getspecific(LocalContext);
2327 if(!Context) Context = GlobalContext;
2329 return Context;
2332 /* alcGetThreadContext
2334 * Returns the currently active thread-local Context
2336 ALC_API ALCcontext* ALC_APIENTRY alcGetThreadContext(void)
2338 ALCcontext *Context;
2339 Context = pthread_getspecific(LocalContext);
2340 return Context;
2344 /* alcMakeContextCurrent
2346 * Makes the given Context the active Context
2348 ALC_API ALCboolean ALC_APIENTRY alcMakeContextCurrent(ALCcontext *context)
2350 /* context must be a valid Context or NULL */
2351 if(context && !(context=VerifyContext(context)))
2353 alcSetError(NULL, ALC_INVALID_CONTEXT);
2354 return ALC_FALSE;
2356 /* context's reference count is already incremented */
2357 context = ExchangePtr((XchgPtr*)&GlobalContext, context);
2358 if(context) ALCcontext_DecRef(context);
2360 if((context=pthread_getspecific(LocalContext)) != NULL)
2362 pthread_setspecific(LocalContext, NULL);
2363 ALCcontext_DecRef(context);
2366 return ALC_TRUE;
2369 /* alcSetThreadContext
2371 * Makes the given Context the active Context for the current thread
2373 ALC_API ALCboolean ALC_APIENTRY alcSetThreadContext(ALCcontext *context)
2375 ALCcontext *old;
2377 /* context must be a valid Context or NULL */
2378 if(context && !(context=VerifyContext(context)))
2380 alcSetError(NULL, ALC_INVALID_CONTEXT);
2381 return ALC_FALSE;
2383 /* context's reference count is already incremented */
2384 old = pthread_getspecific(LocalContext);
2385 pthread_setspecific(LocalContext, context);
2386 if(old) ALCcontext_DecRef(old);
2388 return ALC_TRUE;
2392 /* alcGetContextsDevice
2394 * Returns the Device that a particular Context is attached to
2396 ALC_API ALCdevice* ALC_APIENTRY alcGetContextsDevice(ALCcontext *Context)
2398 ALCdevice *Device;
2400 if(!(Context=VerifyContext(Context)))
2402 alcSetError(NULL, ALC_INVALID_CONTEXT);
2403 return NULL;
2405 Device = Context->Device;
2406 ALCcontext_DecRef(Context);
2408 return Device;
2412 /* alcOpenDevice
2414 * Open the Device specified.
2416 ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName)
2418 const ALCchar *fmt;
2419 ALCdevice *device;
2420 ALCenum err;
2422 DO_INITCONFIG();
2424 if(!PlaybackBackend.name)
2426 alcSetError(NULL, ALC_INVALID_VALUE);
2427 return NULL;
2430 if(deviceName && (!deviceName[0] || strcasecmp(deviceName, alcDefaultName) == 0 || strcasecmp(deviceName, "openal-soft") == 0))
2431 deviceName = NULL;
2433 device = calloc(1, sizeof(ALCdevice)+sizeof(ALeffectslot));
2434 if(!device)
2436 alcSetError(NULL, ALC_OUT_OF_MEMORY);
2437 return NULL;
2440 //Validate device
2441 device->Funcs = &PlaybackBackend.Funcs;
2442 device->ref = 1;
2443 device->Connected = ALC_TRUE;
2444 device->Type = Playback;
2445 InitializeCriticalSection(&device->Mutex);
2446 device->LastError = ALC_NO_ERROR;
2448 device->Flags = 0;
2449 device->Bs2b = NULL;
2450 device->Bs2bLevel = 0;
2451 device->szDeviceName = NULL;
2453 device->ContextList = NULL;
2455 device->MaxNoOfSources = 256;
2456 device->AuxiliaryEffectSlotMax = 4;
2457 device->NumAuxSends = MAX_SENDS;
2459 InitUIntMap(&device->BufferMap, ~0);
2460 InitUIntMap(&device->EffectMap, ~0);
2461 InitUIntMap(&device->FilterMap, ~0);
2463 //Set output format
2464 device->FmtChans = DevFmtStereo;
2465 device->FmtType = DevFmtFloat;
2466 device->Frequency = DEFAULT_OUTPUT_RATE;
2467 device->NumUpdates = 4;
2468 device->UpdateSize = 1024;
2470 if(ConfigValueStr(NULL, "channels", &fmt))
2472 static const struct {
2473 const char name[16];
2474 enum DevFmtChannels chans;
2475 } chanlist[] = {
2476 { "mono", DevFmtMono },
2477 { "stereo", DevFmtStereo },
2478 { "quad", DevFmtQuad },
2479 { "surround51", DevFmtX51 },
2480 { "surround61", DevFmtX61 },
2481 { "surround71", DevFmtX71 },
2483 size_t i;
2485 for(i = 0;i < COUNTOF(chanlist);i++)
2487 if(strcasecmp(chanlist[i].name, fmt) == 0)
2489 device->FmtChans = chanlist[i].chans;
2490 device->Flags |= DEVICE_CHANNELS_REQUEST;
2491 break;
2494 if(i == COUNTOF(chanlist))
2495 ERR("Unsupported channels: %s\n", fmt);
2497 if(ConfigValueStr(NULL, "sample-type", &fmt))
2499 static const struct {
2500 const char name[16];
2501 enum DevFmtType type;
2502 } typelist[] = {
2503 { "int8", DevFmtByte },
2504 { "uint8", DevFmtUByte },
2505 { "int16", DevFmtShort },
2506 { "uint16", DevFmtUShort },
2507 { "int32", DevFmtInt },
2508 { "uint32", DevFmtUInt },
2509 { "float32", DevFmtFloat },
2511 size_t i;
2513 for(i = 0;i < COUNTOF(typelist);i++)
2515 if(strcasecmp(typelist[i].name, fmt) == 0)
2517 device->FmtType = typelist[i].type;
2518 device->Flags |= DEVICE_SAMPLE_TYPE_REQUEST;
2519 break;
2522 if(i == COUNTOF(typelist))
2523 ERR("Unsupported sample-type: %s\n", fmt);
2525 #define DEVICE_FORMAT_REQUEST (DEVICE_CHANNELS_REQUEST|DEVICE_SAMPLE_TYPE_REQUEST)
2526 if((device->Flags&DEVICE_FORMAT_REQUEST) != DEVICE_FORMAT_REQUEST &&
2527 ConfigValueStr(NULL, "format", &fmt))
2529 static const struct {
2530 const char name[32];
2531 enum DevFmtChannels channels;
2532 enum DevFmtType type;
2533 } formats[] = {
2534 { "AL_FORMAT_MONO32", DevFmtMono, DevFmtFloat },
2535 { "AL_FORMAT_STEREO32", DevFmtStereo, DevFmtFloat },
2536 { "AL_FORMAT_QUAD32", DevFmtQuad, DevFmtFloat },
2537 { "AL_FORMAT_51CHN32", DevFmtX51, DevFmtFloat },
2538 { "AL_FORMAT_61CHN32", DevFmtX61, DevFmtFloat },
2539 { "AL_FORMAT_71CHN32", DevFmtX71, DevFmtFloat },
2541 { "AL_FORMAT_MONO16", DevFmtMono, DevFmtShort },
2542 { "AL_FORMAT_STEREO16", DevFmtStereo, DevFmtShort },
2543 { "AL_FORMAT_QUAD16", DevFmtQuad, DevFmtShort },
2544 { "AL_FORMAT_51CHN16", DevFmtX51, DevFmtShort },
2545 { "AL_FORMAT_61CHN16", DevFmtX61, DevFmtShort },
2546 { "AL_FORMAT_71CHN16", DevFmtX71, DevFmtShort },
2548 { "AL_FORMAT_MONO8", DevFmtMono, DevFmtByte },
2549 { "AL_FORMAT_STEREO8", DevFmtStereo, DevFmtByte },
2550 { "AL_FORMAT_QUAD8", DevFmtQuad, DevFmtByte },
2551 { "AL_FORMAT_51CHN8", DevFmtX51, DevFmtByte },
2552 { "AL_FORMAT_61CHN8", DevFmtX61, DevFmtByte },
2553 { "AL_FORMAT_71CHN8", DevFmtX71, DevFmtByte }
2555 size_t i;
2557 ERR("Option 'format' is deprecated, please use 'channels' and 'sample-type'\n");
2558 for(i = 0;i < COUNTOF(formats);i++)
2560 if(strcasecmp(fmt, formats[i].name) == 0)
2562 if(!(device->Flags&DEVICE_CHANNELS_REQUEST))
2563 device->FmtChans = formats[i].channels;
2564 if(!(device->Flags&DEVICE_SAMPLE_TYPE_REQUEST))
2565 device->FmtType = formats[i].type;
2566 device->Flags |= DEVICE_FORMAT_REQUEST;
2567 break;
2570 if(i == COUNTOF(formats))
2571 ERR("Unsupported format: %s\n", fmt);
2573 #undef DEVICE_FORMAT_REQUEST
2575 if(ConfigValueUInt(NULL, "frequency", &device->Frequency))
2577 device->Flags |= DEVICE_FREQUENCY_REQUEST;
2578 if(device->Frequency < MIN_OUTPUT_RATE)
2579 ERR("%uhz request clamped to %uhz minimum\n", device->Frequency, MIN_OUTPUT_RATE);
2580 device->Frequency = maxu(device->Frequency, MIN_OUTPUT_RATE);
2583 ConfigValueUInt(NULL, "periods", &device->NumUpdates);
2584 if(device->NumUpdates < 2) device->NumUpdates = 4;
2586 ConfigValueUInt(NULL, "period_size", &device->UpdateSize);
2587 if(device->UpdateSize == 0) device->UpdateSize = 1024;
2589 ConfigValueUInt(NULL, "sources", &device->MaxNoOfSources);
2590 if(device->MaxNoOfSources == 0) device->MaxNoOfSources = 256;
2592 ConfigValueUInt(NULL, "slots", &device->AuxiliaryEffectSlotMax);
2593 if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
2595 ConfigValueUInt(NULL, "sends", &device->NumAuxSends);
2596 if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
2598 ConfigValueInt(NULL, "cf_level", &device->Bs2bLevel);
2600 device->NumStereoSources = 1;
2601 device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
2603 if(ForcedEffect.type != AL_EFFECT_NULL)
2605 device->DefaultSlot = (ALeffectslot*)(device+1);
2606 if(InitEffectSlot(device->DefaultSlot) != AL_NO_ERROR)
2607 device->DefaultSlot = NULL;
2610 // Find a playback device to open
2611 LockLists();
2612 if((err=ALCdevice_OpenPlayback(device, deviceName)) != ALC_NO_ERROR)
2614 UnlockLists();
2615 DeleteCriticalSection(&device->Mutex);
2616 free(device);
2617 alcSetError(NULL, err);
2618 return NULL;
2620 UnlockLists();
2622 do {
2623 device->next = DeviceList;
2624 } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
2626 TRACE("Created device %p\n", device);
2627 return device;
2630 /* alcCloseDevice
2632 * Close the specified Device
2634 ALC_API ALCboolean ALC_APIENTRY alcCloseDevice(ALCdevice *pDevice)
2636 ALCdevice *volatile*list;
2637 ALCcontext *ctx;
2639 LockLists();
2640 list = &DeviceList;
2641 while(*list && *list != pDevice)
2642 list = &(*list)->next;
2644 if(!*list || (*list)->Type == Capture)
2646 alcSetError(*list, ALC_INVALID_DEVICE);
2647 UnlockLists();
2648 return ALC_FALSE;
2651 *list = (*list)->next;
2652 UnlockLists();
2654 if((ctx=pDevice->ContextList) != NULL)
2656 do {
2657 WARN("Releasing context %p\n", ctx);
2658 ReleaseContext(ctx, pDevice);
2659 } while((ctx=pDevice->ContextList) != NULL);
2660 ALCdevice_StopPlayback(pDevice);
2661 pDevice->Flags &= ~DEVICE_RUNNING;
2663 ALCdevice_ClosePlayback(pDevice);
2665 ALCdevice_DecRef(pDevice);
2667 return ALC_TRUE;
2671 /* alcLoopbackOpenDeviceSOFT
2673 * Open a loopback device, for manual rendering.
2675 ALC_API ALCdevice* ALC_APIENTRY alcLoopbackOpenDeviceSOFT(ALCdevice *device)
2677 DO_INITCONFIG();
2679 /* Make sure the device, if specified, belongs to us. */
2680 if(device && !(device=VerifyDevice(device)))
2682 alcSetError(device, ALC_INVALID_DEVICE);
2683 return NULL;
2685 if(device) ALCdevice_DecRef(device);
2687 device = calloc(1, sizeof(ALCdevice));
2688 if(!device)
2690 alcSetError(NULL, ALC_OUT_OF_MEMORY);
2691 return NULL;
2694 //Validate device
2695 device->Funcs = &BackendLoopback.Funcs;
2696 device->ref = 1;
2697 device->Connected = ALC_TRUE;
2698 device->Type = Loopback;
2699 InitializeCriticalSection(&device->Mutex);
2700 device->LastError = ALC_NO_ERROR;
2702 device->Flags = 0;
2703 device->Bs2b = NULL;
2704 device->Bs2bLevel = 0;
2705 device->szDeviceName = NULL;
2707 device->ContextList = NULL;
2709 device->MaxNoOfSources = 256;
2710 device->AuxiliaryEffectSlotMax = 4;
2711 device->NumAuxSends = MAX_SENDS;
2713 InitUIntMap(&device->BufferMap, ~0);
2714 InitUIntMap(&device->EffectMap, ~0);
2715 InitUIntMap(&device->FilterMap, ~0);
2717 //Set output format
2718 device->NumUpdates = 0;
2719 device->UpdateSize = 0;
2721 device->Frequency = 44100;
2722 device->FmtChans = DevFmtStereo;
2723 device->FmtType = DevFmtShort;
2725 ConfigValueUInt(NULL, "sources", &device->MaxNoOfSources);
2726 if(device->MaxNoOfSources == 0) device->MaxNoOfSources = 256;
2728 ConfigValueUInt(NULL, "slots", &device->AuxiliaryEffectSlotMax);
2729 if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
2731 ConfigValueUInt(NULL, "sends", &device->NumAuxSends);
2732 if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
2734 device->NumStereoSources = 1;
2735 device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
2737 // Open the "backend"
2738 ALCdevice_OpenPlayback(device, "Loopback");
2739 do {
2740 device->next = DeviceList;
2741 } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
2743 TRACE("Created device %p\n", device);
2744 return device;
2747 /* alcIsRenderFormatSupportedSOFT
2749 * Determines if the loopback device supports the given format for rendering.
2751 ALC_API ALCboolean ALC_APIENTRY alcIsRenderFormatSupportedSOFT(ALCdevice *device, ALCsizei freq, ALCenum channels, ALCenum type)
2753 ALCboolean ret = ALC_FALSE;
2755 if(!(device=VerifyDevice(device)) || device->Type != Loopback)
2756 alcSetError(device, ALC_INVALID_DEVICE);
2757 else if(freq <= 0)
2758 alcSetError(device, ALC_INVALID_VALUE);
2759 else if(!IsValidALCType(type) || !IsValidALCChannels(channels))
2760 alcSetError(device, ALC_INVALID_ENUM);
2761 else
2763 if(BytesFromDevFmt(type) > 0 && ChannelsFromDevFmt(channels) > 0 &&
2764 freq >= MIN_OUTPUT_RATE)
2765 ret = ALC_TRUE;
2767 if(device) ALCdevice_DecRef(device);
2769 return ret;
2772 /* alcRenderSamplesSOFT
2774 * Renders some samples into a buffer, using the format last set by the
2775 * attributes given to alcCreateContext.
2777 ALC_API void ALC_APIENTRY alcRenderSamplesSOFT(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
2779 if(!(device=VerifyDevice(device)) || device->Type != Loopback)
2780 alcSetError(device, ALC_INVALID_DEVICE);
2781 else if(samples < 0 || (samples > 0 && buffer == NULL))
2782 alcSetError(device, ALC_INVALID_VALUE);
2783 else
2784 aluMixData(device, buffer, samples);
2785 if(device) ALCdevice_DecRef(device);
2789 static void ReleaseALC(void)
2791 ALCdevice *dev;
2793 free(alcAllDeviceList); alcAllDeviceList = NULL;
2794 alcAllDeviceListSize = 0;
2795 free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
2796 alcCaptureDeviceListSize = 0;
2798 free(alcDefaultAllDeviceSpecifier);
2799 alcDefaultAllDeviceSpecifier = NULL;
2800 free(alcCaptureDefaultDeviceSpecifier);
2801 alcCaptureDefaultDeviceSpecifier = NULL;
2803 if((dev=ExchangePtr((XchgPtr*)&DeviceList, NULL)) != NULL)
2805 ALCuint num = 0;
2806 do {
2807 num++;
2808 } while((dev=dev->next) != NULL);
2809 ERR("%u device%s not closed\n", num, (num>1)?"s":"");
2813 ///////////////////////////////////////////////////////