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(ConfigValueStr(NULL
, "drivers", &devs
))
623 const char *next
= devs
;
624 int endlist
, delitem
;
629 next
= strchr(devs
, ',');
631 delitem
= (devs
[0] == '-');
632 if(devs
[0] == '-') devs
++;
634 if(!devs
[0] || devs
[0] == ',')
641 len
= (next
? ((size_t)(next
-devs
)) : strlen(devs
));
642 for(n
= i
;BackendList
[n
].Init
;n
++)
644 if(len
== strlen(BackendList
[n
].name
) &&
645 strncmp(BackendList
[n
].name
, devs
, len
) == 0)
650 BackendList
[n
] = BackendList
[n
+1];
652 } while(BackendList
[n
].Init
);
656 struct BackendInfo Bkp
= BackendList
[n
];
659 BackendList
[n
] = BackendList
[n
-1];
662 BackendList
[n
] = Bkp
;
673 BackendList
[i
].name
= NULL
;
674 BackendList
[i
].Init
= NULL
;
675 BackendList
[i
].Deinit
= NULL
;
676 BackendList
[i
].Probe
= NULL
;
680 for(i
= 0;BackendList
[i
].Init
&& (!PlaybackBackend
.name
|| !CaptureBackend
.name
);i
++)
682 if(!BackendList
[i
].Init(&BackendList
[i
].Funcs
))
684 WARN("Failed to initialize backend \"%s\"\n", BackendList
[i
].name
);
688 TRACE("Initialized backend \"%s\"\n", BackendList
[i
].name
);
689 if(BackendList
[i
].Funcs
.OpenPlayback
&& !PlaybackBackend
.name
)
691 PlaybackBackend
= BackendList
[i
];
692 TRACE("Added \"%s\" for playback\n", PlaybackBackend
.name
);
694 if(BackendList
[i
].Funcs
.OpenCapture
&& !CaptureBackend
.name
)
696 CaptureBackend
= BackendList
[i
];
697 TRACE("Added \"%s\" for capture\n", CaptureBackend
.name
);
700 BackendLoopback
.Init(&BackendLoopback
.Funcs
);
702 if(ConfigValueStr(NULL
, "excludefx", &str
))
705 const char *next
= str
;
709 next
= strchr(str
, ',');
711 if(!str
[0] || next
== str
)
714 len
= (next
? ((size_t)(next
-str
)) : strlen(str
));
715 for(n
= 0;EffectList
[n
].name
;n
++)
717 if(len
== strlen(EffectList
[n
].name
) &&
718 strncmp(EffectList
[n
].name
, str
, len
) == 0)
719 DisabledEffects
[EffectList
[n
].type
] = AL_TRUE
;
724 InitEffect(&ForcedEffect
);
725 str
= getenv("__ALSOFT_DEFAULT_REVERB");
727 GetReverbEffect(str
, &ForcedEffect
);
728 else if(ConfigValueStr(NULL
, "default-reverb", &str
))
729 GetReverbEffect(str
, &ForcedEffect
);
733 static void LockLists(void)
735 EnterCriticalSection(&ListLock
);
738 static void UnlockLists(void)
740 LeaveCriticalSection(&ListLock
);
744 static void ProbeList(ALCchar
**list
, size_t *listsize
, enum DevProbe type
)
753 if(type
== CAPTURE_DEVICE_PROBE
)
754 CaptureBackend
.Probe(type
);
756 PlaybackBackend
.Probe(type
);
760 static void ProbeDeviceList(void)
761 { ProbeList(&alcDeviceList
, &alcDeviceListSize
, DEVICE_PROBE
); }
762 static void ProbeAllDeviceList(void)
763 { ProbeList(&alcAllDeviceList
, &alcAllDeviceListSize
, ALL_DEVICE_PROBE
); }
764 static void ProbeCaptureDeviceList(void)
765 { ProbeList(&alcCaptureDeviceList
, &alcCaptureDeviceListSize
, CAPTURE_DEVICE_PROBE
); }
768 static void AppendList(const ALCchar
*name
, ALCchar
**List
, size_t *ListSize
)
770 size_t len
= strlen(name
);
776 temp
= realloc(*List
, (*ListSize
) + len
+ 2);
779 ERR("Realloc failed to add %s!\n", name
);
784 memcpy((*List
)+(*ListSize
), name
, len
+1);
786 (*List
)[*ListSize
] = 0;
789 #define DECL_APPEND_LIST_FUNC(type) \
790 void Append##type##List(const ALCchar *name) \
791 { AppendList(name, &alc##type##List, &alc##type##ListSize); }
793 DECL_APPEND_LIST_FUNC(Device
)
794 DECL_APPEND_LIST_FUNC(AllDevice
)
795 DECL_APPEND_LIST_FUNC(CaptureDevice
)
797 #undef DECL_APPEND_LIST_FUNC
800 /* Sets the default channel order used by most non-WaveFormatEx-based APIs */
801 void SetDefaultChannelOrder(ALCdevice
*device
)
803 switch(device
->FmtChans
)
805 case DevFmtX51
: device
->DevChannels
[0] = FRONT_LEFT
;
806 device
->DevChannels
[1] = FRONT_RIGHT
;
807 device
->DevChannels
[2] = BACK_LEFT
;
808 device
->DevChannels
[3] = BACK_RIGHT
;
809 device
->DevChannels
[4] = FRONT_CENTER
;
810 device
->DevChannels
[5] = LFE
;
813 case DevFmtX71
: device
->DevChannels
[0] = FRONT_LEFT
;
814 device
->DevChannels
[1] = FRONT_RIGHT
;
815 device
->DevChannels
[2] = BACK_LEFT
;
816 device
->DevChannels
[3] = BACK_RIGHT
;
817 device
->DevChannels
[4] = FRONT_CENTER
;
818 device
->DevChannels
[5] = LFE
;
819 device
->DevChannels
[6] = SIDE_LEFT
;
820 device
->DevChannels
[7] = SIDE_RIGHT
;
823 /* Same as WFX order */
831 SetDefaultWFXChannelOrder(device
);
833 /* Sets the default order used by WaveFormatEx */
834 void SetDefaultWFXChannelOrder(ALCdevice
*device
)
836 switch(device
->FmtChans
)
838 case DevFmtMono
: device
->DevChannels
[0] = FRONT_CENTER
; break;
840 case DevFmtStereo
: device
->DevChannels
[0] = FRONT_LEFT
;
841 device
->DevChannels
[1] = FRONT_RIGHT
; break;
843 case DevFmtQuad
: device
->DevChannels
[0] = FRONT_LEFT
;
844 device
->DevChannels
[1] = FRONT_RIGHT
;
845 device
->DevChannels
[2] = BACK_LEFT
;
846 device
->DevChannels
[3] = BACK_RIGHT
; break;
848 case DevFmtX51
: device
->DevChannels
[0] = FRONT_LEFT
;
849 device
->DevChannels
[1] = FRONT_RIGHT
;
850 device
->DevChannels
[2] = FRONT_CENTER
;
851 device
->DevChannels
[3] = LFE
;
852 device
->DevChannels
[4] = BACK_LEFT
;
853 device
->DevChannels
[5] = BACK_RIGHT
; break;
855 case DevFmtX51Side
: device
->DevChannels
[0] = FRONT_LEFT
;
856 device
->DevChannels
[1] = FRONT_RIGHT
;
857 device
->DevChannels
[2] = FRONT_CENTER
;
858 device
->DevChannels
[3] = LFE
;
859 device
->DevChannels
[4] = SIDE_LEFT
;
860 device
->DevChannels
[5] = SIDE_RIGHT
; break;
862 case DevFmtX61
: device
->DevChannels
[0] = FRONT_LEFT
;
863 device
->DevChannels
[1] = FRONT_RIGHT
;
864 device
->DevChannels
[2] = FRONT_CENTER
;
865 device
->DevChannels
[3] = LFE
;
866 device
->DevChannels
[4] = BACK_CENTER
;
867 device
->DevChannels
[5] = SIDE_LEFT
;
868 device
->DevChannels
[6] = SIDE_RIGHT
; break;
870 case DevFmtX71
: device
->DevChannels
[0] = FRONT_LEFT
;
871 device
->DevChannels
[1] = FRONT_RIGHT
;
872 device
->DevChannels
[2] = FRONT_CENTER
;
873 device
->DevChannels
[3] = LFE
;
874 device
->DevChannels
[4] = BACK_LEFT
;
875 device
->DevChannels
[5] = BACK_RIGHT
;
876 device
->DevChannels
[6] = SIDE_LEFT
;
877 device
->DevChannels
[7] = SIDE_RIGHT
; break;
882 const ALCchar
*DevFmtTypeString(enum DevFmtType type
)
886 case DevFmtByte
: return "Signed Byte";
887 case DevFmtUByte
: return "Unsigned Byte";
888 case DevFmtShort
: return "Signed Short";
889 case DevFmtUShort
: return "Unsigned Short";
890 case DevFmtInt
: return "Signed Int";
891 case DevFmtUInt
: return "Unsigned Int";
892 case DevFmtFloat
: return "Float";
894 return "(unknown type)";
896 const ALCchar
*DevFmtChannelsString(enum DevFmtChannels chans
)
900 case DevFmtMono
: return "Mono";
901 case DevFmtStereo
: return "Stereo";
902 case DevFmtQuad
: return "Quadraphonic";
903 case DevFmtX51
: return "5.1 Surround";
904 case DevFmtX51Side
: return "5.1 Side";
905 case DevFmtX61
: return "6.1 Surround";
906 case DevFmtX71
: return "7.1 Surround";
908 return "(unknown channels)";
911 ALuint
BytesFromDevFmt(enum DevFmtType type
)
915 case DevFmtByte
: return sizeof(ALbyte
);
916 case DevFmtUByte
: return sizeof(ALubyte
);
917 case DevFmtShort
: return sizeof(ALshort
);
918 case DevFmtUShort
: return sizeof(ALushort
);
919 case DevFmtInt
: return sizeof(ALint
);
920 case DevFmtUInt
: return sizeof(ALuint
);
921 case DevFmtFloat
: return sizeof(ALfloat
);
925 ALuint
ChannelsFromDevFmt(enum DevFmtChannels chans
)
929 case DevFmtMono
: return 1;
930 case DevFmtStereo
: return 2;
931 case DevFmtQuad
: return 4;
932 case DevFmtX51
: return 6;
933 case DevFmtX51Side
: return 6;
934 case DevFmtX61
: return 7;
935 case DevFmtX71
: return 8;
939 static ALboolean
DecomposeDevFormat(ALenum format
, enum DevFmtChannels
*chans
,
940 enum DevFmtType
*type
)
942 static const struct {
944 enum DevFmtChannels channels
;
945 enum DevFmtType type
;
947 { AL_FORMAT_MONO8
, DevFmtMono
, DevFmtUByte
},
948 { AL_FORMAT_MONO16
, DevFmtMono
, DevFmtShort
},
949 { AL_FORMAT_MONO_FLOAT32
, DevFmtMono
, DevFmtFloat
},
951 { AL_FORMAT_STEREO8
, DevFmtStereo
, DevFmtUByte
},
952 { AL_FORMAT_STEREO16
, DevFmtStereo
, DevFmtShort
},
953 { AL_FORMAT_STEREO_FLOAT32
, DevFmtStereo
, DevFmtFloat
},
955 { AL_FORMAT_QUAD8
, DevFmtQuad
, DevFmtUByte
},
956 { AL_FORMAT_QUAD16
, DevFmtQuad
, DevFmtShort
},
957 { AL_FORMAT_QUAD32
, DevFmtQuad
, DevFmtFloat
},
959 { AL_FORMAT_51CHN8
, DevFmtX51
, DevFmtUByte
},
960 { AL_FORMAT_51CHN16
, DevFmtX51
, DevFmtShort
},
961 { AL_FORMAT_51CHN32
, DevFmtX51
, DevFmtFloat
},
963 { AL_FORMAT_61CHN8
, DevFmtX61
, DevFmtUByte
},
964 { AL_FORMAT_61CHN16
, DevFmtX61
, DevFmtShort
},
965 { AL_FORMAT_61CHN32
, DevFmtX61
, DevFmtFloat
},
967 { AL_FORMAT_71CHN8
, DevFmtX71
, DevFmtUByte
},
968 { AL_FORMAT_71CHN16
, DevFmtX71
, DevFmtShort
},
969 { AL_FORMAT_71CHN32
, DevFmtX71
, DevFmtFloat
},
973 for(i
= 0;i
< sizeof(list
)/sizeof(list
[0]);i
++)
975 if(list
[i
].format
== format
)
977 *chans
= list
[i
].channels
;
978 *type
= list
[i
].type
;
986 static ALCboolean
IsValidALCType(ALCenum type
)
991 case ALC_UNSIGNED_BYTE_SOFT
:
993 case ALC_UNSIGNED_SHORT_SOFT
:
995 case ALC_UNSIGNED_INT_SOFT
:
1002 static ALCboolean
IsValidALCChannels(ALCenum channels
)
1007 case ALC_STEREO_SOFT
:
1009 case ALC_5POINT1_SOFT
:
1010 case ALC_6POINT1_SOFT
:
1011 case ALC_7POINT1_SOFT
:
1020 * Stores the latest ALC Error
1022 static void alcSetError(ALCdevice
*device
, ALCenum errorCode
)
1027 /* DebugBreak() will cause an exception if there is no debugger */
1028 if(IsDebuggerPresent())
1030 #elif defined(SIGTRAP)
1036 device
->LastError
= errorCode
;
1038 g_eLastNullDeviceError
= errorCode
;
1042 /* UpdateDeviceParams
1044 * Updates device parameters according to the attribute list (caller is
1045 * responsible for holding the list lock).
1047 static ALCenum
UpdateDeviceParams(ALCdevice
*device
, const ALCint
*attrList
)
1049 ALCcontext
*context
;
1050 enum DevFmtChannels oldChans
;
1051 enum DevFmtType oldType
;
1056 if(device
->IsLoopbackDevice
&& !(attrList
&& attrList
[0]))
1058 WARN("Missing attributes for loopback device\n");
1059 return ALC_INVALID_VALUE
;
1062 // Check for attributes
1063 if(attrList
&& attrList
[0])
1069 GotAll
= GotFreq
|GotChans
|GotType
1071 ALCuint freq
, numMono
, numStereo
, numSends
;
1072 enum DevFmtChannels schans
;
1073 enum DevFmtType stype
;
1074 ALCuint attrIdx
= 0;
1077 // If a context is already running on the device, stop playback so the
1078 // device attributes can be updated
1079 if((device
->Flags
&DEVICE_RUNNING
))
1080 ALCdevice_StopPlayback(device
);
1081 device
->Flags
&= ~DEVICE_RUNNING
;
1083 freq
= device
->Frequency
;
1084 schans
= device
->FmtChans
;
1085 stype
= device
->FmtType
;
1086 numMono
= device
->NumMonoSources
;
1087 numStereo
= device
->NumStereoSources
;
1088 numSends
= device
->NumAuxSends
;
1090 while(attrList
[attrIdx
])
1092 if(attrList
[attrIdx
] == ALC_FORMAT_CHANNELS_SOFT
&&
1093 device
->IsLoopbackDevice
)
1095 ALCint val
= attrList
[attrIdx
+ 1];
1096 if(!IsValidALCChannels(val
) || !ChannelsFromDevFmt(val
))
1097 return ALC_INVALID_VALUE
;
1102 if(attrList
[attrIdx
] == ALC_FORMAT_TYPE_SOFT
&&
1103 device
->IsLoopbackDevice
)
1105 ALCint val
= attrList
[attrIdx
+ 1];
1106 if(!IsValidALCType(val
) || !BytesFromDevFmt(val
))
1107 return ALC_INVALID_VALUE
;
1112 if(attrList
[attrIdx
] == ALC_FREQUENCY
)
1114 if(device
->IsLoopbackDevice
)
1116 freq
= attrList
[attrIdx
+ 1];
1117 if(freq
< MIN_OUTPUT_RATE
)
1118 return ALC_INVALID_VALUE
;
1123 freq
= attrList
[attrIdx
+ 1];
1124 device
->Flags
|= DEVICE_FREQUENCY_REQUEST
;
1128 if(attrList
[attrIdx
] == ALC_STEREO_SOURCES
)
1130 numStereo
= attrList
[attrIdx
+ 1];
1131 if(numStereo
> device
->MaxNoOfSources
)
1132 numStereo
= device
->MaxNoOfSources
;
1134 numMono
= device
->MaxNoOfSources
- numStereo
;
1137 if(attrList
[attrIdx
] == ALC_MAX_AUXILIARY_SENDS
)
1138 numSends
= attrList
[attrIdx
+ 1];
1143 if(device
->IsLoopbackDevice
)
1145 if(gotFmt
!= GotAll
)
1147 WARN("Missing format for loopback device\n");
1148 return ALC_INVALID_VALUE
;
1153 ConfigValueUInt(NULL
, "frequency", &freq
);
1154 freq
= maxu(freq
, MIN_OUTPUT_RATE
);
1156 ConfigValueUInt(NULL
, "sends", &numSends
);
1157 numSends
= minu(MAX_SENDS
, numSends
);
1159 device
->UpdateSize
= (ALuint64
)device
->UpdateSize
* freq
/
1162 device
->Frequency
= freq
;
1163 device
->FmtChans
= schans
;
1164 device
->FmtType
= stype
;
1165 device
->NumMonoSources
= numMono
;
1166 device
->NumStereoSources
= numStereo
;
1167 device
->NumAuxSends
= numSends
;
1170 if((device
->Flags
&DEVICE_RUNNING
))
1171 return ALC_NO_ERROR
;
1175 oldFreq
= device
->Frequency
;
1176 oldChans
= device
->FmtChans
;
1177 oldType
= device
->FmtType
;
1179 TRACE("Format pre-setup: %s%s, %s%s, %uhz%s, %u update size x%d\n",
1180 DevFmtChannelsString(device
->FmtChans
),
1181 (device
->Flags
&DEVICE_CHANNELS_REQUEST
)?" (requested)":"",
1182 DevFmtTypeString(device
->FmtType
),
1183 (device
->Flags
&DEVICE_SAMPLE_TYPE_REQUEST
)?" (requested)":"",
1185 (device
->Flags
&DEVICE_FREQUENCY_REQUEST
)?" (requested)":"",
1186 device
->UpdateSize
, device
->NumUpdates
);
1187 if(ALCdevice_ResetPlayback(device
) == ALC_FALSE
)
1189 UnlockDevice(device
);
1190 return ALC_INVALID_DEVICE
;
1192 device
->Flags
|= DEVICE_RUNNING
;
1194 if(device
->FmtChans
!= oldChans
&& (device
->Flags
&DEVICE_CHANNELS_REQUEST
))
1196 ERR("Failed to set %s, got %s instead\n", DevFmtChannelsString(oldChans
),
1197 DevFmtChannelsString(device
->FmtChans
));
1198 device
->Flags
&= ~DEVICE_CHANNELS_REQUEST
;
1200 if(device
->FmtType
!= oldType
&& (device
->Flags
&DEVICE_SAMPLE_TYPE_REQUEST
))
1202 ERR("Failed to set %s, got %s instead\n", DevFmtTypeString(oldType
),
1203 DevFmtTypeString(device
->FmtType
));
1204 device
->Flags
&= ~DEVICE_SAMPLE_TYPE_REQUEST
;
1206 if(device
->Frequency
!= oldFreq
&& (device
->Flags
&DEVICE_FREQUENCY_REQUEST
))
1208 ERR("Failed to set %uhz, got %uhz instead\n", oldFreq
, device
->Frequency
);
1209 device
->Flags
&= ~DEVICE_FREQUENCY_REQUEST
;
1212 TRACE("Format post-setup: %s, %s, %uhz, %u update size x%d\n",
1213 DevFmtChannelsString(device
->FmtChans
),
1214 DevFmtTypeString(device
->FmtType
), device
->Frequency
,
1215 device
->UpdateSize
, device
->NumUpdates
);
1217 aluInitPanning(device
);
1219 for(i
= 0;i
< MAXCHANNELS
;i
++)
1221 device
->ClickRemoval
[i
] = 0.0f
;
1222 device
->PendingClicks
[i
] = 0.0f
;
1225 device
->Hrtf
= NULL
;
1226 if(!device
->IsLoopbackDevice
&& GetConfigValueBool(NULL
, "hrtf", AL_FALSE
))
1227 device
->Hrtf
= GetHrtf(device
);
1228 TRACE("HRTF %s\n", device
->Hrtf
?"enabled":"disabled");
1230 if(!device
->Hrtf
&& device
->Bs2bLevel
> 0 && device
->Bs2bLevel
<= 6)
1234 device
->Bs2b
= calloc(1, sizeof(*device
->Bs2b
));
1235 bs2b_clear(device
->Bs2b
);
1237 bs2b_set_srate(device
->Bs2b
, device
->Frequency
);
1238 bs2b_set_level(device
->Bs2b
, device
->Bs2bLevel
);
1239 TRACE("BS2B level %d\n", device
->Bs2bLevel
);
1244 device
->Bs2b
= NULL
;
1245 TRACE("BS2B disabled\n");
1248 device
->Flags
&= ~DEVICE_DUPLICATE_STEREO
;
1249 switch(device
->FmtChans
)
1259 if(GetConfigValueBool(NULL
, "stereodup", AL_TRUE
))
1260 device
->Flags
|= DEVICE_DUPLICATE_STEREO
;
1263 TRACE("Stereo duplication %s\n", (device
->Flags
&DEVICE_DUPLICATE_STEREO
)?"enabled":"disabled");
1265 oldMode
= SetMixerFPUMode();
1266 context
= device
->ContextList
;
1271 context
->UpdateSources
= AL_FALSE
;
1272 LockUIntMapRead(&context
->EffectSlotMap
);
1273 for(pos
= 0;pos
< context
->EffectSlotMap
.size
;pos
++)
1275 ALeffectslot
*slot
= context
->EffectSlotMap
.array
[pos
].value
;
1277 if(ALeffectState_DeviceUpdate(slot
->EffectState
, device
) == AL_FALSE
)
1279 UnlockUIntMapRead(&context
->EffectSlotMap
);
1280 RestoreFPUMode(oldMode
);
1281 UnlockDevice(device
);
1282 ALCdevice_StopPlayback(device
);
1283 device
->Flags
&= ~DEVICE_RUNNING
;
1284 return ALC_INVALID_DEVICE
;
1286 slot
->NeedsUpdate
= AL_FALSE
;
1287 ALeffectState_Update(slot
->EffectState
, context
, slot
);
1289 UnlockUIntMapRead(&context
->EffectSlotMap
);
1291 LockUIntMapRead(&context
->SourceMap
);
1292 for(pos
= 0;pos
< context
->SourceMap
.size
;pos
++)
1294 ALsource
*source
= context
->SourceMap
.array
[pos
].value
;
1295 ALuint s
= device
->NumAuxSends
;
1296 while(s
< MAX_SENDS
)
1298 if(source
->Send
[s
].Slot
)
1299 DecrementRef(&source
->Send
[s
].Slot
->ref
);
1300 source
->Send
[s
].Slot
= NULL
;
1301 source
->Send
[s
].WetGain
= 1.0f
;
1302 source
->Send
[s
].WetGainHF
= 1.0f
;
1305 source
->NeedsUpdate
= AL_FALSE
;
1306 ALsource_Update(source
, context
);
1308 UnlockUIntMapRead(&context
->SourceMap
);
1310 context
= context
->next
;
1312 RestoreFPUMode(oldMode
);
1313 UnlockDevice(device
);
1315 return ALC_NO_ERROR
;
1320 * Frees the device structure, and destroys any objects the app failed to
1321 * delete. Called once there's no more references on the device.
1323 static ALCvoid
FreeDevice(ALCdevice
*device
)
1325 TRACE("%p\n", device
);
1327 if(device
->DefaultSlot
)
1329 ALeffectState_Destroy(device
->DefaultSlot
->EffectState
);
1330 device
->DefaultSlot
->EffectState
= NULL
;
1333 if(device
->BufferMap
.size
> 0)
1335 WARN("(%p) Deleting %d Buffer(s)\n", device
, device
->BufferMap
.size
);
1336 ReleaseALBuffers(device
);
1338 ResetUIntMap(&device
->BufferMap
);
1340 if(device
->EffectMap
.size
> 0)
1342 WARN("(%p) Deleting %d Effect(s)\n", device
, device
->EffectMap
.size
);
1343 ReleaseALEffects(device
);
1345 ResetUIntMap(&device
->EffectMap
);
1347 if(device
->FilterMap
.size
> 0)
1349 WARN("(%p) Deleting %d Filter(s)\n", device
, device
->FilterMap
.size
);
1350 ReleaseALFilters(device
);
1352 ResetUIntMap(&device
->FilterMap
);
1355 device
->Bs2b
= NULL
;
1357 free(device
->szDeviceName
);
1358 device
->szDeviceName
= NULL
;
1360 DeleteCriticalSection(&device
->Mutex
);
1366 void ALCdevice_IncRef(ALCdevice
*device
)
1369 ref
= IncrementRef(&device
->ref
);
1370 TRACEREF("%p increasing refcount to %u\n", device
, ref
);
1373 void ALCdevice_DecRef(ALCdevice
*device
)
1376 ref
= DecrementRef(&device
->ref
);
1377 TRACEREF("%p decreasing refcount to %u\n", device
, ref
);
1378 if(ref
== 0) FreeDevice(device
);
1383 * Checks if the device handle is valid, and increments its ref count if so.
1385 static ALCdevice
*VerifyDevice(ALCdevice
*device
)
1387 ALCdevice
*tmpDevice
;
1393 tmpDevice
= DeviceList
;
1394 while(tmpDevice
&& tmpDevice
!= device
)
1395 tmpDevice
= tmpDevice
->next
;
1398 ALCdevice_IncRef(tmpDevice
);
1406 * Initializes context variables
1408 static ALvoid
InitContext(ALCcontext
*pContext
)
1412 //Initialise listener
1413 pContext
->Listener
.Gain
= 1.0f
;
1414 pContext
->Listener
.MetersPerUnit
= 1.0f
;
1415 pContext
->Listener
.Position
[0] = 0.0f
;
1416 pContext
->Listener
.Position
[1] = 0.0f
;
1417 pContext
->Listener
.Position
[2] = 0.0f
;
1418 pContext
->Listener
.Velocity
[0] = 0.0f
;
1419 pContext
->Listener
.Velocity
[1] = 0.0f
;
1420 pContext
->Listener
.Velocity
[2] = 0.0f
;
1421 pContext
->Listener
.Forward
[0] = 0.0f
;
1422 pContext
->Listener
.Forward
[1] = 0.0f
;
1423 pContext
->Listener
.Forward
[2] = -1.0f
;
1424 pContext
->Listener
.Up
[0] = 0.0f
;
1425 pContext
->Listener
.Up
[1] = 1.0f
;
1426 pContext
->Listener
.Up
[2] = 0.0f
;
1427 for(i
= 0;i
< 4;i
++)
1429 for(j
= 0;j
< 4;j
++)
1430 pContext
->Listener
.Matrix
[i
][j
] = ((i
==j
) ? 1.0f
: 0.0f
);
1434 pContext
->LastError
= AL_NO_ERROR
;
1435 pContext
->UpdateSources
= AL_FALSE
;
1436 pContext
->ActiveSourceCount
= 0;
1437 InitUIntMap(&pContext
->SourceMap
, pContext
->Device
->MaxNoOfSources
);
1438 InitUIntMap(&pContext
->EffectSlotMap
, pContext
->Device
->AuxiliaryEffectSlotMax
);
1441 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
1442 pContext
->SourceDistanceModel
= AL_FALSE
;
1443 pContext
->DopplerFactor
= 1.0f
;
1444 pContext
->DopplerVelocity
= 1.0f
;
1445 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
1446 pContext
->DeferUpdates
= AL_FALSE
;
1448 pContext
->ExtensionList
= alExtList
;
1454 * Cleans up the context, and destroys any remaining objects the app failed to
1455 * delete. Called once there's no more references on the context.
1457 static ALCvoid
FreeContext(ALCcontext
*context
)
1459 TRACE("%p\n", context
);
1461 if(context
->SourceMap
.size
> 0)
1463 ERR("(%p) Deleting %d Source(s)\n", context
, context
->SourceMap
.size
);
1464 ReleaseALSources(context
);
1466 ResetUIntMap(&context
->SourceMap
);
1468 if(context
->EffectSlotMap
.size
> 0)
1470 ERR("(%p) Deleting %d AuxiliaryEffectSlot(s)\n", context
, context
->EffectSlotMap
.size
);
1471 ReleaseALAuxiliaryEffectSlots(context
);
1473 ResetUIntMap(&context
->EffectSlotMap
);
1475 context
->ActiveSourceCount
= 0;
1476 free(context
->ActiveSources
);
1477 context
->ActiveSources
= NULL
;
1478 context
->MaxActiveSources
= 0;
1480 context
->ActiveEffectSlotCount
= 0;
1481 free(context
->ActiveEffectSlots
);
1482 context
->ActiveEffectSlots
= NULL
;
1483 context
->MaxActiveEffectSlots
= 0;
1485 ALCdevice_DecRef(context
->Device
);
1486 context
->Device
= NULL
;
1488 //Invalidate context
1489 memset(context
, 0, sizeof(ALCcontext
));
1495 * Removes the context reference from the given device and removes it from
1496 * being current on the running thread or globally.
1498 static void ReleaseContext(ALCcontext
*context
, ALCdevice
*device
)
1500 ALCcontext
*volatile*tmp_ctx
;
1502 if(pthread_getspecific(LocalContext
) == context
)
1504 WARN("%p released while current on thread\n", context
);
1505 pthread_setspecific(LocalContext
, NULL
);
1506 ALCcontext_DecRef(context
);
1509 if(CompExchangePtr((XchgPtr
*)&GlobalContext
, context
, NULL
))
1510 ALCcontext_DecRef(context
);
1513 tmp_ctx
= &device
->ContextList
;
1516 if(CompExchangePtr((XchgPtr
*)tmp_ctx
, context
, context
->next
))
1518 tmp_ctx
= &(*tmp_ctx
)->next
;
1520 UnlockDevice(device
);
1522 ALCcontext_DecRef(context
);
1525 void ALCcontext_IncRef(ALCcontext
*context
)
1528 ref
= IncrementRef(&context
->ref
);
1529 TRACEREF("%p increasing refcount to %u\n", context
, ref
);
1532 void ALCcontext_DecRef(ALCcontext
*context
)
1535 ref
= DecrementRef(&context
->ref
);
1536 TRACEREF("%p decreasing refcount to %u\n", context
, ref
);
1537 if(ref
== 0) FreeContext(context
);
1540 static void ReleaseThreadCtx(void *ptr
)
1542 WARN("%p current for thread being destroyed\n", ptr
);
1543 ALCcontext_DecRef(ptr
);
1548 * Checks that the given context is valid, and increments its reference count.
1550 static ALCcontext
*VerifyContext(ALCcontext
*context
)
1558 ALCcontext
*tmp_ctx
= dev
->ContextList
;
1561 if(tmp_ctx
== context
)
1563 ALCcontext_IncRef(tmp_ctx
);
1567 tmp_ctx
= tmp_ctx
->next
;
1579 * Returns the currently active context, and adds a reference without locking
1582 ALCcontext
*GetContextRef(void)
1584 ALCcontext
*context
;
1586 context
= pthread_getspecific(LocalContext
);
1588 ALCcontext_IncRef(context
);
1592 context
= GlobalContext
;
1594 ALCcontext_IncRef(context
);
1601 ///////////////////////////////////////////////////////
1604 ///////////////////////////////////////////////////////
1605 // ALC Functions calls
1608 // This should probably move to another c file but for now ...
1609 ALC_API ALCdevice
* ALC_APIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
1611 ALCdevice
*device
= NULL
;
1616 if(!CaptureBackend
.name
)
1618 alcSetError(NULL
, ALC_INVALID_VALUE
);
1624 alcSetError(NULL
, ALC_INVALID_VALUE
);
1628 if(deviceName
&& (!deviceName
[0] || strcasecmp(deviceName
, "openal soft") == 0 || strcasecmp(deviceName
, "openal-soft") == 0))
1631 device
= calloc(1, sizeof(ALCdevice
));
1634 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
1639 device
->Funcs
= &CaptureBackend
.Funcs
;
1641 device
->Connected
= ALC_TRUE
;
1642 device
->IsCaptureDevice
= AL_TRUE
;
1643 device
->IsLoopbackDevice
= AL_FALSE
;
1644 InitializeCriticalSection(&device
->Mutex
);
1646 InitUIntMap(&device
->BufferMap
, ~0);
1647 InitUIntMap(&device
->EffectMap
, ~0);
1648 InitUIntMap(&device
->FilterMap
, ~0);
1650 device
->szDeviceName
= NULL
;
1652 device
->Flags
|= DEVICE_FREQUENCY_REQUEST
;
1653 device
->Frequency
= frequency
;
1655 device
->Flags
|= DEVICE_CHANNELS_REQUEST
| DEVICE_SAMPLE_TYPE_REQUEST
;
1656 if(DecomposeDevFormat(format
, &device
->FmtChans
, &device
->FmtType
) == AL_FALSE
)
1658 DeleteCriticalSection(&device
->Mutex
);
1660 alcSetError(NULL
, ALC_INVALID_ENUM
);
1664 device
->UpdateSize
= SampleSize
;
1665 device
->NumUpdates
= 1;
1668 if((err
=ALCdevice_OpenCapture(device
, deviceName
)) != ALC_NO_ERROR
)
1671 DeleteCriticalSection(&device
->Mutex
);
1673 alcSetError(NULL
, err
);
1679 device
->next
= DeviceList
;
1680 } while(!CompExchangePtr((XchgPtr
*)&DeviceList
, device
->next
, device
));
1682 TRACE("Created device %p\n", device
);
1686 ALC_API ALCboolean ALC_APIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
1688 ALCdevice
*volatile*list
;
1692 while(*list
&& *list
!= pDevice
)
1693 list
= &(*list
)->next
;
1695 if(!*list
|| !(*list
)->IsCaptureDevice
)
1697 alcSetError(*list
, ALC_INVALID_DEVICE
);
1702 *list
= (*list
)->next
;
1705 LockDevice(pDevice
);
1706 ALCdevice_CloseCapture(pDevice
);
1707 UnlockDevice(pDevice
);
1709 ALCdevice_DecRef(pDevice
);
1714 ALC_API
void ALC_APIENTRY
alcCaptureStart(ALCdevice
*device
)
1716 if(!(device
=VerifyDevice(device
)) || !device
->IsCaptureDevice
)
1718 alcSetError(device
, ALC_INVALID_DEVICE
);
1719 if(device
) ALCdevice_DecRef(device
);
1723 if(device
->Connected
)
1724 ALCdevice_StartCapture(device
);
1725 UnlockDevice(device
);
1727 ALCdevice_DecRef(device
);
1730 ALC_API
void ALC_APIENTRY
alcCaptureStop(ALCdevice
*device
)
1732 if(!(device
=VerifyDevice(device
)) || !device
->IsCaptureDevice
)
1734 alcSetError(device
, ALC_INVALID_DEVICE
);
1735 if(device
) ALCdevice_DecRef(device
);
1739 if(device
->Connected
)
1740 ALCdevice_StopCapture(device
);
1741 UnlockDevice(device
);
1743 ALCdevice_DecRef(device
);
1746 ALC_API
void ALC_APIENTRY
alcCaptureSamples(ALCdevice
*device
, ALCvoid
*buffer
, ALCsizei samples
)
1748 ALCenum err
= ALC_INVALID_DEVICE
;
1749 if((device
=VerifyDevice(device
)) != NULL
&& device
->IsCaptureDevice
)
1751 err
= ALC_INVALID_VALUE
;
1753 if(samples
>= 0 && ALCdevice_AvailableSamples(device
) >= (ALCuint
)samples
)
1754 err
= ALCdevice_CaptureSamples(device
, buffer
, samples
);
1755 UnlockDevice(device
);
1757 if(err
!= ALC_NO_ERROR
)
1758 alcSetError(device
, err
);
1759 if(device
) ALCdevice_DecRef(device
);
1765 Return last ALC generated error code
1767 ALC_API ALCenum ALC_APIENTRY
alcGetError(ALCdevice
*device
)
1771 if(VerifyDevice(device
))
1773 errorCode
= ExchangeInt(&device
->LastError
, ALC_NO_ERROR
);
1774 ALCdevice_DecRef(device
);
1777 errorCode
= ExchangeInt(&g_eLastNullDeviceError
, ALC_NO_ERROR
);
1783 /* alcSuspendContext
1787 ALC_API ALCvoid ALC_APIENTRY
alcSuspendContext(ALCcontext
*Context
)
1792 /* alcProcessContext
1796 ALC_API ALCvoid ALC_APIENTRY
alcProcessContext(ALCcontext
*Context
)
1804 * Returns information about the Device, and error strings
1806 ALC_API
const ALCchar
* ALC_APIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
1808 const ALCchar
*value
= NULL
;
1816 case ALC_INVALID_ENUM
:
1817 value
= alcErrInvalidEnum
;
1820 case ALC_INVALID_VALUE
:
1821 value
= alcErrInvalidValue
;
1824 case ALC_INVALID_DEVICE
:
1825 value
= alcErrInvalidDevice
;
1828 case ALC_INVALID_CONTEXT
:
1829 value
= alcErrInvalidContext
;
1832 case ALC_OUT_OF_MEMORY
:
1833 value
= alcErrOutOfMemory
;
1836 case ALC_DEVICE_SPECIFIER
:
1837 if(VerifyDevice(pDevice
))
1839 value
= pDevice
->szDeviceName
;
1840 ALCdevice_DecRef(pDevice
);
1845 value
= alcDeviceList
;
1849 case ALC_ALL_DEVICES_SPECIFIER
:
1850 ProbeAllDeviceList();
1851 value
= alcAllDeviceList
;
1854 case ALC_CAPTURE_DEVICE_SPECIFIER
:
1855 if(VerifyDevice(pDevice
))
1857 value
= pDevice
->szDeviceName
;
1858 ALCdevice_DecRef(pDevice
);
1862 ProbeCaptureDeviceList();
1863 value
= alcCaptureDeviceList
;
1867 /* Default devices are always first in the list */
1868 case ALC_DEFAULT_DEVICE_SPECIFIER
:
1872 pDevice
= VerifyDevice(pDevice
);
1874 free(alcDefaultDeviceSpecifier
);
1875 alcDefaultDeviceSpecifier
= strdup(alcDeviceList
? alcDeviceList
: "");
1876 if(!alcDefaultDeviceSpecifier
)
1877 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1879 value
= alcDefaultDeviceSpecifier
;
1880 if(pDevice
) ALCdevice_DecRef(pDevice
);
1883 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
1884 if(!alcAllDeviceList
)
1885 ProbeAllDeviceList();
1887 pDevice
= VerifyDevice(pDevice
);
1889 free(alcDefaultAllDeviceSpecifier
);
1890 alcDefaultAllDeviceSpecifier
= strdup(alcAllDeviceList
?
1891 alcAllDeviceList
: "");
1892 if(!alcDefaultAllDeviceSpecifier
)
1893 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1895 value
= alcDefaultAllDeviceSpecifier
;
1896 if(pDevice
) ALCdevice_DecRef(pDevice
);
1899 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
1900 if(!alcCaptureDeviceList
)
1901 ProbeCaptureDeviceList();
1903 pDevice
= VerifyDevice(pDevice
);
1905 free(alcCaptureDefaultDeviceSpecifier
);
1906 alcCaptureDefaultDeviceSpecifier
= strdup(alcCaptureDeviceList
?
1907 alcCaptureDeviceList
: "");
1908 if(!alcCaptureDefaultDeviceSpecifier
)
1909 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1911 value
= alcCaptureDefaultDeviceSpecifier
;
1912 if(pDevice
) ALCdevice_DecRef(pDevice
);
1915 case ALC_EXTENSIONS
:
1916 if(!VerifyDevice(pDevice
))
1917 value
= alcNoDeviceExtList
;
1920 value
= alcExtensionList
;
1921 ALCdevice_DecRef(pDevice
);
1926 pDevice
= VerifyDevice(pDevice
);
1927 alcSetError(pDevice
, ALC_INVALID_ENUM
);
1928 if(pDevice
) ALCdevice_DecRef(pDevice
);
1938 * Returns information about the Device and the version of Open AL
1940 ALC_API ALCvoid ALC_APIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
1942 device
= VerifyDevice(device
);
1944 if(size
== 0 || data
== NULL
)
1946 alcSetError(device
, ALC_INVALID_VALUE
);
1947 if(device
) ALCdevice_DecRef(device
);
1955 case ALC_MAJOR_VERSION
:
1956 *data
= alcMajorVersion
;
1958 case ALC_MINOR_VERSION
:
1959 *data
= alcMinorVersion
;
1962 case ALC_ATTRIBUTES_SIZE
:
1963 case ALC_ALL_ATTRIBUTES
:
1967 case ALC_MONO_SOURCES
:
1968 case ALC_STEREO_SOURCES
:
1969 case ALC_CAPTURE_SAMPLES
:
1970 case ALC_FORMAT_CHANNELS_SOFT
:
1971 case ALC_FORMAT_TYPE_SOFT
:
1972 alcSetError(NULL
, ALC_INVALID_DEVICE
);
1976 alcSetError(NULL
, ALC_INVALID_ENUM
);
1980 else if(device
->IsCaptureDevice
)
1984 case ALC_CAPTURE_SAMPLES
:
1986 *data
= ALCdevice_AvailableSamples(device
);
1987 UnlockDevice(device
);
1991 *data
= device
->Connected
;
1995 alcSetError(device
, ALC_INVALID_ENUM
);
1999 else /* render device */
2003 case ALC_MAJOR_VERSION
:
2004 *data
= alcMajorVersion
;
2007 case ALC_MINOR_VERSION
:
2008 *data
= alcMinorVersion
;
2011 case ALC_EFX_MAJOR_VERSION
:
2012 *data
= alcEFXMajorVersion
;
2015 case ALC_EFX_MINOR_VERSION
:
2016 *data
= alcEFXMinorVersion
;
2019 case ALC_ATTRIBUTES_SIZE
:
2023 case ALC_ALL_ATTRIBUTES
:
2025 alcSetError(device
, ALC_INVALID_VALUE
);
2030 data
[i
++] = ALC_FREQUENCY
;
2031 data
[i
++] = device
->Frequency
;
2033 if(!device
->IsLoopbackDevice
)
2035 data
[i
++] = ALC_REFRESH
;
2036 data
[i
++] = device
->Frequency
/ device
->UpdateSize
;
2038 data
[i
++] = ALC_SYNC
;
2039 data
[i
++] = ALC_FALSE
;
2043 data
[i
++] = ALC_FORMAT_CHANNELS_SOFT
;
2044 data
[i
++] = device
->FmtChans
;
2046 data
[i
++] = ALC_FORMAT_TYPE_SOFT
;
2047 data
[i
++] = device
->FmtType
;
2050 data
[i
++] = ALC_MONO_SOURCES
;
2051 data
[i
++] = device
->NumMonoSources
;
2053 data
[i
++] = ALC_STEREO_SOURCES
;
2054 data
[i
++] = device
->NumStereoSources
;
2056 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
2057 data
[i
++] = device
->NumAuxSends
;
2064 *data
= device
->Frequency
;
2068 if(device
->IsLoopbackDevice
)
2069 alcSetError(device
, ALC_INVALID_DEVICE
);
2071 *data
= device
->Frequency
/ device
->UpdateSize
;
2075 if(device
->IsLoopbackDevice
)
2076 alcSetError(device
, ALC_INVALID_DEVICE
);
2081 case ALC_FORMAT_CHANNELS_SOFT
:
2082 if(!device
->IsLoopbackDevice
)
2083 alcSetError(device
, ALC_INVALID_DEVICE
);
2085 *data
= device
->FmtChans
;
2088 case ALC_FORMAT_TYPE_SOFT
:
2089 if(!device
->IsLoopbackDevice
)
2090 alcSetError(device
, ALC_INVALID_DEVICE
);
2092 *data
= device
->FmtType
;
2095 case ALC_MONO_SOURCES
:
2096 *data
= device
->NumMonoSources
;
2099 case ALC_STEREO_SOURCES
:
2100 *data
= device
->NumStereoSources
;
2103 case ALC_MAX_AUXILIARY_SENDS
:
2104 *data
= device
->NumAuxSends
;
2108 *data
= device
->Connected
;
2112 alcSetError(device
, ALC_INVALID_ENUM
);
2117 ALCdevice_DecRef(device
);
2121 /* alcIsExtensionPresent
2123 * Determines if there is support for a particular extension
2125 ALC_API ALCboolean ALC_APIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
2127 ALCboolean bResult
= ALC_FALSE
;
2129 device
= VerifyDevice(device
);
2132 alcSetError(device
, ALC_INVALID_VALUE
);
2135 size_t len
= strlen(extName
);
2136 const char *ptr
= (device
? alcExtensionList
: alcNoDeviceExtList
);
2139 if(strncasecmp(ptr
, extName
, len
) == 0 &&
2140 (ptr
[len
] == '\0' || isspace(ptr
[len
])))
2145 if((ptr
=strchr(ptr
, ' ')) != NULL
)
2149 } while(isspace(*ptr
));
2154 ALCdevice_DecRef(device
);
2159 /* alcGetProcAddress
2161 * Retrieves the function address for a particular extension function
2163 ALC_API ALCvoid
* ALC_APIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
2165 ALCvoid
*ptr
= NULL
;
2167 device
= VerifyDevice(device
);
2170 alcSetError(device
, ALC_INVALID_VALUE
);
2174 while(alcFunctions
[i
].funcName
&& strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
2176 ptr
= alcFunctions
[i
].address
;
2179 ALCdevice_DecRef(device
);
2186 * Get the value for a particular ALC Enumerated Value
2188 ALC_API ALCenum ALC_APIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
2192 device
= VerifyDevice(device
);
2195 alcSetError(device
, ALC_INVALID_VALUE
);
2199 while(enumeration
[i
].enumName
&& strcmp(enumeration
[i
].enumName
,enumName
) != 0)
2201 val
= enumeration
[i
].value
;
2204 ALCdevice_DecRef(device
);
2211 * Create and attach a Context to a particular Device.
2213 ALC_API ALCcontext
* ALC_APIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
2215 ALCcontext
*ALContext
;
2219 if(!(device
=VerifyDevice(device
)) || device
->IsCaptureDevice
|| !device
->Connected
)
2222 alcSetError(device
, ALC_INVALID_DEVICE
);
2223 if(device
) ALCdevice_DecRef(device
);
2227 /* Reset Context Last Error code */
2228 device
->LastError
= ALC_NO_ERROR
;
2230 if((err
=UpdateDeviceParams(device
, attrList
)) != ALC_NO_ERROR
)
2233 alcSetError(device
, err
);
2234 if(err
== ALC_INVALID_DEVICE
)
2235 aluHandleDisconnect(device
);
2236 ALCdevice_DecRef(device
);
2240 ALContext
= calloc(1, sizeof(ALCcontext
));
2245 ALContext
->MaxActiveSources
= 256;
2246 ALContext
->ActiveSources
= malloc(sizeof(ALContext
->ActiveSources
[0]) *
2247 ALContext
->MaxActiveSources
);
2249 if(!ALContext
|| !ALContext
->ActiveSources
)
2251 if(!device
->ContextList
)
2253 ALCdevice_StopPlayback(device
);
2254 device
->Flags
&= ~DEVICE_RUNNING
;
2261 alcSetError(device
, ALC_OUT_OF_MEMORY
);
2262 ALCdevice_DecRef(device
);
2266 ALContext
->Device
= device
;
2267 ALCdevice_IncRef(device
);
2268 InitContext(ALContext
);
2271 ALContext
->next
= device
->ContextList
;
2272 } while(!CompExchangePtr((XchgPtr
*)&device
->ContextList
, ALContext
->next
, ALContext
));
2275 if(device
->DefaultSlot
)
2276 InitializeEffect(ALContext
, device
->DefaultSlot
, &ForcedEffect
);
2277 ALContext
->LastError
= AL_NO_ERROR
;
2279 ALCdevice_DecRef(device
);
2281 TRACE("Created context %p\n", ALContext
);
2285 /* alcDestroyContext
2289 ALC_API ALCvoid ALC_APIENTRY
alcDestroyContext(ALCcontext
*context
)
2294 /* alcGetContextsDevice sets an error for invalid contexts */
2295 Device
= alcGetContextsDevice(context
);
2298 ReleaseContext(context
, Device
);
2299 if(!Device
->ContextList
)
2301 ALCdevice_StopPlayback(Device
);
2302 Device
->Flags
&= ~DEVICE_RUNNING
;
2309 /* alcGetCurrentContext
2311 * Returns the currently active Context
2313 ALC_API ALCcontext
* ALC_APIENTRY
alcGetCurrentContext(ALCvoid
)
2315 ALCcontext
*Context
;
2317 Context
= pthread_getspecific(LocalContext
);
2318 if(!Context
) Context
= GlobalContext
;
2323 /* alcGetThreadContext
2325 * Returns the currently active thread-local Context
2327 ALC_API ALCcontext
* ALC_APIENTRY
alcGetThreadContext(void)
2329 ALCcontext
*Context
;
2330 Context
= pthread_getspecific(LocalContext
);
2335 /* alcMakeContextCurrent
2337 * Makes the given Context the active Context
2339 ALC_API ALCboolean ALC_APIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
2341 /* context must be a valid Context or NULL */
2342 if(context
&& !(context
=VerifyContext(context
)))
2344 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2347 /* context's reference count is already incremented */
2348 context
= ExchangePtr((XchgPtr
*)&GlobalContext
, context
);
2349 if(context
) ALCcontext_DecRef(context
);
2351 if((context
=pthread_getspecific(LocalContext
)) != NULL
)
2353 pthread_setspecific(LocalContext
, NULL
);
2354 ALCcontext_DecRef(context
);
2360 /* alcSetThreadContext
2362 * Makes the given Context the active Context for the current thread
2364 ALC_API ALCboolean ALC_APIENTRY
alcSetThreadContext(ALCcontext
*context
)
2368 /* context must be a valid Context or NULL */
2369 if(context
&& !(context
=VerifyContext(context
)))
2371 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2374 /* context's reference count is already incremented */
2375 old
= pthread_getspecific(LocalContext
);
2376 pthread_setspecific(LocalContext
, context
);
2377 if(old
) ALCcontext_DecRef(old
);
2383 /* alcGetContextsDevice
2385 * Returns the Device that a particular Context is attached to
2387 ALC_API ALCdevice
* ALC_APIENTRY
alcGetContextsDevice(ALCcontext
*Context
)
2391 if(!(Context
=VerifyContext(Context
)))
2393 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2396 Device
= Context
->Device
;
2397 ALCcontext_DecRef(Context
);
2405 * Open the Device specified.
2407 ALC_API ALCdevice
* ALC_APIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
2415 if(!PlaybackBackend
.name
)
2417 alcSetError(NULL
, ALC_INVALID_VALUE
);
2421 if(deviceName
&& (!deviceName
[0] || strcasecmp(deviceName
, "openal soft") == 0 || strcasecmp(deviceName
, "openal-soft") == 0))
2424 device
= calloc(1, sizeof(ALCdevice
)+sizeof(ALeffectslot
));
2427 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
2432 device
->Funcs
= &PlaybackBackend
.Funcs
;
2434 device
->Connected
= ALC_TRUE
;
2435 device
->IsCaptureDevice
= AL_FALSE
;
2436 device
->IsLoopbackDevice
= AL_FALSE
;
2437 InitializeCriticalSection(&device
->Mutex
);
2438 device
->LastError
= ALC_NO_ERROR
;
2441 device
->Bs2b
= NULL
;
2442 device
->Bs2bLevel
= 0;
2443 device
->szDeviceName
= NULL
;
2445 device
->ContextList
= NULL
;
2447 device
->MaxNoOfSources
= 256;
2448 device
->AuxiliaryEffectSlotMax
= 4;
2449 device
->NumAuxSends
= MAX_SENDS
;
2451 InitUIntMap(&device
->BufferMap
, ~0);
2452 InitUIntMap(&device
->EffectMap
, ~0);
2453 InitUIntMap(&device
->FilterMap
, ~0);
2456 device
->FmtChans
= DevFmtStereo
;
2457 device
->FmtType
= DevFmtFloat
;
2458 device
->Frequency
= DEFAULT_OUTPUT_RATE
;
2459 device
->NumUpdates
= 4;
2460 device
->UpdateSize
= 1024;
2462 if(ConfigValueStr(NULL
, "channels", &fmt
))
2464 static const struct {
2465 const char name
[16];
2466 enum DevFmtChannels chans
;
2468 { "mono", DevFmtMono
},
2469 { "stereo", DevFmtStereo
},
2470 { "quad", DevFmtQuad
},
2471 { "surround51", DevFmtX51
},
2472 { "surround61", DevFmtX61
},
2473 { "surround71", DevFmtX71
},
2477 for(i
= 0;i
< sizeof(chanlist
)/sizeof(chanlist
[0]);i
++)
2479 if(strcasecmp(chanlist
[i
].name
, fmt
) == 0)
2481 device
->FmtChans
= chanlist
[i
].chans
;
2482 device
->Flags
|= DEVICE_CHANNELS_REQUEST
;
2486 if(i
== sizeof(chanlist
)/sizeof(chanlist
[0]))
2487 ERR("Unsupported channels: %s\n", fmt
);
2489 if(ConfigValueStr(NULL
, "sample-type", &fmt
))
2491 static const struct {
2492 const char name
[16];
2493 enum DevFmtType type
;
2495 { "int8", DevFmtByte
},
2496 { "uint8", DevFmtUByte
},
2497 { "int16", DevFmtShort
},
2498 { "uint16", DevFmtUShort
},
2499 { "int32", DevFmtInt
},
2500 { "uint32", DevFmtUInt
},
2501 { "float32", DevFmtFloat
},
2505 for(i
= 0;i
< sizeof(typelist
)/sizeof(typelist
[0]);i
++)
2507 if(strcasecmp(typelist
[i
].name
, fmt
) == 0)
2509 device
->FmtType
= typelist
[i
].type
;
2510 device
->Flags
|= DEVICE_SAMPLE_TYPE_REQUEST
;
2514 if(i
== sizeof(typelist
)/sizeof(typelist
[0]))
2515 ERR("Unsupported sample-type: %s\n", fmt
);
2517 #define DEVICE_FORMAT_REQUEST (DEVICE_CHANNELS_REQUEST|DEVICE_SAMPLE_TYPE_REQUEST)
2518 if((device
->Flags
&DEVICE_FORMAT_REQUEST
) != DEVICE_FORMAT_REQUEST
&&
2519 ConfigValueStr(NULL
, "format", &fmt
))
2521 static const struct {
2522 const char name
[32];
2523 enum DevFmtChannels channels
;
2524 enum DevFmtType type
;
2526 { "AL_FORMAT_MONO32", DevFmtMono
, DevFmtFloat
},
2527 { "AL_FORMAT_STEREO32", DevFmtStereo
, DevFmtFloat
},
2528 { "AL_FORMAT_QUAD32", DevFmtQuad
, DevFmtFloat
},
2529 { "AL_FORMAT_51CHN32", DevFmtX51
, DevFmtFloat
},
2530 { "AL_FORMAT_61CHN32", DevFmtX61
, DevFmtFloat
},
2531 { "AL_FORMAT_71CHN32", DevFmtX71
, DevFmtFloat
},
2533 { "AL_FORMAT_MONO16", DevFmtMono
, DevFmtShort
},
2534 { "AL_FORMAT_STEREO16", DevFmtStereo
, DevFmtShort
},
2535 { "AL_FORMAT_QUAD16", DevFmtQuad
, DevFmtShort
},
2536 { "AL_FORMAT_51CHN16", DevFmtX51
, DevFmtShort
},
2537 { "AL_FORMAT_61CHN16", DevFmtX61
, DevFmtShort
},
2538 { "AL_FORMAT_71CHN16", DevFmtX71
, DevFmtShort
},
2540 { "AL_FORMAT_MONO8", DevFmtMono
, DevFmtByte
},
2541 { "AL_FORMAT_STEREO8", DevFmtStereo
, DevFmtByte
},
2542 { "AL_FORMAT_QUAD8", DevFmtQuad
, DevFmtByte
},
2543 { "AL_FORMAT_51CHN8", DevFmtX51
, DevFmtByte
},
2544 { "AL_FORMAT_61CHN8", DevFmtX61
, DevFmtByte
},
2545 { "AL_FORMAT_71CHN8", DevFmtX71
, DevFmtByte
}
2549 ERR("Option 'format' is deprecated, please use 'channels' and 'sample-type'\n");
2550 for(i
= 0;i
< sizeof(formats
)/sizeof(formats
[0]);i
++)
2552 if(strcasecmp(fmt
, formats
[i
].name
) == 0)
2554 if(!(device
->Flags
&DEVICE_CHANNELS_REQUEST
))
2555 device
->FmtChans
= formats
[i
].channels
;
2556 if(!(device
->Flags
&DEVICE_SAMPLE_TYPE_REQUEST
))
2557 device
->FmtType
= formats
[i
].type
;
2558 device
->Flags
|= DEVICE_FORMAT_REQUEST
;
2562 if(i
== sizeof(formats
)/sizeof(formats
[0]))
2563 ERR("Unsupported format: %s\n", fmt
);
2565 #undef DEVICE_FORMAT_REQUEST
2567 if(ConfigValueUInt(NULL
, "frequency", &device
->Frequency
))
2569 device
->Flags
|= DEVICE_FREQUENCY_REQUEST
;
2570 if(device
->Frequency
< MIN_OUTPUT_RATE
)
2571 ERR("%uhz request clamped to %uhz minimum\n", device
->Frequency
, MIN_OUTPUT_RATE
);
2572 device
->Frequency
= maxu(device
->Frequency
, MIN_OUTPUT_RATE
);
2575 ConfigValueUInt(NULL
, "periods", &device
->NumUpdates
);
2576 if(device
->NumUpdates
< 2) device
->NumUpdates
= 4;
2578 ConfigValueUInt(NULL
, "period_size", &device
->UpdateSize
);
2579 if(device
->UpdateSize
== 0) device
->UpdateSize
= 1024;
2581 ConfigValueUInt(NULL
, "sources", &device
->MaxNoOfSources
);
2582 if(device
->MaxNoOfSources
== 0) device
->MaxNoOfSources
= 256;
2584 ConfigValueUInt(NULL
, "slots", &device
->AuxiliaryEffectSlotMax
);
2585 if(device
->AuxiliaryEffectSlotMax
== 0) device
->AuxiliaryEffectSlotMax
= 4;
2587 ConfigValueUInt(NULL
, "sends", &device
->NumAuxSends
);
2588 if(device
->NumAuxSends
> MAX_SENDS
) device
->NumAuxSends
= MAX_SENDS
;
2590 ConfigValueInt(NULL
, "cf_level", &device
->Bs2bLevel
);
2592 device
->NumStereoSources
= 1;
2593 device
->NumMonoSources
= device
->MaxNoOfSources
- device
->NumStereoSources
;
2595 if(ForcedEffect
.type
!= AL_EFFECT_NULL
)
2597 device
->DefaultSlot
= (ALeffectslot
*)(device
+1);
2598 if(InitEffectSlot(device
->DefaultSlot
) != AL_NO_ERROR
)
2599 device
->DefaultSlot
= NULL
;
2602 // Find a playback device to open
2604 if((err
=ALCdevice_OpenPlayback(device
, deviceName
)) != ALC_NO_ERROR
)
2607 DeleteCriticalSection(&device
->Mutex
);
2609 alcSetError(NULL
, err
);
2615 device
->next
= DeviceList
;
2616 } while(!CompExchangePtr((XchgPtr
*)&DeviceList
, device
->next
, device
));
2618 TRACE("Created device %p\n", device
);
2624 * Close the specified Device
2626 ALC_API ALCboolean ALC_APIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
2628 ALCdevice
*volatile*list
;
2633 while(*list
&& *list
!= pDevice
)
2634 list
= &(*list
)->next
;
2636 if(!*list
|| (*list
)->IsCaptureDevice
)
2638 alcSetError(*list
, ALC_INVALID_DEVICE
);
2643 *list
= (*list
)->next
;
2646 if((ctx
=pDevice
->ContextList
) != NULL
)
2649 WARN("Releasing context %p\n", ctx
);
2650 ReleaseContext(ctx
, pDevice
);
2651 } while((ctx
=pDevice
->ContextList
) != NULL
);
2652 ALCdevice_StopPlayback(pDevice
);
2653 pDevice
->Flags
&= ~DEVICE_RUNNING
;
2655 ALCdevice_ClosePlayback(pDevice
);
2657 ALCdevice_DecRef(pDevice
);
2663 /* alcLoopbackOpenDeviceSOFT
2665 * Open a loopback device, for manual rendering.
2667 ALC_API ALCdevice
* ALC_APIENTRY
alcLoopbackOpenDeviceSOFT(ALCdevice
*device
)
2671 /* Make sure the device, if specified, belongs to us. */
2672 if(device
&& !(device
=VerifyDevice(device
)))
2674 alcSetError(device
, ALC_INVALID_DEVICE
);
2677 if(device
) ALCdevice_DecRef(device
);
2679 device
= calloc(1, sizeof(ALCdevice
));
2682 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
2687 device
->Funcs
= &BackendLoopback
.Funcs
;
2689 device
->Connected
= ALC_TRUE
;
2690 device
->IsCaptureDevice
= AL_FALSE
;
2691 device
->IsLoopbackDevice
= AL_TRUE
;
2692 InitializeCriticalSection(&device
->Mutex
);
2693 device
->LastError
= ALC_NO_ERROR
;
2696 device
->Bs2b
= NULL
;
2697 device
->Bs2bLevel
= 0;
2698 device
->szDeviceName
= NULL
;
2700 device
->ContextList
= NULL
;
2702 device
->MaxNoOfSources
= 256;
2703 device
->AuxiliaryEffectSlotMax
= 4;
2704 device
->NumAuxSends
= MAX_SENDS
;
2706 InitUIntMap(&device
->BufferMap
, ~0);
2707 InitUIntMap(&device
->EffectMap
, ~0);
2708 InitUIntMap(&device
->FilterMap
, ~0);
2711 device
->NumUpdates
= 0;
2712 device
->UpdateSize
= 0;
2714 device
->Frequency
= 44100;
2715 device
->FmtChans
= DevFmtStereo
;
2716 device
->FmtType
= DevFmtShort
;
2718 ConfigValueUInt(NULL
, "sources", &device
->MaxNoOfSources
);
2719 if(device
->MaxNoOfSources
== 0) device
->MaxNoOfSources
= 256;
2721 ConfigValueUInt(NULL
, "slots", &device
->AuxiliaryEffectSlotMax
);
2722 if(device
->AuxiliaryEffectSlotMax
== 0) device
->AuxiliaryEffectSlotMax
= 4;
2724 ConfigValueUInt(NULL
, "sends", &device
->NumAuxSends
);
2725 if(device
->NumAuxSends
> MAX_SENDS
) device
->NumAuxSends
= MAX_SENDS
;
2727 device
->NumStereoSources
= 1;
2728 device
->NumMonoSources
= device
->MaxNoOfSources
- device
->NumStereoSources
;
2730 // Open the "backend"
2731 ALCdevice_OpenPlayback(device
, "Loopback");
2733 device
->next
= DeviceList
;
2734 } while(!CompExchangePtr((XchgPtr
*)&DeviceList
, device
->next
, device
));
2736 TRACE("Created device %p\n", device
);
2740 /* alcIsRenderFormatSupportedSOFT
2742 * Determines if the loopback device supports the given format for rendering.
2744 ALC_API ALCboolean ALC_APIENTRY
alcIsRenderFormatSupportedSOFT(ALCdevice
*device
, ALCsizei freq
, ALCenum channels
, ALCenum type
)
2746 ALCboolean ret
= ALC_FALSE
;
2748 if(!(device
=VerifyDevice(device
)) || !device
->IsLoopbackDevice
)
2749 alcSetError(device
, ALC_INVALID_DEVICE
);
2751 alcSetError(device
, ALC_INVALID_VALUE
);
2752 else if(!IsValidALCType(type
) || !IsValidALCChannels(channels
))
2753 alcSetError(device
, ALC_INVALID_ENUM
);
2756 if(BytesFromDevFmt(type
) > 0 && ChannelsFromDevFmt(channels
) > 0 &&
2757 freq
>= MIN_OUTPUT_RATE
)
2760 if(device
) ALCdevice_DecRef(device
);
2765 /* alcRenderSamplesSOFT
2767 * Renders some samples into a buffer, using the format last set by the
2768 * attributes given to alcCreateContext.
2770 ALC_API
void ALC_APIENTRY
alcRenderSamplesSOFT(ALCdevice
*device
, ALCvoid
*buffer
, ALCsizei samples
)
2772 if(!(device
=VerifyDevice(device
)) || !device
->IsLoopbackDevice
)
2773 alcSetError(device
, ALC_INVALID_DEVICE
);
2774 else if(samples
< 0 || (samples
> 0 && buffer
== NULL
))
2775 alcSetError(device
, ALC_INVALID_VALUE
);
2777 aluMixData(device
, buffer
, samples
);
2778 if(device
) ALCdevice_DecRef(device
);
2782 static void ReleaseALC(void)
2786 free(alcDeviceList
); alcDeviceList
= NULL
;
2787 alcDeviceListSize
= 0;
2788 free(alcAllDeviceList
); alcAllDeviceList
= NULL
;
2789 alcAllDeviceListSize
= 0;
2790 free(alcCaptureDeviceList
); alcCaptureDeviceList
= NULL
;
2791 alcCaptureDeviceListSize
= 0;
2793 free(alcDefaultDeviceSpecifier
);
2794 alcDefaultDeviceSpecifier
= NULL
;
2795 free(alcDefaultAllDeviceSpecifier
);
2796 alcDefaultAllDeviceSpecifier
= NULL
;
2797 free(alcCaptureDefaultDeviceSpecifier
);
2798 alcCaptureDefaultDeviceSpecifier
= NULL
;
2800 if((dev
=ExchangePtr((XchgPtr
*)&DeviceList
, NULL
)) != NULL
)
2805 } while((dev
=dev
->next
) != NULL
);
2806 ERR("%u device%s not closed\n", num
, (num
>1)?"s":"");
2810 ///////////////////////////////////////////////////////