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_SOFTX_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(HANDLE hModule
,DWORD ul_reason_for_call
,LPVOID lpReserved
)
426 // Perform actions based on the reason for calling.
427 switch(ul_reason_for_call
)
429 case DLL_PROCESS_ATTACH
:
430 InitUIntMap(&TlsDestructor
, ~0);
434 case DLL_THREAD_DETACH
:
435 LockUIntMapRead(&TlsDestructor
);
436 for(i
= 0;i
< TlsDestructor
.size
;i
++)
438 void *ptr
= pthread_getspecific(TlsDestructor
.array
[i
].key
);
439 void (*callback
)(void*) = (void(*)(void*))TlsDestructor
.array
[i
].value
;
443 UnlockUIntMapRead(&TlsDestructor
);
446 case DLL_PROCESS_DETACH
:
451 ResetUIntMap(&TlsDestructor
);
456 #elif defined(_MSC_VER)
457 #pragma section(".CRT$XCU",read)
458 static void alc_constructor(void);
459 static void alc_destructor(void);
460 __declspec(allocate(".CRT$XCU")) void (__cdecl
* alc_constructor_
)(void) = alc_constructor
;
462 static void alc_constructor(void)
464 atexit(alc_destructor
);
468 static void alc_destructor(void)
472 #elif defined(HAVE_GCC_DESTRUCTOR)
473 static void alc_init(void) __attribute__((constructor
));
474 static void alc_deinit(void) __attribute__((destructor
));
476 #error "No static initialization available on this platform!"
478 #elif defined(HAVE_GCC_DESTRUCTOR)
479 static void alc_init(void) __attribute__((constructor
));
480 static void alc_deinit(void) __attribute__((destructor
));
482 #error "No global initialization available on this platform!"
485 static void alc_init(void)
491 str
= getenv("__ALSOFT_HALF_ANGLE_CONES");
492 if(str
&& (strcasecmp(str
, "true") == 0 || strtol(str
, NULL
, 0) == 1))
495 str
= getenv("__ALSOFT_REVERSE_Z");
496 if(str
&& (strcasecmp(str
, "true") == 0 || strtol(str
, NULL
, 0) == 1))
499 str
= getenv("ALSOFT_TRAP_ERROR");
500 if(str
&& (strcasecmp(str
, "true") == 0 || strtol(str
, NULL
, 0) == 1))
502 TrapALError
= AL_TRUE
;
503 TrapALCError
= AL_TRUE
;
507 str
= getenv("ALSOFT_TRAP_AL_ERROR");
508 if(str
&& (strcasecmp(str
, "true") == 0 || strtol(str
, NULL
, 0) == 1))
509 TrapALError
= AL_TRUE
;
511 str
= getenv("ALSOFT_TRAP_ALC_ERROR");
512 if(str
&& (strcasecmp(str
, "true") == 0 || strtol(str
, NULL
, 0) == 1))
513 TrapALCError
= ALC_TRUE
;
516 pthread_key_create(&LocalContext
, ReleaseThreadCtx
);
517 InitializeCriticalSection(&ListLock
);
521 static void alc_deinit_safe(void)
529 DeleteCriticalSection(&ListLock
);
530 pthread_key_delete(LocalContext
);
532 if(LogFile
!= stderr
)
537 static void alc_deinit(void)
543 memset(&PlaybackBackend
, 0, sizeof(PlaybackBackend
));
544 memset(&CaptureBackend
, 0, sizeof(CaptureBackend
));
546 for(i
= 0;BackendList
[i
].Deinit
;i
++)
547 BackendList
[i
].Deinit();
548 BackendLoopback
.Deinit();
553 static void alc_initconfig(void)
555 const char *devs
, *str
;
559 str
= getenv("ALSOFT_LOGLEVEL");
562 long lvl
= strtol(str
, NULL
, 0);
563 if(lvl
>= NoLog
&& lvl
<= LogRef
)
567 str
= getenv("ALSOFT_LOGFILE");
570 FILE *logfile
= fopen(str
, "wat");
571 if(logfile
) LogFile
= logfile
;
572 else ERR("Failed to open log file '%s'\n", str
);
584 ConfigValueInt(NULL
, "rt-prio", &RTPrioLevel
);
586 if(ConfigValueStr(NULL
, "resampler", &str
))
588 if(strcasecmp(str
, "point") == 0 || strcasecmp(str
, "none") == 0)
589 DefaultResampler
= PointResampler
;
590 else if(strcasecmp(str
, "linear") == 0)
591 DefaultResampler
= LinearResampler
;
592 else if(strcasecmp(str
, "cubic") == 0)
593 DefaultResampler
= CubicResampler
;
598 n
= strtol(str
, &end
, 0);
599 if(*end
== '\0' && (n
== PointResampler
|| n
== LinearResampler
|| n
== CubicResampler
))
600 DefaultResampler
= n
;
602 WARN("Invalid resampler: %s\n", str
);
607 TrapALCError
= GetConfigValueBool(NULL
, "trap-alc-error", ALC_FALSE
);
610 TrapALError
= GetConfigValueBool(NULL
, "trap-al-error", AL_FALSE
);
612 if(ConfigValueFloat("reverb", "boost", &valf
))
613 ReverbBoost
*= aluPow(10.0f
, valf
/ 20.0f
);
615 EmulateEAXReverb
= GetConfigValueBool("reverb", "emulate-eax", AL_FALSE
);
617 if(((devs
=getenv("ALSOFT_DRIVERS")) && devs
[0]) ||
618 ConfigValueStr(NULL
, "drivers", &devs
))
622 const char *next
= devs
;
623 int endlist
, delitem
;
628 next
= strchr(devs
, ',');
630 delitem
= (devs
[0] == '-');
631 if(devs
[0] == '-') devs
++;
633 if(!devs
[0] || devs
[0] == ',')
640 len
= (next
? ((size_t)(next
-devs
)) : strlen(devs
));
641 for(n
= i
;BackendList
[n
].Init
;n
++)
643 if(len
== strlen(BackendList
[n
].name
) &&
644 strncmp(BackendList
[n
].name
, devs
, len
) == 0)
649 BackendList
[n
] = BackendList
[n
+1];
651 } while(BackendList
[n
].Init
);
655 struct BackendInfo Bkp
= BackendList
[n
];
658 BackendList
[n
] = BackendList
[n
-1];
661 BackendList
[n
] = Bkp
;
672 BackendList
[i
].name
= NULL
;
673 BackendList
[i
].Init
= NULL
;
674 BackendList
[i
].Deinit
= NULL
;
675 BackendList
[i
].Probe
= NULL
;
679 for(i
= 0;BackendList
[i
].Init
&& (!PlaybackBackend
.name
|| !CaptureBackend
.name
);i
++)
681 if(!BackendList
[i
].Init(&BackendList
[i
].Funcs
))
683 WARN("Failed to initialize backend \"%s\"\n", BackendList
[i
].name
);
687 TRACE("Initialized backend \"%s\"\n", BackendList
[i
].name
);
688 if(BackendList
[i
].Funcs
.OpenPlayback
&& !PlaybackBackend
.name
)
690 PlaybackBackend
= BackendList
[i
];
691 TRACE("Added \"%s\" for playback\n", PlaybackBackend
.name
);
693 if(BackendList
[i
].Funcs
.OpenCapture
&& !CaptureBackend
.name
)
695 CaptureBackend
= BackendList
[i
];
696 TRACE("Added \"%s\" for capture\n", CaptureBackend
.name
);
699 BackendLoopback
.Init(&BackendLoopback
.Funcs
);
701 if(ConfigValueStr(NULL
, "excludefx", &str
))
704 const char *next
= str
;
708 next
= strchr(str
, ',');
710 if(!str
[0] || next
== str
)
713 len
= (next
? ((size_t)(next
-str
)) : strlen(str
));
714 for(n
= 0;EffectList
[n
].name
;n
++)
716 if(len
== strlen(EffectList
[n
].name
) &&
717 strncmp(EffectList
[n
].name
, str
, len
) == 0)
718 DisabledEffects
[EffectList
[n
].type
] = AL_TRUE
;
723 InitEffect(&ForcedEffect
);
724 str
= getenv("ALSOFT_DEFAULT_REVERB");
726 GetReverbEffect(str
, &ForcedEffect
);
727 else if(ConfigValueStr(NULL
, "default-reverb", &str
))
728 GetReverbEffect(str
, &ForcedEffect
);
732 static void LockLists(void)
734 EnterCriticalSection(&ListLock
);
737 static void UnlockLists(void)
739 LeaveCriticalSection(&ListLock
);
743 static void ProbeList(ALCchar
**list
, size_t *listsize
, enum DevProbe type
)
752 if(type
== ALL_DEVICE_PROBE
&& PlaybackBackend
.Probe
)
753 PlaybackBackend
.Probe(type
);
754 else if(type
== CAPTURE_DEVICE_PROBE
&& CaptureBackend
.Probe
)
755 CaptureBackend
.Probe(type
);
759 static void ProbeAllDeviceList(void)
760 { ProbeList(&alcAllDeviceList
, &alcAllDeviceListSize
, ALL_DEVICE_PROBE
); }
761 static void ProbeCaptureDeviceList(void)
762 { ProbeList(&alcCaptureDeviceList
, &alcCaptureDeviceListSize
, CAPTURE_DEVICE_PROBE
); }
765 static void AppendList(const ALCchar
*name
, ALCchar
**List
, size_t *ListSize
)
767 size_t len
= strlen(name
);
773 temp
= realloc(*List
, (*ListSize
) + len
+ 2);
776 ERR("Realloc failed to add %s!\n", name
);
781 memcpy((*List
)+(*ListSize
), name
, len
+1);
783 (*List
)[*ListSize
] = 0;
786 #define DECL_APPEND_LIST_FUNC(type) \
787 void Append##type##List(const ALCchar *name) \
788 { AppendList(name, &alc##type##List, &alc##type##ListSize); }
790 DECL_APPEND_LIST_FUNC(AllDevice
)
791 DECL_APPEND_LIST_FUNC(CaptureDevice
)
793 #undef DECL_APPEND_LIST_FUNC
796 /* Sets the default channel order used by most non-WaveFormatEx-based APIs */
797 void SetDefaultChannelOrder(ALCdevice
*device
)
799 switch(device
->FmtChans
)
801 case DevFmtX51
: device
->DevChannels
[0] = FRONT_LEFT
;
802 device
->DevChannels
[1] = FRONT_RIGHT
;
803 device
->DevChannels
[2] = BACK_LEFT
;
804 device
->DevChannels
[3] = BACK_RIGHT
;
805 device
->DevChannels
[4] = FRONT_CENTER
;
806 device
->DevChannels
[5] = LFE
;
809 case DevFmtX71
: device
->DevChannels
[0] = FRONT_LEFT
;
810 device
->DevChannels
[1] = FRONT_RIGHT
;
811 device
->DevChannels
[2] = BACK_LEFT
;
812 device
->DevChannels
[3] = BACK_RIGHT
;
813 device
->DevChannels
[4] = FRONT_CENTER
;
814 device
->DevChannels
[5] = LFE
;
815 device
->DevChannels
[6] = SIDE_LEFT
;
816 device
->DevChannels
[7] = SIDE_RIGHT
;
819 /* Same as WFX order */
827 SetDefaultWFXChannelOrder(device
);
829 /* Sets the default order used by WaveFormatEx */
830 void SetDefaultWFXChannelOrder(ALCdevice
*device
)
832 switch(device
->FmtChans
)
834 case DevFmtMono
: device
->DevChannels
[0] = FRONT_CENTER
; break;
836 case DevFmtStereo
: device
->DevChannels
[0] = FRONT_LEFT
;
837 device
->DevChannels
[1] = FRONT_RIGHT
; break;
839 case DevFmtQuad
: device
->DevChannels
[0] = FRONT_LEFT
;
840 device
->DevChannels
[1] = FRONT_RIGHT
;
841 device
->DevChannels
[2] = BACK_LEFT
;
842 device
->DevChannels
[3] = BACK_RIGHT
; break;
844 case DevFmtX51
: device
->DevChannels
[0] = FRONT_LEFT
;
845 device
->DevChannels
[1] = FRONT_RIGHT
;
846 device
->DevChannels
[2] = FRONT_CENTER
;
847 device
->DevChannels
[3] = LFE
;
848 device
->DevChannels
[4] = BACK_LEFT
;
849 device
->DevChannels
[5] = BACK_RIGHT
; break;
851 case DevFmtX51Side
: device
->DevChannels
[0] = FRONT_LEFT
;
852 device
->DevChannels
[1] = FRONT_RIGHT
;
853 device
->DevChannels
[2] = FRONT_CENTER
;
854 device
->DevChannels
[3] = LFE
;
855 device
->DevChannels
[4] = SIDE_LEFT
;
856 device
->DevChannels
[5] = SIDE_RIGHT
; break;
858 case DevFmtX61
: device
->DevChannels
[0] = FRONT_LEFT
;
859 device
->DevChannels
[1] = FRONT_RIGHT
;
860 device
->DevChannels
[2] = FRONT_CENTER
;
861 device
->DevChannels
[3] = LFE
;
862 device
->DevChannels
[4] = BACK_CENTER
;
863 device
->DevChannels
[5] = SIDE_LEFT
;
864 device
->DevChannels
[6] = SIDE_RIGHT
; break;
866 case DevFmtX71
: device
->DevChannels
[0] = FRONT_LEFT
;
867 device
->DevChannels
[1] = FRONT_RIGHT
;
868 device
->DevChannels
[2] = FRONT_CENTER
;
869 device
->DevChannels
[3] = LFE
;
870 device
->DevChannels
[4] = BACK_LEFT
;
871 device
->DevChannels
[5] = BACK_RIGHT
;
872 device
->DevChannels
[6] = SIDE_LEFT
;
873 device
->DevChannels
[7] = SIDE_RIGHT
; break;
878 const ALCchar
*DevFmtTypeString(enum DevFmtType type
)
882 case DevFmtByte
: return "Signed Byte";
883 case DevFmtUByte
: return "Unsigned Byte";
884 case DevFmtShort
: return "Signed Short";
885 case DevFmtUShort
: return "Unsigned Short";
886 case DevFmtInt
: return "Signed Int";
887 case DevFmtUInt
: return "Unsigned Int";
888 case DevFmtFloat
: return "Float";
890 return "(unknown type)";
892 const ALCchar
*DevFmtChannelsString(enum DevFmtChannels chans
)
896 case DevFmtMono
: return "Mono";
897 case DevFmtStereo
: return "Stereo";
898 case DevFmtQuad
: return "Quadraphonic";
899 case DevFmtX51
: return "5.1 Surround";
900 case DevFmtX51Side
: return "5.1 Side";
901 case DevFmtX61
: return "6.1 Surround";
902 case DevFmtX71
: return "7.1 Surround";
904 return "(unknown channels)";
907 ALuint
BytesFromDevFmt(enum DevFmtType type
)
911 case DevFmtByte
: return sizeof(ALbyte
);
912 case DevFmtUByte
: return sizeof(ALubyte
);
913 case DevFmtShort
: return sizeof(ALshort
);
914 case DevFmtUShort
: return sizeof(ALushort
);
915 case DevFmtInt
: return sizeof(ALint
);
916 case DevFmtUInt
: return sizeof(ALuint
);
917 case DevFmtFloat
: return sizeof(ALfloat
);
921 ALuint
ChannelsFromDevFmt(enum DevFmtChannels chans
)
925 case DevFmtMono
: return 1;
926 case DevFmtStereo
: return 2;
927 case DevFmtQuad
: return 4;
928 case DevFmtX51
: return 6;
929 case DevFmtX51Side
: return 6;
930 case DevFmtX61
: return 7;
931 case DevFmtX71
: return 8;
935 static ALboolean
DecomposeDevFormat(ALenum format
, enum DevFmtChannels
*chans
,
936 enum DevFmtType
*type
)
938 static const struct {
940 enum DevFmtChannels channels
;
941 enum DevFmtType type
;
943 { AL_FORMAT_MONO8
, DevFmtMono
, DevFmtUByte
},
944 { AL_FORMAT_MONO16
, DevFmtMono
, DevFmtShort
},
945 { AL_FORMAT_MONO_FLOAT32
, DevFmtMono
, DevFmtFloat
},
947 { AL_FORMAT_STEREO8
, DevFmtStereo
, DevFmtUByte
},
948 { AL_FORMAT_STEREO16
, DevFmtStereo
, DevFmtShort
},
949 { AL_FORMAT_STEREO_FLOAT32
, DevFmtStereo
, DevFmtFloat
},
951 { AL_FORMAT_QUAD8
, DevFmtQuad
, DevFmtUByte
},
952 { AL_FORMAT_QUAD16
, DevFmtQuad
, DevFmtShort
},
953 { AL_FORMAT_QUAD32
, DevFmtQuad
, DevFmtFloat
},
955 { AL_FORMAT_51CHN8
, DevFmtX51
, DevFmtUByte
},
956 { AL_FORMAT_51CHN16
, DevFmtX51
, DevFmtShort
},
957 { AL_FORMAT_51CHN32
, DevFmtX51
, DevFmtFloat
},
959 { AL_FORMAT_61CHN8
, DevFmtX61
, DevFmtUByte
},
960 { AL_FORMAT_61CHN16
, DevFmtX61
, DevFmtShort
},
961 { AL_FORMAT_61CHN32
, DevFmtX61
, DevFmtFloat
},
963 { AL_FORMAT_71CHN8
, DevFmtX71
, DevFmtUByte
},
964 { AL_FORMAT_71CHN16
, DevFmtX71
, DevFmtShort
},
965 { AL_FORMAT_71CHN32
, DevFmtX71
, DevFmtFloat
},
969 for(i
= 0;i
< COUNTOF(list
);i
++)
971 if(list
[i
].format
== format
)
973 *chans
= list
[i
].channels
;
974 *type
= list
[i
].type
;
982 static ALCboolean
IsValidALCType(ALCenum type
)
987 case ALC_UNSIGNED_BYTE_SOFT
:
989 case ALC_UNSIGNED_SHORT_SOFT
:
991 case ALC_UNSIGNED_INT_SOFT
:
998 static ALCboolean
IsValidALCChannels(ALCenum channels
)
1003 case ALC_STEREO_SOFT
:
1005 case ALC_5POINT1_SOFT
:
1006 case ALC_6POINT1_SOFT
:
1007 case ALC_7POINT1_SOFT
:
1016 * Stores the latest ALC Error
1018 static void alcSetError(ALCdevice
*device
, ALCenum errorCode
)
1023 /* DebugBreak() will cause an exception if there is no debugger */
1024 if(IsDebuggerPresent())
1026 #elif defined(SIGTRAP)
1032 device
->LastError
= errorCode
;
1034 g_eLastNullDeviceError
= errorCode
;
1038 /* UpdateDeviceParams
1040 * Updates device parameters according to the attribute list (caller is
1041 * responsible for holding the list lock).
1043 static ALCenum
UpdateDeviceParams(ALCdevice
*device
, const ALCint
*attrList
)
1045 ALCcontext
*context
;
1046 enum DevFmtChannels oldChans
;
1047 enum DevFmtType oldType
;
1052 // Check for attributes
1053 if(device
->Type
== Loopback
)
1059 GotAll
= GotFreq
|GotChans
|GotType
1061 ALCuint freq
, numMono
, numStereo
, numSends
;
1062 enum DevFmtChannels schans
;
1063 enum DevFmtType stype
;
1064 ALCuint attrIdx
= 0;
1069 WARN("Missing attributes for loopback device\n");
1070 return ALC_INVALID_VALUE
;
1073 numMono
= device
->NumMonoSources
;
1074 numStereo
= device
->NumStereoSources
;
1075 numSends
= device
->NumAuxSends
;
1076 schans
= device
->FmtChans
;
1077 stype
= device
->FmtType
;
1078 freq
= device
->Frequency
;
1080 while(attrList
[attrIdx
])
1082 if(attrList
[attrIdx
] == ALC_FORMAT_CHANNELS_SOFT
)
1084 ALCint val
= attrList
[attrIdx
+ 1];
1085 if(!IsValidALCChannels(val
) || !ChannelsFromDevFmt(val
))
1086 return ALC_INVALID_VALUE
;
1091 if(attrList
[attrIdx
] == ALC_FORMAT_TYPE_SOFT
)
1093 ALCint val
= attrList
[attrIdx
+ 1];
1094 if(!IsValidALCType(val
) || !BytesFromDevFmt(val
))
1095 return ALC_INVALID_VALUE
;
1100 if(attrList
[attrIdx
] == ALC_FREQUENCY
)
1102 freq
= attrList
[attrIdx
+ 1];
1103 if(freq
< MIN_OUTPUT_RATE
)
1104 return ALC_INVALID_VALUE
;
1108 if(attrList
[attrIdx
] == ALC_STEREO_SOURCES
)
1110 numStereo
= attrList
[attrIdx
+ 1];
1111 if(numStereo
> device
->MaxNoOfSources
)
1112 numStereo
= device
->MaxNoOfSources
;
1114 numMono
= device
->MaxNoOfSources
- numStereo
;
1117 if(attrList
[attrIdx
] == ALC_MAX_AUXILIARY_SENDS
)
1118 numSends
= attrList
[attrIdx
+ 1];
1123 if(gotFmt
!= GotAll
)
1125 WARN("Missing format for loopback device\n");
1126 return ALC_INVALID_VALUE
;
1129 ConfigValueUInt(NULL
, "sends", &numSends
);
1130 numSends
= minu(MAX_SENDS
, numSends
);
1132 if((device
->Flags
&DEVICE_RUNNING
))
1133 ALCdevice_StopPlayback(device
);
1134 device
->Flags
&= ~DEVICE_RUNNING
;
1136 device
->Frequency
= freq
;
1137 device
->FmtChans
= schans
;
1138 device
->FmtType
= stype
;
1139 device
->NumMonoSources
= numMono
;
1140 device
->NumStereoSources
= numStereo
;
1141 device
->NumAuxSends
= numSends
;
1143 else if(attrList
&& attrList
[0])
1145 ALCuint freq
, numMono
, numStereo
, numSends
;
1146 ALCuint attrIdx
= 0;
1148 /* If a context is already running on the device, stop playback so the
1149 * device attributes can be updated. */
1150 if((device
->Flags
&DEVICE_RUNNING
))
1151 ALCdevice_StopPlayback(device
);
1152 device
->Flags
&= ~DEVICE_RUNNING
;
1154 freq
= device
->Frequency
;
1155 numMono
= device
->NumMonoSources
;
1156 numStereo
= device
->NumStereoSources
;
1157 numSends
= device
->NumAuxSends
;
1159 while(attrList
[attrIdx
])
1161 if(attrList
[attrIdx
] == ALC_FREQUENCY
)
1163 freq
= attrList
[attrIdx
+ 1];
1164 device
->Flags
|= DEVICE_FREQUENCY_REQUEST
;
1167 if(attrList
[attrIdx
] == ALC_STEREO_SOURCES
)
1169 numStereo
= attrList
[attrIdx
+ 1];
1170 if(numStereo
> device
->MaxNoOfSources
)
1171 numStereo
= device
->MaxNoOfSources
;
1173 numMono
= device
->MaxNoOfSources
- numStereo
;
1176 if(attrList
[attrIdx
] == ALC_MAX_AUXILIARY_SENDS
)
1177 numSends
= attrList
[attrIdx
+ 1];
1182 ConfigValueUInt(NULL
, "frequency", &freq
);
1183 freq
= maxu(freq
, MIN_OUTPUT_RATE
);
1185 ConfigValueUInt(NULL
, "sends", &numSends
);
1186 numSends
= minu(MAX_SENDS
, numSends
);
1188 device
->UpdateSize
= (ALuint64
)device
->UpdateSize
* freq
/
1191 device
->Frequency
= freq
;
1192 device
->NumMonoSources
= numMono
;
1193 device
->NumStereoSources
= numStereo
;
1194 device
->NumAuxSends
= numSends
;
1197 if((device
->Flags
&DEVICE_RUNNING
))
1198 return ALC_NO_ERROR
;
1200 oldFreq
= device
->Frequency
;
1201 oldChans
= device
->FmtChans
;
1202 oldType
= device
->FmtType
;
1204 TRACE("Format pre-setup: %s%s, %s%s, %uhz%s, %u update size x%d\n",
1205 DevFmtChannelsString(device
->FmtChans
),
1206 (device
->Flags
&DEVICE_CHANNELS_REQUEST
)?" (requested)":"",
1207 DevFmtTypeString(device
->FmtType
),
1208 (device
->Flags
&DEVICE_SAMPLE_TYPE_REQUEST
)?" (requested)":"",
1210 (device
->Flags
&DEVICE_FREQUENCY_REQUEST
)?" (requested)":"",
1211 device
->UpdateSize
, device
->NumUpdates
);
1213 if(ALCdevice_ResetPlayback(device
) == ALC_FALSE
)
1214 return ALC_INVALID_DEVICE
;
1216 if(device
->FmtChans
!= oldChans
&& (device
->Flags
&DEVICE_CHANNELS_REQUEST
))
1218 ERR("Failed to set %s, got %s instead\n", DevFmtChannelsString(oldChans
),
1219 DevFmtChannelsString(device
->FmtChans
));
1220 device
->Flags
&= ~DEVICE_CHANNELS_REQUEST
;
1222 if(device
->FmtType
!= oldType
&& (device
->Flags
&DEVICE_SAMPLE_TYPE_REQUEST
))
1224 ERR("Failed to set %s, got %s instead\n", DevFmtTypeString(oldType
),
1225 DevFmtTypeString(device
->FmtType
));
1226 device
->Flags
&= ~DEVICE_SAMPLE_TYPE_REQUEST
;
1228 if(device
->Frequency
!= oldFreq
&& (device
->Flags
&DEVICE_FREQUENCY_REQUEST
))
1230 ERR("Failed to set %uhz, got %uhz instead\n", oldFreq
, device
->Frequency
);
1231 device
->Flags
&= ~DEVICE_FREQUENCY_REQUEST
;
1234 TRACE("Format post-setup: %s, %s, %uhz, %u update size x%d\n",
1235 DevFmtChannelsString(device
->FmtChans
),
1236 DevFmtTypeString(device
->FmtType
), device
->Frequency
,
1237 device
->UpdateSize
, device
->NumUpdates
);
1239 aluInitPanning(device
);
1241 for(i
= 0;i
< MAXCHANNELS
;i
++)
1243 device
->ClickRemoval
[i
] = 0.0f
;
1244 device
->PendingClicks
[i
] = 0.0f
;
1247 device
->Hrtf
= NULL
;
1248 if(device
->Type
!= Loopback
&& GetConfigValueBool(NULL
, "hrtf", AL_FALSE
))
1249 device
->Hrtf
= GetHrtf(device
);
1250 TRACE("HRTF %s\n", device
->Hrtf
?"enabled":"disabled");
1252 if(!device
->Hrtf
&& device
->Bs2bLevel
> 0 && device
->Bs2bLevel
<= 6)
1256 device
->Bs2b
= calloc(1, sizeof(*device
->Bs2b
));
1257 bs2b_clear(device
->Bs2b
);
1259 bs2b_set_srate(device
->Bs2b
, device
->Frequency
);
1260 bs2b_set_level(device
->Bs2b
, device
->Bs2bLevel
);
1261 TRACE("BS2B level %d\n", device
->Bs2bLevel
);
1266 device
->Bs2b
= NULL
;
1267 TRACE("BS2B disabled\n");
1270 device
->Flags
&= ~DEVICE_DUPLICATE_STEREO
;
1271 switch(device
->FmtChans
)
1281 if(GetConfigValueBool(NULL
, "stereodup", AL_TRUE
))
1282 device
->Flags
|= DEVICE_DUPLICATE_STEREO
;
1285 TRACE("Stereo duplication %s\n", (device
->Flags
&DEVICE_DUPLICATE_STEREO
)?"enabled":"disabled");
1287 oldMode
= SetMixerFPUMode();
1289 context
= device
->ContextList
;
1294 context
->UpdateSources
= AL_FALSE
;
1295 LockUIntMapRead(&context
->EffectSlotMap
);
1296 for(pos
= 0;pos
< context
->EffectSlotMap
.size
;pos
++)
1298 ALeffectslot
*slot
= context
->EffectSlotMap
.array
[pos
].value
;
1300 if(ALeffectState_DeviceUpdate(slot
->EffectState
, device
) == AL_FALSE
)
1302 UnlockUIntMapRead(&context
->EffectSlotMap
);
1303 UnlockDevice(device
);
1304 RestoreFPUMode(oldMode
);
1305 return ALC_INVALID_DEVICE
;
1307 slot
->NeedsUpdate
= AL_FALSE
;
1308 ALeffectState_Update(slot
->EffectState
, context
, slot
);
1310 UnlockUIntMapRead(&context
->EffectSlotMap
);
1312 LockUIntMapRead(&context
->SourceMap
);
1313 for(pos
= 0;pos
< context
->SourceMap
.size
;pos
++)
1315 ALsource
*source
= context
->SourceMap
.array
[pos
].value
;
1316 ALuint s
= device
->NumAuxSends
;
1317 while(s
< MAX_SENDS
)
1319 if(source
->Send
[s
].Slot
)
1320 DecrementRef(&source
->Send
[s
].Slot
->ref
);
1321 source
->Send
[s
].Slot
= NULL
;
1322 source
->Send
[s
].WetGain
= 1.0f
;
1323 source
->Send
[s
].WetGainHF
= 1.0f
;
1326 source
->NeedsUpdate
= AL_FALSE
;
1327 ALsource_Update(source
, context
);
1329 UnlockUIntMapRead(&context
->SourceMap
);
1331 context
= context
->next
;
1333 UnlockDevice(device
);
1334 RestoreFPUMode(oldMode
);
1336 if(ALCdevice_StartPlayback(device
) == ALC_FALSE
)
1337 return ALC_INVALID_DEVICE
;
1338 device
->Flags
|= DEVICE_RUNNING
;
1340 return ALC_NO_ERROR
;
1345 * Frees the device structure, and destroys any objects the app failed to
1346 * delete. Called once there's no more references on the device.
1348 static ALCvoid
FreeDevice(ALCdevice
*device
)
1350 TRACE("%p\n", device
);
1352 if(device
->DefaultSlot
)
1354 ALeffectState_Destroy(device
->DefaultSlot
->EffectState
);
1355 device
->DefaultSlot
->EffectState
= NULL
;
1358 if(device
->BufferMap
.size
> 0)
1360 WARN("(%p) Deleting %d Buffer(s)\n", device
, device
->BufferMap
.size
);
1361 ReleaseALBuffers(device
);
1363 ResetUIntMap(&device
->BufferMap
);
1365 if(device
->EffectMap
.size
> 0)
1367 WARN("(%p) Deleting %d Effect(s)\n", device
, device
->EffectMap
.size
);
1368 ReleaseALEffects(device
);
1370 ResetUIntMap(&device
->EffectMap
);
1372 if(device
->FilterMap
.size
> 0)
1374 WARN("(%p) Deleting %d Filter(s)\n", device
, device
->FilterMap
.size
);
1375 ReleaseALFilters(device
);
1377 ResetUIntMap(&device
->FilterMap
);
1380 device
->Bs2b
= NULL
;
1382 free(device
->szDeviceName
);
1383 device
->szDeviceName
= NULL
;
1385 DeleteCriticalSection(&device
->Mutex
);
1391 void ALCdevice_IncRef(ALCdevice
*device
)
1394 ref
= IncrementRef(&device
->ref
);
1395 TRACEREF("%p increasing refcount to %u\n", device
, ref
);
1398 void ALCdevice_DecRef(ALCdevice
*device
)
1401 ref
= DecrementRef(&device
->ref
);
1402 TRACEREF("%p decreasing refcount to %u\n", device
, ref
);
1403 if(ref
== 0) FreeDevice(device
);
1408 * Checks if the device handle is valid, and increments its ref count if so.
1410 static ALCdevice
*VerifyDevice(ALCdevice
*device
)
1412 ALCdevice
*tmpDevice
;
1418 tmpDevice
= DeviceList
;
1419 while(tmpDevice
&& tmpDevice
!= device
)
1420 tmpDevice
= tmpDevice
->next
;
1423 ALCdevice_IncRef(tmpDevice
);
1431 * Initializes context variables
1433 static ALvoid
InitContext(ALCcontext
*pContext
)
1437 //Initialise listener
1438 pContext
->Listener
.Gain
= 1.0f
;
1439 pContext
->Listener
.MetersPerUnit
= 1.0f
;
1440 pContext
->Listener
.Position
[0] = 0.0f
;
1441 pContext
->Listener
.Position
[1] = 0.0f
;
1442 pContext
->Listener
.Position
[2] = 0.0f
;
1443 pContext
->Listener
.Velocity
[0] = 0.0f
;
1444 pContext
->Listener
.Velocity
[1] = 0.0f
;
1445 pContext
->Listener
.Velocity
[2] = 0.0f
;
1446 pContext
->Listener
.Forward
[0] = 0.0f
;
1447 pContext
->Listener
.Forward
[1] = 0.0f
;
1448 pContext
->Listener
.Forward
[2] = -1.0f
;
1449 pContext
->Listener
.Up
[0] = 0.0f
;
1450 pContext
->Listener
.Up
[1] = 1.0f
;
1451 pContext
->Listener
.Up
[2] = 0.0f
;
1452 for(i
= 0;i
< 4;i
++)
1454 for(j
= 0;j
< 4;j
++)
1455 pContext
->Listener
.Matrix
[i
][j
] = ((i
==j
) ? 1.0f
: 0.0f
);
1459 pContext
->LastError
= AL_NO_ERROR
;
1460 pContext
->UpdateSources
= AL_FALSE
;
1461 pContext
->ActiveSourceCount
= 0;
1462 InitUIntMap(&pContext
->SourceMap
, pContext
->Device
->MaxNoOfSources
);
1463 InitUIntMap(&pContext
->EffectSlotMap
, pContext
->Device
->AuxiliaryEffectSlotMax
);
1466 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
1467 pContext
->SourceDistanceModel
= AL_FALSE
;
1468 pContext
->DopplerFactor
= 1.0f
;
1469 pContext
->DopplerVelocity
= 1.0f
;
1470 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
1471 pContext
->DeferUpdates
= AL_FALSE
;
1473 pContext
->ExtensionList
= alExtList
;
1479 * Cleans up the context, and destroys any remaining objects the app failed to
1480 * delete. Called once there's no more references on the context.
1482 static ALCvoid
FreeContext(ALCcontext
*context
)
1484 TRACE("%p\n", context
);
1486 if(context
->SourceMap
.size
> 0)
1488 ERR("(%p) Deleting %d Source(s)\n", context
, context
->SourceMap
.size
);
1489 ReleaseALSources(context
);
1491 ResetUIntMap(&context
->SourceMap
);
1493 if(context
->EffectSlotMap
.size
> 0)
1495 ERR("(%p) Deleting %d AuxiliaryEffectSlot(s)\n", context
, context
->EffectSlotMap
.size
);
1496 ReleaseALAuxiliaryEffectSlots(context
);
1498 ResetUIntMap(&context
->EffectSlotMap
);
1500 context
->ActiveSourceCount
= 0;
1501 free(context
->ActiveSources
);
1502 context
->ActiveSources
= NULL
;
1503 context
->MaxActiveSources
= 0;
1505 context
->ActiveEffectSlotCount
= 0;
1506 free(context
->ActiveEffectSlots
);
1507 context
->ActiveEffectSlots
= NULL
;
1508 context
->MaxActiveEffectSlots
= 0;
1510 ALCdevice_DecRef(context
->Device
);
1511 context
->Device
= NULL
;
1513 //Invalidate context
1514 memset(context
, 0, sizeof(ALCcontext
));
1520 * Removes the context reference from the given device and removes it from
1521 * being current on the running thread or globally.
1523 static void ReleaseContext(ALCcontext
*context
, ALCdevice
*device
)
1525 ALCcontext
*volatile*tmp_ctx
;
1527 if(pthread_getspecific(LocalContext
) == context
)
1529 WARN("%p released while current on thread\n", context
);
1530 pthread_setspecific(LocalContext
, NULL
);
1531 ALCcontext_DecRef(context
);
1534 if(CompExchangePtr((XchgPtr
*)&GlobalContext
, context
, NULL
))
1535 ALCcontext_DecRef(context
);
1538 tmp_ctx
= &device
->ContextList
;
1541 if(CompExchangePtr((XchgPtr
*)tmp_ctx
, context
, context
->next
))
1543 tmp_ctx
= &(*tmp_ctx
)->next
;
1545 UnlockDevice(device
);
1547 ALCcontext_DecRef(context
);
1550 void ALCcontext_IncRef(ALCcontext
*context
)
1553 ref
= IncrementRef(&context
->ref
);
1554 TRACEREF("%p increasing refcount to %u\n", context
, ref
);
1557 void ALCcontext_DecRef(ALCcontext
*context
)
1560 ref
= DecrementRef(&context
->ref
);
1561 TRACEREF("%p decreasing refcount to %u\n", context
, ref
);
1562 if(ref
== 0) FreeContext(context
);
1565 static void ReleaseThreadCtx(void *ptr
)
1567 WARN("%p current for thread being destroyed\n", ptr
);
1568 ALCcontext_DecRef(ptr
);
1573 * Checks that the given context is valid, and increments its reference count.
1575 static ALCcontext
*VerifyContext(ALCcontext
*context
)
1583 ALCcontext
*tmp_ctx
= dev
->ContextList
;
1586 if(tmp_ctx
== context
)
1588 ALCcontext_IncRef(tmp_ctx
);
1592 tmp_ctx
= tmp_ctx
->next
;
1604 * Returns the currently active context, and adds a reference without locking
1607 ALCcontext
*GetContextRef(void)
1609 ALCcontext
*context
;
1611 context
= pthread_getspecific(LocalContext
);
1613 ALCcontext_IncRef(context
);
1617 context
= GlobalContext
;
1619 ALCcontext_IncRef(context
);
1626 ///////////////////////////////////////////////////////
1629 ///////////////////////////////////////////////////////
1630 // ALC Functions calls
1633 // This should probably move to another c file but for now ...
1634 ALC_API ALCdevice
* ALC_APIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
1636 ALCdevice
*device
= NULL
;
1641 if(!CaptureBackend
.name
)
1643 alcSetError(NULL
, ALC_INVALID_VALUE
);
1649 alcSetError(NULL
, ALC_INVALID_VALUE
);
1653 if(deviceName
&& (!deviceName
[0] || strcasecmp(deviceName
, alcDefaultName
) == 0 || strcasecmp(deviceName
, "openal-soft") == 0))
1656 device
= calloc(1, sizeof(ALCdevice
));
1659 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
1664 device
->Funcs
= &CaptureBackend
.Funcs
;
1666 device
->Connected
= ALC_TRUE
;
1667 device
->Type
= Capture
;
1668 InitializeCriticalSection(&device
->Mutex
);
1670 InitUIntMap(&device
->BufferMap
, ~0);
1671 InitUIntMap(&device
->EffectMap
, ~0);
1672 InitUIntMap(&device
->FilterMap
, ~0);
1674 device
->szDeviceName
= NULL
;
1676 device
->Flags
|= DEVICE_FREQUENCY_REQUEST
;
1677 device
->Frequency
= frequency
;
1679 device
->Flags
|= DEVICE_CHANNELS_REQUEST
| DEVICE_SAMPLE_TYPE_REQUEST
;
1680 if(DecomposeDevFormat(format
, &device
->FmtChans
, &device
->FmtType
) == AL_FALSE
)
1682 DeleteCriticalSection(&device
->Mutex
);
1684 alcSetError(NULL
, ALC_INVALID_ENUM
);
1688 device
->UpdateSize
= SampleSize
;
1689 device
->NumUpdates
= 1;
1692 if((err
=ALCdevice_OpenCapture(device
, deviceName
)) != ALC_NO_ERROR
)
1695 DeleteCriticalSection(&device
->Mutex
);
1697 alcSetError(NULL
, err
);
1703 device
->next
= DeviceList
;
1704 } while(!CompExchangePtr((XchgPtr
*)&DeviceList
, device
->next
, device
));
1706 TRACE("Created device %p\n", device
);
1710 ALC_API ALCboolean ALC_APIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
1712 ALCdevice
*volatile*list
;
1716 while(*list
&& *list
!= pDevice
)
1717 list
= &(*list
)->next
;
1719 if(!*list
|| (*list
)->Type
!= Capture
)
1721 alcSetError(*list
, ALC_INVALID_DEVICE
);
1726 *list
= (*list
)->next
;
1729 LockDevice(pDevice
);
1730 ALCdevice_CloseCapture(pDevice
);
1731 UnlockDevice(pDevice
);
1733 ALCdevice_DecRef(pDevice
);
1738 ALC_API
void ALC_APIENTRY
alcCaptureStart(ALCdevice
*device
)
1740 if(!(device
=VerifyDevice(device
)) || device
->Type
!= Capture
)
1742 alcSetError(device
, ALC_INVALID_DEVICE
);
1743 if(device
) ALCdevice_DecRef(device
);
1747 if(device
->Connected
)
1748 ALCdevice_StartCapture(device
);
1749 UnlockDevice(device
);
1751 ALCdevice_DecRef(device
);
1754 ALC_API
void ALC_APIENTRY
alcCaptureStop(ALCdevice
*device
)
1756 if(!(device
=VerifyDevice(device
)) || device
->Type
!= Capture
)
1758 alcSetError(device
, ALC_INVALID_DEVICE
);
1759 if(device
) ALCdevice_DecRef(device
);
1763 if(device
->Connected
)
1764 ALCdevice_StopCapture(device
);
1765 UnlockDevice(device
);
1767 ALCdevice_DecRef(device
);
1770 ALC_API
void ALC_APIENTRY
alcCaptureSamples(ALCdevice
*device
, ALCvoid
*buffer
, ALCsizei samples
)
1772 ALCenum err
= ALC_INVALID_DEVICE
;
1773 if((device
=VerifyDevice(device
)) != NULL
&& device
->Type
== Capture
)
1775 err
= ALC_INVALID_VALUE
;
1777 if(samples
>= 0 && ALCdevice_AvailableSamples(device
) >= (ALCuint
)samples
)
1778 err
= ALCdevice_CaptureSamples(device
, buffer
, samples
);
1779 UnlockDevice(device
);
1781 if(err
!= ALC_NO_ERROR
)
1782 alcSetError(device
, err
);
1783 if(device
) ALCdevice_DecRef(device
);
1789 Return last ALC generated error code
1791 ALC_API ALCenum ALC_APIENTRY
alcGetError(ALCdevice
*device
)
1795 if(VerifyDevice(device
))
1797 errorCode
= ExchangeInt(&device
->LastError
, ALC_NO_ERROR
);
1798 ALCdevice_DecRef(device
);
1801 errorCode
= ExchangeInt(&g_eLastNullDeviceError
, ALC_NO_ERROR
);
1807 /* alcSuspendContext
1811 ALC_API ALCvoid ALC_APIENTRY
alcSuspendContext(ALCcontext
*Context
)
1816 /* alcProcessContext
1820 ALC_API ALCvoid ALC_APIENTRY
alcProcessContext(ALCcontext
*Context
)
1828 * Returns information about the Device, and error strings
1830 ALC_API
const ALCchar
* ALC_APIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
1832 const ALCchar
*value
= NULL
;
1840 case ALC_INVALID_ENUM
:
1841 value
= alcErrInvalidEnum
;
1844 case ALC_INVALID_VALUE
:
1845 value
= alcErrInvalidValue
;
1848 case ALC_INVALID_DEVICE
:
1849 value
= alcErrInvalidDevice
;
1852 case ALC_INVALID_CONTEXT
:
1853 value
= alcErrInvalidContext
;
1856 case ALC_OUT_OF_MEMORY
:
1857 value
= alcErrOutOfMemory
;
1860 case ALC_DEVICE_SPECIFIER
:
1861 value
= alcDefaultName
;
1864 case ALC_ALL_DEVICES_SPECIFIER
:
1865 if(VerifyDevice(pDevice
))
1867 value
= pDevice
->szDeviceName
;
1868 ALCdevice_DecRef(pDevice
);
1872 ProbeAllDeviceList();
1873 value
= alcAllDeviceList
;
1877 case ALC_CAPTURE_DEVICE_SPECIFIER
:
1878 if(VerifyDevice(pDevice
))
1880 value
= pDevice
->szDeviceName
;
1881 ALCdevice_DecRef(pDevice
);
1885 ProbeCaptureDeviceList();
1886 value
= alcCaptureDeviceList
;
1890 /* Default devices are always first in the list */
1891 case ALC_DEFAULT_DEVICE_SPECIFIER
:
1892 value
= alcDefaultName
;
1895 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
1896 if(!alcAllDeviceList
)
1897 ProbeAllDeviceList();
1899 pDevice
= VerifyDevice(pDevice
);
1901 free(alcDefaultAllDeviceSpecifier
);
1902 alcDefaultAllDeviceSpecifier
= strdup(alcAllDeviceList
?
1903 alcAllDeviceList
: "");
1904 if(!alcDefaultAllDeviceSpecifier
)
1905 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1907 value
= alcDefaultAllDeviceSpecifier
;
1908 if(pDevice
) ALCdevice_DecRef(pDevice
);
1911 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
1912 if(!alcCaptureDeviceList
)
1913 ProbeCaptureDeviceList();
1915 pDevice
= VerifyDevice(pDevice
);
1917 free(alcCaptureDefaultDeviceSpecifier
);
1918 alcCaptureDefaultDeviceSpecifier
= strdup(alcCaptureDeviceList
?
1919 alcCaptureDeviceList
: "");
1920 if(!alcCaptureDefaultDeviceSpecifier
)
1921 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1923 value
= alcCaptureDefaultDeviceSpecifier
;
1924 if(pDevice
) ALCdevice_DecRef(pDevice
);
1927 case ALC_EXTENSIONS
:
1928 if(!VerifyDevice(pDevice
))
1929 value
= alcNoDeviceExtList
;
1932 value
= alcExtensionList
;
1933 ALCdevice_DecRef(pDevice
);
1938 pDevice
= VerifyDevice(pDevice
);
1939 alcSetError(pDevice
, ALC_INVALID_ENUM
);
1940 if(pDevice
) ALCdevice_DecRef(pDevice
);
1950 * Returns information about the Device and the version of Open AL
1952 ALC_API ALCvoid ALC_APIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
1954 device
= VerifyDevice(device
);
1956 if(size
== 0 || data
== NULL
)
1958 alcSetError(device
, ALC_INVALID_VALUE
);
1959 if(device
) ALCdevice_DecRef(device
);
1967 case ALC_MAJOR_VERSION
:
1968 *data
= alcMajorVersion
;
1970 case ALC_MINOR_VERSION
:
1971 *data
= alcMinorVersion
;
1974 case ALC_ATTRIBUTES_SIZE
:
1975 case ALC_ALL_ATTRIBUTES
:
1979 case ALC_MONO_SOURCES
:
1980 case ALC_STEREO_SOURCES
:
1981 case ALC_CAPTURE_SAMPLES
:
1982 case ALC_FORMAT_CHANNELS_SOFT
:
1983 case ALC_FORMAT_TYPE_SOFT
:
1984 alcSetError(NULL
, ALC_INVALID_DEVICE
);
1988 alcSetError(NULL
, ALC_INVALID_ENUM
);
1992 else if(device
->Type
== Capture
)
1996 case ALC_CAPTURE_SAMPLES
:
1998 *data
= ALCdevice_AvailableSamples(device
);
1999 UnlockDevice(device
);
2003 *data
= device
->Connected
;
2007 alcSetError(device
, ALC_INVALID_ENUM
);
2011 else /* render device */
2015 case ALC_MAJOR_VERSION
:
2016 *data
= alcMajorVersion
;
2019 case ALC_MINOR_VERSION
:
2020 *data
= alcMinorVersion
;
2023 case ALC_EFX_MAJOR_VERSION
:
2024 *data
= alcEFXMajorVersion
;
2027 case ALC_EFX_MINOR_VERSION
:
2028 *data
= alcEFXMinorVersion
;
2031 case ALC_ATTRIBUTES_SIZE
:
2035 case ALC_ALL_ATTRIBUTES
:
2037 alcSetError(device
, ALC_INVALID_VALUE
);
2042 data
[i
++] = ALC_FREQUENCY
;
2043 data
[i
++] = device
->Frequency
;
2045 if(device
->Type
!= Loopback
)
2047 data
[i
++] = ALC_REFRESH
;
2048 data
[i
++] = device
->Frequency
/ device
->UpdateSize
;
2050 data
[i
++] = ALC_SYNC
;
2051 data
[i
++] = ALC_FALSE
;
2055 data
[i
++] = ALC_FORMAT_CHANNELS_SOFT
;
2056 data
[i
++] = device
->FmtChans
;
2058 data
[i
++] = ALC_FORMAT_TYPE_SOFT
;
2059 data
[i
++] = device
->FmtType
;
2062 data
[i
++] = ALC_MONO_SOURCES
;
2063 data
[i
++] = device
->NumMonoSources
;
2065 data
[i
++] = ALC_STEREO_SOURCES
;
2066 data
[i
++] = device
->NumStereoSources
;
2068 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
2069 data
[i
++] = device
->NumAuxSends
;
2076 *data
= device
->Frequency
;
2080 if(device
->Type
== Loopback
)
2081 alcSetError(device
, ALC_INVALID_DEVICE
);
2083 *data
= device
->Frequency
/ device
->UpdateSize
;
2087 if(device
->Type
== Loopback
)
2088 alcSetError(device
, ALC_INVALID_DEVICE
);
2093 case ALC_FORMAT_CHANNELS_SOFT
:
2094 if(device
->Type
!= Loopback
)
2095 alcSetError(device
, ALC_INVALID_DEVICE
);
2097 *data
= device
->FmtChans
;
2100 case ALC_FORMAT_TYPE_SOFT
:
2101 if(device
->Type
!= Loopback
)
2102 alcSetError(device
, ALC_INVALID_DEVICE
);
2104 *data
= device
->FmtType
;
2107 case ALC_MONO_SOURCES
:
2108 *data
= device
->NumMonoSources
;
2111 case ALC_STEREO_SOURCES
:
2112 *data
= device
->NumStereoSources
;
2115 case ALC_MAX_AUXILIARY_SENDS
:
2116 *data
= device
->NumAuxSends
;
2120 *data
= device
->Connected
;
2124 alcSetError(device
, ALC_INVALID_ENUM
);
2129 ALCdevice_DecRef(device
);
2133 /* alcIsExtensionPresent
2135 * Determines if there is support for a particular extension
2137 ALC_API ALCboolean ALC_APIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
2139 ALCboolean bResult
= ALC_FALSE
;
2141 device
= VerifyDevice(device
);
2144 alcSetError(device
, ALC_INVALID_VALUE
);
2147 size_t len
= strlen(extName
);
2148 const char *ptr
= (device
? alcExtensionList
: alcNoDeviceExtList
);
2151 if(strncasecmp(ptr
, extName
, len
) == 0 &&
2152 (ptr
[len
] == '\0' || isspace(ptr
[len
])))
2157 if((ptr
=strchr(ptr
, ' ')) != NULL
)
2161 } while(isspace(*ptr
));
2166 ALCdevice_DecRef(device
);
2171 /* alcGetProcAddress
2173 * Retrieves the function address for a particular extension function
2175 ALC_API ALCvoid
* ALC_APIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
2177 ALCvoid
*ptr
= NULL
;
2179 device
= VerifyDevice(device
);
2182 alcSetError(device
, ALC_INVALID_VALUE
);
2186 while(alcFunctions
[i
].funcName
&& strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
2188 ptr
= alcFunctions
[i
].address
;
2191 ALCdevice_DecRef(device
);
2198 * Get the value for a particular ALC Enumerated Value
2200 ALC_API ALCenum ALC_APIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
2204 device
= VerifyDevice(device
);
2207 alcSetError(device
, ALC_INVALID_VALUE
);
2211 while(enumeration
[i
].enumName
&& strcmp(enumeration
[i
].enumName
,enumName
) != 0)
2213 val
= enumeration
[i
].value
;
2216 ALCdevice_DecRef(device
);
2223 * Create and attach a Context to a particular Device.
2225 ALC_API ALCcontext
* ALC_APIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
2227 ALCcontext
*ALContext
;
2231 if(!(device
=VerifyDevice(device
)) || device
->Type
== Capture
|| !device
->Connected
)
2234 alcSetError(device
, ALC_INVALID_DEVICE
);
2235 if(device
) ALCdevice_DecRef(device
);
2239 /* Reset Context Last Error code */
2240 device
->LastError
= ALC_NO_ERROR
;
2242 if((err
=UpdateDeviceParams(device
, attrList
)) != ALC_NO_ERROR
)
2245 alcSetError(device
, err
);
2246 if(err
== ALC_INVALID_DEVICE
)
2247 aluHandleDisconnect(device
);
2248 ALCdevice_DecRef(device
);
2252 ALContext
= calloc(1, sizeof(ALCcontext
));
2257 ALContext
->MaxActiveSources
= 256;
2258 ALContext
->ActiveSources
= malloc(sizeof(ALContext
->ActiveSources
[0]) *
2259 ALContext
->MaxActiveSources
);
2261 if(!ALContext
|| !ALContext
->ActiveSources
)
2263 if(!device
->ContextList
)
2265 ALCdevice_StopPlayback(device
);
2266 device
->Flags
&= ~DEVICE_RUNNING
;
2273 alcSetError(device
, ALC_OUT_OF_MEMORY
);
2274 ALCdevice_DecRef(device
);
2278 ALContext
->Device
= device
;
2279 ALCdevice_IncRef(device
);
2280 InitContext(ALContext
);
2283 ALContext
->next
= device
->ContextList
;
2284 } while(!CompExchangePtr((XchgPtr
*)&device
->ContextList
, ALContext
->next
, ALContext
));
2287 if(device
->DefaultSlot
)
2288 InitializeEffect(ALContext
, device
->DefaultSlot
, &ForcedEffect
);
2289 ALContext
->LastError
= AL_NO_ERROR
;
2291 ALCdevice_DecRef(device
);
2293 TRACE("Created context %p\n", ALContext
);
2297 /* alcDestroyContext
2301 ALC_API ALCvoid ALC_APIENTRY
alcDestroyContext(ALCcontext
*context
)
2306 /* alcGetContextsDevice sets an error for invalid contexts */
2307 Device
= alcGetContextsDevice(context
);
2310 ReleaseContext(context
, Device
);
2311 if(!Device
->ContextList
)
2313 ALCdevice_StopPlayback(Device
);
2314 Device
->Flags
&= ~DEVICE_RUNNING
;
2321 /* alcGetCurrentContext
2323 * Returns the currently active Context
2325 ALC_API ALCcontext
* ALC_APIENTRY
alcGetCurrentContext(ALCvoid
)
2327 ALCcontext
*Context
;
2329 Context
= pthread_getspecific(LocalContext
);
2330 if(!Context
) Context
= GlobalContext
;
2335 /* alcGetThreadContext
2337 * Returns the currently active thread-local Context
2339 ALC_API ALCcontext
* ALC_APIENTRY
alcGetThreadContext(void)
2341 ALCcontext
*Context
;
2342 Context
= pthread_getspecific(LocalContext
);
2347 /* alcMakeContextCurrent
2349 * Makes the given Context the active Context
2351 ALC_API ALCboolean ALC_APIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
2353 /* context must be a valid Context or NULL */
2354 if(context
&& !(context
=VerifyContext(context
)))
2356 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2359 /* context's reference count is already incremented */
2360 context
= ExchangePtr((XchgPtr
*)&GlobalContext
, context
);
2361 if(context
) ALCcontext_DecRef(context
);
2363 if((context
=pthread_getspecific(LocalContext
)) != NULL
)
2365 pthread_setspecific(LocalContext
, NULL
);
2366 ALCcontext_DecRef(context
);
2372 /* alcSetThreadContext
2374 * Makes the given Context the active Context for the current thread
2376 ALC_API ALCboolean ALC_APIENTRY
alcSetThreadContext(ALCcontext
*context
)
2380 /* context must be a valid Context or NULL */
2381 if(context
&& !(context
=VerifyContext(context
)))
2383 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2386 /* context's reference count is already incremented */
2387 old
= pthread_getspecific(LocalContext
);
2388 pthread_setspecific(LocalContext
, context
);
2389 if(old
) ALCcontext_DecRef(old
);
2395 /* alcGetContextsDevice
2397 * Returns the Device that a particular Context is attached to
2399 ALC_API ALCdevice
* ALC_APIENTRY
alcGetContextsDevice(ALCcontext
*Context
)
2403 if(!(Context
=VerifyContext(Context
)))
2405 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2408 Device
= Context
->Device
;
2409 ALCcontext_DecRef(Context
);
2417 * Open the Device specified.
2419 ALC_API ALCdevice
* ALC_APIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
2427 if(!PlaybackBackend
.name
)
2429 alcSetError(NULL
, ALC_INVALID_VALUE
);
2433 if(deviceName
&& (!deviceName
[0] || strcasecmp(deviceName
, alcDefaultName
) == 0 || strcasecmp(deviceName
, "openal-soft") == 0))
2436 device
= calloc(1, sizeof(ALCdevice
)+sizeof(ALeffectslot
));
2439 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
2444 device
->Funcs
= &PlaybackBackend
.Funcs
;
2446 device
->Connected
= ALC_TRUE
;
2447 device
->Type
= Playback
;
2448 InitializeCriticalSection(&device
->Mutex
);
2449 device
->LastError
= ALC_NO_ERROR
;
2452 device
->Bs2b
= NULL
;
2453 device
->Bs2bLevel
= 0;
2454 device
->szDeviceName
= NULL
;
2456 device
->ContextList
= NULL
;
2458 device
->MaxNoOfSources
= 256;
2459 device
->AuxiliaryEffectSlotMax
= 4;
2460 device
->NumAuxSends
= MAX_SENDS
;
2462 InitUIntMap(&device
->BufferMap
, ~0);
2463 InitUIntMap(&device
->EffectMap
, ~0);
2464 InitUIntMap(&device
->FilterMap
, ~0);
2467 device
->FmtChans
= DevFmtStereo
;
2468 device
->FmtType
= DevFmtFloat
;
2469 device
->Frequency
= DEFAULT_OUTPUT_RATE
;
2470 device
->NumUpdates
= 4;
2471 device
->UpdateSize
= 1024;
2473 if(ConfigValueStr(NULL
, "channels", &fmt
))
2475 static const struct {
2476 const char name
[16];
2477 enum DevFmtChannels chans
;
2479 { "mono", DevFmtMono
},
2480 { "stereo", DevFmtStereo
},
2481 { "quad", DevFmtQuad
},
2482 { "surround51", DevFmtX51
},
2483 { "surround61", DevFmtX61
},
2484 { "surround71", DevFmtX71
},
2488 for(i
= 0;i
< COUNTOF(chanlist
);i
++)
2490 if(strcasecmp(chanlist
[i
].name
, fmt
) == 0)
2492 device
->FmtChans
= chanlist
[i
].chans
;
2493 device
->Flags
|= DEVICE_CHANNELS_REQUEST
;
2497 if(i
== COUNTOF(chanlist
))
2498 ERR("Unsupported channels: %s\n", fmt
);
2500 if(ConfigValueStr(NULL
, "sample-type", &fmt
))
2502 static const struct {
2503 const char name
[16];
2504 enum DevFmtType type
;
2506 { "int8", DevFmtByte
},
2507 { "uint8", DevFmtUByte
},
2508 { "int16", DevFmtShort
},
2509 { "uint16", DevFmtUShort
},
2510 { "int32", DevFmtInt
},
2511 { "uint32", DevFmtUInt
},
2512 { "float32", DevFmtFloat
},
2516 for(i
= 0;i
< COUNTOF(typelist
);i
++)
2518 if(strcasecmp(typelist
[i
].name
, fmt
) == 0)
2520 device
->FmtType
= typelist
[i
].type
;
2521 device
->Flags
|= DEVICE_SAMPLE_TYPE_REQUEST
;
2525 if(i
== COUNTOF(typelist
))
2526 ERR("Unsupported sample-type: %s\n", fmt
);
2528 #define DEVICE_FORMAT_REQUEST (DEVICE_CHANNELS_REQUEST|DEVICE_SAMPLE_TYPE_REQUEST)
2529 if((device
->Flags
&DEVICE_FORMAT_REQUEST
) != DEVICE_FORMAT_REQUEST
&&
2530 ConfigValueStr(NULL
, "format", &fmt
))
2532 static const struct {
2533 const char name
[32];
2534 enum DevFmtChannels channels
;
2535 enum DevFmtType type
;
2537 { "AL_FORMAT_MONO32", DevFmtMono
, DevFmtFloat
},
2538 { "AL_FORMAT_STEREO32", DevFmtStereo
, DevFmtFloat
},
2539 { "AL_FORMAT_QUAD32", DevFmtQuad
, DevFmtFloat
},
2540 { "AL_FORMAT_51CHN32", DevFmtX51
, DevFmtFloat
},
2541 { "AL_FORMAT_61CHN32", DevFmtX61
, DevFmtFloat
},
2542 { "AL_FORMAT_71CHN32", DevFmtX71
, DevFmtFloat
},
2544 { "AL_FORMAT_MONO16", DevFmtMono
, DevFmtShort
},
2545 { "AL_FORMAT_STEREO16", DevFmtStereo
, DevFmtShort
},
2546 { "AL_FORMAT_QUAD16", DevFmtQuad
, DevFmtShort
},
2547 { "AL_FORMAT_51CHN16", DevFmtX51
, DevFmtShort
},
2548 { "AL_FORMAT_61CHN16", DevFmtX61
, DevFmtShort
},
2549 { "AL_FORMAT_71CHN16", DevFmtX71
, DevFmtShort
},
2551 { "AL_FORMAT_MONO8", DevFmtMono
, DevFmtByte
},
2552 { "AL_FORMAT_STEREO8", DevFmtStereo
, DevFmtByte
},
2553 { "AL_FORMAT_QUAD8", DevFmtQuad
, DevFmtByte
},
2554 { "AL_FORMAT_51CHN8", DevFmtX51
, DevFmtByte
},
2555 { "AL_FORMAT_61CHN8", DevFmtX61
, DevFmtByte
},
2556 { "AL_FORMAT_71CHN8", DevFmtX71
, DevFmtByte
}
2560 ERR("Option 'format' is deprecated, please use 'channels' and 'sample-type'\n");
2561 for(i
= 0;i
< COUNTOF(formats
);i
++)
2563 if(strcasecmp(fmt
, formats
[i
].name
) == 0)
2565 if(!(device
->Flags
&DEVICE_CHANNELS_REQUEST
))
2566 device
->FmtChans
= formats
[i
].channels
;
2567 if(!(device
->Flags
&DEVICE_SAMPLE_TYPE_REQUEST
))
2568 device
->FmtType
= formats
[i
].type
;
2569 device
->Flags
|= DEVICE_FORMAT_REQUEST
;
2573 if(i
== COUNTOF(formats
))
2574 ERR("Unsupported format: %s\n", fmt
);
2576 #undef DEVICE_FORMAT_REQUEST
2578 if(ConfigValueUInt(NULL
, "frequency", &device
->Frequency
))
2580 device
->Flags
|= DEVICE_FREQUENCY_REQUEST
;
2581 if(device
->Frequency
< MIN_OUTPUT_RATE
)
2582 ERR("%uhz request clamped to %uhz minimum\n", device
->Frequency
, MIN_OUTPUT_RATE
);
2583 device
->Frequency
= maxu(device
->Frequency
, MIN_OUTPUT_RATE
);
2586 ConfigValueUInt(NULL
, "periods", &device
->NumUpdates
);
2587 device
->NumUpdates
= clampu(device
->NumUpdates
, 2, 16);
2589 ConfigValueUInt(NULL
, "period_size", &device
->UpdateSize
);
2590 device
->UpdateSize
= clampu(device
->UpdateSize
, 64, 8192);
2592 ConfigValueUInt(NULL
, "sources", &device
->MaxNoOfSources
);
2593 if(device
->MaxNoOfSources
== 0) device
->MaxNoOfSources
= 256;
2595 ConfigValueUInt(NULL
, "slots", &device
->AuxiliaryEffectSlotMax
);
2596 if(device
->AuxiliaryEffectSlotMax
== 0) device
->AuxiliaryEffectSlotMax
= 4;
2598 ConfigValueUInt(NULL
, "sends", &device
->NumAuxSends
);
2599 if(device
->NumAuxSends
> MAX_SENDS
) device
->NumAuxSends
= MAX_SENDS
;
2601 ConfigValueInt(NULL
, "cf_level", &device
->Bs2bLevel
);
2603 device
->NumStereoSources
= 1;
2604 device
->NumMonoSources
= device
->MaxNoOfSources
- device
->NumStereoSources
;
2606 if(ForcedEffect
.type
!= AL_EFFECT_NULL
)
2608 device
->DefaultSlot
= (ALeffectslot
*)(device
+1);
2609 if(InitEffectSlot(device
->DefaultSlot
) != AL_NO_ERROR
)
2610 device
->DefaultSlot
= NULL
;
2613 // Find a playback device to open
2615 if((err
=ALCdevice_OpenPlayback(device
, deviceName
)) != ALC_NO_ERROR
)
2618 DeleteCriticalSection(&device
->Mutex
);
2620 alcSetError(NULL
, err
);
2626 device
->next
= DeviceList
;
2627 } while(!CompExchangePtr((XchgPtr
*)&DeviceList
, device
->next
, device
));
2629 TRACE("Created device %p, \"%s\"\n", device
, device
->szDeviceName
);
2635 * Close the specified Device
2637 ALC_API ALCboolean ALC_APIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
2639 ALCdevice
*volatile*list
;
2644 while(*list
&& *list
!= pDevice
)
2645 list
= &(*list
)->next
;
2647 if(!*list
|| (*list
)->Type
== Capture
)
2649 alcSetError(*list
, ALC_INVALID_DEVICE
);
2654 *list
= (*list
)->next
;
2657 while((ctx
=pDevice
->ContextList
) != NULL
)
2659 WARN("Releasing context %p\n", ctx
);
2660 ReleaseContext(ctx
, pDevice
);
2662 if((pDevice
->Flags
&DEVICE_RUNNING
))
2663 ALCdevice_StopPlayback(pDevice
);
2664 pDevice
->Flags
&= ~DEVICE_RUNNING
;
2666 ALCdevice_ClosePlayback(pDevice
);
2668 ALCdevice_DecRef(pDevice
);
2674 /* alcLoopbackOpenDeviceSOFT
2676 * Open a loopback device, for manual rendering.
2678 ALC_API ALCdevice
* ALC_APIENTRY
alcLoopbackOpenDeviceSOFT(const ALCchar
*deviceName
)
2684 /* Make sure the device name, if specified, is us. */
2685 if(deviceName
&& strcmp(deviceName
, alcDefaultName
) != 0)
2687 alcSetError(NULL
, ALC_INVALID_VALUE
);
2691 device
= calloc(1, sizeof(ALCdevice
));
2694 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
2699 device
->Funcs
= &BackendLoopback
.Funcs
;
2701 device
->Connected
= ALC_TRUE
;
2702 device
->Type
= Loopback
;
2703 InitializeCriticalSection(&device
->Mutex
);
2704 device
->LastError
= ALC_NO_ERROR
;
2707 device
->Bs2b
= NULL
;
2708 device
->Bs2bLevel
= 0;
2709 device
->szDeviceName
= NULL
;
2711 device
->ContextList
= NULL
;
2713 device
->MaxNoOfSources
= 256;
2714 device
->AuxiliaryEffectSlotMax
= 4;
2715 device
->NumAuxSends
= MAX_SENDS
;
2717 InitUIntMap(&device
->BufferMap
, ~0);
2718 InitUIntMap(&device
->EffectMap
, ~0);
2719 InitUIntMap(&device
->FilterMap
, ~0);
2722 device
->NumUpdates
= 0;
2723 device
->UpdateSize
= 0;
2725 device
->Frequency
= 44100;
2726 device
->FmtChans
= DevFmtStereo
;
2727 device
->FmtType
= DevFmtShort
;
2729 ConfigValueUInt(NULL
, "sources", &device
->MaxNoOfSources
);
2730 if(device
->MaxNoOfSources
== 0) device
->MaxNoOfSources
= 256;
2732 ConfigValueUInt(NULL
, "slots", &device
->AuxiliaryEffectSlotMax
);
2733 if(device
->AuxiliaryEffectSlotMax
== 0) device
->AuxiliaryEffectSlotMax
= 4;
2735 ConfigValueUInt(NULL
, "sends", &device
->NumAuxSends
);
2736 if(device
->NumAuxSends
> MAX_SENDS
) device
->NumAuxSends
= MAX_SENDS
;
2738 device
->NumStereoSources
= 1;
2739 device
->NumMonoSources
= device
->MaxNoOfSources
- device
->NumStereoSources
;
2741 // Open the "backend"
2742 ALCdevice_OpenPlayback(device
, "Loopback");
2744 device
->next
= DeviceList
;
2745 } while(!CompExchangePtr((XchgPtr
*)&DeviceList
, device
->next
, device
));
2747 TRACE("Created device %p\n", device
);
2751 /* alcIsRenderFormatSupportedSOFT
2753 * Determines if the loopback device supports the given format for rendering.
2755 ALC_API ALCboolean ALC_APIENTRY
alcIsRenderFormatSupportedSOFT(ALCdevice
*device
, ALCsizei freq
, ALCenum channels
, ALCenum type
)
2757 ALCboolean ret
= ALC_FALSE
;
2759 if(!(device
=VerifyDevice(device
)) || device
->Type
!= Loopback
)
2760 alcSetError(device
, ALC_INVALID_DEVICE
);
2762 alcSetError(device
, ALC_INVALID_VALUE
);
2763 else if(!IsValidALCType(type
) || !IsValidALCChannels(channels
))
2764 alcSetError(device
, ALC_INVALID_ENUM
);
2767 if(BytesFromDevFmt(type
) > 0 && ChannelsFromDevFmt(channels
) > 0 &&
2768 freq
>= MIN_OUTPUT_RATE
)
2771 if(device
) ALCdevice_DecRef(device
);
2776 /* alcRenderSamplesSOFT
2778 * Renders some samples into a buffer, using the format last set by the
2779 * attributes given to alcCreateContext.
2781 ALC_API
void ALC_APIENTRY
alcRenderSamplesSOFT(ALCdevice
*device
, ALCvoid
*buffer
, ALCsizei samples
)
2783 if(!(device
=VerifyDevice(device
)) || device
->Type
!= Loopback
)
2784 alcSetError(device
, ALC_INVALID_DEVICE
);
2785 else if(samples
< 0 || (samples
> 0 && buffer
== NULL
))
2786 alcSetError(device
, ALC_INVALID_VALUE
);
2788 aluMixData(device
, buffer
, samples
);
2789 if(device
) ALCdevice_DecRef(device
);
2793 static void ReleaseALC(void)
2797 free(alcAllDeviceList
); alcAllDeviceList
= NULL
;
2798 alcAllDeviceListSize
= 0;
2799 free(alcCaptureDeviceList
); alcCaptureDeviceList
= NULL
;
2800 alcCaptureDeviceListSize
= 0;
2802 free(alcDefaultAllDeviceSpecifier
);
2803 alcDefaultAllDeviceSpecifier
= NULL
;
2804 free(alcCaptureDefaultDeviceSpecifier
);
2805 alcCaptureDefaultDeviceSpecifier
= NULL
;
2807 if((dev
=ExchangePtr((XchgPtr
*)&DeviceList
, NULL
)) != NULL
)
2812 } while((dev
=dev
->next
) != NULL
);
2813 ERR("%u device%s not closed\n", num
, (num
>1)?"s":"");
2817 ///////////////////////////////////////////////////////