Modify some context checks
[openal-soft.git] / OpenAL32 / alFilter.c
blobd2b3ff62e2c4962ba8016d920554e748f229150e
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);
36 ALvoid AL_APIENTRY alGenFilters(ALsizei n, ALuint *filters)
38 ALCcontext *Context;
39 ALsizei i;
41 Context = GetContextSuspended();
43 if (n > 0)
45 ALCdevice *device = Context->Device;
47 // Check that enough memory has been allocted in the 'filters' array for n Filters
48 if (!IsBadWritePtr((void*)filters, n * sizeof(ALuint)))
50 ALfilter **list = &device->FilterList;
51 while(*list)
52 list = &(*list)->next;
54 i = 0;
55 while(i < n)
57 *list = calloc(1, sizeof(ALfilter));
58 if(!(*list))
60 // We must have run out or memory
61 alDeleteFilters(i, filters);
62 alSetError(AL_OUT_OF_MEMORY);
63 break;
66 filters[i] = (ALuint)ALTHUNK_ADDENTRY(*list);
67 (*list)->filter = filters[i];
69 InitFilterParams(*list, AL_FILTER_NULL);
70 device->FilterCount++;
71 i++;
73 list = &(*list)->next;
78 ProcessContext(Context);
81 ALvoid AL_APIENTRY alDeleteFilters(ALsizei n, ALuint *filters)
83 ALCcontext *Context;
84 ALfilter *ALFilter;
85 ALsizei i;
87 Context = GetContextSuspended();
89 if (n >= 0)
91 ALCdevice *device = Context->Device;
93 // Check that all filters are valid
94 for (i = 0; i < n; i++)
96 if (!alIsFilter(filters[i]))
98 alSetError(AL_INVALID_NAME);
99 break;
103 if (i == n)
105 // All filters are valid
106 for (i = 0; i < n; i++)
108 // Recheck that the filter is valid, because there could be duplicated names
109 if (filters[i] && alIsFilter(filters[i]))
111 ALfilter **list;
113 ALFilter = ((ALfilter*)ALTHUNK_LOOKUPENTRY(filters[i]));
115 // Remove Source from list of Sources
116 list = &device->FilterList;
117 while(*list && *list != ALFilter)
118 list = &(*list)->next;
120 if(*list)
121 *list = (*list)->next;
122 ALTHUNK_REMOVEENTRY(ALFilter->filter);
124 memset(ALFilter, 0, sizeof(ALfilter));
125 free(ALFilter);
127 device->FilterCount--;
132 else
133 alSetError(AL_INVALID_VALUE);
135 ProcessContext(Context);
138 ALboolean AL_APIENTRY alIsFilter(ALuint filter)
140 ALCcontext *Context;
141 ALfilter *list;
143 Context = GetContextSuspended();
145 list = Context->Device->FilterList;
146 while(list && list->filter != filter)
147 list = list->next;
149 ProcessContext(Context);
151 return ((list || !filter) ? AL_TRUE : AL_FALSE);
154 ALvoid AL_APIENTRY alFilteri(ALuint filter, ALenum param, ALint iValue)
156 ALCcontext *Context;
158 Context = GetContextSuspended();
160 if (filter && alIsFilter(filter))
162 ALfilter *ALFilter = (ALfilter*)ALTHUNK_LOOKUPENTRY(filter);
164 switch(param)
166 case AL_FILTER_TYPE:
167 if(iValue == AL_FILTER_NULL ||
168 iValue == AL_FILTER_LOWPASS)
169 InitFilterParams(ALFilter, iValue);
170 else
171 alSetError(AL_INVALID_VALUE);
172 break;
174 default:
175 alSetError(AL_INVALID_ENUM);
176 break;
179 else
180 alSetError(AL_INVALID_NAME);
182 ProcessContext(Context);
185 ALvoid AL_APIENTRY alFilteriv(ALuint filter, ALenum param, ALint *piValues)
187 ALCcontext *Context;
189 Context = GetContextSuspended();
191 if (filter && alIsFilter(filter))
193 switch(param)
195 case AL_FILTER_TYPE:
196 alFilteri(filter, param, piValues[0]);
197 break;
199 default:
200 alSetError(AL_INVALID_ENUM);
201 break;
204 else
205 alSetError(AL_INVALID_NAME);
207 ProcessContext(Context);
210 ALvoid AL_APIENTRY alFilterf(ALuint filter, ALenum param, ALfloat flValue)
212 ALCcontext *Context;
214 Context = GetContextSuspended();
216 if (filter && alIsFilter(filter))
218 ALfilter *ALFilter = (ALfilter*)ALTHUNK_LOOKUPENTRY(filter);
220 switch(ALFilter->type)
222 case AL_FILTER_LOWPASS:
223 switch(param)
225 case AL_LOWPASS_GAIN:
226 if(flValue >= 0.0f && flValue <= 1.0f)
227 ALFilter->Gain = flValue;
228 else
229 alSetError(AL_INVALID_VALUE);
230 break;
232 case AL_LOWPASS_GAINHF:
233 if(flValue >= 0.0f && flValue <= 1.0f)
234 ALFilter->GainHF = flValue;
235 else
236 alSetError(AL_INVALID_VALUE);
237 break;
239 default:
240 alSetError(AL_INVALID_ENUM);
241 break;
243 break;
245 default:
246 alSetError(AL_INVALID_ENUM);
247 break;
250 else
251 alSetError(AL_INVALID_NAME);
253 ProcessContext(Context);
256 ALvoid AL_APIENTRY alFilterfv(ALuint filter, ALenum param, ALfloat *pflValues)
258 ALCcontext *Context;
260 Context = GetContextSuspended();
262 if (filter && alIsFilter(filter))
264 switch(param)
266 default:
267 alFilterf(filter, param, pflValues[0]);
268 break;
271 else
272 alSetError(AL_INVALID_NAME);
274 ProcessContext(Context);
277 ALvoid AL_APIENTRY alGetFilteri(ALuint filter, ALenum param, ALint *piValue)
279 ALCcontext *Context;
281 Context = GetContextSuspended();
283 if (filter && alIsFilter(filter))
285 ALfilter *ALFilter = (ALfilter*)ALTHUNK_LOOKUPENTRY(filter);
287 switch(param)
289 case AL_FILTER_TYPE:
290 *piValue = ALFilter->type;
291 break;
293 default:
294 alSetError(AL_INVALID_ENUM);
295 break;
298 else
299 alSetError(AL_INVALID_NAME);
301 ProcessContext(Context);
304 ALvoid AL_APIENTRY alGetFilteriv(ALuint filter, ALenum param, ALint *piValues)
306 ALCcontext *Context;
308 Context = GetContextSuspended();
310 if (filter && alIsFilter(filter))
312 switch(param)
314 case AL_FILTER_TYPE:
315 alGetFilteri(filter, param, piValues);
316 break;
318 default:
319 alSetError(AL_INVALID_ENUM);
320 break;
323 else
324 alSetError(AL_INVALID_NAME);
326 ProcessContext(Context);
329 ALvoid AL_APIENTRY alGetFilterf(ALuint filter, ALenum param, ALfloat *pflValue)
331 ALCcontext *Context;
333 Context = GetContextSuspended();
335 if (filter && alIsFilter(filter))
337 ALfilter *ALFilter = (ALfilter*)ALTHUNK_LOOKUPENTRY(filter);
339 switch(ALFilter->type)
341 case AL_FILTER_LOWPASS:
342 switch(param)
344 case AL_LOWPASS_GAIN:
345 *pflValue = ALFilter->Gain;
346 break;
348 case AL_LOWPASS_GAINHF:
349 *pflValue = ALFilter->GainHF;
350 break;
352 default:
353 alSetError(AL_INVALID_ENUM);
354 break;
356 break;
358 default:
359 alSetError(AL_INVALID_ENUM);
360 break;
363 else
364 alSetError(AL_INVALID_NAME);
366 ProcessContext(Context);
369 ALvoid AL_APIENTRY alGetFilterfv(ALuint filter, ALenum param, ALfloat *pflValues)
371 ALCcontext *Context;
373 Context = GetContextSuspended();
375 if (filter && alIsFilter(filter))
377 switch(param)
379 default:
380 alGetFilterf(filter, param, pflValues);
381 break;
384 else
385 alSetError(AL_INVALID_NAME);
387 ProcessContext(Context);
391 ALvoid ReleaseALFilters(ALCdevice *device)
393 ALfilter *list = device->FilterList;
394 while(list)
396 ALfilter *temp = list;
397 list = list->next;
399 // Release filter structure
400 memset(temp, 0, sizeof(ALfilter));
401 free(temp);
403 device->FilterList = NULL;
404 device->FilterCount = 0;
408 static void InitFilterParams(ALfilter *filter, ALenum type)
410 filter->type = type;
412 filter->Gain = 1.0;
413 filter->GainHF = 1.0;