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
33 ///////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////
41 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
44 void (*Init
)(BackendFuncs
*);
48 { "alsa", alc_alsa_init
, EmptyFuncs
},
51 { "oss", alc_oss_init
, EmptyFuncs
},
54 { "dsound", alcDSoundInit
, EmptyFuncs
},
57 { "winmm", alcWinMMInit
, EmptyFuncs
},
60 { NULL
, NULL
, EmptyFuncs
}
64 ///////////////////////////////////////////////////////
66 #define ALC_EFX_MAJOR_VERSION 0x20001
67 #define ALC_EFX_MINOR_VERSION 0x20002
68 #define ALC_MAX_AUXILIARY_SENDS 0x20003
70 ///////////////////////////////////////////////////////
71 // STRING and EXTENSIONS
73 typedef struct ALCextension_struct
79 typedef struct ALCfunction_struct
85 static ALCextension alcExtensions
[] = {
86 { "ALC_ENUMERATE_ALL_EXT", (ALvoid
*) NULL
},
87 { "ALC_ENUMERATION_EXT", (ALvoid
*) NULL
},
88 { "ALC_EXT_CAPTURE", (ALvoid
*) NULL
},
89 { NULL
, (ALvoid
*) NULL
}
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";
174 static ALCint alcMajorVersion
= 1;
175 static ALCint alcMinorVersion
= 1;
177 static ALCint alcEFXMajorVersion
= 1;
178 static ALCint alcEFXMinorVersion
= 0;
180 ///////////////////////////////////////////////////////
183 ///////////////////////////////////////////////////////
187 static ALCcontext
*g_pContextList
= NULL
;
188 static ALCuint g_ulContextCount
= 0;
191 static ALCenum g_eLastContextError
= ALC_NO_ERROR
;
193 ///////////////////////////////////////////////////////
196 ///////////////////////////////////////////////////////
197 // ALC Related helper functions
199 static void InitAL(void)
207 InitializeCriticalSection(&g_mutex
);
211 devs
= GetConfigValue(NULL
, "drivers", "");
216 const char *next
= devs
;
222 next
= strchr(devs
, ',');
224 if(!devs
[0] || devs
[0] == ',')
227 len
= (next
? ((size_t)(next
-devs
)) : strlen(devs
));
228 for(n
= i
;BackendList
[n
].Init
;n
++)
230 if(len
== strlen(BackendList
[n
].name
) &&
231 strncmp(BackendList
[n
].name
, devs
, len
) == 0)
233 const char *name
= BackendList
[i
].name
;
234 void (*Init
)(BackendFuncs
*) = BackendList
[i
].Init
;
236 BackendList
[i
].name
= BackendList
[n
].name
;
237 BackendList
[i
].Init
= BackendList
[n
].Init
;
239 BackendList
[n
].name
= name
;
240 BackendList
[n
].Init
= Init
;
247 BackendList
[i
].name
= NULL
;
248 BackendList
[i
].Init
= NULL
;
251 for(i
= 0;BackendList
[i
].Init
;i
++)
252 BackendList
[i
].Init(&BackendList
[i
].Funcs
);
257 ALCchar
*AppendDeviceList(char *name
)
260 ALCchar
*ret
= alcDeviceList
+pos
;
261 pos
+= snprintf(alcDeviceList
+pos
, sizeof(alcDeviceList
)-pos
, "%s", name
) + 1;
265 ALCchar
*AppendAllDeviceList(char *name
)
268 ALCchar
*ret
= alcAllDeviceList
+pos
;
269 pos
+= snprintf(alcAllDeviceList
+pos
, sizeof(alcAllDeviceList
)-pos
, "%s", name
) + 1;
273 ALCchar
*AppendCaptureDeviceList(char *name
)
276 ALCchar
*ret
= alcCaptureDeviceList
+pos
;
277 pos
+= snprintf(alcCaptureDeviceList
+pos
, sizeof(alcCaptureDeviceList
)-pos
, "%s", name
) + 1;
284 Check pContext is a valid Context pointer
286 static ALCboolean
IsContext(ALCcontext
*pContext
)
288 ALCcontext
*pTempContext
;
290 pTempContext
= g_pContextList
;
291 while (pTempContext
&& pTempContext
!= pContext
)
292 pTempContext
= pTempContext
->next
;
294 return (pTempContext
? ALC_TRUE
: ALC_FALSE
);
301 Store latest ALC Error
303 ALCvoid
SetALCError(ALenum errorCode
)
305 g_eLastContextError
= errorCode
;
314 ALCvoid
SuspendContext(ALCcontext
*pContext
)
317 EnterCriticalSection(&g_mutex
);
326 ALCvoid
ProcessContext(ALCcontext
*pContext
)
329 LeaveCriticalSection(&g_mutex
);
336 Initialize Context variables
338 static ALvoid
InitContext(ALCcontext
*pContext
)
340 //Initialise listener
341 pContext
->Listener
.Gain
= 1.0f
;
342 pContext
->Listener
.Position
[0] = 0.0f
;
343 pContext
->Listener
.Position
[1] = 0.0f
;
344 pContext
->Listener
.Position
[2] = 0.0f
;
345 pContext
->Listener
.Velocity
[0] = 0.0f
;
346 pContext
->Listener
.Velocity
[1] = 0.0f
;
347 pContext
->Listener
.Velocity
[2] = 0.0f
;
348 pContext
->Listener
.Forward
[0] = 0.0f
;
349 pContext
->Listener
.Forward
[1] = 0.0f
;
350 pContext
->Listener
.Forward
[2] = -1.0f
;
351 pContext
->Listener
.Up
[0] = 0.0f
;
352 pContext
->Listener
.Up
[1] = 1.0f
;
353 pContext
->Listener
.Up
[2] = 0.0f
;
356 pContext
->LastError
= AL_NO_ERROR
;
357 pContext
->InUse
= AL_FALSE
;
360 pContext
->Frequency
= pContext
->Device
->Frequency
;
363 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
364 pContext
->DopplerFactor
= 1.0f
;
365 pContext
->DopplerVelocity
= 1.0f
;
366 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
368 pContext
->lNumStereoSources
= 1;
369 pContext
->lNumMonoSources
= pContext
->Device
->MaxNoOfSources
- pContext
->lNumStereoSources
;
371 strcpy(pContext
->ExtensionList
, "AL_EXT_EXPONENT_DISTANCE AL_EXT_LINEAR_DISTANCE AL_EXT_OFFSET");
378 Clean up Context, destroy any remaining Sources
380 static ALCvoid
ExitContext(ALCcontext
*pContext
)
384 ALsource
*ALTempSource
;
387 if (pContext
->SourceCount
>0)
388 AL_PRINT("alcDestroyContext() %d Source(s) NOT deleted\n", pContext
->SourceCount
);
391 // Free all the Sources still remaining
392 ALSource
= pContext
->Source
;
393 for (i
= 0; i
< pContext
->SourceCount
; i
++)
395 ALTempSource
= ALSource
->next
;
396 ALTHUNK_REMOVEENTRY(ALSource
->source
);
397 memset(ALSource
, 0, sizeof(ALsource
));
399 ALSource
= ALTempSource
;
403 pContext
->LastError
= AL_NO_ERROR
;
404 pContext
->InUse
= AL_FALSE
;
407 ///////////////////////////////////////////////////////
410 ///////////////////////////////////////////////////////
411 // ALC Functions calls
414 // This should probably move to another c file but for now ...
415 ALCAPI ALCdevice
* ALCAPIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
417 ALCboolean DeviceFound
= ALC_FALSE
;
418 ALCdevice
*pDevice
= NULL
;
423 pDevice
= malloc(sizeof(ALCdevice
));
428 //Initialise device structure
429 memset(pDevice
, 0, sizeof(ALCdevice
));
432 pDevice
->InUse
= AL_TRUE
;
433 pDevice
->IsCaptureDevice
= AL_TRUE
;
435 pDevice
->Frequency
= frequency
;
436 pDevice
->Format
= format
;
437 pDevice
->Channels
= aluChannelsFromFormat(format
);
438 pDevice
->FrameSize
= aluBytesFromFormat(format
) *
441 for(i
= 0;BackendList
[i
].Init
;i
++)
443 pDevice
->Funcs
= &BackendList
[i
].Funcs
;
444 if(ALCdevice_OpenCapture(pDevice
, deviceName
, frequency
, format
, SampleSize
))
446 DeviceFound
= ALC_TRUE
;
452 SetALCError(ALC_INVALID_VALUE
);
461 SetALCError(ALC_OUT_OF_MEMORY
);
466 ALCAPI ALCboolean ALCAPIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
468 ALCboolean bReturn
= ALC_FALSE
;
470 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
472 ALCdevice_CloseCapture(pDevice
);
478 SetALCError(ALC_INVALID_DEVICE
);
483 ALCAPI
void ALCAPIENTRY
alcCaptureStart(ALCdevice
*pDevice
)
485 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
486 ALCdevice_StartCapture(pDevice
);
488 SetALCError(ALC_INVALID_DEVICE
);
491 ALCAPI
void ALCAPIENTRY
alcCaptureStop(ALCdevice
*pDevice
)
493 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
494 ALCdevice_StopCapture(pDevice
);
496 SetALCError(ALC_INVALID_DEVICE
);
499 ALCAPI
void ALCAPIENTRY
alcCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCsizei lSamples
)
501 if ((pDevice
) && (pDevice
->IsCaptureDevice
))
502 ALCdevice_CaptureSamples(pDevice
, pBuffer
, lSamples
);
504 SetALCError(ALC_INVALID_DEVICE
);
510 Return last ALC generated error code
512 ALCAPI ALCenum ALCAPIENTRY
alcGetError(ALCdevice
*device
)
518 errorCode
= g_eLastContextError
;
519 g_eLastContextError
= ALC_NO_ERROR
;
529 ALCAPI ALCvoid ALCAPIENTRY
alcSuspendContext(ALCcontext
*pContext
)
531 // Not a lot happens here !
541 ALCAPI ALCvoid ALCAPIENTRY
alcProcessContext(ALCcontext
*pContext
)
543 // Not a lot happens here !
551 Returns information about the Device, and error strings
553 ALCAPI
const ALCchar
* ALCAPIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
555 const ALCchar
*value
= NULL
;
565 case ALC_INVALID_ENUM
:
566 value
= alcErrInvalidEnum
;
569 case ALC_INVALID_VALUE
:
570 value
= alcErrInvalidValue
;
573 case ALC_INVALID_DEVICE
:
574 value
= alcErrInvalidDevice
;
577 case ALC_INVALID_CONTEXT
:
578 value
= alcErrInvalidContext
;
581 case ALC_OUT_OF_MEMORY
:
582 value
= alcErrOutOfMemory
;
585 case ALC_DEFAULT_DEVICE_SPECIFIER
:
586 value
= alcDefaultDeviceSpecifier
;
589 case ALC_DEVICE_SPECIFIER
:
591 value
= pDevice
->szDeviceName
;
593 value
= alcDeviceList
;
596 case ALC_ALL_DEVICES_SPECIFIER
:
597 value
= alcAllDeviceList
;
600 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
601 value
= alcDefaultAllDeviceSpecifier
;
604 case ALC_CAPTURE_DEVICE_SPECIFIER
:
606 value
= pDevice
->szDeviceName
;
608 value
= alcCaptureDeviceList
;
611 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
612 value
= alcCaptureDefaultDeviceSpecifier
;
616 value
= alcExtensionList
;
620 SetALCError(ALC_INVALID_ENUM
);
631 Returns information about the Device and the version of Open AL
633 ALCAPI ALCvoid ALCAPIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
637 if ((device
)&&(device
->IsCaptureDevice
))
639 SuspendContext(NULL
);
644 case ALC_CAPTURE_SAMPLES
:
645 if ((size
) && (data
))
646 *data
= ALCdevice_AvailableSamples(device
);
648 SetALCError(ALC_INVALID_VALUE
);
652 SetALCError(ALC_INVALID_ENUM
);
656 ProcessContext(NULL
);
665 case ALC_MAJOR_VERSION
:
667 SetALCError(ALC_INVALID_VALUE
);
669 *data
= alcMajorVersion
;
672 case ALC_MINOR_VERSION
:
674 SetALCError(ALC_INVALID_VALUE
);
676 *data
= alcMinorVersion
;
679 case ALC_EFX_MAJOR_VERSION
:
681 SetALCError(ALC_INVALID_VALUE
);
683 *data
= alcEFXMajorVersion
;
686 case ALC_EFX_MINOR_VERSION
:
688 SetALCError(ALC_INVALID_VALUE
);
690 *data
= alcEFXMinorVersion
;
693 case ALC_MAX_AUXILIARY_SENDS
:
695 SetALCError(ALC_INVALID_VALUE
);
697 *data
= 0; /* FIXME: Should be 1 or more */
700 case ALC_ATTRIBUTES_SIZE
:
702 SetALCError(ALC_INVALID_DEVICE
);
704 SetALCError(ALC_INVALID_VALUE
);
709 case ALC_ALL_ATTRIBUTES
:
711 SetALCError(ALC_INVALID_DEVICE
);
713 SetALCError(ALC_INVALID_VALUE
);
718 data
[i
++] = ALC_FREQUENCY
;
719 data
[i
++] = device
->Frequency
;
721 data
[i
++] = ALC_REFRESH
;
722 data
[i
++] = device
->UpdateFreq
;
724 data
[i
++] = ALC_SYNC
;
725 data
[i
++] = ALC_FALSE
;
727 SuspendContext(NULL
);
728 if(device
->Context
&& size
>= 12)
730 data
[i
++] = ALC_MONO_SOURCES
;
731 data
[i
++] = device
->Context
->lNumMonoSources
;
733 data
[i
++] = ALC_STEREO_SOURCES
;
734 data
[i
++] = device
->Context
->lNumStereoSources
;
736 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
737 data
[i
++] = 0; /* FIXME */
739 ProcessContext(NULL
);
747 SetALCError(ALC_INVALID_DEVICE
);
749 SetALCError(ALC_INVALID_VALUE
);
751 *data
= device
->Frequency
;
756 SetALCError(ALC_INVALID_DEVICE
);
758 SetALCError(ALC_INVALID_VALUE
);
760 *data
= device
->UpdateFreq
;
765 SetALCError(ALC_INVALID_DEVICE
);
767 SetALCError(ALC_INVALID_VALUE
);
773 SetALCError(ALC_INVALID_ENUM
);
778 SetALCError(ALC_INVALID_VALUE
);
786 alcIsExtensionPresent
788 Determines if there is support for a particular extension
790 ALCAPI ALCboolean ALCAPIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
792 ALCboolean bResult
= ALC_FALSE
;
799 while(alcExtensions
[i
].extName
&&
800 strcasecmp(alcExtensions
[i
].extName
,extName
) != 0)
803 if (alcExtensions
[i
].extName
)
807 SetALCError(ALC_INVALID_VALUE
);
816 Retrieves the function address for a particular extension function
818 ALCAPI ALCvoid
* ALCAPIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
820 ALCvoid
*pFunction
= NULL
;
827 while(alcFunctions
[i
].funcName
&&
828 strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
830 pFunction
= alcFunctions
[i
].address
;
833 SetALCError(ALC_INVALID_VALUE
);
842 Get the value for a particular ALC Enumerated Value
844 ALCAPI ALCenum ALCAPIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
851 while ((enumeration
[i
].enumName
)&&(strcmp(enumeration
[i
].enumName
,enumName
)))
853 val
= enumeration
[i
].value
;
855 if(!enumeration
[i
].enumName
)
856 SetALCError(ALC_INVALID_VALUE
);
865 Create and attach a Context to a particular Device.
867 ALCAPI ALCcontext
* ALCAPIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
869 ALCcontext
*ALContext
= NULL
;
870 ALuint ulAttributeIndex
, ulRequestedStereoSources
;
872 if ((device
)&&(!device
->IsCaptureDevice
))
874 // Reset Context Last Error code
875 g_eLastContextError
= ALC_NO_ERROR
;
877 // Current implementation only allows one Context per Device
880 ALContext
= calloc(1, sizeof(ALCcontext
));
883 SetALCError(ALC_OUT_OF_MEMORY
);
887 ALContext
->Device
= device
;
888 InitContext(ALContext
);
890 device
->Context
= ALContext
;
892 SuspendContext(NULL
);
894 ALContext
->next
= g_pContextList
;
895 g_pContextList
= ALContext
;
898 ProcessContext(NULL
);
900 // Check for Voice Count attributes
903 ulAttributeIndex
= 0;
904 while ((ulAttributeIndex
< 10) && (attrList
[ulAttributeIndex
]))
906 if (attrList
[ulAttributeIndex
] == ALC_STEREO_SOURCES
)
908 ulRequestedStereoSources
= attrList
[ulAttributeIndex
+ 1];
910 if (ulRequestedStereoSources
> ALContext
->Device
->MaxNoOfSources
)
911 ulRequestedStereoSources
= ALContext
->Device
->MaxNoOfSources
;
913 ALContext
->lNumStereoSources
= ulRequestedStereoSources
;
914 ALContext
->lNumMonoSources
= ALContext
->Device
->MaxNoOfSources
- ALContext
->lNumStereoSources
;
918 ulAttributeIndex
+= 2;
924 SetALCError(ALC_INVALID_VALUE
);
929 SetALCError(ALC_INVALID_DEVICE
);
940 ALCAPI ALCvoid ALCAPIENTRY
alcDestroyContext(ALCcontext
*context
)
945 SuspendContext(NULL
);
947 if (IsContext(context
))
950 SuspendContext(context
);
952 context
->Device
->Context
= NULL
;
954 list
= &g_pContextList
;
955 while(*list
!= context
)
956 list
= &(*list
)->next
;
958 *list
= (*list
)->next
;
962 ProcessContext(context
);
964 ExitContext(context
);
966 // Free memory (MUST do this after ProcessContext)
967 memset(context
, 0, sizeof(ALCcontext
));
971 SetALCError(ALC_INVALID_CONTEXT
);
973 ProcessContext(NULL
);
980 Returns the currently active Context
982 ALCAPI ALCcontext
* ALCAPIENTRY
alcGetCurrentContext(ALCvoid
)
984 ALCcontext
*pContext
= NULL
;
986 SuspendContext(NULL
);
988 pContext
= g_pContextList
;
989 while ((pContext
) && (!pContext
->InUse
))
990 pContext
= pContext
->next
;
992 ProcessContext(NULL
);
1001 Returns the Device that a particular Context is attached to
1003 ALCAPI ALCdevice
* ALCAPIENTRY
alcGetContextsDevice(ALCcontext
*pContext
)
1005 ALCdevice
*pDevice
= NULL
;
1007 SuspendContext(NULL
);
1008 if (IsContext(pContext
))
1009 pDevice
= pContext
->Device
;
1011 SetALCError(ALC_INVALID_CONTEXT
);
1012 ProcessContext(NULL
);
1019 alcMakeContextCurrent
1021 Makes the given Context the active Context
1023 ALCAPI ALCboolean ALCAPIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
1025 ALCcontext
*ALContext
;
1026 ALboolean bReturn
= AL_TRUE
;
1028 SuspendContext(NULL
);
1030 // context must be a valid Context or NULL
1031 if ((IsContext(context
)) || (context
== NULL
))
1033 if ((ALContext
=alcGetCurrentContext()))
1035 SuspendContext(ALContext
);
1036 ALContext
->InUse
=AL_FALSE
;
1037 ProcessContext(ALContext
);
1040 if ((ALContext
=context
) && (ALContext
->Device
))
1042 SuspendContext(ALContext
);
1043 ALContext
->InUse
=AL_TRUE
;
1044 ProcessContext(ALContext
);
1049 SetALCError(ALC_INVALID_CONTEXT
);
1053 ProcessContext(NULL
);
1062 Open the Device specified.
1064 ALCAPI ALCdevice
* ALCAPIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
1066 ALboolean bDeviceFound
= AL_FALSE
;
1072 device
= malloc(sizeof(ALCdevice
));
1077 //Initialise device structure
1078 memset(device
, 0, sizeof(ALCdevice
));
1081 device
->InUse
= AL_TRUE
;
1082 device
->IsCaptureDevice
= AL_FALSE
;
1085 device
->Frequency
= GetConfigValueInt(NULL
, "frequency", SWMIXER_OUTPUT_RATE
);
1086 if((ALint
)device
->Frequency
<= 0)
1087 device
->Frequency
= SWMIXER_OUTPUT_RATE
;
1089 fmt
= GetConfigValue(NULL
, "format", "AL_FORMAT_STEREO16");
1091 device
->Format
= alGetEnumValue(fmt
);
1093 device
->Channels
= aluChannelsFromFormat(device
->Format
);
1094 if(!device
->Channels
)
1096 device
->Format
= AL_FORMAT_STEREO16
;
1097 device
->Channels
= 2;
1098 device
->FrameSize
= 4;
1101 device
->FrameSize
= aluBytesFromFormat(device
->Format
) *
1104 device
->UpdateFreq
= GetConfigValueInt(NULL
, "refresh", 0);
1105 if((ALint
)device
->UpdateFreq
<= 0)
1106 device
->UpdateFreq
= 8192 * device
->Frequency
/ 22050;
1108 // Find a playback device to open
1109 for(i
= 0;BackendList
[i
].Init
;i
++)
1111 device
->Funcs
= &BackendList
[i
].Funcs
;
1112 if(ALCdevice_OpenPlayback(device
, deviceName
))
1114 bDeviceFound
= AL_TRUE
;
1121 // No suitable output device found
1134 Close the specified Device
1136 ALCAPI ALCboolean ALCAPIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
1138 ALCboolean bReturn
= ALC_FALSE
;
1140 if ((pDevice
)&&(!pDevice
->IsCaptureDevice
))
1142 ALCdevice_ClosePlayback(pDevice
);
1144 //Release device structure
1145 memset(pDevice
, 0, sizeof(ALCdevice
));
1151 SetALCError(ALC_INVALID_DEVICE
);
1155 ///////////////////////////////////////////////////////