Move ConeScale and ZScale to ALu.c and alu.h, and make them floats
[openal-soft/android.git] / OpenAL32 / alFilter.c
blobadf689cac8a1212a782aca3ebf405147d776f05a
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 #define LookupFilter(m, k) ((ALfilter*)LookupUIntMapKey(&(m), (k)))
36 #define RemoveFilter(m, k) ((ALfilter*)PopUIntMapValue(&(m), (k)))
38 AL_API ALvoid AL_APIENTRY alGenFilters(ALsizei n, ALuint *filters)
40 ALCcontext *Context;
41 ALsizei i;
43 Context = GetContextRef();
44 if(!Context) return;
46 if(n < 0 || IsBadWritePtr((void*)filters, n * sizeof(ALuint)))
47 alSetError(Context, AL_INVALID_VALUE);
48 else
50 ALCdevice *device = Context->Device;
51 ALenum err;
53 for(i = 0;i < n;i++)
55 ALfilter *filter = calloc(1, sizeof(ALfilter));
56 if(!filter)
58 alSetError(Context, AL_OUT_OF_MEMORY);
59 alDeleteFilters(i, filters);
60 break;
62 InitFilterParams(filter, AL_FILTER_NULL);
64 err = NewThunkEntry(&filter->filter);
65 if(err == AL_NO_ERROR)
66 err = InsertUIntMapEntry(&device->FilterMap, filter->filter, filter);
67 if(err != AL_NO_ERROR)
69 FreeThunkEntry(filter->filter);
70 memset(filter, 0, sizeof(ALfilter));
71 free(filter);
73 alSetError(Context, err);
74 alDeleteFilters(i, filters);
75 break;
78 filters[i] = filter->filter;
82 ALCcontext_DecRef(Context);
85 AL_API ALvoid AL_APIENTRY alDeleteFilters(ALsizei n, const ALuint *filters)
87 ALCcontext *Context;
88 ALCdevice *device;
89 ALfilter *ALFilter;
90 ALsizei i;
92 Context = GetContextRef();
93 if(!Context) return;
95 device = Context->Device;
96 if(n < 0)
97 alSetError(Context, AL_INVALID_VALUE);
98 else
100 // Check that all filters are valid
101 for(i = 0;i < n;i++)
103 if(!filters[i])
104 continue;
106 if(LookupFilter(device->FilterMap, filters[i]) == NULL)
108 alSetError(Context, AL_INVALID_NAME);
109 n = 0;
110 break;
114 for(i = 0;i < n;i++)
116 // Recheck that the filter is valid, because there could be duplicated names
117 if((ALFilter=RemoveFilter(device->FilterMap, filters[i])) == NULL)
118 continue;
119 FreeThunkEntry(ALFilter->filter);
121 memset(ALFilter, 0, sizeof(ALfilter));
122 free(ALFilter);
126 ALCcontext_DecRef(Context);
129 AL_API ALboolean AL_APIENTRY alIsFilter(ALuint filter)
131 ALCcontext *Context;
132 ALboolean result;
134 Context = GetContextRef();
135 if(!Context) return AL_FALSE;
137 result = ((!filter || LookupFilter(Context->Device->FilterMap, filter)) ?
138 AL_TRUE : AL_FALSE);
140 ALCcontext_DecRef(Context);
142 return result;
145 AL_API ALvoid AL_APIENTRY alFilteri(ALuint filter, ALenum param, ALint iValue)
147 ALCcontext *Context;
148 ALCdevice *Device;
149 ALfilter *ALFilter;
151 Context = GetContextRef();
152 if(!Context) return;
154 Device = Context->Device;
155 if((ALFilter=LookupFilter(Device->FilterMap, filter)) != NULL)
157 switch(param)
159 case AL_FILTER_TYPE:
160 if(iValue == AL_FILTER_NULL || iValue == AL_FILTER_LOWPASS)
161 InitFilterParams(ALFilter, iValue);
162 else
163 alSetError(Context, AL_INVALID_VALUE);
164 break;
166 default:
167 /* Call the appropriate handler */
168 ALfilter_SetParami(ALFilter, Context, param, iValue);
169 break;
172 else
173 alSetError(Context, AL_INVALID_NAME);
175 ALCcontext_DecRef(Context);
178 AL_API ALvoid AL_APIENTRY alFilteriv(ALuint filter, ALenum param, const ALint *piValues)
180 ALCcontext *Context;
181 ALCdevice *Device;
182 ALfilter *ALFilter;
184 switch(param)
186 case AL_FILTER_TYPE:
187 alFilteri(filter, param, piValues[0]);
188 return;
191 Context = GetContextRef();
192 if(!Context) return;
194 Device = Context->Device;
195 if((ALFilter=LookupFilter(Device->FilterMap, filter)) != NULL)
197 /* Call the appropriate handler */
198 ALfilter_SetParamiv(ALFilter, Context, param, piValues);
200 else
201 alSetError(Context, AL_INVALID_NAME);
203 ALCcontext_DecRef(Context);
206 AL_API ALvoid AL_APIENTRY alFilterf(ALuint filter, ALenum param, ALfloat flValue)
208 ALCcontext *Context;
209 ALCdevice *Device;
210 ALfilter *ALFilter;
212 Context = GetContextRef();
213 if(!Context) return;
215 Device = Context->Device;
216 if((ALFilter=LookupFilter(Device->FilterMap, filter)) != NULL)
218 /* Call the appropriate handler */
219 ALfilter_SetParamf(ALFilter, Context, param, flValue);
221 else
222 alSetError(Context, AL_INVALID_NAME);
224 ALCcontext_DecRef(Context);
227 AL_API ALvoid AL_APIENTRY alFilterfv(ALuint filter, ALenum param, const ALfloat *pflValues)
229 ALCcontext *Context;
230 ALCdevice *Device;
231 ALfilter *ALFilter;
233 Context = GetContextRef();
234 if(!Context) return;
236 Device = Context->Device;
237 if((ALFilter=LookupFilter(Device->FilterMap, filter)) != NULL)
239 /* Call the appropriate handler */
240 ALfilter_SetParamfv(ALFilter, Context, param, pflValues);
242 else
243 alSetError(Context, AL_INVALID_NAME);
245 ALCcontext_DecRef(Context);
248 AL_API ALvoid AL_APIENTRY alGetFilteri(ALuint filter, ALenum param, ALint *piValue)
250 ALCcontext *Context;
251 ALCdevice *Device;
252 ALfilter *ALFilter;
254 Context = GetContextRef();
255 if(!Context) return;
257 Device = Context->Device;
258 if((ALFilter=LookupFilter(Device->FilterMap, filter)) != NULL)
260 switch(param)
262 case AL_FILTER_TYPE:
263 *piValue = ALFilter->type;
264 break;
266 default:
267 /* Call the appropriate handler */
268 ALfilter_GetParami(ALFilter, Context, param, piValue);
269 break;
272 else
273 alSetError(Context, AL_INVALID_NAME);
275 ALCcontext_DecRef(Context);
278 AL_API ALvoid AL_APIENTRY alGetFilteriv(ALuint filter, ALenum param, ALint *piValues)
280 ALCcontext *Context;
281 ALCdevice *Device;
282 ALfilter *ALFilter;
284 switch(param)
286 case AL_FILTER_TYPE:
287 alGetFilteri(filter, param, piValues);
288 return;
291 Context = GetContextRef();
292 if(!Context) return;
294 Device = Context->Device;
295 if((ALFilter=LookupFilter(Device->FilterMap, filter)) != NULL)
297 /* Call the appropriate handler */
298 ALfilter_GetParamiv(ALFilter, Context, param, piValues);
300 else
301 alSetError(Context, AL_INVALID_NAME);
303 ALCcontext_DecRef(Context);
306 AL_API ALvoid AL_APIENTRY alGetFilterf(ALuint filter, ALenum param, ALfloat *pflValue)
308 ALCcontext *Context;
309 ALCdevice *Device;
310 ALfilter *ALFilter;
312 Context = GetContextRef();
313 if(!Context) return;
315 Device = Context->Device;
316 if((ALFilter=LookupFilter(Device->FilterMap, filter)) != NULL)
318 /* Call the appropriate handler */
319 ALfilter_GetParamf(ALFilter, Context, param, pflValue);
321 else
322 alSetError(Context, AL_INVALID_NAME);
324 ALCcontext_DecRef(Context);
327 AL_API ALvoid AL_APIENTRY alGetFilterfv(ALuint filter, ALenum param, ALfloat *pflValues)
329 ALCcontext *Context;
330 ALCdevice *Device;
331 ALfilter *ALFilter;
333 Context = GetContextRef();
334 if(!Context) return;
336 Device = Context->Device;
337 if((ALFilter=LookupFilter(Device->FilterMap, filter)) != NULL)
339 /* Call the appropriate handler */
340 ALfilter_GetParamfv(ALFilter, Context, param, pflValues);
342 else
343 alSetError(Context, AL_INVALID_NAME);
345 ALCcontext_DecRef(Context);
349 ALfloat lpCoeffCalc(ALfloat g, ALfloat cw)
351 ALfloat a = 0.0f;
353 /* Be careful with gains < 0.01, as that causes the coefficient
354 * head towards 1, which will flatten the signal */
355 if(g < 0.9999f) /* 1-epsilon */
357 g = maxf(g, 0.01f);
358 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) /
359 (1 - g);
362 return a;
366 static void lp_SetParami(ALfilter *filter, ALCcontext *context, ALenum param, ALint val)
367 { (void)filter;(void)param;(void)val; alSetError(context, AL_INVALID_ENUM); }
368 static void lp_SetParamiv(ALfilter *filter, ALCcontext *context, ALenum param, const ALint *vals)
369 { (void)filter;(void)param;(void)vals; alSetError(context, AL_INVALID_ENUM); }
370 static void lp_SetParamf(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat val)
372 switch(param)
374 case AL_LOWPASS_GAIN:
375 if(val >= AL_LOWPASS_MIN_GAIN && val <= AL_LOWPASS_MAX_GAIN)
376 filter->Gain = val;
377 else
378 alSetError(context, AL_INVALID_VALUE);
379 break;
381 case AL_LOWPASS_GAINHF:
382 if(val >= AL_LOWPASS_MIN_GAINHF && val <= AL_LOWPASS_MAX_GAINHF)
383 filter->GainHF = val;
384 else
385 alSetError(context, AL_INVALID_VALUE);
386 break;
388 default:
389 alSetError(context, AL_INVALID_ENUM);
390 break;
393 static void lp_SetParamfv(ALfilter *filter, ALCcontext *context, ALenum param, const ALfloat *vals)
395 lp_SetParamf(filter, context, param, vals[0]);
398 static void lp_GetParami(ALfilter *filter, ALCcontext *context, ALenum param, ALint *val)
399 { (void)filter;(void)param;(void)val; alSetError(context, AL_INVALID_ENUM); }
400 static void lp_GetParamiv(ALfilter *filter, ALCcontext *context, ALenum param, ALint *vals)
401 { (void)filter;(void)param;(void)vals; alSetError(context, AL_INVALID_ENUM); }
402 static void lp_GetParamf(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat *val)
404 switch(param)
406 case AL_LOWPASS_GAIN:
407 *val = filter->Gain;
408 break;
410 case AL_LOWPASS_GAINHF:
411 *val = filter->GainHF;
412 break;
414 default:
415 alSetError(context, AL_INVALID_ENUM);
416 break;
419 static void lp_GetParamfv(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat *vals)
421 lp_GetParamf(filter, context, param, vals);
425 static void null_SetParami(ALfilter *filter, ALCcontext *context, ALenum param, ALint val)
426 { (void)filter;(void)param;(void)val; alSetError(context, AL_INVALID_ENUM); }
427 static void null_SetParamiv(ALfilter *filter, ALCcontext *context, ALenum param, const ALint *vals)
428 { (void)filter;(void)param;(void)vals; alSetError(context, AL_INVALID_ENUM); }
429 static void null_SetParamf(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat val)
430 { (void)filter;(void)param;(void)val; alSetError(context, AL_INVALID_ENUM); }
431 static void null_SetParamfv(ALfilter *filter, ALCcontext *context, ALenum param, const ALfloat *vals)
432 { (void)filter;(void)param;(void)vals; alSetError(context, AL_INVALID_ENUM); }
434 static void null_GetParami(ALfilter *filter, ALCcontext *context, ALenum param, ALint *val)
435 { (void)filter;(void)param;(void)val; alSetError(context, AL_INVALID_ENUM); }
436 static void null_GetParamiv(ALfilter *filter, ALCcontext *context, ALenum param, ALint *vals)
437 { (void)filter;(void)param;(void)vals; alSetError(context, AL_INVALID_ENUM); }
438 static void null_GetParamf(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat *val)
439 { (void)filter;(void)param;(void)val; alSetError(context, AL_INVALID_ENUM); }
440 static void null_GetParamfv(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat *vals)
441 { (void)filter;(void)param;(void)vals; alSetError(context, AL_INVALID_ENUM); }
444 ALvoid ReleaseALFilters(ALCdevice *device)
446 ALsizei i;
447 for(i = 0;i < device->FilterMap.size;i++)
449 ALfilter *temp = device->FilterMap.array[i].value;
450 device->FilterMap.array[i].value = NULL;
452 // Release filter structure
453 FreeThunkEntry(temp->filter);
454 memset(temp, 0, sizeof(ALfilter));
455 free(temp);
460 static void InitFilterParams(ALfilter *filter, ALenum type)
462 if(type == AL_FILTER_LOWPASS)
464 filter->Gain = AL_LOWPASS_DEFAULT_GAIN;
465 filter->GainHF = AL_LOWPASS_DEFAULT_GAINHF;
467 filter->SetParami = lp_SetParami;
468 filter->SetParamiv = lp_SetParamiv;
469 filter->SetParamf = lp_SetParamf;
470 filter->SetParamfv = lp_SetParamfv;
471 filter->GetParami = lp_GetParami;
472 filter->GetParamiv = lp_GetParamiv;
473 filter->GetParamf = lp_GetParamf;
474 filter->GetParamfv = lp_GetParamfv;
476 else
478 filter->SetParami = null_SetParami;
479 filter->SetParamiv = null_SetParamiv;
480 filter->SetParamf = null_SetParamf;
481 filter->SetParamfv = null_SetParamfv;
482 filter->GetParami = null_GetParami;
483 filter->GetParamiv = null_GetParamiv;
484 filter->GetParamf = null_GetParamf;
485 filter->GetParamfv = null_GetParamfv;
487 filter->type = type;