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
37 #include "alExtension.h"
38 #include "alAuxEffectSlot.h"
42 ///////////////////////////////////////////////////////
47 ///////////////////////////////////////////////////////
50 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
53 void (*Init
)(BackendFuncs
*);
57 { "alsa", alc_alsa_init
, EmptyFuncs
},
60 { "oss", alc_oss_init
, EmptyFuncs
},
63 { "solaris", alc_solaris_init
, EmptyFuncs
},
66 { "dsound", alcDSoundInit
, EmptyFuncs
},
69 { "winmm", alcWinMMInit
, EmptyFuncs
},
72 { "port", alc_pa_init
, EmptyFuncs
},
74 #ifdef HAVE_PULSEAUDIO
75 { "pulse", alc_pulse_init
, EmptyFuncs
},
78 { "wave", alc_wave_init
, EmptyFuncs
},
80 { NULL
, NULL
, EmptyFuncs
}
84 ///////////////////////////////////////////////////////
86 #define ALC_EFX_MAJOR_VERSION 0x20001
87 #define ALC_EFX_MINOR_VERSION 0x20002
88 #define ALC_MAX_AUXILIARY_SENDS 0x20003
90 ///////////////////////////////////////////////////////
91 // STRING and EXTENSIONS
93 typedef struct ALCfunction_struct
99 static ALCfunction alcFunctions
[] = {
100 { "alcCreateContext", (ALvoid
*) alcCreateContext
},
101 { "alcMakeContextCurrent", (ALvoid
*) alcMakeContextCurrent
},
102 { "alcProcessContext", (ALvoid
*) alcProcessContext
},
103 { "alcSuspendContext", (ALvoid
*) alcSuspendContext
},
104 { "alcDestroyContext", (ALvoid
*) alcDestroyContext
},
105 { "alcGetCurrentContext", (ALvoid
*) alcGetCurrentContext
},
106 { "alcGetContextsDevice", (ALvoid
*) alcGetContextsDevice
},
107 { "alcOpenDevice", (ALvoid
*) alcOpenDevice
},
108 { "alcCloseDevice", (ALvoid
*) alcCloseDevice
},
109 { "alcGetError", (ALvoid
*) alcGetError
},
110 { "alcIsExtensionPresent", (ALvoid
*) alcIsExtensionPresent
},
111 { "alcGetProcAddress", (ALvoid
*) alcGetProcAddress
},
112 { "alcGetEnumValue", (ALvoid
*) alcGetEnumValue
},
113 { "alcGetString", (ALvoid
*) alcGetString
},
114 { "alcGetIntegerv", (ALvoid
*) alcGetIntegerv
},
115 { "alcCaptureOpenDevice", (ALvoid
*) alcCaptureOpenDevice
},
116 { "alcCaptureCloseDevice", (ALvoid
*) alcCaptureCloseDevice
},
117 { "alcCaptureStart", (ALvoid
*) alcCaptureStart
},
118 { "alcCaptureStop", (ALvoid
*) alcCaptureStop
},
119 { "alcCaptureSamples", (ALvoid
*) alcCaptureSamples
},
120 { NULL
, (ALvoid
*) NULL
}
123 static ALenums enumeration
[]={
125 { (ALchar
*)"ALC_INVALID", ALC_INVALID
},
126 { (ALchar
*)"ALC_FALSE", ALC_FALSE
},
127 { (ALchar
*)"ALC_TRUE", ALC_TRUE
},
130 { (ALchar
*)"ALC_MAJOR_VERSION", ALC_MAJOR_VERSION
},
131 { (ALchar
*)"ALC_MINOR_VERSION", ALC_MINOR_VERSION
},
132 { (ALchar
*)"ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE
},
133 { (ALchar
*)"ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES
},
134 { (ALchar
*)"ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER
},
135 { (ALchar
*)"ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER
},
136 { (ALchar
*)"ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER
},
137 { (ALchar
*)"ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER
},
138 { (ALchar
*)"ALC_EXTENSIONS", ALC_EXTENSIONS
},
139 { (ALchar
*)"ALC_FREQUENCY", ALC_FREQUENCY
},
140 { (ALchar
*)"ALC_REFRESH", ALC_REFRESH
},
141 { (ALchar
*)"ALC_SYNC", ALC_SYNC
},
142 { (ALchar
*)"ALC_MONO_SOURCES", ALC_MONO_SOURCES
},
143 { (ALchar
*)"ALC_STEREO_SOURCES", ALC_STEREO_SOURCES
},
144 { (ALchar
*)"ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER
},
145 { (ALchar
*)"ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
},
146 { (ALchar
*)"ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES
},
149 { (ALchar
*)"ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION
},
150 { (ALchar
*)"ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION
},
151 { (ALchar
*)"ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS
},
154 { (ALchar
*)"ALC_NO_ERROR", ALC_NO_ERROR
},
155 { (ALchar
*)"ALC_INVALID_DEVICE", ALC_INVALID_DEVICE
},
156 { (ALchar
*)"ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT
},
157 { (ALchar
*)"ALC_INVALID_ENUM", ALC_INVALID_ENUM
},
158 { (ALchar
*)"ALC_INVALID_VALUE", ALC_INVALID_VALUE
},
159 { (ALchar
*)"ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY
},
160 { (ALchar
*)NULL
, (ALenum
)0 }
163 static const ALCchar alcNoError
[] = "No Error";
164 static const ALCchar alcErrInvalidDevice
[] = "Invalid Device";
165 static const ALCchar alcErrInvalidContext
[] = "Invalid Context";
166 static const ALCchar alcErrInvalidEnum
[] = "Invalid Enum";
167 static const ALCchar alcErrInvalidValue
[] = "Invalid Value";
168 static const ALCchar alcErrOutOfMemory
[] = "Out of Memory";
171 static ALCchar alcDeviceList
[2048];
172 static ALCchar alcAllDeviceList
[2048];
173 static ALCchar alcCaptureDeviceList
[2048];
174 // Default is always the first in the list
175 static ALCchar
*alcDefaultDeviceSpecifier
= alcDeviceList
;
176 static ALCchar
*alcDefaultAllDeviceSpecifier
= alcAllDeviceList
;
177 static ALCchar
*alcCaptureDefaultDeviceSpecifier
= alcCaptureDeviceList
;
180 static ALCchar alcExtensionList
[] = "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_EFX";
181 static ALCint alcMajorVersion
= 1;
182 static ALCint alcMinorVersion
= 1;
184 static ALCint alcEFXMajorVersion
= 1;
185 static ALCint alcEFXMinorVersion
= 0;
187 ///////////////////////////////////////////////////////
190 ///////////////////////////////////////////////////////
193 static ALCdevice
*g_pDeviceList
= NULL
;
194 static ALCuint g_ulDeviceCount
= 0;
196 static CRITICAL_SECTION g_csMutex
;
199 static ALCcontext
*g_pContextList
= NULL
;
200 static ALCuint g_ulContextCount
= 0;
203 static ALCenum g_eLastContextError
= ALC_NO_ERROR
;
205 static ALboolean init_done
= AL_FALSE
;
207 ///////////////////////////////////////////////////////
210 ///////////////////////////////////////////////////////
211 // ALC Related helper functions
213 BOOL APIENTRY
DllMain(HANDLE hModule
,DWORD ul_reason_for_call
,LPVOID lpReserved
)
217 // Perform actions based on the reason for calling.
218 switch(ul_reason_for_call
)
220 case DLL_PROCESS_ATTACH
:
221 DisableThreadLibraryCalls(hModule
);
224 case DLL_PROCESS_DETACH
:
233 DeleteCriticalSection(&g_csMutex
);
239 #ifdef HAVE_GCC_DESTRUCTOR
240 static void my_deinit() __attribute__((destructor
));
241 static void my_deinit()
243 static ALenum once
= AL_FALSE
;
244 if(once
|| !init_done
) return;
253 DeleteCriticalSection(&g_csMutex
);
258 static void InitAL(void)
263 const char *devs
, *str
;
267 InitializeCriticalSection(&g_csMutex
);
271 devs
= GetConfigValue(NULL
, "drivers", "");
276 const char *next
= devs
;
282 next
= strchr(devs
, ',');
284 if(!devs
[0] || devs
[0] == ',')
287 len
= (next
? ((size_t)(next
-devs
)) : strlen(devs
));
288 for(n
= i
;BackendList
[n
].Init
;n
++)
290 if(len
== strlen(BackendList
[n
].name
) &&
291 strncmp(BackendList
[n
].name
, devs
, len
) == 0)
293 const char *name
= BackendList
[i
].name
;
294 void (*Init
)(BackendFuncs
*) = BackendList
[i
].Init
;
296 BackendList
[i
].name
= BackendList
[n
].name
;
297 BackendList
[i
].Init
= BackendList
[n
].Init
;
299 BackendList
[n
].name
= name
;
300 BackendList
[n
].Init
= Init
;
307 BackendList
[i
].name
= NULL
;
308 BackendList
[i
].Init
= NULL
;
311 for(i
= 0;BackendList
[i
].Init
;i
++)
312 BackendList
[i
].Init(&BackendList
[i
].Funcs
);
314 str
= GetConfigValue(NULL
, "stereodup", "false");
315 DuplicateStereo
= (strcasecmp(str
, "true") == 0 ||
316 strcasecmp(str
, "yes") == 0 ||
317 strcasecmp(str
, "on") == 0 ||
320 str
= GetConfigValue(NULL
, "excludefx", "");
327 { "reverb", REVERB
},
333 const char *next
= str
;
337 next
= strchr(str
, ',');
339 if(!str
[0] || next
== str
)
342 len
= (next
? ((size_t)(next
-str
)) : strlen(str
));
343 for(n
= 0;EffectList
[n
].name
;n
++)
345 if(len
== strlen(EffectList
[n
].name
) &&
346 strncmp(EffectList
[n
].name
, str
, len
) == 0)
347 DisabledEffects
[EffectList
[n
].type
] = AL_TRUE
;
354 ALCchar
*AppendDeviceList(char *name
)
357 ALCchar
*ret
= alcDeviceList
+pos
;
358 if(pos
>= sizeof(alcDeviceList
))
360 AL_PRINT("Not enough room to add %s!\n", name
);
361 return alcDeviceList
+ sizeof(alcDeviceList
) - 1;
363 pos
+= snprintf(alcDeviceList
+pos
, sizeof(alcDeviceList
)-pos
-1, "%s", name
) + 1;
367 ALCchar
*AppendAllDeviceList(char *name
)
370 ALCchar
*ret
= alcAllDeviceList
+pos
;
371 if(pos
>= sizeof(alcAllDeviceList
))
373 AL_PRINT("Not enough room to add %s!\n", name
);
374 return alcAllDeviceList
+ sizeof(alcAllDeviceList
) - 1;
376 pos
+= snprintf(alcAllDeviceList
+pos
, sizeof(alcAllDeviceList
)-pos
-1, "%s", name
) + 1;
380 ALCchar
*AppendCaptureDeviceList(char *name
)
383 ALCchar
*ret
= alcCaptureDeviceList
+pos
;
384 if(pos
>= sizeof(alcCaptureDeviceList
))
386 AL_PRINT("Not enough room to add %s!\n", name
);
387 return alcCaptureDeviceList
+ sizeof(alcCaptureDeviceList
) - 1;
389 pos
+= snprintf(alcCaptureDeviceList
+pos
, sizeof(alcCaptureDeviceList
)-pos
-1, "%s", name
) + 1;
396 Check pContext is a valid Context pointer
398 static ALCboolean
IsContext(ALCcontext
*pContext
)
400 ALCcontext
*pTempContext
;
402 pTempContext
= g_pContextList
;
403 while (pTempContext
&& pTempContext
!= pContext
)
404 pTempContext
= pTempContext
->next
;
406 return (pTempContext
? ALC_TRUE
: ALC_FALSE
);
413 Store latest ALC Error
415 ALCvoid
SetALCError(ALenum errorCode
)
417 g_eLastContextError
= errorCode
;
426 ALCvoid
SuspendContext(ALCcontext
*pContext
)
429 EnterCriticalSection(&g_csMutex
);
438 ALCvoid
ProcessContext(ALCcontext
*pContext
)
441 LeaveCriticalSection(&g_csMutex
);
448 Initialize Context variables
450 static ALvoid
InitContext(ALCcontext
*pContext
)
454 //Initialise listener
455 pContext
->Listener
.Gain
= 1.0f
;
456 pContext
->Listener
.MetersPerUnit
= 1.0f
;
457 pContext
->Listener
.Position
[0] = 0.0f
;
458 pContext
->Listener
.Position
[1] = 0.0f
;
459 pContext
->Listener
.Position
[2] = 0.0f
;
460 pContext
->Listener
.Velocity
[0] = 0.0f
;
461 pContext
->Listener
.Velocity
[1] = 0.0f
;
462 pContext
->Listener
.Velocity
[2] = 0.0f
;
463 pContext
->Listener
.Forward
[0] = 0.0f
;
464 pContext
->Listener
.Forward
[1] = 0.0f
;
465 pContext
->Listener
.Forward
[2] = -1.0f
;
466 pContext
->Listener
.Up
[0] = 0.0f
;
467 pContext
->Listener
.Up
[1] = 1.0f
;
468 pContext
->Listener
.Up
[2] = 0.0f
;
471 pContext
->LastError
= AL_NO_ERROR
;
472 pContext
->InUse
= AL_FALSE
;
475 pContext
->Frequency
= pContext
->Device
->Frequency
;
478 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
479 pContext
->DopplerFactor
= 1.0f
;
480 pContext
->DopplerVelocity
= 1.0f
;
481 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
483 pContext
->lNumStereoSources
= 1;
484 pContext
->lNumMonoSources
= pContext
->Device
->MaxNoOfSources
- pContext
->lNumStereoSources
;
486 pContext
->AuxiliaryEffectSlotMax
= GetConfigValueInt(NULL
, "slots", 4);
487 pContext
->NumSends
= GetConfigValueInt(NULL
, "sends", MAX_SENDS
);
488 if(pContext
->NumSends
> MAX_SENDS
)
489 pContext
->NumSends
= MAX_SENDS
;
491 pContext
->ExtensionList
= "AL_EXTX_buffer_sub_data AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_OFFSET AL_EXTX_source_distance_model AL_LOKI_quadriphonic";
493 level
= GetConfigValueInt(NULL
, "cf_level", 0);
494 if(level
> 0 && level
<= 6)
496 pContext
->bs2b
= calloc(1, sizeof(*pContext
->bs2b
));
497 bs2b_set_srate(pContext
->bs2b
, pContext
->Frequency
);
498 bs2b_set_level(pContext
->bs2b
, level
);
501 aluInitPanning(pContext
);
508 Clean up Context, destroy any remaining Sources
510 static ALCvoid
ExitContext(ALCcontext
*pContext
)
513 pContext
->LastError
= AL_NO_ERROR
;
514 pContext
->InUse
= AL_FALSE
;
516 free(pContext
->bs2b
);
517 pContext
->bs2b
= NULL
;
520 ///////////////////////////////////////////////////////
523 ///////////////////////////////////////////////////////
524 // ALC Functions calls
527 // This should probably move to another c file but for now ...
528 ALCAPI ALCdevice
* ALCAPIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
530 ALCboolean DeviceFound
= ALC_FALSE
;
531 ALCdevice
*pDevice
= NULL
;
538 SetALCError(ALC_INVALID_VALUE
);
542 if(deviceName
&& !deviceName
[0])
545 pDevice
= malloc(sizeof(ALCdevice
));
548 //Initialise device structure
549 memset(pDevice
, 0, sizeof(ALCdevice
));
552 pDevice
->IsCaptureDevice
= AL_TRUE
;
554 pDevice
->Frequency
= frequency
;
555 pDevice
->Format
= format
;
557 for(i
= 0;BackendList
[i
].Init
;i
++)
559 pDevice
->Funcs
= &BackendList
[i
].Funcs
;
560 if(ALCdevice_OpenCapture(pDevice
, deviceName
, frequency
, format
, SampleSize
))
562 SuspendContext(NULL
);
563 pDevice
->next
= g_pDeviceList
;
564 g_pDeviceList
= pDevice
;
566 ProcessContext(NULL
);
568 DeviceFound
= ALC_TRUE
;
575 SetALCError(ALC_INVALID_VALUE
);
581 SetALCError(ALC_OUT_OF_MEMORY
);
586 ALCAPI ALCboolean ALCAPIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
588 ALCboolean bReturn
= ALC_FALSE
;
591 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
593 SuspendContext(NULL
);
595 list
= &g_pDeviceList
;
596 while(*list
!= pDevice
)
597 list
= &(*list
)->next
;
599 *list
= (*list
)->next
;
602 ProcessContext(NULL
);
604 ALCdevice_CloseCapture(pDevice
);
610 SetALCError(ALC_INVALID_DEVICE
);
615 ALCAPI
void ALCAPIENTRY
alcCaptureStart(ALCdevice
*pDevice
)
617 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
618 ALCdevice_StartCapture(pDevice
);
620 SetALCError(ALC_INVALID_DEVICE
);
623 ALCAPI
void ALCAPIENTRY
alcCaptureStop(ALCdevice
*pDevice
)
625 if ((pDevice
)&&(pDevice
->IsCaptureDevice
))
626 ALCdevice_StopCapture(pDevice
);
628 SetALCError(ALC_INVALID_DEVICE
);
631 ALCAPI
void ALCAPIENTRY
alcCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCsizei lSamples
)
633 if ((pDevice
) && (pDevice
->IsCaptureDevice
))
634 ALCdevice_CaptureSamples(pDevice
, pBuffer
, lSamples
);
636 SetALCError(ALC_INVALID_DEVICE
);
642 Return last ALC generated error code
644 ALCAPI ALCenum ALCAPIENTRY
alcGetError(ALCdevice
*device
)
650 errorCode
= g_eLastContextError
;
651 g_eLastContextError
= ALC_NO_ERROR
;
661 ALCAPI ALCvoid ALCAPIENTRY
alcSuspendContext(ALCcontext
*pContext
)
663 // Not a lot happens here !
673 ALCAPI ALCvoid ALCAPIENTRY
alcProcessContext(ALCcontext
*pContext
)
675 // Not a lot happens here !
683 Returns information about the Device, and error strings
685 ALCAPI
const ALCchar
* ALCAPIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
687 const ALCchar
*value
= NULL
;
697 case ALC_INVALID_ENUM
:
698 value
= alcErrInvalidEnum
;
701 case ALC_INVALID_VALUE
:
702 value
= alcErrInvalidValue
;
705 case ALC_INVALID_DEVICE
:
706 value
= alcErrInvalidDevice
;
709 case ALC_INVALID_CONTEXT
:
710 value
= alcErrInvalidContext
;
713 case ALC_OUT_OF_MEMORY
:
714 value
= alcErrOutOfMemory
;
717 case ALC_DEFAULT_DEVICE_SPECIFIER
:
718 value
= alcDefaultDeviceSpecifier
;
721 case ALC_DEVICE_SPECIFIER
:
723 value
= pDevice
->szDeviceName
;
725 value
= alcDeviceList
;
728 case ALC_ALL_DEVICES_SPECIFIER
:
729 value
= alcAllDeviceList
;
732 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
733 value
= alcDefaultAllDeviceSpecifier
;
736 case ALC_CAPTURE_DEVICE_SPECIFIER
:
738 value
= pDevice
->szDeviceName
;
740 value
= alcCaptureDeviceList
;
743 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
744 value
= alcCaptureDefaultDeviceSpecifier
;
748 value
= alcExtensionList
;
752 SetALCError(ALC_INVALID_ENUM
);
763 Returns information about the Device and the version of Open AL
765 ALCAPI ALCvoid ALCAPIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
769 if ((device
)&&(device
->IsCaptureDevice
))
771 SuspendContext(NULL
);
776 case ALC_CAPTURE_SAMPLES
:
777 if ((size
) && (data
))
778 *data
= ALCdevice_AvailableSamples(device
);
780 SetALCError(ALC_INVALID_VALUE
);
784 SetALCError(ALC_INVALID_ENUM
);
788 ProcessContext(NULL
);
797 case ALC_MAJOR_VERSION
:
799 SetALCError(ALC_INVALID_VALUE
);
801 *data
= alcMajorVersion
;
804 case ALC_MINOR_VERSION
:
806 SetALCError(ALC_INVALID_VALUE
);
808 *data
= alcMinorVersion
;
811 case ALC_EFX_MAJOR_VERSION
:
813 SetALCError(ALC_INVALID_VALUE
);
815 *data
= alcEFXMajorVersion
;
818 case ALC_EFX_MINOR_VERSION
:
820 SetALCError(ALC_INVALID_VALUE
);
822 *data
= alcEFXMinorVersion
;
825 case ALC_MAX_AUXILIARY_SENDS
:
827 SetALCError(ALC_INVALID_VALUE
);
828 else if(device
&& device
->Context
)
829 *data
= device
->Context
->NumSends
;
834 case ALC_ATTRIBUTES_SIZE
:
836 SetALCError(ALC_INVALID_DEVICE
);
838 SetALCError(ALC_INVALID_VALUE
);
843 case ALC_ALL_ATTRIBUTES
:
845 SetALCError(ALC_INVALID_DEVICE
);
847 SetALCError(ALC_INVALID_VALUE
);
852 data
[i
++] = ALC_FREQUENCY
;
853 data
[i
++] = device
->Frequency
;
855 data
[i
++] = ALC_REFRESH
;
856 data
[i
++] = device
->Frequency
/ device
->UpdateSize
;
858 data
[i
++] = ALC_SYNC
;
859 data
[i
++] = ALC_FALSE
;
861 SuspendContext(NULL
);
862 if(device
->Context
&& size
>= 12)
864 data
[i
++] = ALC_MONO_SOURCES
;
865 data
[i
++] = device
->Context
->lNumMonoSources
;
867 data
[i
++] = ALC_STEREO_SOURCES
;
868 data
[i
++] = device
->Context
->lNumStereoSources
;
870 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
871 data
[i
++] = device
->Context
->NumSends
;
873 ProcessContext(NULL
);
881 SetALCError(ALC_INVALID_DEVICE
);
883 SetALCError(ALC_INVALID_VALUE
);
885 *data
= device
->Frequency
;
890 SetALCError(ALC_INVALID_DEVICE
);
892 SetALCError(ALC_INVALID_VALUE
);
894 *data
= device
->Frequency
/ device
->UpdateSize
;
899 SetALCError(ALC_INVALID_DEVICE
);
901 SetALCError(ALC_INVALID_VALUE
);
906 case ALC_MONO_SOURCES
:
907 if(!device
|| !device
->Context
)
908 SetALCError(ALC_INVALID_DEVICE
);
910 SetALCError(ALC_INVALID_VALUE
);
912 *data
= device
->Context
->lNumMonoSources
;
915 case ALC_STEREO_SOURCES
:
916 if(!device
|| !device
->Context
)
917 SetALCError(ALC_INVALID_DEVICE
);
919 SetALCError(ALC_INVALID_VALUE
);
921 *data
= device
->Context
->lNumStereoSources
;
925 SetALCError(ALC_INVALID_ENUM
);
930 SetALCError(ALC_INVALID_VALUE
);
938 alcIsExtensionPresent
940 Determines if there is support for a particular extension
942 ALCAPI ALCboolean ALCAPIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
944 ALCboolean bResult
= ALC_FALSE
;
953 len
= strlen(extName
);
954 ptr
= alcExtensionList
;
957 if(strncasecmp(ptr
, extName
, len
) == 0 &&
958 (ptr
[len
] == '\0' || isspace(ptr
[len
])))
963 if((ptr
=strchr(ptr
, ' ')) != NULL
)
967 } while(isspace(*ptr
));
972 SetALCError(ALC_INVALID_VALUE
);
981 Retrieves the function address for a particular extension function
983 ALCAPI ALCvoid
* ALCAPIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
985 ALCvoid
*pFunction
= NULL
;
992 while(alcFunctions
[i
].funcName
&&
993 strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
995 pFunction
= alcFunctions
[i
].address
;
998 SetALCError(ALC_INVALID_VALUE
);
1007 Get the value for a particular ALC Enumerated Value
1009 ALCAPI ALCenum ALCAPIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
1016 while ((enumeration
[i
].enumName
)&&(strcmp(enumeration
[i
].enumName
,enumName
)))
1018 val
= enumeration
[i
].value
;
1020 if(!enumeration
[i
].enumName
)
1021 SetALCError(ALC_INVALID_VALUE
);
1030 Create and attach a Context to a particular Device.
1032 ALCAPI ALCcontext
* ALCAPIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
1034 ALCcontext
*ALContext
= NULL
;
1035 ALuint ulAttributeIndex
, ulRequestedStereoSources
;
1036 ALuint RequestedSends
;
1038 if ((device
)&&(!device
->IsCaptureDevice
))
1040 // Reset Context Last Error code
1041 g_eLastContextError
= ALC_NO_ERROR
;
1043 // Current implementation only allows one Context per Device
1044 if(!device
->Context
)
1046 ALContext
= calloc(1, sizeof(ALCcontext
));
1049 SetALCError(ALC_OUT_OF_MEMORY
);
1053 ALContext
->Device
= device
;
1054 InitContext(ALContext
);
1056 device
->Context
= ALContext
;
1058 SuspendContext(NULL
);
1060 ALContext
->next
= g_pContextList
;
1061 g_pContextList
= ALContext
;
1064 ProcessContext(NULL
);
1066 // Check for attributes
1069 ulAttributeIndex
= 0;
1070 while ((ulAttributeIndex
< 10) && (attrList
[ulAttributeIndex
]))
1072 if (attrList
[ulAttributeIndex
] == ALC_STEREO_SOURCES
)
1074 ulRequestedStereoSources
= attrList
[ulAttributeIndex
+ 1];
1076 if (ulRequestedStereoSources
> ALContext
->Device
->MaxNoOfSources
)
1077 ulRequestedStereoSources
= ALContext
->Device
->MaxNoOfSources
;
1079 ALContext
->lNumStereoSources
= ulRequestedStereoSources
;
1080 ALContext
->lNumMonoSources
= ALContext
->Device
->MaxNoOfSources
- ALContext
->lNumStereoSources
;
1083 if(attrList
[ulAttributeIndex
] == ALC_MAX_AUXILIARY_SENDS
)
1085 RequestedSends
= attrList
[ulAttributeIndex
+ 1];
1087 if(RequestedSends
> ALContext
->NumSends
)
1088 RequestedSends
= ALContext
->NumSends
;
1090 ALContext
->NumSends
= RequestedSends
;
1093 ulAttributeIndex
+= 2;
1099 SetALCError(ALC_INVALID_VALUE
);
1104 SetALCError(ALC_INVALID_DEVICE
);
1115 ALCAPI ALCvoid ALCAPIENTRY
alcDestroyContext(ALCcontext
*context
)
1121 // Lock context list
1122 SuspendContext(NULL
);
1124 if (IsContext(context
))
1127 SuspendContext(context
);
1129 ReleaseALSources(context
);
1130 ReleaseALAuxiliaryEffectSlots(context
);
1132 context
->Device
->Context
= NULL
;
1134 list
= &g_pContextList
;
1135 while(*list
!= context
)
1136 list
= &(*list
)->next
;
1138 *list
= (*list
)->next
;
1142 ProcessContext(context
);
1144 ExitContext(context
);
1146 // Free memory (MUST do this after ProcessContext)
1147 memset(context
, 0, sizeof(ALCcontext
));
1151 SetALCError(ALC_INVALID_CONTEXT
);
1153 ProcessContext(NULL
);
1158 alcGetCurrentContext
1160 Returns the currently active Context
1162 ALCAPI ALCcontext
* ALCAPIENTRY
alcGetCurrentContext(ALCvoid
)
1164 ALCcontext
*pContext
= NULL
;
1168 SuspendContext(NULL
);
1170 pContext
= g_pContextList
;
1171 while ((pContext
) && (!pContext
->InUse
))
1172 pContext
= pContext
->next
;
1174 ProcessContext(NULL
);
1181 alcGetContextsDevice
1183 Returns the Device that a particular Context is attached to
1185 ALCAPI ALCdevice
* ALCAPIENTRY
alcGetContextsDevice(ALCcontext
*pContext
)
1187 ALCdevice
*pDevice
= NULL
;
1191 SuspendContext(NULL
);
1192 if (IsContext(pContext
))
1193 pDevice
= pContext
->Device
;
1195 SetALCError(ALC_INVALID_CONTEXT
);
1196 ProcessContext(NULL
);
1203 alcMakeContextCurrent
1205 Makes the given Context the active Context
1207 ALCAPI ALCboolean ALCAPIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
1209 ALCcontext
*ALContext
;
1210 ALboolean bReturn
= AL_TRUE
;
1214 SuspendContext(NULL
);
1216 // context must be a valid Context or NULL
1217 if ((IsContext(context
)) || (context
== NULL
))
1219 if ((ALContext
=alcGetCurrentContext()))
1221 SuspendContext(ALContext
);
1222 ALContext
->InUse
=AL_FALSE
;
1223 ProcessContext(ALContext
);
1226 if ((ALContext
=context
) && (ALContext
->Device
))
1228 SuspendContext(ALContext
);
1229 ALContext
->InUse
=AL_TRUE
;
1230 ProcessContext(ALContext
);
1235 SetALCError(ALC_INVALID_CONTEXT
);
1239 ProcessContext(NULL
);
1248 Open the Device specified.
1250 ALCAPI ALCdevice
* ALCAPIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
1252 ALboolean bDeviceFound
= AL_FALSE
;
1258 if(deviceName
&& !deviceName
[0])
1261 device
= malloc(sizeof(ALCdevice
));
1266 //Initialise device structure
1267 memset(device
, 0, sizeof(ALCdevice
));
1270 device
->IsCaptureDevice
= AL_FALSE
;
1273 device
->Frequency
= GetConfigValueInt(NULL
, "frequency", SWMIXER_OUTPUT_RATE
);
1274 if((ALint
)device
->Frequency
<= 0)
1275 device
->Frequency
= SWMIXER_OUTPUT_RATE
;
1277 fmt
= GetConfigValue(NULL
, "format", "AL_FORMAT_STEREO16");
1279 device
->Format
= alGetEnumValue(fmt
);
1281 if(!aluChannelsFromFormat(device
->Format
))
1282 device
->Format
= AL_FORMAT_STEREO16
;
1284 device
->UpdateSize
= GetConfigValueInt(NULL
, "refresh", 4096);
1285 if((ALint
)device
->UpdateSize
<= 0)
1286 device
->UpdateSize
= 4096;
1288 device
->MaxNoOfSources
= GetConfigValueInt(NULL
, "sources", 256);
1289 if((ALint
)device
->MaxNoOfSources
<= 0)
1290 device
->MaxNoOfSources
= 256;
1292 // Find a playback device to open
1293 SuspendContext(NULL
);
1294 for(i
= 0;BackendList
[i
].Init
;i
++)
1296 device
->Funcs
= &BackendList
[i
].Funcs
;
1297 if(ALCdevice_OpenPlayback(device
, deviceName
))
1299 device
->next
= g_pDeviceList
;
1300 g_pDeviceList
= device
;
1303 bDeviceFound
= AL_TRUE
;
1307 ProcessContext(NULL
);
1311 // No suitable output device found
1312 SetALCError(ALC_INVALID_VALUE
);
1318 SetALCError(ALC_OUT_OF_MEMORY
);
1327 Close the specified Device
1329 ALCAPI ALCboolean ALCAPIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
1331 ALCboolean bReturn
= ALC_FALSE
;
1334 if ((pDevice
)&&(!pDevice
->IsCaptureDevice
))
1336 SuspendContext(NULL
);
1338 list
= &g_pDeviceList
;
1339 while(*list
!= pDevice
)
1340 list
= &(*list
)->next
;
1342 *list
= (*list
)->next
;
1345 ProcessContext(NULL
);
1347 if(pDevice
->Context
)
1350 AL_PRINT("alcCloseDevice(): destroying 1 Context\n");
1352 alcDestroyContext(pDevice
->Context
);
1354 ALCdevice_ClosePlayback(pDevice
);
1356 //Release device structure
1357 memset(pDevice
, 0, sizeof(ALCdevice
));
1363 SetALCError(ALC_INVALID_DEVICE
);
1369 ALCvoid
ReleaseALC(ALCvoid
)
1372 if(g_ulDeviceCount
> 0)
1373 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount
, (g_ulDeviceCount
>1)?"s":"");
1376 while(g_pDeviceList
)
1378 if(g_pDeviceList
->IsCaptureDevice
)
1379 alcCaptureCloseDevice(g_pDeviceList
);
1381 alcCloseDevice(g_pDeviceList
);
1385 ///////////////////////////////////////////////////////