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)
204 const char *devs
, *str
;
208 InitializeCriticalSection(&_alMutex
);
212 devs
= GetConfigValue(NULL
, "drivers", "");
217 const char *next
= devs
;
223 next
= strchr(devs
, ',');
225 if(!devs
[0] || devs
[0] == ',')
228 len
= (next
? ((size_t)(next
-devs
)) : strlen(devs
));
229 for(n
= i
;BackendList
[n
].Init
;n
++)
231 if(len
== strlen(BackendList
[n
].name
) &&
232 strncmp(BackendList
[n
].name
, devs
, len
) == 0)
234 const char *name
= BackendList
[i
].name
;
235 void (*Init
)(BackendFuncs
*) = BackendList
[i
].Init
;
237 BackendList
[i
].name
= BackendList
[n
].name
;
238 BackendList
[i
].Init
= BackendList
[n
].Init
;
240 BackendList
[n
].name
= name
;
241 BackendList
[n
].Init
= Init
;
248 BackendList
[i
].name
= NULL
;
249 BackendList
[i
].Init
= NULL
;
252 for(i
= 0;BackendList
[i
].Init
;i
++)
253 BackendList
[i
].Init(&BackendList
[i
].Funcs
);
255 str
= GetConfigValue(NULL
, "stereodup", "false");
256 DuplicateStereo
= (strcasecmp(str
, "true") == 0 ||
257 strcasecmp(str
, "yes") == 0 ||
258 strcasecmp(str
, "on") == 0 ||
263 ALCchar
*AppendDeviceList(char *name
)
266 ALCchar
*ret
= alcDeviceList
+pos
;
267 pos
+= snprintf(alcDeviceList
+pos
, sizeof(alcDeviceList
)-pos
, "%s", name
) + 1;
271 ALCchar
*AppendAllDeviceList(char *name
)
274 ALCchar
*ret
= alcAllDeviceList
+pos
;
275 pos
+= snprintf(alcAllDeviceList
+pos
, sizeof(alcAllDeviceList
)-pos
, "%s", name
) + 1;
279 ALCchar
*AppendCaptureDeviceList(char *name
)
282 ALCchar
*ret
= alcCaptureDeviceList
+pos
;
283 pos
+= snprintf(alcCaptureDeviceList
+pos
, sizeof(alcCaptureDeviceList
)-pos
, "%s", name
) + 1;
290 Check pContext is a valid Context pointer
292 static ALCboolean
IsContext(ALCcontext
*pContext
)
294 ALCcontext
*pTempContext
;
296 pTempContext
= g_pContextList
;
297 while (pTempContext
&& pTempContext
!= pContext
)
298 pTempContext
= pTempContext
->next
;
300 return (pTempContext
? ALC_TRUE
: ALC_FALSE
);
307 Store latest ALC Error
309 ALCvoid
SetALCError(ALenum errorCode
)
311 g_eLastContextError
= errorCode
;
320 ALCvoid
SuspendContext(ALCcontext
*pContext
)
323 EnterCriticalSection(&_alMutex
);
332 ALCvoid
ProcessContext(ALCcontext
*pContext
)
335 LeaveCriticalSection(&_alMutex
);
342 Initialize Context variables
344 static ALvoid
InitContext(ALCcontext
*pContext
)
348 //Initialise listener
349 pContext
->Listener
.Gain
= 1.0f
;
350 pContext
->Listener
.MetersPerUnit
= 1.0f
;
351 pContext
->Listener
.Position
[0] = 0.0f
;
352 pContext
->Listener
.Position
[1] = 0.0f
;
353 pContext
->Listener
.Position
[2] = 0.0f
;
354 pContext
->Listener
.Velocity
[0] = 0.0f
;
355 pContext
->Listener
.Velocity
[1] = 0.0f
;
356 pContext
->Listener
.Velocity
[2] = 0.0f
;
357 pContext
->Listener
.Forward
[0] = 0.0f
;
358 pContext
->Listener
.Forward
[1] = 0.0f
;
359 pContext
->Listener
.Forward
[2] = -1.0f
;
360 pContext
->Listener
.Up
[0] = 0.0f
;
361 pContext
->Listener
.Up
[1] = 1.0f
;
362 pContext
->Listener
.Up
[2] = 0.0f
;
365 pContext
->LastError
= AL_NO_ERROR
;
366 pContext
->InUse
= AL_FALSE
;
369 pContext
->Frequency
= pContext
->Device
->Frequency
;
372 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
373 pContext
->DopplerFactor
= 1.0f
;
374 pContext
->DopplerVelocity
= 1.0f
;
375 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
377 pContext
->lNumStereoSources
= 1;
378 pContext
->lNumMonoSources
= pContext
->Device
->MaxNoOfSources
- pContext
->lNumStereoSources
;
380 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");
382 level
= GetConfigValueInt(NULL
, "cf_level", 0);
383 if(level
> 0 && level
<= 6)
385 pContext
->bs2b
= calloc(1, sizeof(*pContext
->bs2b
));
386 bs2b_set_srate(pContext
->bs2b
, pContext
->Frequency
);
387 bs2b_set_level(pContext
->bs2b
, level
);
395 Clean up Context, destroy any remaining Sources
397 static ALCvoid
ExitContext(ALCcontext
*pContext
)
400 pContext
->LastError
= AL_NO_ERROR
;
401 pContext
->InUse
= AL_FALSE
;
403 free(pContext
->bs2b
);
404 pContext
->bs2b
= NULL
;
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 if(deviceName
&& !deviceName
[0])
426 pDevice
= malloc(sizeof(ALCdevice
));
431 //Initialise device structure
432 memset(pDevice
, 0, sizeof(ALCdevice
));
435 pDevice
->InUse
= AL_TRUE
;
436 pDevice
->IsCaptureDevice
= AL_TRUE
;
438 pDevice
->Frequency
= frequency
;
439 pDevice
->Format
= format
;
440 pDevice
->FrameSize
= aluBytesFromFormat(format
) *
441 aluChannelsFromFormat(format
);
443 for(i
= 0;BackendList
[i
].Init
;i
++)
445 pDevice
->Funcs
= &BackendList
[i
].Funcs
;
446 if(ALCdevice_OpenCapture(pDevice
, deviceName
, frequency
, format
, SampleSize
))
448 SuspendContext(NULL
);
449 pDevice
->next
= g_pDeviceList
;
450 g_pDeviceList
= pDevice
;
452 ProcessContext(NULL
);
454 DeviceFound
= ALC_TRUE
;
460 SetALCError(ALC_INVALID_VALUE
);
469 SetALCError(ALC_OUT_OF_MEMORY
);
474 ALCAPI ALCboolean ALCAPIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
476 ALCboolean bReturn
= ALC_FALSE
;
479 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
481 SuspendContext(NULL
);
483 list
= &g_pDeviceList
;
484 while(*list
!= pDevice
)
485 list
= &(*list
)->next
;
487 *list
= (*list
)->next
;
490 ProcessContext(NULL
);
492 ALCdevice_CloseCapture(pDevice
);
498 SetALCError(ALC_INVALID_DEVICE
);
503 ALCAPI
void ALCAPIENTRY
alcCaptureStart(ALCdevice
*pDevice
)
505 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
506 ALCdevice_StartCapture(pDevice
);
508 SetALCError(ALC_INVALID_DEVICE
);
511 ALCAPI
void ALCAPIENTRY
alcCaptureStop(ALCdevice
*pDevice
)
513 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
514 ALCdevice_StopCapture(pDevice
);
516 SetALCError(ALC_INVALID_DEVICE
);
519 ALCAPI
void ALCAPIENTRY
alcCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCsizei lSamples
)
521 if ((pDevice
) && (pDevice
->IsCaptureDevice
))
522 ALCdevice_CaptureSamples(pDevice
, pBuffer
, lSamples
);
524 SetALCError(ALC_INVALID_DEVICE
);
530 Return last ALC generated error code
532 ALCAPI ALCenum ALCAPIENTRY
alcGetError(ALCdevice
*device
)
538 errorCode
= g_eLastContextError
;
539 g_eLastContextError
= ALC_NO_ERROR
;
549 ALCAPI ALCvoid ALCAPIENTRY
alcSuspendContext(ALCcontext
*pContext
)
551 // Not a lot happens here !
561 ALCAPI ALCvoid ALCAPIENTRY
alcProcessContext(ALCcontext
*pContext
)
563 // Not a lot happens here !
571 Returns information about the Device, and error strings
573 ALCAPI
const ALCchar
* ALCAPIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
575 const ALCchar
*value
= NULL
;
585 case ALC_INVALID_ENUM
:
586 value
= alcErrInvalidEnum
;
589 case ALC_INVALID_VALUE
:
590 value
= alcErrInvalidValue
;
593 case ALC_INVALID_DEVICE
:
594 value
= alcErrInvalidDevice
;
597 case ALC_INVALID_CONTEXT
:
598 value
= alcErrInvalidContext
;
601 case ALC_OUT_OF_MEMORY
:
602 value
= alcErrOutOfMemory
;
605 case ALC_DEFAULT_DEVICE_SPECIFIER
:
606 value
= alcDefaultDeviceSpecifier
;
609 case ALC_DEVICE_SPECIFIER
:
611 value
= pDevice
->szDeviceName
;
613 value
= alcDeviceList
;
616 case ALC_ALL_DEVICES_SPECIFIER
:
617 value
= alcAllDeviceList
;
620 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
621 value
= alcDefaultAllDeviceSpecifier
;
624 case ALC_CAPTURE_DEVICE_SPECIFIER
:
626 value
= pDevice
->szDeviceName
;
628 value
= alcCaptureDeviceList
;
631 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
632 value
= alcCaptureDefaultDeviceSpecifier
;
636 value
= alcExtensionList
;
640 SetALCError(ALC_INVALID_ENUM
);
651 Returns information about the Device and the version of Open AL
653 ALCAPI ALCvoid ALCAPIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
657 if ((device
)&&(device
->IsCaptureDevice
))
659 SuspendContext(NULL
);
664 case ALC_CAPTURE_SAMPLES
:
665 if ((size
) && (data
))
666 *data
= ALCdevice_AvailableSamples(device
);
668 SetALCError(ALC_INVALID_VALUE
);
672 SetALCError(ALC_INVALID_ENUM
);
676 ProcessContext(NULL
);
685 case ALC_MAJOR_VERSION
:
687 SetALCError(ALC_INVALID_VALUE
);
689 *data
= alcMajorVersion
;
692 case ALC_MINOR_VERSION
:
694 SetALCError(ALC_INVALID_VALUE
);
696 *data
= alcMinorVersion
;
699 case ALC_EFX_MAJOR_VERSION
:
701 SetALCError(ALC_INVALID_VALUE
);
703 *data
= alcEFXMajorVersion
;
706 case ALC_EFX_MINOR_VERSION
:
708 SetALCError(ALC_INVALID_VALUE
);
710 *data
= alcEFXMinorVersion
;
713 case ALC_MAX_AUXILIARY_SENDS
:
715 SetALCError(ALC_INVALID_VALUE
);
720 case ALC_ATTRIBUTES_SIZE
:
722 SetALCError(ALC_INVALID_DEVICE
);
724 SetALCError(ALC_INVALID_VALUE
);
729 case ALC_ALL_ATTRIBUTES
:
731 SetALCError(ALC_INVALID_DEVICE
);
733 SetALCError(ALC_INVALID_VALUE
);
738 data
[i
++] = ALC_FREQUENCY
;
739 data
[i
++] = device
->Frequency
;
741 data
[i
++] = ALC_REFRESH
;
742 data
[i
++] = device
->Frequency
/ device
->UpdateFreq
;
744 data
[i
++] = ALC_SYNC
;
745 data
[i
++] = ALC_FALSE
;
747 SuspendContext(NULL
);
748 if(device
->Context
&& size
>= 12)
750 data
[i
++] = ALC_MONO_SOURCES
;
751 data
[i
++] = device
->Context
->lNumMonoSources
;
753 data
[i
++] = ALC_STEREO_SOURCES
;
754 data
[i
++] = device
->Context
->lNumStereoSources
;
756 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
757 data
[i
++] = MAX_SENDS
;
759 ProcessContext(NULL
);
767 SetALCError(ALC_INVALID_DEVICE
);
769 SetALCError(ALC_INVALID_VALUE
);
771 *data
= device
->Frequency
;
776 SetALCError(ALC_INVALID_DEVICE
);
778 SetALCError(ALC_INVALID_VALUE
);
780 *data
= device
->Frequency
/ device
->UpdateFreq
;
785 SetALCError(ALC_INVALID_DEVICE
);
787 SetALCError(ALC_INVALID_VALUE
);
792 case ALC_MONO_SOURCES
:
793 if(!device
|| !device
->Context
)
794 SetALCError(ALC_INVALID_DEVICE
);
796 SetALCError(ALC_INVALID_VALUE
);
798 *data
= device
->Context
->lNumMonoSources
;
801 case ALC_STEREO_SOURCES
:
802 if(!device
|| !device
->Context
)
803 SetALCError(ALC_INVALID_DEVICE
);
805 SetALCError(ALC_INVALID_VALUE
);
807 *data
= device
->Context
->lNumStereoSources
;
811 SetALCError(ALC_INVALID_ENUM
);
816 SetALCError(ALC_INVALID_VALUE
);
824 alcIsExtensionPresent
826 Determines if there is support for a particular extension
828 ALCAPI ALCboolean ALCAPIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
830 ALCboolean bResult
= ALC_FALSE
;
839 len
= strlen(extName
);
840 ptr
= alcExtensionList
;
843 if(strncasecmp(ptr
, extName
, len
) == 0 &&
844 (ptr
[len
] == '\0' || isspace(ptr
[len
])))
849 if((ptr
=strchr(ptr
, ' ')) != NULL
)
853 } while(isspace(*ptr
));
858 SetALCError(ALC_INVALID_VALUE
);
867 Retrieves the function address for a particular extension function
869 ALCAPI ALCvoid
* ALCAPIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
871 ALCvoid
*pFunction
= NULL
;
878 while(alcFunctions
[i
].funcName
&&
879 strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
881 pFunction
= alcFunctions
[i
].address
;
884 SetALCError(ALC_INVALID_VALUE
);
893 Get the value for a particular ALC Enumerated Value
895 ALCAPI ALCenum ALCAPIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
902 while ((enumeration
[i
].enumName
)&&(strcmp(enumeration
[i
].enumName
,enumName
)))
904 val
= enumeration
[i
].value
;
906 if(!enumeration
[i
].enumName
)
907 SetALCError(ALC_INVALID_VALUE
);
916 Create and attach a Context to a particular Device.
918 ALCAPI ALCcontext
* ALCAPIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
920 ALCcontext
*ALContext
= NULL
;
921 ALuint ulAttributeIndex
, ulRequestedStereoSources
;
923 if ((device
)&&(!device
->IsCaptureDevice
))
925 // Reset Context Last Error code
926 g_eLastContextError
= ALC_NO_ERROR
;
928 // Current implementation only allows one Context per Device
931 ALContext
= calloc(1, sizeof(ALCcontext
));
934 SetALCError(ALC_OUT_OF_MEMORY
);
938 ALContext
->Device
= device
;
939 InitContext(ALContext
);
941 device
->Context
= ALContext
;
943 SuspendContext(NULL
);
945 ALContext
->next
= g_pContextList
;
946 g_pContextList
= ALContext
;
949 ProcessContext(NULL
);
951 // Check for Voice Count attributes
954 ulAttributeIndex
= 0;
955 while ((ulAttributeIndex
< 10) && (attrList
[ulAttributeIndex
]))
957 if (attrList
[ulAttributeIndex
] == ALC_STEREO_SOURCES
)
959 ulRequestedStereoSources
= attrList
[ulAttributeIndex
+ 1];
961 if (ulRequestedStereoSources
> ALContext
->Device
->MaxNoOfSources
)
962 ulRequestedStereoSources
= ALContext
->Device
->MaxNoOfSources
;
964 ALContext
->lNumStereoSources
= ulRequestedStereoSources
;
965 ALContext
->lNumMonoSources
= ALContext
->Device
->MaxNoOfSources
- ALContext
->lNumStereoSources
;
969 ulAttributeIndex
+= 2;
975 SetALCError(ALC_INVALID_VALUE
);
980 SetALCError(ALC_INVALID_DEVICE
);
991 ALCAPI ALCvoid ALCAPIENTRY
alcDestroyContext(ALCcontext
*context
)
996 SuspendContext(NULL
);
998 if (IsContext(context
))
1001 SuspendContext(context
);
1003 ReleaseALSources(context
);
1004 ReleaseALAuxiliaryEffectSlots(context
);
1006 context
->Device
->Context
= NULL
;
1008 list
= &g_pContextList
;
1009 while(*list
!= context
)
1010 list
= &(*list
)->next
;
1012 *list
= (*list
)->next
;
1016 ProcessContext(context
);
1018 ExitContext(context
);
1020 // Free memory (MUST do this after ProcessContext)
1021 memset(context
, 0, sizeof(ALCcontext
));
1025 SetALCError(ALC_INVALID_CONTEXT
);
1027 ProcessContext(NULL
);
1032 alcGetCurrentContext
1034 Returns the currently active Context
1036 ALCAPI ALCcontext
* ALCAPIENTRY
alcGetCurrentContext(ALCvoid
)
1038 ALCcontext
*pContext
= NULL
;
1040 SuspendContext(NULL
);
1042 pContext
= g_pContextList
;
1043 while ((pContext
) && (!pContext
->InUse
))
1044 pContext
= pContext
->next
;
1046 ProcessContext(NULL
);
1053 alcGetContextsDevice
1055 Returns the Device that a particular Context is attached to
1057 ALCAPI ALCdevice
* ALCAPIENTRY
alcGetContextsDevice(ALCcontext
*pContext
)
1059 ALCdevice
*pDevice
= NULL
;
1061 SuspendContext(NULL
);
1062 if (IsContext(pContext
))
1063 pDevice
= pContext
->Device
;
1065 SetALCError(ALC_INVALID_CONTEXT
);
1066 ProcessContext(NULL
);
1073 alcMakeContextCurrent
1075 Makes the given Context the active Context
1077 ALCAPI ALCboolean ALCAPIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
1079 ALCcontext
*ALContext
;
1080 ALboolean bReturn
= AL_TRUE
;
1082 SuspendContext(NULL
);
1084 // context must be a valid Context or NULL
1085 if ((IsContext(context
)) || (context
== NULL
))
1087 if ((ALContext
=alcGetCurrentContext()))
1089 SuspendContext(ALContext
);
1090 ALContext
->InUse
=AL_FALSE
;
1091 ProcessContext(ALContext
);
1094 if ((ALContext
=context
) && (ALContext
->Device
))
1096 SuspendContext(ALContext
);
1097 ALContext
->InUse
=AL_TRUE
;
1098 ProcessContext(ALContext
);
1103 SetALCError(ALC_INVALID_CONTEXT
);
1107 ProcessContext(NULL
);
1116 Open the Device specified.
1118 ALCAPI ALCdevice
* ALCAPIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
1120 ALboolean bDeviceFound
= AL_FALSE
;
1126 if(deviceName
&& !deviceName
[0])
1129 device
= malloc(sizeof(ALCdevice
));
1134 //Initialise device structure
1135 memset(device
, 0, sizeof(ALCdevice
));
1138 device
->InUse
= AL_TRUE
;
1139 device
->IsCaptureDevice
= AL_FALSE
;
1142 device
->Frequency
= GetConfigValueInt(NULL
, "frequency", SWMIXER_OUTPUT_RATE
);
1143 if((ALint
)device
->Frequency
<= 0)
1144 device
->Frequency
= SWMIXER_OUTPUT_RATE
;
1146 fmt
= GetConfigValue(NULL
, "format", "AL_FORMAT_STEREO16");
1148 device
->Format
= alGetEnumValue(fmt
);
1150 if(!aluChannelsFromFormat(device
->Format
))
1152 device
->Format
= AL_FORMAT_STEREO16
;
1153 device
->FrameSize
= 4;
1156 device
->FrameSize
= aluBytesFromFormat(device
->Format
) *
1157 aluChannelsFromFormat(device
->Format
);
1159 device
->UpdateFreq
= GetConfigValueInt(NULL
, "refresh", 8192);
1160 if((ALint
)device
->UpdateFreq
<= 0)
1161 device
->UpdateFreq
= 8192;
1163 device
->MaxNoOfSources
= GetConfigValueInt(NULL
, "sources", 256);
1164 if((ALint
)device
->MaxNoOfSources
<= 0)
1165 device
->MaxNoOfSources
= 256;
1167 // Find a playback device to open
1168 for(i
= 0;BackendList
[i
].Init
;i
++)
1170 device
->Funcs
= &BackendList
[i
].Funcs
;
1171 if(ALCdevice_OpenPlayback(device
, deviceName
))
1173 SuspendContext(NULL
);
1174 device
->next
= g_pDeviceList
;
1175 g_pDeviceList
= device
;
1177 ProcessContext(NULL
);
1179 bDeviceFound
= AL_TRUE
;
1186 // No suitable output device found
1199 Close the specified Device
1201 ALCAPI ALCboolean ALCAPIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
1203 ALCboolean bReturn
= ALC_FALSE
;
1206 if ((pDevice
)&&(!pDevice
->IsCaptureDevice
))
1208 SuspendContext(NULL
);
1210 list
= &g_pDeviceList
;
1211 while(*list
!= pDevice
)
1212 list
= &(*list
)->next
;
1214 *list
= (*list
)->next
;
1217 ProcessContext(NULL
);
1219 if(pDevice
->Context
)
1220 alcDestroyContext(pDevice
->Context
);
1221 ALCdevice_ClosePlayback(pDevice
);
1223 //Release device structure
1224 memset(pDevice
, 0, sizeof(ALCdevice
));
1230 SetALCError(ALC_INVALID_DEVICE
);
1236 ALCvoid
ReleaseALC(ALCvoid
)
1239 if(g_ulContextCount
> 0)
1240 AL_PRINT("exit() %u device(s) and %u context(s) NOT deleted\n", g_ulDeviceCount
, g_ulContextCount
);
1243 while(g_pDeviceList
)
1245 if(g_pDeviceList
->IsCaptureDevice
)
1246 alcCaptureCloseDevice(g_pDeviceList
);
1248 alcCloseDevice(g_pDeviceList
);
1252 ///////////////////////////////////////////////////////