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 static ALboolean init_done
= AL_FALSE
;
197 ///////////////////////////////////////////////////////
200 ///////////////////////////////////////////////////////
201 // ALC Related helper functions
203 BOOL APIENTRY
DllMain(HANDLE hModule
,DWORD ul_reason_for_call
,LPVOID lpReserved
)
207 // Perform actions based on the reason for calling.
208 switch(ul_reason_for_call
)
210 case DLL_PROCESS_ATTACH
:
211 DisableThreadLibraryCalls(hModule
);
214 case DLL_PROCESS_DETACH
:
223 DeleteCriticalSection(&g_csMutex
);
229 #ifdef HAVE_GCC_DESTRUCTOR
230 static void my_deinit() __attribute__((destructor
));
231 static void my_deinit()
233 static ALenum once
= AL_FALSE
;
234 if(once
|| !init_done
) return;
243 DeleteCriticalSection(&g_csMutex
);
248 static void InitAL(void)
253 const char *devs
, *str
;
257 InitializeCriticalSection(&g_csMutex
);
261 devs
= GetConfigValue(NULL
, "drivers", "");
266 const char *next
= devs
;
272 next
= strchr(devs
, ',');
274 if(!devs
[0] || devs
[0] == ',')
277 len
= (next
? ((size_t)(next
-devs
)) : strlen(devs
));
278 for(n
= i
;BackendList
[n
].Init
;n
++)
280 if(len
== strlen(BackendList
[n
].name
) &&
281 strncmp(BackendList
[n
].name
, devs
, len
) == 0)
283 const char *name
= BackendList
[i
].name
;
284 void (*Init
)(BackendFuncs
*) = BackendList
[i
].Init
;
286 BackendList
[i
].name
= BackendList
[n
].name
;
287 BackendList
[i
].Init
= BackendList
[n
].Init
;
289 BackendList
[n
].name
= name
;
290 BackendList
[n
].Init
= Init
;
297 BackendList
[i
].name
= NULL
;
298 BackendList
[i
].Init
= NULL
;
301 for(i
= 0;BackendList
[i
].Init
;i
++)
302 BackendList
[i
].Init(&BackendList
[i
].Funcs
);
304 str
= GetConfigValue(NULL
, "stereodup", "false");
305 DuplicateStereo
= (strcasecmp(str
, "true") == 0 ||
306 strcasecmp(str
, "yes") == 0 ||
307 strcasecmp(str
, "on") == 0 ||
312 ALCchar
*AppendDeviceList(char *name
)
315 ALCchar
*ret
= alcDeviceList
+pos
;
316 if(pos
>= sizeof(alcDeviceList
))
318 AL_PRINT("Not enough room to add %s!\n", name
);
319 return alcDeviceList
+ sizeof(alcDeviceList
) - 1;
321 pos
+= snprintf(alcDeviceList
+pos
, sizeof(alcDeviceList
)-pos
-1, "%s", name
) + 1;
325 ALCchar
*AppendAllDeviceList(char *name
)
328 ALCchar
*ret
= alcAllDeviceList
+pos
;
329 if(pos
>= sizeof(alcAllDeviceList
))
331 AL_PRINT("Not enough room to add %s!\n", name
);
332 return alcAllDeviceList
+ sizeof(alcAllDeviceList
) - 1;
334 pos
+= snprintf(alcAllDeviceList
+pos
, sizeof(alcAllDeviceList
)-pos
-1, "%s", name
) + 1;
338 ALCchar
*AppendCaptureDeviceList(char *name
)
341 ALCchar
*ret
= alcCaptureDeviceList
+pos
;
342 if(pos
>= sizeof(alcCaptureDeviceList
))
344 AL_PRINT("Not enough room to add %s!\n", name
);
345 return alcCaptureDeviceList
+ sizeof(alcCaptureDeviceList
) - 1;
347 pos
+= snprintf(alcCaptureDeviceList
+pos
, sizeof(alcCaptureDeviceList
)-pos
-1, "%s", name
) + 1;
354 Check pContext is a valid Context pointer
356 static ALCboolean
IsContext(ALCcontext
*pContext
)
358 ALCcontext
*pTempContext
;
360 pTempContext
= g_pContextList
;
361 while (pTempContext
&& pTempContext
!= pContext
)
362 pTempContext
= pTempContext
->next
;
364 return (pTempContext
? ALC_TRUE
: ALC_FALSE
);
371 Store latest ALC Error
373 ALCvoid
SetALCError(ALenum errorCode
)
375 g_eLastContextError
= errorCode
;
384 ALCvoid
SuspendContext(ALCcontext
*pContext
)
387 EnterCriticalSection(&g_csMutex
);
396 ALCvoid
ProcessContext(ALCcontext
*pContext
)
399 LeaveCriticalSection(&g_csMutex
);
406 Initialize Context variables
408 static ALvoid
InitContext(ALCcontext
*pContext
)
412 //Initialise listener
413 pContext
->Listener
.Gain
= 1.0f
;
414 pContext
->Listener
.MetersPerUnit
= 1.0f
;
415 pContext
->Listener
.Position
[0] = 0.0f
;
416 pContext
->Listener
.Position
[1] = 0.0f
;
417 pContext
->Listener
.Position
[2] = 0.0f
;
418 pContext
->Listener
.Velocity
[0] = 0.0f
;
419 pContext
->Listener
.Velocity
[1] = 0.0f
;
420 pContext
->Listener
.Velocity
[2] = 0.0f
;
421 pContext
->Listener
.Forward
[0] = 0.0f
;
422 pContext
->Listener
.Forward
[1] = 0.0f
;
423 pContext
->Listener
.Forward
[2] = -1.0f
;
424 pContext
->Listener
.Up
[0] = 0.0f
;
425 pContext
->Listener
.Up
[1] = 1.0f
;
426 pContext
->Listener
.Up
[2] = 0.0f
;
429 pContext
->LastError
= AL_NO_ERROR
;
430 pContext
->InUse
= AL_FALSE
;
433 pContext
->Frequency
= pContext
->Device
->Frequency
;
436 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
437 pContext
->DopplerFactor
= 1.0f
;
438 pContext
->DopplerVelocity
= 1.0f
;
439 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
441 pContext
->lNumStereoSources
= 1;
442 pContext
->lNumMonoSources
= pContext
->Device
->MaxNoOfSources
- pContext
->lNumStereoSources
;
444 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";
446 level
= GetConfigValueInt(NULL
, "cf_level", 0);
447 if(level
> 0 && level
<= 6)
449 pContext
->bs2b
= calloc(1, sizeof(*pContext
->bs2b
));
450 bs2b_set_srate(pContext
->bs2b
, pContext
->Frequency
);
451 bs2b_set_level(pContext
->bs2b
, level
);
459 Clean up Context, destroy any remaining Sources
461 static ALCvoid
ExitContext(ALCcontext
*pContext
)
464 pContext
->LastError
= AL_NO_ERROR
;
465 pContext
->InUse
= AL_FALSE
;
467 free(pContext
->bs2b
);
468 pContext
->bs2b
= NULL
;
471 ///////////////////////////////////////////////////////
474 ///////////////////////////////////////////////////////
475 // ALC Functions calls
478 // This should probably move to another c file but for now ...
479 ALCAPI ALCdevice
* ALCAPIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
481 ALCboolean DeviceFound
= ALC_FALSE
;
482 ALCdevice
*pDevice
= NULL
;
487 if(deviceName
&& !deviceName
[0])
490 pDevice
= malloc(sizeof(ALCdevice
));
495 //Initialise device structure
496 memset(pDevice
, 0, sizeof(ALCdevice
));
499 pDevice
->IsCaptureDevice
= AL_TRUE
;
501 pDevice
->Frequency
= frequency
;
502 pDevice
->Format
= format
;
504 for(i
= 0;BackendList
[i
].Init
;i
++)
506 pDevice
->Funcs
= &BackendList
[i
].Funcs
;
507 if(ALCdevice_OpenCapture(pDevice
, deviceName
, frequency
, format
, SampleSize
))
509 SuspendContext(NULL
);
510 pDevice
->next
= g_pDeviceList
;
511 g_pDeviceList
= pDevice
;
513 ProcessContext(NULL
);
515 DeviceFound
= ALC_TRUE
;
521 SetALCError(ALC_INVALID_VALUE
);
530 SetALCError(ALC_OUT_OF_MEMORY
);
535 ALCAPI ALCboolean ALCAPIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
537 ALCboolean bReturn
= ALC_FALSE
;
540 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
542 SuspendContext(NULL
);
544 list
= &g_pDeviceList
;
545 while(*list
!= pDevice
)
546 list
= &(*list
)->next
;
548 *list
= (*list
)->next
;
551 ProcessContext(NULL
);
553 ALCdevice_CloseCapture(pDevice
);
559 SetALCError(ALC_INVALID_DEVICE
);
564 ALCAPI
void ALCAPIENTRY
alcCaptureStart(ALCdevice
*pDevice
)
566 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
567 ALCdevice_StartCapture(pDevice
);
569 SetALCError(ALC_INVALID_DEVICE
);
572 ALCAPI
void ALCAPIENTRY
alcCaptureStop(ALCdevice
*pDevice
)
574 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
575 ALCdevice_StopCapture(pDevice
);
577 SetALCError(ALC_INVALID_DEVICE
);
580 ALCAPI
void ALCAPIENTRY
alcCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCsizei lSamples
)
582 if ((pDevice
) && (pDevice
->IsCaptureDevice
))
583 ALCdevice_CaptureSamples(pDevice
, pBuffer
, lSamples
);
585 SetALCError(ALC_INVALID_DEVICE
);
591 Return last ALC generated error code
593 ALCAPI ALCenum ALCAPIENTRY
alcGetError(ALCdevice
*device
)
599 errorCode
= g_eLastContextError
;
600 g_eLastContextError
= ALC_NO_ERROR
;
610 ALCAPI ALCvoid ALCAPIENTRY
alcSuspendContext(ALCcontext
*pContext
)
612 // Not a lot happens here !
622 ALCAPI ALCvoid ALCAPIENTRY
alcProcessContext(ALCcontext
*pContext
)
624 // Not a lot happens here !
632 Returns information about the Device, and error strings
634 ALCAPI
const ALCchar
* ALCAPIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
636 const ALCchar
*value
= NULL
;
646 case ALC_INVALID_ENUM
:
647 value
= alcErrInvalidEnum
;
650 case ALC_INVALID_VALUE
:
651 value
= alcErrInvalidValue
;
654 case ALC_INVALID_DEVICE
:
655 value
= alcErrInvalidDevice
;
658 case ALC_INVALID_CONTEXT
:
659 value
= alcErrInvalidContext
;
662 case ALC_OUT_OF_MEMORY
:
663 value
= alcErrOutOfMemory
;
666 case ALC_DEFAULT_DEVICE_SPECIFIER
:
667 value
= alcDefaultDeviceSpecifier
;
670 case ALC_DEVICE_SPECIFIER
:
672 value
= pDevice
->szDeviceName
;
674 value
= alcDeviceList
;
677 case ALC_ALL_DEVICES_SPECIFIER
:
678 value
= alcAllDeviceList
;
681 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
682 value
= alcDefaultAllDeviceSpecifier
;
685 case ALC_CAPTURE_DEVICE_SPECIFIER
:
687 value
= pDevice
->szDeviceName
;
689 value
= alcCaptureDeviceList
;
692 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
693 value
= alcCaptureDefaultDeviceSpecifier
;
697 value
= alcExtensionList
;
701 SetALCError(ALC_INVALID_ENUM
);
712 Returns information about the Device and the version of Open AL
714 ALCAPI ALCvoid ALCAPIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
718 if ((device
)&&(device
->IsCaptureDevice
))
720 SuspendContext(NULL
);
725 case ALC_CAPTURE_SAMPLES
:
726 if ((size
) && (data
))
727 *data
= ALCdevice_AvailableSamples(device
);
729 SetALCError(ALC_INVALID_VALUE
);
733 SetALCError(ALC_INVALID_ENUM
);
737 ProcessContext(NULL
);
746 case ALC_MAJOR_VERSION
:
748 SetALCError(ALC_INVALID_VALUE
);
750 *data
= alcMajorVersion
;
753 case ALC_MINOR_VERSION
:
755 SetALCError(ALC_INVALID_VALUE
);
757 *data
= alcMinorVersion
;
760 case ALC_EFX_MAJOR_VERSION
:
762 SetALCError(ALC_INVALID_VALUE
);
764 *data
= alcEFXMajorVersion
;
767 case ALC_EFX_MINOR_VERSION
:
769 SetALCError(ALC_INVALID_VALUE
);
771 *data
= alcEFXMinorVersion
;
774 case ALC_MAX_AUXILIARY_SENDS
:
776 SetALCError(ALC_INVALID_VALUE
);
781 case ALC_ATTRIBUTES_SIZE
:
783 SetALCError(ALC_INVALID_DEVICE
);
785 SetALCError(ALC_INVALID_VALUE
);
790 case ALC_ALL_ATTRIBUTES
:
792 SetALCError(ALC_INVALID_DEVICE
);
794 SetALCError(ALC_INVALID_VALUE
);
799 data
[i
++] = ALC_FREQUENCY
;
800 data
[i
++] = device
->Frequency
;
802 data
[i
++] = ALC_REFRESH
;
803 data
[i
++] = device
->Frequency
/ device
->UpdateSize
;
805 data
[i
++] = ALC_SYNC
;
806 data
[i
++] = ALC_FALSE
;
808 SuspendContext(NULL
);
809 if(device
->Context
&& size
>= 12)
811 data
[i
++] = ALC_MONO_SOURCES
;
812 data
[i
++] = device
->Context
->lNumMonoSources
;
814 data
[i
++] = ALC_STEREO_SOURCES
;
815 data
[i
++] = device
->Context
->lNumStereoSources
;
817 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
818 data
[i
++] = MAX_SENDS
;
820 ProcessContext(NULL
);
828 SetALCError(ALC_INVALID_DEVICE
);
830 SetALCError(ALC_INVALID_VALUE
);
832 *data
= device
->Frequency
;
837 SetALCError(ALC_INVALID_DEVICE
);
839 SetALCError(ALC_INVALID_VALUE
);
841 *data
= device
->Frequency
/ device
->UpdateSize
;
846 SetALCError(ALC_INVALID_DEVICE
);
848 SetALCError(ALC_INVALID_VALUE
);
853 case ALC_MONO_SOURCES
:
854 if(!device
|| !device
->Context
)
855 SetALCError(ALC_INVALID_DEVICE
);
857 SetALCError(ALC_INVALID_VALUE
);
859 *data
= device
->Context
->lNumMonoSources
;
862 case ALC_STEREO_SOURCES
:
863 if(!device
|| !device
->Context
)
864 SetALCError(ALC_INVALID_DEVICE
);
866 SetALCError(ALC_INVALID_VALUE
);
868 *data
= device
->Context
->lNumStereoSources
;
872 SetALCError(ALC_INVALID_ENUM
);
877 SetALCError(ALC_INVALID_VALUE
);
885 alcIsExtensionPresent
887 Determines if there is support for a particular extension
889 ALCAPI ALCboolean ALCAPIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
891 ALCboolean bResult
= ALC_FALSE
;
900 len
= strlen(extName
);
901 ptr
= alcExtensionList
;
904 if(strncasecmp(ptr
, extName
, len
) == 0 &&
905 (ptr
[len
] == '\0' || isspace(ptr
[len
])))
910 if((ptr
=strchr(ptr
, ' ')) != NULL
)
914 } while(isspace(*ptr
));
919 SetALCError(ALC_INVALID_VALUE
);
928 Retrieves the function address for a particular extension function
930 ALCAPI ALCvoid
* ALCAPIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
932 ALCvoid
*pFunction
= NULL
;
939 while(alcFunctions
[i
].funcName
&&
940 strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
942 pFunction
= alcFunctions
[i
].address
;
945 SetALCError(ALC_INVALID_VALUE
);
954 Get the value for a particular ALC Enumerated Value
956 ALCAPI ALCenum ALCAPIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
963 while ((enumeration
[i
].enumName
)&&(strcmp(enumeration
[i
].enumName
,enumName
)))
965 val
= enumeration
[i
].value
;
967 if(!enumeration
[i
].enumName
)
968 SetALCError(ALC_INVALID_VALUE
);
977 Create and attach a Context to a particular Device.
979 ALCAPI ALCcontext
* ALCAPIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
981 ALCcontext
*ALContext
= NULL
;
982 ALuint ulAttributeIndex
, ulRequestedStereoSources
;
984 if ((device
)&&(!device
->IsCaptureDevice
))
986 // Reset Context Last Error code
987 g_eLastContextError
= ALC_NO_ERROR
;
989 // Current implementation only allows one Context per Device
992 ALContext
= calloc(1, sizeof(ALCcontext
));
995 SetALCError(ALC_OUT_OF_MEMORY
);
999 ALContext
->Device
= device
;
1000 InitContext(ALContext
);
1002 device
->Context
= ALContext
;
1004 SuspendContext(NULL
);
1006 ALContext
->next
= g_pContextList
;
1007 g_pContextList
= ALContext
;
1010 ProcessContext(NULL
);
1012 // Check for Voice Count attributes
1015 ulAttributeIndex
= 0;
1016 while ((ulAttributeIndex
< 10) && (attrList
[ulAttributeIndex
]))
1018 if (attrList
[ulAttributeIndex
] == ALC_STEREO_SOURCES
)
1020 ulRequestedStereoSources
= attrList
[ulAttributeIndex
+ 1];
1022 if (ulRequestedStereoSources
> ALContext
->Device
->MaxNoOfSources
)
1023 ulRequestedStereoSources
= ALContext
->Device
->MaxNoOfSources
;
1025 ALContext
->lNumStereoSources
= ulRequestedStereoSources
;
1026 ALContext
->lNumMonoSources
= ALContext
->Device
->MaxNoOfSources
- ALContext
->lNumStereoSources
;
1030 ulAttributeIndex
+= 2;
1036 SetALCError(ALC_INVALID_VALUE
);
1041 SetALCError(ALC_INVALID_DEVICE
);
1052 ALCAPI ALCvoid ALCAPIENTRY
alcDestroyContext(ALCcontext
*context
)
1058 // Lock context list
1059 SuspendContext(NULL
);
1061 if (IsContext(context
))
1064 SuspendContext(context
);
1066 ReleaseALSources(context
);
1067 ReleaseALAuxiliaryEffectSlots(context
);
1069 context
->Device
->Context
= NULL
;
1071 list
= &g_pContextList
;
1072 while(*list
!= context
)
1073 list
= &(*list
)->next
;
1075 *list
= (*list
)->next
;
1079 ProcessContext(context
);
1081 ExitContext(context
);
1083 // Free memory (MUST do this after ProcessContext)
1084 memset(context
, 0, sizeof(ALCcontext
));
1088 SetALCError(ALC_INVALID_CONTEXT
);
1090 ProcessContext(NULL
);
1095 alcGetCurrentContext
1097 Returns the currently active Context
1099 ALCAPI ALCcontext
* ALCAPIENTRY
alcGetCurrentContext(ALCvoid
)
1101 ALCcontext
*pContext
= NULL
;
1105 SuspendContext(NULL
);
1107 pContext
= g_pContextList
;
1108 while ((pContext
) && (!pContext
->InUse
))
1109 pContext
= pContext
->next
;
1111 ProcessContext(NULL
);
1118 alcGetContextsDevice
1120 Returns the Device that a particular Context is attached to
1122 ALCAPI ALCdevice
* ALCAPIENTRY
alcGetContextsDevice(ALCcontext
*pContext
)
1124 ALCdevice
*pDevice
= NULL
;
1128 SuspendContext(NULL
);
1129 if (IsContext(pContext
))
1130 pDevice
= pContext
->Device
;
1132 SetALCError(ALC_INVALID_CONTEXT
);
1133 ProcessContext(NULL
);
1140 alcMakeContextCurrent
1142 Makes the given Context the active Context
1144 ALCAPI ALCboolean ALCAPIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
1146 ALCcontext
*ALContext
;
1147 ALboolean bReturn
= AL_TRUE
;
1151 SuspendContext(NULL
);
1153 // context must be a valid Context or NULL
1154 if ((IsContext(context
)) || (context
== NULL
))
1156 if ((ALContext
=alcGetCurrentContext()))
1158 SuspendContext(ALContext
);
1159 ALContext
->InUse
=AL_FALSE
;
1160 ProcessContext(ALContext
);
1163 if ((ALContext
=context
) && (ALContext
->Device
))
1165 SuspendContext(ALContext
);
1166 ALContext
->InUse
=AL_TRUE
;
1167 ProcessContext(ALContext
);
1172 SetALCError(ALC_INVALID_CONTEXT
);
1176 ProcessContext(NULL
);
1185 Open the Device specified.
1187 ALCAPI ALCdevice
* ALCAPIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
1189 ALboolean bDeviceFound
= AL_FALSE
;
1195 if(deviceName
&& !deviceName
[0])
1198 device
= malloc(sizeof(ALCdevice
));
1203 //Initialise device structure
1204 memset(device
, 0, sizeof(ALCdevice
));
1207 device
->IsCaptureDevice
= AL_FALSE
;
1210 device
->Frequency
= GetConfigValueInt(NULL
, "frequency", SWMIXER_OUTPUT_RATE
);
1211 if((ALint
)device
->Frequency
<= 0)
1212 device
->Frequency
= SWMIXER_OUTPUT_RATE
;
1214 fmt
= GetConfigValue(NULL
, "format", "AL_FORMAT_STEREO16");
1216 device
->Format
= alGetEnumValue(fmt
);
1218 if(!aluChannelsFromFormat(device
->Format
))
1219 device
->Format
= AL_FORMAT_STEREO16
;
1221 device
->UpdateSize
= GetConfigValueInt(NULL
, "refresh", 4096);
1222 if((ALint
)device
->UpdateSize
<= 0)
1223 device
->UpdateSize
= 4096;
1225 device
->MaxNoOfSources
= GetConfigValueInt(NULL
, "sources", 256);
1226 if((ALint
)device
->MaxNoOfSources
<= 0)
1227 device
->MaxNoOfSources
= 256;
1229 // Find a playback device to open
1230 for(i
= 0;BackendList
[i
].Init
;i
++)
1232 device
->Funcs
= &BackendList
[i
].Funcs
;
1233 if(ALCdevice_OpenPlayback(device
, deviceName
))
1235 SuspendContext(NULL
);
1236 device
->next
= g_pDeviceList
;
1237 g_pDeviceList
= device
;
1239 ProcessContext(NULL
);
1241 bDeviceFound
= AL_TRUE
;
1248 // No suitable output device found
1261 Close the specified Device
1263 ALCAPI ALCboolean ALCAPIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
1265 ALCboolean bReturn
= ALC_FALSE
;
1268 if ((pDevice
)&&(!pDevice
->IsCaptureDevice
))
1270 SuspendContext(NULL
);
1272 list
= &g_pDeviceList
;
1273 while(*list
!= pDevice
)
1274 list
= &(*list
)->next
;
1276 *list
= (*list
)->next
;
1279 ProcessContext(NULL
);
1281 if(pDevice
->Context
)
1284 AL_PRINT("alcCloseDevice(): destroying 1 Context\n");
1286 alcDestroyContext(pDevice
->Context
);
1288 ALCdevice_ClosePlayback(pDevice
);
1290 //Release device structure
1291 memset(pDevice
, 0, sizeof(ALCdevice
));
1297 SetALCError(ALC_INVALID_DEVICE
);
1303 ALCvoid
ReleaseALC(ALCvoid
)
1306 if(g_ulDeviceCount
> 0)
1307 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount
, (g_ulDeviceCount
>1)?"s":"");
1310 while(g_pDeviceList
)
1312 if(g_pDeviceList
->IsCaptureDevice
)
1313 alcCaptureCloseDevice(g_pDeviceList
);
1315 alcCloseDevice(g_pDeviceList
);
1319 ///////////////////////////////////////////////////////