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 { "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 { "alBufferSubDataSOFT", (ALCvoid
*) alBufferSubDataSOFT
},
241 { "alGenDatabuffersEXT", (ALCvoid
*) alGenDatabuffersEXT
},
242 { "alDeleteDatabuffersEXT", (ALCvoid
*) alDeleteDatabuffersEXT
},
243 { "alIsDatabufferEXT", (ALCvoid
*) alIsDatabufferEXT
},
244 { "alDatabufferDataEXT", (ALCvoid
*) alDatabufferDataEXT
},
245 { "alDatabufferSubDataEXT", (ALCvoid
*) alDatabufferSubDataEXT
},
246 { "alGetDatabufferSubDataEXT", (ALCvoid
*) alGetDatabufferSubDataEXT
},
247 { "alDatabufferfEXT", (ALCvoid
*) alDatabufferfEXT
},
248 { "alDatabufferfvEXT", (ALCvoid
*) alDatabufferfvEXT
},
249 { "alDatabufferiEXT", (ALCvoid
*) alDatabufferiEXT
},
250 { "alDatabufferivEXT", (ALCvoid
*) alDatabufferivEXT
},
251 { "alGetDatabufferfEXT", (ALCvoid
*) alGetDatabufferfEXT
},
252 { "alGetDatabufferfvEXT", (ALCvoid
*) alGetDatabufferfvEXT
},
253 { "alGetDatabufferiEXT", (ALCvoid
*) alGetDatabufferiEXT
},
254 { "alGetDatabufferivEXT", (ALCvoid
*) alGetDatabufferivEXT
},
255 { "alSelectDatabufferEXT", (ALCvoid
*) alSelectDatabufferEXT
},
256 { "alMapDatabufferEXT", (ALCvoid
*) alMapDatabufferEXT
},
257 { "alUnmapDatabufferEXT", (ALCvoid
*) alUnmapDatabufferEXT
},
259 { NULL
, (ALCvoid
*) NULL
}
262 static const ALCenums enumeration
[] = {
264 { "ALC_INVALID", ALC_INVALID
},
265 { "ALC_FALSE", ALC_FALSE
},
266 { "ALC_TRUE", ALC_TRUE
},
269 { "ALC_MAJOR_VERSION", ALC_MAJOR_VERSION
},
270 { "ALC_MINOR_VERSION", ALC_MINOR_VERSION
},
271 { "ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE
},
272 { "ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES
},
273 { "ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER
},
274 { "ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER
},
275 { "ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER
},
276 { "ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER
},
277 { "ALC_EXTENSIONS", ALC_EXTENSIONS
},
278 { "ALC_FREQUENCY", ALC_FREQUENCY
},
279 { "ALC_REFRESH", ALC_REFRESH
},
280 { "ALC_SYNC", ALC_SYNC
},
281 { "ALC_MONO_SOURCES", ALC_MONO_SOURCES
},
282 { "ALC_STEREO_SOURCES", ALC_STEREO_SOURCES
},
283 { "ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER
},
284 { "ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
},
285 { "ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES
},
286 { "ALC_CONNECTED", ALC_CONNECTED
},
289 { "ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION
},
290 { "ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION
},
291 { "ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS
},
294 { "ALC_NO_ERROR", ALC_NO_ERROR
},
295 { "ALC_INVALID_DEVICE", ALC_INVALID_DEVICE
},
296 { "ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT
},
297 { "ALC_INVALID_ENUM", ALC_INVALID_ENUM
},
298 { "ALC_INVALID_VALUE", ALC_INVALID_VALUE
},
299 { "ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY
},
303 static const ALCchar alcNoError
[] = "No Error";
304 static const ALCchar alcErrInvalidDevice
[] = "Invalid Device";
305 static const ALCchar alcErrInvalidContext
[] = "Invalid Context";
306 static const ALCchar alcErrInvalidEnum
[] = "Invalid Enum";
307 static const ALCchar alcErrInvalidValue
[] = "Invalid Value";
308 static const ALCchar alcErrOutOfMemory
[] = "Out of Memory";
310 /* Device lists. Sizes only include the first ending null character, not the
312 static ALCchar
*alcDeviceList
;
313 static size_t alcDeviceListSize
;
314 static ALCchar
*alcAllDeviceList
;
315 static size_t alcAllDeviceListSize
;
316 static ALCchar
*alcCaptureDeviceList
;
317 static size_t alcCaptureDeviceListSize
;
318 // Default is always the first in the list
319 static ALCchar
*alcDefaultDeviceSpecifier
;
320 static ALCchar
*alcDefaultAllDeviceSpecifier
;
321 static ALCchar
*alcCaptureDefaultDeviceSpecifier
;
324 static const ALCchar alcNoDeviceExtList
[] =
325 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
326 "ALC_EXT_thread_local_context";
327 static const ALCchar alcExtensionList
[] =
328 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
329 "ALC_EXT_disconnect ALC_EXT_EFX ALC_EXT_thread_local_context";
330 static const ALCint alcMajorVersion
= 1;
331 static const ALCint alcMinorVersion
= 1;
333 static const ALCint alcEFXMajorVersion
= 1;
334 static const ALCint alcEFXMinorVersion
= 0;
336 ///////////////////////////////////////////////////////
339 ///////////////////////////////////////////////////////
342 static ALCdevice
*g_pDeviceList
= NULL
;
343 static ALCuint g_ulDeviceCount
= 0;
345 static CRITICAL_SECTION g_csMutex
;
348 static ALCcontext
*g_pContextList
= NULL
;
349 static ALCuint g_ulContextCount
= 0;
351 // Thread-local current context
352 static tls_type LocalContext
;
353 // Process-wide current context
354 static ALCcontext
*GlobalContext
;
357 static ALCenum g_eLastNullDeviceError
= ALC_NO_ERROR
;
359 // Default context extensions
360 static const ALchar alExtList
[] =
361 "AL_EXTX_buffer_sub_data AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE "
362 "AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXTX_loop_points "
363 "AL_EXT_MCFORMATS AL_EXT_MULAW AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET "
364 "AL_EXT_source_distance_model AL_LOKI_quadriphonic AL_SOFT_buffer_sub_data";
366 // Mixing Priority Level
367 static ALint RTPrioLevel
;
370 static FILE *LogFile
;
372 ///////////////////////////////////////////////////////
375 ///////////////////////////////////////////////////////
376 // ALC Related helper functions
377 #ifdef HAVE_GCC_DESTRUCTOR
378 static void alc_init(void) __attribute__((constructor
));
379 static void alc_deinit(void) __attribute__((destructor
));
382 static void alc_init(void);
383 static void alc_deinit(void);
385 BOOL APIENTRY
DllMain(HANDLE hModule
,DWORD ul_reason_for_call
,LPVOID lpReserved
)
389 // Perform actions based on the reason for calling.
390 switch(ul_reason_for_call
)
392 case DLL_PROCESS_ATTACH
:
393 DisableThreadLibraryCalls(hModule
);
397 case DLL_PROCESS_DETACH
:
406 static void alc_init(void)
409 const char *devs
, *str
;
411 str
= getenv("ALSOFT_LOGFILE");
414 LogFile
= fopen(str
, "w");
416 fprintf(stderr
, "AL lib: Failed to open log file '%s'\n", str
);
421 InitializeCriticalSection(&g_csMutex
);
425 tls_create(&LocalContext
);
427 RTPrioLevel
= GetConfigValueInt(NULL
, "rt-prio", 0);
429 DefaultResampler
= GetConfigValueInt(NULL
, "resampler", RESAMPLER_DEFAULT
);
430 if(DefaultResampler
>= RESAMPLER_MAX
|| DefaultResampler
<= RESAMPLER_MIN
)
431 DefaultResampler
= RESAMPLER_DEFAULT
;
433 devs
= GetConfigValue(NULL
, "drivers", "");
438 const char *next
= devs
;
439 int endlist
, delitem
;
444 next
= strchr(devs
, ',');
446 delitem
= (devs
[0] == '-');
447 if(devs
[0] == '-') devs
++;
449 if(!devs
[0] || devs
[0] == ',')
456 len
= (next
? ((size_t)(next
-devs
)) : strlen(devs
));
457 for(n
= i
;BackendList
[n
].Init
;n
++)
459 if(len
== strlen(BackendList
[n
].name
) &&
460 strncmp(BackendList
[n
].name
, devs
, len
) == 0)
465 BackendList
[n
] = BackendList
[n
+1];
467 } while(BackendList
[n
].Init
);
471 BackendInfo Bkp
= BackendList
[n
];
474 BackendList
[n
] = BackendList
[n
-1];
477 BackendList
[n
] = Bkp
;
488 BackendList
[i
].name
= NULL
;
489 BackendList
[i
].Init
= NULL
;
490 BackendList
[i
].Deinit
= NULL
;
491 BackendList
[i
].Probe
= NULL
;
495 for(i
= 0;BackendList
[i
].Init
;i
++)
496 BackendList
[i
].Init(&BackendList
[i
].Funcs
);
498 str
= GetConfigValue(NULL
, "excludefx", "");
505 { "eaxreverb", EAXREVERB
},
506 { "reverb", REVERB
},
508 { "modulator", MODULATOR
},
513 const char *next
= str
;
517 next
= strchr(str
, ',');
519 if(!str
[0] || next
== str
)
522 len
= (next
? ((size_t)(next
-str
)) : strlen(str
));
523 for(n
= 0;EffectList
[n
].name
;n
++)
525 if(len
== strlen(EffectList
[n
].name
) &&
526 strncmp(EffectList
[n
].name
, str
, len
) == 0)
527 DisabledEffects
[EffectList
[n
].type
] = AL_TRUE
;
533 static void alc_deinit(void)
539 for(i
= 0;BackendList
[i
].Deinit
;i
++)
540 BackendList
[i
].Deinit();
542 tls_delete(LocalContext
);
546 DeleteCriticalSection(&g_csMutex
);
548 if(LogFile
!= stderr
)
554 static void ProbeDeviceList()
558 free(alcDeviceList
); alcDeviceList
= NULL
;
559 alcDeviceListSize
= 0;
561 for(i
= 0;BackendList
[i
].Probe
;i
++)
562 BackendList
[i
].Probe(DEVICE_PROBE
);
565 static void ProbeAllDeviceList()
569 free(alcAllDeviceList
); alcAllDeviceList
= NULL
;
570 alcAllDeviceListSize
= 0;
572 for(i
= 0;BackendList
[i
].Probe
;i
++)
573 BackendList
[i
].Probe(ALL_DEVICE_PROBE
);
576 static void ProbeCaptureDeviceList()
580 free(alcCaptureDeviceList
); alcCaptureDeviceList
= NULL
;
581 alcCaptureDeviceListSize
= 0;
583 for(i
= 0;BackendList
[i
].Probe
;i
++)
584 BackendList
[i
].Probe(CAPTURE_DEVICE_PROBE
);
588 #define DECL_APPEND_LIST_FUNC(type) \
589 void Append##type##List(const ALCchar *name) \
591 size_t len = strlen(name); \
597 temp = realloc(alc##type##List, alc##type##ListSize + len + 2); \
600 AL_PRINT("Realloc failed to add %s!\n", name); \
603 alc##type##List = temp; \
604 sprintf(alc##type##List+alc##type##ListSize, "%s", name); \
605 alc##type##ListSize += len+1; \
606 alc##type##List[alc##type##ListSize] = 0; \
609 DECL_APPEND_LIST_FUNC(Device
)
610 DECL_APPEND_LIST_FUNC(AllDevice
)
611 DECL_APPEND_LIST_FUNC(CaptureDevice
)
614 void al_print(const char *fname
, unsigned int line
, const char *fmt
, ...)
620 fn
= strrchr(fname
, '/');
621 if(!fn
) fn
= strrchr(fname
, '\\');;
625 i
= snprintf(str
, sizeof(str
), "AL lib: %s:%d: ", fn
, line
);
626 if(i
< (int)sizeof(str
) && i
> 0)
630 vsnprintf(str
+i
, sizeof(str
)-i
, fmt
, ap
);
633 str
[sizeof(str
)-1] = 0;
635 fprintf(LogFile
, "%s", str
);
639 void SetRTPriority(void)
645 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL
);
647 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL
);
648 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM)
649 struct sched_param param
;
653 /* Use the minimum real-time priority possible for now (on Linux this
654 * should be 1 for SCHED_RR) */
655 param
.sched_priority
= sched_get_priority_min(SCHED_RR
);
656 failed
= !!pthread_setschedparam(pthread_self(), SCHED_RR
, ¶m
);
660 param
.sched_priority
= 0;
661 failed
= !!pthread_setschedparam(pthread_self(), SCHED_OTHER
, ¶m
);
664 /* Real-time priority not available */
665 failed
= (RTPrioLevel
>0);
668 AL_PRINT("Failed to set priority level for thread\n");
672 void InitUIntMap(UIntMap
*map
)
679 void ResetUIntMap(UIntMap
*map
)
687 ALenum
InsertUIntMapEntry(UIntMap
*map
, ALuint key
, ALvoid
*value
)
694 ALsizei high
= map
->size
- 1;
697 ALsizei mid
= low
+ (high
-low
)/2;
698 if(map
->array
[mid
].key
< key
)
703 if(map
->array
[low
].key
< key
)
708 if(pos
== map
->size
|| map
->array
[pos
].key
!= key
)
710 if(map
->size
== map
->maxsize
)
715 newsize
= (map
->maxsize
? (map
->maxsize
<<1) : 4);
716 if(newsize
< map
->maxsize
)
717 return AL_OUT_OF_MEMORY
;
719 temp
= realloc(map
->array
, newsize
*sizeof(map
->array
[0]));
720 if(!temp
) return AL_OUT_OF_MEMORY
;
722 map
->maxsize
= newsize
;
726 if(pos
< map
->size
-1)
727 memmove(&map
->array
[pos
+1], &map
->array
[pos
],
728 (map
->size
-1-pos
)*sizeof(map
->array
[0]));
730 map
->array
[pos
].key
= key
;
731 map
->array
[pos
].value
= value
;
736 void RemoveUIntMapKey(UIntMap
*map
, ALuint key
)
741 ALsizei high
= map
->size
- 1;
744 ALsizei mid
= low
+ (high
-low
)/2;
745 if(map
->array
[mid
].key
< key
)
750 if(map
->array
[low
].key
== key
)
752 if(low
< map
->size
-1)
753 memmove(&map
->array
[low
], &map
->array
[low
+1],
754 (map
->size
-1-low
)*sizeof(map
->array
[0]));
764 Check pDevice is a valid Device pointer
766 static ALCboolean
IsDevice(ALCdevice
*pDevice
)
768 ALCdevice
*pTempDevice
;
770 SuspendContext(NULL
);
772 pTempDevice
= g_pDeviceList
;
773 while(pTempDevice
&& pTempDevice
!= pDevice
)
774 pTempDevice
= pTempDevice
->next
;
776 ProcessContext(NULL
);
778 return (pTempDevice
? ALC_TRUE
: ALC_FALSE
);
784 Check pContext is a valid Context pointer
786 static ALCboolean
IsContext(ALCcontext
*pContext
)
788 ALCcontext
*pTempContext
;
790 SuspendContext(NULL
);
792 pTempContext
= g_pContextList
;
793 while (pTempContext
&& pTempContext
!= pContext
)
794 pTempContext
= pTempContext
->next
;
796 ProcessContext(NULL
);
798 return (pTempContext
? ALC_TRUE
: ALC_FALSE
);
805 Store latest ALC Error
807 ALCvoid
alcSetError(ALCdevice
*device
, ALenum errorCode
)
810 device
->LastError
= errorCode
;
812 g_eLastNullDeviceError
= errorCode
;
816 /* UpdateDeviceParams:
818 * Updates device parameters according to the attribute list.
820 static ALCboolean
UpdateDeviceParams(ALCdevice
*device
, const ALCint
*attrList
)
822 ALCuint freq
, numMono
, numStereo
, numSends
;
828 running
= ((device
->NumContexts
> 0) ? AL_TRUE
: AL_FALSE
);
829 oldRate
= device
->Frequency
;
831 // Check for attributes
832 if(attrList
&& attrList
[0])
834 // If a context is already running on the device, stop playback so the
835 // device attributes can be updated
838 ProcessContext(NULL
);
839 ALCdevice_StopPlayback(device
);
840 SuspendContext(NULL
);
844 freq
= device
->Frequency
;
845 numMono
= device
->NumMonoSources
;
846 numStereo
= device
->NumStereoSources
;
847 numSends
= device
->NumAuxSends
;
850 while(attrList
[attrIdx
])
852 if(attrList
[attrIdx
] == ALC_FREQUENCY
&&
853 !ConfigValueExists(NULL
, "frequency"))
855 freq
= attrList
[attrIdx
+ 1];
860 if(attrList
[attrIdx
] == ALC_STEREO_SOURCES
)
862 numStereo
= attrList
[attrIdx
+ 1];
863 if(numStereo
> device
->MaxNoOfSources
)
864 numStereo
= device
->MaxNoOfSources
;
866 numMono
= device
->MaxNoOfSources
- numStereo
;
869 if(attrList
[attrIdx
] == ALC_MAX_AUXILIARY_SENDS
&&
870 !ConfigValueExists(NULL
, "sends"))
872 numSends
= attrList
[attrIdx
+ 1];
873 if(numSends
> MAX_SENDS
)
874 numSends
= MAX_SENDS
;
880 device
->UpdateSize
= (ALuint64
)device
->UpdateSize
* freq
/
883 device
->Frequency
= freq
;
884 device
->NumMonoSources
= numMono
;
885 device
->NumStereoSources
= numStereo
;
886 device
->NumAuxSends
= numSends
;
892 if(ALCdevice_ResetPlayback(device
) == ALC_FALSE
)
895 aluInitPanning(device
);
897 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
899 device
->ClickRemoval
[i
] = 0.0f
;
900 device
->PendingClicks
[i
] = 0.0f
;
903 for(i
= 0;i
< device
->NumContexts
;i
++)
905 ALCcontext
*context
= device
->Contexts
[i
];
908 SuspendContext(context
);
909 for(pos
= 0;pos
< context
->EffectSlotMap
.size
;pos
++)
911 ALeffectslot
*slot
= context
->EffectSlotMap
.array
[pos
].value
;
913 if(ALEffect_DeviceUpdate(slot
->EffectState
, device
) == AL_FALSE
)
915 ProcessContext(context
);
918 ALEffect_Update(slot
->EffectState
, context
, &slot
->effect
);
921 for(pos
= 0;pos
< context
->SourceMap
.size
;pos
++)
923 ALsource
*source
= context
->SourceMap
.array
[pos
].value
;
924 ALuint s
= device
->NumAuxSends
;
927 if(source
->Send
[s
].Slot
)
928 source
->Send
[s
].Slot
->refcount
--;
929 source
->Send
[s
].Slot
= NULL
;
930 source
->Send
[s
].WetFilter
.type
= 0;
931 source
->Send
[s
].WetFilter
.filter
= 0;
934 source
->NeedsUpdate
= AL_TRUE
;
936 ProcessContext(context
);
939 if(device
->Bs2bLevel
> 0 && device
->Bs2bLevel
<= 6)
943 device
->Bs2b
= calloc(1, sizeof(*device
->Bs2b
));
944 bs2b_clear(device
->Bs2b
);
946 bs2b_set_srate(device
->Bs2b
, device
->Frequency
);
947 bs2b_set_level(device
->Bs2b
, device
->Bs2bLevel
);
955 if(aluChannelsFromFormat(device
->Format
) <= 2)
957 device
->HeadDampen
= GetConfigValueFloat(NULL
, "head_dampen", DEFAULT_HEAD_DAMPEN
);
958 device
->HeadDampen
= __min(device
->HeadDampen
, 1.0f
);
959 device
->HeadDampen
= __max(device
->HeadDampen
, 0.0f
);
962 device
->HeadDampen
= 0.0f
;
973 ALCvoid
SuspendContext(ALCcontext
*pContext
)
976 EnterCriticalSection(&g_csMutex
);
985 ALCvoid
ProcessContext(ALCcontext
*pContext
)
988 LeaveCriticalSection(&g_csMutex
);
995 Returns the currently active Context, in a locked state
997 ALCcontext
*GetContextSuspended(void)
999 ALCcontext
*pContext
= NULL
;
1001 SuspendContext(NULL
);
1003 pContext
= tls_get(LocalContext
);
1004 if(pContext
&& !IsContext(pContext
))
1006 tls_set(LocalContext
, NULL
);
1010 pContext
= GlobalContext
;
1013 SuspendContext(pContext
);
1015 ProcessContext(NULL
);
1024 Initialize Context variables
1026 static ALvoid
InitContext(ALCcontext
*pContext
)
1028 //Initialise listener
1029 pContext
->Listener
.Gain
= 1.0f
;
1030 pContext
->Listener
.MetersPerUnit
= 1.0f
;
1031 pContext
->Listener
.Position
[0] = 0.0f
;
1032 pContext
->Listener
.Position
[1] = 0.0f
;
1033 pContext
->Listener
.Position
[2] = 0.0f
;
1034 pContext
->Listener
.Velocity
[0] = 0.0f
;
1035 pContext
->Listener
.Velocity
[1] = 0.0f
;
1036 pContext
->Listener
.Velocity
[2] = 0.0f
;
1037 pContext
->Listener
.Forward
[0] = 0.0f
;
1038 pContext
->Listener
.Forward
[1] = 0.0f
;
1039 pContext
->Listener
.Forward
[2] = -1.0f
;
1040 pContext
->Listener
.Up
[0] = 0.0f
;
1041 pContext
->Listener
.Up
[1] = 1.0f
;
1042 pContext
->Listener
.Up
[2] = 0.0f
;
1045 pContext
->LastError
= AL_NO_ERROR
;
1046 pContext
->Suspended
= AL_FALSE
;
1047 pContext
->ActiveSourceCount
= 0;
1048 InitUIntMap(&pContext
->SourceMap
);
1049 InitUIntMap(&pContext
->EffectSlotMap
);
1052 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
1053 pContext
->SourceDistanceModel
= AL_FALSE
;
1054 pContext
->DopplerFactor
= 1.0f
;
1055 pContext
->DopplerVelocity
= 1.0f
;
1056 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
1058 pContext
->ExtensionList
= alExtList
;
1065 Clean up Context, destroy any remaining Sources
1067 static ALCvoid
ExitContext(ALCcontext
*pContext
)
1069 //Invalidate context
1070 pContext
->LastError
= AL_NO_ERROR
;
1073 ///////////////////////////////////////////////////////
1076 ///////////////////////////////////////////////////////
1077 // ALC Functions calls
1080 // This should probably move to another c file but for now ...
1081 ALC_API ALCdevice
* ALC_APIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
1083 ALCboolean DeviceFound
= ALC_FALSE
;
1084 ALCdevice
*device
= NULL
;
1089 alcSetError(NULL
, ALC_INVALID_VALUE
);
1093 if(deviceName
&& !deviceName
[0])
1096 device
= calloc(1, sizeof(ALCdevice
));
1099 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
1104 device
->Connected
= ALC_TRUE
;
1105 device
->IsCaptureDevice
= AL_TRUE
;
1107 device
->szDeviceName
= NULL
;
1109 device
->Frequency
= frequency
;
1110 device
->Format
= format
;
1111 device
->UpdateSize
= SampleSize
;
1112 device
->NumUpdates
= 1;
1114 SuspendContext(NULL
);
1115 for(i
= 0;BackendList
[i
].Init
;i
++)
1117 device
->Funcs
= &BackendList
[i
].Funcs
;
1118 if(ALCdevice_OpenCapture(device
, deviceName
))
1120 device
->next
= g_pDeviceList
;
1121 g_pDeviceList
= device
;
1124 DeviceFound
= ALC_TRUE
;
1128 ProcessContext(NULL
);
1132 alcSetError(NULL
, ALC_INVALID_VALUE
);
1140 ALC_API ALCboolean ALC_APIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
1144 if(!IsDevice(pDevice
) || !pDevice
->IsCaptureDevice
)
1146 alcSetError(pDevice
, ALC_INVALID_DEVICE
);
1150 SuspendContext(NULL
);
1152 list
= &g_pDeviceList
;
1153 while(*list
!= pDevice
)
1154 list
= &(*list
)->next
;
1156 *list
= (*list
)->next
;
1159 ProcessContext(NULL
);
1161 ALCdevice_CloseCapture(pDevice
);
1163 free(pDevice
->szDeviceName
);
1164 pDevice
->szDeviceName
= NULL
;
1171 ALC_API
void ALC_APIENTRY
alcCaptureStart(ALCdevice
*device
)
1173 SuspendContext(NULL
);
1174 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
1175 alcSetError(device
, ALC_INVALID_DEVICE
);
1176 else if(device
->Connected
)
1177 ALCdevice_StartCapture(device
);
1178 ProcessContext(NULL
);
1181 ALC_API
void ALC_APIENTRY
alcCaptureStop(ALCdevice
*device
)
1183 SuspendContext(NULL
);
1184 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
1185 alcSetError(device
, ALC_INVALID_DEVICE
);
1187 ALCdevice_StopCapture(device
);
1188 ProcessContext(NULL
);
1191 ALC_API
void ALC_APIENTRY
alcCaptureSamples(ALCdevice
*device
, ALCvoid
*buffer
, ALCsizei samples
)
1193 SuspendContext(NULL
);
1194 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
1195 alcSetError(device
, ALC_INVALID_DEVICE
);
1197 ALCdevice_CaptureSamples(device
, buffer
, samples
);
1198 ProcessContext(NULL
);
1204 Return last ALC generated error code
1206 ALC_API ALCenum ALC_APIENTRY
alcGetError(ALCdevice
*device
)
1210 if(IsDevice(device
))
1212 errorCode
= device
->LastError
;
1213 device
->LastError
= ALC_NO_ERROR
;
1217 errorCode
= g_eLastNullDeviceError
;
1218 g_eLastNullDeviceError
= ALC_NO_ERROR
;
1229 ALC_API ALCvoid ALC_APIENTRY
alcSuspendContext(ALCcontext
*pContext
)
1231 SuspendContext(NULL
);
1232 if(IsContext(pContext
))
1233 pContext
->Suspended
= AL_TRUE
;
1234 ProcessContext(NULL
);
1243 ALC_API ALCvoid ALC_APIENTRY
alcProcessContext(ALCcontext
*pContext
)
1245 SuspendContext(NULL
);
1246 if(IsContext(pContext
))
1247 pContext
->Suspended
= AL_FALSE
;
1248 ProcessContext(NULL
);
1255 Returns information about the Device, and error strings
1257 ALC_API
const ALCchar
* ALC_APIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
1259 const ALCchar
*value
= NULL
;
1267 case ALC_INVALID_ENUM
:
1268 value
= alcErrInvalidEnum
;
1271 case ALC_INVALID_VALUE
:
1272 value
= alcErrInvalidValue
;
1275 case ALC_INVALID_DEVICE
:
1276 value
= alcErrInvalidDevice
;
1279 case ALC_INVALID_CONTEXT
:
1280 value
= alcErrInvalidContext
;
1283 case ALC_OUT_OF_MEMORY
:
1284 value
= alcErrOutOfMemory
;
1287 case ALC_DEVICE_SPECIFIER
:
1288 if(IsDevice(pDevice
))
1289 value
= pDevice
->szDeviceName
;
1293 value
= alcDeviceList
;
1297 case ALC_ALL_DEVICES_SPECIFIER
:
1298 ProbeAllDeviceList();
1299 value
= alcAllDeviceList
;
1302 case ALC_CAPTURE_DEVICE_SPECIFIER
:
1303 if(IsDevice(pDevice
))
1304 value
= pDevice
->szDeviceName
;
1307 ProbeCaptureDeviceList();
1308 value
= alcCaptureDeviceList
;
1312 /* Default devices are always first in the list */
1313 case ALC_DEFAULT_DEVICE_SPECIFIER
:
1317 free(alcDefaultDeviceSpecifier
);
1318 alcDefaultDeviceSpecifier
= strdup(alcDeviceList
? alcDeviceList
: "");
1319 if(!alcDefaultDeviceSpecifier
)
1320 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1321 value
= alcDefaultDeviceSpecifier
;
1324 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
1325 if(!alcAllDeviceList
)
1326 ProbeAllDeviceList();
1328 free(alcDefaultAllDeviceSpecifier
);
1329 alcDefaultAllDeviceSpecifier
= strdup(alcAllDeviceList
?
1330 alcAllDeviceList
: "");
1331 if(!alcDefaultAllDeviceSpecifier
)
1332 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1333 value
= alcDefaultAllDeviceSpecifier
;
1336 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
1337 if(!alcCaptureDeviceList
)
1338 ProbeCaptureDeviceList();
1340 free(alcCaptureDefaultDeviceSpecifier
);
1341 alcCaptureDefaultDeviceSpecifier
= strdup(alcCaptureDeviceList
?
1342 alcCaptureDeviceList
: "");
1343 if(!alcCaptureDefaultDeviceSpecifier
)
1344 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1345 value
= alcCaptureDefaultDeviceSpecifier
;
1348 case ALC_EXTENSIONS
:
1349 if(IsDevice(pDevice
))
1350 value
= alcExtensionList
;
1352 value
= alcNoDeviceExtList
;
1356 alcSetError(pDevice
, ALC_INVALID_ENUM
);
1367 Returns information about the Device and the version of Open AL
1369 ALC_API ALCvoid ALC_APIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
1371 if(size
== 0 || data
== NULL
)
1373 alcSetError(device
, ALC_INVALID_VALUE
);
1377 if(IsDevice(device
) && device
->IsCaptureDevice
)
1379 SuspendContext(NULL
);
1384 case ALC_CAPTURE_SAMPLES
:
1385 *data
= ALCdevice_AvailableSamples(device
);
1389 *data
= device
->Connected
;
1393 alcSetError(device
, ALC_INVALID_ENUM
);
1397 ProcessContext(NULL
);
1404 case ALC_MAJOR_VERSION
:
1405 *data
= alcMajorVersion
;
1408 case ALC_MINOR_VERSION
:
1409 *data
= alcMinorVersion
;
1412 case ALC_EFX_MAJOR_VERSION
:
1413 *data
= alcEFXMajorVersion
;
1416 case ALC_EFX_MINOR_VERSION
:
1417 *data
= alcEFXMinorVersion
;
1420 case ALC_MAX_AUXILIARY_SENDS
:
1421 if(!IsDevice(device
))
1422 alcSetError(device
, ALC_INVALID_DEVICE
);
1424 *data
= device
->NumAuxSends
;
1427 case ALC_ATTRIBUTES_SIZE
:
1428 if(!IsDevice(device
))
1429 alcSetError(device
, ALC_INVALID_DEVICE
);
1434 case ALC_ALL_ATTRIBUTES
:
1435 if(!IsDevice(device
))
1436 alcSetError(device
, ALC_INVALID_DEVICE
);
1438 alcSetError(device
, ALC_INVALID_VALUE
);
1443 SuspendContext(NULL
);
1444 data
[i
++] = ALC_FREQUENCY
;
1445 data
[i
++] = device
->Frequency
;
1447 data
[i
++] = ALC_REFRESH
;
1448 data
[i
++] = device
->Frequency
/ device
->UpdateSize
;
1450 data
[i
++] = ALC_SYNC
;
1451 data
[i
++] = ALC_FALSE
;
1453 data
[i
++] = ALC_MONO_SOURCES
;
1454 data
[i
++] = device
->NumMonoSources
;
1456 data
[i
++] = ALC_STEREO_SOURCES
;
1457 data
[i
++] = device
->NumStereoSources
;
1459 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
1460 data
[i
++] = device
->NumAuxSends
;
1463 ProcessContext(NULL
);
1468 if(!IsDevice(device
))
1469 alcSetError(device
, ALC_INVALID_DEVICE
);
1471 *data
= device
->Frequency
;
1475 if(!IsDevice(device
))
1476 alcSetError(device
, ALC_INVALID_DEVICE
);
1478 *data
= device
->Frequency
/ device
->UpdateSize
;
1482 if(!IsDevice(device
))
1483 alcSetError(device
, ALC_INVALID_DEVICE
);
1488 case ALC_MONO_SOURCES
:
1489 if(!IsDevice(device
))
1490 alcSetError(device
, ALC_INVALID_DEVICE
);
1492 *data
= device
->NumMonoSources
;
1495 case ALC_STEREO_SOURCES
:
1496 if(!IsDevice(device
))
1497 alcSetError(device
, ALC_INVALID_DEVICE
);
1499 *data
= device
->NumStereoSources
;
1503 if(!IsDevice(device
))
1504 alcSetError(device
, ALC_INVALID_DEVICE
);
1506 *data
= device
->Connected
;
1510 alcSetError(device
, ALC_INVALID_ENUM
);
1517 alcIsExtensionPresent
1519 Determines if there is support for a particular extension
1521 ALC_API ALCboolean ALC_APIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
1523 ALCboolean bResult
= ALC_FALSE
;
1529 alcSetError(device
, ALC_INVALID_VALUE
);
1533 len
= strlen(extName
);
1534 ptr
= (IsDevice(device
) ? alcExtensionList
: alcNoDeviceExtList
);
1537 if(strncasecmp(ptr
, extName
, len
) == 0 &&
1538 (ptr
[len
] == '\0' || isspace(ptr
[len
])))
1543 if((ptr
=strchr(ptr
, ' ')) != NULL
)
1547 } while(isspace(*ptr
));
1558 Retrieves the function address for a particular extension function
1560 ALC_API ALCvoid
* ALC_APIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
1566 alcSetError(device
, ALC_INVALID_VALUE
);
1570 while(alcFunctions
[i
].funcName
&& strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
1572 return alcFunctions
[i
].address
;
1579 Get the value for a particular ALC Enumerated Value
1581 ALC_API ALCenum ALC_APIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
1587 alcSetError(device
, ALC_INVALID_VALUE
);
1591 while(enumeration
[i
].enumName
&& strcmp(enumeration
[i
].enumName
,enumName
) != 0)
1593 return enumeration
[i
].value
;
1600 Create and attach a Context to a particular Device.
1602 ALC_API ALCcontext
* ALC_APIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
1604 ALCcontext
*ALContext
;
1607 SuspendContext(NULL
);
1609 if(!IsDevice(device
) || device
->IsCaptureDevice
|| !device
->Connected
)
1611 alcSetError(device
, ALC_INVALID_DEVICE
);
1612 ProcessContext(NULL
);
1616 // Reset Context Last Error code
1617 device
->LastError
= ALC_NO_ERROR
;
1619 if(UpdateDeviceParams(device
, attrList
) == ALC_FALSE
)
1621 alcSetError(device
, ALC_INVALID_DEVICE
);
1622 aluHandleDisconnect(device
);
1623 ProcessContext(NULL
);
1624 ALCdevice_StopPlayback(device
);
1629 temp
= realloc(device
->Contexts
, (device
->NumContexts
+1) * sizeof(*device
->Contexts
));
1632 device
->Contexts
= temp
;
1634 ALContext
= calloc(1, sizeof(ALCcontext
));
1637 ALContext
->MaxActiveSources
= 256;
1638 ALContext
->ActiveSources
= malloc(sizeof(ALContext
->ActiveSources
[0]) *
1639 ALContext
->MaxActiveSources
);
1642 if(!ALContext
|| !ALContext
->ActiveSources
)
1645 alcSetError(device
, ALC_OUT_OF_MEMORY
);
1646 ProcessContext(NULL
);
1647 if(device
->NumContexts
== 0)
1648 ALCdevice_StopPlayback(device
);
1652 device
->Contexts
[device
->NumContexts
++] = ALContext
;
1653 ALContext
->Device
= device
;
1655 InitContext(ALContext
);
1657 ALContext
->next
= g_pContextList
;
1658 g_pContextList
= ALContext
;
1661 ProcessContext(NULL
);
1672 ALC_API ALCvoid ALC_APIENTRY
alcDestroyContext(ALCcontext
*context
)
1678 if(!IsContext(context
))
1680 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1684 Device
= context
->Device
;
1686 if(Device
->NumContexts
== 1)
1687 ALCdevice_StopPlayback(Device
);
1689 SuspendContext(NULL
);
1691 if(context
== GlobalContext
)
1692 GlobalContext
= NULL
;
1694 for(i
= 0;i
< Device
->NumContexts
;i
++)
1696 if(Device
->Contexts
[i
] == context
)
1698 Device
->Contexts
[i
] = Device
->Contexts
[Device
->NumContexts
-1];
1699 Device
->NumContexts
--;
1705 SuspendContext(context
);
1707 if(context
->SourceMap
.size
> 0)
1710 AL_PRINT("alcDestroyContext(): deleting %d Source(s)\n", context
->SourceMap
.size
);
1712 ReleaseALSources(context
);
1714 ResetUIntMap(&context
->SourceMap
);
1716 if(context
->EffectSlotMap
.size
> 0)
1719 AL_PRINT("alcDestroyContext(): deleting %d AuxiliaryEffectSlot(s)\n", context
->EffectSlotMap
.size
);
1721 ReleaseALAuxiliaryEffectSlots(context
);
1723 ResetUIntMap(&context
->EffectSlotMap
);
1725 free(context
->ActiveSources
);
1726 context
->ActiveSources
= NULL
;
1727 context
->MaxActiveSources
= 0;
1728 context
->ActiveSourceCount
= 0;
1730 list
= &g_pContextList
;
1731 while(*list
!= context
)
1732 list
= &(*list
)->next
;
1734 *list
= (*list
)->next
;
1738 ProcessContext(context
);
1739 ProcessContext(NULL
);
1741 ExitContext(context
);
1743 // Free memory (MUST do this after ProcessContext)
1744 memset(context
, 0, sizeof(ALCcontext
));
1750 alcGetCurrentContext
1752 Returns the currently active Context
1754 ALC_API ALCcontext
* ALC_APIENTRY
alcGetCurrentContext(ALCvoid
)
1756 ALCcontext
*pContext
;
1758 if((pContext
=GetContextSuspended()) != NULL
)
1759 ProcessContext(pContext
);
1767 Returns the currently active thread-local Context
1769 ALC_API ALCcontext
* ALC_APIENTRY
alcGetThreadContext(void)
1771 ALCcontext
*pContext
= NULL
;
1773 SuspendContext(NULL
);
1775 pContext
= tls_get(LocalContext
);
1776 if(pContext
&& !IsContext(pContext
))
1778 tls_set(LocalContext
, NULL
);
1782 ProcessContext(NULL
);
1789 alcGetContextsDevice
1791 Returns the Device that a particular Context is attached to
1793 ALC_API ALCdevice
* ALC_APIENTRY
alcGetContextsDevice(ALCcontext
*pContext
)
1795 ALCdevice
*pDevice
= NULL
;
1797 SuspendContext(NULL
);
1798 if(IsContext(pContext
))
1799 pDevice
= pContext
->Device
;
1801 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1802 ProcessContext(NULL
);
1809 alcMakeContextCurrent
1811 Makes the given Context the active Context
1813 ALC_API ALCboolean ALC_APIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
1815 ALboolean bReturn
= AL_TRUE
;
1817 SuspendContext(NULL
);
1819 // context must be a valid Context or NULL
1820 if(context
== NULL
|| IsContext(context
))
1822 GlobalContext
= context
;
1823 tls_set(LocalContext
, NULL
);
1827 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1831 ProcessContext(NULL
);
1839 Makes the given Context the active Context for the current thread
1841 ALC_API ALCboolean ALC_APIENTRY
alcSetThreadContext(ALCcontext
*context
)
1843 ALboolean bReturn
= AL_TRUE
;
1845 SuspendContext(NULL
);
1847 // context must be a valid Context or NULL
1848 if(context
== NULL
|| IsContext(context
))
1849 tls_set(LocalContext
, context
);
1852 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1856 ProcessContext(NULL
);
1862 // Sets the default channel order used by most non-WaveFormatEx-based APIs
1863 void SetDefaultChannelOrder(ALCdevice
*device
)
1865 switch(aluChannelsFromFormat(device
->Format
))
1867 case 1: device
->DevChannels
[FRONT_CENTER
] = 0; break;
1869 case 2: device
->DevChannels
[FRONT_LEFT
] = 0;
1870 device
->DevChannels
[FRONT_RIGHT
] = 1; break;
1872 case 4: device
->DevChannels
[FRONT_LEFT
] = 0;
1873 device
->DevChannels
[FRONT_RIGHT
] = 1;
1874 device
->DevChannels
[BACK_LEFT
] = 2;
1875 device
->DevChannels
[BACK_RIGHT
] = 3; break;
1877 case 6: device
->DevChannels
[FRONT_LEFT
] = 0;
1878 device
->DevChannels
[FRONT_RIGHT
] = 1;
1879 device
->DevChannels
[BACK_LEFT
] = 2;
1880 device
->DevChannels
[BACK_RIGHT
] = 3;
1881 device
->DevChannels
[FRONT_CENTER
] = 4;
1882 device
->DevChannels
[LFE
] = 5; break;
1884 case 7: device
->DevChannels
[FRONT_LEFT
] = 0;
1885 device
->DevChannels
[FRONT_RIGHT
] = 1;
1886 device
->DevChannels
[FRONT_CENTER
] = 2;
1887 device
->DevChannels
[LFE
] = 3;
1888 device
->DevChannels
[BACK_CENTER
] = 4;
1889 device
->DevChannels
[SIDE_LEFT
] = 5;
1890 device
->DevChannels
[SIDE_RIGHT
] = 6; break;
1892 case 8: device
->DevChannels
[FRONT_LEFT
] = 0;
1893 device
->DevChannels
[FRONT_RIGHT
] = 1;
1894 device
->DevChannels
[BACK_LEFT
] = 2;
1895 device
->DevChannels
[BACK_RIGHT
] = 3;
1896 device
->DevChannels
[FRONT_CENTER
] = 4;
1897 device
->DevChannels
[LFE
] = 5;
1898 device
->DevChannels
[SIDE_LEFT
] = 6;
1899 device
->DevChannels
[SIDE_RIGHT
] = 7; break;
1902 // Sets the default order used by WaveFormatEx
1903 void SetDefaultWFXChannelOrder(ALCdevice
*device
)
1905 switch(aluChannelsFromFormat(device
->Format
))
1907 case 1: device
->DevChannels
[FRONT_CENTER
] = 0; break;
1909 case 2: device
->DevChannels
[FRONT_LEFT
] = 0;
1910 device
->DevChannels
[FRONT_RIGHT
] = 1; break;
1912 case 4: device
->DevChannels
[FRONT_LEFT
] = 0;
1913 device
->DevChannels
[FRONT_RIGHT
] = 1;
1914 device
->DevChannels
[BACK_LEFT
] = 2;
1915 device
->DevChannels
[BACK_RIGHT
] = 3; break;
1917 case 6: device
->DevChannels
[FRONT_LEFT
] = 0;
1918 device
->DevChannels
[FRONT_RIGHT
] = 1;
1919 device
->DevChannels
[FRONT_CENTER
] = 2;
1920 device
->DevChannels
[LFE
] = 3;
1921 device
->DevChannels
[BACK_LEFT
] = 4;
1922 device
->DevChannels
[BACK_RIGHT
] = 5; break;
1924 case 7: device
->DevChannels
[FRONT_LEFT
] = 0;
1925 device
->DevChannels
[FRONT_RIGHT
] = 1;
1926 device
->DevChannels
[FRONT_CENTER
] = 2;
1927 device
->DevChannels
[LFE
] = 3;
1928 device
->DevChannels
[BACK_CENTER
] = 4;
1929 device
->DevChannels
[SIDE_LEFT
] = 5;
1930 device
->DevChannels
[SIDE_RIGHT
] = 6; break;
1932 case 8: device
->DevChannels
[FRONT_LEFT
] = 0;
1933 device
->DevChannels
[FRONT_RIGHT
] = 1;
1934 device
->DevChannels
[FRONT_CENTER
] = 2;
1935 device
->DevChannels
[LFE
] = 3;
1936 device
->DevChannels
[BACK_LEFT
] = 4;
1937 device
->DevChannels
[BACK_RIGHT
] = 5;
1938 device
->DevChannels
[SIDE_LEFT
] = 6;
1939 device
->DevChannels
[SIDE_RIGHT
] = 7; break;
1943 static ALenum
GetFormatFromString(const char *str
)
1945 if(strcasecmp(str
, "AL_FORMAT_MONO32") == 0) return AL_FORMAT_MONO_FLOAT32
;
1946 if(strcasecmp(str
, "AL_FORMAT_STEREO32") == 0) return AL_FORMAT_STEREO_FLOAT32
;
1947 if(strcasecmp(str
, "AL_FORMAT_QUAD32") == 0) return AL_FORMAT_QUAD32
;
1948 if(strcasecmp(str
, "AL_FORMAT_51CHN32") == 0) return AL_FORMAT_51CHN32
;
1949 if(strcasecmp(str
, "AL_FORMAT_61CHN32") == 0) return AL_FORMAT_61CHN32
;
1950 if(strcasecmp(str
, "AL_FORMAT_71CHN32") == 0) return AL_FORMAT_71CHN32
;
1952 if(strcasecmp(str
, "AL_FORMAT_MONO16") == 0) return AL_FORMAT_MONO16
;
1953 if(strcasecmp(str
, "AL_FORMAT_STEREO16") == 0) return AL_FORMAT_STEREO16
;
1954 if(strcasecmp(str
, "AL_FORMAT_QUAD16") == 0) return AL_FORMAT_QUAD16
;
1955 if(strcasecmp(str
, "AL_FORMAT_51CHN16") == 0) return AL_FORMAT_51CHN16
;
1956 if(strcasecmp(str
, "AL_FORMAT_61CHN16") == 0) return AL_FORMAT_61CHN16
;
1957 if(strcasecmp(str
, "AL_FORMAT_71CHN16") == 0) return AL_FORMAT_71CHN16
;
1959 if(strcasecmp(str
, "AL_FORMAT_MONO8") == 0) return AL_FORMAT_MONO8
;
1960 if(strcasecmp(str
, "AL_FORMAT_STEREO8") == 0) return AL_FORMAT_STEREO8
;
1961 if(strcasecmp(str
, "AL_FORMAT_QUAD8") == 0) return AL_FORMAT_QUAD8
;
1962 if(strcasecmp(str
, "AL_FORMAT_51CHN8") == 0) return AL_FORMAT_51CHN8
;
1963 if(strcasecmp(str
, "AL_FORMAT_61CHN8") == 0) return AL_FORMAT_61CHN8
;
1964 if(strcasecmp(str
, "AL_FORMAT_71CHN8") == 0) return AL_FORMAT_71CHN8
;
1966 AL_PRINT("Unknown format: \"%s\"\n", str
);
1967 return AL_FORMAT_STEREO16
;
1973 Open the Device specified.
1975 ALC_API ALCdevice
* ALC_APIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
1977 ALboolean bDeviceFound
= AL_FALSE
;
1982 if(deviceName
&& !deviceName
[0])
1985 device
= calloc(1, sizeof(ALCdevice
));
1988 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
1993 device
->Connected
= ALC_TRUE
;
1994 device
->IsCaptureDevice
= AL_FALSE
;
1995 device
->LastError
= ALC_NO_ERROR
;
1997 device
->Bs2b
= NULL
;
1998 device
->szDeviceName
= NULL
;
2000 device
->Contexts
= NULL
;
2001 device
->NumContexts
= 0;
2003 InitUIntMap(&device
->BufferMap
);
2004 InitUIntMap(&device
->EffectMap
);
2005 InitUIntMap(&device
->FilterMap
);
2006 InitUIntMap(&device
->DatabufferMap
);
2009 device
->Frequency
= GetConfigValueInt(NULL
, "frequency", SWMIXER_OUTPUT_RATE
);
2010 if(device
->Frequency
< 8000)
2011 device
->Frequency
= 8000;
2013 fmt
= GetConfigValue(NULL
, "format", "AL_FORMAT_STEREO16");
2014 device
->Format
= GetFormatFromString(fmt
);
2016 device
->NumUpdates
= GetConfigValueInt(NULL
, "periods", 4);
2017 if(device
->NumUpdates
< 2)
2018 device
->NumUpdates
= 4;
2020 device
->UpdateSize
= GetConfigValueInt(NULL
, "period_size", 1024);
2021 if(device
->UpdateSize
<= 0)
2022 device
->UpdateSize
= 1024;
2024 device
->MaxNoOfSources
= GetConfigValueInt(NULL
, "sources", 256);
2025 if((ALint
)device
->MaxNoOfSources
<= 0)
2026 device
->MaxNoOfSources
= 256;
2028 device
->AuxiliaryEffectSlotMax
= GetConfigValueInt(NULL
, "slots", 4);
2029 if((ALint
)device
->AuxiliaryEffectSlotMax
<= 0)
2030 device
->AuxiliaryEffectSlotMax
= 4;
2032 device
->NumStereoSources
= 1;
2033 device
->NumMonoSources
= device
->MaxNoOfSources
- device
->NumStereoSources
;
2035 device
->NumAuxSends
= GetConfigValueInt(NULL
, "sends", 1);
2036 if(device
->NumAuxSends
> MAX_SENDS
)
2037 device
->NumAuxSends
= MAX_SENDS
;
2039 device
->Bs2bLevel
= GetConfigValueInt(NULL
, "cf_level", 0);
2041 device
->DuplicateStereo
= GetConfigValueBool(NULL
, "stereodup", 1);
2043 device
->HeadDampen
= 0.0f
;
2045 // Find a playback device to open
2046 SuspendContext(NULL
);
2047 for(i
= 0;BackendList
[i
].Init
;i
++)
2049 device
->Funcs
= &BackendList
[i
].Funcs
;
2050 if(ALCdevice_OpenPlayback(device
, deviceName
))
2052 device
->next
= g_pDeviceList
;
2053 g_pDeviceList
= device
;
2056 bDeviceFound
= AL_TRUE
;
2060 ProcessContext(NULL
);
2064 // No suitable output device found
2065 alcSetError(NULL
, ALC_INVALID_VALUE
);
2077 Close the specified Device
2079 ALC_API ALCboolean ALC_APIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
2083 if(!IsDevice(pDevice
) || pDevice
->IsCaptureDevice
)
2085 alcSetError(pDevice
, ALC_INVALID_DEVICE
);
2089 SuspendContext(NULL
);
2091 list
= &g_pDeviceList
;
2092 while(*list
!= pDevice
)
2093 list
= &(*list
)->next
;
2095 *list
= (*list
)->next
;
2098 ProcessContext(NULL
);
2100 if(pDevice
->NumContexts
> 0)
2103 AL_PRINT("alcCloseDevice(): destroying %u Context(s)\n", pDevice
->NumContexts
);
2105 while(pDevice
->NumContexts
> 0)
2106 alcDestroyContext(pDevice
->Contexts
[0]);
2108 ALCdevice_ClosePlayback(pDevice
);
2110 if(pDevice
->BufferMap
.size
> 0)
2113 AL_PRINT("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice
->BufferMap
.size
);
2115 ReleaseALBuffers(pDevice
);
2117 ResetUIntMap(&pDevice
->BufferMap
);
2119 if(pDevice
->EffectMap
.size
> 0)
2122 AL_PRINT("alcCloseDevice(): deleting %d Effect(s)\n", pDevice
->EffectMap
.size
);
2124 ReleaseALEffects(pDevice
);
2126 ResetUIntMap(&pDevice
->EffectMap
);
2128 if(pDevice
->FilterMap
.size
> 0)
2131 AL_PRINT("alcCloseDevice(): deleting %d Filter(s)\n", pDevice
->FilterMap
.size
);
2133 ReleaseALFilters(pDevice
);
2135 ResetUIntMap(&pDevice
->FilterMap
);
2137 if(pDevice
->DatabufferMap
.size
> 0)
2140 AL_PRINT("alcCloseDevice(): deleting %d Databuffer(s)\n", pDevice
->DatabufferMap
.size
);
2142 ReleaseALDatabuffers(pDevice
);
2144 ResetUIntMap(&pDevice
->DatabufferMap
);
2146 free(pDevice
->Bs2b
);
2147 pDevice
->Bs2b
= NULL
;
2149 free(pDevice
->szDeviceName
);
2150 pDevice
->szDeviceName
= NULL
;
2152 free(pDevice
->Contexts
);
2153 pDevice
->Contexts
= NULL
;
2155 //Release device structure
2156 memset(pDevice
, 0, sizeof(ALCdevice
));
2163 ALCvoid
ReleaseALC(ALCvoid
)
2165 free(alcDeviceList
); alcDeviceList
= NULL
;
2166 alcDeviceListSize
= 0;
2167 free(alcAllDeviceList
); alcAllDeviceList
= NULL
;
2168 alcAllDeviceListSize
= 0;
2169 free(alcCaptureDeviceList
); alcCaptureDeviceList
= NULL
;
2170 alcCaptureDeviceListSize
= 0;
2172 free(alcDefaultDeviceSpecifier
);
2173 alcDefaultDeviceSpecifier
= NULL
;
2174 free(alcDefaultAllDeviceSpecifier
);
2175 alcDefaultAllDeviceSpecifier
= NULL
;
2176 free(alcCaptureDefaultDeviceSpecifier
);
2177 alcCaptureDefaultDeviceSpecifier
= NULL
;
2180 if(g_ulDeviceCount
> 0)
2181 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount
, (g_ulDeviceCount
>1)?"s":"");
2184 while(g_pDeviceList
)
2186 if(g_pDeviceList
->IsCaptureDevice
)
2187 alcCaptureCloseDevice(g_pDeviceList
);
2189 alcCloseDevice(g_pDeviceList
);
2193 ///////////////////////////////////////////////////////