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
35 #include "alExtension.h"
36 #include "alAuxEffectSlot.h"
37 #include "alDatabuffer.h"
42 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
45 void (*Init
)(BackendFuncs
*);
50 { "alsa", alc_alsa_init
, alc_alsa_deinit
, EmptyFuncs
},
53 { "oss", alc_oss_init
, alc_oss_deinit
, EmptyFuncs
},
56 { "solaris", alc_solaris_init
, alc_solaris_deinit
, EmptyFuncs
},
59 { "dsound", alcDSoundInit
, alcDSoundDeinit
, EmptyFuncs
},
62 { "winmm", alcWinMMInit
, alcWinMMDeinit
, EmptyFuncs
},
65 { "port", alc_pa_init
, alc_pa_deinit
, EmptyFuncs
},
67 #ifdef HAVE_PULSEAUDIO
68 { "pulse", alc_pulse_init
, alc_pulse_deinit
, EmptyFuncs
},
71 { "wave", alc_wave_init
, alc_wave_deinit
, EmptyFuncs
},
73 { NULL
, NULL
, NULL
, EmptyFuncs
}
77 ///////////////////////////////////////////////////////
79 #define ALC_EFX_MAJOR_VERSION 0x20001
80 #define ALC_EFX_MINOR_VERSION 0x20002
81 #define ALC_MAX_AUXILIARY_SENDS 0x20003
83 ///////////////////////////////////////////////////////
84 // STRING and EXTENSIONS
86 typedef struct ALCfunction_struct
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 ALC_EXT_disconnect ALC_EXT_EFX";
174 static ALCint alcMajorVersion
= 1;
175 static ALCint alcMinorVersion
= 1;
177 static ALCint alcEFXMajorVersion
= 1;
178 static ALCint alcEFXMinorVersion
= 0;
180 ///////////////////////////////////////////////////////
183 ///////////////////////////////////////////////////////
186 static ALCdevice
*g_pDeviceList
= NULL
;
187 static ALCuint g_ulDeviceCount
= 0;
189 static CRITICAL_SECTION g_csMutex
;
192 static ALCcontext
*g_pContextList
= NULL
;
193 static ALCuint g_ulContextCount
= 0;
196 static ALCenum g_eLastContextError
= ALC_NO_ERROR
;
198 static ALboolean init_done
= AL_FALSE
;
200 ///////////////////////////////////////////////////////
203 ///////////////////////////////////////////////////////
204 // ALC Related helper functions
206 BOOL APIENTRY
DllMain(HANDLE hModule
,DWORD ul_reason_for_call
,LPVOID lpReserved
)
212 // Perform actions based on the reason for calling.
213 switch(ul_reason_for_call
)
215 case DLL_PROCESS_ATTACH
:
216 DisableThreadLibraryCalls(hModule
);
219 case DLL_PROCESS_DETACH
:
224 for(i
= 0;BackendList
[i
].Deinit
;i
++)
225 BackendList
[i
].Deinit();
229 DeleteCriticalSection(&g_csMutex
);
235 #ifdef HAVE_GCC_DESTRUCTOR
236 static void my_deinit() __attribute__((destructor
));
237 static void my_deinit()
239 static ALenum once
= AL_FALSE
;
242 if(once
|| !init_done
) return;
247 for(i
= 0;BackendList
[i
].Deinit
;i
++)
248 BackendList
[i
].Deinit();
252 DeleteCriticalSection(&g_csMutex
);
257 static void InitAL(void)
262 const char *devs
, *str
;
266 InitializeCriticalSection(&g_csMutex
);
270 devs
= GetConfigValue(NULL
, "drivers", "");
275 const char *next
= devs
;
281 next
= strchr(devs
, ',');
283 if(!devs
[0] || devs
[0] == ',')
286 len
= (next
? ((size_t)(next
-devs
)) : strlen(devs
));
287 for(n
= i
;BackendList
[n
].Init
;n
++)
289 if(len
== strlen(BackendList
[n
].name
) &&
290 strncmp(BackendList
[n
].name
, devs
, len
) == 0)
292 const char *name
= BackendList
[i
].name
;
293 void (*Init
)(BackendFuncs
*) = BackendList
[i
].Init
;
295 BackendList
[i
].name
= BackendList
[n
].name
;
296 BackendList
[i
].Init
= BackendList
[n
].Init
;
298 BackendList
[n
].name
= name
;
299 BackendList
[n
].Init
= Init
;
306 BackendList
[i
].name
= NULL
;
307 BackendList
[i
].Init
= NULL
;
310 for(i
= 0;BackendList
[i
].Init
;i
++)
311 BackendList
[i
].Init(&BackendList
[i
].Funcs
);
313 str
= GetConfigValue(NULL
, "stereodup", "false");
314 DuplicateStereo
= (strcasecmp(str
, "true") == 0 ||
315 strcasecmp(str
, "yes") == 0 ||
316 strcasecmp(str
, "on") == 0 ||
319 str
= GetConfigValue(NULL
, "excludefx", "");
326 { "eaxreverb", EAXREVERB
},
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 pDevice is a valid Device pointer
398 static ALCboolean
IsDevice(ALCdevice
*pDevice
)
400 ALCdevice
*pTempDevice
;
402 SuspendContext(NULL
);
404 pTempDevice
= g_pDeviceList
;
405 while(pTempDevice
&& pTempDevice
!= pDevice
)
406 pTempDevice
= pTempDevice
->next
;
408 ProcessContext(NULL
);
410 return (pTempDevice
? ALC_TRUE
: ALC_FALSE
);
416 Check pContext is a valid Context pointer
418 static ALCboolean
IsContext(ALCcontext
*pContext
)
420 ALCcontext
*pTempContext
;
422 SuspendContext(NULL
);
424 pTempContext
= g_pContextList
;
425 while (pTempContext
&& pTempContext
!= pContext
)
426 pTempContext
= pTempContext
->next
;
428 ProcessContext(NULL
);
430 return (pTempContext
? ALC_TRUE
: ALC_FALSE
);
437 Store latest ALC Error
439 ALCvoid
SetALCError(ALenum errorCode
)
441 g_eLastContextError
= errorCode
;
450 ALCvoid
SuspendContext(ALCcontext
*pContext
)
453 EnterCriticalSection(&g_csMutex
);
462 ALCvoid
ProcessContext(ALCcontext
*pContext
)
465 LeaveCriticalSection(&g_csMutex
);
472 Returns the currently active Context, in a locked state
474 ALCcontext
*GetContextSuspended(void)
476 ALCcontext
*pContext
= NULL
;
478 SuspendContext(NULL
);
480 pContext
= g_pContextList
;
481 while(pContext
&& !pContext
->InUse
)
482 pContext
= pContext
->next
;
485 SuspendContext(pContext
);
487 ProcessContext(NULL
);
496 Initialize Context variables
498 static ALvoid
InitContext(ALCcontext
*pContext
)
502 //Initialise listener
503 pContext
->Listener
.Gain
= 1.0f
;
504 pContext
->Listener
.MetersPerUnit
= 1.0f
;
505 pContext
->Listener
.Position
[0] = 0.0f
;
506 pContext
->Listener
.Position
[1] = 0.0f
;
507 pContext
->Listener
.Position
[2] = 0.0f
;
508 pContext
->Listener
.Velocity
[0] = 0.0f
;
509 pContext
->Listener
.Velocity
[1] = 0.0f
;
510 pContext
->Listener
.Velocity
[2] = 0.0f
;
511 pContext
->Listener
.Forward
[0] = 0.0f
;
512 pContext
->Listener
.Forward
[1] = 0.0f
;
513 pContext
->Listener
.Forward
[2] = -1.0f
;
514 pContext
->Listener
.Up
[0] = 0.0f
;
515 pContext
->Listener
.Up
[1] = 1.0f
;
516 pContext
->Listener
.Up
[2] = 0.0f
;
519 pContext
->LastError
= AL_NO_ERROR
;
520 pContext
->InUse
= AL_FALSE
;
523 pContext
->Frequency
= pContext
->Device
->Frequency
;
526 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
527 pContext
->DopplerFactor
= 1.0f
;
528 pContext
->DopplerVelocity
= 1.0f
;
529 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
531 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_sample_buffer_object AL_EXTX_source_distance_model AL_LOKI_quadriphonic";
533 level
= GetConfigValueInt(NULL
, "cf_level", 0);
534 if(level
> 0 && level
<= 6)
536 pContext
->bs2b
= calloc(1, sizeof(*pContext
->bs2b
));
537 bs2b_set_srate(pContext
->bs2b
, pContext
->Frequency
);
538 bs2b_set_level(pContext
->bs2b
, level
);
541 aluInitPanning(pContext
);
548 Clean up Context, destroy any remaining Sources
550 static ALCvoid
ExitContext(ALCcontext
*pContext
)
553 pContext
->LastError
= AL_NO_ERROR
;
554 pContext
->InUse
= AL_FALSE
;
556 free(pContext
->bs2b
);
557 pContext
->bs2b
= NULL
;
560 ///////////////////////////////////////////////////////
563 ///////////////////////////////////////////////////////
564 // ALC Functions calls
567 // This should probably move to another c file but for now ...
568 ALCAPI ALCdevice
* ALCAPIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
570 ALCboolean DeviceFound
= ALC_FALSE
;
571 ALCdevice
*pDevice
= NULL
;
578 SetALCError(ALC_INVALID_VALUE
);
582 if(deviceName
&& !deviceName
[0])
585 pDevice
= malloc(sizeof(ALCdevice
));
588 //Initialise device structure
589 memset(pDevice
, 0, sizeof(ALCdevice
));
592 pDevice
->Connected
= ALC_TRUE
;
593 pDevice
->IsCaptureDevice
= AL_TRUE
;
595 pDevice
->Frequency
= frequency
;
596 pDevice
->Format
= format
;
597 pDevice
->BufferSize
= SampleSize
;
599 SuspendContext(NULL
);
600 for(i
= 0;BackendList
[i
].Init
;i
++)
602 pDevice
->Funcs
= &BackendList
[i
].Funcs
;
603 if(ALCdevice_OpenCapture(pDevice
, deviceName
))
605 pDevice
->next
= g_pDeviceList
;
606 g_pDeviceList
= pDevice
;
609 DeviceFound
= ALC_TRUE
;
613 ProcessContext(NULL
);
617 SetALCError(ALC_INVALID_VALUE
);
623 SetALCError(ALC_OUT_OF_MEMORY
);
628 ALCAPI ALCboolean ALCAPIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
630 ALCboolean bReturn
= ALC_FALSE
;
633 if(IsDevice(pDevice
) && pDevice
->IsCaptureDevice
)
635 SuspendContext(NULL
);
637 list
= &g_pDeviceList
;
638 while(*list
!= pDevice
)
639 list
= &(*list
)->next
;
641 *list
= (*list
)->next
;
644 ProcessContext(NULL
);
646 ALCdevice_CloseCapture(pDevice
);
652 SetALCError(ALC_INVALID_DEVICE
);
657 ALCAPI
void ALCAPIENTRY
alcCaptureStart(ALCdevice
*pDevice
)
659 if(IsDevice(pDevice
) && pDevice
->IsCaptureDevice
)
660 ALCdevice_StartCapture(pDevice
);
662 SetALCError(ALC_INVALID_DEVICE
);
665 ALCAPI
void ALCAPIENTRY
alcCaptureStop(ALCdevice
*pDevice
)
667 if(IsDevice(pDevice
) && pDevice
->IsCaptureDevice
)
668 ALCdevice_StopCapture(pDevice
);
670 SetALCError(ALC_INVALID_DEVICE
);
673 ALCAPI
void ALCAPIENTRY
alcCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCsizei lSamples
)
675 if(IsDevice(pDevice
) && pDevice
->IsCaptureDevice
)
676 ALCdevice_CaptureSamples(pDevice
, pBuffer
, lSamples
);
678 SetALCError(ALC_INVALID_DEVICE
);
684 Return last ALC generated error code
686 ALCAPI ALCenum ALCAPIENTRY
alcGetError(ALCdevice
*device
)
692 errorCode
= g_eLastContextError
;
693 g_eLastContextError
= ALC_NO_ERROR
;
703 ALCAPI ALCvoid ALCAPIENTRY
alcSuspendContext(ALCcontext
*pContext
)
705 // Not a lot happens here !
715 ALCAPI ALCvoid ALCAPIENTRY
alcProcessContext(ALCcontext
*pContext
)
717 // Not a lot happens here !
725 Returns information about the Device, and error strings
727 ALCAPI
const ALCchar
* ALCAPIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
729 const ALCchar
*value
= NULL
;
739 case ALC_INVALID_ENUM
:
740 value
= alcErrInvalidEnum
;
743 case ALC_INVALID_VALUE
:
744 value
= alcErrInvalidValue
;
747 case ALC_INVALID_DEVICE
:
748 value
= alcErrInvalidDevice
;
751 case ALC_INVALID_CONTEXT
:
752 value
= alcErrInvalidContext
;
755 case ALC_OUT_OF_MEMORY
:
756 value
= alcErrOutOfMemory
;
759 case ALC_DEFAULT_DEVICE_SPECIFIER
:
760 value
= alcDefaultDeviceSpecifier
;
763 case ALC_DEVICE_SPECIFIER
:
764 if(IsDevice(pDevice
))
765 value
= pDevice
->szDeviceName
;
767 value
= alcDeviceList
;
770 case ALC_ALL_DEVICES_SPECIFIER
:
771 value
= alcAllDeviceList
;
774 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
775 value
= alcDefaultAllDeviceSpecifier
;
778 case ALC_CAPTURE_DEVICE_SPECIFIER
:
779 if(IsDevice(pDevice
))
780 value
= pDevice
->szDeviceName
;
782 value
= alcCaptureDeviceList
;
785 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
786 value
= alcCaptureDefaultDeviceSpecifier
;
790 value
= alcExtensionList
;
794 SetALCError(ALC_INVALID_ENUM
);
805 Returns information about the Device and the version of Open AL
807 ALCAPI ALCvoid ALCAPIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
811 if(IsDevice(device
) && device
->IsCaptureDevice
)
813 SuspendContext(NULL
);
818 case ALC_CAPTURE_SAMPLES
:
819 if ((size
) && (data
))
820 *data
= ALCdevice_AvailableSamples(device
);
822 SetALCError(ALC_INVALID_VALUE
);
827 SetALCError(ALC_INVALID_VALUE
);
829 *data
= device
->Connected
;
833 SetALCError(ALC_INVALID_ENUM
);
837 ProcessContext(NULL
);
846 case ALC_MAJOR_VERSION
:
848 SetALCError(ALC_INVALID_VALUE
);
850 *data
= alcMajorVersion
;
853 case ALC_MINOR_VERSION
:
855 SetALCError(ALC_INVALID_VALUE
);
857 *data
= alcMinorVersion
;
860 case ALC_EFX_MAJOR_VERSION
:
862 SetALCError(ALC_INVALID_VALUE
);
864 *data
= alcEFXMajorVersion
;
867 case ALC_EFX_MINOR_VERSION
:
869 SetALCError(ALC_INVALID_VALUE
);
871 *data
= alcEFXMinorVersion
;
874 case ALC_MAX_AUXILIARY_SENDS
:
876 SetALCError(ALC_INVALID_VALUE
);
878 *data
= (device
?device
->NumAuxSends
:MAX_SENDS
);
881 case ALC_ATTRIBUTES_SIZE
:
882 if(!IsDevice(device
))
883 SetALCError(ALC_INVALID_DEVICE
);
885 SetALCError(ALC_INVALID_VALUE
);
890 case ALC_ALL_ATTRIBUTES
:
891 if(!IsDevice(device
))
892 SetALCError(ALC_INVALID_DEVICE
);
894 SetALCError(ALC_INVALID_VALUE
);
899 SuspendContext(NULL
);
900 data
[i
++] = ALC_FREQUENCY
;
901 data
[i
++] = device
->Frequency
;
903 data
[i
++] = ALC_REFRESH
;
904 data
[i
++] = device
->Frequency
/ device
->UpdateSize
;
906 data
[i
++] = ALC_SYNC
;
907 data
[i
++] = ALC_FALSE
;
909 data
[i
++] = ALC_MONO_SOURCES
;
910 data
[i
++] = device
->lNumMonoSources
;
912 data
[i
++] = ALC_STEREO_SOURCES
;
913 data
[i
++] = device
->lNumStereoSources
;
915 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
916 data
[i
++] = device
->NumAuxSends
;
919 ProcessContext(NULL
);
924 if(!IsDevice(device
))
925 SetALCError(ALC_INVALID_DEVICE
);
927 SetALCError(ALC_INVALID_VALUE
);
929 *data
= device
->Frequency
;
933 if(!IsDevice(device
))
934 SetALCError(ALC_INVALID_DEVICE
);
936 SetALCError(ALC_INVALID_VALUE
);
938 *data
= device
->Frequency
/ device
->UpdateSize
;
942 if(!IsDevice(device
))
943 SetALCError(ALC_INVALID_DEVICE
);
945 SetALCError(ALC_INVALID_VALUE
);
950 case ALC_MONO_SOURCES
:
951 if(!IsDevice(device
))
952 SetALCError(ALC_INVALID_DEVICE
);
954 SetALCError(ALC_INVALID_VALUE
);
956 *data
= device
->lNumMonoSources
;
959 case ALC_STEREO_SOURCES
:
960 if(!IsDevice(device
))
961 SetALCError(ALC_INVALID_DEVICE
);
963 SetALCError(ALC_INVALID_VALUE
);
965 *data
= device
->lNumStereoSources
;
969 if(!IsDevice(device
))
970 SetALCError(ALC_INVALID_DEVICE
);
972 SetALCError(ALC_INVALID_VALUE
);
974 *data
= device
->Connected
;
978 SetALCError(ALC_INVALID_ENUM
);
983 SetALCError(ALC_INVALID_VALUE
);
991 alcIsExtensionPresent
993 Determines if there is support for a particular extension
995 ALCAPI ALCboolean ALCAPIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
997 ALCboolean bResult
= ALC_FALSE
;
1006 len
= strlen(extName
);
1007 ptr
= alcExtensionList
;
1010 if(strncasecmp(ptr
, extName
, len
) == 0 &&
1011 (ptr
[len
] == '\0' || isspace(ptr
[len
])))
1016 if((ptr
=strchr(ptr
, ' ')) != NULL
)
1020 } while(isspace(*ptr
));
1025 SetALCError(ALC_INVALID_VALUE
);
1034 Retrieves the function address for a particular extension function
1036 ALCAPI ALCvoid
* ALCAPIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
1038 ALCvoid
*pFunction
= NULL
;
1045 while(alcFunctions
[i
].funcName
&&
1046 strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
1048 pFunction
= alcFunctions
[i
].address
;
1051 SetALCError(ALC_INVALID_VALUE
);
1060 Get the value for a particular ALC Enumerated Value
1062 ALCAPI ALCenum ALCAPIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
1069 while ((enumeration
[i
].enumName
)&&(strcmp(enumeration
[i
].enumName
,enumName
)))
1071 val
= enumeration
[i
].value
;
1073 if(!enumeration
[i
].enumName
)
1074 SetALCError(ALC_INVALID_VALUE
);
1083 Create and attach a Context to a particular Device.
1085 ALCAPI ALCcontext
* ALCAPIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
1087 ALCcontext
*ALContext
= NULL
;
1088 ALuint ulAttributeIndex
, ulRequestedStereoSources
;
1089 ALuint RequestedSends
;
1091 if(IsDevice(device
) && !device
->IsCaptureDevice
&& device
->Connected
)
1093 // Reset Context Last Error code
1094 g_eLastContextError
= ALC_NO_ERROR
;
1096 // Current implementation only allows one Context per Device
1097 if(!device
->Context
)
1099 ALContext
= calloc(1, sizeof(ALCcontext
));
1102 SetALCError(ALC_OUT_OF_MEMORY
);
1106 SuspendContext(NULL
);
1108 ALContext
->Device
= device
;
1109 InitContext(ALContext
);
1111 device
->Context
= ALContext
;
1113 ALContext
->next
= g_pContextList
;
1114 g_pContextList
= ALContext
;
1117 // Check for attributes
1120 ALCuint freq
= device
->Frequency
;
1121 ALCint numMono
= device
->lNumMonoSources
;
1122 ALCint numStereo
= device
->lNumStereoSources
;
1123 ALCuint numSends
= device
->NumAuxSends
;
1125 ulAttributeIndex
= 0;
1126 while ((ulAttributeIndex
< 10) && (attrList
[ulAttributeIndex
]))
1128 if(attrList
[ulAttributeIndex
] == ALC_FREQUENCY
)
1130 freq
= attrList
[ulAttributeIndex
+ 1];
1132 freq
= device
->Frequency
;
1135 if(attrList
[ulAttributeIndex
] == ALC_STEREO_SOURCES
)
1137 ulRequestedStereoSources
= attrList
[ulAttributeIndex
+ 1];
1139 if (ulRequestedStereoSources
> device
->MaxNoOfSources
)
1140 ulRequestedStereoSources
= device
->MaxNoOfSources
;
1142 numStereo
= ulRequestedStereoSources
;
1143 numMono
= device
->MaxNoOfSources
- numStereo
;
1146 if(attrList
[ulAttributeIndex
] == ALC_MAX_AUXILIARY_SENDS
)
1148 RequestedSends
= attrList
[ulAttributeIndex
+ 1];
1150 if(RequestedSends
> device
->NumAuxSends
)
1151 RequestedSends
= device
->NumAuxSends
;
1153 numSends
= RequestedSends
;
1156 ulAttributeIndex
+= 2;
1159 device
->Frequency
= GetConfigValueInt(NULL
, "frequency", freq
);
1160 device
->lNumMonoSources
= numMono
;
1161 device
->lNumStereoSources
= numStereo
;
1162 device
->NumAuxSends
= numSends
;
1165 if(ALCdevice_StartContext(device
, ALContext
) == ALC_FALSE
)
1167 alcDestroyContext(ALContext
);
1169 SetALCError(ALC_INVALID_VALUE
);
1172 ALContext
->Frequency
= device
->Frequency
;
1174 ProcessContext(NULL
);
1178 SetALCError(ALC_INVALID_VALUE
);
1183 SetALCError(ALC_INVALID_DEVICE
);
1194 ALCAPI ALCvoid ALCAPIENTRY
alcDestroyContext(ALCcontext
*context
)
1200 if (IsContext(context
))
1202 ALCdevice_StopContext(context
->Device
, context
);
1205 SuspendContext(context
);
1207 if(context
->SourceCount
> 0)
1210 AL_PRINT("alcDestroyContext(): deleting %d Source(s)\n", context
->SourceCount
);
1212 ReleaseALSources(context
);
1214 if(context
->AuxiliaryEffectSlotCount
> 0)
1217 AL_PRINT("alcDestroyContext(): deleting %d AuxiliaryEffectSlot(s)\n", context
->AuxiliaryEffectSlotCount
);
1219 ReleaseALAuxiliaryEffectSlots(context
);
1222 context
->Device
->Context
= NULL
;
1224 list
= &g_pContextList
;
1225 while(*list
!= context
)
1226 list
= &(*list
)->next
;
1228 *list
= (*list
)->next
;
1232 ProcessContext(context
);
1234 ExitContext(context
);
1236 // Free memory (MUST do this after ProcessContext)
1237 memset(context
, 0, sizeof(ALCcontext
));
1241 SetALCError(ALC_INVALID_CONTEXT
);
1246 alcGetCurrentContext
1248 Returns the currently active Context
1250 ALCAPI ALCcontext
* ALCAPIENTRY
alcGetCurrentContext(ALCvoid
)
1252 ALCcontext
*pContext
= NULL
;
1256 SuspendContext(NULL
);
1258 pContext
= g_pContextList
;
1259 while ((pContext
) && (!pContext
->InUse
))
1260 pContext
= pContext
->next
;
1262 ProcessContext(NULL
);
1269 alcGetContextsDevice
1271 Returns the Device that a particular Context is attached to
1273 ALCAPI ALCdevice
* ALCAPIENTRY
alcGetContextsDevice(ALCcontext
*pContext
)
1275 ALCdevice
*pDevice
= NULL
;
1279 SuspendContext(NULL
);
1280 if (IsContext(pContext
))
1281 pDevice
= pContext
->Device
;
1283 SetALCError(ALC_INVALID_CONTEXT
);
1284 ProcessContext(NULL
);
1291 alcMakeContextCurrent
1293 Makes the given Context the active Context
1295 ALCAPI ALCboolean ALCAPIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
1297 ALCcontext
*ALContext
;
1298 ALboolean bReturn
= AL_TRUE
;
1302 SuspendContext(NULL
);
1304 // context must be a valid Context or NULL
1305 if(context
== NULL
|| IsContext(context
))
1307 if((ALContext
=GetContextSuspended()) != NULL
)
1309 ALContext
->InUse
=AL_FALSE
;
1310 ProcessContext(ALContext
);
1313 if((ALContext
=context
) != NULL
&& ALContext
->Device
)
1315 SuspendContext(ALContext
);
1316 ALContext
->InUse
=AL_TRUE
;
1317 ProcessContext(ALContext
);
1322 SetALCError(ALC_INVALID_CONTEXT
);
1326 ProcessContext(NULL
);
1332 static ALenum
GetFormatFromString(const char *str
)
1334 if(strcasecmp(str
, "AL_FORMAT_MONO32") == 0) return AL_FORMAT_MONO_FLOAT32
;
1335 if(strcasecmp(str
, "AL_FORMAT_STEREO32") == 0) return AL_FORMAT_STEREO_FLOAT32
;
1336 if(strcasecmp(str
, "AL_FORMAT_QUAD32") == 0) return AL_FORMAT_QUAD32
;
1337 if(strcasecmp(str
, "AL_FORMAT_51CHN32") == 0) return AL_FORMAT_51CHN32
;
1338 if(strcasecmp(str
, "AL_FORMAT_61CHN32") == 0) return AL_FORMAT_61CHN32
;
1339 if(strcasecmp(str
, "AL_FORMAT_71CHN32") == 0) return AL_FORMAT_71CHN32
;
1341 if(strcasecmp(str
, "AL_FORMAT_MONO16") == 0) return AL_FORMAT_MONO16
;
1342 if(strcasecmp(str
, "AL_FORMAT_STEREO16") == 0) return AL_FORMAT_STEREO16
;
1343 if(strcasecmp(str
, "AL_FORMAT_QUAD16") == 0) return AL_FORMAT_QUAD16
;
1344 if(strcasecmp(str
, "AL_FORMAT_51CHN16") == 0) return AL_FORMAT_51CHN16
;
1345 if(strcasecmp(str
, "AL_FORMAT_61CHN16") == 0) return AL_FORMAT_61CHN16
;
1346 if(strcasecmp(str
, "AL_FORMAT_71CHN16") == 0) return AL_FORMAT_71CHN16
;
1348 if(strcasecmp(str
, "AL_FORMAT_MONO8") == 0) return AL_FORMAT_MONO8
;
1349 if(strcasecmp(str
, "AL_FORMAT_STEREO8") == 0) return AL_FORMAT_STEREO8
;
1350 if(strcasecmp(str
, "AL_FORMAT_QUAD8") == 0) return AL_FORMAT_QUAD8
;
1351 if(strcasecmp(str
, "AL_FORMAT_51CHN8") == 0) return AL_FORMAT_51CHN8
;
1352 if(strcasecmp(str
, "AL_FORMAT_61CHN8") == 0) return AL_FORMAT_61CHN8
;
1353 if(strcasecmp(str
, "AL_FORMAT_71CHN8") == 0) return AL_FORMAT_71CHN8
;
1355 AL_PRINT("Unknown format: \"%s\"\n", str
);
1356 return AL_FORMAT_STEREO16
;
1362 Open the Device specified.
1364 ALCAPI ALCdevice
* ALCAPIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
1366 ALboolean bDeviceFound
= AL_FALSE
;
1372 if(deviceName
&& !deviceName
[0])
1375 device
= malloc(sizeof(ALCdevice
));
1380 //Initialise device structure
1381 memset(device
, 0, sizeof(ALCdevice
));
1384 device
->Connected
= ALC_TRUE
;
1385 device
->IsCaptureDevice
= AL_FALSE
;
1388 device
->Frequency
= GetConfigValueInt(NULL
, "frequency", SWMIXER_OUTPUT_RATE
);
1389 if(device
->Frequency
== 0)
1390 device
->Frequency
= SWMIXER_OUTPUT_RATE
;
1392 fmt
= GetConfigValue(NULL
, "format", "AL_FORMAT_STEREO16");
1393 device
->Format
= GetFormatFromString(fmt
);
1395 device
->BufferSize
= GetConfigValueInt(NULL
, "refresh", 4096);
1396 if((ALint
)device
->BufferSize
<= 0)
1397 device
->BufferSize
= 4096;
1399 device
->MaxNoOfSources
= GetConfigValueInt(NULL
, "sources", 256);
1400 if((ALint
)device
->MaxNoOfSources
<= 0)
1401 device
->MaxNoOfSources
= 256;
1403 device
->AuxiliaryEffectSlotMax
= GetConfigValueInt(NULL
, "slots", 4);
1404 if((ALint
)device
->AuxiliaryEffectSlotMax
<= 0)
1405 device
->AuxiliaryEffectSlotMax
= 4;
1407 device
->lNumStereoSources
= 1;
1408 device
->lNumMonoSources
= device
->MaxNoOfSources
- device
->lNumStereoSources
;
1410 device
->NumAuxSends
= GetConfigValueInt(NULL
, "sends", MAX_SENDS
);
1411 if(device
->NumAuxSends
> MAX_SENDS
)
1412 device
->NumAuxSends
= MAX_SENDS
;
1414 // Find a playback device to open
1415 SuspendContext(NULL
);
1416 for(i
= 0;BackendList
[i
].Init
;i
++)
1418 device
->Funcs
= &BackendList
[i
].Funcs
;
1419 if(ALCdevice_OpenPlayback(device
, deviceName
))
1421 device
->next
= g_pDeviceList
;
1422 g_pDeviceList
= device
;
1425 bDeviceFound
= AL_TRUE
;
1429 ProcessContext(NULL
);
1433 // No suitable output device found
1434 SetALCError(ALC_INVALID_VALUE
);
1440 SetALCError(ALC_OUT_OF_MEMORY
);
1449 Close the specified Device
1451 ALCAPI ALCboolean ALCAPIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
1453 ALCboolean bReturn
= ALC_FALSE
;
1456 if(IsDevice(pDevice
) && !pDevice
->IsCaptureDevice
)
1458 SuspendContext(NULL
);
1460 list
= &g_pDeviceList
;
1461 while(*list
!= pDevice
)
1462 list
= &(*list
)->next
;
1464 *list
= (*list
)->next
;
1467 ProcessContext(NULL
);
1469 if(pDevice
->Context
)
1472 AL_PRINT("alcCloseDevice(): destroying 1 Context\n");
1474 alcDestroyContext(pDevice
->Context
);
1476 ALCdevice_ClosePlayback(pDevice
);
1478 if(pDevice
->BufferCount
> 0)
1481 AL_PRINT("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice
->BufferCount
);
1483 ReleaseALBuffers(pDevice
);
1485 if(pDevice
->EffectCount
> 0)
1488 AL_PRINT("alcCloseDevice(): deleting %d Effect(s)\n", pDevice
->EffectCount
);
1490 ReleaseALEffects(pDevice
);
1492 if(pDevice
->FilterCount
> 0)
1495 AL_PRINT("alcCloseDevice(): deleting %d Filter(s)\n", pDevice
->FilterCount
);
1497 ReleaseALFilters(pDevice
);
1499 if(pDevice
->DatabufferCount
> 0)
1502 AL_PRINT("alcCloseDevice(): deleting %d Databuffer(s)\n", pDevice
->DatabufferCount
);
1504 ReleaseALDatabuffers(pDevice
);
1507 //Release device structure
1508 memset(pDevice
, 0, sizeof(ALCdevice
));
1514 SetALCError(ALC_INVALID_DEVICE
);
1520 ALCvoid
ReleaseALC(ALCvoid
)
1523 if(g_ulDeviceCount
> 0)
1524 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount
, (g_ulDeviceCount
>1)?"s":"");
1527 while(g_pDeviceList
)
1529 if(g_pDeviceList
->IsCaptureDevice
)
1530 alcCaptureCloseDevice(g_pDeviceList
);
1532 alcCloseDevice(g_pDeviceList
);
1536 ///////////////////////////////////////////////////////