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"
41 #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
44 void (*Init
)(BackendFuncs
*);
48 { "alsa", alc_alsa_init
, EmptyFuncs
},
51 { "oss", alc_oss_init
, EmptyFuncs
},
54 { "solaris", alc_solaris_init
, EmptyFuncs
},
57 { "dsound", alcDSoundInit
, EmptyFuncs
},
60 { "winmm", alcWinMMInit
, EmptyFuncs
},
63 { "port", alc_pa_init
, EmptyFuncs
},
65 #ifdef HAVE_PULSEAUDIO
66 { "pulse", alc_pulse_init
, EmptyFuncs
},
69 { "wave", alc_wave_init
, EmptyFuncs
},
71 { NULL
, NULL
, EmptyFuncs
}
75 ///////////////////////////////////////////////////////
77 #define ALC_EFX_MAJOR_VERSION 0x20001
78 #define ALC_EFX_MINOR_VERSION 0x20002
79 #define ALC_MAX_AUXILIARY_SENDS 0x20003
81 ///////////////////////////////////////////////////////
82 // STRING and EXTENSIONS
84 typedef struct ALCfunction_struct
90 static ALCfunction alcFunctions
[] = {
91 { "alcCreateContext", (ALvoid
*) alcCreateContext
},
92 { "alcMakeContextCurrent", (ALvoid
*) alcMakeContextCurrent
},
93 { "alcProcessContext", (ALvoid
*) alcProcessContext
},
94 { "alcSuspendContext", (ALvoid
*) alcSuspendContext
},
95 { "alcDestroyContext", (ALvoid
*) alcDestroyContext
},
96 { "alcGetCurrentContext", (ALvoid
*) alcGetCurrentContext
},
97 { "alcGetContextsDevice", (ALvoid
*) alcGetContextsDevice
},
98 { "alcOpenDevice", (ALvoid
*) alcOpenDevice
},
99 { "alcCloseDevice", (ALvoid
*) alcCloseDevice
},
100 { "alcGetError", (ALvoid
*) alcGetError
},
101 { "alcIsExtensionPresent", (ALvoid
*) alcIsExtensionPresent
},
102 { "alcGetProcAddress", (ALvoid
*) alcGetProcAddress
},
103 { "alcGetEnumValue", (ALvoid
*) alcGetEnumValue
},
104 { "alcGetString", (ALvoid
*) alcGetString
},
105 { "alcGetIntegerv", (ALvoid
*) alcGetIntegerv
},
106 { "alcCaptureOpenDevice", (ALvoid
*) alcCaptureOpenDevice
},
107 { "alcCaptureCloseDevice", (ALvoid
*) alcCaptureCloseDevice
},
108 { "alcCaptureStart", (ALvoid
*) alcCaptureStart
},
109 { "alcCaptureStop", (ALvoid
*) alcCaptureStop
},
110 { "alcCaptureSamples", (ALvoid
*) alcCaptureSamples
},
111 { NULL
, (ALvoid
*) NULL
}
114 static ALenums enumeration
[]={
116 { (ALchar
*)"ALC_INVALID", ALC_INVALID
},
117 { (ALchar
*)"ALC_FALSE", ALC_FALSE
},
118 { (ALchar
*)"ALC_TRUE", ALC_TRUE
},
121 { (ALchar
*)"ALC_MAJOR_VERSION", ALC_MAJOR_VERSION
},
122 { (ALchar
*)"ALC_MINOR_VERSION", ALC_MINOR_VERSION
},
123 { (ALchar
*)"ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE
},
124 { (ALchar
*)"ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES
},
125 { (ALchar
*)"ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER
},
126 { (ALchar
*)"ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER
},
127 { (ALchar
*)"ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER
},
128 { (ALchar
*)"ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER
},
129 { (ALchar
*)"ALC_EXTENSIONS", ALC_EXTENSIONS
},
130 { (ALchar
*)"ALC_FREQUENCY", ALC_FREQUENCY
},
131 { (ALchar
*)"ALC_REFRESH", ALC_REFRESH
},
132 { (ALchar
*)"ALC_SYNC", ALC_SYNC
},
133 { (ALchar
*)"ALC_MONO_SOURCES", ALC_MONO_SOURCES
},
134 { (ALchar
*)"ALC_STEREO_SOURCES", ALC_STEREO_SOURCES
},
135 { (ALchar
*)"ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER
},
136 { (ALchar
*)"ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
},
137 { (ALchar
*)"ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES
},
140 { (ALchar
*)"ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION
},
141 { (ALchar
*)"ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION
},
142 { (ALchar
*)"ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS
},
145 { (ALchar
*)"ALC_NO_ERROR", ALC_NO_ERROR
},
146 { (ALchar
*)"ALC_INVALID_DEVICE", ALC_INVALID_DEVICE
},
147 { (ALchar
*)"ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT
},
148 { (ALchar
*)"ALC_INVALID_ENUM", ALC_INVALID_ENUM
},
149 { (ALchar
*)"ALC_INVALID_VALUE", ALC_INVALID_VALUE
},
150 { (ALchar
*)"ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY
},
151 { (ALchar
*)NULL
, (ALenum
)0 }
154 static const ALCchar alcNoError
[] = "No Error";
155 static const ALCchar alcErrInvalidDevice
[] = "Invalid Device";
156 static const ALCchar alcErrInvalidContext
[] = "Invalid Context";
157 static const ALCchar alcErrInvalidEnum
[] = "Invalid Enum";
158 static const ALCchar alcErrInvalidValue
[] = "Invalid Value";
159 static const ALCchar alcErrOutOfMemory
[] = "Out of Memory";
162 static ALCchar alcDeviceList
[2048];
163 static ALCchar alcAllDeviceList
[2048];
164 static ALCchar alcCaptureDeviceList
[2048];
165 // Default is always the first in the list
166 static ALCchar
*alcDefaultDeviceSpecifier
= alcDeviceList
;
167 static ALCchar
*alcDefaultAllDeviceSpecifier
= alcAllDeviceList
;
168 static ALCchar
*alcCaptureDefaultDeviceSpecifier
= alcCaptureDeviceList
;
171 static ALCchar alcExtensionList
[] = "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_EFX";
172 static ALCint alcMajorVersion
= 1;
173 static ALCint alcMinorVersion
= 1;
175 static ALCint alcEFXMajorVersion
= 1;
176 static ALCint alcEFXMinorVersion
= 0;
178 ///////////////////////////////////////////////////////
181 ///////////////////////////////////////////////////////
184 static ALCdevice
*g_pDeviceList
= NULL
;
185 static ALCuint g_ulDeviceCount
= 0;
187 static CRITICAL_SECTION g_csMutex
;
190 static ALCcontext
*g_pContextList
= NULL
;
191 static ALCuint g_ulContextCount
= 0;
194 static ALCenum g_eLastContextError
= ALC_NO_ERROR
;
196 static ALboolean init_done
= AL_FALSE
;
198 ///////////////////////////////////////////////////////
201 ///////////////////////////////////////////////////////
202 // ALC Related helper functions
204 BOOL APIENTRY
DllMain(HANDLE hModule
,DWORD ul_reason_for_call
,LPVOID lpReserved
)
208 // Perform actions based on the reason for calling.
209 switch(ul_reason_for_call
)
211 case DLL_PROCESS_ATTACH
:
212 DisableThreadLibraryCalls(hModule
);
215 case DLL_PROCESS_DETACH
:
221 DeleteCriticalSection(&g_csMutex
);
227 #ifdef HAVE_GCC_DESTRUCTOR
228 static void my_deinit() __attribute__((destructor
));
229 static void my_deinit()
231 static ALenum once
= AL_FALSE
;
232 if(once
|| !init_done
) return;
238 DeleteCriticalSection(&g_csMutex
);
243 static void InitAL(void)
248 const char *devs
, *str
;
252 InitializeCriticalSection(&g_csMutex
);
256 devs
= GetConfigValue(NULL
, "drivers", "");
261 const char *next
= devs
;
267 next
= strchr(devs
, ',');
269 if(!devs
[0] || devs
[0] == ',')
272 len
= (next
? ((size_t)(next
-devs
)) : strlen(devs
));
273 for(n
= i
;BackendList
[n
].Init
;n
++)
275 if(len
== strlen(BackendList
[n
].name
) &&
276 strncmp(BackendList
[n
].name
, devs
, len
) == 0)
278 const char *name
= BackendList
[i
].name
;
279 void (*Init
)(BackendFuncs
*) = BackendList
[i
].Init
;
281 BackendList
[i
].name
= BackendList
[n
].name
;
282 BackendList
[i
].Init
= BackendList
[n
].Init
;
284 BackendList
[n
].name
= name
;
285 BackendList
[n
].Init
= Init
;
292 BackendList
[i
].name
= NULL
;
293 BackendList
[i
].Init
= NULL
;
296 for(i
= 0;BackendList
[i
].Init
;i
++)
297 BackendList
[i
].Init(&BackendList
[i
].Funcs
);
299 str
= GetConfigValue(NULL
, "stereodup", "false");
300 DuplicateStereo
= (strcasecmp(str
, "true") == 0 ||
301 strcasecmp(str
, "yes") == 0 ||
302 strcasecmp(str
, "on") == 0 ||
305 str
= GetConfigValue(NULL
, "excludefx", "");
312 { "eaxreverb", EAXREVERB
},
313 { "reverb", REVERB
},
319 const char *next
= str
;
323 next
= strchr(str
, ',');
325 if(!str
[0] || next
== str
)
328 len
= (next
? ((size_t)(next
-str
)) : strlen(str
));
329 for(n
= 0;EffectList
[n
].name
;n
++)
331 if(len
== strlen(EffectList
[n
].name
) &&
332 strncmp(EffectList
[n
].name
, str
, len
) == 0)
333 DisabledEffects
[EffectList
[n
].type
] = AL_TRUE
;
340 ALCchar
*AppendDeviceList(char *name
)
343 ALCchar
*ret
= alcDeviceList
+pos
;
344 if(pos
>= sizeof(alcDeviceList
))
346 AL_PRINT("Not enough room to add %s!\n", name
);
347 return alcDeviceList
+ sizeof(alcDeviceList
) - 1;
349 pos
+= snprintf(alcDeviceList
+pos
, sizeof(alcDeviceList
)-pos
-1, "%s", name
) + 1;
353 ALCchar
*AppendAllDeviceList(char *name
)
356 ALCchar
*ret
= alcAllDeviceList
+pos
;
357 if(pos
>= sizeof(alcAllDeviceList
))
359 AL_PRINT("Not enough room to add %s!\n", name
);
360 return alcAllDeviceList
+ sizeof(alcAllDeviceList
) - 1;
362 pos
+= snprintf(alcAllDeviceList
+pos
, sizeof(alcAllDeviceList
)-pos
-1, "%s", name
) + 1;
366 ALCchar
*AppendCaptureDeviceList(char *name
)
369 ALCchar
*ret
= alcCaptureDeviceList
+pos
;
370 if(pos
>= sizeof(alcCaptureDeviceList
))
372 AL_PRINT("Not enough room to add %s!\n", name
);
373 return alcCaptureDeviceList
+ sizeof(alcCaptureDeviceList
) - 1;
375 pos
+= snprintf(alcCaptureDeviceList
+pos
, sizeof(alcCaptureDeviceList
)-pos
-1, "%s", name
) + 1;
382 Check pDevice is a valid Device pointer
384 static ALCboolean
IsDevice(ALCdevice
*pDevice
)
386 ALCdevice
*pTempDevice
;
388 SuspendContext(NULL
);
390 pTempDevice
= g_pDeviceList
;
391 while(pTempDevice
&& pTempDevice
!= pDevice
)
392 pTempDevice
= pTempDevice
->next
;
394 ProcessContext(NULL
);
396 return (pTempDevice
? ALC_TRUE
: ALC_FALSE
);
402 Check pContext is a valid Context pointer
404 static ALCboolean
IsContext(ALCcontext
*pContext
)
406 ALCcontext
*pTempContext
;
408 SuspendContext(NULL
);
410 pTempContext
= g_pContextList
;
411 while (pTempContext
&& pTempContext
!= pContext
)
412 pTempContext
= pTempContext
->next
;
414 ProcessContext(NULL
);
416 return (pTempContext
? ALC_TRUE
: ALC_FALSE
);
423 Store latest ALC Error
425 ALCvoid
SetALCError(ALenum errorCode
)
427 g_eLastContextError
= errorCode
;
436 ALCvoid
SuspendContext(ALCcontext
*pContext
)
439 EnterCriticalSection(&g_csMutex
);
448 ALCvoid
ProcessContext(ALCcontext
*pContext
)
451 LeaveCriticalSection(&g_csMutex
);
458 Initialize Context variables
460 static ALvoid
InitContext(ALCcontext
*pContext
)
464 //Initialise listener
465 pContext
->Listener
.Gain
= 1.0f
;
466 pContext
->Listener
.MetersPerUnit
= 1.0f
;
467 pContext
->Listener
.Position
[0] = 0.0f
;
468 pContext
->Listener
.Position
[1] = 0.0f
;
469 pContext
->Listener
.Position
[2] = 0.0f
;
470 pContext
->Listener
.Velocity
[0] = 0.0f
;
471 pContext
->Listener
.Velocity
[1] = 0.0f
;
472 pContext
->Listener
.Velocity
[2] = 0.0f
;
473 pContext
->Listener
.Forward
[0] = 0.0f
;
474 pContext
->Listener
.Forward
[1] = 0.0f
;
475 pContext
->Listener
.Forward
[2] = -1.0f
;
476 pContext
->Listener
.Up
[0] = 0.0f
;
477 pContext
->Listener
.Up
[1] = 1.0f
;
478 pContext
->Listener
.Up
[2] = 0.0f
;
481 pContext
->LastError
= AL_NO_ERROR
;
482 pContext
->InUse
= AL_FALSE
;
485 pContext
->Frequency
= pContext
->Device
->Frequency
;
488 pContext
->DistanceModel
= AL_INVERSE_DISTANCE_CLAMPED
;
489 pContext
->DopplerFactor
= 1.0f
;
490 pContext
->DopplerVelocity
= 1.0f
;
491 pContext
->flSpeedOfSound
= SPEEDOFSOUNDMETRESPERSEC
;
493 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";
495 level
= GetConfigValueInt(NULL
, "cf_level", 0);
496 if(level
> 0 && level
<= 6)
498 pContext
->bs2b
= calloc(1, sizeof(*pContext
->bs2b
));
499 bs2b_set_srate(pContext
->bs2b
, pContext
->Frequency
);
500 bs2b_set_level(pContext
->bs2b
, level
);
503 aluInitPanning(pContext
);
510 Clean up Context, destroy any remaining Sources
512 static ALCvoid
ExitContext(ALCcontext
*pContext
)
515 pContext
->LastError
= AL_NO_ERROR
;
516 pContext
->InUse
= AL_FALSE
;
518 free(pContext
->bs2b
);
519 pContext
->bs2b
= NULL
;
522 ///////////////////////////////////////////////////////
525 ///////////////////////////////////////////////////////
526 // ALC Functions calls
529 // This should probably move to another c file but for now ...
530 ALCAPI ALCdevice
* ALCAPIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
532 ALCboolean DeviceFound
= ALC_FALSE
;
533 ALCdevice
*pDevice
= NULL
;
540 SetALCError(ALC_INVALID_VALUE
);
544 if(deviceName
&& !deviceName
[0])
547 pDevice
= malloc(sizeof(ALCdevice
));
550 //Initialise device structure
551 memset(pDevice
, 0, sizeof(ALCdevice
));
554 pDevice
->IsCaptureDevice
= AL_TRUE
;
556 pDevice
->Frequency
= frequency
;
557 pDevice
->Format
= format
;
558 pDevice
->BufferSize
= SampleSize
;
560 SuspendContext(NULL
);
561 for(i
= 0;BackendList
[i
].Init
;i
++)
563 pDevice
->Funcs
= &BackendList
[i
].Funcs
;
564 if(ALCdevice_OpenCapture(pDevice
, deviceName
))
566 pDevice
->next
= g_pDeviceList
;
567 g_pDeviceList
= pDevice
;
570 DeviceFound
= ALC_TRUE
;
574 ProcessContext(NULL
);
578 SetALCError(ALC_INVALID_VALUE
);
584 SetALCError(ALC_OUT_OF_MEMORY
);
589 ALCAPI ALCboolean ALCAPIENTRY
alcCaptureCloseDevice(ALCdevice
*pDevice
)
591 ALCboolean bReturn
= ALC_FALSE
;
594 if(IsDevice(pDevice
) && pDevice
->IsCaptureDevice
)
596 SuspendContext(NULL
);
598 list
= &g_pDeviceList
;
599 while(*list
!= pDevice
)
600 list
= &(*list
)->next
;
602 *list
= (*list
)->next
;
605 ProcessContext(NULL
);
607 ALCdevice_CloseCapture(pDevice
);
613 SetALCError(ALC_INVALID_DEVICE
);
618 ALCAPI
void ALCAPIENTRY
alcCaptureStart(ALCdevice
*pDevice
)
620 if(IsDevice(pDevice
) && pDevice
->IsCaptureDevice
)
621 ALCdevice_StartCapture(pDevice
);
623 SetALCError(ALC_INVALID_DEVICE
);
626 ALCAPI
void ALCAPIENTRY
alcCaptureStop(ALCdevice
*pDevice
)
628 if(IsDevice(pDevice
) && pDevice
->IsCaptureDevice
)
629 ALCdevice_StopCapture(pDevice
);
631 SetALCError(ALC_INVALID_DEVICE
);
634 ALCAPI
void ALCAPIENTRY
alcCaptureSamples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCsizei lSamples
)
636 if(IsDevice(pDevice
) && pDevice
->IsCaptureDevice
)
637 ALCdevice_CaptureSamples(pDevice
, pBuffer
, lSamples
);
639 SetALCError(ALC_INVALID_DEVICE
);
645 Return last ALC generated error code
647 ALCAPI ALCenum ALCAPIENTRY
alcGetError(ALCdevice
*device
)
653 errorCode
= g_eLastContextError
;
654 g_eLastContextError
= ALC_NO_ERROR
;
664 ALCAPI ALCvoid ALCAPIENTRY
alcSuspendContext(ALCcontext
*pContext
)
666 // Not a lot happens here !
676 ALCAPI ALCvoid ALCAPIENTRY
alcProcessContext(ALCcontext
*pContext
)
678 // Not a lot happens here !
686 Returns information about the Device, and error strings
688 ALCAPI
const ALCchar
* ALCAPIENTRY
alcGetString(ALCdevice
*pDevice
,ALCenum param
)
690 const ALCchar
*value
= NULL
;
700 case ALC_INVALID_ENUM
:
701 value
= alcErrInvalidEnum
;
704 case ALC_INVALID_VALUE
:
705 value
= alcErrInvalidValue
;
708 case ALC_INVALID_DEVICE
:
709 value
= alcErrInvalidDevice
;
712 case ALC_INVALID_CONTEXT
:
713 value
= alcErrInvalidContext
;
716 case ALC_OUT_OF_MEMORY
:
717 value
= alcErrOutOfMemory
;
720 case ALC_DEFAULT_DEVICE_SPECIFIER
:
721 value
= alcDefaultDeviceSpecifier
;
724 case ALC_DEVICE_SPECIFIER
:
725 if(IsDevice(pDevice
))
726 value
= pDevice
->szDeviceName
;
728 value
= alcDeviceList
;
731 case ALC_ALL_DEVICES_SPECIFIER
:
732 value
= alcAllDeviceList
;
735 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER
:
736 value
= alcDefaultAllDeviceSpecifier
;
739 case ALC_CAPTURE_DEVICE_SPECIFIER
:
740 if(IsDevice(pDevice
))
741 value
= pDevice
->szDeviceName
;
743 value
= alcCaptureDeviceList
;
746 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
:
747 value
= alcCaptureDefaultDeviceSpecifier
;
751 value
= alcExtensionList
;
755 SetALCError(ALC_INVALID_ENUM
);
766 Returns information about the Device and the version of Open AL
768 ALCAPI ALCvoid ALCAPIENTRY
alcGetIntegerv(ALCdevice
*device
,ALCenum param
,ALsizei size
,ALCint
*data
)
772 if(IsDevice(device
) && device
->IsCaptureDevice
)
774 SuspendContext(NULL
);
779 case ALC_CAPTURE_SAMPLES
:
780 if ((size
) && (data
))
781 *data
= ALCdevice_AvailableSamples(device
);
783 SetALCError(ALC_INVALID_VALUE
);
787 SetALCError(ALC_INVALID_ENUM
);
791 ProcessContext(NULL
);
800 case ALC_MAJOR_VERSION
:
802 SetALCError(ALC_INVALID_VALUE
);
804 *data
= alcMajorVersion
;
807 case ALC_MINOR_VERSION
:
809 SetALCError(ALC_INVALID_VALUE
);
811 *data
= alcMinorVersion
;
814 case ALC_EFX_MAJOR_VERSION
:
816 SetALCError(ALC_INVALID_VALUE
);
818 *data
= alcEFXMajorVersion
;
821 case ALC_EFX_MINOR_VERSION
:
823 SetALCError(ALC_INVALID_VALUE
);
825 *data
= alcEFXMinorVersion
;
828 case ALC_MAX_AUXILIARY_SENDS
:
830 SetALCError(ALC_INVALID_VALUE
);
832 *data
= (device
?device
->NumAuxSends
:MAX_SENDS
);
835 case ALC_ATTRIBUTES_SIZE
:
836 if(!IsDevice(device
))
837 SetALCError(ALC_INVALID_DEVICE
);
839 SetALCError(ALC_INVALID_VALUE
);
844 case ALC_ALL_ATTRIBUTES
:
845 if(!IsDevice(device
))
846 SetALCError(ALC_INVALID_DEVICE
);
848 SetALCError(ALC_INVALID_VALUE
);
853 SuspendContext(NULL
);
854 data
[i
++] = ALC_FREQUENCY
;
855 data
[i
++] = device
->Frequency
;
857 data
[i
++] = ALC_REFRESH
;
858 data
[i
++] = device
->Frequency
/ device
->UpdateSize
;
860 data
[i
++] = ALC_SYNC
;
861 data
[i
++] = ALC_FALSE
;
863 data
[i
++] = ALC_MONO_SOURCES
;
864 data
[i
++] = device
->lNumMonoSources
;
866 data
[i
++] = ALC_STEREO_SOURCES
;
867 data
[i
++] = device
->lNumStereoSources
;
869 data
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
870 data
[i
++] = device
->NumAuxSends
;
873 ProcessContext(NULL
);
878 if(!IsDevice(device
))
879 SetALCError(ALC_INVALID_DEVICE
);
881 SetALCError(ALC_INVALID_VALUE
);
883 *data
= device
->Frequency
;
887 if(!IsDevice(device
))
888 SetALCError(ALC_INVALID_DEVICE
);
890 SetALCError(ALC_INVALID_VALUE
);
892 *data
= device
->Frequency
/ device
->UpdateSize
;
896 if(!IsDevice(device
))
897 SetALCError(ALC_INVALID_DEVICE
);
899 SetALCError(ALC_INVALID_VALUE
);
904 case ALC_MONO_SOURCES
:
905 if(!IsDevice(device
))
906 SetALCError(ALC_INVALID_DEVICE
);
908 SetALCError(ALC_INVALID_VALUE
);
910 *data
= device
->lNumMonoSources
;
913 case ALC_STEREO_SOURCES
:
914 if(!IsDevice(device
))
915 SetALCError(ALC_INVALID_DEVICE
);
917 SetALCError(ALC_INVALID_VALUE
);
919 *data
= device
->lNumStereoSources
;
923 SetALCError(ALC_INVALID_ENUM
);
928 SetALCError(ALC_INVALID_VALUE
);
936 alcIsExtensionPresent
938 Determines if there is support for a particular extension
940 ALCAPI ALCboolean ALCAPIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
)
942 ALCboolean bResult
= ALC_FALSE
;
951 len
= strlen(extName
);
952 ptr
= alcExtensionList
;
955 if(strncasecmp(ptr
, extName
, len
) == 0 &&
956 (ptr
[len
] == '\0' || isspace(ptr
[len
])))
961 if((ptr
=strchr(ptr
, ' ')) != NULL
)
965 } while(isspace(*ptr
));
970 SetALCError(ALC_INVALID_VALUE
);
979 Retrieves the function address for a particular extension function
981 ALCAPI ALCvoid
* ALCAPIENTRY
alcGetProcAddress(ALCdevice
*device
, const ALCchar
*funcName
)
983 ALCvoid
*pFunction
= NULL
;
990 while(alcFunctions
[i
].funcName
&&
991 strcmp(alcFunctions
[i
].funcName
,funcName
) != 0)
993 pFunction
= alcFunctions
[i
].address
;
996 SetALCError(ALC_INVALID_VALUE
);
1005 Get the value for a particular ALC Enumerated Value
1007 ALCAPI ALCenum ALCAPIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
)
1014 while ((enumeration
[i
].enumName
)&&(strcmp(enumeration
[i
].enumName
,enumName
)))
1016 val
= enumeration
[i
].value
;
1018 if(!enumeration
[i
].enumName
)
1019 SetALCError(ALC_INVALID_VALUE
);
1028 Create and attach a Context to a particular Device.
1030 ALCAPI ALCcontext
* ALCAPIENTRY
alcCreateContext(ALCdevice
*device
, const ALCint
*attrList
)
1032 ALCcontext
*ALContext
= NULL
;
1033 ALuint ulAttributeIndex
, ulRequestedStereoSources
;
1034 ALuint RequestedSends
;
1036 if(IsDevice(device
) && !device
->IsCaptureDevice
)
1038 // Reset Context Last Error code
1039 g_eLastContextError
= ALC_NO_ERROR
;
1041 // Current implementation only allows one Context per Device
1042 if(!device
->Context
)
1044 ALContext
= calloc(1, sizeof(ALCcontext
));
1047 SetALCError(ALC_OUT_OF_MEMORY
);
1051 SuspendContext(NULL
);
1053 ALContext
->Device
= device
;
1054 InitContext(ALContext
);
1056 device
->Context
= ALContext
;
1058 ALContext
->next
= g_pContextList
;
1059 g_pContextList
= ALContext
;
1062 // Check for attributes
1065 ALCuint freq
= device
->Frequency
;
1066 ALCint numMono
= device
->lNumMonoSources
;
1067 ALCint numStereo
= device
->lNumStereoSources
;
1068 ALCuint numSends
= device
->NumAuxSends
;
1070 ulAttributeIndex
= 0;
1071 while ((ulAttributeIndex
< 10) && (attrList
[ulAttributeIndex
]))
1073 if(attrList
[ulAttributeIndex
] == ALC_FREQUENCY
)
1075 freq
= attrList
[ulAttributeIndex
+ 1];
1077 freq
= device
->Frequency
;
1080 if(attrList
[ulAttributeIndex
] == ALC_STEREO_SOURCES
)
1082 ulRequestedStereoSources
= attrList
[ulAttributeIndex
+ 1];
1084 if (ulRequestedStereoSources
> device
->MaxNoOfSources
)
1085 ulRequestedStereoSources
= device
->MaxNoOfSources
;
1087 numStereo
= ulRequestedStereoSources
;
1088 numMono
= device
->MaxNoOfSources
- numStereo
;
1091 if(attrList
[ulAttributeIndex
] == ALC_MAX_AUXILIARY_SENDS
)
1093 RequestedSends
= attrList
[ulAttributeIndex
+ 1];
1095 if(RequestedSends
> device
->NumAuxSends
)
1096 RequestedSends
= device
->NumAuxSends
;
1098 numSends
= RequestedSends
;
1101 ulAttributeIndex
+= 2;
1104 device
->Frequency
= GetConfigValueInt(NULL
, "frequency", freq
);
1105 device
->lNumMonoSources
= numMono
;
1106 device
->lNumStereoSources
= numStereo
;
1107 device
->NumAuxSends
= numSends
;
1110 if(ALCdevice_StartContext(device
, ALContext
) == ALC_FALSE
)
1112 alcDestroyContext(ALContext
);
1114 SetALCError(ALC_INVALID_VALUE
);
1117 ALContext
->Frequency
= device
->Frequency
;
1119 ProcessContext(NULL
);
1123 SetALCError(ALC_INVALID_VALUE
);
1128 SetALCError(ALC_INVALID_DEVICE
);
1139 ALCAPI ALCvoid ALCAPIENTRY
alcDestroyContext(ALCcontext
*context
)
1145 if (IsContext(context
))
1147 ALCdevice_StopContext(context
->Device
, context
);
1150 SuspendContext(context
);
1152 ReleaseALSources(context
);
1153 ReleaseALAuxiliaryEffectSlots(context
);
1155 context
->Device
->Context
= NULL
;
1157 list
= &g_pContextList
;
1158 while(*list
!= context
)
1159 list
= &(*list
)->next
;
1161 *list
= (*list
)->next
;
1165 ProcessContext(context
);
1167 ExitContext(context
);
1169 // Free memory (MUST do this after ProcessContext)
1170 memset(context
, 0, sizeof(ALCcontext
));
1174 SetALCError(ALC_INVALID_CONTEXT
);
1179 alcGetCurrentContext
1181 Returns the currently active Context
1183 ALCAPI ALCcontext
* ALCAPIENTRY
alcGetCurrentContext(ALCvoid
)
1185 ALCcontext
*pContext
= NULL
;
1189 SuspendContext(NULL
);
1191 pContext
= g_pContextList
;
1192 while ((pContext
) && (!pContext
->InUse
))
1193 pContext
= pContext
->next
;
1195 ProcessContext(NULL
);
1202 alcGetContextsDevice
1204 Returns the Device that a particular Context is attached to
1206 ALCAPI ALCdevice
* ALCAPIENTRY
alcGetContextsDevice(ALCcontext
*pContext
)
1208 ALCdevice
*pDevice
= NULL
;
1212 SuspendContext(NULL
);
1213 if (IsContext(pContext
))
1214 pDevice
= pContext
->Device
;
1216 SetALCError(ALC_INVALID_CONTEXT
);
1217 ProcessContext(NULL
);
1224 alcMakeContextCurrent
1226 Makes the given Context the active Context
1228 ALCAPI ALCboolean ALCAPIENTRY
alcMakeContextCurrent(ALCcontext
*context
)
1230 ALCcontext
*ALContext
;
1231 ALboolean bReturn
= AL_TRUE
;
1235 SuspendContext(NULL
);
1237 // context must be a valid Context or NULL
1238 if ((IsContext(context
)) || (context
== NULL
))
1240 if ((ALContext
=alcGetCurrentContext()))
1242 SuspendContext(ALContext
);
1243 ALContext
->InUse
=AL_FALSE
;
1244 ProcessContext(ALContext
);
1247 if ((ALContext
=context
) && (ALContext
->Device
))
1249 SuspendContext(ALContext
);
1250 ALContext
->InUse
=AL_TRUE
;
1251 ProcessContext(ALContext
);
1256 SetALCError(ALC_INVALID_CONTEXT
);
1260 ProcessContext(NULL
);
1269 Open the Device specified.
1271 ALCAPI ALCdevice
* ALCAPIENTRY
alcOpenDevice(const ALCchar
*deviceName
)
1273 ALboolean bDeviceFound
= AL_FALSE
;
1279 if(deviceName
&& !deviceName
[0])
1282 device
= malloc(sizeof(ALCdevice
));
1287 //Initialise device structure
1288 memset(device
, 0, sizeof(ALCdevice
));
1291 device
->IsCaptureDevice
= AL_FALSE
;
1294 device
->Frequency
= GetConfigValueInt(NULL
, "frequency", SWMIXER_OUTPUT_RATE
);
1295 if(device
->Frequency
== 0)
1296 device
->Frequency
= SWMIXER_OUTPUT_RATE
;
1298 fmt
= GetConfigValue(NULL
, "format", "AL_FORMAT_STEREO16");
1300 device
->Format
= alGetEnumValue(fmt
);
1302 if(!aluChannelsFromFormat(device
->Format
))
1303 device
->Format
= AL_FORMAT_STEREO16
;
1305 device
->BufferSize
= GetConfigValueInt(NULL
, "refresh", 4096);
1306 if((ALint
)device
->BufferSize
<= 0)
1307 device
->BufferSize
= 4096;
1309 device
->MaxNoOfSources
= GetConfigValueInt(NULL
, "sources", 256);
1310 if((ALint
)device
->MaxNoOfSources
<= 0)
1311 device
->MaxNoOfSources
= 256;
1313 device
->AuxiliaryEffectSlotMax
= GetConfigValueInt(NULL
, "slots", 4);
1314 if((ALint
)device
->AuxiliaryEffectSlotMax
<= 0)
1315 device
->AuxiliaryEffectSlotMax
= 4;
1317 device
->lNumStereoSources
= 1;
1318 device
->lNumMonoSources
= device
->MaxNoOfSources
- device
->lNumStereoSources
;
1320 device
->NumAuxSends
= GetConfigValueInt(NULL
, "sends", MAX_SENDS
);
1321 if(device
->NumAuxSends
> MAX_SENDS
)
1322 device
->NumAuxSends
= MAX_SENDS
;
1324 // Find a playback device to open
1325 SuspendContext(NULL
);
1326 for(i
= 0;BackendList
[i
].Init
;i
++)
1328 device
->Funcs
= &BackendList
[i
].Funcs
;
1329 if(ALCdevice_OpenPlayback(device
, deviceName
))
1331 device
->next
= g_pDeviceList
;
1332 g_pDeviceList
= device
;
1335 bDeviceFound
= AL_TRUE
;
1339 ProcessContext(NULL
);
1343 // No suitable output device found
1344 SetALCError(ALC_INVALID_VALUE
);
1350 SetALCError(ALC_OUT_OF_MEMORY
);
1359 Close the specified Device
1361 ALCAPI ALCboolean ALCAPIENTRY
alcCloseDevice(ALCdevice
*pDevice
)
1363 ALCboolean bReturn
= ALC_FALSE
;
1366 if(IsDevice(pDevice
) && !pDevice
->IsCaptureDevice
)
1368 SuspendContext(NULL
);
1370 list
= &g_pDeviceList
;
1371 while(*list
!= pDevice
)
1372 list
= &(*list
)->next
;
1374 *list
= (*list
)->next
;
1377 ProcessContext(NULL
);
1379 if(pDevice
->Context
)
1382 AL_PRINT("alcCloseDevice(): destroying 1 Context\n");
1384 alcDestroyContext(pDevice
->Context
);
1386 ALCdevice_ClosePlayback(pDevice
);
1388 if(pDevice
->BufferCount
> 0)
1391 AL_PRINT("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice
->BufferCount
);
1393 ReleaseALBuffers(pDevice
);
1395 if(pDevice
->EffectCount
> 0)
1398 AL_PRINT("alcCloseDevice(): deleting %d Effect(s)\n", pDevice
->EffectCount
);
1400 ReleaseALEffects(pDevice
);
1402 if(pDevice
->FilterCount
> 0)
1405 AL_PRINT("alcCloseDevice(): deleting %d Filter(s)\n", pDevice
->FilterCount
);
1407 ReleaseALFilters(pDevice
);
1410 //Release device structure
1411 memset(pDevice
, 0, sizeof(ALCdevice
));
1417 SetALCError(ALC_INVALID_DEVICE
);
1423 ALCvoid
ReleaseALC(ALCvoid
)
1426 if(g_ulDeviceCount
> 0)
1427 AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount
, (g_ulDeviceCount
>1)?"s":"");
1430 while(g_pDeviceList
)
1432 if(g_pDeviceList
->IsCaptureDevice
)
1433 alcCaptureCloseDevice(g_pDeviceList
);
1435 alcCloseDevice(g_pDeviceList
);
1439 ///////////////////////////////////////////////////////