diff OpenAL32/alFilter.c @ 0:f9476ff7637e

initial forking of open-al to create multiple listeners
author Robert McIntyre <rlm@mit.edu>
date Tue, 25 Oct 2011 13:02:31 -0700
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/OpenAL32/alFilter.c	Tue Oct 25 13:02:31 2011 -0700
     1.3 @@ -0,0 +1,420 @@
     1.4 +/**
     1.5 + * OpenAL cross platform audio library
     1.6 + * Copyright (C) 1999-2007 by authors.
     1.7 + * This library is free software; you can redistribute it and/or
     1.8 + *  modify it under the terms of the GNU Library General Public
     1.9 + *  License as published by the Free Software Foundation; either
    1.10 + *  version 2 of the License, or (at your option) any later version.
    1.11 + *
    1.12 + * This library is distributed in the hope that it will be useful,
    1.13 + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.15 + *  Library General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU Library General Public
    1.18 + *  License along with this library; if not, write to the
    1.19 + *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    1.20 + *  Boston, MA  02111-1307, USA.
    1.21 + * Or go to http://www.gnu.org/copyleft/lgpl.html
    1.22 + */
    1.23 +
    1.24 +#include "config.h"
    1.25 +
    1.26 +#include <stdlib.h>
    1.27 +
    1.28 +#include "AL/al.h"
    1.29 +#include "AL/alc.h"
    1.30 +#include "alMain.h"
    1.31 +#include "alFilter.h"
    1.32 +#include "alThunk.h"
    1.33 +#include "alError.h"
    1.34 +
    1.35 +
    1.36 +static void InitFilterParams(ALfilter *filter, ALenum type);
    1.37 +
    1.38 +#define LookupFilter(m, k) ((ALfilter*)LookupUIntMapKey(&(m), (k)))
    1.39 +
    1.40 +AL_API ALvoid AL_APIENTRY alGenFilters(ALsizei n, ALuint *filters)
    1.41 +{
    1.42 +    ALCcontext *Context;
    1.43 +    ALsizei i=0;
    1.44 +
    1.45 +    Context = GetLockedContext();
    1.46 +    if(!Context) return;
    1.47 +
    1.48 +    if(n < 0 || IsBadWritePtr((void*)filters, n * sizeof(ALuint)))
    1.49 +        alSetError(Context, AL_INVALID_VALUE);
    1.50 +    else
    1.51 +    {
    1.52 +        ALCdevice *device = Context->Device;
    1.53 +        ALenum err;
    1.54 +
    1.55 +        while(i < n)
    1.56 +        {
    1.57 +            ALfilter *filter = calloc(1, sizeof(ALfilter));
    1.58 +            if(!filter)
    1.59 +            {
    1.60 +                alSetError(Context, AL_OUT_OF_MEMORY);
    1.61 +                alDeleteFilters(i, filters);
    1.62 +                break;
    1.63 +            }
    1.64 +
    1.65 +            err = NewThunkEntry(&filter->filter);
    1.66 +            if(err == AL_NO_ERROR)
    1.67 +                err = InsertUIntMapEntry(&device->FilterMap, filter->filter, filter);
    1.68 +            if(err != AL_NO_ERROR)
    1.69 +            {
    1.70 +                FreeThunkEntry(filter->filter);
    1.71 +                memset(filter, 0, sizeof(ALfilter));
    1.72 +                free(filter);
    1.73 +
    1.74 +                alSetError(Context, err);
    1.75 +                alDeleteFilters(i, filters);
    1.76 +                break;
    1.77 +            }
    1.78 +
    1.79 +            filters[i++] = filter->filter;
    1.80 +            InitFilterParams(filter, AL_FILTER_NULL);
    1.81 +        }
    1.82 +    }
    1.83 +
    1.84 +    UnlockContext(Context);
    1.85 +}
    1.86 +
    1.87 +AL_API ALvoid AL_APIENTRY alDeleteFilters(ALsizei n, ALuint *filters)
    1.88 +{
    1.89 +    ALCcontext *Context;
    1.90 +    ALCdevice *device;
    1.91 +    ALfilter *ALFilter;
    1.92 +    ALboolean Failed;
    1.93 +    ALsizei i;
    1.94 +
    1.95 +    Context = GetLockedContext();
    1.96 +    if(!Context) return;
    1.97 +
    1.98 +    Failed = AL_TRUE;
    1.99 +    device = Context->Device;
   1.100 +    if(n < 0)
   1.101 +        alSetError(Context, AL_INVALID_VALUE);
   1.102 +    else
   1.103 +    {
   1.104 +        Failed = AL_FALSE;
   1.105 +        // Check that all filters are valid
   1.106 +        for(i = 0;i < n;i++)
   1.107 +        {
   1.108 +            if(!filters[i])
   1.109 +                continue;
   1.110 +
   1.111 +            if(LookupFilter(device->FilterMap, filters[i]) == NULL)
   1.112 +            {
   1.113 +                alSetError(Context, AL_INVALID_NAME);
   1.114 +                Failed = AL_TRUE;
   1.115 +                break;
   1.116 +            }
   1.117 +        }
   1.118 +    }
   1.119 +
   1.120 +    if(!Failed)
   1.121 +    {
   1.122 +        // All filters are valid
   1.123 +        for(i = 0;i < n;i++)
   1.124 +        {
   1.125 +            // Recheck that the filter is valid, because there could be duplicated names
   1.126 +            if((ALFilter=LookupFilter(device->FilterMap, filters[i])) == NULL)
   1.127 +                continue;
   1.128 +
   1.129 +            RemoveUIntMapKey(&device->FilterMap, ALFilter->filter);
   1.130 +            FreeThunkEntry(ALFilter->filter);
   1.131 +
   1.132 +            memset(ALFilter, 0, sizeof(ALfilter));
   1.133 +            free(ALFilter);
   1.134 +        }
   1.135 +    }
   1.136 +
   1.137 +    UnlockContext(Context);
   1.138 +}
   1.139 +
   1.140 +AL_API ALboolean AL_APIENTRY alIsFilter(ALuint filter)
   1.141 +{
   1.142 +    ALCcontext *Context;
   1.143 +    ALboolean  result;
   1.144 +
   1.145 +    Context = GetLockedContext();
   1.146 +    if(!Context) return AL_FALSE;
   1.147 +
   1.148 +    result = ((!filter || LookupFilter(Context->Device->FilterMap, filter)) ?
   1.149 +              AL_TRUE : AL_FALSE);
   1.150 +
   1.151 +    UnlockContext(Context);
   1.152 +
   1.153 +    return result;
   1.154 +}
   1.155 +
   1.156 +AL_API ALvoid AL_APIENTRY alFilteri(ALuint filter, ALenum param, ALint iValue)
   1.157 +{
   1.158 +    ALCcontext *Context;
   1.159 +    ALCdevice  *Device;
   1.160 +    ALfilter   *ALFilter;
   1.161 +
   1.162 +    Context = GetLockedContext();
   1.163 +    if(!Context) return;
   1.164 +
   1.165 +    Device = Context->Device;
   1.166 +    if((ALFilter=LookupFilter(Device->FilterMap, filter)) != NULL)
   1.167 +    {
   1.168 +        switch(param)
   1.169 +        {
   1.170 +        case AL_FILTER_TYPE:
   1.171 +            if(iValue == AL_FILTER_NULL ||
   1.172 +               iValue == AL_FILTER_LOWPASS)
   1.173 +                InitFilterParams(ALFilter, iValue);
   1.174 +            else
   1.175 +                alSetError(Context, AL_INVALID_VALUE);
   1.176 +            break;
   1.177 +
   1.178 +        default:
   1.179 +            alSetError(Context, AL_INVALID_ENUM);
   1.180 +            break;
   1.181 +        }
   1.182 +    }
   1.183 +    else
   1.184 +        alSetError(Context, AL_INVALID_NAME);
   1.185 +
   1.186 +    UnlockContext(Context);
   1.187 +}
   1.188 +
   1.189 +AL_API ALvoid AL_APIENTRY alFilteriv(ALuint filter, ALenum param, ALint *piValues)
   1.190 +{
   1.191 +    ALCcontext *Context;
   1.192 +    ALCdevice  *Device;
   1.193 +
   1.194 +    switch(param)
   1.195 +    {
   1.196 +        case AL_FILTER_TYPE:
   1.197 +            alFilteri(filter, param, piValues[0]);
   1.198 +            return;
   1.199 +    }
   1.200 +
   1.201 +    Context = GetLockedContext();
   1.202 +    if(!Context) return;
   1.203 +
   1.204 +    Device = Context->Device;
   1.205 +    if(LookupFilter(Device->FilterMap, filter) != NULL)
   1.206 +    {
   1.207 +        switch(param)
   1.208 +        {
   1.209 +        default:
   1.210 +            alSetError(Context, AL_INVALID_ENUM);
   1.211 +            break;
   1.212 +        }
   1.213 +    }
   1.214 +    else
   1.215 +        alSetError(Context, AL_INVALID_NAME);
   1.216 +
   1.217 +    UnlockContext(Context);
   1.218 +}
   1.219 +
   1.220 +AL_API ALvoid AL_APIENTRY alFilterf(ALuint filter, ALenum param, ALfloat flValue)
   1.221 +{
   1.222 +    ALCcontext *Context;
   1.223 +    ALCdevice  *Device;
   1.224 +    ALfilter   *ALFilter;
   1.225 +
   1.226 +    Context = GetLockedContext();
   1.227 +    if(!Context) return;
   1.228 +
   1.229 +    Device = Context->Device;
   1.230 +    if((ALFilter=LookupFilter(Device->FilterMap, filter)) != NULL)
   1.231 +    {
   1.232 +        switch(ALFilter->type)
   1.233 +        {
   1.234 +        case AL_FILTER_LOWPASS:
   1.235 +            switch(param)
   1.236 +            {
   1.237 +            case AL_LOWPASS_GAIN:
   1.238 +                if(flValue >= AL_LOWPASS_MIN_GAIN &&
   1.239 +                   flValue <= AL_LOWPASS_MAX_GAIN)
   1.240 +                    ALFilter->Gain = flValue;
   1.241 +                else
   1.242 +                    alSetError(Context, AL_INVALID_VALUE);
   1.243 +                break;
   1.244 +
   1.245 +            case AL_LOWPASS_GAINHF:
   1.246 +                if(flValue >= AL_LOWPASS_MIN_GAINHF &&
   1.247 +                   flValue <= AL_LOWPASS_MAX_GAINHF)
   1.248 +                    ALFilter->GainHF = flValue;
   1.249 +                else
   1.250 +                    alSetError(Context, AL_INVALID_VALUE);
   1.251 +                break;
   1.252 +
   1.253 +            default:
   1.254 +                alSetError(Context, AL_INVALID_ENUM);
   1.255 +                break;
   1.256 +            }
   1.257 +            break;
   1.258 +
   1.259 +        default:
   1.260 +            alSetError(Context, AL_INVALID_ENUM);
   1.261 +            break;
   1.262 +        }
   1.263 +    }
   1.264 +    else
   1.265 +        alSetError(Context, AL_INVALID_NAME);
   1.266 +
   1.267 +    UnlockContext(Context);
   1.268 +}
   1.269 +
   1.270 +AL_API ALvoid AL_APIENTRY alFilterfv(ALuint filter, ALenum param, ALfloat *pflValues)
   1.271 +{
   1.272 +    /* There are currently no multi-value filter parameters */
   1.273 +    alFilterf(filter, param, pflValues[0]);
   1.274 +}
   1.275 +
   1.276 +AL_API ALvoid AL_APIENTRY alGetFilteri(ALuint filter, ALenum param, ALint *piValue)
   1.277 +{
   1.278 +    ALCcontext *Context;
   1.279 +    ALCdevice  *Device;
   1.280 +    ALfilter   *ALFilter;
   1.281 +
   1.282 +    Context = GetLockedContext();
   1.283 +    if(!Context) return;
   1.284 +
   1.285 +    Device = Context->Device;
   1.286 +    if((ALFilter=LookupFilter(Device->FilterMap, filter)) != NULL)
   1.287 +    {
   1.288 +        switch(param)
   1.289 +        {
   1.290 +        case AL_FILTER_TYPE:
   1.291 +            *piValue = ALFilter->type;
   1.292 +            break;
   1.293 +
   1.294 +        default:
   1.295 +            alSetError(Context, AL_INVALID_ENUM);
   1.296 +            break;
   1.297 +        }
   1.298 +    }
   1.299 +    else
   1.300 +        alSetError(Context, AL_INVALID_NAME);
   1.301 +
   1.302 +    UnlockContext(Context);
   1.303 +}
   1.304 +
   1.305 +AL_API ALvoid AL_APIENTRY alGetFilteriv(ALuint filter, ALenum param, ALint *piValues)
   1.306 +{
   1.307 +    ALCcontext *Context;
   1.308 +    ALCdevice  *Device;
   1.309 +
   1.310 +    switch(param)
   1.311 +    {
   1.312 +        case AL_FILTER_TYPE:
   1.313 +            alGetFilteri(filter, param, piValues);
   1.314 +            return;
   1.315 +    }
   1.316 +
   1.317 +    Context = GetLockedContext();
   1.318 +    if(!Context) return;
   1.319 +
   1.320 +    Device = Context->Device;
   1.321 +    if(LookupFilter(Device->FilterMap, filter) != NULL)
   1.322 +    {
   1.323 +        switch(param)
   1.324 +        {
   1.325 +        default:
   1.326 +            alSetError(Context, AL_INVALID_ENUM);
   1.327 +            break;
   1.328 +        }
   1.329 +    }
   1.330 +    else
   1.331 +        alSetError(Context, AL_INVALID_NAME);
   1.332 +
   1.333 +    UnlockContext(Context);
   1.334 +}
   1.335 +
   1.336 +AL_API ALvoid AL_APIENTRY alGetFilterf(ALuint filter, ALenum param, ALfloat *pflValue)
   1.337 +{
   1.338 +    ALCcontext *Context;
   1.339 +    ALCdevice  *Device;
   1.340 +    ALfilter   *ALFilter;
   1.341 +
   1.342 +    Context = GetLockedContext();
   1.343 +    if(!Context) return;
   1.344 +
   1.345 +    Device = Context->Device;
   1.346 +    if((ALFilter=LookupFilter(Device->FilterMap, filter)) != NULL)
   1.347 +    {
   1.348 +        switch(ALFilter->type)
   1.349 +        {
   1.350 +        case AL_FILTER_LOWPASS:
   1.351 +            switch(param)
   1.352 +            {
   1.353 +            case AL_LOWPASS_GAIN:
   1.354 +                *pflValue = ALFilter->Gain;
   1.355 +                break;
   1.356 +
   1.357 +            case AL_LOWPASS_GAINHF:
   1.358 +                *pflValue = ALFilter->GainHF;
   1.359 +                break;
   1.360 +
   1.361 +            default:
   1.362 +                alSetError(Context, AL_INVALID_ENUM);
   1.363 +                break;
   1.364 +            }
   1.365 +            break;
   1.366 +
   1.367 +        default:
   1.368 +            alSetError(Context, AL_INVALID_ENUM);
   1.369 +            break;
   1.370 +        }
   1.371 +    }
   1.372 +    else
   1.373 +        alSetError(Context, AL_INVALID_NAME);
   1.374 +
   1.375 +    UnlockContext(Context);
   1.376 +}
   1.377 +
   1.378 +AL_API ALvoid AL_APIENTRY alGetFilterfv(ALuint filter, ALenum param, ALfloat *pflValues)
   1.379 +{
   1.380 +    /* There are currently no multi-value filter parameters */
   1.381 +    alGetFilterf(filter, param, pflValues);
   1.382 +}
   1.383 +
   1.384 +
   1.385 +ALfloat lpCoeffCalc(ALfloat g, ALfloat cw)
   1.386 +{
   1.387 +    ALfloat a = 0.0f;
   1.388 +
   1.389 +    /* Be careful with gains < 0.01, as that causes the coefficient
   1.390 +     * head towards 1, which will flatten the signal */
   1.391 +    if(g < 0.9999f) /* 1-epsilon */
   1.392 +    {
   1.393 +        g = maxf(g, 0.01f);
   1.394 +        a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) /
   1.395 +            (1 - g);
   1.396 +    }
   1.397 +
   1.398 +    return a;
   1.399 +}
   1.400 +
   1.401 +ALvoid ReleaseALFilters(ALCdevice *device)
   1.402 +{
   1.403 +    ALsizei i;
   1.404 +    for(i = 0;i < device->FilterMap.size;i++)
   1.405 +    {
   1.406 +        ALfilter *temp = device->FilterMap.array[i].value;
   1.407 +        device->FilterMap.array[i].value = NULL;
   1.408 +
   1.409 +        // Release filter structure
   1.410 +        FreeThunkEntry(temp->filter);
   1.411 +        memset(temp, 0, sizeof(ALfilter));
   1.412 +        free(temp);
   1.413 +    }
   1.414 +}
   1.415 +
   1.416 +
   1.417 +static void InitFilterParams(ALfilter *filter, ALenum type)
   1.418 +{
   1.419 +    filter->type = type;
   1.420 +
   1.421 +    filter->Gain = AL_LOWPASS_DEFAULT_GAIN;
   1.422 +    filter->GainHF = AL_LOWPASS_DEFAULT_GAINHF;
   1.423 +}