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, 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 const ALCchar alcDefaultName
[] = "OpenAL Soft\0";
331 static ALCchar
*alcAllDeviceList
;
332 static size_t alcAllDeviceListSize
;
333 static ALCchar
*alcCaptureDeviceList
;
334 static size_t alcCaptureDeviceListSize
;
335 /* Default is always the first in the list */
336 static ALCchar
*alcDefaultAllDeviceSpecifier
;
337 static ALCchar
*alcCaptureDefaultDeviceSpecifier
;
340 static const ALCchar alcNoDeviceExtList
[] =
341 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
342 "ALC_EXT_thread_local_context ALC_SOFTX_loopback_device";
343 static const ALCchar alcExtensionList
[] =
344 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
345 "ALC_EXT_DEDICATED ALC_EXT_disconnect ALC_EXT_EFX "
346 "ALC_EXT_thread_local_context ALC_SOFTX_loopback_device";
347 static const ALCint alcMajorVersion
= 1;
348 static const ALCint alcMinorVersion
= 1;
350 static const ALCint alcEFXMajorVersion
= 1;
351 static const ALCint alcEFXMinorVersion
= 0;
353 ///////////////////////////////////////////////////////
356 ///////////////////////////////////////////////////////
359 static CRITICAL_SECTION ListLock
;
362 static ALCdevice
*volatile DeviceList
= NULL
;
364 // Thread-local current context
365 static pthread_key_t LocalContext
;
366 // Process-wide current context
367 static ALCcontext
*GlobalContext
;
370 static volatile ALCenum g_eLastNullDeviceError
= ALC_NO_ERROR
;
372 // Default context extensions
373 static const ALchar alExtList
[] =
374 "AL_EXT_ALAW AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 "
375 "AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_MULAW "
376 "AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET AL_EXT_source_distance_model "
377 "AL_LOKI_quadriphonic AL_SOFT_buffer_samples AL_SOFT_buffer_sub_data "
378 "AL_SOFTX_deferred_updates AL_SOFT_direct_channels AL_SOFT_loop_points";
380 // Mixing Priority Level
388 enum LogLevel LogLevel
= LogWarning
;
390 enum LogLevel LogLevel
= LogError
;
393 /* Flag to trap ALC device errors */
394 static ALCboolean TrapALCError
= ALC_FALSE
;
396 /* One-time configuration init control */
397 static pthread_once_t alc_config_once
= PTHREAD_ONCE_INIT
;
399 /* Forced effect that applies to sources that don't have an effect on send 0 */
400 static ALeffect ForcedEffect
;
402 ///////////////////////////////////////////////////////
405 ///////////////////////////////////////////////////////
406 // ALC Related helper functions
407 static void ReleaseALC(void);
408 static void ReleaseThreadCtx(void *ptr
);
410 static void alc_initconfig(void);
411 #define DO_INITCONFIG() pthread_once(&alc_config_once, alc_initconfig)
414 static void alc_init(void);
415 static void alc_deinit(void);
416 static void alc_deinit_safe(void);
418 UIntMap TlsDestructor
;
420 #ifndef AL_LIBTYPE_STATIC
421 BOOL APIENTRY
DllMain(HINSTANCE hModule
,DWORD ul_reason_for_call
,LPVOID lpReserved
)
425 // Perform actions based on the reason for calling.
426 switch(ul_reason_for_call
)
428 case DLL_PROCESS_ATTACH
:
429 /* Pin the DLL so we won't get unloaded until the process terminates */
430 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN
| GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
,
431 (WCHAR
*)hModule
, &hModule
);
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
== ALL_DEVICE_PROBE
&& PlaybackBackend
.Probe
)
755 PlaybackBackend
.Probe(type
);
756 else if(type
== CAPTURE_DEVICE_PROBE
&& CaptureBackend
.Probe
)
757 CaptureBackend
.Probe(type
);
761 static void ProbeAllDeviceList(void)
762 { ProbeList(&alcAllDeviceList
, &alcAllDeviceListSize
, ALL_DEVICE_PROBE
); }
763 static void ProbeCaptureDeviceList(void)
764 { ProbeList(&alcCaptureDeviceList
, &alcCaptureDeviceListSize
, CAPTURE_DEVICE_PROBE
); }
767 static void AppendList(const ALCchar
*name
, ALCchar
**List
, size_t *ListSize
)
769 size_t len
= strlen(name
);
775 temp
= realloc(*List
, (*ListSize
) + len
+ 2);
778 ERR("Realloc failed to add %s!\n", name
);
783 memcpy((*List
)+(*ListSize
), name
, len
+1);
785 (*List
)[*ListSize
] = 0;
788 #define DECL_APPEND_LIST_FUNC(type) \
789 void Append##type##List(const ALCchar *name) \
790 { AppendList(name, &alc##type##List, &alc##type##ListSize); }
792 DECL_APPEND_LIST_FUNC(AllDevice
)
793 DECL_APPEND_LIST_FUNC(CaptureDevice
)
795 #undef DECL_APPEND_LIST_FUNC
798 /* Sets the default channel order used by most non-WaveFormatEx-based APIs */
799 void SetDefaultChannelOrder(ALCdevice
*device
)
801 switch(device
->FmtChans
)
803 case DevFmtX51
: device
->DevChannels
[0] = FRONT_LEFT
;
804 device
->DevChannels
[1] = FRONT_RIGHT
;
805 device
->DevChannels
[2] = BACK_LEFT
;
806 device
->DevChannels
[3] = BACK_RIGHT
;
807 device
->DevChannels
[4] = FRONT_CENTER
;
808 device
->DevChannels
[5] = LFE
;
811 case DevFmtX71
: device
->DevChannels
[0] = FRONT_LEFT
;
812 device
->DevChannels
[1] = FRONT_RIGHT
;
813 device
->DevChannels
[2] = BACK_LEFT
;
814 device
->DevChannels
[3] = BACK_RIGHT
;
815 device
->DevChannels
[4] = FRONT_CENTER
;
816 device
->DevChannels
[5] = LFE
;
817 device
->DevChannels
[6] = SIDE_LEFT
;
818 device
->DevChannels
[7] = SIDE_RIGHT
;
821 /* Same as WFX order */
829 SetDefaultWFXChannelOrder(device
);
831 /* Sets the default order used by WaveFormatEx */
832 void SetDefaultWFXChannelOrder(ALCdevice
*device
)
834 switch(device
->FmtChans
)
836 case DevFmtMono
: device
->DevChannels
[0] = FRONT_CENTER
; break;
838 case DevFmtStereo
: device
->DevChannels
[0] = FRONT_LEFT
;
839 device
->DevChannels
[1] = FRONT_RIGHT
; break;
841 case DevFmtQuad
: device
->DevChannels
[0] = FRONT_LEFT
;
842 device
->DevChannels
[1] = FRONT_RIGHT
;
843 device
->DevChannels
[2] = BACK_LEFT
;
844 device
->DevChannels
[3] = BACK_RIGHT
; break;
846 case DevFmtX51
: device
->DevChannels
[0] = FRONT_LEFT
;
847 device
->DevChannels
[1] = FRONT_RIGHT
;
848 device
->DevChannels
[2] = FRONT_CENTER
;
849 device
->DevChannels
[3] = LFE
;
850 device
->DevChannels
[4] = BACK_LEFT
;
851 device
->DevChannels
[5] = BACK_RIGHT
; break;
853 case DevFmtX51Side
: device
->DevChannels
[0] = FRONT_LEFT
;
854 device
->DevChannels
[1] = FRONT_RIGHT
;
855 device
->DevChannels
[2] = FRONT_CENTER
;
856 device
->DevChannels
[3] = LFE
;
857 device
->DevChannels
[4] = SIDE_LEFT
;
858 device
->DevChannels
[5] = SIDE_RIGHT
; break;
860 case DevFmtX61
: device
->DevChannels
[0] = FRONT_LEFT
;
861 device
->DevChannels
[1] = FRONT_RIGHT
;
862 device
->DevChannels
[2] = FRONT_CENTER
;
863 device
->DevChannels
[3] = LFE
;
864 device
->DevChannels
[4] = BACK_CENTER
;
865 device
->DevChannels
[5] = SIDE_LEFT
;
866 device
->DevChannels
[6] = SIDE_RIGHT
; break;
868 case DevFmtX71
: device
->DevChannels
[0] = FRONT_LEFT
;
869 device
->DevChannels
[1] = FRONT_RIGHT
;
870 device
->DevChannels
[2] = FRONT_CENTER
;
871 device
->DevChannels
[3] = LFE
;
872 device
->DevChannels
[4] = BACK_LEFT
;
873 device
->DevChannels
[5] = BACK_RIGHT
;
874 device
->DevChannels
[6] = SIDE_LEFT
;
875 device
->DevChannels
[7] = SIDE_RIGHT
; break;
880 const ALCchar
*DevFmtTypeString(enum DevFmtType type
)
884 case DevFmtByte
: return "Signed Byte";
885 case DevFmtUByte
: return "Unsigned Byte";
886 case DevFmtShort
: return "Signed Short";
887 case DevFmtUShort
: return "Unsigned Short";
888 case DevFmtInt
: return "Signed Int";
889 case DevFmtUInt
: return "Unsigned Int";
890 case DevFmtFloat
: return "Float";
892 return "(unknown type)";
894 const ALCchar
*DevFmtChannelsString(enum DevFmtChannels chans
)
898 case DevFmtMono
: return "Mono";
899 case DevFmtStereo
: return "Stereo";
900 case DevFmtQuad
: return "Quadraphonic";
901 case DevFmtX51
: return "5.1 Surround";
902 case DevFmtX51Side
: return "5.1 Side";
903 case DevFmtX61
: return "6.1 Surround";
904 case DevFmtX71
: return "7.1 Surround";
906 return "(unknown channels)";
909 ALuint
BytesFromDevFmt(enum DevFmtType type
)
913 case DevFmtByte
: return sizeof(ALbyte
);
914 case DevFmtUByte
: return sizeof(ALubyte
);
915 case DevFmtShort
: return sizeof(ALshort
);
916 case DevFmtUShort
: return sizeof(ALushort
);
917 case DevFmtInt
: return sizeof(ALint
);
918 case DevFmtUInt
: return sizeof(ALuint
);
919 case DevFmtFloat
: return sizeof(ALfloat
);
923 ALuint
ChannelsFromDevFmt(enum DevFmtChannels chans
)
927 case DevFmtMono
: return 1;
928 case DevFmtStereo
: return 2;
929 case DevFmtQuad
: return 4;
930 case DevFmtX51
: return 6;
931 case DevFmtX51Side
: return 6;
932 case DevFmtX61
: return 7;
933 case DevFmtX71
: return 8;
937 static ALboolean
DecomposeDevFormat(ALenum format
, enum DevFmtChannels
*chans
,
938 enum DevFmtType
*type
)
940 static const struct {
942 enum DevFmtChannels channels
;
943 enum DevFmtType type
;
945 { AL_FORMAT_MONO8
, DevFmtMono
, DevFmtUByte
},
946 { AL_FORMAT_MONO16
, DevFmtMono
, DevFmtShort
},
947 { AL_FORMAT_MONO_FLOAT32
, DevFmtMono
, DevFmtFloat
},
949 { AL_FORMAT_STEREO8
, DevFmtStereo
, DevFmtUByte
},
950 { AL_FORMAT_STEREO16
, DevFmtStereo
, DevFmtShort
},
951 { AL_FORMAT_STEREO_FLOAT32
, DevFmtStereo
, DevFmtFloat
},
953 { AL_FORMAT_QUAD8
, DevFmtQuad
, DevFmtUByte
},
954 { AL_FORMAT_QUAD16
, DevFmtQuad
, DevFmtShort
},
955 { AL_FORMAT_QUAD32
, DevFmtQuad
, DevFmtFloat
},
957 { AL_FORMAT_51CHN8
, DevFmtX51
, DevFmtUByte
},
958 { AL_FORMAT_51CHN16
, DevFmtX51
, DevFmtShort
},
959 { AL_FORMAT_51CHN32
, DevFmtX51
, DevFmtFloat
},
961 { AL_FORMAT_61CHN8
, DevFmtX61
, DevFmtUByte
},
962 { AL_FORMAT_61CHN16
, DevFmtX61
, DevFmtShort
},
963 { AL_FORMAT_61CHN32
, DevFmtX61
, DevFmtFloat
},
965 { AL_FORMAT_71CHN8
, DevFmtX71
, DevFmtUByte
},
966 { AL_FORMAT_71CHN16
, DevFmtX71
, DevFmtShort
},
967 { AL_FORMAT_71CHN32
, DevFmtX71
, DevFmtFloat
},
971 for(i
= 0;i
< COUNTOF(list
);i
++)
973 if(list
[i
].format
== format
)
975 *chans
= list
[i
].channels
;
976 *type
= list
[i
].type
;
984 static ALCboolean
IsValidALCType(ALCenum type
)
989 case ALC_UNSIGNED_BYTE_SOFT
:
991 case ALC_UNSIGNED_SHORT_SOFT
:
993 case ALC_UNSIGNED_INT_SOFT
:
1000 static ALCboolean
IsValidALCChannels(ALCenum channels
)
1005 case ALC_STEREO_SOFT
:
1007 case ALC_5POINT1_SOFT
:
1008 case ALC_6POINT1_SOFT
:
1009 case ALC_7POINT1_SOFT
:
1018 * Stores the latest ALC Error
1020 static void alcSetError(ALCdevice
*device
, ALCenum errorCode
)
1025 /* DebugBreak() will cause an exception if there is no debugger */
1026 if(IsDebuggerPresent())
1028 #elif defined(SIGTRAP)
1034 device
->LastError
= errorCode
;
1036 g_eLastNullDeviceError
= errorCode
;
1040 /* UpdateDeviceParams
1042 * Updates device parameters according to the attribute list (caller is
1043 * responsible for holding the list lock).
1045 static ALCenum
UpdateDeviceParams(ALCdevice
*device
, const ALCint
*attrList
)
1047 ALCcontext
*context
;
1048 enum DevFmtChannels oldChans
;
1049 enum DevFmtType oldType
;
1054 // Check for attributes
1055 if(device
->Type
== Loopback
)
1061 GotAll
= GotFreq
|GotChans
|GotType
1063 ALCuint freq
, numMono
, numStereo
, numSends
;
1064 enum DevFmtChannels schans
;
1065 enum DevFmtType stype
;
1066 ALCuint attrIdx
= 0;
1071 WARN("Missing attributes for loopback device\n");
1072 return ALC_INVALID_VALUE
;
1075 numMono
= device
->NumMonoSources
;
1076 numStereo
= device
->NumStereoSources
;
1077 numSends
= device
->NumAuxSends
;
1078 schans
= device
->FmtChans
;
1079 stype
= device
->FmtType
;
1080 freq
= device
->Frequency
;
1082 while(attrList
[attrIdx
])
1084 if(attrList
[attrIdx
] == ALC_FORMAT_CHANNELS_SOFT
)
1086 ALCint val
= attrList
[attrIdx
+ 1];
1087 if(!IsValidALCChannels(val
) || !ChannelsFromDevFmt(val
))
1088 return ALC_INVALID_VALUE
;
1093 if(attrList
[attrIdx
] == ALC_FORMAT_TYPE_SOFT
)
1095 ALCint val
= attrList
[attrIdx
+ 1];
1096 if(!IsValidALCType(val
) || !BytesFromDevFmt(val
))
1097 return ALC_INVALID_VALUE
;
1102 if(attrList
[attrIdx
] == ALC_FREQUENCY
)
1104 freq
= attrList
[attrIdx
+ 1];
1105 if(freq
< MIN_OUTPUT_RATE
)
1106 return ALC_INVALID_VALUE
;
1110 if(attrList
[attrIdx
] == ALC_STEREO_SOURCES
)
1112 numStereo
= attrList
[attrIdx
+ 1];
1113 if(numStereo
> device
->MaxNoOfSources
)
1114 numStereo
= device
->MaxNoOfSources
;
1116 numMono
= device
->MaxNoOfSources
- numStereo
;
1119 if(attrList
[attrIdx
] == ALC_MAX_AUXILIARY_SENDS
)
1120 numSends
= attrList
[attrIdx
+ 1];
1125 if(gotFmt
!= GotAll
)
1127 WARN("Missing format for loopback device\n");
1128 return ALC_INVALID_VALUE
;
1131 ConfigValueUInt(NULL
, "sends", &numSends
);
1132 numSends
= minu(MAX_SENDS
, numSends
);
1134 if((device
->Flags
&DEVICE_RUNNING
))
1135 ALCdevice_StopPlayback(device
);
1136 device
->Flags
&= ~DEVICE_RUNNING
;
1138 device
->Frequency
= freq
;
1139 device
->FmtChans
= schans
;
1140 device
->FmtType
= stype
;
1141 device
->NumMonoSources
= numMono
;
1142 device
->NumStereoSources
= numStereo
;
1143 device
->NumAuxSends
= numSends
;
1145 else if(attrList
&& attrList
[0])
1147 ALCuint freq
, numMono
, numStereo
, numSends
;
1148 ALCuint attrIdx
= 0;
1150 /* If a context is already running on the device, stop playback so the
1151 * device attributes can be updated. */
1152 if((device
->Flags
&DEVICE_RUNNING
))
1153 ALCdevice_StopPlayback(device
);
1154 device
->Flags
&= ~DEVICE_RUNNING
;
1156 freq
= device
->Frequency
;
1157 numMono
= device
->NumMonoSources
;
1158 numStereo
= device
->NumStereoSources
;
1159 numSends
= device
->NumAuxSends
;
1161 while(attrList
[attrIdx
])
1163 if(attrList
[attrIdx
] == ALC_FREQUENCY
)
1165 freq
= attrList
[attrIdx
+ 1];
1166 device
->Flags
|= DEVICE_FREQUENCY_REQUEST
;
1169 if(attrList
[attrIdx
] == ALC_STEREO_SOURCES
)
1171 numStereo
= attrList
[attrIdx
+ 1];
1172 if(numStereo
> device
->MaxNoOfSources
)
1173 numStereo
= device
->MaxNoOfSources
;
1175 numMono
= device
->MaxNoOfSources
- numStereo
;
1178 if(attrList
[attrIdx
] == ALC_MAX_AUXILIARY_SENDS
)
1179 numSends
= attrList
[attrIdx
+ 1];
1184 ConfigValueUInt(NULL
, "frequency", &freq
);
1185 freq
= maxu(freq
, MIN_OUTPUT_RATE
);
1187 ConfigValueUInt(NULL
, "sends", &numSends
);
1188 numSends
= minu(MAX_SENDS
, numSends
);
1190 device
->UpdateSize
= (ALuint64
)device
->UpdateSize
* freq
/
1193 device
->Frequency
= freq
;
1194 device
->NumMonoSources
= numMono
;
1195 device
->NumStereoSources
= numStereo
;
1196 device
->NumAuxSends
= numSends
;
1199 if((device
->Flags
&DEVICE_RUNNING
))
1200 return ALC_NO_ERROR
;
1202 oldFreq
= device
->Frequency
;
1203 oldChans
= device
->FmtChans
;
1204 oldType
= device
->FmtType
;
1206 TRACE("Format pre-setup: %s%s, %s%s, %uhz%s, %u update size x%d\n",
1207 DevFmtChannelsString(device
->FmtChans
),
1208 (device
->Flags
&DEVICE_CHANNELS_REQUEST
)?" (requested)":"",
1209 DevFmtTypeString(device
->FmtType
),
1210 (device
->Flags
&DEVICE_SAMPLE_TYPE_REQUEST
)?" (requested)":"",
1212 (device
->Flags
&DEVICE_FREQUENCY_REQUEST
)?" (requested)":"",
1213 device
->UpdateSize
, device
->NumUpdates
);
1215 if(ALCdevice_ResetPlayback(device
) == ALC_FALSE
)
1216 return ALC_INVALID_DEVICE
;
1218 if(device
->FmtChans
!= oldChans
&& (device
->Flags
&DEVICE_CHANNELS_REQUEST
))
1220 ERR("Failed to set %s, got %s instead\n", DevFmtChannelsString(oldChans
),
1221 DevFmtChannelsString(device
->FmtChans
));
1222 device
->Flags
&= ~DEVICE_CHANNELS_REQUEST
;
1224 if(device
->FmtType
!= oldType
&& (device
->Flags
&DEVICE_SAMPLE_TYPE_REQUEST
))
1226 ERR("Failed to set %s, got %s instead\n", DevFmtTypeString(oldType
),
1227 DevFmtTypeString(device
->FmtType
));
1228 device
->Flags
&= ~DEVICE_SAMPLE_TYPE_REQUEST
;
1230 if(device
->Frequency
!= oldFreq
&& (device
->Flags
&DEVICE_FREQUENCY_REQUEST
))
1232 ERR("Failed to set %uhz, got %uhz instead\n", oldFreq
, device
->Frequency
);
1233 device
->Flags
&= ~DEVICE_FREQUENCY_REQUEST
;
1236 TRACE("Format post-setup: %s, %s, %uhz, %u update size x%d\n",
1237 DevFmtChannelsString(device
->FmtChans
),
1238 DevFmtTypeString(device
->FmtType
), device
->Frequency
,
1239 device
->UpdateSize
, device
->NumUpdates
);
1241 aluInitPanning(device
);
1243 for(i
= 0;i
< MAXCHANNELS
;i
++)
1245 device
->ClickRemoval
[i
] = 0.0f
;
1246 device
->PendingClicks
[i
] = 0.0f
;
1249 device
->Hrtf
= NULL
;
1250 if(device
->Type
!= Loopback
&& GetConfigValueBool(NULL
, "hrtf", AL_FALSE
))
1251 device
->Hrtf
= GetHrtf(device
);
1252 TRACE("HRTF %s\n", device
->Hrtf
?"enabled":"disabled");
1254 if(!device
->Hrtf
&& device
->Bs2bLevel
> 0 && device
->Bs2bLevel
<= 6)
1258 device
->Bs2b
= calloc(1, sizeof(*device
->Bs2b
));
1259 bs2b_clear(device
->Bs2b
);
1261 bs2b_set_srate(device
->Bs2b
, device
->Frequency
);
1262 bs2b_set_level(device
->Bs2b
, device
->Bs2bLevel
);
1263 TRACE("BS2B level %d\n", device
->Bs2bLevel
);
1268 device
->Bs2b
= NULL
;
1269 TRACE("BS2B disabled\n");
1272 device
->Flags
&= ~DEVICE_DUPLICATE_STEREO
;
1273 switch(device
->FmtChans
)
1283 if(GetConfigValueBool(NULL
, "stereodup", AL_TRUE
))
1284 device
->Flags
|= DEVICE_DUPLICATE_STEREO
;
1287 TRACE("Stereo duplication %s\n", (device
->Flags
&DEVICE_DUPLICATE_STEREO
)?"enabled":"disabled");
1289 oldMode
= SetMixerFPUMode();
1291 context
= device
->ContextList
;
1296 context
->UpdateSources
= AL_FALSE
;
1297 LockUIntMapRead(&context
->EffectSlotMap
);
1298 for(pos
= 0;pos
< context
->EffectSlotMap
.size
;pos
++)
1300 ALeffectslot
*slot
= context
->EffectSlotMap
.array
[pos
].value
;
1302 if(ALeffectState_DeviceUpdate(slot
->EffectState
, device
) == AL_FALSE
)
1304 UnlockUIntMapRead(&context
->EffectSlotMap
);
1305 UnlockDevice(device
);
1306 RestoreFPUMode(oldMode
);
1307 return ALC_INVALID_DEVICE
;
1309 slot
->NeedsUpdate
= AL_FALSE
;
1310 ALeffectState_Update(slot
->EffectState
, context
, slot
);
1312 UnlockUIntMapRead(&context
->EffectSlotMap
);
1314 LockUIntMapRead(&context
->SourceMap
);
1315 for(pos
= 0;pos
< context
->SourceMap
.size
;pos
++)
1317 ALsource
*source
= context
->SourceMap
.array
[pos
].value
;
1318 ALuint s
= device
->NumAuxSends
;
1319 while(s
< MAX_SENDS
)
1321 if(source
->Send
[s
].Slot
)
1322 DecrementRef(&source
->Send
[s
].Slot
->ref
);
1323 source
->Send
[s
].Slot
= NULL
;
1324 source
->Send
[s
].WetGain
= 1.0f
;
1325 source
->Send
[s
].WetGainHF
= 1.0f
;
1328 source
->NeedsUpdate
= AL_FALSE
;
1329 ALsource_Update(source
, context
);
1331 UnlockUIntMapRead(&context
->SourceMap
);
1333 context
= context
->next
;
1335 UnlockDevice(device
);
1336 RestoreFPUMode(oldMode
);
1338 if(ALCdevice_StartPlayback(device
) == ALC_FALSE
)
1339 return ALC_INVALID_DEVICE
;
1340 device
->Flags
|= DEVICE_RUNNING
;
1342 return ALC_NO_ERROR
;
1347 * Frees the device structure, and destroys any objects the app failed to
1348 * delete. Called once there's no more references on the device.
1350 static ALCvoid
FreeDevice(ALCdevice
*device
)
1352 TRACE("%p\n", device
);
1354 if(device
->DefaultSlot
)
1356 ALeffectState_Destroy(device
->DefaultSlot
->EffectState
);
1357 device
->DefaultSlot
->EffectState
= NULL
;
1360 if(device
->BufferMap
.size
> 0)
1362 WARN("(%p) Deleting %d Buffer(s)\n", device
, device
->BufferMap
.size
);
1363 ReleaseALBuffers(device
);
1365 ResetUIntMap(&device
->BufferMap
);
1367 if(device
->EffectMap
.size
> 0)
1369 WARN("(%p) Deleting %d Effect(s)\n", device
, device
->EffectMap
.size
);
1370 ReleaseALEffects(device
);
1372 ResetUIntMap(&device
->EffectMap
);
1374 if(device
->FilterMap
.size
> 0)
1376 WARN("(%p) Deleting %d Filter(s)\n", device
, device
->FilterMap
.size
);
1377 ReleaseALFilters(device
);
1379 ResetUIntMap(&device
->FilterMap
);
1382 device
->Bs2b
= NULL
;
1384 free(device
->szDeviceName
);
1385 device
->szDeviceName
= NULL
;
1387 DeleteCriticalSection(&device
->Mutex
);
1393 void ALCdevice_IncRef(ALCdevice
*device
)
1396 ref
= IncrementRef(&device
->ref
);
1397 TRACEREF("%p increasing refcount to %u\n", device
, ref
);
1400 void ALCdevice_DecRef(ALCdevice
*device
)
1403 ref
= DecrementRef(&device
->ref
);
1404 TRACEREF("%p decreasing refcount to %u\n", device
, ref
);
1405 if(ref
== 0) FreeDevice(device
);
1410 * Checks if the device handle is valid, and increments its ref count if so.
1412 static ALCdevice
*VerifyDevice(ALCdevice
*device
)
1414 ALCdevice
*tmpDevice
;
1420 tmpDevice
= DeviceList
;
1421 while(tmpDevice
&& tmpDevice
!= device
)
1422 tmpDevice
= tmpDevice
->next
;
1425 ALCdevice_IncRef(tmpDevice
);
1433 * Initializes context variables
1435 static ALvoid
InitContext(ALCcontext
*pContext
)
1439 //Initialise listener
1440 pContext
->Listener
.Gain
= 1.0f
;
1441 pContext
->Listener
.MetersPerUnit
= 1.0f
;
1442 pContext
->Listener
.Position
[0] = 0.0f
;
1443 pContext
->Listener
.Position
[1] = 0.0f
;
1444 pContext
->Listener
.Position
[2] = 0.0f
;
1445 pContext
->Listener
.Velocity
[0] = 0.0f
;
1446 pContext
->Listener
.Velocity
[1] = 0.0f
;
1447 pContext
->Listener
.Velocity
[2] = 0.0f
;
1448 pContext
->Listener
.Forward
[0] = 0.0f
;
1449 pContext
->Listener
.Forward
[1] = 0.0f
;
1450 pContext
->Listener
.Forward
[2] = -1.0f
;
1451 pContext
->Listener
.Up
[0] = 0.0f
;
1452 pContext
->Listener
.Up
[1] = 1.0f
;
1453 pContext
->Listener
.Up
[2] = 0.0f
;
1454 for(i
= 0;i
< 4;i
++)
1456 for(j
= 0;j
< 4;j
++)
1457 pContext
->Listener
.Matrix
[i
][j
] = ((i
==j
) ? 1.0f
: 0.0f
);
1461 pContext
->LastError
= AL_NO_ERROR
;
1462 pContext
->UpdateSources
= AL_FALSE
;
1463 pContext
->ActiveSourceCount
= 0;
1464 InitUIntMap(&pContext
->SourceMap
, pContext
->Device
->MaxNoOfSources
);
1465 InitUIntMap(&pContext
->EffectSlotMap
, pContext
->Device
->AuxiliaryEffectSlotMax
);
1468 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
1469 pContext
->SourceDistanceModel
= AL_FALSE
;
1470 pContext
->DopplerFactor
= 1.0f
;
1471 pContext
->DopplerVelocity
= 1.0f
;
1472 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
1473 pContext
->DeferUpdates
= AL_FALSE
;
1475 pContext
->ExtensionList
= alExtList
;
1481 * Cleans up the context, and destroys any remaining objects the app failed to
1482 * delete. Called once there's no more references on the context.
1484 static ALCvoid
FreeContext(ALCcontext
*context
)
1486 TRACE("%p\n", context
);
1488 if(context
->SourceMap
.size
> 0)
1490 ERR("(%p) Deleting %d Source(s)\n", context
, context
->SourceMap
.size
);
1491 ReleaseALSources(context
);
1493 ResetUIntMap(&context
->SourceMap
);
1495 if(context
->EffectSlotMap
.size
> 0)
1497 ERR("(%p) Deleting %d AuxiliaryEffectSlot(s)\n", context
, context
->EffectSlotMap
.size
);
1498 ReleaseALAuxiliaryEffectSlots(context
);
1500 ResetUIntMap(&context
->EffectSlotMap
);
1502 context
->ActiveSourceCount
= 0;
1503 free(context
->ActiveSources
);
1504 context
->ActiveSources
= NULL
;
1505 context
->MaxActiveSources
= 0;
1507 context
->ActiveEffectSlotCount
= 0;
1508 free(context
->ActiveEffectSlots
);
1509 context
->ActiveEffectSlots
= NULL
;
1510 context
->MaxActiveEffectSlots
= 0;
1512 ALCdevice_DecRef(context
->Device
);
1513 context
->Device
= NULL
;
1515 //Invalidate context
1516 memset(context
, 0, sizeof(ALCcontext
));
1522 * Removes the context reference from the given device and removes it from
1523 * being current on the running thread or globally.
1525 static void ReleaseContext(ALCcontext
*context
, ALCdevice
*device
)
1527 ALCcontext
*volatile*tmp_ctx
;
1529 if(pthread_getspecific(LocalContext
) == context
)
1531 WARN("%p released while current on thread\n", context
);
1532 pthread_setspecific(LocalContext
, NULL
);
1533 ALCcontext_DecRef(context
);
1536 if(CompExchangePtr((XchgPtr
*)&GlobalContext
, context
, NULL
))
1537 ALCcontext_DecRef(context
);
1540 tmp_ctx
= &device
->ContextList
;
1543 if(CompExchangePtr((XchgPtr
*)tmp_ctx
, context
, context
->next
))
1545 tmp_ctx
= &(*tmp_ctx
)->next
;
1547 UnlockDevice(device
);
1549 ALCcontext_DecRef(context
);
1552 void ALCcontext_IncRef(ALCcontext
*context
)
1555 ref
= IncrementRef(&context
->ref
);
1556 TRACEREF("%p increasing refcount to %u\n", context
, ref
);
1559 void ALCcontext_DecRef(ALCcontext
*context
)
1562 ref
= DecrementRef(&context
->ref
);
1563 TRACEREF("%p decreasing refcount to %u\n", context
, ref
);
1564 if(ref
== 0) FreeContext(context
);
1567 static void ReleaseThreadCtx(void *ptr
)
1569 WARN("%p current for thread being destroyed\n", ptr
);
1570 ALCcontext_DecRef(ptr
);
1575 * Checks that the given context is valid, and increments its reference count.
1577 static ALCcontext
*VerifyContext(ALCcontext
*context
)
1585 ALCcontext
*tmp_ctx
= dev
->ContextList
;
1588 if(tmp_ctx
== context
)
1590 ALCcontext_IncRef(tmp_ctx
);
1594 tmp_ctx
= tmp_ctx
->next
;
1606 * Returns the currently active context, and adds a reference without locking
1609 ALCcontext
*GetContextRef(void)
1611 ALCcontext
*context
;
1613 context
= pthread_getspecific(LocalContext
);
1615 ALCcontext_IncRef(context
);
1619 context
= GlobalContext
;
1621 ALCcontext_IncRef(context
);
1628 ///////////////////////////////////////////////////////
1631 ///////////////////////////////////////////////////////
1632 // ALC Functions calls
1635 // This should probably move to another c file but for now ...
1636 ALC_API ALCdevice
* ALC_APIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
1638 ALCdevice
*device
= NULL
;
1643 if(!CaptureBackend
.name
)
1645 alcSetError(NULL
, ALC_INVALID_VALUE
);
1651 alcSetError(NULL
, ALC_INVALID_VALUE
);
1655 if(deviceName
&& (!deviceName
[0] || strcasecmp(deviceName
, alcDefaultName
) == 0 || strcasecmp(deviceName
, "openal-soft") == 0))
1658 device
= calloc(1, sizeof(ALCdevice
));
1661 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
1666 device
->Funcs
= &CaptureBackend
.Funcs
;
1668 device
->Connected
= ALC_TRUE
;
1669 device
->Type
= Capture
;
1670 InitializeCriticalSection(&device
->Mutex
);
1672 InitUIntMap(&device
->BufferMap
, ~0);
1673 InitUIntMap(&device
->EffectMap
, ~0);
1674 InitUIntMap(&device
->FilterMap
, ~0);
1676 device
->szDeviceName
= NULL
;
1678 device
->Flags
|= DEVICE_FREQUENCY_REQUEST
;
1679 device
->Frequency
= frequency
;
1681 device
->Flags
|= DEVICE_CHANNELS_REQUEST
| DEVICE_SAMPLE_TYPE_REQUEST
;
1682 if(DecomposeDevFormat(format
, &device
->FmtChans
, &device
->FmtType
) == AL_FALSE
)
1684 DeleteCriticalSection(&device
->Mutex
);
1686 alcSetError(NULL
, ALC_INVALID_ENUM
);
1690 device
->UpdateSize
= SampleSize
;
1691 device
->NumUpdates
= 1;
1694 if((err
=ALCdevice_OpenCapture(device
, deviceName
)) != ALC_NO_ERROR
)
1697 DeleteCriticalSection(&device
->Mutex
);
1699 alcSetError(NULL
, err
);
1705 device
->next
= DeviceList
;
1706 } while(!CompExchangePtr((XchgPtr
*)&DeviceList
, device
->next
, device
));
1708 TRACE("Created device %p\n", device
);
1712 ALC_API ALCboolean ALC_APIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
1714 ALCdevice
*volatile*list
;
1718 while(*list
&& *list
!= pDevice
)
1719 list
= &(*list
)->next
;
1721 if(!*list
|| (*list
)->Type
!= Capture
)
1723 alcSetError(*list
, ALC_INVALID_DEVICE
);
1728 *list
= (*list
)->next
;
1731 ALCdevice_CloseCapture(pDevice
);
1733 ALCdevice_DecRef(pDevice
);
1738 ALC_API
void ALC_APIENTRY
alcCaptureStart(ALCdevice
*device
)
1741 if(!(device
=VerifyDevice(device
)) || device
->Type
!= Capture
)
1744 alcSetError(device
, ALC_INVALID_DEVICE
);
1745 if(device
) ALCdevice_DecRef(device
);
1748 if(device
->Connected
)
1750 if(!(device
->Flags
&DEVICE_RUNNING
))
1751 ALCdevice_StartCapture(device
);
1752 device
->Flags
|= DEVICE_RUNNING
;
1756 ALCdevice_DecRef(device
);
1759 ALC_API
void ALC_APIENTRY
alcCaptureStop(ALCdevice
*device
)
1762 if(!(device
=VerifyDevice(device
)) || device
->Type
!= Capture
)
1765 alcSetError(device
, ALC_INVALID_DEVICE
);
1766 if(device
) ALCdevice_DecRef(device
);
1769 if((device
->Flags
&DEVICE_RUNNING
))
1770 ALCdevice_StopCapture(device
);
1771 device
->Flags
&= ~DEVICE_RUNNING
;
1774 ALCdevice_DecRef(device
);
1777 ALC_API
void ALC_APIENTRY
alcCaptureSamples(ALCdevice
*device
, ALCvoid
*buffer
, ALCsizei samples
)
1779 ALCenum err
= ALC_INVALID_DEVICE
;
1781 if((device
=VerifyDevice(device
)) != NULL
&& device
->Type
== Capture
)
1783 err
= ALC_INVALID_VALUE
;
1784 if(samples
>= 0 && ALCdevice_AvailableSamples(device
) >= (ALCuint
)samples
)
1785 err
= ALCdevice_CaptureSamples(device
, buffer
, samples
);
1788 if(err
!= ALC_NO_ERROR
)
1789 alcSetError(device
, err
);
1790 if(device
) ALCdevice_DecRef(device
);
1796 Return last ALC generated error code
1798 ALC_API ALCenum ALC_APIENTRY
alcGetError(ALCdevice
*device
)
1802 if(VerifyDevice(device
))
1804 errorCode
= ExchangeInt(&device
->LastError
, ALC_NO_ERROR
);
1805 ALCdevice_DecRef(device
);
1808 errorCode
= ExchangeInt(&g_eLastNullDeviceError
, ALC_NO_ERROR
);
1814 /* alcSuspendContext
1818 ALC_API ALCvoid ALC_APIENTRY
alcSuspendContext(ALCcontext
*Context
)
1823 /* alcProcessContext
1827 ALC_API ALCvoid ALC_APIENTRY
alcProcessContext(ALCcontext
*Context
)
1835 * Returns information about the Device, and error strings
1837 ALC_API
const ALCchar
* ALC_APIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
1839 const ALCchar
*value
= NULL
;
1847 case ALC_INVALID_ENUM
:
1848 value
= alcErrInvalidEnum
;
1851 case ALC_INVALID_VALUE
:
1852 value
= alcErrInvalidValue
;
1855 case ALC_INVALID_DEVICE
:
1856 value
= alcErrInvalidDevice
;
1859 case ALC_INVALID_CONTEXT
:
1860 value
= alcErrInvalidContext
;
1863 case ALC_OUT_OF_MEMORY
:
1864 value
= alcErrOutOfMemory
;
1867 case ALC_DEVICE_SPECIFIER
:
1868 value
= alcDefaultName
;
1871 case ALC_ALL_DEVICES_SPECIFIER
:
1872 if(VerifyDevice(pDevice
))
1874 value
= pDevice
->szDeviceName
;
1875 ALCdevice_DecRef(pDevice
);
1879 ProbeAllDeviceList();
1880 value
= alcAllDeviceList
;
1884 case ALC_CAPTURE_DEVICE_SPECIFIER
:
1885 if(VerifyDevice(pDevice
))
1887 value
= pDevice
->szDeviceName
;
1888 ALCdevice_DecRef(pDevice
);
1892 ProbeCaptureDeviceList();
1893 value
= alcCaptureDeviceList
;
1897 /* Default devices are always first in the list */
1898 case ALC_DEFAULT_DEVICE_SPECIFIER
:
1899 value
= alcDefaultName
;
1902 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
1903 if(!alcAllDeviceList
)
1904 ProbeAllDeviceList();
1906 pDevice
= VerifyDevice(pDevice
);
1908 free(alcDefaultAllDeviceSpecifier
);
1909 alcDefaultAllDeviceSpecifier
= strdup(alcAllDeviceList
?
1910 alcAllDeviceList
: "");
1911 if(!alcDefaultAllDeviceSpecifier
)
1912 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1914 value
= alcDefaultAllDeviceSpecifier
;
1915 if(pDevice
) ALCdevice_DecRef(pDevice
);
1918 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
1919 if(!alcCaptureDeviceList
)
1920 ProbeCaptureDeviceList();
1922 pDevice
= VerifyDevice(pDevice
);
1924 free(alcCaptureDefaultDeviceSpecifier
);
1925 alcCaptureDefaultDeviceSpecifier
= strdup(alcCaptureDeviceList
?
1926 alcCaptureDeviceList
: "");
1927 if(!alcCaptureDefaultDeviceSpecifier
)
1928 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1930 value
= alcCaptureDefaultDeviceSpecifier
;
1931 if(pDevice
) ALCdevice_DecRef(pDevice
);
1934 case ALC_EXTENSIONS
:
1935 if(!VerifyDevice(pDevice
))
1936 value
= alcNoDeviceExtList
;
1939 value
= alcExtensionList
;
1940 ALCdevice_DecRef(pDevice
);
1945 pDevice
= VerifyDevice(pDevice
);
1946 alcSetError(pDevice
, ALC_INVALID_ENUM
);
1947 if(pDevice
) ALCdevice_DecRef(pDevice
);
1957 * Returns information about the Device and the version of Open AL
1959 ALC_API ALCvoid ALC_APIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
1961 device
= VerifyDevice(device
);
1963 if(size
== 0 || data
== NULL
)
1965 alcSetError(device
, ALC_INVALID_VALUE
);
1966 if(device
) ALCdevice_DecRef(device
);
1974 case ALC_MAJOR_VERSION
:
1975 *data
= alcMajorVersion
;
1977 case ALC_MINOR_VERSION
:
1978 *data
= alcMinorVersion
;
1981 case ALC_ATTRIBUTES_SIZE
:
1982 case ALC_ALL_ATTRIBUTES
:
1986 case ALC_MONO_SOURCES
:
1987 case ALC_STEREO_SOURCES
:
1988 case ALC_CAPTURE_SAMPLES
:
1989 case ALC_FORMAT_CHANNELS_SOFT
:
1990 case ALC_FORMAT_TYPE_SOFT
:
1991 alcSetError(NULL
, ALC_INVALID_DEVICE
);
1995 alcSetError(NULL
, ALC_INVALID_ENUM
);
1999 else if(device
->Type
== Capture
)
2003 case ALC_CAPTURE_SAMPLES
:
2005 /* Re-validate the device since it may have been closed */
2006 ALCdevice_DecRef(device
);
2007 if((device
=VerifyDevice(device
)) != NULL
)
2008 *data
= ALCdevice_AvailableSamples(device
);
2010 alcSetError(NULL
, ALC_INVALID_DEVICE
);
2015 *data
= device
->Connected
;
2019 alcSetError(device
, ALC_INVALID_ENUM
);
2023 else /* render device */
2027 case ALC_MAJOR_VERSION
:
2028 *data
= alcMajorVersion
;
2031 case ALC_MINOR_VERSION
:
2032 *data
= alcMinorVersion
;
2035 case ALC_EFX_MAJOR_VERSION
:
2036 *data
= alcEFXMajorVersion
;
2039 case ALC_EFX_MINOR_VERSION
:
2040 *data
= alcEFXMinorVersion
;
2043 case ALC_ATTRIBUTES_SIZE
:
2047 case ALC_ALL_ATTRIBUTES
:
2049 alcSetError(device
, ALC_INVALID_VALUE
);
2054 data
[i
++] = ALC_FREQUENCY
;
2055 data
[i
++] = device
->Frequency
;
2057 if(device
->Type
!= Loopback
)
2059 data
[i
++] = ALC_REFRESH
;
2060 data
[i
++] = device
->Frequency
/ device
->UpdateSize
;
2062 data
[i
++] = ALC_SYNC
;
2063 data
[i
++] = ALC_FALSE
;
2067 data
[i
++] = ALC_FORMAT_CHANNELS_SOFT
;
2068 data
[i
++] = device
->FmtChans
;
2070 data
[i
++] = ALC_FORMAT_TYPE_SOFT
;
2071 data
[i
++] = device
->FmtType
;
2074 data
[i
++] = ALC_MONO_SOURCES
;
2075 data
[i
++] = device
->NumMonoSources
;
2077 data
[i
++] = ALC_STEREO_SOURCES
;
2078 data
[i
++] = device
->NumStereoSources
;
2080 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
2081 data
[i
++] = device
->NumAuxSends
;
2088 *data
= device
->Frequency
;
2092 if(device
->Type
== Loopback
)
2093 alcSetError(device
, ALC_INVALID_DEVICE
);
2095 *data
= device
->Frequency
/ device
->UpdateSize
;
2099 if(device
->Type
== Loopback
)
2100 alcSetError(device
, ALC_INVALID_DEVICE
);
2105 case ALC_FORMAT_CHANNELS_SOFT
:
2106 if(device
->Type
!= Loopback
)
2107 alcSetError(device
, ALC_INVALID_DEVICE
);
2109 *data
= device
->FmtChans
;
2112 case ALC_FORMAT_TYPE_SOFT
:
2113 if(device
->Type
!= Loopback
)
2114 alcSetError(device
, ALC_INVALID_DEVICE
);
2116 *data
= device
->FmtType
;
2119 case ALC_MONO_SOURCES
:
2120 *data
= device
->NumMonoSources
;
2123 case ALC_STEREO_SOURCES
:
2124 *data
= device
->NumStereoSources
;
2127 case ALC_MAX_AUXILIARY_SENDS
:
2128 *data
= device
->NumAuxSends
;
2132 *data
= device
->Connected
;
2136 alcSetError(device
, ALC_INVALID_ENUM
);
2141 ALCdevice_DecRef(device
);
2145 /* alcIsExtensionPresent
2147 * Determines if there is support for a particular extension
2149 ALC_API ALCboolean ALC_APIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
2151 ALCboolean bResult
= ALC_FALSE
;
2153 device
= VerifyDevice(device
);
2156 alcSetError(device
, ALC_INVALID_VALUE
);
2159 size_t len
= strlen(extName
);
2160 const char *ptr
= (device
? alcExtensionList
: alcNoDeviceExtList
);
2163 if(strncasecmp(ptr
, extName
, len
) == 0 &&
2164 (ptr
[len
] == '\0' || isspace(ptr
[len
])))
2169 if((ptr
=strchr(ptr
, ' ')) != NULL
)
2173 } while(isspace(*ptr
));
2178 ALCdevice_DecRef(device
);
2183 /* alcGetProcAddress
2185 * Retrieves the function address for a particular extension function
2187 ALC_API ALCvoid
* ALC_APIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
2189 ALCvoid
*ptr
= NULL
;
2191 device
= VerifyDevice(device
);
2194 alcSetError(device
, ALC_INVALID_VALUE
);
2198 while(alcFunctions
[i
].funcName
&& strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
2200 ptr
= alcFunctions
[i
].address
;
2203 ALCdevice_DecRef(device
);
2210 * Get the value for a particular ALC Enumerated Value
2212 ALC_API ALCenum ALC_APIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
2216 device
= VerifyDevice(device
);
2219 alcSetError(device
, ALC_INVALID_VALUE
);
2223 while(enumeration
[i
].enumName
&& strcmp(enumeration
[i
].enumName
,enumName
) != 0)
2225 val
= enumeration
[i
].value
;
2228 ALCdevice_DecRef(device
);
2235 * Create and attach a Context to a particular Device.
2237 ALC_API ALCcontext
* ALC_APIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
2239 ALCcontext
*ALContext
;
2243 if(!(device
=VerifyDevice(device
)) || device
->Type
== Capture
|| !device
->Connected
)
2246 alcSetError(device
, ALC_INVALID_DEVICE
);
2247 if(device
) ALCdevice_DecRef(device
);
2251 /* Reset Context Last Error code */
2252 device
->LastError
= ALC_NO_ERROR
;
2254 if((err
=UpdateDeviceParams(device
, attrList
)) != ALC_NO_ERROR
)
2257 alcSetError(device
, err
);
2258 if(err
== ALC_INVALID_DEVICE
)
2259 aluHandleDisconnect(device
);
2260 ALCdevice_DecRef(device
);
2264 ALContext
= calloc(1, sizeof(ALCcontext
));
2269 ALContext
->MaxActiveSources
= 256;
2270 ALContext
->ActiveSources
= malloc(sizeof(ALContext
->ActiveSources
[0]) *
2271 ALContext
->MaxActiveSources
);
2273 if(!ALContext
|| !ALContext
->ActiveSources
)
2275 if(!device
->ContextList
)
2277 ALCdevice_StopPlayback(device
);
2278 device
->Flags
&= ~DEVICE_RUNNING
;
2285 alcSetError(device
, ALC_OUT_OF_MEMORY
);
2286 ALCdevice_DecRef(device
);
2290 ALContext
->Device
= device
;
2291 ALCdevice_IncRef(device
);
2292 InitContext(ALContext
);
2295 ALContext
->next
= device
->ContextList
;
2296 } while(!CompExchangePtr((XchgPtr
*)&device
->ContextList
, ALContext
->next
, ALContext
));
2299 if(device
->DefaultSlot
)
2300 InitializeEffect(ALContext
, device
->DefaultSlot
, &ForcedEffect
);
2301 ALContext
->LastError
= AL_NO_ERROR
;
2303 ALCdevice_DecRef(device
);
2305 TRACE("Created context %p\n", ALContext
);
2309 /* alcDestroyContext
2313 ALC_API ALCvoid ALC_APIENTRY
alcDestroyContext(ALCcontext
*context
)
2318 /* alcGetContextsDevice sets an error for invalid contexts */
2319 Device
= alcGetContextsDevice(context
);
2322 ReleaseContext(context
, Device
);
2323 if(!Device
->ContextList
)
2325 ALCdevice_StopPlayback(Device
);
2326 Device
->Flags
&= ~DEVICE_RUNNING
;
2333 /* alcGetCurrentContext
2335 * Returns the currently active Context
2337 ALC_API ALCcontext
* ALC_APIENTRY
alcGetCurrentContext(ALCvoid
)
2339 ALCcontext
*Context
;
2341 Context
= pthread_getspecific(LocalContext
);
2342 if(!Context
) Context
= GlobalContext
;
2347 /* alcGetThreadContext
2349 * Returns the currently active thread-local Context
2351 ALC_API ALCcontext
* ALC_APIENTRY
alcGetThreadContext(void)
2353 ALCcontext
*Context
;
2354 Context
= pthread_getspecific(LocalContext
);
2359 /* alcMakeContextCurrent
2361 * Makes the given Context the active Context
2363 ALC_API ALCboolean ALC_APIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
2365 /* context must be a valid Context or NULL */
2366 if(context
&& !(context
=VerifyContext(context
)))
2368 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2371 /* context's reference count is already incremented */
2372 context
= ExchangePtr((XchgPtr
*)&GlobalContext
, context
);
2373 if(context
) ALCcontext_DecRef(context
);
2375 if((context
=pthread_getspecific(LocalContext
)) != NULL
)
2377 pthread_setspecific(LocalContext
, NULL
);
2378 ALCcontext_DecRef(context
);
2384 /* alcSetThreadContext
2386 * Makes the given Context the active Context for the current thread
2388 ALC_API ALCboolean ALC_APIENTRY
alcSetThreadContext(ALCcontext
*context
)
2392 /* context must be a valid Context or NULL */
2393 if(context
&& !(context
=VerifyContext(context
)))
2395 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2398 /* context's reference count is already incremented */
2399 old
= pthread_getspecific(LocalContext
);
2400 pthread_setspecific(LocalContext
, context
);
2401 if(old
) ALCcontext_DecRef(old
);
2407 /* alcGetContextsDevice
2409 * Returns the Device that a particular Context is attached to
2411 ALC_API ALCdevice
* ALC_APIENTRY
alcGetContextsDevice(ALCcontext
*Context
)
2415 if(!(Context
=VerifyContext(Context
)))
2417 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2420 Device
= Context
->Device
;
2421 ALCcontext_DecRef(Context
);
2429 * Open the Device specified.
2431 ALC_API ALCdevice
* ALC_APIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
2439 if(!PlaybackBackend
.name
)
2441 alcSetError(NULL
, ALC_INVALID_VALUE
);
2445 if(deviceName
&& (!deviceName
[0] || strcasecmp(deviceName
, alcDefaultName
) == 0 || strcasecmp(deviceName
, "openal-soft") == 0))
2448 device
= calloc(1, sizeof(ALCdevice
)+sizeof(ALeffectslot
));
2451 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
2456 device
->Funcs
= &PlaybackBackend
.Funcs
;
2458 device
->Connected
= ALC_TRUE
;
2459 device
->Type
= Playback
;
2460 InitializeCriticalSection(&device
->Mutex
);
2461 device
->LastError
= ALC_NO_ERROR
;
2464 device
->Bs2b
= NULL
;
2465 device
->Bs2bLevel
= 0;
2466 device
->szDeviceName
= NULL
;
2468 device
->ContextList
= NULL
;
2470 device
->MaxNoOfSources
= 256;
2471 device
->AuxiliaryEffectSlotMax
= 4;
2472 device
->NumAuxSends
= MAX_SENDS
;
2474 InitUIntMap(&device
->BufferMap
, ~0);
2475 InitUIntMap(&device
->EffectMap
, ~0);
2476 InitUIntMap(&device
->FilterMap
, ~0);
2479 device
->FmtChans
= DevFmtStereo
;
2480 device
->FmtType
= DevFmtFloat
;
2481 device
->Frequency
= DEFAULT_OUTPUT_RATE
;
2482 device
->NumUpdates
= 4;
2483 device
->UpdateSize
= 1024;
2485 if(ConfigValueStr(NULL
, "channels", &fmt
))
2487 static const struct {
2488 const char name
[16];
2489 enum DevFmtChannels chans
;
2491 { "mono", DevFmtMono
},
2492 { "stereo", DevFmtStereo
},
2493 { "quad", DevFmtQuad
},
2494 { "surround51", DevFmtX51
},
2495 { "surround61", DevFmtX61
},
2496 { "surround71", DevFmtX71
},
2500 for(i
= 0;i
< COUNTOF(chanlist
);i
++)
2502 if(strcasecmp(chanlist
[i
].name
, fmt
) == 0)
2504 device
->FmtChans
= chanlist
[i
].chans
;
2505 device
->Flags
|= DEVICE_CHANNELS_REQUEST
;
2509 if(i
== COUNTOF(chanlist
))
2510 ERR("Unsupported channels: %s\n", fmt
);
2512 if(ConfigValueStr(NULL
, "sample-type", &fmt
))
2514 static const struct {
2515 const char name
[16];
2516 enum DevFmtType type
;
2518 { "int8", DevFmtByte
},
2519 { "uint8", DevFmtUByte
},
2520 { "int16", DevFmtShort
},
2521 { "uint16", DevFmtUShort
},
2522 { "int32", DevFmtInt
},
2523 { "uint32", DevFmtUInt
},
2524 { "float32", DevFmtFloat
},
2528 for(i
= 0;i
< COUNTOF(typelist
);i
++)
2530 if(strcasecmp(typelist
[i
].name
, fmt
) == 0)
2532 device
->FmtType
= typelist
[i
].type
;
2533 device
->Flags
|= DEVICE_SAMPLE_TYPE_REQUEST
;
2537 if(i
== COUNTOF(typelist
))
2538 ERR("Unsupported sample-type: %s\n", fmt
);
2540 #define DEVICE_FORMAT_REQUEST (DEVICE_CHANNELS_REQUEST|DEVICE_SAMPLE_TYPE_REQUEST)
2541 if((device
->Flags
&DEVICE_FORMAT_REQUEST
) != DEVICE_FORMAT_REQUEST
&&
2542 ConfigValueStr(NULL
, "format", &fmt
))
2544 static const struct {
2545 const char name
[32];
2546 enum DevFmtChannels channels
;
2547 enum DevFmtType type
;
2549 { "AL_FORMAT_MONO32", DevFmtMono
, DevFmtFloat
},
2550 { "AL_FORMAT_STEREO32", DevFmtStereo
, DevFmtFloat
},
2551 { "AL_FORMAT_QUAD32", DevFmtQuad
, DevFmtFloat
},
2552 { "AL_FORMAT_51CHN32", DevFmtX51
, DevFmtFloat
},
2553 { "AL_FORMAT_61CHN32", DevFmtX61
, DevFmtFloat
},
2554 { "AL_FORMAT_71CHN32", DevFmtX71
, DevFmtFloat
},
2556 { "AL_FORMAT_MONO16", DevFmtMono
, DevFmtShort
},
2557 { "AL_FORMAT_STEREO16", DevFmtStereo
, DevFmtShort
},
2558 { "AL_FORMAT_QUAD16", DevFmtQuad
, DevFmtShort
},
2559 { "AL_FORMAT_51CHN16", DevFmtX51
, DevFmtShort
},
2560 { "AL_FORMAT_61CHN16", DevFmtX61
, DevFmtShort
},
2561 { "AL_FORMAT_71CHN16", DevFmtX71
, DevFmtShort
},
2563 { "AL_FORMAT_MONO8", DevFmtMono
, DevFmtByte
},
2564 { "AL_FORMAT_STEREO8", DevFmtStereo
, DevFmtByte
},
2565 { "AL_FORMAT_QUAD8", DevFmtQuad
, DevFmtByte
},
2566 { "AL_FORMAT_51CHN8", DevFmtX51
, DevFmtByte
},
2567 { "AL_FORMAT_61CHN8", DevFmtX61
, DevFmtByte
},
2568 { "AL_FORMAT_71CHN8", DevFmtX71
, DevFmtByte
}
2572 ERR("Option 'format' is deprecated, please use 'channels' and 'sample-type'\n");
2573 for(i
= 0;i
< COUNTOF(formats
);i
++)
2575 if(strcasecmp(fmt
, formats
[i
].name
) == 0)
2577 if(!(device
->Flags
&DEVICE_CHANNELS_REQUEST
))
2578 device
->FmtChans
= formats
[i
].channels
;
2579 if(!(device
->Flags
&DEVICE_SAMPLE_TYPE_REQUEST
))
2580 device
->FmtType
= formats
[i
].type
;
2581 device
->Flags
|= DEVICE_FORMAT_REQUEST
;
2585 if(i
== COUNTOF(formats
))
2586 ERR("Unsupported format: %s\n", fmt
);
2588 #undef DEVICE_FORMAT_REQUEST
2590 if(ConfigValueUInt(NULL
, "frequency", &device
->Frequency
))
2592 device
->Flags
|= DEVICE_FREQUENCY_REQUEST
;
2593 if(device
->Frequency
< MIN_OUTPUT_RATE
)
2594 ERR("%uhz request clamped to %uhz minimum\n", device
->Frequency
, MIN_OUTPUT_RATE
);
2595 device
->Frequency
= maxu(device
->Frequency
, MIN_OUTPUT_RATE
);
2598 ConfigValueUInt(NULL
, "periods", &device
->NumUpdates
);
2599 device
->NumUpdates
= clampu(device
->NumUpdates
, 2, 16);
2601 ConfigValueUInt(NULL
, "period_size", &device
->UpdateSize
);
2602 device
->UpdateSize
= clampu(device
->UpdateSize
, 64, 8192);
2604 ConfigValueUInt(NULL
, "sources", &device
->MaxNoOfSources
);
2605 if(device
->MaxNoOfSources
== 0) device
->MaxNoOfSources
= 256;
2607 ConfigValueUInt(NULL
, "slots", &device
->AuxiliaryEffectSlotMax
);
2608 if(device
->AuxiliaryEffectSlotMax
== 0) device
->AuxiliaryEffectSlotMax
= 4;
2610 ConfigValueUInt(NULL
, "sends", &device
->NumAuxSends
);
2611 if(device
->NumAuxSends
> MAX_SENDS
) device
->NumAuxSends
= MAX_SENDS
;
2613 ConfigValueInt(NULL
, "cf_level", &device
->Bs2bLevel
);
2615 device
->NumStereoSources
= 1;
2616 device
->NumMonoSources
= device
->MaxNoOfSources
- device
->NumStereoSources
;
2618 if(ForcedEffect
.type
!= AL_EFFECT_NULL
)
2620 device
->DefaultSlot
= (ALeffectslot
*)(device
+1);
2621 if(InitEffectSlot(device
->DefaultSlot
) != AL_NO_ERROR
)
2622 device
->DefaultSlot
= NULL
;
2625 // Find a playback device to open
2627 if((err
=ALCdevice_OpenPlayback(device
, deviceName
)) != ALC_NO_ERROR
)
2630 DeleteCriticalSection(&device
->Mutex
);
2632 alcSetError(NULL
, err
);
2638 device
->next
= DeviceList
;
2639 } while(!CompExchangePtr((XchgPtr
*)&DeviceList
, device
->next
, device
));
2641 TRACE("Created device %p, \"%s\"\n", device
, device
->szDeviceName
);
2647 * Close the specified Device
2649 ALC_API ALCboolean ALC_APIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
2651 ALCdevice
*volatile*list
;
2656 while(*list
&& *list
!= pDevice
)
2657 list
= &(*list
)->next
;
2659 if(!*list
|| (*list
)->Type
== Capture
)
2661 alcSetError(*list
, ALC_INVALID_DEVICE
);
2666 *list
= (*list
)->next
;
2669 while((ctx
=pDevice
->ContextList
) != NULL
)
2671 WARN("Releasing context %p\n", ctx
);
2672 ReleaseContext(ctx
, pDevice
);
2674 if((pDevice
->Flags
&DEVICE_RUNNING
))
2675 ALCdevice_StopPlayback(pDevice
);
2676 pDevice
->Flags
&= ~DEVICE_RUNNING
;
2678 ALCdevice_ClosePlayback(pDevice
);
2680 ALCdevice_DecRef(pDevice
);
2686 /* alcLoopbackOpenDeviceSOFT
2688 * Open a loopback device, for manual rendering.
2690 ALC_API ALCdevice
* ALC_APIENTRY
alcLoopbackOpenDeviceSOFT(const ALCchar
*deviceName
)
2696 /* Make sure the device name, if specified, is us. */
2697 if(deviceName
&& strcmp(deviceName
, alcDefaultName
) != 0)
2699 alcSetError(NULL
, ALC_INVALID_VALUE
);
2703 device
= calloc(1, sizeof(ALCdevice
));
2706 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
2711 device
->Funcs
= &BackendLoopback
.Funcs
;
2713 device
->Connected
= ALC_TRUE
;
2714 device
->Type
= Loopback
;
2715 InitializeCriticalSection(&device
->Mutex
);
2716 device
->LastError
= ALC_NO_ERROR
;
2719 device
->Bs2b
= NULL
;
2720 device
->Bs2bLevel
= 0;
2721 device
->szDeviceName
= NULL
;
2723 device
->ContextList
= NULL
;
2725 device
->MaxNoOfSources
= 256;
2726 device
->AuxiliaryEffectSlotMax
= 4;
2727 device
->NumAuxSends
= MAX_SENDS
;
2729 InitUIntMap(&device
->BufferMap
, ~0);
2730 InitUIntMap(&device
->EffectMap
, ~0);
2731 InitUIntMap(&device
->FilterMap
, ~0);
2734 device
->NumUpdates
= 0;
2735 device
->UpdateSize
= 0;
2737 device
->Frequency
= 44100;
2738 device
->FmtChans
= DevFmtStereo
;
2739 device
->FmtType
= DevFmtShort
;
2741 ConfigValueUInt(NULL
, "sources", &device
->MaxNoOfSources
);
2742 if(device
->MaxNoOfSources
== 0) device
->MaxNoOfSources
= 256;
2744 ConfigValueUInt(NULL
, "slots", &device
->AuxiliaryEffectSlotMax
);
2745 if(device
->AuxiliaryEffectSlotMax
== 0) device
->AuxiliaryEffectSlotMax
= 4;
2747 ConfigValueUInt(NULL
, "sends", &device
->NumAuxSends
);
2748 if(device
->NumAuxSends
> MAX_SENDS
) device
->NumAuxSends
= MAX_SENDS
;
2750 device
->NumStereoSources
= 1;
2751 device
->NumMonoSources
= device
->MaxNoOfSources
- device
->NumStereoSources
;
2753 // Open the "backend"
2754 ALCdevice_OpenPlayback(device
, "Loopback");
2756 device
->next
= DeviceList
;
2757 } while(!CompExchangePtr((XchgPtr
*)&DeviceList
, device
->next
, device
));
2759 TRACE("Created device %p\n", device
);
2763 /* alcIsRenderFormatSupportedSOFT
2765 * Determines if the loopback device supports the given format for rendering.
2767 ALC_API ALCboolean ALC_APIENTRY
alcIsRenderFormatSupportedSOFT(ALCdevice
*device
, ALCsizei freq
, ALCenum channels
, ALCenum type
)
2769 ALCboolean ret
= ALC_FALSE
;
2771 if(!(device
=VerifyDevice(device
)) || device
->Type
!= Loopback
)
2772 alcSetError(device
, ALC_INVALID_DEVICE
);
2774 alcSetError(device
, ALC_INVALID_VALUE
);
2775 else if(!IsValidALCType(type
) || !IsValidALCChannels(channels
))
2776 alcSetError(device
, ALC_INVALID_ENUM
);
2779 if(BytesFromDevFmt(type
) > 0 && ChannelsFromDevFmt(channels
) > 0 &&
2780 freq
>= MIN_OUTPUT_RATE
)
2783 if(device
) ALCdevice_DecRef(device
);
2788 /* alcRenderSamplesSOFT
2790 * Renders some samples into a buffer, using the format last set by the
2791 * attributes given to alcCreateContext.
2793 ALC_API
void ALC_APIENTRY
alcRenderSamplesSOFT(ALCdevice
*device
, ALCvoid
*buffer
, ALCsizei samples
)
2795 if(!(device
=VerifyDevice(device
)) || device
->Type
!= Loopback
)
2796 alcSetError(device
, ALC_INVALID_DEVICE
);
2797 else if(samples
< 0 || (samples
> 0 && buffer
== NULL
))
2798 alcSetError(device
, ALC_INVALID_VALUE
);
2800 aluMixData(device
, buffer
, samples
);
2801 if(device
) ALCdevice_DecRef(device
);
2805 static void ReleaseALC(void)
2809 free(alcAllDeviceList
); alcAllDeviceList
= NULL
;
2810 alcAllDeviceListSize
= 0;
2811 free(alcCaptureDeviceList
); alcCaptureDeviceList
= NULL
;
2812 alcCaptureDeviceListSize
= 0;
2814 free(alcDefaultAllDeviceSpecifier
);
2815 alcDefaultAllDeviceSpecifier
= NULL
;
2816 free(alcCaptureDefaultDeviceSpecifier
);
2817 alcCaptureDefaultDeviceSpecifier
= NULL
;
2819 if((dev
=ExchangePtr((XchgPtr
*)&DeviceList
, NULL
)) != NULL
)
2824 } while((dev
=dev
->next
) != NULL
);
2825 ERR("%u device%s not closed\n", num
, (num
>1)?"s":"");
2829 ///////////////////////////////////////////////////////