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
*ALEffectSlot
, ALeffect
*effect
);
37 DECL_VERIFIER(EffectSlot
, ALeffectslot
, effectslot
)
38 DECL_VERIFIER(Effect
, ALeffect
, effect
)
40 ALvoid AL_APIENTRY
alGenAuxiliaryEffectSlots(ALsizei n
, ALuint
*effectslots
)
45 Context
= GetContextSuspended();
50 ALCdevice
*Device
= Context
->Device
;
52 if(Context
->EffectSlotCount
+n
<= Device
->AuxiliaryEffectSlotMax
)
54 // Check that enough memory has been allocted in the 'effectslots' array for n Effect Slots
55 if (!IsBadWritePtr((void*)effectslots
, n
* sizeof(ALuint
)))
58 ALeffectslot
**list
= &Context
->EffectSlotList
;
60 list
= &(*list
)->next
;
65 *list
= calloc(1, sizeof(ALeffectslot
));
66 if(!(*list
) || !((*list
)->EffectState
=NoneCreate()))
68 // We must have run out or memory
69 free(*list
); *list
= NULL
;
72 ALeffectslot
*temp
= end
->next
;
73 end
->next
= temp
->next
;
75 ALEffect_Destroy(temp
->EffectState
);
76 ALTHUNK_REMOVEENTRY(temp
->effectslot
);
77 Context
->EffectSlotCount
--;
80 alSetError(Context
, AL_OUT_OF_MEMORY
);
85 (*list
)->AuxSendAuto
= AL_TRUE
;
86 for(j
= 0;j
< BUFFERSIZE
;j
++)
87 (*list
)->WetBuffer
[j
] = 0.0f
;
88 (*list
)->refcount
= 0;
90 effectslots
[i
] = (ALuint
)ALTHUNK_ADDENTRY(*list
);
91 (*list
)->effectslot
= effectslots
[i
];
93 Context
->EffectSlotCount
++;
96 list
= &(*list
)->next
;
101 alSetError(Context
, AL_INVALID_OPERATION
);
104 ProcessContext(Context
);
107 ALvoid AL_APIENTRY
alDeleteAuxiliaryEffectSlots(ALsizei n
, ALuint
*effectslots
)
110 ALeffectslot
*ALAuxiliaryEffectSlot
;
113 Context
= GetContextSuspended();
118 // Check that all effectslots are valid
119 for (i
= 0; i
< n
; i
++)
121 if((ALAuxiliaryEffectSlot
=VerifyEffectSlot(Context
->EffectSlotList
, effectslots
[i
])) == NULL
)
123 alSetError(Context
, AL_INVALID_NAME
);
128 if(ALAuxiliaryEffectSlot
->refcount
> 0)
130 alSetError(Context
, AL_INVALID_NAME
);
138 // All effectslots are valid
139 for (i
= 0; i
< n
; i
++)
141 // Recheck that the effectslot is valid, because there could be duplicated names
142 if((ALAuxiliaryEffectSlot
=VerifyEffectSlot(Context
->EffectSlotList
, effectslots
[i
])) != NULL
)
146 // Remove Source from list of Sources
147 list
= &Context
->EffectSlotList
;
148 while(*list
&& *list
!= ALAuxiliaryEffectSlot
)
149 list
= &(*list
)->next
;
152 *list
= (*list
)->next
;
153 ALTHUNK_REMOVEENTRY(ALAuxiliaryEffectSlot
->effectslot
);
155 if(ALAuxiliaryEffectSlot
->EffectState
)
156 ALEffect_Destroy(ALAuxiliaryEffectSlot
->EffectState
);
158 memset(ALAuxiliaryEffectSlot
, 0, sizeof(ALeffectslot
));
159 free(ALAuxiliaryEffectSlot
);
161 Context
->EffectSlotCount
--;
167 alSetError(Context
, AL_INVALID_VALUE
);
169 ProcessContext(Context
);
172 ALboolean AL_APIENTRY
alIsAuxiliaryEffectSlot(ALuint effectslot
)
177 Context
= GetContextSuspended();
178 if(!Context
) return AL_FALSE
;
180 result
= (VerifyEffectSlot(Context
->EffectSlotList
, effectslot
) ?
183 ProcessContext(Context
);
188 ALvoid AL_APIENTRY
alAuxiliaryEffectSloti(ALuint effectslot
, ALenum param
, ALint iValue
)
191 ALboolean updateSources
= AL_FALSE
;
192 ALeffectslot
*ALEffectSlot
;
194 Context
= GetContextSuspended();
197 if((ALEffectSlot
=VerifyEffectSlot(Context
->EffectSlotList
, effectslot
)) != NULL
)
201 case AL_EFFECTSLOT_EFFECT
: {
202 ALeffect
*effect
= NULL
;
205 (effect
=VerifyEffect(Context
->Device
->EffectList
, iValue
)) != NULL
)
207 InitializeEffect(Context
, ALEffectSlot
, effect
);
208 updateSources
= AL_TRUE
;
211 alSetError(Context
, AL_INVALID_VALUE
);
214 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO
:
215 if(iValue
== AL_TRUE
|| iValue
== AL_FALSE
)
217 ALEffectSlot
->AuxSendAuto
= iValue
;
218 updateSources
= AL_TRUE
;
221 alSetError(Context
, AL_INVALID_VALUE
);
225 alSetError(Context
, AL_INVALID_ENUM
);
230 alSetError(Context
, AL_INVALID_NAME
);
232 // Force updating the sources that use this slot, since it affects the
233 // sending parameters
236 ALsource
*source
= Context
->SourceList
;
240 for(i
= 0;i
< MAX_SENDS
;i
++)
242 if(!source
->Send
[i
].Slot
||
243 source
->Send
[i
].Slot
->effectslot
!= effectslot
)
245 source
->NeedsUpdate
= AL_TRUE
;
248 source
= source
->next
;
252 ProcessContext(Context
);
255 ALvoid AL_APIENTRY
alAuxiliaryEffectSlotiv(ALuint effectslot
, ALenum param
, ALint
*piValues
)
259 Context
= GetContextSuspended();
262 if(VerifyEffectSlot(Context
->EffectSlotList
, effectslot
) != NULL
)
266 case AL_EFFECTSLOT_EFFECT
:
267 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO
:
268 alAuxiliaryEffectSloti(effectslot
, param
, piValues
[0]);
272 alSetError(Context
, AL_INVALID_ENUM
);
277 alSetError(Context
, AL_INVALID_NAME
);
279 ProcessContext(Context
);
282 ALvoid AL_APIENTRY
alAuxiliaryEffectSlotf(ALuint effectslot
, ALenum param
, ALfloat flValue
)
285 ALeffectslot
*ALEffectSlot
;
287 Context
= GetContextSuspended();
290 if((ALEffectSlot
=VerifyEffectSlot(Context
->EffectSlotList
, effectslot
)) != NULL
)
294 case AL_EFFECTSLOT_GAIN
:
295 if(flValue
>= 0.0f
&& flValue
<= 1.0f
)
296 ALEffectSlot
->Gain
= flValue
;
298 alSetError(Context
, AL_INVALID_VALUE
);
302 alSetError(Context
, AL_INVALID_ENUM
);
307 alSetError(Context
, AL_INVALID_NAME
);
309 ProcessContext(Context
);
312 ALvoid AL_APIENTRY
alAuxiliaryEffectSlotfv(ALuint effectslot
, ALenum param
, ALfloat
*pflValues
)
316 Context
= GetContextSuspended();
319 if(VerifyEffectSlot(Context
->EffectSlotList
, effectslot
) != NULL
)
323 case AL_EFFECTSLOT_GAIN
:
324 alAuxiliaryEffectSlotf(effectslot
, param
, pflValues
[0]);
328 alSetError(Context
, AL_INVALID_ENUM
);
333 alSetError(Context
, AL_INVALID_NAME
);
335 ProcessContext(Context
);
338 ALvoid AL_APIENTRY
alGetAuxiliaryEffectSloti(ALuint effectslot
, ALenum param
, ALint
*piValue
)
341 ALeffectslot
*ALEffectSlot
;
343 Context
= GetContextSuspended();
346 if((ALEffectSlot
=VerifyEffectSlot(Context
->EffectSlotList
, effectslot
)) != NULL
)
350 case AL_EFFECTSLOT_EFFECT
:
351 *piValue
= ALEffectSlot
->effect
.effect
;
354 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO
:
355 *piValue
= ALEffectSlot
->AuxSendAuto
;
359 alSetError(Context
, AL_INVALID_ENUM
);
364 alSetError(Context
, AL_INVALID_NAME
);
366 ProcessContext(Context
);
369 ALvoid AL_APIENTRY
alGetAuxiliaryEffectSlotiv(ALuint effectslot
, ALenum param
, ALint
*piValues
)
373 Context
= GetContextSuspended();
376 if(VerifyEffectSlot(Context
->EffectSlotList
, effectslot
) != NULL
)
380 case AL_EFFECTSLOT_EFFECT
:
381 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO
:
382 alGetAuxiliaryEffectSloti(effectslot
, param
, piValues
);
386 alSetError(Context
, AL_INVALID_ENUM
);
391 alSetError(Context
, AL_INVALID_NAME
);
393 ProcessContext(Context
);
396 ALvoid AL_APIENTRY
alGetAuxiliaryEffectSlotf(ALuint effectslot
, ALenum param
, ALfloat
*pflValue
)
399 ALeffectslot
*ALEffectSlot
;
401 Context
= GetContextSuspended();
404 if((ALEffectSlot
=VerifyEffectSlot(Context
->EffectSlotList
, effectslot
)) != NULL
)
408 case AL_EFFECTSLOT_GAIN
:
409 *pflValue
= ALEffectSlot
->Gain
;
413 alSetError(Context
, AL_INVALID_ENUM
);
418 alSetError(Context
, AL_INVALID_NAME
);
420 ProcessContext(Context
);
423 ALvoid AL_APIENTRY
alGetAuxiliaryEffectSlotfv(ALuint effectslot
, ALenum param
, ALfloat
*pflValues
)
427 Context
= GetContextSuspended();
430 if(VerifyEffectSlot(Context
->EffectSlotList
, effectslot
) != NULL
)
434 case AL_EFFECTSLOT_GAIN
:
435 alGetAuxiliaryEffectSlotf(effectslot
, param
, pflValues
);
439 alSetError(Context
, AL_INVALID_ENUM
);
444 alSetError(Context
, AL_INVALID_NAME
);
446 ProcessContext(Context
);
450 static ALvoid
NoneDestroy(ALeffectState
*State
)
452 static ALboolean
NoneDeviceUpdate(ALeffectState
*State
, ALCdevice
*Device
)
458 static ALvoid
NoneUpdate(ALeffectState
*State
, ALCcontext
*Context
, const ALeffect
*Effect
)
464 static ALvoid
NoneProcess(ALeffectState
*State
, const ALeffectslot
*Slot
, ALuint SamplesToDo
, const ALfloat
*SamplesIn
, ALfloat (*SamplesOut
)[OUTPUTCHANNELS
])
472 ALeffectState
*NoneCreate(void)
474 ALeffectState
*state
;
476 state
= calloc(1, sizeof(*state
));
480 state
->Destroy
= NoneDestroy
;
481 state
->DeviceUpdate
= NoneDeviceUpdate
;
482 state
->Update
= NoneUpdate
;
483 state
->Process
= NoneProcess
;
488 static ALvoid
InitializeEffect(ALCcontext
*Context
, ALeffectslot
*ALEffectSlot
, ALeffect
*effect
)
490 if(ALEffectSlot
->effect
.type
!= (effect
?effect
->type
:AL_EFFECT_NULL
))
492 ALeffectState
*NewState
= NULL
;
493 if(!effect
|| effect
->type
== AL_EFFECT_NULL
)
494 NewState
= NoneCreate();
495 else if(effect
->type
== AL_EFFECT_EAXREVERB
)
496 NewState
= EAXVerbCreate();
497 else if(effect
->type
== AL_EFFECT_REVERB
)
498 NewState
= VerbCreate();
499 else if(effect
->type
== AL_EFFECT_ECHO
)
500 NewState
= EchoCreate();
501 /* No new state? An error occured.. */
502 if(NewState
== NULL
||
503 ALEffect_DeviceUpdate(NewState
, Context
->Device
) == AL_FALSE
)
506 ALEffect_Destroy(NewState
);
507 alSetError(Context
, AL_OUT_OF_MEMORY
);
510 if(ALEffectSlot
->EffectState
)
511 ALEffect_Destroy(ALEffectSlot
->EffectState
);
512 ALEffectSlot
->EffectState
= NewState
;
515 memset(&ALEffectSlot
->effect
, 0, sizeof(ALEffectSlot
->effect
));
517 memcpy(&ALEffectSlot
->effect
, effect
, sizeof(*effect
));
518 ALEffect_Update(ALEffectSlot
->EffectState
, Context
, effect
);
522 ALvoid
ReleaseALAuxiliaryEffectSlots(ALCcontext
*Context
)
524 while(Context
->EffectSlotList
)
526 ALeffectslot
*temp
= Context
->EffectSlotList
;
527 Context
->EffectSlotList
= temp
->next
;
529 // Release effectslot structure
530 ALEffect_Destroy(temp
->EffectState
);
532 ALTHUNK_REMOVEENTRY(temp
->effectslot
);
533 memset(temp
, 0, sizeof(ALeffectslot
));
536 Context
->EffectSlotCount
= 0;