effect_position.c
changeset 113 c0c3018bd787
child 114 83ab4ef4458b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/effect_position.c	Tue Sep 11 19:14:36 2001 +0000
     1.3 @@ -0,0 +1,555 @@
     1.4 +/*
     1.5 +    MIXERLIB:  An audio mixer library based on the SDL library
     1.6 +    Copyright (C) 1997-1999  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    This file by Ryan C. Gordon (icculus@linuxgames.com)
    1.23 +
    1.24 +    These are some internally supported special effects that use SDL_mixer's
    1.25 +    effect callback API. They are meant for speed over quality.  :)
    1.26 +*/
    1.27 +
    1.28 +/* $Id$ */
    1.29 +
    1.30 +#include <stdio.h>
    1.31 +#include <stdlib.h>
    1.32 +#include <sys/time.h>
    1.33 +#include <unistd.h>
    1.34 +#include "SDL.h"
    1.35 +#include "SDL_mixer.h"
    1.36 +
    1.37 +#define __MIX_INTERNAL_EFFECT__
    1.38 +#include "effects_internal.h"
    1.39 +
    1.40 +/* profile code:
    1.41 +    #include <sys/time.h>
    1.42 +    #include <unistd.h>
    1.43 +    struct timeval tv1;
    1.44 +    struct timeval tv2;
    1.45 +    
    1.46 +    gettimeofday(&tv1, NULL);
    1.47 +
    1.48 +        ... do your thing here ...
    1.49 +
    1.50 +    gettimeofday(&tv2, NULL);
    1.51 +    printf("%ld\n", tv2.tv_usec - tv1.tv_usec);
    1.52 +*/
    1.53 +
    1.54 +
    1.55 +/*
    1.56 + * Positional effects...panning, distance attenuation, etc.
    1.57 + */
    1.58 +
    1.59 +typedef struct _Eff_positionargs
    1.60 +{
    1.61 +    volatile float left_f;
    1.62 +    volatile float right_f;
    1.63 +    volatile Uint8 left_u8;
    1.64 +    volatile Uint8 right_u8;
    1.65 +    volatile float distance_f;
    1.66 +    volatile Uint8 distance_u8;
    1.67 +    volatile int in_use;
    1.68 +    volatile int channels;
    1.69 +} position_args;
    1.70 +
    1.71 +static position_args **pos_args_array = NULL;
    1.72 +static position_args *pos_args_global = NULL;
    1.73 +static int position_channels = 0;
    1.74 +
    1.75 +/* This just frees up the callback-specific data. */
    1.76 +static void _Eff_PositionDone(int channel, void *udata)
    1.77 +{
    1.78 +    if (channel < 0) {
    1.79 +        if (pos_args_global != NULL) {
    1.80 +            free(pos_args_global);
    1.81 +            pos_args_global = NULL;
    1.82 +        }
    1.83 +    }
    1.84 +
    1.85 +    else if (pos_args_array[channel] != NULL) {
    1.86 +        free(pos_args_array[channel]);
    1.87 +        pos_args_array[channel] = NULL;
    1.88 +    }
    1.89 +}
    1.90 +
    1.91 +
    1.92 +static void _Eff_position_u8(int chan, void *stream, int len, void *udata)
    1.93 +{
    1.94 +    volatile position_args *args = (volatile position_args *) udata;
    1.95 +    Uint8 *ptr = (Uint8 *) stream;
    1.96 +    int i;
    1.97 +
    1.98 +        /*
    1.99 +         * if there's only a mono channnel (the only way we wouldn't have
   1.100 +         *  a len divisible by 2 here), then left_f and right_f are always
   1.101 +         *  1.0, and are therefore throwaways.
   1.102 +         */
   1.103 +    if (len % sizeof (Uint16) != 0) {
   1.104 +        *(ptr++) = (Uint8) (((float) *ptr) * args->distance_f);
   1.105 +        len--;
   1.106 +    }
   1.107 +
   1.108 +    for (i = 0; i < len; i += sizeof (Uint8) * 2) {
   1.109 +        *(ptr++) = (Uint8)((((float) *ptr) * args->left_f) * args->distance_f);
   1.110 +        *(ptr++) = (Uint8)((((float) *ptr) * args->right_f) * args->distance_f);
   1.111 +    }
   1.112 +}
   1.113 +
   1.114 +
   1.115 +/*
   1.116 + * This one runs about 10.1 times faster than the non-table version, with
   1.117 + *  no loss in quality. It does, however, require 64k of memory for the
   1.118 + *  lookup table. Also, this will only update position information once per
   1.119 + *  call; the non-table version always checks the arguments for each sample,
   1.120 + *  in case the user has called Mix_SetPanning() or whatnot again while this
   1.121 + *  callback is running.
   1.122 + */
   1.123 +static void _Eff_position_table_u8(int chan, void *stream, int len, void *udata)
   1.124 +{
   1.125 +    volatile position_args *args = (volatile position_args *) udata;
   1.126 +    Uint8 *ptr = (Uint8 *) stream;
   1.127 +    Uint32 *p;
   1.128 +    int i;
   1.129 +    Uint8 *l = ((Uint8 *) _Eff_volume_table) + (256 * args->left_u8);
   1.130 +    Uint8 *r = ((Uint8 *) _Eff_volume_table) + (256 * args->right_u8);
   1.131 +    Uint8 *d = ((Uint8 *) _Eff_volume_table) + (256 * args->distance_u8);
   1.132 +
   1.133 +        /*
   1.134 +         * if there's only a mono channnel, then l[] and r[] are always
   1.135 +         *  volume 255, and are therefore throwaways. Still, we have to
   1.136 +         *  be sure not to overrun the audio buffer...
   1.137 +         */
   1.138 +    while (len % sizeof (Uint32) != 0) {
   1.139 +        *(ptr++) = d[l[*ptr]];
   1.140 +        if (args->channels == 2)
   1.141 +            *(ptr++) = d[r[*ptr]];
   1.142 +        len -= args->channels;
   1.143 +    }
   1.144 +
   1.145 +    p = (Uint32 *) ptr;
   1.146 +
   1.147 +    for (i = 0; i < len; i += sizeof (Uint32)) {
   1.148 +#if (SDL_BYTE_ORDER == SDL_BIG_ENDIAN)
   1.149 +        *(p++) = (d[l[(*p & 0xFF000000) >> 24]] << 24) |
   1.150 +                 (d[r[(*p & 0x00FF0000) >> 16]] << 16) |
   1.151 +                 (d[l[(*p & 0x0000FF00) >>  8]] <<  8) |
   1.152 +                 (d[r[(*p & 0x000000FF)      ]]      ) ;
   1.153 +#else
   1.154 +        *(p++) = (d[r[(*p & 0xFF000000) >> 24]] << 24) |
   1.155 +                 (d[l[(*p & 0x00FF0000) >> 16]] << 16) |
   1.156 +                 (d[r[(*p & 0x0000FF00) >>  8]] <<  8) |
   1.157 +                 (d[l[(*p & 0x000000FF)      ]]      ) ;
   1.158 +#endif
   1.159 +    }
   1.160 +}
   1.161 +
   1.162 +
   1.163 +static void _Eff_position_s8(int chan, void *stream, int len, void *udata)
   1.164 +{
   1.165 +    volatile position_args *args = (volatile position_args *) udata;
   1.166 +    Sint8 *ptr = (Sint8 *) stream;
   1.167 +    int i;
   1.168 +
   1.169 +        /*
   1.170 +         * if there's only a mono channnel (the only way we wouldn't have
   1.171 +         *  a len divisible by 2 here), then left_f and right_f are always
   1.172 +         *  1.0, and are therefore throwaways.
   1.173 +         */
   1.174 +    if (len % sizeof (Sint16) != 0) {
   1.175 +        *(ptr++) = (Sint8) (((float) *ptr) * args->distance_f);
   1.176 +        len--;
   1.177 +    }
   1.178 +
   1.179 +    for (i = 0; i < len; i += sizeof (Sint8) * 2) {
   1.180 +        *(ptr++) = (Sint8)((((float) *ptr) * args->left_f) * args->distance_f);
   1.181 +        *(ptr++) = (Sint8)((((float) *ptr) * args->right_f) * args->distance_f);
   1.182 +    }
   1.183 +}
   1.184 +
   1.185 +
   1.186 +/*
   1.187 + * This one runs about 10.1 times faster than the non-table version, with
   1.188 + *  no loss in quality. It does, however, require 64k of memory for the
   1.189 + *  lookup table. Also, this will only update position information once per
   1.190 + *  call; the non-table version always checks the arguments for each sample,
   1.191 + *  in case the user has called Mix_SetPanning() or whatnot again while this
   1.192 + *  callback is running.
   1.193 + */
   1.194 +static void _Eff_position_table_s8(int chan, void *stream, int len, void *udata)
   1.195 +{
   1.196 +    volatile position_args *args = (volatile position_args *) udata;
   1.197 +    Sint8 *ptr = (Sint8 *) stream;
   1.198 +    Uint32 *p;
   1.199 +    int i;
   1.200 +    Sint8 *l = ((Uint8 *) _Eff_volume_table) + (256 * args->left_u8);
   1.201 +    Sint8 *r = ((Uint8 *) _Eff_volume_table) + (256 * args->right_u8);
   1.202 +    Sint8 *d = ((Sint8 *) _Eff_volume_table) + (256 * args->distance_u8);
   1.203 +
   1.204 +
   1.205 +    while (len % sizeof (Uint32) != 0) {
   1.206 +        *(ptr++) = d[l[*ptr]];
   1.207 +        if (args->channels > 1)
   1.208 +            *(ptr++) = d[r[*ptr]];
   1.209 +        len -= args->channels;
   1.210 +    }
   1.211 +
   1.212 +    p = (Uint32 *) ptr;
   1.213 +
   1.214 +    for (i = 0; i < len; i += sizeof (Uint32)) {
   1.215 +#if (SDL_BYTE_ORDER == SDL_BIG_ENDIAN)
   1.216 +        *(p++) = (d[l[((Sint16)(Sint8)((*p & 0xFF000000) >> 24))+128]] << 24) |
   1.217 +                 (d[r[((Sint16)(Sint8)((*p & 0x00FF0000) >> 16))+128]] << 16) |
   1.218 +                 (d[l[((Sint16)(Sint8)((*p & 0x0000FF00) >>  8))+128]] <<  8) |
   1.219 +                 (d[r[((Sint16)(Sint8)((*p & 0x000000FF)      ))+128]]      ) ;
   1.220 +#else
   1.221 +        *(p++) = (d[r[((Sint16)(Sint8)((*p & 0xFF000000) >> 24))+128]] << 24) |
   1.222 +                 (d[l[((Sint16)(Sint8)((*p & 0x00FF0000) >> 16))+128]] << 16) |
   1.223 +                 (d[r[((Sint16)(Sint8)((*p & 0x0000FF00) >>  8))+128]] <<  8) |
   1.224 +                 (d[l[((Sint16)(Sint8)((*p & 0x000000FF)      ))+128]]      ) ;
   1.225 +#endif
   1.226 +    }
   1.227 +
   1.228 +
   1.229 +}
   1.230 +
   1.231 +
   1.232 +/* !!! FIXME : Optimize the code for 16-bit samples? */
   1.233 +
   1.234 +static void _Eff_position_u16lsb(int chan, void *stream, int len, void *udata)
   1.235 +{
   1.236 +    volatile position_args *args = (volatile position_args *) udata;
   1.237 +    Uint16 *ptr = (Uint16 *) stream;
   1.238 +    int i;
   1.239 +
   1.240 +    for (i = 0; i < len; i += sizeof (Uint16) * 2) {
   1.241 +#if (SDL_BYTE_ORDER == SDL_BIG_ENDIAN)
   1.242 +        Uint16 swapl = (Uint16) ((((float) SDL_Swap16(*(ptr))) *
   1.243 +                                    args->left_f) * args->distance_f);
   1.244 +        Uint16 swapr = (Uint16) (((float) SDL_Swap16(*(ptr+1))) *
   1.245 +                                    args->right_f) * args->distance_f);
   1.246 +        *(ptr++) = (Uint16) SDL_Swap16(swapl);
   1.247 +        *(ptr++) = (Uint16) SDL_Swap16(swapr);
   1.248 +#else
   1.249 +        *(ptr++) = (Uint16) ((((float) *ptr)*args->left_f)*args->distance_f);
   1.250 +        *(ptr++) = (Uint16) ((((float) *ptr)*args->right_f)*args->distance_f);
   1.251 +#endif
   1.252 +    }
   1.253 +}
   1.254 +
   1.255 +static void _Eff_position_s16lsb(int chan, void *stream, int len, void *udata)
   1.256 +{
   1.257 +    /* 16 signed bits (lsb) * 2 channels. */
   1.258 +    volatile position_args *args = (volatile position_args *) udata;
   1.259 +    Sint16 *ptr = (Sint16 *) stream;
   1.260 +    int i;
   1.261 +
   1.262 +    for (i = 0; i < len; i += sizeof (Sint16) * 2) {
   1.263 +#if (SDL_BYTE_ORDER == SDL_BIG_ENDIAN)
   1.264 +        Sint16 swapl = (Sint16) ((((float) SDL_Swap16(*(ptr))) *
   1.265 +                                    args->left_f) * args->distance_f);
   1.266 +        Sint16 swapr = (Sint16) (((float) SDL_Swap16(*(ptr+1))) *
   1.267 +                                    args->right_f) * args->distance_f);
   1.268 +        *(ptr++) = (Sint16) SDL_Swap16(swapl);
   1.269 +        *(ptr++) = (Sint16) SDL_Swap16(swapr);
   1.270 +#else
   1.271 +        *(ptr++) = (Sint16) ((((float) *ptr)*args->left_f)*args->distance_f);
   1.272 +        *(ptr++) = (Sint16) ((((float) *ptr)*args->right_f)*args->distance_f);
   1.273 +#endif
   1.274 +    }
   1.275 +}
   1.276 +
   1.277 +static void _Eff_position_u16msb(int chan, void *stream, int len, void *udata)
   1.278 +{
   1.279 +    /* 16 signed bits (lsb) * 2 channels. */
   1.280 +    volatile position_args *args = (volatile position_args *) udata;
   1.281 +    Uint16 *ptr = (Uint16 *) stream;
   1.282 +    int i;
   1.283 +
   1.284 +    for (i = 0; i < len; i += sizeof (Sint16) * 2) {
   1.285 +#if (SDL_BYTE_ORDER == SDL_LIL_ENDIAN)
   1.286 +        Uint16 swapl = (Uint16) ((((float) SDL_Swap16(*(ptr))) *
   1.287 +                                    args->left_f) * args->distance_f);
   1.288 +        Uint16 swapr = (Uint16) (((float) SDL_Swap16(*(ptr+1))) *
   1.289 +                                    args->right_f) * args->distance_f);
   1.290 +        *(ptr++) = (Uint16) SDL_Swap16(swapl);
   1.291 +        *(ptr++) = (Uint16) SDL_Swap16(swapr);
   1.292 +#else
   1.293 +        *(ptr++) = (Uint16) ((((float) *ptr)*args->left_f)*args->distance_f);
   1.294 +        *(ptr++) = (Uint16) ((((float) *ptr)*args->right_f)*args->distance_f);
   1.295 +#endif
   1.296 +    }
   1.297 +}
   1.298 +
   1.299 +static void _Eff_position_s16msb(int chan, void *stream, int len, void *udata)
   1.300 +{
   1.301 +    /* 16 signed bits (lsb) * 2 channels. */
   1.302 +    volatile position_args *args = (volatile position_args *) udata;
   1.303 +    Sint16 *ptr = (Sint16 *) stream;
   1.304 +    int i;
   1.305 +
   1.306 +    for (i = 0; i < len; i += sizeof (Sint16) * 2) {
   1.307 +#if (SDL_BYTE_ORDER == SDL_LIL_ENDIAN)
   1.308 +        Sint16 swapl = (Sint16) ((((float) SDL_Swap16(*(ptr))) *
   1.309 +                                    args->left_f) * args->distance_f);
   1.310 +        Sint16 swapr = (Sint16) (((float) SDL_Swap16(*(ptr+1))) *
   1.311 +                                    args->right_f) * args->distance_f);
   1.312 +        *(ptr++) = (Sint16) SDL_Swap16(swapl);
   1.313 +        *(ptr++) = (Sint16) SDL_Swap16(swapr);
   1.314 +#else
   1.315 +        *(ptr++) = (Sint16) ((((float) *ptr)*args->left_f)*args->distance_f);
   1.316 +        *(ptr++) = (Sint16) ((((float) *ptr)*args->right_f)*args->distance_f);
   1.317 +#endif
   1.318 +    }
   1.319 +}
   1.320 +
   1.321 +
   1.322 +static void init_position_args(position_args *args)
   1.323 +{
   1.324 +    memset(args, '\0', sizeof (position_args));
   1.325 +    args->in_use = 0;
   1.326 +    args->left_u8 = args->right_u8 = args->distance_u8 = 255;
   1.327 +    args->left_f  = args->right_f  = args->distance_f  = 1.0f;
   1.328 +    Mix_QuerySpec(NULL, NULL, (int *) &args->channels);
   1.329 +}
   1.330 +
   1.331 +
   1.332 +static position_args *get_position_arg(int channel)
   1.333 +{
   1.334 +    void *rc;
   1.335 +    int i;
   1.336 +
   1.337 +    if (channel < 0) {
   1.338 +        if (pos_args_global == NULL) {
   1.339 +            pos_args_global = malloc(sizeof (position_args));
   1.340 +            if (pos_args_global == NULL) {
   1.341 +                Mix_SetError("Out of memory");
   1.342 +                return(NULL);
   1.343 +            }
   1.344 +            init_position_args(pos_args_global);
   1.345 +        }
   1.346 +
   1.347 +        return(pos_args_global);
   1.348 +    }
   1.349 +
   1.350 +    if (channel >= position_channels) {
   1.351 +        rc = realloc(pos_args_array, (channel + 1) * sizeof (position_args *));
   1.352 +        if (rc == NULL) {
   1.353 +            Mix_SetError("Out of memory");
   1.354 +            return(NULL);
   1.355 +        }
   1.356 +        pos_args_array = (position_args **) rc;
   1.357 +        for (i = position_channels; i <= channel; i++) {
   1.358 +            pos_args_array[i] = NULL;
   1.359 +        }
   1.360 +        position_channels = channel + 1;
   1.361 +    }
   1.362 +
   1.363 +    if (pos_args_array[channel] == NULL) {
   1.364 +        pos_args_array[channel] = (position_args *)malloc(sizeof(position_args));
   1.365 +        if (pos_args_array[channel] == NULL) {
   1.366 +            Mix_SetError("Out of memory");
   1.367 +            return(NULL);
   1.368 +        }
   1.369 +        init_position_args(pos_args_array[channel]);
   1.370 +    }
   1.371 +
   1.372 +    return(pos_args_array[channel]);
   1.373 +}
   1.374 +
   1.375 +
   1.376 +static Mix_EffectFunc_t get_position_effect_func(Uint16 format)
   1.377 +{
   1.378 +    Mix_EffectFunc_t f = NULL;
   1.379 +
   1.380 +    switch (format) {
   1.381 +        case AUDIO_U8:
   1.382 +            f = (_Eff_build_volume_table_u8()) ? _Eff_position_table_u8 :
   1.383 +                                                 _Eff_position_u8;
   1.384 +            break;
   1.385 +
   1.386 +        case AUDIO_S8:
   1.387 +            f = (_Eff_build_volume_table_s8()) ? _Eff_position_table_s8 :
   1.388 +                                                 _Eff_position_s8;
   1.389 +            break;
   1.390 +
   1.391 +        case AUDIO_U16LSB:
   1.392 +            f = _Eff_position_u16lsb;
   1.393 +            break;
   1.394 +
   1.395 +        case AUDIO_S16LSB:
   1.396 +            f = _Eff_position_s16lsb;
   1.397 +            break;
   1.398 +
   1.399 +        case AUDIO_U16MSB:
   1.400 +            f = _Eff_position_u16msb;
   1.401 +            break;
   1.402 +
   1.403 +        case AUDIO_S16MSB:
   1.404 +            f = _Eff_position_s16msb;
   1.405 +            break;
   1.406 +
   1.407 +        default:
   1.408 +            Mix_SetError("Unsupported audio format");
   1.409 +    }
   1.410 +
   1.411 +    return(f);
   1.412 +}
   1.413 +
   1.414 +
   1.415 +int Mix_SetPanning(int channel, Uint8 left, Uint8 right)
   1.416 +{
   1.417 +    Mix_EffectFunc_t f = NULL;
   1.418 +    int channels;
   1.419 +    Uint16 format;
   1.420 +    position_args *args = NULL;
   1.421 +
   1.422 +    Mix_QuerySpec(NULL, &format, &channels);
   1.423 +
   1.424 +    if (channels != 2)    /* it's a no-op; we call that successful. */
   1.425 +        return(1);
   1.426 +
   1.427 +    f = get_position_effect_func(format);
   1.428 +    if (f == NULL)
   1.429 +        return(0);
   1.430 +
   1.431 +    args = get_position_arg(channel);
   1.432 +    if (!args)
   1.433 +        return(0);
   1.434 +
   1.435 +        /* it's a no-op; unregister the effect, if it's registered. */
   1.436 +    if ((args->distance_u8 == 255) && (left == 255) &&
   1.437 +        (right == 255) && (args->in_use))
   1.438 +    {
   1.439 +        return(Mix_UnregisterEffect(channel, f));
   1.440 +    }
   1.441 +
   1.442 +    args->left_u8 = left;
   1.443 +    args->right_u8 = right;
   1.444 +    args->left_f = ((float) left) / 255.0;
   1.445 +    args->right_f = ((float) right) / 255.0;
   1.446 +    if (!args->in_use) {
   1.447 +        args->in_use = 1;
   1.448 +        return(Mix_RegisterEffect(channel, f, _Eff_PositionDone, (void *) args));
   1.449 +    }
   1.450 +
   1.451 +    return(1);
   1.452 +}
   1.453 +
   1.454 +
   1.455 +int Mix_SetDistance(int channel, Uint8 distance)
   1.456 +{
   1.457 +    Mix_EffectFunc_t f = NULL;
   1.458 +    Uint16 format;
   1.459 +    position_args *args = NULL;
   1.460 +
   1.461 +    Mix_QuerySpec(NULL, &format, NULL);
   1.462 +    f = get_position_effect_func(format);
   1.463 +    if (f == NULL)
   1.464 +        return(0);
   1.465 +
   1.466 +    args = get_position_arg(channel);
   1.467 +    if (!args)
   1.468 +        return(0);
   1.469 +
   1.470 +    distance = 255 - distance;  /* flip it to our scale. */
   1.471 +
   1.472 +        /* it's a no-op; unregister the effect, if it's registered. */
   1.473 +    if ((distance == 255) && (args->left_u8 == 255) &&
   1.474 +        (args->right_u8 == 255) && (args->in_use))
   1.475 +    {
   1.476 +        return(Mix_UnregisterEffect(channel, f));
   1.477 +    }
   1.478 +
   1.479 +    args->distance_u8 = distance;
   1.480 +    args->distance_f = ((float) distance) / 255.0;
   1.481 +    if (!args->in_use) {
   1.482 +        args->in_use = 1;
   1.483 +        return(Mix_RegisterEffect(channel, f, _Eff_PositionDone, (void *) args));
   1.484 +    }
   1.485 +
   1.486 +    return(1);
   1.487 +}
   1.488 +
   1.489 +
   1.490 +int Mix_SetPosition(int channel, Sint16 angle, Uint8 distance)
   1.491 +{
   1.492 +    Mix_EffectFunc_t f = NULL;
   1.493 +    Uint16 format;
   1.494 +    int channels;
   1.495 +    position_args *args = NULL;
   1.496 +    Uint8 left = 255, right = 255;
   1.497 +    int flip;
   1.498 +    float angle_f;
   1.499 +
   1.500 +    Mix_QuerySpec(NULL, &format, &channels);
   1.501 +    f = get_position_effect_func(format);
   1.502 +    if (f == NULL)
   1.503 +        return(0);
   1.504 +
   1.505 +        /* unwind the angle...it'll be between 0 and 359. */
   1.506 +    while (angle >= 360) angle -= 360;
   1.507 +    while (angle < 0) angle += 360;
   1.508 +
   1.509 +    args = get_position_arg(channel);
   1.510 +    if (!args)
   1.511 +        return(0);
   1.512 +
   1.513 +        /* it's a no-op; unregister the effect, if it's registered. */
   1.514 +    if ((!distance) && (!angle) && (args->in_use))
   1.515 +        return(Mix_UnregisterEffect(channel, f));
   1.516 +
   1.517 +    if (channels == 2)
   1.518 +    {
   1.519 +        /*
   1.520 +         * We only attenuate by position if the angle falls on the far side
   1.521 +         *  of center; That is, an angle that's due north would not attenuate
   1.522 +         *  either channel. Due west attenuates the right channel to 0.0, and
   1.523 +         *  due east attenuates the left channel to 0.0. Slightly east of
   1.524 +         *  center attenuates the left channel a little, and the right channel
   1.525 +         *  not at all. I think of this as occlusion by one's own head.  :)
   1.526 +         *
   1.527 +         *   ...so, we split our angle circle into four quadrants...
   1.528 +         */
   1.529 +        if (angle < 90) {
   1.530 +            left = 255 - ((Uint8) (255.0f * (((float) angle) / 89.0f)));
   1.531 +        } else if (angle < 180) {
   1.532 +            left = (Uint8) (255.0f * (((float) (angle - 90)) / 89.0f));
   1.533 +        } else if (angle < 270) {
   1.534 +            right = 255 - ((Uint8) (255.0f * (((float) (angle - 180)) / 89.0f)));
   1.535 +        } else {
   1.536 +            right = (Uint8) (255.0f * (((float) (angle - 270)) / 89.0f));
   1.537 +        }
   1.538 +    }
   1.539 +
   1.540 +    distance = 255 - distance;  /* flip it to scale Mix_SetDistance() uses. */
   1.541 +
   1.542 +    args->left_u8 = left;
   1.543 +    args->left_f = ((float) left) / 255.0;
   1.544 +    args->right_u8 = right;
   1.545 +    args->right_f = ((float) right) / 255.0;
   1.546 +    args->distance_u8 = distance;
   1.547 +    args->distance_f = ((float) distance) / 255.0;
   1.548 +    if (!args->in_use) {
   1.549 +        args->in_use = 1;
   1.550 +        return(Mix_RegisterEffect(channel, f, _Eff_PositionDone, (void *) args));
   1.551 +    }
   1.552 +
   1.553 +    return(1);
   1.554 +}
   1.555 +
   1.556 +
   1.557 +/* end of effects_position.c ... */
   1.558 +