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_SOFT_loopback";
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_SOFT_loopback";
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
*volatile GlobalContext
= NULL
;
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 DefaultEffect
;
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(&DefaultEffect
);
726 str
= getenv("ALSOFT_DEFAULT_REVERB");
727 if((str
&& str
[0]) || ConfigValueStr(NULL
, "default-reverb", &str
))
728 LoadReverbPreset(str
, &DefaultEffect
);
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
, device
, 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 if(device
->DefaultSlot
)
1335 ALeffectslot
*slot
= device
->DefaultSlot
;
1337 if(ALeffectState_DeviceUpdate(slot
->EffectState
, device
) == AL_FALSE
)
1339 UnlockDevice(device
);
1340 RestoreFPUMode(oldMode
);
1341 return ALC_INVALID_DEVICE
;
1343 slot
->NeedsUpdate
= AL_FALSE
;
1344 ALeffectState_Update(slot
->EffectState
, device
, slot
);
1346 UnlockDevice(device
);
1347 RestoreFPUMode(oldMode
);
1349 if(ALCdevice_StartPlayback(device
) == ALC_FALSE
)
1350 return ALC_INVALID_DEVICE
;
1351 device
->Flags
|= DEVICE_RUNNING
;
1353 return ALC_NO_ERROR
;
1358 * Frees the device structure, and destroys any objects the app failed to
1359 * delete. Called once there's no more references on the device.
1361 static ALCvoid
FreeDevice(ALCdevice
*device
)
1363 TRACE("%p\n", device
);
1365 if(device
->DefaultSlot
)
1367 ALeffectState_Destroy(device
->DefaultSlot
->EffectState
);
1368 device
->DefaultSlot
->EffectState
= NULL
;
1371 if(device
->BufferMap
.size
> 0)
1373 WARN("(%p) Deleting %d Buffer(s)\n", device
, device
->BufferMap
.size
);
1374 ReleaseALBuffers(device
);
1376 ResetUIntMap(&device
->BufferMap
);
1378 if(device
->EffectMap
.size
> 0)
1380 WARN("(%p) Deleting %d Effect(s)\n", device
, device
->EffectMap
.size
);
1381 ReleaseALEffects(device
);
1383 ResetUIntMap(&device
->EffectMap
);
1385 if(device
->FilterMap
.size
> 0)
1387 WARN("(%p) Deleting %d Filter(s)\n", device
, device
->FilterMap
.size
);
1388 ReleaseALFilters(device
);
1390 ResetUIntMap(&device
->FilterMap
);
1393 device
->Bs2b
= NULL
;
1395 free(device
->szDeviceName
);
1396 device
->szDeviceName
= NULL
;
1398 DeleteCriticalSection(&device
->Mutex
);
1404 void ALCdevice_IncRef(ALCdevice
*device
)
1407 ref
= IncrementRef(&device
->ref
);
1408 TRACEREF("%p increasing refcount to %u\n", device
, ref
);
1411 void ALCdevice_DecRef(ALCdevice
*device
)
1414 ref
= DecrementRef(&device
->ref
);
1415 TRACEREF("%p decreasing refcount to %u\n", device
, ref
);
1416 if(ref
== 0) FreeDevice(device
);
1421 * Checks if the device handle is valid, and increments its ref count if so.
1423 static ALCdevice
*VerifyDevice(ALCdevice
*device
)
1425 ALCdevice
*tmpDevice
;
1431 tmpDevice
= DeviceList
;
1432 while(tmpDevice
&& tmpDevice
!= device
)
1433 tmpDevice
= tmpDevice
->next
;
1436 ALCdevice_IncRef(tmpDevice
);
1444 * Initializes context variables
1446 static ALvoid
InitContext(ALCcontext
*pContext
)
1450 //Initialise listener
1451 pContext
->Listener
.Gain
= 1.0f
;
1452 pContext
->Listener
.MetersPerUnit
= 1.0f
;
1453 pContext
->Listener
.Position
[0] = 0.0f
;
1454 pContext
->Listener
.Position
[1] = 0.0f
;
1455 pContext
->Listener
.Position
[2] = 0.0f
;
1456 pContext
->Listener
.Velocity
[0] = 0.0f
;
1457 pContext
->Listener
.Velocity
[1] = 0.0f
;
1458 pContext
->Listener
.Velocity
[2] = 0.0f
;
1459 pContext
->Listener
.Forward
[0] = 0.0f
;
1460 pContext
->Listener
.Forward
[1] = 0.0f
;
1461 pContext
->Listener
.Forward
[2] = -1.0f
;
1462 pContext
->Listener
.Up
[0] = 0.0f
;
1463 pContext
->Listener
.Up
[1] = 1.0f
;
1464 pContext
->Listener
.Up
[2] = 0.0f
;
1465 for(i
= 0;i
< 4;i
++)
1467 for(j
= 0;j
< 4;j
++)
1468 pContext
->Listener
.Matrix
[i
][j
] = ((i
==j
) ? 1.0f
: 0.0f
);
1472 pContext
->LastError
= AL_NO_ERROR
;
1473 pContext
->UpdateSources
= AL_FALSE
;
1474 pContext
->ActiveSourceCount
= 0;
1475 InitUIntMap(&pContext
->SourceMap
, pContext
->Device
->MaxNoOfSources
);
1476 InitUIntMap(&pContext
->EffectSlotMap
, pContext
->Device
->AuxiliaryEffectSlotMax
);
1479 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
1480 pContext
->SourceDistanceModel
= AL_FALSE
;
1481 pContext
->DopplerFactor
= 1.0f
;
1482 pContext
->DopplerVelocity
= 1.0f
;
1483 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
1484 pContext
->DeferUpdates
= AL_FALSE
;
1486 pContext
->ExtensionList
= alExtList
;
1492 * Cleans up the context, and destroys any remaining objects the app failed to
1493 * delete. Called once there's no more references on the context.
1495 static ALCvoid
FreeContext(ALCcontext
*context
)
1497 TRACE("%p\n", context
);
1499 if(context
->SourceMap
.size
> 0)
1501 ERR("(%p) Deleting %d Source(s)\n", context
, context
->SourceMap
.size
);
1502 ReleaseALSources(context
);
1504 ResetUIntMap(&context
->SourceMap
);
1506 if(context
->EffectSlotMap
.size
> 0)
1508 ERR("(%p) Deleting %d AuxiliaryEffectSlot(s)\n", context
, context
->EffectSlotMap
.size
);
1509 ReleaseALAuxiliaryEffectSlots(context
);
1511 ResetUIntMap(&context
->EffectSlotMap
);
1513 context
->ActiveSourceCount
= 0;
1514 free(context
->ActiveSources
);
1515 context
->ActiveSources
= NULL
;
1516 context
->MaxActiveSources
= 0;
1518 context
->ActiveEffectSlotCount
= 0;
1519 free(context
->ActiveEffectSlots
);
1520 context
->ActiveEffectSlots
= NULL
;
1521 context
->MaxActiveEffectSlots
= 0;
1523 ALCdevice_DecRef(context
->Device
);
1524 context
->Device
= NULL
;
1526 //Invalidate context
1527 memset(context
, 0, sizeof(ALCcontext
));
1533 * Removes the context reference from the given device and removes it from
1534 * being current on the running thread or globally.
1536 static void ReleaseContext(ALCcontext
*context
, ALCdevice
*device
)
1538 ALCcontext
*volatile*tmp_ctx
;
1540 if(pthread_getspecific(LocalContext
) == context
)
1542 WARN("%p released while current on thread\n", context
);
1543 pthread_setspecific(LocalContext
, NULL
);
1544 ALCcontext_DecRef(context
);
1547 if(CompExchangePtr((XchgPtr
*)&GlobalContext
, context
, NULL
))
1548 ALCcontext_DecRef(context
);
1551 tmp_ctx
= &device
->ContextList
;
1554 if(CompExchangePtr((XchgPtr
*)tmp_ctx
, context
, context
->next
))
1556 tmp_ctx
= &(*tmp_ctx
)->next
;
1558 UnlockDevice(device
);
1560 ALCcontext_DecRef(context
);
1563 void ALCcontext_IncRef(ALCcontext
*context
)
1566 ref
= IncrementRef(&context
->ref
);
1567 TRACEREF("%p increasing refcount to %u\n", context
, ref
);
1570 void ALCcontext_DecRef(ALCcontext
*context
)
1573 ref
= DecrementRef(&context
->ref
);
1574 TRACEREF("%p decreasing refcount to %u\n", context
, ref
);
1575 if(ref
== 0) FreeContext(context
);
1578 static void ReleaseThreadCtx(void *ptr
)
1580 WARN("%p current for thread being destroyed\n", ptr
);
1581 ALCcontext_DecRef(ptr
);
1586 * Checks that the given context is valid, and increments its reference count.
1588 static ALCcontext
*VerifyContext(ALCcontext
*context
)
1596 ALCcontext
*tmp_ctx
= dev
->ContextList
;
1599 if(tmp_ctx
== context
)
1601 ALCcontext_IncRef(tmp_ctx
);
1605 tmp_ctx
= tmp_ctx
->next
;
1617 * Returns the currently active context, and adds a reference without locking
1620 ALCcontext
*GetContextRef(void)
1622 ALCcontext
*context
;
1624 context
= pthread_getspecific(LocalContext
);
1626 ALCcontext_IncRef(context
);
1630 context
= GlobalContext
;
1632 ALCcontext_IncRef(context
);
1639 ///////////////////////////////////////////////////////
1642 ///////////////////////////////////////////////////////
1643 // ALC Functions calls
1646 // This should probably move to another c file but for now ...
1647 ALC_API ALCdevice
* ALC_APIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
1649 ALCdevice
*device
= NULL
;
1654 if(!CaptureBackend
.name
)
1656 alcSetError(NULL
, ALC_INVALID_VALUE
);
1662 alcSetError(NULL
, ALC_INVALID_VALUE
);
1666 if(deviceName
&& (!deviceName
[0] || strcasecmp(deviceName
, alcDefaultName
) == 0 || strcasecmp(deviceName
, "openal-soft") == 0))
1669 device
= calloc(1, sizeof(ALCdevice
));
1672 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
1677 device
->Funcs
= &CaptureBackend
.Funcs
;
1679 device
->Connected
= ALC_TRUE
;
1680 device
->Type
= Capture
;
1681 InitializeCriticalSection(&device
->Mutex
);
1683 InitUIntMap(&device
->BufferMap
, ~0);
1684 InitUIntMap(&device
->EffectMap
, ~0);
1685 InitUIntMap(&device
->FilterMap
, ~0);
1687 device
->szDeviceName
= NULL
;
1689 device
->Flags
|= DEVICE_FREQUENCY_REQUEST
;
1690 device
->Frequency
= frequency
;
1692 device
->Flags
|= DEVICE_CHANNELS_REQUEST
| DEVICE_SAMPLE_TYPE_REQUEST
;
1693 if(DecomposeDevFormat(format
, &device
->FmtChans
, &device
->FmtType
) == AL_FALSE
)
1695 DeleteCriticalSection(&device
->Mutex
);
1697 alcSetError(NULL
, ALC_INVALID_ENUM
);
1701 device
->UpdateSize
= SampleSize
;
1702 device
->NumUpdates
= 1;
1705 if((err
=ALCdevice_OpenCapture(device
, deviceName
)) != ALC_NO_ERROR
)
1708 DeleteCriticalSection(&device
->Mutex
);
1710 alcSetError(NULL
, err
);
1716 device
->next
= DeviceList
;
1717 } while(!CompExchangePtr((XchgPtr
*)&DeviceList
, device
->next
, device
));
1719 TRACE("Created device %p\n", device
);
1723 ALC_API ALCboolean ALC_APIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
1725 ALCdevice
*volatile*list
;
1729 while(*list
&& *list
!= pDevice
)
1730 list
= &(*list
)->next
;
1732 if(!*list
|| (*list
)->Type
!= Capture
)
1734 alcSetError(*list
, ALC_INVALID_DEVICE
);
1739 *list
= (*list
)->next
;
1742 ALCdevice_CloseCapture(pDevice
);
1744 ALCdevice_DecRef(pDevice
);
1749 ALC_API
void ALC_APIENTRY
alcCaptureStart(ALCdevice
*device
)
1752 if(!(device
=VerifyDevice(device
)) || device
->Type
!= Capture
)
1755 alcSetError(device
, ALC_INVALID_DEVICE
);
1756 if(device
) ALCdevice_DecRef(device
);
1759 if(device
->Connected
)
1761 if(!(device
->Flags
&DEVICE_RUNNING
))
1762 ALCdevice_StartCapture(device
);
1763 device
->Flags
|= DEVICE_RUNNING
;
1767 ALCdevice_DecRef(device
);
1770 ALC_API
void ALC_APIENTRY
alcCaptureStop(ALCdevice
*device
)
1773 if(!(device
=VerifyDevice(device
)) || device
->Type
!= Capture
)
1776 alcSetError(device
, ALC_INVALID_DEVICE
);
1777 if(device
) ALCdevice_DecRef(device
);
1780 if((device
->Flags
&DEVICE_RUNNING
))
1781 ALCdevice_StopCapture(device
);
1782 device
->Flags
&= ~DEVICE_RUNNING
;
1785 ALCdevice_DecRef(device
);
1788 ALC_API
void ALC_APIENTRY
alcCaptureSamples(ALCdevice
*device
, ALCvoid
*buffer
, ALCsizei samples
)
1790 ALCenum err
= ALC_INVALID_DEVICE
;
1792 if((device
=VerifyDevice(device
)) != NULL
&& device
->Type
== Capture
)
1794 err
= ALC_INVALID_VALUE
;
1795 if(samples
>= 0 && ALCdevice_AvailableSamples(device
) >= (ALCuint
)samples
)
1796 err
= ALCdevice_CaptureSamples(device
, buffer
, samples
);
1799 if(err
!= ALC_NO_ERROR
)
1800 alcSetError(device
, err
);
1801 if(device
) ALCdevice_DecRef(device
);
1807 Return last ALC generated error code
1809 ALC_API ALCenum ALC_APIENTRY
alcGetError(ALCdevice
*device
)
1813 if(VerifyDevice(device
))
1815 errorCode
= ExchangeInt(&device
->LastError
, ALC_NO_ERROR
);
1816 ALCdevice_DecRef(device
);
1819 errorCode
= ExchangeInt(&g_eLastNullDeviceError
, ALC_NO_ERROR
);
1825 /* alcSuspendContext
1829 ALC_API ALCvoid ALC_APIENTRY
alcSuspendContext(ALCcontext
*Context
)
1834 /* alcProcessContext
1838 ALC_API ALCvoid ALC_APIENTRY
alcProcessContext(ALCcontext
*Context
)
1846 * Returns information about the Device, and error strings
1848 ALC_API
const ALCchar
* ALC_APIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
1850 const ALCchar
*value
= NULL
;
1858 case ALC_INVALID_ENUM
:
1859 value
= alcErrInvalidEnum
;
1862 case ALC_INVALID_VALUE
:
1863 value
= alcErrInvalidValue
;
1866 case ALC_INVALID_DEVICE
:
1867 value
= alcErrInvalidDevice
;
1870 case ALC_INVALID_CONTEXT
:
1871 value
= alcErrInvalidContext
;
1874 case ALC_OUT_OF_MEMORY
:
1875 value
= alcErrOutOfMemory
;
1878 case ALC_DEVICE_SPECIFIER
:
1879 value
= alcDefaultName
;
1882 case ALC_ALL_DEVICES_SPECIFIER
:
1883 if(VerifyDevice(pDevice
))
1885 value
= pDevice
->szDeviceName
;
1886 ALCdevice_DecRef(pDevice
);
1890 ProbeAllDeviceList();
1891 value
= alcAllDeviceList
;
1895 case ALC_CAPTURE_DEVICE_SPECIFIER
:
1896 if(VerifyDevice(pDevice
))
1898 value
= pDevice
->szDeviceName
;
1899 ALCdevice_DecRef(pDevice
);
1903 ProbeCaptureDeviceList();
1904 value
= alcCaptureDeviceList
;
1908 /* Default devices are always first in the list */
1909 case ALC_DEFAULT_DEVICE_SPECIFIER
:
1910 value
= alcDefaultName
;
1913 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
1914 if(!alcAllDeviceList
)
1915 ProbeAllDeviceList();
1917 pDevice
= VerifyDevice(pDevice
);
1919 free(alcDefaultAllDeviceSpecifier
);
1920 alcDefaultAllDeviceSpecifier
= strdup(alcAllDeviceList
?
1921 alcAllDeviceList
: "");
1922 if(!alcDefaultAllDeviceSpecifier
)
1923 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1925 value
= alcDefaultAllDeviceSpecifier
;
1926 if(pDevice
) ALCdevice_DecRef(pDevice
);
1929 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
1930 if(!alcCaptureDeviceList
)
1931 ProbeCaptureDeviceList();
1933 pDevice
= VerifyDevice(pDevice
);
1935 free(alcCaptureDefaultDeviceSpecifier
);
1936 alcCaptureDefaultDeviceSpecifier
= strdup(alcCaptureDeviceList
?
1937 alcCaptureDeviceList
: "");
1938 if(!alcCaptureDefaultDeviceSpecifier
)
1939 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1941 value
= alcCaptureDefaultDeviceSpecifier
;
1942 if(pDevice
) ALCdevice_DecRef(pDevice
);
1945 case ALC_EXTENSIONS
:
1946 if(!VerifyDevice(pDevice
))
1947 value
= alcNoDeviceExtList
;
1950 value
= alcExtensionList
;
1951 ALCdevice_DecRef(pDevice
);
1956 pDevice
= VerifyDevice(pDevice
);
1957 alcSetError(pDevice
, ALC_INVALID_ENUM
);
1958 if(pDevice
) ALCdevice_DecRef(pDevice
);
1968 * Returns information about the Device and the version of Open AL
1970 ALC_API ALCvoid ALC_APIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
1972 device
= VerifyDevice(device
);
1974 if(size
== 0 || data
== NULL
)
1976 alcSetError(device
, ALC_INVALID_VALUE
);
1977 if(device
) ALCdevice_DecRef(device
);
1985 case ALC_MAJOR_VERSION
:
1986 *data
= alcMajorVersion
;
1988 case ALC_MINOR_VERSION
:
1989 *data
= alcMinorVersion
;
1992 case ALC_ATTRIBUTES_SIZE
:
1993 case ALC_ALL_ATTRIBUTES
:
1997 case ALC_MONO_SOURCES
:
1998 case ALC_STEREO_SOURCES
:
1999 case ALC_CAPTURE_SAMPLES
:
2000 case ALC_FORMAT_CHANNELS_SOFT
:
2001 case ALC_FORMAT_TYPE_SOFT
:
2002 alcSetError(NULL
, ALC_INVALID_DEVICE
);
2006 alcSetError(NULL
, ALC_INVALID_ENUM
);
2010 else if(device
->Type
== Capture
)
2014 case ALC_CAPTURE_SAMPLES
:
2016 /* Re-validate the device since it may have been closed */
2017 ALCdevice_DecRef(device
);
2018 if((device
=VerifyDevice(device
)) != NULL
)
2019 *data
= ALCdevice_AvailableSamples(device
);
2021 alcSetError(NULL
, ALC_INVALID_DEVICE
);
2026 *data
= device
->Connected
;
2030 alcSetError(device
, ALC_INVALID_ENUM
);
2034 else /* render device */
2038 case ALC_MAJOR_VERSION
:
2039 *data
= alcMajorVersion
;
2042 case ALC_MINOR_VERSION
:
2043 *data
= alcMinorVersion
;
2046 case ALC_EFX_MAJOR_VERSION
:
2047 *data
= alcEFXMajorVersion
;
2050 case ALC_EFX_MINOR_VERSION
:
2051 *data
= alcEFXMinorVersion
;
2054 case ALC_ATTRIBUTES_SIZE
:
2058 case ALC_ALL_ATTRIBUTES
:
2060 alcSetError(device
, ALC_INVALID_VALUE
);
2065 data
[i
++] = ALC_FREQUENCY
;
2066 data
[i
++] = device
->Frequency
;
2068 if(device
->Type
!= Loopback
)
2070 data
[i
++] = ALC_REFRESH
;
2071 data
[i
++] = device
->Frequency
/ device
->UpdateSize
;
2073 data
[i
++] = ALC_SYNC
;
2074 data
[i
++] = ALC_FALSE
;
2078 data
[i
++] = ALC_FORMAT_CHANNELS_SOFT
;
2079 data
[i
++] = device
->FmtChans
;
2081 data
[i
++] = ALC_FORMAT_TYPE_SOFT
;
2082 data
[i
++] = device
->FmtType
;
2085 data
[i
++] = ALC_MONO_SOURCES
;
2086 data
[i
++] = device
->NumMonoSources
;
2088 data
[i
++] = ALC_STEREO_SOURCES
;
2089 data
[i
++] = device
->NumStereoSources
;
2091 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
2092 data
[i
++] = device
->NumAuxSends
;
2099 *data
= device
->Frequency
;
2103 if(device
->Type
== Loopback
)
2104 alcSetError(device
, ALC_INVALID_DEVICE
);
2106 *data
= device
->Frequency
/ device
->UpdateSize
;
2110 if(device
->Type
== Loopback
)
2111 alcSetError(device
, ALC_INVALID_DEVICE
);
2116 case ALC_FORMAT_CHANNELS_SOFT
:
2117 if(device
->Type
!= Loopback
)
2118 alcSetError(device
, ALC_INVALID_DEVICE
);
2120 *data
= device
->FmtChans
;
2123 case ALC_FORMAT_TYPE_SOFT
:
2124 if(device
->Type
!= Loopback
)
2125 alcSetError(device
, ALC_INVALID_DEVICE
);
2127 *data
= device
->FmtType
;
2130 case ALC_MONO_SOURCES
:
2131 *data
= device
->NumMonoSources
;
2134 case ALC_STEREO_SOURCES
:
2135 *data
= device
->NumStereoSources
;
2138 case ALC_MAX_AUXILIARY_SENDS
:
2139 *data
= device
->NumAuxSends
;
2143 *data
= device
->Connected
;
2147 alcSetError(device
, ALC_INVALID_ENUM
);
2152 ALCdevice_DecRef(device
);
2156 /* alcIsExtensionPresent
2158 * Determines if there is support for a particular extension
2160 ALC_API ALCboolean ALC_APIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
2162 ALCboolean bResult
= ALC_FALSE
;
2164 device
= VerifyDevice(device
);
2167 alcSetError(device
, ALC_INVALID_VALUE
);
2170 size_t len
= strlen(extName
);
2171 const char *ptr
= (device
? alcExtensionList
: alcNoDeviceExtList
);
2174 if(strncasecmp(ptr
, extName
, len
) == 0 &&
2175 (ptr
[len
] == '\0' || isspace(ptr
[len
])))
2180 if((ptr
=strchr(ptr
, ' ')) != NULL
)
2184 } while(isspace(*ptr
));
2189 ALCdevice_DecRef(device
);
2194 /* alcGetProcAddress
2196 * Retrieves the function address for a particular extension function
2198 ALC_API ALCvoid
* ALC_APIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
2200 ALCvoid
*ptr
= NULL
;
2202 device
= VerifyDevice(device
);
2205 alcSetError(device
, ALC_INVALID_VALUE
);
2209 while(alcFunctions
[i
].funcName
&& strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
2211 ptr
= alcFunctions
[i
].address
;
2214 ALCdevice_DecRef(device
);
2221 * Get the value for a particular ALC Enumerated Value
2223 ALC_API ALCenum ALC_APIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
2227 device
= VerifyDevice(device
);
2230 alcSetError(device
, ALC_INVALID_VALUE
);
2234 while(enumeration
[i
].enumName
&& strcmp(enumeration
[i
].enumName
,enumName
) != 0)
2236 val
= enumeration
[i
].value
;
2239 ALCdevice_DecRef(device
);
2246 * Create and attach a Context to a particular Device.
2248 ALC_API ALCcontext
* ALC_APIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
2250 ALCcontext
*ALContext
;
2254 if(!(device
=VerifyDevice(device
)) || device
->Type
== Capture
|| !device
->Connected
)
2257 alcSetError(device
, ALC_INVALID_DEVICE
);
2258 if(device
) ALCdevice_DecRef(device
);
2262 /* Reset Context Last Error code */
2263 device
->LastError
= ALC_NO_ERROR
;
2265 if((err
=UpdateDeviceParams(device
, attrList
)) != ALC_NO_ERROR
)
2268 alcSetError(device
, err
);
2269 if(err
== ALC_INVALID_DEVICE
)
2270 aluHandleDisconnect(device
);
2271 ALCdevice_DecRef(device
);
2275 ALContext
= calloc(1, sizeof(ALCcontext
));
2280 ALContext
->MaxActiveSources
= 256;
2281 ALContext
->ActiveSources
= malloc(sizeof(ALContext
->ActiveSources
[0]) *
2282 ALContext
->MaxActiveSources
);
2284 if(!ALContext
|| !ALContext
->ActiveSources
)
2286 if(!device
->ContextList
)
2288 ALCdevice_StopPlayback(device
);
2289 device
->Flags
&= ~DEVICE_RUNNING
;
2296 alcSetError(device
, ALC_OUT_OF_MEMORY
);
2297 ALCdevice_DecRef(device
);
2301 ALContext
->Device
= device
;
2302 ALCdevice_IncRef(device
);
2303 InitContext(ALContext
);
2306 ALContext
->next
= device
->ContextList
;
2307 } while(!CompExchangePtr((XchgPtr
*)&device
->ContextList
, ALContext
->next
, ALContext
));
2310 ALCdevice_DecRef(device
);
2312 TRACE("Created context %p\n", ALContext
);
2316 /* alcDestroyContext
2320 ALC_API ALCvoid ALC_APIENTRY
alcDestroyContext(ALCcontext
*context
)
2325 /* alcGetContextsDevice sets an error for invalid contexts */
2326 Device
= alcGetContextsDevice(context
);
2329 ReleaseContext(context
, Device
);
2330 if(!Device
->ContextList
)
2332 ALCdevice_StopPlayback(Device
);
2333 Device
->Flags
&= ~DEVICE_RUNNING
;
2340 /* alcGetCurrentContext
2342 * Returns the currently active Context
2344 ALC_API ALCcontext
* ALC_APIENTRY
alcGetCurrentContext(ALCvoid
)
2346 ALCcontext
*Context
;
2348 Context
= pthread_getspecific(LocalContext
);
2349 if(!Context
) Context
= GlobalContext
;
2354 /* alcGetThreadContext
2356 * Returns the currently active thread-local Context
2358 ALC_API ALCcontext
* ALC_APIENTRY
alcGetThreadContext(void)
2360 ALCcontext
*Context
;
2361 Context
= pthread_getspecific(LocalContext
);
2366 /* alcMakeContextCurrent
2368 * Makes the given Context the active Context
2370 ALC_API ALCboolean ALC_APIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
2372 /* context must be a valid Context or NULL */
2373 if(context
&& !(context
=VerifyContext(context
)))
2375 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2378 /* context's reference count is already incremented */
2379 context
= ExchangePtr((XchgPtr
*)&GlobalContext
, context
);
2380 if(context
) ALCcontext_DecRef(context
);
2382 if((context
=pthread_getspecific(LocalContext
)) != NULL
)
2384 pthread_setspecific(LocalContext
, NULL
);
2385 ALCcontext_DecRef(context
);
2391 /* alcSetThreadContext
2393 * Makes the given Context the active Context for the current thread
2395 ALC_API ALCboolean ALC_APIENTRY
alcSetThreadContext(ALCcontext
*context
)
2399 /* context must be a valid Context or NULL */
2400 if(context
&& !(context
=VerifyContext(context
)))
2402 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2405 /* context's reference count is already incremented */
2406 old
= pthread_getspecific(LocalContext
);
2407 pthread_setspecific(LocalContext
, context
);
2408 if(old
) ALCcontext_DecRef(old
);
2414 /* alcGetContextsDevice
2416 * Returns the Device that a particular Context is attached to
2418 ALC_API ALCdevice
* ALC_APIENTRY
alcGetContextsDevice(ALCcontext
*Context
)
2422 if(!(Context
=VerifyContext(Context
)))
2424 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2427 Device
= Context
->Device
;
2428 ALCcontext_DecRef(Context
);
2436 * Open the Device specified.
2438 ALC_API ALCdevice
* ALC_APIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
2446 if(!PlaybackBackend
.name
)
2448 alcSetError(NULL
, ALC_INVALID_VALUE
);
2452 if(deviceName
&& (!deviceName
[0] || strcasecmp(deviceName
, alcDefaultName
) == 0 || strcasecmp(deviceName
, "openal-soft") == 0))
2455 device
= calloc(1, sizeof(ALCdevice
)+sizeof(ALeffectslot
));
2458 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
2463 device
->Funcs
= &PlaybackBackend
.Funcs
;
2465 device
->Connected
= ALC_TRUE
;
2466 device
->Type
= Playback
;
2467 InitializeCriticalSection(&device
->Mutex
);
2468 device
->LastError
= ALC_NO_ERROR
;
2471 device
->Bs2b
= NULL
;
2472 device
->Bs2bLevel
= 0;
2473 device
->szDeviceName
= NULL
;
2475 device
->ContextList
= NULL
;
2477 device
->MaxNoOfSources
= 256;
2478 device
->AuxiliaryEffectSlotMax
= 4;
2479 device
->NumAuxSends
= MAX_SENDS
;
2481 InitUIntMap(&device
->BufferMap
, ~0);
2482 InitUIntMap(&device
->EffectMap
, ~0);
2483 InitUIntMap(&device
->FilterMap
, ~0);
2486 device
->FmtChans
= DevFmtChannelsDefault
;
2487 device
->FmtType
= DevFmtTypeDefault
;
2488 device
->Frequency
= DEFAULT_OUTPUT_RATE
;
2489 device
->NumUpdates
= 4;
2490 device
->UpdateSize
= 1024;
2492 if(ConfigValueStr(NULL
, "channels", &fmt
))
2494 static const struct {
2495 const char name
[16];
2496 enum DevFmtChannels chans
;
2498 { "mono", DevFmtMono
},
2499 { "stereo", DevFmtStereo
},
2500 { "quad", DevFmtQuad
},
2501 { "surround51", DevFmtX51
},
2502 { "surround61", DevFmtX61
},
2503 { "surround71", DevFmtX71
},
2507 for(i
= 0;i
< COUNTOF(chanlist
);i
++)
2509 if(strcasecmp(chanlist
[i
].name
, fmt
) == 0)
2511 device
->FmtChans
= chanlist
[i
].chans
;
2512 device
->Flags
|= DEVICE_CHANNELS_REQUEST
;
2516 if(i
== COUNTOF(chanlist
))
2517 ERR("Unsupported channels: %s\n", fmt
);
2519 if(ConfigValueStr(NULL
, "sample-type", &fmt
))
2521 static const struct {
2522 const char name
[16];
2523 enum DevFmtType type
;
2525 { "int8", DevFmtByte
},
2526 { "uint8", DevFmtUByte
},
2527 { "int16", DevFmtShort
},
2528 { "uint16", DevFmtUShort
},
2529 { "int32", DevFmtInt
},
2530 { "uint32", DevFmtUInt
},
2531 { "float32", DevFmtFloat
},
2535 for(i
= 0;i
< COUNTOF(typelist
);i
++)
2537 if(strcasecmp(typelist
[i
].name
, fmt
) == 0)
2539 device
->FmtType
= typelist
[i
].type
;
2540 device
->Flags
|= DEVICE_SAMPLE_TYPE_REQUEST
;
2544 if(i
== COUNTOF(typelist
))
2545 ERR("Unsupported sample-type: %s\n", fmt
);
2547 #define DEVICE_FORMAT_REQUEST (DEVICE_CHANNELS_REQUEST|DEVICE_SAMPLE_TYPE_REQUEST)
2548 if((device
->Flags
&DEVICE_FORMAT_REQUEST
) != DEVICE_FORMAT_REQUEST
&&
2549 ConfigValueStr(NULL
, "format", &fmt
))
2551 static const struct {
2552 const char name
[32];
2553 enum DevFmtChannels channels
;
2554 enum DevFmtType type
;
2556 { "AL_FORMAT_MONO32", DevFmtMono
, DevFmtFloat
},
2557 { "AL_FORMAT_STEREO32", DevFmtStereo
, DevFmtFloat
},
2558 { "AL_FORMAT_QUAD32", DevFmtQuad
, DevFmtFloat
},
2559 { "AL_FORMAT_51CHN32", DevFmtX51
, DevFmtFloat
},
2560 { "AL_FORMAT_61CHN32", DevFmtX61
, DevFmtFloat
},
2561 { "AL_FORMAT_71CHN32", DevFmtX71
, DevFmtFloat
},
2563 { "AL_FORMAT_MONO16", DevFmtMono
, DevFmtShort
},
2564 { "AL_FORMAT_STEREO16", DevFmtStereo
, DevFmtShort
},
2565 { "AL_FORMAT_QUAD16", DevFmtQuad
, DevFmtShort
},
2566 { "AL_FORMAT_51CHN16", DevFmtX51
, DevFmtShort
},
2567 { "AL_FORMAT_61CHN16", DevFmtX61
, DevFmtShort
},
2568 { "AL_FORMAT_71CHN16", DevFmtX71
, DevFmtShort
},
2570 { "AL_FORMAT_MONO8", DevFmtMono
, DevFmtByte
},
2571 { "AL_FORMAT_STEREO8", DevFmtStereo
, DevFmtByte
},
2572 { "AL_FORMAT_QUAD8", DevFmtQuad
, DevFmtByte
},
2573 { "AL_FORMAT_51CHN8", DevFmtX51
, DevFmtByte
},
2574 { "AL_FORMAT_61CHN8", DevFmtX61
, DevFmtByte
},
2575 { "AL_FORMAT_71CHN8", DevFmtX71
, DevFmtByte
}
2579 ERR("Option 'format' is deprecated, please use 'channels' and 'sample-type'\n");
2580 for(i
= 0;i
< COUNTOF(formats
);i
++)
2582 if(strcasecmp(fmt
, formats
[i
].name
) == 0)
2584 if(!(device
->Flags
&DEVICE_CHANNELS_REQUEST
))
2585 device
->FmtChans
= formats
[i
].channels
;
2586 if(!(device
->Flags
&DEVICE_SAMPLE_TYPE_REQUEST
))
2587 device
->FmtType
= formats
[i
].type
;
2588 device
->Flags
|= DEVICE_FORMAT_REQUEST
;
2592 if(i
== COUNTOF(formats
))
2593 ERR("Unsupported format: %s\n", fmt
);
2595 #undef DEVICE_FORMAT_REQUEST
2597 if(ConfigValueUInt(NULL
, "frequency", &device
->Frequency
))
2599 device
->Flags
|= DEVICE_FREQUENCY_REQUEST
;
2600 if(device
->Frequency
< MIN_OUTPUT_RATE
)
2601 ERR("%uhz request clamped to %uhz minimum\n", device
->Frequency
, MIN_OUTPUT_RATE
);
2602 device
->Frequency
= maxu(device
->Frequency
, MIN_OUTPUT_RATE
);
2605 ConfigValueUInt(NULL
, "periods", &device
->NumUpdates
);
2606 device
->NumUpdates
= clampu(device
->NumUpdates
, 2, 16);
2608 ConfigValueUInt(NULL
, "period_size", &device
->UpdateSize
);
2609 device
->UpdateSize
= clampu(device
->UpdateSize
, 64, 8192);
2611 ConfigValueUInt(NULL
, "sources", &device
->MaxNoOfSources
);
2612 if(device
->MaxNoOfSources
== 0) device
->MaxNoOfSources
= 256;
2614 ConfigValueUInt(NULL
, "slots", &device
->AuxiliaryEffectSlotMax
);
2615 if(device
->AuxiliaryEffectSlotMax
== 0) device
->AuxiliaryEffectSlotMax
= 4;
2617 ConfigValueUInt(NULL
, "sends", &device
->NumAuxSends
);
2618 if(device
->NumAuxSends
> MAX_SENDS
) device
->NumAuxSends
= MAX_SENDS
;
2620 ConfigValueInt(NULL
, "cf_level", &device
->Bs2bLevel
);
2622 device
->NumStereoSources
= 1;
2623 device
->NumMonoSources
= device
->MaxNoOfSources
- device
->NumStereoSources
;
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
);
2637 if(DefaultEffect
.type
!= AL_EFFECT_NULL
)
2639 device
->DefaultSlot
= (ALeffectslot
*)(device
+1);
2640 if(InitEffectSlot(device
->DefaultSlot
) != AL_NO_ERROR
)
2642 device
->DefaultSlot
= NULL
;
2643 ERR("Failed to initialize the default effect slot\n");
2645 else if(InitializeEffect(device
, device
->DefaultSlot
, &DefaultEffect
) != AL_NO_ERROR
)
2647 ALeffectState_Destroy(device
->DefaultSlot
->EffectState
);
2648 device
->DefaultSlot
= NULL
;
2649 ERR("Failed to initialize the default effect\n");
2654 device
->next
= DeviceList
;
2655 } while(!CompExchangePtr((XchgPtr
*)&DeviceList
, device
->next
, device
));
2657 TRACE("Created device %p, \"%s\"\n", device
, device
->szDeviceName
);
2663 * Close the specified Device
2665 ALC_API ALCboolean ALC_APIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
2667 ALCdevice
*volatile*list
;
2672 while(*list
&& *list
!= pDevice
)
2673 list
= &(*list
)->next
;
2675 if(!*list
|| (*list
)->Type
== Capture
)
2677 alcSetError(*list
, ALC_INVALID_DEVICE
);
2682 *list
= (*list
)->next
;
2685 while((ctx
=pDevice
->ContextList
) != NULL
)
2687 WARN("Releasing context %p\n", ctx
);
2688 ReleaseContext(ctx
, pDevice
);
2690 if((pDevice
->Flags
&DEVICE_RUNNING
))
2691 ALCdevice_StopPlayback(pDevice
);
2692 pDevice
->Flags
&= ~DEVICE_RUNNING
;
2694 ALCdevice_ClosePlayback(pDevice
);
2696 ALCdevice_DecRef(pDevice
);
2702 /* alcLoopbackOpenDeviceSOFT
2704 * Open a loopback device, for manual rendering.
2706 ALC_API ALCdevice
* ALC_APIENTRY
alcLoopbackOpenDeviceSOFT(const ALCchar
*deviceName
)
2712 /* Make sure the device name, if specified, is us. */
2713 if(deviceName
&& strcmp(deviceName
, alcDefaultName
) != 0)
2715 alcSetError(NULL
, ALC_INVALID_VALUE
);
2719 device
= calloc(1, sizeof(ALCdevice
));
2722 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
2727 device
->Funcs
= &BackendLoopback
.Funcs
;
2729 device
->Connected
= ALC_TRUE
;
2730 device
->Type
= Loopback
;
2731 InitializeCriticalSection(&device
->Mutex
);
2732 device
->LastError
= ALC_NO_ERROR
;
2735 device
->Bs2b
= NULL
;
2736 device
->Bs2bLevel
= 0;
2737 device
->szDeviceName
= NULL
;
2739 device
->ContextList
= NULL
;
2741 device
->MaxNoOfSources
= 256;
2742 device
->AuxiliaryEffectSlotMax
= 4;
2743 device
->NumAuxSends
= MAX_SENDS
;
2745 InitUIntMap(&device
->BufferMap
, ~0);
2746 InitUIntMap(&device
->EffectMap
, ~0);
2747 InitUIntMap(&device
->FilterMap
, ~0);
2750 device
->NumUpdates
= 0;
2751 device
->UpdateSize
= 0;
2753 device
->Frequency
= DEFAULT_OUTPUT_RATE
;
2754 device
->FmtChans
= DevFmtChannelsDefault
;
2755 device
->FmtType
= DevFmtTypeDefault
;
2757 ConfigValueUInt(NULL
, "sources", &device
->MaxNoOfSources
);
2758 if(device
->MaxNoOfSources
== 0) device
->MaxNoOfSources
= 256;
2760 ConfigValueUInt(NULL
, "slots", &device
->AuxiliaryEffectSlotMax
);
2761 if(device
->AuxiliaryEffectSlotMax
== 0) device
->AuxiliaryEffectSlotMax
= 4;
2763 ConfigValueUInt(NULL
, "sends", &device
->NumAuxSends
);
2764 if(device
->NumAuxSends
> MAX_SENDS
) device
->NumAuxSends
= MAX_SENDS
;
2766 device
->NumStereoSources
= 1;
2767 device
->NumMonoSources
= device
->MaxNoOfSources
- device
->NumStereoSources
;
2769 // Open the "backend"
2770 ALCdevice_OpenPlayback(device
, "Loopback");
2772 device
->next
= DeviceList
;
2773 } while(!CompExchangePtr((XchgPtr
*)&DeviceList
, device
->next
, device
));
2775 TRACE("Created device %p\n", device
);
2779 /* alcIsRenderFormatSupportedSOFT
2781 * Determines if the loopback device supports the given format for rendering.
2783 ALC_API ALCboolean ALC_APIENTRY
alcIsRenderFormatSupportedSOFT(ALCdevice
*device
, ALCsizei freq
, ALCenum channels
, ALCenum type
)
2785 ALCboolean ret
= ALC_FALSE
;
2787 if(!(device
=VerifyDevice(device
)) || device
->Type
!= Loopback
)
2788 alcSetError(device
, ALC_INVALID_DEVICE
);
2790 alcSetError(device
, ALC_INVALID_VALUE
);
2793 if(IsValidALCType(type
) && BytesFromDevFmt(type
) > 0 &&
2794 IsValidALCChannels(channels
) && ChannelsFromDevFmt(channels
) > 0 &&
2795 freq
>= MIN_OUTPUT_RATE
)
2798 if(device
) ALCdevice_DecRef(device
);
2803 /* alcRenderSamplesSOFT
2805 * Renders some samples into a buffer, using the format last set by the
2806 * attributes given to alcCreateContext.
2808 ALC_API
void ALC_APIENTRY
alcRenderSamplesSOFT(ALCdevice
*device
, ALCvoid
*buffer
, ALCsizei samples
)
2810 if(!(device
=VerifyDevice(device
)) || device
->Type
!= Loopback
)
2811 alcSetError(device
, ALC_INVALID_DEVICE
);
2812 else if(samples
< 0 || (samples
> 0 && buffer
== NULL
))
2813 alcSetError(device
, ALC_INVALID_VALUE
);
2815 aluMixData(device
, buffer
, samples
);
2816 if(device
) ALCdevice_DecRef(device
);
2820 static void ReleaseALC(void)
2824 free(alcAllDeviceList
); alcAllDeviceList
= NULL
;
2825 alcAllDeviceListSize
= 0;
2826 free(alcCaptureDeviceList
); alcCaptureDeviceList
= NULL
;
2827 alcCaptureDeviceListSize
= 0;
2829 free(alcDefaultAllDeviceSpecifier
);
2830 alcDefaultAllDeviceSpecifier
= NULL
;
2831 free(alcCaptureDefaultDeviceSpecifier
);
2832 alcCaptureDefaultDeviceSpecifier
= NULL
;
2834 if((dev
=ExchangePtr((XchgPtr
*)&DeviceList
, NULL
)) != NULL
)
2839 } while((dev
=dev
->next
) != NULL
);
2840 ERR("%u device%s not closed\n", num
, (num
>1)?"s":"");
2844 ///////////////////////////////////////////////////////