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
->FrameSize
= aluBytesFromFormat(format
) *
434 aluChannelsFromFormat(format
);
436 for(i
= 0;BackendList
[i
].Init
;i
++)
438 pDevice
->Funcs
= &BackendList
[i
].Funcs
;
439 if(ALCdevice_OpenCapture(pDevice
, deviceName
, frequency
, format
, SampleSize
))
441 SuspendContext(NULL
);
442 pDevice
->next
= g_pDeviceList
;
443 g_pDeviceList
= pDevice
;
445 ProcessContext(NULL
);
447 DeviceFound
= ALC_TRUE
;
453 SetALCError(ALC_INVALID_VALUE
);
462 SetALCError(ALC_OUT_OF_MEMORY
);
467 ALCAPI ALCboolean ALCAPIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
469 ALCboolean bReturn
= ALC_FALSE
;
472 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
474 SuspendContext(NULL
);
476 list
= &g_pDeviceList
;
477 while(*list
!= pDevice
)
478 list
= &(*list
)->next
;
480 *list
= (*list
)->next
;
483 ProcessContext(NULL
);
485 ALCdevice_CloseCapture(pDevice
);
491 SetALCError(ALC_INVALID_DEVICE
);
496 ALCAPI
void ALCAPIENTRY
alcCaptureStart(ALCdevice
*pDevice
)
498 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
499 ALCdevice_StartCapture(pDevice
);
501 SetALCError(ALC_INVALID_DEVICE
);
504 ALCAPI
void ALCAPIENTRY
alcCaptureStop(ALCdevice
*pDevice
)
506 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
507 ALCdevice_StopCapture(pDevice
);
509 SetALCError(ALC_INVALID_DEVICE
);
512 ALCAPI
void ALCAPIENTRY
alcCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCsizei lSamples
)
514 if ((pDevice
) && (pDevice
->IsCaptureDevice
))
515 ALCdevice_CaptureSamples(pDevice
, pBuffer
, lSamples
);
517 SetALCError(ALC_INVALID_DEVICE
);
523 Return last ALC generated error code
525 ALCAPI ALCenum ALCAPIENTRY
alcGetError(ALCdevice
*device
)
531 errorCode
= g_eLastContextError
;
532 g_eLastContextError
= ALC_NO_ERROR
;
542 ALCAPI ALCvoid ALCAPIENTRY
alcSuspendContext(ALCcontext
*pContext
)
544 // Not a lot happens here !
554 ALCAPI ALCvoid ALCAPIENTRY
alcProcessContext(ALCcontext
*pContext
)
556 // Not a lot happens here !
564 Returns information about the Device, and error strings
566 ALCAPI
const ALCchar
* ALCAPIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
568 const ALCchar
*value
= NULL
;
578 case ALC_INVALID_ENUM
:
579 value
= alcErrInvalidEnum
;
582 case ALC_INVALID_VALUE
:
583 value
= alcErrInvalidValue
;
586 case ALC_INVALID_DEVICE
:
587 value
= alcErrInvalidDevice
;
590 case ALC_INVALID_CONTEXT
:
591 value
= alcErrInvalidContext
;
594 case ALC_OUT_OF_MEMORY
:
595 value
= alcErrOutOfMemory
;
598 case ALC_DEFAULT_DEVICE_SPECIFIER
:
599 value
= alcDefaultDeviceSpecifier
;
602 case ALC_DEVICE_SPECIFIER
:
604 value
= pDevice
->szDeviceName
;
606 value
= alcDeviceList
;
609 case ALC_ALL_DEVICES_SPECIFIER
:
610 value
= alcAllDeviceList
;
613 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
614 value
= alcDefaultAllDeviceSpecifier
;
617 case ALC_CAPTURE_DEVICE_SPECIFIER
:
619 value
= pDevice
->szDeviceName
;
621 value
= alcCaptureDeviceList
;
624 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
625 value
= alcCaptureDefaultDeviceSpecifier
;
629 value
= alcExtensionList
;
633 SetALCError(ALC_INVALID_ENUM
);
644 Returns information about the Device and the version of Open AL
646 ALCAPI ALCvoid ALCAPIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
650 if ((device
)&&(device
->IsCaptureDevice
))
652 SuspendContext(NULL
);
657 case ALC_CAPTURE_SAMPLES
:
658 if ((size
) && (data
))
659 *data
= ALCdevice_AvailableSamples(device
);
661 SetALCError(ALC_INVALID_VALUE
);
665 SetALCError(ALC_INVALID_ENUM
);
669 ProcessContext(NULL
);
678 case ALC_MAJOR_VERSION
:
680 SetALCError(ALC_INVALID_VALUE
);
682 *data
= alcMajorVersion
;
685 case ALC_MINOR_VERSION
:
687 SetALCError(ALC_INVALID_VALUE
);
689 *data
= alcMinorVersion
;
692 case ALC_EFX_MAJOR_VERSION
:
694 SetALCError(ALC_INVALID_VALUE
);
696 *data
= alcEFXMajorVersion
;
699 case ALC_EFX_MINOR_VERSION
:
701 SetALCError(ALC_INVALID_VALUE
);
703 *data
= alcEFXMinorVersion
;
706 case ALC_MAX_AUXILIARY_SENDS
:
708 SetALCError(ALC_INVALID_VALUE
);
713 case ALC_ATTRIBUTES_SIZE
:
715 SetALCError(ALC_INVALID_DEVICE
);
717 SetALCError(ALC_INVALID_VALUE
);
722 case ALC_ALL_ATTRIBUTES
:
724 SetALCError(ALC_INVALID_DEVICE
);
726 SetALCError(ALC_INVALID_VALUE
);
731 data
[i
++] = ALC_FREQUENCY
;
732 data
[i
++] = device
->Frequency
;
734 data
[i
++] = ALC_REFRESH
;
735 data
[i
++] = device
->Frequency
/ device
->UpdateFreq
;
737 data
[i
++] = ALC_SYNC
;
738 data
[i
++] = ALC_FALSE
;
740 SuspendContext(NULL
);
741 if(device
->Context
&& size
>= 12)
743 data
[i
++] = ALC_MONO_SOURCES
;
744 data
[i
++] = device
->Context
->lNumMonoSources
;
746 data
[i
++] = ALC_STEREO_SOURCES
;
747 data
[i
++] = device
->Context
->lNumStereoSources
;
749 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
750 data
[i
++] = MAX_SENDS
;
752 ProcessContext(NULL
);
760 SetALCError(ALC_INVALID_DEVICE
);
762 SetALCError(ALC_INVALID_VALUE
);
764 *data
= device
->Frequency
;
769 SetALCError(ALC_INVALID_DEVICE
);
771 SetALCError(ALC_INVALID_VALUE
);
773 *data
= device
->Frequency
/ device
->UpdateFreq
;
778 SetALCError(ALC_INVALID_DEVICE
);
780 SetALCError(ALC_INVALID_VALUE
);
785 case ALC_MONO_SOURCES
:
786 if(!device
|| !device
->Context
)
787 SetALCError(ALC_INVALID_DEVICE
);
789 SetALCError(ALC_INVALID_VALUE
);
791 *data
= device
->Context
->lNumMonoSources
;
794 case ALC_STEREO_SOURCES
:
795 if(!device
|| !device
->Context
)
796 SetALCError(ALC_INVALID_DEVICE
);
798 SetALCError(ALC_INVALID_VALUE
);
800 *data
= device
->Context
->lNumStereoSources
;
804 SetALCError(ALC_INVALID_ENUM
);
809 SetALCError(ALC_INVALID_VALUE
);
817 alcIsExtensionPresent
819 Determines if there is support for a particular extension
821 ALCAPI ALCboolean ALCAPIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
823 ALCboolean bResult
= ALC_FALSE
;
832 len
= strlen(extName
);
833 ptr
= alcExtensionList
;
836 if(strncasecmp(ptr
, extName
, len
) == 0 &&
837 (ptr
[len
] == '\0' || isspace(ptr
[len
])))
842 if((ptr
=strchr(ptr
, ' ')) != NULL
)
846 } while(isspace(*ptr
));
851 SetALCError(ALC_INVALID_VALUE
);
860 Retrieves the function address for a particular extension function
862 ALCAPI ALCvoid
* ALCAPIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
864 ALCvoid
*pFunction
= NULL
;
871 while(alcFunctions
[i
].funcName
&&
872 strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
874 pFunction
= alcFunctions
[i
].address
;
877 SetALCError(ALC_INVALID_VALUE
);
886 Get the value for a particular ALC Enumerated Value
888 ALCAPI ALCenum ALCAPIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
895 while ((enumeration
[i
].enumName
)&&(strcmp(enumeration
[i
].enumName
,enumName
)))
897 val
= enumeration
[i
].value
;
899 if(!enumeration
[i
].enumName
)
900 SetALCError(ALC_INVALID_VALUE
);
909 Create and attach a Context to a particular Device.
911 ALCAPI ALCcontext
* ALCAPIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
913 ALCcontext
*ALContext
= NULL
;
914 ALuint ulAttributeIndex
, ulRequestedStereoSources
;
916 if ((device
)&&(!device
->IsCaptureDevice
))
918 // Reset Context Last Error code
919 g_eLastContextError
= ALC_NO_ERROR
;
921 // Current implementation only allows one Context per Device
924 ALContext
= calloc(1, sizeof(ALCcontext
));
927 SetALCError(ALC_OUT_OF_MEMORY
);
931 ALContext
->Device
= device
;
932 InitContext(ALContext
);
934 device
->Context
= ALContext
;
936 SuspendContext(NULL
);
938 ALContext
->next
= g_pContextList
;
939 g_pContextList
= ALContext
;
942 ProcessContext(NULL
);
944 // Check for Voice Count attributes
947 ulAttributeIndex
= 0;
948 while ((ulAttributeIndex
< 10) && (attrList
[ulAttributeIndex
]))
950 if (attrList
[ulAttributeIndex
] == ALC_STEREO_SOURCES
)
952 ulRequestedStereoSources
= attrList
[ulAttributeIndex
+ 1];
954 if (ulRequestedStereoSources
> ALContext
->Device
->MaxNoOfSources
)
955 ulRequestedStereoSources
= ALContext
->Device
->MaxNoOfSources
;
957 ALContext
->lNumStereoSources
= ulRequestedStereoSources
;
958 ALContext
->lNumMonoSources
= ALContext
->Device
->MaxNoOfSources
- ALContext
->lNumStereoSources
;
962 ulAttributeIndex
+= 2;
968 SetALCError(ALC_INVALID_VALUE
);
973 SetALCError(ALC_INVALID_DEVICE
);
984 ALCAPI ALCvoid ALCAPIENTRY
alcDestroyContext(ALCcontext
*context
)
989 SuspendContext(NULL
);
991 if (IsContext(context
))
994 SuspendContext(context
);
996 ReleaseALSources(context
);
997 ReleaseALAuxiliaryEffectSlots(context
);
999 context
->Device
->Context
= NULL
;
1001 list
= &g_pContextList
;
1002 while(*list
!= context
)
1003 list
= &(*list
)->next
;
1005 *list
= (*list
)->next
;
1009 ProcessContext(context
);
1011 ExitContext(context
);
1013 // Free memory (MUST do this after ProcessContext)
1014 memset(context
, 0, sizeof(ALCcontext
));
1018 SetALCError(ALC_INVALID_CONTEXT
);
1020 ProcessContext(NULL
);
1025 alcGetCurrentContext
1027 Returns the currently active Context
1029 ALCAPI ALCcontext
* ALCAPIENTRY
alcGetCurrentContext(ALCvoid
)
1031 ALCcontext
*pContext
= NULL
;
1033 SuspendContext(NULL
);
1035 pContext
= g_pContextList
;
1036 while ((pContext
) && (!pContext
->InUse
))
1037 pContext
= pContext
->next
;
1039 ProcessContext(NULL
);
1046 alcGetContextsDevice
1048 Returns the Device that a particular Context is attached to
1050 ALCAPI ALCdevice
* ALCAPIENTRY
alcGetContextsDevice(ALCcontext
*pContext
)
1052 ALCdevice
*pDevice
= NULL
;
1054 SuspendContext(NULL
);
1055 if (IsContext(pContext
))
1056 pDevice
= pContext
->Device
;
1058 SetALCError(ALC_INVALID_CONTEXT
);
1059 ProcessContext(NULL
);
1066 alcMakeContextCurrent
1068 Makes the given Context the active Context
1070 ALCAPI ALCboolean ALCAPIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
1072 ALCcontext
*ALContext
;
1073 ALboolean bReturn
= AL_TRUE
;
1075 SuspendContext(NULL
);
1077 // context must be a valid Context or NULL
1078 if ((IsContext(context
)) || (context
== NULL
))
1080 if ((ALContext
=alcGetCurrentContext()))
1082 SuspendContext(ALContext
);
1083 ALContext
->InUse
=AL_FALSE
;
1084 ProcessContext(ALContext
);
1087 if ((ALContext
=context
) && (ALContext
->Device
))
1089 SuspendContext(ALContext
);
1090 ALContext
->InUse
=AL_TRUE
;
1091 ProcessContext(ALContext
);
1096 SetALCError(ALC_INVALID_CONTEXT
);
1100 ProcessContext(NULL
);
1109 Open the Device specified.
1111 ALCAPI ALCdevice
* ALCAPIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
1113 ALboolean bDeviceFound
= AL_FALSE
;
1119 if(deviceName
&& !deviceName
[0])
1122 device
= malloc(sizeof(ALCdevice
));
1127 //Initialise device structure
1128 memset(device
, 0, sizeof(ALCdevice
));
1131 device
->InUse
= AL_TRUE
;
1132 device
->IsCaptureDevice
= AL_FALSE
;
1135 device
->Frequency
= GetConfigValueInt(NULL
, "frequency", SWMIXER_OUTPUT_RATE
);
1136 if((ALint
)device
->Frequency
<= 0)
1137 device
->Frequency
= SWMIXER_OUTPUT_RATE
;
1139 fmt
= GetConfigValue(NULL
, "format", "AL_FORMAT_STEREO16");
1141 device
->Format
= alGetEnumValue(fmt
);
1143 if(!aluChannelsFromFormat(device
->Format
))
1145 device
->Format
= AL_FORMAT_STEREO16
;
1146 device
->FrameSize
= 4;
1149 device
->FrameSize
= aluBytesFromFormat(device
->Format
) *
1150 aluChannelsFromFormat(device
->Format
);
1152 device
->UpdateFreq
= GetConfigValueInt(NULL
, "refresh", 8192);
1153 if((ALint
)device
->UpdateFreq
<= 0)
1154 device
->UpdateFreq
= 8192;
1156 device
->MaxNoOfSources
= GetConfigValueInt(NULL
, "sources", 256);
1157 if((ALint
)device
->MaxNoOfSources
<= 0)
1158 device
->MaxNoOfSources
= 256;
1160 // Find a playback device to open
1161 for(i
= 0;BackendList
[i
].Init
;i
++)
1163 device
->Funcs
= &BackendList
[i
].Funcs
;
1164 if(ALCdevice_OpenPlayback(device
, deviceName
))
1166 SuspendContext(NULL
);
1167 device
->next
= g_pDeviceList
;
1168 g_pDeviceList
= device
;
1170 ProcessContext(NULL
);
1172 bDeviceFound
= AL_TRUE
;
1179 // No suitable output device found
1192 Close the specified Device
1194 ALCAPI ALCboolean ALCAPIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
1196 ALCboolean bReturn
= ALC_FALSE
;
1199 if ((pDevice
)&&(!pDevice
->IsCaptureDevice
))
1201 SuspendContext(NULL
);
1203 list
= &g_pDeviceList
;
1204 while(*list
!= pDevice
)
1205 list
= &(*list
)->next
;
1207 *list
= (*list
)->next
;
1210 ProcessContext(NULL
);
1212 if(pDevice
->Context
)
1213 alcDestroyContext(pDevice
->Context
);
1214 ALCdevice_ClosePlayback(pDevice
);
1216 //Release device structure
1217 memset(pDevice
, 0, sizeof(ALCdevice
));
1223 SetALCError(ALC_INVALID_DEVICE
);
1229 ALCvoid
ReleaseALC(ALCvoid
)
1234 if(g_ulContextCount
> 0)
1235 AL_PRINT("exit() %u device(s) and %u context(s) NOT deleted\n", g_ulDeviceCount
, g_ulContextCount
);
1238 while(g_pDeviceList
)
1240 Dev
= g_pDeviceList
;
1241 g_pDeviceList
= g_pDeviceList
->next
;
1242 if(Dev
->IsCaptureDevice
)
1243 alcCaptureCloseDevice(Dev
);
1245 alcCloseDevice(Dev
);
1249 ///////////////////////////////////////////////////////