2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
37 #include "alAuxEffectSlot.h"
43 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
44 static struct BackendInfo BackendList
[] = {
45 #ifdef HAVE_PULSEAUDIO
46 { "pulse", alc_pulse_init
, alc_pulse_deinit
, alc_pulse_probe
, EmptyFuncs
},
49 { "alsa", alc_alsa_init
, alc_alsa_deinit
, alc_alsa_probe
, EmptyFuncs
},
52 { "core", alc_ca_init
, alc_ca_deinit
, alc_ca_probe
, EmptyFuncs
},
55 { "oss", alc_oss_init
, alc_oss_deinit
, alc_oss_probe
, EmptyFuncs
},
58 { "solaris", alc_solaris_init
, alc_solaris_deinit
, alc_solaris_probe
, EmptyFuncs
},
61 { "sndio", alc_sndio_init
, alc_sndio_deinit
, alc_sndio_probe
, EmptyFuncs
},
64 { "mmdevapi", alcMMDevApiInit
, alcMMDevApiDeinit
, alcMMDevApiProbe
, EmptyFuncs
},
67 { "dsound", alcDSoundInit
, alcDSoundDeinit
, alcDSoundProbe
, EmptyFuncs
},
70 { "winmm", alcWinMMInit
, alcWinMMDeinit
, alcWinMMProbe
, EmptyFuncs
},
73 { "port", alc_pa_init
, alc_pa_deinit
, alc_pa_probe
, EmptyFuncs
},
76 { "opensl", alc_opensl_init
, alc_opensl_deinit
, alc_opensl_probe
, EmptyFuncs
},
79 { "null", alc_null_init
, alc_null_deinit
, alc_null_probe
, EmptyFuncs
},
81 { "wave", alc_wave_init
, alc_wave_deinit
, alc_wave_probe
, EmptyFuncs
},
84 { NULL
, NULL
, NULL
, NULL
, EmptyFuncs
}
86 static struct BackendInfo BackendLoopback
= {
87 "loopback", alc_loopback_init
, alc_loopback_deinit
, alc_loopback_probe
, EmptyFuncs
91 static struct BackendInfo PlaybackBackend
;
92 static struct BackendInfo CaptureBackend
;
94 ///////////////////////////////////////////////////////
95 // STRING and EXTENSIONS
97 typedef struct ALCfunction
{
98 const ALCchar
*funcName
;
102 typedef struct ALCenums
{
103 const ALCchar
*enumName
;
108 static const ALCfunction alcFunctions
[] = {
109 { "alcCreateContext", (ALCvoid
*) alcCreateContext
},
110 { "alcMakeContextCurrent", (ALCvoid
*) alcMakeContextCurrent
},
111 { "alcProcessContext", (ALCvoid
*) alcProcessContext
},
112 { "alcSuspendContext", (ALCvoid
*) alcSuspendContext
},
113 { "alcDestroyContext", (ALCvoid
*) alcDestroyContext
},
114 { "alcGetCurrentContext", (ALCvoid
*) alcGetCurrentContext
},
115 { "alcGetContextsDevice", (ALCvoid
*) alcGetContextsDevice
},
116 { "alcOpenDevice", (ALCvoid
*) alcOpenDevice
},
117 { "alcCloseDevice", (ALCvoid
*) alcCloseDevice
},
118 { "alcGetError", (ALCvoid
*) alcGetError
},
119 { "alcIsExtensionPresent", (ALCvoid
*) alcIsExtensionPresent
},
120 { "alcGetProcAddress", (ALCvoid
*) alcGetProcAddress
},
121 { "alcGetEnumValue", (ALCvoid
*) alcGetEnumValue
},
122 { "alcGetString", (ALCvoid
*) alcGetString
},
123 { "alcGetIntegerv", (ALCvoid
*) alcGetIntegerv
},
124 { "alcCaptureOpenDevice", (ALCvoid
*) alcCaptureOpenDevice
},
125 { "alcCaptureCloseDevice", (ALCvoid
*) alcCaptureCloseDevice
},
126 { "alcCaptureStart", (ALCvoid
*) alcCaptureStart
},
127 { "alcCaptureStop", (ALCvoid
*) alcCaptureStop
},
128 { "alcCaptureSamples", (ALCvoid
*) alcCaptureSamples
},
130 { "alcSetThreadContext", (ALCvoid
*) alcSetThreadContext
},
131 { "alcGetThreadContext", (ALCvoid
*) alcGetThreadContext
},
133 { "alcLoopbackOpenDeviceSOFT", (ALCvoid
*) alcLoopbackOpenDeviceSOFT
},
134 { "alcIsRenderFormatSupportedSOFT",(ALCvoid
*) alcIsRenderFormatSupportedSOFT
},
135 { "alcRenderSamplesSOFT", (ALCvoid
*) alcRenderSamplesSOFT
},
137 { "alEnable", (ALCvoid
*) alEnable
},
138 { "alDisable", (ALCvoid
*) alDisable
},
139 { "alIsEnabled", (ALCvoid
*) alIsEnabled
},
140 { "alGetString", (ALCvoid
*) alGetString
},
141 { "alGetBooleanv", (ALCvoid
*) alGetBooleanv
},
142 { "alGetIntegerv", (ALCvoid
*) alGetIntegerv
},
143 { "alGetFloatv", (ALCvoid
*) alGetFloatv
},
144 { "alGetDoublev", (ALCvoid
*) alGetDoublev
},
145 { "alGetBoolean", (ALCvoid
*) alGetBoolean
},
146 { "alGetInteger", (ALCvoid
*) alGetInteger
},
147 { "alGetFloat", (ALCvoid
*) alGetFloat
},
148 { "alGetDouble", (ALCvoid
*) alGetDouble
},
149 { "alGetError", (ALCvoid
*) alGetError
},
150 { "alIsExtensionPresent", (ALCvoid
*) alIsExtensionPresent
},
151 { "alGetProcAddress", (ALCvoid
*) alGetProcAddress
},
152 { "alGetEnumValue", (ALCvoid
*) alGetEnumValue
},
153 { "alListenerf", (ALCvoid
*) alListenerf
},
154 { "alListener3f", (ALCvoid
*) alListener3f
},
155 { "alListenerfv", (ALCvoid
*) alListenerfv
},
156 { "alListeneri", (ALCvoid
*) alListeneri
},
157 { "alListener3i", (ALCvoid
*) alListener3i
},
158 { "alListeneriv", (ALCvoid
*) alListeneriv
},
159 { "alGetListenerf", (ALCvoid
*) alGetListenerf
},
160 { "alGetListener3f", (ALCvoid
*) alGetListener3f
},
161 { "alGetListenerfv", (ALCvoid
*) alGetListenerfv
},
162 { "alGetListeneri", (ALCvoid
*) alGetListeneri
},
163 { "alGetListener3i", (ALCvoid
*) alGetListener3i
},
164 { "alGetListeneriv", (ALCvoid
*) alGetListeneriv
},
165 { "alGenSources", (ALCvoid
*) alGenSources
},
166 { "alDeleteSources", (ALCvoid
*) alDeleteSources
},
167 { "alIsSource", (ALCvoid
*) alIsSource
},
168 { "alSourcef", (ALCvoid
*) alSourcef
},
169 { "alSource3f", (ALCvoid
*) alSource3f
},
170 { "alSourcefv", (ALCvoid
*) alSourcefv
},
171 { "alSourcei", (ALCvoid
*) alSourcei
},
172 { "alSource3i", (ALCvoid
*) alSource3i
},
173 { "alSourceiv", (ALCvoid
*) alSourceiv
},
174 { "alGetSourcef", (ALCvoid
*) alGetSourcef
},
175 { "alGetSource3f", (ALCvoid
*) alGetSource3f
},
176 { "alGetSourcefv", (ALCvoid
*) alGetSourcefv
},
177 { "alGetSourcei", (ALCvoid
*) alGetSourcei
},
178 { "alGetSource3i", (ALCvoid
*) alGetSource3i
},
179 { "alGetSourceiv", (ALCvoid
*) alGetSourceiv
},
180 { "alSourcePlayv", (ALCvoid
*) alSourcePlayv
},
181 { "alSourceStopv", (ALCvoid
*) alSourceStopv
},
182 { "alSourceRewindv", (ALCvoid
*) alSourceRewindv
},
183 { "alSourcePausev", (ALCvoid
*) alSourcePausev
},
184 { "alSourcePlay", (ALCvoid
*) alSourcePlay
},
185 { "alSourceStop", (ALCvoid
*) alSourceStop
},
186 { "alSourceRewind", (ALCvoid
*) alSourceRewind
},
187 { "alSourcePause", (ALCvoid
*) alSourcePause
},
188 { "alSourceQueueBuffers", (ALCvoid
*) alSourceQueueBuffers
},
189 { "alSourceUnqueueBuffers", (ALCvoid
*) alSourceUnqueueBuffers
},
190 { "alGenBuffers", (ALCvoid
*) alGenBuffers
},
191 { "alDeleteBuffers", (ALCvoid
*) alDeleteBuffers
},
192 { "alIsBuffer", (ALCvoid
*) alIsBuffer
},
193 { "alBufferData", (ALCvoid
*) alBufferData
},
194 { "alBufferf", (ALCvoid
*) alBufferf
},
195 { "alBuffer3f", (ALCvoid
*) alBuffer3f
},
196 { "alBufferfv", (ALCvoid
*) alBufferfv
},
197 { "alBufferi", (ALCvoid
*) alBufferi
},
198 { "alBuffer3i", (ALCvoid
*) alBuffer3i
},
199 { "alBufferiv", (ALCvoid
*) alBufferiv
},
200 { "alGetBufferf", (ALCvoid
*) alGetBufferf
},
201 { "alGetBuffer3f", (ALCvoid
*) alGetBuffer3f
},
202 { "alGetBufferfv", (ALCvoid
*) alGetBufferfv
},
203 { "alGetBufferi", (ALCvoid
*) alGetBufferi
},
204 { "alGetBuffer3i", (ALCvoid
*) alGetBuffer3i
},
205 { "alGetBufferiv", (ALCvoid
*) alGetBufferiv
},
206 { "alDopplerFactor", (ALCvoid
*) alDopplerFactor
},
207 { "alDopplerVelocity", (ALCvoid
*) alDopplerVelocity
},
208 { "alSpeedOfSound", (ALCvoid
*) alSpeedOfSound
},
209 { "alDistanceModel", (ALCvoid
*) alDistanceModel
},
211 { "alGenFilters", (ALCvoid
*) alGenFilters
},
212 { "alDeleteFilters", (ALCvoid
*) alDeleteFilters
},
213 { "alIsFilter", (ALCvoid
*) alIsFilter
},
214 { "alFilteri", (ALCvoid
*) alFilteri
},
215 { "alFilteriv", (ALCvoid
*) alFilteriv
},
216 { "alFilterf", (ALCvoid
*) alFilterf
},
217 { "alFilterfv", (ALCvoid
*) alFilterfv
},
218 { "alGetFilteri", (ALCvoid
*) alGetFilteri
},
219 { "alGetFilteriv", (ALCvoid
*) alGetFilteriv
},
220 { "alGetFilterf", (ALCvoid
*) alGetFilterf
},
221 { "alGetFilterfv", (ALCvoid
*) alGetFilterfv
},
222 { "alGenEffects", (ALCvoid
*) alGenEffects
},
223 { "alDeleteEffects", (ALCvoid
*) alDeleteEffects
},
224 { "alIsEffect", (ALCvoid
*) alIsEffect
},
225 { "alEffecti", (ALCvoid
*) alEffecti
},
226 { "alEffectiv", (ALCvoid
*) alEffectiv
},
227 { "alEffectf", (ALCvoid
*) alEffectf
},
228 { "alEffectfv", (ALCvoid
*) alEffectfv
},
229 { "alGetEffecti", (ALCvoid
*) alGetEffecti
},
230 { "alGetEffectiv", (ALCvoid
*) alGetEffectiv
},
231 { "alGetEffectf", (ALCvoid
*) alGetEffectf
},
232 { "alGetEffectfv", (ALCvoid
*) alGetEffectfv
},
233 { "alGenAuxiliaryEffectSlots", (ALCvoid
*) alGenAuxiliaryEffectSlots
},
234 { "alDeleteAuxiliaryEffectSlots",(ALCvoid
*) alDeleteAuxiliaryEffectSlots
},
235 { "alIsAuxiliaryEffectSlot", (ALCvoid
*) alIsAuxiliaryEffectSlot
},
236 { "alAuxiliaryEffectSloti", (ALCvoid
*) alAuxiliaryEffectSloti
},
237 { "alAuxiliaryEffectSlotiv", (ALCvoid
*) alAuxiliaryEffectSlotiv
},
238 { "alAuxiliaryEffectSlotf", (ALCvoid
*) alAuxiliaryEffectSlotf
},
239 { "alAuxiliaryEffectSlotfv", (ALCvoid
*) alAuxiliaryEffectSlotfv
},
240 { "alGetAuxiliaryEffectSloti", (ALCvoid
*) alGetAuxiliaryEffectSloti
},
241 { "alGetAuxiliaryEffectSlotiv", (ALCvoid
*) alGetAuxiliaryEffectSlotiv
},
242 { "alGetAuxiliaryEffectSlotf", (ALCvoid
*) alGetAuxiliaryEffectSlotf
},
243 { "alGetAuxiliaryEffectSlotfv", (ALCvoid
*) alGetAuxiliaryEffectSlotfv
},
245 { "alBufferSubDataSOFT", (ALCvoid
*) alBufferSubDataSOFT
},
247 { "alBufferSamplesSOFT", (ALCvoid
*) alBufferSamplesSOFT
},
248 { "alBufferSubSamplesSOFT", (ALCvoid
*) alBufferSubSamplesSOFT
},
249 { "alGetBufferSamplesSOFT", (ALCvoid
*) alGetBufferSamplesSOFT
},
250 { "alIsBufferFormatSupportedSOFT",(ALCvoid
*) alIsBufferFormatSupportedSOFT
},
252 { "alDeferUpdatesSOFT", (ALCvoid
*) alDeferUpdatesSOFT
},
253 { "alProcessUpdatesSOFT", (ALCvoid
*) alProcessUpdatesSOFT
},
255 { NULL
, (ALCvoid
*) NULL
}
258 static const ALCenums enumeration
[] = {
260 { "ALC_INVALID", ALC_INVALID
},
261 { "ALC_FALSE", ALC_FALSE
},
262 { "ALC_TRUE", ALC_TRUE
},
265 { "ALC_MAJOR_VERSION", ALC_MAJOR_VERSION
},
266 { "ALC_MINOR_VERSION", ALC_MINOR_VERSION
},
267 { "ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE
},
268 { "ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES
},
269 { "ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER
},
270 { "ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER
},
271 { "ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER
},
272 { "ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER
},
273 { "ALC_EXTENSIONS", ALC_EXTENSIONS
},
274 { "ALC_FREQUENCY", ALC_FREQUENCY
},
275 { "ALC_REFRESH", ALC_REFRESH
},
276 { "ALC_SYNC", ALC_SYNC
},
277 { "ALC_MONO_SOURCES", ALC_MONO_SOURCES
},
278 { "ALC_STEREO_SOURCES", ALC_STEREO_SOURCES
},
279 { "ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER
},
280 { "ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
},
281 { "ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES
},
282 { "ALC_CONNECTED", ALC_CONNECTED
},
285 { "ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION
},
286 { "ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION
},
287 { "ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS
},
289 // Loopback device Properties
290 { "ALC_FORMAT_CHANNELS_SOFT", ALC_FORMAT_CHANNELS_SOFT
},
291 { "ALC_FORMAT_TYPE_SOFT", ALC_FORMAT_TYPE_SOFT
},
293 // Buffer Channel Configurations
294 { "ALC_MONO", ALC_MONO
},
295 { "ALC_STEREO", ALC_STEREO
},
296 { "ALC_QUAD", ALC_QUAD
},
297 { "ALC_5POINT1", ALC_5POINT1
},
298 { "ALC_6POINT1", ALC_6POINT1
},
299 { "ALC_7POINT1", ALC_7POINT1
},
301 // Buffer Sample Types
302 { "ALC_BYTE", ALC_BYTE
},
303 { "ALC_UNSIGNED_BYTE", ALC_UNSIGNED_BYTE
},
304 { "ALC_SHORT", ALC_SHORT
},
305 { "ALC_UNSIGNED_SHORT", ALC_UNSIGNED_SHORT
},
306 { "ALC_INT", ALC_INT
},
307 { "ALC_UNSIGNED_INT", ALC_UNSIGNED_INT
},
308 { "ALC_FLOAT", ALC_FLOAT
},
311 { "ALC_NO_ERROR", ALC_NO_ERROR
},
312 { "ALC_INVALID_DEVICE", ALC_INVALID_DEVICE
},
313 { "ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT
},
314 { "ALC_INVALID_ENUM", ALC_INVALID_ENUM
},
315 { "ALC_INVALID_VALUE", ALC_INVALID_VALUE
},
316 { "ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY
},
321 static const ALCchar alcNoError
[] = "No Error";
322 static const ALCchar alcErrInvalidDevice
[] = "Invalid Device";
323 static const ALCchar alcErrInvalidContext
[] = "Invalid Context";
324 static const ALCchar alcErrInvalidEnum
[] = "Invalid Enum";
325 static const ALCchar alcErrInvalidValue
[] = "Invalid Value";
326 static const ALCchar alcErrOutOfMemory
[] = "Out of Memory";
328 /* Device lists. Sizes only include the first ending null character, not the
330 static ALCchar
*alcDeviceList
;
331 static size_t alcDeviceListSize
;
332 static ALCchar
*alcAllDeviceList
;
333 static size_t alcAllDeviceListSize
;
334 static ALCchar
*alcCaptureDeviceList
;
335 static size_t alcCaptureDeviceListSize
;
336 /* Default is always the first in the list */
337 static ALCchar
*alcDefaultDeviceSpecifier
;
338 static ALCchar
*alcDefaultAllDeviceSpecifier
;
339 static ALCchar
*alcCaptureDefaultDeviceSpecifier
;
342 static const ALCchar alcNoDeviceExtList
[] =
343 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
344 "ALC_EXT_thread_local_context ALC_SOFTX_loopback_device";
345 static const ALCchar alcExtensionList
[] =
346 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
347 "ALC_EXT_DEDICATED ALC_EXT_disconnect ALC_EXT_EFX "
348 "ALC_EXT_thread_local_context ALC_SOFTX_loopback_device";
349 static const ALCint alcMajorVersion
= 1;
350 static const ALCint alcMinorVersion
= 1;
352 static const ALCint alcEFXMajorVersion
= 1;
353 static const ALCint alcEFXMinorVersion
= 0;
355 ///////////////////////////////////////////////////////
358 ///////////////////////////////////////////////////////
361 static CRITICAL_SECTION ListLock
;
364 static ALCdevice
*volatile DeviceList
= NULL
;
366 // Thread-local current context
367 static pthread_key_t LocalContext
;
368 // Process-wide current context
369 static ALCcontext
*GlobalContext
;
372 static volatile ALCenum g_eLastNullDeviceError
= ALC_NO_ERROR
;
374 // Default context extensions
375 static const ALchar alExtList
[] =
376 "AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 AL_EXT_IMA4 "
377 "AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_MULAW "
378 "AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET AL_EXT_source_distance_model "
379 "AL_LOKI_quadriphonic AL_SOFTX_buffer_samples AL_SOFT_buffer_sub_data "
380 "AL_SOFTX_deferred_updates AL_SOFT_loop_points "
381 "AL_SOFTX_non_virtual_channels";
383 // Mixing Priority Level
391 enum LogLevel LogLevel
= LogWarning
;
393 enum LogLevel LogLevel
= LogError
;
397 ALdouble ConeScale
= 0.5;
399 // Localized Z scalar for mono sources
400 ALdouble ZScale
= 1.0;
402 /* Flag to trap ALC device errors */
403 static ALCboolean TrapALCError
= ALC_FALSE
;
405 /* One-time configuration init control */
406 static pthread_once_t alc_config_once
= PTHREAD_ONCE_INIT
;
408 ///////////////////////////////////////////////////////
411 ///////////////////////////////////////////////////////
412 // ALC Related helper functions
413 static void ReleaseALC(ALCboolean doclose
);
414 static void ReleaseThreadCtx(void *ptr
);
416 static void alc_initconfig(void);
417 #define DO_INITCONFIG() pthread_once(&alc_config_once, alc_initconfig)
420 static void alc_init(void);
421 static void alc_deinit(void);
422 static void alc_deinit_safe(void);
424 #ifndef AL_LIBTYPE_STATIC
425 UIntMap TlsDestructor
;
427 BOOL APIENTRY
DllMain(HANDLE hModule
,DWORD ul_reason_for_call
,LPVOID lpReserved
)
432 // Perform actions based on the reason for calling.
433 switch(ul_reason_for_call
)
435 case DLL_PROCESS_ATTACH
:
436 InitUIntMap(&TlsDestructor
, ~0);
440 case DLL_THREAD_DETACH
:
441 LockUIntMapRead(&TlsDestructor
);
442 for(i
= 0;i
< TlsDestructor
.size
;i
++)
444 void *ptr
= pthread_getspecific(TlsDestructor
.array
[i
].key
);
445 void (*callback
)(void*) = (void(*)(void*))TlsDestructor
.array
[i
].value
;
449 UnlockUIntMapRead(&TlsDestructor
);
452 case DLL_PROCESS_DETACH
:
457 ResetUIntMap(&TlsDestructor
);
462 #elif defined(_MSC_VER)
463 #pragma section(".CRT$XCU",read)
464 static void alc_constructor(void);
465 static void alc_destructor(void);
466 __declspec(allocate(".CRT$XCU")) void (__cdecl
* alc_constructor_
)(void) = alc_constructor
;
468 static void alc_constructor(void)
470 atexit(alc_destructor
);
474 static void alc_destructor(void)
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 static initialization available on this platform!"
484 #elif defined(HAVE_GCC_DESTRUCTOR)
485 static void alc_init(void) __attribute__((constructor
));
486 static void alc_deinit(void) __attribute__((destructor
));
488 #error "No global initialization available on this platform!"
491 static void alc_init(void)
497 str
= getenv("__ALSOFT_HALF_ANGLE_CONES");
498 if(str
&& (strcasecmp(str
, "true") == 0 || strtol(str
, NULL
, 0) == 1))
501 str
= getenv("__ALSOFT_REVERSE_Z");
502 if(str
&& (strcasecmp(str
, "true") == 0 || strtol(str
, NULL
, 0) == 1))
505 str
= getenv("__ALSOFT_TRAP_ERROR");
506 if(str
&& (strcasecmp(str
, "true") == 0 || strtol(str
, NULL
, 0) == 1))
508 TrapALError
= AL_TRUE
;
509 TrapALCError
= AL_TRUE
;
513 str
= getenv("__ALSOFT_TRAP_AL_ERROR");
514 if(str
&& (strcasecmp(str
, "true") == 0 || strtol(str
, NULL
, 0) == 1))
515 TrapALError
= AL_TRUE
;
517 str
= getenv("__ALSOFT_TRAP_ALC_ERROR");
518 if(str
&& (strcasecmp(str
, "true") == 0 || strtol(str
, NULL
, 0) == 1))
519 TrapALCError
= ALC_TRUE
;
522 pthread_key_create(&LocalContext
, ReleaseThreadCtx
);
523 InitializeCriticalSection(&ListLock
);
527 static void alc_deinit_safe(void)
529 ReleaseALC(ALC_FALSE
);
534 DeleteCriticalSection(&ListLock
);
535 pthread_key_delete(LocalContext
);
537 if(LogFile
!= stderr
)
542 static void alc_deinit(void)
546 ReleaseALC(ALC_TRUE
);
548 memset(&PlaybackBackend
, 0, sizeof(PlaybackBackend
));
549 memset(&CaptureBackend
, 0, sizeof(CaptureBackend
));
551 for(i
= 0;BackendList
[i
].Deinit
;i
++)
552 BackendList
[i
].Deinit();
553 BackendLoopback
.Deinit();
558 static void alc_initconfig(void)
560 const char *devs
, *str
;
563 str
= getenv("ALSOFT_LOGLEVEL");
566 long lvl
= strtol(str
, NULL
, 0);
567 if(lvl
>= NoLog
&& lvl
<= LogTrace
)
571 str
= getenv("ALSOFT_LOGFILE");
574 FILE *logfile
= fopen(str
, "wat");
575 if(logfile
) LogFile
= logfile
;
576 else ERR("Failed to open log file '%s'\n", str
);
584 RTPrioLevel
= GetConfigValueInt(NULL
, "rt-prio", 1);
586 RTPrioLevel
= GetConfigValueInt(NULL
, "rt-prio", 0);
589 DefaultResampler
= GetConfigValueInt(NULL
, "resampler", RESAMPLER_DEFAULT
);
590 if(DefaultResampler
>= RESAMPLER_MAX
|| DefaultResampler
<= RESAMPLER_MIN
)
591 DefaultResampler
= RESAMPLER_DEFAULT
;
594 TrapALCError
= GetConfigValueBool(NULL
, "trap-alc-error", ALC_FALSE
);
597 TrapALError
= GetConfigValueBool(NULL
, "trap-al-error", AL_FALSE
);
599 ReverbBoost
*= aluPow(10.0f
, GetConfigValueFloat("reverb", "boost", 0.0f
) /
601 EmulateEAXReverb
= GetConfigValueBool("reverb", "emulate-eax", AL_FALSE
);
603 devs
= GetConfigValue(NULL
, "drivers", "");
608 const char *next
= devs
;
609 int endlist
, delitem
;
614 next
= strchr(devs
, ',');
616 delitem
= (devs
[0] == '-');
617 if(devs
[0] == '-') devs
++;
619 if(!devs
[0] || devs
[0] == ',')
626 len
= (next
? ((size_t)(next
-devs
)) : strlen(devs
));
627 for(n
= i
;BackendList
[n
].Init
;n
++)
629 if(len
== strlen(BackendList
[n
].name
) &&
630 strncmp(BackendList
[n
].name
, devs
, len
) == 0)
635 BackendList
[n
] = BackendList
[n
+1];
637 } while(BackendList
[n
].Init
);
641 struct BackendInfo Bkp
= BackendList
[n
];
644 BackendList
[n
] = BackendList
[n
-1];
647 BackendList
[n
] = Bkp
;
658 BackendList
[i
].name
= NULL
;
659 BackendList
[i
].Init
= NULL
;
660 BackendList
[i
].Deinit
= NULL
;
661 BackendList
[i
].Probe
= NULL
;
665 for(i
= 0;BackendList
[i
].Init
&& (!PlaybackBackend
.name
|| !CaptureBackend
.name
);i
++)
667 if(!BackendList
[i
].Init(&BackendList
[i
].Funcs
))
669 WARN("Failed to initialize backend \"%s\"\n", BackendList
[i
].name
);
673 TRACE("Initialized backend \"%s\"\n", BackendList
[i
].name
);
674 if(BackendList
[i
].Funcs
.OpenPlayback
&& !PlaybackBackend
.name
)
676 PlaybackBackend
= BackendList
[i
];
677 TRACE("Added \"%s\" for playback\n", PlaybackBackend
.name
);
679 if(BackendList
[i
].Funcs
.OpenCapture
&& !CaptureBackend
.name
)
681 CaptureBackend
= BackendList
[i
];
682 TRACE("Added \"%s\" for capture\n", CaptureBackend
.name
);
685 BackendLoopback
.Init(&BackendLoopback
.Funcs
);
687 str
= GetConfigValue(NULL
, "excludefx", "");
691 const char *next
= str
;
695 next
= strchr(str
, ',');
697 if(!str
[0] || next
== str
)
700 len
= (next
? ((size_t)(next
-str
)) : strlen(str
));
701 for(n
= 0;EffectList
[n
].name
;n
++)
703 if(len
== strlen(EffectList
[n
].name
) &&
704 strncmp(EffectList
[n
].name
, str
, len
) == 0)
705 DisabledEffects
[EffectList
[n
].type
] = AL_TRUE
;
712 static void LockLists(void)
714 EnterCriticalSection(&ListLock
);
717 static void UnlockLists(void)
719 LeaveCriticalSection(&ListLock
);
723 static void ProbeList(ALCchar
**list
, size_t *listsize
, enum DevProbe type
)
732 if(type
== CAPTURE_DEVICE_PROBE
)
733 CaptureBackend
.Probe(type
);
735 PlaybackBackend
.Probe(type
);
739 static void ProbeDeviceList(void)
740 { ProbeList(&alcDeviceList
, &alcDeviceListSize
, DEVICE_PROBE
); }
741 static void ProbeAllDeviceList(void)
742 { ProbeList(&alcAllDeviceList
, &alcAllDeviceListSize
, ALL_DEVICE_PROBE
); }
743 static void ProbeCaptureDeviceList(void)
744 { ProbeList(&alcCaptureDeviceList
, &alcCaptureDeviceListSize
, CAPTURE_DEVICE_PROBE
); }
747 static void AppendList(const ALCchar
*name
, ALCchar
**List
, size_t *ListSize
)
749 size_t len
= strlen(name
);
755 temp
= realloc(*List
, (*ListSize
) + len
+ 2);
758 ERR("Realloc failed to add %s!\n", name
);
763 memcpy((*List
)+(*ListSize
), name
, len
+1);
765 (*List
)[*ListSize
] = 0;
768 #define DECL_APPEND_LIST_FUNC(type) \
769 void Append##type##List(const ALCchar *name) \
770 { AppendList(name, &alc##type##List, &alc##type##ListSize); }
772 DECL_APPEND_LIST_FUNC(Device
)
773 DECL_APPEND_LIST_FUNC(AllDevice
)
774 DECL_APPEND_LIST_FUNC(CaptureDevice
)
776 #undef DECL_APPEND_LIST_FUNC
779 /* Sets the default channel order used by most non-WaveFormatEx-based APIs */
780 void SetDefaultChannelOrder(ALCdevice
*device
)
782 switch(device
->FmtChans
)
784 case DevFmtX51
: device
->DevChannels
[0] = FRONT_LEFT
;
785 device
->DevChannels
[1] = FRONT_RIGHT
;
786 device
->DevChannels
[2] = BACK_LEFT
;
787 device
->DevChannels
[3] = BACK_RIGHT
;
788 device
->DevChannels
[4] = FRONT_CENTER
;
789 device
->DevChannels
[5] = LFE
;
792 case DevFmtX71
: device
->DevChannels
[0] = FRONT_LEFT
;
793 device
->DevChannels
[1] = FRONT_RIGHT
;
794 device
->DevChannels
[2] = BACK_LEFT
;
795 device
->DevChannels
[3] = BACK_RIGHT
;
796 device
->DevChannels
[4] = FRONT_CENTER
;
797 device
->DevChannels
[5] = LFE
;
798 device
->DevChannels
[6] = SIDE_LEFT
;
799 device
->DevChannels
[7] = SIDE_RIGHT
;
802 /* Same as WFX order */
810 SetDefaultWFXChannelOrder(device
);
812 /* Sets the default order used by WaveFormatEx */
813 void SetDefaultWFXChannelOrder(ALCdevice
*device
)
815 switch(device
->FmtChans
)
817 case DevFmtMono
: device
->DevChannels
[0] = FRONT_CENTER
; break;
819 case DevFmtStereo
: device
->DevChannels
[0] = FRONT_LEFT
;
820 device
->DevChannels
[1] = FRONT_RIGHT
; break;
822 case DevFmtQuad
: device
->DevChannels
[0] = FRONT_LEFT
;
823 device
->DevChannels
[1] = FRONT_RIGHT
;
824 device
->DevChannels
[2] = BACK_LEFT
;
825 device
->DevChannels
[3] = BACK_RIGHT
; break;
827 case DevFmtX51
: device
->DevChannels
[0] = FRONT_LEFT
;
828 device
->DevChannels
[1] = FRONT_RIGHT
;
829 device
->DevChannels
[2] = FRONT_CENTER
;
830 device
->DevChannels
[3] = LFE
;
831 device
->DevChannels
[4] = BACK_LEFT
;
832 device
->DevChannels
[5] = BACK_RIGHT
; break;
834 case DevFmtX51Side
: device
->DevChannels
[0] = FRONT_LEFT
;
835 device
->DevChannels
[1] = FRONT_RIGHT
;
836 device
->DevChannels
[2] = FRONT_CENTER
;
837 device
->DevChannels
[3] = LFE
;
838 device
->DevChannels
[4] = SIDE_LEFT
;
839 device
->DevChannels
[5] = SIDE_RIGHT
; break;
841 case DevFmtX61
: device
->DevChannels
[0] = FRONT_LEFT
;
842 device
->DevChannels
[1] = FRONT_RIGHT
;
843 device
->DevChannels
[2] = FRONT_CENTER
;
844 device
->DevChannels
[3] = LFE
;
845 device
->DevChannels
[4] = BACK_CENTER
;
846 device
->DevChannels
[5] = SIDE_LEFT
;
847 device
->DevChannels
[6] = SIDE_RIGHT
; break;
849 case DevFmtX71
: device
->DevChannels
[0] = FRONT_LEFT
;
850 device
->DevChannels
[1] = FRONT_RIGHT
;
851 device
->DevChannels
[2] = FRONT_CENTER
;
852 device
->DevChannels
[3] = LFE
;
853 device
->DevChannels
[4] = BACK_LEFT
;
854 device
->DevChannels
[5] = BACK_RIGHT
;
855 device
->DevChannels
[6] = SIDE_LEFT
;
856 device
->DevChannels
[7] = SIDE_RIGHT
; break;
861 const ALCchar
*DevFmtTypeString(enum DevFmtType type
)
865 case DevFmtByte
: return "Signed Byte";
866 case DevFmtUByte
: return "Unsigned Byte";
867 case DevFmtShort
: return "Signed Short";
868 case DevFmtUShort
: return "Unsigned Short";
869 case DevFmtFloat
: return "Float";
871 return "(unknown type)";
873 const ALCchar
*DevFmtChannelsString(enum DevFmtChannels chans
)
877 case DevFmtMono
: return "Mono";
878 case DevFmtStereo
: return "Stereo";
879 case DevFmtQuad
: return "Quadraphonic";
880 case DevFmtX51
: return "5.1 Surround";
881 case DevFmtX51Side
: return "5.1 Side";
882 case DevFmtX61
: return "6.1 Surround";
883 case DevFmtX71
: return "7.1 Surround";
885 return "(unknown channels)";
888 ALuint
BytesFromDevFmt(enum DevFmtType type
)
892 case DevFmtByte
: return sizeof(ALbyte
);
893 case DevFmtUByte
: return sizeof(ALubyte
);
894 case DevFmtShort
: return sizeof(ALshort
);
895 case DevFmtUShort
: return sizeof(ALushort
);
896 case DevFmtFloat
: return sizeof(ALfloat
);
900 ALuint
ChannelsFromDevFmt(enum DevFmtChannels chans
)
904 case DevFmtMono
: return 1;
905 case DevFmtStereo
: return 2;
906 case DevFmtQuad
: return 4;
907 case DevFmtX51
: return 6;
908 case DevFmtX51Side
: return 6;
909 case DevFmtX61
: return 7;
910 case DevFmtX71
: return 8;
914 ALboolean
DecomposeDevFormat(ALenum format
, enum DevFmtChannels
*chans
,
915 enum DevFmtType
*type
)
919 case AL_FORMAT_MONO8
:
923 case AL_FORMAT_MONO16
:
927 case AL_FORMAT_MONO_FLOAT32
:
931 case AL_FORMAT_STEREO8
:
932 *chans
= DevFmtStereo
;
935 case AL_FORMAT_STEREO16
:
936 *chans
= DevFmtStereo
;
939 case AL_FORMAT_STEREO_FLOAT32
:
940 *chans
= DevFmtStereo
;
943 case AL_FORMAT_QUAD8
:
947 case AL_FORMAT_QUAD16
:
951 case AL_FORMAT_QUAD32
:
955 case AL_FORMAT_51CHN8
:
959 case AL_FORMAT_51CHN16
:
963 case AL_FORMAT_51CHN32
:
967 case AL_FORMAT_61CHN8
:
971 case AL_FORMAT_61CHN16
:
975 case AL_FORMAT_61CHN32
:
979 case AL_FORMAT_71CHN8
:
983 case AL_FORMAT_71CHN16
:
987 case AL_FORMAT_71CHN32
:
995 static ALCboolean
IsValidALCType(ALCenum type
)
1000 case ALC_UNSIGNED_BYTE
:
1002 case ALC_UNSIGNED_SHORT
:
1004 case ALC_UNSIGNED_INT
:
1011 static ALCboolean
IsValidALCChannels(ALCenum channels
)
1029 * Stores the latest ALC Error
1031 static void alcSetError(ALCdevice
*device
, ALCenum errorCode
)
1036 /* DebugBreak() will cause an exception if there is no debugger */
1037 if(IsDebuggerPresent())
1039 #elif defined(SIGTRAP)
1040 kill(getpid(), SIGTRAP
);
1045 device
->LastError
= errorCode
;
1047 g_eLastNullDeviceError
= errorCode
;
1051 /* UpdateDeviceParams
1053 * Updates device parameters according to the attribute list (caller is
1054 * responsible for holding the list lock).
1056 static ALCboolean
UpdateDeviceParams(ALCdevice
*device
, const ALCint
*attrList
)
1058 ALCcontext
*context
;
1061 // Check for attributes
1062 if(attrList
&& attrList
[0])
1064 ALCuint freq
, numMono
, numStereo
, numSends
;
1065 enum DevFmtChannels schans
;
1066 enum DevFmtType stype
;
1069 // If a context is already running on the device, stop playback so the
1070 // device attributes can be updated
1071 if((device
->Flags
&DEVICE_RUNNING
))
1072 ALCdevice_StopPlayback(device
);
1073 device
->Flags
&= ~DEVICE_RUNNING
;
1075 freq
= device
->Frequency
;
1076 schans
= device
->FmtChans
;
1077 stype
= device
->FmtType
;
1078 numMono
= device
->NumMonoSources
;
1079 numStereo
= device
->NumStereoSources
;
1080 numSends
= device
->NumAuxSends
;
1082 freq
= GetConfigValueInt(NULL
, "frequency", freq
);
1083 if(freq
< 8000) freq
= 8000;
1086 while(attrList
[attrIdx
])
1088 if(attrList
[attrIdx
] == ALC_FORMAT_CHANNELS_SOFT
&&
1089 device
->IsLoopbackDevice
)
1091 ALCint val
= attrList
[attrIdx
+ 1];
1092 if(!IsValidALCChannels(val
) || !ChannelsFromDevFmt(val
))
1094 alcSetError(device
, ALC_INVALID_VALUE
);
1100 if(attrList
[attrIdx
] == ALC_FORMAT_TYPE_SOFT
&&
1101 device
->IsLoopbackDevice
)
1103 ALCint val
= attrList
[attrIdx
+ 1];
1104 if(!IsValidALCType(val
) || !BytesFromDevFmt(val
))
1106 alcSetError(device
, ALC_INVALID_VALUE
);
1112 if(attrList
[attrIdx
] == ALC_FREQUENCY
)
1114 if(device
->IsLoopbackDevice
)
1116 freq
= attrList
[attrIdx
+ 1];
1119 alcSetError(device
, ALC_INVALID_VALUE
);
1123 else if(!ConfigValueExists(NULL
, "frequency"))
1125 freq
= attrList
[attrIdx
+ 1];
1126 if(freq
< 8000) freq
= 8000;
1127 device
->Flags
|= DEVICE_FREQUENCY_REQUEST
;
1131 if(attrList
[attrIdx
] == ALC_STEREO_SOURCES
)
1133 numStereo
= attrList
[attrIdx
+ 1];
1134 if(numStereo
> device
->MaxNoOfSources
)
1135 numStereo
= device
->MaxNoOfSources
;
1137 numMono
= device
->MaxNoOfSources
- numStereo
;
1140 if(attrList
[attrIdx
] == ALC_MAX_AUXILIARY_SENDS
&&
1141 !ConfigValueExists(NULL
, "sends"))
1143 numSends
= attrList
[attrIdx
+ 1];
1144 if(numSends
> MAX_SENDS
)
1145 numSends
= MAX_SENDS
;
1151 device
->UpdateSize
= (ALuint64
)device
->UpdateSize
* freq
/
1154 device
->Frequency
= freq
;
1155 device
->FmtChans
= schans
;
1156 device
->FmtType
= stype
;
1157 device
->NumMonoSources
= numMono
;
1158 device
->NumStereoSources
= numStereo
;
1159 device
->NumAuxSends
= numSends
;
1162 if((device
->Flags
&DEVICE_RUNNING
))
1166 TRACE("Format pre-setup: %s%s, %s, %uhz%s, %u update size x%d\n",
1167 DevFmtChannelsString(device
->FmtChans
),
1168 (device
->Flags
&DEVICE_CHANNELS_REQUEST
)?" (requested)":"",
1169 DevFmtTypeString(device
->FmtType
), device
->Frequency
,
1170 (device
->Flags
&DEVICE_FREQUENCY_REQUEST
)?" (requested)":"",
1171 device
->UpdateSize
, device
->NumUpdates
);
1172 if(ALCdevice_ResetPlayback(device
) == ALC_FALSE
)
1174 UnlockDevice(device
);
1177 device
->Flags
|= DEVICE_RUNNING
;
1178 TRACE("Format post-setup: %s%s, %s, %uhz%s, %u update size x%d\n",
1179 DevFmtChannelsString(device
->FmtChans
),
1180 (device
->Flags
&DEVICE_CHANNELS_REQUEST
)?" (requested)":"",
1181 DevFmtTypeString(device
->FmtType
), device
->Frequency
,
1182 (device
->Flags
&DEVICE_FREQUENCY_REQUEST
)?" (requested)":"",
1183 device
->UpdateSize
, device
->NumUpdates
);
1185 aluInitPanning(device
);
1187 for(i
= 0;i
< MAXCHANNELS
;i
++)
1189 device
->ClickRemoval
[i
] = 0.0f
;
1190 device
->PendingClicks
[i
] = 0.0f
;
1193 if(!device
->IsLoopbackDevice
&& GetConfigValueBool(NULL
, "hrtf", AL_FALSE
))
1194 device
->Flags
|= DEVICE_USE_HRTF
;
1195 if((device
->Flags
&DEVICE_USE_HRTF
) && !IsHrtfCompatible(device
))
1196 device
->Flags
&= ~DEVICE_USE_HRTF
;
1197 TRACE("HRTF %s\n", (device
->Flags
&DEVICE_USE_HRTF
)?"enabled":"disabled");
1199 if(!(device
->Flags
&DEVICE_USE_HRTF
) && device
->Bs2bLevel
> 0 && device
->Bs2bLevel
<= 6)
1203 device
->Bs2b
= calloc(1, sizeof(*device
->Bs2b
));
1204 bs2b_clear(device
->Bs2b
);
1206 bs2b_set_srate(device
->Bs2b
, device
->Frequency
);
1207 bs2b_set_level(device
->Bs2b
, device
->Bs2bLevel
);
1208 TRACE("BS2B level %d\n", device
->Bs2bLevel
);
1213 device
->Bs2b
= NULL
;
1214 TRACE("BS2B disabled\n");
1217 device
->Flags
&= ~DEVICE_DUPLICATE_STEREO
;
1218 switch(device
->FmtChans
)
1228 if(GetConfigValueBool(NULL
, "stereodup", AL_TRUE
))
1229 device
->Flags
|= DEVICE_DUPLICATE_STEREO
;
1232 TRACE("Stereo duplication %s\n", (device
->Flags
&DEVICE_DUPLICATE_STEREO
)?"enabled":"disabled");
1234 context
= device
->ContextList
;
1239 context
->UpdateSources
= AL_FALSE
;
1240 LockUIntMapRead(&context
->EffectSlotMap
);
1241 for(pos
= 0;pos
< context
->EffectSlotMap
.size
;pos
++)
1243 ALeffectslot
*slot
= context
->EffectSlotMap
.array
[pos
].value
;
1245 if(ALeffectState_DeviceUpdate(slot
->EffectState
, device
) == AL_FALSE
)
1247 UnlockUIntMapRead(&context
->EffectSlotMap
);
1248 UnlockDevice(device
);
1249 ALCdevice_StopPlayback(device
);
1250 device
->Flags
&= ~DEVICE_RUNNING
;
1253 slot
->NeedsUpdate
= AL_FALSE
;
1254 ALeffectState_Update(slot
->EffectState
, context
, slot
);
1256 UnlockUIntMapRead(&context
->EffectSlotMap
);
1258 LockUIntMapRead(&context
->SourceMap
);
1259 for(pos
= 0;pos
< context
->SourceMap
.size
;pos
++)
1261 ALsource
*source
= context
->SourceMap
.array
[pos
].value
;
1262 ALuint s
= device
->NumAuxSends
;
1263 while(s
< MAX_SENDS
)
1265 if(source
->Send
[s
].Slot
)
1266 DecrementRef(&source
->Send
[s
].Slot
->ref
);
1267 source
->Send
[s
].Slot
= NULL
;
1268 source
->Send
[s
].WetGain
= 1.0f
;
1269 source
->Send
[s
].WetGainHF
= 1.0f
;
1272 source
->NeedsUpdate
= AL_FALSE
;
1273 ALsource_Update(source
, context
);
1275 UnlockUIntMapRead(&context
->SourceMap
);
1277 context
= context
->next
;
1279 UnlockDevice(device
);
1286 * Frees the device structure, and destroys any objects the app failed to
1287 * delete. Called once there's no more references on the device.
1289 static ALCvoid
FreeDevice(ALCdevice
*device
)
1291 TRACE("%p\n", device
);
1293 if(device
->BufferMap
.size
> 0)
1295 WARN("(%p) Deleting %d Buffer(s)\n", device
, device
->BufferMap
.size
);
1296 ReleaseALBuffers(device
);
1298 ResetUIntMap(&device
->BufferMap
);
1300 if(device
->EffectMap
.size
> 0)
1302 WARN("(%p) Deleting %d Effect(s)\n", device
, device
->EffectMap
.size
);
1303 ReleaseALEffects(device
);
1305 ResetUIntMap(&device
->EffectMap
);
1307 if(device
->FilterMap
.size
> 0)
1309 WARN("(%p) Deleting %d Filter(s)\n", device
, device
->FilterMap
.size
);
1310 ReleaseALFilters(device
);
1312 ResetUIntMap(&device
->FilterMap
);
1315 device
->Bs2b
= NULL
;
1317 free(device
->szDeviceName
);
1318 device
->szDeviceName
= NULL
;
1320 DeleteCriticalSection(&device
->Mutex
);
1326 void ALCdevice_IncRef(ALCdevice
*device
)
1329 ref
= IncrementRef(&device
->ref
);
1330 TRACE("%p increasing refcount to %u\n", device
, ref
);
1333 void ALCdevice_DecRef(ALCdevice
*device
)
1336 ref
= DecrementRef(&device
->ref
);
1337 TRACE("%p decreasing refcount to %u\n", device
, ref
);
1338 if(ref
== 0) FreeDevice(device
);
1343 * Checks if the device handle is valid, and increments its ref count if so.
1345 static ALCdevice
*VerifyDevice(ALCdevice
*device
)
1347 ALCdevice
*tmpDevice
;
1353 tmpDevice
= DeviceList
;
1354 while(tmpDevice
&& tmpDevice
!= device
)
1355 tmpDevice
= tmpDevice
->next
;
1358 ALCdevice_IncRef(tmpDevice
);
1364 ALCvoid
LockDevice(ALCdevice
*device
)
1366 EnterCriticalSection(&device
->Mutex
);
1369 ALCvoid
UnlockDevice(ALCdevice
*device
)
1371 LeaveCriticalSection(&device
->Mutex
);
1377 * Initializes context variables
1379 static ALvoid
InitContext(ALCcontext
*pContext
)
1381 //Initialise listener
1382 pContext
->Listener
.Gain
= 1.0f
;
1383 pContext
->Listener
.MetersPerUnit
= 1.0f
;
1384 pContext
->Listener
.Position
[0] = 0.0f
;
1385 pContext
->Listener
.Position
[1] = 0.0f
;
1386 pContext
->Listener
.Position
[2] = 0.0f
;
1387 pContext
->Listener
.Velocity
[0] = 0.0f
;
1388 pContext
->Listener
.Velocity
[1] = 0.0f
;
1389 pContext
->Listener
.Velocity
[2] = 0.0f
;
1390 pContext
->Listener
.Forward
[0] = 0.0f
;
1391 pContext
->Listener
.Forward
[1] = 0.0f
;
1392 pContext
->Listener
.Forward
[2] = -1.0f
;
1393 pContext
->Listener
.Up
[0] = 0.0f
;
1394 pContext
->Listener
.Up
[1] = 1.0f
;
1395 pContext
->Listener
.Up
[2] = 0.0f
;
1398 pContext
->LastError
= AL_NO_ERROR
;
1399 pContext
->UpdateSources
= AL_FALSE
;
1400 pContext
->ActiveSourceCount
= 0;
1401 InitUIntMap(&pContext
->SourceMap
, pContext
->Device
->MaxNoOfSources
);
1402 InitUIntMap(&pContext
->EffectSlotMap
, pContext
->Device
->AuxiliaryEffectSlotMax
);
1405 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
1406 pContext
->SourceDistanceModel
= AL_FALSE
;
1407 pContext
->DopplerFactor
= 1.0f
;
1408 pContext
->DopplerVelocity
= 1.0f
;
1409 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
1410 pContext
->DeferUpdates
= AL_FALSE
;
1412 pContext
->ExtensionList
= alExtList
;
1418 * Cleans up the context, and destroys any remaining objects the app failed to
1419 * delete. Called once there's no more references on the context.
1421 static ALCvoid
FreeContext(ALCcontext
*context
)
1423 TRACE("%p\n", context
);
1425 if(context
->SourceMap
.size
> 0)
1427 ERR("(%p) Deleting %d Source(s)\n", context
, context
->SourceMap
.size
);
1428 ReleaseALSources(context
);
1430 ResetUIntMap(&context
->SourceMap
);
1432 if(context
->EffectSlotMap
.size
> 0)
1434 ERR("(%p) Deleting %d AuxiliaryEffectSlot(s)\n", context
, context
->EffectSlotMap
.size
);
1435 ReleaseALAuxiliaryEffectSlots(context
);
1437 ResetUIntMap(&context
->EffectSlotMap
);
1439 context
->ActiveSourceCount
= 0;
1440 free(context
->ActiveSources
);
1441 context
->ActiveSources
= NULL
;
1442 context
->MaxActiveSources
= 0;
1444 context
->ActiveEffectSlotCount
= 0;
1445 free(context
->ActiveEffectSlots
);
1446 context
->ActiveEffectSlots
= NULL
;
1447 context
->MaxActiveEffectSlots
= 0;
1449 ALCdevice_DecRef(context
->Device
);
1450 context
->Device
= NULL
;
1452 //Invalidate context
1453 memset(context
, 0, sizeof(ALCcontext
));
1459 * Removes the context reference from the given device and removes it from
1460 * being current on the running thread or globally.
1462 static void ReleaseContext(ALCcontext
*context
, ALCdevice
*device
)
1464 ALCcontext
*volatile*tmp_ctx
;
1466 if(pthread_getspecific(LocalContext
) == context
)
1468 WARN("%p released while current on thread\n", context
);
1469 pthread_setspecific(LocalContext
, NULL
);
1470 ALCcontext_DecRef(context
);
1473 if(CompExchangePtr((void**)&GlobalContext
, context
, NULL
))
1475 WARN("%p released while current\n", context
);
1476 ALCcontext_DecRef(context
);
1479 tmp_ctx
= &device
->ContextList
;
1482 if(*tmp_ctx
== context
)
1484 *tmp_ctx
= context
->next
;
1487 tmp_ctx
= &(*tmp_ctx
)->next
;
1491 /* Lock the device to make sure the mixer is not using the contexts in the
1492 * list before we go about deleting this one. There should be a more
1493 * efficient way of doing this. */
1494 UnlockDevice(device
);
1496 ALCcontext_DecRef(context
);
1499 void ALCcontext_IncRef(ALCcontext
*context
)
1502 ref
= IncrementRef(&context
->ref
);
1503 TRACE("%p increasing refcount to %u\n", context
, ref
);
1506 void ALCcontext_DecRef(ALCcontext
*context
)
1509 ref
= DecrementRef(&context
->ref
);
1510 TRACE("%p decreasing refcount to %u\n", context
, ref
);
1511 if(ref
== 0) FreeContext(context
);
1514 static void ReleaseThreadCtx(void *ptr
)
1516 WARN("%p current for thread being destroyed\n", ptr
);
1517 ALCcontext_DecRef(ptr
);
1522 * Checks that the given context is valid, and increments its reference count.
1524 static ALCcontext
*VerifyContext(ALCcontext
*context
)
1532 ALCcontext
*tmp_ctx
= dev
->ContextList
;
1535 if(tmp_ctx
== context
)
1537 ALCcontext_IncRef(tmp_ctx
);
1541 tmp_ctx
= tmp_ctx
->next
;
1551 ALCvoid
LockContext(ALCcontext
*context
)
1553 EnterCriticalSection(&context
->Device
->Mutex
);
1556 ALCvoid
UnlockContext(ALCcontext
*context
)
1558 LeaveCriticalSection(&context
->Device
->Mutex
);
1563 * Returns the currently active context, and adds a reference without locking
1566 ALCcontext
*GetContextRef(void)
1568 ALCcontext
*context
;
1570 context
= pthread_getspecific(LocalContext
);
1572 ALCcontext_IncRef(context
);
1576 context
= GlobalContext
;
1578 ALCcontext_IncRef(context
);
1585 ///////////////////////////////////////////////////////
1588 ///////////////////////////////////////////////////////
1589 // ALC Functions calls
1592 // This should probably move to another c file but for now ...
1593 ALC_API ALCdevice
* ALC_APIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
1595 ALCdevice
*device
= NULL
;
1600 if(!CaptureBackend
.name
)
1602 alcSetError(NULL
, ALC_INVALID_VALUE
);
1608 alcSetError(NULL
, ALC_INVALID_VALUE
);
1612 if(deviceName
&& (!deviceName
[0] || strcasecmp(deviceName
, "openal soft") == 0 || strcasecmp(deviceName
, "openal-soft") == 0))
1615 device
= calloc(1, sizeof(ALCdevice
));
1618 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
1623 device
->Funcs
= &CaptureBackend
.Funcs
;
1625 device
->Connected
= ALC_TRUE
;
1626 device
->IsCaptureDevice
= AL_TRUE
;
1627 device
->IsLoopbackDevice
= AL_FALSE
;
1628 InitializeCriticalSection(&device
->Mutex
);
1630 InitUIntMap(&device
->BufferMap
, ~0);
1631 InitUIntMap(&device
->EffectMap
, ~0);
1632 InitUIntMap(&device
->FilterMap
, ~0);
1634 device
->szDeviceName
= NULL
;
1636 device
->Flags
|= DEVICE_FREQUENCY_REQUEST
;
1637 device
->Frequency
= frequency
;
1639 device
->Flags
|= DEVICE_CHANNELS_REQUEST
;
1640 if(DecomposeDevFormat(format
, &device
->FmtChans
, &device
->FmtType
) == AL_FALSE
)
1643 alcSetError(NULL
, ALC_INVALID_ENUM
);
1647 device
->UpdateSize
= SampleSize
;
1648 device
->NumUpdates
= 1;
1651 if((err
=ALCdevice_OpenCapture(device
, deviceName
)) != ALC_NO_ERROR
)
1654 DeleteCriticalSection(&device
->Mutex
);
1656 alcSetError(NULL
, err
);
1662 device
->next
= DeviceList
;
1663 } while(!CompExchangePtr((void**)&DeviceList
, device
->next
, device
));
1668 ALC_API ALCboolean ALC_APIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
1670 ALCdevice
*volatile*list
;
1674 while(*list
&& *list
!= pDevice
)
1675 list
= &(*list
)->next
;
1677 if(!*list
|| !(*list
)->IsCaptureDevice
)
1679 alcSetError(*list
, ALC_INVALID_DEVICE
);
1684 *list
= (*list
)->next
;
1687 LockDevice(pDevice
);
1688 ALCdevice_CloseCapture(pDevice
);
1689 UnlockDevice(pDevice
);
1691 ALCdevice_DecRef(pDevice
);
1696 ALC_API
void ALC_APIENTRY
alcCaptureStart(ALCdevice
*device
)
1698 if(!(device
=VerifyDevice(device
)) || !device
->IsCaptureDevice
)
1700 alcSetError(device
, ALC_INVALID_DEVICE
);
1701 if(device
) ALCdevice_DecRef(device
);
1705 if(device
->Connected
)
1706 ALCdevice_StartCapture(device
);
1707 UnlockDevice(device
);
1709 ALCdevice_DecRef(device
);
1712 ALC_API
void ALC_APIENTRY
alcCaptureStop(ALCdevice
*device
)
1714 if(!(device
=VerifyDevice(device
)) || !device
->IsCaptureDevice
)
1716 alcSetError(device
, ALC_INVALID_DEVICE
);
1717 if(device
) ALCdevice_DecRef(device
);
1721 if(device
->Connected
)
1722 ALCdevice_StopCapture(device
);
1723 UnlockDevice(device
);
1725 ALCdevice_DecRef(device
);
1728 ALC_API
void ALC_APIENTRY
alcCaptureSamples(ALCdevice
*device
, ALCvoid
*buffer
, ALCsizei samples
)
1730 ALCenum err
= ALC_INVALID_DEVICE
;
1731 if((device
=VerifyDevice(device
)) != NULL
&& device
->IsCaptureDevice
)
1734 err
= ALCdevice_CaptureSamples(device
, buffer
, samples
);
1735 UnlockDevice(device
);
1737 if(err
!= ALC_NO_ERROR
)
1738 alcSetError(device
, err
);
1739 if(device
) ALCdevice_DecRef(device
);
1745 Return last ALC generated error code
1747 ALC_API ALCenum ALC_APIENTRY
alcGetError(ALCdevice
*device
)
1751 if(VerifyDevice(device
))
1753 errorCode
= ExchangeInt(&device
->LastError
, ALC_NO_ERROR
);
1754 ALCdevice_DecRef(device
);
1757 errorCode
= ExchangeInt(&g_eLastNullDeviceError
, ALC_NO_ERROR
);
1763 /* alcSuspendContext
1767 ALC_API ALCvoid ALC_APIENTRY
alcSuspendContext(ALCcontext
*Context
)
1772 /* alcProcessContext
1776 ALC_API ALCvoid ALC_APIENTRY
alcProcessContext(ALCcontext
*Context
)
1784 * Returns information about the Device, and error strings
1786 ALC_API
const ALCchar
* ALC_APIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
1788 const ALCchar
*value
= NULL
;
1796 case ALC_INVALID_ENUM
:
1797 value
= alcErrInvalidEnum
;
1800 case ALC_INVALID_VALUE
:
1801 value
= alcErrInvalidValue
;
1804 case ALC_INVALID_DEVICE
:
1805 value
= alcErrInvalidDevice
;
1808 case ALC_INVALID_CONTEXT
:
1809 value
= alcErrInvalidContext
;
1812 case ALC_OUT_OF_MEMORY
:
1813 value
= alcErrOutOfMemory
;
1816 case ALC_DEVICE_SPECIFIER
:
1817 if(VerifyDevice(pDevice
))
1819 value
= pDevice
->szDeviceName
;
1820 ALCdevice_DecRef(pDevice
);
1825 value
= alcDeviceList
;
1829 case ALC_ALL_DEVICES_SPECIFIER
:
1830 ProbeAllDeviceList();
1831 value
= alcAllDeviceList
;
1834 case ALC_CAPTURE_DEVICE_SPECIFIER
:
1835 if(VerifyDevice(pDevice
))
1837 value
= pDevice
->szDeviceName
;
1838 ALCdevice_DecRef(pDevice
);
1842 ProbeCaptureDeviceList();
1843 value
= alcCaptureDeviceList
;
1847 /* Default devices are always first in the list */
1848 case ALC_DEFAULT_DEVICE_SPECIFIER
:
1852 pDevice
= VerifyDevice(pDevice
);
1854 free(alcDefaultDeviceSpecifier
);
1855 alcDefaultDeviceSpecifier
= strdup(alcDeviceList
? alcDeviceList
: "");
1856 if(!alcDefaultDeviceSpecifier
)
1857 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1859 value
= alcDefaultDeviceSpecifier
;
1860 if(pDevice
) ALCdevice_DecRef(pDevice
);
1863 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
1864 if(!alcAllDeviceList
)
1865 ProbeAllDeviceList();
1867 pDevice
= VerifyDevice(pDevice
);
1869 free(alcDefaultAllDeviceSpecifier
);
1870 alcDefaultAllDeviceSpecifier
= strdup(alcAllDeviceList
?
1871 alcAllDeviceList
: "");
1872 if(!alcDefaultAllDeviceSpecifier
)
1873 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1875 value
= alcDefaultAllDeviceSpecifier
;
1876 if(pDevice
) ALCdevice_DecRef(pDevice
);
1879 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
1880 if(!alcCaptureDeviceList
)
1881 ProbeCaptureDeviceList();
1883 pDevice
= VerifyDevice(pDevice
);
1885 free(alcCaptureDefaultDeviceSpecifier
);
1886 alcCaptureDefaultDeviceSpecifier
= strdup(alcCaptureDeviceList
?
1887 alcCaptureDeviceList
: "");
1888 if(!alcCaptureDefaultDeviceSpecifier
)
1889 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1891 value
= alcCaptureDefaultDeviceSpecifier
;
1892 if(pDevice
) ALCdevice_DecRef(pDevice
);
1895 case ALC_EXTENSIONS
:
1896 if(!VerifyDevice(pDevice
))
1897 value
= alcNoDeviceExtList
;
1900 value
= alcExtensionList
;
1901 ALCdevice_DecRef(pDevice
);
1906 pDevice
= VerifyDevice(pDevice
);
1907 alcSetError(pDevice
, ALC_INVALID_ENUM
);
1908 if(pDevice
) ALCdevice_DecRef(pDevice
);
1918 * Returns information about the Device and the version of Open AL
1920 ALC_API ALCvoid ALC_APIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
1922 device
= VerifyDevice(device
);
1924 if(size
== 0 || data
== NULL
)
1926 alcSetError(device
, ALC_INVALID_VALUE
);
1927 if(device
) ALCdevice_DecRef(device
);
1935 case ALC_MAJOR_VERSION
:
1936 *data
= alcMajorVersion
;
1938 case ALC_MINOR_VERSION
:
1939 *data
= alcMinorVersion
;
1942 case ALC_ATTRIBUTES_SIZE
:
1943 case ALC_ALL_ATTRIBUTES
:
1947 case ALC_MONO_SOURCES
:
1948 case ALC_STEREO_SOURCES
:
1949 case ALC_CAPTURE_SAMPLES
:
1950 case ALC_FORMAT_CHANNELS_SOFT
:
1951 case ALC_FORMAT_TYPE_SOFT
:
1952 alcSetError(NULL
, ALC_INVALID_DEVICE
);
1956 alcSetError(NULL
, ALC_INVALID_ENUM
);
1960 else if(device
->IsCaptureDevice
)
1964 case ALC_CAPTURE_SAMPLES
:
1966 *data
= ALCdevice_AvailableSamples(device
);
1967 UnlockDevice(device
);
1971 *data
= device
->Connected
;
1975 alcSetError(device
, ALC_INVALID_ENUM
);
1979 else /* render device */
1983 case ALC_MAJOR_VERSION
:
1984 *data
= alcMajorVersion
;
1987 case ALC_MINOR_VERSION
:
1988 *data
= alcMinorVersion
;
1991 case ALC_EFX_MAJOR_VERSION
:
1992 *data
= alcEFXMajorVersion
;
1995 case ALC_EFX_MINOR_VERSION
:
1996 *data
= alcEFXMinorVersion
;
1999 case ALC_ATTRIBUTES_SIZE
:
2003 case ALC_ALL_ATTRIBUTES
:
2005 alcSetError(device
, ALC_INVALID_VALUE
);
2010 data
[i
++] = ALC_FREQUENCY
;
2011 data
[i
++] = device
->Frequency
;
2013 if(!device
->IsLoopbackDevice
)
2015 data
[i
++] = ALC_REFRESH
;
2016 data
[i
++] = device
->Frequency
/ device
->UpdateSize
;
2018 data
[i
++] = ALC_SYNC
;
2019 data
[i
++] = ALC_FALSE
;
2023 data
[i
++] = ALC_FORMAT_CHANNELS_SOFT
;
2024 data
[i
++] = device
->FmtChans
;
2026 data
[i
++] = ALC_FORMAT_TYPE_SOFT
;
2027 data
[i
++] = device
->FmtType
;
2030 data
[i
++] = ALC_MONO_SOURCES
;
2031 data
[i
++] = device
->NumMonoSources
;
2033 data
[i
++] = ALC_STEREO_SOURCES
;
2034 data
[i
++] = device
->NumStereoSources
;
2036 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
2037 data
[i
++] = device
->NumAuxSends
;
2044 *data
= device
->Frequency
;
2048 if(device
->IsLoopbackDevice
)
2049 alcSetError(device
, ALC_INVALID_DEVICE
);
2051 *data
= device
->Frequency
/ device
->UpdateSize
;
2055 if(device
->IsLoopbackDevice
)
2056 alcSetError(device
, ALC_INVALID_DEVICE
);
2061 case ALC_FORMAT_CHANNELS_SOFT
:
2062 if(!device
->IsLoopbackDevice
)
2063 alcSetError(device
, ALC_INVALID_DEVICE
);
2065 *data
= device
->FmtChans
;
2068 case ALC_FORMAT_TYPE_SOFT
:
2069 if(!device
->IsLoopbackDevice
)
2070 alcSetError(device
, ALC_INVALID_DEVICE
);
2072 *data
= device
->FmtType
;
2075 case ALC_MONO_SOURCES
:
2076 *data
= device
->NumMonoSources
;
2079 case ALC_STEREO_SOURCES
:
2080 *data
= device
->NumStereoSources
;
2083 case ALC_MAX_AUXILIARY_SENDS
:
2084 *data
= device
->NumAuxSends
;
2088 *data
= device
->Connected
;
2092 alcSetError(device
, ALC_INVALID_ENUM
);
2097 ALCdevice_DecRef(device
);
2101 /* alcIsExtensionPresent
2103 * Determines if there is support for a particular extension
2105 ALC_API ALCboolean ALC_APIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
2107 ALCboolean bResult
= ALC_FALSE
;
2109 device
= VerifyDevice(device
);
2112 alcSetError(device
, ALC_INVALID_VALUE
);
2115 size_t len
= strlen(extName
);
2116 const char *ptr
= (device
? alcExtensionList
: alcNoDeviceExtList
);
2119 if(strncasecmp(ptr
, extName
, len
) == 0 &&
2120 (ptr
[len
] == '\0' || isspace(ptr
[len
])))
2125 if((ptr
=strchr(ptr
, ' ')) != NULL
)
2129 } while(isspace(*ptr
));
2134 ALCdevice_DecRef(device
);
2139 /* alcGetProcAddress
2141 * Retrieves the function address for a particular extension function
2143 ALC_API ALCvoid
* ALC_APIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
2145 ALCvoid
*ptr
= NULL
;
2147 device
= VerifyDevice(device
);
2150 alcSetError(device
, ALC_INVALID_VALUE
);
2154 while(alcFunctions
[i
].funcName
&& strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
2156 ptr
= alcFunctions
[i
].address
;
2159 ALCdevice_DecRef(device
);
2166 * Get the value for a particular ALC Enumerated Value
2168 ALC_API ALCenum ALC_APIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
2172 device
= VerifyDevice(device
);
2175 alcSetError(device
, ALC_INVALID_VALUE
);
2179 while(enumeration
[i
].enumName
&& strcmp(enumeration
[i
].enumName
,enumName
) != 0)
2181 val
= enumeration
[i
].value
;
2184 ALCdevice_DecRef(device
);
2191 * Create and attach a Context to a particular Device.
2193 ALC_API ALCcontext
* ALC_APIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
2195 ALCcontext
*ALContext
;
2198 if(!(device
=VerifyDevice(device
)) || device
->IsCaptureDevice
|| !device
->Connected
)
2201 alcSetError(device
, ALC_INVALID_DEVICE
);
2202 if(device
) ALCdevice_DecRef(device
);
2206 /* Reset Context Last Error code */
2207 device
->LastError
= ALC_NO_ERROR
;
2209 if(UpdateDeviceParams(device
, attrList
) == ALC_FALSE
)
2212 alcSetError(device
, ALC_INVALID_DEVICE
);
2213 aluHandleDisconnect(device
);
2214 ALCdevice_DecRef(device
);
2218 ALContext
= calloc(1, sizeof(ALCcontext
));
2223 ALContext
->MaxActiveSources
= 256;
2224 ALContext
->ActiveSources
= malloc(sizeof(ALContext
->ActiveSources
[0]) *
2225 ALContext
->MaxActiveSources
);
2227 if(!ALContext
|| !ALContext
->ActiveSources
)
2229 if(!device
->ContextList
)
2231 ALCdevice_StopPlayback(device
);
2232 device
->Flags
&= ~DEVICE_RUNNING
;
2239 alcSetError(device
, ALC_OUT_OF_MEMORY
);
2240 ALCdevice_DecRef(device
);
2244 ALContext
->Device
= device
;
2245 ALCdevice_IncRef(device
);
2246 InitContext(ALContext
);
2248 ALContext
->next
= device
->ContextList
;
2249 device
->ContextList
= ALContext
;
2252 ALCdevice_DecRef(device
);
2256 /* alcDestroyContext
2260 ALC_API ALCvoid ALC_APIENTRY
alcDestroyContext(ALCcontext
*context
)
2265 /* alcGetContextsDevice sets an error for invalid contexts */
2266 Device
= alcGetContextsDevice(context
);
2269 ReleaseContext(context
, Device
);
2270 if(!Device
->ContextList
)
2272 ALCdevice_StopPlayback(Device
);
2273 Device
->Flags
&= ~DEVICE_RUNNING
;
2280 /* alcGetCurrentContext
2282 * Returns the currently active Context
2284 ALC_API ALCcontext
* ALC_APIENTRY
alcGetCurrentContext(ALCvoid
)
2286 ALCcontext
*Context
;
2288 Context
= pthread_getspecific(LocalContext
);
2289 if(!Context
) Context
= GlobalContext
;
2294 /* alcGetThreadContext
2296 * Returns the currently active thread-local Context
2298 ALC_API ALCcontext
* ALC_APIENTRY
alcGetThreadContext(void)
2300 ALCcontext
*Context
;
2301 Context
= pthread_getspecific(LocalContext
);
2306 /* alcMakeContextCurrent
2308 * Makes the given Context the active Context
2310 ALC_API ALCboolean ALC_APIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
2312 /* context must be a valid Context or NULL */
2313 if(context
&& !(context
=VerifyContext(context
)))
2315 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2318 /* context's reference count is already incremented */
2319 context
= ExchangePtr((void**)&GlobalContext
, context
);
2320 if(context
) ALCcontext_DecRef(context
);
2322 if((context
=pthread_getspecific(LocalContext
)) != NULL
)
2324 pthread_setspecific(LocalContext
, NULL
);
2325 ALCcontext_DecRef(context
);
2331 /* alcSetThreadContext
2333 * Makes the given Context the active Context for the current thread
2335 ALC_API ALCboolean ALC_APIENTRY
alcSetThreadContext(ALCcontext
*context
)
2339 /* context must be a valid Context or NULL */
2340 if(context
&& !(context
=VerifyContext(context
)))
2342 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2345 /* context's reference count is already incremented */
2346 old
= pthread_getspecific(LocalContext
);
2347 pthread_setspecific(LocalContext
, context
);
2348 if(old
) ALCcontext_DecRef(old
);
2354 /* alcGetContextsDevice
2356 * Returns the Device that a particular Context is attached to
2358 ALC_API ALCdevice
* ALC_APIENTRY
alcGetContextsDevice(ALCcontext
*Context
)
2362 if(!(Context
=VerifyContext(Context
)))
2364 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2367 Device
= Context
->Device
;
2368 ALCcontext_DecRef(Context
);
2374 static void GetFormatFromString(const char *str
, enum DevFmtChannels
*chans
, enum DevFmtType
*type
)
2376 static const struct {
2377 const char name
[32];
2378 enum DevFmtChannels channels
;
2379 enum DevFmtType type
;
2381 { "AL_FORMAT_MONO32", DevFmtMono
, DevFmtFloat
},
2382 { "AL_FORMAT_STEREO32", DevFmtStereo
, DevFmtFloat
},
2383 { "AL_FORMAT_QUAD32", DevFmtQuad
, DevFmtFloat
},
2384 { "AL_FORMAT_51CHN32", DevFmtX51
, DevFmtFloat
},
2385 { "AL_FORMAT_61CHN32", DevFmtX61
, DevFmtFloat
},
2386 { "AL_FORMAT_71CHN32", DevFmtX71
, DevFmtFloat
},
2388 { "AL_FORMAT_MONO16", DevFmtMono
, DevFmtShort
},
2389 { "AL_FORMAT_STEREO16", DevFmtStereo
, DevFmtShort
},
2390 { "AL_FORMAT_QUAD16", DevFmtQuad
, DevFmtShort
},
2391 { "AL_FORMAT_51CHN16", DevFmtX51
, DevFmtShort
},
2392 { "AL_FORMAT_61CHN16", DevFmtX61
, DevFmtShort
},
2393 { "AL_FORMAT_71CHN16", DevFmtX71
, DevFmtShort
},
2395 { "AL_FORMAT_MONO8", DevFmtMono
, DevFmtByte
},
2396 { "AL_FORMAT_STEREO8", DevFmtStereo
, DevFmtByte
},
2397 { "AL_FORMAT_QUAD8", DevFmtQuad
, DevFmtByte
},
2398 { "AL_FORMAT_51CHN8", DevFmtX51
, DevFmtByte
},
2399 { "AL_FORMAT_61CHN8", DevFmtX61
, DevFmtByte
},
2400 { "AL_FORMAT_71CHN8", DevFmtX71
, DevFmtByte
}
2404 for(i
= 0;i
< sizeof(formats
)/sizeof(formats
[0]);i
++)
2406 if(strcasecmp(str
, formats
[i
].name
) == 0)
2408 *chans
= formats
[i
].channels
;
2409 *type
= formats
[i
].type
;
2414 ERR("Unknown format: \"%s\"\n", str
);
2415 *chans
= DevFmtStereo
;
2416 *type
= DevFmtShort
;
2421 * Open the Device specified.
2423 ALC_API ALCdevice
* ALC_APIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
2431 if(!PlaybackBackend
.name
)
2433 alcSetError(NULL
, ALC_INVALID_VALUE
);
2437 if(deviceName
&& (!deviceName
[0] || strcasecmp(deviceName
, "openal soft") == 0 || strcasecmp(deviceName
, "openal-soft") == 0))
2440 device
= calloc(1, sizeof(ALCdevice
));
2443 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
2448 device
->Funcs
= &PlaybackBackend
.Funcs
;
2450 device
->Connected
= ALC_TRUE
;
2451 device
->IsCaptureDevice
= AL_FALSE
;
2452 device
->IsLoopbackDevice
= AL_FALSE
;
2453 InitializeCriticalSection(&device
->Mutex
);
2454 device
->LastError
= ALC_NO_ERROR
;
2457 device
->Bs2b
= NULL
;
2458 device
->szDeviceName
= NULL
;
2460 device
->ContextList
= NULL
;
2462 InitUIntMap(&device
->BufferMap
, ~0);
2463 InitUIntMap(&device
->EffectMap
, ~0);
2464 InitUIntMap(&device
->FilterMap
, ~0);
2467 if(ConfigValueExists(NULL
, "frequency"))
2468 device
->Flags
|= DEVICE_FREQUENCY_REQUEST
;
2469 device
->Frequency
= GetConfigValueInt(NULL
, "frequency", DEFAULT_OUTPUT_RATE
);
2470 if(device
->Frequency
< 8000)
2471 device
->Frequency
= 8000;
2473 if(ConfigValueExists(NULL
, "format"))
2474 device
->Flags
|= DEVICE_CHANNELS_REQUEST
;
2475 fmt
= GetConfigValue(NULL
, "format", "AL_FORMAT_STEREO16");
2476 GetFormatFromString(fmt
, &device
->FmtChans
, &device
->FmtType
);
2478 device
->NumUpdates
= GetConfigValueInt(NULL
, "periods", 4);
2479 if(device
->NumUpdates
< 2)
2480 device
->NumUpdates
= 4;
2482 device
->UpdateSize
= GetConfigValueInt(NULL
, "period_size", 1024);
2483 if(device
->UpdateSize
<= 0)
2484 device
->UpdateSize
= 1024;
2486 device
->MaxNoOfSources
= GetConfigValueInt(NULL
, "sources", 256);
2487 if(device
->MaxNoOfSources
<= 0)
2488 device
->MaxNoOfSources
= 256;
2490 device
->AuxiliaryEffectSlotMax
= GetConfigValueInt(NULL
, "slots", 4);
2491 if(device
->AuxiliaryEffectSlotMax
<= 0)
2492 device
->AuxiliaryEffectSlotMax
= 4;
2494 device
->NumStereoSources
= 1;
2495 device
->NumMonoSources
= device
->MaxNoOfSources
- device
->NumStereoSources
;
2497 device
->NumAuxSends
= GetConfigValueInt(NULL
, "sends", MAX_SENDS
);
2498 if(device
->NumAuxSends
> MAX_SENDS
)
2499 device
->NumAuxSends
= MAX_SENDS
;
2501 device
->Bs2bLevel
= GetConfigValueInt(NULL
, "cf_level", 0);
2503 // Find a playback device to open
2505 if((err
=ALCdevice_OpenPlayback(device
, deviceName
)) != ALC_NO_ERROR
)
2508 DeleteCriticalSection(&device
->Mutex
);
2510 alcSetError(NULL
, err
);
2516 device
->next
= DeviceList
;
2517 } while(!CompExchangePtr((void**)&DeviceList
, device
->next
, device
));
2524 * Close the specified Device
2526 ALC_API ALCboolean ALC_APIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
2528 ALCdevice
*volatile*list
;
2533 while(*list
&& *list
!= pDevice
)
2534 list
= &(*list
)->next
;
2536 if(!*list
|| (*list
)->IsCaptureDevice
)
2538 alcSetError(*list
, ALC_INVALID_DEVICE
);
2543 *list
= (*list
)->next
;
2546 if((ctx
=pDevice
->ContextList
) != NULL
)
2549 WARN("Destroying context %p\n", ctx
);
2550 ReleaseContext(ctx
, pDevice
);
2551 } while((ctx
=pDevice
->ContextList
) != NULL
);
2552 ALCdevice_StopPlayback(pDevice
);
2553 pDevice
->Flags
&= ~DEVICE_RUNNING
;
2555 ALCdevice_ClosePlayback(pDevice
);
2557 ALCdevice_DecRef(pDevice
);
2563 /* alcLoopbackOpenDeviceSOFT
2565 * Open a loopback device, for manual rendering.
2567 ALC_API ALCdevice
* ALC_APIENTRY
alcLoopbackOpenDeviceSOFT(void)
2573 device
= calloc(1, sizeof(ALCdevice
));
2576 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
2581 device
->Funcs
= &BackendLoopback
.Funcs
;
2583 device
->Connected
= ALC_TRUE
;
2584 device
->IsCaptureDevice
= AL_FALSE
;
2585 device
->IsLoopbackDevice
= AL_TRUE
;
2586 InitializeCriticalSection(&device
->Mutex
);
2587 device
->LastError
= ALC_NO_ERROR
;
2590 device
->Bs2b
= NULL
;
2591 device
->szDeviceName
= NULL
;
2593 device
->ContextList
= NULL
;
2595 InitUIntMap(&device
->BufferMap
, ~0);
2596 InitUIntMap(&device
->EffectMap
, ~0);
2597 InitUIntMap(&device
->FilterMap
, ~0);
2600 device
->Frequency
= 44100;
2601 device
->FmtChans
= DevFmtStereo
;
2602 device
->FmtType
= DevFmtShort
;
2604 device
->NumUpdates
= 0;
2605 device
->UpdateSize
= 0;
2607 device
->MaxNoOfSources
= GetConfigValueInt(NULL
, "sources", 256);
2608 if(device
->MaxNoOfSources
<= 0)
2609 device
->MaxNoOfSources
= 256;
2611 device
->AuxiliaryEffectSlotMax
= GetConfigValueInt(NULL
, "slots", 4);
2612 if(device
->AuxiliaryEffectSlotMax
<= 0)
2613 device
->AuxiliaryEffectSlotMax
= 4;
2615 device
->NumStereoSources
= 1;
2616 device
->NumMonoSources
= device
->MaxNoOfSources
- device
->NumStereoSources
;
2618 device
->NumAuxSends
= GetConfigValueInt(NULL
, "sends", MAX_SENDS
);
2619 if(device
->NumAuxSends
> MAX_SENDS
)
2620 device
->NumAuxSends
= MAX_SENDS
;
2622 device
->Bs2bLevel
= GetConfigValueInt(NULL
, "cf_level", 0);
2624 // Open the "backend"
2625 ALCdevice_OpenPlayback(device
, "Loopback");
2627 device
->next
= DeviceList
;
2628 } while(!CompExchangePtr((void**)&DeviceList
, device
->next
, device
));
2632 /* alcIsRenderFormatSupportedSOFT
2634 * Determines if the loopback device supports the given format for rendering.
2636 ALC_API ALCboolean ALC_APIENTRY
alcIsRenderFormatSupportedSOFT(ALCdevice
*device
, ALCsizei freq
, ALCenum channels
, ALCenum type
)
2638 ALCboolean ret
= ALC_FALSE
;
2640 if(!(device
=VerifyDevice(device
)) || !device
->IsLoopbackDevice
)
2641 alcSetError(device
, ALC_INVALID_DEVICE
);
2643 alcSetError(device
, ALC_INVALID_VALUE
);
2644 else if(!IsValidALCType(type
) || !IsValidALCChannels(channels
))
2645 alcSetError(device
, ALC_INVALID_ENUM
);
2648 if(BytesFromDevFmt(type
) > 0 && ChannelsFromDevFmt(channels
) > 0 &&
2652 if(device
) ALCdevice_DecRef(device
);
2657 /* alcRenderSamplesSOFT
2659 * Renders some samples into a buffer, using the format last set by the
2660 * attributes given to alcCreateContext.
2662 ALC_API
void ALC_APIENTRY
alcRenderSamplesSOFT(ALCdevice
*device
, ALCvoid
*buffer
, ALCsizei samples
)
2664 if(!(device
=VerifyDevice(device
)) || !device
->IsLoopbackDevice
)
2665 alcSetError(device
, ALC_INVALID_DEVICE
);
2666 else if(samples
< 0 || (samples
> 0 && buffer
== NULL
))
2667 alcSetError(device
, ALC_INVALID_VALUE
);
2669 aluMixData(device
, buffer
, samples
);
2670 if(device
) ALCdevice_DecRef(device
);
2674 static void ReleaseALC(ALCboolean doclose
)
2676 free(alcDeviceList
); alcDeviceList
= NULL
;
2677 alcDeviceListSize
= 0;
2678 free(alcAllDeviceList
); alcAllDeviceList
= NULL
;
2679 alcAllDeviceListSize
= 0;
2680 free(alcCaptureDeviceList
); alcCaptureDeviceList
= NULL
;
2681 alcCaptureDeviceListSize
= 0;
2683 free(alcDefaultDeviceSpecifier
);
2684 alcDefaultDeviceSpecifier
= NULL
;
2685 free(alcDefaultAllDeviceSpecifier
);
2686 alcDefaultAllDeviceSpecifier
= NULL
;
2687 free(alcCaptureDefaultDeviceSpecifier
);
2688 alcCaptureDefaultDeviceSpecifier
= NULL
;
2693 while((dev
=DeviceList
) != NULL
)
2695 WARN("Closing device %p\n", dev
);
2696 if(!dev
->IsCaptureDevice
) alcCloseDevice(dev
);
2697 else alcCaptureCloseDevice(dev
);
2703 if((dev
=DeviceList
) != NULL
)
2708 } while((dev
=dev
->next
) != NULL
);
2709 WARN("%u device%s not closed\n", num
, (num
>1)?"s":"");
2714 ///////////////////////////////////////////////////////