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
21 #define _CRT_SECURE_NO_DEPRECATE // get rid of sprintf security warnings on VS2005
37 #include "alExtension.h"
38 #include "alAuxEffectSlot.h"
41 ///////////////////////////////////////////////////////
46 ///////////////////////////////////////////////////////
49 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
52 void (*Init
)(BackendFuncs
*);
56 { "alsa", alc_alsa_init
, EmptyFuncs
},
59 { "oss", alc_oss_init
, EmptyFuncs
},
62 { "dsound", alcDSoundInit
, EmptyFuncs
},
65 { "winmm", alcWinMMInit
, EmptyFuncs
},
68 { "wave", alc_wave_init
, EmptyFuncs
},
70 { NULL
, NULL
, EmptyFuncs
}
74 ///////////////////////////////////////////////////////
76 #define ALC_EFX_MAJOR_VERSION 0x20001
77 #define ALC_EFX_MINOR_VERSION 0x20002
78 #define ALC_MAX_AUXILIARY_SENDS 0x20003
80 ///////////////////////////////////////////////////////
81 // STRING and EXTENSIONS
83 typedef struct ALCfunction_struct
89 static ALCfunction alcFunctions
[] = {
90 { "alcCreateContext", (ALvoid
*) alcCreateContext
},
91 { "alcMakeContextCurrent", (ALvoid
*) alcMakeContextCurrent
},
92 { "alcProcessContext", (ALvoid
*) alcProcessContext
},
93 { "alcSuspendContext", (ALvoid
*) alcSuspendContext
},
94 { "alcDestroyContext", (ALvoid
*) alcDestroyContext
},
95 { "alcGetCurrentContext", (ALvoid
*) alcGetCurrentContext
},
96 { "alcGetContextsDevice", (ALvoid
*) alcGetContextsDevice
},
97 { "alcOpenDevice", (ALvoid
*) alcOpenDevice
},
98 { "alcCloseDevice", (ALvoid
*) alcCloseDevice
},
99 { "alcGetError", (ALvoid
*) alcGetError
},
100 { "alcIsExtensionPresent", (ALvoid
*) alcIsExtensionPresent
},
101 { "alcGetProcAddress", (ALvoid
*) alcGetProcAddress
},
102 { "alcGetEnumValue", (ALvoid
*) alcGetEnumValue
},
103 { "alcGetString", (ALvoid
*) alcGetString
},
104 { "alcGetIntegerv", (ALvoid
*) alcGetIntegerv
},
105 { "alcCaptureOpenDevice", (ALvoid
*) alcCaptureOpenDevice
},
106 { "alcCaptureCloseDevice", (ALvoid
*) alcCaptureCloseDevice
},
107 { "alcCaptureStart", (ALvoid
*) alcCaptureStart
},
108 { "alcCaptureStop", (ALvoid
*) alcCaptureStop
},
109 { "alcCaptureSamples", (ALvoid
*) alcCaptureSamples
},
110 { NULL
, (ALvoid
*) NULL
}
113 static ALenums enumeration
[]={
115 { (ALchar
*)"ALC_INVALID", ALC_INVALID
},
116 { (ALchar
*)"ALC_FALSE", ALC_FALSE
},
117 { (ALchar
*)"ALC_TRUE", ALC_TRUE
},
120 { (ALchar
*)"ALC_MAJOR_VERSION", ALC_MAJOR_VERSION
},
121 { (ALchar
*)"ALC_MINOR_VERSION", ALC_MINOR_VERSION
},
122 { (ALchar
*)"ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE
},
123 { (ALchar
*)"ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES
},
124 { (ALchar
*)"ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER
},
125 { (ALchar
*)"ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER
},
126 { (ALchar
*)"ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER
},
127 { (ALchar
*)"ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER
},
128 { (ALchar
*)"ALC_EXTENSIONS", ALC_EXTENSIONS
},
129 { (ALchar
*)"ALC_FREQUENCY", ALC_FREQUENCY
},
130 { (ALchar
*)"ALC_REFRESH", ALC_REFRESH
},
131 { (ALchar
*)"ALC_SYNC", ALC_SYNC
},
132 { (ALchar
*)"ALC_MONO_SOURCES", ALC_MONO_SOURCES
},
133 { (ALchar
*)"ALC_STEREO_SOURCES", ALC_STEREO_SOURCES
},
134 { (ALchar
*)"ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER
},
135 { (ALchar
*)"ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
},
136 { (ALchar
*)"ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES
},
139 { (ALchar
*)"ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION
},
140 { (ALchar
*)"ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION
},
141 { (ALchar
*)"ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS
},
144 { (ALchar
*)"ALC_NO_ERROR", ALC_NO_ERROR
},
145 { (ALchar
*)"ALC_INVALID_DEVICE", ALC_INVALID_DEVICE
},
146 { (ALchar
*)"ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT
},
147 { (ALchar
*)"ALC_INVALID_ENUM", ALC_INVALID_ENUM
},
148 { (ALchar
*)"ALC_INVALID_VALUE", ALC_INVALID_VALUE
},
149 { (ALchar
*)"ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY
},
150 { (ALchar
*)NULL
, (ALenum
)0 }
153 static const ALCchar alcNoError
[] = "No Error";
154 static const ALCchar alcErrInvalidDevice
[] = "Invalid Device";
155 static const ALCchar alcErrInvalidContext
[] = "Invalid Context";
156 static const ALCchar alcErrInvalidEnum
[] = "Invalid Enum";
157 static const ALCchar alcErrInvalidValue
[] = "Invalid Value";
158 static const ALCchar alcErrOutOfMemory
[] = "Out of Memory";
161 static ALCchar alcDeviceList
[2048];
162 static ALCchar alcAllDeviceList
[2048];
163 static ALCchar alcCaptureDeviceList
[2048];
164 // Default is always the first in the list
165 static ALCchar
*alcDefaultDeviceSpecifier
= alcDeviceList
;
166 static ALCchar
*alcDefaultAllDeviceSpecifier
= alcAllDeviceList
;
167 static ALCchar
*alcCaptureDefaultDeviceSpecifier
= alcCaptureDeviceList
;
170 static ALCchar alcExtensionList
[] = "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_EFX";
171 static ALCint alcMajorVersion
= 1;
172 static ALCint alcMinorVersion
= 1;
174 static ALCint alcEFXMajorVersion
= 1;
175 static ALCint alcEFXMinorVersion
= 0;
177 ///////////////////////////////////////////////////////
180 ///////////////////////////////////////////////////////
183 static ALCdevice
*g_pDeviceList
= NULL
;
184 static ALCuint g_ulDeviceCount
= 0;
186 static CRITICAL_SECTION g_csMutex
;
189 static ALCcontext
*g_pContextList
= NULL
;
190 static ALCuint g_ulContextCount
= 0;
193 static ALCenum g_eLastContextError
= ALC_NO_ERROR
;
195 ///////////////////////////////////////////////////////
198 ///////////////////////////////////////////////////////
199 // ALC Related helper functions
201 BOOL APIENTRY
DllMain(HANDLE hModule
,DWORD ul_reason_for_call
,LPVOID lpReserved
)
205 // Perform actions based on the reason for calling.
206 switch(ul_reason_for_call
)
208 case DLL_PROCESS_ATTACH
:
209 DisableThreadLibraryCalls(hModule
);
212 case DLL_PROCESS_DETACH
:
219 DeleteCriticalSection(&g_csMutex
);
225 #ifdef HAVE_GCC_DESTRUCTOR
226 static void my_deinit() __attribute__((destructor
));
227 static void my_deinit()
229 static ALenum once
= AL_FALSE
;
239 DeleteCriticalSection(&g_csMutex
);
244 static void InitAL(void)
250 const char *devs
, *str
;
254 InitializeCriticalSection(&g_csMutex
);
258 devs
= GetConfigValue(NULL
, "drivers", "");
263 const char *next
= devs
;
269 next
= strchr(devs
, ',');
271 if(!devs
[0] || devs
[0] == ',')
274 len
= (next
? ((size_t)(next
-devs
)) : strlen(devs
));
275 for(n
= i
;BackendList
[n
].Init
;n
++)
277 if(len
== strlen(BackendList
[n
].name
) &&
278 strncmp(BackendList
[n
].name
, devs
, len
) == 0)
280 const char *name
= BackendList
[i
].name
;
281 void (*Init
)(BackendFuncs
*) = BackendList
[i
].Init
;
283 BackendList
[i
].name
= BackendList
[n
].name
;
284 BackendList
[i
].Init
= BackendList
[n
].Init
;
286 BackendList
[n
].name
= name
;
287 BackendList
[n
].Init
= Init
;
294 BackendList
[i
].name
= NULL
;
295 BackendList
[i
].Init
= NULL
;
298 for(i
= 0;BackendList
[i
].Init
;i
++)
299 BackendList
[i
].Init(&BackendList
[i
].Funcs
);
301 str
= GetConfigValue(NULL
, "stereodup", "false");
302 DuplicateStereo
= (strcasecmp(str
, "true") == 0 ||
303 strcasecmp(str
, "yes") == 0 ||
304 strcasecmp(str
, "on") == 0 ||
309 ALCchar
*AppendDeviceList(char *name
)
312 ALCchar
*ret
= alcDeviceList
+pos
;
313 if(pos
>= sizeof(alcDeviceList
))
315 AL_PRINT("Not enough room to add %s!\n", name
);
316 return alcDeviceList
+ sizeof(alcDeviceList
) - 1;
318 pos
+= snprintf(alcDeviceList
+pos
, sizeof(alcDeviceList
)-pos
-1, "%s", name
) + 1;
322 ALCchar
*AppendAllDeviceList(char *name
)
325 ALCchar
*ret
= alcAllDeviceList
+pos
;
326 if(pos
>= sizeof(alcAllDeviceList
))
328 AL_PRINT("Not enough room to add %s!\n", name
);
329 return alcAllDeviceList
+ sizeof(alcAllDeviceList
) - 1;
331 pos
+= snprintf(alcAllDeviceList
+pos
, sizeof(alcAllDeviceList
)-pos
-1, "%s", name
) + 1;
335 ALCchar
*AppendCaptureDeviceList(char *name
)
338 ALCchar
*ret
= alcCaptureDeviceList
+pos
;
339 if(pos
>= sizeof(alcCaptureDeviceList
))
341 AL_PRINT("Not enough room to add %s!\n", name
);
342 return alcCaptureDeviceList
+ sizeof(alcCaptureDeviceList
) - 1;
344 pos
+= snprintf(alcCaptureDeviceList
+pos
, sizeof(alcCaptureDeviceList
)-pos
-1, "%s", name
) + 1;
351 Check pContext is a valid Context pointer
353 static ALCboolean
IsContext(ALCcontext
*pContext
)
355 ALCcontext
*pTempContext
;
357 pTempContext
= g_pContextList
;
358 while (pTempContext
&& pTempContext
!= pContext
)
359 pTempContext
= pTempContext
->next
;
361 return (pTempContext
? ALC_TRUE
: ALC_FALSE
);
368 Store latest ALC Error
370 ALCvoid
SetALCError(ALenum errorCode
)
372 g_eLastContextError
= errorCode
;
381 ALCvoid
SuspendContext(ALCcontext
*pContext
)
384 EnterCriticalSection(&g_csMutex
);
393 ALCvoid
ProcessContext(ALCcontext
*pContext
)
396 LeaveCriticalSection(&g_csMutex
);
403 Initialize Context variables
405 static ALvoid
InitContext(ALCcontext
*pContext
)
409 //Initialise listener
410 pContext
->Listener
.Gain
= 1.0f
;
411 pContext
->Listener
.MetersPerUnit
= 1.0f
;
412 pContext
->Listener
.Position
[0] = 0.0f
;
413 pContext
->Listener
.Position
[1] = 0.0f
;
414 pContext
->Listener
.Position
[2] = 0.0f
;
415 pContext
->Listener
.Velocity
[0] = 0.0f
;
416 pContext
->Listener
.Velocity
[1] = 0.0f
;
417 pContext
->Listener
.Velocity
[2] = 0.0f
;
418 pContext
->Listener
.Forward
[0] = 0.0f
;
419 pContext
->Listener
.Forward
[1] = 0.0f
;
420 pContext
->Listener
.Forward
[2] = -1.0f
;
421 pContext
->Listener
.Up
[0] = 0.0f
;
422 pContext
->Listener
.Up
[1] = 1.0f
;
423 pContext
->Listener
.Up
[2] = 0.0f
;
426 pContext
->LastError
= AL_NO_ERROR
;
427 pContext
->InUse
= AL_FALSE
;
430 pContext
->Frequency
= pContext
->Device
->Frequency
;
433 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
434 pContext
->DopplerFactor
= 1.0f
;
435 pContext
->DopplerVelocity
= 1.0f
;
436 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
438 pContext
->lNumStereoSources
= 1;
439 pContext
->lNumMonoSources
= pContext
->Device
->MaxNoOfSources
- pContext
->lNumStereoSources
;
441 strcpy(pContext
->ExtensionList
, "AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_OFFSET AL_LOKI_quadriphonic");
443 level
= GetConfigValueInt(NULL
, "cf_level", 0);
444 if(level
> 0 && level
<= 6)
446 pContext
->bs2b
= calloc(1, sizeof(*pContext
->bs2b
));
447 bs2b_set_srate(pContext
->bs2b
, pContext
->Frequency
);
448 bs2b_set_level(pContext
->bs2b
, level
);
456 Clean up Context, destroy any remaining Sources
458 static ALCvoid
ExitContext(ALCcontext
*pContext
)
461 pContext
->LastError
= AL_NO_ERROR
;
462 pContext
->InUse
= AL_FALSE
;
464 free(pContext
->bs2b
);
465 pContext
->bs2b
= NULL
;
468 ///////////////////////////////////////////////////////
471 ///////////////////////////////////////////////////////
472 // ALC Functions calls
475 // This should probably move to another c file but for now ...
476 ALCAPI ALCdevice
* ALCAPIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
478 ALCboolean DeviceFound
= ALC_FALSE
;
479 ALCdevice
*pDevice
= NULL
;
484 if(deviceName
&& !deviceName
[0])
487 pDevice
= malloc(sizeof(ALCdevice
));
492 //Initialise device structure
493 memset(pDevice
, 0, sizeof(ALCdevice
));
496 pDevice
->IsCaptureDevice
= AL_TRUE
;
498 pDevice
->Frequency
= frequency
;
499 pDevice
->Format
= format
;
501 for(i
= 0;BackendList
[i
].Init
;i
++)
503 pDevice
->Funcs
= &BackendList
[i
].Funcs
;
504 if(ALCdevice_OpenCapture(pDevice
, deviceName
, frequency
, format
, SampleSize
))
506 SuspendContext(NULL
);
507 pDevice
->next
= g_pDeviceList
;
508 g_pDeviceList
= pDevice
;
510 ProcessContext(NULL
);
512 DeviceFound
= ALC_TRUE
;
518 SetALCError(ALC_INVALID_VALUE
);
527 SetALCError(ALC_OUT_OF_MEMORY
);
532 ALCAPI ALCboolean ALCAPIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
534 ALCboolean bReturn
= ALC_FALSE
;
537 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
539 SuspendContext(NULL
);
541 list
= &g_pDeviceList
;
542 while(*list
!= pDevice
)
543 list
= &(*list
)->next
;
545 *list
= (*list
)->next
;
548 ProcessContext(NULL
);
550 ALCdevice_CloseCapture(pDevice
);
556 SetALCError(ALC_INVALID_DEVICE
);
561 ALCAPI
void ALCAPIENTRY
alcCaptureStart(ALCdevice
*pDevice
)
563 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
564 ALCdevice_StartCapture(pDevice
);
566 SetALCError(ALC_INVALID_DEVICE
);
569 ALCAPI
void ALCAPIENTRY
alcCaptureStop(ALCdevice
*pDevice
)
571 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
572 ALCdevice_StopCapture(pDevice
);
574 SetALCError(ALC_INVALID_DEVICE
);
577 ALCAPI
void ALCAPIENTRY
alcCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCsizei lSamples
)
579 if ((pDevice
) && (pDevice
->IsCaptureDevice
))
580 ALCdevice_CaptureSamples(pDevice
, pBuffer
, lSamples
);
582 SetALCError(ALC_INVALID_DEVICE
);
588 Return last ALC generated error code
590 ALCAPI ALCenum ALCAPIENTRY
alcGetError(ALCdevice
*device
)
596 errorCode
= g_eLastContextError
;
597 g_eLastContextError
= ALC_NO_ERROR
;
607 ALCAPI ALCvoid ALCAPIENTRY
alcSuspendContext(ALCcontext
*pContext
)
609 // Not a lot happens here !
619 ALCAPI ALCvoid ALCAPIENTRY
alcProcessContext(ALCcontext
*pContext
)
621 // Not a lot happens here !
629 Returns information about the Device, and error strings
631 ALCAPI
const ALCchar
* ALCAPIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
633 const ALCchar
*value
= NULL
;
643 case ALC_INVALID_ENUM
:
644 value
= alcErrInvalidEnum
;
647 case ALC_INVALID_VALUE
:
648 value
= alcErrInvalidValue
;
651 case ALC_INVALID_DEVICE
:
652 value
= alcErrInvalidDevice
;
655 case ALC_INVALID_CONTEXT
:
656 value
= alcErrInvalidContext
;
659 case ALC_OUT_OF_MEMORY
:
660 value
= alcErrOutOfMemory
;
663 case ALC_DEFAULT_DEVICE_SPECIFIER
:
664 value
= alcDefaultDeviceSpecifier
;
667 case ALC_DEVICE_SPECIFIER
:
669 value
= pDevice
->szDeviceName
;
671 value
= alcDeviceList
;
674 case ALC_ALL_DEVICES_SPECIFIER
:
675 value
= alcAllDeviceList
;
678 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
679 value
= alcDefaultAllDeviceSpecifier
;
682 case ALC_CAPTURE_DEVICE_SPECIFIER
:
684 value
= pDevice
->szDeviceName
;
686 value
= alcCaptureDeviceList
;
689 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
690 value
= alcCaptureDefaultDeviceSpecifier
;
694 value
= alcExtensionList
;
698 SetALCError(ALC_INVALID_ENUM
);
709 Returns information about the Device and the version of Open AL
711 ALCAPI ALCvoid ALCAPIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
715 if ((device
)&&(device
->IsCaptureDevice
))
717 SuspendContext(NULL
);
722 case ALC_CAPTURE_SAMPLES
:
723 if ((size
) && (data
))
724 *data
= ALCdevice_AvailableSamples(device
);
726 SetALCError(ALC_INVALID_VALUE
);
730 SetALCError(ALC_INVALID_ENUM
);
734 ProcessContext(NULL
);
743 case ALC_MAJOR_VERSION
:
745 SetALCError(ALC_INVALID_VALUE
);
747 *data
= alcMajorVersion
;
750 case ALC_MINOR_VERSION
:
752 SetALCError(ALC_INVALID_VALUE
);
754 *data
= alcMinorVersion
;
757 case ALC_EFX_MAJOR_VERSION
:
759 SetALCError(ALC_INVALID_VALUE
);
761 *data
= alcEFXMajorVersion
;
764 case ALC_EFX_MINOR_VERSION
:
766 SetALCError(ALC_INVALID_VALUE
);
768 *data
= alcEFXMinorVersion
;
771 case ALC_MAX_AUXILIARY_SENDS
:
773 SetALCError(ALC_INVALID_VALUE
);
778 case ALC_ATTRIBUTES_SIZE
:
780 SetALCError(ALC_INVALID_DEVICE
);
782 SetALCError(ALC_INVALID_VALUE
);
787 case ALC_ALL_ATTRIBUTES
:
789 SetALCError(ALC_INVALID_DEVICE
);
791 SetALCError(ALC_INVALID_VALUE
);
796 data
[i
++] = ALC_FREQUENCY
;
797 data
[i
++] = device
->Frequency
;
799 data
[i
++] = ALC_REFRESH
;
800 data
[i
++] = device
->Frequency
/ device
->UpdateSize
;
802 data
[i
++] = ALC_SYNC
;
803 data
[i
++] = ALC_FALSE
;
805 SuspendContext(NULL
);
806 if(device
->Context
&& size
>= 12)
808 data
[i
++] = ALC_MONO_SOURCES
;
809 data
[i
++] = device
->Context
->lNumMonoSources
;
811 data
[i
++] = ALC_STEREO_SOURCES
;
812 data
[i
++] = device
->Context
->lNumStereoSources
;
814 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
815 data
[i
++] = MAX_SENDS
;
817 ProcessContext(NULL
);
825 SetALCError(ALC_INVALID_DEVICE
);
827 SetALCError(ALC_INVALID_VALUE
);
829 *data
= device
->Frequency
;
834 SetALCError(ALC_INVALID_DEVICE
);
836 SetALCError(ALC_INVALID_VALUE
);
838 *data
= device
->Frequency
/ device
->UpdateSize
;
843 SetALCError(ALC_INVALID_DEVICE
);
845 SetALCError(ALC_INVALID_VALUE
);
850 case ALC_MONO_SOURCES
:
851 if(!device
|| !device
->Context
)
852 SetALCError(ALC_INVALID_DEVICE
);
854 SetALCError(ALC_INVALID_VALUE
);
856 *data
= device
->Context
->lNumMonoSources
;
859 case ALC_STEREO_SOURCES
:
860 if(!device
|| !device
->Context
)
861 SetALCError(ALC_INVALID_DEVICE
);
863 SetALCError(ALC_INVALID_VALUE
);
865 *data
= device
->Context
->lNumStereoSources
;
869 SetALCError(ALC_INVALID_ENUM
);
874 SetALCError(ALC_INVALID_VALUE
);
882 alcIsExtensionPresent
884 Determines if there is support for a particular extension
886 ALCAPI ALCboolean ALCAPIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
888 ALCboolean bResult
= ALC_FALSE
;
897 len
= strlen(extName
);
898 ptr
= alcExtensionList
;
901 if(strncasecmp(ptr
, extName
, len
) == 0 &&
902 (ptr
[len
] == '\0' || isspace(ptr
[len
])))
907 if((ptr
=strchr(ptr
, ' ')) != NULL
)
911 } while(isspace(*ptr
));
916 SetALCError(ALC_INVALID_VALUE
);
925 Retrieves the function address for a particular extension function
927 ALCAPI ALCvoid
* ALCAPIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
929 ALCvoid
*pFunction
= NULL
;
936 while(alcFunctions
[i
].funcName
&&
937 strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
939 pFunction
= alcFunctions
[i
].address
;
942 SetALCError(ALC_INVALID_VALUE
);
951 Get the value for a particular ALC Enumerated Value
953 ALCAPI ALCenum ALCAPIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
960 while ((enumeration
[i
].enumName
)&&(strcmp(enumeration
[i
].enumName
,enumName
)))
962 val
= enumeration
[i
].value
;
964 if(!enumeration
[i
].enumName
)
965 SetALCError(ALC_INVALID_VALUE
);
974 Create and attach a Context to a particular Device.
976 ALCAPI ALCcontext
* ALCAPIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
978 ALCcontext
*ALContext
= NULL
;
979 ALuint ulAttributeIndex
, ulRequestedStereoSources
;
981 if ((device
)&&(!device
->IsCaptureDevice
))
983 // Reset Context Last Error code
984 g_eLastContextError
= ALC_NO_ERROR
;
986 // Current implementation only allows one Context per Device
989 ALContext
= calloc(1, sizeof(ALCcontext
));
992 SetALCError(ALC_OUT_OF_MEMORY
);
996 ALContext
->Device
= device
;
997 InitContext(ALContext
);
999 device
->Context
= ALContext
;
1001 SuspendContext(NULL
);
1003 ALContext
->next
= g_pContextList
;
1004 g_pContextList
= ALContext
;
1007 ProcessContext(NULL
);
1009 // Check for Voice Count attributes
1012 ulAttributeIndex
= 0;
1013 while ((ulAttributeIndex
< 10) && (attrList
[ulAttributeIndex
]))
1015 if (attrList
[ulAttributeIndex
] == ALC_STEREO_SOURCES
)
1017 ulRequestedStereoSources
= attrList
[ulAttributeIndex
+ 1];
1019 if (ulRequestedStereoSources
> ALContext
->Device
->MaxNoOfSources
)
1020 ulRequestedStereoSources
= ALContext
->Device
->MaxNoOfSources
;
1022 ALContext
->lNumStereoSources
= ulRequestedStereoSources
;
1023 ALContext
->lNumMonoSources
= ALContext
->Device
->MaxNoOfSources
- ALContext
->lNumStereoSources
;
1027 ulAttributeIndex
+= 2;
1033 SetALCError(ALC_INVALID_VALUE
);
1038 SetALCError(ALC_INVALID_DEVICE
);
1049 ALCAPI ALCvoid ALCAPIENTRY
alcDestroyContext(ALCcontext
*context
)
1055 // Lock context list
1056 SuspendContext(NULL
);
1058 if (IsContext(context
))
1061 SuspendContext(context
);
1063 ReleaseALSources(context
);
1064 ReleaseALAuxiliaryEffectSlots(context
);
1066 context
->Device
->Context
= NULL
;
1068 list
= &g_pContextList
;
1069 while(*list
!= context
)
1070 list
= &(*list
)->next
;
1072 *list
= (*list
)->next
;
1076 ProcessContext(context
);
1078 ExitContext(context
);
1080 // Free memory (MUST do this after ProcessContext)
1081 memset(context
, 0, sizeof(ALCcontext
));
1085 SetALCError(ALC_INVALID_CONTEXT
);
1087 ProcessContext(NULL
);
1092 alcGetCurrentContext
1094 Returns the currently active Context
1096 ALCAPI ALCcontext
* ALCAPIENTRY
alcGetCurrentContext(ALCvoid
)
1098 ALCcontext
*pContext
= NULL
;
1102 SuspendContext(NULL
);
1104 pContext
= g_pContextList
;
1105 while ((pContext
) && (!pContext
->InUse
))
1106 pContext
= pContext
->next
;
1108 ProcessContext(NULL
);
1115 alcGetContextsDevice
1117 Returns the Device that a particular Context is attached to
1119 ALCAPI ALCdevice
* ALCAPIENTRY
alcGetContextsDevice(ALCcontext
*pContext
)
1121 ALCdevice
*pDevice
= NULL
;
1125 SuspendContext(NULL
);
1126 if (IsContext(pContext
))
1127 pDevice
= pContext
->Device
;
1129 SetALCError(ALC_INVALID_CONTEXT
);
1130 ProcessContext(NULL
);
1137 alcMakeContextCurrent
1139 Makes the given Context the active Context
1141 ALCAPI ALCboolean ALCAPIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
1143 ALCcontext
*ALContext
;
1144 ALboolean bReturn
= AL_TRUE
;
1148 SuspendContext(NULL
);
1150 // context must be a valid Context or NULL
1151 if ((IsContext(context
)) || (context
== NULL
))
1153 if ((ALContext
=alcGetCurrentContext()))
1155 SuspendContext(ALContext
);
1156 ALContext
->InUse
=AL_FALSE
;
1157 ProcessContext(ALContext
);
1160 if ((ALContext
=context
) && (ALContext
->Device
))
1162 SuspendContext(ALContext
);
1163 ALContext
->InUse
=AL_TRUE
;
1164 ProcessContext(ALContext
);
1169 SetALCError(ALC_INVALID_CONTEXT
);
1173 ProcessContext(NULL
);
1182 Open the Device specified.
1184 ALCAPI ALCdevice
* ALCAPIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
1186 ALboolean bDeviceFound
= AL_FALSE
;
1192 if(deviceName
&& !deviceName
[0])
1195 device
= malloc(sizeof(ALCdevice
));
1200 //Initialise device structure
1201 memset(device
, 0, sizeof(ALCdevice
));
1204 device
->IsCaptureDevice
= AL_FALSE
;
1207 device
->Frequency
= GetConfigValueInt(NULL
, "frequency", SWMIXER_OUTPUT_RATE
);
1208 if((ALint
)device
->Frequency
<= 0)
1209 device
->Frequency
= SWMIXER_OUTPUT_RATE
;
1211 fmt
= GetConfigValue(NULL
, "format", "AL_FORMAT_STEREO16");
1213 device
->Format
= alGetEnumValue(fmt
);
1215 if(!aluChannelsFromFormat(device
->Format
))
1216 device
->Format
= AL_FORMAT_STEREO16
;
1218 device
->UpdateSize
= GetConfigValueInt(NULL
, "refresh", 8192);
1219 if((ALint
)device
->UpdateSize
<= 0)
1220 device
->UpdateSize
= 8192;
1222 device
->MaxNoOfSources
= GetConfigValueInt(NULL
, "sources", 256);
1223 if((ALint
)device
->MaxNoOfSources
<= 0)
1224 device
->MaxNoOfSources
= 256;
1226 // Find a playback device to open
1227 for(i
= 0;BackendList
[i
].Init
;i
++)
1229 device
->Funcs
= &BackendList
[i
].Funcs
;
1230 if(ALCdevice_OpenPlayback(device
, deviceName
))
1232 SuspendContext(NULL
);
1233 device
->next
= g_pDeviceList
;
1234 g_pDeviceList
= device
;
1236 ProcessContext(NULL
);
1238 bDeviceFound
= AL_TRUE
;
1245 // No suitable output device found
1258 Close the specified Device
1260 ALCAPI ALCboolean ALCAPIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
1262 ALCboolean bReturn
= ALC_FALSE
;
1265 if ((pDevice
)&&(!pDevice
->IsCaptureDevice
))
1267 SuspendContext(NULL
);
1269 list
= &g_pDeviceList
;
1270 while(*list
!= pDevice
)
1271 list
= &(*list
)->next
;
1273 *list
= (*list
)->next
;
1276 ProcessContext(NULL
);
1278 if(pDevice
->Context
)
1279 alcDestroyContext(pDevice
->Context
);
1280 ALCdevice_ClosePlayback(pDevice
);
1282 //Release device structure
1283 memset(pDevice
, 0, sizeof(ALCdevice
));
1289 SetALCError(ALC_INVALID_DEVICE
);
1295 ALCvoid
ReleaseALC(ALCvoid
)
1300 if(g_ulContextCount
> 0)
1301 AL_PRINT("exit() %u device(s) and %u context(s) NOT deleted\n", g_ulDeviceCount
, g_ulContextCount
);
1304 while(g_pDeviceList
)
1306 if(g_pDeviceList
->IsCaptureDevice
)
1307 alcCaptureCloseDevice(g_pDeviceList
);
1309 alcCloseDevice(g_pDeviceList
);
1313 ///////////////////////////////////////////////////////