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
36 #include "alAuxEffectSlot.h"
41 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
42 static struct BackendInfo BackendList
[] = {
43 #ifdef HAVE_PULSEAUDIO
44 { "pulse", alc_pulse_init
, alc_pulse_deinit
, alc_pulse_probe
, EmptyFuncs
},
47 { "alsa", alc_alsa_init
, alc_alsa_deinit
, alc_alsa_probe
, EmptyFuncs
},
50 { "core", alc_ca_init
, alc_ca_deinit
, alc_ca_probe
, EmptyFuncs
},
53 { "oss", alc_oss_init
, alc_oss_deinit
, alc_oss_probe
, EmptyFuncs
},
56 { "solaris", alc_solaris_init
, alc_solaris_deinit
, alc_solaris_probe
, EmptyFuncs
},
59 { "sndio", alc_sndio_init
, alc_sndio_deinit
, alc_sndio_probe
, EmptyFuncs
},
62 { "mmdevapi", alcMMDevApiInit
, alcMMDevApiDeinit
, alcMMDevApiProbe
, EmptyFuncs
},
65 { "dsound", alcDSoundInit
, alcDSoundDeinit
, alcDSoundProbe
, EmptyFuncs
},
68 { "winmm", alcWinMMInit
, alcWinMMDeinit
, alcWinMMProbe
, EmptyFuncs
},
71 { "port", alc_pa_init
, alc_pa_deinit
, alc_pa_probe
, EmptyFuncs
},
74 { "opensl", alc_opensl_init
, alc_opensl_deinit
, alc_opensl_probe
, EmptyFuncs
},
77 { "null", alc_null_init
, alc_null_deinit
, alc_null_probe
, EmptyFuncs
},
79 { "wave", alc_wave_init
, alc_wave_deinit
, alc_wave_probe
, EmptyFuncs
},
82 { NULL
, NULL
, NULL
, NULL
, EmptyFuncs
}
84 static struct BackendInfo BackendLoopback
= {
85 "loopback", alc_loopback_init
, alc_loopback_deinit
, alc_loopback_probe
, EmptyFuncs
89 static struct BackendInfo PlaybackBackend
;
90 static struct BackendInfo CaptureBackend
;
92 ///////////////////////////////////////////////////////
93 // STRING and EXTENSIONS
95 typedef struct ALCfunction
{
96 const ALCchar
*funcName
;
100 typedef struct ALCenums
{
101 const ALCchar
*enumName
;
106 static const ALCfunction alcFunctions
[] = {
107 { "alcCreateContext", (ALCvoid
*) alcCreateContext
},
108 { "alcMakeContextCurrent", (ALCvoid
*) alcMakeContextCurrent
},
109 { "alcProcessContext", (ALCvoid
*) alcProcessContext
},
110 { "alcSuspendContext", (ALCvoid
*) alcSuspendContext
},
111 { "alcDestroyContext", (ALCvoid
*) alcDestroyContext
},
112 { "alcGetCurrentContext", (ALCvoid
*) alcGetCurrentContext
},
113 { "alcGetContextsDevice", (ALCvoid
*) alcGetContextsDevice
},
114 { "alcOpenDevice", (ALCvoid
*) alcOpenDevice
},
115 { "alcCloseDevice", (ALCvoid
*) alcCloseDevice
},
116 { "alcGetError", (ALCvoid
*) alcGetError
},
117 { "alcIsExtensionPresent", (ALCvoid
*) alcIsExtensionPresent
},
118 { "alcGetProcAddress", (ALCvoid
*) alcGetProcAddress
},
119 { "alcGetEnumValue", (ALCvoid
*) alcGetEnumValue
},
120 { "alcGetString", (ALCvoid
*) alcGetString
},
121 { "alcGetIntegerv", (ALCvoid
*) alcGetIntegerv
},
122 { "alcCaptureOpenDevice", (ALCvoid
*) alcCaptureOpenDevice
},
123 { "alcCaptureCloseDevice", (ALCvoid
*) alcCaptureCloseDevice
},
124 { "alcCaptureStart", (ALCvoid
*) alcCaptureStart
},
125 { "alcCaptureStop", (ALCvoid
*) alcCaptureStop
},
126 { "alcCaptureSamples", (ALCvoid
*) alcCaptureSamples
},
128 { "alcSetThreadContext", (ALCvoid
*) alcSetThreadContext
},
129 { "alcGetThreadContext", (ALCvoid
*) alcGetThreadContext
},
131 { "alcLoopbackOpenDeviceSOFT", (ALCvoid
*) alcLoopbackOpenDeviceSOFT
},
132 { "alcIsRenderFormatSupportedSOFT",(ALCvoid
*) alcIsRenderFormatSupportedSOFT
},
133 { "alcRenderSamplesSOFT", (ALCvoid
*) alcRenderSamplesSOFT
},
135 { "alEnable", (ALCvoid
*) alEnable
},
136 { "alDisable", (ALCvoid
*) alDisable
},
137 { "alIsEnabled", (ALCvoid
*) alIsEnabled
},
138 { "alGetString", (ALCvoid
*) alGetString
},
139 { "alGetBooleanv", (ALCvoid
*) alGetBooleanv
},
140 { "alGetIntegerv", (ALCvoid
*) alGetIntegerv
},
141 { "alGetFloatv", (ALCvoid
*) alGetFloatv
},
142 { "alGetDoublev", (ALCvoid
*) alGetDoublev
},
143 { "alGetBoolean", (ALCvoid
*) alGetBoolean
},
144 { "alGetInteger", (ALCvoid
*) alGetInteger
},
145 { "alGetFloat", (ALCvoid
*) alGetFloat
},
146 { "alGetDouble", (ALCvoid
*) alGetDouble
},
147 { "alGetError", (ALCvoid
*) alGetError
},
148 { "alIsExtensionPresent", (ALCvoid
*) alIsExtensionPresent
},
149 { "alGetProcAddress", (ALCvoid
*) alGetProcAddress
},
150 { "alGetEnumValue", (ALCvoid
*) alGetEnumValue
},
151 { "alListenerf", (ALCvoid
*) alListenerf
},
152 { "alListener3f", (ALCvoid
*) alListener3f
},
153 { "alListenerfv", (ALCvoid
*) alListenerfv
},
154 { "alListeneri", (ALCvoid
*) alListeneri
},
155 { "alListener3i", (ALCvoid
*) alListener3i
},
156 { "alListeneriv", (ALCvoid
*) alListeneriv
},
157 { "alGetListenerf", (ALCvoid
*) alGetListenerf
},
158 { "alGetListener3f", (ALCvoid
*) alGetListener3f
},
159 { "alGetListenerfv", (ALCvoid
*) alGetListenerfv
},
160 { "alGetListeneri", (ALCvoid
*) alGetListeneri
},
161 { "alGetListener3i", (ALCvoid
*) alGetListener3i
},
162 { "alGetListeneriv", (ALCvoid
*) alGetListeneriv
},
163 { "alGenSources", (ALCvoid
*) alGenSources
},
164 { "alDeleteSources", (ALCvoid
*) alDeleteSources
},
165 { "alIsSource", (ALCvoid
*) alIsSource
},
166 { "alSourcef", (ALCvoid
*) alSourcef
},
167 { "alSource3f", (ALCvoid
*) alSource3f
},
168 { "alSourcefv", (ALCvoid
*) alSourcefv
},
169 { "alSourcei", (ALCvoid
*) alSourcei
},
170 { "alSource3i", (ALCvoid
*) alSource3i
},
171 { "alSourceiv", (ALCvoid
*) alSourceiv
},
172 { "alGetSourcef", (ALCvoid
*) alGetSourcef
},
173 { "alGetSource3f", (ALCvoid
*) alGetSource3f
},
174 { "alGetSourcefv", (ALCvoid
*) alGetSourcefv
},
175 { "alGetSourcei", (ALCvoid
*) alGetSourcei
},
176 { "alGetSource3i", (ALCvoid
*) alGetSource3i
},
177 { "alGetSourceiv", (ALCvoid
*) alGetSourceiv
},
178 { "alSourcePlayv", (ALCvoid
*) alSourcePlayv
},
179 { "alSourceStopv", (ALCvoid
*) alSourceStopv
},
180 { "alSourceRewindv", (ALCvoid
*) alSourceRewindv
},
181 { "alSourcePausev", (ALCvoid
*) alSourcePausev
},
182 { "alSourcePlay", (ALCvoid
*) alSourcePlay
},
183 { "alSourceStop", (ALCvoid
*) alSourceStop
},
184 { "alSourceRewind", (ALCvoid
*) alSourceRewind
},
185 { "alSourcePause", (ALCvoid
*) alSourcePause
},
186 { "alSourceQueueBuffers", (ALCvoid
*) alSourceQueueBuffers
},
187 { "alSourceUnqueueBuffers", (ALCvoid
*) alSourceUnqueueBuffers
},
188 { "alGenBuffers", (ALCvoid
*) alGenBuffers
},
189 { "alDeleteBuffers", (ALCvoid
*) alDeleteBuffers
},
190 { "alIsBuffer", (ALCvoid
*) alIsBuffer
},
191 { "alBufferData", (ALCvoid
*) alBufferData
},
192 { "alBufferf", (ALCvoid
*) alBufferf
},
193 { "alBuffer3f", (ALCvoid
*) alBuffer3f
},
194 { "alBufferfv", (ALCvoid
*) alBufferfv
},
195 { "alBufferi", (ALCvoid
*) alBufferi
},
196 { "alBuffer3i", (ALCvoid
*) alBuffer3i
},
197 { "alBufferiv", (ALCvoid
*) alBufferiv
},
198 { "alGetBufferf", (ALCvoid
*) alGetBufferf
},
199 { "alGetBuffer3f", (ALCvoid
*) alGetBuffer3f
},
200 { "alGetBufferfv", (ALCvoid
*) alGetBufferfv
},
201 { "alGetBufferi", (ALCvoid
*) alGetBufferi
},
202 { "alGetBuffer3i", (ALCvoid
*) alGetBuffer3i
},
203 { "alGetBufferiv", (ALCvoid
*) alGetBufferiv
},
204 { "alDopplerFactor", (ALCvoid
*) alDopplerFactor
},
205 { "alDopplerVelocity", (ALCvoid
*) alDopplerVelocity
},
206 { "alSpeedOfSound", (ALCvoid
*) alSpeedOfSound
},
207 { "alDistanceModel", (ALCvoid
*) alDistanceModel
},
209 { "alGenFilters", (ALCvoid
*) alGenFilters
},
210 { "alDeleteFilters", (ALCvoid
*) alDeleteFilters
},
211 { "alIsFilter", (ALCvoid
*) alIsFilter
},
212 { "alFilteri", (ALCvoid
*) alFilteri
},
213 { "alFilteriv", (ALCvoid
*) alFilteriv
},
214 { "alFilterf", (ALCvoid
*) alFilterf
},
215 { "alFilterfv", (ALCvoid
*) alFilterfv
},
216 { "alGetFilteri", (ALCvoid
*) alGetFilteri
},
217 { "alGetFilteriv", (ALCvoid
*) alGetFilteriv
},
218 { "alGetFilterf", (ALCvoid
*) alGetFilterf
},
219 { "alGetFilterfv", (ALCvoid
*) alGetFilterfv
},
220 { "alGenEffects", (ALCvoid
*) alGenEffects
},
221 { "alDeleteEffects", (ALCvoid
*) alDeleteEffects
},
222 { "alIsEffect", (ALCvoid
*) alIsEffect
},
223 { "alEffecti", (ALCvoid
*) alEffecti
},
224 { "alEffectiv", (ALCvoid
*) alEffectiv
},
225 { "alEffectf", (ALCvoid
*) alEffectf
},
226 { "alEffectfv", (ALCvoid
*) alEffectfv
},
227 { "alGetEffecti", (ALCvoid
*) alGetEffecti
},
228 { "alGetEffectiv", (ALCvoid
*) alGetEffectiv
},
229 { "alGetEffectf", (ALCvoid
*) alGetEffectf
},
230 { "alGetEffectfv", (ALCvoid
*) alGetEffectfv
},
231 { "alGenAuxiliaryEffectSlots", (ALCvoid
*) alGenAuxiliaryEffectSlots
},
232 { "alDeleteAuxiliaryEffectSlots",(ALCvoid
*) alDeleteAuxiliaryEffectSlots
},
233 { "alIsAuxiliaryEffectSlot", (ALCvoid
*) alIsAuxiliaryEffectSlot
},
234 { "alAuxiliaryEffectSloti", (ALCvoid
*) alAuxiliaryEffectSloti
},
235 { "alAuxiliaryEffectSlotiv", (ALCvoid
*) alAuxiliaryEffectSlotiv
},
236 { "alAuxiliaryEffectSlotf", (ALCvoid
*) alAuxiliaryEffectSlotf
},
237 { "alAuxiliaryEffectSlotfv", (ALCvoid
*) alAuxiliaryEffectSlotfv
},
238 { "alGetAuxiliaryEffectSloti", (ALCvoid
*) alGetAuxiliaryEffectSloti
},
239 { "alGetAuxiliaryEffectSlotiv", (ALCvoid
*) alGetAuxiliaryEffectSlotiv
},
240 { "alGetAuxiliaryEffectSlotf", (ALCvoid
*) alGetAuxiliaryEffectSlotf
},
241 { "alGetAuxiliaryEffectSlotfv", (ALCvoid
*) alGetAuxiliaryEffectSlotfv
},
243 { "alBufferSubDataSOFT", (ALCvoid
*) alBufferSubDataSOFT
},
245 { "alBufferSamplesSOFT", (ALCvoid
*) alBufferSamplesSOFT
},
246 { "alBufferSubSamplesSOFT", (ALCvoid
*) alBufferSubSamplesSOFT
},
247 { "alGetBufferSamplesSOFT", (ALCvoid
*) alGetBufferSamplesSOFT
},
248 { "alIsBufferFormatSupportedSOFT",(ALCvoid
*) alIsBufferFormatSupportedSOFT
},
250 { "alDeferUpdatesSOFT", (ALCvoid
*) alDeferUpdatesSOFT
},
251 { "alProcessUpdatesSOFT", (ALCvoid
*) alProcessUpdatesSOFT
},
253 { NULL
, (ALCvoid
*) NULL
}
256 static const ALCenums enumeration
[] = {
258 { "ALC_INVALID", ALC_INVALID
},
259 { "ALC_FALSE", ALC_FALSE
},
260 { "ALC_TRUE", ALC_TRUE
},
263 { "ALC_MAJOR_VERSION", ALC_MAJOR_VERSION
},
264 { "ALC_MINOR_VERSION", ALC_MINOR_VERSION
},
265 { "ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE
},
266 { "ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES
},
267 { "ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER
},
268 { "ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER
},
269 { "ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER
},
270 { "ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER
},
271 { "ALC_EXTENSIONS", ALC_EXTENSIONS
},
272 { "ALC_FREQUENCY", ALC_FREQUENCY
},
273 { "ALC_REFRESH", ALC_REFRESH
},
274 { "ALC_SYNC", ALC_SYNC
},
275 { "ALC_MONO_SOURCES", ALC_MONO_SOURCES
},
276 { "ALC_STEREO_SOURCES", ALC_STEREO_SOURCES
},
277 { "ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER
},
278 { "ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
},
279 { "ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES
},
280 { "ALC_CONNECTED", ALC_CONNECTED
},
283 { "ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION
},
284 { "ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION
},
285 { "ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS
},
287 // Loopback device Properties
288 { "ALC_FORMAT_CHANNELS_SOFT", ALC_FORMAT_CHANNELS_SOFT
},
289 { "ALC_FORMAT_TYPE_SOFT", ALC_FORMAT_TYPE_SOFT
},
291 // Buffer Channel Configurations
292 { "ALC_MONO", ALC_MONO
},
293 { "ALC_STEREO", ALC_STEREO
},
294 { "ALC_QUAD", ALC_QUAD
},
295 { "ALC_5POINT1", ALC_5POINT1
},
296 { "ALC_6POINT1", ALC_6POINT1
},
297 { "ALC_7POINT1", ALC_7POINT1
},
299 // Buffer Sample Types
300 { "ALC_BYTE", ALC_BYTE
},
301 { "ALC_UNSIGNED_BYTE", ALC_UNSIGNED_BYTE
},
302 { "ALC_SHORT", ALC_SHORT
},
303 { "ALC_UNSIGNED_SHORT", ALC_UNSIGNED_SHORT
},
304 { "ALC_INT", ALC_INT
},
305 { "ALC_UNSIGNED_INT", ALC_UNSIGNED_INT
},
306 { "ALC_FLOAT", ALC_FLOAT
},
309 { "ALC_NO_ERROR", ALC_NO_ERROR
},
310 { "ALC_INVALID_DEVICE", ALC_INVALID_DEVICE
},
311 { "ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT
},
312 { "ALC_INVALID_ENUM", ALC_INVALID_ENUM
},
313 { "ALC_INVALID_VALUE", ALC_INVALID_VALUE
},
314 { "ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY
},
319 static const ALCchar alcNoError
[] = "No Error";
320 static const ALCchar alcErrInvalidDevice
[] = "Invalid Device";
321 static const ALCchar alcErrInvalidContext
[] = "Invalid Context";
322 static const ALCchar alcErrInvalidEnum
[] = "Invalid Enum";
323 static const ALCchar alcErrInvalidValue
[] = "Invalid Value";
324 static const ALCchar alcErrOutOfMemory
[] = "Out of Memory";
326 /* Device lists. Sizes only include the first ending null character, not the
328 static ALCchar
*alcDeviceList
;
329 static size_t alcDeviceListSize
;
330 static ALCchar
*alcAllDeviceList
;
331 static size_t alcAllDeviceListSize
;
332 static ALCchar
*alcCaptureDeviceList
;
333 static size_t alcCaptureDeviceListSize
;
334 /* Default is always the first in the list */
335 static ALCchar
*alcDefaultDeviceSpecifier
;
336 static ALCchar
*alcDefaultAllDeviceSpecifier
;
337 static ALCchar
*alcCaptureDefaultDeviceSpecifier
;
340 static const ALCchar alcNoDeviceExtList
[] =
341 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
342 "ALC_EXT_thread_local_context ALC_SOFTX_loopback_device";
343 static const ALCchar alcExtensionList
[] =
344 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
345 "ALC_EXT_DEDICATED ALC_EXT_disconnect ALC_EXT_EFX "
346 "ALC_EXT_thread_local_context ALC_SOFTX_loopback_device";
347 static const ALCint alcMajorVersion
= 1;
348 static const ALCint alcMinorVersion
= 1;
350 static const ALCint alcEFXMajorVersion
= 1;
351 static const ALCint alcEFXMinorVersion
= 0;
353 ///////////////////////////////////////////////////////
356 ///////////////////////////////////////////////////////
359 static CRITICAL_SECTION ListLock
;
362 static ALCdevice
*g_pDeviceList
= NULL
;
363 static ALCuint g_ulDeviceCount
= 0;
366 static ALCcontext
*g_pContextList
= NULL
;
367 static ALCuint g_ulContextCount
= 0;
369 // Thread-local current context
370 static pthread_key_t LocalContext
;
371 // Process-wide current context
372 static ALCcontext
*GlobalContext
;
375 static ALCenum g_eLastNullDeviceError
= ALC_NO_ERROR
;
377 // Default context extensions
378 static const ALchar alExtList
[] =
379 "AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 AL_EXT_IMA4 "
380 "AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_MULAW "
381 "AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET AL_EXT_source_distance_model "
382 "AL_LOKI_quadriphonic AL_SOFTX_buffer_samples AL_SOFT_buffer_sub_data "
383 "AL_SOFTX_deferred_updates AL_SOFT_loop_points "
384 "AL_SOFTX_non_virtual_channels";
386 // Mixing Priority Level
394 enum LogLevel LogLevel
= LogWarning
;
396 enum LogLevel LogLevel
= LogError
;
400 ALdouble ConeScale
= 0.5;
402 // Localized Z scalar for mono sources
403 ALdouble ZScale
= 1.0;
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
);
440 case DLL_THREAD_DETACH
:
441 for(i
= 0;i
< TlsDestructor
.size
;i
++)
443 void *ptr
= pthread_getspecific(TlsDestructor
.array
[i
].key
);
444 void (*callback
)(void*) = (void(*)(void*))TlsDestructor
.array
[i
].value
;
450 case DLL_PROCESS_DETACH
:
455 ResetUIntMap(&TlsDestructor
);
460 #elif defined(_MSC_VER)
461 #pragma section(".CRT$XCU",read)
462 static void alc_constructor(void);
463 static void alc_destructor(void);
464 __declspec(allocate(".CRT$XCU")) void (__cdecl
* alc_constructor_
)(void) = alc_constructor
;
466 static void alc_constructor(void)
468 atexit(alc_destructor
);
472 static void alc_destructor(void)
476 #elif defined(HAVE_GCC_DESTRUCTOR)
477 static void alc_init(void) __attribute__((constructor
));
478 static void alc_deinit(void) __attribute__((destructor
));
480 #error "No static initialization available on this platform!"
482 #elif defined(HAVE_GCC_DESTRUCTOR)
483 static void alc_init(void) __attribute__((constructor
));
484 static void alc_deinit(void) __attribute__((destructor
));
486 #error "No global initialization available on this platform!"
489 static void alc_init(void)
495 str
= getenv("__ALSOFT_HALF_ANGLE_CONES");
496 if(str
&& (strcasecmp(str
, "true") == 0 || strtol(str
, NULL
, 0) == 1))
499 str
= getenv("__ALSOFT_REVERSE_Z");
500 if(str
&& (strcasecmp(str
, "true") == 0 || strtol(str
, NULL
, 0) == 1))
503 pthread_key_create(&LocalContext
, ReleaseThreadCtx
);
504 InitializeCriticalSection(&ListLock
);
508 static void alc_deinit_safe(void)
510 ReleaseALC(ALC_FALSE
);
515 DeleteCriticalSection(&ListLock
);
516 pthread_key_delete(LocalContext
);
518 if(LogFile
!= stderr
)
523 static void alc_deinit(void)
527 ReleaseALC(ALC_TRUE
);
529 memset(&PlaybackBackend
, 0, sizeof(PlaybackBackend
));
530 memset(&CaptureBackend
, 0, sizeof(CaptureBackend
));
532 for(i
= 0;BackendList
[i
].Deinit
;i
++)
533 BackendList
[i
].Deinit();
534 BackendLoopback
.Deinit();
539 static void alc_initconfig(void)
541 const char *devs
, *str
;
544 str
= getenv("ALSOFT_LOGLEVEL");
547 long lvl
= strtol(str
, NULL
, 0);
548 if(lvl
>= NoLog
&& lvl
<= LogTrace
)
552 str
= getenv("ALSOFT_LOGFILE");
555 FILE *logfile
= fopen(str
, "wat");
556 if(logfile
) LogFile
= logfile
;
557 else ERR("Failed to open log file '%s'\n", str
);
565 RTPrioLevel
= GetConfigValueInt(NULL
, "rt-prio", 1);
567 RTPrioLevel
= GetConfigValueInt(NULL
, "rt-prio", 0);
570 DefaultResampler
= GetConfigValueInt(NULL
, "resampler", RESAMPLER_DEFAULT
);
571 if(DefaultResampler
>= RESAMPLER_MAX
|| DefaultResampler
<= RESAMPLER_MIN
)
572 DefaultResampler
= RESAMPLER_DEFAULT
;
574 ReverbBoost
*= aluPow(10.0f
, GetConfigValueFloat("reverb", "boost", 0.0f
) /
576 EmulateEAXReverb
= GetConfigValueBool("reverb", "emulate-eax", AL_FALSE
);
578 devs
= GetConfigValue(NULL
, "drivers", "");
583 const char *next
= devs
;
584 int endlist
, delitem
;
589 next
= strchr(devs
, ',');
591 delitem
= (devs
[0] == '-');
592 if(devs
[0] == '-') devs
++;
594 if(!devs
[0] || devs
[0] == ',')
601 len
= (next
? ((size_t)(next
-devs
)) : strlen(devs
));
602 for(n
= i
;BackendList
[n
].Init
;n
++)
604 if(len
== strlen(BackendList
[n
].name
) &&
605 strncmp(BackendList
[n
].name
, devs
, len
) == 0)
610 BackendList
[n
] = BackendList
[n
+1];
612 } while(BackendList
[n
].Init
);
616 struct BackendInfo Bkp
= BackendList
[n
];
619 BackendList
[n
] = BackendList
[n
-1];
622 BackendList
[n
] = Bkp
;
633 BackendList
[i
].name
= NULL
;
634 BackendList
[i
].Init
= NULL
;
635 BackendList
[i
].Deinit
= NULL
;
636 BackendList
[i
].Probe
= NULL
;
640 for(i
= 0;BackendList
[i
].Init
&& (!PlaybackBackend
.name
|| !CaptureBackend
.name
);i
++)
642 if(!BackendList
[i
].Init(&BackendList
[i
].Funcs
))
644 WARN("Failed to initialize backend \"%s\"\n", BackendList
[i
].name
);
648 TRACE("Initialized backend \"%s\"\n", BackendList
[i
].name
);
649 if(BackendList
[i
].Funcs
.OpenPlayback
&& !PlaybackBackend
.name
)
651 PlaybackBackend
= BackendList
[i
];
652 TRACE("Added \"%s\" for playback\n", PlaybackBackend
.name
);
654 if(BackendList
[i
].Funcs
.OpenCapture
&& !CaptureBackend
.name
)
656 CaptureBackend
= BackendList
[i
];
657 TRACE("Added \"%s\" for capture\n", CaptureBackend
.name
);
660 BackendLoopback
.Init(&BackendLoopback
.Funcs
);
662 str
= GetConfigValue(NULL
, "excludefx", "");
666 const char *next
= str
;
670 next
= strchr(str
, ',');
672 if(!str
[0] || next
== str
)
675 len
= (next
? ((size_t)(next
-str
)) : strlen(str
));
676 for(n
= 0;EffectList
[n
].name
;n
++)
678 if(len
== strlen(EffectList
[n
].name
) &&
679 strncmp(EffectList
[n
].name
, str
, len
) == 0)
680 DisabledEffects
[EffectList
[n
].type
] = AL_TRUE
;
687 static void ProbeList(ALCchar
**list
, size_t *listsize
, enum DevProbe type
)
694 if(type
== CAPTURE_DEVICE_PROBE
)
695 CaptureBackend
.Probe(type
);
697 PlaybackBackend
.Probe(type
);
700 static void ProbeDeviceList(void)
701 { ProbeList(&alcDeviceList
, &alcDeviceListSize
, DEVICE_PROBE
); }
702 static void ProbeAllDeviceList(void)
703 { ProbeList(&alcAllDeviceList
, &alcAllDeviceListSize
, ALL_DEVICE_PROBE
); }
704 static void ProbeCaptureDeviceList(void)
705 { ProbeList(&alcCaptureDeviceList
, &alcCaptureDeviceListSize
, CAPTURE_DEVICE_PROBE
); }
708 static void AppendList(const ALCchar
*name
, ALCchar
**List
, size_t *ListSize
)
710 size_t len
= strlen(name
);
716 temp
= realloc(*List
, (*ListSize
) + len
+ 2);
719 ERR("Realloc failed to add %s!\n", name
);
724 memcpy((*List
)+(*ListSize
), name
, len
+1);
726 (*List
)[*ListSize
] = 0;
729 #define DECL_APPEND_LIST_FUNC(type) \
730 void Append##type##List(const ALCchar *name) \
731 { AppendList(name, &alc##type##List, &alc##type##ListSize); }
733 DECL_APPEND_LIST_FUNC(Device
)
734 DECL_APPEND_LIST_FUNC(AllDevice
)
735 DECL_APPEND_LIST_FUNC(CaptureDevice
)
737 #undef DECL_APPEND_LIST_FUNC
740 /* Sets the default channel order used by most non-WaveFormatEx-based APIs */
741 void SetDefaultChannelOrder(ALCdevice
*device
)
743 switch(device
->FmtChans
)
745 case DevFmtX51
: device
->DevChannels
[0] = FRONT_LEFT
;
746 device
->DevChannels
[1] = FRONT_RIGHT
;
747 device
->DevChannels
[2] = BACK_LEFT
;
748 device
->DevChannels
[3] = BACK_RIGHT
;
749 device
->DevChannels
[4] = FRONT_CENTER
;
750 device
->DevChannels
[5] = LFE
;
753 case DevFmtX71
: device
->DevChannels
[0] = FRONT_LEFT
;
754 device
->DevChannels
[1] = FRONT_RIGHT
;
755 device
->DevChannels
[2] = BACK_LEFT
;
756 device
->DevChannels
[3] = BACK_RIGHT
;
757 device
->DevChannels
[4] = FRONT_CENTER
;
758 device
->DevChannels
[5] = LFE
;
759 device
->DevChannels
[6] = SIDE_LEFT
;
760 device
->DevChannels
[7] = SIDE_RIGHT
;
763 /* Same as WFX order */
771 SetDefaultWFXChannelOrder(device
);
773 /* Sets the default order used by WaveFormatEx */
774 void SetDefaultWFXChannelOrder(ALCdevice
*device
)
776 switch(device
->FmtChans
)
778 case DevFmtMono
: device
->DevChannels
[0] = FRONT_CENTER
; break;
780 case DevFmtStereo
: device
->DevChannels
[0] = FRONT_LEFT
;
781 device
->DevChannels
[1] = FRONT_RIGHT
; break;
783 case DevFmtQuad
: device
->DevChannels
[0] = FRONT_LEFT
;
784 device
->DevChannels
[1] = FRONT_RIGHT
;
785 device
->DevChannels
[2] = BACK_LEFT
;
786 device
->DevChannels
[3] = BACK_RIGHT
; break;
788 case DevFmtX51
: device
->DevChannels
[0] = FRONT_LEFT
;
789 device
->DevChannels
[1] = FRONT_RIGHT
;
790 device
->DevChannels
[2] = FRONT_CENTER
;
791 device
->DevChannels
[3] = LFE
;
792 device
->DevChannels
[4] = BACK_LEFT
;
793 device
->DevChannels
[5] = BACK_RIGHT
; break;
795 case DevFmtX51Side
: device
->DevChannels
[0] = FRONT_LEFT
;
796 device
->DevChannels
[1] = FRONT_RIGHT
;
797 device
->DevChannels
[2] = FRONT_CENTER
;
798 device
->DevChannels
[3] = LFE
;
799 device
->DevChannels
[4] = SIDE_LEFT
;
800 device
->DevChannels
[5] = SIDE_RIGHT
; break;
802 case DevFmtX61
: device
->DevChannels
[0] = FRONT_LEFT
;
803 device
->DevChannels
[1] = FRONT_RIGHT
;
804 device
->DevChannels
[2] = FRONT_CENTER
;
805 device
->DevChannels
[3] = LFE
;
806 device
->DevChannels
[4] = BACK_CENTER
;
807 device
->DevChannels
[5] = SIDE_LEFT
;
808 device
->DevChannels
[6] = SIDE_RIGHT
; break;
810 case DevFmtX71
: device
->DevChannels
[0] = FRONT_LEFT
;
811 device
->DevChannels
[1] = FRONT_RIGHT
;
812 device
->DevChannels
[2] = FRONT_CENTER
;
813 device
->DevChannels
[3] = LFE
;
814 device
->DevChannels
[4] = BACK_LEFT
;
815 device
->DevChannels
[5] = BACK_RIGHT
;
816 device
->DevChannels
[6] = SIDE_LEFT
;
817 device
->DevChannels
[7] = SIDE_RIGHT
; break;
822 const ALCchar
*DevFmtTypeString(enum DevFmtType type
)
826 case DevFmtByte
: return "Signed Byte";
827 case DevFmtUByte
: return "Unsigned Byte";
828 case DevFmtShort
: return "Signed Short";
829 case DevFmtUShort
: return "Unsigned Short";
830 case DevFmtFloat
: return "Float";
832 return "(unknown type)";
834 const ALCchar
*DevFmtChannelsString(enum DevFmtChannels chans
)
838 case DevFmtMono
: return "Mono";
839 case DevFmtStereo
: return "Stereo";
840 case DevFmtQuad
: return "Quadraphonic";
841 case DevFmtX51
: return "5.1 Surround";
842 case DevFmtX51Side
: return "5.1 Side";
843 case DevFmtX61
: return "6.1 Surround";
844 case DevFmtX71
: return "7.1 Surround";
846 return "(unknown channels)";
849 ALuint
BytesFromDevFmt(enum DevFmtType type
)
853 case DevFmtByte
: return sizeof(ALbyte
);
854 case DevFmtUByte
: return sizeof(ALubyte
);
855 case DevFmtShort
: return sizeof(ALshort
);
856 case DevFmtUShort
: return sizeof(ALushort
);
857 case DevFmtFloat
: return sizeof(ALfloat
);
861 ALuint
ChannelsFromDevFmt(enum DevFmtChannels chans
)
865 case DevFmtMono
: return 1;
866 case DevFmtStereo
: return 2;
867 case DevFmtQuad
: return 4;
868 case DevFmtX51
: return 6;
869 case DevFmtX51Side
: return 6;
870 case DevFmtX61
: return 7;
871 case DevFmtX71
: return 8;
875 ALboolean
DecomposeDevFormat(ALenum format
, enum DevFmtChannels
*chans
,
876 enum DevFmtType
*type
)
880 case AL_FORMAT_MONO8
:
884 case AL_FORMAT_MONO16
:
888 case AL_FORMAT_MONO_FLOAT32
:
892 case AL_FORMAT_STEREO8
:
893 *chans
= DevFmtStereo
;
896 case AL_FORMAT_STEREO16
:
897 *chans
= DevFmtStereo
;
900 case AL_FORMAT_STEREO_FLOAT32
:
901 *chans
= DevFmtStereo
;
904 case AL_FORMAT_QUAD8
:
908 case AL_FORMAT_QUAD16
:
912 case AL_FORMAT_QUAD32
:
916 case AL_FORMAT_51CHN8
:
920 case AL_FORMAT_51CHN16
:
924 case AL_FORMAT_51CHN32
:
928 case AL_FORMAT_61CHN8
:
932 case AL_FORMAT_61CHN16
:
936 case AL_FORMAT_61CHN32
:
940 case AL_FORMAT_71CHN8
:
944 case AL_FORMAT_71CHN16
:
948 case AL_FORMAT_71CHN32
:
956 static ALCboolean
IsValidALCType(ALCenum type
)
961 case ALC_UNSIGNED_BYTE
:
963 case ALC_UNSIGNED_SHORT
:
965 case ALC_UNSIGNED_INT
:
972 static ALCboolean
IsValidALCChannels(ALCenum channels
)
988 static void LockLists(void)
990 EnterCriticalSection(&ListLock
);
993 static void UnlockLists(void)
995 LeaveCriticalSection(&ListLock
);
1001 Check pDevice is a valid Device pointer
1003 static ALCboolean
IsDevice(ALCdevice
*pDevice
)
1005 ALCdevice
*pTempDevice
;
1007 pTempDevice
= g_pDeviceList
;
1008 while(pTempDevice
&& pTempDevice
!= pDevice
)
1009 pTempDevice
= pTempDevice
->next
;
1011 return (pTempDevice
? ALC_TRUE
: ALC_FALSE
);
1017 Check pContext is a valid Context pointer
1019 static ALCboolean
IsContext(ALCcontext
*pContext
)
1021 ALCcontext
*pTempContext
;
1023 pTempContext
= g_pContextList
;
1024 while (pTempContext
&& pTempContext
!= pContext
)
1025 pTempContext
= pTempContext
->next
;
1027 return (pTempContext
? ALC_TRUE
: ALC_FALSE
);
1034 Store latest ALC Error
1036 ALCvoid
alcSetError(ALCdevice
*device
, ALenum errorCode
)
1039 if(IsDevice(device
))
1040 device
->LastError
= errorCode
;
1042 g_eLastNullDeviceError
= errorCode
;
1047 /* UpdateDeviceParams:
1049 * Updates device parameters according to the attribute list.
1051 static ALCboolean
UpdateDeviceParams(ALCdevice
*device
, const ALCint
*attrList
)
1055 // Check for attributes
1056 if(attrList
&& attrList
[0])
1058 ALCuint freq
, numMono
, numStereo
, numSends
;
1059 enum DevFmtChannels schans
;
1060 enum DevFmtType stype
;
1063 // If a context is already running on the device, stop playback so the
1064 // device attributes can be updated
1065 if((device
->Flags
&DEVICE_RUNNING
))
1066 ALCdevice_StopPlayback(device
);
1067 device
->Flags
&= ~DEVICE_RUNNING
;
1069 freq
= device
->Frequency
;
1070 schans
= device
->FmtChans
;
1071 stype
= device
->FmtType
;
1072 numMono
= device
->NumMonoSources
;
1073 numStereo
= device
->NumStereoSources
;
1074 numSends
= device
->NumAuxSends
;
1076 freq
= GetConfigValueInt(NULL
, "frequency", freq
);
1077 if(freq
< 8000) freq
= 8000;
1080 while(attrList
[attrIdx
])
1082 if(attrList
[attrIdx
] == ALC_FORMAT_CHANNELS_SOFT
&&
1083 device
->IsLoopbackDevice
)
1085 ALCint val
= attrList
[attrIdx
+ 1];
1086 if(!IsValidALCChannels(val
) || !ChannelsFromDevFmt(val
))
1088 alcSetError(device
, ALC_INVALID_VALUE
);
1094 if(attrList
[attrIdx
] == ALC_FORMAT_TYPE_SOFT
&&
1095 device
->IsLoopbackDevice
)
1097 ALCint val
= attrList
[attrIdx
+ 1];
1098 if(!IsValidALCType(val
) || !BytesFromDevFmt(val
))
1100 alcSetError(device
, ALC_INVALID_VALUE
);
1106 if(attrList
[attrIdx
] == ALC_FREQUENCY
)
1108 if(device
->IsLoopbackDevice
)
1110 freq
= attrList
[attrIdx
+ 1];
1113 alcSetError(device
, ALC_INVALID_VALUE
);
1117 else if(!ConfigValueExists(NULL
, "frequency"))
1119 freq
= attrList
[attrIdx
+ 1];
1120 if(freq
< 8000) freq
= 8000;
1121 device
->Flags
|= DEVICE_FREQUENCY_REQUEST
;
1125 if(attrList
[attrIdx
] == ALC_STEREO_SOURCES
)
1127 numStereo
= attrList
[attrIdx
+ 1];
1128 if(numStereo
> device
->MaxNoOfSources
)
1129 numStereo
= device
->MaxNoOfSources
;
1131 numMono
= device
->MaxNoOfSources
- numStereo
;
1134 if(attrList
[attrIdx
] == ALC_MAX_AUXILIARY_SENDS
&&
1135 !ConfigValueExists(NULL
, "sends"))
1137 numSends
= attrList
[attrIdx
+ 1];
1138 if(numSends
> MAX_SENDS
)
1139 numSends
= MAX_SENDS
;
1145 device
->UpdateSize
= (ALuint64
)device
->UpdateSize
* freq
/
1148 device
->Frequency
= freq
;
1149 device
->FmtChans
= schans
;
1150 device
->FmtType
= stype
;
1151 device
->NumMonoSources
= numMono
;
1152 device
->NumStereoSources
= numStereo
;
1153 device
->NumAuxSends
= numSends
;
1156 if((device
->Flags
&DEVICE_RUNNING
))
1160 TRACE("Format pre-setup: %s%s, %s, %uhz%s, %u update size x%d\n",
1161 DevFmtChannelsString(device
->FmtChans
),
1162 (device
->Flags
&DEVICE_CHANNELS_REQUEST
)?" (requested)":"",
1163 DevFmtTypeString(device
->FmtType
), device
->Frequency
,
1164 (device
->Flags
&DEVICE_FREQUENCY_REQUEST
)?" (requested)":"",
1165 device
->UpdateSize
, device
->NumUpdates
);
1166 if(ALCdevice_ResetPlayback(device
) == ALC_FALSE
)
1168 UnlockDevice(device
);
1171 device
->Flags
|= DEVICE_RUNNING
;
1172 TRACE("Format post-setup: %s%s, %s, %uhz%s, %u update size x%d\n",
1173 DevFmtChannelsString(device
->FmtChans
),
1174 (device
->Flags
&DEVICE_CHANNELS_REQUEST
)?" (requested)":"",
1175 DevFmtTypeString(device
->FmtType
), device
->Frequency
,
1176 (device
->Flags
&DEVICE_FREQUENCY_REQUEST
)?" (requested)":"",
1177 device
->UpdateSize
, device
->NumUpdates
);
1179 aluInitPanning(device
);
1181 for(i
= 0;i
< MAXCHANNELS
;i
++)
1183 device
->ClickRemoval
[i
] = 0.0f
;
1184 device
->PendingClicks
[i
] = 0.0f
;
1187 if(!device
->IsLoopbackDevice
&& GetConfigValueBool(NULL
, "hrtf", AL_FALSE
))
1188 device
->Flags
|= DEVICE_USE_HRTF
;
1189 if((device
->Flags
&DEVICE_USE_HRTF
) && !IsHrtfCompatible(device
))
1190 device
->Flags
&= ~DEVICE_USE_HRTF
;
1191 TRACE("HRTF %s\n", (device
->Flags
&DEVICE_USE_HRTF
)?"enabled":"disabled");
1193 if(!(device
->Flags
&DEVICE_USE_HRTF
) && device
->Bs2bLevel
> 0 && device
->Bs2bLevel
<= 6)
1197 device
->Bs2b
= calloc(1, sizeof(*device
->Bs2b
));
1198 bs2b_clear(device
->Bs2b
);
1200 bs2b_set_srate(device
->Bs2b
, device
->Frequency
);
1201 bs2b_set_level(device
->Bs2b
, device
->Bs2bLevel
);
1202 TRACE("BS2B level %d\n", device
->Bs2bLevel
);
1207 device
->Bs2b
= NULL
;
1208 TRACE("BS2B disabled\n");
1211 device
->Flags
&= ~DEVICE_DUPLICATE_STEREO
;
1212 switch(device
->FmtChans
)
1222 if(GetConfigValueBool(NULL
, "stereodup", AL_TRUE
))
1223 device
->Flags
|= DEVICE_DUPLICATE_STEREO
;
1226 TRACE("Stereo duplication %s\n", (device
->Flags
&DEVICE_DUPLICATE_STEREO
)?"enabled":"disabled");
1228 for(i
= 0;i
< device
->NumContexts
;i
++)
1230 ALCcontext
*context
= device
->Contexts
[i
];
1233 context
->UpdateSources
= AL_FALSE
;
1234 for(pos
= 0;pos
< context
->EffectSlotMap
.size
;pos
++)
1236 ALeffectslot
*slot
= context
->EffectSlotMap
.array
[pos
].value
;
1238 if(ALEffect_DeviceUpdate(slot
->EffectState
, device
) == AL_FALSE
)
1240 UnlockDevice(device
);
1241 ALCdevice_StopPlayback(device
);
1242 device
->Flags
&= ~DEVICE_RUNNING
;
1245 slot
->NeedsUpdate
= AL_FALSE
;
1246 ALEffect_Update(slot
->EffectState
, context
, slot
);
1249 for(pos
= 0;pos
< context
->SourceMap
.size
;pos
++)
1251 ALsource
*source
= context
->SourceMap
.array
[pos
].value
;
1252 ALuint s
= device
->NumAuxSends
;
1253 while(s
< MAX_SENDS
)
1255 if(source
->Send
[s
].Slot
)
1256 source
->Send
[s
].Slot
->refcount
--;
1257 source
->Send
[s
].Slot
= NULL
;
1258 source
->Send
[s
].WetFilter
.type
= 0;
1259 source
->Send
[s
].WetFilter
.filter
= 0;
1262 source
->NeedsUpdate
= AL_FALSE
;
1263 ALsource_Update(source
, context
);
1266 UnlockDevice(device
);
1272 ALCvoid
LockDevice(ALCdevice
*device
)
1274 EnterCriticalSection(&device
->Mutex
);
1277 ALCvoid
UnlockDevice(ALCdevice
*device
)
1279 LeaveCriticalSection(&device
->Mutex
);
1287 ALCvoid
LockContext(ALCcontext
*context
)
1289 EnterCriticalSection(&context
->Device
->Mutex
);
1298 ALCvoid
UnlockContext(ALCcontext
*context
)
1300 LeaveCriticalSection(&context
->Device
->Mutex
);
1307 Returns the currently active Context, in a locked state
1309 ALCcontext
*GetLockedContext(void)
1311 ALCcontext
*context
= NULL
;
1313 context
= pthread_getspecific(LocalContext
);
1315 LockContext(context
);
1319 context
= GlobalContext
;
1321 LockContext(context
);
1332 Initialize Context variables
1334 static ALvoid
InitContext(ALCcontext
*pContext
)
1336 //Initialise listener
1337 pContext
->Listener
.Gain
= 1.0f
;
1338 pContext
->Listener
.MetersPerUnit
= 1.0f
;
1339 pContext
->Listener
.Position
[0] = 0.0f
;
1340 pContext
->Listener
.Position
[1] = 0.0f
;
1341 pContext
->Listener
.Position
[2] = 0.0f
;
1342 pContext
->Listener
.Velocity
[0] = 0.0f
;
1343 pContext
->Listener
.Velocity
[1] = 0.0f
;
1344 pContext
->Listener
.Velocity
[2] = 0.0f
;
1345 pContext
->Listener
.Forward
[0] = 0.0f
;
1346 pContext
->Listener
.Forward
[1] = 0.0f
;
1347 pContext
->Listener
.Forward
[2] = -1.0f
;
1348 pContext
->Listener
.Up
[0] = 0.0f
;
1349 pContext
->Listener
.Up
[1] = 1.0f
;
1350 pContext
->Listener
.Up
[2] = 0.0f
;
1353 pContext
->LastError
= AL_NO_ERROR
;
1354 pContext
->UpdateSources
= AL_FALSE
;
1355 pContext
->ActiveSourceCount
= 0;
1356 InitUIntMap(&pContext
->SourceMap
);
1357 InitUIntMap(&pContext
->EffectSlotMap
);
1360 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
1361 pContext
->SourceDistanceModel
= AL_FALSE
;
1362 pContext
->DopplerFactor
= 1.0f
;
1363 pContext
->DopplerVelocity
= 1.0f
;
1364 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
1365 pContext
->DeferUpdates
= AL_FALSE
;
1367 pContext
->ExtensionList
= alExtList
;
1374 Clean up Context, destroy any remaining Sources
1376 static ALCvoid
FreeContext(ALCcontext
*context
)
1379 if(context
== GlobalContext
)
1380 GlobalContext
= NULL
;
1383 if(context
->SourceMap
.size
> 0)
1385 ERR("FreeContext(%p): deleting %d Source(s)\n", context
, context
->SourceMap
.size
);
1386 ReleaseALSources(context
);
1388 ResetUIntMap(&context
->SourceMap
);
1390 if(context
->EffectSlotMap
.size
> 0)
1392 ERR("FreeContext(%p): deleting %d AuxiliaryEffectSlot(s)\n", context
, context
->EffectSlotMap
.size
);
1393 ReleaseALAuxiliaryEffectSlots(context
);
1395 ResetUIntMap(&context
->EffectSlotMap
);
1397 free(context
->ActiveSources
);
1398 context
->ActiveSources
= NULL
;
1399 context
->MaxActiveSources
= 0;
1400 context
->ActiveSourceCount
= 0;
1402 //Invalidate context
1403 memset(context
, 0, sizeof(ALCcontext
));
1407 static void ALCcontext_IncRef(ALCcontext
*context
)
1410 ref
= IncrementRef(&context
->ref
);
1411 TRACE("%p refcount increment to %d\n", context
, ref
);
1414 static void ALCcontext_DecRef(ALCcontext
*context
)
1417 ref
= DecrementRef(&context
->ref
);
1418 TRACE("%p refcount decrement to %d\n", context
, ref
);
1419 if(ref
== 0) FreeContext(context
);
1422 static void ReleaseThreadCtx(void *ptr
)
1424 ALCcontext_DecRef(ptr
);
1427 ///////////////////////////////////////////////////////
1430 ///////////////////////////////////////////////////////
1431 // ALC Functions calls
1434 // This should probably move to another c file but for now ...
1435 ALC_API ALCdevice
* ALC_APIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
1437 ALCdevice
*device
= NULL
;
1442 if(!CaptureBackend
.name
)
1444 alcSetError(NULL
, ALC_INVALID_VALUE
);
1450 alcSetError(NULL
, ALC_INVALID_VALUE
);
1454 if(deviceName
&& (!deviceName
[0] || strcasecmp(deviceName
, "openal soft") == 0 || strcasecmp(deviceName
, "openal-soft") == 0))
1457 device
= calloc(1, sizeof(ALCdevice
));
1460 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
1465 device
->Funcs
= &CaptureBackend
.Funcs
;
1466 device
->Connected
= ALC_TRUE
;
1467 device
->IsCaptureDevice
= AL_TRUE
;
1468 device
->IsLoopbackDevice
= AL_FALSE
;
1469 InitializeCriticalSection(&device
->Mutex
);
1471 device
->szDeviceName
= NULL
;
1473 device
->Flags
|= DEVICE_FREQUENCY_REQUEST
;
1474 device
->Frequency
= frequency
;
1476 device
->Flags
|= DEVICE_CHANNELS_REQUEST
;
1477 if(DecomposeDevFormat(format
, &device
->FmtChans
, &device
->FmtType
) == AL_FALSE
)
1480 alcSetError(NULL
, ALC_INVALID_ENUM
);
1484 device
->UpdateSize
= SampleSize
;
1485 device
->NumUpdates
= 1;
1488 if((err
=ALCdevice_OpenCapture(device
, deviceName
)) == ALC_NO_ERROR
)
1490 device
->next
= g_pDeviceList
;
1491 g_pDeviceList
= device
;
1496 DeleteCriticalSection(&device
->Mutex
);
1499 alcSetError(NULL
, err
);
1506 ALC_API ALCboolean ALC_APIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
1511 list
= &g_pDeviceList
;
1512 while(*list
&& *list
!= pDevice
)
1513 list
= &(*list
)->next
;
1515 if(!*list
|| !(*list
)->IsCaptureDevice
)
1517 alcSetError(*list
, ALC_INVALID_DEVICE
);
1522 *list
= (*list
)->next
;
1527 LockDevice(pDevice
);
1528 ALCdevice_CloseCapture(pDevice
);
1529 UnlockDevice(pDevice
);
1531 free(pDevice
->szDeviceName
);
1532 pDevice
->szDeviceName
= NULL
;
1534 DeleteCriticalSection(&pDevice
->Mutex
);
1541 ALC_API
void ALC_APIENTRY
alcCaptureStart(ALCdevice
*device
)
1544 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
1546 alcSetError(device
, ALC_INVALID_DEVICE
);
1552 if(device
->Connected
)
1553 ALCdevice_StartCapture(device
);
1554 UnlockDevice(device
);
1557 ALC_API
void ALC_APIENTRY
alcCaptureStop(ALCdevice
*device
)
1560 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
1562 alcSetError(device
, ALC_INVALID_DEVICE
);
1568 if(device
->Connected
)
1569 ALCdevice_StopCapture(device
);
1570 UnlockDevice(device
);
1573 ALC_API
void ALC_APIENTRY
alcCaptureSamples(ALCdevice
*device
, ALCvoid
*buffer
, ALCsizei samples
)
1576 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
1578 alcSetError(device
, ALC_INVALID_DEVICE
);
1584 if(device
->Connected
)
1585 ALCdevice_CaptureSamples(device
, buffer
, samples
);
1586 UnlockDevice(device
);
1592 Return last ALC generated error code
1594 ALC_API ALCenum ALC_APIENTRY
alcGetError(ALCdevice
*device
)
1599 if(IsDevice(device
))
1601 errorCode
= device
->LastError
;
1602 device
->LastError
= ALC_NO_ERROR
;
1606 errorCode
= g_eLastNullDeviceError
;
1607 g_eLastNullDeviceError
= ALC_NO_ERROR
;
1619 ALC_API ALCvoid ALC_APIENTRY
alcSuspendContext(ALCcontext
*Context
)
1630 ALC_API ALCvoid ALC_APIENTRY
alcProcessContext(ALCcontext
*Context
)
1639 Returns information about the Device, and error strings
1641 ALC_API
const ALCchar
* ALC_APIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
1643 const ALCchar
*value
= NULL
;
1651 case ALC_INVALID_ENUM
:
1652 value
= alcErrInvalidEnum
;
1655 case ALC_INVALID_VALUE
:
1656 value
= alcErrInvalidValue
;
1659 case ALC_INVALID_DEVICE
:
1660 value
= alcErrInvalidDevice
;
1663 case ALC_INVALID_CONTEXT
:
1664 value
= alcErrInvalidContext
;
1667 case ALC_OUT_OF_MEMORY
:
1668 value
= alcErrOutOfMemory
;
1671 case ALC_DEVICE_SPECIFIER
:
1673 if(IsDevice(pDevice
))
1674 value
= pDevice
->szDeviceName
;
1678 value
= alcDeviceList
;
1683 case ALC_ALL_DEVICES_SPECIFIER
:
1684 ProbeAllDeviceList();
1685 value
= alcAllDeviceList
;
1688 case ALC_CAPTURE_DEVICE_SPECIFIER
:
1690 if(IsDevice(pDevice
))
1691 value
= pDevice
->szDeviceName
;
1694 ProbeCaptureDeviceList();
1695 value
= alcCaptureDeviceList
;
1700 /* Default devices are always first in the list */
1701 case ALC_DEFAULT_DEVICE_SPECIFIER
:
1705 free(alcDefaultDeviceSpecifier
);
1706 alcDefaultDeviceSpecifier
= strdup(alcDeviceList
? alcDeviceList
: "");
1707 if(!alcDefaultDeviceSpecifier
)
1708 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1709 value
= alcDefaultDeviceSpecifier
;
1712 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
1713 if(!alcAllDeviceList
)
1714 ProbeAllDeviceList();
1716 free(alcDefaultAllDeviceSpecifier
);
1717 alcDefaultAllDeviceSpecifier
= strdup(alcAllDeviceList
?
1718 alcAllDeviceList
: "");
1719 if(!alcDefaultAllDeviceSpecifier
)
1720 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1721 value
= alcDefaultAllDeviceSpecifier
;
1724 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
1725 if(!alcCaptureDeviceList
)
1726 ProbeCaptureDeviceList();
1728 free(alcCaptureDefaultDeviceSpecifier
);
1729 alcCaptureDefaultDeviceSpecifier
= strdup(alcCaptureDeviceList
?
1730 alcCaptureDeviceList
: "");
1731 if(!alcCaptureDefaultDeviceSpecifier
)
1732 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1733 value
= alcCaptureDefaultDeviceSpecifier
;
1736 case ALC_EXTENSIONS
:
1738 if(IsDevice(pDevice
))
1739 value
= alcExtensionList
;
1741 value
= alcNoDeviceExtList
;
1746 alcSetError(pDevice
, ALC_INVALID_ENUM
);
1757 Returns information about the Device and the version of Open AL
1759 ALC_API ALCvoid ALC_APIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
1761 if(size
== 0 || data
== NULL
)
1763 alcSetError(device
, ALC_INVALID_VALUE
);
1768 if(!IsDevice(device
))
1772 case ALC_MAJOR_VERSION
:
1773 *data
= alcMajorVersion
;
1775 case ALC_MINOR_VERSION
:
1776 *data
= alcMinorVersion
;
1779 case ALC_ATTRIBUTES_SIZE
:
1780 case ALC_ALL_ATTRIBUTES
:
1784 case ALC_MONO_SOURCES
:
1785 case ALC_STEREO_SOURCES
:
1786 case ALC_CAPTURE_SAMPLES
:
1787 case ALC_FORMAT_CHANNELS_SOFT
:
1788 case ALC_FORMAT_TYPE_SOFT
:
1789 alcSetError(NULL
, ALC_INVALID_DEVICE
);
1793 alcSetError(NULL
, ALC_INVALID_ENUM
);
1797 else if(device
->IsCaptureDevice
)
1801 case ALC_CAPTURE_SAMPLES
:
1803 *data
= ALCdevice_AvailableSamples(device
);
1804 UnlockDevice(device
);
1808 *data
= device
->Connected
;
1812 alcSetError(device
, ALC_INVALID_ENUM
);
1816 else /* render device */
1820 case ALC_MAJOR_VERSION
:
1821 *data
= alcMajorVersion
;
1824 case ALC_MINOR_VERSION
:
1825 *data
= alcMinorVersion
;
1828 case ALC_EFX_MAJOR_VERSION
:
1829 *data
= alcEFXMajorVersion
;
1832 case ALC_EFX_MINOR_VERSION
:
1833 *data
= alcEFXMinorVersion
;
1836 case ALC_ATTRIBUTES_SIZE
:
1840 case ALC_ALL_ATTRIBUTES
:
1842 alcSetError(device
, ALC_INVALID_VALUE
);
1847 data
[i
++] = ALC_FREQUENCY
;
1848 data
[i
++] = device
->Frequency
;
1850 if(!device
->IsLoopbackDevice
)
1852 data
[i
++] = ALC_REFRESH
;
1853 data
[i
++] = device
->Frequency
/ device
->UpdateSize
;
1855 data
[i
++] = ALC_SYNC
;
1856 data
[i
++] = ALC_FALSE
;
1860 data
[i
++] = ALC_FORMAT_CHANNELS_SOFT
;
1861 data
[i
++] = device
->FmtChans
;
1863 data
[i
++] = ALC_FORMAT_TYPE_SOFT
;
1864 data
[i
++] = device
->FmtType
;
1867 data
[i
++] = ALC_MONO_SOURCES
;
1868 data
[i
++] = device
->NumMonoSources
;
1870 data
[i
++] = ALC_STEREO_SOURCES
;
1871 data
[i
++] = device
->NumStereoSources
;
1873 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
1874 data
[i
++] = device
->NumAuxSends
;
1881 *data
= device
->Frequency
;
1885 if(device
->IsLoopbackDevice
)
1886 alcSetError(device
, ALC_INVALID_DEVICE
);
1888 *data
= device
->Frequency
/ device
->UpdateSize
;
1892 if(device
->IsLoopbackDevice
)
1893 alcSetError(device
, ALC_INVALID_DEVICE
);
1898 case ALC_FORMAT_CHANNELS_SOFT
:
1899 if(!device
->IsLoopbackDevice
)
1900 alcSetError(device
, ALC_INVALID_DEVICE
);
1902 *data
= device
->FmtChans
;
1905 case ALC_FORMAT_TYPE_SOFT
:
1906 if(!device
->IsLoopbackDevice
)
1907 alcSetError(device
, ALC_INVALID_DEVICE
);
1909 *data
= device
->FmtType
;
1912 case ALC_MONO_SOURCES
:
1913 *data
= device
->NumMonoSources
;
1916 case ALC_STEREO_SOURCES
:
1917 *data
= device
->NumStereoSources
;
1920 case ALC_MAX_AUXILIARY_SENDS
:
1921 *data
= device
->NumAuxSends
;
1925 *data
= device
->Connected
;
1929 alcSetError(device
, ALC_INVALID_ENUM
);
1938 alcIsExtensionPresent
1940 Determines if there is support for a particular extension
1942 ALC_API ALCboolean ALC_APIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
1944 ALCboolean bResult
= ALC_FALSE
;
1950 alcSetError(device
, ALC_INVALID_VALUE
);
1954 len
= strlen(extName
);
1956 ptr
= (IsDevice(device
) ? alcExtensionList
: alcNoDeviceExtList
);
1960 if(strncasecmp(ptr
, extName
, len
) == 0 &&
1961 (ptr
[len
] == '\0' || isspace(ptr
[len
])))
1966 if((ptr
=strchr(ptr
, ' ')) != NULL
)
1970 } while(isspace(*ptr
));
1981 Retrieves the function address for a particular extension function
1983 ALC_API ALCvoid
* ALC_APIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
1989 alcSetError(device
, ALC_INVALID_VALUE
);
1993 while(alcFunctions
[i
].funcName
&& strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
1995 return alcFunctions
[i
].address
;
2002 Get the value for a particular ALC Enumerated Value
2004 ALC_API ALCenum ALC_APIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
2010 alcSetError(device
, ALC_INVALID_VALUE
);
2014 while(enumeration
[i
].enumName
&& strcmp(enumeration
[i
].enumName
,enumName
) != 0)
2016 return enumeration
[i
].value
;
2023 Create and attach a Context to a particular Device.
2025 ALC_API ALCcontext
* ALC_APIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
2027 ALCcontext
*ALContext
;
2031 if(!IsDevice(device
) || device
->IsCaptureDevice
|| !device
->Connected
)
2033 alcSetError(device
, ALC_INVALID_DEVICE
);
2038 // Reset Context Last Error code
2039 device
->LastError
= ALC_NO_ERROR
;
2041 if(UpdateDeviceParams(device
, attrList
) == ALC_FALSE
)
2043 alcSetError(device
, ALC_INVALID_DEVICE
);
2044 aluHandleDisconnect(device
);
2051 temp
= realloc(device
->Contexts
, (device
->NumContexts
+1) * sizeof(*device
->Contexts
));
2054 device
->Contexts
= temp
;
2056 ALContext
= calloc(1, sizeof(ALCcontext
));
2061 ALContext
->MaxActiveSources
= 256;
2062 ALContext
->ActiveSources
= malloc(sizeof(ALContext
->ActiveSources
[0]) *
2063 ALContext
->MaxActiveSources
);
2066 if(!ALContext
|| !ALContext
->ActiveSources
)
2069 alcSetError(device
, ALC_OUT_OF_MEMORY
);
2070 UnlockDevice(device
);
2071 if(device
->NumContexts
== 0)
2073 ALCdevice_StopPlayback(device
);
2074 device
->Flags
&= ~DEVICE_RUNNING
;
2080 device
->Contexts
[device
->NumContexts
++] = ALContext
;
2081 ALContext
->Device
= device
;
2083 InitContext(ALContext
);
2084 UnlockDevice(device
);
2086 ALContext
->next
= g_pContextList
;
2087 g_pContextList
= ALContext
;
2101 ALC_API ALCvoid ALC_APIENTRY
alcDestroyContext(ALCcontext
*context
)
2108 list
= &g_pContextList
;
2109 while(*list
&& *list
!= context
)
2110 list
= &(*list
)->next
;
2114 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2119 *list
= (*list
)->next
;
2122 Device
= context
->Device
;
2124 for(i
= 0;i
< Device
->NumContexts
;i
++)
2126 if(Device
->Contexts
[i
] == context
)
2128 Device
->Contexts
[i
] = Device
->Contexts
[Device
->NumContexts
-1];
2129 Device
->NumContexts
--;
2133 UnlockDevice(Device
);
2135 if(Device
->NumContexts
== 0)
2137 ALCdevice_StopPlayback(Device
);
2138 Device
->Flags
&= ~DEVICE_RUNNING
;
2142 ALCcontext_DecRef(context
);
2147 alcGetCurrentContext
2149 Returns the currently active Context
2151 ALC_API ALCcontext
* ALC_APIENTRY
alcGetCurrentContext(ALCvoid
)
2153 ALCcontext
*Context
;
2155 Context
= pthread_getspecific(LocalContext
);
2159 Context
= GlobalContext
;
2169 Returns the currently active thread-local Context
2171 ALC_API ALCcontext
* ALC_APIENTRY
alcGetThreadContext(void)
2173 ALCcontext
*Context
;
2174 Context
= pthread_getspecific(LocalContext
);
2180 alcGetContextsDevice
2182 Returns the Device that a particular Context is attached to
2184 ALC_API ALCdevice
* ALC_APIENTRY
alcGetContextsDevice(ALCcontext
*pContext
)
2186 ALCdevice
*pDevice
= NULL
;
2189 if(IsContext(pContext
))
2190 pDevice
= pContext
->Device
;
2192 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2200 alcMakeContextCurrent
2202 Makes the given Context the active Context
2204 ALC_API ALCboolean ALC_APIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
2206 ALboolean bReturn
= AL_TRUE
;
2210 // context must be a valid Context or NULL
2211 if(context
== NULL
|| IsContext(context
))
2213 GlobalContext
= context
;
2214 if((context
=pthread_getspecific(LocalContext
)) != NULL
)
2216 pthread_setspecific(LocalContext
, NULL
);
2217 ALCcontext_DecRef(context
);
2222 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2234 Makes the given Context the active Context for the current thread
2236 ALC_API ALCboolean ALC_APIENTRY
alcSetThreadContext(ALCcontext
*context
)
2238 ALboolean bReturn
= AL_TRUE
;
2241 // context must be a valid Context or NULL
2242 old
= pthread_getspecific(LocalContext
);
2246 if(context
== NULL
|| IsContext(context
))
2248 if(context
) ALCcontext_IncRef(context
);
2249 pthread_setspecific(LocalContext
, context
);
2250 if(old
) ALCcontext_DecRef(old
);
2254 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2264 static void GetFormatFromString(const char *str
, enum DevFmtChannels
*chans
, enum DevFmtType
*type
)
2266 if(strcasecmp(str
, "AL_FORMAT_MONO32") == 0)
2268 *chans
= DevFmtMono
;
2269 *type
= DevFmtFloat
;
2272 if(strcasecmp(str
, "AL_FORMAT_STEREO32") == 0)
2274 *chans
= DevFmtStereo
;
2275 *type
= DevFmtFloat
;
2278 if(strcasecmp(str
, "AL_FORMAT_QUAD32") == 0)
2280 *chans
= DevFmtQuad
;
2281 *type
= DevFmtFloat
;
2284 if(strcasecmp(str
, "AL_FORMAT_51CHN32") == 0)
2287 *type
= DevFmtFloat
;
2290 if(strcasecmp(str
, "AL_FORMAT_61CHN32") == 0)
2293 *type
= DevFmtFloat
;
2296 if(strcasecmp(str
, "AL_FORMAT_71CHN32") == 0)
2299 *type
= DevFmtFloat
;
2303 if(strcasecmp(str
, "AL_FORMAT_MONO16") == 0)
2305 *chans
= DevFmtMono
;
2306 *type
= DevFmtShort
;
2309 if(strcasecmp(str
, "AL_FORMAT_STEREO16") == 0)
2311 *chans
= DevFmtStereo
;
2312 *type
= DevFmtShort
;
2315 if(strcasecmp(str
, "AL_FORMAT_QUAD16") == 0)
2317 *chans
= DevFmtQuad
;
2318 *type
= DevFmtShort
;
2321 if(strcasecmp(str
, "AL_FORMAT_51CHN16") == 0)
2324 *type
= DevFmtShort
;
2327 if(strcasecmp(str
, "AL_FORMAT_61CHN16") == 0)
2330 *type
= DevFmtShort
;
2333 if(strcasecmp(str
, "AL_FORMAT_71CHN16") == 0)
2336 *type
= DevFmtShort
;
2340 if(strcasecmp(str
, "AL_FORMAT_MONO8") == 0)
2342 *chans
= DevFmtMono
;
2346 if(strcasecmp(str
, "AL_FORMAT_STEREO8") == 0)
2348 *chans
= DevFmtStereo
;
2352 if(strcasecmp(str
, "AL_FORMAT_QUAD8") == 0)
2354 *chans
= DevFmtQuad
;
2358 if(strcasecmp(str
, "AL_FORMAT_51CHN8") == 0)
2364 if(strcasecmp(str
, "AL_FORMAT_61CHN8") == 0)
2370 if(strcasecmp(str
, "AL_FORMAT_71CHN8") == 0)
2377 ERR("Unknown format: \"%s\"\n", str
);
2378 *chans
= DevFmtStereo
;
2379 *type
= DevFmtShort
;
2385 Open the Device specified.
2387 ALC_API ALCdevice
* ALC_APIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
2395 if(!PlaybackBackend
.name
)
2397 alcSetError(NULL
, ALC_INVALID_VALUE
);
2401 if(deviceName
&& (!deviceName
[0] || strcasecmp(deviceName
, "openal soft") == 0 || strcasecmp(deviceName
, "openal-soft") == 0))
2404 device
= calloc(1, sizeof(ALCdevice
));
2407 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
2412 device
->Funcs
= &PlaybackBackend
.Funcs
;
2413 device
->Connected
= ALC_TRUE
;
2414 device
->IsCaptureDevice
= AL_FALSE
;
2415 device
->IsLoopbackDevice
= AL_FALSE
;
2416 InitializeCriticalSection(&device
->Mutex
);
2417 device
->LastError
= ALC_NO_ERROR
;
2420 device
->Bs2b
= NULL
;
2421 device
->szDeviceName
= NULL
;
2423 device
->Contexts
= NULL
;
2424 device
->NumContexts
= 0;
2426 InitUIntMap(&device
->BufferMap
);
2427 InitUIntMap(&device
->EffectMap
);
2428 InitUIntMap(&device
->FilterMap
);
2431 if(ConfigValueExists(NULL
, "frequency"))
2432 device
->Flags
|= DEVICE_FREQUENCY_REQUEST
;
2433 device
->Frequency
= GetConfigValueInt(NULL
, "frequency", DEFAULT_OUTPUT_RATE
);
2434 if(device
->Frequency
< 8000)
2435 device
->Frequency
= 8000;
2437 if(ConfigValueExists(NULL
, "format"))
2438 device
->Flags
|= DEVICE_CHANNELS_REQUEST
;
2439 fmt
= GetConfigValue(NULL
, "format", "AL_FORMAT_STEREO16");
2440 GetFormatFromString(fmt
, &device
->FmtChans
, &device
->FmtType
);
2442 device
->NumUpdates
= GetConfigValueInt(NULL
, "periods", 4);
2443 if(device
->NumUpdates
< 2)
2444 device
->NumUpdates
= 4;
2446 device
->UpdateSize
= GetConfigValueInt(NULL
, "period_size", 1024);
2447 if(device
->UpdateSize
<= 0)
2448 device
->UpdateSize
= 1024;
2450 device
->MaxNoOfSources
= GetConfigValueInt(NULL
, "sources", 256);
2451 if(device
->MaxNoOfSources
<= 0)
2452 device
->MaxNoOfSources
= 256;
2454 device
->AuxiliaryEffectSlotMax
= GetConfigValueInt(NULL
, "slots", 4);
2455 if(device
->AuxiliaryEffectSlotMax
<= 0)
2456 device
->AuxiliaryEffectSlotMax
= 4;
2458 device
->NumStereoSources
= 1;
2459 device
->NumMonoSources
= device
->MaxNoOfSources
- device
->NumStereoSources
;
2461 device
->NumAuxSends
= GetConfigValueInt(NULL
, "sends", MAX_SENDS
);
2462 if(device
->NumAuxSends
> MAX_SENDS
)
2463 device
->NumAuxSends
= MAX_SENDS
;
2465 device
->Bs2bLevel
= GetConfigValueInt(NULL
, "cf_level", 0);
2467 // Find a playback device to open
2469 if((err
=ALCdevice_OpenPlayback(device
, deviceName
)) == ALC_NO_ERROR
)
2471 device
->next
= g_pDeviceList
;
2472 g_pDeviceList
= device
;
2477 // No suitable output device found
2478 DeleteCriticalSection(&device
->Mutex
);
2481 alcSetError(NULL
, err
);
2492 Close the specified Device
2494 ALC_API ALCboolean ALC_APIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
2499 list
= &g_pDeviceList
;
2500 while(*list
&& *list
!= pDevice
)
2501 list
= &(*list
)->next
;
2503 if(!*list
|| (*list
)->IsCaptureDevice
)
2505 alcSetError(*list
, ALC_INVALID_DEVICE
);
2510 *list
= (*list
)->next
;
2515 if(pDevice
->NumContexts
> 0)
2517 WARN("alcCloseDevice(): destroying %u Context(s)\n", pDevice
->NumContexts
);
2518 while(pDevice
->NumContexts
> 0)
2519 alcDestroyContext(pDevice
->Contexts
[0]);
2521 ALCdevice_ClosePlayback(pDevice
);
2523 if(pDevice
->BufferMap
.size
> 0)
2525 WARN("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice
->BufferMap
.size
);
2526 ReleaseALBuffers(pDevice
);
2528 ResetUIntMap(&pDevice
->BufferMap
);
2530 if(pDevice
->EffectMap
.size
> 0)
2532 WARN("alcCloseDevice(): deleting %d Effect(s)\n", pDevice
->EffectMap
.size
);
2533 ReleaseALEffects(pDevice
);
2535 ResetUIntMap(&pDevice
->EffectMap
);
2537 if(pDevice
->FilterMap
.size
> 0)
2539 WARN("alcCloseDevice(): deleting %d Filter(s)\n", pDevice
->FilterMap
.size
);
2540 ReleaseALFilters(pDevice
);
2542 ResetUIntMap(&pDevice
->FilterMap
);
2544 free(pDevice
->Bs2b
);
2545 pDevice
->Bs2b
= NULL
;
2547 free(pDevice
->szDeviceName
);
2548 pDevice
->szDeviceName
= NULL
;
2550 free(pDevice
->Contexts
);
2551 pDevice
->Contexts
= NULL
;
2553 DeleteCriticalSection(&pDevice
->Mutex
);
2555 //Release device structure
2556 memset(pDevice
, 0, sizeof(ALCdevice
));
2563 ALC_API ALCdevice
* ALC_APIENTRY
alcLoopbackOpenDeviceSOFT(void)
2569 device
= calloc(1, sizeof(ALCdevice
));
2572 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
2577 device
->Funcs
= &BackendLoopback
.Funcs
;
2578 device
->Connected
= ALC_TRUE
;
2579 device
->IsCaptureDevice
= AL_FALSE
;
2580 device
->IsLoopbackDevice
= AL_TRUE
;
2581 InitializeCriticalSection(&device
->Mutex
);
2582 device
->LastError
= ALC_NO_ERROR
;
2585 device
->Bs2b
= NULL
;
2586 device
->szDeviceName
= NULL
;
2588 device
->Contexts
= NULL
;
2589 device
->NumContexts
= 0;
2591 InitUIntMap(&device
->BufferMap
);
2592 InitUIntMap(&device
->EffectMap
);
2593 InitUIntMap(&device
->FilterMap
);
2596 device
->Frequency
= 44100;
2597 device
->FmtChans
= DevFmtStereo
;
2598 device
->FmtType
= DevFmtShort
;
2600 device
->NumUpdates
= 0;
2601 device
->UpdateSize
= 0;
2603 device
->MaxNoOfSources
= GetConfigValueInt(NULL
, "sources", 256);
2604 if(device
->MaxNoOfSources
<= 0)
2605 device
->MaxNoOfSources
= 256;
2607 device
->AuxiliaryEffectSlotMax
= GetConfigValueInt(NULL
, "slots", 4);
2608 if(device
->AuxiliaryEffectSlotMax
<= 0)
2609 device
->AuxiliaryEffectSlotMax
= 4;
2611 device
->NumStereoSources
= 1;
2612 device
->NumMonoSources
= device
->MaxNoOfSources
- device
->NumStereoSources
;
2614 device
->NumAuxSends
= GetConfigValueInt(NULL
, "sends", MAX_SENDS
);
2615 if(device
->NumAuxSends
> MAX_SENDS
)
2616 device
->NumAuxSends
= MAX_SENDS
;
2618 device
->Bs2bLevel
= GetConfigValueInt(NULL
, "cf_level", 0);
2620 // Open the "backend"
2622 ALCdevice_OpenPlayback(device
, "Loopback");
2624 device
->next
= g_pDeviceList
;
2625 g_pDeviceList
= device
;
2632 ALC_API ALCboolean ALC_APIENTRY
alcIsRenderFormatSupportedSOFT(ALCdevice
*device
, ALCsizei freq
, ALCenum channels
, ALCenum type
)
2634 ALCboolean ret
= ALC_FALSE
;
2637 if(!IsDevice(device
) || !device
->IsLoopbackDevice
)
2638 alcSetError(device
, ALC_INVALID_DEVICE
);
2640 alcSetError(device
, ALC_INVALID_VALUE
);
2641 else if(!IsValidALCType(type
) || !IsValidALCChannels(channels
))
2642 alcSetError(device
, ALC_INVALID_ENUM
);
2645 if(BytesFromDevFmt(type
) > 0 && ChannelsFromDevFmt(channels
) > 0 &&
2654 ALC_API
void ALC_APIENTRY
alcRenderSamplesSOFT(ALCdevice
*device
, ALCvoid
*buffer
, ALCsizei samples
)
2657 if(!IsDevice(device
) || !device
->IsLoopbackDevice
)
2658 alcSetError(device
, ALC_INVALID_DEVICE
);
2659 else if(samples
< 0 || (samples
> 0 && buffer
== NULL
))
2660 alcSetError(device
, ALC_INVALID_VALUE
);
2662 aluMixData(device
, buffer
, samples
);
2667 static void ReleaseALC(ALCboolean doclose
)
2669 free(alcDeviceList
); alcDeviceList
= NULL
;
2670 alcDeviceListSize
= 0;
2671 free(alcAllDeviceList
); alcAllDeviceList
= NULL
;
2672 alcAllDeviceListSize
= 0;
2673 free(alcCaptureDeviceList
); alcCaptureDeviceList
= NULL
;
2674 alcCaptureDeviceListSize
= 0;
2676 free(alcDefaultDeviceSpecifier
);
2677 alcDefaultDeviceSpecifier
= NULL
;
2678 free(alcDefaultAllDeviceSpecifier
);
2679 alcDefaultAllDeviceSpecifier
= NULL
;
2680 free(alcCaptureDefaultDeviceSpecifier
);
2681 alcCaptureDefaultDeviceSpecifier
= NULL
;
2685 if(g_ulDeviceCount
> 0)
2686 WARN("ReleaseALC(): closing %u Device%s\n", g_ulDeviceCount
, (g_ulDeviceCount
>1)?"s":"");
2688 while(g_pDeviceList
)
2690 if(g_pDeviceList
->IsCaptureDevice
)
2691 alcCaptureCloseDevice(g_pDeviceList
);
2693 alcCloseDevice(g_pDeviceList
);
2698 if(g_ulDeviceCount
> 0)
2699 WARN("ReleaseALC(): %u Device%s not closed\n", g_ulDeviceCount
, (g_ulDeviceCount
>1)?"s":"");
2703 ///////////////////////////////////////////////////////