annotate OpenAL32/alFilter.c @ 28:1fc162d84343

updated README
author Robert McIntyre <rlm@mit.edu>
date Sat, 19 Nov 2011 20:09:50 -0700
parents f9476ff7637e
children
rev   line source
rlm@0 1 /**
rlm@0 2 * OpenAL cross platform audio library
rlm@0 3 * Copyright (C) 1999-2007 by authors.
rlm@0 4 * This library is free software; you can redistribute it and/or
rlm@0 5 * modify it under the terms of the GNU Library General Public
rlm@0 6 * License as published by the Free Software Foundation; either
rlm@0 7 * version 2 of the License, or (at your option) any later version.
rlm@0 8 *
rlm@0 9 * This library is distributed in the hope that it will be useful,
rlm@0 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rlm@0 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
rlm@0 12 * Library General Public License for more details.
rlm@0 13 *
rlm@0 14 * You should have received a copy of the GNU Library General Public
rlm@0 15 * License along with this library; if not, write to the
rlm@0 16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
rlm@0 17 * Boston, MA 02111-1307, USA.
rlm@0 18 * Or go to http://www.gnu.org/copyleft/lgpl.html
rlm@0 19 */
rlm@0 20
rlm@0 21 #include "config.h"
rlm@0 22
rlm@0 23 #include <stdlib.h>
rlm@0 24
rlm@0 25 #include "AL/al.h"
rlm@0 26 #include "AL/alc.h"
rlm@0 27 #include "alMain.h"
rlm@0 28 #include "alFilter.h"
rlm@0 29 #include "alThunk.h"
rlm@0 30 #include "alError.h"
rlm@0 31
rlm@0 32
rlm@0 33 static void InitFilterParams(ALfilter *filter, ALenum type);
rlm@0 34
rlm@0 35 #define LookupFilter(m, k) ((ALfilter*)LookupUIntMapKey(&(m), (k)))
rlm@0 36
rlm@0 37 AL_API ALvoid AL_APIENTRY alGenFilters(ALsizei n, ALuint *filters)
rlm@0 38 {
rlm@0 39 ALCcontext *Context;
rlm@0 40 ALsizei i=0;
rlm@0 41
rlm@0 42 Context = GetLockedContext();
rlm@0 43 if(!Context) return;
rlm@0 44
rlm@0 45 if(n < 0 || IsBadWritePtr((void*)filters, n * sizeof(ALuint)))
rlm@0 46 alSetError(Context, AL_INVALID_VALUE);
rlm@0 47 else
rlm@0 48 {
rlm@0 49 ALCdevice *device = Context->Device;
rlm@0 50 ALenum err;
rlm@0 51
rlm@0 52 while(i < n)
rlm@0 53 {
rlm@0 54 ALfilter *filter = calloc(1, sizeof(ALfilter));
rlm@0 55 if(!filter)
rlm@0 56 {
rlm@0 57 alSetError(Context, AL_OUT_OF_MEMORY);
rlm@0 58 alDeleteFilters(i, filters);
rlm@0 59 break;
rlm@0 60 }
rlm@0 61
rlm@0 62 err = NewThunkEntry(&filter->filter);
rlm@0 63 if(err == AL_NO_ERROR)
rlm@0 64 err = InsertUIntMapEntry(&device->FilterMap, filter->filter, filter);
rlm@0 65 if(err != AL_NO_ERROR)
rlm@0 66 {
rlm@0 67 FreeThunkEntry(filter->filter);
rlm@0 68 memset(filter, 0, sizeof(ALfilter));
rlm@0 69 free(filter);
rlm@0 70
rlm@0 71 alSetError(Context, err);
rlm@0 72 alDeleteFilters(i, filters);
rlm@0 73 break;
rlm@0 74 }
rlm@0 75
rlm@0 76 filters[i++] = filter->filter;
rlm@0 77 InitFilterParams(filter, AL_FILTER_NULL);
rlm@0 78 }
rlm@0 79 }
rlm@0 80
rlm@0 81 UnlockContext(Context);
rlm@0 82 }
rlm@0 83
rlm@0 84 AL_API ALvoid AL_APIENTRY alDeleteFilters(ALsizei n, ALuint *filters)
rlm@0 85 {
rlm@0 86 ALCcontext *Context;
rlm@0 87 ALCdevice *device;
rlm@0 88 ALfilter *ALFilter;
rlm@0 89 ALboolean Failed;
rlm@0 90 ALsizei i;
rlm@0 91
rlm@0 92 Context = GetLockedContext();
rlm@0 93 if(!Context) return;
rlm@0 94
rlm@0 95 Failed = AL_TRUE;
rlm@0 96 device = Context->Device;
rlm@0 97 if(n < 0)
rlm@0 98 alSetError(Context, AL_INVALID_VALUE);
rlm@0 99 else
rlm@0 100 {
rlm@0 101 Failed = AL_FALSE;
rlm@0 102 // Check that all filters are valid
rlm@0 103 for(i = 0;i < n;i++)
rlm@0 104 {
rlm@0 105 if(!filters[i])
rlm@0 106 continue;
rlm@0 107
rlm@0 108 if(LookupFilter(device->FilterMap, filters[i]) == NULL)
rlm@0 109 {
rlm@0 110 alSetError(Context, AL_INVALID_NAME);
rlm@0 111 Failed = AL_TRUE;
rlm@0 112 break;
rlm@0 113 }
rlm@0 114 }
rlm@0 115 }
rlm@0 116
rlm@0 117 if(!Failed)
rlm@0 118 {
rlm@0 119 // All filters are valid
rlm@0 120 for(i = 0;i < n;i++)
rlm@0 121 {
rlm@0 122 // Recheck that the filter is valid, because there could be duplicated names
rlm@0 123 if((ALFilter=LookupFilter(device->FilterMap, filters[i])) == NULL)
rlm@0 124 continue;
rlm@0 125
rlm@0 126 RemoveUIntMapKey(&device->FilterMap, ALFilter->filter);
rlm@0 127 FreeThunkEntry(ALFilter->filter);
rlm@0 128
rlm@0 129 memset(ALFilter, 0, sizeof(ALfilter));
rlm@0 130 free(ALFilter);
rlm@0 131 }
rlm@0 132 }
rlm@0 133
rlm@0 134 UnlockContext(Context);
rlm@0 135 }
rlm@0 136
rlm@0 137 AL_API ALboolean AL_APIENTRY alIsFilter(ALuint filter)
rlm@0 138 {
rlm@0 139 ALCcontext *Context;
rlm@0 140 ALboolean result;
rlm@0 141
rlm@0 142 Context = GetLockedContext();
rlm@0 143 if(!Context) return AL_FALSE;
rlm@0 144
rlm@0 145 result = ((!filter || LookupFilter(Context->Device->FilterMap, filter)) ?
rlm@0 146 AL_TRUE : AL_FALSE);
rlm@0 147
rlm@0 148 UnlockContext(Context);
rlm@0 149
rlm@0 150 return result;
rlm@0 151 }
rlm@0 152
rlm@0 153 AL_API ALvoid AL_APIENTRY alFilteri(ALuint filter, ALenum param, ALint iValue)
rlm@0 154 {
rlm@0 155 ALCcontext *Context;
rlm@0 156 ALCdevice *Device;
rlm@0 157 ALfilter *ALFilter;
rlm@0 158
rlm@0 159 Context = GetLockedContext();
rlm@0 160 if(!Context) return;
rlm@0 161
rlm@0 162 Device = Context->Device;
rlm@0 163 if((ALFilter=LookupFilter(Device->FilterMap, filter)) != NULL)
rlm@0 164 {
rlm@0 165 switch(param)
rlm@0 166 {
rlm@0 167 case AL_FILTER_TYPE:
rlm@0 168 if(iValue == AL_FILTER_NULL ||
rlm@0 169 iValue == AL_FILTER_LOWPASS)
rlm@0 170 InitFilterParams(ALFilter, iValue);
rlm@0 171 else
rlm@0 172 alSetError(Context, AL_INVALID_VALUE);
rlm@0 173 break;
rlm@0 174
rlm@0 175 default:
rlm@0 176 alSetError(Context, AL_INVALID_ENUM);
rlm@0 177 break;
rlm@0 178 }
rlm@0 179 }
rlm@0 180 else
rlm@0 181 alSetError(Context, AL_INVALID_NAME);
rlm@0 182
rlm@0 183 UnlockContext(Context);
rlm@0 184 }
rlm@0 185
rlm@0 186 AL_API ALvoid AL_APIENTRY alFilteriv(ALuint filter, ALenum param, ALint *piValues)
rlm@0 187 {
rlm@0 188 ALCcontext *Context;
rlm@0 189 ALCdevice *Device;
rlm@0 190
rlm@0 191 switch(param)
rlm@0 192 {
rlm@0 193 case AL_FILTER_TYPE:
rlm@0 194 alFilteri(filter, param, piValues[0]);
rlm@0 195 return;
rlm@0 196 }
rlm@0 197
rlm@0 198 Context = GetLockedContext();
rlm@0 199 if(!Context) return;
rlm@0 200
rlm@0 201 Device = Context->Device;
rlm@0 202 if(LookupFilter(Device->FilterMap, filter) != NULL)
rlm@0 203 {
rlm@0 204 switch(param)
rlm@0 205 {
rlm@0 206 default:
rlm@0 207 alSetError(Context, AL_INVALID_ENUM);
rlm@0 208 break;
rlm@0 209 }
rlm@0 210 }
rlm@0 211 else
rlm@0 212 alSetError(Context, AL_INVALID_NAME);
rlm@0 213
rlm@0 214 UnlockContext(Context);
rlm@0 215 }
rlm@0 216
rlm@0 217 AL_API ALvoid AL_APIENTRY alFilterf(ALuint filter, ALenum param, ALfloat flValue)
rlm@0 218 {
rlm@0 219 ALCcontext *Context;
rlm@0 220 ALCdevice *Device;
rlm@0 221 ALfilter *ALFilter;
rlm@0 222
rlm@0 223 Context = GetLockedContext();
rlm@0 224 if(!Context) return;
rlm@0 225
rlm@0 226 Device = Context->Device;
rlm@0 227 if((ALFilter=LookupFilter(Device->FilterMap, filter)) != NULL)
rlm@0 228 {
rlm@0 229 switch(ALFilter->type)
rlm@0 230 {
rlm@0 231 case AL_FILTER_LOWPASS:
rlm@0 232 switch(param)
rlm@0 233 {
rlm@0 234 case AL_LOWPASS_GAIN:
rlm@0 235 if(flValue >= AL_LOWPASS_MIN_GAIN &&
rlm@0 236 flValue <= AL_LOWPASS_MAX_GAIN)
rlm@0 237 ALFilter->Gain = flValue;
rlm@0 238 else
rlm@0 239 alSetError(Context, AL_INVALID_VALUE);
rlm@0 240 break;
rlm@0 241
rlm@0 242 case AL_LOWPASS_GAINHF:
rlm@0 243 if(flValue >= AL_LOWPASS_MIN_GAINHF &&
rlm@0 244 flValue <= AL_LOWPASS_MAX_GAINHF)
rlm@0 245 ALFilter->GainHF = flValue;
rlm@0 246 else
rlm@0 247 alSetError(Context, AL_INVALID_VALUE);
rlm@0 248 break;
rlm@0 249
rlm@0 250 default:
rlm@0 251 alSetError(Context, AL_INVALID_ENUM);
rlm@0 252 break;
rlm@0 253 }
rlm@0 254 break;
rlm@0 255
rlm@0 256 default:
rlm@0 257 alSetError(Context, AL_INVALID_ENUM);
rlm@0 258 break;
rlm@0 259 }
rlm@0 260 }
rlm@0 261 else
rlm@0 262 alSetError(Context, AL_INVALID_NAME);
rlm@0 263
rlm@0 264 UnlockContext(Context);
rlm@0 265 }
rlm@0 266
rlm@0 267 AL_API ALvoid AL_APIENTRY alFilterfv(ALuint filter, ALenum param, ALfloat *pflValues)
rlm@0 268 {
rlm@0 269 /* There are currently no multi-value filter parameters */
rlm@0 270 alFilterf(filter, param, pflValues[0]);
rlm@0 271 }
rlm@0 272
rlm@0 273 AL_API ALvoid AL_APIENTRY alGetFilteri(ALuint filter, ALenum param, ALint *piValue)
rlm@0 274 {
rlm@0 275 ALCcontext *Context;
rlm@0 276 ALCdevice *Device;
rlm@0 277 ALfilter *ALFilter;
rlm@0 278
rlm@0 279 Context = GetLockedContext();
rlm@0 280 if(!Context) return;
rlm@0 281
rlm@0 282 Device = Context->Device;
rlm@0 283 if((ALFilter=LookupFilter(Device->FilterMap, filter)) != NULL)
rlm@0 284 {
rlm@0 285 switch(param)
rlm@0 286 {
rlm@0 287 case AL_FILTER_TYPE:
rlm@0 288 *piValue = ALFilter->type;
rlm@0 289 break;
rlm@0 290
rlm@0 291 default:
rlm@0 292 alSetError(Context, AL_INVALID_ENUM);
rlm@0 293 break;
rlm@0 294 }
rlm@0 295 }
rlm@0 296 else
rlm@0 297 alSetError(Context, AL_INVALID_NAME);
rlm@0 298
rlm@0 299 UnlockContext(Context);
rlm@0 300 }
rlm@0 301
rlm@0 302 AL_API ALvoid AL_APIENTRY alGetFilteriv(ALuint filter, ALenum param, ALint *piValues)
rlm@0 303 {
rlm@0 304 ALCcontext *Context;
rlm@0 305 ALCdevice *Device;
rlm@0 306
rlm@0 307 switch(param)
rlm@0 308 {
rlm@0 309 case AL_FILTER_TYPE:
rlm@0 310 alGetFilteri(filter, param, piValues);
rlm@0 311 return;
rlm@0 312 }
rlm@0 313
rlm@0 314 Context = GetLockedContext();
rlm@0 315 if(!Context) return;
rlm@0 316
rlm@0 317 Device = Context->Device;
rlm@0 318 if(LookupFilter(Device->FilterMap, filter) != NULL)
rlm@0 319 {
rlm@0 320 switch(param)
rlm@0 321 {
rlm@0 322 default:
rlm@0 323 alSetError(Context, AL_INVALID_ENUM);
rlm@0 324 break;
rlm@0 325 }
rlm@0 326 }
rlm@0 327 else
rlm@0 328 alSetError(Context, AL_INVALID_NAME);
rlm@0 329
rlm@0 330 UnlockContext(Context);
rlm@0 331 }
rlm@0 332
rlm@0 333 AL_API ALvoid AL_APIENTRY alGetFilterf(ALuint filter, ALenum param, ALfloat *pflValue)
rlm@0 334 {
rlm@0 335 ALCcontext *Context;
rlm@0 336 ALCdevice *Device;
rlm@0 337 ALfilter *ALFilter;
rlm@0 338
rlm@0 339 Context = GetLockedContext();
rlm@0 340 if(!Context) return;
rlm@0 341
rlm@0 342 Device = Context->Device;
rlm@0 343 if((ALFilter=LookupFilter(Device->FilterMap, filter)) != NULL)
rlm@0 344 {
rlm@0 345 switch(ALFilter->type)
rlm@0 346 {
rlm@0 347 case AL_FILTER_LOWPASS:
rlm@0 348 switch(param)
rlm@0 349 {
rlm@0 350 case AL_LOWPASS_GAIN:
rlm@0 351 *pflValue = ALFilter->Gain;
rlm@0 352 break;
rlm@0 353
rlm@0 354 case AL_LOWPASS_GAINHF:
rlm@0 355 *pflValue = ALFilter->GainHF;
rlm@0 356 break;
rlm@0 357
rlm@0 358 default:
rlm@0 359 alSetError(Context, AL_INVALID_ENUM);
rlm@0 360 break;
rlm@0 361 }
rlm@0 362 break;
rlm@0 363
rlm@0 364 default:
rlm@0 365 alSetError(Context, AL_INVALID_ENUM);
rlm@0 366 break;
rlm@0 367 }
rlm@0 368 }
rlm@0 369 else
rlm@0 370 alSetError(Context, AL_INVALID_NAME);
rlm@0 371
rlm@0 372 UnlockContext(Context);
rlm@0 373 }
rlm@0 374
rlm@0 375 AL_API ALvoid AL_APIENTRY alGetFilterfv(ALuint filter, ALenum param, ALfloat *pflValues)
rlm@0 376 {
rlm@0 377 /* There are currently no multi-value filter parameters */
rlm@0 378 alGetFilterf(filter, param, pflValues);
rlm@0 379 }
rlm@0 380
rlm@0 381
rlm@0 382 ALfloat lpCoeffCalc(ALfloat g, ALfloat cw)
rlm@0 383 {
rlm@0 384 ALfloat a = 0.0f;
rlm@0 385
rlm@0 386 /* Be careful with gains < 0.01, as that causes the coefficient
rlm@0 387 * head towards 1, which will flatten the signal */
rlm@0 388 if(g < 0.9999f) /* 1-epsilon */
rlm@0 389 {
rlm@0 390 g = maxf(g, 0.01f);
rlm@0 391 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) /
rlm@0 392 (1 - g);
rlm@0 393 }
rlm@0 394
rlm@0 395 return a;
rlm@0 396 }
rlm@0 397
rlm@0 398 ALvoid ReleaseALFilters(ALCdevice *device)
rlm@0 399 {
rlm@0 400 ALsizei i;
rlm@0 401 for(i = 0;i < device->FilterMap.size;i++)
rlm@0 402 {
rlm@0 403 ALfilter *temp = device->FilterMap.array[i].value;
rlm@0 404 device->FilterMap.array[i].value = NULL;
rlm@0 405
rlm@0 406 // Release filter structure
rlm@0 407 FreeThunkEntry(temp->filter);
rlm@0 408 memset(temp, 0, sizeof(ALfilter));
rlm@0 409 free(temp);
rlm@0 410 }
rlm@0 411 }
rlm@0 412
rlm@0 413
rlm@0 414 static void InitFilterParams(ALfilter *filter, ALenum type)
rlm@0 415 {
rlm@0 416 filter->type = type;
rlm@0 417
rlm@0 418 filter->Gain = AL_LOWPASS_DEFAULT_GAIN;
rlm@0 419 filter->GainHF = AL_LOWPASS_DEFAULT_GAINHF;
rlm@0 420 }