Add some helper wrappers to mmap files
[openal-soft.git] / Alc / ALc.c
blob7759393b0371ffb0534f55b7a4f1969c90f0e3c3
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_SOFTX_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 DECL_CONST static ALboolean DecomposeDevFormat(ALenum format,
1338 enum DevFmtChannels *chans, 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 DECL_CONST 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 DECL_CONST 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 RWLockInit(&Context->PropLock);
2323 ATOMIC_INIT(&Context->LastError, AL_NO_ERROR);
2324 InitUIntMap(&Context->SourceMap, Context->Device->SourcesMax);
2325 InitUIntMap(&Context->EffectSlotMap, Context->Device->AuxiliaryEffectSlotMax);
2327 //Set globals
2328 Context->DistanceModel = DefaultDistanceModel;
2329 Context->SourceDistanceModel = AL_FALSE;
2330 Context->DopplerFactor = 1.0f;
2331 Context->DopplerVelocity = 1.0f;
2332 Context->SpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
2333 ATOMIC_INIT(&Context->DeferUpdates, AL_FALSE);
2335 Context->ExtensionList = alExtList;
2339 /* FreeContext
2341 * Cleans up the context, and destroys any remaining objects the app failed to
2342 * delete. Called once there's no more references on the context.
2344 static void FreeContext(ALCcontext *context)
2346 ALlistener *listener = context->Listener;
2347 struct ALlistenerProps *lprops;
2348 size_t count;
2350 TRACE("%p\n", context);
2352 if(context->SourceMap.size > 0)
2354 WARN("(%p) Deleting %d Source%s\n", context, context->SourceMap.size,
2355 (context->SourceMap.size==1)?"":"s");
2356 ReleaseALSources(context);
2358 ResetUIntMap(&context->SourceMap);
2360 if(context->EffectSlotMap.size > 0)
2362 WARN("(%p) Deleting %d AuxiliaryEffectSlot%s\n", context, context->EffectSlotMap.size,
2363 (context->EffectSlotMap.size==1)?"":"s");
2364 ReleaseALAuxiliaryEffectSlots(context);
2366 ResetUIntMap(&context->EffectSlotMap);
2368 al_free(context->Voices);
2369 context->Voices = NULL;
2370 context->VoiceCount = 0;
2371 context->MaxVoices = 0;
2373 if((lprops=ATOMIC_LOAD(&listener->Update, almemory_order_acquire)) != NULL)
2375 TRACE("Freed unapplied listener update %p\n", lprops);
2376 al_free(lprops);
2378 count = 0;
2379 lprops = ATOMIC_LOAD(&listener->FreeList, almemory_order_consume);
2380 while(lprops)
2382 struct ALlistenerProps *next = ATOMIC_LOAD(&lprops->next, almemory_order_consume);
2383 al_free(lprops);
2384 lprops = next;
2385 ++count;
2387 TRACE("Freed "SZFMT" listener property object%s\n", count, (count==1)?"":"s");
2389 ALCdevice_DecRef(context->Device);
2390 context->Device = NULL;
2392 //Invalidate context
2393 memset(context, 0, sizeof(ALCcontext));
2394 al_free(context);
2397 /* ReleaseContext
2399 * Removes the context reference from the given device and removes it from
2400 * being current on the running thread or globally.
2402 static void ReleaseContext(ALCcontext *context, ALCdevice *device)
2404 ALCcontext *nextctx;
2405 ALCcontext *origctx;
2407 if(altss_get(LocalContext) == context)
2409 WARN("%p released while current on thread\n", context);
2410 altss_set(LocalContext, NULL);
2411 ALCcontext_DecRef(context);
2414 origctx = context;
2415 if(ATOMIC_COMPARE_EXCHANGE_STRONG(ALCcontext*, &GlobalContext, &origctx, NULL))
2416 ALCcontext_DecRef(context);
2418 ALCdevice_Lock(device);
2419 origctx = context;
2420 nextctx = context->next;
2421 if(!ATOMIC_COMPARE_EXCHANGE_STRONG(ALCcontext*, &device->ContextList, &origctx, nextctx))
2423 ALCcontext *list;
2424 do {
2425 list = origctx;
2426 origctx = context;
2427 } while(!COMPARE_EXCHANGE(&list->next, &origctx, nextctx));
2429 ALCdevice_Unlock(device);
2431 ALCcontext_DecRef(context);
2434 void ALCcontext_IncRef(ALCcontext *context)
2436 uint ref = IncrementRef(&context->ref);
2437 TRACEREF("%p increasing refcount to %u\n", context, ref);
2440 void ALCcontext_DecRef(ALCcontext *context)
2442 uint ref = DecrementRef(&context->ref);
2443 TRACEREF("%p decreasing refcount to %u\n", context, ref);
2444 if(ref == 0) FreeContext(context);
2447 static void ReleaseThreadCtx(void *ptr)
2449 ALCcontext *context = ptr;
2450 uint ref = DecrementRef(&context->ref);
2451 TRACEREF("%p decreasing refcount to %u\n", context, ref);
2452 ERR("Context %p current for thread being destroyed, possible leak!\n", context);
2455 /* VerifyContext
2457 * Checks that the given context is valid, and increments its reference count.
2459 static ALCboolean VerifyContext(ALCcontext **context)
2461 ALCdevice *dev;
2463 LockLists();
2464 dev = ATOMIC_LOAD(&DeviceList);
2465 while(dev)
2467 ALCcontext *ctx = ATOMIC_LOAD(&dev->ContextList);
2468 while(ctx)
2470 if(ctx == *context)
2472 ALCcontext_IncRef(ctx);
2473 UnlockLists();
2474 return ALC_TRUE;
2476 ctx = ctx->next;
2478 dev = dev->next;
2480 UnlockLists();
2482 *context = NULL;
2483 return ALC_FALSE;
2487 /* GetContextRef
2489 * Returns the currently active context for this thread, and adds a reference
2490 * without locking it.
2492 ALCcontext *GetContextRef(void)
2494 ALCcontext *context;
2496 context = altss_get(LocalContext);
2497 if(context)
2498 ALCcontext_IncRef(context);
2499 else
2501 LockLists();
2502 context = ATOMIC_LOAD(&GlobalContext);
2503 if(context)
2504 ALCcontext_IncRef(context);
2505 UnlockLists();
2508 return context;
2512 /************************************************
2513 * Standard ALC functions
2514 ************************************************/
2516 /* alcGetError
2518 * Return last ALC generated error code for the given device
2520 ALC_API ALCenum ALC_APIENTRY alcGetError(ALCdevice *device)
2522 ALCenum errorCode;
2524 if(VerifyDevice(&device))
2526 errorCode = ATOMIC_EXCHANGE(ALCenum, &device->LastError, ALC_NO_ERROR);
2527 ALCdevice_DecRef(device);
2529 else
2530 errorCode = ATOMIC_EXCHANGE(ALCenum, &LastNullDeviceError, ALC_NO_ERROR);
2532 return errorCode;
2536 /* alcSuspendContext
2538 * Suspends updates for the given context
2540 ALC_API ALCvoid ALC_APIENTRY alcSuspendContext(ALCcontext *context)
2542 if(!SuspendDefers)
2543 return;
2545 if(!VerifyContext(&context))
2546 alcSetError(NULL, ALC_INVALID_CONTEXT);
2547 else
2549 ALCcontext_DeferUpdates(context, DeferAllowPlay);
2550 ALCcontext_DecRef(context);
2554 /* alcProcessContext
2556 * Resumes processing updates for the given context
2558 ALC_API ALCvoid ALC_APIENTRY alcProcessContext(ALCcontext *context)
2560 if(!SuspendDefers)
2561 return;
2563 if(!VerifyContext(&context))
2564 alcSetError(NULL, ALC_INVALID_CONTEXT);
2565 else
2567 ALCcontext_ProcessUpdates(context);
2568 ALCcontext_DecRef(context);
2573 /* alcGetString
2575 * Returns information about the device, and error strings
2577 ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *Device, ALCenum param)
2579 const ALCchar *value = NULL;
2581 switch(param)
2583 case ALC_NO_ERROR:
2584 value = alcNoError;
2585 break;
2587 case ALC_INVALID_ENUM:
2588 value = alcErrInvalidEnum;
2589 break;
2591 case ALC_INVALID_VALUE:
2592 value = alcErrInvalidValue;
2593 break;
2595 case ALC_INVALID_DEVICE:
2596 value = alcErrInvalidDevice;
2597 break;
2599 case ALC_INVALID_CONTEXT:
2600 value = alcErrInvalidContext;
2601 break;
2603 case ALC_OUT_OF_MEMORY:
2604 value = alcErrOutOfMemory;
2605 break;
2607 case ALC_DEVICE_SPECIFIER:
2608 value = alcDefaultName;
2609 break;
2611 case ALC_ALL_DEVICES_SPECIFIER:
2612 if(VerifyDevice(&Device))
2614 value = al_string_get_cstr(Device->DeviceName);
2615 ALCdevice_DecRef(Device);
2617 else
2619 ProbeAllDevicesList();
2620 value = al_string_get_cstr(alcAllDevicesList);
2622 break;
2624 case ALC_CAPTURE_DEVICE_SPECIFIER:
2625 if(VerifyDevice(&Device))
2627 value = al_string_get_cstr(Device->DeviceName);
2628 ALCdevice_DecRef(Device);
2630 else
2632 ProbeCaptureDeviceList();
2633 value = al_string_get_cstr(alcCaptureDeviceList);
2635 break;
2637 /* Default devices are always first in the list */
2638 case ALC_DEFAULT_DEVICE_SPECIFIER:
2639 value = alcDefaultName;
2640 break;
2642 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
2643 if(al_string_empty(alcAllDevicesList))
2644 ProbeAllDevicesList();
2646 VerifyDevice(&Device);
2648 free(alcDefaultAllDevicesSpecifier);
2649 alcDefaultAllDevicesSpecifier = strdup(al_string_get_cstr(alcAllDevicesList));
2650 if(!alcDefaultAllDevicesSpecifier)
2651 alcSetError(Device, ALC_OUT_OF_MEMORY);
2653 value = alcDefaultAllDevicesSpecifier;
2654 if(Device) ALCdevice_DecRef(Device);
2655 break;
2657 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
2658 if(al_string_empty(alcCaptureDeviceList))
2659 ProbeCaptureDeviceList();
2661 VerifyDevice(&Device);
2663 free(alcCaptureDefaultDeviceSpecifier);
2664 alcCaptureDefaultDeviceSpecifier = strdup(al_string_get_cstr(alcCaptureDeviceList));
2665 if(!alcCaptureDefaultDeviceSpecifier)
2666 alcSetError(Device, ALC_OUT_OF_MEMORY);
2668 value = alcCaptureDefaultDeviceSpecifier;
2669 if(Device) ALCdevice_DecRef(Device);
2670 break;
2672 case ALC_EXTENSIONS:
2673 if(!VerifyDevice(&Device))
2674 value = alcNoDeviceExtList;
2675 else
2677 value = alcExtensionList;
2678 ALCdevice_DecRef(Device);
2680 break;
2682 case ALC_HRTF_SPECIFIER_SOFT:
2683 if(!VerifyDevice(&Device))
2684 alcSetError(NULL, ALC_INVALID_DEVICE);
2685 else
2687 almtx_lock(&Device->BackendLock);
2688 value = (Device->Hrtf.Handle ? al_string_get_cstr(Device->Hrtf.Name) : "");
2689 almtx_unlock(&Device->BackendLock);
2690 ALCdevice_DecRef(Device);
2692 break;
2694 default:
2695 VerifyDevice(&Device);
2696 alcSetError(Device, ALC_INVALID_ENUM);
2697 if(Device) ALCdevice_DecRef(Device);
2698 break;
2701 return value;
2705 static ALCsizei GetIntegerv(ALCdevice *device, ALCenum param, ALCsizei size, ALCint *values)
2707 ALCsizei i;
2709 if(size <= 0 || values == NULL)
2711 alcSetError(device, ALC_INVALID_VALUE);
2712 return 0;
2715 if(!device)
2717 switch(param)
2719 case ALC_MAJOR_VERSION:
2720 values[0] = alcMajorVersion;
2721 return 1;
2722 case ALC_MINOR_VERSION:
2723 values[0] = alcMinorVersion;
2724 return 1;
2726 case ALC_ATTRIBUTES_SIZE:
2727 case ALC_ALL_ATTRIBUTES:
2728 case ALC_FREQUENCY:
2729 case ALC_REFRESH:
2730 case ALC_SYNC:
2731 case ALC_MONO_SOURCES:
2732 case ALC_STEREO_SOURCES:
2733 case ALC_CAPTURE_SAMPLES:
2734 case ALC_FORMAT_CHANNELS_SOFT:
2735 case ALC_FORMAT_TYPE_SOFT:
2736 alcSetError(NULL, ALC_INVALID_DEVICE);
2737 return 0;
2739 default:
2740 alcSetError(NULL, ALC_INVALID_ENUM);
2741 return 0;
2743 return 0;
2746 if(device->Type == Capture)
2748 switch(param)
2750 case ALC_CAPTURE_SAMPLES:
2751 almtx_lock(&device->BackendLock);
2752 values[0] = V0(device->Backend,availableSamples)();
2753 almtx_unlock(&device->BackendLock);
2754 return 1;
2756 case ALC_CONNECTED:
2757 values[0] = device->Connected;
2758 return 1;
2760 default:
2761 alcSetError(device, ALC_INVALID_ENUM);
2762 return 0;
2764 return 0;
2767 /* render device */
2768 switch(param)
2770 case ALC_MAJOR_VERSION:
2771 values[0] = alcMajorVersion;
2772 return 1;
2774 case ALC_MINOR_VERSION:
2775 values[0] = alcMinorVersion;
2776 return 1;
2778 case ALC_EFX_MAJOR_VERSION:
2779 values[0] = alcEFXMajorVersion;
2780 return 1;
2782 case ALC_EFX_MINOR_VERSION:
2783 values[0] = alcEFXMinorVersion;
2784 return 1;
2786 case ALC_ATTRIBUTES_SIZE:
2787 values[0] = 17;
2788 return 1;
2790 case ALC_ALL_ATTRIBUTES:
2791 if(size < 17)
2793 alcSetError(device, ALC_INVALID_VALUE);
2794 return 0;
2797 i = 0;
2798 almtx_lock(&device->BackendLock);
2799 values[i++] = ALC_FREQUENCY;
2800 values[i++] = device->Frequency;
2802 if(device->Type != Loopback)
2804 values[i++] = ALC_REFRESH;
2805 values[i++] = device->Frequency / device->UpdateSize;
2807 values[i++] = ALC_SYNC;
2808 values[i++] = ALC_FALSE;
2810 else
2812 values[i++] = ALC_FORMAT_CHANNELS_SOFT;
2813 values[i++] = device->FmtChans;
2815 values[i++] = ALC_FORMAT_TYPE_SOFT;
2816 values[i++] = device->FmtType;
2819 values[i++] = ALC_MONO_SOURCES;
2820 values[i++] = device->NumMonoSources;
2822 values[i++] = ALC_STEREO_SOURCES;
2823 values[i++] = device->NumStereoSources;
2825 values[i++] = ALC_MAX_AUXILIARY_SENDS;
2826 values[i++] = device->NumAuxSends;
2828 values[i++] = ALC_HRTF_SOFT;
2829 values[i++] = (device->Hrtf.Handle ? ALC_TRUE : ALC_FALSE);
2831 values[i++] = ALC_HRTF_STATUS_SOFT;
2832 values[i++] = device->Hrtf.Status;
2833 almtx_unlock(&device->BackendLock);
2835 values[i++] = 0;
2836 return i;
2838 case ALC_FREQUENCY:
2839 values[0] = device->Frequency;
2840 return 1;
2842 case ALC_REFRESH:
2843 if(device->Type == Loopback)
2845 alcSetError(device, ALC_INVALID_DEVICE);
2846 return 0;
2848 almtx_lock(&device->BackendLock);
2849 values[0] = device->Frequency / device->UpdateSize;
2850 almtx_unlock(&device->BackendLock);
2851 return 1;
2853 case ALC_SYNC:
2854 if(device->Type == Loopback)
2856 alcSetError(device, ALC_INVALID_DEVICE);
2857 return 0;
2859 values[0] = ALC_FALSE;
2860 return 1;
2862 case ALC_FORMAT_CHANNELS_SOFT:
2863 if(device->Type != Loopback)
2865 alcSetError(device, ALC_INVALID_DEVICE);
2866 return 0;
2868 values[0] = device->FmtChans;
2869 return 1;
2871 case ALC_FORMAT_TYPE_SOFT:
2872 if(device->Type != Loopback)
2874 alcSetError(device, ALC_INVALID_DEVICE);
2875 return 0;
2877 values[0] = device->FmtType;
2878 return 1;
2880 case ALC_MONO_SOURCES:
2881 values[0] = device->NumMonoSources;
2882 return 1;
2884 case ALC_STEREO_SOURCES:
2885 values[0] = device->NumStereoSources;
2886 return 1;
2888 case ALC_MAX_AUXILIARY_SENDS:
2889 values[0] = device->NumAuxSends;
2890 return 1;
2892 case ALC_CONNECTED:
2893 values[0] = device->Connected;
2894 return 1;
2896 case ALC_HRTF_SOFT:
2897 values[0] = (device->Hrtf.Handle ? ALC_TRUE : ALC_FALSE);
2898 return 1;
2900 case ALC_HRTF_STATUS_SOFT:
2901 values[0] = device->Hrtf.Status;
2902 return 1;
2904 case ALC_NUM_HRTF_SPECIFIERS_SOFT:
2905 almtx_lock(&device->BackendLock);
2906 FreeHrtfList(&device->Hrtf.List);
2907 device->Hrtf.List = EnumerateHrtf(device->DeviceName);
2908 values[0] = (ALCint)VECTOR_SIZE(device->Hrtf.List);
2909 almtx_unlock(&device->BackendLock);
2910 return 1;
2912 default:
2913 alcSetError(device, ALC_INVALID_ENUM);
2914 return 0;
2916 return 0;
2919 /* alcGetIntegerv
2921 * Returns information about the device and the version of OpenAL
2923 ALC_API void ALC_APIENTRY alcGetIntegerv(ALCdevice *device, ALCenum param, ALCsizei size, ALCint *values)
2925 VerifyDevice(&device);
2926 if(size <= 0 || values == NULL)
2927 alcSetError(device, ALC_INVALID_VALUE);
2928 else
2929 GetIntegerv(device, param, size, values);
2930 if(device) ALCdevice_DecRef(device);
2933 ALC_API void ALC_APIENTRY alcGetInteger64vSOFT(ALCdevice *device, ALCenum pname, ALCsizei size, ALCint64SOFT *values)
2935 ALCint *ivals;
2936 ALsizei i;
2938 VerifyDevice(&device);
2939 if(size <= 0 || values == NULL)
2940 alcSetError(device, ALC_INVALID_VALUE);
2941 else if(!device || device->Type == Capture)
2943 ivals = malloc(size * sizeof(ALCint));
2944 size = GetIntegerv(device, pname, size, ivals);
2945 for(i = 0;i < size;i++)
2946 values[i] = ivals[i];
2947 free(ivals);
2949 else /* render device */
2951 ClockLatency clock;
2952 ALuint64 basecount;
2953 ALuint samplecount;
2954 ALuint refcount;
2956 switch(pname)
2958 case ALC_ATTRIBUTES_SIZE:
2959 *values = 21;
2960 break;
2962 case ALC_ALL_ATTRIBUTES:
2963 if(size < 21)
2964 alcSetError(device, ALC_INVALID_VALUE);
2965 else
2967 i = 0;
2968 almtx_lock(&device->BackendLock);
2969 values[i++] = ALC_FREQUENCY;
2970 values[i++] = device->Frequency;
2972 if(device->Type != Loopback)
2974 values[i++] = ALC_REFRESH;
2975 values[i++] = device->Frequency / device->UpdateSize;
2977 values[i++] = ALC_SYNC;
2978 values[i++] = ALC_FALSE;
2980 else
2982 values[i++] = ALC_FORMAT_CHANNELS_SOFT;
2983 values[i++] = device->FmtChans;
2985 values[i++] = ALC_FORMAT_TYPE_SOFT;
2986 values[i++] = device->FmtType;
2989 values[i++] = ALC_MONO_SOURCES;
2990 values[i++] = device->NumMonoSources;
2992 values[i++] = ALC_STEREO_SOURCES;
2993 values[i++] = device->NumStereoSources;
2995 values[i++] = ALC_MAX_AUXILIARY_SENDS;
2996 values[i++] = device->NumAuxSends;
2998 values[i++] = ALC_HRTF_SOFT;
2999 values[i++] = (device->Hrtf.Handle ? ALC_TRUE : ALC_FALSE);
3001 values[i++] = ALC_HRTF_STATUS_SOFT;
3002 values[i++] = device->Hrtf.Status;
3004 clock = V0(device->Backend,getClockLatency)();
3005 values[i++] = ALC_DEVICE_CLOCK_SOFT;
3006 values[i++] = clock.ClockTime;
3008 values[i++] = ALC_DEVICE_LATENCY_SOFT;
3009 values[i++] = clock.Latency;
3010 almtx_unlock(&device->BackendLock);
3012 values[i++] = 0;
3014 break;
3016 case ALC_DEVICE_CLOCK_SOFT:
3017 almtx_lock(&device->BackendLock);
3018 do {
3019 while(((refcount=ReadRef(&device->MixCount))&1) != 0)
3020 althrd_yield();
3021 basecount = device->ClockBase;
3022 samplecount = device->SamplesDone;
3023 } while(refcount != ReadRef(&device->MixCount));
3024 *values = basecount + (samplecount*DEVICE_CLOCK_RES/device->Frequency);
3025 almtx_unlock(&device->BackendLock);
3026 break;
3028 case ALC_DEVICE_LATENCY_SOFT:
3030 almtx_lock(&device->BackendLock);
3031 clock = V0(device->Backend,getClockLatency)();
3032 almtx_unlock(&device->BackendLock);
3033 *values = clock.Latency;
3035 break;
3037 case ALC_DEVICE_CLOCK_LATENCY_SOFT:
3038 if(size < 2)
3039 alcSetError(device, ALC_INVALID_VALUE);
3040 else
3042 ClockLatency clock;
3043 almtx_lock(&device->BackendLock);
3044 clock = V0(device->Backend,getClockLatency)();
3045 almtx_unlock(&device->BackendLock);
3046 values[0] = clock.ClockTime;
3047 values[1] = clock.Latency;
3049 break;
3051 default:
3052 ivals = malloc(size * sizeof(ALCint));
3053 size = GetIntegerv(device, pname, size, ivals);
3054 for(i = 0;i < size;i++)
3055 values[i] = ivals[i];
3056 free(ivals);
3057 break;
3060 if(device)
3061 ALCdevice_DecRef(device);
3065 /* alcIsExtensionPresent
3067 * Determines if there is support for a particular extension
3069 ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
3071 ALCboolean bResult = ALC_FALSE;
3073 VerifyDevice(&device);
3075 if(!extName)
3076 alcSetError(device, ALC_INVALID_VALUE);
3077 else
3079 size_t len = strlen(extName);
3080 const char *ptr = (device ? alcExtensionList : alcNoDeviceExtList);
3081 while(ptr && *ptr)
3083 if(strncasecmp(ptr, extName, len) == 0 &&
3084 (ptr[len] == '\0' || isspace(ptr[len])))
3086 bResult = ALC_TRUE;
3087 break;
3089 if((ptr=strchr(ptr, ' ')) != NULL)
3091 do {
3092 ++ptr;
3093 } while(isspace(*ptr));
3097 if(device)
3098 ALCdevice_DecRef(device);
3099 return bResult;
3103 /* alcGetProcAddress
3105 * Retrieves the function address for a particular extension function
3107 ALC_API ALCvoid* ALC_APIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
3109 ALCvoid *ptr = NULL;
3111 if(!funcName)
3113 VerifyDevice(&device);
3114 alcSetError(device, ALC_INVALID_VALUE);
3115 if(device) ALCdevice_DecRef(device);
3117 else
3119 ALsizei i = 0;
3120 while(alcFunctions[i].funcName && strcmp(alcFunctions[i].funcName, funcName) != 0)
3121 i++;
3122 ptr = alcFunctions[i].address;
3125 return ptr;
3129 /* alcGetEnumValue
3131 * Get the value for a particular ALC enumeration name
3133 ALC_API ALCenum ALC_APIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
3135 ALCenum val = 0;
3137 if(!enumName)
3139 VerifyDevice(&device);
3140 alcSetError(device, ALC_INVALID_VALUE);
3141 if(device) ALCdevice_DecRef(device);
3143 else
3145 ALsizei i = 0;
3146 while(enumeration[i].enumName && strcmp(enumeration[i].enumName, enumName) != 0)
3147 i++;
3148 val = enumeration[i].value;
3151 return val;
3155 /* alcCreateContext
3157 * Create and attach a context to the given device.
3159 ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
3161 ALCcontext *ALContext;
3162 ALCenum err;
3164 LockLists();
3165 if(!VerifyDevice(&device) || device->Type == Capture || !device->Connected)
3167 UnlockLists();
3168 alcSetError(device, ALC_INVALID_DEVICE);
3169 if(device) ALCdevice_DecRef(device);
3170 return NULL;
3172 almtx_lock(&device->BackendLock);
3173 UnlockLists();
3175 ATOMIC_STORE(&device->LastError, ALC_NO_ERROR);
3177 ALContext = al_calloc(16, sizeof(ALCcontext)+sizeof(ALlistener));
3178 if(ALContext)
3180 InitRef(&ALContext->ref, 1);
3181 ALContext->Listener = (ALlistener*)ALContext->_listener_mem;
3183 ATOMIC_INIT(&ALContext->ActiveAuxSlotList, NULL);
3185 ALContext->VoiceCount = 0;
3186 ALContext->MaxVoices = 256;
3187 ALContext->Voices = al_calloc(16, ALContext->MaxVoices * sizeof(ALContext->Voices[0]));
3189 if(!ALContext || !ALContext->Voices)
3191 almtx_unlock(&device->BackendLock);
3193 if(ALContext)
3195 al_free(ALContext->Voices);
3196 ALContext->Voices = NULL;
3198 al_free(ALContext);
3199 ALContext = NULL;
3202 alcSetError(device, ALC_OUT_OF_MEMORY);
3203 ALCdevice_DecRef(device);
3204 return NULL;
3207 if((err=UpdateDeviceParams(device, attrList)) != ALC_NO_ERROR)
3209 almtx_unlock(&device->BackendLock);
3211 al_free(ALContext->Voices);
3212 ALContext->Voices = NULL;
3214 al_free(ALContext);
3215 ALContext = NULL;
3217 alcSetError(device, err);
3218 if(err == ALC_INVALID_DEVICE)
3220 V0(device->Backend,lock)();
3221 aluHandleDisconnect(device);
3222 V0(device->Backend,unlock)();
3224 ALCdevice_DecRef(device);
3225 return NULL;
3228 ALContext->Device = device;
3229 ALCdevice_IncRef(device);
3230 InitContext(ALContext);
3233 ALCcontext *head = ATOMIC_LOAD(&device->ContextList);
3234 do {
3235 ALContext->next = head;
3236 } while(!ATOMIC_COMPARE_EXCHANGE_WEAK(ALCcontext*, &device->ContextList, &head, ALContext));
3238 almtx_unlock(&device->BackendLock);
3240 ALCdevice_DecRef(device);
3242 TRACE("Created context %p\n", ALContext);
3243 return ALContext;
3246 /* alcDestroyContext
3248 * Remove a context from its device
3250 ALC_API ALCvoid ALC_APIENTRY alcDestroyContext(ALCcontext *context)
3252 ALCdevice *Device;
3254 LockLists();
3255 /* alcGetContextsDevice sets an error for invalid contexts */
3256 Device = alcGetContextsDevice(context);
3257 if(Device)
3259 almtx_lock(&Device->BackendLock);
3260 ReleaseContext(context, Device);
3261 if(!ATOMIC_LOAD(&Device->ContextList))
3263 V0(Device->Backend,stop)();
3264 Device->Flags &= ~DEVICE_RUNNING;
3266 almtx_unlock(&Device->BackendLock);
3268 UnlockLists();
3272 /* alcGetCurrentContext
3274 * Returns the currently active context on the calling thread
3276 ALC_API ALCcontext* ALC_APIENTRY alcGetCurrentContext(void)
3278 ALCcontext *Context = altss_get(LocalContext);
3279 if(!Context) Context = ATOMIC_LOAD(&GlobalContext);
3280 return Context;
3283 /* alcGetThreadContext
3285 * Returns the currently active thread-local context
3287 ALC_API ALCcontext* ALC_APIENTRY alcGetThreadContext(void)
3289 return altss_get(LocalContext);
3293 /* alcMakeContextCurrent
3295 * Makes the given context the active process-wide context, and removes the
3296 * thread-local context for the calling thread.
3298 ALC_API ALCboolean ALC_APIENTRY alcMakeContextCurrent(ALCcontext *context)
3300 /* context must be valid or NULL */
3301 if(context && !VerifyContext(&context))
3303 alcSetError(NULL, ALC_INVALID_CONTEXT);
3304 return ALC_FALSE;
3306 /* context's reference count is already incremented */
3307 context = ATOMIC_EXCHANGE(ALCcontext*, &GlobalContext, context);
3308 if(context) ALCcontext_DecRef(context);
3310 if((context=altss_get(LocalContext)) != NULL)
3312 altss_set(LocalContext, NULL);
3313 ALCcontext_DecRef(context);
3316 return ALC_TRUE;
3319 /* alcSetThreadContext
3321 * Makes the given context the active context for the current thread
3323 ALC_API ALCboolean ALC_APIENTRY alcSetThreadContext(ALCcontext *context)
3325 ALCcontext *old;
3327 /* context must be valid or NULL */
3328 if(context && !VerifyContext(&context))
3330 alcSetError(NULL, ALC_INVALID_CONTEXT);
3331 return ALC_FALSE;
3333 /* context's reference count is already incremented */
3334 old = altss_get(LocalContext);
3335 altss_set(LocalContext, context);
3336 if(old) ALCcontext_DecRef(old);
3338 return ALC_TRUE;
3342 /* alcGetContextsDevice
3344 * Returns the device that a particular context is attached to
3346 ALC_API ALCdevice* ALC_APIENTRY alcGetContextsDevice(ALCcontext *Context)
3348 ALCdevice *Device;
3350 if(!VerifyContext(&Context))
3352 alcSetError(NULL, ALC_INVALID_CONTEXT);
3353 return NULL;
3355 Device = Context->Device;
3356 ALCcontext_DecRef(Context);
3358 return Device;
3362 /* alcOpenDevice
3364 * Opens the named device.
3366 ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName)
3368 const ALCchar *fmt;
3369 ALCdevice *device;
3370 ALCenum err;
3372 DO_INITCONFIG();
3374 if(!PlaybackBackend.name)
3376 alcSetError(NULL, ALC_INVALID_VALUE);
3377 return NULL;
3380 if(deviceName && (!deviceName[0] || strcasecmp(deviceName, alcDefaultName) == 0 || strcasecmp(deviceName, "openal-soft") == 0
3381 #ifdef _WIN32
3382 /* Some old Windows apps hardcode these expecting OpenAL to use a
3383 * specific audio API, even when they're not enumerated. Creative's
3384 * router effectively ignores them too.
3386 || strcasecmp(deviceName, "DirectSound3D") == 0 || strcasecmp(deviceName, "DirectSound") == 0
3387 || strcasecmp(deviceName, "MMSYSTEM") == 0
3388 #endif
3390 deviceName = NULL;
3392 device = al_calloc(16, sizeof(ALCdevice)+sizeof(ALeffectslot));
3393 if(!device)
3395 alcSetError(NULL, ALC_OUT_OF_MEMORY);
3396 return NULL;
3399 //Validate device
3400 InitRef(&device->ref, 1);
3401 device->Connected = ALC_TRUE;
3402 device->Type = Playback;
3403 ATOMIC_INIT(&device->LastError, ALC_NO_ERROR);
3405 device->Flags = 0;
3406 device->Bs2b = NULL;
3407 device->Uhj_Encoder = NULL;
3408 VECTOR_INIT(device->Hrtf.List);
3409 AL_STRING_INIT(device->Hrtf.Name);
3410 device->Render_Mode = NormalRender;
3411 AL_STRING_INIT(device->DeviceName);
3412 device->Dry.Buffer = NULL;
3413 device->Dry.NumChannels = 0;
3414 device->FOAOut.Buffer = NULL;
3415 device->FOAOut.NumChannels = 0;
3416 device->RealOut.Buffer = NULL;
3417 device->RealOut.NumChannels = 0;
3419 ATOMIC_INIT(&device->ContextList, NULL);
3421 device->ClockBase = 0;
3422 device->SamplesDone = 0;
3424 device->SourcesMax = 256;
3425 device->AuxiliaryEffectSlotMax = 4;
3426 device->NumAuxSends = MAX_SENDS;
3428 InitUIntMap(&device->BufferMap, ~0);
3429 InitUIntMap(&device->EffectMap, ~0);
3430 InitUIntMap(&device->FilterMap, ~0);
3432 //Set output format
3433 device->FmtChans = DevFmtChannelsDefault;
3434 device->FmtType = DevFmtTypeDefault;
3435 device->Frequency = DEFAULT_OUTPUT_RATE;
3436 device->IsHeadphones = AL_FALSE;
3437 device->AmbiFmt = AmbiFormat_Default;
3438 device->NumUpdates = 4;
3439 device->UpdateSize = 1024;
3441 if(!PlaybackBackend.getFactory)
3442 device->Backend = create_backend_wrapper(device, &PlaybackBackend.Funcs,
3443 ALCbackend_Playback);
3444 else
3446 ALCbackendFactory *factory = PlaybackBackend.getFactory();
3447 device->Backend = V(factory,createBackend)(device, ALCbackend_Playback);
3449 if(!device->Backend)
3451 al_free(device);
3452 alcSetError(NULL, ALC_OUT_OF_MEMORY);
3453 return NULL;
3457 if(ConfigValueStr(deviceName, NULL, "channels", &fmt))
3459 static const struct {
3460 const char name[16];
3461 enum DevFmtChannels chans;
3462 } chanlist[] = {
3463 { "mono", DevFmtMono },
3464 { "stereo", DevFmtStereo },
3465 { "quad", DevFmtQuad },
3466 { "surround51", DevFmtX51 },
3467 { "surround61", DevFmtX61 },
3468 { "surround71", DevFmtX71 },
3469 { "surround51rear", DevFmtX51Rear },
3470 { "ambi1", DevFmtAmbi1 },
3471 { "ambi2", DevFmtAmbi2 },
3472 { "ambi3", DevFmtAmbi3 },
3474 size_t i;
3476 for(i = 0;i < COUNTOF(chanlist);i++)
3478 if(strcasecmp(chanlist[i].name, fmt) == 0)
3480 device->FmtChans = chanlist[i].chans;
3481 device->Flags |= DEVICE_CHANNELS_REQUEST;
3482 break;
3485 if(i == COUNTOF(chanlist))
3486 ERR("Unsupported channels: %s\n", fmt);
3488 if(ConfigValueStr(deviceName, NULL, "sample-type", &fmt))
3490 static const struct {
3491 const char name[16];
3492 enum DevFmtType type;
3493 } typelist[] = {
3494 { "int8", DevFmtByte },
3495 { "uint8", DevFmtUByte },
3496 { "int16", DevFmtShort },
3497 { "uint16", DevFmtUShort },
3498 { "int32", DevFmtInt },
3499 { "uint32", DevFmtUInt },
3500 { "float32", DevFmtFloat },
3502 size_t i;
3504 for(i = 0;i < COUNTOF(typelist);i++)
3506 if(strcasecmp(typelist[i].name, fmt) == 0)
3508 device->FmtType = typelist[i].type;
3509 device->Flags |= DEVICE_SAMPLE_TYPE_REQUEST;
3510 break;
3513 if(i == COUNTOF(typelist))
3514 ERR("Unsupported sample-type: %s\n", fmt);
3517 if(ConfigValueUInt(deviceName, NULL, "frequency", &device->Frequency))
3519 device->Flags |= DEVICE_FREQUENCY_REQUEST;
3520 if(device->Frequency < MIN_OUTPUT_RATE)
3521 ERR("%uhz request clamped to %uhz minimum\n", device->Frequency, MIN_OUTPUT_RATE);
3522 device->Frequency = maxu(device->Frequency, MIN_OUTPUT_RATE);
3525 ConfigValueUInt(deviceName, NULL, "periods", &device->NumUpdates);
3526 device->NumUpdates = clampu(device->NumUpdates, 2, 16);
3528 ConfigValueUInt(deviceName, NULL, "period_size", &device->UpdateSize);
3529 device->UpdateSize = clampu(device->UpdateSize, 64, 8192);
3530 if((CPUCapFlags&(CPU_CAP_SSE|CPU_CAP_NEON)) != 0)
3531 device->UpdateSize = (device->UpdateSize+3)&~3;
3533 ConfigValueUInt(deviceName, NULL, "sources", &device->SourcesMax);
3534 if(device->SourcesMax == 0) device->SourcesMax = 256;
3536 ConfigValueUInt(deviceName, NULL, "slots", &device->AuxiliaryEffectSlotMax);
3537 if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
3539 ConfigValueUInt(deviceName, NULL, "sends", &device->NumAuxSends);
3540 if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
3542 device->NumStereoSources = 1;
3543 device->NumMonoSources = device->SourcesMax - device->NumStereoSources;
3545 // Find a playback device to open
3546 if((err=V(device->Backend,open)(deviceName)) != ALC_NO_ERROR)
3548 DELETE_OBJ(device->Backend);
3549 al_free(device);
3550 alcSetError(NULL, err);
3551 return NULL;
3553 almtx_init(&device->BackendLock, almtx_plain);
3555 if(ConfigValueStr(al_string_get_cstr(device->DeviceName), NULL, "ambi-format", &fmt))
3557 if(strcasecmp(fmt, "fuma") == 0)
3558 device->AmbiFmt = AmbiFormat_FuMa;
3559 else if(strcasecmp(fmt, "acn+sn3d") == 0)
3560 device->AmbiFmt = AmbiFormat_ACN_SN3D;
3561 else if(strcasecmp(fmt, "acn+n3d") == 0)
3562 device->AmbiFmt = AmbiFormat_ACN_N3D;
3563 else
3564 ERR("Unsupported ambi-format: %s\n", fmt);
3567 if(DefaultEffect.type != AL_EFFECT_NULL)
3569 device->DefaultSlot = (ALeffectslot*)device->_slot_mem;
3570 if(InitEffectSlot(device->DefaultSlot) != AL_NO_ERROR)
3572 device->DefaultSlot = NULL;
3573 ERR("Failed to initialize the default effect slot\n");
3575 else
3577 aluInitEffectPanning(device->DefaultSlot);
3578 if(InitializeEffect(device, device->DefaultSlot, &DefaultEffect) != AL_NO_ERROR)
3580 DeinitEffectSlot(device->DefaultSlot);
3581 device->DefaultSlot = NULL;
3582 ERR("Failed to initialize the default effect\n");
3588 ALCdevice *head = ATOMIC_LOAD(&DeviceList);
3589 do {
3590 device->next = head;
3591 } while(!ATOMIC_COMPARE_EXCHANGE_WEAK(ALCdevice*, &DeviceList, &head, device));
3594 TRACE("Created device %p, \"%s\"\n", device, al_string_get_cstr(device->DeviceName));
3595 return device;
3598 /* alcCloseDevice
3600 * Closes the given device.
3602 ALC_API ALCboolean ALC_APIENTRY alcCloseDevice(ALCdevice *device)
3604 ALCdevice *list, *origdev, *nextdev;
3605 ALCcontext *ctx;
3607 LockLists();
3608 list = ATOMIC_LOAD(&DeviceList);
3609 do {
3610 if(list == device)
3611 break;
3612 } while((list=list->next) != NULL);
3613 if(!list || list->Type == Capture)
3615 alcSetError(list, ALC_INVALID_DEVICE);
3616 UnlockLists();
3617 return ALC_FALSE;
3619 almtx_lock(&device->BackendLock);
3621 origdev = device;
3622 nextdev = device->next;
3623 if(!ATOMIC_COMPARE_EXCHANGE_STRONG(ALCdevice*, &DeviceList, &origdev, nextdev))
3625 do {
3626 list = origdev;
3627 origdev = device;
3628 } while(!COMPARE_EXCHANGE(&list->next, &origdev, nextdev));
3630 UnlockLists();
3632 ctx = ATOMIC_LOAD(&device->ContextList);
3633 while(ctx != NULL)
3635 ALCcontext *next = ctx->next;
3636 WARN("Releasing context %p\n", ctx);
3637 ReleaseContext(ctx, device);
3638 ctx = next;
3640 if((device->Flags&DEVICE_RUNNING))
3641 V0(device->Backend,stop)();
3642 device->Flags &= ~DEVICE_RUNNING;
3643 almtx_unlock(&device->BackendLock);
3645 ALCdevice_DecRef(device);
3647 return ALC_TRUE;
3651 /************************************************
3652 * ALC capture functions
3653 ************************************************/
3654 ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei samples)
3656 ALCdevice *device = NULL;
3657 ALCenum err;
3659 DO_INITCONFIG();
3661 if(!CaptureBackend.name)
3663 alcSetError(NULL, ALC_INVALID_VALUE);
3664 return NULL;
3667 if(samples <= 0)
3669 alcSetError(NULL, ALC_INVALID_VALUE);
3670 return NULL;
3673 if(deviceName && (!deviceName[0] || strcasecmp(deviceName, alcDefaultName) == 0 || strcasecmp(deviceName, "openal-soft") == 0))
3674 deviceName = NULL;
3676 device = al_calloc(16, sizeof(ALCdevice));
3677 if(!device)
3679 alcSetError(NULL, ALC_OUT_OF_MEMORY);
3680 return NULL;
3683 //Validate device
3684 InitRef(&device->ref, 1);
3685 device->Connected = ALC_TRUE;
3686 device->Type = Capture;
3688 VECTOR_INIT(device->Hrtf.List);
3689 AL_STRING_INIT(device->Hrtf.Name);
3691 AL_STRING_INIT(device->DeviceName);
3692 device->Dry.Buffer = NULL;
3693 device->Dry.NumChannels = 0;
3694 device->FOAOut.Buffer = NULL;
3695 device->FOAOut.NumChannels = 0;
3696 device->RealOut.Buffer = NULL;
3697 device->RealOut.NumChannels = 0;
3699 InitUIntMap(&device->BufferMap, ~0);
3700 InitUIntMap(&device->EffectMap, ~0);
3701 InitUIntMap(&device->FilterMap, ~0);
3703 if(!CaptureBackend.getFactory)
3704 device->Backend = create_backend_wrapper(device, &CaptureBackend.Funcs,
3705 ALCbackend_Capture);
3706 else
3708 ALCbackendFactory *factory = CaptureBackend.getFactory();
3709 device->Backend = V(factory,createBackend)(device, ALCbackend_Capture);
3711 if(!device->Backend)
3713 al_free(device);
3714 alcSetError(NULL, ALC_OUT_OF_MEMORY);
3715 return NULL;
3718 device->Flags |= DEVICE_FREQUENCY_REQUEST;
3719 device->Frequency = frequency;
3721 device->Flags |= DEVICE_CHANNELS_REQUEST | DEVICE_SAMPLE_TYPE_REQUEST;
3722 if(DecomposeDevFormat(format, &device->FmtChans, &device->FmtType) == AL_FALSE)
3724 al_free(device);
3725 alcSetError(NULL, ALC_INVALID_ENUM);
3726 return NULL;
3728 device->IsHeadphones = AL_FALSE;
3729 device->AmbiFmt = AmbiFormat_Default;
3731 device->UpdateSize = samples;
3732 device->NumUpdates = 1;
3734 if((err=V(device->Backend,open)(deviceName)) != ALC_NO_ERROR)
3736 al_free(device);
3737 alcSetError(NULL, err);
3738 return NULL;
3740 almtx_init(&device->BackendLock, almtx_plain);
3743 ALCdevice *head = ATOMIC_LOAD(&DeviceList);
3744 do {
3745 device->next = head;
3746 } while(!ATOMIC_COMPARE_EXCHANGE_WEAK(ALCdevice*, &DeviceList, &head, device));
3749 TRACE("Created device %p, \"%s\"\n", device, al_string_get_cstr(device->DeviceName));
3750 return device;
3753 ALC_API ALCboolean ALC_APIENTRY alcCaptureCloseDevice(ALCdevice *device)
3755 ALCdevice *list, *next, *nextdev;
3757 LockLists();
3758 list = ATOMIC_LOAD(&DeviceList);
3759 do {
3760 if(list == device)
3761 break;
3762 } while((list=list->next) != NULL);
3763 if(!list || list->Type != Capture)
3765 alcSetError(list, ALC_INVALID_DEVICE);
3766 UnlockLists();
3767 return ALC_FALSE;
3770 next = device;
3771 nextdev = device->next;
3772 if(!ATOMIC_COMPARE_EXCHANGE_STRONG(ALCdevice*, &DeviceList, &next, nextdev))
3774 do {
3775 list = next;
3776 next = device;
3777 } while(!COMPARE_EXCHANGE(&list->next, &next, nextdev));
3779 UnlockLists();
3781 ALCdevice_DecRef(device);
3783 return ALC_TRUE;
3786 ALC_API void ALC_APIENTRY alcCaptureStart(ALCdevice *device)
3788 if(!VerifyDevice(&device) || device->Type != Capture)
3789 alcSetError(device, ALC_INVALID_DEVICE);
3790 else
3792 almtx_lock(&device->BackendLock);
3793 if(!device->Connected)
3794 alcSetError(device, ALC_INVALID_DEVICE);
3795 else if(!(device->Flags&DEVICE_RUNNING))
3797 if(V0(device->Backend,start)())
3798 device->Flags |= DEVICE_RUNNING;
3799 else
3801 aluHandleDisconnect(device);
3802 alcSetError(device, ALC_INVALID_DEVICE);
3805 almtx_unlock(&device->BackendLock);
3808 if(device) ALCdevice_DecRef(device);
3811 ALC_API void ALC_APIENTRY alcCaptureStop(ALCdevice *device)
3813 if(!VerifyDevice(&device) || device->Type != Capture)
3814 alcSetError(device, ALC_INVALID_DEVICE);
3815 else
3817 almtx_lock(&device->BackendLock);
3818 if((device->Flags&DEVICE_RUNNING))
3819 V0(device->Backend,stop)();
3820 device->Flags &= ~DEVICE_RUNNING;
3821 almtx_unlock(&device->BackendLock);
3824 if(device) ALCdevice_DecRef(device);
3827 ALC_API void ALC_APIENTRY alcCaptureSamples(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
3829 if(!VerifyDevice(&device) || device->Type != Capture)
3830 alcSetError(device, ALC_INVALID_DEVICE);
3831 else
3833 ALCenum err = ALC_INVALID_VALUE;
3835 almtx_lock(&device->BackendLock);
3836 if(samples >= 0 && V0(device->Backend,availableSamples)() >= (ALCuint)samples)
3837 err = V(device->Backend,captureSamples)(buffer, samples);
3838 almtx_unlock(&device->BackendLock);
3840 if(err != ALC_NO_ERROR)
3841 alcSetError(device, err);
3843 if(device) ALCdevice_DecRef(device);
3847 /************************************************
3848 * ALC loopback functions
3849 ************************************************/
3851 /* alcLoopbackOpenDeviceSOFT
3853 * Open a loopback device, for manual rendering.
3855 ALC_API ALCdevice* ALC_APIENTRY alcLoopbackOpenDeviceSOFT(const ALCchar *deviceName)
3857 ALCbackendFactory *factory;
3858 ALCdevice *device;
3860 DO_INITCONFIG();
3862 /* Make sure the device name, if specified, is us. */
3863 if(deviceName && strcmp(deviceName, alcDefaultName) != 0)
3865 alcSetError(NULL, ALC_INVALID_VALUE);
3866 return NULL;
3869 device = al_calloc(16, sizeof(ALCdevice));
3870 if(!device)
3872 alcSetError(NULL, ALC_OUT_OF_MEMORY);
3873 return NULL;
3876 //Validate device
3877 InitRef(&device->ref, 1);
3878 device->Connected = ALC_TRUE;
3879 device->Type = Loopback;
3880 ATOMIC_INIT(&device->LastError, ALC_NO_ERROR);
3882 device->Flags = 0;
3883 VECTOR_INIT(device->Hrtf.List);
3884 AL_STRING_INIT(device->Hrtf.Name);
3885 device->Bs2b = NULL;
3886 device->Uhj_Encoder = NULL;
3887 device->Render_Mode = NormalRender;
3888 AL_STRING_INIT(device->DeviceName);
3889 device->Dry.Buffer = NULL;
3890 device->Dry.NumChannels = 0;
3891 device->FOAOut.Buffer = NULL;
3892 device->FOAOut.NumChannels = 0;
3893 device->RealOut.Buffer = NULL;
3894 device->RealOut.NumChannels = 0;
3896 ATOMIC_INIT(&device->ContextList, NULL);
3898 device->ClockBase = 0;
3899 device->SamplesDone = 0;
3901 device->SourcesMax = 256;
3902 device->AuxiliaryEffectSlotMax = 4;
3903 device->NumAuxSends = MAX_SENDS;
3905 InitUIntMap(&device->BufferMap, ~0);
3906 InitUIntMap(&device->EffectMap, ~0);
3907 InitUIntMap(&device->FilterMap, ~0);
3909 factory = ALCloopbackFactory_getFactory();
3910 device->Backend = V(factory,createBackend)(device, ALCbackend_Loopback);
3911 if(!device->Backend)
3913 al_free(device);
3914 alcSetError(NULL, ALC_OUT_OF_MEMORY);
3915 return NULL;
3917 almtx_init(&device->BackendLock, almtx_plain);
3919 //Set output format
3920 device->NumUpdates = 0;
3921 device->UpdateSize = 0;
3923 device->Frequency = DEFAULT_OUTPUT_RATE;
3924 device->FmtChans = DevFmtChannelsDefault;
3925 device->FmtType = DevFmtTypeDefault;
3926 device->IsHeadphones = AL_FALSE;
3927 device->AmbiFmt = AmbiFormat_Default;
3929 ConfigValueUInt(NULL, NULL, "sources", &device->SourcesMax);
3930 if(device->SourcesMax == 0) device->SourcesMax = 256;
3932 ConfigValueUInt(NULL, NULL, "slots", &device->AuxiliaryEffectSlotMax);
3933 if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
3935 ConfigValueUInt(NULL, NULL, "sends", &device->NumAuxSends);
3936 if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
3938 device->NumStereoSources = 1;
3939 device->NumMonoSources = device->SourcesMax - device->NumStereoSources;
3941 // Open the "backend"
3942 V(device->Backend,open)("Loopback");
3945 ALCdevice *head = ATOMIC_LOAD(&DeviceList);
3946 do {
3947 device->next = head;
3948 } while(!ATOMIC_COMPARE_EXCHANGE_WEAK(ALCdevice*, &DeviceList, &head, device));
3951 TRACE("Created device %p\n", device);
3952 return device;
3955 /* alcIsRenderFormatSupportedSOFT
3957 * Determines if the loopback device supports the given format for rendering.
3959 ALC_API ALCboolean ALC_APIENTRY alcIsRenderFormatSupportedSOFT(ALCdevice *device, ALCsizei freq, ALCenum channels, ALCenum type)
3961 ALCboolean ret = ALC_FALSE;
3963 if(!VerifyDevice(&device) || device->Type != Loopback)
3964 alcSetError(device, ALC_INVALID_DEVICE);
3965 else if(freq <= 0)
3966 alcSetError(device, ALC_INVALID_VALUE);
3967 else
3969 if(IsValidALCType(type) && BytesFromDevFmt(type) > 0 &&
3970 IsValidALCChannels(channels) && ChannelsFromDevFmt(channels) > 0 &&
3971 freq >= MIN_OUTPUT_RATE)
3972 ret = ALC_TRUE;
3974 if(device) ALCdevice_DecRef(device);
3976 return ret;
3979 /* alcRenderSamplesSOFT
3981 * Renders some samples into a buffer, using the format last set by the
3982 * attributes given to alcCreateContext.
3984 FORCE_ALIGN ALC_API void ALC_APIENTRY alcRenderSamplesSOFT(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
3986 if(!VerifyDevice(&device) || device->Type != Loopback)
3987 alcSetError(device, ALC_INVALID_DEVICE);
3988 else if(samples < 0 || (samples > 0 && buffer == NULL))
3989 alcSetError(device, ALC_INVALID_VALUE);
3990 else
3991 aluMixData(device, buffer, samples);
3992 if(device) ALCdevice_DecRef(device);
3996 /************************************************
3997 * ALC DSP pause/resume functions
3998 ************************************************/
4000 /* alcDevicePauseSOFT
4002 * Pause the DSP to stop audio processing.
4004 ALC_API void ALC_APIENTRY alcDevicePauseSOFT(ALCdevice *device)
4006 if(!VerifyDevice(&device) || device->Type != Playback)
4007 alcSetError(device, ALC_INVALID_DEVICE);
4008 else
4010 almtx_lock(&device->BackendLock);
4011 if((device->Flags&DEVICE_RUNNING))
4012 V0(device->Backend,stop)();
4013 device->Flags &= ~DEVICE_RUNNING;
4014 device->Flags |= DEVICE_PAUSED;
4015 almtx_unlock(&device->BackendLock);
4017 if(device) ALCdevice_DecRef(device);
4020 /* alcDeviceResumeSOFT
4022 * Resume the DSP to restart audio processing.
4024 ALC_API void ALC_APIENTRY alcDeviceResumeSOFT(ALCdevice *device)
4026 if(!VerifyDevice(&device) || device->Type != Playback)
4027 alcSetError(device, ALC_INVALID_DEVICE);
4028 else
4030 almtx_lock(&device->BackendLock);
4031 if((device->Flags&DEVICE_PAUSED))
4033 device->Flags &= ~DEVICE_PAUSED;
4034 if(ATOMIC_LOAD(&device->ContextList) != NULL)
4036 if(V0(device->Backend,start)() != ALC_FALSE)
4037 device->Flags |= DEVICE_RUNNING;
4038 else
4040 alcSetError(device, ALC_INVALID_DEVICE);
4041 V0(device->Backend,lock)();
4042 aluHandleDisconnect(device);
4043 V0(device->Backend,unlock)();
4047 almtx_unlock(&device->BackendLock);
4049 if(device) ALCdevice_DecRef(device);
4053 /************************************************
4054 * ALC HRTF functions
4055 ************************************************/
4057 /* alcGetStringiSOFT
4059 * Gets a string parameter at the given index.
4061 ALC_API const ALCchar* ALC_APIENTRY alcGetStringiSOFT(ALCdevice *device, ALCenum paramName, ALCsizei index)
4063 const ALCchar *str = NULL;
4065 if(!VerifyDevice(&device) || device->Type == Capture)
4066 alcSetError(device, ALC_INVALID_DEVICE);
4067 else switch(paramName)
4069 case ALC_HRTF_SPECIFIER_SOFT:
4070 if(index >= 0 && (size_t)index < VECTOR_SIZE(device->Hrtf.List))
4071 str = al_string_get_cstr(VECTOR_ELEM(device->Hrtf.List, index).name);
4072 else
4073 alcSetError(device, ALC_INVALID_VALUE);
4074 break;
4076 default:
4077 alcSetError(device, ALC_INVALID_ENUM);
4078 break;
4080 if(device) ALCdevice_DecRef(device);
4082 return str;
4085 /* alcResetDeviceSOFT
4087 * Resets the given device output, using the specified attribute list.
4089 ALC_API ALCboolean ALC_APIENTRY alcResetDeviceSOFT(ALCdevice *device, const ALCint *attribs)
4091 ALCenum err;
4093 LockLists();
4094 if(!VerifyDevice(&device) || device->Type == Capture || !device->Connected)
4096 UnlockLists();
4097 alcSetError(device, ALC_INVALID_DEVICE);
4098 if(device) ALCdevice_DecRef(device);
4099 return ALC_FALSE;
4101 almtx_lock(&device->BackendLock);
4102 UnlockLists();
4104 err = UpdateDeviceParams(device, attribs);
4105 almtx_unlock(&device->BackendLock);
4107 if(err != ALC_NO_ERROR)
4109 alcSetError(device, err);
4110 if(err == ALC_INVALID_DEVICE)
4112 V0(device->Backend,lock)();
4113 aluHandleDisconnect(device);
4114 V0(device->Backend,unlock)();
4116 ALCdevice_DecRef(device);
4117 return ALC_FALSE;
4119 ALCdevice_DecRef(device);
4121 return ALC_TRUE;