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
},
74 { "wave", alc_wave_init
, alc_wave_deinit
, alc_wave_probe
, EmptyFuncs
},
76 { NULL
, NULL
, NULL
, NULL
, EmptyFuncs
}
80 ///////////////////////////////////////////////////////
82 #define ALC_EFX_MAJOR_VERSION 0x20001
83 #define ALC_EFX_MINOR_VERSION 0x20002
84 #define ALC_MAX_AUXILIARY_SENDS 0x20003
86 ///////////////////////////////////////////////////////
87 // STRING and EXTENSIONS
89 typedef struct ALCfunction
{
90 const ALCchar
*funcName
;
94 typedef struct ALCenums
{
95 const ALCchar
*enumName
;
100 static const ALCfunction alcFunctions
[] = {
101 { "alcCreateContext", (ALCvoid
*) alcCreateContext
},
102 { "alcMakeContextCurrent", (ALCvoid
*) alcMakeContextCurrent
},
103 { "alcProcessContext", (ALCvoid
*) alcProcessContext
},
104 { "alcSuspendContext", (ALCvoid
*) alcSuspendContext
},
105 { "alcDestroyContext", (ALCvoid
*) alcDestroyContext
},
106 { "alcGetCurrentContext", (ALCvoid
*) alcGetCurrentContext
},
107 { "alcGetContextsDevice", (ALCvoid
*) alcGetContextsDevice
},
108 { "alcOpenDevice", (ALCvoid
*) alcOpenDevice
},
109 { "alcCloseDevice", (ALCvoid
*) alcCloseDevice
},
110 { "alcGetError", (ALCvoid
*) alcGetError
},
111 { "alcIsExtensionPresent", (ALCvoid
*) alcIsExtensionPresent
},
112 { "alcGetProcAddress", (ALCvoid
*) alcGetProcAddress
},
113 { "alcGetEnumValue", (ALCvoid
*) alcGetEnumValue
},
114 { "alcGetString", (ALCvoid
*) alcGetString
},
115 { "alcGetIntegerv", (ALCvoid
*) alcGetIntegerv
},
116 { "alcCaptureOpenDevice", (ALCvoid
*) alcCaptureOpenDevice
},
117 { "alcCaptureCloseDevice", (ALCvoid
*) alcCaptureCloseDevice
},
118 { "alcCaptureStart", (ALCvoid
*) alcCaptureStart
},
119 { "alcCaptureStop", (ALCvoid
*) alcCaptureStop
},
120 { "alcCaptureSamples", (ALCvoid
*) alcCaptureSamples
},
122 { "alcSetThreadContext", (ALCvoid
*) alcSetThreadContext
},
123 { "alcGetThreadContext", (ALCvoid
*) alcGetThreadContext
},
125 { "alEnable", (ALCvoid
*) alEnable
},
126 { "alDisable", (ALCvoid
*) alDisable
},
127 { "alIsEnabled", (ALCvoid
*) alIsEnabled
},
128 { "alGetString", (ALCvoid
*) alGetString
},
129 { "alGetBooleanv", (ALCvoid
*) alGetBooleanv
},
130 { "alGetIntegerv", (ALCvoid
*) alGetIntegerv
},
131 { "alGetFloatv", (ALCvoid
*) alGetFloatv
},
132 { "alGetDoublev", (ALCvoid
*) alGetDoublev
},
133 { "alGetBoolean", (ALCvoid
*) alGetBoolean
},
134 { "alGetInteger", (ALCvoid
*) alGetInteger
},
135 { "alGetFloat", (ALCvoid
*) alGetFloat
},
136 { "alGetDouble", (ALCvoid
*) alGetDouble
},
137 { "alGetError", (ALCvoid
*) alGetError
},
138 { "alIsExtensionPresent", (ALCvoid
*) alIsExtensionPresent
},
139 { "alGetProcAddress", (ALCvoid
*) alGetProcAddress
},
140 { "alGetEnumValue", (ALCvoid
*) alGetEnumValue
},
141 { "alListenerf", (ALCvoid
*) alListenerf
},
142 { "alListener3f", (ALCvoid
*) alListener3f
},
143 { "alListenerfv", (ALCvoid
*) alListenerfv
},
144 { "alListeneri", (ALCvoid
*) alListeneri
},
145 { "alListener3i", (ALCvoid
*) alListener3i
},
146 { "alListeneriv", (ALCvoid
*) alListeneriv
},
147 { "alGetListenerf", (ALCvoid
*) alGetListenerf
},
148 { "alGetListener3f", (ALCvoid
*) alGetListener3f
},
149 { "alGetListenerfv", (ALCvoid
*) alGetListenerfv
},
150 { "alGetListeneri", (ALCvoid
*) alGetListeneri
},
151 { "alGetListener3i", (ALCvoid
*) alGetListener3i
},
152 { "alGetListeneriv", (ALCvoid
*) alGetListeneriv
},
153 { "alGenSources", (ALCvoid
*) alGenSources
},
154 { "alDeleteSources", (ALCvoid
*) alDeleteSources
},
155 { "alIsSource", (ALCvoid
*) alIsSource
},
156 { "alSourcef", (ALCvoid
*) alSourcef
},
157 { "alSource3f", (ALCvoid
*) alSource3f
},
158 { "alSourcefv", (ALCvoid
*) alSourcefv
},
159 { "alSourcei", (ALCvoid
*) alSourcei
},
160 { "alSource3i", (ALCvoid
*) alSource3i
},
161 { "alSourceiv", (ALCvoid
*) alSourceiv
},
162 { "alGetSourcef", (ALCvoid
*) alGetSourcef
},
163 { "alGetSource3f", (ALCvoid
*) alGetSource3f
},
164 { "alGetSourcefv", (ALCvoid
*) alGetSourcefv
},
165 { "alGetSourcei", (ALCvoid
*) alGetSourcei
},
166 { "alGetSource3i", (ALCvoid
*) alGetSource3i
},
167 { "alGetSourceiv", (ALCvoid
*) alGetSourceiv
},
168 { "alSourcePlayv", (ALCvoid
*) alSourcePlayv
},
169 { "alSourceStopv", (ALCvoid
*) alSourceStopv
},
170 { "alSourceRewindv", (ALCvoid
*) alSourceRewindv
},
171 { "alSourcePausev", (ALCvoid
*) alSourcePausev
},
172 { "alSourcePlay", (ALCvoid
*) alSourcePlay
},
173 { "alSourceStop", (ALCvoid
*) alSourceStop
},
174 { "alSourceRewind", (ALCvoid
*) alSourceRewind
},
175 { "alSourcePause", (ALCvoid
*) alSourcePause
},
176 { "alSourceQueueBuffers", (ALCvoid
*) alSourceQueueBuffers
},
177 { "alSourceUnqueueBuffers", (ALCvoid
*) alSourceUnqueueBuffers
},
178 { "alGenBuffers", (ALCvoid
*) alGenBuffers
},
179 { "alDeleteBuffers", (ALCvoid
*) alDeleteBuffers
},
180 { "alIsBuffer", (ALCvoid
*) alIsBuffer
},
181 { "alBufferData", (ALCvoid
*) alBufferData
},
182 { "alBufferf", (ALCvoid
*) alBufferf
},
183 { "alBuffer3f", (ALCvoid
*) alBuffer3f
},
184 { "alBufferfv", (ALCvoid
*) alBufferfv
},
185 { "alBufferi", (ALCvoid
*) alBufferi
},
186 { "alBuffer3i", (ALCvoid
*) alBuffer3i
},
187 { "alBufferiv", (ALCvoid
*) alBufferiv
},
188 { "alGetBufferf", (ALCvoid
*) alGetBufferf
},
189 { "alGetBuffer3f", (ALCvoid
*) alGetBuffer3f
},
190 { "alGetBufferfv", (ALCvoid
*) alGetBufferfv
},
191 { "alGetBufferi", (ALCvoid
*) alGetBufferi
},
192 { "alGetBuffer3i", (ALCvoid
*) alGetBuffer3i
},
193 { "alGetBufferiv", (ALCvoid
*) alGetBufferiv
},
194 { "alDopplerFactor", (ALCvoid
*) alDopplerFactor
},
195 { "alDopplerVelocity", (ALCvoid
*) alDopplerVelocity
},
196 { "alSpeedOfSound", (ALCvoid
*) alSpeedOfSound
},
197 { "alDistanceModel", (ALCvoid
*) alDistanceModel
},
199 { "alGenFilters", (ALCvoid
*) alGenFilters
},
200 { "alDeleteFilters", (ALCvoid
*) alDeleteFilters
},
201 { "alIsFilter", (ALCvoid
*) alIsFilter
},
202 { "alFilteri", (ALCvoid
*) alFilteri
},
203 { "alFilteriv", (ALCvoid
*) alFilteriv
},
204 { "alFilterf", (ALCvoid
*) alFilterf
},
205 { "alFilterfv", (ALCvoid
*) alFilterfv
},
206 { "alGetFilteri", (ALCvoid
*) alGetFilteri
},
207 { "alGetFilteriv", (ALCvoid
*) alGetFilteriv
},
208 { "alGetFilterf", (ALCvoid
*) alGetFilterf
},
209 { "alGetFilterfv", (ALCvoid
*) alGetFilterfv
},
211 { "alGenEffects", (ALCvoid
*) alGenEffects
},
212 { "alDeleteEffects", (ALCvoid
*) alDeleteEffects
},
213 { "alIsEffect", (ALCvoid
*) alIsEffect
},
214 { "alEffecti", (ALCvoid
*) alEffecti
},
215 { "alEffectiv", (ALCvoid
*) alEffectiv
},
216 { "alEffectf", (ALCvoid
*) alEffectf
},
217 { "alEffectfv", (ALCvoid
*) alEffectfv
},
218 { "alGetEffecti", (ALCvoid
*) alGetEffecti
},
219 { "alGetEffectiv", (ALCvoid
*) alGetEffectiv
},
220 { "alGetEffectf", (ALCvoid
*) alGetEffectf
},
221 { "alGetEffectfv", (ALCvoid
*) alGetEffectfv
},
223 { "alGenAuxiliaryEffectSlots", (ALCvoid
*) alGenAuxiliaryEffectSlots
},
224 { "alDeleteAuxiliaryEffectSlots",(ALCvoid
*) alDeleteAuxiliaryEffectSlots
},
225 { "alIsAuxiliaryEffectSlot", (ALCvoid
*) alIsAuxiliaryEffectSlot
},
226 { "alAuxiliaryEffectSloti", (ALCvoid
*) alAuxiliaryEffectSloti
},
227 { "alAuxiliaryEffectSlotiv", (ALCvoid
*) alAuxiliaryEffectSlotiv
},
228 { "alAuxiliaryEffectSlotf", (ALCvoid
*) alAuxiliaryEffectSlotf
},
229 { "alAuxiliaryEffectSlotfv", (ALCvoid
*) alAuxiliaryEffectSlotfv
},
230 { "alGetAuxiliaryEffectSloti", (ALCvoid
*) alGetAuxiliaryEffectSloti
},
231 { "alGetAuxiliaryEffectSlotiv", (ALCvoid
*) alGetAuxiliaryEffectSlotiv
},
232 { "alGetAuxiliaryEffectSlotf", (ALCvoid
*) alGetAuxiliaryEffectSlotf
},
233 { "alGetAuxiliaryEffectSlotfv", (ALCvoid
*) alGetAuxiliaryEffectSlotfv
},
235 { "alBufferSubDataEXT", (ALCvoid
*) alBufferSubDataEXT
},
237 { "alGenDatabuffersEXT", (ALCvoid
*) alGenDatabuffersEXT
},
238 { "alDeleteDatabuffersEXT", (ALCvoid
*) alDeleteDatabuffersEXT
},
239 { "alIsDatabufferEXT", (ALCvoid
*) alIsDatabufferEXT
},
240 { "alDatabufferDataEXT", (ALCvoid
*) alDatabufferDataEXT
},
241 { "alDatabufferSubDataEXT", (ALCvoid
*) alDatabufferSubDataEXT
},
242 { "alGetDatabufferSubDataEXT", (ALCvoid
*) alGetDatabufferSubDataEXT
},
243 { "alDatabufferfEXT", (ALCvoid
*) alDatabufferfEXT
},
244 { "alDatabufferfvEXT", (ALCvoid
*) alDatabufferfvEXT
},
245 { "alDatabufferiEXT", (ALCvoid
*) alDatabufferiEXT
},
246 { "alDatabufferivEXT", (ALCvoid
*) alDatabufferivEXT
},
247 { "alGetDatabufferfEXT", (ALCvoid
*) alGetDatabufferfEXT
},
248 { "alGetDatabufferfvEXT", (ALCvoid
*) alGetDatabufferfvEXT
},
249 { "alGetDatabufferiEXT", (ALCvoid
*) alGetDatabufferiEXT
},
250 { "alGetDatabufferivEXT", (ALCvoid
*) alGetDatabufferivEXT
},
251 { "alSelectDatabufferEXT", (ALCvoid
*) alSelectDatabufferEXT
},
252 { "alMapDatabufferEXT", (ALCvoid
*) alMapDatabufferEXT
},
253 { "alUnmapDatabufferEXT", (ALCvoid
*) alUnmapDatabufferEXT
},
255 { NULL
, (ALCvoid
*) NULL
}
258 static const ALCenums enumeration
[] = {
260 { "ALC_INVALID", ALC_INVALID
},
261 { "ALC_FALSE", ALC_FALSE
},
262 { "ALC_TRUE", ALC_TRUE
},
265 { "ALC_MAJOR_VERSION", ALC_MAJOR_VERSION
},
266 { "ALC_MINOR_VERSION", ALC_MINOR_VERSION
},
267 { "ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE
},
268 { "ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES
},
269 { "ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER
},
270 { "ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER
},
271 { "ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER
},
272 { "ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER
},
273 { "ALC_EXTENSIONS", ALC_EXTENSIONS
},
274 { "ALC_FREQUENCY", ALC_FREQUENCY
},
275 { "ALC_REFRESH", ALC_REFRESH
},
276 { "ALC_SYNC", ALC_SYNC
},
277 { "ALC_MONO_SOURCES", ALC_MONO_SOURCES
},
278 { "ALC_STEREO_SOURCES", ALC_STEREO_SOURCES
},
279 { "ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER
},
280 { "ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
},
281 { "ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES
},
282 { "ALC_CONNECTED", ALC_CONNECTED
},
285 { "ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION
},
286 { "ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION
},
287 { "ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS
},
290 { "ALC_NO_ERROR", ALC_NO_ERROR
},
291 { "ALC_INVALID_DEVICE", ALC_INVALID_DEVICE
},
292 { "ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT
},
293 { "ALC_INVALID_ENUM", ALC_INVALID_ENUM
},
294 { "ALC_INVALID_VALUE", ALC_INVALID_VALUE
},
295 { "ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY
},
299 static const ALCchar alcNoError
[] = "No Error";
300 static const ALCchar alcErrInvalidDevice
[] = "Invalid Device";
301 static const ALCchar alcErrInvalidContext
[] = "Invalid Context";
302 static const ALCchar alcErrInvalidEnum
[] = "Invalid Enum";
303 static const ALCchar alcErrInvalidValue
[] = "Invalid Value";
304 static const ALCchar alcErrOutOfMemory
[] = "Out of Memory";
306 /* Device lists. Sizes only include the first ending null character, not the
308 static ALCchar
*alcDeviceList
;
309 static size_t alcDeviceListSize
;
310 static ALCchar
*alcAllDeviceList
;
311 static size_t alcAllDeviceListSize
;
312 static ALCchar
*alcCaptureDeviceList
;
313 static size_t alcCaptureDeviceListSize
;
314 // Default is always the first in the list
315 static ALCchar
*alcDefaultDeviceSpecifier
;
316 static ALCchar
*alcDefaultAllDeviceSpecifier
;
317 static ALCchar
*alcCaptureDefaultDeviceSpecifier
;
320 static const ALCchar alcNoDeviceExtList
[] =
321 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
322 "ALC_EXT_thread_local_context";
323 static const ALCchar alcExtensionList
[] =
324 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
325 "ALC_EXT_disconnect ALC_EXT_EFX ALC_EXT_thread_local_context";
326 static const ALCint alcMajorVersion
= 1;
327 static const ALCint alcMinorVersion
= 1;
329 static const ALCint alcEFXMajorVersion
= 1;
330 static const ALCint alcEFXMinorVersion
= 0;
332 ///////////////////////////////////////////////////////
335 ///////////////////////////////////////////////////////
338 static ALCdevice
*g_pDeviceList
= NULL
;
339 static ALCuint g_ulDeviceCount
= 0;
341 static CRITICAL_SECTION g_csMutex
;
344 static ALCcontext
*g_pContextList
= NULL
;
345 static ALCuint g_ulContextCount
= 0;
347 // Thread-local current context
348 static tls_type LocalContext
;
349 // Process-wide current context
350 static ALCcontext
*GlobalContext
;
353 static ALCenum g_eLastNullDeviceError
= ALC_NO_ERROR
;
355 // Default context extensions
356 static const ALchar alExtList
[] =
357 "AL_EXTX_buffer_sub_data AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE "
358 "AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXTX_loop_points "
359 "AL_EXT_MCFORMATS AL_EXT_MULAW AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET "
360 "AL_EXTX_sample_buffer_object AL_EXT_source_distance_model "
361 "AL_LOKI_quadriphonic";
363 // Mixing Priority Level
364 static ALint RTPrioLevel
;
367 resampler_t DefaultResampler
;
370 static FILE *LogFile
;
372 ///////////////////////////////////////////////////////
375 ///////////////////////////////////////////////////////
376 // ALC Related helper functions
378 static void alc_init(void);
379 static void alc_deinit(void);
381 BOOL APIENTRY
DllMain(HANDLE hModule
,DWORD ul_reason_for_call
,LPVOID lpReserved
)
385 // Perform actions based on the reason for calling.
386 switch(ul_reason_for_call
)
388 case DLL_PROCESS_ATTACH
:
389 DisableThreadLibraryCalls(hModule
);
393 case DLL_PROCESS_DETACH
:
400 #ifdef HAVE_GCC_DESTRUCTOR
401 static void alc_init(void) __attribute__((constructor
));
402 static void alc_deinit(void) __attribute__((destructor
));
406 static void alc_init(void)
409 const char *devs
, *str
;
411 InitializeCriticalSection(&g_csMutex
);
415 tls_create(&LocalContext
);
417 str
= getenv("ALSOFT_LOGFILE");
420 LogFile
= fopen(str
, "w");
422 fprintf(stderr
, "AL lib: Failed to open log file '%s'\n", str
);
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 if(LogFile
!= stderr
)
546 tls_delete(LocalContext
);
550 DeleteCriticalSection(&g_csMutex
);
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
;
821 ALCvoid
SuspendContext(ALCcontext
*pContext
)
824 EnterCriticalSection(&g_csMutex
);
833 ALCvoid
ProcessContext(ALCcontext
*pContext
)
836 LeaveCriticalSection(&g_csMutex
);
843 Returns the currently active Context, in a locked state
845 ALCcontext
*GetContextSuspended(void)
847 ALCcontext
*pContext
= NULL
;
849 SuspendContext(NULL
);
851 pContext
= tls_get(LocalContext
);
852 if(pContext
&& !IsContext(pContext
))
854 tls_set(LocalContext
, NULL
);
858 pContext
= GlobalContext
;
861 SuspendContext(pContext
);
863 ProcessContext(NULL
);
872 Initialize Context variables
874 static ALvoid
InitContext(ALCcontext
*pContext
)
876 //Initialise listener
877 pContext
->Listener
.Gain
= 1.0f
;
878 pContext
->Listener
.MetersPerUnit
= 1.0f
;
879 pContext
->Listener
.Position
[0] = 0.0f
;
880 pContext
->Listener
.Position
[1] = 0.0f
;
881 pContext
->Listener
.Position
[2] = 0.0f
;
882 pContext
->Listener
.Velocity
[0] = 0.0f
;
883 pContext
->Listener
.Velocity
[1] = 0.0f
;
884 pContext
->Listener
.Velocity
[2] = 0.0f
;
885 pContext
->Listener
.Forward
[0] = 0.0f
;
886 pContext
->Listener
.Forward
[1] = 0.0f
;
887 pContext
->Listener
.Forward
[2] = -1.0f
;
888 pContext
->Listener
.Up
[0] = 0.0f
;
889 pContext
->Listener
.Up
[1] = 1.0f
;
890 pContext
->Listener
.Up
[2] = 0.0f
;
893 pContext
->LastError
= AL_NO_ERROR
;
894 pContext
->Suspended
= AL_FALSE
;
895 InitUIntMap(&pContext
->SourceMap
);
898 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
899 pContext
->SourceDistanceModel
= AL_FALSE
;
900 pContext
->DopplerFactor
= 1.0f
;
901 pContext
->DopplerVelocity
= 1.0f
;
902 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
904 pContext
->ExtensionList
= alExtList
;
911 Clean up Context, destroy any remaining Sources
913 static ALCvoid
ExitContext(ALCcontext
*pContext
)
916 pContext
->LastError
= AL_NO_ERROR
;
919 ///////////////////////////////////////////////////////
922 ///////////////////////////////////////////////////////
923 // ALC Functions calls
926 // This should probably move to another c file but for now ...
927 ALC_API ALCdevice
* ALC_APIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
929 ALCboolean DeviceFound
= ALC_FALSE
;
930 ALCdevice
*device
= NULL
;
935 alcSetError(NULL
, ALC_INVALID_VALUE
);
939 if(deviceName
&& !deviceName
[0])
942 device
= calloc(1, sizeof(ALCdevice
));
945 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
950 device
->Connected
= ALC_TRUE
;
951 device
->IsCaptureDevice
= AL_TRUE
;
953 device
->szDeviceName
= NULL
;
955 device
->Frequency
= frequency
;
956 device
->Format
= format
;
957 device
->UpdateSize
= SampleSize
;
958 device
->NumUpdates
= 1;
960 SuspendContext(NULL
);
961 for(i
= 0;BackendList
[i
].Init
;i
++)
963 device
->Funcs
= &BackendList
[i
].Funcs
;
964 if(ALCdevice_OpenCapture(device
, deviceName
))
966 device
->next
= g_pDeviceList
;
967 g_pDeviceList
= device
;
970 DeviceFound
= ALC_TRUE
;
974 ProcessContext(NULL
);
978 alcSetError(NULL
, ALC_INVALID_VALUE
);
986 ALC_API ALCboolean ALC_APIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
990 if(!IsDevice(pDevice
) || !pDevice
->IsCaptureDevice
)
992 alcSetError(pDevice
, ALC_INVALID_DEVICE
);
996 SuspendContext(NULL
);
998 list
= &g_pDeviceList
;
999 while(*list
!= pDevice
)
1000 list
= &(*list
)->next
;
1002 *list
= (*list
)->next
;
1005 ProcessContext(NULL
);
1007 ALCdevice_CloseCapture(pDevice
);
1009 free(pDevice
->szDeviceName
);
1010 pDevice
->szDeviceName
= NULL
;
1017 ALC_API
void ALC_APIENTRY
alcCaptureStart(ALCdevice
*device
)
1019 SuspendContext(NULL
);
1020 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
1021 alcSetError(device
, ALC_INVALID_DEVICE
);
1022 else if(device
->Connected
)
1023 ALCdevice_StartCapture(device
);
1024 ProcessContext(NULL
);
1027 ALC_API
void ALC_APIENTRY
alcCaptureStop(ALCdevice
*device
)
1029 SuspendContext(NULL
);
1030 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
1031 alcSetError(device
, ALC_INVALID_DEVICE
);
1033 ALCdevice_StopCapture(device
);
1034 ProcessContext(NULL
);
1037 ALC_API
void ALC_APIENTRY
alcCaptureSamples(ALCdevice
*device
, ALCvoid
*buffer
, ALCsizei samples
)
1039 SuspendContext(NULL
);
1040 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
1041 alcSetError(device
, ALC_INVALID_DEVICE
);
1043 ALCdevice_CaptureSamples(device
, buffer
, samples
);
1044 ProcessContext(NULL
);
1050 Return last ALC generated error code
1052 ALC_API ALCenum ALC_APIENTRY
alcGetError(ALCdevice
*device
)
1056 if(IsDevice(device
))
1058 errorCode
= device
->LastError
;
1059 device
->LastError
= ALC_NO_ERROR
;
1063 errorCode
= g_eLastNullDeviceError
;
1064 g_eLastNullDeviceError
= ALC_NO_ERROR
;
1075 ALC_API ALCvoid ALC_APIENTRY
alcSuspendContext(ALCcontext
*pContext
)
1077 SuspendContext(NULL
);
1078 if(IsContext(pContext
))
1079 pContext
->Suspended
= AL_TRUE
;
1080 ProcessContext(NULL
);
1089 ALC_API ALCvoid ALC_APIENTRY
alcProcessContext(ALCcontext
*pContext
)
1091 SuspendContext(NULL
);
1092 if(IsContext(pContext
))
1093 pContext
->Suspended
= AL_FALSE
;
1094 ProcessContext(NULL
);
1101 Returns information about the Device, and error strings
1103 ALC_API
const ALCchar
* ALC_APIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
1105 const ALCchar
*value
= NULL
;
1113 case ALC_INVALID_ENUM
:
1114 value
= alcErrInvalidEnum
;
1117 case ALC_INVALID_VALUE
:
1118 value
= alcErrInvalidValue
;
1121 case ALC_INVALID_DEVICE
:
1122 value
= alcErrInvalidDevice
;
1125 case ALC_INVALID_CONTEXT
:
1126 value
= alcErrInvalidContext
;
1129 case ALC_OUT_OF_MEMORY
:
1130 value
= alcErrOutOfMemory
;
1133 case ALC_DEVICE_SPECIFIER
:
1134 if(IsDevice(pDevice
))
1135 value
= pDevice
->szDeviceName
;
1139 value
= alcDeviceList
;
1143 case ALC_ALL_DEVICES_SPECIFIER
:
1144 ProbeAllDeviceList();
1145 value
= alcAllDeviceList
;
1148 case ALC_CAPTURE_DEVICE_SPECIFIER
:
1149 if(IsDevice(pDevice
))
1150 value
= pDevice
->szDeviceName
;
1153 ProbeCaptureDeviceList();
1154 value
= alcCaptureDeviceList
;
1158 /* Default devices are always first in the list */
1159 case ALC_DEFAULT_DEVICE_SPECIFIER
:
1163 free(alcDefaultDeviceSpecifier
);
1164 alcDefaultDeviceSpecifier
= strdup(alcDeviceList
? alcDeviceList
: "");
1165 if(!alcDefaultDeviceSpecifier
)
1166 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1167 value
= alcDefaultDeviceSpecifier
;
1170 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
1171 if(!alcAllDeviceList
)
1172 ProbeAllDeviceList();
1174 free(alcDefaultAllDeviceSpecifier
);
1175 alcDefaultAllDeviceSpecifier
= strdup(alcAllDeviceList
?
1176 alcAllDeviceList
: "");
1177 if(!alcDefaultAllDeviceSpecifier
)
1178 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1179 value
= alcDefaultAllDeviceSpecifier
;
1182 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
1183 if(!alcCaptureDeviceList
)
1184 ProbeCaptureDeviceList();
1186 free(alcCaptureDefaultDeviceSpecifier
);
1187 alcCaptureDefaultDeviceSpecifier
= strdup(alcCaptureDeviceList
?
1188 alcCaptureDeviceList
: "");
1189 if(!alcCaptureDefaultDeviceSpecifier
)
1190 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
1191 value
= alcCaptureDefaultDeviceSpecifier
;
1194 case ALC_EXTENSIONS
:
1195 if(IsDevice(pDevice
))
1196 value
= alcExtensionList
;
1198 value
= alcNoDeviceExtList
;
1202 alcSetError(pDevice
, ALC_INVALID_ENUM
);
1213 Returns information about the Device and the version of Open AL
1215 ALC_API ALCvoid ALC_APIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
1217 if(size
== 0 || data
== NULL
)
1219 alcSetError(device
, ALC_INVALID_VALUE
);
1223 if(IsDevice(device
) && device
->IsCaptureDevice
)
1225 SuspendContext(NULL
);
1230 case ALC_CAPTURE_SAMPLES
:
1231 *data
= ALCdevice_AvailableSamples(device
);
1235 *data
= device
->Connected
;
1239 alcSetError(device
, ALC_INVALID_ENUM
);
1243 ProcessContext(NULL
);
1250 case ALC_MAJOR_VERSION
:
1251 *data
= alcMajorVersion
;
1254 case ALC_MINOR_VERSION
:
1255 *data
= alcMinorVersion
;
1258 case ALC_EFX_MAJOR_VERSION
:
1259 *data
= alcEFXMajorVersion
;
1262 case ALC_EFX_MINOR_VERSION
:
1263 *data
= alcEFXMinorVersion
;
1266 case ALC_MAX_AUXILIARY_SENDS
:
1267 if(!IsDevice(device
))
1268 alcSetError(device
, ALC_INVALID_DEVICE
);
1270 *data
= device
->NumAuxSends
;
1273 case ALC_ATTRIBUTES_SIZE
:
1274 if(!IsDevice(device
))
1275 alcSetError(device
, ALC_INVALID_DEVICE
);
1280 case ALC_ALL_ATTRIBUTES
:
1281 if(!IsDevice(device
))
1282 alcSetError(device
, ALC_INVALID_DEVICE
);
1284 alcSetError(device
, ALC_INVALID_VALUE
);
1289 SuspendContext(NULL
);
1290 data
[i
++] = ALC_FREQUENCY
;
1291 data
[i
++] = device
->Frequency
;
1293 data
[i
++] = ALC_REFRESH
;
1294 data
[i
++] = device
->Frequency
/ device
->UpdateSize
;
1296 data
[i
++] = ALC_SYNC
;
1297 data
[i
++] = ALC_FALSE
;
1299 data
[i
++] = ALC_MONO_SOURCES
;
1300 data
[i
++] = device
->NumMonoSources
;
1302 data
[i
++] = ALC_STEREO_SOURCES
;
1303 data
[i
++] = device
->NumStereoSources
;
1305 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
1306 data
[i
++] = device
->NumAuxSends
;
1309 ProcessContext(NULL
);
1314 if(!IsDevice(device
))
1315 alcSetError(device
, ALC_INVALID_DEVICE
);
1317 *data
= device
->Frequency
;
1321 if(!IsDevice(device
))
1322 alcSetError(device
, ALC_INVALID_DEVICE
);
1324 *data
= device
->Frequency
/ device
->UpdateSize
;
1328 if(!IsDevice(device
))
1329 alcSetError(device
, ALC_INVALID_DEVICE
);
1334 case ALC_MONO_SOURCES
:
1335 if(!IsDevice(device
))
1336 alcSetError(device
, ALC_INVALID_DEVICE
);
1338 *data
= device
->NumMonoSources
;
1341 case ALC_STEREO_SOURCES
:
1342 if(!IsDevice(device
))
1343 alcSetError(device
, ALC_INVALID_DEVICE
);
1345 *data
= device
->NumStereoSources
;
1349 if(!IsDevice(device
))
1350 alcSetError(device
, ALC_INVALID_DEVICE
);
1352 *data
= device
->Connected
;
1356 alcSetError(device
, ALC_INVALID_ENUM
);
1363 alcIsExtensionPresent
1365 Determines if there is support for a particular extension
1367 ALC_API ALCboolean ALC_APIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
1369 ALCboolean bResult
= ALC_FALSE
;
1375 alcSetError(device
, ALC_INVALID_VALUE
);
1379 len
= strlen(extName
);
1380 ptr
= (IsDevice(device
) ? alcExtensionList
: alcNoDeviceExtList
);
1383 if(strncasecmp(ptr
, extName
, len
) == 0 &&
1384 (ptr
[len
] == '\0' || isspace(ptr
[len
])))
1389 if((ptr
=strchr(ptr
, ' ')) != NULL
)
1393 } while(isspace(*ptr
));
1404 Retrieves the function address for a particular extension function
1406 ALC_API ALCvoid
* ALC_APIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
1412 alcSetError(device
, ALC_INVALID_VALUE
);
1416 while(alcFunctions
[i
].funcName
&& strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
1418 return alcFunctions
[i
].address
;
1425 Get the value for a particular ALC Enumerated Value
1427 ALC_API ALCenum ALC_APIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
1433 alcSetError(device
, ALC_INVALID_VALUE
);
1437 while(enumeration
[i
].enumName
&& strcmp(enumeration
[i
].enumName
,enumName
) != 0)
1439 return enumeration
[i
].value
;
1446 Create and attach a Context to a particular Device.
1448 ALC_API ALCcontext
* ALC_APIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
1450 ALCcontext
*ALContext
;
1456 SuspendContext(NULL
);
1458 if(!IsDevice(device
) || device
->IsCaptureDevice
|| !device
->Connected
)
1460 alcSetError(device
, ALC_INVALID_DEVICE
);
1461 ProcessContext(NULL
);
1465 running
= ((device
->NumContexts
> 0) ? AL_TRUE
: AL_FALSE
);
1467 // Reset Context Last Error code
1468 device
->LastError
= ALC_NO_ERROR
;
1470 // Check for attributes
1471 if(attrList
&& attrList
[0])
1473 ALCuint freq
, numMono
, numStereo
, numSends
;
1475 // If a context is already running on the device, stop playback so the
1476 // device attributes can be updated
1479 ProcessContext(NULL
);
1480 ALCdevice_StopPlayback(device
);
1481 SuspendContext(NULL
);
1485 freq
= device
->Frequency
;
1486 numMono
= device
->NumMonoSources
;
1487 numStereo
= device
->NumStereoSources
;
1488 numSends
= device
->NumAuxSends
;
1491 while(attrList
[attrIdx
])
1493 if(attrList
[attrIdx
] == ALC_FREQUENCY
&&
1494 !ConfigValueExists(NULL
, "frequency"))
1496 freq
= attrList
[attrIdx
+ 1];
1501 if(attrList
[attrIdx
] == ALC_STEREO_SOURCES
)
1503 numStereo
= attrList
[attrIdx
+ 1];
1504 if(numStereo
> device
->MaxNoOfSources
)
1505 numStereo
= device
->MaxNoOfSources
;
1507 numMono
= device
->MaxNoOfSources
- numStereo
;
1510 if(attrList
[attrIdx
] == ALC_MAX_AUXILIARY_SENDS
&&
1511 !ConfigValueExists(NULL
, "sends"))
1513 numSends
= attrList
[attrIdx
+ 1];
1514 if(numSends
> MAX_SENDS
)
1515 numSends
= MAX_SENDS
;
1521 device
->UpdateSize
= (ALuint64
)device
->UpdateSize
* freq
/
1524 device
->Frequency
= freq
;
1525 device
->NumMonoSources
= numMono
;
1526 device
->NumStereoSources
= numStereo
;
1527 device
->NumAuxSends
= numSends
;
1530 if(running
== AL_FALSE
&& ALCdevice_ResetPlayback(device
) == ALC_FALSE
)
1532 alcSetError(device
, ALC_INVALID_DEVICE
);
1533 aluHandleDisconnect(device
);
1534 ProcessContext(NULL
);
1537 aluInitPanning(device
);
1539 for(i
= 0;i
< device
->NumContexts
;i
++)
1541 ALCcontext
*context
= device
->Contexts
[i
];
1544 SuspendContext(context
);
1545 for(pos
= 0;pos
< context
->EffectSlotMap
.size
;pos
++)
1547 ALeffectslot
*slot
= context
->EffectSlotMap
.array
[pos
].value
;
1548 if(!slot
->EffectState
)
1551 if(ALEffect_DeviceUpdate(slot
->EffectState
, device
) == AL_FALSE
)
1553 alcSetError(device
, ALC_INVALID_DEVICE
);
1554 aluHandleDisconnect(device
);
1555 ProcessContext(context
);
1556 ProcessContext(NULL
);
1557 ALCdevice_StopPlayback(device
);
1560 ALEffect_Update(slot
->EffectState
, context
, &slot
->effect
);
1563 for(pos
= 0;pos
< context
->SourceMap
.size
;pos
++)
1565 ALsource
*source
= context
->SourceMap
.array
[pos
].value
;
1566 ALuint s
= device
->NumAuxSends
;
1567 while(s
< MAX_SENDS
)
1569 if(source
->Send
[s
].Slot
)
1570 source
->Send
[s
].Slot
->refcount
--;
1571 source
->Send
[s
].Slot
= NULL
;
1572 source
->Send
[s
].WetFilter
.type
= 0;
1573 source
->Send
[s
].WetFilter
.filter
= 0;
1576 source
->NeedsUpdate
= AL_TRUE
;
1578 ProcessContext(context
);
1581 if(device
->Bs2bLevel
> 0 && device
->Bs2bLevel
<= 6)
1585 device
->Bs2b
= calloc(1, sizeof(*device
->Bs2b
));
1586 bs2b_clear(device
->Bs2b
);
1588 bs2b_set_srate(device
->Bs2b
, device
->Frequency
);
1589 bs2b_set_level(device
->Bs2b
, device
->Bs2bLevel
);
1594 device
->Bs2b
= NULL
;
1597 if(aluChannelsFromFormat(device
->Format
) <= 2)
1599 device
->HeadDampen
= GetConfigValueFloat(NULL
, "head_dampen", DEFAULT_HEAD_DAMPEN
);
1600 device
->HeadDampen
= __min(device
->HeadDampen
, 1.0f
);
1601 device
->HeadDampen
= __max(device
->HeadDampen
, 0.0f
);
1604 device
->HeadDampen
= 0.0f
;
1606 temp
= realloc(device
->Contexts
, (device
->NumContexts
+1) * sizeof(*device
->Contexts
));
1609 alcSetError(device
, ALC_OUT_OF_MEMORY
);
1610 ProcessContext(NULL
);
1613 device
->Contexts
= temp
;
1615 ALContext
= calloc(1, sizeof(ALCcontext
));
1618 alcSetError(device
, ALC_OUT_OF_MEMORY
);
1619 ProcessContext(NULL
);
1623 device
->Contexts
[device
->NumContexts
++] = ALContext
;
1624 ALContext
->Device
= device
;
1626 InitContext(ALContext
);
1628 ALContext
->next
= g_pContextList
;
1629 g_pContextList
= ALContext
;
1632 ProcessContext(NULL
);
1643 ALC_API ALCvoid ALC_APIENTRY
alcDestroyContext(ALCcontext
*context
)
1649 if(!IsContext(context
))
1651 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1655 Device
= context
->Device
;
1657 if(Device
->NumContexts
== 1)
1658 ALCdevice_StopPlayback(Device
);
1660 SuspendContext(NULL
);
1662 if(context
== GlobalContext
)
1663 GlobalContext
= NULL
;
1665 for(i
= 0;i
< Device
->NumContexts
-1;i
++)
1667 if(Device
->Contexts
[i
] == context
)
1669 Device
->Contexts
[i
] = Device
->Contexts
[Device
->NumContexts
-1];
1673 Device
->NumContexts
--;
1676 SuspendContext(context
);
1678 if(context
->SourceMap
.size
> 0)
1681 AL_PRINT("alcDestroyContext(): deleting %d Source(s)\n", context
->SourceMap
.size
);
1683 ReleaseALSources(context
);
1685 ResetUIntMap(&context
->SourceMap
);
1687 if(context
->EffectSlotMap
.size
> 0)
1690 AL_PRINT("alcDestroyContext(): deleting %d AuxiliaryEffectSlot(s)\n", context
->EffectSlotMap
.size
);
1692 ReleaseALAuxiliaryEffectSlots(context
);
1694 ResetUIntMap(&context
->EffectSlotMap
);
1696 list
= &g_pContextList
;
1697 while(*list
!= context
)
1698 list
= &(*list
)->next
;
1700 *list
= (*list
)->next
;
1704 ProcessContext(context
);
1705 ProcessContext(NULL
);
1707 ExitContext(context
);
1709 // Free memory (MUST do this after ProcessContext)
1710 memset(context
, 0, sizeof(ALCcontext
));
1716 alcGetCurrentContext
1718 Returns the currently active Context
1720 ALC_API ALCcontext
* ALC_APIENTRY
alcGetCurrentContext(ALCvoid
)
1722 ALCcontext
*pContext
;
1724 if((pContext
=GetContextSuspended()) != NULL
)
1725 ProcessContext(pContext
);
1733 Returns the currently active thread-local Context
1735 ALC_API ALCcontext
* ALC_APIENTRY
alcGetThreadContext(void)
1737 ALCcontext
*pContext
= NULL
;
1739 SuspendContext(NULL
);
1741 pContext
= tls_get(LocalContext
);
1742 if(pContext
&& !IsContext(pContext
))
1744 tls_set(LocalContext
, NULL
);
1748 ProcessContext(NULL
);
1755 alcGetContextsDevice
1757 Returns the Device that a particular Context is attached to
1759 ALC_API ALCdevice
* ALC_APIENTRY
alcGetContextsDevice(ALCcontext
*pContext
)
1761 ALCdevice
*pDevice
= NULL
;
1763 SuspendContext(NULL
);
1764 if(IsContext(pContext
))
1765 pDevice
= pContext
->Device
;
1767 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1768 ProcessContext(NULL
);
1775 alcMakeContextCurrent
1777 Makes the given Context the active Context
1779 ALC_API ALCboolean ALC_APIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
1781 ALboolean bReturn
= AL_TRUE
;
1783 SuspendContext(NULL
);
1785 // context must be a valid Context or NULL
1786 if(context
== NULL
|| IsContext(context
))
1788 GlobalContext
= context
;
1789 tls_set(LocalContext
, NULL
);
1793 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1797 ProcessContext(NULL
);
1805 Makes the given Context the active Context for the current thread
1807 ALC_API ALCboolean ALC_APIENTRY
alcSetThreadContext(ALCcontext
*context
)
1809 ALboolean bReturn
= AL_TRUE
;
1811 SuspendContext(NULL
);
1813 // context must be a valid Context or NULL
1814 if(context
== NULL
|| IsContext(context
))
1815 tls_set(LocalContext
, context
);
1818 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1822 ProcessContext(NULL
);
1828 // Sets the default channel order used by most non-WaveFormatEx-based APIs
1829 void SetDefaultChannelOrder(ALCdevice
*device
)
1831 switch(aluChannelsFromFormat(device
->Format
))
1833 case 1: device
->DevChannels
[FRONT_CENTER
] = 0; break;
1835 case 2: device
->DevChannels
[FRONT_LEFT
] = 0;
1836 device
->DevChannels
[FRONT_RIGHT
] = 1; break;
1838 case 4: device
->DevChannels
[FRONT_LEFT
] = 0;
1839 device
->DevChannels
[FRONT_RIGHT
] = 1;
1840 device
->DevChannels
[BACK_LEFT
] = 2;
1841 device
->DevChannels
[BACK_RIGHT
] = 3; break;
1843 case 6: device
->DevChannels
[FRONT_LEFT
] = 0;
1844 device
->DevChannels
[FRONT_RIGHT
] = 1;
1845 device
->DevChannels
[BACK_LEFT
] = 2;
1846 device
->DevChannels
[BACK_RIGHT
] = 3;
1847 device
->DevChannels
[FRONT_CENTER
] = 4;
1848 device
->DevChannels
[LFE
] = 5; break;
1850 case 7: device
->DevChannels
[FRONT_LEFT
] = 0;
1851 device
->DevChannels
[FRONT_RIGHT
] = 1;
1852 device
->DevChannels
[FRONT_CENTER
] = 2;
1853 device
->DevChannels
[LFE
] = 3;
1854 device
->DevChannels
[BACK_CENTER
] = 4;
1855 device
->DevChannels
[SIDE_LEFT
] = 5;
1856 device
->DevChannels
[SIDE_RIGHT
] = 6; break;
1858 case 8: device
->DevChannels
[FRONT_LEFT
] = 0;
1859 device
->DevChannels
[FRONT_RIGHT
] = 1;
1860 device
->DevChannels
[BACK_LEFT
] = 2;
1861 device
->DevChannels
[BACK_RIGHT
] = 3;
1862 device
->DevChannels
[FRONT_CENTER
] = 4;
1863 device
->DevChannels
[LFE
] = 5;
1864 device
->DevChannels
[SIDE_LEFT
] = 6;
1865 device
->DevChannels
[SIDE_RIGHT
] = 7; break;
1868 // Sets the default order used by WaveFormatEx
1869 void SetDefaultWFXChannelOrder(ALCdevice
*device
)
1871 switch(aluChannelsFromFormat(device
->Format
))
1873 case 1: device
->DevChannels
[FRONT_CENTER
] = 0; break;
1875 case 2: device
->DevChannels
[FRONT_LEFT
] = 0;
1876 device
->DevChannels
[FRONT_RIGHT
] = 1; break;
1878 case 4: device
->DevChannels
[FRONT_LEFT
] = 0;
1879 device
->DevChannels
[FRONT_RIGHT
] = 1;
1880 device
->DevChannels
[BACK_LEFT
] = 2;
1881 device
->DevChannels
[BACK_RIGHT
] = 3; break;
1883 case 6: device
->DevChannels
[FRONT_LEFT
] = 0;
1884 device
->DevChannels
[FRONT_RIGHT
] = 1;
1885 device
->DevChannels
[FRONT_CENTER
] = 2;
1886 device
->DevChannels
[LFE
] = 3;
1887 device
->DevChannels
[BACK_LEFT
] = 4;
1888 device
->DevChannels
[BACK_RIGHT
] = 5; break;
1890 case 7: device
->DevChannels
[FRONT_LEFT
] = 0;
1891 device
->DevChannels
[FRONT_RIGHT
] = 1;
1892 device
->DevChannels
[FRONT_CENTER
] = 2;
1893 device
->DevChannels
[LFE
] = 3;
1894 device
->DevChannels
[BACK_CENTER
] = 4;
1895 device
->DevChannels
[SIDE_LEFT
] = 5;
1896 device
->DevChannels
[SIDE_RIGHT
] = 6; break;
1898 case 8: device
->DevChannels
[FRONT_LEFT
] = 0;
1899 device
->DevChannels
[FRONT_RIGHT
] = 1;
1900 device
->DevChannels
[FRONT_CENTER
] = 2;
1901 device
->DevChannels
[LFE
] = 3;
1902 device
->DevChannels
[BACK_LEFT
] = 4;
1903 device
->DevChannels
[BACK_RIGHT
] = 5;
1904 device
->DevChannels
[SIDE_LEFT
] = 6;
1905 device
->DevChannels
[SIDE_RIGHT
] = 7; break;
1909 static ALenum
GetFormatFromString(const char *str
)
1911 if(strcasecmp(str
, "AL_FORMAT_MONO32") == 0) return AL_FORMAT_MONO_FLOAT32
;
1912 if(strcasecmp(str
, "AL_FORMAT_STEREO32") == 0) return AL_FORMAT_STEREO_FLOAT32
;
1913 if(strcasecmp(str
, "AL_FORMAT_QUAD32") == 0) return AL_FORMAT_QUAD32
;
1914 if(strcasecmp(str
, "AL_FORMAT_51CHN32") == 0) return AL_FORMAT_51CHN32
;
1915 if(strcasecmp(str
, "AL_FORMAT_61CHN32") == 0) return AL_FORMAT_61CHN32
;
1916 if(strcasecmp(str
, "AL_FORMAT_71CHN32") == 0) return AL_FORMAT_71CHN32
;
1918 if(strcasecmp(str
, "AL_FORMAT_MONO16") == 0) return AL_FORMAT_MONO16
;
1919 if(strcasecmp(str
, "AL_FORMAT_STEREO16") == 0) return AL_FORMAT_STEREO16
;
1920 if(strcasecmp(str
, "AL_FORMAT_QUAD16") == 0) return AL_FORMAT_QUAD16
;
1921 if(strcasecmp(str
, "AL_FORMAT_51CHN16") == 0) return AL_FORMAT_51CHN16
;
1922 if(strcasecmp(str
, "AL_FORMAT_61CHN16") == 0) return AL_FORMAT_61CHN16
;
1923 if(strcasecmp(str
, "AL_FORMAT_71CHN16") == 0) return AL_FORMAT_71CHN16
;
1925 if(strcasecmp(str
, "AL_FORMAT_MONO8") == 0) return AL_FORMAT_MONO8
;
1926 if(strcasecmp(str
, "AL_FORMAT_STEREO8") == 0) return AL_FORMAT_STEREO8
;
1927 if(strcasecmp(str
, "AL_FORMAT_QUAD8") == 0) return AL_FORMAT_QUAD8
;
1928 if(strcasecmp(str
, "AL_FORMAT_51CHN8") == 0) return AL_FORMAT_51CHN8
;
1929 if(strcasecmp(str
, "AL_FORMAT_61CHN8") == 0) return AL_FORMAT_61CHN8
;
1930 if(strcasecmp(str
, "AL_FORMAT_71CHN8") == 0) return AL_FORMAT_71CHN8
;
1932 AL_PRINT("Unknown format: \"%s\"\n", str
);
1933 return AL_FORMAT_STEREO16
;
1939 Open the Device specified.
1941 ALC_API ALCdevice
* ALC_APIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
1943 ALboolean bDeviceFound
= AL_FALSE
;
1948 if(deviceName
&& !deviceName
[0])
1951 device
= calloc(1, sizeof(ALCdevice
));
1954 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
1959 device
->Connected
= ALC_TRUE
;
1960 device
->IsCaptureDevice
= AL_FALSE
;
1961 device
->LastError
= ALC_NO_ERROR
;
1963 device
->Bs2b
= NULL
;
1964 device
->szDeviceName
= NULL
;
1966 device
->Contexts
= NULL
;
1967 device
->NumContexts
= 0;
1969 InitUIntMap(&device
->BufferMap
);
1970 InitUIntMap(&device
->EffectMap
);
1971 InitUIntMap(&device
->FilterMap
);
1972 InitUIntMap(&device
->DatabufferMap
);
1975 device
->Frequency
= GetConfigValueInt(NULL
, "frequency", SWMIXER_OUTPUT_RATE
);
1976 if(device
->Frequency
< 8000)
1977 device
->Frequency
= 8000;
1979 fmt
= GetConfigValue(NULL
, "format", "AL_FORMAT_STEREO16");
1980 device
->Format
= GetFormatFromString(fmt
);
1982 device
->NumUpdates
= GetConfigValueInt(NULL
, "periods", 4);
1983 if(device
->NumUpdates
< 2)
1984 device
->NumUpdates
= 4;
1986 i
= GetConfigValueInt(NULL
, "refresh", 4096);
1987 if(i
<= 0) i
= 4096;
1989 device
->UpdateSize
= GetConfigValueInt(NULL
, "period_size", i
/device
->NumUpdates
);
1990 if(device
->UpdateSize
<= 0)
1991 device
->UpdateSize
= i
/device
->NumUpdates
;
1993 device
->MaxNoOfSources
= GetConfigValueInt(NULL
, "sources", 256);
1994 if((ALint
)device
->MaxNoOfSources
<= 0)
1995 device
->MaxNoOfSources
= 256;
1997 device
->AuxiliaryEffectSlotMax
= GetConfigValueInt(NULL
, "slots", 4);
1998 if((ALint
)device
->AuxiliaryEffectSlotMax
<= 0)
1999 device
->AuxiliaryEffectSlotMax
= 4;
2001 device
->NumStereoSources
= 1;
2002 device
->NumMonoSources
= device
->MaxNoOfSources
- device
->NumStereoSources
;
2004 device
->NumAuxSends
= GetConfigValueInt(NULL
, "sends", MAX_SENDS
);
2005 if(device
->NumAuxSends
> MAX_SENDS
)
2006 device
->NumAuxSends
= MAX_SENDS
;
2008 device
->Bs2bLevel
= GetConfigValueInt(NULL
, "cf_level", 0);
2010 device
->HeadDampen
= 0.0f
;
2012 // Find a playback device to open
2013 SuspendContext(NULL
);
2014 for(i
= 0;BackendList
[i
].Init
;i
++)
2016 device
->Funcs
= &BackendList
[i
].Funcs
;
2017 if(ALCdevice_OpenPlayback(device
, deviceName
))
2019 device
->next
= g_pDeviceList
;
2020 g_pDeviceList
= device
;
2023 bDeviceFound
= AL_TRUE
;
2027 ProcessContext(NULL
);
2031 // No suitable output device found
2032 alcSetError(NULL
, ALC_INVALID_VALUE
);
2044 Close the specified Device
2046 ALC_API ALCboolean ALC_APIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
2050 if(!IsDevice(pDevice
) || pDevice
->IsCaptureDevice
)
2052 alcSetError(pDevice
, ALC_INVALID_DEVICE
);
2056 SuspendContext(NULL
);
2058 list
= &g_pDeviceList
;
2059 while(*list
!= pDevice
)
2060 list
= &(*list
)->next
;
2062 *list
= (*list
)->next
;
2065 ProcessContext(NULL
);
2067 if(pDevice
->NumContexts
> 0)
2070 AL_PRINT("alcCloseDevice(): destroying %u Context(s)\n", pDevice
->NumContexts
);
2072 while(pDevice
->NumContexts
> 0)
2073 alcDestroyContext(pDevice
->Contexts
[0]);
2075 ALCdevice_ClosePlayback(pDevice
);
2077 if(pDevice
->BufferMap
.size
> 0)
2080 AL_PRINT("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice
->BufferMap
.size
);
2082 ReleaseALBuffers(pDevice
);
2084 ResetUIntMap(&pDevice
->BufferMap
);
2086 if(pDevice
->EffectMap
.size
> 0)
2089 AL_PRINT("alcCloseDevice(): deleting %d Effect(s)\n", pDevice
->EffectMap
.size
);
2091 ReleaseALEffects(pDevice
);
2093 ResetUIntMap(&pDevice
->EffectMap
);
2095 if(pDevice
->FilterMap
.size
> 0)
2098 AL_PRINT("alcCloseDevice(): deleting %d Filter(s)\n", pDevice
->FilterMap
.size
);
2100 ReleaseALFilters(pDevice
);
2102 ResetUIntMap(&pDevice
->FilterMap
);
2104 if(pDevice
->DatabufferMap
.size
> 0)
2107 AL_PRINT("alcCloseDevice(): deleting %d Databuffer(s)\n", pDevice
->DatabufferMap
.size
);
2109 ReleaseALDatabuffers(pDevice
);
2111 ResetUIntMap(&pDevice
->DatabufferMap
);
2113 free(pDevice
->Bs2b
);
2114 pDevice
->Bs2b
= NULL
;
2116 free(pDevice
->szDeviceName
);
2117 pDevice
->szDeviceName
= NULL
;
2119 free(pDevice
->Contexts
);
2120 pDevice
->Contexts
= NULL
;
2122 //Release device structure
2123 memset(pDevice
, 0, sizeof(ALCdevice
));
2130 ALCvoid
ReleaseALC(ALCvoid
)
2132 free(alcDeviceList
); alcDeviceList
= NULL
;
2133 alcDeviceListSize
= 0;
2134 free(alcAllDeviceList
); alcAllDeviceList
= NULL
;
2135 alcAllDeviceListSize
= 0;
2136 free(alcCaptureDeviceList
); alcCaptureDeviceList
= NULL
;
2137 alcCaptureDeviceListSize
= 0;
2139 free(alcDefaultDeviceSpecifier
);
2140 alcDefaultDeviceSpecifier
= NULL
;
2141 free(alcDefaultAllDeviceSpecifier
);
2142 alcDefaultAllDeviceSpecifier
= NULL
;
2143 free(alcCaptureDefaultDeviceSpecifier
);
2144 alcCaptureDefaultDeviceSpecifier
= NULL
;
2147 if(g_ulDeviceCount
> 0)
2148 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount
, (g_ulDeviceCount
>1)?"s":"");
2151 while(g_pDeviceList
)
2153 if(g_pDeviceList
->IsCaptureDevice
)
2154 alcCaptureCloseDevice(g_pDeviceList
);
2156 alcCloseDevice(g_pDeviceList
);
2160 ///////////////////////////////////////////////////////