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
37 #include "alAuxEffectSlot.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
},
49 { "alsa", alc_alsa_init
, alc_alsa_deinit
, alc_alsa_probe
, EmptyFuncs
},
52 { "core", alc_ca_init
, alc_ca_deinit
, alc_ca_probe
, EmptyFuncs
},
55 { "oss", alc_oss_init
, alc_oss_deinit
, alc_oss_probe
, EmptyFuncs
},
58 { "solaris", alc_solaris_init
, alc_solaris_deinit
, alc_solaris_probe
, EmptyFuncs
},
61 { "sndio", alc_sndio_init
, alc_sndio_deinit
, alc_sndio_probe
, EmptyFuncs
},
64 { "mmdevapi", alcMMDevApiInit
, alcMMDevApiDeinit
, alcMMDevApiProbe
, EmptyFuncs
},
67 { "dsound", alcDSoundInit
, alcDSoundDeinit
, alcDSoundProbe
, EmptyFuncs
},
70 { "winmm", alcWinMMInit
, alcWinMMDeinit
, alcWinMMProbe
, EmptyFuncs
},
73 { "port", alc_pa_init
, alc_pa_deinit
, alc_pa_probe
, EmptyFuncs
},
76 { "opensl", alc_opensl_init
, alc_opensl_deinit
, alc_opensl_probe
, EmptyFuncs
},
79 { "null", alc_null_init
, alc_null_deinit
, alc_null_probe
, EmptyFuncs
},
81 { "wave", alc_wave_init
, alc_wave_deinit
, alc_wave_probe
, EmptyFuncs
},
84 { NULL
, NULL
, NULL
, NULL
, EmptyFuncs
}
86 static struct BackendInfo BackendLoopback
= {
87 "loopback", alc_loopback_init
, alc_loopback_deinit
, alc_loopback_probe
, EmptyFuncs
91 static struct BackendInfo PlaybackBackend
;
92 static struct BackendInfo CaptureBackend
;
94 ///////////////////////////////////////////////////////
95 // STRING and EXTENSIONS
97 typedef struct ALCfunction
{
98 const ALCchar
*funcName
;
102 typedef struct ALCenums
{
103 const ALCchar
*enumName
;
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
[] = {
260 { "ALC_INVALID", ALC_INVALID
},
261 { "ALC_FALSE", ALC_FALSE
},
262 { "ALC_TRUE", ALC_TRUE
},
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
},
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
},
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
},
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
330 static ALCchar
*alcDeviceList
;
331 static size_t alcDeviceListSize
;
332 static ALCchar
*alcAllDeviceList
;
333 static size_t alcAllDeviceListSize
;
334 static ALCchar
*alcCaptureDeviceList
;
335 static size_t alcCaptureDeviceListSize
;
336 /* Default is always the first in the list */
337 static ALCchar
*alcDefaultDeviceSpecifier
;
338 static ALCchar
*alcDefaultAllDeviceSpecifier
;
339 static ALCchar
*alcCaptureDefaultDeviceSpecifier
;
342 static const ALCchar alcNoDeviceExtList
[] =
343 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
344 "ALC_EXT_thread_local_context ALC_SOFTX_loopback_device";
345 static const ALCchar alcExtensionList
[] =
346 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
347 "ALC_EXT_DEDICATED ALC_EXT_disconnect ALC_EXT_EFX "
348 "ALC_EXT_thread_local_context ALC_SOFTX_loopback_device";
349 static const ALCint alcMajorVersion
= 1;
350 static const ALCint alcMinorVersion
= 1;
352 static const ALCint alcEFXMajorVersion
= 1;
353 static const ALCint alcEFXMinorVersion
= 0;
355 ///////////////////////////////////////////////////////
358 ///////////////////////////////////////////////////////
361 static CRITICAL_SECTION ListLock
;
364 static ALCdevice
*volatile DeviceList
= NULL
;
366 // Thread-local current context
367 static pthread_key_t LocalContext
;
368 // Process-wide current context
369 static ALCcontext
*GlobalContext
;
372 static volatile ALCenum g_eLastNullDeviceError
= ALC_NO_ERROR
;
374 // Default context extensions
375 static const ALchar alExtList
[] =
376 "AL_EXT_ALAW AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 "
377 "AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_MULAW "
378 "AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET AL_EXT_source_distance_model "
379 "AL_LOKI_quadriphonic AL_SOFT_buffer_samples AL_SOFT_buffer_sub_data "
380 "AL_SOFTX_deferred_updates AL_SOFTX_direct_channels AL_SOFT_loop_points";
382 // Mixing Priority Level
390 enum LogLevel LogLevel
= LogWarning
;
392 enum LogLevel LogLevel
= LogError
;
395 /* Flag to trap ALC device errors */
396 static ALCboolean TrapALCError
= ALC_FALSE
;
398 /* One-time configuration init control */
399 static pthread_once_t alc_config_once
= PTHREAD_ONCE_INIT
;
401 /* Forced effect that applies to sources that don't have an effect on send 0 */
402 static ALeffect ForcedEffect
;
404 ///////////////////////////////////////////////////////
407 ///////////////////////////////////////////////////////
408 // ALC Related helper functions
409 static void ReleaseALC(void);
410 static void ReleaseThreadCtx(void *ptr
);
412 static void alc_initconfig(void);
413 #define DO_INITCONFIG() pthread_once(&alc_config_once, alc_initconfig)
416 static void alc_init(void);
417 static void alc_deinit(void);
418 static void alc_deinit_safe(void);
420 UIntMap TlsDestructor
;
422 #ifndef AL_LIBTYPE_STATIC
423 BOOL APIENTRY
DllMain(HANDLE hModule
,DWORD ul_reason_for_call
,LPVOID lpReserved
)
428 // Perform actions based on the reason for calling.
429 switch(ul_reason_for_call
)
431 case DLL_PROCESS_ATTACH
:
432 InitUIntMap(&TlsDestructor
, ~0);
436 case DLL_THREAD_DETACH
:
437 LockUIntMapRead(&TlsDestructor
);
438 for(i
= 0;i
< TlsDestructor
.size
;i
++)
440 void *ptr
= pthread_getspecific(TlsDestructor
.array
[i
].key
);
441 void (*callback
)(void*) = (void(*)(void*))TlsDestructor
.array
[i
].value
;
445 UnlockUIntMapRead(&TlsDestructor
);
448 case DLL_PROCESS_DETACH
:
453 ResetUIntMap(&TlsDestructor
);
458 #elif defined(_MSC_VER)
459 #pragma section(".CRT$XCU",read)
460 static void alc_constructor(void);
461 static void alc_destructor(void);
462 __declspec(allocate(".CRT$XCU")) void (__cdecl
* alc_constructor_
)(void) = alc_constructor
;
464 static void alc_constructor(void)
466 atexit(alc_destructor
);
470 static void alc_destructor(void)
474 #elif defined(HAVE_GCC_DESTRUCTOR)
475 static void alc_init(void) __attribute__((constructor
));
476 static void alc_deinit(void) __attribute__((destructor
));
478 #error "No static initialization available on this platform!"
480 #elif defined(HAVE_GCC_DESTRUCTOR)
481 static void alc_init(void) __attribute__((constructor
));
482 static void alc_deinit(void) __attribute__((destructor
));
484 #error "No global initialization available on this platform!"
487 static void alc_init(void)
493 str
= getenv("__ALSOFT_HALF_ANGLE_CONES");
494 if(str
&& (strcasecmp(str
, "true") == 0 || strtol(str
, NULL
, 0) == 1))
497 str
= getenv("__ALSOFT_REVERSE_Z");
498 if(str
&& (strcasecmp(str
, "true") == 0 || strtol(str
, NULL
, 0) == 1))
501 str
= getenv("ALSOFT_TRAP_ERROR");
502 if(str
&& (strcasecmp(str
, "true") == 0 || strtol(str
, NULL
, 0) == 1))
504 TrapALError
= AL_TRUE
;
505 TrapALCError
= AL_TRUE
;
509 str
= getenv("ALSOFT_TRAP_AL_ERROR");
510 if(str
&& (strcasecmp(str
, "true") == 0 || strtol(str
, NULL
, 0) == 1))
511 TrapALError
= AL_TRUE
;
513 str
= getenv("ALSOFT_TRAP_ALC_ERROR");
514 if(str
&& (strcasecmp(str
, "true") == 0 || strtol(str
, NULL
, 0) == 1))
515 TrapALCError
= ALC_TRUE
;
518 pthread_key_create(&LocalContext
, ReleaseThreadCtx
);
519 InitializeCriticalSection(&ListLock
);
523 static void alc_deinit_safe(void)
531 DeleteCriticalSection(&ListLock
);
532 pthread_key_delete(LocalContext
);
534 if(LogFile
!= stderr
)
539 static void alc_deinit(void)
545 memset(&PlaybackBackend
, 0, sizeof(PlaybackBackend
));
546 memset(&CaptureBackend
, 0, sizeof(CaptureBackend
));
548 for(i
= 0;BackendList
[i
].Deinit
;i
++)
549 BackendList
[i
].Deinit();
550 BackendLoopback
.Deinit();
555 static void alc_initconfig(void)
557 const char *devs
, *str
;
561 str
= getenv("ALSOFT_LOGLEVEL");
564 long lvl
= strtol(str
, NULL
, 0);
565 if(lvl
>= NoLog
&& lvl
<= LogRef
)
569 str
= getenv("ALSOFT_LOGFILE");
572 FILE *logfile
= fopen(str
, "wat");
573 if(logfile
) LogFile
= logfile
;
574 else ERR("Failed to open log file '%s'\n", str
);
586 ConfigValueInt(NULL
, "rt-prio", &RTPrioLevel
);
588 if(ConfigValueStr(NULL
, "resampler", &str
))
590 if(strcasecmp(str
, "point") == 0 || strcasecmp(str
, "none") == 0)
591 DefaultResampler
= PointResampler
;
592 else if(strcasecmp(str
, "linear") == 0)
593 DefaultResampler
= LinearResampler
;
594 else if(strcasecmp(str
, "cubic") == 0)
595 DefaultResampler
= CubicResampler
;
600 n
= strtol(str
, &end
, 0);
601 if(*end
== '\0' && (n
== PointResampler
|| n
== LinearResampler
|| n
== CubicResampler
))
602 DefaultResampler
= n
;
604 WARN("Invalid resampler: %s\n", str
);
609 TrapALCError
= GetConfigValueBool(NULL
, "trap-alc-error", ALC_FALSE
);
612 TrapALError
= GetConfigValueBool(NULL
, "trap-al-error", AL_FALSE
);
614 if(ConfigValueFloat("reverb", "boost", &valf
))
615 ReverbBoost
*= aluPow(10.0f
, valf
/ 20.0f
);
617 EmulateEAXReverb
= GetConfigValueBool("reverb", "emulate-eax", AL_FALSE
);
619 if(((devs
=getenv("ALSOFT_DRIVERS")) && devs
[0]) ||
620 ConfigValueStr(NULL
, "drivers", &devs
))
624 const char *next
= devs
;
625 int endlist
, delitem
;
630 next
= strchr(devs
, ',');
632 delitem
= (devs
[0] == '-');
633 if(devs
[0] == '-') devs
++;
635 if(!devs
[0] || devs
[0] == ',')
642 len
= (next
? ((size_t)(next
-devs
)) : strlen(devs
));
643 for(n
= i
;BackendList
[n
].Init
;n
++)
645 if(len
== strlen(BackendList
[n
].name
) &&
646 strncmp(BackendList
[n
].name
, devs
, len
) == 0)
651 BackendList
[n
] = BackendList
[n
+1];
653 } while(BackendList
[n
].Init
);
657 struct BackendInfo Bkp
= BackendList
[n
];
660 BackendList
[n
] = BackendList
[n
-1];
663 BackendList
[n
] = Bkp
;
674 BackendList
[i
].name
= NULL
;
675 BackendList
[i
].Init
= NULL
;
676 BackendList
[i
].Deinit
= NULL
;
677 BackendList
[i
].Probe
= NULL
;
681 for(i
= 0;BackendList
[i
].Init
&& (!PlaybackBackend
.name
|| !CaptureBackend
.name
);i
++)
683 if(!BackendList
[i
].Init(&BackendList
[i
].Funcs
))
685 WARN("Failed to initialize backend \"%s\"\n", BackendList
[i
].name
);
689 TRACE("Initialized backend \"%s\"\n", BackendList
[i
].name
);
690 if(BackendList
[i
].Funcs
.OpenPlayback
&& !PlaybackBackend
.name
)
692 PlaybackBackend
= BackendList
[i
];
693 TRACE("Added \"%s\" for playback\n", PlaybackBackend
.name
);
695 if(BackendList
[i
].Funcs
.OpenCapture
&& !CaptureBackend
.name
)
697 CaptureBackend
= BackendList
[i
];
698 TRACE("Added \"%s\" for capture\n", CaptureBackend
.name
);
701 BackendLoopback
.Init(&BackendLoopback
.Funcs
);
703 if(ConfigValueStr(NULL
, "excludefx", &str
))
706 const char *next
= str
;
710 next
= strchr(str
, ',');
712 if(!str
[0] || next
== str
)
715 len
= (next
? ((size_t)(next
-str
)) : strlen(str
));
716 for(n
= 0;EffectList
[n
].name
;n
++)
718 if(len
== strlen(EffectList
[n
].name
) &&
719 strncmp(EffectList
[n
].name
, str
, len
) == 0)
720 DisabledEffects
[EffectList
[n
].type
] = AL_TRUE
;
725 InitEffect(&ForcedEffect
);
726 str
= getenv("ALSOFT_DEFAULT_REVERB");
728 GetReverbEffect(str
, &ForcedEffect
);
729 else if(ConfigValueStr(NULL
, "default-reverb", &str
))
730 GetReverbEffect(str
, &ForcedEffect
);
734 static void LockLists(void)
736 EnterCriticalSection(&ListLock
);
739 static void UnlockLists(void)
741 LeaveCriticalSection(&ListLock
);
745 static void ProbeList(ALCchar
**list
, size_t *listsize
, enum DevProbe type
)
754 if(type
== CAPTURE_DEVICE_PROBE
)
755 CaptureBackend
.Probe(type
);
757 PlaybackBackend
.Probe(type
);
761 static void ProbeDeviceList(void)
762 { ProbeList(&alcDeviceList
, &alcDeviceListSize
, DEVICE_PROBE
); }
763 static void ProbeAllDeviceList(void)
764 { ProbeList(&alcAllDeviceList
, &alcAllDeviceListSize
, ALL_DEVICE_PROBE
); }
765 static void ProbeCaptureDeviceList(void)
766 { ProbeList(&alcCaptureDeviceList
, &alcCaptureDeviceListSize
, CAPTURE_DEVICE_PROBE
); }
769 static void AppendList(const ALCchar
*name
, ALCchar
**List
, size_t *ListSize
)
771 size_t len
= strlen(name
);
777 temp
= realloc(*List
, (*ListSize
) + len
+ 2);
780 ERR("Realloc failed to add %s!\n", name
);
785 memcpy((*List
)+(*ListSize
), name
, len
+1);
787 (*List
)[*ListSize
] = 0;
790 #define DECL_APPEND_LIST_FUNC(type) \
791 void Append##type##List(const ALCchar *name) \
792 { AppendList(name, &alc##type##List, &alc##type##ListSize); }
794 DECL_APPEND_LIST_FUNC(Device
)
795 DECL_APPEND_LIST_FUNC(AllDevice
)
796 DECL_APPEND_LIST_FUNC(CaptureDevice
)
798 #undef DECL_APPEND_LIST_FUNC
801 /* Sets the default channel order used by most non-WaveFormatEx-based APIs */
802 void SetDefaultChannelOrder(ALCdevice
*device
)
804 switch(device
->FmtChans
)
806 case DevFmtX51
: device
->DevChannels
[0] = FRONT_LEFT
;
807 device
->DevChannels
[1] = FRONT_RIGHT
;
808 device
->DevChannels
[2] = BACK_LEFT
;
809 device
->DevChannels
[3] = BACK_RIGHT
;
810 device
->DevChannels
[4] = FRONT_CENTER
;
811 device
->DevChannels
[5] = LFE
;
814 case DevFmtX71
: device
->DevChannels
[0] = FRONT_LEFT
;
815 device
->DevChannels
[1] = FRONT_RIGHT
;
816 device
->DevChannels
[2] = BACK_LEFT
;
817 device
->DevChannels
[3] = BACK_RIGHT
;
818 device
->DevChannels
[4] = FRONT_CENTER
;
819 device
->DevChannels
[5] = LFE
;
820 device
->DevChannels
[6] = SIDE_LEFT
;
821 device
->DevChannels
[7] = SIDE_RIGHT
;
824 /* Same as WFX order */
832 SetDefaultWFXChannelOrder(device
);
834 /* Sets the default order used by WaveFormatEx */
835 void SetDefaultWFXChannelOrder(ALCdevice
*device
)
837 switch(device
->FmtChans
)
839 case DevFmtMono
: device
->DevChannels
[0] = FRONT_CENTER
; break;
841 case DevFmtStereo
: device
->DevChannels
[0] = FRONT_LEFT
;
842 device
->DevChannels
[1] = FRONT_RIGHT
; break;
844 case DevFmtQuad
: device
->DevChannels
[0] = FRONT_LEFT
;
845 device
->DevChannels
[1] = FRONT_RIGHT
;
846 device
->DevChannels
[2] = BACK_LEFT
;
847 device
->DevChannels
[3] = BACK_RIGHT
; break;
849 case DevFmtX51
: device
->DevChannels
[0] = FRONT_LEFT
;
850 device
->DevChannels
[1] = FRONT_RIGHT
;
851 device
->DevChannels
[2] = FRONT_CENTER
;
852 device
->DevChannels
[3] = LFE
;
853 device
->DevChannels
[4] = BACK_LEFT
;
854 device
->DevChannels
[5] = BACK_RIGHT
; break;
856 case DevFmtX51Side
: device
->DevChannels
[0] = FRONT_LEFT
;
857 device
->DevChannels
[1] = FRONT_RIGHT
;
858 device
->DevChannels
[2] = FRONT_CENTER
;
859 device
->DevChannels
[3] = LFE
;
860 device
->DevChannels
[4] = SIDE_LEFT
;
861 device
->DevChannels
[5] = SIDE_RIGHT
; break;
863 case DevFmtX61
: device
->DevChannels
[0] = FRONT_LEFT
;
864 device
->DevChannels
[1] = FRONT_RIGHT
;
865 device
->DevChannels
[2] = FRONT_CENTER
;
866 device
->DevChannels
[3] = LFE
;
867 device
->DevChannels
[4] = BACK_CENTER
;
868 device
->DevChannels
[5] = SIDE_LEFT
;
869 device
->DevChannels
[6] = SIDE_RIGHT
; break;
871 case DevFmtX71
: device
->DevChannels
[0] = FRONT_LEFT
;
872 device
->DevChannels
[1] = FRONT_RIGHT
;
873 device
->DevChannels
[2] = FRONT_CENTER
;
874 device
->DevChannels
[3] = LFE
;
875 device
->DevChannels
[4] = BACK_LEFT
;
876 device
->DevChannels
[5] = BACK_RIGHT
;
877 device
->DevChannels
[6] = SIDE_LEFT
;
878 device
->DevChannels
[7] = SIDE_RIGHT
; break;
883 const ALCchar
*DevFmtTypeString(enum DevFmtType type
)
887 case DevFmtByte
: return "Signed Byte";
888 case DevFmtUByte
: return "Unsigned Byte";
889 case DevFmtShort
: return "Signed Short";
890 case DevFmtUShort
: return "Unsigned Short";
891 case DevFmtInt
: return "Signed Int";
892 case DevFmtUInt
: return "Unsigned Int";
893 case DevFmtFloat
: return "Float";
895 return "(unknown type)";
897 const ALCchar
*DevFmtChannelsString(enum DevFmtChannels chans
)
901 case DevFmtMono
: return "Mono";
902 case DevFmtStereo
: return "Stereo";
903 case DevFmtQuad
: return "Quadraphonic";
904 case DevFmtX51
: return "5.1 Surround";
905 case DevFmtX51Side
: return "5.1 Side";
906 case DevFmtX61
: return "6.1 Surround";
907 case DevFmtX71
: return "7.1 Surround";
909 return "(unknown channels)";
912 ALuint
BytesFromDevFmt(enum DevFmtType type
)
916 case DevFmtByte
: return sizeof(ALbyte
);
917 case DevFmtUByte
: return sizeof(ALubyte
);
918 case DevFmtShort
: return sizeof(ALshort
);
919 case DevFmtUShort
: return sizeof(ALushort
);
920 case DevFmtInt
: return sizeof(ALint
);
921 case DevFmtUInt
: return sizeof(ALuint
);
922 case DevFmtFloat
: return sizeof(ALfloat
);
926 ALuint
ChannelsFromDevFmt(enum DevFmtChannels chans
)
930 case DevFmtMono
: return 1;
931 case DevFmtStereo
: return 2;
932 case DevFmtQuad
: return 4;
933 case DevFmtX51
: return 6;
934 case DevFmtX51Side
: return 6;
935 case DevFmtX61
: return 7;
936 case DevFmtX71
: return 8;
940 static ALboolean
DecomposeDevFormat(ALenum format
, enum DevFmtChannels
*chans
,
941 enum DevFmtType
*type
)
943 static const struct {
945 enum DevFmtChannels channels
;
946 enum DevFmtType type
;
948 { AL_FORMAT_MONO8
, DevFmtMono
, DevFmtUByte
},
949 { AL_FORMAT_MONO16
, DevFmtMono
, DevFmtShort
},
950 { AL_FORMAT_MONO_FLOAT32
, DevFmtMono
, DevFmtFloat
},
952 { AL_FORMAT_STEREO8
, DevFmtStereo
, DevFmtUByte
},
953 { AL_FORMAT_STEREO16
, DevFmtStereo
, DevFmtShort
},
954 { AL_FORMAT_STEREO_FLOAT32
, DevFmtStereo
, DevFmtFloat
},
956 { AL_FORMAT_QUAD8
, DevFmtQuad
, DevFmtUByte
},
957 { AL_FORMAT_QUAD16
, DevFmtQuad
, DevFmtShort
},
958 { AL_FORMAT_QUAD32
, DevFmtQuad
, DevFmtFloat
},
960 { AL_FORMAT_51CHN8
, DevFmtX51
, DevFmtUByte
},
961 { AL_FORMAT_51CHN16
, DevFmtX51
, DevFmtShort
},
962 { AL_FORMAT_51CHN32
, DevFmtX51
, DevFmtFloat
},
964 { AL_FORMAT_61CHN8
, DevFmtX61
, DevFmtUByte
},
965 { AL_FORMAT_61CHN16
, DevFmtX61
, DevFmtShort
},
966 { AL_FORMAT_61CHN32
, DevFmtX61
, DevFmtFloat
},
968 { AL_FORMAT_71CHN8
, DevFmtX71
, DevFmtUByte
},
969 { AL_FORMAT_71CHN16
, DevFmtX71
, DevFmtShort
},
970 { AL_FORMAT_71CHN32
, DevFmtX71
, DevFmtFloat
},
974 for(i
= 0;i
< COUNTOF(list
);i
++)
976 if(list
[i
].format
== format
)
978 *chans
= list
[i
].channels
;
979 *type
= list
[i
].type
;
987 static ALCboolean
IsValidALCType(ALCenum type
)
992 case ALC_UNSIGNED_BYTE_SOFT
:
994 case ALC_UNSIGNED_SHORT_SOFT
:
996 case ALC_UNSIGNED_INT_SOFT
:
1003 static ALCboolean
IsValidALCChannels(ALCenum channels
)
1008 case ALC_STEREO_SOFT
:
1010 case ALC_5POINT1_SOFT
:
1011 case ALC_6POINT1_SOFT
:
1012 case ALC_7POINT1_SOFT
:
1021 * Stores the latest ALC Error
1023 static void alcSetError(ALCdevice
*device
, ALCenum errorCode
)
1028 /* DebugBreak() will cause an exception if there is no debugger */
1029 if(IsDebuggerPresent())
1031 #elif defined(SIGTRAP)
1037 device
->LastError
= errorCode
;
1039 g_eLastNullDeviceError
= errorCode
;
1043 /* UpdateDeviceParams
1045 * Updates device parameters according to the attribute list (caller is
1046 * responsible for holding the list lock).
1048 static ALCenum
UpdateDeviceParams(ALCdevice
*device
, const ALCint
*attrList
)
1050 ALCcontext
*context
;
1051 enum DevFmtChannels oldChans
;
1052 enum DevFmtType oldType
;
1057 if(device
->IsLoopbackDevice
&& !(attrList
&& attrList
[0]))
1059 WARN("Missing attributes for loopback device\n");
1060 return ALC_INVALID_VALUE
;
1063 // Check for attributes
1064 if(attrList
&& attrList
[0])
1070 GotAll
= GotFreq
|GotChans
|GotType
1072 ALCuint freq
, numMono
, numStereo
, numSends
;
1073 enum DevFmtChannels schans
;
1074 enum DevFmtType stype
;
1075 ALCuint attrIdx
= 0;
1078 // If a context is already running on the device, stop playback so the
1079 // device attributes can be updated
1080 if((device
->Flags
&DEVICE_RUNNING
))
1081 ALCdevice_StopPlayback(device
);
1082 device
->Flags
&= ~DEVICE_RUNNING
;
1084 freq
= device
->Frequency
;
1085 schans
= device
->FmtChans
;
1086 stype
= device
->FmtType
;
1087 numMono
= device
->NumMonoSources
;
1088 numStereo
= device
->NumStereoSources
;
1089 numSends
= device
->NumAuxSends
;
1091 while(attrList
[attrIdx
])
1093 if(attrList
[attrIdx
] == ALC_FORMAT_CHANNELS_SOFT
&&
1094 device
->IsLoopbackDevice
)
1096 ALCint val
= attrList
[attrIdx
+ 1];
1097 if(!IsValidALCChannels(val
) || !ChannelsFromDevFmt(val
))
1098 return ALC_INVALID_VALUE
;
1103 if(attrList
[attrIdx
] == ALC_FORMAT_TYPE_SOFT
&&
1104 device
->IsLoopbackDevice
)
1106 ALCint val
= attrList
[attrIdx
+ 1];
1107 if(!IsValidALCType(val
) || !BytesFromDevFmt(val
))
1108 return ALC_INVALID_VALUE
;
1113 if(attrList
[attrIdx
] == ALC_FREQUENCY
)
1115 if(device
->IsLoopbackDevice
)
1117 freq
= attrList
[attrIdx
+ 1];
1118 if(freq
< MIN_OUTPUT_RATE
)
1119 return ALC_INVALID_VALUE
;
1124 freq
= attrList
[attrIdx
+ 1];
1125 device
->Flags
|= DEVICE_FREQUENCY_REQUEST
;
1129 if(attrList
[attrIdx
] == ALC_STEREO_SOURCES
)
1131 numStereo
= attrList
[attrIdx
+ 1];
1132 if(numStereo
> device
->MaxNoOfSources
)
1133 numStereo
= device
->MaxNoOfSources
;
1135 numMono
= device
->MaxNoOfSources
- numStereo
;
1138 if(attrList
[attrIdx
] == ALC_MAX_AUXILIARY_SENDS
)
1139 numSends
= attrList
[attrIdx
+ 1];
1144 if(device
->IsLoopbackDevice
)
1146 if(gotFmt
!= GotAll
)
1148 WARN("Missing format for loopback device\n");
1149 return ALC_INVALID_VALUE
;
1154 ConfigValueUInt(NULL
, "frequency", &freq
);
1155 freq
= maxu(freq
, MIN_OUTPUT_RATE
);
1157 ConfigValueUInt(NULL
, "sends", &numSends
);
1158 numSends
= minu(MAX_SENDS
, numSends
);
1160 device
->UpdateSize
= (ALuint64
)device
->UpdateSize
* freq
/
1163 device
->Frequency
= freq
;
1164 device
->FmtChans
= schans
;
1165 device
->FmtType
= stype
;
1166 device
->NumMonoSources
= numMono
;
1167 device
->NumStereoSources
= numStereo
;
1168 device
->NumAuxSends
= numSends
;
1171 if((device
->Flags
&DEVICE_RUNNING
))
1172 return ALC_NO_ERROR
;
1176 oldFreq
= device
->Frequency
;
1177 oldChans
= device
->FmtChans
;
1178 oldType
= device
->FmtType
;
1180 TRACE("Format pre-setup: %s%s, %s%s, %uhz%s, %u update size x%d\n",
1181 DevFmtChannelsString(device
->FmtChans
),
1182 (device
->Flags
&DEVICE_CHANNELS_REQUEST
)?" (requested)":"",
1183 DevFmtTypeString(device
->FmtType
),
1184 (device
->Flags
&DEVICE_SAMPLE_TYPE_REQUEST
)?" (requested)":"",
1186 (device
->Flags
&DEVICE_FREQUENCY_REQUEST
)?" (requested)":"",
1187 device
->UpdateSize
, device
->NumUpdates
);
1188 if(ALCdevice_ResetPlayback(device
) == ALC_FALSE
)
1190 UnlockDevice(device
);
1191 return ALC_INVALID_DEVICE
;
1193 device
->Flags
|= DEVICE_RUNNING
;
1195 if(device
->FmtChans
!= oldChans
&& (device
->Flags
&DEVICE_CHANNELS_REQUEST
))
1197 ERR("Failed to set %s, got %s instead\n", DevFmtChannelsString(oldChans
),
1198 DevFmtChannelsString(device
->FmtChans
));
1199 device
->Flags
&= ~DEVICE_CHANNELS_REQUEST
;
1201 if(device
->FmtType
!= oldType
&& (device
->Flags
&DEVICE_SAMPLE_TYPE_REQUEST
))
1203 ERR("Failed to set %s, got %s instead\n", DevFmtTypeString(oldType
),
1204 DevFmtTypeString(device
->FmtType
));
1205 device
->Flags
&= ~DEVICE_SAMPLE_TYPE_REQUEST
;
1207 if(device
->Frequency
!= oldFreq
&& (device
->Flags
&DEVICE_FREQUENCY_REQUEST
))
1209 ERR("Failed to set %uhz, got %uhz instead\n", oldFreq
, device
->Frequency
);
1210 device
->Flags
&= ~DEVICE_FREQUENCY_REQUEST
;
1213 TRACE("Format post-setup: %s, %s, %uhz, %u update size x%d\n",
1214 DevFmtChannelsString(device
->FmtChans
),
1215 DevFmtTypeString(device
->FmtType
), device
->Frequency
,
1216 device
->UpdateSize
, device
->NumUpdates
);
1218 aluInitPanning(device
);
1220 for(i
= 0;i
< MAXCHANNELS
;i
++)
1222 device
->ClickRemoval
[i
] = 0.0f
;
1223 device
->PendingClicks
[i
] = 0.0f
;
1226 device
->Hrtf
= NULL
;
1227 if(!device
->IsLoopbackDevice
&& GetConfigValueBool(NULL
, "hrtf", AL_FALSE
))
1228 device
->Hrtf
= GetHrtf(device
);
1229 TRACE("HRTF %s\n", device
->Hrtf
?"enabled":"disabled");
1231 if(!device
->Hrtf
&& device
->Bs2bLevel
> 0 && device
->Bs2bLevel
<= 6)
1235 device
->Bs2b
= calloc(1, sizeof(*device
->Bs2b
));
1236 bs2b_clear(device
->Bs2b
);
1238 bs2b_set_srate(device
->Bs2b
, device
->Frequency
);
1239 bs2b_set_level(device
->Bs2b
, device
->Bs2bLevel
);
1240 TRACE("BS2B level %d\n", device
->Bs2bLevel
);
1245 device
->Bs2b
= NULL
;
1246 TRACE("BS2B disabled\n");
1249 device
->Flags
&= ~DEVICE_DUPLICATE_STEREO
;
1250 switch(device
->FmtChans
)
1260 if(GetConfigValueBool(NULL
, "stereodup", AL_TRUE
))
1261 device
->Flags
|= DEVICE_DUPLICATE_STEREO
;
1264 TRACE("Stereo duplication %s\n", (device
->Flags
&DEVICE_DUPLICATE_STEREO
)?"enabled":"disabled");
1266 oldMode
= SetMixerFPUMode();
1267 context
= device
->ContextList
;
1272 context
->UpdateSources
= AL_FALSE
;
1273 LockUIntMapRead(&context
->EffectSlotMap
);
1274 for(pos
= 0;pos
< context
->EffectSlotMap
.size
;pos
++)
1276 ALeffectslot
*slot
= context
->EffectSlotMap
.array
[pos
].value
;
1278 if(ALeffectState_DeviceUpdate(slot
->EffectState
, device
) == AL_FALSE
)
1280 UnlockUIntMapRead(&context
->EffectSlotMap
);
1281 RestoreFPUMode(oldMode
);
1282 UnlockDevice(device
);
1283 ALCdevice_StopPlayback(device
);
1284 device
->Flags
&= ~DEVICE_RUNNING
;
1285 return ALC_INVALID_DEVICE
;
1287 slot
->NeedsUpdate
= AL_FALSE
;
1288 ALeffectState_Update(slot
->EffectState
, context
, slot
);
1290 UnlockUIntMapRead(&context
->EffectSlotMap
);
1292 LockUIntMapRead(&context
->SourceMap
);
1293 for(pos
= 0;pos
< context
->SourceMap
.size
;pos
++)
1295 ALsource
*source
= context
->SourceMap
.array
[pos
].value
;
1296 ALuint s
= device
->NumAuxSends
;
1297 while(s
< MAX_SENDS
)
1299 if(source
->Send
[s
].Slot
)
1300 DecrementRef(&source
->Send
[s
].Slot
->ref
);
1301 source
->Send
[s
].Slot
= NULL
;
1302 source
->Send
[s
].WetGain
= 1.0f
;
1303 source
->Send
[s
].WetGainHF
= 1.0f
;
1306 source
->NeedsUpdate
= AL_FALSE
;
1307 ALsource_Update(source
, context
);
1309 UnlockUIntMapRead(&context
->SourceMap
);
1311 context
= context
->next
;
1313 RestoreFPUMode(oldMode
);
1314 UnlockDevice(device
);
1316 return ALC_NO_ERROR
;
1321 * Frees the device structure, and destroys any objects the app failed to
1322 * delete. Called once there's no more references on the device.
1324 static ALCvoid
FreeDevice(ALCdevice
*device
)
1326 TRACE("%p\n", device
);
1328 if(device
->DefaultSlot
)
1330 ALeffectState_Destroy(device
->DefaultSlot
->EffectState
);
1331 device
->DefaultSlot
->EffectState
= NULL
;
1334 if(device
->BufferMap
.size
> 0)
1336 WARN("(%p) Deleting %d Buffer(s)\n", device
, device
->BufferMap
.size
);
1337 ReleaseALBuffers(device
);
1339 ResetUIntMap(&device
->BufferMap
);
1341 if(device
->EffectMap
.size
> 0)
1343 WARN("(%p) Deleting %d Effect(s)\n", device
, device
->EffectMap
.size
);
1344 ReleaseALEffects(device
);
1346 ResetUIntMap(&device
->EffectMap
);
1348 if(device
->FilterMap
.size
> 0)
1350 WARN("(%p) Deleting %d Filter(s)\n", device
, device
->FilterMap
.size
);
1351 ReleaseALFilters(device
);
1353 ResetUIntMap(&device
->FilterMap
);
1356 device
->Bs2b
= NULL
;
1358 free(device
->szDeviceName
);
1359 device
->szDeviceName
= NULL
;
1361 DeleteCriticalSection(&device
->Mutex
);
1367 void ALCdevice_IncRef(ALCdevice
*device
)
1370 ref
= IncrementRef(&device
->ref
);
1371 TRACEREF("%p increasing refcount to %u\n", device
, ref
);
1374 void ALCdevice_DecRef(ALCdevice
*device
)
1377 ref
= DecrementRef(&device
->ref
);
1378 TRACEREF("%p decreasing refcount to %u\n", device
, ref
);
1379 if(ref
== 0) FreeDevice(device
);
1384 * Checks if the device handle is valid, and increments its ref count if so.
1386 static ALCdevice
*VerifyDevice(ALCdevice
*device
)
1388 ALCdevice
*tmpDevice
;
1394 tmpDevice
= DeviceList
;
1395 while(tmpDevice
&& tmpDevice
!= device
)
1396 tmpDevice
= tmpDevice
->next
;
1399 ALCdevice_IncRef(tmpDevice
);
1407 * Initializes context variables
1409 static ALvoid
InitContext(ALCcontext
*pContext
)
1413 //Initialise listener
1414 pContext
->Listener
.Gain
= 1.0f
;
1415 pContext
->Listener
.MetersPerUnit
= 1.0f
;
1416 pContext
->Listener
.Position
[0] = 0.0f
;
1417 pContext
->Listener
.Position
[1] = 0.0f
;
1418 pContext
->Listener
.Position
[2] = 0.0f
;
1419 pContext
->Listener
.Velocity
[0] = 0.0f
;
1420 pContext
->Listener
.Velocity
[1] = 0.0f
;
1421 pContext
->Listener
.Velocity
[2] = 0.0f
;
1422 pContext
->Listener
.Forward
[0] = 0.0f
;
1423 pContext
->Listener
.Forward
[1] = 0.0f
;
1424 pContext
->Listener
.Forward
[2] = -1.0f
;
1425 pContext
->Listener
.Up
[0] = 0.0f
;
1426 pContext
->Listener
.Up
[1] = 1.0f
;
1427 pContext
->Listener
.Up
[2] = 0.0f
;
1428 for(i
= 0;i
< 4;i
++)
1430 for(j
= 0;j
< 4;j
++)
1431 pContext
->Listener
.Matrix
[i
][j
] = ((i
==j
) ? 1.0f
: 0.0f
);
1435 pContext
->LastError
= AL_NO_ERROR
;
1436 pContext
->UpdateSources
= AL_FALSE
;
1437 pContext
->ActiveSourceCount
= 0;
1438 InitUIntMap(&pContext
->SourceMap
, pContext
->Device
->MaxNoOfSources
);
1439 InitUIntMap(&pContext
->EffectSlotMap
, pContext
->Device
->AuxiliaryEffectSlotMax
);
1442 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
1443 pContext
->SourceDistanceModel
= AL_FALSE
;
1444 pContext
->DopplerFactor
= 1.0f
;
1445 pContext
->DopplerVelocity
= 1.0f
;
1446 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
1447 pContext
->DeferUpdates
= AL_FALSE
;
1449 pContext
->ExtensionList
= alExtList
;
1455 * Cleans up the context, and destroys any remaining objects the app failed to
1456 * delete. Called once there's no more references on the context.
1458 static ALCvoid
FreeContext(ALCcontext
*context
)
1460 TRACE("%p\n", context
);
1462 if(context
->SourceMap
.size
> 0)
1464 ERR("(%p) Deleting %d Source(s)\n", context
, context
->SourceMap
.size
);
1465 ReleaseALSources(context
);
1467 ResetUIntMap(&context
->SourceMap
);
1469 if(context
->EffectSlotMap
.size
> 0)
1471 ERR("(%p) Deleting %d AuxiliaryEffectSlot(s)\n", context
, context
->EffectSlotMap
.size
);
1472 ReleaseALAuxiliaryEffectSlots(context
);
1474 ResetUIntMap(&context
->EffectSlotMap
);
1476 context
->ActiveSourceCount
= 0;
1477 free(context
->ActiveSources
);
1478 context
->ActiveSources
= NULL
;
1479 context
->MaxActiveSources
= 0;
1481 context
->ActiveEffectSlotCount
= 0;
1482 free(context
->ActiveEffectSlots
);
1483 context
->ActiveEffectSlots
= NULL
;
1484 context
->MaxActiveEffectSlots
= 0;
1486 ALCdevice_DecRef(context
->Device
);
1487 context
->Device
= NULL
;
1489 //Invalidate context
1490 memset(context
, 0, sizeof(ALCcontext
));
1496 * Removes the context reference from the given device and removes it from
1497 * being current on the running thread or globally.
1499 static void ReleaseContext(ALCcontext
*context
, ALCdevice
*device
)
1501 ALCcontext
*volatile*tmp_ctx
;
1503 if(pthread_getspecific(LocalContext
) == context
)
1505 WARN("%p released while current on thread\n", context
);
1506 pthread_setspecific(LocalContext
, NULL
);
1507 ALCcontext_DecRef(context
);
1510 if(CompExchangePtr((XchgPtr
*)&GlobalContext
, context
, NULL
))
1511 ALCcontext_DecRef(context
);
1514 tmp_ctx
= &device
->ContextList
;
1517 if(CompExchangePtr((XchgPtr
*)tmp_ctx
, context
, context
->next
))
1519 tmp_ctx
= &(*tmp_ctx
)->next
;
1521 UnlockDevice(device
);
1523 ALCcontext_DecRef(context
);
1526 void ALCcontext_IncRef(ALCcontext
*context
)
1529 ref
= IncrementRef(&context
->ref
);
1530 TRACEREF("%p increasing refcount to %u\n", context
, ref
);
1533 void ALCcontext_DecRef(ALCcontext
*context
)
1536 ref
= DecrementRef(&context
->ref
);
1537 TRACEREF("%p decreasing refcount to %u\n", context
, ref
);
1538 if(ref
== 0) FreeContext(context
);
1541 static void ReleaseThreadCtx(void *ptr
)
1543 WARN("%p current for thread being destroyed\n", ptr
);
1544 ALCcontext_DecRef(ptr
);
1549 * Checks that the given context is valid, and increments its reference count.
1551 static ALCcontext
*VerifyContext(ALCcontext
*context
)
1559 ALCcontext
*tmp_ctx
= dev
->ContextList
;
1562 if(tmp_ctx
== context
)
1564 ALCcontext_IncRef(tmp_ctx
);
1568 tmp_ctx
= tmp_ctx
->next
;
1580 * Returns the currently active context, and adds a reference without locking
1583 ALCcontext
*GetContextRef(void)
1585 ALCcontext
*context
;
1587 context
= pthread_getspecific(LocalContext
);
1589 ALCcontext_IncRef(context
);
1593 context
= GlobalContext
;
1595 ALCcontext_IncRef(context
);
1602 ///////////////////////////////////////////////////////
1605 ///////////////////////////////////////////////////////
1606 // ALC Functions calls
1609 // This should probably move to another c file but for now ...
1610 ALC_API ALCdevice
* ALC_APIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
1612 ALCdevice
*device
= NULL
;
1617 if(!CaptureBackend
.name
)
1619 alcSetError(NULL
, ALC_INVALID_VALUE
);
1625 alcSetError(NULL
, ALC_INVALID_VALUE
);
1629 if(deviceName
&& (!deviceName
[0] || strcasecmp(deviceName
, "openal soft") == 0 || strcasecmp(deviceName
, "openal-soft") == 0))
1632 device
= calloc(1, sizeof(ALCdevice
));
1635 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
1640 device
->Funcs
= &CaptureBackend
.Funcs
;
1642 device
->Connected
= ALC_TRUE
;
1643 device
->IsCaptureDevice
= AL_TRUE
;
1644 device
->IsLoopbackDevice
= AL_FALSE
;
1645 InitializeCriticalSection(&device
->Mutex
);
1647 InitUIntMap(&device
->BufferMap
, ~0);
1648 InitUIntMap(&device
->EffectMap
, ~0);
1649 InitUIntMap(&device
->FilterMap
, ~0);
1651 device
->szDeviceName
= NULL
;
1653 device
->Flags
|= DEVICE_FREQUENCY_REQUEST
;
1654 device
->Frequency
= frequency
;
1656 device
->Flags
|= DEVICE_CHANNELS_REQUEST
| DEVICE_SAMPLE_TYPE_REQUEST
;
1657 if(DecomposeDevFormat(format
, &device
->FmtChans
, &device
->FmtType
) == AL_FALSE
)
1659 DeleteCriticalSection(&device
->Mutex
);
1661 alcSetError(NULL
, ALC_INVALID_ENUM
);
1665 device
->UpdateSize
= SampleSize
;
1666 device
->NumUpdates
= 1;
1669 if((err
=ALCdevice_OpenCapture(device
, deviceName
)) != ALC_NO_ERROR
)
1672 DeleteCriticalSection(&device
->Mutex
);
1674 alcSetError(NULL
, err
);
1680 device
->next
= DeviceList
;
1681 } while(!CompExchangePtr((XchgPtr
*)&DeviceList
, device
->next
, device
));
1683 TRACE("Created device %p\n", device
);
1687 ALC_API ALCboolean ALC_APIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
1689 ALCdevice
*volatile*list
;
1693 while(*list
&& *list
!= pDevice
)
1694 list
= &(*list
)->next
;
1696 if(!*list
|| !(*list
)->IsCaptureDevice
)
1698 alcSetError(*list
, ALC_INVALID_DEVICE
);
1703 *list
= (*list
)->next
;
1706 LockDevice(pDevice
);
1707 ALCdevice_CloseCapture(pDevice
);
1708 UnlockDevice(pDevice
);
1710 ALCdevice_DecRef(pDevice
);
1715 ALC_API
void ALC_APIENTRY
alcCaptureStart(ALCdevice
*device
)
1717 if(!(device
=VerifyDevice(device
)) || !device
->IsCaptureDevice
)
1719 alcSetError(device
, ALC_INVALID_DEVICE
);
1720 if(device
) ALCdevice_DecRef(device
);
1724 if(device
->Connected
)
1725 ALCdevice_StartCapture(device
);
1726 UnlockDevice(device
);
1728 ALCdevice_DecRef(device
);
1731 ALC_API
void ALC_APIENTRY
alcCaptureStop(ALCdevice
*device
)
1733 if(!(device
=VerifyDevice(device
)) || !device
->IsCaptureDevice
)
1735 alcSetError(device
, ALC_INVALID_DEVICE
);
1736 if(device
) ALCdevice_DecRef(device
);
1740 if(device
->Connected
)
1741 ALCdevice_StopCapture(device
);
1742 UnlockDevice(device
);
1744 ALCdevice_DecRef(device
);
1747 ALC_API
void ALC_APIENTRY
alcCaptureSamples(ALCdevice
*device
, ALCvoid
*buffer
, ALCsizei samples
)
1749 ALCenum err
= ALC_INVALID_DEVICE
;
1750 if((device
=VerifyDevice(device
)) != NULL
&& device
->IsCaptureDevice
)
1752 err
= ALC_INVALID_VALUE
;
1754 if(samples
>= 0 && ALCdevice_AvailableSamples(device
) >= (ALCuint
)samples
)
1755 err
= ALCdevice_CaptureSamples(device
, buffer
, samples
);
1756 UnlockDevice(device
);
1758 if(err
!= ALC_NO_ERROR
)
1759 alcSetError(device
, err
);
1760 if(device
) ALCdevice_DecRef(device
);
1766 Return last ALC generated error code
1768 ALC_API ALCenum ALC_APIENTRY
alcGetError(ALCdevice
*device
)
1772 if(VerifyDevice(device
))
1774 errorCode
= ExchangeInt(&device
->LastError
, ALC_NO_ERROR
);
1775 ALCdevice_DecRef(device
);
1778 errorCode
= ExchangeInt(&g_eLastNullDeviceError
, ALC_NO_ERROR
);
1784 /* alcSuspendContext
1788 ALC_API ALCvoid ALC_APIENTRY
alcSuspendContext(ALCcontext
*Context
)
1793 /* alcProcessContext
1797 ALC_API ALCvoid ALC_APIENTRY
alcProcessContext(ALCcontext
*Context
)
1805 * Returns information about the Device, and error strings
1807 ALC_API
const ALCchar
* ALC_APIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
1809 const ALCchar
*value
= NULL
;
1817 case ALC_INVALID_ENUM
:
1818 value
= alcErrInvalidEnum
;
1821 case ALC_INVALID_VALUE
:
1822 value
= alcErrInvalidValue
;
1825 case ALC_INVALID_DEVICE
:
1826 value
= alcErrInvalidDevice
;
1829 case ALC_INVALID_CONTEXT
:
1830 value
= alcErrInvalidContext
;
1833 case ALC_OUT_OF_MEMORY
:
1834 value
= alcErrOutOfMemory
;
1837 case ALC_DEVICE_SPECIFIER
:
1838 if(VerifyDevice(pDevice
))
1840 value
= pDevice
->szDeviceName
;
1841 ALCdevice_DecRef(pDevice
);
1846 value
= alcDeviceList
;
1850 case ALC_ALL_DEVICES_SPECIFIER
:
1851 ProbeAllDeviceList();
1852 value
= alcAllDeviceList
;
1855 case ALC_CAPTURE_DEVICE_SPECIFIER
:
1856 if(VerifyDevice(pDevice
))
1858 value
= pDevice
->szDeviceName
;
1859 ALCdevice_DecRef(pDevice
);
1863 ProbeCaptureDeviceList();
1864 value
= alcCaptureDeviceList
;
1868 /* Default devices are always first in the list */
1869 case ALC_DEFAULT_DEVICE_SPECIFIER
:
1873 pDevice
= VerifyDevice(pDevice
);
1875 free(alcDefaultDeviceSpecifier
);
1876 alcDefaultDeviceSpecifier
= strdup(alcDeviceList
? alcDeviceList
: "");
1877 if(!alcDefaultDeviceSpecifier
)
1878 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1880 value
= alcDefaultDeviceSpecifier
;
1881 if(pDevice
) ALCdevice_DecRef(pDevice
);
1884 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
1885 if(!alcAllDeviceList
)
1886 ProbeAllDeviceList();
1888 pDevice
= VerifyDevice(pDevice
);
1890 free(alcDefaultAllDeviceSpecifier
);
1891 alcDefaultAllDeviceSpecifier
= strdup(alcAllDeviceList
?
1892 alcAllDeviceList
: "");
1893 if(!alcDefaultAllDeviceSpecifier
)
1894 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1896 value
= alcDefaultAllDeviceSpecifier
;
1897 if(pDevice
) ALCdevice_DecRef(pDevice
);
1900 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
1901 if(!alcCaptureDeviceList
)
1902 ProbeCaptureDeviceList();
1904 pDevice
= VerifyDevice(pDevice
);
1906 free(alcCaptureDefaultDeviceSpecifier
);
1907 alcCaptureDefaultDeviceSpecifier
= strdup(alcCaptureDeviceList
?
1908 alcCaptureDeviceList
: "");
1909 if(!alcCaptureDefaultDeviceSpecifier
)
1910 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1912 value
= alcCaptureDefaultDeviceSpecifier
;
1913 if(pDevice
) ALCdevice_DecRef(pDevice
);
1916 case ALC_EXTENSIONS
:
1917 if(!VerifyDevice(pDevice
))
1918 value
= alcNoDeviceExtList
;
1921 value
= alcExtensionList
;
1922 ALCdevice_DecRef(pDevice
);
1927 pDevice
= VerifyDevice(pDevice
);
1928 alcSetError(pDevice
, ALC_INVALID_ENUM
);
1929 if(pDevice
) ALCdevice_DecRef(pDevice
);
1939 * Returns information about the Device and the version of Open AL
1941 ALC_API ALCvoid ALC_APIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
1943 device
= VerifyDevice(device
);
1945 if(size
== 0 || data
== NULL
)
1947 alcSetError(device
, ALC_INVALID_VALUE
);
1948 if(device
) ALCdevice_DecRef(device
);
1956 case ALC_MAJOR_VERSION
:
1957 *data
= alcMajorVersion
;
1959 case ALC_MINOR_VERSION
:
1960 *data
= alcMinorVersion
;
1963 case ALC_ATTRIBUTES_SIZE
:
1964 case ALC_ALL_ATTRIBUTES
:
1968 case ALC_MONO_SOURCES
:
1969 case ALC_STEREO_SOURCES
:
1970 case ALC_CAPTURE_SAMPLES
:
1971 case ALC_FORMAT_CHANNELS_SOFT
:
1972 case ALC_FORMAT_TYPE_SOFT
:
1973 alcSetError(NULL
, ALC_INVALID_DEVICE
);
1977 alcSetError(NULL
, ALC_INVALID_ENUM
);
1981 else if(device
->IsCaptureDevice
)
1985 case ALC_CAPTURE_SAMPLES
:
1987 *data
= ALCdevice_AvailableSamples(device
);
1988 UnlockDevice(device
);
1992 *data
= device
->Connected
;
1996 alcSetError(device
, ALC_INVALID_ENUM
);
2000 else /* render device */
2004 case ALC_MAJOR_VERSION
:
2005 *data
= alcMajorVersion
;
2008 case ALC_MINOR_VERSION
:
2009 *data
= alcMinorVersion
;
2012 case ALC_EFX_MAJOR_VERSION
:
2013 *data
= alcEFXMajorVersion
;
2016 case ALC_EFX_MINOR_VERSION
:
2017 *data
= alcEFXMinorVersion
;
2020 case ALC_ATTRIBUTES_SIZE
:
2024 case ALC_ALL_ATTRIBUTES
:
2026 alcSetError(device
, ALC_INVALID_VALUE
);
2031 data
[i
++] = ALC_FREQUENCY
;
2032 data
[i
++] = device
->Frequency
;
2034 if(!device
->IsLoopbackDevice
)
2036 data
[i
++] = ALC_REFRESH
;
2037 data
[i
++] = device
->Frequency
/ device
->UpdateSize
;
2039 data
[i
++] = ALC_SYNC
;
2040 data
[i
++] = ALC_FALSE
;
2044 data
[i
++] = ALC_FORMAT_CHANNELS_SOFT
;
2045 data
[i
++] = device
->FmtChans
;
2047 data
[i
++] = ALC_FORMAT_TYPE_SOFT
;
2048 data
[i
++] = device
->FmtType
;
2051 data
[i
++] = ALC_MONO_SOURCES
;
2052 data
[i
++] = device
->NumMonoSources
;
2054 data
[i
++] = ALC_STEREO_SOURCES
;
2055 data
[i
++] = device
->NumStereoSources
;
2057 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
2058 data
[i
++] = device
->NumAuxSends
;
2065 *data
= device
->Frequency
;
2069 if(device
->IsLoopbackDevice
)
2070 alcSetError(device
, ALC_INVALID_DEVICE
);
2072 *data
= device
->Frequency
/ device
->UpdateSize
;
2076 if(device
->IsLoopbackDevice
)
2077 alcSetError(device
, ALC_INVALID_DEVICE
);
2082 case ALC_FORMAT_CHANNELS_SOFT
:
2083 if(!device
->IsLoopbackDevice
)
2084 alcSetError(device
, ALC_INVALID_DEVICE
);
2086 *data
= device
->FmtChans
;
2089 case ALC_FORMAT_TYPE_SOFT
:
2090 if(!device
->IsLoopbackDevice
)
2091 alcSetError(device
, ALC_INVALID_DEVICE
);
2093 *data
= device
->FmtType
;
2096 case ALC_MONO_SOURCES
:
2097 *data
= device
->NumMonoSources
;
2100 case ALC_STEREO_SOURCES
:
2101 *data
= device
->NumStereoSources
;
2104 case ALC_MAX_AUXILIARY_SENDS
:
2105 *data
= device
->NumAuxSends
;
2109 *data
= device
->Connected
;
2113 alcSetError(device
, ALC_INVALID_ENUM
);
2118 ALCdevice_DecRef(device
);
2122 /* alcIsExtensionPresent
2124 * Determines if there is support for a particular extension
2126 ALC_API ALCboolean ALC_APIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
2128 ALCboolean bResult
= ALC_FALSE
;
2130 device
= VerifyDevice(device
);
2133 alcSetError(device
, ALC_INVALID_VALUE
);
2136 size_t len
= strlen(extName
);
2137 const char *ptr
= (device
? alcExtensionList
: alcNoDeviceExtList
);
2140 if(strncasecmp(ptr
, extName
, len
) == 0 &&
2141 (ptr
[len
] == '\0' || isspace(ptr
[len
])))
2146 if((ptr
=strchr(ptr
, ' ')) != NULL
)
2150 } while(isspace(*ptr
));
2155 ALCdevice_DecRef(device
);
2160 /* alcGetProcAddress
2162 * Retrieves the function address for a particular extension function
2164 ALC_API ALCvoid
* ALC_APIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
2166 ALCvoid
*ptr
= NULL
;
2168 device
= VerifyDevice(device
);
2171 alcSetError(device
, ALC_INVALID_VALUE
);
2175 while(alcFunctions
[i
].funcName
&& strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
2177 ptr
= alcFunctions
[i
].address
;
2180 ALCdevice_DecRef(device
);
2187 * Get the value for a particular ALC Enumerated Value
2189 ALC_API ALCenum ALC_APIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
2193 device
= VerifyDevice(device
);
2196 alcSetError(device
, ALC_INVALID_VALUE
);
2200 while(enumeration
[i
].enumName
&& strcmp(enumeration
[i
].enumName
,enumName
) != 0)
2202 val
= enumeration
[i
].value
;
2205 ALCdevice_DecRef(device
);
2212 * Create and attach a Context to a particular Device.
2214 ALC_API ALCcontext
* ALC_APIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
2216 ALCcontext
*ALContext
;
2220 if(!(device
=VerifyDevice(device
)) || device
->IsCaptureDevice
|| !device
->Connected
)
2223 alcSetError(device
, ALC_INVALID_DEVICE
);
2224 if(device
) ALCdevice_DecRef(device
);
2228 /* Reset Context Last Error code */
2229 device
->LastError
= ALC_NO_ERROR
;
2231 if((err
=UpdateDeviceParams(device
, attrList
)) != ALC_NO_ERROR
)
2234 alcSetError(device
, err
);
2235 if(err
== ALC_INVALID_DEVICE
)
2236 aluHandleDisconnect(device
);
2237 ALCdevice_DecRef(device
);
2241 ALContext
= calloc(1, sizeof(ALCcontext
));
2246 ALContext
->MaxActiveSources
= 256;
2247 ALContext
->ActiveSources
= malloc(sizeof(ALContext
->ActiveSources
[0]) *
2248 ALContext
->MaxActiveSources
);
2250 if(!ALContext
|| !ALContext
->ActiveSources
)
2252 if(!device
->ContextList
)
2254 ALCdevice_StopPlayback(device
);
2255 device
->Flags
&= ~DEVICE_RUNNING
;
2262 alcSetError(device
, ALC_OUT_OF_MEMORY
);
2263 ALCdevice_DecRef(device
);
2267 ALContext
->Device
= device
;
2268 ALCdevice_IncRef(device
);
2269 InitContext(ALContext
);
2272 ALContext
->next
= device
->ContextList
;
2273 } while(!CompExchangePtr((XchgPtr
*)&device
->ContextList
, ALContext
->next
, ALContext
));
2276 if(device
->DefaultSlot
)
2277 InitializeEffect(ALContext
, device
->DefaultSlot
, &ForcedEffect
);
2278 ALContext
->LastError
= AL_NO_ERROR
;
2280 ALCdevice_DecRef(device
);
2282 TRACE("Created context %p\n", ALContext
);
2286 /* alcDestroyContext
2290 ALC_API ALCvoid ALC_APIENTRY
alcDestroyContext(ALCcontext
*context
)
2295 /* alcGetContextsDevice sets an error for invalid contexts */
2296 Device
= alcGetContextsDevice(context
);
2299 ReleaseContext(context
, Device
);
2300 if(!Device
->ContextList
)
2302 ALCdevice_StopPlayback(Device
);
2303 Device
->Flags
&= ~DEVICE_RUNNING
;
2310 /* alcGetCurrentContext
2312 * Returns the currently active Context
2314 ALC_API ALCcontext
* ALC_APIENTRY
alcGetCurrentContext(ALCvoid
)
2316 ALCcontext
*Context
;
2318 Context
= pthread_getspecific(LocalContext
);
2319 if(!Context
) Context
= GlobalContext
;
2324 /* alcGetThreadContext
2326 * Returns the currently active thread-local Context
2328 ALC_API ALCcontext
* ALC_APIENTRY
alcGetThreadContext(void)
2330 ALCcontext
*Context
;
2331 Context
= pthread_getspecific(LocalContext
);
2336 /* alcMakeContextCurrent
2338 * Makes the given Context the active Context
2340 ALC_API ALCboolean ALC_APIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
2342 /* context must be a valid Context or NULL */
2343 if(context
&& !(context
=VerifyContext(context
)))
2345 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2348 /* context's reference count is already incremented */
2349 context
= ExchangePtr((XchgPtr
*)&GlobalContext
, context
);
2350 if(context
) ALCcontext_DecRef(context
);
2352 if((context
=pthread_getspecific(LocalContext
)) != NULL
)
2354 pthread_setspecific(LocalContext
, NULL
);
2355 ALCcontext_DecRef(context
);
2361 /* alcSetThreadContext
2363 * Makes the given Context the active Context for the current thread
2365 ALC_API ALCboolean ALC_APIENTRY
alcSetThreadContext(ALCcontext
*context
)
2369 /* context must be a valid Context or NULL */
2370 if(context
&& !(context
=VerifyContext(context
)))
2372 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2375 /* context's reference count is already incremented */
2376 old
= pthread_getspecific(LocalContext
);
2377 pthread_setspecific(LocalContext
, context
);
2378 if(old
) ALCcontext_DecRef(old
);
2384 /* alcGetContextsDevice
2386 * Returns the Device that a particular Context is attached to
2388 ALC_API ALCdevice
* ALC_APIENTRY
alcGetContextsDevice(ALCcontext
*Context
)
2392 if(!(Context
=VerifyContext(Context
)))
2394 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2397 Device
= Context
->Device
;
2398 ALCcontext_DecRef(Context
);
2406 * Open the Device specified.
2408 ALC_API ALCdevice
* ALC_APIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
2416 if(!PlaybackBackend
.name
)
2418 alcSetError(NULL
, ALC_INVALID_VALUE
);
2422 if(deviceName
&& (!deviceName
[0] || strcasecmp(deviceName
, "openal soft") == 0 || strcasecmp(deviceName
, "openal-soft") == 0))
2425 device
= calloc(1, sizeof(ALCdevice
)+sizeof(ALeffectslot
));
2428 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
2433 device
->Funcs
= &PlaybackBackend
.Funcs
;
2435 device
->Connected
= ALC_TRUE
;
2436 device
->IsCaptureDevice
= AL_FALSE
;
2437 device
->IsLoopbackDevice
= AL_FALSE
;
2438 InitializeCriticalSection(&device
->Mutex
);
2439 device
->LastError
= ALC_NO_ERROR
;
2442 device
->Bs2b
= NULL
;
2443 device
->Bs2bLevel
= 0;
2444 device
->szDeviceName
= NULL
;
2446 device
->ContextList
= NULL
;
2448 device
->MaxNoOfSources
= 256;
2449 device
->AuxiliaryEffectSlotMax
= 4;
2450 device
->NumAuxSends
= MAX_SENDS
;
2452 InitUIntMap(&device
->BufferMap
, ~0);
2453 InitUIntMap(&device
->EffectMap
, ~0);
2454 InitUIntMap(&device
->FilterMap
, ~0);
2457 device
->FmtChans
= DevFmtStereo
;
2458 device
->FmtType
= DevFmtFloat
;
2459 device
->Frequency
= DEFAULT_OUTPUT_RATE
;
2460 device
->NumUpdates
= 4;
2461 device
->UpdateSize
= 1024;
2463 if(ConfigValueStr(NULL
, "channels", &fmt
))
2465 static const struct {
2466 const char name
[16];
2467 enum DevFmtChannels chans
;
2469 { "mono", DevFmtMono
},
2470 { "stereo", DevFmtStereo
},
2471 { "quad", DevFmtQuad
},
2472 { "surround51", DevFmtX51
},
2473 { "surround61", DevFmtX61
},
2474 { "surround71", DevFmtX71
},
2478 for(i
= 0;i
< COUNTOF(chanlist
);i
++)
2480 if(strcasecmp(chanlist
[i
].name
, fmt
) == 0)
2482 device
->FmtChans
= chanlist
[i
].chans
;
2483 device
->Flags
|= DEVICE_CHANNELS_REQUEST
;
2487 if(i
== COUNTOF(chanlist
))
2488 ERR("Unsupported channels: %s\n", fmt
);
2490 if(ConfigValueStr(NULL
, "sample-type", &fmt
))
2492 static const struct {
2493 const char name
[16];
2494 enum DevFmtType type
;
2496 { "int8", DevFmtByte
},
2497 { "uint8", DevFmtUByte
},
2498 { "int16", DevFmtShort
},
2499 { "uint16", DevFmtUShort
},
2500 { "int32", DevFmtInt
},
2501 { "uint32", DevFmtUInt
},
2502 { "float32", DevFmtFloat
},
2506 for(i
= 0;i
< COUNTOF(typelist
);i
++)
2508 if(strcasecmp(typelist
[i
].name
, fmt
) == 0)
2510 device
->FmtType
= typelist
[i
].type
;
2511 device
->Flags
|= DEVICE_SAMPLE_TYPE_REQUEST
;
2515 if(i
== COUNTOF(typelist
))
2516 ERR("Unsupported sample-type: %s\n", fmt
);
2518 #define DEVICE_FORMAT_REQUEST (DEVICE_CHANNELS_REQUEST|DEVICE_SAMPLE_TYPE_REQUEST)
2519 if((device
->Flags
&DEVICE_FORMAT_REQUEST
) != DEVICE_FORMAT_REQUEST
&&
2520 ConfigValueStr(NULL
, "format", &fmt
))
2522 static const struct {
2523 const char name
[32];
2524 enum DevFmtChannels channels
;
2525 enum DevFmtType type
;
2527 { "AL_FORMAT_MONO32", DevFmtMono
, DevFmtFloat
},
2528 { "AL_FORMAT_STEREO32", DevFmtStereo
, DevFmtFloat
},
2529 { "AL_FORMAT_QUAD32", DevFmtQuad
, DevFmtFloat
},
2530 { "AL_FORMAT_51CHN32", DevFmtX51
, DevFmtFloat
},
2531 { "AL_FORMAT_61CHN32", DevFmtX61
, DevFmtFloat
},
2532 { "AL_FORMAT_71CHN32", DevFmtX71
, DevFmtFloat
},
2534 { "AL_FORMAT_MONO16", DevFmtMono
, DevFmtShort
},
2535 { "AL_FORMAT_STEREO16", DevFmtStereo
, DevFmtShort
},
2536 { "AL_FORMAT_QUAD16", DevFmtQuad
, DevFmtShort
},
2537 { "AL_FORMAT_51CHN16", DevFmtX51
, DevFmtShort
},
2538 { "AL_FORMAT_61CHN16", DevFmtX61
, DevFmtShort
},
2539 { "AL_FORMAT_71CHN16", DevFmtX71
, DevFmtShort
},
2541 { "AL_FORMAT_MONO8", DevFmtMono
, DevFmtByte
},
2542 { "AL_FORMAT_STEREO8", DevFmtStereo
, DevFmtByte
},
2543 { "AL_FORMAT_QUAD8", DevFmtQuad
, DevFmtByte
},
2544 { "AL_FORMAT_51CHN8", DevFmtX51
, DevFmtByte
},
2545 { "AL_FORMAT_61CHN8", DevFmtX61
, DevFmtByte
},
2546 { "AL_FORMAT_71CHN8", DevFmtX71
, DevFmtByte
}
2550 ERR("Option 'format' is deprecated, please use 'channels' and 'sample-type'\n");
2551 for(i
= 0;i
< COUNTOF(formats
);i
++)
2553 if(strcasecmp(fmt
, formats
[i
].name
) == 0)
2555 if(!(device
->Flags
&DEVICE_CHANNELS_REQUEST
))
2556 device
->FmtChans
= formats
[i
].channels
;
2557 if(!(device
->Flags
&DEVICE_SAMPLE_TYPE_REQUEST
))
2558 device
->FmtType
= formats
[i
].type
;
2559 device
->Flags
|= DEVICE_FORMAT_REQUEST
;
2563 if(i
== COUNTOF(formats
))
2564 ERR("Unsupported format: %s\n", fmt
);
2566 #undef DEVICE_FORMAT_REQUEST
2568 if(ConfigValueUInt(NULL
, "frequency", &device
->Frequency
))
2570 device
->Flags
|= DEVICE_FREQUENCY_REQUEST
;
2571 if(device
->Frequency
< MIN_OUTPUT_RATE
)
2572 ERR("%uhz request clamped to %uhz minimum\n", device
->Frequency
, MIN_OUTPUT_RATE
);
2573 device
->Frequency
= maxu(device
->Frequency
, MIN_OUTPUT_RATE
);
2576 ConfigValueUInt(NULL
, "periods", &device
->NumUpdates
);
2577 if(device
->NumUpdates
< 2) device
->NumUpdates
= 4;
2579 ConfigValueUInt(NULL
, "period_size", &device
->UpdateSize
);
2580 if(device
->UpdateSize
== 0) device
->UpdateSize
= 1024;
2582 ConfigValueUInt(NULL
, "sources", &device
->MaxNoOfSources
);
2583 if(device
->MaxNoOfSources
== 0) device
->MaxNoOfSources
= 256;
2585 ConfigValueUInt(NULL
, "slots", &device
->AuxiliaryEffectSlotMax
);
2586 if(device
->AuxiliaryEffectSlotMax
== 0) device
->AuxiliaryEffectSlotMax
= 4;
2588 ConfigValueUInt(NULL
, "sends", &device
->NumAuxSends
);
2589 if(device
->NumAuxSends
> MAX_SENDS
) device
->NumAuxSends
= MAX_SENDS
;
2591 ConfigValueInt(NULL
, "cf_level", &device
->Bs2bLevel
);
2593 device
->NumStereoSources
= 1;
2594 device
->NumMonoSources
= device
->MaxNoOfSources
- device
->NumStereoSources
;
2596 if(ForcedEffect
.type
!= AL_EFFECT_NULL
)
2598 device
->DefaultSlot
= (ALeffectslot
*)(device
+1);
2599 if(InitEffectSlot(device
->DefaultSlot
) != AL_NO_ERROR
)
2600 device
->DefaultSlot
= NULL
;
2603 // Find a playback device to open
2605 if((err
=ALCdevice_OpenPlayback(device
, deviceName
)) != ALC_NO_ERROR
)
2608 DeleteCriticalSection(&device
->Mutex
);
2610 alcSetError(NULL
, err
);
2616 device
->next
= DeviceList
;
2617 } while(!CompExchangePtr((XchgPtr
*)&DeviceList
, device
->next
, device
));
2619 TRACE("Created device %p\n", device
);
2625 * Close the specified Device
2627 ALC_API ALCboolean ALC_APIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
2629 ALCdevice
*volatile*list
;
2634 while(*list
&& *list
!= pDevice
)
2635 list
= &(*list
)->next
;
2637 if(!*list
|| (*list
)->IsCaptureDevice
)
2639 alcSetError(*list
, ALC_INVALID_DEVICE
);
2644 *list
= (*list
)->next
;
2647 if((ctx
=pDevice
->ContextList
) != NULL
)
2650 WARN("Releasing context %p\n", ctx
);
2651 ReleaseContext(ctx
, pDevice
);
2652 } while((ctx
=pDevice
->ContextList
) != NULL
);
2653 ALCdevice_StopPlayback(pDevice
);
2654 pDevice
->Flags
&= ~DEVICE_RUNNING
;
2656 ALCdevice_ClosePlayback(pDevice
);
2658 ALCdevice_DecRef(pDevice
);
2664 /* alcLoopbackOpenDeviceSOFT
2666 * Open a loopback device, for manual rendering.
2668 ALC_API ALCdevice
* ALC_APIENTRY
alcLoopbackOpenDeviceSOFT(ALCdevice
*device
)
2672 /* Make sure the device, if specified, belongs to us. */
2673 if(device
&& !(device
=VerifyDevice(device
)))
2675 alcSetError(device
, ALC_INVALID_DEVICE
);
2678 if(device
) ALCdevice_DecRef(device
);
2680 device
= calloc(1, sizeof(ALCdevice
));
2683 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
2688 device
->Funcs
= &BackendLoopback
.Funcs
;
2690 device
->Connected
= ALC_TRUE
;
2691 device
->IsCaptureDevice
= AL_FALSE
;
2692 device
->IsLoopbackDevice
= AL_TRUE
;
2693 InitializeCriticalSection(&device
->Mutex
);
2694 device
->LastError
= ALC_NO_ERROR
;
2697 device
->Bs2b
= NULL
;
2698 device
->Bs2bLevel
= 0;
2699 device
->szDeviceName
= NULL
;
2701 device
->ContextList
= NULL
;
2703 device
->MaxNoOfSources
= 256;
2704 device
->AuxiliaryEffectSlotMax
= 4;
2705 device
->NumAuxSends
= MAX_SENDS
;
2707 InitUIntMap(&device
->BufferMap
, ~0);
2708 InitUIntMap(&device
->EffectMap
, ~0);
2709 InitUIntMap(&device
->FilterMap
, ~0);
2712 device
->NumUpdates
= 0;
2713 device
->UpdateSize
= 0;
2715 device
->Frequency
= 44100;
2716 device
->FmtChans
= DevFmtStereo
;
2717 device
->FmtType
= DevFmtShort
;
2719 ConfigValueUInt(NULL
, "sources", &device
->MaxNoOfSources
);
2720 if(device
->MaxNoOfSources
== 0) device
->MaxNoOfSources
= 256;
2722 ConfigValueUInt(NULL
, "slots", &device
->AuxiliaryEffectSlotMax
);
2723 if(device
->AuxiliaryEffectSlotMax
== 0) device
->AuxiliaryEffectSlotMax
= 4;
2725 ConfigValueUInt(NULL
, "sends", &device
->NumAuxSends
);
2726 if(device
->NumAuxSends
> MAX_SENDS
) device
->NumAuxSends
= MAX_SENDS
;
2728 device
->NumStereoSources
= 1;
2729 device
->NumMonoSources
= device
->MaxNoOfSources
- device
->NumStereoSources
;
2731 // Open the "backend"
2732 ALCdevice_OpenPlayback(device
, "Loopback");
2734 device
->next
= DeviceList
;
2735 } while(!CompExchangePtr((XchgPtr
*)&DeviceList
, device
->next
, device
));
2737 TRACE("Created device %p\n", device
);
2741 /* alcIsRenderFormatSupportedSOFT
2743 * Determines if the loopback device supports the given format for rendering.
2745 ALC_API ALCboolean ALC_APIENTRY
alcIsRenderFormatSupportedSOFT(ALCdevice
*device
, ALCsizei freq
, ALCenum channels
, ALCenum type
)
2747 ALCboolean ret
= ALC_FALSE
;
2749 if(!(device
=VerifyDevice(device
)) || !device
->IsLoopbackDevice
)
2750 alcSetError(device
, ALC_INVALID_DEVICE
);
2752 alcSetError(device
, ALC_INVALID_VALUE
);
2753 else if(!IsValidALCType(type
) || !IsValidALCChannels(channels
))
2754 alcSetError(device
, ALC_INVALID_ENUM
);
2757 if(BytesFromDevFmt(type
) > 0 && ChannelsFromDevFmt(channels
) > 0 &&
2758 freq
>= MIN_OUTPUT_RATE
)
2761 if(device
) ALCdevice_DecRef(device
);
2766 /* alcRenderSamplesSOFT
2768 * Renders some samples into a buffer, using the format last set by the
2769 * attributes given to alcCreateContext.
2771 ALC_API
void ALC_APIENTRY
alcRenderSamplesSOFT(ALCdevice
*device
, ALCvoid
*buffer
, ALCsizei samples
)
2773 if(!(device
=VerifyDevice(device
)) || !device
->IsLoopbackDevice
)
2774 alcSetError(device
, ALC_INVALID_DEVICE
);
2775 else if(samples
< 0 || (samples
> 0 && buffer
== NULL
))
2776 alcSetError(device
, ALC_INVALID_VALUE
);
2778 aluMixData(device
, buffer
, samples
);
2779 if(device
) ALCdevice_DecRef(device
);
2783 static void ReleaseALC(void)
2787 free(alcDeviceList
); alcDeviceList
= NULL
;
2788 alcDeviceListSize
= 0;
2789 free(alcAllDeviceList
); alcAllDeviceList
= NULL
;
2790 alcAllDeviceListSize
= 0;
2791 free(alcCaptureDeviceList
); alcCaptureDeviceList
= NULL
;
2792 alcCaptureDeviceListSize
= 0;
2794 free(alcDefaultDeviceSpecifier
);
2795 alcDefaultDeviceSpecifier
= NULL
;
2796 free(alcDefaultAllDeviceSpecifier
);
2797 alcDefaultAllDeviceSpecifier
= NULL
;
2798 free(alcCaptureDefaultDeviceSpecifier
);
2799 alcCaptureDefaultDeviceSpecifier
= NULL
;
2801 if((dev
=ExchangePtr((XchgPtr
*)&DeviceList
, NULL
)) != NULL
)
2806 } while((dev
=dev
->next
) != NULL
);
2807 ERR("%u device%s not closed\n", num
, (num
>1)?"s":"");
2811 ///////////////////////////////////////////////////////