Finalize AL_SOFT_gain_clamp_ex
[openal-soft.git] / Alc / ALc.c
blob7e220205cb7797c00d22f60e10c348ba4459959c
1 /**
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.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <math.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <memory.h>
27 #include <ctype.h>
28 #include <signal.h>
30 #include "alMain.h"
31 #include "alSource.h"
32 #include "alListener.h"
33 #include "alThunk.h"
34 #include "alSource.h"
35 #include "alBuffer.h"
36 #include "alAuxEffectSlot.h"
37 #include "alError.h"
38 #include "bformatdec.h"
39 #include "alu.h"
41 #include "compat.h"
42 #include "threads.h"
43 #include "alstring.h"
44 #include "almalloc.h"
46 #include "backends/base.h"
49 /************************************************
50 * Backends
51 ************************************************/
52 struct BackendInfo {
53 const char *name;
54 ALCbackendFactory* (*getFactory)(void);
55 ALCboolean (*Init)(BackendFuncs*);
56 void (*Deinit)(void);
57 void (*Probe)(enum DevProbe);
58 BackendFuncs Funcs;
61 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
62 static struct BackendInfo BackendList[] = {
63 #ifdef HAVE_JACK
64 { "jack", ALCjackBackendFactory_getFactory, NULL, NULL, NULL, EmptyFuncs },
65 #endif
66 #ifdef HAVE_PULSEAUDIO
67 { "pulse", ALCpulseBackendFactory_getFactory, NULL, NULL, NULL, EmptyFuncs },
68 #endif
69 #ifdef HAVE_ALSA
70 { "alsa", ALCalsaBackendFactory_getFactory, NULL, NULL, NULL, EmptyFuncs },
71 #endif
72 #ifdef HAVE_COREAUDIO
73 { "core", NULL, alc_ca_init, alc_ca_deinit, alc_ca_probe, EmptyFuncs },
74 #endif
75 #ifdef HAVE_OSS
76 { "oss", ALCossBackendFactory_getFactory, NULL, NULL, NULL, EmptyFuncs },
77 #endif
78 #ifdef HAVE_SOLARIS
79 { "solaris", ALCsolarisBackendFactory_getFactory, NULL, NULL, NULL, EmptyFuncs },
80 #endif
81 #ifdef HAVE_SNDIO
82 { "sndio", NULL, alc_sndio_init, alc_sndio_deinit, alc_sndio_probe, EmptyFuncs },
83 #endif
84 #ifdef HAVE_QSA
85 { "qsa", NULL, alc_qsa_init, alc_qsa_deinit, alc_qsa_probe, EmptyFuncs },
86 #endif
87 #ifdef HAVE_MMDEVAPI
88 { "mmdevapi", ALCmmdevBackendFactory_getFactory, NULL, NULL, NULL, EmptyFuncs },
89 #endif
90 #ifdef HAVE_DSOUND
91 { "dsound", ALCdsoundBackendFactory_getFactory, NULL, NULL, NULL, EmptyFuncs },
92 #endif
93 #ifdef HAVE_WINMM
94 { "winmm", ALCwinmmBackendFactory_getFactory, NULL, NULL, NULL, EmptyFuncs },
95 #endif
96 #ifdef HAVE_PORTAUDIO
97 { "port", ALCportBackendFactory_getFactory, NULL, NULL, NULL, EmptyFuncs },
98 #endif
99 #ifdef HAVE_OPENSL
100 { "opensl", NULL, alc_opensl_init, alc_opensl_deinit, alc_opensl_probe, EmptyFuncs },
101 #endif
103 { "null", ALCnullBackendFactory_getFactory, NULL, NULL, NULL, EmptyFuncs },
104 #ifdef HAVE_WAVE
105 { "wave", ALCwaveBackendFactory_getFactory, NULL, NULL, NULL, EmptyFuncs },
106 #endif
108 { NULL, NULL, NULL, NULL, NULL, EmptyFuncs }
110 #undef EmptyFuncs
112 static struct BackendInfo PlaybackBackend;
113 static struct BackendInfo CaptureBackend;
116 /************************************************
117 * Functions, enums, and errors
118 ************************************************/
119 typedef struct ALCfunction {
120 const ALCchar *funcName;
121 ALCvoid *address;
122 } ALCfunction;
124 typedef struct ALCenums {
125 const ALCchar *enumName;
126 ALCenum value;
127 } ALCenums;
129 #define DECL(x) { #x, (ALCvoid*)(x) }
130 static const ALCfunction alcFunctions[] = {
131 DECL(alcCreateContext),
132 DECL(alcMakeContextCurrent),
133 DECL(alcProcessContext),
134 DECL(alcSuspendContext),
135 DECL(alcDestroyContext),
136 DECL(alcGetCurrentContext),
137 DECL(alcGetContextsDevice),
138 DECL(alcOpenDevice),
139 DECL(alcCloseDevice),
140 DECL(alcGetError),
141 DECL(alcIsExtensionPresent),
142 DECL(alcGetProcAddress),
143 DECL(alcGetEnumValue),
144 DECL(alcGetString),
145 DECL(alcGetIntegerv),
146 DECL(alcCaptureOpenDevice),
147 DECL(alcCaptureCloseDevice),
148 DECL(alcCaptureStart),
149 DECL(alcCaptureStop),
150 DECL(alcCaptureSamples),
152 DECL(alcSetThreadContext),
153 DECL(alcGetThreadContext),
155 DECL(alcLoopbackOpenDeviceSOFT),
156 DECL(alcIsRenderFormatSupportedSOFT),
157 DECL(alcRenderSamplesSOFT),
159 DECL(alcDevicePauseSOFT),
160 DECL(alcDeviceResumeSOFT),
162 DECL(alcGetStringiSOFT),
163 DECL(alcResetDeviceSOFT),
165 DECL(alcGetInteger64vSOFT),
167 DECL(alEnable),
168 DECL(alDisable),
169 DECL(alIsEnabled),
170 DECL(alGetString),
171 DECL(alGetBooleanv),
172 DECL(alGetIntegerv),
173 DECL(alGetFloatv),
174 DECL(alGetDoublev),
175 DECL(alGetBoolean),
176 DECL(alGetInteger),
177 DECL(alGetFloat),
178 DECL(alGetDouble),
179 DECL(alGetError),
180 DECL(alIsExtensionPresent),
181 DECL(alGetProcAddress),
182 DECL(alGetEnumValue),
183 DECL(alListenerf),
184 DECL(alListener3f),
185 DECL(alListenerfv),
186 DECL(alListeneri),
187 DECL(alListener3i),
188 DECL(alListeneriv),
189 DECL(alGetListenerf),
190 DECL(alGetListener3f),
191 DECL(alGetListenerfv),
192 DECL(alGetListeneri),
193 DECL(alGetListener3i),
194 DECL(alGetListeneriv),
195 DECL(alGenSources),
196 DECL(alDeleteSources),
197 DECL(alIsSource),
198 DECL(alSourcef),
199 DECL(alSource3f),
200 DECL(alSourcefv),
201 DECL(alSourcei),
202 DECL(alSource3i),
203 DECL(alSourceiv),
204 DECL(alGetSourcef),
205 DECL(alGetSource3f),
206 DECL(alGetSourcefv),
207 DECL(alGetSourcei),
208 DECL(alGetSource3i),
209 DECL(alGetSourceiv),
210 DECL(alSourcePlayv),
211 DECL(alSourceStopv),
212 DECL(alSourceRewindv),
213 DECL(alSourcePausev),
214 DECL(alSourcePlay),
215 DECL(alSourceStop),
216 DECL(alSourceRewind),
217 DECL(alSourcePause),
218 DECL(alSourceQueueBuffers),
219 DECL(alSourceUnqueueBuffers),
220 DECL(alGenBuffers),
221 DECL(alDeleteBuffers),
222 DECL(alIsBuffer),
223 DECL(alBufferData),
224 DECL(alBufferf),
225 DECL(alBuffer3f),
226 DECL(alBufferfv),
227 DECL(alBufferi),
228 DECL(alBuffer3i),
229 DECL(alBufferiv),
230 DECL(alGetBufferf),
231 DECL(alGetBuffer3f),
232 DECL(alGetBufferfv),
233 DECL(alGetBufferi),
234 DECL(alGetBuffer3i),
235 DECL(alGetBufferiv),
236 DECL(alDopplerFactor),
237 DECL(alDopplerVelocity),
238 DECL(alSpeedOfSound),
239 DECL(alDistanceModel),
241 DECL(alGenFilters),
242 DECL(alDeleteFilters),
243 DECL(alIsFilter),
244 DECL(alFilteri),
245 DECL(alFilteriv),
246 DECL(alFilterf),
247 DECL(alFilterfv),
248 DECL(alGetFilteri),
249 DECL(alGetFilteriv),
250 DECL(alGetFilterf),
251 DECL(alGetFilterfv),
252 DECL(alGenEffects),
253 DECL(alDeleteEffects),
254 DECL(alIsEffect),
255 DECL(alEffecti),
256 DECL(alEffectiv),
257 DECL(alEffectf),
258 DECL(alEffectfv),
259 DECL(alGetEffecti),
260 DECL(alGetEffectiv),
261 DECL(alGetEffectf),
262 DECL(alGetEffectfv),
263 DECL(alGenAuxiliaryEffectSlots),
264 DECL(alDeleteAuxiliaryEffectSlots),
265 DECL(alIsAuxiliaryEffectSlot),
266 DECL(alAuxiliaryEffectSloti),
267 DECL(alAuxiliaryEffectSlotiv),
268 DECL(alAuxiliaryEffectSlotf),
269 DECL(alAuxiliaryEffectSlotfv),
270 DECL(alGetAuxiliaryEffectSloti),
271 DECL(alGetAuxiliaryEffectSlotiv),
272 DECL(alGetAuxiliaryEffectSlotf),
273 DECL(alGetAuxiliaryEffectSlotfv),
275 DECL(alDeferUpdatesSOFT),
276 DECL(alProcessUpdatesSOFT),
278 DECL(alSourcedSOFT),
279 DECL(alSource3dSOFT),
280 DECL(alSourcedvSOFT),
281 DECL(alGetSourcedSOFT),
282 DECL(alGetSource3dSOFT),
283 DECL(alGetSourcedvSOFT),
284 DECL(alSourcei64SOFT),
285 DECL(alSource3i64SOFT),
286 DECL(alSourcei64vSOFT),
287 DECL(alGetSourcei64SOFT),
288 DECL(alGetSource3i64SOFT),
289 DECL(alGetSourcei64vSOFT),
291 DECL(alBufferSamplesSOFT),
292 DECL(alGetBufferSamplesSOFT),
293 DECL(alIsBufferFormatSupportedSOFT),
295 { NULL, NULL }
297 #undef DECL
299 #define DECL(x) { #x, (x) }
300 static const ALCenums enumeration[] = {
301 DECL(ALC_INVALID),
302 DECL(ALC_FALSE),
303 DECL(ALC_TRUE),
305 DECL(ALC_MAJOR_VERSION),
306 DECL(ALC_MINOR_VERSION),
307 DECL(ALC_ATTRIBUTES_SIZE),
308 DECL(ALC_ALL_ATTRIBUTES),
309 DECL(ALC_DEFAULT_DEVICE_SPECIFIER),
310 DECL(ALC_DEVICE_SPECIFIER),
311 DECL(ALC_ALL_DEVICES_SPECIFIER),
312 DECL(ALC_DEFAULT_ALL_DEVICES_SPECIFIER),
313 DECL(ALC_EXTENSIONS),
314 DECL(ALC_FREQUENCY),
315 DECL(ALC_REFRESH),
316 DECL(ALC_SYNC),
317 DECL(ALC_MONO_SOURCES),
318 DECL(ALC_STEREO_SOURCES),
319 DECL(ALC_CAPTURE_DEVICE_SPECIFIER),
320 DECL(ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER),
321 DECL(ALC_CAPTURE_SAMPLES),
322 DECL(ALC_CONNECTED),
324 DECL(ALC_EFX_MAJOR_VERSION),
325 DECL(ALC_EFX_MINOR_VERSION),
326 DECL(ALC_MAX_AUXILIARY_SENDS),
328 DECL(ALC_FORMAT_CHANNELS_SOFT),
329 DECL(ALC_FORMAT_TYPE_SOFT),
331 DECL(ALC_MONO_SOFT),
332 DECL(ALC_STEREO_SOFT),
333 DECL(ALC_QUAD_SOFT),
334 DECL(ALC_5POINT1_SOFT),
335 DECL(ALC_6POINT1_SOFT),
336 DECL(ALC_7POINT1_SOFT),
338 DECL(ALC_BYTE_SOFT),
339 DECL(ALC_UNSIGNED_BYTE_SOFT),
340 DECL(ALC_SHORT_SOFT),
341 DECL(ALC_UNSIGNED_SHORT_SOFT),
342 DECL(ALC_INT_SOFT),
343 DECL(ALC_UNSIGNED_INT_SOFT),
344 DECL(ALC_FLOAT_SOFT),
346 DECL(ALC_HRTF_SOFT),
347 DECL(ALC_DONT_CARE_SOFT),
348 DECL(ALC_HRTF_STATUS_SOFT),
349 DECL(ALC_HRTF_DISABLED_SOFT),
350 DECL(ALC_HRTF_ENABLED_SOFT),
351 DECL(ALC_HRTF_DENIED_SOFT),
352 DECL(ALC_HRTF_REQUIRED_SOFT),
353 DECL(ALC_HRTF_HEADPHONES_DETECTED_SOFT),
354 DECL(ALC_HRTF_UNSUPPORTED_FORMAT_SOFT),
355 DECL(ALC_NUM_HRTF_SPECIFIERS_SOFT),
356 DECL(ALC_HRTF_SPECIFIER_SOFT),
357 DECL(ALC_HRTF_ID_SOFT),
359 DECL(ALC_NO_ERROR),
360 DECL(ALC_INVALID_DEVICE),
361 DECL(ALC_INVALID_CONTEXT),
362 DECL(ALC_INVALID_ENUM),
363 DECL(ALC_INVALID_VALUE),
364 DECL(ALC_OUT_OF_MEMORY),
367 DECL(AL_INVALID),
368 DECL(AL_NONE),
369 DECL(AL_FALSE),
370 DECL(AL_TRUE),
372 DECL(AL_SOURCE_RELATIVE),
373 DECL(AL_CONE_INNER_ANGLE),
374 DECL(AL_CONE_OUTER_ANGLE),
375 DECL(AL_PITCH),
376 DECL(AL_POSITION),
377 DECL(AL_DIRECTION),
378 DECL(AL_VELOCITY),
379 DECL(AL_LOOPING),
380 DECL(AL_BUFFER),
381 DECL(AL_GAIN),
382 DECL(AL_MIN_GAIN),
383 DECL(AL_MAX_GAIN),
384 DECL(AL_ORIENTATION),
385 DECL(AL_REFERENCE_DISTANCE),
386 DECL(AL_ROLLOFF_FACTOR),
387 DECL(AL_CONE_OUTER_GAIN),
388 DECL(AL_MAX_DISTANCE),
389 DECL(AL_SEC_OFFSET),
390 DECL(AL_SAMPLE_OFFSET),
391 DECL(AL_BYTE_OFFSET),
392 DECL(AL_SOURCE_TYPE),
393 DECL(AL_STATIC),
394 DECL(AL_STREAMING),
395 DECL(AL_UNDETERMINED),
396 DECL(AL_METERS_PER_UNIT),
397 DECL(AL_LOOP_POINTS_SOFT),
398 DECL(AL_DIRECT_CHANNELS_SOFT),
400 DECL(AL_DIRECT_FILTER),
401 DECL(AL_AUXILIARY_SEND_FILTER),
402 DECL(AL_AIR_ABSORPTION_FACTOR),
403 DECL(AL_ROOM_ROLLOFF_FACTOR),
404 DECL(AL_CONE_OUTER_GAINHF),
405 DECL(AL_DIRECT_FILTER_GAINHF_AUTO),
406 DECL(AL_AUXILIARY_SEND_FILTER_GAIN_AUTO),
407 DECL(AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO),
409 DECL(AL_SOURCE_STATE),
410 DECL(AL_INITIAL),
411 DECL(AL_PLAYING),
412 DECL(AL_PAUSED),
413 DECL(AL_STOPPED),
415 DECL(AL_BUFFERS_QUEUED),
416 DECL(AL_BUFFERS_PROCESSED),
418 DECL(AL_FORMAT_MONO8),
419 DECL(AL_FORMAT_MONO16),
420 DECL(AL_FORMAT_MONO_FLOAT32),
421 DECL(AL_FORMAT_MONO_DOUBLE_EXT),
422 DECL(AL_FORMAT_STEREO8),
423 DECL(AL_FORMAT_STEREO16),
424 DECL(AL_FORMAT_STEREO_FLOAT32),
425 DECL(AL_FORMAT_STEREO_DOUBLE_EXT),
426 DECL(AL_FORMAT_MONO_IMA4),
427 DECL(AL_FORMAT_STEREO_IMA4),
428 DECL(AL_FORMAT_MONO_MSADPCM_SOFT),
429 DECL(AL_FORMAT_STEREO_MSADPCM_SOFT),
430 DECL(AL_FORMAT_QUAD8_LOKI),
431 DECL(AL_FORMAT_QUAD16_LOKI),
432 DECL(AL_FORMAT_QUAD8),
433 DECL(AL_FORMAT_QUAD16),
434 DECL(AL_FORMAT_QUAD32),
435 DECL(AL_FORMAT_51CHN8),
436 DECL(AL_FORMAT_51CHN16),
437 DECL(AL_FORMAT_51CHN32),
438 DECL(AL_FORMAT_61CHN8),
439 DECL(AL_FORMAT_61CHN16),
440 DECL(AL_FORMAT_61CHN32),
441 DECL(AL_FORMAT_71CHN8),
442 DECL(AL_FORMAT_71CHN16),
443 DECL(AL_FORMAT_71CHN32),
444 DECL(AL_FORMAT_REAR8),
445 DECL(AL_FORMAT_REAR16),
446 DECL(AL_FORMAT_REAR32),
447 DECL(AL_FORMAT_MONO_MULAW),
448 DECL(AL_FORMAT_MONO_MULAW_EXT),
449 DECL(AL_FORMAT_STEREO_MULAW),
450 DECL(AL_FORMAT_STEREO_MULAW_EXT),
451 DECL(AL_FORMAT_QUAD_MULAW),
452 DECL(AL_FORMAT_51CHN_MULAW),
453 DECL(AL_FORMAT_61CHN_MULAW),
454 DECL(AL_FORMAT_71CHN_MULAW),
455 DECL(AL_FORMAT_REAR_MULAW),
456 DECL(AL_FORMAT_MONO_ALAW_EXT),
457 DECL(AL_FORMAT_STEREO_ALAW_EXT),
459 DECL(AL_FORMAT_BFORMAT2D_8),
460 DECL(AL_FORMAT_BFORMAT2D_16),
461 DECL(AL_FORMAT_BFORMAT2D_FLOAT32),
462 DECL(AL_FORMAT_BFORMAT2D_MULAW),
463 DECL(AL_FORMAT_BFORMAT3D_8),
464 DECL(AL_FORMAT_BFORMAT3D_16),
465 DECL(AL_FORMAT_BFORMAT3D_FLOAT32),
466 DECL(AL_FORMAT_BFORMAT3D_MULAW),
468 DECL(AL_MONO8_SOFT),
469 DECL(AL_MONO16_SOFT),
470 DECL(AL_MONO32F_SOFT),
471 DECL(AL_STEREO8_SOFT),
472 DECL(AL_STEREO16_SOFT),
473 DECL(AL_STEREO32F_SOFT),
474 DECL(AL_QUAD8_SOFT),
475 DECL(AL_QUAD16_SOFT),
476 DECL(AL_QUAD32F_SOFT),
477 DECL(AL_REAR8_SOFT),
478 DECL(AL_REAR16_SOFT),
479 DECL(AL_REAR32F_SOFT),
480 DECL(AL_5POINT1_8_SOFT),
481 DECL(AL_5POINT1_16_SOFT),
482 DECL(AL_5POINT1_32F_SOFT),
483 DECL(AL_6POINT1_8_SOFT),
484 DECL(AL_6POINT1_16_SOFT),
485 DECL(AL_6POINT1_32F_SOFT),
486 DECL(AL_7POINT1_8_SOFT),
487 DECL(AL_7POINT1_16_SOFT),
488 DECL(AL_7POINT1_32F_SOFT),
489 DECL(AL_BFORMAT2D_8_SOFT),
490 DECL(AL_BFORMAT2D_16_SOFT),
491 DECL(AL_BFORMAT2D_32F_SOFT),
492 DECL(AL_BFORMAT3D_8_SOFT),
493 DECL(AL_BFORMAT3D_16_SOFT),
494 DECL(AL_BFORMAT3D_32F_SOFT),
496 DECL(AL_MONO_SOFT),
497 DECL(AL_STEREO_SOFT),
498 DECL(AL_QUAD_SOFT),
499 DECL(AL_REAR_SOFT),
500 DECL(AL_5POINT1_SOFT),
501 DECL(AL_6POINT1_SOFT),
502 DECL(AL_7POINT1_SOFT),
503 DECL(AL_BFORMAT2D_SOFT),
504 DECL(AL_BFORMAT3D_SOFT),
506 DECL(AL_BYTE_SOFT),
507 DECL(AL_UNSIGNED_BYTE_SOFT),
508 DECL(AL_SHORT_SOFT),
509 DECL(AL_UNSIGNED_SHORT_SOFT),
510 DECL(AL_INT_SOFT),
511 DECL(AL_UNSIGNED_INT_SOFT),
512 DECL(AL_FLOAT_SOFT),
513 DECL(AL_DOUBLE_SOFT),
514 DECL(AL_BYTE3_SOFT),
515 DECL(AL_UNSIGNED_BYTE3_SOFT),
516 DECL(AL_MULAW_SOFT),
518 DECL(AL_FREQUENCY),
519 DECL(AL_BITS),
520 DECL(AL_CHANNELS),
521 DECL(AL_SIZE),
522 DECL(AL_INTERNAL_FORMAT_SOFT),
523 DECL(AL_BYTE_LENGTH_SOFT),
524 DECL(AL_SAMPLE_LENGTH_SOFT),
525 DECL(AL_SEC_LENGTH_SOFT),
526 DECL(AL_UNPACK_BLOCK_ALIGNMENT_SOFT),
527 DECL(AL_PACK_BLOCK_ALIGNMENT_SOFT),
529 DECL(AL_SOURCE_RADIUS),
531 DECL(AL_STEREO_ANGLES),
533 DECL(AL_UNUSED),
534 DECL(AL_PENDING),
535 DECL(AL_PROCESSED),
537 DECL(AL_NO_ERROR),
538 DECL(AL_INVALID_NAME),
539 DECL(AL_INVALID_ENUM),
540 DECL(AL_INVALID_VALUE),
541 DECL(AL_INVALID_OPERATION),
542 DECL(AL_OUT_OF_MEMORY),
544 DECL(AL_VENDOR),
545 DECL(AL_VERSION),
546 DECL(AL_RENDERER),
547 DECL(AL_EXTENSIONS),
549 DECL(AL_DOPPLER_FACTOR),
550 DECL(AL_DOPPLER_VELOCITY),
551 DECL(AL_DISTANCE_MODEL),
552 DECL(AL_SPEED_OF_SOUND),
553 DECL(AL_SOURCE_DISTANCE_MODEL),
554 DECL(AL_DEFERRED_UPDATES_SOFT),
555 DECL(AL_GAIN_LIMIT_SOFT),
557 DECL(AL_INVERSE_DISTANCE),
558 DECL(AL_INVERSE_DISTANCE_CLAMPED),
559 DECL(AL_LINEAR_DISTANCE),
560 DECL(AL_LINEAR_DISTANCE_CLAMPED),
561 DECL(AL_EXPONENT_DISTANCE),
562 DECL(AL_EXPONENT_DISTANCE_CLAMPED),
564 DECL(AL_FILTER_TYPE),
565 DECL(AL_FILTER_NULL),
566 DECL(AL_FILTER_LOWPASS),
567 DECL(AL_FILTER_HIGHPASS),
568 DECL(AL_FILTER_BANDPASS),
570 DECL(AL_LOWPASS_GAIN),
571 DECL(AL_LOWPASS_GAINHF),
573 DECL(AL_HIGHPASS_GAIN),
574 DECL(AL_HIGHPASS_GAINLF),
576 DECL(AL_BANDPASS_GAIN),
577 DECL(AL_BANDPASS_GAINHF),
578 DECL(AL_BANDPASS_GAINLF),
580 DECL(AL_EFFECT_TYPE),
581 DECL(AL_EFFECT_NULL),
582 DECL(AL_EFFECT_REVERB),
583 DECL(AL_EFFECT_EAXREVERB),
584 DECL(AL_EFFECT_CHORUS),
585 DECL(AL_EFFECT_DISTORTION),
586 DECL(AL_EFFECT_ECHO),
587 DECL(AL_EFFECT_FLANGER),
588 #if 0
589 DECL(AL_EFFECT_FREQUENCY_SHIFTER),
590 DECL(AL_EFFECT_VOCAL_MORPHER),
591 DECL(AL_EFFECT_PITCH_SHIFTER),
592 #endif
593 DECL(AL_EFFECT_RING_MODULATOR),
594 #if 0
595 DECL(AL_EFFECT_AUTOWAH),
596 #endif
597 DECL(AL_EFFECT_COMPRESSOR),
598 DECL(AL_EFFECT_EQUALIZER),
599 DECL(AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT),
600 DECL(AL_EFFECT_DEDICATED_DIALOGUE),
602 DECL(AL_EAXREVERB_DENSITY),
603 DECL(AL_EAXREVERB_DIFFUSION),
604 DECL(AL_EAXREVERB_GAIN),
605 DECL(AL_EAXREVERB_GAINHF),
606 DECL(AL_EAXREVERB_GAINLF),
607 DECL(AL_EAXREVERB_DECAY_TIME),
608 DECL(AL_EAXREVERB_DECAY_HFRATIO),
609 DECL(AL_EAXREVERB_DECAY_LFRATIO),
610 DECL(AL_EAXREVERB_REFLECTIONS_GAIN),
611 DECL(AL_EAXREVERB_REFLECTIONS_DELAY),
612 DECL(AL_EAXREVERB_REFLECTIONS_PAN),
613 DECL(AL_EAXREVERB_LATE_REVERB_GAIN),
614 DECL(AL_EAXREVERB_LATE_REVERB_DELAY),
615 DECL(AL_EAXREVERB_LATE_REVERB_PAN),
616 DECL(AL_EAXREVERB_ECHO_TIME),
617 DECL(AL_EAXREVERB_ECHO_DEPTH),
618 DECL(AL_EAXREVERB_MODULATION_TIME),
619 DECL(AL_EAXREVERB_MODULATION_DEPTH),
620 DECL(AL_EAXREVERB_AIR_ABSORPTION_GAINHF),
621 DECL(AL_EAXREVERB_HFREFERENCE),
622 DECL(AL_EAXREVERB_LFREFERENCE),
623 DECL(AL_EAXREVERB_ROOM_ROLLOFF_FACTOR),
624 DECL(AL_EAXREVERB_DECAY_HFLIMIT),
626 DECL(AL_REVERB_DENSITY),
627 DECL(AL_REVERB_DIFFUSION),
628 DECL(AL_REVERB_GAIN),
629 DECL(AL_REVERB_GAINHF),
630 DECL(AL_REVERB_DECAY_TIME),
631 DECL(AL_REVERB_DECAY_HFRATIO),
632 DECL(AL_REVERB_REFLECTIONS_GAIN),
633 DECL(AL_REVERB_REFLECTIONS_DELAY),
634 DECL(AL_REVERB_LATE_REVERB_GAIN),
635 DECL(AL_REVERB_LATE_REVERB_DELAY),
636 DECL(AL_REVERB_AIR_ABSORPTION_GAINHF),
637 DECL(AL_REVERB_ROOM_ROLLOFF_FACTOR),
638 DECL(AL_REVERB_DECAY_HFLIMIT),
640 DECL(AL_CHORUS_WAVEFORM),
641 DECL(AL_CHORUS_PHASE),
642 DECL(AL_CHORUS_RATE),
643 DECL(AL_CHORUS_DEPTH),
644 DECL(AL_CHORUS_FEEDBACK),
645 DECL(AL_CHORUS_DELAY),
647 DECL(AL_DISTORTION_EDGE),
648 DECL(AL_DISTORTION_GAIN),
649 DECL(AL_DISTORTION_LOWPASS_CUTOFF),
650 DECL(AL_DISTORTION_EQCENTER),
651 DECL(AL_DISTORTION_EQBANDWIDTH),
653 DECL(AL_ECHO_DELAY),
654 DECL(AL_ECHO_LRDELAY),
655 DECL(AL_ECHO_DAMPING),
656 DECL(AL_ECHO_FEEDBACK),
657 DECL(AL_ECHO_SPREAD),
659 DECL(AL_FLANGER_WAVEFORM),
660 DECL(AL_FLANGER_PHASE),
661 DECL(AL_FLANGER_RATE),
662 DECL(AL_FLANGER_DEPTH),
663 DECL(AL_FLANGER_FEEDBACK),
664 DECL(AL_FLANGER_DELAY),
666 DECL(AL_RING_MODULATOR_FREQUENCY),
667 DECL(AL_RING_MODULATOR_HIGHPASS_CUTOFF),
668 DECL(AL_RING_MODULATOR_WAVEFORM),
670 DECL(AL_COMPRESSOR_ONOFF),
672 DECL(AL_EQUALIZER_LOW_GAIN),
673 DECL(AL_EQUALIZER_LOW_CUTOFF),
674 DECL(AL_EQUALIZER_MID1_GAIN),
675 DECL(AL_EQUALIZER_MID1_CENTER),
676 DECL(AL_EQUALIZER_MID1_WIDTH),
677 DECL(AL_EQUALIZER_MID2_GAIN),
678 DECL(AL_EQUALIZER_MID2_CENTER),
679 DECL(AL_EQUALIZER_MID2_WIDTH),
680 DECL(AL_EQUALIZER_HIGH_GAIN),
681 DECL(AL_EQUALIZER_HIGH_CUTOFF),
683 DECL(AL_DEDICATED_GAIN),
685 { NULL, (ALCenum)0 }
687 #undef DECL
689 static const ALCchar alcNoError[] = "No Error";
690 static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
691 static const ALCchar alcErrInvalidContext[] = "Invalid Context";
692 static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
693 static const ALCchar alcErrInvalidValue[] = "Invalid Value";
694 static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
697 /************************************************
698 * Global variables
699 ************************************************/
701 /* Enumerated device names */
702 static const ALCchar alcDefaultName[] = "OpenAL Soft\0";
704 static al_string alcAllDevicesList;
705 static al_string alcCaptureDeviceList;
707 /* Default is always the first in the list */
708 static ALCchar *alcDefaultAllDevicesSpecifier;
709 static ALCchar *alcCaptureDefaultDeviceSpecifier;
711 /* Default context extensions */
712 static const ALchar alExtList[] =
713 "AL_EXT_ALAW AL_EXT_BFORMAT AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE "
714 "AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS "
715 "AL_EXT_MULAW AL_EXT_MULAW_BFORMAT AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET "
716 "AL_EXT_source_distance_model AL_EXT_SOURCE_RADIUS AL_EXT_STEREO_ANGLES "
717 "AL_LOKI_quadriphonic AL_SOFT_block_alignment AL_SOFT_deferred_updates "
718 "AL_SOFT_direct_channels AL_SOFT_gain_clamp_ex AL_SOFT_loop_points "
719 "AL_SOFT_MSADPCM AL_SOFT_source_latency AL_SOFT_source_length";
721 static ATOMIC(ALCenum) LastNullDeviceError = ATOMIC_INIT_STATIC(ALC_NO_ERROR);
723 /* Thread-local current context */
724 static altss_t LocalContext;
725 /* Process-wide current context */
726 static ATOMIC(ALCcontext*) GlobalContext = ATOMIC_INIT_STATIC(NULL);
728 /* Mixing thread piority level */
729 ALint RTPrioLevel;
731 FILE *LogFile;
732 #ifdef _DEBUG
733 enum LogLevel LogLevel = LogWarning;
734 #else
735 enum LogLevel LogLevel = LogError;
736 #endif
738 /* Flag to trap ALC device errors */
739 static ALCboolean TrapALCError = ALC_FALSE;
741 /* One-time configuration init control */
742 static alonce_flag alc_config_once = AL_ONCE_FLAG_INIT;
744 /* Default effect that applies to sources that don't have an effect on send 0 */
745 static ALeffect DefaultEffect;
747 /* Flag to specify if alcSuspendContext/alcProcessContext should defer/process
748 * updates.
750 static ALCboolean SuspendDefers = ALC_TRUE;
753 /************************************************
754 * ALC information
755 ************************************************/
756 static const ALCchar alcNoDeviceExtList[] =
757 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
758 "ALC_EXT_thread_local_context ALC_SOFT_loopback";
759 static const ALCchar alcExtensionList[] =
760 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
761 "ALC_EXT_DEDICATED ALC_EXT_disconnect ALC_EXT_EFX "
762 "ALC_EXT_thread_local_context ALC_SOFTX_device_clock ALC_SOFT_HRTF "
763 "ALC_SOFT_loopback ALC_SOFT_pause_device";
764 static const ALCint alcMajorVersion = 1;
765 static const ALCint alcMinorVersion = 1;
767 static const ALCint alcEFXMajorVersion = 1;
768 static const ALCint alcEFXMinorVersion = 0;
771 /************************************************
772 * Device lists
773 ************************************************/
774 static ATOMIC(ALCdevice*) DeviceList = ATOMIC_INIT_STATIC(NULL);
776 static almtx_t ListLock;
777 static inline void LockLists(void)
779 int ret = almtx_lock(&ListLock);
780 assert(ret == althrd_success);
782 static inline void UnlockLists(void)
784 int ret = almtx_unlock(&ListLock);
785 assert(ret == althrd_success);
788 /************************************************
789 * Library initialization
790 ************************************************/
791 #if defined(_WIN32)
792 static void alc_init(void);
793 static void alc_deinit(void);
794 static void alc_deinit_safe(void);
796 #ifndef AL_LIBTYPE_STATIC
797 BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD reason, LPVOID lpReserved)
799 switch(reason)
801 case DLL_PROCESS_ATTACH:
802 /* Pin the DLL so we won't get unloaded until the process terminates */
803 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
804 (WCHAR*)hModule, &hModule);
805 alc_init();
806 break;
808 case DLL_THREAD_DETACH:
809 break;
811 case DLL_PROCESS_DETACH:
812 if(!lpReserved)
813 alc_deinit();
814 else
815 alc_deinit_safe();
816 break;
818 return TRUE;
820 #elif defined(_MSC_VER)
821 #pragma section(".CRT$XCU",read)
822 static void alc_constructor(void);
823 static void alc_destructor(void);
824 __declspec(allocate(".CRT$XCU")) void (__cdecl* alc_constructor_)(void) = alc_constructor;
826 static void alc_constructor(void)
828 atexit(alc_destructor);
829 alc_init();
832 static void alc_destructor(void)
834 alc_deinit();
836 #elif defined(HAVE_GCC_DESTRUCTOR)
837 static void alc_init(void) __attribute__((constructor));
838 static void alc_deinit(void) __attribute__((destructor));
839 #else
840 #error "No static initialization available on this platform!"
841 #endif
843 #elif defined(HAVE_GCC_DESTRUCTOR)
845 static void alc_init(void) __attribute__((constructor));
846 static void alc_deinit(void) __attribute__((destructor));
848 #else
849 #error "No global initialization available on this platform!"
850 #endif
852 static void ReleaseThreadCtx(void *ptr);
853 static void alc_init(void)
855 const char *str;
856 int ret;
858 LogFile = stderr;
860 AL_STRING_INIT(alcAllDevicesList);
861 AL_STRING_INIT(alcCaptureDeviceList);
863 str = getenv("__ALSOFT_HALF_ANGLE_CONES");
864 if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
865 ConeScale *= 0.5f;
867 str = getenv("__ALSOFT_REVERSE_Z");
868 if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
869 ZScale *= -1.0f;
871 ret = altss_create(&LocalContext, ReleaseThreadCtx);
872 assert(ret == althrd_success);
874 ret = almtx_init(&ListLock, almtx_recursive);
875 assert(ret == althrd_success);
877 ThunkInit();
880 static void alc_initconfig(void)
882 const char *devs, *str;
883 ALuint capfilter;
884 float valf;
885 int i, n;
887 str = getenv("ALSOFT_LOGLEVEL");
888 if(str)
890 long lvl = strtol(str, NULL, 0);
891 if(lvl >= NoLog && lvl <= LogRef)
892 LogLevel = lvl;
895 str = getenv("ALSOFT_LOGFILE");
896 if(str && str[0])
898 FILE *logfile = al_fopen(str, "wt");
899 if(logfile) LogFile = logfile;
900 else ERR("Failed to open log file '%s'\n", str);
904 char buf[1024] = "";
905 int len = snprintf(buf, sizeof(buf), "%s", BackendList[0].name);
906 for(i = 1;BackendList[i].name;i++)
907 len += snprintf(buf+len, sizeof(buf)-len, ", %s", BackendList[i].name);
908 TRACE("Supported backends: %s\n", buf);
910 ReadALConfig();
912 str = getenv("__ALSOFT_SUSPEND_CONTEXT");
913 if(str && *str)
915 if(strcasecmp(str, "ignore") == 0)
917 SuspendDefers = ALC_FALSE;
918 TRACE("Selected context suspend behavior, \"ignore\"\n");
920 else
921 ERR("Unhandled context suspend behavior setting: \"%s\"\n", str);
924 capfilter = 0;
925 #if defined(HAVE_SSE4_1)
926 capfilter |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3 | CPU_CAP_SSE4_1;
927 #elif defined(HAVE_SSE3)
928 capfilter |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3;
929 #elif defined(HAVE_SSE2)
930 capfilter |= CPU_CAP_SSE | CPU_CAP_SSE2;
931 #elif defined(HAVE_SSE)
932 capfilter |= CPU_CAP_SSE;
933 #endif
934 #ifdef HAVE_NEON
935 capfilter |= CPU_CAP_NEON;
936 #endif
937 if(ConfigValueStr(NULL, NULL, "disable-cpu-exts", &str))
939 if(strcasecmp(str, "all") == 0)
940 capfilter = 0;
941 else
943 size_t len;
944 const char *next = str;
946 do {
947 str = next;
948 while(isspace(str[0]))
949 str++;
950 next = strchr(str, ',');
952 if(!str[0] || str[0] == ',')
953 continue;
955 len = (next ? ((size_t)(next-str)) : strlen(str));
956 while(len > 0 && isspace(str[len-1]))
957 len--;
958 if(len == 3 && strncasecmp(str, "sse", len) == 0)
959 capfilter &= ~CPU_CAP_SSE;
960 else if(len == 4 && strncasecmp(str, "sse2", len) == 0)
961 capfilter &= ~CPU_CAP_SSE2;
962 else if(len == 4 && strncasecmp(str, "sse3", len) == 0)
963 capfilter &= ~CPU_CAP_SSE3;
964 else if(len == 6 && strncasecmp(str, "sse4.1", len) == 0)
965 capfilter &= ~CPU_CAP_SSE4_1;
966 else if(len == 4 && strncasecmp(str, "neon", len) == 0)
967 capfilter &= ~CPU_CAP_NEON;
968 else
969 WARN("Invalid CPU extension \"%s\"\n", str);
970 } while(next++);
973 FillCPUCaps(capfilter);
975 #ifdef _WIN32
976 RTPrioLevel = 1;
977 #else
978 RTPrioLevel = 0;
979 #endif
980 ConfigValueInt(NULL, NULL, "rt-prio", &RTPrioLevel);
982 aluInitMixer();
984 str = getenv("ALSOFT_TRAP_ERROR");
985 if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
987 TrapALError = AL_TRUE;
988 TrapALCError = AL_TRUE;
990 else
992 str = getenv("ALSOFT_TRAP_AL_ERROR");
993 if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
994 TrapALError = AL_TRUE;
995 TrapALError = GetConfigValueBool(NULL, NULL, "trap-al-error", TrapALError);
997 str = getenv("ALSOFT_TRAP_ALC_ERROR");
998 if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
999 TrapALCError = ALC_TRUE;
1000 TrapALCError = GetConfigValueBool(NULL, NULL, "trap-alc-error", TrapALCError);
1003 if(ConfigValueFloat(NULL, "reverb", "boost", &valf))
1004 ReverbBoost *= powf(10.0f, valf / 20.0f);
1006 EmulateEAXReverb = GetConfigValueBool(NULL, "reverb", "emulate-eax", AL_FALSE);
1008 if(((devs=getenv("ALSOFT_DRIVERS")) && devs[0]) ||
1009 ConfigValueStr(NULL, NULL, "drivers", &devs))
1011 int n;
1012 size_t len;
1013 const char *next = devs;
1014 int endlist, delitem;
1016 i = 0;
1017 do {
1018 devs = next;
1019 while(isspace(devs[0]))
1020 devs++;
1021 next = strchr(devs, ',');
1023 delitem = (devs[0] == '-');
1024 if(devs[0] == '-') devs++;
1026 if(!devs[0] || devs[0] == ',')
1028 endlist = 0;
1029 continue;
1031 endlist = 1;
1033 len = (next ? ((size_t)(next-devs)) : strlen(devs));
1034 while(len > 0 && isspace(devs[len-1]))
1035 len--;
1036 for(n = i;BackendList[n].name;n++)
1038 if(len == strlen(BackendList[n].name) &&
1039 strncmp(BackendList[n].name, devs, len) == 0)
1041 if(delitem)
1043 do {
1044 BackendList[n] = BackendList[n+1];
1045 ++n;
1046 } while(BackendList[n].name);
1048 else
1050 struct BackendInfo Bkp = BackendList[n];
1051 while(n > i)
1053 BackendList[n] = BackendList[n-1];
1054 --n;
1056 BackendList[n] = Bkp;
1058 i++;
1060 break;
1063 } while(next++);
1065 if(endlist)
1067 BackendList[i].name = NULL;
1068 BackendList[i].getFactory = NULL;
1069 BackendList[i].Init = NULL;
1070 BackendList[i].Deinit = NULL;
1071 BackendList[i].Probe = NULL;
1075 for(i = 0;(BackendList[i].Init || BackendList[i].getFactory) && (!PlaybackBackend.name || !CaptureBackend.name);i++)
1077 if(BackendList[i].getFactory)
1079 ALCbackendFactory *factory = BackendList[i].getFactory();
1080 if(!V0(factory,init)())
1082 WARN("Failed to initialize backend \"%s\"\n", BackendList[i].name);
1083 continue;
1086 TRACE("Initialized backend \"%s\"\n", BackendList[i].name);
1087 if(!PlaybackBackend.name && V(factory,querySupport)(ALCbackend_Playback))
1089 PlaybackBackend = BackendList[i];
1090 TRACE("Added \"%s\" for playback\n", PlaybackBackend.name);
1092 if(!CaptureBackend.name && V(factory,querySupport)(ALCbackend_Capture))
1094 CaptureBackend = BackendList[i];
1095 TRACE("Added \"%s\" for capture\n", CaptureBackend.name);
1098 continue;
1101 if(!BackendList[i].Init(&BackendList[i].Funcs))
1103 WARN("Failed to initialize backend \"%s\"\n", BackendList[i].name);
1104 continue;
1107 TRACE("Initialized backend \"%s\"\n", BackendList[i].name);
1108 if(BackendList[i].Funcs.OpenPlayback && !PlaybackBackend.name)
1110 PlaybackBackend = BackendList[i];
1111 TRACE("Added \"%s\" for playback\n", PlaybackBackend.name);
1113 if(BackendList[i].Funcs.OpenCapture && !CaptureBackend.name)
1115 CaptureBackend = BackendList[i];
1116 TRACE("Added \"%s\" for capture\n", CaptureBackend.name);
1120 ALCbackendFactory *factory = ALCloopbackFactory_getFactory();
1121 V0(factory,init)();
1124 if(!PlaybackBackend.name)
1125 WARN("No playback backend available!\n");
1126 if(!CaptureBackend.name)
1127 WARN("No capture backend available!\n");
1129 if(ConfigValueStr(NULL, NULL, "excludefx", &str))
1131 size_t len;
1132 const char *next = str;
1134 do {
1135 str = next;
1136 next = strchr(str, ',');
1138 if(!str[0] || next == str)
1139 continue;
1141 len = (next ? ((size_t)(next-str)) : strlen(str));
1142 for(n = 0;EffectList[n].name;n++)
1144 if(len == strlen(EffectList[n].name) &&
1145 strncmp(EffectList[n].name, str, len) == 0)
1146 DisabledEffects[EffectList[n].type] = AL_TRUE;
1148 } while(next++);
1151 InitEffectFactoryMap();
1153 InitEffect(&DefaultEffect);
1154 str = getenv("ALSOFT_DEFAULT_REVERB");
1155 if((str && str[0]) || ConfigValueStr(NULL, NULL, "default-reverb", &str))
1156 LoadReverbPreset(str, &DefaultEffect);
1158 #define DO_INITCONFIG() alcall_once(&alc_config_once, alc_initconfig)
1161 /************************************************
1162 * Library deinitialization
1163 ************************************************/
1164 static void alc_cleanup(void)
1166 ALCdevice *dev;
1168 AL_STRING_DEINIT(alcAllDevicesList);
1169 AL_STRING_DEINIT(alcCaptureDeviceList);
1171 free(alcDefaultAllDevicesSpecifier);
1172 alcDefaultAllDevicesSpecifier = NULL;
1173 free(alcCaptureDefaultDeviceSpecifier);
1174 alcCaptureDefaultDeviceSpecifier = NULL;
1176 if((dev=ATOMIC_EXCHANGE(ALCdevice*, &DeviceList, NULL)) != NULL)
1178 ALCuint num = 0;
1179 do {
1180 num++;
1181 } while((dev=dev->next) != NULL);
1182 ERR("%u device%s not closed\n", num, (num>1)?"s":"");
1185 DeinitEffectFactoryMap();
1188 static void alc_deinit_safe(void)
1190 alc_cleanup();
1192 FreeHrtfs();
1193 FreeALConfig();
1195 ThunkExit();
1196 almtx_destroy(&ListLock);
1197 altss_delete(LocalContext);
1199 if(LogFile != stderr)
1200 fclose(LogFile);
1201 LogFile = NULL;
1204 static void alc_deinit(void)
1206 int i;
1208 alc_cleanup();
1210 memset(&PlaybackBackend, 0, sizeof(PlaybackBackend));
1211 memset(&CaptureBackend, 0, sizeof(CaptureBackend));
1213 for(i = 0;BackendList[i].Deinit || BackendList[i].getFactory;i++)
1215 if(!BackendList[i].getFactory)
1216 BackendList[i].Deinit();
1217 else
1219 ALCbackendFactory *factory = BackendList[i].getFactory();
1220 V0(factory,deinit)();
1224 ALCbackendFactory *factory = ALCloopbackFactory_getFactory();
1225 V0(factory,deinit)();
1228 alc_deinit_safe();
1232 /************************************************
1233 * Device enumeration
1234 ************************************************/
1235 static void ProbeDevices(al_string *list, struct BackendInfo *backendinfo, enum DevProbe type)
1237 DO_INITCONFIG();
1239 LockLists();
1240 al_string_clear(list);
1242 if(backendinfo->Probe)
1243 backendinfo->Probe(type);
1244 else if(backendinfo->getFactory)
1246 ALCbackendFactory *factory = backendinfo->getFactory();
1247 V(factory,probe)(type);
1250 UnlockLists();
1252 static void ProbeAllDevicesList(void)
1253 { ProbeDevices(&alcAllDevicesList, &PlaybackBackend, ALL_DEVICE_PROBE); }
1254 static void ProbeCaptureDeviceList(void)
1255 { ProbeDevices(&alcCaptureDeviceList, &CaptureBackend, CAPTURE_DEVICE_PROBE); }
1257 static void AppendDevice(const ALCchar *name, al_string *devnames)
1259 size_t len = strlen(name);
1260 if(len > 0)
1261 al_string_append_range(devnames, name, name+len+1);
1263 void AppendAllDevicesList(const ALCchar *name)
1264 { AppendDevice(name, &alcAllDevicesList); }
1265 void AppendCaptureDeviceList(const ALCchar *name)
1266 { AppendDevice(name, &alcCaptureDeviceList); }
1269 /************************************************
1270 * Device format information
1271 ************************************************/
1272 const ALCchar *DevFmtTypeString(enum DevFmtType type)
1274 switch(type)
1276 case DevFmtByte: return "Signed Byte";
1277 case DevFmtUByte: return "Unsigned Byte";
1278 case DevFmtShort: return "Signed Short";
1279 case DevFmtUShort: return "Unsigned Short";
1280 case DevFmtInt: return "Signed Int";
1281 case DevFmtUInt: return "Unsigned Int";
1282 case DevFmtFloat: return "Float";
1284 return "(unknown type)";
1286 const ALCchar *DevFmtChannelsString(enum DevFmtChannels chans)
1288 switch(chans)
1290 case DevFmtMono: return "Mono";
1291 case DevFmtStereo: return "Stereo";
1292 case DevFmtQuad: return "Quadraphonic";
1293 case DevFmtX51: return "5.1 Surround";
1294 case DevFmtX51Rear: return "5.1 Surround (Rear)";
1295 case DevFmtX61: return "6.1 Surround";
1296 case DevFmtX71: return "7.1 Surround";
1297 case DevFmtAmbi1: return "Ambisonic (1st Order)";
1298 case DevFmtAmbi2: return "Ambisonic (2nd Order)";
1299 case DevFmtAmbi3: return "Ambisonic (3rd Order)";
1301 return "(unknown channels)";
1304 extern inline ALuint FrameSizeFromDevFmt(enum DevFmtChannels chans, enum DevFmtType type);
1305 ALuint BytesFromDevFmt(enum DevFmtType type)
1307 switch(type)
1309 case DevFmtByte: return sizeof(ALbyte);
1310 case DevFmtUByte: return sizeof(ALubyte);
1311 case DevFmtShort: return sizeof(ALshort);
1312 case DevFmtUShort: return sizeof(ALushort);
1313 case DevFmtInt: return sizeof(ALint);
1314 case DevFmtUInt: return sizeof(ALuint);
1315 case DevFmtFloat: return sizeof(ALfloat);
1317 return 0;
1319 ALuint ChannelsFromDevFmt(enum DevFmtChannels chans)
1321 switch(chans)
1323 case DevFmtMono: return 1;
1324 case DevFmtStereo: return 2;
1325 case DevFmtQuad: return 4;
1326 case DevFmtX51: return 6;
1327 case DevFmtX51Rear: return 6;
1328 case DevFmtX61: return 7;
1329 case DevFmtX71: return 8;
1330 case DevFmtAmbi1: return 4;
1331 case DevFmtAmbi2: return 9;
1332 case DevFmtAmbi3: return 16;
1334 return 0;
1337 static ALboolean DecomposeDevFormat(ALenum format, enum DevFmtChannels *chans,
1338 enum DevFmtType *type)
1340 static const struct {
1341 ALenum format;
1342 enum DevFmtChannels channels;
1343 enum DevFmtType type;
1344 } list[] = {
1345 { AL_FORMAT_MONO8, DevFmtMono, DevFmtUByte },
1346 { AL_FORMAT_MONO16, DevFmtMono, DevFmtShort },
1347 { AL_FORMAT_MONO_FLOAT32, DevFmtMono, DevFmtFloat },
1349 { AL_FORMAT_STEREO8, DevFmtStereo, DevFmtUByte },
1350 { AL_FORMAT_STEREO16, DevFmtStereo, DevFmtShort },
1351 { AL_FORMAT_STEREO_FLOAT32, DevFmtStereo, DevFmtFloat },
1353 { AL_FORMAT_QUAD8, DevFmtQuad, DevFmtUByte },
1354 { AL_FORMAT_QUAD16, DevFmtQuad, DevFmtShort },
1355 { AL_FORMAT_QUAD32, DevFmtQuad, DevFmtFloat },
1357 { AL_FORMAT_51CHN8, DevFmtX51, DevFmtUByte },
1358 { AL_FORMAT_51CHN16, DevFmtX51, DevFmtShort },
1359 { AL_FORMAT_51CHN32, DevFmtX51, DevFmtFloat },
1361 { AL_FORMAT_61CHN8, DevFmtX61, DevFmtUByte },
1362 { AL_FORMAT_61CHN16, DevFmtX61, DevFmtShort },
1363 { AL_FORMAT_61CHN32, DevFmtX61, DevFmtFloat },
1365 { AL_FORMAT_71CHN8, DevFmtX71, DevFmtUByte },
1366 { AL_FORMAT_71CHN16, DevFmtX71, DevFmtShort },
1367 { AL_FORMAT_71CHN32, DevFmtX71, DevFmtFloat },
1369 ALuint i;
1371 for(i = 0;i < COUNTOF(list);i++)
1373 if(list[i].format == format)
1375 *chans = list[i].channels;
1376 *type = list[i].type;
1377 return AL_TRUE;
1381 return AL_FALSE;
1384 static ALCboolean IsValidALCType(ALCenum type)
1386 switch(type)
1388 case ALC_BYTE_SOFT:
1389 case ALC_UNSIGNED_BYTE_SOFT:
1390 case ALC_SHORT_SOFT:
1391 case ALC_UNSIGNED_SHORT_SOFT:
1392 case ALC_INT_SOFT:
1393 case ALC_UNSIGNED_INT_SOFT:
1394 case ALC_FLOAT_SOFT:
1395 return ALC_TRUE;
1397 return ALC_FALSE;
1400 static ALCboolean IsValidALCChannels(ALCenum channels)
1402 switch(channels)
1404 case ALC_MONO_SOFT:
1405 case ALC_STEREO_SOFT:
1406 case ALC_QUAD_SOFT:
1407 case ALC_5POINT1_SOFT:
1408 case ALC_6POINT1_SOFT:
1409 case ALC_7POINT1_SOFT:
1410 return ALC_TRUE;
1412 return ALC_FALSE;
1416 /************************************************
1417 * Miscellaneous ALC helpers
1418 ************************************************/
1420 extern inline void LockContext(ALCcontext *context);
1421 extern inline void UnlockContext(ALCcontext *context);
1423 void ALCdevice_Lock(ALCdevice *device)
1425 V0(device->Backend,lock)();
1428 void ALCdevice_Unlock(ALCdevice *device)
1430 V0(device->Backend,unlock)();
1434 /* SetDefaultWFXChannelOrder
1436 * Sets the default channel order used by WaveFormatEx.
1438 void SetDefaultWFXChannelOrder(ALCdevice *device)
1440 ALuint i;
1442 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
1443 device->RealOut.ChannelName[i] = InvalidChannel;
1445 switch(device->FmtChans)
1447 case DevFmtMono:
1448 device->RealOut.ChannelName[0] = FrontCenter;
1449 break;
1450 case DevFmtStereo:
1451 device->RealOut.ChannelName[0] = FrontLeft;
1452 device->RealOut.ChannelName[1] = FrontRight;
1453 break;
1454 case DevFmtQuad:
1455 device->RealOut.ChannelName[0] = FrontLeft;
1456 device->RealOut.ChannelName[1] = FrontRight;
1457 device->RealOut.ChannelName[2] = BackLeft;
1458 device->RealOut.ChannelName[3] = BackRight;
1459 break;
1460 case DevFmtX51:
1461 device->RealOut.ChannelName[0] = FrontLeft;
1462 device->RealOut.ChannelName[1] = FrontRight;
1463 device->RealOut.ChannelName[2] = FrontCenter;
1464 device->RealOut.ChannelName[3] = LFE;
1465 device->RealOut.ChannelName[4] = SideLeft;
1466 device->RealOut.ChannelName[5] = SideRight;
1467 break;
1468 case DevFmtX51Rear:
1469 device->RealOut.ChannelName[0] = FrontLeft;
1470 device->RealOut.ChannelName[1] = FrontRight;
1471 device->RealOut.ChannelName[2] = FrontCenter;
1472 device->RealOut.ChannelName[3] = LFE;
1473 device->RealOut.ChannelName[4] = BackLeft;
1474 device->RealOut.ChannelName[5] = BackRight;
1475 break;
1476 case DevFmtX61:
1477 device->RealOut.ChannelName[0] = FrontLeft;
1478 device->RealOut.ChannelName[1] = FrontRight;
1479 device->RealOut.ChannelName[2] = FrontCenter;
1480 device->RealOut.ChannelName[3] = LFE;
1481 device->RealOut.ChannelName[4] = BackCenter;
1482 device->RealOut.ChannelName[5] = SideLeft;
1483 device->RealOut.ChannelName[6] = SideRight;
1484 break;
1485 case DevFmtX71:
1486 device->RealOut.ChannelName[0] = FrontLeft;
1487 device->RealOut.ChannelName[1] = FrontRight;
1488 device->RealOut.ChannelName[2] = FrontCenter;
1489 device->RealOut.ChannelName[3] = LFE;
1490 device->RealOut.ChannelName[4] = BackLeft;
1491 device->RealOut.ChannelName[5] = BackRight;
1492 device->RealOut.ChannelName[6] = SideLeft;
1493 device->RealOut.ChannelName[7] = SideRight;
1494 break;
1495 case DevFmtAmbi1:
1496 device->RealOut.ChannelName[0] = Aux0;
1497 device->RealOut.ChannelName[1] = Aux1;
1498 device->RealOut.ChannelName[2] = Aux2;
1499 device->RealOut.ChannelName[3] = Aux3;
1500 break;
1501 case DevFmtAmbi2:
1502 device->RealOut.ChannelName[0] = Aux0;
1503 device->RealOut.ChannelName[1] = Aux1;
1504 device->RealOut.ChannelName[2] = Aux2;
1505 device->RealOut.ChannelName[3] = Aux3;
1506 device->RealOut.ChannelName[4] = Aux4;
1507 device->RealOut.ChannelName[5] = Aux5;
1508 device->RealOut.ChannelName[6] = Aux6;
1509 device->RealOut.ChannelName[7] = Aux7;
1510 device->RealOut.ChannelName[8] = Aux8;
1511 break;
1512 case DevFmtAmbi3:
1513 device->RealOut.ChannelName[0] = Aux0;
1514 device->RealOut.ChannelName[1] = Aux1;
1515 device->RealOut.ChannelName[2] = Aux2;
1516 device->RealOut.ChannelName[3] = Aux3;
1517 device->RealOut.ChannelName[4] = Aux4;
1518 device->RealOut.ChannelName[5] = Aux5;
1519 device->RealOut.ChannelName[6] = Aux6;
1520 device->RealOut.ChannelName[7] = Aux7;
1521 device->RealOut.ChannelName[8] = Aux8;
1522 device->RealOut.ChannelName[9] = Aux9;
1523 device->RealOut.ChannelName[10] = Aux10;
1524 device->RealOut.ChannelName[11] = Aux11;
1525 device->RealOut.ChannelName[12] = Aux12;
1526 device->RealOut.ChannelName[13] = Aux13;
1527 device->RealOut.ChannelName[14] = Aux14;
1528 device->RealOut.ChannelName[15] = Aux15;
1529 break;
1533 /* SetDefaultChannelOrder
1535 * Sets the default channel order used by most non-WaveFormatEx-based APIs.
1537 void SetDefaultChannelOrder(ALCdevice *device)
1539 ALuint i;
1541 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
1542 device->RealOut.ChannelName[i] = InvalidChannel;
1544 switch(device->FmtChans)
1546 case DevFmtX51Rear:
1547 device->RealOut.ChannelName[0] = FrontLeft;
1548 device->RealOut.ChannelName[1] = FrontRight;
1549 device->RealOut.ChannelName[2] = BackLeft;
1550 device->RealOut.ChannelName[3] = BackRight;
1551 device->RealOut.ChannelName[4] = FrontCenter;
1552 device->RealOut.ChannelName[5] = LFE;
1553 return;
1554 case DevFmtX71:
1555 device->RealOut.ChannelName[0] = FrontLeft;
1556 device->RealOut.ChannelName[1] = FrontRight;
1557 device->RealOut.ChannelName[2] = BackLeft;
1558 device->RealOut.ChannelName[3] = BackRight;
1559 device->RealOut.ChannelName[4] = FrontCenter;
1560 device->RealOut.ChannelName[5] = LFE;
1561 device->RealOut.ChannelName[6] = SideLeft;
1562 device->RealOut.ChannelName[7] = SideRight;
1563 return;
1565 /* Same as WFX order */
1566 case DevFmtMono:
1567 case DevFmtStereo:
1568 case DevFmtQuad:
1569 case DevFmtX51:
1570 case DevFmtX61:
1571 case DevFmtAmbi1:
1572 case DevFmtAmbi2:
1573 case DevFmtAmbi3:
1574 SetDefaultWFXChannelOrder(device);
1575 break;
1579 extern inline ALint GetChannelIndex(const enum Channel names[MAX_OUTPUT_CHANNELS], enum Channel chan);
1582 /* ALCcontext_DeferUpdates
1584 * Defers/suspends updates for the given context's listener and sources. This
1585 * does *NOT* stop mixing, but rather prevents certain property changes from
1586 * taking effect.
1588 void ALCcontext_DeferUpdates(ALCcontext *context, ALenum type)
1590 ATOMIC_STORE(&context->DeferUpdates, type);
1593 /* ALCcontext_ProcessUpdates
1595 * Resumes update processing after being deferred.
1597 void ALCcontext_ProcessUpdates(ALCcontext *context)
1599 ALCdevice *device = context->Device;
1601 ReadLock(&context->PropLock);
1602 if(ATOMIC_EXCHANGE(ALenum, &context->DeferUpdates, AL_FALSE))
1604 ALsizei pos;
1605 uint updates;
1607 /* Tell the mixer to stop applying updates, then wait for any active
1608 * updating to finish, before providing updates.
1610 ATOMIC_STORE(&context->HoldUpdates, AL_TRUE);
1611 while(((updates=ReadRef(&context->UpdateCount))&1) != 0)
1612 althrd_yield();
1614 UpdateListenerProps(context);
1615 UpdateAllEffectSlotProps(context);
1617 LockUIntMapRead(&context->SourceMap);
1618 V0(device->Backend,lock)();
1619 for(pos = 0;pos < context->SourceMap.size;pos++)
1621 ALsource *Source = context->SourceMap.values[pos];
1622 ALenum new_state;
1624 if((Source->state == AL_PLAYING || Source->state == AL_PAUSED) &&
1625 Source->OffsetType != AL_NONE)
1627 WriteLock(&Source->queue_lock);
1628 ApplyOffset(Source);
1629 WriteUnlock(&Source->queue_lock);
1632 new_state = Source->new_state;
1633 Source->new_state = AL_NONE;
1634 if(new_state)
1635 SetSourceState(Source, context, new_state);
1637 V0(device->Backend,unlock)();
1638 UnlockUIntMapRead(&context->SourceMap);
1640 UpdateAllSourceProps(context);
1642 /* Now with all updates declared, let the mixer continue applying them
1643 * so they all happen at once.
1645 ATOMIC_STORE(&context->HoldUpdates, AL_FALSE);
1647 ReadUnlock(&context->PropLock);
1651 /* alcSetError
1653 * Stores the latest ALC device error
1655 static void alcSetError(ALCdevice *device, ALCenum errorCode)
1657 if(TrapALCError)
1659 #ifdef _WIN32
1660 /* DebugBreak() will cause an exception if there is no debugger */
1661 if(IsDebuggerPresent())
1662 DebugBreak();
1663 #elif defined(SIGTRAP)
1664 raise(SIGTRAP);
1665 #endif
1668 if(device)
1669 ATOMIC_STORE(&device->LastError, errorCode);
1670 else
1671 ATOMIC_STORE(&LastNullDeviceError, errorCode);
1675 /* UpdateClockBase
1677 * Updates the device's base clock time with however many samples have been
1678 * done. This is used so frequency changes on the device don't cause the time
1679 * to jump forward or back.
1681 static inline void UpdateClockBase(ALCdevice *device)
1683 device->ClockBase += device->SamplesDone * DEVICE_CLOCK_RES / device->Frequency;
1684 device->SamplesDone = 0;
1687 /* UpdateDeviceParams
1689 * Updates device parameters according to the attribute list (caller is
1690 * responsible for holding the list lock).
1692 static ALCenum UpdateDeviceParams(ALCdevice *device, const ALCint *attrList)
1694 ALCcontext *context;
1695 enum HrtfRequestMode hrtf_appreq = Hrtf_Default;
1696 enum HrtfRequestMode hrtf_userreq = Hrtf_Default;
1697 enum DevFmtChannels oldChans;
1698 enum DevFmtType oldType;
1699 ALCuint oldFreq;
1700 FPUCtl oldMode;
1701 ALCsizei hrtf_id = -1;
1702 size_t size;
1704 // Check for attributes
1705 if(device->Type == Loopback)
1707 enum {
1708 GotFreq = 1<<0,
1709 GotChans = 1<<1,
1710 GotType = 1<<2,
1711 GotAll = GotFreq|GotChans|GotType
1713 ALCuint freq, numMono, numStereo, numSends;
1714 enum DevFmtChannels schans;
1715 enum DevFmtType stype;
1716 ALCuint attrIdx = 0;
1717 ALCint gotFmt = 0;
1719 if(!attrList)
1721 WARN("Missing attributes for loopback device\n");
1722 return ALC_INVALID_VALUE;
1725 numMono = device->NumMonoSources;
1726 numStereo = device->NumStereoSources;
1727 numSends = device->NumAuxSends;
1728 schans = device->FmtChans;
1729 stype = device->FmtType;
1730 freq = device->Frequency;
1732 #define TRACE_ATTR(a, v) TRACE("Loopback %s = %d\n", #a, v)
1733 while(attrList[attrIdx])
1735 if(attrList[attrIdx] == ALC_FORMAT_CHANNELS_SOFT)
1737 ALCint val = attrList[attrIdx + 1];
1738 TRACE_ATTR(ALC_FORMAT_CHANNELS_SOFT, val);
1739 if(!IsValidALCChannels(val) || !ChannelsFromDevFmt(val))
1740 return ALC_INVALID_VALUE;
1741 schans = val;
1742 gotFmt |= GotChans;
1745 if(attrList[attrIdx] == ALC_FORMAT_TYPE_SOFT)
1747 ALCint val = attrList[attrIdx + 1];
1748 TRACE_ATTR(ALC_FORMAT_TYPE_SOFT, val);
1749 if(!IsValidALCType(val) || !BytesFromDevFmt(val))
1750 return ALC_INVALID_VALUE;
1751 stype = val;
1752 gotFmt |= GotType;
1755 if(attrList[attrIdx] == ALC_FREQUENCY)
1757 freq = attrList[attrIdx + 1];
1758 TRACE_ATTR(ALC_FREQUENCY, freq);
1759 if(freq < MIN_OUTPUT_RATE)
1760 return ALC_INVALID_VALUE;
1761 gotFmt |= GotFreq;
1764 if(attrList[attrIdx] == ALC_STEREO_SOURCES)
1766 numStereo = attrList[attrIdx + 1];
1767 TRACE_ATTR(ALC_STEREO_SOURCES, numStereo);
1768 if(numStereo > device->SourcesMax)
1769 numStereo = device->SourcesMax;
1771 numMono = device->SourcesMax - numStereo;
1774 if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS)
1776 numSends = attrList[attrIdx + 1];
1777 TRACE_ATTR(ALC_MAX_AUXILIARY_SENDS, numSends);
1780 if(attrList[attrIdx] == ALC_HRTF_SOFT)
1782 TRACE_ATTR(ALC_HRTF_SOFT, attrList[attrIdx + 1]);
1783 if(attrList[attrIdx + 1] == ALC_FALSE)
1784 hrtf_appreq = Hrtf_Disable;
1785 else if(attrList[attrIdx + 1] == ALC_TRUE)
1786 hrtf_appreq = Hrtf_Enable;
1787 else
1788 hrtf_appreq = Hrtf_Default;
1791 if(attrList[attrIdx] == ALC_HRTF_ID_SOFT)
1793 hrtf_id = attrList[attrIdx + 1];
1794 TRACE_ATTR(ALC_HRTF_ID_SOFT, hrtf_id);
1797 attrIdx += 2;
1799 #undef TRACE_ATTR
1801 if(gotFmt != GotAll)
1803 WARN("Missing format for loopback device\n");
1804 return ALC_INVALID_VALUE;
1807 ConfigValueUInt(NULL, NULL, "sends", &numSends);
1808 numSends = minu(MAX_SENDS, numSends);
1810 if((device->Flags&DEVICE_RUNNING))
1811 V0(device->Backend,stop)();
1812 device->Flags &= ~DEVICE_RUNNING;
1814 UpdateClockBase(device);
1816 device->Frequency = freq;
1817 device->FmtChans = schans;
1818 device->FmtType = stype;
1819 device->NumMonoSources = numMono;
1820 device->NumStereoSources = numStereo;
1821 device->NumAuxSends = numSends;
1823 else if(attrList && attrList[0])
1825 ALCuint freq, numMono, numStereo, numSends;
1826 ALCuint attrIdx = 0;
1828 /* If a context is already running on the device, stop playback so the
1829 * device attributes can be updated. */
1830 if((device->Flags&DEVICE_RUNNING))
1831 V0(device->Backend,stop)();
1832 device->Flags &= ~DEVICE_RUNNING;
1834 freq = device->Frequency;
1835 numMono = device->NumMonoSources;
1836 numStereo = device->NumStereoSources;
1837 numSends = device->NumAuxSends;
1839 #define TRACE_ATTR(a, v) TRACE("%s = %d\n", #a, v)
1840 while(attrList[attrIdx])
1842 if(attrList[attrIdx] == ALC_FREQUENCY)
1844 freq = attrList[attrIdx + 1];
1845 device->Flags |= DEVICE_FREQUENCY_REQUEST;
1846 TRACE_ATTR(ALC_FREQUENCY, freq);
1849 if(attrList[attrIdx] == ALC_STEREO_SOURCES)
1851 numStereo = attrList[attrIdx + 1];
1852 TRACE_ATTR(ALC_STEREO_SOURCES, numStereo);
1853 if(numStereo > device->SourcesMax)
1854 numStereo = device->SourcesMax;
1856 numMono = device->SourcesMax - numStereo;
1859 if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS)
1861 numSends = attrList[attrIdx + 1];
1862 TRACE_ATTR(ALC_MAX_AUXILIARY_SENDS, numSends);
1865 if(attrList[attrIdx] == ALC_HRTF_SOFT)
1867 TRACE_ATTR(ALC_HRTF_SOFT, attrList[attrIdx + 1]);
1868 if(attrList[attrIdx + 1] == ALC_FALSE)
1869 hrtf_appreq = Hrtf_Disable;
1870 else if(attrList[attrIdx + 1] == ALC_TRUE)
1871 hrtf_appreq = Hrtf_Enable;
1872 else
1873 hrtf_appreq = Hrtf_Default;
1876 if(attrList[attrIdx] == ALC_HRTF_ID_SOFT)
1878 hrtf_id = attrList[attrIdx + 1];
1879 TRACE_ATTR(ALC_HRTF_ID_SOFT, hrtf_id);
1882 attrIdx += 2;
1884 #undef TRACE_ATTR
1886 ConfigValueUInt(al_string_get_cstr(device->DeviceName), NULL, "frequency", &freq);
1887 freq = maxu(freq, MIN_OUTPUT_RATE);
1889 ConfigValueUInt(al_string_get_cstr(device->DeviceName), NULL, "sends", &numSends);
1890 numSends = minu(MAX_SENDS, numSends);
1892 UpdateClockBase(device);
1894 device->UpdateSize = (ALuint64)device->UpdateSize * freq /
1895 device->Frequency;
1896 /* SSE and Neon do best with the update size being a multiple of 4 */
1897 if((CPUCapFlags&(CPU_CAP_SSE|CPU_CAP_NEON)) != 0)
1898 device->UpdateSize = (device->UpdateSize+3)&~3;
1900 device->Frequency = freq;
1901 device->NumMonoSources = numMono;
1902 device->NumStereoSources = numStereo;
1903 device->NumAuxSends = numSends;
1906 if((device->Flags&DEVICE_RUNNING))
1907 return ALC_NO_ERROR;
1909 al_free(device->Uhj_Encoder);
1910 device->Uhj_Encoder = NULL;
1912 al_free(device->Bs2b);
1913 device->Bs2b = NULL;
1915 al_free(device->Dry.Buffer);
1916 device->Dry.Buffer = NULL;
1917 device->Dry.NumChannels = 0;
1918 device->FOAOut.Buffer = NULL;
1919 device->FOAOut.NumChannels = 0;
1920 device->RealOut.Buffer = NULL;
1921 device->RealOut.NumChannels = 0;
1923 UpdateClockBase(device);
1925 /*************************************************************************
1926 * Update device format request if HRTF is requested
1928 device->Hrtf.Status = ALC_HRTF_DISABLED_SOFT;
1929 if(device->Type != Loopback)
1931 const char *hrtf;
1932 if(ConfigValueStr(al_string_get_cstr(device->DeviceName), NULL, "hrtf", &hrtf))
1934 if(strcasecmp(hrtf, "true") == 0)
1935 hrtf_userreq = Hrtf_Enable;
1936 else if(strcasecmp(hrtf, "false") == 0)
1937 hrtf_userreq = Hrtf_Disable;
1938 else if(strcasecmp(hrtf, "auto") != 0)
1939 ERR("Unexpected hrtf value: %s\n", hrtf);
1942 if(hrtf_userreq == Hrtf_Enable || (hrtf_userreq != Hrtf_Disable && hrtf_appreq == Hrtf_Enable))
1944 if(VECTOR_SIZE(device->Hrtf.List) == 0)
1946 VECTOR_DEINIT(device->Hrtf.List);
1947 device->Hrtf.List = EnumerateHrtf(device->DeviceName);
1949 if(VECTOR_SIZE(device->Hrtf.List) > 0)
1951 device->FmtChans = DevFmtStereo;
1952 if(hrtf_id >= 0 && (size_t)hrtf_id < VECTOR_SIZE(device->Hrtf.List))
1953 device->Frequency = VECTOR_ELEM(device->Hrtf.List, hrtf_id).hrtf->sampleRate;
1954 else
1955 device->Frequency = VECTOR_ELEM(device->Hrtf.List, 0).hrtf->sampleRate;
1956 device->Flags |= DEVICE_CHANNELS_REQUEST | DEVICE_FREQUENCY_REQUEST;
1958 else
1960 hrtf_userreq = Hrtf_Default;
1961 hrtf_appreq = Hrtf_Disable;
1962 device->Hrtf.Status = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1966 else if(hrtf_appreq == Hrtf_Enable)
1968 size_t i = VECTOR_SIZE(device->Hrtf.List);
1969 /* Loopback device. We don't need to match to a specific HRTF entry
1970 * here. If the requested ID matches, we'll pick that later, if not,
1971 * we'll try to auto-select one anyway. Just make sure one exists
1972 * that'll work.
1974 if(device->FmtChans == DevFmtStereo)
1976 if(VECTOR_SIZE(device->Hrtf.List) == 0)
1978 VECTOR_DEINIT(device->Hrtf.List);
1979 device->Hrtf.List = EnumerateHrtf(device->DeviceName);
1981 for(i = 0;i < VECTOR_SIZE(device->Hrtf.List);i++)
1983 const struct Hrtf *hrtf = VECTOR_ELEM(device->Hrtf.List, i).hrtf;
1984 if(hrtf->sampleRate == device->Frequency)
1985 break;
1988 if(i == VECTOR_SIZE(device->Hrtf.List))
1990 ERR("Requested format not HRTF compatible: %s, %uhz\n",
1991 DevFmtChannelsString(device->FmtChans), device->Frequency);
1992 hrtf_appreq = Hrtf_Disable;
1993 device->Hrtf.Status = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
1997 oldFreq = device->Frequency;
1998 oldChans = device->FmtChans;
1999 oldType = device->FmtType;
2001 TRACE("Pre-reset: %s%s, %s%s, %s%uhz, %u update size x%d\n",
2002 (device->Flags&DEVICE_CHANNELS_REQUEST)?"*":"", DevFmtChannelsString(device->FmtChans),
2003 (device->Flags&DEVICE_SAMPLE_TYPE_REQUEST)?"*":"", DevFmtTypeString(device->FmtType),
2004 (device->Flags&DEVICE_FREQUENCY_REQUEST)?"*":"", device->Frequency,
2005 device->UpdateSize, device->NumUpdates
2008 if(V0(device->Backend,reset)() == ALC_FALSE)
2009 return ALC_INVALID_DEVICE;
2011 if(device->FmtChans != oldChans && (device->Flags&DEVICE_CHANNELS_REQUEST))
2013 ERR("Failed to set %s, got %s instead\n", DevFmtChannelsString(oldChans),
2014 DevFmtChannelsString(device->FmtChans));
2015 device->Flags &= ~DEVICE_CHANNELS_REQUEST;
2017 if(device->FmtType != oldType && (device->Flags&DEVICE_SAMPLE_TYPE_REQUEST))
2019 ERR("Failed to set %s, got %s instead\n", DevFmtTypeString(oldType),
2020 DevFmtTypeString(device->FmtType));
2021 device->Flags &= ~DEVICE_SAMPLE_TYPE_REQUEST;
2023 if(device->Frequency != oldFreq && (device->Flags&DEVICE_FREQUENCY_REQUEST))
2025 ERR("Failed to set %uhz, got %uhz instead\n", oldFreq, device->Frequency);
2026 device->Flags &= ~DEVICE_FREQUENCY_REQUEST;
2029 if((device->UpdateSize&3) != 0)
2031 if((CPUCapFlags&CPU_CAP_SSE))
2032 WARN("SSE performs best with multiple of 4 update sizes (%u)\n", device->UpdateSize);
2033 if((CPUCapFlags&CPU_CAP_NEON))
2034 WARN("NEON performs best with multiple of 4 update sizes (%u)\n", device->UpdateSize);
2037 TRACE("Post-reset: %s, %s, %uhz, %u update size x%d\n",
2038 DevFmtChannelsString(device->FmtChans), DevFmtTypeString(device->FmtType),
2039 device->Frequency, device->UpdateSize, device->NumUpdates
2042 aluInitRenderer(device, hrtf_id, hrtf_appreq, hrtf_userreq);
2044 /* Allocate extra channels for any post-filter output. */
2045 size = device->Dry.NumChannels * sizeof(device->Dry.Buffer[0]);
2046 if((device->AmbiDecoder && bformatdec_getOrder(device->AmbiDecoder) >= 2))
2047 size += (ChannelsFromDevFmt(device->FmtChans)+4) * sizeof(device->Dry.Buffer[0]);
2048 else if(device->Hrtf.Handle || device->Uhj_Encoder || device->AmbiDecoder)
2049 size += ChannelsFromDevFmt(device->FmtChans) * sizeof(device->Dry.Buffer[0]);
2050 else if(device->FmtChans > DevFmtAmbi1 && device->FmtChans <= DevFmtAmbi3)
2051 size += 4 * sizeof(device->Dry.Buffer[0]);
2052 device->Dry.Buffer = al_calloc(16, size);
2053 if(!device->Dry.Buffer)
2055 ERR("Failed to allocate "SZFMT" bytes for mix buffer\n", size);
2056 return ALC_INVALID_DEVICE;
2059 if(device->Hrtf.Handle || device->Uhj_Encoder || device->AmbiDecoder)
2061 device->RealOut.Buffer = device->Dry.Buffer + device->Dry.NumChannels;
2062 device->RealOut.NumChannels = ChannelsFromDevFmt(device->FmtChans);
2064 else
2066 device->RealOut.Buffer = device->Dry.Buffer;
2067 device->RealOut.NumChannels = device->Dry.NumChannels;
2070 if((device->AmbiDecoder && bformatdec_getOrder(device->AmbiDecoder) >= 2) ||
2071 (device->FmtChans > DevFmtAmbi1 && device->FmtChans <= DevFmtAmbi3))
2073 /* Higher-order rendering requires upsampling first-order content, so
2074 * make sure to mix it separately.
2076 device->FOAOut.Buffer = device->RealOut.Buffer + device->RealOut.NumChannels;
2077 device->FOAOut.NumChannels = 4;
2079 else
2081 device->FOAOut.Buffer = device->Dry.Buffer;
2082 device->FOAOut.NumChannels = device->Dry.NumChannels;
2085 SetMixerFPUMode(&oldMode);
2086 if(device->DefaultSlot)
2088 ALeffectslot *slot = device->DefaultSlot;
2089 ALeffectState *state = slot->Effect.State;
2091 state->OutBuffer = device->Dry.Buffer;
2092 state->OutChannels = device->Dry.NumChannels;
2093 if(V(state,deviceUpdate)(device) == AL_FALSE)
2095 RestoreFPUMode(&oldMode);
2096 return ALC_INVALID_DEVICE;
2098 UpdateEffectSlotProps(slot);
2101 context = ATOMIC_LOAD(&device->ContextList);
2102 while(context)
2104 ALsizei pos;
2106 ReadLock(&context->PropLock);
2107 LockUIntMapRead(&context->EffectSlotMap);
2108 for(pos = 0;pos < context->EffectSlotMap.size;pos++)
2110 ALeffectslot *slot = context->EffectSlotMap.values[pos];
2111 ALeffectState *state = slot->Effect.State;
2113 state->OutBuffer = device->Dry.Buffer;
2114 state->OutChannels = device->Dry.NumChannels;
2115 if(V(state,deviceUpdate)(device) == AL_FALSE)
2117 UnlockUIntMapRead(&context->EffectSlotMap);
2118 ReadUnlock(&context->PropLock);
2119 RestoreFPUMode(&oldMode);
2120 return ALC_INVALID_DEVICE;
2123 UpdateEffectSlotProps(slot);
2125 UnlockUIntMapRead(&context->EffectSlotMap);
2127 LockUIntMapRead(&context->SourceMap);
2128 for(pos = 0;pos < context->SourceMap.size;pos++)
2130 ALsource *source = context->SourceMap.values[pos];
2131 ALuint s = device->NumAuxSends;
2132 while(s < MAX_SENDS)
2134 if(source->Send[s].Slot)
2135 DecrementRef(&source->Send[s].Slot->ref);
2136 source->Send[s].Slot = NULL;
2137 source->Send[s].Gain = 1.0f;
2138 source->Send[s].GainHF = 1.0f;
2139 source->Send[s].HFReference = LOWPASSFREQREF;
2140 source->Send[s].GainLF = 1.0f;
2141 source->Send[s].LFReference = HIGHPASSFREQREF;
2142 s++;
2145 UnlockUIntMapRead(&context->SourceMap);
2147 UpdateAllSourceProps(context);
2148 ReadUnlock(&context->PropLock);
2150 context = context->next;
2152 RestoreFPUMode(&oldMode);
2154 if(!(device->Flags&DEVICE_PAUSED))
2156 if(V0(device->Backend,start)() == ALC_FALSE)
2157 return ALC_INVALID_DEVICE;
2158 device->Flags |= DEVICE_RUNNING;
2161 return ALC_NO_ERROR;
2164 /* FreeDevice
2166 * Frees the device structure, and destroys any objects the app failed to
2167 * delete. Called once there's no more references on the device.
2169 static ALCvoid FreeDevice(ALCdevice *device)
2171 TRACE("%p\n", device);
2173 V0(device->Backend,close)();
2174 DELETE_OBJ(device->Backend);
2175 device->Backend = NULL;
2177 almtx_destroy(&device->BackendLock);
2179 if(device->DefaultSlot)
2181 DeinitEffectSlot(device->DefaultSlot);
2182 device->DefaultSlot = NULL;
2185 if(device->BufferMap.size > 0)
2187 WARN("(%p) Deleting %d Buffer%s\n", device, device->BufferMap.size,
2188 (device->BufferMap.size==1)?"":"s");
2189 ReleaseALBuffers(device);
2191 ResetUIntMap(&device->BufferMap);
2193 if(device->EffectMap.size > 0)
2195 WARN("(%p) Deleting %d Effect%s\n", device, device->EffectMap.size,
2196 (device->EffectMap.size==1)?"":"s");
2197 ReleaseALEffects(device);
2199 ResetUIntMap(&device->EffectMap);
2201 if(device->FilterMap.size > 0)
2203 WARN("(%p) Deleting %d Filter%s\n", device, device->FilterMap.size,
2204 (device->FilterMap.size==1)?"":"s");
2205 ReleaseALFilters(device);
2207 ResetUIntMap(&device->FilterMap);
2209 AL_STRING_DEINIT(device->Hrtf.Name);
2210 FreeHrtfList(&device->Hrtf.List);
2212 al_free(device->Bs2b);
2213 device->Bs2b = NULL;
2215 al_free(device->Uhj_Encoder);
2216 device->Uhj_Encoder = NULL;
2218 bformatdec_free(device->AmbiDecoder);
2219 device->AmbiDecoder = NULL;
2221 ambiup_free(device->AmbiUp);
2222 device->AmbiUp = NULL;
2224 AL_STRING_DEINIT(device->DeviceName);
2226 al_free(device->Dry.Buffer);
2227 device->Dry.Buffer = NULL;
2228 device->Dry.NumChannels = 0;
2229 device->FOAOut.Buffer = NULL;
2230 device->FOAOut.NumChannels = 0;
2231 device->RealOut.Buffer = NULL;
2232 device->RealOut.NumChannels = 0;
2234 al_free(device);
2238 void ALCdevice_IncRef(ALCdevice *device)
2240 uint ref;
2241 ref = IncrementRef(&device->ref);
2242 TRACEREF("%p increasing refcount to %u\n", device, ref);
2245 void ALCdevice_DecRef(ALCdevice *device)
2247 uint ref;
2248 ref = DecrementRef(&device->ref);
2249 TRACEREF("%p decreasing refcount to %u\n", device, ref);
2250 if(ref == 0) FreeDevice(device);
2253 /* VerifyDevice
2255 * Checks if the device handle is valid, and increments its ref count if so.
2257 static ALCboolean VerifyDevice(ALCdevice **device)
2259 ALCdevice *tmpDevice;
2261 LockLists();
2262 tmpDevice = ATOMIC_LOAD(&DeviceList);
2263 while(tmpDevice)
2265 if(tmpDevice == *device)
2267 ALCdevice_IncRef(tmpDevice);
2268 UnlockLists();
2269 return ALC_TRUE;
2271 tmpDevice = tmpDevice->next;
2273 UnlockLists();
2275 *device = NULL;
2276 return ALC_FALSE;
2280 /* InitContext
2282 * Initializes context fields
2284 static ALvoid InitContext(ALCcontext *Context)
2286 ALlistener *listener = Context->Listener;
2288 //Initialise listener
2289 listener->Gain = 1.0f;
2290 listener->MetersPerUnit = 1.0f;
2291 listener->Position[0] = 0.0f;
2292 listener->Position[1] = 0.0f;
2293 listener->Position[2] = 0.0f;
2294 listener->Velocity[0] = 0.0f;
2295 listener->Velocity[1] = 0.0f;
2296 listener->Velocity[2] = 0.0f;
2297 listener->Forward[0] = 0.0f;
2298 listener->Forward[1] = 0.0f;
2299 listener->Forward[2] = -1.0f;
2300 listener->Up[0] = 0.0f;
2301 listener->Up[1] = 1.0f;
2302 listener->Up[2] = 0.0f;
2304 aluMatrixfSet(&listener->Params.Matrix,
2305 1.0f, 0.0f, 0.0f, 0.0f,
2306 0.0f, 1.0f, 0.0f, 0.0f,
2307 0.0f, 0.0f, 1.0f, 0.0f,
2308 0.0f, 0.0f, 0.0f, 1.0f
2310 aluVectorSet(&listener->Params.Velocity, 0.0f, 0.0f, 0.0f, 0.0f);
2311 listener->Params.Gain = 1.0f;
2312 listener->Params.MetersPerUnit = 1.0f;
2313 listener->Params.DopplerFactor = 1.0f;
2314 listener->Params.SpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
2316 ATOMIC_INIT(&listener->Update, NULL);
2317 ATOMIC_INIT(&listener->FreeList, NULL);
2319 //Validate Context
2320 InitRef(&Context->UpdateCount, 0);
2321 ATOMIC_INIT(&Context->HoldUpdates, AL_FALSE);
2322 Context->GainBoost = 1.0f;
2323 RWLockInit(&Context->PropLock);
2324 ATOMIC_INIT(&Context->LastError, AL_NO_ERROR);
2325 InitUIntMap(&Context->SourceMap, Context->Device->SourcesMax);
2326 InitUIntMap(&Context->EffectSlotMap, Context->Device->AuxiliaryEffectSlotMax);
2328 //Set globals
2329 Context->DistanceModel = DefaultDistanceModel;
2330 Context->SourceDistanceModel = AL_FALSE;
2331 Context->DopplerFactor = 1.0f;
2332 Context->DopplerVelocity = 1.0f;
2333 Context->SpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
2334 ATOMIC_INIT(&Context->DeferUpdates, AL_FALSE);
2336 Context->ExtensionList = alExtList;
2340 /* FreeContext
2342 * Cleans up the context, and destroys any remaining objects the app failed to
2343 * delete. Called once there's no more references on the context.
2345 static void FreeContext(ALCcontext *context)
2347 ALlistener *listener = context->Listener;
2348 struct ALlistenerProps *lprops;
2349 size_t count;
2351 TRACE("%p\n", context);
2353 if(context->SourceMap.size > 0)
2355 WARN("(%p) Deleting %d Source%s\n", context, context->SourceMap.size,
2356 (context->SourceMap.size==1)?"":"s");
2357 ReleaseALSources(context);
2359 ResetUIntMap(&context->SourceMap);
2361 if(context->EffectSlotMap.size > 0)
2363 WARN("(%p) Deleting %d AuxiliaryEffectSlot%s\n", context, context->EffectSlotMap.size,
2364 (context->EffectSlotMap.size==1)?"":"s");
2365 ReleaseALAuxiliaryEffectSlots(context);
2367 ResetUIntMap(&context->EffectSlotMap);
2369 al_free(context->Voices);
2370 context->Voices = NULL;
2371 context->VoiceCount = 0;
2372 context->MaxVoices = 0;
2374 if((lprops=ATOMIC_LOAD(&listener->Update, almemory_order_acquire)) != NULL)
2376 TRACE("Freed unapplied listener update %p\n", lprops);
2377 al_free(lprops);
2379 count = 0;
2380 lprops = ATOMIC_LOAD(&listener->FreeList, almemory_order_consume);
2381 while(lprops)
2383 struct ALlistenerProps *next = ATOMIC_LOAD(&lprops->next, almemory_order_consume);
2384 al_free(lprops);
2385 lprops = next;
2386 ++count;
2388 TRACE("Freed "SZFMT" listener property object%s\n", count, (count==1)?"":"s");
2390 ALCdevice_DecRef(context->Device);
2391 context->Device = NULL;
2393 //Invalidate context
2394 memset(context, 0, sizeof(ALCcontext));
2395 al_free(context);
2398 /* ReleaseContext
2400 * Removes the context reference from the given device and removes it from
2401 * being current on the running thread or globally.
2403 static void ReleaseContext(ALCcontext *context, ALCdevice *device)
2405 ALCcontext *nextctx;
2406 ALCcontext *origctx;
2408 if(altss_get(LocalContext) == context)
2410 WARN("%p released while current on thread\n", context);
2411 altss_set(LocalContext, NULL);
2412 ALCcontext_DecRef(context);
2415 origctx = context;
2416 if(ATOMIC_COMPARE_EXCHANGE_STRONG(ALCcontext*, &GlobalContext, &origctx, NULL))
2417 ALCcontext_DecRef(context);
2419 ALCdevice_Lock(device);
2420 origctx = context;
2421 nextctx = context->next;
2422 if(!ATOMIC_COMPARE_EXCHANGE_STRONG(ALCcontext*, &device->ContextList, &origctx, nextctx))
2424 ALCcontext *list;
2425 do {
2426 list = origctx;
2427 origctx = context;
2428 } while(!COMPARE_EXCHANGE(&list->next, &origctx, nextctx));
2430 ALCdevice_Unlock(device);
2432 ALCcontext_DecRef(context);
2435 void ALCcontext_IncRef(ALCcontext *context)
2437 uint ref = IncrementRef(&context->ref);
2438 TRACEREF("%p increasing refcount to %u\n", context, ref);
2441 void ALCcontext_DecRef(ALCcontext *context)
2443 uint ref = DecrementRef(&context->ref);
2444 TRACEREF("%p decreasing refcount to %u\n", context, ref);
2445 if(ref == 0) FreeContext(context);
2448 static void ReleaseThreadCtx(void *ptr)
2450 ALCcontext *context = ptr;
2451 uint ref = DecrementRef(&context->ref);
2452 TRACEREF("%p decreasing refcount to %u\n", context, ref);
2453 ERR("Context %p current for thread being destroyed, possible leak!\n", context);
2456 /* VerifyContext
2458 * Checks that the given context is valid, and increments its reference count.
2460 static ALCboolean VerifyContext(ALCcontext **context)
2462 ALCdevice *dev;
2464 LockLists();
2465 dev = ATOMIC_LOAD(&DeviceList);
2466 while(dev)
2468 ALCcontext *ctx = ATOMIC_LOAD(&dev->ContextList);
2469 while(ctx)
2471 if(ctx == *context)
2473 ALCcontext_IncRef(ctx);
2474 UnlockLists();
2475 return ALC_TRUE;
2477 ctx = ctx->next;
2479 dev = dev->next;
2481 UnlockLists();
2483 *context = NULL;
2484 return ALC_FALSE;
2488 /* GetContextRef
2490 * Returns the currently active context for this thread, and adds a reference
2491 * without locking it.
2493 ALCcontext *GetContextRef(void)
2495 ALCcontext *context;
2497 context = altss_get(LocalContext);
2498 if(context)
2499 ALCcontext_IncRef(context);
2500 else
2502 LockLists();
2503 context = ATOMIC_LOAD(&GlobalContext);
2504 if(context)
2505 ALCcontext_IncRef(context);
2506 UnlockLists();
2509 return context;
2513 /************************************************
2514 * Standard ALC functions
2515 ************************************************/
2517 /* alcGetError
2519 * Return last ALC generated error code for the given device
2521 ALC_API ALCenum ALC_APIENTRY alcGetError(ALCdevice *device)
2523 ALCenum errorCode;
2525 if(VerifyDevice(&device))
2527 errorCode = ATOMIC_EXCHANGE(ALCenum, &device->LastError, ALC_NO_ERROR);
2528 ALCdevice_DecRef(device);
2530 else
2531 errorCode = ATOMIC_EXCHANGE(ALCenum, &LastNullDeviceError, ALC_NO_ERROR);
2533 return errorCode;
2537 /* alcSuspendContext
2539 * Suspends updates for the given context
2541 ALC_API ALCvoid ALC_APIENTRY alcSuspendContext(ALCcontext *context)
2543 if(!SuspendDefers)
2544 return;
2546 if(!VerifyContext(&context))
2547 alcSetError(NULL, ALC_INVALID_CONTEXT);
2548 else
2550 ALCcontext_DeferUpdates(context, DeferAllowPlay);
2551 ALCcontext_DecRef(context);
2555 /* alcProcessContext
2557 * Resumes processing updates for the given context
2559 ALC_API ALCvoid ALC_APIENTRY alcProcessContext(ALCcontext *context)
2561 if(!SuspendDefers)
2562 return;
2564 if(!VerifyContext(&context))
2565 alcSetError(NULL, ALC_INVALID_CONTEXT);
2566 else
2568 ALCcontext_ProcessUpdates(context);
2569 ALCcontext_DecRef(context);
2574 /* alcGetString
2576 * Returns information about the device, and error strings
2578 ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *Device, ALCenum param)
2580 const ALCchar *value = NULL;
2582 switch(param)
2584 case ALC_NO_ERROR:
2585 value = alcNoError;
2586 break;
2588 case ALC_INVALID_ENUM:
2589 value = alcErrInvalidEnum;
2590 break;
2592 case ALC_INVALID_VALUE:
2593 value = alcErrInvalidValue;
2594 break;
2596 case ALC_INVALID_DEVICE:
2597 value = alcErrInvalidDevice;
2598 break;
2600 case ALC_INVALID_CONTEXT:
2601 value = alcErrInvalidContext;
2602 break;
2604 case ALC_OUT_OF_MEMORY:
2605 value = alcErrOutOfMemory;
2606 break;
2608 case ALC_DEVICE_SPECIFIER:
2609 value = alcDefaultName;
2610 break;
2612 case ALC_ALL_DEVICES_SPECIFIER:
2613 if(VerifyDevice(&Device))
2615 value = al_string_get_cstr(Device->DeviceName);
2616 ALCdevice_DecRef(Device);
2618 else
2620 ProbeAllDevicesList();
2621 value = al_string_get_cstr(alcAllDevicesList);
2623 break;
2625 case ALC_CAPTURE_DEVICE_SPECIFIER:
2626 if(VerifyDevice(&Device))
2628 value = al_string_get_cstr(Device->DeviceName);
2629 ALCdevice_DecRef(Device);
2631 else
2633 ProbeCaptureDeviceList();
2634 value = al_string_get_cstr(alcCaptureDeviceList);
2636 break;
2638 /* Default devices are always first in the list */
2639 case ALC_DEFAULT_DEVICE_SPECIFIER:
2640 value = alcDefaultName;
2641 break;
2643 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
2644 if(al_string_empty(alcAllDevicesList))
2645 ProbeAllDevicesList();
2647 VerifyDevice(&Device);
2649 free(alcDefaultAllDevicesSpecifier);
2650 alcDefaultAllDevicesSpecifier = strdup(al_string_get_cstr(alcAllDevicesList));
2651 if(!alcDefaultAllDevicesSpecifier)
2652 alcSetError(Device, ALC_OUT_OF_MEMORY);
2654 value = alcDefaultAllDevicesSpecifier;
2655 if(Device) ALCdevice_DecRef(Device);
2656 break;
2658 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
2659 if(al_string_empty(alcCaptureDeviceList))
2660 ProbeCaptureDeviceList();
2662 VerifyDevice(&Device);
2664 free(alcCaptureDefaultDeviceSpecifier);
2665 alcCaptureDefaultDeviceSpecifier = strdup(al_string_get_cstr(alcCaptureDeviceList));
2666 if(!alcCaptureDefaultDeviceSpecifier)
2667 alcSetError(Device, ALC_OUT_OF_MEMORY);
2669 value = alcCaptureDefaultDeviceSpecifier;
2670 if(Device) ALCdevice_DecRef(Device);
2671 break;
2673 case ALC_EXTENSIONS:
2674 if(!VerifyDevice(&Device))
2675 value = alcNoDeviceExtList;
2676 else
2678 value = alcExtensionList;
2679 ALCdevice_DecRef(Device);
2681 break;
2683 case ALC_HRTF_SPECIFIER_SOFT:
2684 if(!VerifyDevice(&Device))
2685 alcSetError(NULL, ALC_INVALID_DEVICE);
2686 else
2688 almtx_lock(&Device->BackendLock);
2689 value = (Device->Hrtf.Handle ? al_string_get_cstr(Device->Hrtf.Name) : "");
2690 almtx_unlock(&Device->BackendLock);
2691 ALCdevice_DecRef(Device);
2693 break;
2695 default:
2696 VerifyDevice(&Device);
2697 alcSetError(Device, ALC_INVALID_ENUM);
2698 if(Device) ALCdevice_DecRef(Device);
2699 break;
2702 return value;
2706 static ALCsizei GetIntegerv(ALCdevice *device, ALCenum param, ALCsizei size, ALCint *values)
2708 ALCsizei i;
2710 if(size <= 0 || values == NULL)
2712 alcSetError(device, ALC_INVALID_VALUE);
2713 return 0;
2716 if(!device)
2718 switch(param)
2720 case ALC_MAJOR_VERSION:
2721 values[0] = alcMajorVersion;
2722 return 1;
2723 case ALC_MINOR_VERSION:
2724 values[0] = alcMinorVersion;
2725 return 1;
2727 case ALC_ATTRIBUTES_SIZE:
2728 case ALC_ALL_ATTRIBUTES:
2729 case ALC_FREQUENCY:
2730 case ALC_REFRESH:
2731 case ALC_SYNC:
2732 case ALC_MONO_SOURCES:
2733 case ALC_STEREO_SOURCES:
2734 case ALC_CAPTURE_SAMPLES:
2735 case ALC_FORMAT_CHANNELS_SOFT:
2736 case ALC_FORMAT_TYPE_SOFT:
2737 alcSetError(NULL, ALC_INVALID_DEVICE);
2738 return 0;
2740 default:
2741 alcSetError(NULL, ALC_INVALID_ENUM);
2742 return 0;
2744 return 0;
2747 if(device->Type == Capture)
2749 switch(param)
2751 case ALC_CAPTURE_SAMPLES:
2752 almtx_lock(&device->BackendLock);
2753 values[0] = V0(device->Backend,availableSamples)();
2754 almtx_unlock(&device->BackendLock);
2755 return 1;
2757 case ALC_CONNECTED:
2758 values[0] = device->Connected;
2759 return 1;
2761 default:
2762 alcSetError(device, ALC_INVALID_ENUM);
2763 return 0;
2765 return 0;
2768 /* render device */
2769 switch(param)
2771 case ALC_MAJOR_VERSION:
2772 values[0] = alcMajorVersion;
2773 return 1;
2775 case ALC_MINOR_VERSION:
2776 values[0] = alcMinorVersion;
2777 return 1;
2779 case ALC_EFX_MAJOR_VERSION:
2780 values[0] = alcEFXMajorVersion;
2781 return 1;
2783 case ALC_EFX_MINOR_VERSION:
2784 values[0] = alcEFXMinorVersion;
2785 return 1;
2787 case ALC_ATTRIBUTES_SIZE:
2788 values[0] = 17;
2789 return 1;
2791 case ALC_ALL_ATTRIBUTES:
2792 if(size < 17)
2794 alcSetError(device, ALC_INVALID_VALUE);
2795 return 0;
2798 i = 0;
2799 almtx_lock(&device->BackendLock);
2800 values[i++] = ALC_FREQUENCY;
2801 values[i++] = device->Frequency;
2803 if(device->Type != Loopback)
2805 values[i++] = ALC_REFRESH;
2806 values[i++] = device->Frequency / device->UpdateSize;
2808 values[i++] = ALC_SYNC;
2809 values[i++] = ALC_FALSE;
2811 else
2813 values[i++] = ALC_FORMAT_CHANNELS_SOFT;
2814 values[i++] = device->FmtChans;
2816 values[i++] = ALC_FORMAT_TYPE_SOFT;
2817 values[i++] = device->FmtType;
2820 values[i++] = ALC_MONO_SOURCES;
2821 values[i++] = device->NumMonoSources;
2823 values[i++] = ALC_STEREO_SOURCES;
2824 values[i++] = device->NumStereoSources;
2826 values[i++] = ALC_MAX_AUXILIARY_SENDS;
2827 values[i++] = device->NumAuxSends;
2829 values[i++] = ALC_HRTF_SOFT;
2830 values[i++] = (device->Hrtf.Handle ? ALC_TRUE : ALC_FALSE);
2832 values[i++] = ALC_HRTF_STATUS_SOFT;
2833 values[i++] = device->Hrtf.Status;
2834 almtx_unlock(&device->BackendLock);
2836 values[i++] = 0;
2837 return i;
2839 case ALC_FREQUENCY:
2840 values[0] = device->Frequency;
2841 return 1;
2843 case ALC_REFRESH:
2844 if(device->Type == Loopback)
2846 alcSetError(device, ALC_INVALID_DEVICE);
2847 return 0;
2849 almtx_lock(&device->BackendLock);
2850 values[0] = device->Frequency / device->UpdateSize;
2851 almtx_unlock(&device->BackendLock);
2852 return 1;
2854 case ALC_SYNC:
2855 if(device->Type == Loopback)
2857 alcSetError(device, ALC_INVALID_DEVICE);
2858 return 0;
2860 values[0] = ALC_FALSE;
2861 return 1;
2863 case ALC_FORMAT_CHANNELS_SOFT:
2864 if(device->Type != Loopback)
2866 alcSetError(device, ALC_INVALID_DEVICE);
2867 return 0;
2869 values[0] = device->FmtChans;
2870 return 1;
2872 case ALC_FORMAT_TYPE_SOFT:
2873 if(device->Type != Loopback)
2875 alcSetError(device, ALC_INVALID_DEVICE);
2876 return 0;
2878 values[0] = device->FmtType;
2879 return 1;
2881 case ALC_MONO_SOURCES:
2882 values[0] = device->NumMonoSources;
2883 return 1;
2885 case ALC_STEREO_SOURCES:
2886 values[0] = device->NumStereoSources;
2887 return 1;
2889 case ALC_MAX_AUXILIARY_SENDS:
2890 values[0] = device->NumAuxSends;
2891 return 1;
2893 case ALC_CONNECTED:
2894 values[0] = device->Connected;
2895 return 1;
2897 case ALC_HRTF_SOFT:
2898 values[0] = (device->Hrtf.Handle ? ALC_TRUE : ALC_FALSE);
2899 return 1;
2901 case ALC_HRTF_STATUS_SOFT:
2902 values[0] = device->Hrtf.Status;
2903 return 1;
2905 case ALC_NUM_HRTF_SPECIFIERS_SOFT:
2906 almtx_lock(&device->BackendLock);
2907 FreeHrtfList(&device->Hrtf.List);
2908 device->Hrtf.List = EnumerateHrtf(device->DeviceName);
2909 values[0] = (ALCint)VECTOR_SIZE(device->Hrtf.List);
2910 almtx_unlock(&device->BackendLock);
2911 return 1;
2913 default:
2914 alcSetError(device, ALC_INVALID_ENUM);
2915 return 0;
2917 return 0;
2920 /* alcGetIntegerv
2922 * Returns information about the device and the version of OpenAL
2924 ALC_API void ALC_APIENTRY alcGetIntegerv(ALCdevice *device, ALCenum param, ALCsizei size, ALCint *values)
2926 VerifyDevice(&device);
2927 if(size <= 0 || values == NULL)
2928 alcSetError(device, ALC_INVALID_VALUE);
2929 else
2930 GetIntegerv(device, param, size, values);
2931 if(device) ALCdevice_DecRef(device);
2934 ALC_API void ALC_APIENTRY alcGetInteger64vSOFT(ALCdevice *device, ALCenum pname, ALCsizei size, ALCint64SOFT *values)
2936 ALCint *ivals;
2937 ALsizei i;
2939 VerifyDevice(&device);
2940 if(size <= 0 || values == NULL)
2941 alcSetError(device, ALC_INVALID_VALUE);
2942 else if(!device || device->Type == Capture)
2944 ivals = malloc(size * sizeof(ALCint));
2945 size = GetIntegerv(device, pname, size, ivals);
2946 for(i = 0;i < size;i++)
2947 values[i] = ivals[i];
2948 free(ivals);
2950 else /* render device */
2952 ClockLatency clock;
2953 ALuint64 basecount;
2954 ALuint samplecount;
2955 ALuint refcount;
2957 switch(pname)
2959 case ALC_ATTRIBUTES_SIZE:
2960 *values = 21;
2961 break;
2963 case ALC_ALL_ATTRIBUTES:
2964 if(size < 21)
2965 alcSetError(device, ALC_INVALID_VALUE);
2966 else
2968 i = 0;
2969 almtx_lock(&device->BackendLock);
2970 values[i++] = ALC_FREQUENCY;
2971 values[i++] = device->Frequency;
2973 if(device->Type != Loopback)
2975 values[i++] = ALC_REFRESH;
2976 values[i++] = device->Frequency / device->UpdateSize;
2978 values[i++] = ALC_SYNC;
2979 values[i++] = ALC_FALSE;
2981 else
2983 values[i++] = ALC_FORMAT_CHANNELS_SOFT;
2984 values[i++] = device->FmtChans;
2986 values[i++] = ALC_FORMAT_TYPE_SOFT;
2987 values[i++] = device->FmtType;
2990 values[i++] = ALC_MONO_SOURCES;
2991 values[i++] = device->NumMonoSources;
2993 values[i++] = ALC_STEREO_SOURCES;
2994 values[i++] = device->NumStereoSources;
2996 values[i++] = ALC_MAX_AUXILIARY_SENDS;
2997 values[i++] = device->NumAuxSends;
2999 values[i++] = ALC_HRTF_SOFT;
3000 values[i++] = (device->Hrtf.Handle ? ALC_TRUE : ALC_FALSE);
3002 values[i++] = ALC_HRTF_STATUS_SOFT;
3003 values[i++] = device->Hrtf.Status;
3005 clock = V0(device->Backend,getClockLatency)();
3006 values[i++] = ALC_DEVICE_CLOCK_SOFT;
3007 values[i++] = clock.ClockTime;
3009 values[i++] = ALC_DEVICE_LATENCY_SOFT;
3010 values[i++] = clock.Latency;
3011 almtx_unlock(&device->BackendLock);
3013 values[i++] = 0;
3015 break;
3017 case ALC_DEVICE_CLOCK_SOFT:
3018 almtx_lock(&device->BackendLock);
3019 do {
3020 while(((refcount=ReadRef(&device->MixCount))&1) != 0)
3021 althrd_yield();
3022 basecount = device->ClockBase;
3023 samplecount = device->SamplesDone;
3024 } while(refcount != ReadRef(&device->MixCount));
3025 *values = basecount + (samplecount*DEVICE_CLOCK_RES/device->Frequency);
3026 almtx_unlock(&device->BackendLock);
3027 break;
3029 case ALC_DEVICE_LATENCY_SOFT:
3031 almtx_lock(&device->BackendLock);
3032 clock = V0(device->Backend,getClockLatency)();
3033 almtx_unlock(&device->BackendLock);
3034 *values = clock.Latency;
3036 break;
3038 case ALC_DEVICE_CLOCK_LATENCY_SOFT:
3039 if(size < 2)
3040 alcSetError(device, ALC_INVALID_VALUE);
3041 else
3043 ClockLatency clock;
3044 almtx_lock(&device->BackendLock);
3045 clock = V0(device->Backend,getClockLatency)();
3046 almtx_unlock(&device->BackendLock);
3047 values[0] = clock.ClockTime;
3048 values[1] = clock.Latency;
3050 break;
3052 default:
3053 ivals = malloc(size * sizeof(ALCint));
3054 size = GetIntegerv(device, pname, size, ivals);
3055 for(i = 0;i < size;i++)
3056 values[i] = ivals[i];
3057 free(ivals);
3058 break;
3061 if(device)
3062 ALCdevice_DecRef(device);
3066 /* alcIsExtensionPresent
3068 * Determines if there is support for a particular extension
3070 ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
3072 ALCboolean bResult = ALC_FALSE;
3074 VerifyDevice(&device);
3076 if(!extName)
3077 alcSetError(device, ALC_INVALID_VALUE);
3078 else
3080 size_t len = strlen(extName);
3081 const char *ptr = (device ? alcExtensionList : alcNoDeviceExtList);
3082 while(ptr && *ptr)
3084 if(strncasecmp(ptr, extName, len) == 0 &&
3085 (ptr[len] == '\0' || isspace(ptr[len])))
3087 bResult = ALC_TRUE;
3088 break;
3090 if((ptr=strchr(ptr, ' ')) != NULL)
3092 do {
3093 ++ptr;
3094 } while(isspace(*ptr));
3098 if(device)
3099 ALCdevice_DecRef(device);
3100 return bResult;
3104 /* alcGetProcAddress
3106 * Retrieves the function address for a particular extension function
3108 ALC_API ALCvoid* ALC_APIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
3110 ALCvoid *ptr = NULL;
3112 if(!funcName)
3114 VerifyDevice(&device);
3115 alcSetError(device, ALC_INVALID_VALUE);
3116 if(device) ALCdevice_DecRef(device);
3118 else
3120 ALsizei i = 0;
3121 while(alcFunctions[i].funcName && strcmp(alcFunctions[i].funcName, funcName) != 0)
3122 i++;
3123 ptr = alcFunctions[i].address;
3126 return ptr;
3130 /* alcGetEnumValue
3132 * Get the value for a particular ALC enumeration name
3134 ALC_API ALCenum ALC_APIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
3136 ALCenum val = 0;
3138 if(!enumName)
3140 VerifyDevice(&device);
3141 alcSetError(device, ALC_INVALID_VALUE);
3142 if(device) ALCdevice_DecRef(device);
3144 else
3146 ALsizei i = 0;
3147 while(enumeration[i].enumName && strcmp(enumeration[i].enumName, enumName) != 0)
3148 i++;
3149 val = enumeration[i].value;
3152 return val;
3156 /* alcCreateContext
3158 * Create and attach a context to the given device.
3160 ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
3162 ALCcontext *ALContext;
3163 ALfloat valf;
3164 ALCenum err;
3166 LockLists();
3167 if(!VerifyDevice(&device) || device->Type == Capture || !device->Connected)
3169 UnlockLists();
3170 alcSetError(device, ALC_INVALID_DEVICE);
3171 if(device) ALCdevice_DecRef(device);
3172 return NULL;
3174 almtx_lock(&device->BackendLock);
3175 UnlockLists();
3177 ATOMIC_STORE(&device->LastError, ALC_NO_ERROR);
3179 ALContext = al_calloc(16, sizeof(ALCcontext)+sizeof(ALlistener));
3180 if(ALContext)
3182 InitRef(&ALContext->ref, 1);
3183 ALContext->Listener = (ALlistener*)ALContext->_listener_mem;
3185 ATOMIC_INIT(&ALContext->ActiveAuxSlotList, NULL);
3187 ALContext->VoiceCount = 0;
3188 ALContext->MaxVoices = 256;
3189 ALContext->Voices = al_calloc(16, ALContext->MaxVoices * sizeof(ALContext->Voices[0]));
3191 if(!ALContext || !ALContext->Voices)
3193 almtx_unlock(&device->BackendLock);
3195 if(ALContext)
3197 al_free(ALContext->Voices);
3198 ALContext->Voices = NULL;
3200 al_free(ALContext);
3201 ALContext = NULL;
3204 alcSetError(device, ALC_OUT_OF_MEMORY);
3205 ALCdevice_DecRef(device);
3206 return NULL;
3209 if((err=UpdateDeviceParams(device, attrList)) != ALC_NO_ERROR)
3211 almtx_unlock(&device->BackendLock);
3213 al_free(ALContext->Voices);
3214 ALContext->Voices = NULL;
3216 al_free(ALContext);
3217 ALContext = NULL;
3219 alcSetError(device, err);
3220 if(err == ALC_INVALID_DEVICE)
3222 V0(device->Backend,lock)();
3223 aluHandleDisconnect(device);
3224 V0(device->Backend,unlock)();
3226 ALCdevice_DecRef(device);
3227 return NULL;
3230 ALContext->Device = device;
3231 ALCdevice_IncRef(device);
3232 InitContext(ALContext);
3234 if(ConfigValueFloat(al_string_get_cstr(device->DeviceName), NULL, "volume-adjust", &valf))
3236 if(!isfinite(valf))
3237 ERR("volume-adjust must be finite: %f\n", valf);
3238 else
3240 ALfloat db = clampf(valf, -24.0f, 24.0f);
3241 if(db != valf)
3242 WARN("volume-adjust clamped: %f, range: +/-%f\n", valf, 24.0f);
3243 ALContext->GainBoost = powf(10.0f, db/20.0f);
3244 TRACE("volume-adjust gain: %f\n", ALContext->GainBoost);
3247 UpdateListenerProps(ALContext);
3250 ALCcontext *head = ATOMIC_LOAD(&device->ContextList);
3251 do {
3252 ALContext->next = head;
3253 } while(!ATOMIC_COMPARE_EXCHANGE_WEAK(ALCcontext*, &device->ContextList, &head, ALContext));
3255 almtx_unlock(&device->BackendLock);
3257 ALCdevice_DecRef(device);
3259 TRACE("Created context %p\n", ALContext);
3260 return ALContext;
3263 /* alcDestroyContext
3265 * Remove a context from its device
3267 ALC_API ALCvoid ALC_APIENTRY alcDestroyContext(ALCcontext *context)
3269 ALCdevice *Device;
3271 LockLists();
3272 /* alcGetContextsDevice sets an error for invalid contexts */
3273 Device = alcGetContextsDevice(context);
3274 if(Device)
3276 almtx_lock(&Device->BackendLock);
3277 ReleaseContext(context, Device);
3278 if(!ATOMIC_LOAD(&Device->ContextList))
3280 V0(Device->Backend,stop)();
3281 Device->Flags &= ~DEVICE_RUNNING;
3283 almtx_unlock(&Device->BackendLock);
3285 UnlockLists();
3289 /* alcGetCurrentContext
3291 * Returns the currently active context on the calling thread
3293 ALC_API ALCcontext* ALC_APIENTRY alcGetCurrentContext(void)
3295 ALCcontext *Context = altss_get(LocalContext);
3296 if(!Context) Context = ATOMIC_LOAD(&GlobalContext);
3297 return Context;
3300 /* alcGetThreadContext
3302 * Returns the currently active thread-local context
3304 ALC_API ALCcontext* ALC_APIENTRY alcGetThreadContext(void)
3306 return altss_get(LocalContext);
3310 /* alcMakeContextCurrent
3312 * Makes the given context the active process-wide context, and removes the
3313 * thread-local context for the calling thread.
3315 ALC_API ALCboolean ALC_APIENTRY alcMakeContextCurrent(ALCcontext *context)
3317 /* context must be valid or NULL */
3318 if(context && !VerifyContext(&context))
3320 alcSetError(NULL, ALC_INVALID_CONTEXT);
3321 return ALC_FALSE;
3323 /* context's reference count is already incremented */
3324 context = ATOMIC_EXCHANGE(ALCcontext*, &GlobalContext, context);
3325 if(context) ALCcontext_DecRef(context);
3327 if((context=altss_get(LocalContext)) != NULL)
3329 altss_set(LocalContext, NULL);
3330 ALCcontext_DecRef(context);
3333 return ALC_TRUE;
3336 /* alcSetThreadContext
3338 * Makes the given context the active context for the current thread
3340 ALC_API ALCboolean ALC_APIENTRY alcSetThreadContext(ALCcontext *context)
3342 ALCcontext *old;
3344 /* context must be valid or NULL */
3345 if(context && !VerifyContext(&context))
3347 alcSetError(NULL, ALC_INVALID_CONTEXT);
3348 return ALC_FALSE;
3350 /* context's reference count is already incremented */
3351 old = altss_get(LocalContext);
3352 altss_set(LocalContext, context);
3353 if(old) ALCcontext_DecRef(old);
3355 return ALC_TRUE;
3359 /* alcGetContextsDevice
3361 * Returns the device that a particular context is attached to
3363 ALC_API ALCdevice* ALC_APIENTRY alcGetContextsDevice(ALCcontext *Context)
3365 ALCdevice *Device;
3367 if(!VerifyContext(&Context))
3369 alcSetError(NULL, ALC_INVALID_CONTEXT);
3370 return NULL;
3372 Device = Context->Device;
3373 ALCcontext_DecRef(Context);
3375 return Device;
3379 /* alcOpenDevice
3381 * Opens the named device.
3383 ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName)
3385 const ALCchar *fmt;
3386 ALCdevice *device;
3387 ALCenum err;
3389 DO_INITCONFIG();
3391 if(!PlaybackBackend.name)
3393 alcSetError(NULL, ALC_INVALID_VALUE);
3394 return NULL;
3397 if(deviceName && (!deviceName[0] || strcasecmp(deviceName, alcDefaultName) == 0 || strcasecmp(deviceName, "openal-soft") == 0
3398 #ifdef _WIN32
3399 /* Some old Windows apps hardcode these expecting OpenAL to use a
3400 * specific audio API, even when they're not enumerated. Creative's
3401 * router effectively ignores them too.
3403 || strcasecmp(deviceName, "DirectSound3D") == 0 || strcasecmp(deviceName, "DirectSound") == 0
3404 || strcasecmp(deviceName, "MMSYSTEM") == 0
3405 #endif
3407 deviceName = NULL;
3409 device = al_calloc(16, sizeof(ALCdevice)+sizeof(ALeffectslot));
3410 if(!device)
3412 alcSetError(NULL, ALC_OUT_OF_MEMORY);
3413 return NULL;
3416 //Validate device
3417 InitRef(&device->ref, 1);
3418 device->Connected = ALC_TRUE;
3419 device->Type = Playback;
3420 ATOMIC_INIT(&device->LastError, ALC_NO_ERROR);
3422 device->Flags = 0;
3423 device->Bs2b = NULL;
3424 device->Uhj_Encoder = NULL;
3425 VECTOR_INIT(device->Hrtf.List);
3426 AL_STRING_INIT(device->Hrtf.Name);
3427 device->Render_Mode = NormalRender;
3428 AL_STRING_INIT(device->DeviceName);
3429 device->Dry.Buffer = NULL;
3430 device->Dry.NumChannels = 0;
3431 device->FOAOut.Buffer = NULL;
3432 device->FOAOut.NumChannels = 0;
3433 device->RealOut.Buffer = NULL;
3434 device->RealOut.NumChannels = 0;
3436 ATOMIC_INIT(&device->ContextList, NULL);
3438 device->ClockBase = 0;
3439 device->SamplesDone = 0;
3441 device->SourcesMax = 256;
3442 device->AuxiliaryEffectSlotMax = 4;
3443 device->NumAuxSends = MAX_SENDS;
3445 InitUIntMap(&device->BufferMap, ~0);
3446 InitUIntMap(&device->EffectMap, ~0);
3447 InitUIntMap(&device->FilterMap, ~0);
3449 //Set output format
3450 device->FmtChans = DevFmtChannelsDefault;
3451 device->FmtType = DevFmtTypeDefault;
3452 device->Frequency = DEFAULT_OUTPUT_RATE;
3453 device->IsHeadphones = AL_FALSE;
3454 device->AmbiFmt = AmbiFormat_Default;
3455 device->NumUpdates = 4;
3456 device->UpdateSize = 1024;
3458 if(!PlaybackBackend.getFactory)
3459 device->Backend = create_backend_wrapper(device, &PlaybackBackend.Funcs,
3460 ALCbackend_Playback);
3461 else
3463 ALCbackendFactory *factory = PlaybackBackend.getFactory();
3464 device->Backend = V(factory,createBackend)(device, ALCbackend_Playback);
3466 if(!device->Backend)
3468 al_free(device);
3469 alcSetError(NULL, ALC_OUT_OF_MEMORY);
3470 return NULL;
3474 if(ConfigValueStr(deviceName, NULL, "channels", &fmt))
3476 static const struct {
3477 const char name[16];
3478 enum DevFmtChannels chans;
3479 } chanlist[] = {
3480 { "mono", DevFmtMono },
3481 { "stereo", DevFmtStereo },
3482 { "quad", DevFmtQuad },
3483 { "surround51", DevFmtX51 },
3484 { "surround61", DevFmtX61 },
3485 { "surround71", DevFmtX71 },
3486 { "surround51rear", DevFmtX51Rear },
3487 { "ambi1", DevFmtAmbi1 },
3488 { "ambi2", DevFmtAmbi2 },
3489 { "ambi3", DevFmtAmbi3 },
3491 size_t i;
3493 for(i = 0;i < COUNTOF(chanlist);i++)
3495 if(strcasecmp(chanlist[i].name, fmt) == 0)
3497 device->FmtChans = chanlist[i].chans;
3498 device->Flags |= DEVICE_CHANNELS_REQUEST;
3499 break;
3502 if(i == COUNTOF(chanlist))
3503 ERR("Unsupported channels: %s\n", fmt);
3505 if(ConfigValueStr(deviceName, NULL, "sample-type", &fmt))
3507 static const struct {
3508 const char name[16];
3509 enum DevFmtType type;
3510 } typelist[] = {
3511 { "int8", DevFmtByte },
3512 { "uint8", DevFmtUByte },
3513 { "int16", DevFmtShort },
3514 { "uint16", DevFmtUShort },
3515 { "int32", DevFmtInt },
3516 { "uint32", DevFmtUInt },
3517 { "float32", DevFmtFloat },
3519 size_t i;
3521 for(i = 0;i < COUNTOF(typelist);i++)
3523 if(strcasecmp(typelist[i].name, fmt) == 0)
3525 device->FmtType = typelist[i].type;
3526 device->Flags |= DEVICE_SAMPLE_TYPE_REQUEST;
3527 break;
3530 if(i == COUNTOF(typelist))
3531 ERR("Unsupported sample-type: %s\n", fmt);
3534 if(ConfigValueUInt(deviceName, NULL, "frequency", &device->Frequency))
3536 device->Flags |= DEVICE_FREQUENCY_REQUEST;
3537 if(device->Frequency < MIN_OUTPUT_RATE)
3538 ERR("%uhz request clamped to %uhz minimum\n", device->Frequency, MIN_OUTPUT_RATE);
3539 device->Frequency = maxu(device->Frequency, MIN_OUTPUT_RATE);
3542 ConfigValueUInt(deviceName, NULL, "periods", &device->NumUpdates);
3543 device->NumUpdates = clampu(device->NumUpdates, 2, 16);
3545 ConfigValueUInt(deviceName, NULL, "period_size", &device->UpdateSize);
3546 device->UpdateSize = clampu(device->UpdateSize, 64, 8192);
3547 if((CPUCapFlags&(CPU_CAP_SSE|CPU_CAP_NEON)) != 0)
3548 device->UpdateSize = (device->UpdateSize+3)&~3;
3550 ConfigValueUInt(deviceName, NULL, "sources", &device->SourcesMax);
3551 if(device->SourcesMax == 0) device->SourcesMax = 256;
3553 ConfigValueUInt(deviceName, NULL, "slots", &device->AuxiliaryEffectSlotMax);
3554 if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
3556 ConfigValueUInt(deviceName, NULL, "sends", &device->NumAuxSends);
3557 if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
3559 device->NumStereoSources = 1;
3560 device->NumMonoSources = device->SourcesMax - device->NumStereoSources;
3562 // Find a playback device to open
3563 if((err=V(device->Backend,open)(deviceName)) != ALC_NO_ERROR)
3565 DELETE_OBJ(device->Backend);
3566 al_free(device);
3567 alcSetError(NULL, err);
3568 return NULL;
3570 almtx_init(&device->BackendLock, almtx_plain);
3572 if(ConfigValueStr(al_string_get_cstr(device->DeviceName), NULL, "ambi-format", &fmt))
3574 if(strcasecmp(fmt, "fuma") == 0)
3575 device->AmbiFmt = AmbiFormat_FuMa;
3576 else if(strcasecmp(fmt, "acn+sn3d") == 0)
3577 device->AmbiFmt = AmbiFormat_ACN_SN3D;
3578 else if(strcasecmp(fmt, "acn+n3d") == 0)
3579 device->AmbiFmt = AmbiFormat_ACN_N3D;
3580 else
3581 ERR("Unsupported ambi-format: %s\n", fmt);
3584 if(DefaultEffect.type != AL_EFFECT_NULL)
3586 device->DefaultSlot = (ALeffectslot*)device->_slot_mem;
3587 if(InitEffectSlot(device->DefaultSlot) != AL_NO_ERROR)
3589 device->DefaultSlot = NULL;
3590 ERR("Failed to initialize the default effect slot\n");
3592 else
3594 aluInitEffectPanning(device->DefaultSlot);
3595 if(InitializeEffect(device, device->DefaultSlot, &DefaultEffect) != AL_NO_ERROR)
3597 DeinitEffectSlot(device->DefaultSlot);
3598 device->DefaultSlot = NULL;
3599 ERR("Failed to initialize the default effect\n");
3605 ALCdevice *head = ATOMIC_LOAD(&DeviceList);
3606 do {
3607 device->next = head;
3608 } while(!ATOMIC_COMPARE_EXCHANGE_WEAK(ALCdevice*, &DeviceList, &head, device));
3611 TRACE("Created device %p, \"%s\"\n", device, al_string_get_cstr(device->DeviceName));
3612 return device;
3615 /* alcCloseDevice
3617 * Closes the given device.
3619 ALC_API ALCboolean ALC_APIENTRY alcCloseDevice(ALCdevice *device)
3621 ALCdevice *list, *origdev, *nextdev;
3622 ALCcontext *ctx;
3624 LockLists();
3625 list = ATOMIC_LOAD(&DeviceList);
3626 do {
3627 if(list == device)
3628 break;
3629 } while((list=list->next) != NULL);
3630 if(!list || list->Type == Capture)
3632 alcSetError(list, ALC_INVALID_DEVICE);
3633 UnlockLists();
3634 return ALC_FALSE;
3636 almtx_lock(&device->BackendLock);
3638 origdev = device;
3639 nextdev = device->next;
3640 if(!ATOMIC_COMPARE_EXCHANGE_STRONG(ALCdevice*, &DeviceList, &origdev, nextdev))
3642 do {
3643 list = origdev;
3644 origdev = device;
3645 } while(!COMPARE_EXCHANGE(&list->next, &origdev, nextdev));
3647 UnlockLists();
3649 ctx = ATOMIC_LOAD(&device->ContextList);
3650 while(ctx != NULL)
3652 ALCcontext *next = ctx->next;
3653 WARN("Releasing context %p\n", ctx);
3654 ReleaseContext(ctx, device);
3655 ctx = next;
3657 if((device->Flags&DEVICE_RUNNING))
3658 V0(device->Backend,stop)();
3659 device->Flags &= ~DEVICE_RUNNING;
3660 almtx_unlock(&device->BackendLock);
3662 ALCdevice_DecRef(device);
3664 return ALC_TRUE;
3668 /************************************************
3669 * ALC capture functions
3670 ************************************************/
3671 ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei samples)
3673 ALCdevice *device = NULL;
3674 ALCenum err;
3676 DO_INITCONFIG();
3678 if(!CaptureBackend.name)
3680 alcSetError(NULL, ALC_INVALID_VALUE);
3681 return NULL;
3684 if(samples <= 0)
3686 alcSetError(NULL, ALC_INVALID_VALUE);
3687 return NULL;
3690 if(deviceName && (!deviceName[0] || strcasecmp(deviceName, alcDefaultName) == 0 || strcasecmp(deviceName, "openal-soft") == 0))
3691 deviceName = NULL;
3693 device = al_calloc(16, sizeof(ALCdevice));
3694 if(!device)
3696 alcSetError(NULL, ALC_OUT_OF_MEMORY);
3697 return NULL;
3700 //Validate device
3701 InitRef(&device->ref, 1);
3702 device->Connected = ALC_TRUE;
3703 device->Type = Capture;
3705 VECTOR_INIT(device->Hrtf.List);
3706 AL_STRING_INIT(device->Hrtf.Name);
3708 AL_STRING_INIT(device->DeviceName);
3709 device->Dry.Buffer = NULL;
3710 device->Dry.NumChannels = 0;
3711 device->FOAOut.Buffer = NULL;
3712 device->FOAOut.NumChannels = 0;
3713 device->RealOut.Buffer = NULL;
3714 device->RealOut.NumChannels = 0;
3716 InitUIntMap(&device->BufferMap, ~0);
3717 InitUIntMap(&device->EffectMap, ~0);
3718 InitUIntMap(&device->FilterMap, ~0);
3720 if(!CaptureBackend.getFactory)
3721 device->Backend = create_backend_wrapper(device, &CaptureBackend.Funcs,
3722 ALCbackend_Capture);
3723 else
3725 ALCbackendFactory *factory = CaptureBackend.getFactory();
3726 device->Backend = V(factory,createBackend)(device, ALCbackend_Capture);
3728 if(!device->Backend)
3730 al_free(device);
3731 alcSetError(NULL, ALC_OUT_OF_MEMORY);
3732 return NULL;
3735 device->Flags |= DEVICE_FREQUENCY_REQUEST;
3736 device->Frequency = frequency;
3738 device->Flags |= DEVICE_CHANNELS_REQUEST | DEVICE_SAMPLE_TYPE_REQUEST;
3739 if(DecomposeDevFormat(format, &device->FmtChans, &device->FmtType) == AL_FALSE)
3741 al_free(device);
3742 alcSetError(NULL, ALC_INVALID_ENUM);
3743 return NULL;
3745 device->IsHeadphones = AL_FALSE;
3746 device->AmbiFmt = AmbiFormat_Default;
3748 device->UpdateSize = samples;
3749 device->NumUpdates = 1;
3751 if((err=V(device->Backend,open)(deviceName)) != ALC_NO_ERROR)
3753 al_free(device);
3754 alcSetError(NULL, err);
3755 return NULL;
3757 almtx_init(&device->BackendLock, almtx_plain);
3760 ALCdevice *head = ATOMIC_LOAD(&DeviceList);
3761 do {
3762 device->next = head;
3763 } while(!ATOMIC_COMPARE_EXCHANGE_WEAK(ALCdevice*, &DeviceList, &head, device));
3766 TRACE("Created device %p, \"%s\"\n", device, al_string_get_cstr(device->DeviceName));
3767 return device;
3770 ALC_API ALCboolean ALC_APIENTRY alcCaptureCloseDevice(ALCdevice *device)
3772 ALCdevice *list, *next, *nextdev;
3774 LockLists();
3775 list = ATOMIC_LOAD(&DeviceList);
3776 do {
3777 if(list == device)
3778 break;
3779 } while((list=list->next) != NULL);
3780 if(!list || list->Type != Capture)
3782 alcSetError(list, ALC_INVALID_DEVICE);
3783 UnlockLists();
3784 return ALC_FALSE;
3787 next = device;
3788 nextdev = device->next;
3789 if(!ATOMIC_COMPARE_EXCHANGE_STRONG(ALCdevice*, &DeviceList, &next, nextdev))
3791 do {
3792 list = next;
3793 next = device;
3794 } while(!COMPARE_EXCHANGE(&list->next, &next, nextdev));
3796 UnlockLists();
3798 ALCdevice_DecRef(device);
3800 return ALC_TRUE;
3803 ALC_API void ALC_APIENTRY alcCaptureStart(ALCdevice *device)
3805 if(!VerifyDevice(&device) || device->Type != Capture)
3806 alcSetError(device, ALC_INVALID_DEVICE);
3807 else
3809 almtx_lock(&device->BackendLock);
3810 if(!device->Connected)
3811 alcSetError(device, ALC_INVALID_DEVICE);
3812 else if(!(device->Flags&DEVICE_RUNNING))
3814 if(V0(device->Backend,start)())
3815 device->Flags |= DEVICE_RUNNING;
3816 else
3818 aluHandleDisconnect(device);
3819 alcSetError(device, ALC_INVALID_DEVICE);
3822 almtx_unlock(&device->BackendLock);
3825 if(device) ALCdevice_DecRef(device);
3828 ALC_API void ALC_APIENTRY alcCaptureStop(ALCdevice *device)
3830 if(!VerifyDevice(&device) || device->Type != Capture)
3831 alcSetError(device, ALC_INVALID_DEVICE);
3832 else
3834 almtx_lock(&device->BackendLock);
3835 if((device->Flags&DEVICE_RUNNING))
3836 V0(device->Backend,stop)();
3837 device->Flags &= ~DEVICE_RUNNING;
3838 almtx_unlock(&device->BackendLock);
3841 if(device) ALCdevice_DecRef(device);
3844 ALC_API void ALC_APIENTRY alcCaptureSamples(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
3846 if(!VerifyDevice(&device) || device->Type != Capture)
3847 alcSetError(device, ALC_INVALID_DEVICE);
3848 else
3850 ALCenum err = ALC_INVALID_VALUE;
3852 almtx_lock(&device->BackendLock);
3853 if(samples >= 0 && V0(device->Backend,availableSamples)() >= (ALCuint)samples)
3854 err = V(device->Backend,captureSamples)(buffer, samples);
3855 almtx_unlock(&device->BackendLock);
3857 if(err != ALC_NO_ERROR)
3858 alcSetError(device, err);
3860 if(device) ALCdevice_DecRef(device);
3864 /************************************************
3865 * ALC loopback functions
3866 ************************************************/
3868 /* alcLoopbackOpenDeviceSOFT
3870 * Open a loopback device, for manual rendering.
3872 ALC_API ALCdevice* ALC_APIENTRY alcLoopbackOpenDeviceSOFT(const ALCchar *deviceName)
3874 ALCbackendFactory *factory;
3875 ALCdevice *device;
3877 DO_INITCONFIG();
3879 /* Make sure the device name, if specified, is us. */
3880 if(deviceName && strcmp(deviceName, alcDefaultName) != 0)
3882 alcSetError(NULL, ALC_INVALID_VALUE);
3883 return NULL;
3886 device = al_calloc(16, sizeof(ALCdevice));
3887 if(!device)
3889 alcSetError(NULL, ALC_OUT_OF_MEMORY);
3890 return NULL;
3893 //Validate device
3894 InitRef(&device->ref, 1);
3895 device->Connected = ALC_TRUE;
3896 device->Type = Loopback;
3897 ATOMIC_INIT(&device->LastError, ALC_NO_ERROR);
3899 device->Flags = 0;
3900 VECTOR_INIT(device->Hrtf.List);
3901 AL_STRING_INIT(device->Hrtf.Name);
3902 device->Bs2b = NULL;
3903 device->Uhj_Encoder = NULL;
3904 device->Render_Mode = NormalRender;
3905 AL_STRING_INIT(device->DeviceName);
3906 device->Dry.Buffer = NULL;
3907 device->Dry.NumChannels = 0;
3908 device->FOAOut.Buffer = NULL;
3909 device->FOAOut.NumChannels = 0;
3910 device->RealOut.Buffer = NULL;
3911 device->RealOut.NumChannels = 0;
3913 ATOMIC_INIT(&device->ContextList, NULL);
3915 device->ClockBase = 0;
3916 device->SamplesDone = 0;
3918 device->SourcesMax = 256;
3919 device->AuxiliaryEffectSlotMax = 4;
3920 device->NumAuxSends = MAX_SENDS;
3922 InitUIntMap(&device->BufferMap, ~0);
3923 InitUIntMap(&device->EffectMap, ~0);
3924 InitUIntMap(&device->FilterMap, ~0);
3926 factory = ALCloopbackFactory_getFactory();
3927 device->Backend = V(factory,createBackend)(device, ALCbackend_Loopback);
3928 if(!device->Backend)
3930 al_free(device);
3931 alcSetError(NULL, ALC_OUT_OF_MEMORY);
3932 return NULL;
3934 almtx_init(&device->BackendLock, almtx_plain);
3936 //Set output format
3937 device->NumUpdates = 0;
3938 device->UpdateSize = 0;
3940 device->Frequency = DEFAULT_OUTPUT_RATE;
3941 device->FmtChans = DevFmtChannelsDefault;
3942 device->FmtType = DevFmtTypeDefault;
3943 device->IsHeadphones = AL_FALSE;
3944 device->AmbiFmt = AmbiFormat_Default;
3946 ConfigValueUInt(NULL, NULL, "sources", &device->SourcesMax);
3947 if(device->SourcesMax == 0) device->SourcesMax = 256;
3949 ConfigValueUInt(NULL, NULL, "slots", &device->AuxiliaryEffectSlotMax);
3950 if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
3952 ConfigValueUInt(NULL, NULL, "sends", &device->NumAuxSends);
3953 if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
3955 device->NumStereoSources = 1;
3956 device->NumMonoSources = device->SourcesMax - device->NumStereoSources;
3958 // Open the "backend"
3959 V(device->Backend,open)("Loopback");
3962 ALCdevice *head = ATOMIC_LOAD(&DeviceList);
3963 do {
3964 device->next = head;
3965 } while(!ATOMIC_COMPARE_EXCHANGE_WEAK(ALCdevice*, &DeviceList, &head, device));
3968 TRACE("Created device %p\n", device);
3969 return device;
3972 /* alcIsRenderFormatSupportedSOFT
3974 * Determines if the loopback device supports the given format for rendering.
3976 ALC_API ALCboolean ALC_APIENTRY alcIsRenderFormatSupportedSOFT(ALCdevice *device, ALCsizei freq, ALCenum channels, ALCenum type)
3978 ALCboolean ret = ALC_FALSE;
3980 if(!VerifyDevice(&device) || device->Type != Loopback)
3981 alcSetError(device, ALC_INVALID_DEVICE);
3982 else if(freq <= 0)
3983 alcSetError(device, ALC_INVALID_VALUE);
3984 else
3986 if(IsValidALCType(type) && BytesFromDevFmt(type) > 0 &&
3987 IsValidALCChannels(channels) && ChannelsFromDevFmt(channels) > 0 &&
3988 freq >= MIN_OUTPUT_RATE)
3989 ret = ALC_TRUE;
3991 if(device) ALCdevice_DecRef(device);
3993 return ret;
3996 /* alcRenderSamplesSOFT
3998 * Renders some samples into a buffer, using the format last set by the
3999 * attributes given to alcCreateContext.
4001 FORCE_ALIGN ALC_API void ALC_APIENTRY alcRenderSamplesSOFT(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
4003 if(!VerifyDevice(&device) || device->Type != Loopback)
4004 alcSetError(device, ALC_INVALID_DEVICE);
4005 else if(samples < 0 || (samples > 0 && buffer == NULL))
4006 alcSetError(device, ALC_INVALID_VALUE);
4007 else
4008 aluMixData(device, buffer, samples);
4009 if(device) ALCdevice_DecRef(device);
4013 /************************************************
4014 * ALC DSP pause/resume functions
4015 ************************************************/
4017 /* alcDevicePauseSOFT
4019 * Pause the DSP to stop audio processing.
4021 ALC_API void ALC_APIENTRY alcDevicePauseSOFT(ALCdevice *device)
4023 if(!VerifyDevice(&device) || device->Type != Playback)
4024 alcSetError(device, ALC_INVALID_DEVICE);
4025 else
4027 almtx_lock(&device->BackendLock);
4028 if((device->Flags&DEVICE_RUNNING))
4029 V0(device->Backend,stop)();
4030 device->Flags &= ~DEVICE_RUNNING;
4031 device->Flags |= DEVICE_PAUSED;
4032 almtx_unlock(&device->BackendLock);
4034 if(device) ALCdevice_DecRef(device);
4037 /* alcDeviceResumeSOFT
4039 * Resume the DSP to restart audio processing.
4041 ALC_API void ALC_APIENTRY alcDeviceResumeSOFT(ALCdevice *device)
4043 if(!VerifyDevice(&device) || device->Type != Playback)
4044 alcSetError(device, ALC_INVALID_DEVICE);
4045 else
4047 almtx_lock(&device->BackendLock);
4048 if((device->Flags&DEVICE_PAUSED))
4050 device->Flags &= ~DEVICE_PAUSED;
4051 if(ATOMIC_LOAD(&device->ContextList) != NULL)
4053 if(V0(device->Backend,start)() != ALC_FALSE)
4054 device->Flags |= DEVICE_RUNNING;
4055 else
4057 alcSetError(device, ALC_INVALID_DEVICE);
4058 V0(device->Backend,lock)();
4059 aluHandleDisconnect(device);
4060 V0(device->Backend,unlock)();
4064 almtx_unlock(&device->BackendLock);
4066 if(device) ALCdevice_DecRef(device);
4070 /************************************************
4071 * ALC HRTF functions
4072 ************************************************/
4074 /* alcGetStringiSOFT
4076 * Gets a string parameter at the given index.
4078 ALC_API const ALCchar* ALC_APIENTRY alcGetStringiSOFT(ALCdevice *device, ALCenum paramName, ALCsizei index)
4080 const ALCchar *str = NULL;
4082 if(!VerifyDevice(&device) || device->Type == Capture)
4083 alcSetError(device, ALC_INVALID_DEVICE);
4084 else switch(paramName)
4086 case ALC_HRTF_SPECIFIER_SOFT:
4087 if(index >= 0 && (size_t)index < VECTOR_SIZE(device->Hrtf.List))
4088 str = al_string_get_cstr(VECTOR_ELEM(device->Hrtf.List, index).name);
4089 else
4090 alcSetError(device, ALC_INVALID_VALUE);
4091 break;
4093 default:
4094 alcSetError(device, ALC_INVALID_ENUM);
4095 break;
4097 if(device) ALCdevice_DecRef(device);
4099 return str;
4102 /* alcResetDeviceSOFT
4104 * Resets the given device output, using the specified attribute list.
4106 ALC_API ALCboolean ALC_APIENTRY alcResetDeviceSOFT(ALCdevice *device, const ALCint *attribs)
4108 ALCenum err;
4110 LockLists();
4111 if(!VerifyDevice(&device) || device->Type == Capture || !device->Connected)
4113 UnlockLists();
4114 alcSetError(device, ALC_INVALID_DEVICE);
4115 if(device) ALCdevice_DecRef(device);
4116 return ALC_FALSE;
4118 almtx_lock(&device->BackendLock);
4119 UnlockLists();
4121 err = UpdateDeviceParams(device, attribs);
4122 almtx_unlock(&device->BackendLock);
4124 if(err != ALC_NO_ERROR)
4126 alcSetError(device, err);
4127 if(err == ALC_INVALID_DEVICE)
4129 V0(device->Backend,lock)();
4130 aluHandleDisconnect(device);
4131 V0(device->Backend,unlock)();
4133 ALCdevice_DecRef(device);
4134 return ALC_FALSE;
4136 ALCdevice_DecRef(device);
4138 return ALC_TRUE;