Reorder setting of some variables
[openal-soft.git] / OpenAL32 / alAuxEffectSlot.c
blob282b8030546969e83f163775ed7a9734a760d223
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 <stdlib.h>
23 #include "config.h"
25 #include "AL/al.h"
26 #include "AL/alc.h"
28 #include "alMain.h"
29 #include "alAuxEffectSlot.h"
30 #include "alThunk.h"
31 #include "alError.h"
33 static ALeffectslot *g_AuxiliaryEffectSlotList;
34 static ALuint g_AuxiliaryEffectSlotCount;
37 AL_API ALvoid AL_APIENTRY alGenAuxiliaryEffectSlots(ALsizei n, ALuint *effectslots)
39 ALCcontext *Context;
40 ALsizei i;
42 Context = alcGetCurrentContext();
43 SuspendContext(Context);
45 if (n > 0)
47 /* NOTE: We only support one slot currently */
48 if(n == 1 && g_AuxiliaryEffectSlotCount == 0)
50 // Check that enough memory has been allocted in the 'sources' array for n Sources
51 if (!IsBadWritePtr((void*)effectslots, n * sizeof(ALuint)))
53 ALeffectslot **list = &g_AuxiliaryEffectSlotList;
54 while(*list)
55 list = &(*list)->next;
57 i = 0;
58 while(i < n)
60 *list = calloc(1, sizeof(ALeffectslot));
61 if(!(*list))
63 // We must have run out or memory
64 alDeleteAuxiliaryEffectSlots(i, effectslots);
65 alSetError(AL_OUT_OF_MEMORY);
66 break;
69 (*list)->Gain = 1.0;
70 (*list)->AuxSendAuto = AL_TRUE;
72 effectslots[i] = (ALuint)ALTHUNK_ADDENTRY(*list);
73 (*list)->effectslot = effectslots[i];
75 g_AuxiliaryEffectSlotCount++;
76 i++;
80 else
81 alSetError(AL_INVALID_OPERATION);
84 ProcessContext(Context);
87 AL_API ALvoid AL_APIENTRY alDeleteAuxiliaryEffectSlots(ALsizei n, ALuint *effectslots)
89 ALCcontext *Context;
90 ALeffectslot *ALAuxiliaryEffectSlot;
91 ALsizei i;
93 Context = alcGetCurrentContext();
94 SuspendContext(Context);
96 if (n >= 0)
98 // Check that all effectslots are valid
99 for (i = 0; i < n; i++)
101 if (!alIsAuxiliaryEffectSlot(effectslots[i]))
103 alSetError(AL_INVALID_NAME);
104 break;
108 if (i == n)
110 // All effectslots are valid
111 for (i = 0; i < n; i++)
113 // Recheck that the effectslot is valid, because there could be duplicated names
114 if (alIsAuxiliaryEffectSlot(effectslots[i]))
116 ALeffectslot **list;
118 ALAuxiliaryEffectSlot = ((ALeffectslot*)ALTHUNK_LOOKUPENTRY(effectslots[i]));
120 // Remove Source from list of Sources
121 list = &g_AuxiliaryEffectSlotList;
122 while(*list && *list != ALAuxiliaryEffectSlot)
123 list = &(*list)->next;
125 if(*list)
126 *list = (*list)->next;
127 ALTHUNK_REMOVEENTRY(ALAuxiliaryEffectSlot->effectslot);
129 memset(ALAuxiliaryEffectSlot, 0, sizeof(ALeffectslot));
130 free(ALAuxiliaryEffectSlot);
132 g_AuxiliaryEffectSlotCount--;
137 else
138 alSetError(AL_INVALID_VALUE);
140 ProcessContext(Context);
143 AL_API ALboolean AL_APIENTRY alIsAuxiliaryEffectSlot(ALuint effectslot)
145 ALCcontext *Context;
146 ALeffectslot **list;
148 Context = alcGetCurrentContext();
149 SuspendContext(Context);
151 list = &g_AuxiliaryEffectSlotList;
152 while(*list && (*list)->effectslot != effectslot)
153 list = &(*list)->next;
155 ProcessContext(Context);
157 return (*list ? AL_TRUE : AL_FALSE);
160 AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSloti(ALuint effectslot, ALenum param, ALint iValue)
162 ALCcontext *Context;
164 Context = alcGetCurrentContext();
165 SuspendContext(Context);
167 if (alIsAuxiliaryEffectSlot(effectslot))
169 ALeffectslot *ALEffectSlot = (ALeffectslot*)ALTHUNK_LOOKUPENTRY(effectslot);
171 switch(param)
173 case AL_EFFECTSLOT_EFFECT:
174 if(alIsEffect(iValue))
176 ALeffect *effect = (ALeffect*)ALTHUNK_LOOKUPENTRY(iValue);
177 if(!effect)
179 ALEffectSlot->effect.type = AL_EFFECT_NULL;
180 ALEffectSlot->effect.effect = 0;
182 else
183 memcpy(&ALEffectSlot->effect, effect, sizeof(*effect));
185 else
186 alSetError(AL_INVALID_VALUE);
187 break;
189 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
190 if(iValue == AL_TRUE || iValue == AL_FALSE)
191 ALEffectSlot->AuxSendAuto = iValue;
192 else
193 alSetError(AL_INVALID_VALUE);
194 break;
196 default:
197 alSetError(AL_INVALID_ENUM);
198 break;
201 else
202 alSetError(AL_INVALID_NAME);
204 ProcessContext(Context);
207 AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotiv(ALuint effectslot, ALenum param, ALint *piValues)
209 ALCcontext *Context;
211 Context = alcGetCurrentContext();
212 SuspendContext(Context);
214 if (alIsAuxiliaryEffectSlot(effectslot))
216 switch(param)
218 case AL_EFFECTSLOT_EFFECT:
219 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
220 alAuxiliaryEffectSloti(effectslot, param, piValues[0]);
221 break;
223 default:
224 alSetError(AL_INVALID_ENUM);
225 break;
228 else
229 alSetError(AL_INVALID_NAME);
231 ProcessContext(Context);
234 AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotf(ALuint effectslot, ALenum param, ALfloat flValue)
236 ALCcontext *Context;
238 (void)flValue;
240 Context = alcGetCurrentContext();
241 SuspendContext(Context);
243 if (alIsAuxiliaryEffectSlot(effectslot))
245 ALeffectslot *ALEffectSlot = (ALeffectslot*)ALTHUNK_LOOKUPENTRY(effectslot);
247 switch(param)
249 case AL_EFFECTSLOT_GAIN:
250 if(flValue >= 0.0f && flValue <= 1.0f)
251 ALEffectSlot->Gain = flValue;
252 else
253 alSetError(AL_INVALID_VALUE);
254 break;
256 default:
257 alSetError(AL_INVALID_ENUM);
258 break;
261 else
262 alSetError(AL_INVALID_NAME);
264 ProcessContext(Context);
267 AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotfv(ALuint effectslot, ALenum param, ALfloat *pflValues)
269 ALCcontext *Context;
271 Context = alcGetCurrentContext();
272 SuspendContext(Context);
274 if (alIsAuxiliaryEffectSlot(effectslot))
276 switch(param)
278 case AL_EFFECTSLOT_GAIN:
279 alAuxiliaryEffectSlotf(effectslot, param, pflValues[0]);
280 break;
282 default:
283 alSetError(AL_INVALID_ENUM);
284 break;
287 else
288 alSetError(AL_INVALID_NAME);
290 ProcessContext(Context);
293 AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSloti(ALuint effectslot, ALenum param, ALint *piValue)
295 ALCcontext *Context;
297 Context = alcGetCurrentContext();
298 SuspendContext(Context);
300 if (alIsAuxiliaryEffectSlot(effectslot))
302 ALeffectslot *ALEffectSlot = (ALeffectslot*)ALTHUNK_LOOKUPENTRY(effectslot);
304 switch(param)
306 case AL_EFFECTSLOT_EFFECT:
307 *piValue = ALEffectSlot->effect.effect;
308 break;
310 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
311 *piValue = ALEffectSlot->AuxSendAuto;
312 break;
314 default:
315 alSetError(AL_INVALID_ENUM);
316 break;
319 else
320 alSetError(AL_INVALID_NAME);
322 ProcessContext(Context);
325 AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotiv(ALuint effectslot, ALenum param, ALint *piValues)
327 ALCcontext *Context;
329 Context = alcGetCurrentContext();
330 SuspendContext(Context);
332 if (alIsAuxiliaryEffectSlot(effectslot))
334 switch(param)
336 case AL_EFFECTSLOT_EFFECT:
337 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
338 alGetAuxiliaryEffectSloti(effectslot, param, piValues);
339 break;
341 default:
342 alSetError(AL_INVALID_ENUM);
343 break;
346 else
347 alSetError(AL_INVALID_NAME);
349 ProcessContext(Context);
352 AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotf(ALuint effectslot, ALenum param, ALfloat *pflValue)
354 ALCcontext *Context;
356 Context = alcGetCurrentContext();
357 SuspendContext(Context);
359 if (alIsAuxiliaryEffectSlot(effectslot))
361 ALeffectslot *ALEffectSlot = (ALeffectslot*)ALTHUNK_LOOKUPENTRY(effectslot);
363 switch(param)
365 case AL_EFFECTSLOT_GAIN:
366 *pflValue = ALEffectSlot->Gain;
367 break;
369 default:
370 alSetError(AL_INVALID_ENUM);
371 break;
374 else
375 alSetError(AL_INVALID_NAME);
377 ProcessContext(Context);
380 AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotfv(ALuint effectslot, ALenum param, ALfloat *pflValues)
382 ALCcontext *Context;
384 Context = alcGetCurrentContext();
385 SuspendContext(Context);
387 if (alIsAuxiliaryEffectSlot(effectslot))
389 switch(param)
391 case AL_EFFECTSLOT_GAIN:
392 alGetAuxiliaryEffectSlotf(effectslot, param, pflValues);
393 break;
395 default:
396 alSetError(AL_INVALID_ENUM);
397 break;
400 else
401 alSetError(AL_INVALID_NAME);
403 ProcessContext(Context);
407 ALvoid ReleaseALAuxiliaryEffectSlots(ALvoid)
409 #ifdef _DEBUG
410 if(g_AuxiliaryEffectSlotCount > 0)
411 AL_PRINT("exit() %d AuxiliaryEffectSlot(s) NOT deleted\n", g_AuxiliaryEffectSlotCount);
412 #endif
414 while(g_AuxiliaryEffectSlotList)
416 ALeffectslot *temp = g_AuxiliaryEffectSlotList;
417 g_AuxiliaryEffectSlotList = g_AuxiliaryEffectSlotList->next;
419 // Release effectslot structure
420 memset(temp, 0, sizeof(ALeffectslot));
421 free(temp);
423 g_AuxiliaryEffectSlotCount = 0;