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"
37 #include "alDatabuffer.h"
42 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
43 typedef struct BackendInfo
{
45 void (*Init
)(BackendFuncs
*);
50 static BackendInfo BackendList
[] = {
51 #ifdef HAVE_PULSEAUDIO
52 { "pulse", alc_pulse_init
, alc_pulse_deinit
, alc_pulse_probe
, EmptyFuncs
},
55 { "alsa", alc_alsa_init
, alc_alsa_deinit
, alc_alsa_probe
, EmptyFuncs
},
58 { "oss", alc_oss_init
, alc_oss_deinit
, alc_oss_probe
, EmptyFuncs
},
61 { "solaris", alc_solaris_init
, alc_solaris_deinit
, alc_solaris_probe
, EmptyFuncs
},
64 { "dsound", alcDSoundInit
, alcDSoundDeinit
, alcDSoundProbe
, EmptyFuncs
},
67 { "winmm", alcWinMMInit
, alcWinMMDeinit
, alcWinMMProbe
, EmptyFuncs
},
70 { "port", alc_pa_init
, alc_pa_deinit
, alc_pa_probe
, EmptyFuncs
},
73 { "null", alc_null_init
, alc_null_deinit
, alc_null_probe
, EmptyFuncs
},
75 { "wave", alc_wave_init
, alc_wave_deinit
, alc_wave_probe
, EmptyFuncs
},
78 { NULL
, NULL
, NULL
, NULL
, EmptyFuncs
}
82 ///////////////////////////////////////////////////////
84 #define ALC_EFX_MAJOR_VERSION 0x20001
85 #define ALC_EFX_MINOR_VERSION 0x20002
86 #define ALC_MAX_AUXILIARY_SENDS 0x20003
88 ///////////////////////////////////////////////////////
89 // STRING and EXTENSIONS
91 typedef struct ALCfunction
{
92 const ALCchar
*funcName
;
96 typedef struct ALCenums
{
97 const ALCchar
*enumName
;
102 static const ALCfunction alcFunctions
[] = {
103 { "alcCreateContext", (ALCvoid
*) alcCreateContext
},
104 { "alcMakeContextCurrent", (ALCvoid
*) alcMakeContextCurrent
},
105 { "alcProcessContext", (ALCvoid
*) alcProcessContext
},
106 { "alcSuspendContext", (ALCvoid
*) alcSuspendContext
},
107 { "alcDestroyContext", (ALCvoid
*) alcDestroyContext
},
108 { "alcGetCurrentContext", (ALCvoid
*) alcGetCurrentContext
},
109 { "alcGetContextsDevice", (ALCvoid
*) alcGetContextsDevice
},
110 { "alcOpenDevice", (ALCvoid
*) alcOpenDevice
},
111 { "alcCloseDevice", (ALCvoid
*) alcCloseDevice
},
112 { "alcGetError", (ALCvoid
*) alcGetError
},
113 { "alcIsExtensionPresent", (ALCvoid
*) alcIsExtensionPresent
},
114 { "alcGetProcAddress", (ALCvoid
*) alcGetProcAddress
},
115 { "alcGetEnumValue", (ALCvoid
*) alcGetEnumValue
},
116 { "alcGetString", (ALCvoid
*) alcGetString
},
117 { "alcGetIntegerv", (ALCvoid
*) alcGetIntegerv
},
118 { "alcCaptureOpenDevice", (ALCvoid
*) alcCaptureOpenDevice
},
119 { "alcCaptureCloseDevice", (ALCvoid
*) alcCaptureCloseDevice
},
120 { "alcCaptureStart", (ALCvoid
*) alcCaptureStart
},
121 { "alcCaptureStop", (ALCvoid
*) alcCaptureStop
},
122 { "alcCaptureSamples", (ALCvoid
*) alcCaptureSamples
},
124 { "alcSetThreadContext", (ALCvoid
*) alcSetThreadContext
},
125 { "alcGetThreadContext", (ALCvoid
*) alcGetThreadContext
},
127 { "alEnable", (ALCvoid
*) alEnable
},
128 { "alDisable", (ALCvoid
*) alDisable
},
129 { "alIsEnabled", (ALCvoid
*) alIsEnabled
},
130 { "alGetString", (ALCvoid
*) alGetString
},
131 { "alGetBooleanv", (ALCvoid
*) alGetBooleanv
},
132 { "alGetIntegerv", (ALCvoid
*) alGetIntegerv
},
133 { "alGetFloatv", (ALCvoid
*) alGetFloatv
},
134 { "alGetDoublev", (ALCvoid
*) alGetDoublev
},
135 { "alGetBoolean", (ALCvoid
*) alGetBoolean
},
136 { "alGetInteger", (ALCvoid
*) alGetInteger
},
137 { "alGetFloat", (ALCvoid
*) alGetFloat
},
138 { "alGetDouble", (ALCvoid
*) alGetDouble
},
139 { "alGetError", (ALCvoid
*) alGetError
},
140 { "alIsExtensionPresent", (ALCvoid
*) alIsExtensionPresent
},
141 { "alGetProcAddress", (ALCvoid
*) alGetProcAddress
},
142 { "alGetEnumValue", (ALCvoid
*) alGetEnumValue
},
143 { "alListenerf", (ALCvoid
*) alListenerf
},
144 { "alListener3f", (ALCvoid
*) alListener3f
},
145 { "alListenerfv", (ALCvoid
*) alListenerfv
},
146 { "alListeneri", (ALCvoid
*) alListeneri
},
147 { "alListener3i", (ALCvoid
*) alListener3i
},
148 { "alListeneriv", (ALCvoid
*) alListeneriv
},
149 { "alGetListenerf", (ALCvoid
*) alGetListenerf
},
150 { "alGetListener3f", (ALCvoid
*) alGetListener3f
},
151 { "alGetListenerfv", (ALCvoid
*) alGetListenerfv
},
152 { "alGetListeneri", (ALCvoid
*) alGetListeneri
},
153 { "alGetListener3i", (ALCvoid
*) alGetListener3i
},
154 { "alGetListeneriv", (ALCvoid
*) alGetListeneriv
},
155 { "alGenSources", (ALCvoid
*) alGenSources
},
156 { "alDeleteSources", (ALCvoid
*) alDeleteSources
},
157 { "alIsSource", (ALCvoid
*) alIsSource
},
158 { "alSourcef", (ALCvoid
*) alSourcef
},
159 { "alSource3f", (ALCvoid
*) alSource3f
},
160 { "alSourcefv", (ALCvoid
*) alSourcefv
},
161 { "alSourcei", (ALCvoid
*) alSourcei
},
162 { "alSource3i", (ALCvoid
*) alSource3i
},
163 { "alSourceiv", (ALCvoid
*) alSourceiv
},
164 { "alGetSourcef", (ALCvoid
*) alGetSourcef
},
165 { "alGetSource3f", (ALCvoid
*) alGetSource3f
},
166 { "alGetSourcefv", (ALCvoid
*) alGetSourcefv
},
167 { "alGetSourcei", (ALCvoid
*) alGetSourcei
},
168 { "alGetSource3i", (ALCvoid
*) alGetSource3i
},
169 { "alGetSourceiv", (ALCvoid
*) alGetSourceiv
},
170 { "alSourcePlayv", (ALCvoid
*) alSourcePlayv
},
171 { "alSourceStopv", (ALCvoid
*) alSourceStopv
},
172 { "alSourceRewindv", (ALCvoid
*) alSourceRewindv
},
173 { "alSourcePausev", (ALCvoid
*) alSourcePausev
},
174 { "alSourcePlay", (ALCvoid
*) alSourcePlay
},
175 { "alSourceStop", (ALCvoid
*) alSourceStop
},
176 { "alSourceRewind", (ALCvoid
*) alSourceRewind
},
177 { "alSourcePause", (ALCvoid
*) alSourcePause
},
178 { "alSourceQueueBuffers", (ALCvoid
*) alSourceQueueBuffers
},
179 { "alSourceUnqueueBuffers", (ALCvoid
*) alSourceUnqueueBuffers
},
180 { "alGenBuffers", (ALCvoid
*) alGenBuffers
},
181 { "alDeleteBuffers", (ALCvoid
*) alDeleteBuffers
},
182 { "alIsBuffer", (ALCvoid
*) alIsBuffer
},
183 { "alBufferData", (ALCvoid
*) alBufferData
},
184 { "alBufferf", (ALCvoid
*) alBufferf
},
185 { "alBuffer3f", (ALCvoid
*) alBuffer3f
},
186 { "alBufferfv", (ALCvoid
*) alBufferfv
},
187 { "alBufferi", (ALCvoid
*) alBufferi
},
188 { "alBuffer3i", (ALCvoid
*) alBuffer3i
},
189 { "alBufferiv", (ALCvoid
*) alBufferiv
},
190 { "alGetBufferf", (ALCvoid
*) alGetBufferf
},
191 { "alGetBuffer3f", (ALCvoid
*) alGetBuffer3f
},
192 { "alGetBufferfv", (ALCvoid
*) alGetBufferfv
},
193 { "alGetBufferi", (ALCvoid
*) alGetBufferi
},
194 { "alGetBuffer3i", (ALCvoid
*) alGetBuffer3i
},
195 { "alGetBufferiv", (ALCvoid
*) alGetBufferiv
},
196 { "alDopplerFactor", (ALCvoid
*) alDopplerFactor
},
197 { "alDopplerVelocity", (ALCvoid
*) alDopplerVelocity
},
198 { "alSpeedOfSound", (ALCvoid
*) alSpeedOfSound
},
199 { "alDistanceModel", (ALCvoid
*) alDistanceModel
},
201 { "alGenFilters", (ALCvoid
*) alGenFilters
},
202 { "alDeleteFilters", (ALCvoid
*) alDeleteFilters
},
203 { "alIsFilter", (ALCvoid
*) alIsFilter
},
204 { "alFilteri", (ALCvoid
*) alFilteri
},
205 { "alFilteriv", (ALCvoid
*) alFilteriv
},
206 { "alFilterf", (ALCvoid
*) alFilterf
},
207 { "alFilterfv", (ALCvoid
*) alFilterfv
},
208 { "alGetFilteri", (ALCvoid
*) alGetFilteri
},
209 { "alGetFilteriv", (ALCvoid
*) alGetFilteriv
},
210 { "alGetFilterf", (ALCvoid
*) alGetFilterf
},
211 { "alGetFilterfv", (ALCvoid
*) alGetFilterfv
},
213 { "alGenEffects", (ALCvoid
*) alGenEffects
},
214 { "alDeleteEffects", (ALCvoid
*) alDeleteEffects
},
215 { "alIsEffect", (ALCvoid
*) alIsEffect
},
216 { "alEffecti", (ALCvoid
*) alEffecti
},
217 { "alEffectiv", (ALCvoid
*) alEffectiv
},
218 { "alEffectf", (ALCvoid
*) alEffectf
},
219 { "alEffectfv", (ALCvoid
*) alEffectfv
},
220 { "alGetEffecti", (ALCvoid
*) alGetEffecti
},
221 { "alGetEffectiv", (ALCvoid
*) alGetEffectiv
},
222 { "alGetEffectf", (ALCvoid
*) alGetEffectf
},
223 { "alGetEffectfv", (ALCvoid
*) alGetEffectfv
},
225 { "alGenAuxiliaryEffectSlots", (ALCvoid
*) alGenAuxiliaryEffectSlots
},
226 { "alDeleteAuxiliaryEffectSlots",(ALCvoid
*) alDeleteAuxiliaryEffectSlots
},
227 { "alIsAuxiliaryEffectSlot", (ALCvoid
*) alIsAuxiliaryEffectSlot
},
228 { "alAuxiliaryEffectSloti", (ALCvoid
*) alAuxiliaryEffectSloti
},
229 { "alAuxiliaryEffectSlotiv", (ALCvoid
*) alAuxiliaryEffectSlotiv
},
230 { "alAuxiliaryEffectSlotf", (ALCvoid
*) alAuxiliaryEffectSlotf
},
231 { "alAuxiliaryEffectSlotfv", (ALCvoid
*) alAuxiliaryEffectSlotfv
},
232 { "alGetAuxiliaryEffectSloti", (ALCvoid
*) alGetAuxiliaryEffectSloti
},
233 { "alGetAuxiliaryEffectSlotiv", (ALCvoid
*) alGetAuxiliaryEffectSlotiv
},
234 { "alGetAuxiliaryEffectSlotf", (ALCvoid
*) alGetAuxiliaryEffectSlotf
},
235 { "alGetAuxiliaryEffectSlotfv", (ALCvoid
*) alGetAuxiliaryEffectSlotfv
},
237 { "alBufferSubDataEXT", (ALCvoid
*) alBufferSubDataEXT
},
239 { "alGenDatabuffersEXT", (ALCvoid
*) alGenDatabuffersEXT
},
240 { "alDeleteDatabuffersEXT", (ALCvoid
*) alDeleteDatabuffersEXT
},
241 { "alIsDatabufferEXT", (ALCvoid
*) alIsDatabufferEXT
},
242 { "alDatabufferDataEXT", (ALCvoid
*) alDatabufferDataEXT
},
243 { "alDatabufferSubDataEXT", (ALCvoid
*) alDatabufferSubDataEXT
},
244 { "alGetDatabufferSubDataEXT", (ALCvoid
*) alGetDatabufferSubDataEXT
},
245 { "alDatabufferfEXT", (ALCvoid
*) alDatabufferfEXT
},
246 { "alDatabufferfvEXT", (ALCvoid
*) alDatabufferfvEXT
},
247 { "alDatabufferiEXT", (ALCvoid
*) alDatabufferiEXT
},
248 { "alDatabufferivEXT", (ALCvoid
*) alDatabufferivEXT
},
249 { "alGetDatabufferfEXT", (ALCvoid
*) alGetDatabufferfEXT
},
250 { "alGetDatabufferfvEXT", (ALCvoid
*) alGetDatabufferfvEXT
},
251 { "alGetDatabufferiEXT", (ALCvoid
*) alGetDatabufferiEXT
},
252 { "alGetDatabufferivEXT", (ALCvoid
*) alGetDatabufferivEXT
},
253 { "alSelectDatabufferEXT", (ALCvoid
*) alSelectDatabufferEXT
},
254 { "alMapDatabufferEXT", (ALCvoid
*) alMapDatabufferEXT
},
255 { "alUnmapDatabufferEXT", (ALCvoid
*) alUnmapDatabufferEXT
},
257 { NULL
, (ALCvoid
*) NULL
}
260 static const ALCenums enumeration
[] = {
262 { "ALC_INVALID", ALC_INVALID
},
263 { "ALC_FALSE", ALC_FALSE
},
264 { "ALC_TRUE", ALC_TRUE
},
267 { "ALC_MAJOR_VERSION", ALC_MAJOR_VERSION
},
268 { "ALC_MINOR_VERSION", ALC_MINOR_VERSION
},
269 { "ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE
},
270 { "ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES
},
271 { "ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER
},
272 { "ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER
},
273 { "ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER
},
274 { "ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER
},
275 { "ALC_EXTENSIONS", ALC_EXTENSIONS
},
276 { "ALC_FREQUENCY", ALC_FREQUENCY
},
277 { "ALC_REFRESH", ALC_REFRESH
},
278 { "ALC_SYNC", ALC_SYNC
},
279 { "ALC_MONO_SOURCES", ALC_MONO_SOURCES
},
280 { "ALC_STEREO_SOURCES", ALC_STEREO_SOURCES
},
281 { "ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER
},
282 { "ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
},
283 { "ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES
},
284 { "ALC_CONNECTED", ALC_CONNECTED
},
287 { "ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION
},
288 { "ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION
},
289 { "ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS
},
292 { "ALC_NO_ERROR", ALC_NO_ERROR
},
293 { "ALC_INVALID_DEVICE", ALC_INVALID_DEVICE
},
294 { "ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT
},
295 { "ALC_INVALID_ENUM", ALC_INVALID_ENUM
},
296 { "ALC_INVALID_VALUE", ALC_INVALID_VALUE
},
297 { "ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY
},
301 static const ALCchar alcNoError
[] = "No Error";
302 static const ALCchar alcErrInvalidDevice
[] = "Invalid Device";
303 static const ALCchar alcErrInvalidContext
[] = "Invalid Context";
304 static const ALCchar alcErrInvalidEnum
[] = "Invalid Enum";
305 static const ALCchar alcErrInvalidValue
[] = "Invalid Value";
306 static const ALCchar alcErrOutOfMemory
[] = "Out of Memory";
308 /* Device lists. Sizes only include the first ending null character, not the
310 static ALCchar
*alcDeviceList
;
311 static size_t alcDeviceListSize
;
312 static ALCchar
*alcAllDeviceList
;
313 static size_t alcAllDeviceListSize
;
314 static ALCchar
*alcCaptureDeviceList
;
315 static size_t alcCaptureDeviceListSize
;
316 // Default is always the first in the list
317 static ALCchar
*alcDefaultDeviceSpecifier
;
318 static ALCchar
*alcDefaultAllDeviceSpecifier
;
319 static ALCchar
*alcCaptureDefaultDeviceSpecifier
;
322 static const ALCchar alcNoDeviceExtList
[] =
323 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
324 "ALC_EXT_thread_local_context";
325 static const ALCchar alcExtensionList
[] =
326 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
327 "ALC_EXT_disconnect ALC_EXT_EFX ALC_EXT_thread_local_context";
328 static const ALCint alcMajorVersion
= 1;
329 static const ALCint alcMinorVersion
= 1;
331 static const ALCint alcEFXMajorVersion
= 1;
332 static const ALCint alcEFXMinorVersion
= 0;
334 ///////////////////////////////////////////////////////
337 ///////////////////////////////////////////////////////
340 static ALCdevice
*g_pDeviceList
= NULL
;
341 static ALCuint g_ulDeviceCount
= 0;
343 static CRITICAL_SECTION g_csMutex
;
346 static ALCcontext
*g_pContextList
= NULL
;
347 static ALCuint g_ulContextCount
= 0;
349 // Thread-local current context
350 static tls_type LocalContext
;
351 // Process-wide current context
352 static ALCcontext
*GlobalContext
;
355 static ALCenum g_eLastNullDeviceError
= ALC_NO_ERROR
;
357 // Default context extensions
358 static const ALchar alExtList
[] =
359 "AL_EXTX_buffer_sub_data AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE "
360 "AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXTX_loop_points "
361 "AL_EXT_MCFORMATS AL_EXT_MULAW AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET "
362 "AL_EXTX_sample_buffer_object AL_EXT_source_distance_model "
363 "AL_LOKI_quadriphonic";
365 // Mixing Priority Level
366 static ALint RTPrioLevel
;
369 resampler_t DefaultResampler
;
372 static FILE *LogFile
;
374 ///////////////////////////////////////////////////////
377 ///////////////////////////////////////////////////////
378 // ALC Related helper functions
380 static void alc_init(void);
381 static void alc_deinit(void);
383 BOOL APIENTRY
DllMain(HANDLE hModule
,DWORD ul_reason_for_call
,LPVOID lpReserved
)
387 // Perform actions based on the reason for calling.
388 switch(ul_reason_for_call
)
390 case DLL_PROCESS_ATTACH
:
391 DisableThreadLibraryCalls(hModule
);
395 case DLL_PROCESS_DETACH
:
402 #ifdef HAVE_GCC_DESTRUCTOR
403 static void alc_init(void) __attribute__((constructor
));
404 static void alc_deinit(void) __attribute__((destructor
));
408 static void alc_init(void)
411 const char *devs
, *str
;
413 str
= getenv("ALSOFT_LOGFILE");
416 LogFile
= fopen(str
, "w");
418 fprintf(stderr
, "AL lib: Failed to open log file '%s'\n", str
);
423 InitializeCriticalSection(&g_csMutex
);
427 tls_create(&LocalContext
);
429 RTPrioLevel
= GetConfigValueInt(NULL
, "rt-prio", 0);
431 DefaultResampler
= GetConfigValueInt(NULL
, "resampler", RESAMPLER_DEFAULT
);
432 if(DefaultResampler
>= RESAMPLER_MAX
|| DefaultResampler
<= RESAMPLER_MIN
)
433 DefaultResampler
= RESAMPLER_DEFAULT
;
435 devs
= GetConfigValue(NULL
, "drivers", "");
440 const char *next
= devs
;
441 int endlist
, delitem
;
446 next
= strchr(devs
, ',');
448 delitem
= (devs
[0] == '-');
449 if(devs
[0] == '-') devs
++;
451 if(!devs
[0] || devs
[0] == ',')
458 len
= (next
? ((size_t)(next
-devs
)) : strlen(devs
));
459 for(n
= i
;BackendList
[n
].Init
;n
++)
461 if(len
== strlen(BackendList
[n
].name
) &&
462 strncmp(BackendList
[n
].name
, devs
, len
) == 0)
467 BackendList
[n
] = BackendList
[n
+1];
469 } while(BackendList
[n
].Init
);
473 BackendInfo Bkp
= BackendList
[n
];
476 BackendList
[n
] = BackendList
[n
-1];
479 BackendList
[n
] = Bkp
;
490 BackendList
[i
].name
= NULL
;
491 BackendList
[i
].Init
= NULL
;
492 BackendList
[i
].Deinit
= NULL
;
493 BackendList
[i
].Probe
= NULL
;
497 for(i
= 0;BackendList
[i
].Init
;i
++)
498 BackendList
[i
].Init(&BackendList
[i
].Funcs
);
500 str
= GetConfigValue(NULL
, "excludefx", "");
507 { "eaxreverb", EAXREVERB
},
508 { "reverb", REVERB
},
510 { "modulator", MODULATOR
},
515 const char *next
= str
;
519 next
= strchr(str
, ',');
521 if(!str
[0] || next
== str
)
524 len
= (next
? ((size_t)(next
-str
)) : strlen(str
));
525 for(n
= 0;EffectList
[n
].name
;n
++)
527 if(len
== strlen(EffectList
[n
].name
) &&
528 strncmp(EffectList
[n
].name
, str
, len
) == 0)
529 DisabledEffects
[EffectList
[n
].type
] = AL_TRUE
;
535 static void alc_deinit(void)
541 for(i
= 0;BackendList
[i
].Deinit
;i
++)
542 BackendList
[i
].Deinit();
544 tls_delete(LocalContext
);
548 DeleteCriticalSection(&g_csMutex
);
550 if(LogFile
!= stderr
)
556 static void ProbeDeviceList()
560 free(alcDeviceList
); alcDeviceList
= NULL
;
561 alcDeviceListSize
= 0;
563 for(i
= 0;BackendList
[i
].Probe
;i
++)
564 BackendList
[i
].Probe(DEVICE_PROBE
);
567 static void ProbeAllDeviceList()
571 free(alcAllDeviceList
); alcAllDeviceList
= NULL
;
572 alcAllDeviceListSize
= 0;
574 for(i
= 0;BackendList
[i
].Probe
;i
++)
575 BackendList
[i
].Probe(ALL_DEVICE_PROBE
);
578 static void ProbeCaptureDeviceList()
582 free(alcCaptureDeviceList
); alcCaptureDeviceList
= NULL
;
583 alcCaptureDeviceListSize
= 0;
585 for(i
= 0;BackendList
[i
].Probe
;i
++)
586 BackendList
[i
].Probe(CAPTURE_DEVICE_PROBE
);
590 #define DECL_APPEND_LIST_FUNC(type) \
591 void Append##type##List(const ALCchar *name) \
593 size_t len = strlen(name); \
599 temp = realloc(alc##type##List, alc##type##ListSize + len + 2); \
602 AL_PRINT("Realloc failed to add %s!\n", name); \
605 alc##type##List = temp; \
606 sprintf(alc##type##List+alc##type##ListSize, "%s", name); \
607 alc##type##ListSize += len+1; \
608 alc##type##List[alc##type##ListSize] = 0; \
611 DECL_APPEND_LIST_FUNC(Device
)
612 DECL_APPEND_LIST_FUNC(AllDevice
)
613 DECL_APPEND_LIST_FUNC(CaptureDevice
)
616 void al_print(const char *fname
, unsigned int line
, const char *fmt
, ...)
622 fn
= strrchr(fname
, '/');
623 if(!fn
) fn
= strrchr(fname
, '\\');;
627 i
= snprintf(str
, sizeof(str
), "AL lib: %s:%d: ", fn
, line
);
628 if(i
< (int)sizeof(str
) && i
> 0)
632 vsnprintf(str
+i
, sizeof(str
)-i
, fmt
, ap
);
635 str
[sizeof(str
)-1] = 0;
637 fprintf(LogFile
, "%s", str
);
641 void SetRTPriority(void)
647 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL
);
649 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL
);
650 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM)
651 struct sched_param param
;
655 /* Use the minimum real-time priority possible for now (on Linux this
656 * should be 1 for SCHED_RR) */
657 param
.sched_priority
= sched_get_priority_min(SCHED_RR
);
658 failed
= !!pthread_setschedparam(pthread_self(), SCHED_RR
, ¶m
);
662 param
.sched_priority
= 0;
663 failed
= !!pthread_setschedparam(pthread_self(), SCHED_OTHER
, ¶m
);
666 /* Real-time priority not available */
667 failed
= (RTPrioLevel
>0);
670 AL_PRINT("Failed to set priority level for thread\n");
674 void InitUIntMap(UIntMap
*map
)
681 void ResetUIntMap(UIntMap
*map
)
689 ALenum
InsertUIntMapEntry(UIntMap
*map
, ALuint key
, ALvoid
*value
)
696 ALsizei high
= map
->size
- 1;
699 ALsizei mid
= low
+ (high
-low
)/2;
700 if(map
->array
[mid
].key
< key
)
705 if(map
->array
[low
].key
< key
)
710 if(pos
== map
->size
|| map
->array
[pos
].key
!= key
)
712 if(map
->size
== map
->maxsize
)
717 newsize
= (map
->maxsize
? (map
->maxsize
<<1) : 4);
718 if(newsize
< map
->maxsize
)
719 return AL_OUT_OF_MEMORY
;
721 temp
= realloc(map
->array
, newsize
*sizeof(map
->array
[0]));
722 if(!temp
) return AL_OUT_OF_MEMORY
;
724 map
->maxsize
= newsize
;
728 if(pos
< map
->size
-1)
729 memmove(&map
->array
[pos
+1], &map
->array
[pos
],
730 (map
->size
-1-pos
)*sizeof(map
->array
[0]));
732 map
->array
[pos
].key
= key
;
733 map
->array
[pos
].value
= value
;
738 void RemoveUIntMapKey(UIntMap
*map
, ALuint key
)
743 ALsizei high
= map
->size
- 1;
746 ALsizei mid
= low
+ (high
-low
)/2;
747 if(map
->array
[mid
].key
< key
)
752 if(map
->array
[low
].key
== key
)
754 if(low
< map
->size
-1)
755 memmove(&map
->array
[low
], &map
->array
[low
+1],
756 (map
->size
-1-low
)*sizeof(map
->array
[0]));
766 Check pDevice is a valid Device pointer
768 static ALCboolean
IsDevice(ALCdevice
*pDevice
)
770 ALCdevice
*pTempDevice
;
772 SuspendContext(NULL
);
774 pTempDevice
= g_pDeviceList
;
775 while(pTempDevice
&& pTempDevice
!= pDevice
)
776 pTempDevice
= pTempDevice
->next
;
778 ProcessContext(NULL
);
780 return (pTempDevice
? ALC_TRUE
: ALC_FALSE
);
786 Check pContext is a valid Context pointer
788 static ALCboolean
IsContext(ALCcontext
*pContext
)
790 ALCcontext
*pTempContext
;
792 SuspendContext(NULL
);
794 pTempContext
= g_pContextList
;
795 while (pTempContext
&& pTempContext
!= pContext
)
796 pTempContext
= pTempContext
->next
;
798 ProcessContext(NULL
);
800 return (pTempContext
? ALC_TRUE
: ALC_FALSE
);
807 Store latest ALC Error
809 ALCvoid
alcSetError(ALCdevice
*device
, ALenum errorCode
)
812 device
->LastError
= errorCode
;
814 g_eLastNullDeviceError
= errorCode
;
823 ALCvoid
SuspendContext(ALCcontext
*pContext
)
826 EnterCriticalSection(&g_csMutex
);
835 ALCvoid
ProcessContext(ALCcontext
*pContext
)
838 LeaveCriticalSection(&g_csMutex
);
845 Returns the currently active Context, in a locked state
847 ALCcontext
*GetContextSuspended(void)
849 ALCcontext
*pContext
= NULL
;
851 SuspendContext(NULL
);
853 pContext
= tls_get(LocalContext
);
854 if(pContext
&& !IsContext(pContext
))
856 tls_set(LocalContext
, NULL
);
860 pContext
= GlobalContext
;
863 SuspendContext(pContext
);
865 ProcessContext(NULL
);
874 Initialize Context variables
876 static ALvoid
InitContext(ALCcontext
*pContext
)
878 //Initialise listener
879 pContext
->Listener
.Gain
= 1.0f
;
880 pContext
->Listener
.MetersPerUnit
= 1.0f
;
881 pContext
->Listener
.Position
[0] = 0.0f
;
882 pContext
->Listener
.Position
[1] = 0.0f
;
883 pContext
->Listener
.Position
[2] = 0.0f
;
884 pContext
->Listener
.Velocity
[0] = 0.0f
;
885 pContext
->Listener
.Velocity
[1] = 0.0f
;
886 pContext
->Listener
.Velocity
[2] = 0.0f
;
887 pContext
->Listener
.Forward
[0] = 0.0f
;
888 pContext
->Listener
.Forward
[1] = 0.0f
;
889 pContext
->Listener
.Forward
[2] = -1.0f
;
890 pContext
->Listener
.Up
[0] = 0.0f
;
891 pContext
->Listener
.Up
[1] = 1.0f
;
892 pContext
->Listener
.Up
[2] = 0.0f
;
895 pContext
->LastError
= AL_NO_ERROR
;
896 pContext
->Suspended
= AL_FALSE
;
897 pContext
->ActiveSourceCount
= 0;
898 InitUIntMap(&pContext
->SourceMap
);
899 InitUIntMap(&pContext
->EffectSlotMap
);
902 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
903 pContext
->SourceDistanceModel
= AL_FALSE
;
904 pContext
->DopplerFactor
= 1.0f
;
905 pContext
->DopplerVelocity
= 1.0f
;
906 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
908 pContext
->ExtensionList
= alExtList
;
915 Clean up Context, destroy any remaining Sources
917 static ALCvoid
ExitContext(ALCcontext
*pContext
)
920 pContext
->LastError
= AL_NO_ERROR
;
923 ///////////////////////////////////////////////////////
926 ///////////////////////////////////////////////////////
927 // ALC Functions calls
930 // This should probably move to another c file but for now ...
931 ALC_API ALCdevice
* ALC_APIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
933 ALCboolean DeviceFound
= ALC_FALSE
;
934 ALCdevice
*device
= NULL
;
939 alcSetError(NULL
, ALC_INVALID_VALUE
);
943 if(deviceName
&& !deviceName
[0])
946 device
= calloc(1, sizeof(ALCdevice
));
949 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
954 device
->Connected
= ALC_TRUE
;
955 device
->IsCaptureDevice
= AL_TRUE
;
957 device
->szDeviceName
= NULL
;
959 device
->Frequency
= frequency
;
960 device
->Format
= format
;
961 device
->UpdateSize
= SampleSize
;
962 device
->NumUpdates
= 1;
964 SuspendContext(NULL
);
965 for(i
= 0;BackendList
[i
].Init
;i
++)
967 device
->Funcs
= &BackendList
[i
].Funcs
;
968 if(ALCdevice_OpenCapture(device
, deviceName
))
970 device
->next
= g_pDeviceList
;
971 g_pDeviceList
= device
;
974 DeviceFound
= ALC_TRUE
;
978 ProcessContext(NULL
);
982 alcSetError(NULL
, ALC_INVALID_VALUE
);
990 ALC_API ALCboolean ALC_APIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
994 if(!IsDevice(pDevice
) || !pDevice
->IsCaptureDevice
)
996 alcSetError(pDevice
, ALC_INVALID_DEVICE
);
1000 SuspendContext(NULL
);
1002 list
= &g_pDeviceList
;
1003 while(*list
!= pDevice
)
1004 list
= &(*list
)->next
;
1006 *list
= (*list
)->next
;
1009 ProcessContext(NULL
);
1011 ALCdevice_CloseCapture(pDevice
);
1013 free(pDevice
->szDeviceName
);
1014 pDevice
->szDeviceName
= NULL
;
1021 ALC_API
void ALC_APIENTRY
alcCaptureStart(ALCdevice
*device
)
1023 SuspendContext(NULL
);
1024 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
1025 alcSetError(device
, ALC_INVALID_DEVICE
);
1026 else if(device
->Connected
)
1027 ALCdevice_StartCapture(device
);
1028 ProcessContext(NULL
);
1031 ALC_API
void ALC_APIENTRY
alcCaptureStop(ALCdevice
*device
)
1033 SuspendContext(NULL
);
1034 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
1035 alcSetError(device
, ALC_INVALID_DEVICE
);
1037 ALCdevice_StopCapture(device
);
1038 ProcessContext(NULL
);
1041 ALC_API
void ALC_APIENTRY
alcCaptureSamples(ALCdevice
*device
, ALCvoid
*buffer
, ALCsizei samples
)
1043 SuspendContext(NULL
);
1044 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
1045 alcSetError(device
, ALC_INVALID_DEVICE
);
1047 ALCdevice_CaptureSamples(device
, buffer
, samples
);
1048 ProcessContext(NULL
);
1054 Return last ALC generated error code
1056 ALC_API ALCenum ALC_APIENTRY
alcGetError(ALCdevice
*device
)
1060 if(IsDevice(device
))
1062 errorCode
= device
->LastError
;
1063 device
->LastError
= ALC_NO_ERROR
;
1067 errorCode
= g_eLastNullDeviceError
;
1068 g_eLastNullDeviceError
= ALC_NO_ERROR
;
1079 ALC_API ALCvoid ALC_APIENTRY
alcSuspendContext(ALCcontext
*pContext
)
1081 SuspendContext(NULL
);
1082 if(IsContext(pContext
))
1083 pContext
->Suspended
= AL_TRUE
;
1084 ProcessContext(NULL
);
1093 ALC_API ALCvoid ALC_APIENTRY
alcProcessContext(ALCcontext
*pContext
)
1095 SuspendContext(NULL
);
1096 if(IsContext(pContext
))
1097 pContext
->Suspended
= AL_FALSE
;
1098 ProcessContext(NULL
);
1105 Returns information about the Device, and error strings
1107 ALC_API
const ALCchar
* ALC_APIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
1109 const ALCchar
*value
= NULL
;
1117 case ALC_INVALID_ENUM
:
1118 value
= alcErrInvalidEnum
;
1121 case ALC_INVALID_VALUE
:
1122 value
= alcErrInvalidValue
;
1125 case ALC_INVALID_DEVICE
:
1126 value
= alcErrInvalidDevice
;
1129 case ALC_INVALID_CONTEXT
:
1130 value
= alcErrInvalidContext
;
1133 case ALC_OUT_OF_MEMORY
:
1134 value
= alcErrOutOfMemory
;
1137 case ALC_DEVICE_SPECIFIER
:
1138 if(IsDevice(pDevice
))
1139 value
= pDevice
->szDeviceName
;
1143 value
= alcDeviceList
;
1147 case ALC_ALL_DEVICES_SPECIFIER
:
1148 ProbeAllDeviceList();
1149 value
= alcAllDeviceList
;
1152 case ALC_CAPTURE_DEVICE_SPECIFIER
:
1153 if(IsDevice(pDevice
))
1154 value
= pDevice
->szDeviceName
;
1157 ProbeCaptureDeviceList();
1158 value
= alcCaptureDeviceList
;
1162 /* Default devices are always first in the list */
1163 case ALC_DEFAULT_DEVICE_SPECIFIER
:
1167 free(alcDefaultDeviceSpecifier
);
1168 alcDefaultDeviceSpecifier
= strdup(alcDeviceList
? alcDeviceList
: "");
1169 if(!alcDefaultDeviceSpecifier
)
1170 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1171 value
= alcDefaultDeviceSpecifier
;
1174 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
1175 if(!alcAllDeviceList
)
1176 ProbeAllDeviceList();
1178 free(alcDefaultAllDeviceSpecifier
);
1179 alcDefaultAllDeviceSpecifier
= strdup(alcAllDeviceList
?
1180 alcAllDeviceList
: "");
1181 if(!alcDefaultAllDeviceSpecifier
)
1182 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1183 value
= alcDefaultAllDeviceSpecifier
;
1186 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
1187 if(!alcCaptureDeviceList
)
1188 ProbeCaptureDeviceList();
1190 free(alcCaptureDefaultDeviceSpecifier
);
1191 alcCaptureDefaultDeviceSpecifier
= strdup(alcCaptureDeviceList
?
1192 alcCaptureDeviceList
: "");
1193 if(!alcCaptureDefaultDeviceSpecifier
)
1194 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1195 value
= alcCaptureDefaultDeviceSpecifier
;
1198 case ALC_EXTENSIONS
:
1199 if(IsDevice(pDevice
))
1200 value
= alcExtensionList
;
1202 value
= alcNoDeviceExtList
;
1206 alcSetError(pDevice
, ALC_INVALID_ENUM
);
1217 Returns information about the Device and the version of Open AL
1219 ALC_API ALCvoid ALC_APIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
1221 if(size
== 0 || data
== NULL
)
1223 alcSetError(device
, ALC_INVALID_VALUE
);
1227 if(IsDevice(device
) && device
->IsCaptureDevice
)
1229 SuspendContext(NULL
);
1234 case ALC_CAPTURE_SAMPLES
:
1235 *data
= ALCdevice_AvailableSamples(device
);
1239 *data
= device
->Connected
;
1243 alcSetError(device
, ALC_INVALID_ENUM
);
1247 ProcessContext(NULL
);
1254 case ALC_MAJOR_VERSION
:
1255 *data
= alcMajorVersion
;
1258 case ALC_MINOR_VERSION
:
1259 *data
= alcMinorVersion
;
1262 case ALC_EFX_MAJOR_VERSION
:
1263 *data
= alcEFXMajorVersion
;
1266 case ALC_EFX_MINOR_VERSION
:
1267 *data
= alcEFXMinorVersion
;
1270 case ALC_MAX_AUXILIARY_SENDS
:
1271 if(!IsDevice(device
))
1272 alcSetError(device
, ALC_INVALID_DEVICE
);
1274 *data
= device
->NumAuxSends
;
1277 case ALC_ATTRIBUTES_SIZE
:
1278 if(!IsDevice(device
))
1279 alcSetError(device
, ALC_INVALID_DEVICE
);
1284 case ALC_ALL_ATTRIBUTES
:
1285 if(!IsDevice(device
))
1286 alcSetError(device
, ALC_INVALID_DEVICE
);
1288 alcSetError(device
, ALC_INVALID_VALUE
);
1293 SuspendContext(NULL
);
1294 data
[i
++] = ALC_FREQUENCY
;
1295 data
[i
++] = device
->Frequency
;
1297 data
[i
++] = ALC_REFRESH
;
1298 data
[i
++] = device
->Frequency
/ device
->UpdateSize
;
1300 data
[i
++] = ALC_SYNC
;
1301 data
[i
++] = ALC_FALSE
;
1303 data
[i
++] = ALC_MONO_SOURCES
;
1304 data
[i
++] = device
->NumMonoSources
;
1306 data
[i
++] = ALC_STEREO_SOURCES
;
1307 data
[i
++] = device
->NumStereoSources
;
1309 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
1310 data
[i
++] = device
->NumAuxSends
;
1313 ProcessContext(NULL
);
1318 if(!IsDevice(device
))
1319 alcSetError(device
, ALC_INVALID_DEVICE
);
1321 *data
= device
->Frequency
;
1325 if(!IsDevice(device
))
1326 alcSetError(device
, ALC_INVALID_DEVICE
);
1328 *data
= device
->Frequency
/ device
->UpdateSize
;
1332 if(!IsDevice(device
))
1333 alcSetError(device
, ALC_INVALID_DEVICE
);
1338 case ALC_MONO_SOURCES
:
1339 if(!IsDevice(device
))
1340 alcSetError(device
, ALC_INVALID_DEVICE
);
1342 *data
= device
->NumMonoSources
;
1345 case ALC_STEREO_SOURCES
:
1346 if(!IsDevice(device
))
1347 alcSetError(device
, ALC_INVALID_DEVICE
);
1349 *data
= device
->NumStereoSources
;
1353 if(!IsDevice(device
))
1354 alcSetError(device
, ALC_INVALID_DEVICE
);
1356 *data
= device
->Connected
;
1359 case ALC_GET_TIME_EXT
:
1360 if(!IsDevice(device
))
1361 alcSetError(device
, ALC_INVALID_DEVICE
);
1363 alcSetError(device
, ALC_INVALID_VALUE
);
1366 ALuint64 t
= ALCdevice_GetTime(device
);
1367 t
-= t
%device
->TimeRes
;
1368 data
[0] = t
&0xffffffff;
1373 case ALC_GET_TIME_RES_EXT
:
1374 if(!IsDevice(device
))
1375 alcSetError(device
, ALC_INVALID_DEVICE
);
1377 alcSetError(device
, ALC_INVALID_VALUE
);
1380 data
[0] = device
->TimeRes
&0xffffffff;
1381 data
[1] = device
->TimeRes
>>32;
1386 alcSetError(device
, ALC_INVALID_ENUM
);
1393 alcIsExtensionPresent
1395 Determines if there is support for a particular extension
1397 ALC_API ALCboolean ALC_APIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
1399 ALCboolean bResult
= ALC_FALSE
;
1405 alcSetError(device
, ALC_INVALID_VALUE
);
1409 len
= strlen(extName
);
1410 ptr
= (IsDevice(device
) ? alcExtensionList
: alcNoDeviceExtList
);
1413 if(strncasecmp(ptr
, extName
, len
) == 0 &&
1414 (ptr
[len
] == '\0' || isspace(ptr
[len
])))
1419 if((ptr
=strchr(ptr
, ' ')) != NULL
)
1423 } while(isspace(*ptr
));
1434 Retrieves the function address for a particular extension function
1436 ALC_API ALCvoid
* ALC_APIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
1442 alcSetError(device
, ALC_INVALID_VALUE
);
1446 while(alcFunctions
[i
].funcName
&& strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
1448 return alcFunctions
[i
].address
;
1455 Get the value for a particular ALC Enumerated Value
1457 ALC_API ALCenum ALC_APIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
1463 alcSetError(device
, ALC_INVALID_VALUE
);
1467 while(enumeration
[i
].enumName
&& strcmp(enumeration
[i
].enumName
,enumName
) != 0)
1469 return enumeration
[i
].value
;
1476 Create and attach a Context to a particular Device.
1478 ALC_API ALCcontext
* ALC_APIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
1480 ALCcontext
*ALContext
;
1487 SuspendContext(NULL
);
1489 if(!IsDevice(device
) || device
->IsCaptureDevice
|| !device
->Connected
)
1491 alcSetError(device
, ALC_INVALID_DEVICE
);
1492 ProcessContext(NULL
);
1496 running
= ((device
->NumContexts
> 0) ? AL_TRUE
: AL_FALSE
);
1497 oldRate
= device
->Frequency
;
1499 // Reset Context Last Error code
1500 device
->LastError
= ALC_NO_ERROR
;
1502 // Check for attributes
1503 if(attrList
&& attrList
[0])
1505 ALCuint freq
, numMono
, numStereo
, numSends
;
1507 // If a context is already running on the device, stop playback so the
1508 // device attributes can be updated
1511 ProcessContext(NULL
);
1512 ALCdevice_StopPlayback(device
);
1513 SuspendContext(NULL
);
1517 freq
= device
->Frequency
;
1518 numMono
= device
->NumMonoSources
;
1519 numStereo
= device
->NumStereoSources
;
1520 numSends
= device
->NumAuxSends
;
1523 while(attrList
[attrIdx
])
1525 if(attrList
[attrIdx
] == ALC_FREQUENCY
&&
1526 !ConfigValueExists(NULL
, "frequency"))
1528 freq
= attrList
[attrIdx
+ 1];
1533 if(attrList
[attrIdx
] == ALC_STEREO_SOURCES
)
1535 numStereo
= attrList
[attrIdx
+ 1];
1536 if(numStereo
> device
->MaxNoOfSources
)
1537 numStereo
= device
->MaxNoOfSources
;
1539 numMono
= device
->MaxNoOfSources
- numStereo
;
1542 if(attrList
[attrIdx
] == ALC_MAX_AUXILIARY_SENDS
&&
1543 !ConfigValueExists(NULL
, "sends"))
1545 numSends
= attrList
[attrIdx
+ 1];
1546 if(numSends
> MAX_SENDS
)
1547 numSends
= MAX_SENDS
;
1553 device
->UpdateSize
= (ALuint64
)device
->UpdateSize
* freq
/
1556 device
->Frequency
= freq
;
1557 device
->NumMonoSources
= numMono
;
1558 device
->NumStereoSources
= numStereo
;
1559 device
->NumAuxSends
= numSends
;
1562 if(running
== AL_FALSE
&& ALCdevice_ResetPlayback(device
) == ALC_FALSE
)
1564 alcSetError(device
, ALC_INVALID_DEVICE
);
1565 aluHandleDisconnect(device
);
1566 ProcessContext(NULL
);
1569 aluInitPanning(device
);
1571 // Scale the number of samples played according to the new sample rate
1572 device
->SamplesPlayed
*= device
->Frequency
;
1573 device
->SamplesPlayed
+= oldRate
-1;
1574 device
->SamplesPlayed
/= oldRate
;
1576 for(i
= 0;i
< device
->NumContexts
;i
++)
1578 ALCcontext
*context
= device
->Contexts
[i
];
1581 SuspendContext(context
);
1582 for(pos
= 0;pos
< context
->EffectSlotMap
.size
;pos
++)
1584 ALeffectslot
*slot
= context
->EffectSlotMap
.array
[pos
].value
;
1586 if(ALEffect_DeviceUpdate(slot
->EffectState
, device
) == AL_FALSE
)
1588 alcSetError(device
, ALC_INVALID_DEVICE
);
1589 aluHandleDisconnect(device
);
1590 ProcessContext(context
);
1591 ProcessContext(NULL
);
1592 ALCdevice_StopPlayback(device
);
1595 ALEffect_Update(slot
->EffectState
, context
, &slot
->effect
);
1598 for(pos
= 0;pos
< context
->SourceMap
.size
;pos
++)
1600 ALsource
*source
= context
->SourceMap
.array
[pos
].value
;
1601 ALuint s
= device
->NumAuxSends
;
1602 while(s
< MAX_SENDS
)
1604 if(source
->Send
[s
].Slot
)
1605 source
->Send
[s
].Slot
->refcount
--;
1606 source
->Send
[s
].Slot
= NULL
;
1607 source
->Send
[s
].WetFilter
.type
= 0;
1608 source
->Send
[s
].WetFilter
.filter
= 0;
1611 source
->NeedsUpdate
= AL_TRUE
;
1613 ProcessContext(context
);
1616 if(device
->Bs2bLevel
> 0 && device
->Bs2bLevel
<= 6)
1620 device
->Bs2b
= calloc(1, sizeof(*device
->Bs2b
));
1621 bs2b_clear(device
->Bs2b
);
1623 bs2b_set_srate(device
->Bs2b
, device
->Frequency
);
1624 bs2b_set_level(device
->Bs2b
, device
->Bs2bLevel
);
1629 device
->Bs2b
= NULL
;
1632 if(aluChannelsFromFormat(device
->Format
) <= 2)
1634 device
->HeadDampen
= GetConfigValueFloat(NULL
, "head_dampen", DEFAULT_HEAD_DAMPEN
);
1635 device
->HeadDampen
= __min(device
->HeadDampen
, 1.0f
);
1636 device
->HeadDampen
= __max(device
->HeadDampen
, 0.0f
);
1639 device
->HeadDampen
= 0.0f
;
1641 temp
= realloc(device
->Contexts
, (device
->NumContexts
+1) * sizeof(*device
->Contexts
));
1644 alcSetError(device
, ALC_OUT_OF_MEMORY
);
1645 ProcessContext(NULL
);
1646 if(device
->NumContexts
== 0)
1647 ALCdevice_StopPlayback(device
);
1650 device
->Contexts
= temp
;
1652 ALContext
= calloc(1, sizeof(ALCcontext
));
1655 ALContext
->MaxActiveSources
= 256;
1656 ALContext
->ActiveSources
= malloc(sizeof(*ALContext
->ActiveSources
) *
1657 ALContext
->MaxActiveSources
);
1659 if(!ALContext
|| !ALContext
->ActiveSources
)
1662 alcSetError(device
, ALC_OUT_OF_MEMORY
);
1663 ProcessContext(NULL
);
1664 if(device
->NumContexts
== 0)
1665 ALCdevice_StopPlayback(device
);
1669 device
->Contexts
[device
->NumContexts
++] = ALContext
;
1670 ALContext
->Device
= device
;
1672 InitContext(ALContext
);
1674 ALContext
->next
= g_pContextList
;
1675 g_pContextList
= ALContext
;
1678 ProcessContext(NULL
);
1689 ALC_API ALCvoid ALC_APIENTRY
alcDestroyContext(ALCcontext
*context
)
1695 if(!IsContext(context
))
1697 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1701 Device
= context
->Device
;
1703 if(Device
->NumContexts
== 1)
1704 ALCdevice_StopPlayback(Device
);
1706 SuspendContext(NULL
);
1708 if(context
== GlobalContext
)
1709 GlobalContext
= NULL
;
1711 for(i
= 0;i
< Device
->NumContexts
-1;i
++)
1713 if(Device
->Contexts
[i
] == context
)
1715 Device
->Contexts
[i
] = Device
->Contexts
[Device
->NumContexts
-1];
1719 Device
->NumContexts
--;
1722 SuspendContext(context
);
1724 if(context
->SourceMap
.size
> 0)
1727 AL_PRINT("alcDestroyContext(): deleting %d Source(s)\n", context
->SourceMap
.size
);
1729 ReleaseALSources(context
);
1731 ResetUIntMap(&context
->SourceMap
);
1733 if(context
->EffectSlotMap
.size
> 0)
1736 AL_PRINT("alcDestroyContext(): deleting %d AuxiliaryEffectSlot(s)\n", context
->EffectSlotMap
.size
);
1738 ReleaseALAuxiliaryEffectSlots(context
);
1740 ResetUIntMap(&context
->EffectSlotMap
);
1742 free(context
->ActiveSources
);
1743 context
->ActiveSources
= NULL
;
1744 context
->MaxActiveSources
= 0;
1745 context
->ActiveSourceCount
= 0;
1747 list
= &g_pContextList
;
1748 while(*list
!= context
)
1749 list
= &(*list
)->next
;
1751 *list
= (*list
)->next
;
1755 ProcessContext(context
);
1756 ProcessContext(NULL
);
1758 ExitContext(context
);
1760 // Free memory (MUST do this after ProcessContext)
1761 memset(context
, 0, sizeof(ALCcontext
));
1767 alcGetCurrentContext
1769 Returns the currently active Context
1771 ALC_API ALCcontext
* ALC_APIENTRY
alcGetCurrentContext(ALCvoid
)
1773 ALCcontext
*pContext
;
1775 if((pContext
=GetContextSuspended()) != NULL
)
1776 ProcessContext(pContext
);
1784 Returns the currently active thread-local Context
1786 ALC_API ALCcontext
* ALC_APIENTRY
alcGetThreadContext(void)
1788 ALCcontext
*pContext
= NULL
;
1790 SuspendContext(NULL
);
1792 pContext
= tls_get(LocalContext
);
1793 if(pContext
&& !IsContext(pContext
))
1795 tls_set(LocalContext
, NULL
);
1799 ProcessContext(NULL
);
1806 alcGetContextsDevice
1808 Returns the Device that a particular Context is attached to
1810 ALC_API ALCdevice
* ALC_APIENTRY
alcGetContextsDevice(ALCcontext
*pContext
)
1812 ALCdevice
*pDevice
= NULL
;
1814 SuspendContext(NULL
);
1815 if(IsContext(pContext
))
1816 pDevice
= pContext
->Device
;
1818 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1819 ProcessContext(NULL
);
1826 alcMakeContextCurrent
1828 Makes the given Context the active Context
1830 ALC_API ALCboolean ALC_APIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
1832 ALboolean bReturn
= AL_TRUE
;
1834 SuspendContext(NULL
);
1836 // context must be a valid Context or NULL
1837 if(context
== NULL
|| IsContext(context
))
1839 GlobalContext
= context
;
1840 tls_set(LocalContext
, NULL
);
1844 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1848 ProcessContext(NULL
);
1856 Makes the given Context the active Context for the current thread
1858 ALC_API ALCboolean ALC_APIENTRY
alcSetThreadContext(ALCcontext
*context
)
1860 ALboolean bReturn
= AL_TRUE
;
1862 SuspendContext(NULL
);
1864 // context must be a valid Context or NULL
1865 if(context
== NULL
|| IsContext(context
))
1866 tls_set(LocalContext
, context
);
1869 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1873 ProcessContext(NULL
);
1879 // Sets the default channel order used by most non-WaveFormatEx-based APIs
1880 void SetDefaultChannelOrder(ALCdevice
*device
)
1882 switch(aluChannelsFromFormat(device
->Format
))
1884 case 1: device
->DevChannels
[FRONT_CENTER
] = 0; break;
1886 case 2: device
->DevChannels
[FRONT_LEFT
] = 0;
1887 device
->DevChannels
[FRONT_RIGHT
] = 1; break;
1889 case 4: device
->DevChannels
[FRONT_LEFT
] = 0;
1890 device
->DevChannels
[FRONT_RIGHT
] = 1;
1891 device
->DevChannels
[BACK_LEFT
] = 2;
1892 device
->DevChannels
[BACK_RIGHT
] = 3; break;
1894 case 6: device
->DevChannels
[FRONT_LEFT
] = 0;
1895 device
->DevChannels
[FRONT_RIGHT
] = 1;
1896 device
->DevChannels
[BACK_LEFT
] = 2;
1897 device
->DevChannels
[BACK_RIGHT
] = 3;
1898 device
->DevChannels
[FRONT_CENTER
] = 4;
1899 device
->DevChannels
[LFE
] = 5; break;
1901 case 7: device
->DevChannels
[FRONT_LEFT
] = 0;
1902 device
->DevChannels
[FRONT_RIGHT
] = 1;
1903 device
->DevChannels
[FRONT_CENTER
] = 2;
1904 device
->DevChannels
[LFE
] = 3;
1905 device
->DevChannels
[BACK_CENTER
] = 4;
1906 device
->DevChannels
[SIDE_LEFT
] = 5;
1907 device
->DevChannels
[SIDE_RIGHT
] = 6; break;
1909 case 8: device
->DevChannels
[FRONT_LEFT
] = 0;
1910 device
->DevChannels
[FRONT_RIGHT
] = 1;
1911 device
->DevChannels
[BACK_LEFT
] = 2;
1912 device
->DevChannels
[BACK_RIGHT
] = 3;
1913 device
->DevChannels
[FRONT_CENTER
] = 4;
1914 device
->DevChannels
[LFE
] = 5;
1915 device
->DevChannels
[SIDE_LEFT
] = 6;
1916 device
->DevChannels
[SIDE_RIGHT
] = 7; break;
1919 // Sets the default order used by WaveFormatEx
1920 void SetDefaultWFXChannelOrder(ALCdevice
*device
)
1922 switch(aluChannelsFromFormat(device
->Format
))
1924 case 1: device
->DevChannels
[FRONT_CENTER
] = 0; break;
1926 case 2: device
->DevChannels
[FRONT_LEFT
] = 0;
1927 device
->DevChannels
[FRONT_RIGHT
] = 1; break;
1929 case 4: device
->DevChannels
[FRONT_LEFT
] = 0;
1930 device
->DevChannels
[FRONT_RIGHT
] = 1;
1931 device
->DevChannels
[BACK_LEFT
] = 2;
1932 device
->DevChannels
[BACK_RIGHT
] = 3; break;
1934 case 6: device
->DevChannels
[FRONT_LEFT
] = 0;
1935 device
->DevChannels
[FRONT_RIGHT
] = 1;
1936 device
->DevChannels
[FRONT_CENTER
] = 2;
1937 device
->DevChannels
[LFE
] = 3;
1938 device
->DevChannels
[BACK_LEFT
] = 4;
1939 device
->DevChannels
[BACK_RIGHT
] = 5; break;
1941 case 7: device
->DevChannels
[FRONT_LEFT
] = 0;
1942 device
->DevChannels
[FRONT_RIGHT
] = 1;
1943 device
->DevChannels
[FRONT_CENTER
] = 2;
1944 device
->DevChannels
[LFE
] = 3;
1945 device
->DevChannels
[BACK_CENTER
] = 4;
1946 device
->DevChannels
[SIDE_LEFT
] = 5;
1947 device
->DevChannels
[SIDE_RIGHT
] = 6; break;
1949 case 8: device
->DevChannels
[FRONT_LEFT
] = 0;
1950 device
->DevChannels
[FRONT_RIGHT
] = 1;
1951 device
->DevChannels
[FRONT_CENTER
] = 2;
1952 device
->DevChannels
[LFE
] = 3;
1953 device
->DevChannels
[BACK_LEFT
] = 4;
1954 device
->DevChannels
[BACK_RIGHT
] = 5;
1955 device
->DevChannels
[SIDE_LEFT
] = 6;
1956 device
->DevChannels
[SIDE_RIGHT
] = 7; break;
1960 static ALenum
GetFormatFromString(const char *str
)
1962 if(strcasecmp(str
, "AL_FORMAT_MONO32") == 0) return AL_FORMAT_MONO_FLOAT32
;
1963 if(strcasecmp(str
, "AL_FORMAT_STEREO32") == 0) return AL_FORMAT_STEREO_FLOAT32
;
1964 if(strcasecmp(str
, "AL_FORMAT_QUAD32") == 0) return AL_FORMAT_QUAD32
;
1965 if(strcasecmp(str
, "AL_FORMAT_51CHN32") == 0) return AL_FORMAT_51CHN32
;
1966 if(strcasecmp(str
, "AL_FORMAT_61CHN32") == 0) return AL_FORMAT_61CHN32
;
1967 if(strcasecmp(str
, "AL_FORMAT_71CHN32") == 0) return AL_FORMAT_71CHN32
;
1969 if(strcasecmp(str
, "AL_FORMAT_MONO16") == 0) return AL_FORMAT_MONO16
;
1970 if(strcasecmp(str
, "AL_FORMAT_STEREO16") == 0) return AL_FORMAT_STEREO16
;
1971 if(strcasecmp(str
, "AL_FORMAT_QUAD16") == 0) return AL_FORMAT_QUAD16
;
1972 if(strcasecmp(str
, "AL_FORMAT_51CHN16") == 0) return AL_FORMAT_51CHN16
;
1973 if(strcasecmp(str
, "AL_FORMAT_61CHN16") == 0) return AL_FORMAT_61CHN16
;
1974 if(strcasecmp(str
, "AL_FORMAT_71CHN16") == 0) return AL_FORMAT_71CHN16
;
1976 if(strcasecmp(str
, "AL_FORMAT_MONO8") == 0) return AL_FORMAT_MONO8
;
1977 if(strcasecmp(str
, "AL_FORMAT_STEREO8") == 0) return AL_FORMAT_STEREO8
;
1978 if(strcasecmp(str
, "AL_FORMAT_QUAD8") == 0) return AL_FORMAT_QUAD8
;
1979 if(strcasecmp(str
, "AL_FORMAT_51CHN8") == 0) return AL_FORMAT_51CHN8
;
1980 if(strcasecmp(str
, "AL_FORMAT_61CHN8") == 0) return AL_FORMAT_61CHN8
;
1981 if(strcasecmp(str
, "AL_FORMAT_71CHN8") == 0) return AL_FORMAT_71CHN8
;
1983 AL_PRINT("Unknown format: \"%s\"\n", str
);
1984 return AL_FORMAT_STEREO16
;
1990 Open the Device specified.
1992 ALC_API ALCdevice
* ALC_APIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
1994 ALboolean bDeviceFound
= AL_FALSE
;
1999 if(deviceName
&& !deviceName
[0])
2002 device
= calloc(1, sizeof(ALCdevice
));
2005 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
2010 device
->Connected
= ALC_TRUE
;
2011 device
->IsCaptureDevice
= AL_FALSE
;
2012 device
->LastError
= ALC_NO_ERROR
;
2014 device
->Bs2b
= NULL
;
2015 device
->szDeviceName
= NULL
;
2017 device
->Contexts
= NULL
;
2018 device
->NumContexts
= 0;
2020 device
->SamplesPlayed
= 0;
2022 device
->TimeRes
= 1;
2024 InitUIntMap(&device
->BufferMap
);
2025 InitUIntMap(&device
->EffectMap
);
2026 InitUIntMap(&device
->FilterMap
);
2027 InitUIntMap(&device
->DatabufferMap
);
2030 device
->Frequency
= GetConfigValueInt(NULL
, "frequency", SWMIXER_OUTPUT_RATE
);
2031 if(device
->Frequency
< 8000)
2032 device
->Frequency
= 8000;
2034 fmt
= GetConfigValue(NULL
, "format", "AL_FORMAT_STEREO16");
2035 device
->Format
= GetFormatFromString(fmt
);
2037 device
->NumUpdates
= GetConfigValueInt(NULL
, "periods", 4);
2038 if(device
->NumUpdates
< 2)
2039 device
->NumUpdates
= 4;
2041 device
->UpdateSize
= GetConfigValueInt(NULL
, "period_size", 1024);
2042 if(device
->UpdateSize
<= 0)
2043 device
->UpdateSize
= 1024;
2045 device
->MaxNoOfSources
= GetConfigValueInt(NULL
, "sources", 256);
2046 if((ALint
)device
->MaxNoOfSources
<= 0)
2047 device
->MaxNoOfSources
= 256;
2049 device
->AuxiliaryEffectSlotMax
= GetConfigValueInt(NULL
, "slots", 4);
2050 if((ALint
)device
->AuxiliaryEffectSlotMax
<= 0)
2051 device
->AuxiliaryEffectSlotMax
= 4;
2053 device
->NumStereoSources
= 1;
2054 device
->NumMonoSources
= device
->MaxNoOfSources
- device
->NumStereoSources
;
2056 device
->NumAuxSends
= GetConfigValueInt(NULL
, "sends", MAX_SENDS
);
2057 if(device
->NumAuxSends
> MAX_SENDS
)
2058 device
->NumAuxSends
= MAX_SENDS
;
2060 device
->Bs2bLevel
= GetConfigValueInt(NULL
, "cf_level", 0);
2062 device
->HeadDampen
= 0.0f
;
2064 // Find a playback device to open
2065 SuspendContext(NULL
);
2066 for(i
= 0;BackendList
[i
].Init
;i
++)
2068 device
->Funcs
= &BackendList
[i
].Funcs
;
2069 if(ALCdevice_OpenPlayback(device
, deviceName
))
2071 device
->next
= g_pDeviceList
;
2072 g_pDeviceList
= device
;
2075 bDeviceFound
= AL_TRUE
;
2079 ProcessContext(NULL
);
2083 // No suitable output device found
2084 alcSetError(NULL
, ALC_INVALID_VALUE
);
2096 Close the specified Device
2098 ALC_API ALCboolean ALC_APIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
2102 if(!IsDevice(pDevice
) || pDevice
->IsCaptureDevice
)
2104 alcSetError(pDevice
, ALC_INVALID_DEVICE
);
2108 SuspendContext(NULL
);
2110 list
= &g_pDeviceList
;
2111 while(*list
!= pDevice
)
2112 list
= &(*list
)->next
;
2114 *list
= (*list
)->next
;
2117 ProcessContext(NULL
);
2119 if(pDevice
->NumContexts
> 0)
2122 AL_PRINT("alcCloseDevice(): destroying %u Context(s)\n", pDevice
->NumContexts
);
2124 while(pDevice
->NumContexts
> 0)
2125 alcDestroyContext(pDevice
->Contexts
[0]);
2127 ALCdevice_ClosePlayback(pDevice
);
2129 if(pDevice
->BufferMap
.size
> 0)
2132 AL_PRINT("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice
->BufferMap
.size
);
2134 ReleaseALBuffers(pDevice
);
2136 ResetUIntMap(&pDevice
->BufferMap
);
2138 if(pDevice
->EffectMap
.size
> 0)
2141 AL_PRINT("alcCloseDevice(): deleting %d Effect(s)\n", pDevice
->EffectMap
.size
);
2143 ReleaseALEffects(pDevice
);
2145 ResetUIntMap(&pDevice
->EffectMap
);
2147 if(pDevice
->FilterMap
.size
> 0)
2150 AL_PRINT("alcCloseDevice(): deleting %d Filter(s)\n", pDevice
->FilterMap
.size
);
2152 ReleaseALFilters(pDevice
);
2154 ResetUIntMap(&pDevice
->FilterMap
);
2156 if(pDevice
->DatabufferMap
.size
> 0)
2159 AL_PRINT("alcCloseDevice(): deleting %d Databuffer(s)\n", pDevice
->DatabufferMap
.size
);
2161 ReleaseALDatabuffers(pDevice
);
2163 ResetUIntMap(&pDevice
->DatabufferMap
);
2165 free(pDevice
->Bs2b
);
2166 pDevice
->Bs2b
= NULL
;
2168 free(pDevice
->szDeviceName
);
2169 pDevice
->szDeviceName
= NULL
;
2171 free(pDevice
->Contexts
);
2172 pDevice
->Contexts
= NULL
;
2174 //Release device structure
2175 memset(pDevice
, 0, sizeof(ALCdevice
));
2182 ALCvoid
ReleaseALC(ALCvoid
)
2184 free(alcDeviceList
); alcDeviceList
= NULL
;
2185 alcDeviceListSize
= 0;
2186 free(alcAllDeviceList
); alcAllDeviceList
= NULL
;
2187 alcAllDeviceListSize
= 0;
2188 free(alcCaptureDeviceList
); alcCaptureDeviceList
= NULL
;
2189 alcCaptureDeviceListSize
= 0;
2191 free(alcDefaultDeviceSpecifier
);
2192 alcDefaultDeviceSpecifier
= NULL
;
2193 free(alcDefaultAllDeviceSpecifier
);
2194 alcDefaultAllDeviceSpecifier
= NULL
;
2195 free(alcCaptureDefaultDeviceSpecifier
);
2196 alcCaptureDefaultDeviceSpecifier
= NULL
;
2199 if(g_ulDeviceCount
> 0)
2200 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount
, (g_ulDeviceCount
>1)?"s":"");
2203 while(g_pDeviceList
)
2205 if(g_pDeviceList
->IsCaptureDevice
)
2206 alcCaptureCloseDevice(g_pDeviceList
);
2208 alcCloseDevice(g_pDeviceList
);
2212 ///////////////////////////////////////////////////////