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
35 #include "alExtension.h"
38 ///////////////////////////////////////////////////////
43 ///////////////////////////////////////////////////////
46 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
49 void (*Init
)(BackendFuncs
*);
53 { "alsa", alc_alsa_init
, EmptyFuncs
},
56 { "oss", alc_oss_init
, EmptyFuncs
},
59 { "dsound", alcDSoundInit
, EmptyFuncs
},
62 { "winmm", alcWinMMInit
, EmptyFuncs
},
65 { "wave", alc_wave_init
, EmptyFuncs
},
67 { NULL
, NULL
, EmptyFuncs
}
71 ///////////////////////////////////////////////////////
73 #define ALC_EFX_MAJOR_VERSION 0x20001
74 #define ALC_EFX_MINOR_VERSION 0x20002
75 #define ALC_MAX_AUXILIARY_SENDS 0x20003
77 ///////////////////////////////////////////////////////
78 // STRING and EXTENSIONS
80 typedef struct ALCextension_struct
86 typedef struct ALCfunction_struct
92 static ALCextension alcExtensions
[] = {
93 { "ALC_ENUMERATE_ALL_EXT", (ALvoid
*) NULL
},
94 { "ALC_ENUMERATION_EXT", (ALvoid
*) NULL
},
95 { "ALC_EXT_CAPTURE", (ALvoid
*) NULL
},
96 { "ALC_EXT_EFX", (ALvoid
*) NULL
},
97 { NULL
, (ALvoid
*) NULL
}
100 static ALCfunction alcFunctions
[] = {
101 { "alcCreateContext", (ALvoid
*) alcCreateContext
},
102 { "alcMakeContextCurrent", (ALvoid
*) alcMakeContextCurrent
},
103 { "alcProcessContext", (ALvoid
*) alcProcessContext
},
104 { "alcSuspendContext", (ALvoid
*) alcSuspendContext
},
105 { "alcDestroyContext", (ALvoid
*) alcDestroyContext
},
106 { "alcGetCurrentContext", (ALvoid
*) alcGetCurrentContext
},
107 { "alcGetContextsDevice", (ALvoid
*) alcGetContextsDevice
},
108 { "alcOpenDevice", (ALvoid
*) alcOpenDevice
},
109 { "alcCloseDevice", (ALvoid
*) alcCloseDevice
},
110 { "alcGetError", (ALvoid
*) alcGetError
},
111 { "alcIsExtensionPresent", (ALvoid
*) alcIsExtensionPresent
},
112 { "alcGetProcAddress", (ALvoid
*) alcGetProcAddress
},
113 { "alcGetEnumValue", (ALvoid
*) alcGetEnumValue
},
114 { "alcGetString", (ALvoid
*) alcGetString
},
115 { "alcGetIntegerv", (ALvoid
*) alcGetIntegerv
},
116 { "alcCaptureOpenDevice", (ALvoid
*) alcCaptureOpenDevice
},
117 { "alcCaptureCloseDevice", (ALvoid
*) alcCaptureCloseDevice
},
118 { "alcCaptureStart", (ALvoid
*) alcCaptureStart
},
119 { "alcCaptureStop", (ALvoid
*) alcCaptureStop
},
120 { "alcCaptureSamples", (ALvoid
*) alcCaptureSamples
},
121 { NULL
, (ALvoid
*) NULL
}
124 static ALenums enumeration
[]={
126 { (ALchar
*)"ALC_INVALID", ALC_INVALID
},
127 { (ALchar
*)"ALC_FALSE", ALC_FALSE
},
128 { (ALchar
*)"ALC_TRUE", ALC_TRUE
},
131 { (ALchar
*)"ALC_MAJOR_VERSION", ALC_MAJOR_VERSION
},
132 { (ALchar
*)"ALC_MINOR_VERSION", ALC_MINOR_VERSION
},
133 { (ALchar
*)"ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE
},
134 { (ALchar
*)"ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES
},
135 { (ALchar
*)"ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER
},
136 { (ALchar
*)"ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER
},
137 { (ALchar
*)"ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER
},
138 { (ALchar
*)"ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER
},
139 { (ALchar
*)"ALC_EXTENSIONS", ALC_EXTENSIONS
},
140 { (ALchar
*)"ALC_FREQUENCY", ALC_FREQUENCY
},
141 { (ALchar
*)"ALC_REFRESH", ALC_REFRESH
},
142 { (ALchar
*)"ALC_SYNC", ALC_SYNC
},
143 { (ALchar
*)"ALC_MONO_SOURCES", ALC_MONO_SOURCES
},
144 { (ALchar
*)"ALC_STEREO_SOURCES", ALC_STEREO_SOURCES
},
145 { (ALchar
*)"ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER
},
146 { (ALchar
*)"ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
},
147 { (ALchar
*)"ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES
},
150 { (ALchar
*)"ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION
},
151 { (ALchar
*)"ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION
},
152 { (ALchar
*)"ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS
},
155 { (ALchar
*)"ALC_NO_ERROR", ALC_NO_ERROR
},
156 { (ALchar
*)"ALC_INVALID_DEVICE", ALC_INVALID_DEVICE
},
157 { (ALchar
*)"ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT
},
158 { (ALchar
*)"ALC_INVALID_ENUM", ALC_INVALID_ENUM
},
159 { (ALchar
*)"ALC_INVALID_VALUE", ALC_INVALID_VALUE
},
160 { (ALchar
*)"ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY
},
161 { (ALchar
*)NULL
, (ALenum
)0 }
164 static const ALCchar alcNoError
[] = "No Error";
165 static const ALCchar alcErrInvalidDevice
[] = "Invalid Device";
166 static const ALCchar alcErrInvalidContext
[] = "Invalid Context";
167 static const ALCchar alcErrInvalidEnum
[] = "Invalid Enum";
168 static const ALCchar alcErrInvalidValue
[] = "Invalid Value";
169 static const ALCchar alcErrOutOfMemory
[] = "Out of Memory";
172 static ALCchar alcDeviceList
[2048];
173 static ALCchar alcAllDeviceList
[2048];
174 static ALCchar alcCaptureDeviceList
[2048];
175 // Default is always the first in the list
176 static ALCchar
*alcDefaultDeviceSpecifier
= alcDeviceList
;
177 static ALCchar
*alcDefaultAllDeviceSpecifier
= alcAllDeviceList
;
178 static ALCchar
*alcCaptureDefaultDeviceSpecifier
= alcCaptureDeviceList
;
181 static ALCchar alcExtensionList
[] = "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_EFX";
182 static ALCint alcMajorVersion
= 1;
183 static ALCint alcMinorVersion
= 1;
185 static ALCint alcEFXMajorVersion
= 1;
186 static ALCint alcEFXMinorVersion
= 0;
188 ///////////////////////////////////////////////////////
191 ///////////////////////////////////////////////////////
194 static ALCdevice
*g_pDeviceList
= NULL
;
195 static ALCuint g_ulDeviceCount
= 0;
198 static ALCcontext
*g_pContextList
= NULL
;
199 static ALCuint g_ulContextCount
= 0;
202 static ALCenum g_eLastContextError
= ALC_NO_ERROR
;
204 ///////////////////////////////////////////////////////
207 ///////////////////////////////////////////////////////
208 // ALC Related helper functions
210 static void InitAL(void)
218 InitializeCriticalSection(&_alMutex
);
222 devs
= GetConfigValue(NULL
, "drivers", "");
227 const char *next
= devs
;
233 next
= strchr(devs
, ',');
235 if(!devs
[0] || devs
[0] == ',')
238 len
= (next
? ((size_t)(next
-devs
)) : strlen(devs
));
239 for(n
= i
;BackendList
[n
].Init
;n
++)
241 if(len
== strlen(BackendList
[n
].name
) &&
242 strncmp(BackendList
[n
].name
, devs
, len
) == 0)
244 const char *name
= BackendList
[i
].name
;
245 void (*Init
)(BackendFuncs
*) = BackendList
[i
].Init
;
247 BackendList
[i
].name
= BackendList
[n
].name
;
248 BackendList
[i
].Init
= BackendList
[n
].Init
;
250 BackendList
[n
].name
= name
;
251 BackendList
[n
].Init
= Init
;
258 BackendList
[i
].name
= NULL
;
259 BackendList
[i
].Init
= NULL
;
262 for(i
= 0;BackendList
[i
].Init
;i
++)
263 BackendList
[i
].Init(&BackendList
[i
].Funcs
);
268 ALCchar
*AppendDeviceList(char *name
)
271 ALCchar
*ret
= alcDeviceList
+pos
;
272 pos
+= snprintf(alcDeviceList
+pos
, sizeof(alcDeviceList
)-pos
, "%s", name
) + 1;
276 ALCchar
*AppendAllDeviceList(char *name
)
279 ALCchar
*ret
= alcAllDeviceList
+pos
;
280 pos
+= snprintf(alcAllDeviceList
+pos
, sizeof(alcAllDeviceList
)-pos
, "%s", name
) + 1;
284 ALCchar
*AppendCaptureDeviceList(char *name
)
287 ALCchar
*ret
= alcCaptureDeviceList
+pos
;
288 pos
+= snprintf(alcCaptureDeviceList
+pos
, sizeof(alcCaptureDeviceList
)-pos
, "%s", name
) + 1;
295 Check pContext is a valid Context pointer
297 static ALCboolean
IsContext(ALCcontext
*pContext
)
299 ALCcontext
*pTempContext
;
301 pTempContext
= g_pContextList
;
302 while (pTempContext
&& pTempContext
!= pContext
)
303 pTempContext
= pTempContext
->next
;
305 return (pTempContext
? ALC_TRUE
: ALC_FALSE
);
312 Store latest ALC Error
314 ALCvoid
SetALCError(ALenum errorCode
)
316 g_eLastContextError
= errorCode
;
325 ALCvoid
SuspendContext(ALCcontext
*pContext
)
328 EnterCriticalSection(&_alMutex
);
337 ALCvoid
ProcessContext(ALCcontext
*pContext
)
340 LeaveCriticalSection(&_alMutex
);
347 Initialize Context variables
349 static ALvoid
InitContext(ALCcontext
*pContext
)
353 //Initialise listener
354 pContext
->Listener
.Gain
= 1.0f
;
355 pContext
->Listener
.MetersPerUnit
= 1.0f
;
356 pContext
->Listener
.Position
[0] = 0.0f
;
357 pContext
->Listener
.Position
[1] = 0.0f
;
358 pContext
->Listener
.Position
[2] = 0.0f
;
359 pContext
->Listener
.Velocity
[0] = 0.0f
;
360 pContext
->Listener
.Velocity
[1] = 0.0f
;
361 pContext
->Listener
.Velocity
[2] = 0.0f
;
362 pContext
->Listener
.Forward
[0] = 0.0f
;
363 pContext
->Listener
.Forward
[1] = 0.0f
;
364 pContext
->Listener
.Forward
[2] = -1.0f
;
365 pContext
->Listener
.Up
[0] = 0.0f
;
366 pContext
->Listener
.Up
[1] = 1.0f
;
367 pContext
->Listener
.Up
[2] = 0.0f
;
370 pContext
->LastError
= AL_NO_ERROR
;
371 pContext
->InUse
= AL_FALSE
;
374 pContext
->Frequency
= pContext
->Device
->Frequency
;
377 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
378 pContext
->DopplerFactor
= 1.0f
;
379 pContext
->DopplerVelocity
= 1.0f
;
380 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
382 pContext
->lNumStereoSources
= 1;
383 pContext
->lNumMonoSources
= pContext
->Device
->MaxNoOfSources
- pContext
->lNumStereoSources
;
385 strcpy(pContext
->ExtensionList
, "AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_OFFSET");
387 level
= GetConfigValueInt(NULL
, "cf_level", 0);
388 if(level
> 0 && level
<= 6)
390 pContext
->bs2b
= calloc(1, sizeof(*pContext
->bs2b
));
391 bs2b_set_srate(pContext
->bs2b
, pContext
->Frequency
);
392 bs2b_set_level(pContext
->bs2b
, level
);
400 Clean up Context, destroy any remaining Sources
402 static ALCvoid
ExitContext(ALCcontext
*pContext
)
406 ALsource
*ALTempSource
;
409 if (pContext
->SourceCount
>0)
410 AL_PRINT("alcDestroyContext() %d Source(s) NOT deleted\n", pContext
->SourceCount
);
413 // Free all the Sources still remaining
414 ALSource
= pContext
->Source
;
415 for (i
= 0; i
< pContext
->SourceCount
; i
++)
417 ALTempSource
= ALSource
->next
;
418 ALTHUNK_REMOVEENTRY(ALSource
->source
);
419 memset(ALSource
, 0, sizeof(ALsource
));
421 ALSource
= ALTempSource
;
425 pContext
->LastError
= AL_NO_ERROR
;
426 pContext
->InUse
= AL_FALSE
;
428 free(pContext
->bs2b
);
429 pContext
->bs2b
= NULL
;
432 ///////////////////////////////////////////////////////
435 ///////////////////////////////////////////////////////
436 // ALC Functions calls
439 // This should probably move to another c file but for now ...
440 ALCAPI ALCdevice
* ALCAPIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
442 ALCboolean DeviceFound
= ALC_FALSE
;
443 ALCdevice
*pDevice
= NULL
;
448 if(deviceName
&& !deviceName
[0])
451 pDevice
= malloc(sizeof(ALCdevice
));
456 //Initialise device structure
457 memset(pDevice
, 0, sizeof(ALCdevice
));
460 pDevice
->InUse
= AL_TRUE
;
461 pDevice
->IsCaptureDevice
= AL_TRUE
;
463 pDevice
->Frequency
= frequency
;
464 pDevice
->Format
= format
;
465 pDevice
->Channels
= aluChannelsFromFormat(format
);
466 pDevice
->FrameSize
= aluBytesFromFormat(format
) *
469 for(i
= 0;BackendList
[i
].Init
;i
++)
471 pDevice
->Funcs
= &BackendList
[i
].Funcs
;
472 if(ALCdevice_OpenCapture(pDevice
, deviceName
, frequency
, format
, SampleSize
))
474 SuspendContext(NULL
);
475 pDevice
->next
= g_pDeviceList
;
476 g_pDeviceList
= pDevice
;
478 ProcessContext(NULL
);
480 DeviceFound
= ALC_TRUE
;
486 SetALCError(ALC_INVALID_VALUE
);
495 SetALCError(ALC_OUT_OF_MEMORY
);
500 ALCAPI ALCboolean ALCAPIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
502 ALCboolean bReturn
= ALC_FALSE
;
505 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
507 SuspendContext(NULL
);
509 list
= &g_pDeviceList
;
510 while(*list
!= pDevice
)
511 list
= &(*list
)->next
;
513 *list
= (*list
)->next
;
516 ProcessContext(NULL
);
518 ALCdevice_CloseCapture(pDevice
);
524 SetALCError(ALC_INVALID_DEVICE
);
529 ALCAPI
void ALCAPIENTRY
alcCaptureStart(ALCdevice
*pDevice
)
531 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
532 ALCdevice_StartCapture(pDevice
);
534 SetALCError(ALC_INVALID_DEVICE
);
537 ALCAPI
void ALCAPIENTRY
alcCaptureStop(ALCdevice
*pDevice
)
539 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
540 ALCdevice_StopCapture(pDevice
);
542 SetALCError(ALC_INVALID_DEVICE
);
545 ALCAPI
void ALCAPIENTRY
alcCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCsizei lSamples
)
547 if ((pDevice
) && (pDevice
->IsCaptureDevice
))
548 ALCdevice_CaptureSamples(pDevice
, pBuffer
, lSamples
);
550 SetALCError(ALC_INVALID_DEVICE
);
556 Return last ALC generated error code
558 ALCAPI ALCenum ALCAPIENTRY
alcGetError(ALCdevice
*device
)
564 errorCode
= g_eLastContextError
;
565 g_eLastContextError
= ALC_NO_ERROR
;
575 ALCAPI ALCvoid ALCAPIENTRY
alcSuspendContext(ALCcontext
*pContext
)
577 // Not a lot happens here !
587 ALCAPI ALCvoid ALCAPIENTRY
alcProcessContext(ALCcontext
*pContext
)
589 // Not a lot happens here !
597 Returns information about the Device, and error strings
599 ALCAPI
const ALCchar
* ALCAPIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
601 const ALCchar
*value
= NULL
;
611 case ALC_INVALID_ENUM
:
612 value
= alcErrInvalidEnum
;
615 case ALC_INVALID_VALUE
:
616 value
= alcErrInvalidValue
;
619 case ALC_INVALID_DEVICE
:
620 value
= alcErrInvalidDevice
;
623 case ALC_INVALID_CONTEXT
:
624 value
= alcErrInvalidContext
;
627 case ALC_OUT_OF_MEMORY
:
628 value
= alcErrOutOfMemory
;
631 case ALC_DEFAULT_DEVICE_SPECIFIER
:
632 value
= alcDefaultDeviceSpecifier
;
635 case ALC_DEVICE_SPECIFIER
:
637 value
= pDevice
->szDeviceName
;
639 value
= alcDeviceList
;
642 case ALC_ALL_DEVICES_SPECIFIER
:
643 value
= alcAllDeviceList
;
646 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
647 value
= alcDefaultAllDeviceSpecifier
;
650 case ALC_CAPTURE_DEVICE_SPECIFIER
:
652 value
= pDevice
->szDeviceName
;
654 value
= alcCaptureDeviceList
;
657 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
658 value
= alcCaptureDefaultDeviceSpecifier
;
662 value
= alcExtensionList
;
666 SetALCError(ALC_INVALID_ENUM
);
677 Returns information about the Device and the version of Open AL
679 ALCAPI ALCvoid ALCAPIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
683 if ((device
)&&(device
->IsCaptureDevice
))
685 SuspendContext(NULL
);
690 case ALC_CAPTURE_SAMPLES
:
691 if ((size
) && (data
))
692 *data
= ALCdevice_AvailableSamples(device
);
694 SetALCError(ALC_INVALID_VALUE
);
698 SetALCError(ALC_INVALID_ENUM
);
702 ProcessContext(NULL
);
711 case ALC_MAJOR_VERSION
:
713 SetALCError(ALC_INVALID_VALUE
);
715 *data
= alcMajorVersion
;
718 case ALC_MINOR_VERSION
:
720 SetALCError(ALC_INVALID_VALUE
);
722 *data
= alcMinorVersion
;
725 case ALC_EFX_MAJOR_VERSION
:
727 SetALCError(ALC_INVALID_VALUE
);
729 *data
= alcEFXMajorVersion
;
732 case ALC_EFX_MINOR_VERSION
:
734 SetALCError(ALC_INVALID_VALUE
);
736 *data
= alcEFXMinorVersion
;
739 case ALC_MAX_AUXILIARY_SENDS
:
741 SetALCError(ALC_INVALID_VALUE
);
746 case ALC_ATTRIBUTES_SIZE
:
748 SetALCError(ALC_INVALID_DEVICE
);
750 SetALCError(ALC_INVALID_VALUE
);
755 case ALC_ALL_ATTRIBUTES
:
757 SetALCError(ALC_INVALID_DEVICE
);
759 SetALCError(ALC_INVALID_VALUE
);
764 data
[i
++] = ALC_FREQUENCY
;
765 data
[i
++] = device
->Frequency
;
767 data
[i
++] = ALC_REFRESH
;
768 data
[i
++] = device
->Frequency
/ device
->UpdateFreq
;
770 data
[i
++] = ALC_SYNC
;
771 data
[i
++] = ALC_FALSE
;
773 SuspendContext(NULL
);
774 if(device
->Context
&& size
>= 12)
776 data
[i
++] = ALC_MONO_SOURCES
;
777 data
[i
++] = device
->Context
->lNumMonoSources
;
779 data
[i
++] = ALC_STEREO_SOURCES
;
780 data
[i
++] = device
->Context
->lNumStereoSources
;
782 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
783 data
[i
++] = MAX_SENDS
;
785 ProcessContext(NULL
);
793 SetALCError(ALC_INVALID_DEVICE
);
795 SetALCError(ALC_INVALID_VALUE
);
797 *data
= device
->Frequency
;
802 SetALCError(ALC_INVALID_DEVICE
);
804 SetALCError(ALC_INVALID_VALUE
);
806 *data
= device
->Frequency
/ device
->UpdateFreq
;
811 SetALCError(ALC_INVALID_DEVICE
);
813 SetALCError(ALC_INVALID_VALUE
);
818 case ALC_MONO_SOURCES
:
819 if(!device
|| !device
->Context
)
820 SetALCError(ALC_INVALID_DEVICE
);
822 SetALCError(ALC_INVALID_VALUE
);
824 *data
= device
->Context
->lNumMonoSources
;
827 case ALC_STEREO_SOURCES
:
828 if(!device
|| !device
->Context
)
829 SetALCError(ALC_INVALID_DEVICE
);
831 SetALCError(ALC_INVALID_VALUE
);
833 *data
= device
->Context
->lNumStereoSources
;
837 SetALCError(ALC_INVALID_ENUM
);
842 SetALCError(ALC_INVALID_VALUE
);
850 alcIsExtensionPresent
852 Determines if there is support for a particular extension
854 ALCAPI ALCboolean ALCAPIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
856 ALCboolean bResult
= ALC_FALSE
;
863 while(alcExtensions
[i
].extName
&&
864 strcasecmp(alcExtensions
[i
].extName
,extName
) != 0)
867 if (alcExtensions
[i
].extName
)
871 SetALCError(ALC_INVALID_VALUE
);
880 Retrieves the function address for a particular extension function
882 ALCAPI ALCvoid
* ALCAPIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
884 ALCvoid
*pFunction
= NULL
;
891 while(alcFunctions
[i
].funcName
&&
892 strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
894 pFunction
= alcFunctions
[i
].address
;
897 SetALCError(ALC_INVALID_VALUE
);
906 Get the value for a particular ALC Enumerated Value
908 ALCAPI ALCenum ALCAPIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
915 while ((enumeration
[i
].enumName
)&&(strcmp(enumeration
[i
].enumName
,enumName
)))
917 val
= enumeration
[i
].value
;
919 if(!enumeration
[i
].enumName
)
920 SetALCError(ALC_INVALID_VALUE
);
929 Create and attach a Context to a particular Device.
931 ALCAPI ALCcontext
* ALCAPIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
933 ALCcontext
*ALContext
= NULL
;
934 ALuint ulAttributeIndex
, ulRequestedStereoSources
;
936 if ((device
)&&(!device
->IsCaptureDevice
))
938 // Reset Context Last Error code
939 g_eLastContextError
= ALC_NO_ERROR
;
941 // Current implementation only allows one Context per Device
944 ALContext
= calloc(1, sizeof(ALCcontext
));
947 SetALCError(ALC_OUT_OF_MEMORY
);
951 ALContext
->Device
= device
;
952 InitContext(ALContext
);
954 device
->Context
= ALContext
;
956 SuspendContext(NULL
);
958 ALContext
->next
= g_pContextList
;
959 g_pContextList
= ALContext
;
962 ProcessContext(NULL
);
964 // Check for Voice Count attributes
967 ulAttributeIndex
= 0;
968 while ((ulAttributeIndex
< 10) && (attrList
[ulAttributeIndex
]))
970 if (attrList
[ulAttributeIndex
] == ALC_STEREO_SOURCES
)
972 ulRequestedStereoSources
= attrList
[ulAttributeIndex
+ 1];
974 if (ulRequestedStereoSources
> ALContext
->Device
->MaxNoOfSources
)
975 ulRequestedStereoSources
= ALContext
->Device
->MaxNoOfSources
;
977 ALContext
->lNumStereoSources
= ulRequestedStereoSources
;
978 ALContext
->lNumMonoSources
= ALContext
->Device
->MaxNoOfSources
- ALContext
->lNumStereoSources
;
982 ulAttributeIndex
+= 2;
988 SetALCError(ALC_INVALID_VALUE
);
993 SetALCError(ALC_INVALID_DEVICE
);
1004 ALCAPI ALCvoid ALCAPIENTRY
alcDestroyContext(ALCcontext
*context
)
1008 // Lock context list
1009 SuspendContext(NULL
);
1011 if (IsContext(context
))
1014 SuspendContext(context
);
1016 context
->Device
->Context
= NULL
;
1018 list
= &g_pContextList
;
1019 while(*list
!= context
)
1020 list
= &(*list
)->next
;
1022 *list
= (*list
)->next
;
1026 ProcessContext(context
);
1028 ExitContext(context
);
1030 // Free memory (MUST do this after ProcessContext)
1031 memset(context
, 0, sizeof(ALCcontext
));
1035 SetALCError(ALC_INVALID_CONTEXT
);
1037 ProcessContext(NULL
);
1042 alcGetCurrentContext
1044 Returns the currently active Context
1046 ALCAPI ALCcontext
* ALCAPIENTRY
alcGetCurrentContext(ALCvoid
)
1048 ALCcontext
*pContext
= NULL
;
1050 SuspendContext(NULL
);
1052 pContext
= g_pContextList
;
1053 while ((pContext
) && (!pContext
->InUse
))
1054 pContext
= pContext
->next
;
1056 ProcessContext(NULL
);
1063 alcGetContextsDevice
1065 Returns the Device that a particular Context is attached to
1067 ALCAPI ALCdevice
* ALCAPIENTRY
alcGetContextsDevice(ALCcontext
*pContext
)
1069 ALCdevice
*pDevice
= NULL
;
1071 SuspendContext(NULL
);
1072 if (IsContext(pContext
))
1073 pDevice
= pContext
->Device
;
1075 SetALCError(ALC_INVALID_CONTEXT
);
1076 ProcessContext(NULL
);
1083 alcMakeContextCurrent
1085 Makes the given Context the active Context
1087 ALCAPI ALCboolean ALCAPIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
1089 ALCcontext
*ALContext
;
1090 ALboolean bReturn
= AL_TRUE
;
1092 SuspendContext(NULL
);
1094 // context must be a valid Context or NULL
1095 if ((IsContext(context
)) || (context
== NULL
))
1097 if ((ALContext
=alcGetCurrentContext()))
1099 SuspendContext(ALContext
);
1100 ALContext
->InUse
=AL_FALSE
;
1101 ProcessContext(ALContext
);
1104 if ((ALContext
=context
) && (ALContext
->Device
))
1106 SuspendContext(ALContext
);
1107 ALContext
->InUse
=AL_TRUE
;
1108 ProcessContext(ALContext
);
1113 SetALCError(ALC_INVALID_CONTEXT
);
1117 ProcessContext(NULL
);
1126 Open the Device specified.
1128 ALCAPI ALCdevice
* ALCAPIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
1130 ALboolean bDeviceFound
= AL_FALSE
;
1136 if(deviceName
&& !deviceName
[0])
1139 device
= malloc(sizeof(ALCdevice
));
1144 //Initialise device structure
1145 memset(device
, 0, sizeof(ALCdevice
));
1148 device
->InUse
= AL_TRUE
;
1149 device
->IsCaptureDevice
= AL_FALSE
;
1152 device
->Frequency
= GetConfigValueInt(NULL
, "frequency", SWMIXER_OUTPUT_RATE
);
1153 if((ALint
)device
->Frequency
<= 0)
1154 device
->Frequency
= SWMIXER_OUTPUT_RATE
;
1156 fmt
= GetConfigValue(NULL
, "format", "AL_FORMAT_STEREO16");
1158 device
->Format
= alGetEnumValue(fmt
);
1160 device
->Channels
= aluChannelsFromFormat(device
->Format
);
1161 if(!device
->Channels
)
1163 device
->Format
= AL_FORMAT_STEREO16
;
1164 device
->Channels
= 2;
1165 device
->FrameSize
= 4;
1168 device
->FrameSize
= aluBytesFromFormat(device
->Format
) *
1171 device
->UpdateFreq
= GetConfigValueInt(NULL
, "refresh", 0);
1172 if((ALint
)device
->UpdateFreq
<= 0)
1173 device
->UpdateFreq
= 8192 * device
->Frequency
/ 22050;
1175 // Find a playback device to open
1176 for(i
= 0;BackendList
[i
].Init
;i
++)
1178 device
->Funcs
= &BackendList
[i
].Funcs
;
1179 if(ALCdevice_OpenPlayback(device
, deviceName
))
1181 SuspendContext(NULL
);
1182 device
->next
= g_pDeviceList
;
1183 g_pDeviceList
= device
;
1185 ProcessContext(NULL
);
1187 bDeviceFound
= AL_TRUE
;
1194 // No suitable output device found
1207 Close the specified Device
1209 ALCAPI ALCboolean ALCAPIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
1211 ALCboolean bReturn
= ALC_FALSE
;
1214 if ((pDevice
)&&(!pDevice
->IsCaptureDevice
))
1216 SuspendContext(NULL
);
1218 list
= &g_pDeviceList
;
1219 while(*list
!= pDevice
)
1220 list
= &(*list
)->next
;
1222 *list
= (*list
)->next
;
1225 ProcessContext(NULL
);
1227 if(pDevice
->Context
)
1228 alcDestroyContext(pDevice
->Context
);
1229 ALCdevice_ClosePlayback(pDevice
);
1231 //Release device structure
1232 memset(pDevice
, 0, sizeof(ALCdevice
));
1238 SetALCError(ALC_INVALID_DEVICE
);
1244 ALCvoid
ReleaseALC(ALCvoid
)
1249 if(g_ulContextCount
> 0)
1250 AL_PRINT("exit() %u device(s) and %u context(s) NOT deleted\n", g_ulDeviceCount
, g_ulContextCount
);
1253 while(g_pDeviceList
)
1255 Dev
= g_pDeviceList
;
1256 g_pDeviceList
= g_pDeviceList
->next
;
1257 if(Dev
->IsCaptureDevice
)
1258 alcCaptureCloseDevice(Dev
);
1260 alcCloseDevice(Dev
);
1264 ///////////////////////////////////////////////////////