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
36 #include "alExtension.h"
37 #include "alAuxEffectSlot.h"
40 ///////////////////////////////////////////////////////
45 ///////////////////////////////////////////////////////
48 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
51 void (*Init
)(BackendFuncs
*);
55 { "alsa", alc_alsa_init
, EmptyFuncs
},
58 { "oss", alc_oss_init
, EmptyFuncs
},
61 { "dsound", alcDSoundInit
, EmptyFuncs
},
64 { "winmm", alcWinMMInit
, EmptyFuncs
},
67 { "wave", alc_wave_init
, EmptyFuncs
},
69 { NULL
, NULL
, EmptyFuncs
}
73 ///////////////////////////////////////////////////////
75 #define ALC_EFX_MAJOR_VERSION 0x20001
76 #define ALC_EFX_MINOR_VERSION 0x20002
77 #define ALC_MAX_AUXILIARY_SENDS 0x20003
79 ///////////////////////////////////////////////////////
80 // STRING and EXTENSIONS
82 typedef struct ALCfunction_struct
88 static ALCfunction alcFunctions
[] = {
89 { "alcCreateContext", (ALvoid
*) alcCreateContext
},
90 { "alcMakeContextCurrent", (ALvoid
*) alcMakeContextCurrent
},
91 { "alcProcessContext", (ALvoid
*) alcProcessContext
},
92 { "alcSuspendContext", (ALvoid
*) alcSuspendContext
},
93 { "alcDestroyContext", (ALvoid
*) alcDestroyContext
},
94 { "alcGetCurrentContext", (ALvoid
*) alcGetCurrentContext
},
95 { "alcGetContextsDevice", (ALvoid
*) alcGetContextsDevice
},
96 { "alcOpenDevice", (ALvoid
*) alcOpenDevice
},
97 { "alcCloseDevice", (ALvoid
*) alcCloseDevice
},
98 { "alcGetError", (ALvoid
*) alcGetError
},
99 { "alcIsExtensionPresent", (ALvoid
*) alcIsExtensionPresent
},
100 { "alcGetProcAddress", (ALvoid
*) alcGetProcAddress
},
101 { "alcGetEnumValue", (ALvoid
*) alcGetEnumValue
},
102 { "alcGetString", (ALvoid
*) alcGetString
},
103 { "alcGetIntegerv", (ALvoid
*) alcGetIntegerv
},
104 { "alcCaptureOpenDevice", (ALvoid
*) alcCaptureOpenDevice
},
105 { "alcCaptureCloseDevice", (ALvoid
*) alcCaptureCloseDevice
},
106 { "alcCaptureStart", (ALvoid
*) alcCaptureStart
},
107 { "alcCaptureStop", (ALvoid
*) alcCaptureStop
},
108 { "alcCaptureSamples", (ALvoid
*) alcCaptureSamples
},
109 { NULL
, (ALvoid
*) NULL
}
112 static ALenums enumeration
[]={
114 { (ALchar
*)"ALC_INVALID", ALC_INVALID
},
115 { (ALchar
*)"ALC_FALSE", ALC_FALSE
},
116 { (ALchar
*)"ALC_TRUE", ALC_TRUE
},
119 { (ALchar
*)"ALC_MAJOR_VERSION", ALC_MAJOR_VERSION
},
120 { (ALchar
*)"ALC_MINOR_VERSION", ALC_MINOR_VERSION
},
121 { (ALchar
*)"ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE
},
122 { (ALchar
*)"ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES
},
123 { (ALchar
*)"ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER
},
124 { (ALchar
*)"ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER
},
125 { (ALchar
*)"ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER
},
126 { (ALchar
*)"ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER
},
127 { (ALchar
*)"ALC_EXTENSIONS", ALC_EXTENSIONS
},
128 { (ALchar
*)"ALC_FREQUENCY", ALC_FREQUENCY
},
129 { (ALchar
*)"ALC_REFRESH", ALC_REFRESH
},
130 { (ALchar
*)"ALC_SYNC", ALC_SYNC
},
131 { (ALchar
*)"ALC_MONO_SOURCES", ALC_MONO_SOURCES
},
132 { (ALchar
*)"ALC_STEREO_SOURCES", ALC_STEREO_SOURCES
},
133 { (ALchar
*)"ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER
},
134 { (ALchar
*)"ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
},
135 { (ALchar
*)"ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES
},
138 { (ALchar
*)"ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION
},
139 { (ALchar
*)"ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION
},
140 { (ALchar
*)"ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS
},
143 { (ALchar
*)"ALC_NO_ERROR", ALC_NO_ERROR
},
144 { (ALchar
*)"ALC_INVALID_DEVICE", ALC_INVALID_DEVICE
},
145 { (ALchar
*)"ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT
},
146 { (ALchar
*)"ALC_INVALID_ENUM", ALC_INVALID_ENUM
},
147 { (ALchar
*)"ALC_INVALID_VALUE", ALC_INVALID_VALUE
},
148 { (ALchar
*)"ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY
},
149 { (ALchar
*)NULL
, (ALenum
)0 }
152 static const ALCchar alcNoError
[] = "No Error";
153 static const ALCchar alcErrInvalidDevice
[] = "Invalid Device";
154 static const ALCchar alcErrInvalidContext
[] = "Invalid Context";
155 static const ALCchar alcErrInvalidEnum
[] = "Invalid Enum";
156 static const ALCchar alcErrInvalidValue
[] = "Invalid Value";
157 static const ALCchar alcErrOutOfMemory
[] = "Out of Memory";
160 static ALCchar alcDeviceList
[2048];
161 static ALCchar alcAllDeviceList
[2048];
162 static ALCchar alcCaptureDeviceList
[2048];
163 // Default is always the first in the list
164 static ALCchar
*alcDefaultDeviceSpecifier
= alcDeviceList
;
165 static ALCchar
*alcDefaultAllDeviceSpecifier
= alcAllDeviceList
;
166 static ALCchar
*alcCaptureDefaultDeviceSpecifier
= alcCaptureDeviceList
;
169 static ALCchar alcExtensionList
[] = "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_EFX";
170 static ALCint alcMajorVersion
= 1;
171 static ALCint alcMinorVersion
= 1;
173 static ALCint alcEFXMajorVersion
= 1;
174 static ALCint alcEFXMinorVersion
= 0;
176 ///////////////////////////////////////////////////////
179 ///////////////////////////////////////////////////////
182 static ALCdevice
*g_pDeviceList
= NULL
;
183 static ALCuint g_ulDeviceCount
= 0;
186 static ALCcontext
*g_pContextList
= NULL
;
187 static ALCuint g_ulContextCount
= 0;
190 static ALCenum g_eLastContextError
= ALC_NO_ERROR
;
192 ///////////////////////////////////////////////////////
195 ///////////////////////////////////////////////////////
196 // ALC Related helper functions
198 static void InitAL(void)
206 InitializeCriticalSection(&_alMutex
);
210 devs
= GetConfigValue(NULL
, "drivers", "");
215 const char *next
= devs
;
221 next
= strchr(devs
, ',');
223 if(!devs
[0] || devs
[0] == ',')
226 len
= (next
? ((size_t)(next
-devs
)) : strlen(devs
));
227 for(n
= i
;BackendList
[n
].Init
;n
++)
229 if(len
== strlen(BackendList
[n
].name
) &&
230 strncmp(BackendList
[n
].name
, devs
, len
) == 0)
232 const char *name
= BackendList
[i
].name
;
233 void (*Init
)(BackendFuncs
*) = BackendList
[i
].Init
;
235 BackendList
[i
].name
= BackendList
[n
].name
;
236 BackendList
[i
].Init
= BackendList
[n
].Init
;
238 BackendList
[n
].name
= name
;
239 BackendList
[n
].Init
= Init
;
246 BackendList
[i
].name
= NULL
;
247 BackendList
[i
].Init
= NULL
;
250 for(i
= 0;BackendList
[i
].Init
;i
++)
251 BackendList
[i
].Init(&BackendList
[i
].Funcs
);
256 ALCchar
*AppendDeviceList(char *name
)
259 ALCchar
*ret
= alcDeviceList
+pos
;
260 pos
+= snprintf(alcDeviceList
+pos
, sizeof(alcDeviceList
)-pos
, "%s", name
) + 1;
264 ALCchar
*AppendAllDeviceList(char *name
)
267 ALCchar
*ret
= alcAllDeviceList
+pos
;
268 pos
+= snprintf(alcAllDeviceList
+pos
, sizeof(alcAllDeviceList
)-pos
, "%s", name
) + 1;
272 ALCchar
*AppendCaptureDeviceList(char *name
)
275 ALCchar
*ret
= alcCaptureDeviceList
+pos
;
276 pos
+= snprintf(alcCaptureDeviceList
+pos
, sizeof(alcCaptureDeviceList
)-pos
, "%s", name
) + 1;
283 Check pContext is a valid Context pointer
285 static ALCboolean
IsContext(ALCcontext
*pContext
)
287 ALCcontext
*pTempContext
;
289 pTempContext
= g_pContextList
;
290 while (pTempContext
&& pTempContext
!= pContext
)
291 pTempContext
= pTempContext
->next
;
293 return (pTempContext
? ALC_TRUE
: ALC_FALSE
);
300 Store latest ALC Error
302 ALCvoid
SetALCError(ALenum errorCode
)
304 g_eLastContextError
= errorCode
;
313 ALCvoid
SuspendContext(ALCcontext
*pContext
)
316 EnterCriticalSection(&_alMutex
);
325 ALCvoid
ProcessContext(ALCcontext
*pContext
)
328 LeaveCriticalSection(&_alMutex
);
335 Initialize Context variables
337 static ALvoid
InitContext(ALCcontext
*pContext
)
341 //Initialise listener
342 pContext
->Listener
.Gain
= 1.0f
;
343 pContext
->Listener
.MetersPerUnit
= 1.0f
;
344 pContext
->Listener
.Position
[0] = 0.0f
;
345 pContext
->Listener
.Position
[1] = 0.0f
;
346 pContext
->Listener
.Position
[2] = 0.0f
;
347 pContext
->Listener
.Velocity
[0] = 0.0f
;
348 pContext
->Listener
.Velocity
[1] = 0.0f
;
349 pContext
->Listener
.Velocity
[2] = 0.0f
;
350 pContext
->Listener
.Forward
[0] = 0.0f
;
351 pContext
->Listener
.Forward
[1] = 0.0f
;
352 pContext
->Listener
.Forward
[2] = -1.0f
;
353 pContext
->Listener
.Up
[0] = 0.0f
;
354 pContext
->Listener
.Up
[1] = 1.0f
;
355 pContext
->Listener
.Up
[2] = 0.0f
;
358 pContext
->LastError
= AL_NO_ERROR
;
359 pContext
->InUse
= AL_FALSE
;
362 pContext
->Frequency
= pContext
->Device
->Frequency
;
365 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
366 pContext
->DopplerFactor
= 1.0f
;
367 pContext
->DopplerVelocity
= 1.0f
;
368 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
370 pContext
->lNumStereoSources
= 1;
371 pContext
->lNumMonoSources
= pContext
->Device
->MaxNoOfSources
- pContext
->lNumStereoSources
;
373 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");
375 level
= GetConfigValueInt(NULL
, "cf_level", 0);
376 if(level
> 0 && level
<= 6)
378 pContext
->bs2b
= calloc(1, sizeof(*pContext
->bs2b
));
379 bs2b_set_srate(pContext
->bs2b
, pContext
->Frequency
);
380 bs2b_set_level(pContext
->bs2b
, level
);
388 Clean up Context, destroy any remaining Sources
390 static ALCvoid
ExitContext(ALCcontext
*pContext
)
393 pContext
->LastError
= AL_NO_ERROR
;
394 pContext
->InUse
= AL_FALSE
;
396 free(pContext
->bs2b
);
397 pContext
->bs2b
= NULL
;
400 ///////////////////////////////////////////////////////
403 ///////////////////////////////////////////////////////
404 // ALC Functions calls
407 // This should probably move to another c file but for now ...
408 ALCAPI ALCdevice
* ALCAPIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
410 ALCboolean DeviceFound
= ALC_FALSE
;
411 ALCdevice
*pDevice
= NULL
;
416 if(deviceName
&& !deviceName
[0])
419 pDevice
= malloc(sizeof(ALCdevice
));
424 //Initialise device structure
425 memset(pDevice
, 0, sizeof(ALCdevice
));
428 pDevice
->InUse
= AL_TRUE
;
429 pDevice
->IsCaptureDevice
= AL_TRUE
;
431 pDevice
->Frequency
= frequency
;
432 pDevice
->Format
= format
;
433 pDevice
->Channels
= aluChannelsFromFormat(format
);
434 pDevice
->FrameSize
= aluBytesFromFormat(format
) *
437 for(i
= 0;BackendList
[i
].Init
;i
++)
439 pDevice
->Funcs
= &BackendList
[i
].Funcs
;
440 if(ALCdevice_OpenCapture(pDevice
, deviceName
, frequency
, format
, SampleSize
))
442 SuspendContext(NULL
);
443 pDevice
->next
= g_pDeviceList
;
444 g_pDeviceList
= pDevice
;
446 ProcessContext(NULL
);
448 DeviceFound
= ALC_TRUE
;
454 SetALCError(ALC_INVALID_VALUE
);
463 SetALCError(ALC_OUT_OF_MEMORY
);
468 ALCAPI ALCboolean ALCAPIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
470 ALCboolean bReturn
= ALC_FALSE
;
473 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
475 SuspendContext(NULL
);
477 list
= &g_pDeviceList
;
478 while(*list
!= pDevice
)
479 list
= &(*list
)->next
;
481 *list
= (*list
)->next
;
484 ProcessContext(NULL
);
486 ALCdevice_CloseCapture(pDevice
);
492 SetALCError(ALC_INVALID_DEVICE
);
497 ALCAPI
void ALCAPIENTRY
alcCaptureStart(ALCdevice
*pDevice
)
499 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
500 ALCdevice_StartCapture(pDevice
);
502 SetALCError(ALC_INVALID_DEVICE
);
505 ALCAPI
void ALCAPIENTRY
alcCaptureStop(ALCdevice
*pDevice
)
507 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
508 ALCdevice_StopCapture(pDevice
);
510 SetALCError(ALC_INVALID_DEVICE
);
513 ALCAPI
void ALCAPIENTRY
alcCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCsizei lSamples
)
515 if ((pDevice
) && (pDevice
->IsCaptureDevice
))
516 ALCdevice_CaptureSamples(pDevice
, pBuffer
, lSamples
);
518 SetALCError(ALC_INVALID_DEVICE
);
524 Return last ALC generated error code
526 ALCAPI ALCenum ALCAPIENTRY
alcGetError(ALCdevice
*device
)
532 errorCode
= g_eLastContextError
;
533 g_eLastContextError
= ALC_NO_ERROR
;
543 ALCAPI ALCvoid ALCAPIENTRY
alcSuspendContext(ALCcontext
*pContext
)
545 // Not a lot happens here !
555 ALCAPI ALCvoid ALCAPIENTRY
alcProcessContext(ALCcontext
*pContext
)
557 // Not a lot happens here !
565 Returns information about the Device, and error strings
567 ALCAPI
const ALCchar
* ALCAPIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
569 const ALCchar
*value
= NULL
;
579 case ALC_INVALID_ENUM
:
580 value
= alcErrInvalidEnum
;
583 case ALC_INVALID_VALUE
:
584 value
= alcErrInvalidValue
;
587 case ALC_INVALID_DEVICE
:
588 value
= alcErrInvalidDevice
;
591 case ALC_INVALID_CONTEXT
:
592 value
= alcErrInvalidContext
;
595 case ALC_OUT_OF_MEMORY
:
596 value
= alcErrOutOfMemory
;
599 case ALC_DEFAULT_DEVICE_SPECIFIER
:
600 value
= alcDefaultDeviceSpecifier
;
603 case ALC_DEVICE_SPECIFIER
:
605 value
= pDevice
->szDeviceName
;
607 value
= alcDeviceList
;
610 case ALC_ALL_DEVICES_SPECIFIER
:
611 value
= alcAllDeviceList
;
614 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
615 value
= alcDefaultAllDeviceSpecifier
;
618 case ALC_CAPTURE_DEVICE_SPECIFIER
:
620 value
= pDevice
->szDeviceName
;
622 value
= alcCaptureDeviceList
;
625 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
626 value
= alcCaptureDefaultDeviceSpecifier
;
630 value
= alcExtensionList
;
634 SetALCError(ALC_INVALID_ENUM
);
645 Returns information about the Device and the version of Open AL
647 ALCAPI ALCvoid ALCAPIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
651 if ((device
)&&(device
->IsCaptureDevice
))
653 SuspendContext(NULL
);
658 case ALC_CAPTURE_SAMPLES
:
659 if ((size
) && (data
))
660 *data
= ALCdevice_AvailableSamples(device
);
662 SetALCError(ALC_INVALID_VALUE
);
666 SetALCError(ALC_INVALID_ENUM
);
670 ProcessContext(NULL
);
679 case ALC_MAJOR_VERSION
:
681 SetALCError(ALC_INVALID_VALUE
);
683 *data
= alcMajorVersion
;
686 case ALC_MINOR_VERSION
:
688 SetALCError(ALC_INVALID_VALUE
);
690 *data
= alcMinorVersion
;
693 case ALC_EFX_MAJOR_VERSION
:
695 SetALCError(ALC_INVALID_VALUE
);
697 *data
= alcEFXMajorVersion
;
700 case ALC_EFX_MINOR_VERSION
:
702 SetALCError(ALC_INVALID_VALUE
);
704 *data
= alcEFXMinorVersion
;
707 case ALC_MAX_AUXILIARY_SENDS
:
709 SetALCError(ALC_INVALID_VALUE
);
714 case ALC_ATTRIBUTES_SIZE
:
716 SetALCError(ALC_INVALID_DEVICE
);
718 SetALCError(ALC_INVALID_VALUE
);
723 case ALC_ALL_ATTRIBUTES
:
725 SetALCError(ALC_INVALID_DEVICE
);
727 SetALCError(ALC_INVALID_VALUE
);
732 data
[i
++] = ALC_FREQUENCY
;
733 data
[i
++] = device
->Frequency
;
735 data
[i
++] = ALC_REFRESH
;
736 data
[i
++] = device
->Frequency
/ device
->UpdateFreq
;
738 data
[i
++] = ALC_SYNC
;
739 data
[i
++] = ALC_FALSE
;
741 SuspendContext(NULL
);
742 if(device
->Context
&& size
>= 12)
744 data
[i
++] = ALC_MONO_SOURCES
;
745 data
[i
++] = device
->Context
->lNumMonoSources
;
747 data
[i
++] = ALC_STEREO_SOURCES
;
748 data
[i
++] = device
->Context
->lNumStereoSources
;
750 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
751 data
[i
++] = MAX_SENDS
;
753 ProcessContext(NULL
);
761 SetALCError(ALC_INVALID_DEVICE
);
763 SetALCError(ALC_INVALID_VALUE
);
765 *data
= device
->Frequency
;
770 SetALCError(ALC_INVALID_DEVICE
);
772 SetALCError(ALC_INVALID_VALUE
);
774 *data
= device
->Frequency
/ device
->UpdateFreq
;
779 SetALCError(ALC_INVALID_DEVICE
);
781 SetALCError(ALC_INVALID_VALUE
);
786 case ALC_MONO_SOURCES
:
787 if(!device
|| !device
->Context
)
788 SetALCError(ALC_INVALID_DEVICE
);
790 SetALCError(ALC_INVALID_VALUE
);
792 *data
= device
->Context
->lNumMonoSources
;
795 case ALC_STEREO_SOURCES
:
796 if(!device
|| !device
->Context
)
797 SetALCError(ALC_INVALID_DEVICE
);
799 SetALCError(ALC_INVALID_VALUE
);
801 *data
= device
->Context
->lNumStereoSources
;
805 SetALCError(ALC_INVALID_ENUM
);
810 SetALCError(ALC_INVALID_VALUE
);
818 alcIsExtensionPresent
820 Determines if there is support for a particular extension
822 ALCAPI ALCboolean ALCAPIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
824 ALCboolean bResult
= ALC_FALSE
;
833 len
= strlen(extName
);
834 ptr
= alcExtensionList
;
837 if(strncasecmp(ptr
, extName
, len
) == 0 &&
838 (ptr
[len
] == '\0' || isspace(ptr
[len
])))
843 if((ptr
=strchr(ptr
, ' ')) != NULL
)
847 } while(isspace(*ptr
));
852 SetALCError(ALC_INVALID_VALUE
);
861 Retrieves the function address for a particular extension function
863 ALCAPI ALCvoid
* ALCAPIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
865 ALCvoid
*pFunction
= NULL
;
872 while(alcFunctions
[i
].funcName
&&
873 strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
875 pFunction
= alcFunctions
[i
].address
;
878 SetALCError(ALC_INVALID_VALUE
);
887 Get the value for a particular ALC Enumerated Value
889 ALCAPI ALCenum ALCAPIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
896 while ((enumeration
[i
].enumName
)&&(strcmp(enumeration
[i
].enumName
,enumName
)))
898 val
= enumeration
[i
].value
;
900 if(!enumeration
[i
].enumName
)
901 SetALCError(ALC_INVALID_VALUE
);
910 Create and attach a Context to a particular Device.
912 ALCAPI ALCcontext
* ALCAPIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
914 ALCcontext
*ALContext
= NULL
;
915 ALuint ulAttributeIndex
, ulRequestedStereoSources
;
917 if ((device
)&&(!device
->IsCaptureDevice
))
919 // Reset Context Last Error code
920 g_eLastContextError
= ALC_NO_ERROR
;
922 // Current implementation only allows one Context per Device
925 ALContext
= calloc(1, sizeof(ALCcontext
));
928 SetALCError(ALC_OUT_OF_MEMORY
);
932 ALContext
->Device
= device
;
933 InitContext(ALContext
);
935 device
->Context
= ALContext
;
937 SuspendContext(NULL
);
939 ALContext
->next
= g_pContextList
;
940 g_pContextList
= ALContext
;
943 ProcessContext(NULL
);
945 // Check for Voice Count attributes
948 ulAttributeIndex
= 0;
949 while ((ulAttributeIndex
< 10) && (attrList
[ulAttributeIndex
]))
951 if (attrList
[ulAttributeIndex
] == ALC_STEREO_SOURCES
)
953 ulRequestedStereoSources
= attrList
[ulAttributeIndex
+ 1];
955 if (ulRequestedStereoSources
> ALContext
->Device
->MaxNoOfSources
)
956 ulRequestedStereoSources
= ALContext
->Device
->MaxNoOfSources
;
958 ALContext
->lNumStereoSources
= ulRequestedStereoSources
;
959 ALContext
->lNumMonoSources
= ALContext
->Device
->MaxNoOfSources
- ALContext
->lNumStereoSources
;
963 ulAttributeIndex
+= 2;
969 SetALCError(ALC_INVALID_VALUE
);
974 SetALCError(ALC_INVALID_DEVICE
);
985 ALCAPI ALCvoid ALCAPIENTRY
alcDestroyContext(ALCcontext
*context
)
990 SuspendContext(NULL
);
992 if (IsContext(context
))
995 SuspendContext(context
);
997 ReleaseALSources(context
);
998 ReleaseALAuxiliaryEffectSlots(context
);
1000 context
->Device
->Context
= NULL
;
1002 list
= &g_pContextList
;
1003 while(*list
!= context
)
1004 list
= &(*list
)->next
;
1006 *list
= (*list
)->next
;
1010 ProcessContext(context
);
1012 ExitContext(context
);
1014 // Free memory (MUST do this after ProcessContext)
1015 memset(context
, 0, sizeof(ALCcontext
));
1019 SetALCError(ALC_INVALID_CONTEXT
);
1021 ProcessContext(NULL
);
1026 alcGetCurrentContext
1028 Returns the currently active Context
1030 ALCAPI ALCcontext
* ALCAPIENTRY
alcGetCurrentContext(ALCvoid
)
1032 ALCcontext
*pContext
= NULL
;
1034 SuspendContext(NULL
);
1036 pContext
= g_pContextList
;
1037 while ((pContext
) && (!pContext
->InUse
))
1038 pContext
= pContext
->next
;
1040 ProcessContext(NULL
);
1047 alcGetContextsDevice
1049 Returns the Device that a particular Context is attached to
1051 ALCAPI ALCdevice
* ALCAPIENTRY
alcGetContextsDevice(ALCcontext
*pContext
)
1053 ALCdevice
*pDevice
= NULL
;
1055 SuspendContext(NULL
);
1056 if (IsContext(pContext
))
1057 pDevice
= pContext
->Device
;
1059 SetALCError(ALC_INVALID_CONTEXT
);
1060 ProcessContext(NULL
);
1067 alcMakeContextCurrent
1069 Makes the given Context the active Context
1071 ALCAPI ALCboolean ALCAPIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
1073 ALCcontext
*ALContext
;
1074 ALboolean bReturn
= AL_TRUE
;
1076 SuspendContext(NULL
);
1078 // context must be a valid Context or NULL
1079 if ((IsContext(context
)) || (context
== NULL
))
1081 if ((ALContext
=alcGetCurrentContext()))
1083 SuspendContext(ALContext
);
1084 ALContext
->InUse
=AL_FALSE
;
1085 ProcessContext(ALContext
);
1088 if ((ALContext
=context
) && (ALContext
->Device
))
1090 SuspendContext(ALContext
);
1091 ALContext
->InUse
=AL_TRUE
;
1092 ProcessContext(ALContext
);
1097 SetALCError(ALC_INVALID_CONTEXT
);
1101 ProcessContext(NULL
);
1110 Open the Device specified.
1112 ALCAPI ALCdevice
* ALCAPIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
1114 ALboolean bDeviceFound
= AL_FALSE
;
1120 if(deviceName
&& !deviceName
[0])
1123 device
= malloc(sizeof(ALCdevice
));
1128 //Initialise device structure
1129 memset(device
, 0, sizeof(ALCdevice
));
1132 device
->InUse
= AL_TRUE
;
1133 device
->IsCaptureDevice
= AL_FALSE
;
1136 device
->Frequency
= GetConfigValueInt(NULL
, "frequency", SWMIXER_OUTPUT_RATE
);
1137 if((ALint
)device
->Frequency
<= 0)
1138 device
->Frequency
= SWMIXER_OUTPUT_RATE
;
1140 fmt
= GetConfigValue(NULL
, "format", "AL_FORMAT_STEREO16");
1142 device
->Format
= alGetEnumValue(fmt
);
1144 device
->Channels
= aluChannelsFromFormat(device
->Format
);
1145 if(!device
->Channels
)
1147 device
->Format
= AL_FORMAT_STEREO16
;
1148 device
->Channels
= 2;
1149 device
->FrameSize
= 4;
1152 device
->FrameSize
= aluBytesFromFormat(device
->Format
) *
1155 device
->UpdateFreq
= GetConfigValueInt(NULL
, "refresh", 8192);
1156 if((ALint
)device
->UpdateFreq
<= 0)
1157 device
->UpdateFreq
= 8192;
1159 device
->MaxNoOfSources
= GetConfigValueInt(NULL
, "sources", 256);
1160 if((ALint
)device
->MaxNoOfSources
<= 0)
1161 device
->MaxNoOfSources
= 256;
1163 // Find a playback device to open
1164 for(i
= 0;BackendList
[i
].Init
;i
++)
1166 device
->Funcs
= &BackendList
[i
].Funcs
;
1167 if(ALCdevice_OpenPlayback(device
, deviceName
))
1169 SuspendContext(NULL
);
1170 device
->next
= g_pDeviceList
;
1171 g_pDeviceList
= device
;
1173 ProcessContext(NULL
);
1175 bDeviceFound
= AL_TRUE
;
1182 // No suitable output device found
1195 Close the specified Device
1197 ALCAPI ALCboolean ALCAPIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
1199 ALCboolean bReturn
= ALC_FALSE
;
1202 if ((pDevice
)&&(!pDevice
->IsCaptureDevice
))
1204 SuspendContext(NULL
);
1206 list
= &g_pDeviceList
;
1207 while(*list
!= pDevice
)
1208 list
= &(*list
)->next
;
1210 *list
= (*list
)->next
;
1213 ProcessContext(NULL
);
1215 if(pDevice
->Context
)
1216 alcDestroyContext(pDevice
->Context
);
1217 ALCdevice_ClosePlayback(pDevice
);
1219 //Release device structure
1220 memset(pDevice
, 0, sizeof(ALCdevice
));
1226 SetALCError(ALC_INVALID_DEVICE
);
1232 ALCvoid
ReleaseALC(ALCvoid
)
1237 if(g_ulContextCount
> 0)
1238 AL_PRINT("exit() %u device(s) and %u context(s) NOT deleted\n", g_ulDeviceCount
, g_ulContextCount
);
1241 while(g_pDeviceList
)
1243 Dev
= g_pDeviceList
;
1244 g_pDeviceList
= g_pDeviceList
->next
;
1245 if(Dev
->IsCaptureDevice
)
1246 alcCaptureCloseDevice(Dev
);
1248 alcCloseDevice(Dev
);
1252 ///////////////////////////////////////////////////////