Add click removal for wet sends
[openal-soft.git] / OpenAL32 / alAuxEffectSlot.c
bloba7ea0e326a301698bbf2b73f4417b5086ceae97d
1 /**
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 #include "config.h"
23 #include <stdlib.h>
24 #include <math.h>
26 #include "AL/al.h"
27 #include "AL/alc.h"
28 #include "alMain.h"
29 #include "alAuxEffectSlot.h"
30 #include "alThunk.h"
31 #include "alError.h"
32 #include "alSource.h"
35 static ALvoid InitializeEffect(ALCcontext *Context, ALeffectslot *EffectSlot, ALeffect *effect);
37 #define LookupEffectSlot(m, k) ((ALeffectslot*)LookupUIntMapKey(&(m), (k)))
38 #define LookupEffect(m, k) ((ALeffect*)LookupUIntMapKey(&(m), (k)))
40 AL_API ALvoid AL_APIENTRY alGenAuxiliaryEffectSlots(ALsizei n, ALuint *effectslots)
42 ALCcontext *Context;
43 ALsizei i=0, j;
45 Context = GetContextSuspended();
46 if(!Context) return;
48 if(n > 0)
50 ALCdevice *Device = Context->Device;
52 if(Context->EffectSlotMap.size+n <= (ALsizei)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)))
57 ALenum err;
59 while(i < n)
61 ALeffectslot *slot = calloc(1, sizeof(ALeffectslot));
62 if(!slot || !(slot->EffectState=NoneCreate()))
64 free(slot);
65 // We must have run out or memory
66 alSetError(Context, AL_OUT_OF_MEMORY);
67 alDeleteAuxiliaryEffectSlots(i, effectslots);
68 break;
71 slot->effectslot = (ALuint)ALTHUNK_ADDENTRY(slot);
72 err = InsertUIntMapEntry(&Context->EffectSlotMap,
73 slot->effectslot, slot);
74 if(err != AL_NO_ERROR)
76 ALTHUNK_REMOVEENTRY(slot->effectslot);
77 ALEffect_Destroy(slot->EffectState);
78 free(slot);
80 alSetError(Context, err);
81 alDeleteAuxiliaryEffectSlots(i, effectslots);
82 break;
85 effectslots[i++] = slot->effectslot;
87 slot->Gain = 1.0;
88 slot->AuxSendAuto = AL_TRUE;
89 for(j = 0;j < BUFFERSIZE;j++)
90 slot->WetBuffer[j] = 0.0f;
91 for(j = 0;j < 1;j++)
92 slot->ClickRemoval[j] = 0.0f;
93 slot->refcount = 0;
97 else
98 alSetError(Context, AL_INVALID_OPERATION);
101 ProcessContext(Context);
104 AL_API ALvoid AL_APIENTRY alDeleteAuxiliaryEffectSlots(ALsizei n, ALuint *effectslots)
106 ALCcontext *Context;
107 ALeffectslot *EffectSlot;
108 ALsizei i;
110 Context = GetContextSuspended();
111 if(!Context) return;
113 if (n >= 0)
115 // Check that all effectslots are valid
116 for (i = 0; i < n; i++)
118 if((EffectSlot=LookupEffectSlot(Context->EffectSlotMap, effectslots[i])) == NULL)
120 alSetError(Context, AL_INVALID_NAME);
121 break;
123 else
125 if(EffectSlot->refcount > 0)
127 alSetError(Context, AL_INVALID_NAME);
128 break;
133 if (i == n)
135 // All effectslots are valid
136 for (i = 0; i < n; i++)
138 // Recheck that the effectslot is valid, because there could be duplicated names
139 if((EffectSlot=LookupEffectSlot(Context->EffectSlotMap, effectslots[i])) != NULL)
141 ALEffect_Destroy(EffectSlot->EffectState);
143 RemoveUIntMapKey(&Context->EffectSlotMap, EffectSlot->effectslot);
144 ALTHUNK_REMOVEENTRY(EffectSlot->effectslot);
146 memset(EffectSlot, 0, sizeof(ALeffectslot));
147 free(EffectSlot);
152 else
153 alSetError(Context, AL_INVALID_VALUE);
155 ProcessContext(Context);
158 AL_API ALboolean AL_APIENTRY alIsAuxiliaryEffectSlot(ALuint effectslot)
160 ALCcontext *Context;
161 ALboolean result;
163 Context = GetContextSuspended();
164 if(!Context) return AL_FALSE;
166 result = (LookupEffectSlot(Context->EffectSlotMap, effectslot) ?
167 AL_TRUE : AL_FALSE);
169 ProcessContext(Context);
171 return result;
174 AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSloti(ALuint effectslot, ALenum param, ALint iValue)
176 ALCcontext *Context;
177 ALboolean updateSources = AL_FALSE;
178 ALeffectslot *EffectSlot;
180 Context = GetContextSuspended();
181 if(!Context) return;
183 if((EffectSlot=LookupEffectSlot(Context->EffectSlotMap, effectslot)) != NULL)
185 switch(param)
187 case AL_EFFECTSLOT_EFFECT: {
188 ALeffect *effect = NULL;
190 if(iValue == 0 ||
191 (effect=LookupEffect(Context->Device->EffectMap, iValue)) != NULL)
193 InitializeEffect(Context, EffectSlot, effect);
194 updateSources = AL_TRUE;
196 else
197 alSetError(Context, AL_INVALID_VALUE);
198 } break;
200 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
201 if(iValue == AL_TRUE || iValue == AL_FALSE)
203 EffectSlot->AuxSendAuto = iValue;
204 updateSources = AL_TRUE;
206 else
207 alSetError(Context, AL_INVALID_VALUE);
208 break;
210 default:
211 alSetError(Context, AL_INVALID_ENUM);
212 break;
215 else
216 alSetError(Context, AL_INVALID_NAME);
218 // Force updating the sources that use this slot, since it affects the
219 // sending parameters
220 if(updateSources)
222 ALsizei pos;
223 for(pos = 0;pos < Context->SourceMap.size;pos++)
225 ALsource *source = Context->SourceMap.array[pos].value;
226 ALuint i;
227 for(i = 0;i < MAX_SENDS;i++)
229 if(!source->Send[i].Slot ||
230 source->Send[i].Slot->effectslot != effectslot)
231 continue;
232 source->NeedsUpdate = AL_TRUE;
233 break;
238 ProcessContext(Context);
241 AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotiv(ALuint effectslot, ALenum param, ALint *piValues)
243 ALCcontext *Context;
245 Context = GetContextSuspended();
246 if(!Context) return;
248 if(LookupEffectSlot(Context->EffectSlotMap, effectslot) != NULL)
250 switch(param)
252 case AL_EFFECTSLOT_EFFECT:
253 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
254 alAuxiliaryEffectSloti(effectslot, param, piValues[0]);
255 break;
257 default:
258 alSetError(Context, AL_INVALID_ENUM);
259 break;
262 else
263 alSetError(Context, AL_INVALID_NAME);
265 ProcessContext(Context);
268 AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotf(ALuint effectslot, ALenum param, ALfloat flValue)
270 ALCcontext *Context;
271 ALeffectslot *EffectSlot;
273 Context = GetContextSuspended();
274 if(!Context) return;
276 if((EffectSlot=LookupEffectSlot(Context->EffectSlotMap, effectslot)) != NULL)
278 switch(param)
280 case AL_EFFECTSLOT_GAIN:
281 if(flValue >= 0.0f && flValue <= 1.0f)
282 EffectSlot->Gain = flValue;
283 else
284 alSetError(Context, AL_INVALID_VALUE);
285 break;
287 default:
288 alSetError(Context, AL_INVALID_ENUM);
289 break;
292 else
293 alSetError(Context, AL_INVALID_NAME);
295 ProcessContext(Context);
298 AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotfv(ALuint effectslot, ALenum param, ALfloat *pflValues)
300 ALCcontext *Context;
302 Context = GetContextSuspended();
303 if(!Context) return;
305 if(LookupEffectSlot(Context->EffectSlotMap, effectslot) != NULL)
307 switch(param)
309 case AL_EFFECTSLOT_GAIN:
310 alAuxiliaryEffectSlotf(effectslot, param, pflValues[0]);
311 break;
313 default:
314 alSetError(Context, AL_INVALID_ENUM);
315 break;
318 else
319 alSetError(Context, AL_INVALID_NAME);
321 ProcessContext(Context);
324 AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSloti(ALuint effectslot, ALenum param, ALint *piValue)
326 ALCcontext *Context;
327 ALeffectslot *EffectSlot;
329 Context = GetContextSuspended();
330 if(!Context) return;
332 if((EffectSlot=LookupEffectSlot(Context->EffectSlotMap, effectslot)) != NULL)
334 switch(param)
336 case AL_EFFECTSLOT_EFFECT:
337 *piValue = EffectSlot->effect.effect;
338 break;
340 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
341 *piValue = EffectSlot->AuxSendAuto;
342 break;
344 default:
345 alSetError(Context, AL_INVALID_ENUM);
346 break;
349 else
350 alSetError(Context, AL_INVALID_NAME);
352 ProcessContext(Context);
355 AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotiv(ALuint effectslot, ALenum param, ALint *piValues)
357 ALCcontext *Context;
359 Context = GetContextSuspended();
360 if(!Context) return;
362 if(LookupEffectSlot(Context->EffectSlotMap, effectslot) != NULL)
364 switch(param)
366 case AL_EFFECTSLOT_EFFECT:
367 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
368 alGetAuxiliaryEffectSloti(effectslot, param, piValues);
369 break;
371 default:
372 alSetError(Context, AL_INVALID_ENUM);
373 break;
376 else
377 alSetError(Context, AL_INVALID_NAME);
379 ProcessContext(Context);
382 AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotf(ALuint effectslot, ALenum param, ALfloat *pflValue)
384 ALCcontext *Context;
385 ALeffectslot *EffectSlot;
387 Context = GetContextSuspended();
388 if(!Context) return;
390 if((EffectSlot=LookupEffectSlot(Context->EffectSlotMap, effectslot)) != NULL)
392 switch(param)
394 case AL_EFFECTSLOT_GAIN:
395 *pflValue = EffectSlot->Gain;
396 break;
398 default:
399 alSetError(Context, AL_INVALID_ENUM);
400 break;
403 else
404 alSetError(Context, AL_INVALID_NAME);
406 ProcessContext(Context);
409 AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotfv(ALuint effectslot, ALenum param, ALfloat *pflValues)
411 ALCcontext *Context;
413 Context = GetContextSuspended();
414 if(!Context) return;
416 if(LookupEffectSlot(Context->EffectSlotMap, effectslot) != NULL)
418 switch(param)
420 case AL_EFFECTSLOT_GAIN:
421 alGetAuxiliaryEffectSlotf(effectslot, param, pflValues);
422 break;
424 default:
425 alSetError(Context, AL_INVALID_ENUM);
426 break;
429 else
430 alSetError(Context, AL_INVALID_NAME);
432 ProcessContext(Context);
436 static ALvoid NoneDestroy(ALeffectState *State)
437 { free(State); }
438 static ALboolean NoneDeviceUpdate(ALeffectState *State, ALCdevice *Device)
440 return AL_TRUE;
441 (void)State;
442 (void)Device;
444 static ALvoid NoneUpdate(ALeffectState *State, ALCcontext *Context, const ALeffect *Effect)
446 (void)State;
447 (void)Context;
448 (void)Effect;
450 static ALvoid NoneProcess(ALeffectState *State, const ALeffectslot *Slot, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[OUTPUTCHANNELS])
452 (void)State;
453 (void)Slot;
454 (void)SamplesToDo;
455 (void)SamplesIn;
456 (void)SamplesOut;
458 ALeffectState *NoneCreate(void)
460 ALeffectState *state;
462 state = calloc(1, sizeof(*state));
463 if(!state)
464 return NULL;
466 state->Destroy = NoneDestroy;
467 state->DeviceUpdate = NoneDeviceUpdate;
468 state->Update = NoneUpdate;
469 state->Process = NoneProcess;
471 return state;
474 static ALvoid InitializeEffect(ALCcontext *Context, ALeffectslot *EffectSlot, ALeffect *effect)
476 if(EffectSlot->effect.type != (effect?effect->type:AL_EFFECT_NULL))
478 ALeffectState *NewState = NULL;
479 if(!effect || effect->type == AL_EFFECT_NULL)
480 NewState = NoneCreate();
481 else if(effect->type == AL_EFFECT_EAXREVERB)
482 NewState = EAXVerbCreate();
483 else if(effect->type == AL_EFFECT_REVERB)
484 NewState = VerbCreate();
485 else if(effect->type == AL_EFFECT_ECHO)
486 NewState = EchoCreate();
487 else if(effect->type == AL_EFFECT_RING_MODULATOR)
488 NewState = ModulatorCreate();
489 /* No new state? An error occured.. */
490 if(NewState == NULL ||
491 ALEffect_DeviceUpdate(NewState, Context->Device) == AL_FALSE)
493 if(NewState)
494 ALEffect_Destroy(NewState);
495 alSetError(Context, AL_OUT_OF_MEMORY);
496 return;
498 if(EffectSlot->EffectState)
499 ALEffect_Destroy(EffectSlot->EffectState);
500 EffectSlot->EffectState = NewState;
502 if(!effect)
503 memset(&EffectSlot->effect, 0, sizeof(EffectSlot->effect));
504 else
505 memcpy(&EffectSlot->effect, effect, sizeof(*effect));
506 ALEffect_Update(EffectSlot->EffectState, Context, effect);
510 ALvoid ReleaseALAuxiliaryEffectSlots(ALCcontext *Context)
512 ALsizei pos;
513 for(pos = 0;pos < Context->EffectSlotMap.size;pos++)
515 ALeffectslot *temp = Context->EffectSlotMap.array[pos].value;
516 Context->EffectSlotMap.array[pos].value = NULL;
518 // Release effectslot structure
519 ALEffect_Destroy(temp->EffectState);
521 ALTHUNK_REMOVEENTRY(temp->effectslot);
522 memset(temp, 0, sizeof(ALeffectslot));
523 free(temp);