2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
36 #include "alAuxEffectSlot.h"
37 #include "alDatabuffer.h"
42 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
43 typedef struct BackendInfo
{
45 void (*Init
)(BackendFuncs
*);
50 static BackendInfo BackendList
[] = {
51 #ifdef HAVE_PULSEAUDIO
52 { "pulse", alc_pulse_init
, alc_pulse_deinit
, alc_pulse_probe
, EmptyFuncs
},
55 { "alsa", alc_alsa_init
, alc_alsa_deinit
, alc_alsa_probe
, EmptyFuncs
},
58 { "oss", alc_oss_init
, alc_oss_deinit
, alc_oss_probe
, EmptyFuncs
},
61 { "solaris", alc_solaris_init
, alc_solaris_deinit
, alc_solaris_probe
, EmptyFuncs
},
64 { "dsound", alcDSoundInit
, alcDSoundDeinit
, alcDSoundProbe
, EmptyFuncs
},
67 { "winmm", alcWinMMInit
, alcWinMMDeinit
, alcWinMMProbe
, EmptyFuncs
},
70 { "port", alc_pa_init
, alc_pa_deinit
, alc_pa_probe
, EmptyFuncs
},
73 { "null", alc_null_init
, alc_null_deinit
, alc_null_probe
, EmptyFuncs
},
75 { "wave", alc_wave_init
, alc_wave_deinit
, alc_wave_probe
, EmptyFuncs
},
78 { NULL
, NULL
, NULL
, NULL
, EmptyFuncs
}
82 ///////////////////////////////////////////////////////
84 #define ALC_EFX_MAJOR_VERSION 0x20001
85 #define ALC_EFX_MINOR_VERSION 0x20002
86 #define ALC_MAX_AUXILIARY_SENDS 0x20003
88 ///////////////////////////////////////////////////////
89 // STRING and EXTENSIONS
91 typedef struct ALCfunction
{
92 const ALCchar
*funcName
;
96 typedef struct ALCenums
{
97 const ALCchar
*enumName
;
102 static const ALCfunction alcFunctions
[] = {
103 { "alcCreateContext", (ALCvoid
*) alcCreateContext
},
104 { "alcMakeContextCurrent", (ALCvoid
*) alcMakeContextCurrent
},
105 { "alcProcessContext", (ALCvoid
*) alcProcessContext
},
106 { "alcSuspendContext", (ALCvoid
*) alcSuspendContext
},
107 { "alcDestroyContext", (ALCvoid
*) alcDestroyContext
},
108 { "alcGetCurrentContext", (ALCvoid
*) alcGetCurrentContext
},
109 { "alcGetContextsDevice", (ALCvoid
*) alcGetContextsDevice
},
110 { "alcOpenDevice", (ALCvoid
*) alcOpenDevice
},
111 { "alcCloseDevice", (ALCvoid
*) alcCloseDevice
},
112 { "alcGetError", (ALCvoid
*) alcGetError
},
113 { "alcIsExtensionPresent", (ALCvoid
*) alcIsExtensionPresent
},
114 { "alcGetProcAddress", (ALCvoid
*) alcGetProcAddress
},
115 { "alcGetEnumValue", (ALCvoid
*) alcGetEnumValue
},
116 { "alcGetString", (ALCvoid
*) alcGetString
},
117 { "alcGetIntegerv", (ALCvoid
*) alcGetIntegerv
},
118 { "alcCaptureOpenDevice", (ALCvoid
*) alcCaptureOpenDevice
},
119 { "alcCaptureCloseDevice", (ALCvoid
*) alcCaptureCloseDevice
},
120 { "alcCaptureStart", (ALCvoid
*) alcCaptureStart
},
121 { "alcCaptureStop", (ALCvoid
*) alcCaptureStop
},
122 { "alcCaptureSamples", (ALCvoid
*) alcCaptureSamples
},
124 { "alcSetThreadContext", (ALCvoid
*) alcSetThreadContext
},
125 { "alcGetThreadContext", (ALCvoid
*) alcGetThreadContext
},
127 { "alEnable", (ALCvoid
*) alEnable
},
128 { "alDisable", (ALCvoid
*) alDisable
},
129 { "alIsEnabled", (ALCvoid
*) alIsEnabled
},
130 { "alGetString", (ALCvoid
*) alGetString
},
131 { "alGetBooleanv", (ALCvoid
*) alGetBooleanv
},
132 { "alGetIntegerv", (ALCvoid
*) alGetIntegerv
},
133 { "alGetFloatv", (ALCvoid
*) alGetFloatv
},
134 { "alGetDoublev", (ALCvoid
*) alGetDoublev
},
135 { "alGetBoolean", (ALCvoid
*) alGetBoolean
},
136 { "alGetInteger", (ALCvoid
*) alGetInteger
},
137 { "alGetFloat", (ALCvoid
*) alGetFloat
},
138 { "alGetDouble", (ALCvoid
*) alGetDouble
},
139 { "alGetError", (ALCvoid
*) alGetError
},
140 { "alIsExtensionPresent", (ALCvoid
*) alIsExtensionPresent
},
141 { "alGetProcAddress", (ALCvoid
*) alGetProcAddress
},
142 { "alGetEnumValue", (ALCvoid
*) alGetEnumValue
},
143 { "alListenerf", (ALCvoid
*) alListenerf
},
144 { "alListener3f", (ALCvoid
*) alListener3f
},
145 { "alListenerfv", (ALCvoid
*) alListenerfv
},
146 { "alListeneri", (ALCvoid
*) alListeneri
},
147 { "alListener3i", (ALCvoid
*) alListener3i
},
148 { "alListeneriv", (ALCvoid
*) alListeneriv
},
149 { "alGetListenerf", (ALCvoid
*) alGetListenerf
},
150 { "alGetListener3f", (ALCvoid
*) alGetListener3f
},
151 { "alGetListenerfv", (ALCvoid
*) alGetListenerfv
},
152 { "alGetListeneri", (ALCvoid
*) alGetListeneri
},
153 { "alGetListener3i", (ALCvoid
*) alGetListener3i
},
154 { "alGetListeneriv", (ALCvoid
*) alGetListeneriv
},
155 { "alGenSources", (ALCvoid
*) alGenSources
},
156 { "alDeleteSources", (ALCvoid
*) alDeleteSources
},
157 { "alIsSource", (ALCvoid
*) alIsSource
},
158 { "alSourcef", (ALCvoid
*) alSourcef
},
159 { "alSource3f", (ALCvoid
*) alSource3f
},
160 { "alSourcefv", (ALCvoid
*) alSourcefv
},
161 { "alSourcei", (ALCvoid
*) alSourcei
},
162 { "alSource3i", (ALCvoid
*) alSource3i
},
163 { "alSourceiv", (ALCvoid
*) alSourceiv
},
164 { "alGetSourcef", (ALCvoid
*) alGetSourcef
},
165 { "alGetSource3f", (ALCvoid
*) alGetSource3f
},
166 { "alGetSourcefv", (ALCvoid
*) alGetSourcefv
},
167 { "alGetSourcei", (ALCvoid
*) alGetSourcei
},
168 { "alGetSource3i", (ALCvoid
*) alGetSource3i
},
169 { "alGetSourceiv", (ALCvoid
*) alGetSourceiv
},
170 { "alSourcePlayv", (ALCvoid
*) alSourcePlayv
},
171 { "alSourceStopv", (ALCvoid
*) alSourceStopv
},
172 { "alSourceRewindv", (ALCvoid
*) alSourceRewindv
},
173 { "alSourcePausev", (ALCvoid
*) alSourcePausev
},
174 { "alSourcePlay", (ALCvoid
*) alSourcePlay
},
175 { "alSourceStop", (ALCvoid
*) alSourceStop
},
176 { "alSourceRewind", (ALCvoid
*) alSourceRewind
},
177 { "alSourcePause", (ALCvoid
*) alSourcePause
},
178 { "alSourceQueueBuffers", (ALCvoid
*) alSourceQueueBuffers
},
179 { "alSourceUnqueueBuffers", (ALCvoid
*) alSourceUnqueueBuffers
},
180 { "alGenBuffers", (ALCvoid
*) alGenBuffers
},
181 { "alDeleteBuffers", (ALCvoid
*) alDeleteBuffers
},
182 { "alIsBuffer", (ALCvoid
*) alIsBuffer
},
183 { "alBufferData", (ALCvoid
*) alBufferData
},
184 { "alBufferf", (ALCvoid
*) alBufferf
},
185 { "alBuffer3f", (ALCvoid
*) alBuffer3f
},
186 { "alBufferfv", (ALCvoid
*) alBufferfv
},
187 { "alBufferi", (ALCvoid
*) alBufferi
},
188 { "alBuffer3i", (ALCvoid
*) alBuffer3i
},
189 { "alBufferiv", (ALCvoid
*) alBufferiv
},
190 { "alGetBufferf", (ALCvoid
*) alGetBufferf
},
191 { "alGetBuffer3f", (ALCvoid
*) alGetBuffer3f
},
192 { "alGetBufferfv", (ALCvoid
*) alGetBufferfv
},
193 { "alGetBufferi", (ALCvoid
*) alGetBufferi
},
194 { "alGetBuffer3i", (ALCvoid
*) alGetBuffer3i
},
195 { "alGetBufferiv", (ALCvoid
*) alGetBufferiv
},
196 { "alDopplerFactor", (ALCvoid
*) alDopplerFactor
},
197 { "alDopplerVelocity", (ALCvoid
*) alDopplerVelocity
},
198 { "alSpeedOfSound", (ALCvoid
*) alSpeedOfSound
},
199 { "alDistanceModel", (ALCvoid
*) alDistanceModel
},
201 { "alGenFilters", (ALCvoid
*) alGenFilters
},
202 { "alDeleteFilters", (ALCvoid
*) alDeleteFilters
},
203 { "alIsFilter", (ALCvoid
*) alIsFilter
},
204 { "alFilteri", (ALCvoid
*) alFilteri
},
205 { "alFilteriv", (ALCvoid
*) alFilteriv
},
206 { "alFilterf", (ALCvoid
*) alFilterf
},
207 { "alFilterfv", (ALCvoid
*) alFilterfv
},
208 { "alGetFilteri", (ALCvoid
*) alGetFilteri
},
209 { "alGetFilteriv", (ALCvoid
*) alGetFilteriv
},
210 { "alGetFilterf", (ALCvoid
*) alGetFilterf
},
211 { "alGetFilterfv", (ALCvoid
*) alGetFilterfv
},
213 { "alGenEffects", (ALCvoid
*) alGenEffects
},
214 { "alDeleteEffects", (ALCvoid
*) alDeleteEffects
},
215 { "alIsEffect", (ALCvoid
*) alIsEffect
},
216 { "alEffecti", (ALCvoid
*) alEffecti
},
217 { "alEffectiv", (ALCvoid
*) alEffectiv
},
218 { "alEffectf", (ALCvoid
*) alEffectf
},
219 { "alEffectfv", (ALCvoid
*) alEffectfv
},
220 { "alGetEffecti", (ALCvoid
*) alGetEffecti
},
221 { "alGetEffectiv", (ALCvoid
*) alGetEffectiv
},
222 { "alGetEffectf", (ALCvoid
*) alGetEffectf
},
223 { "alGetEffectfv", (ALCvoid
*) alGetEffectfv
},
225 { "alGenAuxiliaryEffectSlots", (ALCvoid
*) alGenAuxiliaryEffectSlots
},
226 { "alDeleteAuxiliaryEffectSlots",(ALCvoid
*) alDeleteAuxiliaryEffectSlots
},
227 { "alIsAuxiliaryEffectSlot", (ALCvoid
*) alIsAuxiliaryEffectSlot
},
228 { "alAuxiliaryEffectSloti", (ALCvoid
*) alAuxiliaryEffectSloti
},
229 { "alAuxiliaryEffectSlotiv", (ALCvoid
*) alAuxiliaryEffectSlotiv
},
230 { "alAuxiliaryEffectSlotf", (ALCvoid
*) alAuxiliaryEffectSlotf
},
231 { "alAuxiliaryEffectSlotfv", (ALCvoid
*) alAuxiliaryEffectSlotfv
},
232 { "alGetAuxiliaryEffectSloti", (ALCvoid
*) alGetAuxiliaryEffectSloti
},
233 { "alGetAuxiliaryEffectSlotiv", (ALCvoid
*) alGetAuxiliaryEffectSlotiv
},
234 { "alGetAuxiliaryEffectSlotf", (ALCvoid
*) alGetAuxiliaryEffectSlotf
},
235 { "alGetAuxiliaryEffectSlotfv", (ALCvoid
*) alGetAuxiliaryEffectSlotfv
},
237 { "alBufferSubDataEXT", (ALCvoid
*) alBufferSubDataEXT
},
239 { "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_EXTX_sample_buffer_object AL_EXT_source_distance_model "
365 "AL_LOKI_quadriphonic AL_SOFT_buffer_sub_data";
367 // Mixing Priority Level
368 static ALint RTPrioLevel
;
371 resampler_t DefaultResampler
;
374 static FILE *LogFile
;
376 ///////////////////////////////////////////////////////
379 ///////////////////////////////////////////////////////
380 // ALC Related helper functions
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
:
404 #ifdef HAVE_GCC_DESTRUCTOR
405 static void alc_init(void) __attribute__((constructor
));
406 static void alc_deinit(void) __attribute__((destructor
));
410 static void alc_init(void)
413 const char *devs
, *str
;
415 str
= getenv("ALSOFT_LOGFILE");
418 LogFile
= fopen(str
, "w");
420 fprintf(stderr
, "AL lib: Failed to open log file '%s'\n", str
);
425 InitializeCriticalSection(&g_csMutex
);
429 tls_create(&LocalContext
);
431 RTPrioLevel
= GetConfigValueInt(NULL
, "rt-prio", 0);
433 DefaultResampler
= GetConfigValueInt(NULL
, "resampler", RESAMPLER_DEFAULT
);
434 if(DefaultResampler
>= RESAMPLER_MAX
|| DefaultResampler
<= RESAMPLER_MIN
)
435 DefaultResampler
= RESAMPLER_DEFAULT
;
437 devs
= GetConfigValue(NULL
, "drivers", "");
442 const char *next
= devs
;
443 int endlist
, delitem
;
448 next
= strchr(devs
, ',');
450 delitem
= (devs
[0] == '-');
451 if(devs
[0] == '-') devs
++;
453 if(!devs
[0] || devs
[0] == ',')
460 len
= (next
? ((size_t)(next
-devs
)) : strlen(devs
));
461 for(n
= i
;BackendList
[n
].Init
;n
++)
463 if(len
== strlen(BackendList
[n
].name
) &&
464 strncmp(BackendList
[n
].name
, devs
, len
) == 0)
469 BackendList
[n
] = BackendList
[n
+1];
471 } while(BackendList
[n
].Init
);
475 BackendInfo Bkp
= BackendList
[n
];
478 BackendList
[n
] = BackendList
[n
-1];
481 BackendList
[n
] = Bkp
;
492 BackendList
[i
].name
= NULL
;
493 BackendList
[i
].Init
= NULL
;
494 BackendList
[i
].Deinit
= NULL
;
495 BackendList
[i
].Probe
= NULL
;
499 for(i
= 0;BackendList
[i
].Init
;i
++)
500 BackendList
[i
].Init(&BackendList
[i
].Funcs
);
502 str
= GetConfigValue(NULL
, "excludefx", "");
509 { "eaxreverb", EAXREVERB
},
510 { "reverb", REVERB
},
512 { "modulator", MODULATOR
},
517 const char *next
= str
;
521 next
= strchr(str
, ',');
523 if(!str
[0] || next
== str
)
526 len
= (next
? ((size_t)(next
-str
)) : strlen(str
));
527 for(n
= 0;EffectList
[n
].name
;n
++)
529 if(len
== strlen(EffectList
[n
].name
) &&
530 strncmp(EffectList
[n
].name
, str
, len
) == 0)
531 DisabledEffects
[EffectList
[n
].type
] = AL_TRUE
;
537 static void alc_deinit(void)
543 for(i
= 0;BackendList
[i
].Deinit
;i
++)
544 BackendList
[i
].Deinit();
546 tls_delete(LocalContext
);
550 DeleteCriticalSection(&g_csMutex
);
552 if(LogFile
!= stderr
)
558 static void ProbeDeviceList()
562 free(alcDeviceList
); alcDeviceList
= NULL
;
563 alcDeviceListSize
= 0;
565 for(i
= 0;BackendList
[i
].Probe
;i
++)
566 BackendList
[i
].Probe(DEVICE_PROBE
);
569 static void ProbeAllDeviceList()
573 free(alcAllDeviceList
); alcAllDeviceList
= NULL
;
574 alcAllDeviceListSize
= 0;
576 for(i
= 0;BackendList
[i
].Probe
;i
++)
577 BackendList
[i
].Probe(ALL_DEVICE_PROBE
);
580 static void ProbeCaptureDeviceList()
584 free(alcCaptureDeviceList
); alcCaptureDeviceList
= NULL
;
585 alcCaptureDeviceListSize
= 0;
587 for(i
= 0;BackendList
[i
].Probe
;i
++)
588 BackendList
[i
].Probe(CAPTURE_DEVICE_PROBE
);
592 #define DECL_APPEND_LIST_FUNC(type) \
593 void Append##type##List(const ALCchar *name) \
595 size_t len = strlen(name); \
601 temp = realloc(alc##type##List, alc##type##ListSize + len + 2); \
604 AL_PRINT("Realloc failed to add %s!\n", name); \
607 alc##type##List = temp; \
608 sprintf(alc##type##List+alc##type##ListSize, "%s", name); \
609 alc##type##ListSize += len+1; \
610 alc##type##List[alc##type##ListSize] = 0; \
613 DECL_APPEND_LIST_FUNC(Device
)
614 DECL_APPEND_LIST_FUNC(AllDevice
)
615 DECL_APPEND_LIST_FUNC(CaptureDevice
)
618 void al_print(const char *fname
, unsigned int line
, const char *fmt
, ...)
624 fn
= strrchr(fname
, '/');
625 if(!fn
) fn
= strrchr(fname
, '\\');;
629 i
= snprintf(str
, sizeof(str
), "AL lib: %s:%d: ", fn
, line
);
630 if(i
< (int)sizeof(str
) && i
> 0)
634 vsnprintf(str
+i
, sizeof(str
)-i
, fmt
, ap
);
637 str
[sizeof(str
)-1] = 0;
639 fprintf(LogFile
, "%s", str
);
643 void SetRTPriority(void)
649 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL
);
651 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL
);
652 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM)
653 struct sched_param param
;
657 /* Use the minimum real-time priority possible for now (on Linux this
658 * should be 1 for SCHED_RR) */
659 param
.sched_priority
= sched_get_priority_min(SCHED_RR
);
660 failed
= !!pthread_setschedparam(pthread_self(), SCHED_RR
, ¶m
);
664 param
.sched_priority
= 0;
665 failed
= !!pthread_setschedparam(pthread_self(), SCHED_OTHER
, ¶m
);
668 /* Real-time priority not available */
669 failed
= (RTPrioLevel
>0);
672 AL_PRINT("Failed to set priority level for thread\n");
676 void InitUIntMap(UIntMap
*map
)
683 void ResetUIntMap(UIntMap
*map
)
691 ALenum
InsertUIntMapEntry(UIntMap
*map
, ALuint key
, ALvoid
*value
)
698 ALsizei high
= map
->size
- 1;
701 ALsizei mid
= low
+ (high
-low
)/2;
702 if(map
->array
[mid
].key
< key
)
707 if(map
->array
[low
].key
< key
)
712 if(pos
== map
->size
|| map
->array
[pos
].key
!= key
)
714 if(map
->size
== map
->maxsize
)
719 newsize
= (map
->maxsize
? (map
->maxsize
<<1) : 4);
720 if(newsize
< map
->maxsize
)
721 return AL_OUT_OF_MEMORY
;
723 temp
= realloc(map
->array
, newsize
*sizeof(map
->array
[0]));
724 if(!temp
) return AL_OUT_OF_MEMORY
;
726 map
->maxsize
= newsize
;
730 if(pos
< map
->size
-1)
731 memmove(&map
->array
[pos
+1], &map
->array
[pos
],
732 (map
->size
-1-pos
)*sizeof(map
->array
[0]));
734 map
->array
[pos
].key
= key
;
735 map
->array
[pos
].value
= value
;
740 void RemoveUIntMapKey(UIntMap
*map
, ALuint key
)
745 ALsizei high
= map
->size
- 1;
748 ALsizei mid
= low
+ (high
-low
)/2;
749 if(map
->array
[mid
].key
< key
)
754 if(map
->array
[low
].key
== key
)
756 if(low
< map
->size
-1)
757 memmove(&map
->array
[low
], &map
->array
[low
+1],
758 (map
->size
-1-low
)*sizeof(map
->array
[0]));
768 Check pDevice is a valid Device pointer
770 static ALCboolean
IsDevice(ALCdevice
*pDevice
)
772 ALCdevice
*pTempDevice
;
774 SuspendContext(NULL
);
776 pTempDevice
= g_pDeviceList
;
777 while(pTempDevice
&& pTempDevice
!= pDevice
)
778 pTempDevice
= pTempDevice
->next
;
780 ProcessContext(NULL
);
782 return (pTempDevice
? ALC_TRUE
: ALC_FALSE
);
788 Check pContext is a valid Context pointer
790 static ALCboolean
IsContext(ALCcontext
*pContext
)
792 ALCcontext
*pTempContext
;
794 SuspendContext(NULL
);
796 pTempContext
= g_pContextList
;
797 while (pTempContext
&& pTempContext
!= pContext
)
798 pTempContext
= pTempContext
->next
;
800 ProcessContext(NULL
);
802 return (pTempContext
? ALC_TRUE
: ALC_FALSE
);
809 Store latest ALC Error
811 ALCvoid
alcSetError(ALCdevice
*device
, ALenum errorCode
)
814 device
->LastError
= errorCode
;
816 g_eLastNullDeviceError
= errorCode
;
820 /* UpdateDeviceParams:
822 * Updates device parameters according to the attribute list.
824 static ALCboolean
UpdateDeviceParams(ALCdevice
*device
, const ALCint
*attrList
)
826 ALCuint freq
, numMono
, numStereo
, numSends
;
832 running
= ((device
->NumContexts
> 0) ? AL_TRUE
: AL_FALSE
);
833 oldRate
= device
->Frequency
;
835 // Check for attributes
836 if(attrList
&& attrList
[0])
838 // If a context is already running on the device, stop playback so the
839 // device attributes can be updated
842 ProcessContext(NULL
);
843 ALCdevice_StopPlayback(device
);
844 SuspendContext(NULL
);
848 freq
= device
->Frequency
;
849 numMono
= device
->NumMonoSources
;
850 numStereo
= device
->NumStereoSources
;
851 numSends
= device
->NumAuxSends
;
854 while(attrList
[attrIdx
])
856 if(attrList
[attrIdx
] == ALC_FREQUENCY
&&
857 !ConfigValueExists(NULL
, "frequency"))
859 freq
= attrList
[attrIdx
+ 1];
864 if(attrList
[attrIdx
] == ALC_STEREO_SOURCES
)
866 numStereo
= attrList
[attrIdx
+ 1];
867 if(numStereo
> device
->MaxNoOfSources
)
868 numStereo
= device
->MaxNoOfSources
;
870 numMono
= device
->MaxNoOfSources
- numStereo
;
873 if(attrList
[attrIdx
] == ALC_MAX_AUXILIARY_SENDS
&&
874 !ConfigValueExists(NULL
, "sends"))
876 numSends
= attrList
[attrIdx
+ 1];
877 if(numSends
> MAX_SENDS
)
878 numSends
= MAX_SENDS
;
884 device
->UpdateSize
= (ALuint64
)device
->UpdateSize
* freq
/
887 device
->Frequency
= freq
;
888 device
->NumMonoSources
= numMono
;
889 device
->NumStereoSources
= numStereo
;
890 device
->NumAuxSends
= numSends
;
896 if(ALCdevice_ResetPlayback(device
) == ALC_FALSE
)
899 aluInitPanning(device
);
901 // Scale the number of samples played according to the new sample rate
902 device
->SamplesPlayed
*= device
->Frequency
;
903 device
->SamplesPlayed
+= oldRate
-1;
904 device
->SamplesPlayed
/= oldRate
;
906 for(i
= 0;i
< OUTPUTCHANNELS
;i
++)
908 device
->ClickRemoval
[i
] = 0.0f
;
909 device
->PendingClicks
[i
] = 0.0f
;
912 for(i
= 0;i
< device
->NumContexts
;i
++)
914 ALCcontext
*context
= device
->Contexts
[i
];
917 SuspendContext(context
);
918 for(pos
= 0;pos
< context
->EffectSlotMap
.size
;pos
++)
920 ALeffectslot
*slot
= context
->EffectSlotMap
.array
[pos
].value
;
922 if(ALEffect_DeviceUpdate(slot
->EffectState
, device
) == AL_FALSE
)
924 ProcessContext(context
);
927 ALEffect_Update(slot
->EffectState
, context
, &slot
->effect
);
930 for(pos
= 0;pos
< context
->SourceMap
.size
;pos
++)
932 ALsource
*source
= context
->SourceMap
.array
[pos
].value
;
933 ALuint s
= device
->NumAuxSends
;
936 if(source
->Send
[s
].Slot
)
937 source
->Send
[s
].Slot
->refcount
--;
938 source
->Send
[s
].Slot
= NULL
;
939 source
->Send
[s
].WetFilter
.type
= 0;
940 source
->Send
[s
].WetFilter
.filter
= 0;
943 source
->NeedsUpdate
= AL_TRUE
;
945 ProcessContext(context
);
948 if(device
->Bs2bLevel
> 0 && device
->Bs2bLevel
<= 6)
952 device
->Bs2b
= calloc(1, sizeof(*device
->Bs2b
));
953 bs2b_clear(device
->Bs2b
);
955 bs2b_set_srate(device
->Bs2b
, device
->Frequency
);
956 bs2b_set_level(device
->Bs2b
, device
->Bs2bLevel
);
964 if(aluChannelsFromFormat(device
->Format
) <= 2)
966 device
->HeadDampen
= GetConfigValueFloat(NULL
, "head_dampen", DEFAULT_HEAD_DAMPEN
);
967 device
->HeadDampen
= __min(device
->HeadDampen
, 1.0f
);
968 device
->HeadDampen
= __max(device
->HeadDampen
, 0.0f
);
971 device
->HeadDampen
= 0.0f
;
982 ALCvoid
SuspendContext(ALCcontext
*pContext
)
985 EnterCriticalSection(&g_csMutex
);
994 ALCvoid
ProcessContext(ALCcontext
*pContext
)
997 LeaveCriticalSection(&g_csMutex
);
1004 Returns the currently active Context, in a locked state
1006 ALCcontext
*GetContextSuspended(void)
1008 ALCcontext
*pContext
= NULL
;
1010 SuspendContext(NULL
);
1012 pContext
= tls_get(LocalContext
);
1013 if(pContext
&& !IsContext(pContext
))
1015 tls_set(LocalContext
, NULL
);
1019 pContext
= GlobalContext
;
1022 SuspendContext(pContext
);
1024 ProcessContext(NULL
);
1033 Initialize Context variables
1035 static ALvoid
InitContext(ALCcontext
*pContext
)
1037 //Initialise listener
1038 pContext
->Listener
.Gain
= 1.0f
;
1039 pContext
->Listener
.MetersPerUnit
= 1.0f
;
1040 pContext
->Listener
.Position
[0] = 0.0f
;
1041 pContext
->Listener
.Position
[1] = 0.0f
;
1042 pContext
->Listener
.Position
[2] = 0.0f
;
1043 pContext
->Listener
.Velocity
[0] = 0.0f
;
1044 pContext
->Listener
.Velocity
[1] = 0.0f
;
1045 pContext
->Listener
.Velocity
[2] = 0.0f
;
1046 pContext
->Listener
.Forward
[0] = 0.0f
;
1047 pContext
->Listener
.Forward
[1] = 0.0f
;
1048 pContext
->Listener
.Forward
[2] = -1.0f
;
1049 pContext
->Listener
.Up
[0] = 0.0f
;
1050 pContext
->Listener
.Up
[1] = 1.0f
;
1051 pContext
->Listener
.Up
[2] = 0.0f
;
1054 pContext
->LastError
= AL_NO_ERROR
;
1055 pContext
->Suspended
= AL_FALSE
;
1056 pContext
->ActiveSourceCount
= 0;
1057 InitUIntMap(&pContext
->SourceMap
);
1058 InitUIntMap(&pContext
->EffectSlotMap
);
1061 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
1062 pContext
->SourceDistanceModel
= AL_FALSE
;
1063 pContext
->DopplerFactor
= 1.0f
;
1064 pContext
->DopplerVelocity
= 1.0f
;
1065 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
1067 pContext
->ExtensionList
= alExtList
;
1074 Clean up Context, destroy any remaining Sources
1076 static ALCvoid
ExitContext(ALCcontext
*pContext
)
1078 //Invalidate context
1079 pContext
->LastError
= AL_NO_ERROR
;
1082 ///////////////////////////////////////////////////////
1085 ///////////////////////////////////////////////////////
1086 // ALC Functions calls
1089 // This should probably move to another c file but for now ...
1090 ALC_API ALCdevice
* ALC_APIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
1092 ALCboolean DeviceFound
= ALC_FALSE
;
1093 ALCdevice
*device
= NULL
;
1098 alcSetError(NULL
, ALC_INVALID_VALUE
);
1102 if(deviceName
&& !deviceName
[0])
1105 device
= calloc(1, sizeof(ALCdevice
));
1108 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
1113 device
->Connected
= ALC_TRUE
;
1114 device
->IsCaptureDevice
= AL_TRUE
;
1116 device
->szDeviceName
= NULL
;
1118 device
->Frequency
= frequency
;
1119 device
->Format
= format
;
1120 device
->UpdateSize
= SampleSize
;
1121 device
->NumUpdates
= 1;
1123 SuspendContext(NULL
);
1124 for(i
= 0;BackendList
[i
].Init
;i
++)
1126 device
->Funcs
= &BackendList
[i
].Funcs
;
1127 if(ALCdevice_OpenCapture(device
, deviceName
))
1129 device
->next
= g_pDeviceList
;
1130 g_pDeviceList
= device
;
1133 DeviceFound
= ALC_TRUE
;
1137 ProcessContext(NULL
);
1141 alcSetError(NULL
, ALC_INVALID_VALUE
);
1149 ALC_API ALCboolean ALC_APIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
1153 if(!IsDevice(pDevice
) || !pDevice
->IsCaptureDevice
)
1155 alcSetError(pDevice
, ALC_INVALID_DEVICE
);
1159 SuspendContext(NULL
);
1161 list
= &g_pDeviceList
;
1162 while(*list
!= pDevice
)
1163 list
= &(*list
)->next
;
1165 *list
= (*list
)->next
;
1168 ProcessContext(NULL
);
1170 ALCdevice_CloseCapture(pDevice
);
1172 free(pDevice
->szDeviceName
);
1173 pDevice
->szDeviceName
= NULL
;
1180 ALC_API
void ALC_APIENTRY
alcCaptureStart(ALCdevice
*device
)
1182 SuspendContext(NULL
);
1183 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
1184 alcSetError(device
, ALC_INVALID_DEVICE
);
1185 else if(device
->Connected
)
1186 ALCdevice_StartCapture(device
);
1187 ProcessContext(NULL
);
1190 ALC_API
void ALC_APIENTRY
alcCaptureStop(ALCdevice
*device
)
1192 SuspendContext(NULL
);
1193 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
1194 alcSetError(device
, ALC_INVALID_DEVICE
);
1196 ALCdevice_StopCapture(device
);
1197 ProcessContext(NULL
);
1200 ALC_API
void ALC_APIENTRY
alcCaptureSamples(ALCdevice
*device
, ALCvoid
*buffer
, ALCsizei samples
)
1202 SuspendContext(NULL
);
1203 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
1204 alcSetError(device
, ALC_INVALID_DEVICE
);
1206 ALCdevice_CaptureSamples(device
, buffer
, samples
);
1207 ProcessContext(NULL
);
1213 Return last ALC generated error code
1215 ALC_API ALCenum ALC_APIENTRY
alcGetError(ALCdevice
*device
)
1219 if(IsDevice(device
))
1221 errorCode
= device
->LastError
;
1222 device
->LastError
= ALC_NO_ERROR
;
1226 errorCode
= g_eLastNullDeviceError
;
1227 g_eLastNullDeviceError
= ALC_NO_ERROR
;
1238 ALC_API ALCvoid ALC_APIENTRY
alcSuspendContext(ALCcontext
*pContext
)
1240 SuspendContext(NULL
);
1241 if(IsContext(pContext
))
1242 pContext
->Suspended
= AL_TRUE
;
1243 ProcessContext(NULL
);
1252 ALC_API ALCvoid ALC_APIENTRY
alcProcessContext(ALCcontext
*pContext
)
1254 SuspendContext(NULL
);
1255 if(IsContext(pContext
))
1256 pContext
->Suspended
= AL_FALSE
;
1257 ProcessContext(NULL
);
1264 Returns information about the Device, and error strings
1266 ALC_API
const ALCchar
* ALC_APIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
1268 const ALCchar
*value
= NULL
;
1276 case ALC_INVALID_ENUM
:
1277 value
= alcErrInvalidEnum
;
1280 case ALC_INVALID_VALUE
:
1281 value
= alcErrInvalidValue
;
1284 case ALC_INVALID_DEVICE
:
1285 value
= alcErrInvalidDevice
;
1288 case ALC_INVALID_CONTEXT
:
1289 value
= alcErrInvalidContext
;
1292 case ALC_OUT_OF_MEMORY
:
1293 value
= alcErrOutOfMemory
;
1296 case ALC_DEVICE_SPECIFIER
:
1297 if(IsDevice(pDevice
))
1298 value
= pDevice
->szDeviceName
;
1302 value
= alcDeviceList
;
1306 case ALC_ALL_DEVICES_SPECIFIER
:
1307 ProbeAllDeviceList();
1308 value
= alcAllDeviceList
;
1311 case ALC_CAPTURE_DEVICE_SPECIFIER
:
1312 if(IsDevice(pDevice
))
1313 value
= pDevice
->szDeviceName
;
1316 ProbeCaptureDeviceList();
1317 value
= alcCaptureDeviceList
;
1321 /* Default devices are always first in the list */
1322 case ALC_DEFAULT_DEVICE_SPECIFIER
:
1326 free(alcDefaultDeviceSpecifier
);
1327 alcDefaultDeviceSpecifier
= strdup(alcDeviceList
? alcDeviceList
: "");
1328 if(!alcDefaultDeviceSpecifier
)
1329 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1330 value
= alcDefaultDeviceSpecifier
;
1333 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
1334 if(!alcAllDeviceList
)
1335 ProbeAllDeviceList();
1337 free(alcDefaultAllDeviceSpecifier
);
1338 alcDefaultAllDeviceSpecifier
= strdup(alcAllDeviceList
?
1339 alcAllDeviceList
: "");
1340 if(!alcDefaultAllDeviceSpecifier
)
1341 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1342 value
= alcDefaultAllDeviceSpecifier
;
1345 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
1346 if(!alcCaptureDeviceList
)
1347 ProbeCaptureDeviceList();
1349 free(alcCaptureDefaultDeviceSpecifier
);
1350 alcCaptureDefaultDeviceSpecifier
= strdup(alcCaptureDeviceList
?
1351 alcCaptureDeviceList
: "");
1352 if(!alcCaptureDefaultDeviceSpecifier
)
1353 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1354 value
= alcCaptureDefaultDeviceSpecifier
;
1357 case ALC_EXTENSIONS
:
1358 if(IsDevice(pDevice
))
1359 value
= alcExtensionList
;
1361 value
= alcNoDeviceExtList
;
1365 alcSetError(pDevice
, ALC_INVALID_ENUM
);
1376 Returns information about the Device and the version of Open AL
1378 ALC_API ALCvoid ALC_APIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
1380 if(size
== 0 || data
== NULL
)
1382 alcSetError(device
, ALC_INVALID_VALUE
);
1386 if(IsDevice(device
) && device
->IsCaptureDevice
)
1388 SuspendContext(NULL
);
1393 case ALC_CAPTURE_SAMPLES
:
1394 *data
= ALCdevice_AvailableSamples(device
);
1398 *data
= device
->Connected
;
1402 alcSetError(device
, ALC_INVALID_ENUM
);
1406 ProcessContext(NULL
);
1413 case ALC_MAJOR_VERSION
:
1414 *data
= alcMajorVersion
;
1417 case ALC_MINOR_VERSION
:
1418 *data
= alcMinorVersion
;
1421 case ALC_EFX_MAJOR_VERSION
:
1422 *data
= alcEFXMajorVersion
;
1425 case ALC_EFX_MINOR_VERSION
:
1426 *data
= alcEFXMinorVersion
;
1429 case ALC_MAX_AUXILIARY_SENDS
:
1430 if(!IsDevice(device
))
1431 alcSetError(device
, ALC_INVALID_DEVICE
);
1433 *data
= device
->NumAuxSends
;
1436 case ALC_ATTRIBUTES_SIZE
:
1437 if(!IsDevice(device
))
1438 alcSetError(device
, ALC_INVALID_DEVICE
);
1443 case ALC_ALL_ATTRIBUTES
:
1444 if(!IsDevice(device
))
1445 alcSetError(device
, ALC_INVALID_DEVICE
);
1447 alcSetError(device
, ALC_INVALID_VALUE
);
1452 SuspendContext(NULL
);
1453 data
[i
++] = ALC_FREQUENCY
;
1454 data
[i
++] = device
->Frequency
;
1456 data
[i
++] = ALC_REFRESH
;
1457 data
[i
++] = device
->Frequency
/ device
->UpdateSize
;
1459 data
[i
++] = ALC_SYNC
;
1460 data
[i
++] = ALC_FALSE
;
1462 data
[i
++] = ALC_MONO_SOURCES
;
1463 data
[i
++] = device
->NumMonoSources
;
1465 data
[i
++] = ALC_STEREO_SOURCES
;
1466 data
[i
++] = device
->NumStereoSources
;
1468 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
1469 data
[i
++] = device
->NumAuxSends
;
1472 ProcessContext(NULL
);
1477 if(!IsDevice(device
))
1478 alcSetError(device
, ALC_INVALID_DEVICE
);
1480 *data
= device
->Frequency
;
1484 if(!IsDevice(device
))
1485 alcSetError(device
, ALC_INVALID_DEVICE
);
1487 *data
= device
->Frequency
/ device
->UpdateSize
;
1491 if(!IsDevice(device
))
1492 alcSetError(device
, ALC_INVALID_DEVICE
);
1497 case ALC_MONO_SOURCES
:
1498 if(!IsDevice(device
))
1499 alcSetError(device
, ALC_INVALID_DEVICE
);
1501 *data
= device
->NumMonoSources
;
1504 case ALC_STEREO_SOURCES
:
1505 if(!IsDevice(device
))
1506 alcSetError(device
, ALC_INVALID_DEVICE
);
1508 *data
= device
->NumStereoSources
;
1512 if(!IsDevice(device
))
1513 alcSetError(device
, ALC_INVALID_DEVICE
);
1515 *data
= device
->Connected
;
1518 case ALC_GET_TIME_EXT
:
1519 if(!IsDevice(device
))
1520 alcSetError(device
, ALC_INVALID_DEVICE
);
1522 alcSetError(device
, ALC_INVALID_VALUE
);
1525 ALuint64 t
= ALCdevice_GetTime(device
);
1526 t
-= t
%device
->TimeRes
;
1527 data
[0] = t
&0xffffffff;
1532 case ALC_GET_TIME_RES_EXT
:
1533 if(!IsDevice(device
))
1534 alcSetError(device
, ALC_INVALID_DEVICE
);
1536 alcSetError(device
, ALC_INVALID_VALUE
);
1539 data
[0] = device
->TimeRes
&0xffffffff;
1540 data
[1] = device
->TimeRes
>>32;
1545 alcSetError(device
, ALC_INVALID_ENUM
);
1552 alcIsExtensionPresent
1554 Determines if there is support for a particular extension
1556 ALC_API ALCboolean ALC_APIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
1558 ALCboolean bResult
= ALC_FALSE
;
1564 alcSetError(device
, ALC_INVALID_VALUE
);
1568 len
= strlen(extName
);
1569 ptr
= (IsDevice(device
) ? alcExtensionList
: alcNoDeviceExtList
);
1572 if(strncasecmp(ptr
, extName
, len
) == 0 &&
1573 (ptr
[len
] == '\0' || isspace(ptr
[len
])))
1578 if((ptr
=strchr(ptr
, ' ')) != NULL
)
1582 } while(isspace(*ptr
));
1593 Retrieves the function address for a particular extension function
1595 ALC_API ALCvoid
* ALC_APIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
1601 alcSetError(device
, ALC_INVALID_VALUE
);
1605 while(alcFunctions
[i
].funcName
&& strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
1607 return alcFunctions
[i
].address
;
1614 Get the value for a particular ALC Enumerated Value
1616 ALC_API ALCenum ALC_APIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
1622 alcSetError(device
, ALC_INVALID_VALUE
);
1626 while(enumeration
[i
].enumName
&& strcmp(enumeration
[i
].enumName
,enumName
) != 0)
1628 return enumeration
[i
].value
;
1635 Create and attach a Context to a particular Device.
1637 ALC_API ALCcontext
* ALC_APIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
1639 ALCcontext
*ALContext
;
1642 SuspendContext(NULL
);
1644 if(!IsDevice(device
) || device
->IsCaptureDevice
|| !device
->Connected
)
1646 alcSetError(device
, ALC_INVALID_DEVICE
);
1647 ProcessContext(NULL
);
1651 // Reset Context Last Error code
1652 device
->LastError
= ALC_NO_ERROR
;
1654 if(UpdateDeviceParams(device
, attrList
) == ALC_FALSE
)
1656 alcSetError(device
, ALC_INVALID_DEVICE
);
1657 aluHandleDisconnect(device
);
1658 ProcessContext(NULL
);
1659 ALCdevice_StopPlayback(device
);
1664 temp
= realloc(device
->Contexts
, (device
->NumContexts
+1) * sizeof(*device
->Contexts
));
1667 device
->Contexts
= temp
;
1669 ALContext
= calloc(1, sizeof(ALCcontext
));
1672 ALContext
->MaxActiveSources
= 256;
1673 ALContext
->ActiveSources
= malloc(sizeof(ALContext
->ActiveSources
[0]) *
1674 ALContext
->MaxActiveSources
);
1677 if(!ALContext
|| !ALContext
->ActiveSources
)
1680 alcSetError(device
, ALC_OUT_OF_MEMORY
);
1681 ProcessContext(NULL
);
1682 if(device
->NumContexts
== 0)
1683 ALCdevice_StopPlayback(device
);
1687 device
->Contexts
[device
->NumContexts
++] = ALContext
;
1688 ALContext
->Device
= device
;
1690 InitContext(ALContext
);
1692 ALContext
->next
= g_pContextList
;
1693 g_pContextList
= ALContext
;
1696 ProcessContext(NULL
);
1707 ALC_API ALCvoid ALC_APIENTRY
alcDestroyContext(ALCcontext
*context
)
1713 if(!IsContext(context
))
1715 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1719 Device
= context
->Device
;
1721 if(Device
->NumContexts
== 1)
1722 ALCdevice_StopPlayback(Device
);
1724 SuspendContext(NULL
);
1726 if(context
== GlobalContext
)
1727 GlobalContext
= NULL
;
1729 for(i
= 0;i
< Device
->NumContexts
;i
++)
1731 if(Device
->Contexts
[i
] == context
)
1733 Device
->Contexts
[i
] = Device
->Contexts
[Device
->NumContexts
-1];
1734 Device
->NumContexts
--;
1740 SuspendContext(context
);
1742 if(context
->SourceMap
.size
> 0)
1745 AL_PRINT("alcDestroyContext(): deleting %d Source(s)\n", context
->SourceMap
.size
);
1747 ReleaseALSources(context
);
1749 ResetUIntMap(&context
->SourceMap
);
1751 if(context
->EffectSlotMap
.size
> 0)
1754 AL_PRINT("alcDestroyContext(): deleting %d AuxiliaryEffectSlot(s)\n", context
->EffectSlotMap
.size
);
1756 ReleaseALAuxiliaryEffectSlots(context
);
1758 ResetUIntMap(&context
->EffectSlotMap
);
1760 free(context
->ActiveSources
);
1761 context
->ActiveSources
= NULL
;
1762 context
->MaxActiveSources
= 0;
1763 context
->ActiveSourceCount
= 0;
1765 list
= &g_pContextList
;
1766 while(*list
!= context
)
1767 list
= &(*list
)->next
;
1769 *list
= (*list
)->next
;
1773 ProcessContext(context
);
1774 ProcessContext(NULL
);
1776 ExitContext(context
);
1778 // Free memory (MUST do this after ProcessContext)
1779 memset(context
, 0, sizeof(ALCcontext
));
1785 alcGetCurrentContext
1787 Returns the currently active Context
1789 ALC_API ALCcontext
* ALC_APIENTRY
alcGetCurrentContext(ALCvoid
)
1791 ALCcontext
*pContext
;
1793 if((pContext
=GetContextSuspended()) != NULL
)
1794 ProcessContext(pContext
);
1802 Returns the currently active thread-local Context
1804 ALC_API ALCcontext
* ALC_APIENTRY
alcGetThreadContext(void)
1806 ALCcontext
*pContext
= NULL
;
1808 SuspendContext(NULL
);
1810 pContext
= tls_get(LocalContext
);
1811 if(pContext
&& !IsContext(pContext
))
1813 tls_set(LocalContext
, NULL
);
1817 ProcessContext(NULL
);
1824 alcGetContextsDevice
1826 Returns the Device that a particular Context is attached to
1828 ALC_API ALCdevice
* ALC_APIENTRY
alcGetContextsDevice(ALCcontext
*pContext
)
1830 ALCdevice
*pDevice
= NULL
;
1832 SuspendContext(NULL
);
1833 if(IsContext(pContext
))
1834 pDevice
= pContext
->Device
;
1836 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1837 ProcessContext(NULL
);
1844 alcMakeContextCurrent
1846 Makes the given Context the active Context
1848 ALC_API ALCboolean ALC_APIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
1850 ALboolean bReturn
= AL_TRUE
;
1852 SuspendContext(NULL
);
1854 // context must be a valid Context or NULL
1855 if(context
== NULL
|| IsContext(context
))
1857 GlobalContext
= context
;
1858 tls_set(LocalContext
, NULL
);
1862 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1866 ProcessContext(NULL
);
1874 Makes the given Context the active Context for the current thread
1876 ALC_API ALCboolean ALC_APIENTRY
alcSetThreadContext(ALCcontext
*context
)
1878 ALboolean bReturn
= AL_TRUE
;
1880 SuspendContext(NULL
);
1882 // context must be a valid Context or NULL
1883 if(context
== NULL
|| IsContext(context
))
1884 tls_set(LocalContext
, context
);
1887 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1891 ProcessContext(NULL
);
1897 // Sets the default channel order used by most non-WaveFormatEx-based APIs
1898 void SetDefaultChannelOrder(ALCdevice
*device
)
1900 switch(aluChannelsFromFormat(device
->Format
))
1902 case 1: device
->DevChannels
[FRONT_CENTER
] = 0; break;
1904 case 2: device
->DevChannels
[FRONT_LEFT
] = 0;
1905 device
->DevChannels
[FRONT_RIGHT
] = 1; break;
1907 case 4: device
->DevChannels
[FRONT_LEFT
] = 0;
1908 device
->DevChannels
[FRONT_RIGHT
] = 1;
1909 device
->DevChannels
[BACK_LEFT
] = 2;
1910 device
->DevChannels
[BACK_RIGHT
] = 3; break;
1912 case 6: device
->DevChannels
[FRONT_LEFT
] = 0;
1913 device
->DevChannels
[FRONT_RIGHT
] = 1;
1914 device
->DevChannels
[BACK_LEFT
] = 2;
1915 device
->DevChannels
[BACK_RIGHT
] = 3;
1916 device
->DevChannels
[FRONT_CENTER
] = 4;
1917 device
->DevChannels
[LFE
] = 5; break;
1919 case 7: device
->DevChannels
[FRONT_LEFT
] = 0;
1920 device
->DevChannels
[FRONT_RIGHT
] = 1;
1921 device
->DevChannels
[FRONT_CENTER
] = 2;
1922 device
->DevChannels
[LFE
] = 3;
1923 device
->DevChannels
[BACK_CENTER
] = 4;
1924 device
->DevChannels
[SIDE_LEFT
] = 5;
1925 device
->DevChannels
[SIDE_RIGHT
] = 6; break;
1927 case 8: device
->DevChannels
[FRONT_LEFT
] = 0;
1928 device
->DevChannels
[FRONT_RIGHT
] = 1;
1929 device
->DevChannels
[BACK_LEFT
] = 2;
1930 device
->DevChannels
[BACK_RIGHT
] = 3;
1931 device
->DevChannels
[FRONT_CENTER
] = 4;
1932 device
->DevChannels
[LFE
] = 5;
1933 device
->DevChannels
[SIDE_LEFT
] = 6;
1934 device
->DevChannels
[SIDE_RIGHT
] = 7; break;
1937 // Sets the default order used by WaveFormatEx
1938 void SetDefaultWFXChannelOrder(ALCdevice
*device
)
1940 switch(aluChannelsFromFormat(device
->Format
))
1942 case 1: device
->DevChannels
[FRONT_CENTER
] = 0; break;
1944 case 2: device
->DevChannels
[FRONT_LEFT
] = 0;
1945 device
->DevChannels
[FRONT_RIGHT
] = 1; break;
1947 case 4: device
->DevChannels
[FRONT_LEFT
] = 0;
1948 device
->DevChannels
[FRONT_RIGHT
] = 1;
1949 device
->DevChannels
[BACK_LEFT
] = 2;
1950 device
->DevChannels
[BACK_RIGHT
] = 3; break;
1952 case 6: device
->DevChannels
[FRONT_LEFT
] = 0;
1953 device
->DevChannels
[FRONT_RIGHT
] = 1;
1954 device
->DevChannels
[FRONT_CENTER
] = 2;
1955 device
->DevChannels
[LFE
] = 3;
1956 device
->DevChannels
[BACK_LEFT
] = 4;
1957 device
->DevChannels
[BACK_RIGHT
] = 5; break;
1959 case 7: device
->DevChannels
[FRONT_LEFT
] = 0;
1960 device
->DevChannels
[FRONT_RIGHT
] = 1;
1961 device
->DevChannels
[FRONT_CENTER
] = 2;
1962 device
->DevChannels
[LFE
] = 3;
1963 device
->DevChannels
[BACK_CENTER
] = 4;
1964 device
->DevChannels
[SIDE_LEFT
] = 5;
1965 device
->DevChannels
[SIDE_RIGHT
] = 6; break;
1967 case 8: device
->DevChannels
[FRONT_LEFT
] = 0;
1968 device
->DevChannels
[FRONT_RIGHT
] = 1;
1969 device
->DevChannels
[FRONT_CENTER
] = 2;
1970 device
->DevChannels
[LFE
] = 3;
1971 device
->DevChannels
[BACK_LEFT
] = 4;
1972 device
->DevChannels
[BACK_RIGHT
] = 5;
1973 device
->DevChannels
[SIDE_LEFT
] = 6;
1974 device
->DevChannels
[SIDE_RIGHT
] = 7; break;
1978 static ALenum
GetFormatFromString(const char *str
)
1980 if(strcasecmp(str
, "AL_FORMAT_MONO32") == 0) return AL_FORMAT_MONO_FLOAT32
;
1981 if(strcasecmp(str
, "AL_FORMAT_STEREO32") == 0) return AL_FORMAT_STEREO_FLOAT32
;
1982 if(strcasecmp(str
, "AL_FORMAT_QUAD32") == 0) return AL_FORMAT_QUAD32
;
1983 if(strcasecmp(str
, "AL_FORMAT_51CHN32") == 0) return AL_FORMAT_51CHN32
;
1984 if(strcasecmp(str
, "AL_FORMAT_61CHN32") == 0) return AL_FORMAT_61CHN32
;
1985 if(strcasecmp(str
, "AL_FORMAT_71CHN32") == 0) return AL_FORMAT_71CHN32
;
1987 if(strcasecmp(str
, "AL_FORMAT_MONO16") == 0) return AL_FORMAT_MONO16
;
1988 if(strcasecmp(str
, "AL_FORMAT_STEREO16") == 0) return AL_FORMAT_STEREO16
;
1989 if(strcasecmp(str
, "AL_FORMAT_QUAD16") == 0) return AL_FORMAT_QUAD16
;
1990 if(strcasecmp(str
, "AL_FORMAT_51CHN16") == 0) return AL_FORMAT_51CHN16
;
1991 if(strcasecmp(str
, "AL_FORMAT_61CHN16") == 0) return AL_FORMAT_61CHN16
;
1992 if(strcasecmp(str
, "AL_FORMAT_71CHN16") == 0) return AL_FORMAT_71CHN16
;
1994 if(strcasecmp(str
, "AL_FORMAT_MONO8") == 0) return AL_FORMAT_MONO8
;
1995 if(strcasecmp(str
, "AL_FORMAT_STEREO8") == 0) return AL_FORMAT_STEREO8
;
1996 if(strcasecmp(str
, "AL_FORMAT_QUAD8") == 0) return AL_FORMAT_QUAD8
;
1997 if(strcasecmp(str
, "AL_FORMAT_51CHN8") == 0) return AL_FORMAT_51CHN8
;
1998 if(strcasecmp(str
, "AL_FORMAT_61CHN8") == 0) return AL_FORMAT_61CHN8
;
1999 if(strcasecmp(str
, "AL_FORMAT_71CHN8") == 0) return AL_FORMAT_71CHN8
;
2001 AL_PRINT("Unknown format: \"%s\"\n", str
);
2002 return AL_FORMAT_STEREO16
;
2008 Open the Device specified.
2010 ALC_API ALCdevice
* ALC_APIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
2012 ALboolean bDeviceFound
= AL_FALSE
;
2017 if(deviceName
&& !deviceName
[0])
2020 device
= calloc(1, sizeof(ALCdevice
));
2023 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
2028 device
->Connected
= ALC_TRUE
;
2029 device
->IsCaptureDevice
= AL_FALSE
;
2030 device
->LastError
= ALC_NO_ERROR
;
2032 device
->Bs2b
= NULL
;
2033 device
->szDeviceName
= NULL
;
2035 device
->Contexts
= NULL
;
2036 device
->NumContexts
= 0;
2038 device
->SamplesPlayed
= 0;
2040 device
->TimeRes
= 1;
2042 InitUIntMap(&device
->BufferMap
);
2043 InitUIntMap(&device
->EffectMap
);
2044 InitUIntMap(&device
->FilterMap
);
2045 InitUIntMap(&device
->DatabufferMap
);
2048 device
->Frequency
= GetConfigValueInt(NULL
, "frequency", SWMIXER_OUTPUT_RATE
);
2049 if(device
->Frequency
< 8000)
2050 device
->Frequency
= 8000;
2052 fmt
= GetConfigValue(NULL
, "format", "AL_FORMAT_STEREO16");
2053 device
->Format
= GetFormatFromString(fmt
);
2055 device
->NumUpdates
= GetConfigValueInt(NULL
, "periods", 4);
2056 if(device
->NumUpdates
< 2)
2057 device
->NumUpdates
= 4;
2059 device
->UpdateSize
= GetConfigValueInt(NULL
, "period_size", 1024);
2060 if(device
->UpdateSize
<= 0)
2061 device
->UpdateSize
= 1024;
2063 device
->MaxNoOfSources
= GetConfigValueInt(NULL
, "sources", 256);
2064 if((ALint
)device
->MaxNoOfSources
<= 0)
2065 device
->MaxNoOfSources
= 256;
2067 device
->AuxiliaryEffectSlotMax
= GetConfigValueInt(NULL
, "slots", 4);
2068 if((ALint
)device
->AuxiliaryEffectSlotMax
<= 0)
2069 device
->AuxiliaryEffectSlotMax
= 4;
2071 device
->NumStereoSources
= 1;
2072 device
->NumMonoSources
= device
->MaxNoOfSources
- device
->NumStereoSources
;
2074 device
->NumAuxSends
= GetConfigValueInt(NULL
, "sends", MAX_SENDS
);
2075 if(device
->NumAuxSends
> MAX_SENDS
)
2076 device
->NumAuxSends
= MAX_SENDS
;
2078 device
->Bs2bLevel
= GetConfigValueInt(NULL
, "cf_level", 0);
2080 device
->DuplicateStereo
= GetConfigValueBool(NULL
, "stereodup", 1);
2082 device
->HeadDampen
= 0.0f
;
2084 // Find a playback device to open
2085 SuspendContext(NULL
);
2086 for(i
= 0;BackendList
[i
].Init
;i
++)
2088 device
->Funcs
= &BackendList
[i
].Funcs
;
2089 if(ALCdevice_OpenPlayback(device
, deviceName
))
2091 device
->next
= g_pDeviceList
;
2092 g_pDeviceList
= device
;
2095 bDeviceFound
= AL_TRUE
;
2099 ProcessContext(NULL
);
2103 // No suitable output device found
2104 alcSetError(NULL
, ALC_INVALID_VALUE
);
2116 Close the specified Device
2118 ALC_API ALCboolean ALC_APIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
2122 if(!IsDevice(pDevice
) || pDevice
->IsCaptureDevice
)
2124 alcSetError(pDevice
, ALC_INVALID_DEVICE
);
2128 SuspendContext(NULL
);
2130 list
= &g_pDeviceList
;
2131 while(*list
!= pDevice
)
2132 list
= &(*list
)->next
;
2134 *list
= (*list
)->next
;
2137 ProcessContext(NULL
);
2139 if(pDevice
->NumContexts
> 0)
2142 AL_PRINT("alcCloseDevice(): destroying %u Context(s)\n", pDevice
->NumContexts
);
2144 while(pDevice
->NumContexts
> 0)
2145 alcDestroyContext(pDevice
->Contexts
[0]);
2147 ALCdevice_ClosePlayback(pDevice
);
2149 if(pDevice
->BufferMap
.size
> 0)
2152 AL_PRINT("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice
->BufferMap
.size
);
2154 ReleaseALBuffers(pDevice
);
2156 ResetUIntMap(&pDevice
->BufferMap
);
2158 if(pDevice
->EffectMap
.size
> 0)
2161 AL_PRINT("alcCloseDevice(): deleting %d Effect(s)\n", pDevice
->EffectMap
.size
);
2163 ReleaseALEffects(pDevice
);
2165 ResetUIntMap(&pDevice
->EffectMap
);
2167 if(pDevice
->FilterMap
.size
> 0)
2170 AL_PRINT("alcCloseDevice(): deleting %d Filter(s)\n", pDevice
->FilterMap
.size
);
2172 ReleaseALFilters(pDevice
);
2174 ResetUIntMap(&pDevice
->FilterMap
);
2176 if(pDevice
->DatabufferMap
.size
> 0)
2179 AL_PRINT("alcCloseDevice(): deleting %d Databuffer(s)\n", pDevice
->DatabufferMap
.size
);
2181 ReleaseALDatabuffers(pDevice
);
2183 ResetUIntMap(&pDevice
->DatabufferMap
);
2185 free(pDevice
->Bs2b
);
2186 pDevice
->Bs2b
= NULL
;
2188 free(pDevice
->szDeviceName
);
2189 pDevice
->szDeviceName
= NULL
;
2191 free(pDevice
->Contexts
);
2192 pDevice
->Contexts
= NULL
;
2194 //Release device structure
2195 memset(pDevice
, 0, sizeof(ALCdevice
));
2202 ALCvoid
ReleaseALC(ALCvoid
)
2204 free(alcDeviceList
); alcDeviceList
= NULL
;
2205 alcDeviceListSize
= 0;
2206 free(alcAllDeviceList
); alcAllDeviceList
= NULL
;
2207 alcAllDeviceListSize
= 0;
2208 free(alcCaptureDeviceList
); alcCaptureDeviceList
= NULL
;
2209 alcCaptureDeviceListSize
= 0;
2211 free(alcDefaultDeviceSpecifier
);
2212 alcDefaultDeviceSpecifier
= NULL
;
2213 free(alcDefaultAllDeviceSpecifier
);
2214 alcDefaultAllDeviceSpecifier
= NULL
;
2215 free(alcCaptureDefaultDeviceSpecifier
);
2216 alcCaptureDefaultDeviceSpecifier
= NULL
;
2219 if(g_ulDeviceCount
> 0)
2220 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount
, (g_ulDeviceCount
>1)?"s":"");
2223 while(g_pDeviceList
)
2225 if(g_pDeviceList
->IsCaptureDevice
)
2226 alcCaptureCloseDevice(g_pDeviceList
);
2228 alcCloseDevice(g_pDeviceList
);
2232 ///////////////////////////////////////////////////////