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
23 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
32 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM
, 0x00000001, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
33 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
, 0x00000003, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
35 DEFINE_GUID(CLSID_MMDeviceEnumerator
, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
36 DEFINE_GUID(IID_IMMDeviceEnumerator
, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
37 DEFINE_GUID(IID_IAudioClient
, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
38 DEFINE_GUID(IID_IAudioRenderClient
, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
58 #include "alAuxEffectSlot.h"
63 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
64 typedef struct BackendInfo
{
66 void (*Init
)(BackendFuncs
*);
68 void (*Probe
)(enum DevProbe
);
71 static BackendInfo BackendList
[] = {
72 #ifdef HAVE_PULSEAUDIO
73 { "pulse", alc_pulse_init
, alc_pulse_deinit
, alc_pulse_probe
, EmptyFuncs
},
76 { "alsa", alc_alsa_init
, alc_alsa_deinit
, alc_alsa_probe
, EmptyFuncs
},
79 { "core", alc_ca_init
, alc_ca_deinit
, alc_ca_probe
, EmptyFuncs
},
82 { "oss", alc_oss_init
, alc_oss_deinit
, alc_oss_probe
, EmptyFuncs
},
85 { "solaris", alc_solaris_init
, alc_solaris_deinit
, alc_solaris_probe
, EmptyFuncs
},
88 { "sndio", alc_sndio_init
, alc_sndio_deinit
, alc_sndio_probe
, EmptyFuncs
},
91 { "mmdevapi", alcMMDevApiInit
, alcMMDevApiDeinit
, alcMMDevApiProbe
, EmptyFuncs
},
94 { "dsound", alcDSoundInit
, alcDSoundDeinit
, alcDSoundProbe
, EmptyFuncs
},
97 { "winmm", alcWinMMInit
, alcWinMMDeinit
, alcWinMMProbe
, EmptyFuncs
},
100 { "port", alc_pa_init
, alc_pa_deinit
, alc_pa_probe
, EmptyFuncs
},
103 { "opensl", alc_opensl_init
, alc_opensl_deinit
, alc_opensl_probe
, EmptyFuncs
},
106 { "null", alc_null_init
, alc_null_deinit
, alc_null_probe
, EmptyFuncs
},
108 { "wave", alc_wave_init
, alc_wave_deinit
, alc_wave_probe
, EmptyFuncs
},
111 { NULL
, NULL
, NULL
, NULL
, EmptyFuncs
}
113 static BackendInfo BackendLoopback
= {
114 "loopback", alc_loopback_init
, alc_loopback_deinit
, alc_loopback_probe
, EmptyFuncs
118 ///////////////////////////////////////////////////////
119 // STRING and EXTENSIONS
121 typedef struct ALCfunction
{
122 const ALCchar
*funcName
;
126 typedef struct ALCenums
{
127 const ALCchar
*enumName
;
132 static const ALCfunction alcFunctions
[] = {
133 { "alcCreateContext", (ALCvoid
*) alcCreateContext
},
134 { "alcMakeContextCurrent", (ALCvoid
*) alcMakeContextCurrent
},
135 { "alcProcessContext", (ALCvoid
*) alcProcessContext
},
136 { "alcSuspendContext", (ALCvoid
*) alcSuspendContext
},
137 { "alcDestroyContext", (ALCvoid
*) alcDestroyContext
},
138 { "alcGetCurrentContext", (ALCvoid
*) alcGetCurrentContext
},
139 { "alcGetContextsDevice", (ALCvoid
*) alcGetContextsDevice
},
140 { "alcOpenDevice", (ALCvoid
*) alcOpenDevice
},
141 { "alcCloseDevice", (ALCvoid
*) alcCloseDevice
},
142 { "alcGetError", (ALCvoid
*) alcGetError
},
143 { "alcIsExtensionPresent", (ALCvoid
*) alcIsExtensionPresent
},
144 { "alcGetProcAddress", (ALCvoid
*) alcGetProcAddress
},
145 { "alcGetEnumValue", (ALCvoid
*) alcGetEnumValue
},
146 { "alcGetString", (ALCvoid
*) alcGetString
},
147 { "alcGetIntegerv", (ALCvoid
*) alcGetIntegerv
},
148 { "alcCaptureOpenDevice", (ALCvoid
*) alcCaptureOpenDevice
},
149 { "alcCaptureCloseDevice", (ALCvoid
*) alcCaptureCloseDevice
},
150 { "alcCaptureStart", (ALCvoid
*) alcCaptureStart
},
151 { "alcCaptureStop", (ALCvoid
*) alcCaptureStop
},
152 { "alcCaptureSamples", (ALCvoid
*) alcCaptureSamples
},
154 { "alcSetThreadContext", (ALCvoid
*) alcSetThreadContext
},
155 { "alcGetThreadContext", (ALCvoid
*) alcGetThreadContext
},
157 { "alcLoopbackOpenDeviceSOFT", (ALCvoid
*) alcLoopbackOpenDeviceSOFT
},
158 { "alcIsRenderFormatSupportedSOFT",(ALCvoid
*) alcIsRenderFormatSupportedSOFT
},
159 { "alcRenderSamplesSOFT", (ALCvoid
*) alcRenderSamplesSOFT
},
161 { "alEnable", (ALCvoid
*) alEnable
},
162 { "alDisable", (ALCvoid
*) alDisable
},
163 { "alIsEnabled", (ALCvoid
*) alIsEnabled
},
164 { "alGetString", (ALCvoid
*) alGetString
},
165 { "alGetBooleanv", (ALCvoid
*) alGetBooleanv
},
166 { "alGetIntegerv", (ALCvoid
*) alGetIntegerv
},
167 { "alGetFloatv", (ALCvoid
*) alGetFloatv
},
168 { "alGetDoublev", (ALCvoid
*) alGetDoublev
},
169 { "alGetBoolean", (ALCvoid
*) alGetBoolean
},
170 { "alGetInteger", (ALCvoid
*) alGetInteger
},
171 { "alGetFloat", (ALCvoid
*) alGetFloat
},
172 { "alGetDouble", (ALCvoid
*) alGetDouble
},
173 { "alGetError", (ALCvoid
*) alGetError
},
174 { "alIsExtensionPresent", (ALCvoid
*) alIsExtensionPresent
},
175 { "alGetProcAddress", (ALCvoid
*) alGetProcAddress
},
176 { "alGetEnumValue", (ALCvoid
*) alGetEnumValue
},
177 { "alListenerf", (ALCvoid
*) alListenerf
},
178 { "alListener3f", (ALCvoid
*) alListener3f
},
179 { "alListenerfv", (ALCvoid
*) alListenerfv
},
180 { "alListeneri", (ALCvoid
*) alListeneri
},
181 { "alListener3i", (ALCvoid
*) alListener3i
},
182 { "alListeneriv", (ALCvoid
*) alListeneriv
},
183 { "alGetListenerf", (ALCvoid
*) alGetListenerf
},
184 { "alGetListener3f", (ALCvoid
*) alGetListener3f
},
185 { "alGetListenerfv", (ALCvoid
*) alGetListenerfv
},
186 { "alGetListeneri", (ALCvoid
*) alGetListeneri
},
187 { "alGetListener3i", (ALCvoid
*) alGetListener3i
},
188 { "alGetListeneriv", (ALCvoid
*) alGetListeneriv
},
189 { "alGenSources", (ALCvoid
*) alGenSources
},
190 { "alDeleteSources", (ALCvoid
*) alDeleteSources
},
191 { "alIsSource", (ALCvoid
*) alIsSource
},
192 { "alSourcef", (ALCvoid
*) alSourcef
},
193 { "alSource3f", (ALCvoid
*) alSource3f
},
194 { "alSourcefv", (ALCvoid
*) alSourcefv
},
195 { "alSourcei", (ALCvoid
*) alSourcei
},
196 { "alSource3i", (ALCvoid
*) alSource3i
},
197 { "alSourceiv", (ALCvoid
*) alSourceiv
},
198 { "alGetSourcef", (ALCvoid
*) alGetSourcef
},
199 { "alGetSource3f", (ALCvoid
*) alGetSource3f
},
200 { "alGetSourcefv", (ALCvoid
*) alGetSourcefv
},
201 { "alGetSourcei", (ALCvoid
*) alGetSourcei
},
202 { "alGetSource3i", (ALCvoid
*) alGetSource3i
},
203 { "alGetSourceiv", (ALCvoid
*) alGetSourceiv
},
204 { "alSourcePlayv", (ALCvoid
*) alSourcePlayv
},
205 { "alSourceStopv", (ALCvoid
*) alSourceStopv
},
206 { "alSourceRewindv", (ALCvoid
*) alSourceRewindv
},
207 { "alSourcePausev", (ALCvoid
*) alSourcePausev
},
208 { "alSourcePlay", (ALCvoid
*) alSourcePlay
},
209 { "alSourceStop", (ALCvoid
*) alSourceStop
},
210 { "alSourceRewind", (ALCvoid
*) alSourceRewind
},
211 { "alSourcePause", (ALCvoid
*) alSourcePause
},
212 { "alSourceQueueBuffers", (ALCvoid
*) alSourceQueueBuffers
},
213 { "alSourceUnqueueBuffers", (ALCvoid
*) alSourceUnqueueBuffers
},
214 { "alGenBuffers", (ALCvoid
*) alGenBuffers
},
215 { "alDeleteBuffers", (ALCvoid
*) alDeleteBuffers
},
216 { "alIsBuffer", (ALCvoid
*) alIsBuffer
},
217 { "alBufferData", (ALCvoid
*) alBufferData
},
218 { "alBufferf", (ALCvoid
*) alBufferf
},
219 { "alBuffer3f", (ALCvoid
*) alBuffer3f
},
220 { "alBufferfv", (ALCvoid
*) alBufferfv
},
221 { "alBufferi", (ALCvoid
*) alBufferi
},
222 { "alBuffer3i", (ALCvoid
*) alBuffer3i
},
223 { "alBufferiv", (ALCvoid
*) alBufferiv
},
224 { "alGetBufferf", (ALCvoid
*) alGetBufferf
},
225 { "alGetBuffer3f", (ALCvoid
*) alGetBuffer3f
},
226 { "alGetBufferfv", (ALCvoid
*) alGetBufferfv
},
227 { "alGetBufferi", (ALCvoid
*) alGetBufferi
},
228 { "alGetBuffer3i", (ALCvoid
*) alGetBuffer3i
},
229 { "alGetBufferiv", (ALCvoid
*) alGetBufferiv
},
230 { "alDopplerFactor", (ALCvoid
*) alDopplerFactor
},
231 { "alDopplerVelocity", (ALCvoid
*) alDopplerVelocity
},
232 { "alSpeedOfSound", (ALCvoid
*) alSpeedOfSound
},
233 { "alDistanceModel", (ALCvoid
*) alDistanceModel
},
235 { "alGenFilters", (ALCvoid
*) alGenFilters
},
236 { "alDeleteFilters", (ALCvoid
*) alDeleteFilters
},
237 { "alIsFilter", (ALCvoid
*) alIsFilter
},
238 { "alFilteri", (ALCvoid
*) alFilteri
},
239 { "alFilteriv", (ALCvoid
*) alFilteriv
},
240 { "alFilterf", (ALCvoid
*) alFilterf
},
241 { "alFilterfv", (ALCvoid
*) alFilterfv
},
242 { "alGetFilteri", (ALCvoid
*) alGetFilteri
},
243 { "alGetFilteriv", (ALCvoid
*) alGetFilteriv
},
244 { "alGetFilterf", (ALCvoid
*) alGetFilterf
},
245 { "alGetFilterfv", (ALCvoid
*) alGetFilterfv
},
247 { "alGenEffects", (ALCvoid
*) alGenEffects
},
248 { "alDeleteEffects", (ALCvoid
*) alDeleteEffects
},
249 { "alIsEffect", (ALCvoid
*) alIsEffect
},
250 { "alEffecti", (ALCvoid
*) alEffecti
},
251 { "alEffectiv", (ALCvoid
*) alEffectiv
},
252 { "alEffectf", (ALCvoid
*) alEffectf
},
253 { "alEffectfv", (ALCvoid
*) alEffectfv
},
254 { "alGetEffecti", (ALCvoid
*) alGetEffecti
},
255 { "alGetEffectiv", (ALCvoid
*) alGetEffectiv
},
256 { "alGetEffectf", (ALCvoid
*) alGetEffectf
},
257 { "alGetEffectfv", (ALCvoid
*) alGetEffectfv
},
259 { "alGenAuxiliaryEffectSlots", (ALCvoid
*) alGenAuxiliaryEffectSlots
},
260 { "alDeleteAuxiliaryEffectSlots",(ALCvoid
*) alDeleteAuxiliaryEffectSlots
},
261 { "alIsAuxiliaryEffectSlot", (ALCvoid
*) alIsAuxiliaryEffectSlot
},
262 { "alAuxiliaryEffectSloti", (ALCvoid
*) alAuxiliaryEffectSloti
},
263 { "alAuxiliaryEffectSlotiv", (ALCvoid
*) alAuxiliaryEffectSlotiv
},
264 { "alAuxiliaryEffectSlotf", (ALCvoid
*) alAuxiliaryEffectSlotf
},
265 { "alAuxiliaryEffectSlotfv", (ALCvoid
*) alAuxiliaryEffectSlotfv
},
266 { "alGetAuxiliaryEffectSloti", (ALCvoid
*) alGetAuxiliaryEffectSloti
},
267 { "alGetAuxiliaryEffectSlotiv", (ALCvoid
*) alGetAuxiliaryEffectSlotiv
},
268 { "alGetAuxiliaryEffectSlotf", (ALCvoid
*) alGetAuxiliaryEffectSlotf
},
269 { "alGetAuxiliaryEffectSlotfv", (ALCvoid
*) alGetAuxiliaryEffectSlotfv
},
271 { "alBufferSubDataSOFT", (ALCvoid
*) alBufferSubDataSOFT
},
273 { "alBufferSamplesSOFT", (ALCvoid
*) alBufferSamplesSOFT
},
274 { "alBufferSubSamplesSOFT", (ALCvoid
*) alBufferSubSamplesSOFT
},
275 { "alGetBufferSamplesSOFT", (ALCvoid
*) alGetBufferSamplesSOFT
},
276 { "alIsBufferFormatSupportedSOFT",(ALCvoid
*) alIsBufferFormatSupportedSOFT
},
278 { "alDeferUpdatesSOFT", (ALCvoid
*) alDeferUpdatesSOFT
},
279 { "alProcessUpdatesSOFT", (ALCvoid
*) alProcessUpdatesSOFT
},
281 { NULL
, (ALCvoid
*) NULL
}
284 static const ALCenums enumeration
[] = {
286 { "ALC_INVALID", ALC_INVALID
},
287 { "ALC_FALSE", ALC_FALSE
},
288 { "ALC_TRUE", ALC_TRUE
},
291 { "ALC_MAJOR_VERSION", ALC_MAJOR_VERSION
},
292 { "ALC_MINOR_VERSION", ALC_MINOR_VERSION
},
293 { "ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE
},
294 { "ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES
},
295 { "ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER
},
296 { "ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER
},
297 { "ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER
},
298 { "ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER
},
299 { "ALC_EXTENSIONS", ALC_EXTENSIONS
},
300 { "ALC_FREQUENCY", ALC_FREQUENCY
},
301 { "ALC_REFRESH", ALC_REFRESH
},
302 { "ALC_SYNC", ALC_SYNC
},
303 { "ALC_MONO_SOURCES", ALC_MONO_SOURCES
},
304 { "ALC_STEREO_SOURCES", ALC_STEREO_SOURCES
},
305 { "ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER
},
306 { "ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
},
307 { "ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES
},
308 { "ALC_CONNECTED", ALC_CONNECTED
},
311 { "ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION
},
312 { "ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION
},
313 { "ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS
},
315 // Loopback device Properties
316 { "ALC_FORMAT_CHANNELS_SOFT", ALC_FORMAT_CHANNELS_SOFT
},
317 { "ALC_FORMAT_TYPE_SOFT", ALC_FORMAT_TYPE_SOFT
},
319 // Buffer Channel Configurations
320 { "ALC_MONO", ALC_MONO
},
321 { "ALC_STEREO", ALC_STEREO
},
322 { "ALC_QUAD", ALC_QUAD
},
323 { "ALC_5POINT1", ALC_5POINT1
},
324 { "ALC_6POINT1", ALC_6POINT1
},
325 { "ALC_7POINT1", ALC_7POINT1
},
327 // Buffer Sample Types
328 { "ALC_BYTE", ALC_BYTE
},
329 { "ALC_UNSIGNED_BYTE", ALC_UNSIGNED_BYTE
},
330 { "ALC_SHORT", ALC_SHORT
},
331 { "ALC_UNSIGNED_SHORT", ALC_UNSIGNED_SHORT
},
332 { "ALC_INT", ALC_INT
},
333 { "ALC_UNSIGNED_INT", ALC_UNSIGNED_INT
},
334 { "ALC_FLOAT", ALC_FLOAT
},
337 { "ALC_NO_ERROR", ALC_NO_ERROR
},
338 { "ALC_INVALID_DEVICE", ALC_INVALID_DEVICE
},
339 { "ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT
},
340 { "ALC_INVALID_ENUM", ALC_INVALID_ENUM
},
341 { "ALC_INVALID_VALUE", ALC_INVALID_VALUE
},
342 { "ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY
},
347 static const ALCchar alcNoError
[] = "No Error";
348 static const ALCchar alcErrInvalidDevice
[] = "Invalid Device";
349 static const ALCchar alcErrInvalidContext
[] = "Invalid Context";
350 static const ALCchar alcErrInvalidEnum
[] = "Invalid Enum";
351 static const ALCchar alcErrInvalidValue
[] = "Invalid Value";
352 static const ALCchar alcErrOutOfMemory
[] = "Out of Memory";
354 /* Device lists. Sizes only include the first ending null character, not the
356 static ALCchar
*alcDeviceList
;
357 static size_t alcDeviceListSize
;
358 static ALCchar
*alcAllDeviceList
;
359 static size_t alcAllDeviceListSize
;
360 static ALCchar
*alcCaptureDeviceList
;
361 static size_t alcCaptureDeviceListSize
;
362 // Default is always the first in the list
363 static ALCchar
*alcDefaultDeviceSpecifier
;
364 static ALCchar
*alcDefaultAllDeviceSpecifier
;
365 static ALCchar
*alcCaptureDefaultDeviceSpecifier
;
368 static const ALCchar alcNoDeviceExtList
[] =
369 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
370 "ALC_EXT_thread_local_context ALC_SOFTX_loopback_device";
371 static const ALCchar alcExtensionList
[] =
372 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
373 "ALC_EXT_DEDICATED ALC_EXT_disconnect ALC_EXT_EFX "
374 "ALC_EXT_thread_local_context ALC_SOFTX_loopback_device";
375 static const ALCint alcMajorVersion
= 1;
376 static const ALCint alcMinorVersion
= 1;
378 static const ALCint alcEFXMajorVersion
= 1;
379 static const ALCint alcEFXMinorVersion
= 0;
381 ///////////////////////////////////////////////////////
384 ///////////////////////////////////////////////////////
387 static CRITICAL_SECTION ListLock
;
390 static ALCdevice
*g_pDeviceList
= NULL
;
391 static ALCuint g_ulDeviceCount
= 0;
394 static ALCcontext
*g_pContextList
= NULL
;
395 static ALCuint g_ulContextCount
= 0;
397 // Thread-local current context
398 static tls_type LocalContext
;
399 // Process-wide current context
400 static ALCcontext
*GlobalContext
;
403 static ALCenum g_eLastNullDeviceError
= ALC_NO_ERROR
;
405 // Default context extensions
406 static const ALchar alExtList
[] =
407 "AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 AL_EXT_IMA4 "
408 "AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_MULAW "
409 "AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET AL_EXT_source_distance_model "
410 "AL_LOKI_quadriphonic AL_SOFTX_buffer_samples AL_SOFT_buffer_sub_data "
411 "AL_SOFT_loop_points AL_SOFTX_non_virtual_channels";
413 // Mixing Priority Level
414 static ALint RTPrioLevel
;
417 static FILE *LogFile
;
421 enum LogLevel LogLevel
= LogWarning
;
423 enum LogLevel LogLevel
= LogError
;
427 ALdouble ConeScale
= 0.5;
429 // Localized Z scalar for mono sources
430 ALdouble ZScale
= 1.0;
432 ///////////////////////////////////////////////////////
435 ///////////////////////////////////////////////////////
436 // ALC Related helper functions
437 static void ReleaseALC(void);
439 static void alc_initconfig(void);
442 static void alc_init(void);
443 static void alc_deinit(void);
445 #ifndef AL_LIBTYPE_STATIC
446 BOOL APIENTRY
DllMain(HANDLE hModule
,DWORD ul_reason_for_call
,LPVOID lpReserved
)
450 // Perform actions based on the reason for calling.
451 switch(ul_reason_for_call
)
453 case DLL_PROCESS_ATTACH
:
454 DisableThreadLibraryCalls(hModule
);
458 case DLL_PROCESS_DETACH
:
464 #elif defined(_MSC_VER)
465 #pragma section(".CRT$XCU",read)
466 static void alc_constructor(void);
467 static void alc_destructor(void);
468 __declspec(allocate(".CRT$XCU")) void (__cdecl
* alc_constructor_
)(void) = alc_constructor
;
470 static void alc_constructor(void)
472 atexit(alc_destructor
);
476 static void alc_destructor(void)
480 #elif defined(HAVE_GCC_DESTRUCTOR)
481 static void alc_init(void) __attribute__((constructor
));
482 static void alc_deinit(void) __attribute__((destructor
));
484 #error "No static initialization available on this platform!"
486 #elif defined(HAVE_GCC_DESTRUCTOR)
487 static void alc_init(void) __attribute__((constructor
));
488 static void alc_deinit(void) __attribute__((destructor
));
490 #error "No global initialization available on this platform!"
493 static void alc_init(void)
499 str
= getenv("__ALSOFT_HALF_ANGLE_CONES");
500 if(str
&& (strcasecmp(str
, "true") == 0 || strtol(str
, NULL
, 0) == 1))
503 str
= getenv("__ALSOFT_REVERSE_Z");
504 if(str
&& (strcasecmp(str
, "true") == 0 || strtol(str
, NULL
, 0) == 1))
507 tls_create(&LocalContext
);
508 InitializeCriticalSection(&ListLock
);
516 static void alc_deinit(void)
522 for(i
= 0;BackendList
[i
].Deinit
;i
++)
523 BackendList
[i
].Deinit();
524 BackendLoopback
.Deinit();
528 DeleteCriticalSection(&ListLock
);
529 tls_delete(LocalContext
);
531 if(LogFile
!= stderr
)
536 static void alc_initconfig(void)
539 const char *devs
, *str
;
541 str
= getenv("ALSOFT_LOGLEVEL");
544 long lvl
= strtol(str
, NULL
, 0);
545 if(lvl
>= NoLog
&& lvl
<= LogTrace
)
549 str
= getenv("ALSOFT_LOGFILE");
552 FILE *logfile
= fopen(str
, "wat");
553 if(logfile
) LogFile
= logfile
;
554 else ERR("Failed to open log file '%s'\n", str
);
561 RTPrioLevel
= GetConfigValueInt(NULL
, "rt-prio", 0);
563 DefaultResampler
= GetConfigValueInt(NULL
, "resampler", RESAMPLER_DEFAULT
);
564 if(DefaultResampler
>= RESAMPLER_MAX
|| DefaultResampler
<= RESAMPLER_MIN
)
565 DefaultResampler
= RESAMPLER_DEFAULT
;
567 ReverbBoost
*= aluPow(10.0f
, GetConfigValueFloat("reverb", "boost", 0.0f
) /
569 EmulateEAXReverb
= GetConfigValueBool("reverb", "emulate-eax", AL_FALSE
);
571 devs
= GetConfigValue(NULL
, "drivers", "");
576 const char *next
= devs
;
577 int endlist
, delitem
;
582 next
= strchr(devs
, ',');
584 delitem
= (devs
[0] == '-');
585 if(devs
[0] == '-') devs
++;
587 if(!devs
[0] || devs
[0] == ',')
594 len
= (next
? ((size_t)(next
-devs
)) : strlen(devs
));
595 for(n
= i
;BackendList
[n
].Init
;n
++)
597 if(len
== strlen(BackendList
[n
].name
) &&
598 strncmp(BackendList
[n
].name
, devs
, len
) == 0)
603 BackendList
[n
] = BackendList
[n
+1];
605 } while(BackendList
[n
].Init
);
609 BackendInfo Bkp
= BackendList
[n
];
612 BackendList
[n
] = BackendList
[n
-1];
615 BackendList
[n
] = Bkp
;
626 BackendList
[i
].name
= NULL
;
627 BackendList
[i
].Init
= NULL
;
628 BackendList
[i
].Deinit
= NULL
;
629 BackendList
[i
].Probe
= NULL
;
633 for(i
= 0;BackendList
[i
].Init
;i
++)
634 BackendList
[i
].Init(&BackendList
[i
].Funcs
);
635 BackendLoopback
.Init(&BackendLoopback
.Funcs
);
637 str
= GetConfigValue(NULL
, "excludefx", "");
642 const char *next
= str
;
646 next
= strchr(str
, ',');
648 if(!str
[0] || next
== str
)
651 len
= (next
? ((size_t)(next
-str
)) : strlen(str
));
652 for(n
= 0;EffectList
[n
].name
;n
++)
654 if(len
== strlen(EffectList
[n
].name
) &&
655 strncmp(EffectList
[n
].name
, str
, len
) == 0)
656 DisabledEffects
[EffectList
[n
].type
] = AL_TRUE
;
663 static pthread_once_t once_control
= PTHREAD_ONCE_INIT
;
664 #define DO_INITCONFIG() pthread_once(&once_control, alc_initconfig)
666 #define DO_INITCONFIG()
669 static void ProbeList(ALCchar
**list
, size_t *listsize
, int type
)
678 for(i
= 0;BackendList
[i
].Probe
;i
++)
679 BackendList
[i
].Probe(type
);
682 static void ProbeDeviceList()
683 { ProbeList(&alcDeviceList
, &alcDeviceListSize
, DEVICE_PROBE
); }
684 static void ProbeAllDeviceList()
685 { ProbeList(&alcAllDeviceList
, &alcAllDeviceListSize
, ALL_DEVICE_PROBE
); }
686 static void ProbeCaptureDeviceList()
687 { ProbeList(&alcCaptureDeviceList
, &alcCaptureDeviceListSize
, CAPTURE_DEVICE_PROBE
); }
690 static void AppendList(const ALCchar
*name
, ALCchar
**List
, size_t *ListSize
)
692 size_t len
= strlen(name
);
698 temp
= realloc(*List
, (*ListSize
) + len
+ 2);
701 ERR("Realloc failed to add %s!\n", name
);
706 memcpy((*List
)+(*ListSize
), name
, len
+1);
708 (*List
)[*ListSize
] = 0;
711 #define DECL_APPEND_LIST_FUNC(type) \
712 void Append##type##List(const ALCchar *name) \
713 { AppendList(name, &alc##type##List, &alc##type##ListSize); }
715 DECL_APPEND_LIST_FUNC(Device
)
716 DECL_APPEND_LIST_FUNC(AllDevice
)
717 DECL_APPEND_LIST_FUNC(CaptureDevice
)
719 #undef DECL_APPEND_LIST_FUNC
722 void al_print(const char *fname
, unsigned int line
, const char *fmt
, ...)
728 fn
= strrchr(fname
, '/');
729 if(!fn
) fn
= strrchr(fname
, '\\');
733 i
= snprintf(str
, sizeof(str
), "AL lib: %s:%d: ", fn
, line
);
734 if(i
< (int)sizeof(str
) && i
> 0)
738 vsnprintf(str
+i
, sizeof(str
)-i
, fmt
, ap
);
741 str
[sizeof(str
)-1] = 0;
743 fprintf(LogFile
, "%s", str
);
747 void SetRTPriority(void)
753 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL
);
755 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL
);
756 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
757 struct sched_param param
;
761 /* Use the minimum real-time priority possible for now (on Linux this
762 * should be 1 for SCHED_RR) */
763 param
.sched_priority
= sched_get_priority_min(SCHED_RR
);
764 failed
= !!pthread_setschedparam(pthread_self(), SCHED_RR
, ¶m
);
768 param
.sched_priority
= 0;
769 failed
= !!pthread_setschedparam(pthread_self(), SCHED_OTHER
, ¶m
);
772 /* Real-time priority not available */
773 failed
= (RTPrioLevel
>0);
776 ERR("Failed to set priority level for thread\n");
780 void InitUIntMap(UIntMap
*map
)
787 void ResetUIntMap(UIntMap
*map
)
795 ALenum
InsertUIntMapEntry(UIntMap
*map
, ALuint key
, ALvoid
*value
)
802 ALsizei high
= map
->size
- 1;
805 ALsizei mid
= low
+ (high
-low
)/2;
806 if(map
->array
[mid
].key
< key
)
811 if(map
->array
[low
].key
< key
)
816 if(pos
== map
->size
|| map
->array
[pos
].key
!= key
)
818 if(map
->size
== map
->maxsize
)
823 newsize
= (map
->maxsize
? (map
->maxsize
<<1) : 4);
824 if(newsize
< map
->maxsize
)
825 return AL_OUT_OF_MEMORY
;
827 temp
= realloc(map
->array
, newsize
*sizeof(map
->array
[0]));
828 if(!temp
) return AL_OUT_OF_MEMORY
;
830 map
->maxsize
= newsize
;
834 if(pos
< map
->size
-1)
835 memmove(&map
->array
[pos
+1], &map
->array
[pos
],
836 (map
->size
-1-pos
)*sizeof(map
->array
[0]));
838 map
->array
[pos
].key
= key
;
839 map
->array
[pos
].value
= value
;
844 void RemoveUIntMapKey(UIntMap
*map
, ALuint key
)
849 ALsizei high
= map
->size
- 1;
852 ALsizei mid
= low
+ (high
-low
)/2;
853 if(map
->array
[mid
].key
< key
)
858 if(map
->array
[low
].key
== key
)
860 if(low
< map
->size
-1)
861 memmove(&map
->array
[low
], &map
->array
[low
+1],
862 (map
->size
-1-low
)*sizeof(map
->array
[0]));
868 ALvoid
*LookupUIntMapKey(UIntMap
*map
, ALuint key
)
873 ALsizei high
= map
->size
- 1;
876 ALsizei mid
= low
+ (high
-low
)/2;
877 if(map
->array
[mid
].key
< key
)
882 if(map
->array
[low
].key
== key
)
883 return map
->array
[low
].value
;
889 const ALCchar
*DevFmtTypeString(enum DevFmtType type
)
893 case DevFmtByte
: return "Signed Byte";
894 case DevFmtUByte
: return "Unsigned Byte";
895 case DevFmtShort
: return "Signed Short";
896 case DevFmtUShort
: return "Unsigned Short";
897 case DevFmtFloat
: return "Float";
899 return "(unknown type)";
901 const ALCchar
*DevFmtChannelsString(enum DevFmtChannels chans
)
905 case DevFmtMono
: return "Mono";
906 case DevFmtStereo
: return "Stereo";
907 case DevFmtQuad
: return "Quadraphonic";
908 case DevFmtX51
: return "5.1 Surround";
909 case DevFmtX51Side
: return "5.1 Side";
910 case DevFmtX61
: return "6.1 Surround";
911 case DevFmtX71
: return "7.1 Surround";
913 return "(unknown channels)";
916 ALuint
BytesFromDevFmt(enum DevFmtType type
)
920 case DevFmtByte
: return sizeof(ALbyte
);
921 case DevFmtUByte
: return sizeof(ALubyte
);
922 case DevFmtShort
: return sizeof(ALshort
);
923 case DevFmtUShort
: return sizeof(ALushort
);
924 case DevFmtFloat
: return sizeof(ALfloat
);
928 ALuint
ChannelsFromDevFmt(enum DevFmtChannels chans
)
932 case DevFmtMono
: return 1;
933 case DevFmtStereo
: return 2;
934 case DevFmtQuad
: return 4;
935 case DevFmtX51
: return 6;
936 case DevFmtX51Side
: return 6;
937 case DevFmtX61
: return 7;
938 case DevFmtX71
: return 8;
942 ALboolean
DecomposeDevFormat(ALenum format
, enum DevFmtChannels
*chans
,
943 enum DevFmtType
*type
)
947 case AL_FORMAT_MONO8
:
951 case AL_FORMAT_MONO16
:
955 case AL_FORMAT_MONO_FLOAT32
:
959 case AL_FORMAT_STEREO8
:
960 *chans
= DevFmtStereo
;
963 case AL_FORMAT_STEREO16
:
964 *chans
= DevFmtStereo
;
967 case AL_FORMAT_STEREO_FLOAT32
:
968 *chans
= DevFmtStereo
;
971 case AL_FORMAT_QUAD8
:
975 case AL_FORMAT_QUAD16
:
979 case AL_FORMAT_QUAD32
:
983 case AL_FORMAT_51CHN8
:
987 case AL_FORMAT_51CHN16
:
991 case AL_FORMAT_51CHN32
:
995 case AL_FORMAT_61CHN8
:
999 case AL_FORMAT_61CHN16
:
1001 *type
= DevFmtShort
;
1003 case AL_FORMAT_61CHN32
:
1005 *type
= DevFmtFloat
;
1007 case AL_FORMAT_71CHN8
:
1009 *type
= DevFmtUByte
;
1011 case AL_FORMAT_71CHN16
:
1013 *type
= DevFmtShort
;
1015 case AL_FORMAT_71CHN32
:
1017 *type
= DevFmtFloat
;
1023 static ALCboolean
IsValidALCType(ALCenum type
)
1028 case ALC_UNSIGNED_BYTE
:
1030 case ALC_UNSIGNED_SHORT
:
1032 case ALC_UNSIGNED_INT
:
1039 static ALCboolean
IsValidALCChannels(ALCenum channels
)
1056 void InitializeCriticalSection(CRITICAL_SECTION
*cs
)
1058 pthread_mutexattr_t attrib
;
1061 ret
= pthread_mutexattr_init(&attrib
);
1064 ret
= pthread_mutexattr_settype(&attrib
, PTHREAD_MUTEX_RECURSIVE
);
1065 #ifdef HAVE_PTHREAD_NP_H
1067 ret
= pthread_mutexattr_setkind_np(&attrib
, PTHREAD_MUTEX_RECURSIVE
);
1070 ret
= pthread_mutex_init(cs
, &attrib
);
1073 pthread_mutexattr_destroy(&attrib
);
1075 void DeleteCriticalSection(CRITICAL_SECTION
*cs
)
1078 ret
= pthread_mutex_destroy(cs
);
1081 void EnterCriticalSection(CRITICAL_SECTION
*cs
)
1084 ret
= pthread_mutex_lock(cs
);
1087 void LeaveCriticalSection(CRITICAL_SECTION
*cs
)
1090 ret
= pthread_mutex_unlock(cs
);
1094 /* NOTE: This wrapper isn't quite accurate as it returns an ALuint, as opposed
1095 * to the expected DWORD. Both are defined as unsigned 32-bit types, however.
1096 * Additionally, Win32 is supposed to measure the time since Windows started,
1097 * as opposed to the actual time. */
1098 ALuint
timeGetTime(void)
1100 #if _POSIX_TIMERS > 0
1104 #if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0)
1105 #if _POSIX_MONOTONIC_CLOCK == 0
1106 static int hasmono
= 0;
1107 if(hasmono
> 0 || (hasmono
== 0 &&
1108 (hasmono
=sysconf(_SC_MONOTONIC_CLOCK
)) > 0))
1110 ret
= clock_gettime(CLOCK_MONOTONIC
, &ts
);
1113 ret
= clock_gettime(CLOCK_REALTIME
, &ts
);
1116 return ts
.tv_nsec
/1000000 + ts
.tv_sec
*1000;
1121 ret
= gettimeofday(&tv
, NULL
);
1124 return tv
.tv_usec
/1000 + tv
.tv_sec
*1000;
1130 void *LoadLib(const char *name
)
1131 { return LoadLibraryA(name
); }
1133 void CloseLib(void *handle
)
1134 { FreeLibrary((HANDLE
)handle
); }
1136 void *GetSymbol(void *handle
, const char *name
)
1140 ret
= (void*)GetProcAddress((HANDLE
)handle
, name
);
1142 ERR("Failed to load %s\n", name
);
1146 #elif defined(HAVE_DLFCN_H)
1148 void *LoadLib(const char *name
)
1154 handle
= dlopen(name
, RTLD_NOW
);
1155 if((err
=dlerror()) != NULL
)
1160 void CloseLib(void *handle
)
1161 { dlclose(handle
); }
1163 void *GetSymbol(void *handle
, const char *name
)
1169 sym
= dlsym(handle
, name
);
1170 if((err
=dlerror()) != NULL
)
1172 ERR("Failed to load %s: %s\n", name
, err
);
1179 static void LockLists(void)
1181 EnterCriticalSection(&ListLock
);
1184 static void UnlockLists(void)
1186 LeaveCriticalSection(&ListLock
);
1192 Check pDevice is a valid Device pointer
1194 static ALCboolean
IsDevice(ALCdevice
*pDevice
)
1196 ALCdevice
*pTempDevice
;
1198 pTempDevice
= g_pDeviceList
;
1199 while(pTempDevice
&& pTempDevice
!= pDevice
)
1200 pTempDevice
= pTempDevice
->next
;
1202 return (pTempDevice
? ALC_TRUE
: ALC_FALSE
);
1208 Check pContext is a valid Context pointer
1210 static ALCboolean
IsContext(ALCcontext
*pContext
)
1212 ALCcontext
*pTempContext
;
1214 pTempContext
= g_pContextList
;
1215 while (pTempContext
&& pTempContext
!= pContext
)
1216 pTempContext
= pTempContext
->next
;
1218 return (pTempContext
? ALC_TRUE
: ALC_FALSE
);
1225 Store latest ALC Error
1227 ALCvoid
alcSetError(ALCdevice
*device
, ALenum errorCode
)
1230 if(IsDevice(device
))
1231 device
->LastError
= errorCode
;
1233 g_eLastNullDeviceError
= errorCode
;
1238 /* UpdateDeviceParams:
1240 * Updates device parameters according to the attribute list.
1242 static ALCboolean
UpdateDeviceParams(ALCdevice
*device
, const ALCint
*attrList
)
1246 // Check for attributes
1247 if(attrList
&& attrList
[0])
1249 ALCuint freq
, numMono
, numStereo
, numSends
;
1250 enum DevFmtChannels schans
;
1251 enum DevFmtType stype
;
1254 // If a context is already running on the device, stop playback so the
1255 // device attributes can be updated
1256 if((device
->Flags
&DEVICE_RUNNING
))
1257 ALCdevice_StopPlayback(device
);
1258 device
->Flags
&= ~DEVICE_RUNNING
;
1260 freq
= device
->Frequency
;
1261 schans
= device
->FmtChans
;
1262 stype
= device
->FmtType
;
1263 numMono
= device
->NumMonoSources
;
1264 numStereo
= device
->NumStereoSources
;
1265 numSends
= device
->NumAuxSends
;
1267 freq
= GetConfigValueInt(NULL
, "frequency", freq
);
1268 if(freq
< 8000) freq
= 8000;
1271 while(attrList
[attrIdx
])
1273 if(attrList
[attrIdx
] == ALC_FORMAT_CHANNELS_SOFT
&&
1274 device
->IsLoopbackDevice
)
1276 ALCint val
= attrList
[attrIdx
+ 1];
1277 if(!IsValidALCChannels(val
) || !ChannelsFromDevFmt(val
))
1279 alcSetError(device
, ALC_INVALID_VALUE
);
1285 if(attrList
[attrIdx
] == ALC_FORMAT_TYPE_SOFT
&&
1286 device
->IsLoopbackDevice
)
1288 ALCint val
= attrList
[attrIdx
+ 1];
1289 if(!IsValidALCType(val
) || !BytesFromDevFmt(val
))
1291 alcSetError(device
, ALC_INVALID_VALUE
);
1297 if(attrList
[attrIdx
] == ALC_FREQUENCY
)
1299 if(device
->IsLoopbackDevice
)
1301 freq
= attrList
[attrIdx
+ 1];
1304 alcSetError(device
, ALC_INVALID_VALUE
);
1308 else if(!ConfigValueExists(NULL
, "frequency"))
1310 freq
= attrList
[attrIdx
+ 1];
1311 if(freq
< 8000) freq
= 8000;
1312 device
->Flags
|= DEVICE_FREQUENCY_REQUEST
;
1316 if(attrList
[attrIdx
] == ALC_STEREO_SOURCES
)
1318 numStereo
= attrList
[attrIdx
+ 1];
1319 if(numStereo
> device
->MaxNoOfSources
)
1320 numStereo
= device
->MaxNoOfSources
;
1322 numMono
= device
->MaxNoOfSources
- numStereo
;
1325 if(attrList
[attrIdx
] == ALC_MAX_AUXILIARY_SENDS
&&
1326 !ConfigValueExists(NULL
, "sends"))
1328 numSends
= attrList
[attrIdx
+ 1];
1329 if(numSends
> MAX_SENDS
)
1330 numSends
= MAX_SENDS
;
1336 device
->UpdateSize
= (ALuint64
)device
->UpdateSize
* freq
/
1339 device
->Frequency
= freq
;
1340 device
->FmtChans
= schans
;
1341 device
->FmtType
= stype
;
1342 device
->NumMonoSources
= numMono
;
1343 device
->NumStereoSources
= numStereo
;
1344 device
->NumAuxSends
= numSends
;
1347 if((device
->Flags
&DEVICE_RUNNING
))
1351 TRACE("Format pre-setup: %s%s, %s, %uhz%s, %u update size x%d\n",
1352 DevFmtChannelsString(device
->FmtChans
),
1353 (device
->Flags
&DEVICE_CHANNELS_REQUEST
)?" (requested)":"",
1354 DevFmtTypeString(device
->FmtType
), device
->Frequency
,
1355 (device
->Flags
&DEVICE_FREQUENCY_REQUEST
)?" (requested)":"",
1356 device
->UpdateSize
, device
->NumUpdates
);
1357 if(ALCdevice_ResetPlayback(device
) == ALC_FALSE
)
1359 UnlockDevice(device
);
1362 device
->Flags
|= DEVICE_RUNNING
;
1363 TRACE("Format post-setup: %s%s, %s, %uhz%s, %u update size x%d\n",
1364 DevFmtChannelsString(device
->FmtChans
),
1365 (device
->Flags
&DEVICE_CHANNELS_REQUEST
)?" (requested)":"",
1366 DevFmtTypeString(device
->FmtType
), device
->Frequency
,
1367 (device
->Flags
&DEVICE_FREQUENCY_REQUEST
)?" (requested)":"",
1368 device
->UpdateSize
, device
->NumUpdates
);
1370 aluInitPanning(device
);
1372 for(i
= 0;i
< MAXCHANNELS
;i
++)
1374 device
->ClickRemoval
[i
] = 0.0f
;
1375 device
->PendingClicks
[i
] = 0.0f
;
1378 if(!device
->IsLoopbackDevice
&& GetConfigValueBool(NULL
, "hrtf", AL_FALSE
))
1379 device
->Flags
|= DEVICE_USE_HRTF
;
1380 if((device
->Flags
&DEVICE_USE_HRTF
) && !IsHrtfCompatible(device
))
1381 device
->Flags
&= ~DEVICE_USE_HRTF
;
1382 TRACE("HRTF %s\n", (device
->Flags
&DEVICE_USE_HRTF
)?"enabled":"disabled");
1384 if(!(device
->Flags
&DEVICE_USE_HRTF
) && device
->Bs2bLevel
> 0 && device
->Bs2bLevel
<= 6)
1388 device
->Bs2b
= calloc(1, sizeof(*device
->Bs2b
));
1389 bs2b_clear(device
->Bs2b
);
1391 bs2b_set_srate(device
->Bs2b
, device
->Frequency
);
1392 bs2b_set_level(device
->Bs2b
, device
->Bs2bLevel
);
1393 TRACE("BS2B level %d\n", device
->Bs2bLevel
);
1398 device
->Bs2b
= NULL
;
1399 TRACE("BS2B disabled\n");
1402 device
->Flags
&= ~DEVICE_DUPLICATE_STEREO
;
1403 switch(device
->FmtChans
)
1413 if(GetConfigValueBool(NULL
, "stereodup", AL_TRUE
))
1414 device
->Flags
|= DEVICE_DUPLICATE_STEREO
;
1417 TRACE("Stereo duplication %s\n", (device
->Flags
&DEVICE_DUPLICATE_STEREO
)?"enabled":"disabled");
1419 for(i
= 0;i
< device
->NumContexts
;i
++)
1421 ALCcontext
*context
= device
->Contexts
[i
];
1424 context
->UpdateSources
= AL_FALSE
;
1425 for(pos
= 0;pos
< context
->EffectSlotMap
.size
;pos
++)
1427 ALeffectslot
*slot
= context
->EffectSlotMap
.array
[pos
].value
;
1429 if(ALEffect_DeviceUpdate(slot
->EffectState
, device
) == AL_FALSE
)
1431 UnlockDevice(device
);
1432 ALCdevice_StopPlayback(device
);
1433 device
->Flags
&= ~DEVICE_RUNNING
;
1436 slot
->NeedsUpdate
= AL_FALSE
;
1437 ALEffect_Update(slot
->EffectState
, context
, slot
);
1440 for(pos
= 0;pos
< context
->SourceMap
.size
;pos
++)
1442 ALsource
*source
= context
->SourceMap
.array
[pos
].value
;
1443 ALuint s
= device
->NumAuxSends
;
1444 while(s
< MAX_SENDS
)
1446 if(source
->Send
[s
].Slot
)
1447 source
->Send
[s
].Slot
->refcount
--;
1448 source
->Send
[s
].Slot
= NULL
;
1449 source
->Send
[s
].WetFilter
.type
= 0;
1450 source
->Send
[s
].WetFilter
.filter
= 0;
1453 source
->NeedsUpdate
= AL_FALSE
;
1454 ALsource_Update(source
, context
);
1457 UnlockDevice(device
);
1463 ALCvoid
LockDevice(ALCdevice
*device
)
1465 EnterCriticalSection(&device
->Mutex
);
1468 ALCvoid
UnlockDevice(ALCdevice
*device
)
1470 LeaveCriticalSection(&device
->Mutex
);
1478 ALCvoid
LockContext(ALCcontext
*context
)
1480 EnterCriticalSection(&context
->Device
->Mutex
);
1489 ALCvoid
UnlockContext(ALCcontext
*context
)
1491 LeaveCriticalSection(&context
->Device
->Mutex
);
1498 Returns the currently active Context, in a locked state
1500 ALCcontext
*GetLockedContext(void)
1502 ALCcontext
*pContext
= NULL
;
1506 pContext
= tls_get(LocalContext
);
1507 if(pContext
&& !IsContext(pContext
))
1509 tls_set(LocalContext
, NULL
);
1513 pContext
= GlobalContext
;
1515 LockContext(pContext
);
1526 Initialize Context variables
1528 static ALvoid
InitContext(ALCcontext
*pContext
)
1530 //Initialise listener
1531 pContext
->Listener
.Gain
= 1.0f
;
1532 pContext
->Listener
.MetersPerUnit
= 1.0f
;
1533 pContext
->Listener
.Position
[0] = 0.0f
;
1534 pContext
->Listener
.Position
[1] = 0.0f
;
1535 pContext
->Listener
.Position
[2] = 0.0f
;
1536 pContext
->Listener
.Velocity
[0] = 0.0f
;
1537 pContext
->Listener
.Velocity
[1] = 0.0f
;
1538 pContext
->Listener
.Velocity
[2] = 0.0f
;
1539 pContext
->Listener
.Forward
[0] = 0.0f
;
1540 pContext
->Listener
.Forward
[1] = 0.0f
;
1541 pContext
->Listener
.Forward
[2] = -1.0f
;
1542 pContext
->Listener
.Up
[0] = 0.0f
;
1543 pContext
->Listener
.Up
[1] = 1.0f
;
1544 pContext
->Listener
.Up
[2] = 0.0f
;
1547 pContext
->LastError
= AL_NO_ERROR
;
1548 pContext
->UpdateSources
= AL_FALSE
;
1549 pContext
->ActiveSourceCount
= 0;
1550 InitUIntMap(&pContext
->SourceMap
);
1551 InitUIntMap(&pContext
->EffectSlotMap
);
1554 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
1555 pContext
->SourceDistanceModel
= AL_FALSE
;
1556 pContext
->DopplerFactor
= 1.0f
;
1557 pContext
->DopplerVelocity
= 1.0f
;
1558 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
1559 pContext
->DeferUpdates
= AL_FALSE
;
1561 pContext
->ExtensionList
= alExtList
;
1568 Clean up Context, destroy any remaining Sources
1570 static ALCvoid
ExitContext(ALCcontext
*pContext
)
1572 //Invalidate context
1573 pContext
->LastError
= AL_NO_ERROR
;
1576 ///////////////////////////////////////////////////////
1579 ///////////////////////////////////////////////////////
1580 // ALC Functions calls
1583 // This should probably move to another c file but for now ...
1584 ALC_API ALCdevice
* ALC_APIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
1586 ALCboolean DeviceFound
= ALC_FALSE
;
1587 ALCdevice
*device
= NULL
;
1594 alcSetError(NULL
, ALC_INVALID_VALUE
);
1598 if(deviceName
&& (!deviceName
[0] || strcasecmp(deviceName
, "openal soft") == 0 || strcasecmp(deviceName
, "openal-soft") == 0))
1601 device
= calloc(1, sizeof(ALCdevice
));
1604 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
1609 device
->Connected
= ALC_TRUE
;
1610 device
->IsCaptureDevice
= AL_TRUE
;
1611 device
->IsLoopbackDevice
= AL_FALSE
;
1612 InitializeCriticalSection(&device
->Mutex
);
1614 device
->szDeviceName
= NULL
;
1616 device
->Flags
|= DEVICE_FREQUENCY_REQUEST
;
1617 device
->Frequency
= frequency
;
1619 device
->Flags
|= DEVICE_CHANNELS_REQUEST
;
1620 if(DecomposeDevFormat(format
, &device
->FmtChans
, &device
->FmtType
) == AL_FALSE
)
1623 alcSetError(NULL
, ALC_INVALID_ENUM
);
1627 device
->UpdateSize
= SampleSize
;
1628 device
->NumUpdates
= 1;
1631 for(i
= 0;BackendList
[i
].Init
;i
++)
1633 device
->Funcs
= &BackendList
[i
].Funcs
;
1634 if(ALCdevice_OpenCapture(device
, deviceName
))
1636 device
->next
= g_pDeviceList
;
1637 g_pDeviceList
= device
;
1640 DeviceFound
= ALC_TRUE
;
1648 alcSetError(NULL
, ALC_INVALID_VALUE
);
1656 ALC_API ALCboolean ALC_APIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
1661 list
= &g_pDeviceList
;
1662 while(*list
&& *list
!= pDevice
)
1663 list
= &(*list
)->next
;
1665 if(!*list
|| !(*list
)->IsCaptureDevice
)
1667 alcSetError(*list
, ALC_INVALID_DEVICE
);
1672 *list
= (*list
)->next
;
1677 LockDevice(pDevice
);
1678 ALCdevice_CloseCapture(pDevice
);
1679 UnlockDevice(pDevice
);
1681 free(pDevice
->szDeviceName
);
1682 pDevice
->szDeviceName
= NULL
;
1684 DeleteCriticalSection(&pDevice
->Mutex
);
1691 ALC_API
void ALC_APIENTRY
alcCaptureStart(ALCdevice
*device
)
1694 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
1696 alcSetError(device
, ALC_INVALID_DEVICE
);
1702 if(device
->Connected
)
1703 ALCdevice_StartCapture(device
);
1704 UnlockDevice(device
);
1707 ALC_API
void ALC_APIENTRY
alcCaptureStop(ALCdevice
*device
)
1710 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
1712 alcSetError(device
, ALC_INVALID_DEVICE
);
1718 if(device
->Connected
)
1719 ALCdevice_StopCapture(device
);
1720 UnlockDevice(device
);
1723 ALC_API
void ALC_APIENTRY
alcCaptureSamples(ALCdevice
*device
, ALCvoid
*buffer
, ALCsizei samples
)
1726 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
1728 alcSetError(device
, ALC_INVALID_DEVICE
);
1734 if(device
->Connected
)
1735 ALCdevice_CaptureSamples(device
, buffer
, samples
);
1736 UnlockDevice(device
);
1742 Return last ALC generated error code
1744 ALC_API ALCenum ALC_APIENTRY
alcGetError(ALCdevice
*device
)
1749 if(IsDevice(device
))
1751 errorCode
= device
->LastError
;
1752 device
->LastError
= ALC_NO_ERROR
;
1756 errorCode
= g_eLastNullDeviceError
;
1757 g_eLastNullDeviceError
= ALC_NO_ERROR
;
1769 ALC_API ALCvoid ALC_APIENTRY
alcSuspendContext(ALCcontext
*Context
)
1780 ALC_API ALCvoid ALC_APIENTRY
alcProcessContext(ALCcontext
*Context
)
1789 Returns information about the Device, and error strings
1791 ALC_API
const ALCchar
* ALC_APIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
1793 const ALCchar
*value
= NULL
;
1801 case ALC_INVALID_ENUM
:
1802 value
= alcErrInvalidEnum
;
1805 case ALC_INVALID_VALUE
:
1806 value
= alcErrInvalidValue
;
1809 case ALC_INVALID_DEVICE
:
1810 value
= alcErrInvalidDevice
;
1813 case ALC_INVALID_CONTEXT
:
1814 value
= alcErrInvalidContext
;
1817 case ALC_OUT_OF_MEMORY
:
1818 value
= alcErrOutOfMemory
;
1821 case ALC_DEVICE_SPECIFIER
:
1823 if(IsDevice(pDevice
))
1824 value
= pDevice
->szDeviceName
;
1828 value
= alcDeviceList
;
1833 case ALC_ALL_DEVICES_SPECIFIER
:
1834 ProbeAllDeviceList();
1835 value
= alcAllDeviceList
;
1838 case ALC_CAPTURE_DEVICE_SPECIFIER
:
1840 if(IsDevice(pDevice
))
1841 value
= pDevice
->szDeviceName
;
1844 ProbeCaptureDeviceList();
1845 value
= alcCaptureDeviceList
;
1850 /* Default devices are always first in the list */
1851 case ALC_DEFAULT_DEVICE_SPECIFIER
:
1855 free(alcDefaultDeviceSpecifier
);
1856 alcDefaultDeviceSpecifier
= strdup(alcDeviceList
? alcDeviceList
: "");
1857 if(!alcDefaultDeviceSpecifier
)
1858 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1859 value
= alcDefaultDeviceSpecifier
;
1862 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
1863 if(!alcAllDeviceList
)
1864 ProbeAllDeviceList();
1866 free(alcDefaultAllDeviceSpecifier
);
1867 alcDefaultAllDeviceSpecifier
= strdup(alcAllDeviceList
?
1868 alcAllDeviceList
: "");
1869 if(!alcDefaultAllDeviceSpecifier
)
1870 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1871 value
= alcDefaultAllDeviceSpecifier
;
1874 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
1875 if(!alcCaptureDeviceList
)
1876 ProbeCaptureDeviceList();
1878 free(alcCaptureDefaultDeviceSpecifier
);
1879 alcCaptureDefaultDeviceSpecifier
= strdup(alcCaptureDeviceList
?
1880 alcCaptureDeviceList
: "");
1881 if(!alcCaptureDefaultDeviceSpecifier
)
1882 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1883 value
= alcCaptureDefaultDeviceSpecifier
;
1886 case ALC_EXTENSIONS
:
1888 if(IsDevice(pDevice
))
1889 value
= alcExtensionList
;
1891 value
= alcNoDeviceExtList
;
1896 alcSetError(pDevice
, ALC_INVALID_ENUM
);
1907 Returns information about the Device and the version of Open AL
1909 ALC_API ALCvoid ALC_APIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
1911 if(size
== 0 || data
== NULL
)
1913 alcSetError(device
, ALC_INVALID_VALUE
);
1918 if(!IsDevice(device
))
1922 case ALC_MAJOR_VERSION
:
1923 *data
= alcMajorVersion
;
1925 case ALC_MINOR_VERSION
:
1926 *data
= alcMinorVersion
;
1929 case ALC_ATTRIBUTES_SIZE
:
1930 case ALC_ALL_ATTRIBUTES
:
1934 case ALC_MONO_SOURCES
:
1935 case ALC_STEREO_SOURCES
:
1936 case ALC_CAPTURE_SAMPLES
:
1937 case ALC_FORMAT_CHANNELS_SOFT
:
1938 case ALC_FORMAT_TYPE_SOFT
:
1939 alcSetError(NULL
, ALC_INVALID_DEVICE
);
1943 alcSetError(NULL
, ALC_INVALID_ENUM
);
1947 else if(device
->IsCaptureDevice
)
1951 case ALC_CAPTURE_SAMPLES
:
1953 *data
= ALCdevice_AvailableSamples(device
);
1954 UnlockDevice(device
);
1958 *data
= device
->Connected
;
1962 alcSetError(device
, ALC_INVALID_ENUM
);
1966 else /* render device */
1970 case ALC_MAJOR_VERSION
:
1971 *data
= alcMajorVersion
;
1974 case ALC_MINOR_VERSION
:
1975 *data
= alcMinorVersion
;
1978 case ALC_EFX_MAJOR_VERSION
:
1979 *data
= alcEFXMajorVersion
;
1982 case ALC_EFX_MINOR_VERSION
:
1983 *data
= alcEFXMinorVersion
;
1986 case ALC_ATTRIBUTES_SIZE
:
1990 case ALC_ALL_ATTRIBUTES
:
1992 alcSetError(device
, ALC_INVALID_VALUE
);
1997 data
[i
++] = ALC_FREQUENCY
;
1998 data
[i
++] = device
->Frequency
;
2000 if(!device
->IsLoopbackDevice
)
2002 data
[i
++] = ALC_REFRESH
;
2003 data
[i
++] = device
->Frequency
/ device
->UpdateSize
;
2005 data
[i
++] = ALC_SYNC
;
2006 data
[i
++] = ALC_FALSE
;
2010 data
[i
++] = ALC_FORMAT_CHANNELS_SOFT
;
2011 data
[i
++] = device
->FmtChans
;
2013 data
[i
++] = ALC_FORMAT_TYPE_SOFT
;
2014 data
[i
++] = device
->FmtType
;
2017 data
[i
++] = ALC_MONO_SOURCES
;
2018 data
[i
++] = device
->NumMonoSources
;
2020 data
[i
++] = ALC_STEREO_SOURCES
;
2021 data
[i
++] = device
->NumStereoSources
;
2023 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
2024 data
[i
++] = device
->NumAuxSends
;
2031 *data
= device
->Frequency
;
2035 if(device
->IsLoopbackDevice
)
2036 alcSetError(device
, ALC_INVALID_DEVICE
);
2038 *data
= device
->Frequency
/ device
->UpdateSize
;
2042 if(device
->IsLoopbackDevice
)
2043 alcSetError(device
, ALC_INVALID_DEVICE
);
2048 case ALC_FORMAT_CHANNELS_SOFT
:
2049 if(!device
->IsLoopbackDevice
)
2050 alcSetError(device
, ALC_INVALID_DEVICE
);
2052 *data
= device
->FmtChans
;
2055 case ALC_FORMAT_TYPE_SOFT
:
2056 if(!device
->IsLoopbackDevice
)
2057 alcSetError(device
, ALC_INVALID_DEVICE
);
2059 *data
= device
->FmtType
;
2062 case ALC_MONO_SOURCES
:
2063 *data
= device
->NumMonoSources
;
2066 case ALC_STEREO_SOURCES
:
2067 *data
= device
->NumStereoSources
;
2070 case ALC_MAX_AUXILIARY_SENDS
:
2071 *data
= device
->NumAuxSends
;
2075 *data
= device
->Connected
;
2079 alcSetError(device
, ALC_INVALID_ENUM
);
2088 alcIsExtensionPresent
2090 Determines if there is support for a particular extension
2092 ALC_API ALCboolean ALC_APIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
2094 ALCboolean bResult
= ALC_FALSE
;
2100 alcSetError(device
, ALC_INVALID_VALUE
);
2104 len
= strlen(extName
);
2106 ptr
= (IsDevice(device
) ? alcExtensionList
: alcNoDeviceExtList
);
2110 if(strncasecmp(ptr
, extName
, len
) == 0 &&
2111 (ptr
[len
] == '\0' || isspace(ptr
[len
])))
2116 if((ptr
=strchr(ptr
, ' ')) != NULL
)
2120 } while(isspace(*ptr
));
2131 Retrieves the function address for a particular extension function
2133 ALC_API ALCvoid
* ALC_APIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
2139 alcSetError(device
, ALC_INVALID_VALUE
);
2143 while(alcFunctions
[i
].funcName
&& strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
2145 return alcFunctions
[i
].address
;
2152 Get the value for a particular ALC Enumerated Value
2154 ALC_API ALCenum ALC_APIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
2160 alcSetError(device
, ALC_INVALID_VALUE
);
2164 while(enumeration
[i
].enumName
&& strcmp(enumeration
[i
].enumName
,enumName
) != 0)
2166 return enumeration
[i
].value
;
2173 Create and attach a Context to a particular Device.
2175 ALC_API ALCcontext
* ALC_APIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
2177 ALCcontext
*ALContext
;
2181 if(!IsDevice(device
) || device
->IsCaptureDevice
|| !device
->Connected
)
2183 alcSetError(device
, ALC_INVALID_DEVICE
);
2188 // Reset Context Last Error code
2189 device
->LastError
= ALC_NO_ERROR
;
2191 if(UpdateDeviceParams(device
, attrList
) == ALC_FALSE
)
2193 alcSetError(device
, ALC_INVALID_DEVICE
);
2194 aluHandleDisconnect(device
);
2201 temp
= realloc(device
->Contexts
, (device
->NumContexts
+1) * sizeof(*device
->Contexts
));
2204 device
->Contexts
= temp
;
2206 ALContext
= calloc(1, sizeof(ALCcontext
));
2209 ALContext
->MaxActiveSources
= 256;
2210 ALContext
->ActiveSources
= malloc(sizeof(ALContext
->ActiveSources
[0]) *
2211 ALContext
->MaxActiveSources
);
2214 if(!ALContext
|| !ALContext
->ActiveSources
)
2217 alcSetError(device
, ALC_OUT_OF_MEMORY
);
2218 UnlockDevice(device
);
2219 if(device
->NumContexts
== 0)
2221 ALCdevice_StopPlayback(device
);
2222 device
->Flags
&= ~DEVICE_RUNNING
;
2228 device
->Contexts
[device
->NumContexts
++] = ALContext
;
2229 ALContext
->Device
= device
;
2231 InitContext(ALContext
);
2232 UnlockDevice(device
);
2234 ALContext
->next
= g_pContextList
;
2235 g_pContextList
= ALContext
;
2249 ALC_API ALCvoid ALC_APIENTRY
alcDestroyContext(ALCcontext
*context
)
2256 list
= &g_pContextList
;
2257 while(*list
&& *list
!= context
)
2258 list
= &(*list
)->next
;
2262 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2267 *list
= (*list
)->next
;
2270 if(context
== tls_get(LocalContext
))
2271 tls_set(LocalContext
, NULL
);
2272 if(context
== GlobalContext
)
2273 GlobalContext
= NULL
;
2275 Device
= context
->Device
;
2277 for(i
= 0;i
< Device
->NumContexts
;i
++)
2279 if(Device
->Contexts
[i
] == context
)
2281 Device
->Contexts
[i
] = Device
->Contexts
[Device
->NumContexts
-1];
2282 Device
->NumContexts
--;
2286 UnlockDevice(Device
);
2288 if(Device
->NumContexts
== 0)
2290 ALCdevice_StopPlayback(Device
);
2291 Device
->Flags
&= ~DEVICE_RUNNING
;
2295 if(context
->SourceMap
.size
> 0)
2297 ERR("alcDestroyContext(): deleting %d Source(s)\n", context
->SourceMap
.size
);
2298 ReleaseALSources(context
);
2300 ResetUIntMap(&context
->SourceMap
);
2302 if(context
->EffectSlotMap
.size
> 0)
2304 ERR("alcDestroyContext(): deleting %d AuxiliaryEffectSlot(s)\n", context
->EffectSlotMap
.size
);
2305 ReleaseALAuxiliaryEffectSlots(context
);
2307 ResetUIntMap(&context
->EffectSlotMap
);
2309 free(context
->ActiveSources
);
2310 context
->ActiveSources
= NULL
;
2311 context
->MaxActiveSources
= 0;
2312 context
->ActiveSourceCount
= 0;
2314 ExitContext(context
);
2316 memset(context
, 0, sizeof(ALCcontext
));
2322 alcGetCurrentContext
2324 Returns the currently active Context
2326 ALC_API ALCcontext
* ALC_APIENTRY
alcGetCurrentContext(ALCvoid
)
2328 ALCcontext
*Context
;
2331 Context
= tls_get(LocalContext
);
2332 if(Context
&& !IsContext(Context
))
2334 tls_set(LocalContext
, NULL
);
2338 Context
= GlobalContext
;
2347 Returns the currently active thread-local Context
2349 ALC_API ALCcontext
* ALC_APIENTRY
alcGetThreadContext(void)
2351 ALCcontext
*pContext
= NULL
;
2355 pContext
= tls_get(LocalContext
);
2356 if(pContext
&& !IsContext(pContext
))
2358 tls_set(LocalContext
, NULL
);
2369 alcGetContextsDevice
2371 Returns the Device that a particular Context is attached to
2373 ALC_API ALCdevice
* ALC_APIENTRY
alcGetContextsDevice(ALCcontext
*pContext
)
2375 ALCdevice
*pDevice
= NULL
;
2378 if(IsContext(pContext
))
2379 pDevice
= pContext
->Device
;
2381 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2389 alcMakeContextCurrent
2391 Makes the given Context the active Context
2393 ALC_API ALCboolean ALC_APIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
2395 ALboolean bReturn
= AL_TRUE
;
2399 // context must be a valid Context or NULL
2400 if(context
== NULL
|| IsContext(context
))
2402 GlobalContext
= context
;
2403 tls_set(LocalContext
, NULL
);
2407 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2419 Makes the given Context the active Context for the current thread
2421 ALC_API ALCboolean ALC_APIENTRY
alcSetThreadContext(ALCcontext
*context
)
2423 ALboolean bReturn
= AL_TRUE
;
2427 // context must be a valid Context or NULL
2428 if(context
== NULL
|| IsContext(context
))
2429 tls_set(LocalContext
, context
);
2432 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
2442 // Sets the default channel order used by most non-WaveFormatEx-based APIs
2443 void SetDefaultChannelOrder(ALCdevice
*device
)
2445 switch(device
->FmtChans
)
2447 case DevFmtX51
: device
->DevChannels
[0] = FRONT_LEFT
;
2448 device
->DevChannels
[1] = FRONT_RIGHT
;
2449 device
->DevChannels
[2] = BACK_LEFT
;
2450 device
->DevChannels
[3] = BACK_RIGHT
;
2451 device
->DevChannels
[4] = FRONT_CENTER
;
2452 device
->DevChannels
[5] = LFE
;
2455 case DevFmtX71
: device
->DevChannels
[0] = FRONT_LEFT
;
2456 device
->DevChannels
[1] = FRONT_RIGHT
;
2457 device
->DevChannels
[2] = BACK_LEFT
;
2458 device
->DevChannels
[3] = BACK_RIGHT
;
2459 device
->DevChannels
[4] = FRONT_CENTER
;
2460 device
->DevChannels
[5] = LFE
;
2461 device
->DevChannels
[6] = SIDE_LEFT
;
2462 device
->DevChannels
[7] = SIDE_RIGHT
;
2465 /* Same as WFX order */
2473 SetDefaultWFXChannelOrder(device
);
2475 // Sets the default order used by WaveFormatEx
2476 void SetDefaultWFXChannelOrder(ALCdevice
*device
)
2478 switch(device
->FmtChans
)
2480 case DevFmtMono
: device
->DevChannels
[0] = FRONT_CENTER
; break;
2482 case DevFmtStereo
: device
->DevChannels
[0] = FRONT_LEFT
;
2483 device
->DevChannels
[1] = FRONT_RIGHT
; break;
2485 case DevFmtQuad
: device
->DevChannels
[0] = FRONT_LEFT
;
2486 device
->DevChannels
[1] = FRONT_RIGHT
;
2487 device
->DevChannels
[2] = BACK_LEFT
;
2488 device
->DevChannels
[3] = BACK_RIGHT
; break;
2490 case DevFmtX51
: device
->DevChannels
[0] = FRONT_LEFT
;
2491 device
->DevChannels
[1] = FRONT_RIGHT
;
2492 device
->DevChannels
[2] = FRONT_CENTER
;
2493 device
->DevChannels
[3] = LFE
;
2494 device
->DevChannels
[4] = BACK_LEFT
;
2495 device
->DevChannels
[5] = BACK_RIGHT
; break;
2497 case DevFmtX51Side
: device
->DevChannels
[0] = FRONT_LEFT
;
2498 device
->DevChannels
[1] = FRONT_RIGHT
;
2499 device
->DevChannels
[2] = FRONT_CENTER
;
2500 device
->DevChannels
[3] = LFE
;
2501 device
->DevChannels
[4] = SIDE_LEFT
;
2502 device
->DevChannels
[5] = SIDE_RIGHT
; break;
2504 case DevFmtX61
: device
->DevChannels
[0] = FRONT_LEFT
;
2505 device
->DevChannels
[1] = FRONT_RIGHT
;
2506 device
->DevChannels
[2] = FRONT_CENTER
;
2507 device
->DevChannels
[3] = LFE
;
2508 device
->DevChannels
[4] = BACK_CENTER
;
2509 device
->DevChannels
[5] = SIDE_LEFT
;
2510 device
->DevChannels
[6] = SIDE_RIGHT
; break;
2512 case DevFmtX71
: device
->DevChannels
[0] = FRONT_LEFT
;
2513 device
->DevChannels
[1] = FRONT_RIGHT
;
2514 device
->DevChannels
[2] = FRONT_CENTER
;
2515 device
->DevChannels
[3] = LFE
;
2516 device
->DevChannels
[4] = BACK_LEFT
;
2517 device
->DevChannels
[5] = BACK_RIGHT
;
2518 device
->DevChannels
[6] = SIDE_LEFT
;
2519 device
->DevChannels
[7] = SIDE_RIGHT
; break;
2523 static void GetFormatFromString(const char *str
, enum DevFmtChannels
*chans
, enum DevFmtType
*type
)
2525 if(strcasecmp(str
, "AL_FORMAT_MONO32") == 0)
2527 *chans
= DevFmtMono
;
2528 *type
= DevFmtFloat
;
2531 if(strcasecmp(str
, "AL_FORMAT_STEREO32") == 0)
2533 *chans
= DevFmtStereo
;
2534 *type
= DevFmtFloat
;
2537 if(strcasecmp(str
, "AL_FORMAT_QUAD32") == 0)
2539 *chans
= DevFmtQuad
;
2540 *type
= DevFmtFloat
;
2543 if(strcasecmp(str
, "AL_FORMAT_51CHN32") == 0)
2546 *type
= DevFmtFloat
;
2549 if(strcasecmp(str
, "AL_FORMAT_61CHN32") == 0)
2552 *type
= DevFmtFloat
;
2555 if(strcasecmp(str
, "AL_FORMAT_71CHN32") == 0)
2558 *type
= DevFmtFloat
;
2562 if(strcasecmp(str
, "AL_FORMAT_MONO16") == 0)
2564 *chans
= DevFmtMono
;
2565 *type
= DevFmtShort
;
2568 if(strcasecmp(str
, "AL_FORMAT_STEREO16") == 0)
2570 *chans
= DevFmtStereo
;
2571 *type
= DevFmtShort
;
2574 if(strcasecmp(str
, "AL_FORMAT_QUAD16") == 0)
2576 *chans
= DevFmtQuad
;
2577 *type
= DevFmtShort
;
2580 if(strcasecmp(str
, "AL_FORMAT_51CHN16") == 0)
2583 *type
= DevFmtShort
;
2586 if(strcasecmp(str
, "AL_FORMAT_61CHN16") == 0)
2589 *type
= DevFmtShort
;
2592 if(strcasecmp(str
, "AL_FORMAT_71CHN16") == 0)
2595 *type
= DevFmtShort
;
2599 if(strcasecmp(str
, "AL_FORMAT_MONO8") == 0)
2601 *chans
= DevFmtMono
;
2605 if(strcasecmp(str
, "AL_FORMAT_STEREO8") == 0)
2607 *chans
= DevFmtStereo
;
2611 if(strcasecmp(str
, "AL_FORMAT_QUAD8") == 0)
2613 *chans
= DevFmtQuad
;
2617 if(strcasecmp(str
, "AL_FORMAT_51CHN8") == 0)
2623 if(strcasecmp(str
, "AL_FORMAT_61CHN8") == 0)
2629 if(strcasecmp(str
, "AL_FORMAT_71CHN8") == 0)
2636 ERR("Unknown format: \"%s\"\n", str
);
2637 *chans
= DevFmtStereo
;
2638 *type
= DevFmtShort
;
2644 Open the Device specified.
2646 ALC_API ALCdevice
* ALC_APIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
2648 ALboolean bDeviceFound
= AL_FALSE
;
2655 if(deviceName
&& (!deviceName
[0] || strcasecmp(deviceName
, "openal soft") == 0 || strcasecmp(deviceName
, "openal-soft") == 0))
2658 device
= calloc(1, sizeof(ALCdevice
));
2661 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
2666 device
->Connected
= ALC_TRUE
;
2667 device
->IsCaptureDevice
= AL_FALSE
;
2668 device
->IsLoopbackDevice
= AL_FALSE
;
2669 InitializeCriticalSection(&device
->Mutex
);
2670 device
->LastError
= ALC_NO_ERROR
;
2673 device
->Bs2b
= NULL
;
2674 device
->szDeviceName
= NULL
;
2676 device
->Contexts
= NULL
;
2677 device
->NumContexts
= 0;
2679 InitUIntMap(&device
->BufferMap
);
2680 InitUIntMap(&device
->EffectMap
);
2681 InitUIntMap(&device
->FilterMap
);
2684 if(ConfigValueExists(NULL
, "frequency"))
2685 device
->Flags
|= DEVICE_FREQUENCY_REQUEST
;
2686 device
->Frequency
= GetConfigValueInt(NULL
, "frequency", DEFAULT_OUTPUT_RATE
);
2687 if(device
->Frequency
< 8000)
2688 device
->Frequency
= 8000;
2690 if(ConfigValueExists(NULL
, "format"))
2691 device
->Flags
|= DEVICE_CHANNELS_REQUEST
;
2692 fmt
= GetConfigValue(NULL
, "format", "AL_FORMAT_STEREO16");
2693 GetFormatFromString(fmt
, &device
->FmtChans
, &device
->FmtType
);
2695 device
->NumUpdates
= GetConfigValueInt(NULL
, "periods", 4);
2696 if(device
->NumUpdates
< 2)
2697 device
->NumUpdates
= 4;
2699 device
->UpdateSize
= GetConfigValueInt(NULL
, "period_size", 1024);
2700 if(device
->UpdateSize
<= 0)
2701 device
->UpdateSize
= 1024;
2703 device
->MaxNoOfSources
= GetConfigValueInt(NULL
, "sources", 256);
2704 if(device
->MaxNoOfSources
<= 0)
2705 device
->MaxNoOfSources
= 256;
2707 device
->AuxiliaryEffectSlotMax
= GetConfigValueInt(NULL
, "slots", 4);
2708 if(device
->AuxiliaryEffectSlotMax
<= 0)
2709 device
->AuxiliaryEffectSlotMax
= 4;
2711 device
->NumStereoSources
= 1;
2712 device
->NumMonoSources
= device
->MaxNoOfSources
- device
->NumStereoSources
;
2714 device
->NumAuxSends
= GetConfigValueInt(NULL
, "sends", MAX_SENDS
);
2715 if(device
->NumAuxSends
> MAX_SENDS
)
2716 device
->NumAuxSends
= MAX_SENDS
;
2718 device
->Bs2bLevel
= GetConfigValueInt(NULL
, "cf_level", 0);
2720 // Find a playback device to open
2722 for(i
= 0;BackendList
[i
].Init
;i
++)
2724 device
->Funcs
= &BackendList
[i
].Funcs
;
2725 if(ALCdevice_OpenPlayback(device
, deviceName
))
2727 device
->next
= g_pDeviceList
;
2728 g_pDeviceList
= device
;
2731 bDeviceFound
= AL_TRUE
;
2739 // No suitable output device found
2740 alcSetError(NULL
, ALC_INVALID_VALUE
);
2752 Close the specified Device
2754 ALC_API ALCboolean ALC_APIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
2759 list
= &g_pDeviceList
;
2760 while(*list
&& *list
!= pDevice
)
2761 list
= &(*list
)->next
;
2763 if(!*list
|| (*list
)->IsCaptureDevice
)
2765 alcSetError(*list
, ALC_INVALID_DEVICE
);
2770 *list
= (*list
)->next
;
2775 if(pDevice
->NumContexts
> 0)
2777 WARN("alcCloseDevice(): destroying %u Context(s)\n", pDevice
->NumContexts
);
2778 while(pDevice
->NumContexts
> 0)
2779 alcDestroyContext(pDevice
->Contexts
[0]);
2781 ALCdevice_ClosePlayback(pDevice
);
2783 if(pDevice
->BufferMap
.size
> 0)
2785 WARN("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice
->BufferMap
.size
);
2786 ReleaseALBuffers(pDevice
);
2788 ResetUIntMap(&pDevice
->BufferMap
);
2790 if(pDevice
->EffectMap
.size
> 0)
2792 WARN("alcCloseDevice(): deleting %d Effect(s)\n", pDevice
->EffectMap
.size
);
2793 ReleaseALEffects(pDevice
);
2795 ResetUIntMap(&pDevice
->EffectMap
);
2797 if(pDevice
->FilterMap
.size
> 0)
2799 WARN("alcCloseDevice(): deleting %d Filter(s)\n", pDevice
->FilterMap
.size
);
2800 ReleaseALFilters(pDevice
);
2802 ResetUIntMap(&pDevice
->FilterMap
);
2804 free(pDevice
->Bs2b
);
2805 pDevice
->Bs2b
= NULL
;
2807 free(pDevice
->szDeviceName
);
2808 pDevice
->szDeviceName
= NULL
;
2810 free(pDevice
->Contexts
);
2811 pDevice
->Contexts
= NULL
;
2813 DeleteCriticalSection(&pDevice
->Mutex
);
2815 //Release device structure
2816 memset(pDevice
, 0, sizeof(ALCdevice
));
2823 ALC_API ALCdevice
* ALC_APIENTRY
alcLoopbackOpenDeviceSOFT(void)
2829 device
= calloc(1, sizeof(ALCdevice
));
2832 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
2837 device
->Connected
= ALC_TRUE
;
2838 device
->IsCaptureDevice
= AL_FALSE
;
2839 device
->IsLoopbackDevice
= AL_TRUE
;
2840 InitializeCriticalSection(&device
->Mutex
);
2841 device
->LastError
= ALC_NO_ERROR
;
2844 device
->Bs2b
= NULL
;
2845 device
->szDeviceName
= NULL
;
2847 device
->Contexts
= NULL
;
2848 device
->NumContexts
= 0;
2850 InitUIntMap(&device
->BufferMap
);
2851 InitUIntMap(&device
->EffectMap
);
2852 InitUIntMap(&device
->FilterMap
);
2855 device
->Frequency
= 44100;
2856 device
->FmtChans
= DevFmtStereo
;
2857 device
->FmtType
= DevFmtShort
;
2859 device
->NumUpdates
= 0;
2860 device
->UpdateSize
= 0;
2862 device
->MaxNoOfSources
= GetConfigValueInt(NULL
, "sources", 256);
2863 if(device
->MaxNoOfSources
<= 0)
2864 device
->MaxNoOfSources
= 256;
2866 device
->AuxiliaryEffectSlotMax
= GetConfigValueInt(NULL
, "slots", 4);
2867 if(device
->AuxiliaryEffectSlotMax
<= 0)
2868 device
->AuxiliaryEffectSlotMax
= 4;
2870 device
->NumStereoSources
= 1;
2871 device
->NumMonoSources
= device
->MaxNoOfSources
- device
->NumStereoSources
;
2873 device
->NumAuxSends
= GetConfigValueInt(NULL
, "sends", MAX_SENDS
);
2874 if(device
->NumAuxSends
> MAX_SENDS
)
2875 device
->NumAuxSends
= MAX_SENDS
;
2877 device
->Bs2bLevel
= GetConfigValueInt(NULL
, "cf_level", 0);
2879 // Open the "backend"
2881 device
->Funcs
= &BackendLoopback
.Funcs
;
2882 ALCdevice_OpenPlayback(device
, "Loopback");
2884 device
->next
= g_pDeviceList
;
2885 g_pDeviceList
= device
;
2892 ALC_API ALCboolean ALC_APIENTRY
alcIsRenderFormatSupportedSOFT(ALCdevice
*device
, ALCsizei freq
, ALCenum channels
, ALCenum type
)
2894 ALCboolean ret
= ALC_FALSE
;
2897 if(!IsDevice(device
) || !device
->IsLoopbackDevice
)
2898 alcSetError(device
, ALC_INVALID_DEVICE
);
2900 alcSetError(device
, ALC_INVALID_VALUE
);
2901 else if(!IsValidALCType(type
) || !IsValidALCChannels(channels
))
2902 alcSetError(device
, ALC_INVALID_ENUM
);
2905 if(BytesFromDevFmt(type
) > 0 && ChannelsFromDevFmt(channels
) > 0 &&
2914 ALC_API
void ALC_APIENTRY
alcRenderSamplesSOFT(ALCdevice
*device
, ALCvoid
*buffer
, ALCsizei samples
)
2917 if(!IsDevice(device
) || !device
->IsLoopbackDevice
)
2918 alcSetError(device
, ALC_INVALID_DEVICE
);
2919 else if(samples
< 0)
2920 alcSetError(device
, ALC_INVALID_VALUE
);
2922 aluMixData(device
, buffer
, samples
);
2927 static void ReleaseALC(void)
2929 free(alcDeviceList
); alcDeviceList
= NULL
;
2930 alcDeviceListSize
= 0;
2931 free(alcAllDeviceList
); alcAllDeviceList
= NULL
;
2932 alcAllDeviceListSize
= 0;
2933 free(alcCaptureDeviceList
); alcCaptureDeviceList
= NULL
;
2934 alcCaptureDeviceListSize
= 0;
2936 free(alcDefaultDeviceSpecifier
);
2937 alcDefaultDeviceSpecifier
= NULL
;
2938 free(alcDefaultAllDeviceSpecifier
);
2939 alcDefaultAllDeviceSpecifier
= NULL
;
2940 free(alcCaptureDefaultDeviceSpecifier
);
2941 alcCaptureDefaultDeviceSpecifier
= NULL
;
2943 if(g_ulDeviceCount
> 0)
2944 WARN("ReleaseALC(): closing %u Device%s\n", g_ulDeviceCount
, (g_ulDeviceCount
>1)?"s":"");
2946 while(g_pDeviceList
)
2948 if(g_pDeviceList
->IsCaptureDevice
)
2949 alcCaptureCloseDevice(g_pDeviceList
);
2951 alcCloseDevice(g_pDeviceList
);
2955 ///////////////////////////////////////////////////////