2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
36 #include "alAuxEffectSlot.h"
37 #include "alDatabuffer.h"
42 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
43 typedef struct BackendInfo
{
45 void (*Init
)(BackendFuncs
*);
50 static BackendInfo BackendList
[] = {
51 #ifdef HAVE_PULSEAUDIO
52 { "pulse", alc_pulse_init
, alc_pulse_deinit
, alc_pulse_probe
, EmptyFuncs
},
55 { "alsa", alc_alsa_init
, alc_alsa_deinit
, alc_alsa_probe
, EmptyFuncs
},
58 { "oss", alc_oss_init
, alc_oss_deinit
, alc_oss_probe
, EmptyFuncs
},
61 { "solaris", alc_solaris_init
, alc_solaris_deinit
, alc_solaris_probe
, EmptyFuncs
},
64 { "dsound", alcDSoundInit
, alcDSoundDeinit
, alcDSoundProbe
, EmptyFuncs
},
67 { "winmm", alcWinMMInit
, alcWinMMDeinit
, alcWinMMProbe
, EmptyFuncs
},
70 { "port", alc_pa_init
, alc_pa_deinit
, alc_pa_probe
, EmptyFuncs
},
73 { "wave", alc_wave_init
, alc_wave_deinit
, alc_wave_probe
, EmptyFuncs
},
75 { NULL
, NULL
, NULL
, NULL
, EmptyFuncs
}
79 ///////////////////////////////////////////////////////
81 #define ALC_EFX_MAJOR_VERSION 0x20001
82 #define ALC_EFX_MINOR_VERSION 0x20002
83 #define ALC_MAX_AUXILIARY_SENDS 0x20003
85 ///////////////////////////////////////////////////////
86 // STRING and EXTENSIONS
88 typedef struct ALCfunction
{
89 const ALCchar
*funcName
;
93 typedef struct ALCenums
{
94 const ALCchar
*enumName
;
99 static const ALCfunction alcFunctions
[] = {
100 { "alcCreateContext", (ALCvoid
*) alcCreateContext
},
101 { "alcMakeContextCurrent", (ALCvoid
*) alcMakeContextCurrent
},
102 { "alcProcessContext", (ALCvoid
*) alcProcessContext
},
103 { "alcSuspendContext", (ALCvoid
*) alcSuspendContext
},
104 { "alcDestroyContext", (ALCvoid
*) alcDestroyContext
},
105 { "alcGetCurrentContext", (ALCvoid
*) alcGetCurrentContext
},
106 { "alcGetContextsDevice", (ALCvoid
*) alcGetContextsDevice
},
107 { "alcOpenDevice", (ALCvoid
*) alcOpenDevice
},
108 { "alcCloseDevice", (ALCvoid
*) alcCloseDevice
},
109 { "alcGetError", (ALCvoid
*) alcGetError
},
110 { "alcIsExtensionPresent", (ALCvoid
*) alcIsExtensionPresent
},
111 { "alcGetProcAddress", (ALCvoid
*) alcGetProcAddress
},
112 { "alcGetEnumValue", (ALCvoid
*) alcGetEnumValue
},
113 { "alcGetString", (ALCvoid
*) alcGetString
},
114 { "alcGetIntegerv", (ALCvoid
*) alcGetIntegerv
},
115 { "alcCaptureOpenDevice", (ALCvoid
*) alcCaptureOpenDevice
},
116 { "alcCaptureCloseDevice", (ALCvoid
*) alcCaptureCloseDevice
},
117 { "alcCaptureStart", (ALCvoid
*) alcCaptureStart
},
118 { "alcCaptureStop", (ALCvoid
*) alcCaptureStop
},
119 { "alcCaptureSamples", (ALCvoid
*) alcCaptureSamples
},
121 { "alcSetThreadContext", (ALCvoid
*) alcSetThreadContext
},
122 { "alcGetThreadContext", (ALCvoid
*) alcGetThreadContext
},
124 { NULL
, (ALCvoid
*) NULL
}
127 static const ALCenums enumeration
[] = {
129 { "ALC_INVALID", ALC_INVALID
},
130 { "ALC_FALSE", ALC_FALSE
},
131 { "ALC_TRUE", ALC_TRUE
},
134 { "ALC_MAJOR_VERSION", ALC_MAJOR_VERSION
},
135 { "ALC_MINOR_VERSION", ALC_MINOR_VERSION
},
136 { "ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE
},
137 { "ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES
},
138 { "ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER
},
139 { "ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER
},
140 { "ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER
},
141 { "ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER
},
142 { "ALC_EXTENSIONS", ALC_EXTENSIONS
},
143 { "ALC_FREQUENCY", ALC_FREQUENCY
},
144 { "ALC_REFRESH", ALC_REFRESH
},
145 { "ALC_SYNC", ALC_SYNC
},
146 { "ALC_MONO_SOURCES", ALC_MONO_SOURCES
},
147 { "ALC_STEREO_SOURCES", ALC_STEREO_SOURCES
},
148 { "ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER
},
149 { "ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
},
150 { "ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES
},
153 { "ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION
},
154 { "ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION
},
155 { "ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS
},
158 { "ALC_NO_ERROR", ALC_NO_ERROR
},
159 { "ALC_INVALID_DEVICE", ALC_INVALID_DEVICE
},
160 { "ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT
},
161 { "ALC_INVALID_ENUM", ALC_INVALID_ENUM
},
162 { "ALC_INVALID_VALUE", ALC_INVALID_VALUE
},
163 { "ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY
},
167 static const ALCchar alcNoError
[] = "No Error";
168 static const ALCchar alcErrInvalidDevice
[] = "Invalid Device";
169 static const ALCchar alcErrInvalidContext
[] = "Invalid Context";
170 static const ALCchar alcErrInvalidEnum
[] = "Invalid Enum";
171 static const ALCchar alcErrInvalidValue
[] = "Invalid Value";
172 static const ALCchar alcErrOutOfMemory
[] = "Out of Memory";
174 /* Device lists. Sizes only include the first ending null character, not the
176 static ALCchar
*alcDeviceList
;
177 static size_t alcDeviceListSize
;
178 static ALCchar
*alcAllDeviceList
;
179 static size_t alcAllDeviceListSize
;
180 static ALCchar
*alcCaptureDeviceList
;
181 static size_t alcCaptureDeviceListSize
;
182 // Default is always the first in the list
183 static ALCchar
*alcDefaultDeviceSpecifier
;
184 static ALCchar
*alcDefaultAllDeviceSpecifier
;
185 static ALCchar
*alcCaptureDefaultDeviceSpecifier
;
188 static const ALCchar alcNoDeviceExtList
[] =
189 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
190 "ALC_EXTX_thread_local_context";
191 static const ALCchar alcExtensionList
[] =
192 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
193 "ALC_EXT_disconnect ALC_EXT_EFX ALC_EXTX_thread_local_context";
194 static const ALCint alcMajorVersion
= 1;
195 static const ALCint alcMinorVersion
= 1;
197 static const ALCint alcEFXMajorVersion
= 1;
198 static const ALCint alcEFXMinorVersion
= 0;
200 ///////////////////////////////////////////////////////
203 ///////////////////////////////////////////////////////
206 static ALCdevice
*g_pDeviceList
= NULL
;
207 static ALCuint g_ulDeviceCount
= 0;
209 static CRITICAL_SECTION g_csMutex
;
212 static ALCcontext
*g_pContextList
= NULL
;
213 static ALCuint g_ulContextCount
= 0;
215 // Thread-local current context
216 static tls_type LocalContext
;
217 // Process-wide current context
218 static ALCcontext
*GlobalContext
;
221 static ALCenum g_eLastContextError
= ALC_NO_ERROR
;
223 // Default context extensions
224 static const ALchar alExtList
[] =
225 "AL_EXTX_buffer_sub_data AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE "
226 "AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS "
227 "AL_EXT_MULAW AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET "
228 "AL_EXTX_sample_buffer_object AL_EXT_source_distance_model "
229 "AL_LOKI_quadriphonic";
231 // Mixing Priority Level
235 resampler_t DefaultResampler
;
237 ///////////////////////////////////////////////////////
240 ///////////////////////////////////////////////////////
241 // ALC Related helper functions
243 static void alc_init(void);
244 static void alc_deinit(void);
246 BOOL APIENTRY
DllMain(HANDLE hModule
,DWORD ul_reason_for_call
,LPVOID lpReserved
)
250 // Perform actions based on the reason for calling.
251 switch(ul_reason_for_call
)
253 case DLL_PROCESS_ATTACH
:
254 DisableThreadLibraryCalls(hModule
);
258 case DLL_PROCESS_DETACH
:
265 #ifdef HAVE_GCC_DESTRUCTOR
266 static void alc_init(void) __attribute__((constructor
));
267 static void alc_deinit(void) __attribute__((destructor
));
271 static void alc_init(void)
274 const char *devs
, *str
;
276 InitializeCriticalSection(&g_csMutex
);
280 tls_create(&LocalContext
);
282 RTPrioLevel
= GetConfigValueInt(NULL
, "rt-prio", 0);
284 DefaultResampler
= GetConfigValueInt(NULL
, "resampler", RESAMPLER_DEFAULT
);
285 if(DefaultResampler
>= RESAMPLER_MAX
|| DefaultResampler
<= RESAMPLER_MIN
)
286 DefaultResampler
= RESAMPLER_DEFAULT
;
288 devs
= GetConfigValue(NULL
, "drivers", "");
293 const char *next
= devs
;
299 next
= strchr(devs
, ',');
301 if(!devs
[0] || devs
[0] == ',')
308 len
= (next
? ((size_t)(next
-devs
)) : strlen(devs
));
309 for(n
= i
;BackendList
[n
].Init
;n
++)
311 if(len
== strlen(BackendList
[n
].name
) &&
312 strncmp(BackendList
[n
].name
, devs
, len
) == 0)
314 BackendInfo Bkp
= BackendList
[n
];
317 BackendList
[n
] = BackendList
[n
-1];
320 BackendList
[n
] = Bkp
;
330 BackendList
[i
].name
= NULL
;
331 BackendList
[i
].Init
= NULL
;
332 BackendList
[i
].Deinit
= NULL
;
333 BackendList
[i
].Probe
= NULL
;
337 for(i
= 0;BackendList
[i
].Init
;i
++)
338 BackendList
[i
].Init(&BackendList
[i
].Funcs
);
340 DuplicateStereo
= GetConfigValueBool(NULL
, "stereodup", 0);
342 str
= GetConfigValue(NULL
, "excludefx", "");
349 { "eaxreverb", EAXREVERB
},
350 { "reverb", REVERB
},
356 const char *next
= str
;
360 next
= strchr(str
, ',');
362 if(!str
[0] || next
== str
)
365 len
= (next
? ((size_t)(next
-str
)) : strlen(str
));
366 for(n
= 0;EffectList
[n
].name
;n
++)
368 if(len
== strlen(EffectList
[n
].name
) &&
369 strncmp(EffectList
[n
].name
, str
, len
) == 0)
370 DisabledEffects
[EffectList
[n
].type
] = AL_TRUE
;
376 static void alc_deinit(void)
382 for(i
= 0;BackendList
[i
].Deinit
;i
++)
383 BackendList
[i
].Deinit();
385 tls_delete(LocalContext
);
389 DeleteCriticalSection(&g_csMutex
);
393 static void ProbeDeviceList()
397 free(alcDeviceList
); alcDeviceList
= NULL
;
398 alcDeviceListSize
= 0;
400 for(i
= 0;BackendList
[i
].Probe
;i
++)
401 BackendList
[i
].Probe(DEVICE_PROBE
);
404 static void ProbeAllDeviceList()
408 free(alcAllDeviceList
); alcAllDeviceList
= NULL
;
409 alcAllDeviceListSize
= 0;
411 for(i
= 0;BackendList
[i
].Probe
;i
++)
412 BackendList
[i
].Probe(ALL_DEVICE_PROBE
);
415 static void ProbeCaptureDeviceList()
419 free(alcCaptureDeviceList
); alcCaptureDeviceList
= NULL
;
420 alcCaptureDeviceListSize
= 0;
422 for(i
= 0;BackendList
[i
].Probe
;i
++)
423 BackendList
[i
].Probe(CAPTURE_DEVICE_PROBE
);
427 #define DECL_APPEND_LIST_FUNC(type) \
428 void Append##type##List(const ALCchar *name) \
430 size_t len = strlen(name); \
436 temp = realloc(alc##type##List, alc##type##ListSize + len + 2); \
439 AL_PRINT("Realloc failed to add %s!\n", name); \
442 alc##type##List = temp; \
443 sprintf(alc##type##List+alc##type##ListSize, "%s", name); \
444 alc##type##ListSize += len+1; \
445 alc##type##List[alc##type##ListSize] = 0; \
448 DECL_APPEND_LIST_FUNC(Device
)
449 DECL_APPEND_LIST_FUNC(AllDevice
)
450 DECL_APPEND_LIST_FUNC(CaptureDevice
)
453 void al_print(const char *fname
, unsigned int line
, const char *fmt
, ...)
459 fn
= strrchr(fname
, '/');
460 if(!fn
) fn
= strrchr(fname
, '\\');;
464 i
= snprintf(str
, sizeof(str
), "AL lib: %s:%d: ", fn
, line
);
465 if(i
< (int)sizeof(str
) && i
> 0)
469 vsnprintf(str
+i
, sizeof(str
)-i
, fmt
, ap
);
472 str
[sizeof(str
)-1] = 0;
474 fprintf(stderr
, "%s", str
);
477 void EnableRTPrio(ALint level
)
483 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL
);
485 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL
);
486 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM)
487 struct sched_param param
;
491 /* Use the minimum real-time priority possible for now (on Linux this
492 * should be 1 for SCHED_RR) */
493 param
.sched_priority
= sched_get_priority_min(SCHED_RR
);
494 failed
= !!pthread_setschedparam(pthread_self(), SCHED_RR
, ¶m
);
498 param
.sched_priority
= 0;
499 failed
= !!pthread_setschedparam(pthread_self(), SCHED_OTHER
, ¶m
);
502 /* Real-time priority not available */
506 AL_PRINT("Failed to set priority level for thread\n");
513 Check pDevice is a valid Device pointer
515 static ALCboolean
IsDevice(ALCdevice
*pDevice
)
517 ALCdevice
*pTempDevice
;
519 SuspendContext(NULL
);
521 pTempDevice
= g_pDeviceList
;
522 while(pTempDevice
&& pTempDevice
!= pDevice
)
523 pTempDevice
= pTempDevice
->next
;
525 ProcessContext(NULL
);
527 return (pTempDevice
? ALC_TRUE
: ALC_FALSE
);
533 Check pContext is a valid Context pointer
535 static ALCboolean
IsContext(ALCcontext
*pContext
)
537 ALCcontext
*pTempContext
;
539 SuspendContext(NULL
);
541 pTempContext
= g_pContextList
;
542 while (pTempContext
&& pTempContext
!= pContext
)
543 pTempContext
= pTempContext
->next
;
545 ProcessContext(NULL
);
547 return (pTempContext
? ALC_TRUE
: ALC_FALSE
);
554 Store latest ALC Error
556 ALCvoid
alcSetError(ALCdevice
*device
, ALenum errorCode
)
559 device
->LastError
= errorCode
;
561 g_eLastContextError
= errorCode
;
570 ALCvoid
SuspendContext(ALCcontext
*pContext
)
573 EnterCriticalSection(&g_csMutex
);
582 ALCvoid
ProcessContext(ALCcontext
*pContext
)
585 LeaveCriticalSection(&g_csMutex
);
592 Returns the currently active Context, in a locked state
594 ALCcontext
*GetContextSuspended(void)
596 ALCcontext
*pContext
= NULL
;
598 SuspendContext(NULL
);
600 pContext
= tls_get(LocalContext
);
601 if(pContext
&& !IsContext(pContext
))
603 tls_set(LocalContext
, NULL
);
607 pContext
= GlobalContext
;
610 SuspendContext(pContext
);
612 ProcessContext(NULL
);
621 Initialize Context variables
623 static ALvoid
InitContext(ALCcontext
*pContext
)
625 //Initialise listener
626 pContext
->Listener
.Gain
= 1.0f
;
627 pContext
->Listener
.MetersPerUnit
= 1.0f
;
628 pContext
->Listener
.Position
[0] = 0.0f
;
629 pContext
->Listener
.Position
[1] = 0.0f
;
630 pContext
->Listener
.Position
[2] = 0.0f
;
631 pContext
->Listener
.Velocity
[0] = 0.0f
;
632 pContext
->Listener
.Velocity
[1] = 0.0f
;
633 pContext
->Listener
.Velocity
[2] = 0.0f
;
634 pContext
->Listener
.Forward
[0] = 0.0f
;
635 pContext
->Listener
.Forward
[1] = 0.0f
;
636 pContext
->Listener
.Forward
[2] = -1.0f
;
637 pContext
->Listener
.Up
[0] = 0.0f
;
638 pContext
->Listener
.Up
[1] = 1.0f
;
639 pContext
->Listener
.Up
[2] = 0.0f
;
642 pContext
->LastError
= AL_NO_ERROR
;
643 pContext
->Suspended
= AL_FALSE
;
646 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
647 pContext
->SourceDistanceModel
= AL_FALSE
;
648 pContext
->DopplerFactor
= 1.0f
;
649 pContext
->DopplerVelocity
= 1.0f
;
650 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
652 pContext
->ExtensionList
= alExtList
;
659 Clean up Context, destroy any remaining Sources
661 static ALCvoid
ExitContext(ALCcontext
*pContext
)
664 pContext
->LastError
= AL_NO_ERROR
;
667 ///////////////////////////////////////////////////////
670 ///////////////////////////////////////////////////////
671 // ALC Functions calls
674 // This should probably move to another c file but for now ...
675 ALC_API ALCdevice
* ALC_APIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
677 ALCboolean DeviceFound
= ALC_FALSE
;
678 ALCdevice
*device
= NULL
;
683 alcSetError(NULL
, ALC_INVALID_VALUE
);
687 if(deviceName
&& !deviceName
[0])
690 device
= calloc(1, sizeof(ALCdevice
));
693 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
698 device
->Connected
= ALC_TRUE
;
699 device
->IsCaptureDevice
= AL_TRUE
;
701 device
->szDeviceName
= NULL
;
703 device
->Frequency
= frequency
;
704 device
->Format
= format
;
705 device
->UpdateSize
= SampleSize
;
706 device
->NumUpdates
= 1;
708 SuspendContext(NULL
);
709 for(i
= 0;BackendList
[i
].Init
;i
++)
711 device
->Funcs
= &BackendList
[i
].Funcs
;
712 if(ALCdevice_OpenCapture(device
, deviceName
))
714 device
->next
= g_pDeviceList
;
715 g_pDeviceList
= device
;
718 DeviceFound
= ALC_TRUE
;
722 ProcessContext(NULL
);
726 alcSetError(NULL
, ALC_INVALID_VALUE
);
734 ALC_API ALCboolean ALC_APIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
738 if(!IsDevice(pDevice
) || !pDevice
->IsCaptureDevice
)
740 alcSetError(pDevice
, ALC_INVALID_DEVICE
);
744 SuspendContext(NULL
);
746 list
= &g_pDeviceList
;
747 while(*list
!= pDevice
)
748 list
= &(*list
)->next
;
750 *list
= (*list
)->next
;
753 ProcessContext(NULL
);
755 ALCdevice_CloseCapture(pDevice
);
757 free(pDevice
->szDeviceName
);
758 pDevice
->szDeviceName
= NULL
;
765 ALC_API
void ALC_APIENTRY
alcCaptureStart(ALCdevice
*device
)
767 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
769 alcSetError(device
, ALC_INVALID_DEVICE
);
772 SuspendContext(NULL
);
773 ALCdevice_StartCapture(device
);
774 ProcessContext(NULL
);
777 ALC_API
void ALC_APIENTRY
alcCaptureStop(ALCdevice
*device
)
779 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
781 alcSetError(device
, ALC_INVALID_DEVICE
);
784 SuspendContext(NULL
);
785 ALCdevice_StopCapture(device
);
786 ProcessContext(NULL
);
789 ALC_API
void ALC_APIENTRY
alcCaptureSamples(ALCdevice
*device
, ALCvoid
*buffer
, ALCsizei samples
)
791 if(!IsDevice(device
) || !device
->IsCaptureDevice
)
793 alcSetError(device
, ALC_INVALID_DEVICE
);
796 SuspendContext(NULL
);
797 ALCdevice_CaptureSamples(device
, buffer
, samples
);
798 ProcessContext(NULL
);
804 Return last ALC generated error code
806 ALC_API ALCenum ALC_APIENTRY
alcGetError(ALCdevice
*device
)
808 ALCenum errorCode
= ALC_NO_ERROR
;
812 errorCode
= device
->LastError
;
813 device
->LastError
= ALC_NO_ERROR
;
817 errorCode
= g_eLastContextError
;
818 g_eLastContextError
= ALC_NO_ERROR
;
829 ALC_API ALCvoid ALC_APIENTRY
alcSuspendContext(ALCcontext
*pContext
)
831 SuspendContext(NULL
);
832 if(IsContext(pContext
))
833 pContext
->Suspended
= AL_TRUE
;
834 ProcessContext(NULL
);
843 ALC_API ALCvoid ALC_APIENTRY
alcProcessContext(ALCcontext
*pContext
)
845 SuspendContext(NULL
);
846 if(IsContext(pContext
))
847 pContext
->Suspended
= AL_FALSE
;
848 ProcessContext(NULL
);
855 Returns information about the Device, and error strings
857 ALC_API
const ALCchar
* ALC_APIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
859 const ALCchar
*value
= NULL
;
867 case ALC_INVALID_ENUM
:
868 value
= alcErrInvalidEnum
;
871 case ALC_INVALID_VALUE
:
872 value
= alcErrInvalidValue
;
875 case ALC_INVALID_DEVICE
:
876 value
= alcErrInvalidDevice
;
879 case ALC_INVALID_CONTEXT
:
880 value
= alcErrInvalidContext
;
883 case ALC_OUT_OF_MEMORY
:
884 value
= alcErrOutOfMemory
;
887 case ALC_DEVICE_SPECIFIER
:
888 if(IsDevice(pDevice
))
889 value
= pDevice
->szDeviceName
;
893 value
= alcDeviceList
;
897 case ALC_ALL_DEVICES_SPECIFIER
:
898 ProbeAllDeviceList();
899 value
= alcAllDeviceList
;
902 case ALC_CAPTURE_DEVICE_SPECIFIER
:
903 if(IsDevice(pDevice
))
904 value
= pDevice
->szDeviceName
;
907 ProbeCaptureDeviceList();
908 value
= alcCaptureDeviceList
;
912 /* Default devices are always first in the list */
913 case ALC_DEFAULT_DEVICE_SPECIFIER
:
917 free(alcDefaultDeviceSpecifier
);
918 alcDefaultDeviceSpecifier
= strdup(alcDeviceList
? alcDeviceList
: "");
919 if(!alcDefaultDeviceSpecifier
)
920 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
921 value
= alcDefaultDeviceSpecifier
;
924 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
925 if(!alcAllDeviceList
)
926 ProbeAllDeviceList();
928 free(alcDefaultAllDeviceSpecifier
);
929 alcDefaultAllDeviceSpecifier
= strdup(alcAllDeviceList
?
930 alcAllDeviceList
: "");
931 if(!alcDefaultAllDeviceSpecifier
)
932 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
933 value
= alcDefaultAllDeviceSpecifier
;
936 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
937 if(!alcCaptureDeviceList
)
938 ProbeCaptureDeviceList();
940 free(alcCaptureDefaultDeviceSpecifier
);
941 alcCaptureDefaultDeviceSpecifier
= strdup(alcCaptureDeviceList
?
942 alcCaptureDeviceList
: "");
943 if(!alcCaptureDefaultDeviceSpecifier
)
944 alcSetError(pDevice
, ALC_OUT_OF_MEMORY
);
945 value
= alcCaptureDefaultDeviceSpecifier
;
949 if(IsDevice(pDevice
))
950 value
= alcExtensionList
;
952 value
= alcNoDeviceExtList
;
956 alcSetError(pDevice
, ALC_INVALID_ENUM
);
967 Returns information about the Device and the version of Open AL
969 ALC_API ALCvoid ALC_APIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
971 if(size
== 0 || data
== NULL
)
973 alcSetError(device
, ALC_INVALID_VALUE
);
977 if(IsDevice(device
) && device
->IsCaptureDevice
)
979 SuspendContext(NULL
);
984 case ALC_CAPTURE_SAMPLES
:
985 *data
= ALCdevice_AvailableSamples(device
);
989 *data
= device
->Connected
;
993 alcSetError(device
, ALC_INVALID_ENUM
);
997 ProcessContext(NULL
);
1004 case ALC_MAJOR_VERSION
:
1005 *data
= alcMajorVersion
;
1008 case ALC_MINOR_VERSION
:
1009 *data
= alcMinorVersion
;
1012 case ALC_EFX_MAJOR_VERSION
:
1013 *data
= alcEFXMajorVersion
;
1016 case ALC_EFX_MINOR_VERSION
:
1017 *data
= alcEFXMinorVersion
;
1020 case ALC_MAX_AUXILIARY_SENDS
:
1021 if(!IsDevice(device
))
1022 alcSetError(device
, ALC_INVALID_DEVICE
);
1024 *data
= device
->NumAuxSends
;
1027 case ALC_ATTRIBUTES_SIZE
:
1028 if(!IsDevice(device
))
1029 alcSetError(device
, ALC_INVALID_DEVICE
);
1034 case ALC_ALL_ATTRIBUTES
:
1035 if(!IsDevice(device
))
1036 alcSetError(device
, ALC_INVALID_DEVICE
);
1038 alcSetError(device
, ALC_INVALID_VALUE
);
1043 SuspendContext(NULL
);
1044 data
[i
++] = ALC_FREQUENCY
;
1045 data
[i
++] = device
->Frequency
;
1047 data
[i
++] = ALC_REFRESH
;
1048 data
[i
++] = device
->Frequency
/ device
->UpdateSize
;
1050 data
[i
++] = ALC_SYNC
;
1051 data
[i
++] = ALC_FALSE
;
1053 data
[i
++] = ALC_MONO_SOURCES
;
1054 data
[i
++] = device
->lNumMonoSources
;
1056 data
[i
++] = ALC_STEREO_SOURCES
;
1057 data
[i
++] = device
->lNumStereoSources
;
1059 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
1060 data
[i
++] = device
->NumAuxSends
;
1063 ProcessContext(NULL
);
1068 if(!IsDevice(device
))
1069 alcSetError(device
, ALC_INVALID_DEVICE
);
1071 *data
= device
->Frequency
;
1075 if(!IsDevice(device
))
1076 alcSetError(device
, ALC_INVALID_DEVICE
);
1078 *data
= device
->Frequency
/ device
->UpdateSize
;
1082 if(!IsDevice(device
))
1083 alcSetError(device
, ALC_INVALID_DEVICE
);
1088 case ALC_MONO_SOURCES
:
1089 if(!IsDevice(device
))
1090 alcSetError(device
, ALC_INVALID_DEVICE
);
1092 *data
= device
->lNumMonoSources
;
1095 case ALC_STEREO_SOURCES
:
1096 if(!IsDevice(device
))
1097 alcSetError(device
, ALC_INVALID_DEVICE
);
1099 *data
= device
->lNumStereoSources
;
1103 if(!IsDevice(device
))
1104 alcSetError(device
, ALC_INVALID_DEVICE
);
1106 *data
= device
->Connected
;
1110 alcSetError(device
, ALC_INVALID_ENUM
);
1117 alcIsExtensionPresent
1119 Determines if there is support for a particular extension
1121 ALC_API ALCboolean ALC_APIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
1123 ALCboolean bResult
= ALC_FALSE
;
1129 alcSetError(device
, ALC_INVALID_VALUE
);
1133 len
= strlen(extName
);
1134 ptr
= (IsDevice(device
) ? alcExtensionList
: alcNoDeviceExtList
);
1137 if(strncasecmp(ptr
, extName
, len
) == 0 &&
1138 (ptr
[len
] == '\0' || isspace(ptr
[len
])))
1143 if((ptr
=strchr(ptr
, ' ')) != NULL
)
1147 } while(isspace(*ptr
));
1158 Retrieves the function address for a particular extension function
1160 ALC_API ALCvoid
* ALC_APIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
1166 alcSetError(device
, ALC_INVALID_VALUE
);
1170 while(alcFunctions
[i
].funcName
&& strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
1172 return alcFunctions
[i
].address
;
1179 Get the value for a particular ALC Enumerated Value
1181 ALC_API ALCenum ALC_APIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
1186 while(enumeration
[i
].enumName
&& strcmp(enumeration
[i
].enumName
,enumName
) == 0)
1188 val
= enumeration
[i
].value
;
1190 if(!enumeration
[i
].enumName
)
1191 alcSetError(device
, ALC_INVALID_VALUE
);
1200 Create and attach a Context to a particular Device.
1202 ALC_API ALCcontext
* ALC_APIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
1204 ALuint attrIdx
, reqStereoSources
;
1205 ALCcontext
*ALContext
;
1209 SuspendContext(NULL
);
1211 if(!IsDevice(device
) || device
->IsCaptureDevice
|| !device
->Connected
)
1213 alcSetError(device
, ALC_INVALID_DEVICE
);
1214 ProcessContext(NULL
);
1218 // Reset Context Last Error code
1219 device
->LastError
= ALC_NO_ERROR
;
1221 // If a context is already running on the device, stop playback so the
1222 // device attributes can be updated
1223 if(device
->NumContexts
> 0)
1225 ProcessContext(NULL
);
1226 ALCdevice_StopPlayback(device
);
1227 SuspendContext(NULL
);
1230 // Check for attributes
1233 ALCint level
= device
->Bs2bLevel
;
1234 ALCuint freq
= device
->Frequency
;
1235 ALCint numMono
= device
->lNumMonoSources
;
1236 ALCint numStereo
= device
->lNumStereoSources
;
1237 ALCuint numSends
= device
->NumAuxSends
;
1240 while(attrList
[attrIdx
])
1242 if(attrList
[attrIdx
] == ALC_FREQUENCY
&&
1243 !ConfigValueExists(NULL
, "frequency"))
1245 freq
= attrList
[attrIdx
+ 1];
1250 if(attrList
[attrIdx
] == ALC_STEREO_SOURCES
)
1252 reqStereoSources
= attrList
[attrIdx
+ 1];
1253 if(reqStereoSources
> device
->MaxNoOfSources
)
1254 reqStereoSources
= device
->MaxNoOfSources
;
1256 numStereo
= reqStereoSources
;
1257 numMono
= device
->MaxNoOfSources
- numStereo
;
1260 if(attrList
[attrIdx
] == ALC_MAX_AUXILIARY_SENDS
&&
1261 !ConfigValueExists(NULL
, "sends"))
1263 numSends
= attrList
[attrIdx
+ 1];
1264 if(numSends
> MAX_SENDS
)
1265 numSends
= MAX_SENDS
;
1271 device
->UpdateSize
= (ALuint64
)device
->UpdateSize
* freq
/
1274 device
->Bs2bLevel
= level
;
1275 device
->Frequency
= freq
;
1276 device
->lNumMonoSources
= numMono
;
1277 device
->lNumStereoSources
= numStereo
;
1278 device
->NumAuxSends
= numSends
;
1281 if(ALCdevice_ResetPlayback(device
) == ALC_FALSE
)
1283 alcSetError(device
, ALC_INVALID_DEVICE
);
1284 aluHandleDisconnect(device
);
1285 ProcessContext(NULL
);
1289 for(i
= 0;i
< device
->NumContexts
;i
++)
1291 ALCcontext
*context
= device
->Contexts
[i
];
1295 SuspendContext(context
);
1296 for(slot
= context
->EffectSlotList
;slot
!= NULL
;slot
= slot
->next
)
1298 if(!slot
->EffectState
)
1301 if(ALEffect_DeviceUpdate(slot
->EffectState
, device
) == AL_FALSE
)
1303 alcSetError(device
, ALC_INVALID_DEVICE
);
1304 aluHandleDisconnect(device
);
1305 ProcessContext(context
);
1306 ProcessContext(NULL
);
1307 ALCdevice_StopPlayback(device
);
1310 ALEffect_Update(slot
->EffectState
, context
, &slot
->effect
);
1313 for(source
= context
->SourceList
;source
!= NULL
;source
= source
->next
)
1315 ALuint s
= device
->NumAuxSends
;
1316 while(s
< MAX_SENDS
)
1318 if(source
->Send
[s
].Slot
)
1319 source
->Send
[s
].Slot
->refcount
--;
1320 source
->Send
[s
].Slot
= NULL
;
1321 source
->Send
[s
].WetFilter
.type
= 0;
1322 source
->Send
[s
].WetFilter
.filter
= 0;
1325 source
->NeedsUpdate
= AL_TRUE
;
1327 aluInitPanning(context
);
1328 ProcessContext(context
);
1331 if(device
->Bs2bLevel
> 0 && device
->Bs2bLevel
<= 6)
1335 device
->Bs2b
= calloc(1, sizeof(*device
->Bs2b
));
1336 bs2b_clear(device
->Bs2b
);
1338 bs2b_set_srate(device
->Bs2b
, device
->Frequency
);
1339 bs2b_set_level(device
->Bs2b
, device
->Bs2bLevel
);
1344 device
->Bs2b
= NULL
;
1347 temp
= realloc(device
->Contexts
, (device
->NumContexts
+1) * sizeof(*device
->Contexts
));
1350 alcSetError(device
, ALC_OUT_OF_MEMORY
);
1351 ProcessContext(NULL
);
1354 device
->Contexts
= temp
;
1356 ALContext
= calloc(1, sizeof(ALCcontext
));
1359 alcSetError(device
, ALC_OUT_OF_MEMORY
);
1360 ProcessContext(NULL
);
1364 device
->Contexts
[device
->NumContexts
++] = ALContext
;
1365 ALContext
->Device
= device
;
1367 InitContext(ALContext
);
1368 aluInitPanning(ALContext
);
1370 ALContext
->next
= g_pContextList
;
1371 g_pContextList
= ALContext
;
1374 ProcessContext(NULL
);
1385 ALC_API ALCvoid ALC_APIENTRY
alcDestroyContext(ALCcontext
*context
)
1391 if(!IsContext(context
))
1393 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1397 Device
= context
->Device
;
1399 if(Device
->NumContexts
== 1)
1400 ALCdevice_StopPlayback(Device
);
1402 SuspendContext(NULL
);
1404 if(context
== GlobalContext
)
1405 GlobalContext
= NULL
;
1407 for(i
= 0;i
< Device
->NumContexts
-1;i
++)
1409 if(Device
->Contexts
[i
] == context
)
1411 Device
->Contexts
[i
] = Device
->Contexts
[Device
->NumContexts
-1];
1415 Device
->NumContexts
--;
1418 SuspendContext(context
);
1420 if(context
->SourceCount
> 0)
1423 AL_PRINT("alcDestroyContext(): deleting %d Source(s)\n", context
->SourceCount
);
1425 ReleaseALSources(context
);
1427 if(context
->EffectSlotCount
> 0)
1430 AL_PRINT("alcDestroyContext(): deleting %d AuxiliaryEffectSlot(s)\n", context
->EffectSlotCount
);
1432 ReleaseALAuxiliaryEffectSlots(context
);
1435 list
= &g_pContextList
;
1436 while(*list
!= context
)
1437 list
= &(*list
)->next
;
1439 *list
= (*list
)->next
;
1443 ProcessContext(context
);
1444 ProcessContext(NULL
);
1446 ExitContext(context
);
1448 // Free memory (MUST do this after ProcessContext)
1449 memset(context
, 0, sizeof(ALCcontext
));
1455 alcGetCurrentContext
1457 Returns the currently active Context
1459 ALC_API ALCcontext
* ALC_APIENTRY
alcGetCurrentContext(ALCvoid
)
1461 ALCcontext
*pContext
;
1463 if((pContext
=GetContextSuspended()) != NULL
)
1464 ProcessContext(pContext
);
1472 Returns the currently active thread-local Context
1474 ALC_API ALCcontext
* ALC_APIENTRY
alcGetThreadContext(void)
1476 ALCcontext
*pContext
= NULL
;
1478 SuspendContext(NULL
);
1480 pContext
= tls_get(LocalContext
);
1481 if(pContext
&& !IsContext(pContext
))
1483 tls_set(LocalContext
, NULL
);
1487 ProcessContext(NULL
);
1494 alcGetContextsDevice
1496 Returns the Device that a particular Context is attached to
1498 ALC_API ALCdevice
* ALC_APIENTRY
alcGetContextsDevice(ALCcontext
*pContext
)
1500 ALCdevice
*pDevice
= NULL
;
1502 SuspendContext(NULL
);
1503 if(IsContext(pContext
))
1504 pDevice
= pContext
->Device
;
1506 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1507 ProcessContext(NULL
);
1514 alcMakeContextCurrent
1516 Makes the given Context the active Context
1518 ALC_API ALCboolean ALC_APIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
1520 ALboolean bReturn
= AL_TRUE
;
1522 SuspendContext(NULL
);
1524 // context must be a valid Context or NULL
1525 if(context
== NULL
|| IsContext(context
))
1527 GlobalContext
= context
;
1528 tls_set(LocalContext
, NULL
);
1532 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1536 ProcessContext(NULL
);
1544 Makes the given Context the active Context for the current thread
1546 ALC_API ALCboolean ALC_APIENTRY
alcSetThreadContext(ALCcontext
*context
)
1548 ALboolean bReturn
= AL_TRUE
;
1550 SuspendContext(NULL
);
1552 // context must be a valid Context or NULL
1553 if(context
== NULL
|| IsContext(context
))
1554 tls_set(LocalContext
, context
);
1557 alcSetError(NULL
, ALC_INVALID_CONTEXT
);
1561 ProcessContext(NULL
);
1567 // Sets the default channel order used by most non-WaveFormatEx-based APIs
1568 void SetDefaultChannelOrder(ALCdevice
*device
)
1570 switch(aluChannelsFromFormat(device
->Format
))
1572 case 1: device
->DevChannels
[0] = FRONT_CENTER
; break;
1574 case 2: device
->DevChannels
[0] = FRONT_LEFT
;
1575 device
->DevChannels
[1] = FRONT_RIGHT
; break;
1577 case 4: device
->DevChannels
[0] = FRONT_LEFT
;
1578 device
->DevChannels
[1] = FRONT_RIGHT
;
1579 device
->DevChannels
[2] = BACK_LEFT
;
1580 device
->DevChannels
[3] = BACK_RIGHT
; break;
1582 case 6: device
->DevChannels
[0] = FRONT_LEFT
;
1583 device
->DevChannels
[1] = FRONT_RIGHT
;
1584 device
->DevChannels
[2] = BACK_LEFT
;
1585 device
->DevChannels
[3] = BACK_RIGHT
;
1586 device
->DevChannels
[4] = FRONT_CENTER
;
1587 device
->DevChannels
[5] = LFE
; break;
1589 case 7: device
->DevChannels
[0] = FRONT_LEFT
;
1590 device
->DevChannels
[1] = FRONT_RIGHT
;
1591 device
->DevChannels
[2] = FRONT_CENTER
;
1592 device
->DevChannels
[3] = LFE
;
1593 device
->DevChannels
[4] = BACK_CENTER
;
1594 device
->DevChannels
[5] = SIDE_LEFT
;
1595 device
->DevChannels
[6] = SIDE_RIGHT
; break;
1597 case 8: device
->DevChannels
[0] = FRONT_LEFT
;
1598 device
->DevChannels
[1] = FRONT_RIGHT
;
1599 device
->DevChannels
[2] = BACK_LEFT
;
1600 device
->DevChannels
[3] = BACK_RIGHT
;
1601 device
->DevChannels
[4] = FRONT_CENTER
;
1602 device
->DevChannels
[5] = LFE
;
1603 device
->DevChannels
[6] = SIDE_LEFT
;
1604 device
->DevChannels
[7] = SIDE_RIGHT
; break;
1607 // Sets the default order used by WaveFormatEx
1608 void SetDefaultWFXChannelOrder(ALCdevice
*device
)
1610 switch(aluChannelsFromFormat(device
->Format
))
1612 case 1: device
->DevChannels
[0] = FRONT_CENTER
; break;
1614 case 2: device
->DevChannels
[0] = FRONT_LEFT
;
1615 device
->DevChannels
[1] = FRONT_RIGHT
; break;
1617 case 4: device
->DevChannels
[0] = FRONT_LEFT
;
1618 device
->DevChannels
[1] = FRONT_RIGHT
;
1619 device
->DevChannels
[2] = BACK_LEFT
;
1620 device
->DevChannels
[3] = BACK_RIGHT
; break;
1622 case 6: device
->DevChannels
[0] = FRONT_LEFT
;
1623 device
->DevChannels
[1] = FRONT_RIGHT
;
1624 device
->DevChannels
[2] = FRONT_CENTER
;
1625 device
->DevChannels
[3] = LFE
;
1626 device
->DevChannels
[4] = BACK_LEFT
;
1627 device
->DevChannels
[5] = BACK_RIGHT
; break;
1629 case 7: device
->DevChannels
[0] = FRONT_LEFT
;
1630 device
->DevChannels
[1] = FRONT_RIGHT
;
1631 device
->DevChannels
[2] = FRONT_CENTER
;
1632 device
->DevChannels
[3] = LFE
;
1633 device
->DevChannels
[4] = BACK_CENTER
;
1634 device
->DevChannels
[5] = SIDE_LEFT
;
1635 device
->DevChannels
[6] = SIDE_RIGHT
; break;
1637 case 8: device
->DevChannels
[0] = FRONT_LEFT
;
1638 device
->DevChannels
[1] = FRONT_RIGHT
;
1639 device
->DevChannels
[2] = FRONT_CENTER
;
1640 device
->DevChannels
[3] = LFE
;
1641 device
->DevChannels
[4] = BACK_LEFT
;
1642 device
->DevChannels
[5] = BACK_RIGHT
;
1643 device
->DevChannels
[6] = SIDE_LEFT
;
1644 device
->DevChannels
[7] = SIDE_RIGHT
; break;
1648 static ALenum
GetFormatFromString(const char *str
)
1650 if(strcasecmp(str
, "AL_FORMAT_MONO32") == 0) return AL_FORMAT_MONO_FLOAT32
;
1651 if(strcasecmp(str
, "AL_FORMAT_STEREO32") == 0) return AL_FORMAT_STEREO_FLOAT32
;
1652 if(strcasecmp(str
, "AL_FORMAT_QUAD32") == 0) return AL_FORMAT_QUAD32
;
1653 if(strcasecmp(str
, "AL_FORMAT_51CHN32") == 0) return AL_FORMAT_51CHN32
;
1654 if(strcasecmp(str
, "AL_FORMAT_61CHN32") == 0) return AL_FORMAT_61CHN32
;
1655 if(strcasecmp(str
, "AL_FORMAT_71CHN32") == 0) return AL_FORMAT_71CHN32
;
1657 if(strcasecmp(str
, "AL_FORMAT_MONO16") == 0) return AL_FORMAT_MONO16
;
1658 if(strcasecmp(str
, "AL_FORMAT_STEREO16") == 0) return AL_FORMAT_STEREO16
;
1659 if(strcasecmp(str
, "AL_FORMAT_QUAD16") == 0) return AL_FORMAT_QUAD16
;
1660 if(strcasecmp(str
, "AL_FORMAT_51CHN16") == 0) return AL_FORMAT_51CHN16
;
1661 if(strcasecmp(str
, "AL_FORMAT_61CHN16") == 0) return AL_FORMAT_61CHN16
;
1662 if(strcasecmp(str
, "AL_FORMAT_71CHN16") == 0) return AL_FORMAT_71CHN16
;
1664 if(strcasecmp(str
, "AL_FORMAT_MONO8") == 0) return AL_FORMAT_MONO8
;
1665 if(strcasecmp(str
, "AL_FORMAT_STEREO8") == 0) return AL_FORMAT_STEREO8
;
1666 if(strcasecmp(str
, "AL_FORMAT_QUAD8") == 0) return AL_FORMAT_QUAD8
;
1667 if(strcasecmp(str
, "AL_FORMAT_51CHN8") == 0) return AL_FORMAT_51CHN8
;
1668 if(strcasecmp(str
, "AL_FORMAT_61CHN8") == 0) return AL_FORMAT_61CHN8
;
1669 if(strcasecmp(str
, "AL_FORMAT_71CHN8") == 0) return AL_FORMAT_71CHN8
;
1671 AL_PRINT("Unknown format: \"%s\"\n", str
);
1672 return AL_FORMAT_STEREO16
;
1678 Open the Device specified.
1680 ALC_API ALCdevice
* ALC_APIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
1682 ALboolean bDeviceFound
= AL_FALSE
;
1687 if(deviceName
&& !deviceName
[0])
1690 device
= calloc(1, sizeof(ALCdevice
));
1693 alcSetError(NULL
, ALC_OUT_OF_MEMORY
);
1698 device
->Connected
= ALC_TRUE
;
1699 device
->IsCaptureDevice
= AL_FALSE
;
1700 device
->LastError
= ALC_NO_ERROR
;
1702 device
->Bs2b
= NULL
;
1703 device
->szDeviceName
= NULL
;
1705 device
->Contexts
= NULL
;
1706 device
->NumContexts
= 0;
1709 device
->Frequency
= GetConfigValueInt(NULL
, "frequency", SWMIXER_OUTPUT_RATE
);
1710 if(device
->Frequency
< 8000)
1711 device
->Frequency
= 8000;
1713 fmt
= GetConfigValue(NULL
, "format", "AL_FORMAT_STEREO16");
1714 device
->Format
= GetFormatFromString(fmt
);
1716 device
->NumUpdates
= GetConfigValueInt(NULL
, "periods", 4);
1717 if(device
->NumUpdates
< 2)
1718 device
->NumUpdates
= 4;
1720 i
= GetConfigValueInt(NULL
, "refresh", 4096);
1721 if(i
<= 0) i
= 4096;
1723 device
->UpdateSize
= GetConfigValueInt(NULL
, "period_size", i
/device
->NumUpdates
);
1724 if(device
->UpdateSize
<= 0)
1725 device
->UpdateSize
= i
/device
->NumUpdates
;
1727 device
->MaxNoOfSources
= GetConfigValueInt(NULL
, "sources", 256);
1728 if((ALint
)device
->MaxNoOfSources
<= 0)
1729 device
->MaxNoOfSources
= 256;
1731 device
->AuxiliaryEffectSlotMax
= GetConfigValueInt(NULL
, "slots", 4);
1732 if((ALint
)device
->AuxiliaryEffectSlotMax
<= 0)
1733 device
->AuxiliaryEffectSlotMax
= 4;
1735 device
->lNumStereoSources
= 1;
1736 device
->lNumMonoSources
= device
->MaxNoOfSources
- device
->lNumStereoSources
;
1738 device
->NumAuxSends
= GetConfigValueInt(NULL
, "sends", MAX_SENDS
);
1739 if(device
->NumAuxSends
> MAX_SENDS
)
1740 device
->NumAuxSends
= MAX_SENDS
;
1742 device
->Bs2bLevel
= GetConfigValueInt(NULL
, "cf_level", 0);
1744 if(aluChannelsFromFormat(device
->Format
) <= 2)
1746 device
->HeadDampen
= GetConfigValueFloat(NULL
, "head_dampen", DEFAULT_HEAD_DAMPEN
);
1747 device
->HeadDampen
= __min(device
->HeadDampen
, 1.0f
);
1748 device
->HeadDampen
= __max(device
->HeadDampen
, 0.0f
);
1751 device
->HeadDampen
= 0.0f
;
1753 // Find a playback device to open
1754 SuspendContext(NULL
);
1755 for(i
= 0;BackendList
[i
].Init
;i
++)
1757 device
->Funcs
= &BackendList
[i
].Funcs
;
1758 if(ALCdevice_OpenPlayback(device
, deviceName
))
1760 device
->next
= g_pDeviceList
;
1761 g_pDeviceList
= device
;
1764 bDeviceFound
= AL_TRUE
;
1768 ProcessContext(NULL
);
1772 // No suitable output device found
1773 alcSetError(NULL
, ALC_INVALID_VALUE
);
1785 Close the specified Device
1787 ALC_API ALCboolean ALC_APIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
1791 if(!IsDevice(pDevice
) || pDevice
->IsCaptureDevice
)
1793 alcSetError(pDevice
, ALC_INVALID_DEVICE
);
1797 SuspendContext(NULL
);
1799 list
= &g_pDeviceList
;
1800 while(*list
!= pDevice
)
1801 list
= &(*list
)->next
;
1803 *list
= (*list
)->next
;
1806 ProcessContext(NULL
);
1808 if(pDevice
->NumContexts
> 0)
1811 AL_PRINT("alcCloseDevice(): destroying %u Context(s)\n", pDevice
->NumContexts
);
1813 while(pDevice
->NumContexts
> 0)
1814 alcDestroyContext(pDevice
->Contexts
[0]);
1816 ALCdevice_ClosePlayback(pDevice
);
1818 if(pDevice
->BufferCount
> 0)
1821 AL_PRINT("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice
->BufferCount
);
1823 ReleaseALBuffers(pDevice
);
1825 if(pDevice
->EffectCount
> 0)
1828 AL_PRINT("alcCloseDevice(): deleting %d Effect(s)\n", pDevice
->EffectCount
);
1830 ReleaseALEffects(pDevice
);
1832 if(pDevice
->FilterCount
> 0)
1835 AL_PRINT("alcCloseDevice(): deleting %d Filter(s)\n", pDevice
->FilterCount
);
1837 ReleaseALFilters(pDevice
);
1839 if(pDevice
->DatabufferCount
> 0)
1842 AL_PRINT("alcCloseDevice(): deleting %d Databuffer(s)\n", pDevice
->DatabufferCount
);
1844 ReleaseALDatabuffers(pDevice
);
1847 free(pDevice
->Bs2b
);
1848 pDevice
->Bs2b
= NULL
;
1850 free(pDevice
->szDeviceName
);
1851 pDevice
->szDeviceName
= NULL
;
1853 free(pDevice
->Contexts
);
1854 pDevice
->Contexts
= NULL
;
1856 //Release device structure
1857 memset(pDevice
, 0, sizeof(ALCdevice
));
1864 ALCvoid
ReleaseALC(ALCvoid
)
1866 free(alcDeviceList
); alcDeviceList
= NULL
;
1867 alcDeviceListSize
= 0;
1868 free(alcAllDeviceList
); alcAllDeviceList
= NULL
;
1869 alcAllDeviceListSize
= 0;
1870 free(alcCaptureDeviceList
); alcCaptureDeviceList
= NULL
;
1871 alcCaptureDeviceListSize
= 0;
1873 free(alcDefaultDeviceSpecifier
);
1874 alcDefaultDeviceSpecifier
= NULL
;
1875 free(alcDefaultAllDeviceSpecifier
);
1876 alcDefaultAllDeviceSpecifier
= NULL
;
1877 free(alcCaptureDefaultDeviceSpecifier
);
1878 alcCaptureDefaultDeviceSpecifier
= NULL
;
1881 if(g_ulDeviceCount
> 0)
1882 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount
, (g_ulDeviceCount
>1)?"s":"");
1885 while(g_pDeviceList
)
1887 if(g_pDeviceList
->IsCaptureDevice
)
1888 alcCaptureCloseDevice(g_pDeviceList
);
1890 alcCloseDevice(g_pDeviceList
);
1894 ///////////////////////////////////////////////////////