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 { "solaris", alc_solaris_init
, EmptyFuncs
},
65 { "dsound", alcDSoundInit
, EmptyFuncs
},
68 { "winmm", alcWinMMInit
, EmptyFuncs
},
71 { "wave", alc_wave_init
, EmptyFuncs
},
73 { NULL
, NULL
, EmptyFuncs
}
77 ///////////////////////////////////////////////////////
79 #define ALC_EFX_MAJOR_VERSION 0x20001
80 #define ALC_EFX_MINOR_VERSION 0x20002
81 #define ALC_MAX_AUXILIARY_SENDS 0x20003
83 ///////////////////////////////////////////////////////
84 // STRING and EXTENSIONS
86 typedef struct ALCfunction_struct
92 static ALCfunction alcFunctions
[] = {
93 { "alcCreateContext", (ALvoid
*) alcCreateContext
},
94 { "alcMakeContextCurrent", (ALvoid
*) alcMakeContextCurrent
},
95 { "alcProcessContext", (ALvoid
*) alcProcessContext
},
96 { "alcSuspendContext", (ALvoid
*) alcSuspendContext
},
97 { "alcDestroyContext", (ALvoid
*) alcDestroyContext
},
98 { "alcGetCurrentContext", (ALvoid
*) alcGetCurrentContext
},
99 { "alcGetContextsDevice", (ALvoid
*) alcGetContextsDevice
},
100 { "alcOpenDevice", (ALvoid
*) alcOpenDevice
},
101 { "alcCloseDevice", (ALvoid
*) alcCloseDevice
},
102 { "alcGetError", (ALvoid
*) alcGetError
},
103 { "alcIsExtensionPresent", (ALvoid
*) alcIsExtensionPresent
},
104 { "alcGetProcAddress", (ALvoid
*) alcGetProcAddress
},
105 { "alcGetEnumValue", (ALvoid
*) alcGetEnumValue
},
106 { "alcGetString", (ALvoid
*) alcGetString
},
107 { "alcGetIntegerv", (ALvoid
*) alcGetIntegerv
},
108 { "alcCaptureOpenDevice", (ALvoid
*) alcCaptureOpenDevice
},
109 { "alcCaptureCloseDevice", (ALvoid
*) alcCaptureCloseDevice
},
110 { "alcCaptureStart", (ALvoid
*) alcCaptureStart
},
111 { "alcCaptureStop", (ALvoid
*) alcCaptureStop
},
112 { "alcCaptureSamples", (ALvoid
*) alcCaptureSamples
},
113 { NULL
, (ALvoid
*) NULL
}
116 static ALenums enumeration
[]={
118 { (ALchar
*)"ALC_INVALID", ALC_INVALID
},
119 { (ALchar
*)"ALC_FALSE", ALC_FALSE
},
120 { (ALchar
*)"ALC_TRUE", ALC_TRUE
},
123 { (ALchar
*)"ALC_MAJOR_VERSION", ALC_MAJOR_VERSION
},
124 { (ALchar
*)"ALC_MINOR_VERSION", ALC_MINOR_VERSION
},
125 { (ALchar
*)"ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE
},
126 { (ALchar
*)"ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES
},
127 { (ALchar
*)"ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER
},
128 { (ALchar
*)"ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER
},
129 { (ALchar
*)"ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER
},
130 { (ALchar
*)"ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER
},
131 { (ALchar
*)"ALC_EXTENSIONS", ALC_EXTENSIONS
},
132 { (ALchar
*)"ALC_FREQUENCY", ALC_FREQUENCY
},
133 { (ALchar
*)"ALC_REFRESH", ALC_REFRESH
},
134 { (ALchar
*)"ALC_SYNC", ALC_SYNC
},
135 { (ALchar
*)"ALC_MONO_SOURCES", ALC_MONO_SOURCES
},
136 { (ALchar
*)"ALC_STEREO_SOURCES", ALC_STEREO_SOURCES
},
137 { (ALchar
*)"ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER
},
138 { (ALchar
*)"ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
},
139 { (ALchar
*)"ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES
},
142 { (ALchar
*)"ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION
},
143 { (ALchar
*)"ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION
},
144 { (ALchar
*)"ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS
},
147 { (ALchar
*)"ALC_NO_ERROR", ALC_NO_ERROR
},
148 { (ALchar
*)"ALC_INVALID_DEVICE", ALC_INVALID_DEVICE
},
149 { (ALchar
*)"ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT
},
150 { (ALchar
*)"ALC_INVALID_ENUM", ALC_INVALID_ENUM
},
151 { (ALchar
*)"ALC_INVALID_VALUE", ALC_INVALID_VALUE
},
152 { (ALchar
*)"ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY
},
153 { (ALchar
*)NULL
, (ALenum
)0 }
156 static const ALCchar alcNoError
[] = "No Error";
157 static const ALCchar alcErrInvalidDevice
[] = "Invalid Device";
158 static const ALCchar alcErrInvalidContext
[] = "Invalid Context";
159 static const ALCchar alcErrInvalidEnum
[] = "Invalid Enum";
160 static const ALCchar alcErrInvalidValue
[] = "Invalid Value";
161 static const ALCchar alcErrOutOfMemory
[] = "Out of Memory";
164 static ALCchar alcDeviceList
[2048];
165 static ALCchar alcAllDeviceList
[2048];
166 static ALCchar alcCaptureDeviceList
[2048];
167 // Default is always the first in the list
168 static ALCchar
*alcDefaultDeviceSpecifier
= alcDeviceList
;
169 static ALCchar
*alcDefaultAllDeviceSpecifier
= alcAllDeviceList
;
170 static ALCchar
*alcCaptureDefaultDeviceSpecifier
= alcCaptureDeviceList
;
173 static ALCchar alcExtensionList
[] = "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_EFX";
174 static ALCint alcMajorVersion
= 1;
175 static ALCint alcMinorVersion
= 1;
177 static ALCint alcEFXMajorVersion
= 1;
178 static ALCint alcEFXMinorVersion
= 0;
180 ///////////////////////////////////////////////////////
183 ///////////////////////////////////////////////////////
186 static ALCdevice
*g_pDeviceList
= NULL
;
187 static ALCuint g_ulDeviceCount
= 0;
189 static CRITICAL_SECTION g_csMutex
;
192 static ALCcontext
*g_pContextList
= NULL
;
193 static ALCuint g_ulContextCount
= 0;
196 static ALCenum g_eLastContextError
= ALC_NO_ERROR
;
198 static ALboolean init_done
= AL_FALSE
;
200 ///////////////////////////////////////////////////////
203 ///////////////////////////////////////////////////////
204 // ALC Related helper functions
206 BOOL APIENTRY
DllMain(HANDLE hModule
,DWORD ul_reason_for_call
,LPVOID lpReserved
)
210 // Perform actions based on the reason for calling.
211 switch(ul_reason_for_call
)
213 case DLL_PROCESS_ATTACH
:
214 DisableThreadLibraryCalls(hModule
);
217 case DLL_PROCESS_DETACH
:
226 DeleteCriticalSection(&g_csMutex
);
232 #ifdef HAVE_GCC_DESTRUCTOR
233 static void my_deinit() __attribute__((destructor
));
234 static void my_deinit()
236 static ALenum once
= AL_FALSE
;
237 if(once
|| !init_done
) return;
246 DeleteCriticalSection(&g_csMutex
);
251 static void InitAL(void)
256 const char *devs
, *str
;
260 InitializeCriticalSection(&g_csMutex
);
264 devs
= GetConfigValue(NULL
, "drivers", "");
269 const char *next
= devs
;
275 next
= strchr(devs
, ',');
277 if(!devs
[0] || devs
[0] == ',')
280 len
= (next
? ((size_t)(next
-devs
)) : strlen(devs
));
281 for(n
= i
;BackendList
[n
].Init
;n
++)
283 if(len
== strlen(BackendList
[n
].name
) &&
284 strncmp(BackendList
[n
].name
, devs
, len
) == 0)
286 const char *name
= BackendList
[i
].name
;
287 void (*Init
)(BackendFuncs
*) = BackendList
[i
].Init
;
289 BackendList
[i
].name
= BackendList
[n
].name
;
290 BackendList
[i
].Init
= BackendList
[n
].Init
;
292 BackendList
[n
].name
= name
;
293 BackendList
[n
].Init
= Init
;
300 BackendList
[i
].name
= NULL
;
301 BackendList
[i
].Init
= NULL
;
304 for(i
= 0;BackendList
[i
].Init
;i
++)
305 BackendList
[i
].Init(&BackendList
[i
].Funcs
);
307 str
= GetConfigValue(NULL
, "stereodup", "false");
308 DuplicateStereo
= (strcasecmp(str
, "true") == 0 ||
309 strcasecmp(str
, "yes") == 0 ||
310 strcasecmp(str
, "on") == 0 ||
315 ALCchar
*AppendDeviceList(char *name
)
318 ALCchar
*ret
= alcDeviceList
+pos
;
319 if(pos
>= sizeof(alcDeviceList
))
321 AL_PRINT("Not enough room to add %s!\n", name
);
322 return alcDeviceList
+ sizeof(alcDeviceList
) - 1;
324 pos
+= snprintf(alcDeviceList
+pos
, sizeof(alcDeviceList
)-pos
-1, "%s", name
) + 1;
328 ALCchar
*AppendAllDeviceList(char *name
)
331 ALCchar
*ret
= alcAllDeviceList
+pos
;
332 if(pos
>= sizeof(alcAllDeviceList
))
334 AL_PRINT("Not enough room to add %s!\n", name
);
335 return alcAllDeviceList
+ sizeof(alcAllDeviceList
) - 1;
337 pos
+= snprintf(alcAllDeviceList
+pos
, sizeof(alcAllDeviceList
)-pos
-1, "%s", name
) + 1;
341 ALCchar
*AppendCaptureDeviceList(char *name
)
344 ALCchar
*ret
= alcCaptureDeviceList
+pos
;
345 if(pos
>= sizeof(alcCaptureDeviceList
))
347 AL_PRINT("Not enough room to add %s!\n", name
);
348 return alcCaptureDeviceList
+ sizeof(alcCaptureDeviceList
) - 1;
350 pos
+= snprintf(alcCaptureDeviceList
+pos
, sizeof(alcCaptureDeviceList
)-pos
-1, "%s", name
) + 1;
357 Check pContext is a valid Context pointer
359 static ALCboolean
IsContext(ALCcontext
*pContext
)
361 ALCcontext
*pTempContext
;
363 pTempContext
= g_pContextList
;
364 while (pTempContext
&& pTempContext
!= pContext
)
365 pTempContext
= pTempContext
->next
;
367 return (pTempContext
? ALC_TRUE
: ALC_FALSE
);
374 Store latest ALC Error
376 ALCvoid
SetALCError(ALenum errorCode
)
378 g_eLastContextError
= errorCode
;
387 ALCvoid
SuspendContext(ALCcontext
*pContext
)
390 EnterCriticalSection(&g_csMutex
);
399 ALCvoid
ProcessContext(ALCcontext
*pContext
)
402 LeaveCriticalSection(&g_csMutex
);
409 Initialize Context variables
411 static ALvoid
InitContext(ALCcontext
*pContext
)
415 //Initialise listener
416 pContext
->Listener
.Gain
= 1.0f
;
417 pContext
->Listener
.MetersPerUnit
= 1.0f
;
418 pContext
->Listener
.Position
[0] = 0.0f
;
419 pContext
->Listener
.Position
[1] = 0.0f
;
420 pContext
->Listener
.Position
[2] = 0.0f
;
421 pContext
->Listener
.Velocity
[0] = 0.0f
;
422 pContext
->Listener
.Velocity
[1] = 0.0f
;
423 pContext
->Listener
.Velocity
[2] = 0.0f
;
424 pContext
->Listener
.Forward
[0] = 0.0f
;
425 pContext
->Listener
.Forward
[1] = 0.0f
;
426 pContext
->Listener
.Forward
[2] = -1.0f
;
427 pContext
->Listener
.Up
[0] = 0.0f
;
428 pContext
->Listener
.Up
[1] = 1.0f
;
429 pContext
->Listener
.Up
[2] = 0.0f
;
432 pContext
->LastError
= AL_NO_ERROR
;
433 pContext
->InUse
= AL_FALSE
;
436 pContext
->Frequency
= pContext
->Device
->Frequency
;
439 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
440 pContext
->DopplerFactor
= 1.0f
;
441 pContext
->DopplerVelocity
= 1.0f
;
442 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
444 pContext
->lNumStereoSources
= 1;
445 pContext
->lNumMonoSources
= pContext
->Device
->MaxNoOfSources
- pContext
->lNumStereoSources
;
447 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";
449 level
= GetConfigValueInt(NULL
, "cf_level", 0);
450 if(level
> 0 && level
<= 6)
452 pContext
->bs2b
= calloc(1, sizeof(*pContext
->bs2b
));
453 bs2b_set_srate(pContext
->bs2b
, pContext
->Frequency
);
454 bs2b_set_level(pContext
->bs2b
, level
);
462 Clean up Context, destroy any remaining Sources
464 static ALCvoid
ExitContext(ALCcontext
*pContext
)
467 pContext
->LastError
= AL_NO_ERROR
;
468 pContext
->InUse
= AL_FALSE
;
470 free(pContext
->bs2b
);
471 pContext
->bs2b
= NULL
;
474 ///////////////////////////////////////////////////////
477 ///////////////////////////////////////////////////////
478 // ALC Functions calls
481 // This should probably move to another c file but for now ...
482 ALCAPI ALCdevice
* ALCAPIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
484 ALCboolean DeviceFound
= ALC_FALSE
;
485 ALCdevice
*pDevice
= NULL
;
490 if(deviceName
&& !deviceName
[0])
493 pDevice
= malloc(sizeof(ALCdevice
));
498 //Initialise device structure
499 memset(pDevice
, 0, sizeof(ALCdevice
));
502 pDevice
->IsCaptureDevice
= AL_TRUE
;
504 pDevice
->Frequency
= frequency
;
505 pDevice
->Format
= format
;
507 for(i
= 0;BackendList
[i
].Init
;i
++)
509 pDevice
->Funcs
= &BackendList
[i
].Funcs
;
510 if(ALCdevice_OpenCapture(pDevice
, deviceName
, frequency
, format
, SampleSize
))
512 SuspendContext(NULL
);
513 pDevice
->next
= g_pDeviceList
;
514 g_pDeviceList
= pDevice
;
516 ProcessContext(NULL
);
518 DeviceFound
= ALC_TRUE
;
524 SetALCError(ALC_INVALID_VALUE
);
533 SetALCError(ALC_OUT_OF_MEMORY
);
538 ALCAPI ALCboolean ALCAPIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
540 ALCboolean bReturn
= ALC_FALSE
;
543 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
545 SuspendContext(NULL
);
547 list
= &g_pDeviceList
;
548 while(*list
!= pDevice
)
549 list
= &(*list
)->next
;
551 *list
= (*list
)->next
;
554 ProcessContext(NULL
);
556 ALCdevice_CloseCapture(pDevice
);
562 SetALCError(ALC_INVALID_DEVICE
);
567 ALCAPI
void ALCAPIENTRY
alcCaptureStart(ALCdevice
*pDevice
)
569 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
570 ALCdevice_StartCapture(pDevice
);
572 SetALCError(ALC_INVALID_DEVICE
);
575 ALCAPI
void ALCAPIENTRY
alcCaptureStop(ALCdevice
*pDevice
)
577 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
578 ALCdevice_StopCapture(pDevice
);
580 SetALCError(ALC_INVALID_DEVICE
);
583 ALCAPI
void ALCAPIENTRY
alcCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCsizei lSamples
)
585 if ((pDevice
) && (pDevice
->IsCaptureDevice
))
586 ALCdevice_CaptureSamples(pDevice
, pBuffer
, lSamples
);
588 SetALCError(ALC_INVALID_DEVICE
);
594 Return last ALC generated error code
596 ALCAPI ALCenum ALCAPIENTRY
alcGetError(ALCdevice
*device
)
602 errorCode
= g_eLastContextError
;
603 g_eLastContextError
= ALC_NO_ERROR
;
613 ALCAPI ALCvoid ALCAPIENTRY
alcSuspendContext(ALCcontext
*pContext
)
615 // Not a lot happens here !
625 ALCAPI ALCvoid ALCAPIENTRY
alcProcessContext(ALCcontext
*pContext
)
627 // Not a lot happens here !
635 Returns information about the Device, and error strings
637 ALCAPI
const ALCchar
* ALCAPIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
639 const ALCchar
*value
= NULL
;
649 case ALC_INVALID_ENUM
:
650 value
= alcErrInvalidEnum
;
653 case ALC_INVALID_VALUE
:
654 value
= alcErrInvalidValue
;
657 case ALC_INVALID_DEVICE
:
658 value
= alcErrInvalidDevice
;
661 case ALC_INVALID_CONTEXT
:
662 value
= alcErrInvalidContext
;
665 case ALC_OUT_OF_MEMORY
:
666 value
= alcErrOutOfMemory
;
669 case ALC_DEFAULT_DEVICE_SPECIFIER
:
670 value
= alcDefaultDeviceSpecifier
;
673 case ALC_DEVICE_SPECIFIER
:
675 value
= pDevice
->szDeviceName
;
677 value
= alcDeviceList
;
680 case ALC_ALL_DEVICES_SPECIFIER
:
681 value
= alcAllDeviceList
;
684 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
685 value
= alcDefaultAllDeviceSpecifier
;
688 case ALC_CAPTURE_DEVICE_SPECIFIER
:
690 value
= pDevice
->szDeviceName
;
692 value
= alcCaptureDeviceList
;
695 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
696 value
= alcCaptureDefaultDeviceSpecifier
;
700 value
= alcExtensionList
;
704 SetALCError(ALC_INVALID_ENUM
);
715 Returns information about the Device and the version of Open AL
717 ALCAPI ALCvoid ALCAPIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
721 if ((device
)&&(device
->IsCaptureDevice
))
723 SuspendContext(NULL
);
728 case ALC_CAPTURE_SAMPLES
:
729 if ((size
) && (data
))
730 *data
= ALCdevice_AvailableSamples(device
);
732 SetALCError(ALC_INVALID_VALUE
);
736 SetALCError(ALC_INVALID_ENUM
);
740 ProcessContext(NULL
);
749 case ALC_MAJOR_VERSION
:
751 SetALCError(ALC_INVALID_VALUE
);
753 *data
= alcMajorVersion
;
756 case ALC_MINOR_VERSION
:
758 SetALCError(ALC_INVALID_VALUE
);
760 *data
= alcMinorVersion
;
763 case ALC_EFX_MAJOR_VERSION
:
765 SetALCError(ALC_INVALID_VALUE
);
767 *data
= alcEFXMajorVersion
;
770 case ALC_EFX_MINOR_VERSION
:
772 SetALCError(ALC_INVALID_VALUE
);
774 *data
= alcEFXMinorVersion
;
777 case ALC_MAX_AUXILIARY_SENDS
:
779 SetALCError(ALC_INVALID_VALUE
);
784 case ALC_ATTRIBUTES_SIZE
:
786 SetALCError(ALC_INVALID_DEVICE
);
788 SetALCError(ALC_INVALID_VALUE
);
793 case ALC_ALL_ATTRIBUTES
:
795 SetALCError(ALC_INVALID_DEVICE
);
797 SetALCError(ALC_INVALID_VALUE
);
802 data
[i
++] = ALC_FREQUENCY
;
803 data
[i
++] = device
->Frequency
;
805 data
[i
++] = ALC_REFRESH
;
806 data
[i
++] = device
->Frequency
/ device
->UpdateSize
;
808 data
[i
++] = ALC_SYNC
;
809 data
[i
++] = ALC_FALSE
;
811 SuspendContext(NULL
);
812 if(device
->Context
&& size
>= 12)
814 data
[i
++] = ALC_MONO_SOURCES
;
815 data
[i
++] = device
->Context
->lNumMonoSources
;
817 data
[i
++] = ALC_STEREO_SOURCES
;
818 data
[i
++] = device
->Context
->lNumStereoSources
;
820 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
821 data
[i
++] = MAX_SENDS
;
823 ProcessContext(NULL
);
831 SetALCError(ALC_INVALID_DEVICE
);
833 SetALCError(ALC_INVALID_VALUE
);
835 *data
= device
->Frequency
;
840 SetALCError(ALC_INVALID_DEVICE
);
842 SetALCError(ALC_INVALID_VALUE
);
844 *data
= device
->Frequency
/ device
->UpdateSize
;
849 SetALCError(ALC_INVALID_DEVICE
);
851 SetALCError(ALC_INVALID_VALUE
);
856 case ALC_MONO_SOURCES
:
857 if(!device
|| !device
->Context
)
858 SetALCError(ALC_INVALID_DEVICE
);
860 SetALCError(ALC_INVALID_VALUE
);
862 *data
= device
->Context
->lNumMonoSources
;
865 case ALC_STEREO_SOURCES
:
866 if(!device
|| !device
->Context
)
867 SetALCError(ALC_INVALID_DEVICE
);
869 SetALCError(ALC_INVALID_VALUE
);
871 *data
= device
->Context
->lNumStereoSources
;
875 SetALCError(ALC_INVALID_ENUM
);
880 SetALCError(ALC_INVALID_VALUE
);
888 alcIsExtensionPresent
890 Determines if there is support for a particular extension
892 ALCAPI ALCboolean ALCAPIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
894 ALCboolean bResult
= ALC_FALSE
;
903 len
= strlen(extName
);
904 ptr
= alcExtensionList
;
907 if(strncasecmp(ptr
, extName
, len
) == 0 &&
908 (ptr
[len
] == '\0' || isspace(ptr
[len
])))
913 if((ptr
=strchr(ptr
, ' ')) != NULL
)
917 } while(isspace(*ptr
));
922 SetALCError(ALC_INVALID_VALUE
);
931 Retrieves the function address for a particular extension function
933 ALCAPI ALCvoid
* ALCAPIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
935 ALCvoid
*pFunction
= NULL
;
942 while(alcFunctions
[i
].funcName
&&
943 strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
945 pFunction
= alcFunctions
[i
].address
;
948 SetALCError(ALC_INVALID_VALUE
);
957 Get the value for a particular ALC Enumerated Value
959 ALCAPI ALCenum ALCAPIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
966 while ((enumeration
[i
].enumName
)&&(strcmp(enumeration
[i
].enumName
,enumName
)))
968 val
= enumeration
[i
].value
;
970 if(!enumeration
[i
].enumName
)
971 SetALCError(ALC_INVALID_VALUE
);
980 Create and attach a Context to a particular Device.
982 ALCAPI ALCcontext
* ALCAPIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
984 ALCcontext
*ALContext
= NULL
;
985 ALuint ulAttributeIndex
, ulRequestedStereoSources
;
987 if ((device
)&&(!device
->IsCaptureDevice
))
989 // Reset Context Last Error code
990 g_eLastContextError
= ALC_NO_ERROR
;
992 // Current implementation only allows one Context per Device
995 ALContext
= calloc(1, sizeof(ALCcontext
));
998 SetALCError(ALC_OUT_OF_MEMORY
);
1002 ALContext
->Device
= device
;
1003 InitContext(ALContext
);
1005 device
->Context
= ALContext
;
1007 SuspendContext(NULL
);
1009 ALContext
->next
= g_pContextList
;
1010 g_pContextList
= ALContext
;
1013 ProcessContext(NULL
);
1015 // Check for Voice Count attributes
1018 ulAttributeIndex
= 0;
1019 while ((ulAttributeIndex
< 10) && (attrList
[ulAttributeIndex
]))
1021 if (attrList
[ulAttributeIndex
] == ALC_STEREO_SOURCES
)
1023 ulRequestedStereoSources
= attrList
[ulAttributeIndex
+ 1];
1025 if (ulRequestedStereoSources
> ALContext
->Device
->MaxNoOfSources
)
1026 ulRequestedStereoSources
= ALContext
->Device
->MaxNoOfSources
;
1028 ALContext
->lNumStereoSources
= ulRequestedStereoSources
;
1029 ALContext
->lNumMonoSources
= ALContext
->Device
->MaxNoOfSources
- ALContext
->lNumStereoSources
;
1033 ulAttributeIndex
+= 2;
1039 SetALCError(ALC_INVALID_VALUE
);
1044 SetALCError(ALC_INVALID_DEVICE
);
1055 ALCAPI ALCvoid ALCAPIENTRY
alcDestroyContext(ALCcontext
*context
)
1061 // Lock context list
1062 SuspendContext(NULL
);
1064 if (IsContext(context
))
1067 SuspendContext(context
);
1069 ReleaseALSources(context
);
1070 ReleaseALAuxiliaryEffectSlots(context
);
1072 context
->Device
->Context
= NULL
;
1074 list
= &g_pContextList
;
1075 while(*list
!= context
)
1076 list
= &(*list
)->next
;
1078 *list
= (*list
)->next
;
1082 ProcessContext(context
);
1084 ExitContext(context
);
1086 // Free memory (MUST do this after ProcessContext)
1087 memset(context
, 0, sizeof(ALCcontext
));
1091 SetALCError(ALC_INVALID_CONTEXT
);
1093 ProcessContext(NULL
);
1098 alcGetCurrentContext
1100 Returns the currently active Context
1102 ALCAPI ALCcontext
* ALCAPIENTRY
alcGetCurrentContext(ALCvoid
)
1104 ALCcontext
*pContext
= NULL
;
1108 SuspendContext(NULL
);
1110 pContext
= g_pContextList
;
1111 while ((pContext
) && (!pContext
->InUse
))
1112 pContext
= pContext
->next
;
1114 ProcessContext(NULL
);
1121 alcGetContextsDevice
1123 Returns the Device that a particular Context is attached to
1125 ALCAPI ALCdevice
* ALCAPIENTRY
alcGetContextsDevice(ALCcontext
*pContext
)
1127 ALCdevice
*pDevice
= NULL
;
1131 SuspendContext(NULL
);
1132 if (IsContext(pContext
))
1133 pDevice
= pContext
->Device
;
1135 SetALCError(ALC_INVALID_CONTEXT
);
1136 ProcessContext(NULL
);
1143 alcMakeContextCurrent
1145 Makes the given Context the active Context
1147 ALCAPI ALCboolean ALCAPIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
1149 ALCcontext
*ALContext
;
1150 ALboolean bReturn
= AL_TRUE
;
1154 SuspendContext(NULL
);
1156 // context must be a valid Context or NULL
1157 if ((IsContext(context
)) || (context
== NULL
))
1159 if ((ALContext
=alcGetCurrentContext()))
1161 SuspendContext(ALContext
);
1162 ALContext
->InUse
=AL_FALSE
;
1163 ProcessContext(ALContext
);
1166 if ((ALContext
=context
) && (ALContext
->Device
))
1168 SuspendContext(ALContext
);
1169 ALContext
->InUse
=AL_TRUE
;
1170 ProcessContext(ALContext
);
1175 SetALCError(ALC_INVALID_CONTEXT
);
1179 ProcessContext(NULL
);
1188 Open the Device specified.
1190 ALCAPI ALCdevice
* ALCAPIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
1192 ALboolean bDeviceFound
= AL_FALSE
;
1198 if(deviceName
&& !deviceName
[0])
1201 device
= malloc(sizeof(ALCdevice
));
1206 //Initialise device structure
1207 memset(device
, 0, sizeof(ALCdevice
));
1210 device
->IsCaptureDevice
= AL_FALSE
;
1213 device
->Frequency
= GetConfigValueInt(NULL
, "frequency", SWMIXER_OUTPUT_RATE
);
1214 if((ALint
)device
->Frequency
<= 0)
1215 device
->Frequency
= SWMIXER_OUTPUT_RATE
;
1217 fmt
= GetConfigValue(NULL
, "format", "AL_FORMAT_STEREO16");
1219 device
->Format
= alGetEnumValue(fmt
);
1221 if(!aluChannelsFromFormat(device
->Format
))
1222 device
->Format
= AL_FORMAT_STEREO16
;
1224 device
->UpdateSize
= GetConfigValueInt(NULL
, "refresh", 4096);
1225 if((ALint
)device
->UpdateSize
<= 0)
1226 device
->UpdateSize
= 4096;
1228 device
->MaxNoOfSources
= GetConfigValueInt(NULL
, "sources", 256);
1229 if((ALint
)device
->MaxNoOfSources
<= 0)
1230 device
->MaxNoOfSources
= 256;
1232 // Find a playback device to open
1233 for(i
= 0;BackendList
[i
].Init
;i
++)
1235 device
->Funcs
= &BackendList
[i
].Funcs
;
1236 if(ALCdevice_OpenPlayback(device
, deviceName
))
1238 SuspendContext(NULL
);
1239 device
->next
= g_pDeviceList
;
1240 g_pDeviceList
= device
;
1242 ProcessContext(NULL
);
1244 bDeviceFound
= AL_TRUE
;
1251 // No suitable output device found
1264 Close the specified Device
1266 ALCAPI ALCboolean ALCAPIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
1268 ALCboolean bReturn
= ALC_FALSE
;
1271 if ((pDevice
)&&(!pDevice
->IsCaptureDevice
))
1273 SuspendContext(NULL
);
1275 list
= &g_pDeviceList
;
1276 while(*list
!= pDevice
)
1277 list
= &(*list
)->next
;
1279 *list
= (*list
)->next
;
1282 ProcessContext(NULL
);
1284 if(pDevice
->Context
)
1287 AL_PRINT("alcCloseDevice(): destroying 1 Context\n");
1289 alcDestroyContext(pDevice
->Context
);
1291 ALCdevice_ClosePlayback(pDevice
);
1293 //Release device structure
1294 memset(pDevice
, 0, sizeof(ALCdevice
));
1300 SetALCError(ALC_INVALID_DEVICE
);
1306 ALCvoid
ReleaseALC(ALCvoid
)
1309 if(g_ulDeviceCount
> 0)
1310 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount
, (g_ulDeviceCount
>1)?"s":"");
1313 while(g_pDeviceList
)
1315 if(g_pDeviceList
->IsCaptureDevice
)
1316 alcCaptureCloseDevice(g_pDeviceList
);
1318 alcCloseDevice(g_pDeviceList
);
1322 ///////////////////////////////////////////////////////