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
29 #include "alAuxEffectSlot.h"
35 static ALvoid
InitializeEffect(ALCcontext
*Context
, ALeffectslot
*EffectSlot
, ALeffect
*effect
);
37 DECL_VERIFIER(Effect
, ALeffect
, effect
)
39 #define LookupEffectSlot(m, k) ((ALeffectslot*)LookupUIntMapKey(&(m), (k)))
41 AL_API ALvoid AL_APIENTRY
alGenAuxiliaryEffectSlots(ALsizei n
, ALuint
*effectslots
)
46 Context
= GetContextSuspended();
51 ALCdevice
*Device
= Context
->Device
;
53 if(Context
->EffectSlotMap
.size
+n
<= (ALsizei
)Device
->AuxiliaryEffectSlotMax
)
55 // Check that enough memory has been allocted in the 'effectslots' array for n Effect Slots
56 if(!IsBadWritePtr((void*)effectslots
, n
* sizeof(ALuint
)))
62 ALeffectslot
*slot
= calloc(1, sizeof(ALeffectslot
));
63 if(!slot
|| !(slot
->EffectState
=NoneCreate()))
66 // We must have run out or memory
67 alSetError(Context
, AL_OUT_OF_MEMORY
);
68 alDeleteAuxiliaryEffectSlots(i
, effectslots
);
72 slot
->effectslot
= (ALuint
)ALTHUNK_ADDENTRY(slot
);
73 err
= InsertUIntMapEntry(&Context
->EffectSlotMap
,
74 slot
->effectslot
, slot
);
75 if(err
!= AL_NO_ERROR
)
77 ALTHUNK_REMOVEENTRY(slot
->effectslot
);
78 ALEffect_Destroy(slot
->EffectState
);
81 alSetError(Context
, err
);
82 alDeleteAuxiliaryEffectSlots(i
, effectslots
);
86 effectslots
[i
++] = slot
->effectslot
;
89 slot
->AuxSendAuto
= AL_TRUE
;
90 for(j
= 0;j
< BUFFERSIZE
;j
++)
91 slot
->WetBuffer
[j
] = 0.0f
;
97 alSetError(Context
, AL_INVALID_OPERATION
);
100 ProcessContext(Context
);
103 AL_API ALvoid AL_APIENTRY
alDeleteAuxiliaryEffectSlots(ALsizei n
, ALuint
*effectslots
)
106 ALeffectslot
*EffectSlot
;
109 Context
= GetContextSuspended();
114 // Check that all effectslots are valid
115 for (i
= 0; i
< n
; i
++)
117 if((EffectSlot
=LookupEffectSlot(Context
->EffectSlotMap
, effectslots
[i
])) == NULL
)
119 alSetError(Context
, AL_INVALID_NAME
);
124 if(EffectSlot
->refcount
> 0)
126 alSetError(Context
, AL_INVALID_NAME
);
134 // All effectslots are valid
135 for (i
= 0; i
< n
; i
++)
137 // Recheck that the effectslot is valid, because there could be duplicated names
138 if((EffectSlot
=LookupEffectSlot(Context
->EffectSlotMap
, effectslots
[i
])) != NULL
)
140 ALEffect_Destroy(EffectSlot
->EffectState
);
142 RemoveUIntMapKey(&Context
->EffectSlotMap
, EffectSlot
->effectslot
);
143 ALTHUNK_REMOVEENTRY(EffectSlot
->effectslot
);
145 memset(EffectSlot
, 0, sizeof(ALeffectslot
));
152 alSetError(Context
, AL_INVALID_VALUE
);
154 ProcessContext(Context
);
157 AL_API ALboolean AL_APIENTRY
alIsAuxiliaryEffectSlot(ALuint effectslot
)
162 Context
= GetContextSuspended();
163 if(!Context
) return AL_FALSE
;
165 result
= (LookupEffectSlot(Context
->EffectSlotMap
, effectslot
) ?
168 ProcessContext(Context
);
173 AL_API ALvoid AL_APIENTRY
alAuxiliaryEffectSloti(ALuint effectslot
, ALenum param
, ALint iValue
)
176 ALboolean updateSources
= AL_FALSE
;
177 ALeffectslot
*EffectSlot
;
179 Context
= GetContextSuspended();
182 if((EffectSlot
=LookupEffectSlot(Context
->EffectSlotMap
, effectslot
)) != NULL
)
186 case AL_EFFECTSLOT_EFFECT
: {
187 ALeffect
*effect
= NULL
;
190 (effect
=VerifyEffect(Context
->Device
->EffectList
, iValue
)) != NULL
)
192 InitializeEffect(Context
, EffectSlot
, effect
);
193 updateSources
= AL_TRUE
;
196 alSetError(Context
, AL_INVALID_VALUE
);
199 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO
:
200 if(iValue
== AL_TRUE
|| iValue
== AL_FALSE
)
202 EffectSlot
->AuxSendAuto
= iValue
;
203 updateSources
= AL_TRUE
;
206 alSetError(Context
, AL_INVALID_VALUE
);
210 alSetError(Context
, AL_INVALID_ENUM
);
215 alSetError(Context
, AL_INVALID_NAME
);
217 // Force updating the sources that use this slot, since it affects the
218 // sending parameters
222 for(pos
= 0;pos
< Context
->SourceMap
.size
;pos
++)
224 ALsource
*source
= Context
->SourceMap
.array
[pos
].value
;
226 for(i
= 0;i
< MAX_SENDS
;i
++)
228 if(!source
->Send
[i
].Slot
||
229 source
->Send
[i
].Slot
->effectslot
!= effectslot
)
231 source
->NeedsUpdate
= AL_TRUE
;
237 ProcessContext(Context
);
240 AL_API ALvoid AL_APIENTRY
alAuxiliaryEffectSlotiv(ALuint effectslot
, ALenum param
, ALint
*piValues
)
244 Context
= GetContextSuspended();
247 if(LookupEffectSlot(Context
->EffectSlotMap
, effectslot
) != NULL
)
251 case AL_EFFECTSLOT_EFFECT
:
252 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO
:
253 alAuxiliaryEffectSloti(effectslot
, param
, piValues
[0]);
257 alSetError(Context
, AL_INVALID_ENUM
);
262 alSetError(Context
, AL_INVALID_NAME
);
264 ProcessContext(Context
);
267 AL_API ALvoid AL_APIENTRY
alAuxiliaryEffectSlotf(ALuint effectslot
, ALenum param
, ALfloat flValue
)
270 ALeffectslot
*EffectSlot
;
272 Context
= GetContextSuspended();
275 if((EffectSlot
=LookupEffectSlot(Context
->EffectSlotMap
, effectslot
)) != NULL
)
279 case AL_EFFECTSLOT_GAIN
:
280 if(flValue
>= 0.0f
&& flValue
<= 1.0f
)
281 EffectSlot
->Gain
= flValue
;
283 alSetError(Context
, AL_INVALID_VALUE
);
287 alSetError(Context
, AL_INVALID_ENUM
);
292 alSetError(Context
, AL_INVALID_NAME
);
294 ProcessContext(Context
);
297 AL_API ALvoid AL_APIENTRY
alAuxiliaryEffectSlotfv(ALuint effectslot
, ALenum param
, ALfloat
*pflValues
)
301 Context
= GetContextSuspended();
304 if(LookupEffectSlot(Context
->EffectSlotMap
, effectslot
) != NULL
)
308 case AL_EFFECTSLOT_GAIN
:
309 alAuxiliaryEffectSlotf(effectslot
, param
, pflValues
[0]);
313 alSetError(Context
, AL_INVALID_ENUM
);
318 alSetError(Context
, AL_INVALID_NAME
);
320 ProcessContext(Context
);
323 AL_API ALvoid AL_APIENTRY
alGetAuxiliaryEffectSloti(ALuint effectslot
, ALenum param
, ALint
*piValue
)
326 ALeffectslot
*EffectSlot
;
328 Context
= GetContextSuspended();
331 if((EffectSlot
=LookupEffectSlot(Context
->EffectSlotMap
, effectslot
)) != NULL
)
335 case AL_EFFECTSLOT_EFFECT
:
336 *piValue
= EffectSlot
->effect
.effect
;
339 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO
:
340 *piValue
= EffectSlot
->AuxSendAuto
;
344 alSetError(Context
, AL_INVALID_ENUM
);
349 alSetError(Context
, AL_INVALID_NAME
);
351 ProcessContext(Context
);
354 AL_API ALvoid AL_APIENTRY
alGetAuxiliaryEffectSlotiv(ALuint effectslot
, ALenum param
, ALint
*piValues
)
358 Context
= GetContextSuspended();
361 if(LookupEffectSlot(Context
->EffectSlotMap
, effectslot
) != NULL
)
365 case AL_EFFECTSLOT_EFFECT
:
366 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO
:
367 alGetAuxiliaryEffectSloti(effectslot
, param
, piValues
);
371 alSetError(Context
, AL_INVALID_ENUM
);
376 alSetError(Context
, AL_INVALID_NAME
);
378 ProcessContext(Context
);
381 AL_API ALvoid AL_APIENTRY
alGetAuxiliaryEffectSlotf(ALuint effectslot
, ALenum param
, ALfloat
*pflValue
)
384 ALeffectslot
*EffectSlot
;
386 Context
= GetContextSuspended();
389 if((EffectSlot
=LookupEffectSlot(Context
->EffectSlotMap
, effectslot
)) != NULL
)
393 case AL_EFFECTSLOT_GAIN
:
394 *pflValue
= EffectSlot
->Gain
;
398 alSetError(Context
, AL_INVALID_ENUM
);
403 alSetError(Context
, AL_INVALID_NAME
);
405 ProcessContext(Context
);
408 AL_API ALvoid AL_APIENTRY
alGetAuxiliaryEffectSlotfv(ALuint effectslot
, ALenum param
, ALfloat
*pflValues
)
412 Context
= GetContextSuspended();
415 if(LookupEffectSlot(Context
->EffectSlotMap
, effectslot
) != NULL
)
419 case AL_EFFECTSLOT_GAIN
:
420 alGetAuxiliaryEffectSlotf(effectslot
, param
, pflValues
);
424 alSetError(Context
, AL_INVALID_ENUM
);
429 alSetError(Context
, AL_INVALID_NAME
);
431 ProcessContext(Context
);
435 static ALvoid
NoneDestroy(ALeffectState
*State
)
437 static ALboolean
NoneDeviceUpdate(ALeffectState
*State
, ALCdevice
*Device
)
443 static ALvoid
NoneUpdate(ALeffectState
*State
, ALCcontext
*Context
, const ALeffect
*Effect
)
449 static ALvoid
NoneProcess(ALeffectState
*State
, const ALeffectslot
*Slot
, ALuint SamplesToDo
, const ALfloat
*SamplesIn
, ALfloat (*SamplesOut
)[OUTPUTCHANNELS
])
457 ALeffectState
*NoneCreate(void)
459 ALeffectState
*state
;
461 state
= calloc(1, sizeof(*state
));
465 state
->Destroy
= NoneDestroy
;
466 state
->DeviceUpdate
= NoneDeviceUpdate
;
467 state
->Update
= NoneUpdate
;
468 state
->Process
= NoneProcess
;
473 static ALvoid
InitializeEffect(ALCcontext
*Context
, ALeffectslot
*EffectSlot
, ALeffect
*effect
)
475 if(EffectSlot
->effect
.type
!= (effect
?effect
->type
:AL_EFFECT_NULL
))
477 ALeffectState
*NewState
= NULL
;
478 if(!effect
|| effect
->type
== AL_EFFECT_NULL
)
479 NewState
= NoneCreate();
480 else if(effect
->type
== AL_EFFECT_EAXREVERB
)
481 NewState
= EAXVerbCreate();
482 else if(effect
->type
== AL_EFFECT_REVERB
)
483 NewState
= VerbCreate();
484 else if(effect
->type
== AL_EFFECT_ECHO
)
485 NewState
= EchoCreate();
486 else if(effect
->type
== AL_EFFECT_RING_MODULATOR
)
487 NewState
= ModulatorCreate();
488 /* No new state? An error occured.. */
489 if(NewState
== NULL
||
490 ALEffect_DeviceUpdate(NewState
, Context
->Device
) == AL_FALSE
)
493 ALEffect_Destroy(NewState
);
494 alSetError(Context
, AL_OUT_OF_MEMORY
);
497 if(EffectSlot
->EffectState
)
498 ALEffect_Destroy(EffectSlot
->EffectState
);
499 EffectSlot
->EffectState
= NewState
;
502 memset(&EffectSlot
->effect
, 0, sizeof(EffectSlot
->effect
));
504 memcpy(&EffectSlot
->effect
, effect
, sizeof(*effect
));
505 ALEffect_Update(EffectSlot
->EffectState
, Context
, effect
);
509 ALvoid
ReleaseALAuxiliaryEffectSlots(ALCcontext
*Context
)
512 for(pos
= 0;pos
< Context
->EffectSlotMap
.size
;pos
++)
514 ALeffectslot
*temp
= Context
->EffectSlotMap
.array
[pos
].value
;
515 Context
->EffectSlotMap
.array
[pos
].value
= NULL
;
517 // Release effectslot structure
518 ALEffect_Destroy(temp
->EffectState
);
520 ALTHUNK_REMOVEENTRY(temp
->effectslot
);
521 memset(temp
, 0, sizeof(ALeffectslot
));