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"
36 #include "alAuxEffectSlot.h"
39 ///////////////////////////////////////////////////////
44 ///////////////////////////////////////////////////////
47 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
50 void (*Init
)(BackendFuncs
*);
54 { "alsa", alc_alsa_init
, EmptyFuncs
},
57 { "oss", alc_oss_init
, EmptyFuncs
},
60 { "dsound", alcDSoundInit
, EmptyFuncs
},
63 { "winmm", alcWinMMInit
, EmptyFuncs
},
66 { "wave", alc_wave_init
, EmptyFuncs
},
68 { NULL
, NULL
, EmptyFuncs
}
72 ///////////////////////////////////////////////////////
74 #define ALC_EFX_MAJOR_VERSION 0x20001
75 #define ALC_EFX_MINOR_VERSION 0x20002
76 #define ALC_MAX_AUXILIARY_SENDS 0x20003
78 ///////////////////////////////////////////////////////
79 // STRING and EXTENSIONS
81 typedef struct ALCextension_struct
87 typedef struct ALCfunction_struct
93 static ALCextension alcExtensions
[] = {
94 { "ALC_ENUMERATE_ALL_EXT", (ALvoid
*) NULL
},
95 { "ALC_ENUMERATION_EXT", (ALvoid
*) NULL
},
96 { "ALC_EXT_CAPTURE", (ALvoid
*) NULL
},
97 { "ALC_EXT_EFX", (ALvoid
*) NULL
},
98 { NULL
, (ALvoid
*) NULL
}
101 static ALCfunction alcFunctions
[] = {
102 { "alcCreateContext", (ALvoid
*) alcCreateContext
},
103 { "alcMakeContextCurrent", (ALvoid
*) alcMakeContextCurrent
},
104 { "alcProcessContext", (ALvoid
*) alcProcessContext
},
105 { "alcSuspendContext", (ALvoid
*) alcSuspendContext
},
106 { "alcDestroyContext", (ALvoid
*) alcDestroyContext
},
107 { "alcGetCurrentContext", (ALvoid
*) alcGetCurrentContext
},
108 { "alcGetContextsDevice", (ALvoid
*) alcGetContextsDevice
},
109 { "alcOpenDevice", (ALvoid
*) alcOpenDevice
},
110 { "alcCloseDevice", (ALvoid
*) alcCloseDevice
},
111 { "alcGetError", (ALvoid
*) alcGetError
},
112 { "alcIsExtensionPresent", (ALvoid
*) alcIsExtensionPresent
},
113 { "alcGetProcAddress", (ALvoid
*) alcGetProcAddress
},
114 { "alcGetEnumValue", (ALvoid
*) alcGetEnumValue
},
115 { "alcGetString", (ALvoid
*) alcGetString
},
116 { "alcGetIntegerv", (ALvoid
*) alcGetIntegerv
},
117 { "alcCaptureOpenDevice", (ALvoid
*) alcCaptureOpenDevice
},
118 { "alcCaptureCloseDevice", (ALvoid
*) alcCaptureCloseDevice
},
119 { "alcCaptureStart", (ALvoid
*) alcCaptureStart
},
120 { "alcCaptureStop", (ALvoid
*) alcCaptureStop
},
121 { "alcCaptureSamples", (ALvoid
*) alcCaptureSamples
},
122 { NULL
, (ALvoid
*) NULL
}
125 static ALenums enumeration
[]={
127 { (ALchar
*)"ALC_INVALID", ALC_INVALID
},
128 { (ALchar
*)"ALC_FALSE", ALC_FALSE
},
129 { (ALchar
*)"ALC_TRUE", ALC_TRUE
},
132 { (ALchar
*)"ALC_MAJOR_VERSION", ALC_MAJOR_VERSION
},
133 { (ALchar
*)"ALC_MINOR_VERSION", ALC_MINOR_VERSION
},
134 { (ALchar
*)"ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE
},
135 { (ALchar
*)"ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES
},
136 { (ALchar
*)"ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER
},
137 { (ALchar
*)"ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER
},
138 { (ALchar
*)"ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER
},
139 { (ALchar
*)"ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER
},
140 { (ALchar
*)"ALC_EXTENSIONS", ALC_EXTENSIONS
},
141 { (ALchar
*)"ALC_FREQUENCY", ALC_FREQUENCY
},
142 { (ALchar
*)"ALC_REFRESH", ALC_REFRESH
},
143 { (ALchar
*)"ALC_SYNC", ALC_SYNC
},
144 { (ALchar
*)"ALC_MONO_SOURCES", ALC_MONO_SOURCES
},
145 { (ALchar
*)"ALC_STEREO_SOURCES", ALC_STEREO_SOURCES
},
146 { (ALchar
*)"ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER
},
147 { (ALchar
*)"ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
},
148 { (ALchar
*)"ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES
},
151 { (ALchar
*)"ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION
},
152 { (ALchar
*)"ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION
},
153 { (ALchar
*)"ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS
},
156 { (ALchar
*)"ALC_NO_ERROR", ALC_NO_ERROR
},
157 { (ALchar
*)"ALC_INVALID_DEVICE", ALC_INVALID_DEVICE
},
158 { (ALchar
*)"ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT
},
159 { (ALchar
*)"ALC_INVALID_ENUM", ALC_INVALID_ENUM
},
160 { (ALchar
*)"ALC_INVALID_VALUE", ALC_INVALID_VALUE
},
161 { (ALchar
*)"ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY
},
162 { (ALchar
*)NULL
, (ALenum
)0 }
165 static const ALCchar alcNoError
[] = "No Error";
166 static const ALCchar alcErrInvalidDevice
[] = "Invalid Device";
167 static const ALCchar alcErrInvalidContext
[] = "Invalid Context";
168 static const ALCchar alcErrInvalidEnum
[] = "Invalid Enum";
169 static const ALCchar alcErrInvalidValue
[] = "Invalid Value";
170 static const ALCchar alcErrOutOfMemory
[] = "Out of Memory";
173 static ALCchar alcDeviceList
[2048];
174 static ALCchar alcAllDeviceList
[2048];
175 static ALCchar alcCaptureDeviceList
[2048];
176 // Default is always the first in the list
177 static ALCchar
*alcDefaultDeviceSpecifier
= alcDeviceList
;
178 static ALCchar
*alcDefaultAllDeviceSpecifier
= alcAllDeviceList
;
179 static ALCchar
*alcCaptureDefaultDeviceSpecifier
= alcCaptureDeviceList
;
182 static ALCchar alcExtensionList
[] = "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_EFX";
183 static ALCint alcMajorVersion
= 1;
184 static ALCint alcMinorVersion
= 1;
186 static ALCint alcEFXMajorVersion
= 1;
187 static ALCint alcEFXMinorVersion
= 0;
189 ///////////////////////////////////////////////////////
192 ///////////////////////////////////////////////////////
195 static ALCdevice
*g_pDeviceList
= NULL
;
196 static ALCuint g_ulDeviceCount
= 0;
199 static ALCcontext
*g_pContextList
= NULL
;
200 static ALCuint g_ulContextCount
= 0;
203 static ALCenum g_eLastContextError
= ALC_NO_ERROR
;
205 ///////////////////////////////////////////////////////
208 ///////////////////////////////////////////////////////
209 // ALC Related helper functions
211 static void InitAL(void)
219 InitializeCriticalSection(&_alMutex
);
223 devs
= GetConfigValue(NULL
, "drivers", "");
228 const char *next
= devs
;
234 next
= strchr(devs
, ',');
236 if(!devs
[0] || devs
[0] == ',')
239 len
= (next
? ((size_t)(next
-devs
)) : strlen(devs
));
240 for(n
= i
;BackendList
[n
].Init
;n
++)
242 if(len
== strlen(BackendList
[n
].name
) &&
243 strncmp(BackendList
[n
].name
, devs
, len
) == 0)
245 const char *name
= BackendList
[i
].name
;
246 void (*Init
)(BackendFuncs
*) = BackendList
[i
].Init
;
248 BackendList
[i
].name
= BackendList
[n
].name
;
249 BackendList
[i
].Init
= BackendList
[n
].Init
;
251 BackendList
[n
].name
= name
;
252 BackendList
[n
].Init
= Init
;
259 BackendList
[i
].name
= NULL
;
260 BackendList
[i
].Init
= NULL
;
263 for(i
= 0;BackendList
[i
].Init
;i
++)
264 BackendList
[i
].Init(&BackendList
[i
].Funcs
);
269 ALCchar
*AppendDeviceList(char *name
)
272 ALCchar
*ret
= alcDeviceList
+pos
;
273 pos
+= snprintf(alcDeviceList
+pos
, sizeof(alcDeviceList
)-pos
, "%s", name
) + 1;
277 ALCchar
*AppendAllDeviceList(char *name
)
280 ALCchar
*ret
= alcAllDeviceList
+pos
;
281 pos
+= snprintf(alcAllDeviceList
+pos
, sizeof(alcAllDeviceList
)-pos
, "%s", name
) + 1;
285 ALCchar
*AppendCaptureDeviceList(char *name
)
288 ALCchar
*ret
= alcCaptureDeviceList
+pos
;
289 pos
+= snprintf(alcCaptureDeviceList
+pos
, sizeof(alcCaptureDeviceList
)-pos
, "%s", name
) + 1;
296 Check pContext is a valid Context pointer
298 static ALCboolean
IsContext(ALCcontext
*pContext
)
300 ALCcontext
*pTempContext
;
302 pTempContext
= g_pContextList
;
303 while (pTempContext
&& pTempContext
!= pContext
)
304 pTempContext
= pTempContext
->next
;
306 return (pTempContext
? ALC_TRUE
: ALC_FALSE
);
313 Store latest ALC Error
315 ALCvoid
SetALCError(ALenum errorCode
)
317 g_eLastContextError
= errorCode
;
326 ALCvoid
SuspendContext(ALCcontext
*pContext
)
329 EnterCriticalSection(&_alMutex
);
338 ALCvoid
ProcessContext(ALCcontext
*pContext
)
341 LeaveCriticalSection(&_alMutex
);
348 Initialize Context variables
350 static ALvoid
InitContext(ALCcontext
*pContext
)
354 //Initialise listener
355 pContext
->Listener
.Gain
= 1.0f
;
356 pContext
->Listener
.MetersPerUnit
= 1.0f
;
357 pContext
->Listener
.Position
[0] = 0.0f
;
358 pContext
->Listener
.Position
[1] = 0.0f
;
359 pContext
->Listener
.Position
[2] = 0.0f
;
360 pContext
->Listener
.Velocity
[0] = 0.0f
;
361 pContext
->Listener
.Velocity
[1] = 0.0f
;
362 pContext
->Listener
.Velocity
[2] = 0.0f
;
363 pContext
->Listener
.Forward
[0] = 0.0f
;
364 pContext
->Listener
.Forward
[1] = 0.0f
;
365 pContext
->Listener
.Forward
[2] = -1.0f
;
366 pContext
->Listener
.Up
[0] = 0.0f
;
367 pContext
->Listener
.Up
[1] = 1.0f
;
368 pContext
->Listener
.Up
[2] = 0.0f
;
371 pContext
->LastError
= AL_NO_ERROR
;
372 pContext
->InUse
= AL_FALSE
;
375 pContext
->Frequency
= pContext
->Device
->Frequency
;
378 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
379 pContext
->DopplerFactor
= 1.0f
;
380 pContext
->DopplerVelocity
= 1.0f
;
381 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
383 pContext
->lNumStereoSources
= 1;
384 pContext
->lNumMonoSources
= pContext
->Device
->MaxNoOfSources
- pContext
->lNumStereoSources
;
386 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");
388 level
= GetConfigValueInt(NULL
, "cf_level", 0);
389 if(level
> 0 && level
<= 6)
391 pContext
->bs2b
= calloc(1, sizeof(*pContext
->bs2b
));
392 bs2b_set_srate(pContext
->bs2b
, pContext
->Frequency
);
393 bs2b_set_level(pContext
->bs2b
, level
);
401 Clean up Context, destroy any remaining Sources
403 static ALCvoid
ExitContext(ALCcontext
*pContext
)
407 ALsource
*ALTempSource
;
410 if (pContext
->SourceCount
>0)
411 AL_PRINT("alcDestroyContext() %d Source(s) NOT deleted\n", pContext
->SourceCount
);
414 // Free all the Sources still remaining
415 ALSource
= pContext
->Source
;
416 for (i
= 0; i
< pContext
->SourceCount
; i
++)
418 ALTempSource
= ALSource
->next
;
419 ALTHUNK_REMOVEENTRY(ALSource
->source
);
420 memset(ALSource
, 0, sizeof(ALsource
));
422 ALSource
= ALTempSource
;
426 pContext
->LastError
= AL_NO_ERROR
;
427 pContext
->InUse
= AL_FALSE
;
429 free(pContext
->bs2b
);
430 pContext
->bs2b
= NULL
;
433 ///////////////////////////////////////////////////////
436 ///////////////////////////////////////////////////////
437 // ALC Functions calls
440 // This should probably move to another c file but for now ...
441 ALCAPI ALCdevice
* ALCAPIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
443 ALCboolean DeviceFound
= ALC_FALSE
;
444 ALCdevice
*pDevice
= NULL
;
449 if(deviceName
&& !deviceName
[0])
452 pDevice
= malloc(sizeof(ALCdevice
));
457 //Initialise device structure
458 memset(pDevice
, 0, sizeof(ALCdevice
));
461 pDevice
->InUse
= AL_TRUE
;
462 pDevice
->IsCaptureDevice
= AL_TRUE
;
464 pDevice
->Frequency
= frequency
;
465 pDevice
->Format
= format
;
466 pDevice
->Channels
= aluChannelsFromFormat(format
);
467 pDevice
->FrameSize
= aluBytesFromFormat(format
) *
470 for(i
= 0;BackendList
[i
].Init
;i
++)
472 pDevice
->Funcs
= &BackendList
[i
].Funcs
;
473 if(ALCdevice_OpenCapture(pDevice
, deviceName
, frequency
, format
, SampleSize
))
475 SuspendContext(NULL
);
476 pDevice
->next
= g_pDeviceList
;
477 g_pDeviceList
= pDevice
;
479 ProcessContext(NULL
);
481 DeviceFound
= ALC_TRUE
;
487 SetALCError(ALC_INVALID_VALUE
);
496 SetALCError(ALC_OUT_OF_MEMORY
);
501 ALCAPI ALCboolean ALCAPIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
503 ALCboolean bReturn
= ALC_FALSE
;
506 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
508 SuspendContext(NULL
);
510 list
= &g_pDeviceList
;
511 while(*list
!= pDevice
)
512 list
= &(*list
)->next
;
514 *list
= (*list
)->next
;
517 ProcessContext(NULL
);
519 ALCdevice_CloseCapture(pDevice
);
525 SetALCError(ALC_INVALID_DEVICE
);
530 ALCAPI
void ALCAPIENTRY
alcCaptureStart(ALCdevice
*pDevice
)
532 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
533 ALCdevice_StartCapture(pDevice
);
535 SetALCError(ALC_INVALID_DEVICE
);
538 ALCAPI
void ALCAPIENTRY
alcCaptureStop(ALCdevice
*pDevice
)
540 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
541 ALCdevice_StopCapture(pDevice
);
543 SetALCError(ALC_INVALID_DEVICE
);
546 ALCAPI
void ALCAPIENTRY
alcCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCsizei lSamples
)
548 if ((pDevice
) && (pDevice
->IsCaptureDevice
))
549 ALCdevice_CaptureSamples(pDevice
, pBuffer
, lSamples
);
551 SetALCError(ALC_INVALID_DEVICE
);
557 Return last ALC generated error code
559 ALCAPI ALCenum ALCAPIENTRY
alcGetError(ALCdevice
*device
)
565 errorCode
= g_eLastContextError
;
566 g_eLastContextError
= ALC_NO_ERROR
;
576 ALCAPI ALCvoid ALCAPIENTRY
alcSuspendContext(ALCcontext
*pContext
)
578 // Not a lot happens here !
588 ALCAPI ALCvoid ALCAPIENTRY
alcProcessContext(ALCcontext
*pContext
)
590 // Not a lot happens here !
598 Returns information about the Device, and error strings
600 ALCAPI
const ALCchar
* ALCAPIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
602 const ALCchar
*value
= NULL
;
612 case ALC_INVALID_ENUM
:
613 value
= alcErrInvalidEnum
;
616 case ALC_INVALID_VALUE
:
617 value
= alcErrInvalidValue
;
620 case ALC_INVALID_DEVICE
:
621 value
= alcErrInvalidDevice
;
624 case ALC_INVALID_CONTEXT
:
625 value
= alcErrInvalidContext
;
628 case ALC_OUT_OF_MEMORY
:
629 value
= alcErrOutOfMemory
;
632 case ALC_DEFAULT_DEVICE_SPECIFIER
:
633 value
= alcDefaultDeviceSpecifier
;
636 case ALC_DEVICE_SPECIFIER
:
638 value
= pDevice
->szDeviceName
;
640 value
= alcDeviceList
;
643 case ALC_ALL_DEVICES_SPECIFIER
:
644 value
= alcAllDeviceList
;
647 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
648 value
= alcDefaultAllDeviceSpecifier
;
651 case ALC_CAPTURE_DEVICE_SPECIFIER
:
653 value
= pDevice
->szDeviceName
;
655 value
= alcCaptureDeviceList
;
658 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
659 value
= alcCaptureDefaultDeviceSpecifier
;
663 value
= alcExtensionList
;
667 SetALCError(ALC_INVALID_ENUM
);
678 Returns information about the Device and the version of Open AL
680 ALCAPI ALCvoid ALCAPIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
684 if ((device
)&&(device
->IsCaptureDevice
))
686 SuspendContext(NULL
);
691 case ALC_CAPTURE_SAMPLES
:
692 if ((size
) && (data
))
693 *data
= ALCdevice_AvailableSamples(device
);
695 SetALCError(ALC_INVALID_VALUE
);
699 SetALCError(ALC_INVALID_ENUM
);
703 ProcessContext(NULL
);
712 case ALC_MAJOR_VERSION
:
714 SetALCError(ALC_INVALID_VALUE
);
716 *data
= alcMajorVersion
;
719 case ALC_MINOR_VERSION
:
721 SetALCError(ALC_INVALID_VALUE
);
723 *data
= alcMinorVersion
;
726 case ALC_EFX_MAJOR_VERSION
:
728 SetALCError(ALC_INVALID_VALUE
);
730 *data
= alcEFXMajorVersion
;
733 case ALC_EFX_MINOR_VERSION
:
735 SetALCError(ALC_INVALID_VALUE
);
737 *data
= alcEFXMinorVersion
;
740 case ALC_MAX_AUXILIARY_SENDS
:
742 SetALCError(ALC_INVALID_VALUE
);
747 case ALC_ATTRIBUTES_SIZE
:
749 SetALCError(ALC_INVALID_DEVICE
);
751 SetALCError(ALC_INVALID_VALUE
);
756 case ALC_ALL_ATTRIBUTES
:
758 SetALCError(ALC_INVALID_DEVICE
);
760 SetALCError(ALC_INVALID_VALUE
);
765 data
[i
++] = ALC_FREQUENCY
;
766 data
[i
++] = device
->Frequency
;
768 data
[i
++] = ALC_REFRESH
;
769 data
[i
++] = device
->Frequency
/ device
->UpdateFreq
;
771 data
[i
++] = ALC_SYNC
;
772 data
[i
++] = ALC_FALSE
;
774 SuspendContext(NULL
);
775 if(device
->Context
&& size
>= 12)
777 data
[i
++] = ALC_MONO_SOURCES
;
778 data
[i
++] = device
->Context
->lNumMonoSources
;
780 data
[i
++] = ALC_STEREO_SOURCES
;
781 data
[i
++] = device
->Context
->lNumStereoSources
;
783 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
784 data
[i
++] = MAX_SENDS
;
786 ProcessContext(NULL
);
794 SetALCError(ALC_INVALID_DEVICE
);
796 SetALCError(ALC_INVALID_VALUE
);
798 *data
= device
->Frequency
;
803 SetALCError(ALC_INVALID_DEVICE
);
805 SetALCError(ALC_INVALID_VALUE
);
807 *data
= device
->Frequency
/ device
->UpdateFreq
;
812 SetALCError(ALC_INVALID_DEVICE
);
814 SetALCError(ALC_INVALID_VALUE
);
819 case ALC_MONO_SOURCES
:
820 if(!device
|| !device
->Context
)
821 SetALCError(ALC_INVALID_DEVICE
);
823 SetALCError(ALC_INVALID_VALUE
);
825 *data
= device
->Context
->lNumMonoSources
;
828 case ALC_STEREO_SOURCES
:
829 if(!device
|| !device
->Context
)
830 SetALCError(ALC_INVALID_DEVICE
);
832 SetALCError(ALC_INVALID_VALUE
);
834 *data
= device
->Context
->lNumStereoSources
;
838 SetALCError(ALC_INVALID_ENUM
);
843 SetALCError(ALC_INVALID_VALUE
);
851 alcIsExtensionPresent
853 Determines if there is support for a particular extension
855 ALCAPI ALCboolean ALCAPIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
857 ALCboolean bResult
= ALC_FALSE
;
864 while(alcExtensions
[i
].extName
&&
865 strcasecmp(alcExtensions
[i
].extName
,extName
) != 0)
868 if (alcExtensions
[i
].extName
)
872 SetALCError(ALC_INVALID_VALUE
);
881 Retrieves the function address for a particular extension function
883 ALCAPI ALCvoid
* ALCAPIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
885 ALCvoid
*pFunction
= NULL
;
892 while(alcFunctions
[i
].funcName
&&
893 strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
895 pFunction
= alcFunctions
[i
].address
;
898 SetALCError(ALC_INVALID_VALUE
);
907 Get the value for a particular ALC Enumerated Value
909 ALCAPI ALCenum ALCAPIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
916 while ((enumeration
[i
].enumName
)&&(strcmp(enumeration
[i
].enumName
,enumName
)))
918 val
= enumeration
[i
].value
;
920 if(!enumeration
[i
].enumName
)
921 SetALCError(ALC_INVALID_VALUE
);
930 Create and attach a Context to a particular Device.
932 ALCAPI ALCcontext
* ALCAPIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
934 ALCcontext
*ALContext
= NULL
;
935 ALuint ulAttributeIndex
, ulRequestedStereoSources
;
937 if ((device
)&&(!device
->IsCaptureDevice
))
939 // Reset Context Last Error code
940 g_eLastContextError
= ALC_NO_ERROR
;
942 // Current implementation only allows one Context per Device
945 ALContext
= calloc(1, sizeof(ALCcontext
));
948 SetALCError(ALC_OUT_OF_MEMORY
);
952 ALContext
->Device
= device
;
953 InitContext(ALContext
);
955 device
->Context
= ALContext
;
957 SuspendContext(NULL
);
959 ALContext
->next
= g_pContextList
;
960 g_pContextList
= ALContext
;
963 ProcessContext(NULL
);
965 // Check for Voice Count attributes
968 ulAttributeIndex
= 0;
969 while ((ulAttributeIndex
< 10) && (attrList
[ulAttributeIndex
]))
971 if (attrList
[ulAttributeIndex
] == ALC_STEREO_SOURCES
)
973 ulRequestedStereoSources
= attrList
[ulAttributeIndex
+ 1];
975 if (ulRequestedStereoSources
> ALContext
->Device
->MaxNoOfSources
)
976 ulRequestedStereoSources
= ALContext
->Device
->MaxNoOfSources
;
978 ALContext
->lNumStereoSources
= ulRequestedStereoSources
;
979 ALContext
->lNumMonoSources
= ALContext
->Device
->MaxNoOfSources
- ALContext
->lNumStereoSources
;
983 ulAttributeIndex
+= 2;
989 SetALCError(ALC_INVALID_VALUE
);
994 SetALCError(ALC_INVALID_DEVICE
);
1005 ALCAPI ALCvoid ALCAPIENTRY
alcDestroyContext(ALCcontext
*context
)
1009 // Lock context list
1010 SuspendContext(NULL
);
1012 if (IsContext(context
))
1015 SuspendContext(context
);
1017 ReleaseALSources(context
);
1018 ReleaseALAuxiliaryEffectSlots(context
);
1020 context
->Device
->Context
= NULL
;
1022 list
= &g_pContextList
;
1023 while(*list
!= context
)
1024 list
= &(*list
)->next
;
1026 *list
= (*list
)->next
;
1030 ProcessContext(context
);
1032 ExitContext(context
);
1034 // Free memory (MUST do this after ProcessContext)
1035 memset(context
, 0, sizeof(ALCcontext
));
1039 SetALCError(ALC_INVALID_CONTEXT
);
1041 ProcessContext(NULL
);
1046 alcGetCurrentContext
1048 Returns the currently active Context
1050 ALCAPI ALCcontext
* ALCAPIENTRY
alcGetCurrentContext(ALCvoid
)
1052 ALCcontext
*pContext
= NULL
;
1054 SuspendContext(NULL
);
1056 pContext
= g_pContextList
;
1057 while ((pContext
) && (!pContext
->InUse
))
1058 pContext
= pContext
->next
;
1060 ProcessContext(NULL
);
1067 alcGetContextsDevice
1069 Returns the Device that a particular Context is attached to
1071 ALCAPI ALCdevice
* ALCAPIENTRY
alcGetContextsDevice(ALCcontext
*pContext
)
1073 ALCdevice
*pDevice
= NULL
;
1075 SuspendContext(NULL
);
1076 if (IsContext(pContext
))
1077 pDevice
= pContext
->Device
;
1079 SetALCError(ALC_INVALID_CONTEXT
);
1080 ProcessContext(NULL
);
1087 alcMakeContextCurrent
1089 Makes the given Context the active Context
1091 ALCAPI ALCboolean ALCAPIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
1093 ALCcontext
*ALContext
;
1094 ALboolean bReturn
= AL_TRUE
;
1096 SuspendContext(NULL
);
1098 // context must be a valid Context or NULL
1099 if ((IsContext(context
)) || (context
== NULL
))
1101 if ((ALContext
=alcGetCurrentContext()))
1103 SuspendContext(ALContext
);
1104 ALContext
->InUse
=AL_FALSE
;
1105 ProcessContext(ALContext
);
1108 if ((ALContext
=context
) && (ALContext
->Device
))
1110 SuspendContext(ALContext
);
1111 ALContext
->InUse
=AL_TRUE
;
1112 ProcessContext(ALContext
);
1117 SetALCError(ALC_INVALID_CONTEXT
);
1121 ProcessContext(NULL
);
1130 Open the Device specified.
1132 ALCAPI ALCdevice
* ALCAPIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
1134 ALboolean bDeviceFound
= AL_FALSE
;
1140 if(deviceName
&& !deviceName
[0])
1143 device
= malloc(sizeof(ALCdevice
));
1148 //Initialise device structure
1149 memset(device
, 0, sizeof(ALCdevice
));
1152 device
->InUse
= AL_TRUE
;
1153 device
->IsCaptureDevice
= AL_FALSE
;
1156 device
->Frequency
= GetConfigValueInt(NULL
, "frequency", SWMIXER_OUTPUT_RATE
);
1157 if((ALint
)device
->Frequency
<= 0)
1158 device
->Frequency
= SWMIXER_OUTPUT_RATE
;
1160 fmt
= GetConfigValue(NULL
, "format", "AL_FORMAT_STEREO16");
1162 device
->Format
= alGetEnumValue(fmt
);
1164 device
->Channels
= aluChannelsFromFormat(device
->Format
);
1165 if(!device
->Channels
)
1167 device
->Format
= AL_FORMAT_STEREO16
;
1168 device
->Channels
= 2;
1169 device
->FrameSize
= 4;
1172 device
->FrameSize
= aluBytesFromFormat(device
->Format
) *
1175 device
->UpdateFreq
= GetConfigValueInt(NULL
, "refresh", 0);
1176 if((ALint
)device
->UpdateFreq
<= 0)
1177 device
->UpdateFreq
= 8192 * device
->Frequency
/ 22050;
1179 // Find a playback device to open
1180 for(i
= 0;BackendList
[i
].Init
;i
++)
1182 device
->Funcs
= &BackendList
[i
].Funcs
;
1183 if(ALCdevice_OpenPlayback(device
, deviceName
))
1185 SuspendContext(NULL
);
1186 device
->next
= g_pDeviceList
;
1187 g_pDeviceList
= device
;
1189 ProcessContext(NULL
);
1191 bDeviceFound
= AL_TRUE
;
1198 // No suitable output device found
1211 Close the specified Device
1213 ALCAPI ALCboolean ALCAPIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
1215 ALCboolean bReturn
= ALC_FALSE
;
1218 if ((pDevice
)&&(!pDevice
->IsCaptureDevice
))
1220 SuspendContext(NULL
);
1222 list
= &g_pDeviceList
;
1223 while(*list
!= pDevice
)
1224 list
= &(*list
)->next
;
1226 *list
= (*list
)->next
;
1229 ProcessContext(NULL
);
1231 if(pDevice
->Context
)
1232 alcDestroyContext(pDevice
->Context
);
1233 ALCdevice_ClosePlayback(pDevice
);
1235 //Release device structure
1236 memset(pDevice
, 0, sizeof(ALCdevice
));
1242 SetALCError(ALC_INVALID_DEVICE
);
1248 ALCvoid
ReleaseALC(ALCvoid
)
1253 if(g_ulContextCount
> 0)
1254 AL_PRINT("exit() %u device(s) and %u context(s) NOT deleted\n", g_ulDeviceCount
, g_ulContextCount
);
1257 while(g_pDeviceList
)
1259 Dev
= g_pDeviceList
;
1260 g_pDeviceList
= g_pDeviceList
->next
;
1261 if(Dev
->IsCaptureDevice
)
1262 alcCaptureCloseDevice(Dev
);
1264 alcCloseDevice(Dev
);
1268 ///////////////////////////////////////////////////////