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 }
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 { "wave", alc_wave_init
, alc_wave_deinit
, alc_wave_probe
, EmptyFuncs
},
75 { NULL
, NULL
, NULL
, NULL
, EmptyFuncs
}
79 ///////////////////////////////////////////////////////
81 #define ALC_EFX_MAJOR_VERSION 0x20001
82 #define ALC_EFX_MINOR_VERSION 0x20002
83 #define ALC_MAX_AUXILIARY_SENDS 0x20003
85 ///////////////////////////////////////////////////////
86 // STRING and EXTENSIONS
88 typedef struct ALCfunction
{
89 const ALCchar
*funcName
;
93 typedef struct ALCenums
{
94 const ALCchar
*enumName
;
99 static const ALCfunction alcFunctions
[] = {
100 { "alcCreateContext", (ALCvoid
*) alcCreateContext
},
101 { "alcMakeContextCurrent", (ALCvoid
*) alcMakeContextCurrent
},
102 { "alcProcessContext", (ALCvoid
*) alcProcessContext
},
103 { "alcSuspendContext", (ALCvoid
*) alcSuspendContext
},
104 { "alcDestroyContext", (ALCvoid
*) alcDestroyContext
},
105 { "alcGetCurrentContext", (ALCvoid
*) alcGetCurrentContext
},
106 { "alcGetContextsDevice", (ALCvoid
*) alcGetContextsDevice
},
107 { "alcOpenDevice", (ALCvoid
*) alcOpenDevice
},
108 { "alcCloseDevice", (ALCvoid
*) alcCloseDevice
},
109 { "alcGetError", (ALCvoid
*) alcGetError
},
110 { "alcIsExtensionPresent", (ALCvoid
*) alcIsExtensionPresent
},
111 { "alcGetProcAddress", (ALCvoid
*) alcGetProcAddress
},
112 { "alcGetEnumValue", (ALCvoid
*) alcGetEnumValue
},
113 { "alcGetString", (ALCvoid
*) alcGetString
},
114 { "alcGetIntegerv", (ALCvoid
*) alcGetIntegerv
},
115 { "alcCaptureOpenDevice", (ALCvoid
*) alcCaptureOpenDevice
},
116 { "alcCaptureCloseDevice", (ALCvoid
*) alcCaptureCloseDevice
},
117 { "alcCaptureStart", (ALCvoid
*) alcCaptureStart
},
118 { "alcCaptureStop", (ALCvoid
*) alcCaptureStop
},
119 { "alcCaptureSamples", (ALCvoid
*) alcCaptureSamples
},
121 { "alcSetThreadContext", (ALCvoid
*) alcSetThreadContext
},
122 { "alcGetThreadContext", (ALCvoid
*) alcGetThreadContext
},
124 { "alEnable", (ALCvoid
*) alEnable
},
125 { "alDisable", (ALCvoid
*) alDisable
},
126 { "alIsEnabled", (ALCvoid
*) alIsEnabled
},
127 { "alGetString", (ALCvoid
*) alGetString
},
128 { "alGetBooleanv", (ALCvoid
*) alGetBooleanv
},
129 { "alGetIntegerv", (ALCvoid
*) alGetIntegerv
},
130 { "alGetFloatv", (ALCvoid
*) alGetFloatv
},
131 { "alGetDoublev", (ALCvoid
*) alGetDoublev
},
132 { "alGetBoolean", (ALCvoid
*) alGetBoolean
},
133 { "alGetInteger", (ALCvoid
*) alGetInteger
},
134 { "alGetFloat", (ALCvoid
*) alGetFloat
},
135 { "alGetDouble", (ALCvoid
*) alGetDouble
},
136 { "alGetError", (ALCvoid
*) alGetError
},
137 { "alIsExtensionPresent", (ALCvoid
*) alIsExtensionPresent
},
138 { "alGetProcAddress", (ALCvoid
*) alGetProcAddress
},
139 { "alGetEnumValue", (ALCvoid
*) alGetEnumValue
},
140 { "alListenerf", (ALCvoid
*) alListenerf
},
141 { "alListener3f", (ALCvoid
*) alListener3f
},
142 { "alListenerfv", (ALCvoid
*) alListenerfv
},
143 { "alListeneri", (ALCvoid
*) alListeneri
},
144 { "alListener3i", (ALCvoid
*) alListener3i
},
145 { "alListeneriv", (ALCvoid
*) alListeneriv
},
146 { "alGetListenerf", (ALCvoid
*) alGetListenerf
},
147 { "alGetListener3f", (ALCvoid
*) alGetListener3f
},
148 { "alGetListenerfv", (ALCvoid
*) alGetListenerfv
},
149 { "alGetListeneri", (ALCvoid
*) alGetListeneri
},
150 { "alGetListener3i", (ALCvoid
*) alGetListener3i
},
151 { "alGetListeneriv", (ALCvoid
*) alGetListeneriv
},
152 { "alGenSources", (ALCvoid
*) alGenSources
},
153 { "alDeleteSources", (ALCvoid
*) alDeleteSources
},
154 { "alIsSource", (ALCvoid
*) alIsSource
},
155 { "alSourcef", (ALCvoid
*) alSourcef
},
156 { "alSource3f", (ALCvoid
*) alSource3f
},
157 { "alSourcefv", (ALCvoid
*) alSourcefv
},
158 { "alSourcei", (ALCvoid
*) alSourcei
},
159 { "alSource3i", (ALCvoid
*) alSource3i
},
160 { "alSourceiv", (ALCvoid
*) alSourceiv
},
161 { "alGetSourcef", (ALCvoid
*) alGetSourcef
},
162 { "alGetSource3f", (ALCvoid
*) alGetSource3f
},
163 { "alGetSourcefv", (ALCvoid
*) alGetSourcefv
},
164 { "alGetSourcei", (ALCvoid
*) alGetSourcei
},
165 { "alGetSource3i", (ALCvoid
*) alGetSource3i
},
166 { "alGetSourceiv", (ALCvoid
*) alGetSourceiv
},
167 { "alSourcePlayv", (ALCvoid
*) alSourcePlayv
},
168 { "alSourceStopv", (ALCvoid
*) alSourceStopv
},
169 { "alSourceRewindv", (ALCvoid
*) alSourceRewindv
},
170 { "alSourcePausev", (ALCvoid
*) alSourcePausev
},
171 { "alSourcePlay", (ALCvoid
*) alSourcePlay
},
172 { "alSourceStop", (ALCvoid
*) alSourceStop
},
173 { "alSourceRewind", (ALCvoid
*) alSourceRewind
},
174 { "alSourcePause", (ALCvoid
*) alSourcePause
},
175 { "alSourceQueueBuffers", (ALCvoid
*) alSourceQueueBuffers
},
176 { "alSourceUnqueueBuffers", (ALCvoid
*) alSourceUnqueueBuffers
},
177 { "alGenBuffers", (ALCvoid
*) alGenBuffers
},
178 { "alDeleteBuffers", (ALCvoid
*) alDeleteBuffers
},
179 { "alIsBuffer", (ALCvoid
*) alIsBuffer
},
180 { "alBufferData", (ALCvoid
*) alBufferData
},
181 { "alBufferf", (ALCvoid
*) alBufferf
},
182 { "alBuffer3f", (ALCvoid
*) alBuffer3f
},
183 { "alBufferfv", (ALCvoid
*) alBufferfv
},
184 { "alBufferi", (ALCvoid
*) alBufferi
},
185 { "alBuffer3i", (ALCvoid
*) alBuffer3i
},
186 { "alBufferiv", (ALCvoid
*) alBufferiv
},
187 { "alGetBufferf", (ALCvoid
*) alGetBufferf
},
188 { "alGetBuffer3f", (ALCvoid
*) alGetBuffer3f
},
189 { "alGetBufferfv", (ALCvoid
*) alGetBufferfv
},
190 { "alGetBufferi", (ALCvoid
*) alGetBufferi
},
191 { "alGetBuffer3i", (ALCvoid
*) alGetBuffer3i
},
192 { "alGetBufferiv", (ALCvoid
*) alGetBufferiv
},
193 { "alDopplerFactor", (ALCvoid
*) alDopplerFactor
},
194 { "alDopplerVelocity", (ALCvoid
*) alDopplerVelocity
},
195 { "alSpeedOfSound", (ALCvoid
*) alSpeedOfSound
},
196 { "alDistanceModel", (ALCvoid
*) alDistanceModel
},
198 { "alGenFilters", (ALCvoid
*) alGenFilters
},
199 { "alDeleteFilters", (ALCvoid
*) alDeleteFilters
},
200 { "alIsFilter", (ALCvoid
*) alIsFilter
},
201 { "alFilteri", (ALCvoid
*) alFilteri
},
202 { "alFilteriv", (ALCvoid
*) alFilteriv
},
203 { "alFilterf", (ALCvoid
*) alFilterf
},
204 { "alFilterfv", (ALCvoid
*) alFilterfv
},
205 { "alGetFilteri", (ALCvoid
*) alGetFilteri
},
206 { "alGetFilteriv", (ALCvoid
*) alGetFilteriv
},
207 { "alGetFilterf", (ALCvoid
*) alGetFilterf
},
208 { "alGetFilterfv", (ALCvoid
*) alGetFilterfv
},
210 { "alGenEffects", (ALCvoid
*) alGenEffects
},
211 { "alDeleteEffects", (ALCvoid
*) alDeleteEffects
},
212 { "alIsEffect", (ALCvoid
*) alIsEffect
},
213 { "alEffecti", (ALCvoid
*) alEffecti
},
214 { "alEffectiv", (ALCvoid
*) alEffectiv
},
215 { "alEffectf", (ALCvoid
*) alEffectf
},
216 { "alEffectfv", (ALCvoid
*) alEffectfv
},
217 { "alGetEffecti", (ALCvoid
*) alGetEffecti
},
218 { "alGetEffectiv", (ALCvoid
*) alGetEffectiv
},
219 { "alGetEffectf", (ALCvoid
*) alGetEffectf
},
220 { "alGetEffectfv", (ALCvoid
*) alGetEffectfv
},
222 { "alGenAuxiliaryEffectSlots", (ALCvoid
*) alGenAuxiliaryEffectSlots
},
223 { "alDeleteAuxiliaryEffectSlots",(ALCvoid
*) alDeleteAuxiliaryEffectSlots
},
224 { "alIsAuxiliaryEffectSlot", (ALCvoid
*) alIsAuxiliaryEffectSlot
},
225 { "alAuxiliaryEffectSloti", (ALCvoid
*) alAuxiliaryEffectSloti
},
226 { "alAuxiliaryEffectSlotiv", (ALCvoid
*) alAuxiliaryEffectSlotiv
},
227 { "alAuxiliaryEffectSlotf", (ALCvoid
*) alAuxiliaryEffectSlotf
},
228 { "alAuxiliaryEffectSlotfv", (ALCvoid
*) alAuxiliaryEffectSlotfv
},
229 { "alGetAuxiliaryEffectSloti", (ALCvoid
*) alGetAuxiliaryEffectSloti
},
230 { "alGetAuxiliaryEffectSlotiv", (ALCvoid
*) alGetAuxiliaryEffectSlotiv
},
231 { "alGetAuxiliaryEffectSlotf", (ALCvoid
*) alGetAuxiliaryEffectSlotf
},
232 { "alGetAuxiliaryEffectSlotfv", (ALCvoid
*) alGetAuxiliaryEffectSlotfv
},
234 { "alBufferSubDataEXT", (ALCvoid
*) alBufferSubDataEXT
},
236 { "alGenDatabuffersEXT", (ALCvoid
*) alGenDatabuffersEXT
},
237 { "alDeleteDatabuffersEXT", (ALCvoid
*) alDeleteDatabuffersEXT
},
238 { "alIsDatabufferEXT", (ALCvoid
*) alIsDatabufferEXT
},
239 { "alDatabufferDataEXT", (ALCvoid
*) alDatabufferDataEXT
},
240 { "alDatabufferSubDataEXT", (ALCvoid
*) alDatabufferSubDataEXT
},
241 { "alGetDatabufferSubDataEXT", (ALCvoid
*) alGetDatabufferSubDataEXT
},
242 { "alDatabufferfEXT", (ALCvoid
*) alDatabufferfEXT
},
243 { "alDatabufferfvEXT", (ALCvoid
*) alDatabufferfvEXT
},
244 { "alDatabufferiEXT", (ALCvoid
*) alDatabufferiEXT
},
245 { "alDatabufferivEXT", (ALCvoid
*) alDatabufferivEXT
},
246 { "alGetDatabufferfEXT", (ALCvoid
*) alGetDatabufferfEXT
},
247 { "alGetDatabufferfvEXT", (ALCvoid
*) alGetDatabufferfvEXT
},
248 { "alGetDatabufferiEXT", (ALCvoid
*) alGetDatabufferiEXT
},
249 { "alGetDatabufferivEXT", (ALCvoid
*) alGetDatabufferivEXT
},
250 { "alSelectDatabufferEXT", (ALCvoid
*) alSelectDatabufferEXT
},
251 { "alMapDatabufferEXT", (ALCvoid
*) alMapDatabufferEXT
},
252 { "alUnmapDatabufferEXT", (ALCvoid
*) alUnmapDatabufferEXT
},
254 { NULL
, (ALCvoid
*) NULL
}
257 static const ALCenums enumeration
[] = {
259 { "ALC_INVALID", ALC_INVALID
},
260 { "ALC_FALSE", ALC_FALSE
},
261 { "ALC_TRUE", ALC_TRUE
},
264 { "ALC_MAJOR_VERSION", ALC_MAJOR_VERSION
},
265 { "ALC_MINOR_VERSION", ALC_MINOR_VERSION
},
266 { "ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE
},
267 { "ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES
},
268 { "ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER
},
269 { "ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER
},
270 { "ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER
},
271 { "ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER
},
272 { "ALC_EXTENSIONS", ALC_EXTENSIONS
},
273 { "ALC_FREQUENCY", ALC_FREQUENCY
},
274 { "ALC_REFRESH", ALC_REFRESH
},
275 { "ALC_SYNC", ALC_SYNC
},
276 { "ALC_MONO_SOURCES", ALC_MONO_SOURCES
},
277 { "ALC_STEREO_SOURCES", ALC_STEREO_SOURCES
},
278 { "ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER
},
279 { "ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
},
280 { "ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES
},
281 { "ALC_CONNECTED", ALC_CONNECTED
},
284 { "ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION
},
285 { "ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION
},
286 { "ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS
},
289 { "ALC_NO_ERROR", ALC_NO_ERROR
},
290 { "ALC_INVALID_DEVICE", ALC_INVALID_DEVICE
},
291 { "ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT
},
292 { "ALC_INVALID_ENUM", ALC_INVALID_ENUM
},
293 { "ALC_INVALID_VALUE", ALC_INVALID_VALUE
},
294 { "ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY
},
298 static const ALCchar alcNoError
[] = "No Error";
299 static const ALCchar alcErrInvalidDevice
[] = "Invalid Device";
300 static const ALCchar alcErrInvalidContext
[] = "Invalid Context";
301 static const ALCchar alcErrInvalidEnum
[] = "Invalid Enum";
302 static const ALCchar alcErrInvalidValue
[] = "Invalid Value";
303 static const ALCchar alcErrOutOfMemory
[] = "Out of Memory";
305 /* Device lists. Sizes only include the first ending null character, not the
307 static ALCchar
*alcDeviceList
;
308 static size_t alcDeviceListSize
;
309 static ALCchar
*alcAllDeviceList
;
310 static size_t alcAllDeviceListSize
;
311 static ALCchar
*alcCaptureDeviceList
;
312 static size_t alcCaptureDeviceListSize
;
313 // Default is always the first in the list
314 static ALCchar
*alcDefaultDeviceSpecifier
;
315 static ALCchar
*alcDefaultAllDeviceSpecifier
;
316 static ALCchar
*alcCaptureDefaultDeviceSpecifier
;
319 static const ALCchar alcNoDeviceExtList
[] =
320 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
321 "ALC_EXT_thread_local_context";
322 static const ALCchar alcExtensionList
[] =
323 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
324 "ALC_EXT_disconnect ALC_EXT_EFX ALC_EXT_thread_local_context";
325 static const ALCint alcMajorVersion
= 1;
326 static const ALCint alcMinorVersion
= 1;
328 static const ALCint alcEFXMajorVersion
= 1;
329 static const ALCint alcEFXMinorVersion
= 0;
331 ///////////////////////////////////////////////////////
334 ///////////////////////////////////////////////////////
337 static ALCdevice
*g_pDeviceList
= NULL
;
338 static ALCuint g_ulDeviceCount
= 0;
340 static CRITICAL_SECTION g_csMutex
;
343 static ALCcontext
*g_pContextList
= NULL
;
344 static ALCuint g_ulContextCount
= 0;
346 // Thread-local current context
347 static tls_type LocalContext
;
348 // Process-wide current context
349 static ALCcontext
*GlobalContext
;
352 static ALCenum g_eLastNullDeviceError
= ALC_NO_ERROR
;
354 // Default context extensions
355 static const ALchar alExtList
[] =
356 "AL_EXTX_buffer_sub_data AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE "
357 "AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXTX_loop_points "
358 "AL_EXT_MCFORMATS AL_EXT_MULAW AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET "
359 "AL_EXTX_sample_buffer_object AL_EXT_source_distance_model "
360 "AL_LOKI_quadriphonic";
362 // Mixing Priority Level
363 static ALint RTPrioLevel
;
366 resampler_t DefaultResampler
;
368 ///////////////////////////////////////////////////////
371 ///////////////////////////////////////////////////////
372 // ALC Related helper functions
374 static void alc_init(void);
375 static void alc_deinit(void);
377 BOOL APIENTRY
DllMain(HANDLE hModule
,DWORD ul_reason_for_call
,LPVOID lpReserved
)
381 // Perform actions based on the reason for calling.
382 switch(ul_reason_for_call
)
384 case DLL_PROCESS_ATTACH
:
385 DisableThreadLibraryCalls(hModule
);
389 case DLL_PROCESS_DETACH
:
396 #ifdef HAVE_GCC_DESTRUCTOR
397 static void alc_init(void) __attribute__((constructor
));
398 static void alc_deinit(void) __attribute__((destructor
));
402 static void alc_init(void)
405 const char *devs
, *str
;
407 InitializeCriticalSection(&g_csMutex
);
411 tls_create(&LocalContext
);
413 RTPrioLevel
= GetConfigValueInt(NULL
, "rt-prio", 0);
415 DefaultResampler
= GetConfigValueInt(NULL
, "resampler", RESAMPLER_DEFAULT
);
416 if(DefaultResampler
>= RESAMPLER_MAX
|| DefaultResampler
<= RESAMPLER_MIN
)
417 DefaultResampler
= RESAMPLER_DEFAULT
;
419 devs
= GetConfigValue(NULL
, "drivers", "");
424 const char *next
= devs
;
430 next
= strchr(devs
, ',');
432 if(!devs
[0] || devs
[0] == ',')
439 len
= (next
? ((size_t)(next
-devs
)) : strlen(devs
));
440 for(n
= i
;BackendList
[n
].Init
;n
++)
442 if(len
== strlen(BackendList
[n
].name
) &&
443 strncmp(BackendList
[n
].name
, devs
, len
) == 0)
445 BackendInfo Bkp
= BackendList
[n
];
448 BackendList
[n
] = BackendList
[n
-1];
451 BackendList
[n
] = Bkp
;
461 BackendList
[i
].name
= NULL
;
462 BackendList
[i
].Init
= NULL
;
463 BackendList
[i
].Deinit
= NULL
;
464 BackendList
[i
].Probe
= NULL
;
468 for(i
= 0;BackendList
[i
].Init
;i
++)
469 BackendList
[i
].Init(&BackendList
[i
].Funcs
);
471 str
= GetConfigValue(NULL
, "excludefx", "");
478 { "eaxreverb", EAXREVERB
},
479 { "reverb", REVERB
},
481 { "modulator", MODULATOR
},
486 const char *next
= str
;
490 next
= strchr(str
, ',');
492 if(!str
[0] || next
== str
)
495 len
= (next
? ((size_t)(next
-str
)) : strlen(str
));
496 for(n
= 0;EffectList
[n
].name
;n
++)
498 if(len
== strlen(EffectList
[n
].name
) &&
499 strncmp(EffectList
[n
].name
, str
, len
) == 0)
500 DisabledEffects
[EffectList
[n
].type
] = AL_TRUE
;
506 static void alc_deinit(void)
512 for(i
= 0;BackendList
[i
].Deinit
;i
++)
513 BackendList
[i
].Deinit();
515 tls_delete(LocalContext
);
519 DeleteCriticalSection(&g_csMutex
);
523 static void ProbeDeviceList()
527 free(alcDeviceList
); alcDeviceList
= NULL
;
528 alcDeviceListSize
= 0;
530 for(i
= 0;BackendList
[i
].Probe
;i
++)
531 BackendList
[i
].Probe(DEVICE_PROBE
);
534 static void ProbeAllDeviceList()
538 free(alcAllDeviceList
); alcAllDeviceList
= NULL
;
539 alcAllDeviceListSize
= 0;
541 for(i
= 0;BackendList
[i
].Probe
;i
++)
542 BackendList
[i
].Probe(ALL_DEVICE_PROBE
);
545 static void ProbeCaptureDeviceList()
549 free(alcCaptureDeviceList
); alcCaptureDeviceList
= NULL
;
550 alcCaptureDeviceListSize
= 0;
552 for(i
= 0;BackendList
[i
].Probe
;i
++)
553 BackendList
[i
].Probe(CAPTURE_DEVICE_PROBE
);
557 #define DECL_APPEND_LIST_FUNC(type) \
558 void Append##type##List(const ALCchar *name) \
560 size_t len = strlen(name); \
566 temp = realloc(alc##type##List, alc##type##ListSize + len + 2); \
569 AL_PRINT("Realloc failed to add %s!\n", name); \
572 alc##type##List = temp; \
573 sprintf(alc##type##List+alc##type##ListSize, "%s", name); \
574 alc##type##ListSize += len+1; \
575 alc##type##List[alc##type##ListSize] = 0; \
578 DECL_APPEND_LIST_FUNC(Device
)
579 DECL_APPEND_LIST_FUNC(AllDevice
)
580 DECL_APPEND_LIST_FUNC(CaptureDevice
)
583 void al_print(const char *fname
, unsigned int line
, const char *fmt
, ...)
589 fn
= strrchr(fname
, '/');
590 if(!fn
) fn
= strrchr(fname
, '\\');;
594 i
= snprintf(str
, sizeof(str
), "AL lib: %s:%d: ", fn
, line
);
595 if(i
< (int)sizeof(str
) && i
> 0)
599 vsnprintf(str
+i
, sizeof(str
)-i
, fmt
, ap
);
602 str
[sizeof(str
)-1] = 0;
604 fprintf(stderr
, "%s", str
);
607 void SetRTPriority(void)
613 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL
);
615 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL
);
616 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM)
617 struct sched_param param
;
621 /* Use the minimum real-time priority possible for now (on Linux this
622 * should be 1 for SCHED_RR) */
623 param
.sched_priority
= sched_get_priority_min(SCHED_RR
);
624 failed
= !!pthread_setschedparam(pthread_self(), SCHED_RR
, ¶m
);
628 param
.sched_priority
= 0;
629 failed
= !!pthread_setschedparam(pthread_self(), SCHED_OTHER
, ¶m
);
632 /* Real-time priority not available */
633 failed
= (RTPrioLevel
>0);
636 AL_PRINT("Failed to set priority level for thread\n");
640 void InitUIntMap(UIntMap
*map
)
647 void ResetUIntMap(UIntMap
*map
)
655 ALenum
InsertUIntMapEntry(UIntMap
*map
, ALuint key
, ALvoid
*value
)
662 ALsizei high
= map
->size
- 1;
665 ALsizei mid
= low
+ (high
-low
)/2;
666 if(map
->array
[mid
].key
< key
)
671 if(map
->array
[low
].key
< key
)
676 if(pos
== map
->size
|| map
->array
[pos
].key
!= key
)
678 if(map
->size
== map
->maxsize
)
683 newsize
= (map
->maxsize
? (map
->maxsize
<<1) : 4);
684 if(newsize
< map
->maxsize
)
685 return AL_OUT_OF_MEMORY
;
687 temp
= realloc(map
->array
, newsize
*sizeof(map
->array
[0]));
688 if(!temp
) return AL_OUT_OF_MEMORY
;
690 map
->maxsize
= newsize
;
694 if(pos
< map
->size
-1)
695 memmove(&map
->array
[pos
+1], &map
->array
[pos
],
696 (map
->size
-1-pos
)*sizeof(map
->array
[0]));
698 map
->array
[pos
].key
= key
;
699 map
->array
[pos
].value
= value
;
704 void RemoveUIntMapKey(UIntMap
*map
, ALuint key
)
709 ALsizei high
= map
->size
- 1;
712 ALsizei mid
= low
+ (high
-low
)/2;
713 if(map
->array
[mid
].key
< key
)
718 if(map
->array
[low
].key
== key
)
720 if(low
< map
->size
-1)
721 memmove(&map
->array
[low
], &map
->array
[low
+1],
722 (map
->size
-1-low
)*sizeof(map
->array
[0]));
732 Check pDevice is a valid Device pointer
734 static ALCboolean
IsDevice(ALCdevice
*pDevice
)
736 ALCdevice
*pTempDevice
;
738 SuspendContext(NULL
);
740 pTempDevice
= g_pDeviceList
;
741 while(pTempDevice
&& pTempDevice
!= pDevice
)
742 pTempDevice
= pTempDevice
->next
;
744 ProcessContext(NULL
);
746 return (pTempDevice
? ALC_TRUE
: ALC_FALSE
);
752 Check pContext is a valid Context pointer
754 static ALCboolean
IsContext(ALCcontext
*pContext
)
756 ALCcontext
*pTempContext
;
758 SuspendContext(NULL
);
760 pTempContext
= g_pContextList
;
761 while (pTempContext
&& pTempContext
!= pContext
)
762 pTempContext
= pTempContext
->next
;
764 ProcessContext(NULL
);
766 return (pTempContext
? ALC_TRUE
: ALC_FALSE
);
773 Store latest ALC Error
775 ALCvoid
alcSetError(ALCdevice
*device
, ALenum errorCode
)
778 device
->LastError
= errorCode
;
780 g_eLastNullDeviceError
= errorCode
;
789 ALCvoid
SuspendContext(ALCcontext
*pContext
)
792 EnterCriticalSection(&g_csMutex
);
801 ALCvoid
ProcessContext(ALCcontext
*pContext
)
804 LeaveCriticalSection(&g_csMutex
);
811 Returns the currently active Context, in a locked state
813 ALCcontext
*GetContextSuspended(void)
815 ALCcontext
*pContext
= NULL
;
817 SuspendContext(NULL
);
819 pContext
= tls_get(LocalContext
);
820 if(pContext
&& !IsContext(pContext
))
822 tls_set(LocalContext
, NULL
);
826 pContext
= GlobalContext
;
829 SuspendContext(pContext
);
831 ProcessContext(NULL
);
840 Initialize Context variables
842 static ALvoid
InitContext(ALCcontext
*pContext
)
844 //Initialise listener
845 pContext
->Listener
.Gain
= 1.0f
;
846 pContext
->Listener
.MetersPerUnit
= 1.0f
;
847 pContext
->Listener
.Position
[0] = 0.0f
;
848 pContext
->Listener
.Position
[1] = 0.0f
;
849 pContext
->Listener
.Position
[2] = 0.0f
;
850 pContext
->Listener
.Velocity
[0] = 0.0f
;
851 pContext
->Listener
.Velocity
[1] = 0.0f
;
852 pContext
->Listener
.Velocity
[2] = 0.0f
;
853 pContext
->Listener
.Forward
[0] = 0.0f
;
854 pContext
->Listener
.Forward
[1] = 0.0f
;
855 pContext
->Listener
.Forward
[2] = -1.0f
;
856 pContext
->Listener
.Up
[0] = 0.0f
;
857 pContext
->Listener
.Up
[1] = 1.0f
;
858 pContext
->Listener
.Up
[2] = 0.0f
;
861 pContext
->LastError
= AL_NO_ERROR
;
862 pContext
->Suspended
= AL_FALSE
;
863 InitUIntMap(&pContext
->SourceMap
);
866 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
867 pContext
->SourceDistanceModel
= AL_FALSE
;
868 pContext
->DopplerFactor
= 1.0f
;
869 pContext
->DopplerVelocity
= 1.0f
;
870 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
872 pContext
->ExtensionList
= alExtList
;
879 Clean up Context, destroy any remaining Sources
881 static ALCvoid
ExitContext(ALCcontext
*pContext
)
884 pContext
->LastError
= AL_NO_ERROR
;
887 ///////////////////////////////////////////////////////
890 ///////////////////////////////////////////////////////
891 // ALC Functions calls
894 // This should probably move to another c file but for now ...
895 ALC_API ALCdevice
* ALC_APIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
897 ALCboolean DeviceFound
= ALC_FALSE
;
898 ALCdevice
*device
= NULL
;
903 alcSetError(NULL
, ALC_INVALID_VALUE
);
907 if(deviceName
&& !deviceName
[0])
910 device
= calloc(1, sizeof(ALCdevice
));
913 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
918 device
->Connected
= ALC_TRUE
;
919 device
->IsCaptureDevice
= AL_TRUE
;
921 device
->szDeviceName
= NULL
;
923 device
->Frequency
= frequency
;
924 device
->Format
= format
;
925 device
->UpdateSize
= SampleSize
;
926 device
->NumUpdates
= 1;
928 SuspendContext(NULL
);
929 for(i
= 0;BackendList
[i
].Init
;i
++)
931 device
->Funcs
= &BackendList
[i
].Funcs
;
932 if(ALCdevice_OpenCapture(device
, deviceName
))
934 device
->next
= g_pDeviceList
;
935 g_pDeviceList
= device
;
938 DeviceFound
= ALC_TRUE
;
942 ProcessContext(NULL
);
946 alcSetError(NULL
, ALC_INVALID_VALUE
);
954 ALC_API ALCboolean ALC_APIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
958 if(!IsDevice(pDevice
) || !pDevice
->IsCaptureDevice
)
960 alcSetError(pDevice
, ALC_INVALID_DEVICE
);
964 SuspendContext(NULL
);
966 list
= &g_pDeviceList
;
967 while(*list
!= pDevice
)
968 list
= &(*list
)->next
;
970 *list
= (*list
)->next
;
973 ProcessContext(NULL
);
975 ALCdevice_CloseCapture(pDevice
);
977 free(pDevice
->szDeviceName
);
978 pDevice
->szDeviceName
= NULL
;
985 ALC_API
void ALC_APIENTRY
alcCaptureStart(ALCdevice
*device
)
987 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
989 alcSetError(device
, ALC_INVALID_DEVICE
);
992 SuspendContext(NULL
);
993 ALCdevice_StartCapture(device
);
994 ProcessContext(NULL
);
997 ALC_API
void ALC_APIENTRY
alcCaptureStop(ALCdevice
*device
)
999 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
1001 alcSetError(device
, ALC_INVALID_DEVICE
);
1004 SuspendContext(NULL
);
1005 ALCdevice_StopCapture(device
);
1006 ProcessContext(NULL
);
1009 ALC_API
void ALC_APIENTRY
alcCaptureSamples(ALCdevice
*device
, ALCvoid
*buffer
, ALCsizei samples
)
1011 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
1013 alcSetError(device
, ALC_INVALID_DEVICE
);
1016 SuspendContext(NULL
);
1017 ALCdevice_CaptureSamples(device
, buffer
, samples
);
1018 ProcessContext(NULL
);
1024 Return last ALC generated error code
1026 ALC_API ALCenum ALC_APIENTRY
alcGetError(ALCdevice
*device
)
1030 if(IsDevice(device
))
1032 errorCode
= device
->LastError
;
1033 device
->LastError
= ALC_NO_ERROR
;
1037 errorCode
= g_eLastNullDeviceError
;
1038 g_eLastNullDeviceError
= ALC_NO_ERROR
;
1049 ALC_API ALCvoid ALC_APIENTRY
alcSuspendContext(ALCcontext
*pContext
)
1051 SuspendContext(NULL
);
1052 if(IsContext(pContext
))
1053 pContext
->Suspended
= AL_TRUE
;
1054 ProcessContext(NULL
);
1063 ALC_API ALCvoid ALC_APIENTRY
alcProcessContext(ALCcontext
*pContext
)
1065 SuspendContext(NULL
);
1066 if(IsContext(pContext
))
1067 pContext
->Suspended
= AL_FALSE
;
1068 ProcessContext(NULL
);
1075 Returns information about the Device, and error strings
1077 ALC_API
const ALCchar
* ALC_APIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
1079 const ALCchar
*value
= NULL
;
1087 case ALC_INVALID_ENUM
:
1088 value
= alcErrInvalidEnum
;
1091 case ALC_INVALID_VALUE
:
1092 value
= alcErrInvalidValue
;
1095 case ALC_INVALID_DEVICE
:
1096 value
= alcErrInvalidDevice
;
1099 case ALC_INVALID_CONTEXT
:
1100 value
= alcErrInvalidContext
;
1103 case ALC_OUT_OF_MEMORY
:
1104 value
= alcErrOutOfMemory
;
1107 case ALC_DEVICE_SPECIFIER
:
1108 if(IsDevice(pDevice
))
1109 value
= pDevice
->szDeviceName
;
1113 value
= alcDeviceList
;
1117 case ALC_ALL_DEVICES_SPECIFIER
:
1118 ProbeAllDeviceList();
1119 value
= alcAllDeviceList
;
1122 case ALC_CAPTURE_DEVICE_SPECIFIER
:
1123 if(IsDevice(pDevice
))
1124 value
= pDevice
->szDeviceName
;
1127 ProbeCaptureDeviceList();
1128 value
= alcCaptureDeviceList
;
1132 /* Default devices are always first in the list */
1133 case ALC_DEFAULT_DEVICE_SPECIFIER
:
1137 free(alcDefaultDeviceSpecifier
);
1138 alcDefaultDeviceSpecifier
= strdup(alcDeviceList
? alcDeviceList
: "");
1139 if(!alcDefaultDeviceSpecifier
)
1140 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1141 value
= alcDefaultDeviceSpecifier
;
1144 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
1145 if(!alcAllDeviceList
)
1146 ProbeAllDeviceList();
1148 free(alcDefaultAllDeviceSpecifier
);
1149 alcDefaultAllDeviceSpecifier
= strdup(alcAllDeviceList
?
1150 alcAllDeviceList
: "");
1151 if(!alcDefaultAllDeviceSpecifier
)
1152 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1153 value
= alcDefaultAllDeviceSpecifier
;
1156 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
1157 if(!alcCaptureDeviceList
)
1158 ProbeCaptureDeviceList();
1160 free(alcCaptureDefaultDeviceSpecifier
);
1161 alcCaptureDefaultDeviceSpecifier
= strdup(alcCaptureDeviceList
?
1162 alcCaptureDeviceList
: "");
1163 if(!alcCaptureDefaultDeviceSpecifier
)
1164 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1165 value
= alcCaptureDefaultDeviceSpecifier
;
1168 case ALC_EXTENSIONS
:
1169 if(IsDevice(pDevice
))
1170 value
= alcExtensionList
;
1172 value
= alcNoDeviceExtList
;
1176 alcSetError(pDevice
, ALC_INVALID_ENUM
);
1187 Returns information about the Device and the version of Open AL
1189 ALC_API ALCvoid ALC_APIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
1191 if(size
== 0 || data
== NULL
)
1193 alcSetError(device
, ALC_INVALID_VALUE
);
1197 if(IsDevice(device
) && device
->IsCaptureDevice
)
1199 SuspendContext(NULL
);
1204 case ALC_CAPTURE_SAMPLES
:
1205 *data
= ALCdevice_AvailableSamples(device
);
1209 *data
= device
->Connected
;
1213 alcSetError(device
, ALC_INVALID_ENUM
);
1217 ProcessContext(NULL
);
1224 case ALC_MAJOR_VERSION
:
1225 *data
= alcMajorVersion
;
1228 case ALC_MINOR_VERSION
:
1229 *data
= alcMinorVersion
;
1232 case ALC_EFX_MAJOR_VERSION
:
1233 *data
= alcEFXMajorVersion
;
1236 case ALC_EFX_MINOR_VERSION
:
1237 *data
= alcEFXMinorVersion
;
1240 case ALC_MAX_AUXILIARY_SENDS
:
1241 if(!IsDevice(device
))
1242 alcSetError(device
, ALC_INVALID_DEVICE
);
1244 *data
= device
->NumAuxSends
;
1247 case ALC_ATTRIBUTES_SIZE
:
1248 if(!IsDevice(device
))
1249 alcSetError(device
, ALC_INVALID_DEVICE
);
1254 case ALC_ALL_ATTRIBUTES
:
1255 if(!IsDevice(device
))
1256 alcSetError(device
, ALC_INVALID_DEVICE
);
1258 alcSetError(device
, ALC_INVALID_VALUE
);
1263 SuspendContext(NULL
);
1264 data
[i
++] = ALC_FREQUENCY
;
1265 data
[i
++] = device
->Frequency
;
1267 data
[i
++] = ALC_REFRESH
;
1268 data
[i
++] = device
->Frequency
/ device
->UpdateSize
;
1270 data
[i
++] = ALC_SYNC
;
1271 data
[i
++] = ALC_FALSE
;
1273 data
[i
++] = ALC_MONO_SOURCES
;
1274 data
[i
++] = device
->NumMonoSources
;
1276 data
[i
++] = ALC_STEREO_SOURCES
;
1277 data
[i
++] = device
->NumStereoSources
;
1279 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
1280 data
[i
++] = device
->NumAuxSends
;
1283 ProcessContext(NULL
);
1288 if(!IsDevice(device
))
1289 alcSetError(device
, ALC_INVALID_DEVICE
);
1291 *data
= device
->Frequency
;
1295 if(!IsDevice(device
))
1296 alcSetError(device
, ALC_INVALID_DEVICE
);
1298 *data
= device
->Frequency
/ device
->UpdateSize
;
1302 if(!IsDevice(device
))
1303 alcSetError(device
, ALC_INVALID_DEVICE
);
1308 case ALC_MONO_SOURCES
:
1309 if(!IsDevice(device
))
1310 alcSetError(device
, ALC_INVALID_DEVICE
);
1312 *data
= device
->NumMonoSources
;
1315 case ALC_STEREO_SOURCES
:
1316 if(!IsDevice(device
))
1317 alcSetError(device
, ALC_INVALID_DEVICE
);
1319 *data
= device
->NumStereoSources
;
1323 if(!IsDevice(device
))
1324 alcSetError(device
, ALC_INVALID_DEVICE
);
1326 *data
= device
->Connected
;
1330 alcSetError(device
, ALC_INVALID_ENUM
);
1337 alcIsExtensionPresent
1339 Determines if there is support for a particular extension
1341 ALC_API ALCboolean ALC_APIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
1343 ALCboolean bResult
= ALC_FALSE
;
1349 alcSetError(device
, ALC_INVALID_VALUE
);
1353 len
= strlen(extName
);
1354 ptr
= (IsDevice(device
) ? alcExtensionList
: alcNoDeviceExtList
);
1357 if(strncasecmp(ptr
, extName
, len
) == 0 &&
1358 (ptr
[len
] == '\0' || isspace(ptr
[len
])))
1363 if((ptr
=strchr(ptr
, ' ')) != NULL
)
1367 } while(isspace(*ptr
));
1378 Retrieves the function address for a particular extension function
1380 ALC_API ALCvoid
* ALC_APIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
1386 alcSetError(device
, ALC_INVALID_VALUE
);
1390 while(alcFunctions
[i
].funcName
&& strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
1392 return alcFunctions
[i
].address
;
1399 Get the value for a particular ALC Enumerated Value
1401 ALC_API ALCenum ALC_APIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
1407 alcSetError(device
, ALC_INVALID_VALUE
);
1411 while(enumeration
[i
].enumName
&& strcmp(enumeration
[i
].enumName
,enumName
) != 0)
1413 return enumeration
[i
].value
;
1420 Create and attach a Context to a particular Device.
1422 ALC_API ALCcontext
* ALC_APIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
1424 ALCcontext
*ALContext
;
1430 SuspendContext(NULL
);
1432 if(!IsDevice(device
) || device
->IsCaptureDevice
|| !device
->Connected
)
1434 alcSetError(device
, ALC_INVALID_DEVICE
);
1435 ProcessContext(NULL
);
1439 running
= ((device
->NumContexts
> 0) ? AL_TRUE
: AL_FALSE
);
1441 // Reset Context Last Error code
1442 device
->LastError
= ALC_NO_ERROR
;
1444 // Check for attributes
1445 if(attrList
&& attrList
[0])
1447 ALCuint freq
, numMono
, numStereo
, numSends
;
1449 // If a context is already running on the device, stop playback so the
1450 // device attributes can be updated
1453 ProcessContext(NULL
);
1454 ALCdevice_StopPlayback(device
);
1455 SuspendContext(NULL
);
1459 freq
= device
->Frequency
;
1460 numMono
= device
->NumMonoSources
;
1461 numStereo
= device
->NumStereoSources
;
1462 numSends
= device
->NumAuxSends
;
1465 while(attrList
[attrIdx
])
1467 if(attrList
[attrIdx
] == ALC_FREQUENCY
&&
1468 !ConfigValueExists(NULL
, "frequency"))
1470 freq
= attrList
[attrIdx
+ 1];
1475 if(attrList
[attrIdx
] == ALC_STEREO_SOURCES
)
1477 numStereo
= attrList
[attrIdx
+ 1];
1478 if(numStereo
> device
->MaxNoOfSources
)
1479 numStereo
= device
->MaxNoOfSources
;
1481 numMono
= device
->MaxNoOfSources
- numStereo
;
1484 if(attrList
[attrIdx
] == ALC_MAX_AUXILIARY_SENDS
&&
1485 !ConfigValueExists(NULL
, "sends"))
1487 numSends
= attrList
[attrIdx
+ 1];
1488 if(numSends
> MAX_SENDS
)
1489 numSends
= MAX_SENDS
;
1495 device
->UpdateSize
= (ALuint64
)device
->UpdateSize
* freq
/
1498 device
->Frequency
= freq
;
1499 device
->NumMonoSources
= numMono
;
1500 device
->NumStereoSources
= numStereo
;
1501 device
->NumAuxSends
= numSends
;
1504 if(running
== AL_FALSE
&& ALCdevice_ResetPlayback(device
) == ALC_FALSE
)
1506 alcSetError(device
, ALC_INVALID_DEVICE
);
1507 aluHandleDisconnect(device
);
1508 ProcessContext(NULL
);
1511 aluInitPanning(device
);
1513 for(i
= 0;i
< device
->NumContexts
;i
++)
1515 ALCcontext
*context
= device
->Contexts
[i
];
1518 SuspendContext(context
);
1519 for(pos
= 0;pos
< context
->EffectSlotMap
.size
;pos
++)
1521 ALeffectslot
*slot
= context
->EffectSlotMap
.array
[pos
].value
;
1522 if(!slot
->EffectState
)
1525 if(ALEffect_DeviceUpdate(slot
->EffectState
, device
) == AL_FALSE
)
1527 alcSetError(device
, ALC_INVALID_DEVICE
);
1528 aluHandleDisconnect(device
);
1529 ProcessContext(context
);
1530 ProcessContext(NULL
);
1531 ALCdevice_StopPlayback(device
);
1534 ALEffect_Update(slot
->EffectState
, context
, &slot
->effect
);
1537 for(pos
= 0;pos
< context
->SourceMap
.size
;pos
++)
1539 ALsource
*source
= context
->SourceMap
.array
[pos
].value
;
1540 ALuint s
= device
->NumAuxSends
;
1541 while(s
< MAX_SENDS
)
1543 if(source
->Send
[s
].Slot
)
1544 source
->Send
[s
].Slot
->refcount
--;
1545 source
->Send
[s
].Slot
= NULL
;
1546 source
->Send
[s
].WetFilter
.type
= 0;
1547 source
->Send
[s
].WetFilter
.filter
= 0;
1550 source
->NeedsUpdate
= AL_TRUE
;
1552 ProcessContext(context
);
1555 if(device
->Bs2bLevel
> 0 && device
->Bs2bLevel
<= 6)
1559 device
->Bs2b
= calloc(1, sizeof(*device
->Bs2b
));
1560 bs2b_clear(device
->Bs2b
);
1562 bs2b_set_srate(device
->Bs2b
, device
->Frequency
);
1563 bs2b_set_level(device
->Bs2b
, device
->Bs2bLevel
);
1568 device
->Bs2b
= NULL
;
1571 temp
= realloc(device
->Contexts
, (device
->NumContexts
+1) * sizeof(*device
->Contexts
));
1574 alcSetError(device
, ALC_OUT_OF_MEMORY
);
1575 ProcessContext(NULL
);
1578 device
->Contexts
= temp
;
1580 ALContext
= calloc(1, sizeof(ALCcontext
));
1583 alcSetError(device
, ALC_OUT_OF_MEMORY
);
1584 ProcessContext(NULL
);
1588 device
->Contexts
[device
->NumContexts
++] = ALContext
;
1589 ALContext
->Device
= device
;
1591 InitContext(ALContext
);
1593 ALContext
->next
= g_pContextList
;
1594 g_pContextList
= ALContext
;
1597 ProcessContext(NULL
);
1608 ALC_API ALCvoid ALC_APIENTRY
alcDestroyContext(ALCcontext
*context
)
1614 if(!IsContext(context
))
1616 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1620 Device
= context
->Device
;
1622 if(Device
->NumContexts
== 1)
1623 ALCdevice_StopPlayback(Device
);
1625 SuspendContext(NULL
);
1627 if(context
== GlobalContext
)
1628 GlobalContext
= NULL
;
1630 for(i
= 0;i
< Device
->NumContexts
-1;i
++)
1632 if(Device
->Contexts
[i
] == context
)
1634 Device
->Contexts
[i
] = Device
->Contexts
[Device
->NumContexts
-1];
1638 Device
->NumContexts
--;
1641 SuspendContext(context
);
1643 if(context
->SourceMap
.size
> 0)
1646 AL_PRINT("alcDestroyContext(): deleting %d Source(s)\n", context
->SourceMap
.size
);
1648 ReleaseALSources(context
);
1650 ResetUIntMap(&context
->SourceMap
);
1652 if(context
->EffectSlotMap
.size
> 0)
1655 AL_PRINT("alcDestroyContext(): deleting %d AuxiliaryEffectSlot(s)\n", context
->EffectSlotMap
.size
);
1657 ReleaseALAuxiliaryEffectSlots(context
);
1659 ResetUIntMap(&context
->EffectSlotMap
);
1661 list
= &g_pContextList
;
1662 while(*list
!= context
)
1663 list
= &(*list
)->next
;
1665 *list
= (*list
)->next
;
1669 ProcessContext(context
);
1670 ProcessContext(NULL
);
1672 ExitContext(context
);
1674 // Free memory (MUST do this after ProcessContext)
1675 memset(context
, 0, sizeof(ALCcontext
));
1681 alcGetCurrentContext
1683 Returns the currently active Context
1685 ALC_API ALCcontext
* ALC_APIENTRY
alcGetCurrentContext(ALCvoid
)
1687 ALCcontext
*pContext
;
1689 if((pContext
=GetContextSuspended()) != NULL
)
1690 ProcessContext(pContext
);
1698 Returns the currently active thread-local Context
1700 ALC_API ALCcontext
* ALC_APIENTRY
alcGetThreadContext(void)
1702 ALCcontext
*pContext
= NULL
;
1704 SuspendContext(NULL
);
1706 pContext
= tls_get(LocalContext
);
1707 if(pContext
&& !IsContext(pContext
))
1709 tls_set(LocalContext
, NULL
);
1713 ProcessContext(NULL
);
1720 alcGetContextsDevice
1722 Returns the Device that a particular Context is attached to
1724 ALC_API ALCdevice
* ALC_APIENTRY
alcGetContextsDevice(ALCcontext
*pContext
)
1726 ALCdevice
*pDevice
= NULL
;
1728 SuspendContext(NULL
);
1729 if(IsContext(pContext
))
1730 pDevice
= pContext
->Device
;
1732 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1733 ProcessContext(NULL
);
1740 alcMakeContextCurrent
1742 Makes the given Context the active Context
1744 ALC_API ALCboolean ALC_APIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
1746 ALboolean bReturn
= AL_TRUE
;
1748 SuspendContext(NULL
);
1750 // context must be a valid Context or NULL
1751 if(context
== NULL
|| IsContext(context
))
1753 GlobalContext
= context
;
1754 tls_set(LocalContext
, NULL
);
1758 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1762 ProcessContext(NULL
);
1770 Makes the given Context the active Context for the current thread
1772 ALC_API ALCboolean ALC_APIENTRY
alcSetThreadContext(ALCcontext
*context
)
1774 ALboolean bReturn
= AL_TRUE
;
1776 SuspendContext(NULL
);
1778 // context must be a valid Context or NULL
1779 if(context
== NULL
|| IsContext(context
))
1780 tls_set(LocalContext
, context
);
1783 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1787 ProcessContext(NULL
);
1793 // Sets the default channel order used by most non-WaveFormatEx-based APIs
1794 void SetDefaultChannelOrder(ALCdevice
*device
)
1796 switch(aluChannelsFromFormat(device
->Format
))
1798 case 1: device
->DevChannels
[FRONT_CENTER
] = 0; break;
1800 case 2: device
->DevChannels
[FRONT_LEFT
] = 0;
1801 device
->DevChannels
[FRONT_RIGHT
] = 1; break;
1803 case 4: device
->DevChannels
[FRONT_LEFT
] = 0;
1804 device
->DevChannels
[FRONT_RIGHT
] = 1;
1805 device
->DevChannels
[BACK_LEFT
] = 2;
1806 device
->DevChannels
[BACK_RIGHT
] = 3; break;
1808 case 6: device
->DevChannels
[FRONT_LEFT
] = 0;
1809 device
->DevChannels
[FRONT_RIGHT
] = 1;
1810 device
->DevChannels
[BACK_LEFT
] = 2;
1811 device
->DevChannels
[BACK_RIGHT
] = 3;
1812 device
->DevChannels
[FRONT_CENTER
] = 4;
1813 device
->DevChannels
[LFE
] = 5; break;
1815 case 7: device
->DevChannels
[FRONT_LEFT
] = 0;
1816 device
->DevChannels
[FRONT_RIGHT
] = 1;
1817 device
->DevChannels
[FRONT_CENTER
] = 2;
1818 device
->DevChannels
[LFE
] = 3;
1819 device
->DevChannels
[BACK_CENTER
] = 4;
1820 device
->DevChannels
[SIDE_LEFT
] = 5;
1821 device
->DevChannels
[SIDE_RIGHT
] = 6; break;
1823 case 8: device
->DevChannels
[FRONT_LEFT
] = 0;
1824 device
->DevChannels
[FRONT_RIGHT
] = 1;
1825 device
->DevChannels
[BACK_LEFT
] = 2;
1826 device
->DevChannels
[BACK_RIGHT
] = 3;
1827 device
->DevChannels
[FRONT_CENTER
] = 4;
1828 device
->DevChannels
[LFE
] = 5;
1829 device
->DevChannels
[SIDE_LEFT
] = 6;
1830 device
->DevChannels
[SIDE_RIGHT
] = 7; break;
1833 // Sets the default order used by WaveFormatEx
1834 void SetDefaultWFXChannelOrder(ALCdevice
*device
)
1836 switch(aluChannelsFromFormat(device
->Format
))
1838 case 1: device
->DevChannels
[FRONT_CENTER
] = 0; break;
1840 case 2: device
->DevChannels
[FRONT_LEFT
] = 0;
1841 device
->DevChannels
[FRONT_RIGHT
] = 1; break;
1843 case 4: device
->DevChannels
[FRONT_LEFT
] = 0;
1844 device
->DevChannels
[FRONT_RIGHT
] = 1;
1845 device
->DevChannels
[BACK_LEFT
] = 2;
1846 device
->DevChannels
[BACK_RIGHT
] = 3; break;
1848 case 6: device
->DevChannels
[FRONT_LEFT
] = 0;
1849 device
->DevChannels
[FRONT_RIGHT
] = 1;
1850 device
->DevChannels
[FRONT_CENTER
] = 2;
1851 device
->DevChannels
[LFE
] = 3;
1852 device
->DevChannels
[BACK_LEFT
] = 4;
1853 device
->DevChannels
[BACK_RIGHT
] = 5; break;
1855 case 7: device
->DevChannels
[FRONT_LEFT
] = 0;
1856 device
->DevChannels
[FRONT_RIGHT
] = 1;
1857 device
->DevChannels
[FRONT_CENTER
] = 2;
1858 device
->DevChannels
[LFE
] = 3;
1859 device
->DevChannels
[BACK_CENTER
] = 4;
1860 device
->DevChannels
[SIDE_LEFT
] = 5;
1861 device
->DevChannels
[SIDE_RIGHT
] = 6; break;
1863 case 8: device
->DevChannels
[FRONT_LEFT
] = 0;
1864 device
->DevChannels
[FRONT_RIGHT
] = 1;
1865 device
->DevChannels
[FRONT_CENTER
] = 2;
1866 device
->DevChannels
[LFE
] = 3;
1867 device
->DevChannels
[BACK_LEFT
] = 4;
1868 device
->DevChannels
[BACK_RIGHT
] = 5;
1869 device
->DevChannels
[SIDE_LEFT
] = 6;
1870 device
->DevChannels
[SIDE_RIGHT
] = 7; break;
1874 static ALenum
GetFormatFromString(const char *str
)
1876 if(strcasecmp(str
, "AL_FORMAT_MONO32") == 0) return AL_FORMAT_MONO_FLOAT32
;
1877 if(strcasecmp(str
, "AL_FORMAT_STEREO32") == 0) return AL_FORMAT_STEREO_FLOAT32
;
1878 if(strcasecmp(str
, "AL_FORMAT_QUAD32") == 0) return AL_FORMAT_QUAD32
;
1879 if(strcasecmp(str
, "AL_FORMAT_51CHN32") == 0) return AL_FORMAT_51CHN32
;
1880 if(strcasecmp(str
, "AL_FORMAT_61CHN32") == 0) return AL_FORMAT_61CHN32
;
1881 if(strcasecmp(str
, "AL_FORMAT_71CHN32") == 0) return AL_FORMAT_71CHN32
;
1883 if(strcasecmp(str
, "AL_FORMAT_MONO16") == 0) return AL_FORMAT_MONO16
;
1884 if(strcasecmp(str
, "AL_FORMAT_STEREO16") == 0) return AL_FORMAT_STEREO16
;
1885 if(strcasecmp(str
, "AL_FORMAT_QUAD16") == 0) return AL_FORMAT_QUAD16
;
1886 if(strcasecmp(str
, "AL_FORMAT_51CHN16") == 0) return AL_FORMAT_51CHN16
;
1887 if(strcasecmp(str
, "AL_FORMAT_61CHN16") == 0) return AL_FORMAT_61CHN16
;
1888 if(strcasecmp(str
, "AL_FORMAT_71CHN16") == 0) return AL_FORMAT_71CHN16
;
1890 if(strcasecmp(str
, "AL_FORMAT_MONO8") == 0) return AL_FORMAT_MONO8
;
1891 if(strcasecmp(str
, "AL_FORMAT_STEREO8") == 0) return AL_FORMAT_STEREO8
;
1892 if(strcasecmp(str
, "AL_FORMAT_QUAD8") == 0) return AL_FORMAT_QUAD8
;
1893 if(strcasecmp(str
, "AL_FORMAT_51CHN8") == 0) return AL_FORMAT_51CHN8
;
1894 if(strcasecmp(str
, "AL_FORMAT_61CHN8") == 0) return AL_FORMAT_61CHN8
;
1895 if(strcasecmp(str
, "AL_FORMAT_71CHN8") == 0) return AL_FORMAT_71CHN8
;
1897 AL_PRINT("Unknown format: \"%s\"\n", str
);
1898 return AL_FORMAT_STEREO16
;
1904 Open the Device specified.
1906 ALC_API ALCdevice
* ALC_APIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
1908 ALboolean bDeviceFound
= AL_FALSE
;
1913 if(deviceName
&& !deviceName
[0])
1916 device
= calloc(1, sizeof(ALCdevice
));
1919 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
1924 device
->Connected
= ALC_TRUE
;
1925 device
->IsCaptureDevice
= AL_FALSE
;
1926 device
->LastError
= ALC_NO_ERROR
;
1928 device
->Bs2b
= NULL
;
1929 device
->szDeviceName
= NULL
;
1931 device
->Contexts
= NULL
;
1932 device
->NumContexts
= 0;
1934 InitUIntMap(&device
->BufferMap
);
1935 InitUIntMap(&device
->EffectMap
);
1938 device
->Frequency
= GetConfigValueInt(NULL
, "frequency", SWMIXER_OUTPUT_RATE
);
1939 if(device
->Frequency
< 8000)
1940 device
->Frequency
= 8000;
1942 fmt
= GetConfigValue(NULL
, "format", "AL_FORMAT_STEREO16");
1943 device
->Format
= GetFormatFromString(fmt
);
1945 device
->NumUpdates
= GetConfigValueInt(NULL
, "periods", 4);
1946 if(device
->NumUpdates
< 2)
1947 device
->NumUpdates
= 4;
1949 i
= GetConfigValueInt(NULL
, "refresh", 4096);
1950 if(i
<= 0) i
= 4096;
1952 device
->UpdateSize
= GetConfigValueInt(NULL
, "period_size", i
/device
->NumUpdates
);
1953 if(device
->UpdateSize
<= 0)
1954 device
->UpdateSize
= i
/device
->NumUpdates
;
1956 device
->MaxNoOfSources
= GetConfigValueInt(NULL
, "sources", 256);
1957 if((ALint
)device
->MaxNoOfSources
<= 0)
1958 device
->MaxNoOfSources
= 256;
1960 device
->AuxiliaryEffectSlotMax
= GetConfigValueInt(NULL
, "slots", 4);
1961 if((ALint
)device
->AuxiliaryEffectSlotMax
<= 0)
1962 device
->AuxiliaryEffectSlotMax
= 4;
1964 device
->NumStereoSources
= 1;
1965 device
->NumMonoSources
= device
->MaxNoOfSources
- device
->NumStereoSources
;
1967 device
->NumAuxSends
= GetConfigValueInt(NULL
, "sends", MAX_SENDS
);
1968 if(device
->NumAuxSends
> MAX_SENDS
)
1969 device
->NumAuxSends
= MAX_SENDS
;
1971 device
->Bs2bLevel
= GetConfigValueInt(NULL
, "cf_level", 0);
1973 if(aluChannelsFromFormat(device
->Format
) <= 2)
1975 device
->HeadDampen
= GetConfigValueFloat(NULL
, "head_dampen", DEFAULT_HEAD_DAMPEN
);
1976 device
->HeadDampen
= __min(device
->HeadDampen
, 1.0f
);
1977 device
->HeadDampen
= __max(device
->HeadDampen
, 0.0f
);
1980 device
->HeadDampen
= 0.0f
;
1982 // Find a playback device to open
1983 SuspendContext(NULL
);
1984 for(i
= 0;BackendList
[i
].Init
;i
++)
1986 device
->Funcs
= &BackendList
[i
].Funcs
;
1987 if(ALCdevice_OpenPlayback(device
, deviceName
))
1989 device
->next
= g_pDeviceList
;
1990 g_pDeviceList
= device
;
1993 bDeviceFound
= AL_TRUE
;
1997 ProcessContext(NULL
);
2001 // No suitable output device found
2002 alcSetError(NULL
, ALC_INVALID_VALUE
);
2014 Close the specified Device
2016 ALC_API ALCboolean ALC_APIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
2020 if(!IsDevice(pDevice
) || pDevice
->IsCaptureDevice
)
2022 alcSetError(pDevice
, ALC_INVALID_DEVICE
);
2026 SuspendContext(NULL
);
2028 list
= &g_pDeviceList
;
2029 while(*list
!= pDevice
)
2030 list
= &(*list
)->next
;
2032 *list
= (*list
)->next
;
2035 ProcessContext(NULL
);
2037 if(pDevice
->NumContexts
> 0)
2040 AL_PRINT("alcCloseDevice(): destroying %u Context(s)\n", pDevice
->NumContexts
);
2042 while(pDevice
->NumContexts
> 0)
2043 alcDestroyContext(pDevice
->Contexts
[0]);
2045 ALCdevice_ClosePlayback(pDevice
);
2047 if(pDevice
->BufferMap
.size
> 0)
2050 AL_PRINT("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice
->BufferMap
.size
);
2052 ReleaseALBuffers(pDevice
);
2054 ResetUIntMap(&pDevice
->BufferMap
);
2056 if(pDevice
->EffectMap
.size
> 0)
2059 AL_PRINT("alcCloseDevice(): deleting %d Effect(s)\n", pDevice
->EffectMap
.size
);
2061 ReleaseALEffects(pDevice
);
2063 ResetUIntMap(&pDevice
->EffectMap
);
2065 if(pDevice
->FilterCount
> 0)
2068 AL_PRINT("alcCloseDevice(): deleting %d Filter(s)\n", pDevice
->FilterCount
);
2070 ReleaseALFilters(pDevice
);
2072 if(pDevice
->DatabufferCount
> 0)
2075 AL_PRINT("alcCloseDevice(): deleting %d Databuffer(s)\n", pDevice
->DatabufferCount
);
2077 ReleaseALDatabuffers(pDevice
);
2080 free(pDevice
->Bs2b
);
2081 pDevice
->Bs2b
= NULL
;
2083 free(pDevice
->szDeviceName
);
2084 pDevice
->szDeviceName
= NULL
;
2086 free(pDevice
->Contexts
);
2087 pDevice
->Contexts
= NULL
;
2089 //Release device structure
2090 memset(pDevice
, 0, sizeof(ALCdevice
));
2097 ALCvoid
ReleaseALC(ALCvoid
)
2099 free(alcDeviceList
); alcDeviceList
= NULL
;
2100 alcDeviceListSize
= 0;
2101 free(alcAllDeviceList
); alcAllDeviceList
= NULL
;
2102 alcAllDeviceListSize
= 0;
2103 free(alcCaptureDeviceList
); alcCaptureDeviceList
= NULL
;
2104 alcCaptureDeviceListSize
= 0;
2106 free(alcDefaultDeviceSpecifier
);
2107 alcDefaultDeviceSpecifier
= NULL
;
2108 free(alcDefaultAllDeviceSpecifier
);
2109 alcDefaultAllDeviceSpecifier
= NULL
;
2110 free(alcCaptureDefaultDeviceSpecifier
);
2111 alcCaptureDefaultDeviceSpecifier
= NULL
;
2114 if(g_ulDeviceCount
> 0)
2115 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount
, (g_ulDeviceCount
>1)?"s":"");
2118 while(g_pDeviceList
)
2120 if(g_pDeviceList
->IsCaptureDevice
)
2121 alcCaptureCloseDevice(g_pDeviceList
);
2123 alcCloseDevice(g_pDeviceList
);
2127 ///////////////////////////////////////////////////////