Don't return unsupported effects from alGetEnumValue
[openal-soft.git] / OpenAL32 / alFilter.c
blob52030246d350afb70d61f2462a343f29c3040b0d
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>
25 #include "AL/al.h"
26 #include "AL/alc.h"
27 #include "alMain.h"
28 #include "alFilter.h"
29 #include "alThunk.h"
30 #include "alError.h"
33 static void InitFilterParams(ALfilter *filter, ALenum type);
35 DECL_VERIFIER(Filter, ALfilter, filter)
37 ALvoid AL_APIENTRY alGenFilters(ALsizei n, ALuint *filters)
39 ALCcontext *Context;
40 ALsizei i;
42 Context = GetContextSuspended();
43 if(!Context) return;
45 if (n > 0)
47 ALCdevice *device = Context->Device;
49 // Check that enough memory has been allocted in the 'filters' array for n Filters
50 if (!IsBadWritePtr((void*)filters, n * sizeof(ALuint)))
52 ALfilter **list = &device->FilterList;
53 while(*list)
54 list = &(*list)->next;
56 i = 0;
57 while(i < n)
59 *list = calloc(1, sizeof(ALfilter));
60 if(!(*list))
62 // We must have run out or memory
63 alDeleteFilters(i, filters);
64 alSetError(Context, AL_OUT_OF_MEMORY);
65 break;
68 filters[i] = (ALuint)ALTHUNK_ADDENTRY(*list);
69 (*list)->filter = filters[i];
71 InitFilterParams(*list, AL_FILTER_NULL);
72 device->FilterCount++;
73 i++;
75 list = &(*list)->next;
80 ProcessContext(Context);
83 ALvoid AL_APIENTRY alDeleteFilters(ALsizei n, ALuint *filters)
85 ALCcontext *Context;
86 ALfilter *ALFilter;
87 ALsizei i;
89 Context = GetContextSuspended();
90 if(!Context) return;
92 if (n >= 0)
94 ALCdevice *device = Context->Device;
96 // Check that all filters are valid
97 for (i = 0; i < n; i++)
99 if(!filters[i])
100 continue;
102 if(!VerifyFilter(device->FilterList, filters[i]))
104 alSetError(Context, AL_INVALID_NAME);
105 break;
109 if (i == n)
111 // All filters are valid
112 for (i = 0; i < n; i++)
114 // Recheck that the filter is valid, because there could be duplicated names
115 if((ALFilter=VerifyFilter(device->FilterList, filters[i])) != NULL)
117 ALfilter **list;
119 // Remove Source from list of Sources
120 list = &device->FilterList;
121 while(*list && *list != ALFilter)
122 list = &(*list)->next;
124 if(*list)
125 *list = (*list)->next;
126 ALTHUNK_REMOVEENTRY(ALFilter->filter);
128 memset(ALFilter, 0, sizeof(ALfilter));
129 free(ALFilter);
131 device->FilterCount--;
136 else
137 alSetError(Context, AL_INVALID_VALUE);
139 ProcessContext(Context);
142 ALboolean AL_APIENTRY alIsFilter(ALuint filter)
144 ALCcontext *Context;
145 ALboolean result = AL_TRUE;
147 Context = GetContextSuspended();
148 if(!Context) return AL_FALSE;
150 if(filter)
151 result = (VerifyFilter(Context->Device->FilterList, filter) ?
152 AL_TRUE : AL_FALSE);
154 ProcessContext(Context);
156 return result;
159 ALvoid AL_APIENTRY alFilteri(ALuint filter, ALenum param, ALint iValue)
161 ALCcontext *Context;
162 ALCdevice *Device;
163 ALfilter *ALFilter;
165 Context = GetContextSuspended();
166 if(!Context) return;
168 Device = Context->Device;
169 if((ALFilter=VerifyFilter(Device->FilterList, filter)) != NULL)
171 switch(param)
173 case AL_FILTER_TYPE:
174 if(iValue == AL_FILTER_NULL ||
175 iValue == AL_FILTER_LOWPASS)
176 InitFilterParams(ALFilter, iValue);
177 else
178 alSetError(Context, AL_INVALID_VALUE);
179 break;
181 default:
182 alSetError(Context, AL_INVALID_ENUM);
183 break;
186 else
187 alSetError(Context, AL_INVALID_NAME);
189 ProcessContext(Context);
192 ALvoid AL_APIENTRY alFilteriv(ALuint filter, ALenum param, ALint *piValues)
194 ALCcontext *Context;
195 ALCdevice *Device;
197 Context = GetContextSuspended();
198 if(!Context) return;
200 Device = Context->Device;
201 if(VerifyFilter(Device->FilterList, filter) != NULL)
203 switch(param)
205 case AL_FILTER_TYPE:
206 alFilteri(filter, param, piValues[0]);
207 break;
209 default:
210 alSetError(Context, AL_INVALID_ENUM);
211 break;
214 else
215 alSetError(Context, AL_INVALID_NAME);
217 ProcessContext(Context);
220 ALvoid AL_APIENTRY alFilterf(ALuint filter, ALenum param, ALfloat flValue)
222 ALCcontext *Context;
223 ALCdevice *Device;
224 ALfilter *ALFilter;
226 Context = GetContextSuspended();
227 if(!Context) return;
229 Device = Context->Device;
230 if((ALFilter=VerifyFilter(Device->FilterList, filter)) != NULL)
232 switch(ALFilter->type)
234 case AL_FILTER_LOWPASS:
235 switch(param)
237 case AL_LOWPASS_GAIN:
238 if(flValue >= 0.0f && flValue <= 1.0f)
239 ALFilter->Gain = flValue;
240 else
241 alSetError(Context, AL_INVALID_VALUE);
242 break;
244 case AL_LOWPASS_GAINHF:
245 if(flValue >= 0.0f && flValue <= 1.0f)
246 ALFilter->GainHF = flValue;
247 else
248 alSetError(Context, AL_INVALID_VALUE);
249 break;
251 default:
252 alSetError(Context, AL_INVALID_ENUM);
253 break;
255 break;
257 default:
258 alSetError(Context, AL_INVALID_ENUM);
259 break;
262 else
263 alSetError(Context, AL_INVALID_NAME);
265 ProcessContext(Context);
268 ALvoid AL_APIENTRY alFilterfv(ALuint filter, ALenum param, ALfloat *pflValues)
270 ALCcontext *Context;
271 ALCdevice *Device;
273 Context = GetContextSuspended();
274 if(!Context) return;
276 Device = Context->Device;
277 if(VerifyFilter(Device->FilterList, filter) != NULL)
279 switch(param)
281 default:
282 alFilterf(filter, param, pflValues[0]);
283 break;
286 else
287 alSetError(Context, AL_INVALID_NAME);
289 ProcessContext(Context);
292 ALvoid AL_APIENTRY alGetFilteri(ALuint filter, ALenum param, ALint *piValue)
294 ALCcontext *Context;
295 ALCdevice *Device;
296 ALfilter *ALFilter;
298 Context = GetContextSuspended();
299 if(!Context) return;
301 Device = Context->Device;
302 if((ALFilter=VerifyFilter(Device->FilterList, filter)) != NULL)
304 switch(param)
306 case AL_FILTER_TYPE:
307 *piValue = ALFilter->type;
308 break;
310 default:
311 alSetError(Context, AL_INVALID_ENUM);
312 break;
315 else
316 alSetError(Context, AL_INVALID_NAME);
318 ProcessContext(Context);
321 ALvoid AL_APIENTRY alGetFilteriv(ALuint filter, ALenum param, ALint *piValues)
323 ALCcontext *Context;
324 ALCdevice *Device;
326 Context = GetContextSuspended();
327 if(!Context) return;
329 Device = Context->Device;
330 if(VerifyFilter(Device->FilterList, filter) != NULL)
332 switch(param)
334 case AL_FILTER_TYPE:
335 alGetFilteri(filter, param, piValues);
336 break;
338 default:
339 alSetError(Context, AL_INVALID_ENUM);
340 break;
343 else
344 alSetError(Context, AL_INVALID_NAME);
346 ProcessContext(Context);
349 ALvoid AL_APIENTRY alGetFilterf(ALuint filter, ALenum param, ALfloat *pflValue)
351 ALCcontext *Context;
352 ALCdevice *Device;
353 ALfilter *ALFilter;
355 Context = GetContextSuspended();
356 if(!Context) return;
358 Device = Context->Device;
359 if((ALFilter=VerifyFilter(Device->FilterList, filter)) != NULL)
361 switch(ALFilter->type)
363 case AL_FILTER_LOWPASS:
364 switch(param)
366 case AL_LOWPASS_GAIN:
367 *pflValue = ALFilter->Gain;
368 break;
370 case AL_LOWPASS_GAINHF:
371 *pflValue = ALFilter->GainHF;
372 break;
374 default:
375 alSetError(Context, AL_INVALID_ENUM);
376 break;
378 break;
380 default:
381 alSetError(Context, AL_INVALID_ENUM);
382 break;
385 else
386 alSetError(Context, AL_INVALID_NAME);
388 ProcessContext(Context);
391 ALvoid AL_APIENTRY alGetFilterfv(ALuint filter, ALenum param, ALfloat *pflValues)
393 ALCcontext *Context;
394 ALCdevice *Device;
396 Context = GetContextSuspended();
397 if(!Context) return;
399 Device = Context->Device;
400 if(VerifyFilter(Device->FilterList, filter) != NULL)
402 switch(param)
404 default:
405 alGetFilterf(filter, param, pflValues);
406 break;
409 else
410 alSetError(Context, AL_INVALID_NAME);
412 ProcessContext(Context);
416 ALvoid ReleaseALFilters(ALCdevice *device)
418 ALfilter *list = device->FilterList;
419 while(list)
421 ALfilter *temp = list;
422 list = list->next;
424 // Release filter structure
425 memset(temp, 0, sizeof(ALfilter));
426 free(temp);
428 device->FilterList = NULL;
429 device->FilterCount = 0;
433 static void InitFilterParams(ALfilter *filter, ALenum type)
435 filter->type = type;
437 filter->Gain = 1.0;
438 filter->GainHF = 1.0;