effect_position.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Jan 2004 17:41:55 +0000
changeset 241 503416fca921
parent 238 2eae977be9d0
child 245 63b3650714de
permissions -rw-r--r--
Updated copyright information for 2004 (Happy New Year!)
slouken@113
     1
/*
slouken@138
     2
    SDL_mixer:  An audio mixer library based on the SDL library
slouken@241
     3
    Copyright (C) 1997-2004 Sam Lantinga
slouken@113
     4
slouken@113
     5
    This library is free software; you can redistribute it and/or
slouken@113
     6
    modify it under the terms of the GNU Library General Public
slouken@113
     7
    License as published by the Free Software Foundation; either
slouken@113
     8
    version 2 of the License, or (at your option) any later version.
slouken@113
     9
slouken@113
    10
    This library is distributed in the hope that it will be useful,
slouken@113
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@113
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@113
    13
    Library General Public License for more details.
slouken@113
    14
slouken@113
    15
    You should have received a copy of the GNU Library General Public
slouken@113
    16
    License along with this library; if not, write to the Free
slouken@113
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@113
    18
slouken@113
    19
    This file by Ryan C. Gordon (icculus@linuxgames.com)
slouken@113
    20
slouken@113
    21
    These are some internally supported special effects that use SDL_mixer's
slouken@113
    22
    effect callback API. They are meant for speed over quality.  :)
slouken@113
    23
*/
slouken@113
    24
slouken@113
    25
/* $Id$ */
slouken@113
    26
slouken@113
    27
#include <stdio.h>
slouken@113
    28
#include <stdlib.h>
slouken@114
    29
#include <string.h>
slouken@114
    30
slouken@113
    31
#include "SDL.h"
slouken@113
    32
#include "SDL_mixer.h"
slouken@159
    33
#include "SDL_endian.h"
slouken@113
    34
slouken@113
    35
#define __MIX_INTERNAL_EFFECT__
slouken@113
    36
#include "effects_internal.h"
slouken@113
    37
slouken@113
    38
/* profile code:
slouken@113
    39
    #include <sys/time.h>
slouken@113
    40
    #include <unistd.h>
slouken@113
    41
    struct timeval tv1;
slouken@113
    42
    struct timeval tv2;
slouken@113
    43
    
slouken@113
    44
    gettimeofday(&tv1, NULL);
slouken@113
    45
slouken@113
    46
        ... do your thing here ...
slouken@113
    47
slouken@113
    48
    gettimeofday(&tv2, NULL);
slouken@113
    49
    printf("%ld\n", tv2.tv_usec - tv1.tv_usec);
slouken@113
    50
*/
slouken@113
    51
slouken@113
    52
slouken@113
    53
/*
slouken@113
    54
 * Positional effects...panning, distance attenuation, etc.
slouken@113
    55
 */
slouken@113
    56
slouken@113
    57
typedef struct _Eff_positionargs
slouken@113
    58
{
slouken@113
    59
    volatile float left_f;
slouken@113
    60
    volatile float right_f;
slouken@113
    61
    volatile Uint8 left_u8;
slouken@113
    62
    volatile Uint8 right_u8;
slouken@113
    63
    volatile float distance_f;
slouken@113
    64
    volatile Uint8 distance_u8;
slouken@113
    65
    volatile int in_use;
slouken@113
    66
    volatile int channels;
slouken@113
    67
} position_args;
slouken@113
    68
slouken@113
    69
static position_args **pos_args_array = NULL;
slouken@113
    70
static position_args *pos_args_global = NULL;
slouken@113
    71
static int position_channels = 0;
slouken@113
    72
slouken@113
    73
/* This just frees up the callback-specific data. */
slouken@113
    74
static void _Eff_PositionDone(int channel, void *udata)
slouken@113
    75
{
slouken@113
    76
    if (channel < 0) {
slouken@113
    77
        if (pos_args_global != NULL) {
slouken@113
    78
            free(pos_args_global);
slouken@113
    79
            pos_args_global = NULL;
slouken@113
    80
        }
slouken@113
    81
    }
slouken@113
    82
slouken@113
    83
    else if (pos_args_array[channel] != NULL) {
slouken@113
    84
        free(pos_args_array[channel]);
slouken@113
    85
        pos_args_array[channel] = NULL;
slouken@113
    86
    }
slouken@113
    87
}
slouken@113
    88
slouken@113
    89
slouken@113
    90
static void _Eff_position_u8(int chan, void *stream, int len, void *udata)
slouken@113
    91
{
slouken@113
    92
    volatile position_args *args = (volatile position_args *) udata;
slouken@113
    93
    Uint8 *ptr = (Uint8 *) stream;
slouken@113
    94
    int i;
slouken@113
    95
slouken@113
    96
        /*
slouken@113
    97
         * if there's only a mono channnel (the only way we wouldn't have
slouken@113
    98
         *  a len divisible by 2 here), then left_f and right_f are always
slouken@113
    99
         *  1.0, and are therefore throwaways.
slouken@113
   100
         */
slouken@113
   101
    if (len % sizeof (Uint16) != 0) {
slouken@218
   102
        *ptr = (Uint8) (((float) *ptr) * args->distance_f);
slouken@218
   103
        ptr++;
slouken@113
   104
        len--;
slouken@113
   105
    }
slouken@113
   106
slouken@113
   107
    for (i = 0; i < len; i += sizeof (Uint8) * 2) {
icculus@201
   108
        /* must adjust the sample so that 0 is the center */
slouken@218
   109
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
icculus@201
   110
            * args->left_f) * args->distance_f) + 128);
slouken@218
   111
        ptr++;
slouken@218
   112
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
icculus@201
   113
            * args->right_f) * args->distance_f) + 128);
slouken@218
   114
        ptr++;
slouken@113
   115
    }
slouken@113
   116
}
slouken@113
   117
slouken@113
   118
slouken@113
   119
/*
slouken@113
   120
 * This one runs about 10.1 times faster than the non-table version, with
slouken@113
   121
 *  no loss in quality. It does, however, require 64k of memory for the
slouken@113
   122
 *  lookup table. Also, this will only update position information once per
slouken@113
   123
 *  call; the non-table version always checks the arguments for each sample,
slouken@113
   124
 *  in case the user has called Mix_SetPanning() or whatnot again while this
slouken@113
   125
 *  callback is running.
slouken@113
   126
 */
slouken@113
   127
static void _Eff_position_table_u8(int chan, void *stream, int len, void *udata)
slouken@113
   128
{
slouken@113
   129
    volatile position_args *args = (volatile position_args *) udata;
slouken@113
   130
    Uint8 *ptr = (Uint8 *) stream;
slouken@113
   131
    Uint32 *p;
slouken@113
   132
    int i;
slouken@113
   133
    Uint8 *l = ((Uint8 *) _Eff_volume_table) + (256 * args->left_u8);
slouken@113
   134
    Uint8 *r = ((Uint8 *) _Eff_volume_table) + (256 * args->right_u8);
slouken@113
   135
    Uint8 *d = ((Uint8 *) _Eff_volume_table) + (256 * args->distance_u8);
slouken@113
   136
slouken@113
   137
        /*
slouken@113
   138
         * if there's only a mono channnel, then l[] and r[] are always
slouken@113
   139
         *  volume 255, and are therefore throwaways. Still, we have to
slouken@113
   140
         *  be sure not to overrun the audio buffer...
slouken@113
   141
         */
slouken@113
   142
    while (len % sizeof (Uint32) != 0) {
slouken@218
   143
        *ptr = d[l[*ptr]];
slouken@218
   144
        ptr++;
slouken@218
   145
        if (args->channels > 1) {
slouken@218
   146
            *ptr = d[r[*ptr]];
slouken@218
   147
            ptr++;
slouken@218
   148
        }
slouken@113
   149
        len -= args->channels;
slouken@113
   150
    }
slouken@113
   151
slouken@113
   152
    p = (Uint32 *) ptr;
slouken@113
   153
slouken@113
   154
    for (i = 0; i < len; i += sizeof (Uint32)) {
slouken@159
   155
#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
slouken@113
   156
        *(p++) = (d[l[(*p & 0xFF000000) >> 24]] << 24) |
slouken@113
   157
                 (d[r[(*p & 0x00FF0000) >> 16]] << 16) |
slouken@113
   158
                 (d[l[(*p & 0x0000FF00) >>  8]] <<  8) |
slouken@113
   159
                 (d[r[(*p & 0x000000FF)      ]]      ) ;
slouken@113
   160
#else
slouken@113
   161
        *(p++) = (d[r[(*p & 0xFF000000) >> 24]] << 24) |
slouken@113
   162
                 (d[l[(*p & 0x00FF0000) >> 16]] << 16) |
slouken@113
   163
                 (d[r[(*p & 0x0000FF00) >>  8]] <<  8) |
slouken@113
   164
                 (d[l[(*p & 0x000000FF)      ]]      ) ;
slouken@113
   165
#endif
slouken@113
   166
    }
slouken@113
   167
}
slouken@113
   168
slouken@113
   169
slouken@113
   170
static void _Eff_position_s8(int chan, void *stream, int len, void *udata)
slouken@113
   171
{
slouken@113
   172
    volatile position_args *args = (volatile position_args *) udata;
slouken@113
   173
    Sint8 *ptr = (Sint8 *) stream;
slouken@113
   174
    int i;
slouken@113
   175
slouken@113
   176
        /*
slouken@113
   177
         * if there's only a mono channnel (the only way we wouldn't have
slouken@113
   178
         *  a len divisible by 2 here), then left_f and right_f are always
slouken@113
   179
         *  1.0, and are therefore throwaways.
slouken@113
   180
         */
slouken@113
   181
    if (len % sizeof (Sint16) != 0) {
slouken@218
   182
        *ptr = (Sint8) (((float) *ptr) * args->distance_f);
slouken@218
   183
        ptr++;
slouken@113
   184
        len--;
slouken@113
   185
    }
slouken@113
   186
slouken@113
   187
    for (i = 0; i < len; i += sizeof (Sint8) * 2) {
slouken@218
   188
        *ptr = (Sint8)((((float) *ptr) * args->left_f) * args->distance_f);
slouken@218
   189
        ptr++;
slouken@218
   190
        *ptr = (Sint8)((((float) *ptr) * args->right_f) * args->distance_f);
slouken@218
   191
        ptr++;
slouken@113
   192
    }
slouken@113
   193
}
slouken@113
   194
slouken@113
   195
slouken@113
   196
/*
slouken@113
   197
 * This one runs about 10.1 times faster than the non-table version, with
slouken@113
   198
 *  no loss in quality. It does, however, require 64k of memory for the
slouken@113
   199
 *  lookup table. Also, this will only update position information once per
slouken@113
   200
 *  call; the non-table version always checks the arguments for each sample,
slouken@113
   201
 *  in case the user has called Mix_SetPanning() or whatnot again while this
slouken@113
   202
 *  callback is running.
slouken@113
   203
 */
slouken@113
   204
static void _Eff_position_table_s8(int chan, void *stream, int len, void *udata)
slouken@113
   205
{
slouken@113
   206
    volatile position_args *args = (volatile position_args *) udata;
slouken@113
   207
    Sint8 *ptr = (Sint8 *) stream;
slouken@113
   208
    Uint32 *p;
slouken@113
   209
    int i;
slouken@132
   210
    Sint8 *l = ((Sint8 *) _Eff_volume_table) + (256 * args->left_u8);
slouken@132
   211
    Sint8 *r = ((Sint8 *) _Eff_volume_table) + (256 * args->right_u8);
slouken@113
   212
    Sint8 *d = ((Sint8 *) _Eff_volume_table) + (256 * args->distance_u8);
slouken@113
   213
slouken@113
   214
slouken@113
   215
    while (len % sizeof (Uint32) != 0) {
slouken@218
   216
        *ptr = d[l[*ptr]];
slouken@218
   217
        ptr++;
slouken@218
   218
        if (args->channels > 1) {
slouken@218
   219
            *ptr = d[r[*ptr]];
slouken@218
   220
            ptr++;
slouken@218
   221
        }
slouken@113
   222
        len -= args->channels;
slouken@113
   223
    }
slouken@113
   224
slouken@113
   225
    p = (Uint32 *) ptr;
slouken@113
   226
slouken@113
   227
    for (i = 0; i < len; i += sizeof (Uint32)) {
slouken@159
   228
#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
slouken@113
   229
        *(p++) = (d[l[((Sint16)(Sint8)((*p & 0xFF000000) >> 24))+128]] << 24) |
slouken@113
   230
                 (d[r[((Sint16)(Sint8)((*p & 0x00FF0000) >> 16))+128]] << 16) |
slouken@113
   231
                 (d[l[((Sint16)(Sint8)((*p & 0x0000FF00) >>  8))+128]] <<  8) |
slouken@113
   232
                 (d[r[((Sint16)(Sint8)((*p & 0x000000FF)      ))+128]]      ) ;
slouken@113
   233
#else
slouken@113
   234
        *(p++) = (d[r[((Sint16)(Sint8)((*p & 0xFF000000) >> 24))+128]] << 24) |
slouken@113
   235
                 (d[l[((Sint16)(Sint8)((*p & 0x00FF0000) >> 16))+128]] << 16) |
slouken@113
   236
                 (d[r[((Sint16)(Sint8)((*p & 0x0000FF00) >>  8))+128]] <<  8) |
slouken@113
   237
                 (d[l[((Sint16)(Sint8)((*p & 0x000000FF)      ))+128]]      ) ;
slouken@113
   238
#endif
slouken@113
   239
    }
slouken@113
   240
slouken@113
   241
slouken@113
   242
}
slouken@113
   243
slouken@113
   244
slouken@113
   245
/* !!! FIXME : Optimize the code for 16-bit samples? */
slouken@113
   246
slouken@113
   247
static void _Eff_position_u16lsb(int chan, void *stream, int len, void *udata)
slouken@113
   248
{
slouken@113
   249
    volatile position_args *args = (volatile position_args *) udata;
slouken@113
   250
    Uint16 *ptr = (Uint16 *) stream;
slouken@113
   251
    int i;
slouken@113
   252
slouken@113
   253
    for (i = 0; i < len; i += sizeof (Uint16) * 2) {
slouken@218
   254
        Sint16 sampl = (Sint16) (SDL_SwapLE16(*(ptr+0)) - 32768);
slouken@218
   255
        Sint16 sampr = (Sint16) (SDL_SwapLE16(*(ptr+1)) - 32768);
icculus@201
   256
        
icculus@201
   257
        Uint16 swapl = (Uint16) ((Sint16) (((float) sampl * args->left_f)
icculus@201
   258
                                    * args->distance_f) + 32768);
icculus@201
   259
        Uint16 swapr = (Uint16) ((Sint16) (((float) sampr * args->right_f)
icculus@201
   260
                                    * args->distance_f) + 32768);
icculus@201
   261
slouken@218
   262
        *(ptr++) = (Uint16) SDL_SwapLE16(swapl);
slouken@218
   263
        *(ptr++) = (Uint16) SDL_SwapLE16(swapr);
slouken@113
   264
    }
slouken@113
   265
}
slouken@113
   266
slouken@113
   267
static void _Eff_position_s16lsb(int chan, void *stream, int len, void *udata)
slouken@113
   268
{
slouken@113
   269
    /* 16 signed bits (lsb) * 2 channels. */
slouken@113
   270
    volatile position_args *args = (volatile position_args *) udata;
slouken@113
   271
    Sint16 *ptr = (Sint16 *) stream;
slouken@113
   272
    int i;
slouken@113
   273
slouken@113
   274
    for (i = 0; i < len; i += sizeof (Sint16) * 2) {
icculus@238
   275
        Sint16 swapl = (Sint16) ((((float) (Sint16) SDL_SwapLE16(*(ptr+0))) *
slouken@113
   276
                                    args->left_f) * args->distance_f);
icculus@238
   277
        Sint16 swapr = (Sint16) ((((float) (Sint16) SDL_SwapLE16(*(ptr+1))) *
slouken@113
   278
                                    args->right_f) * args->distance_f);
slouken@218
   279
        *(ptr++) = (Sint16) SDL_SwapLE16(swapl);
slouken@218
   280
        *(ptr++) = (Sint16) SDL_SwapLE16(swapr);
slouken@113
   281
    }
slouken@113
   282
}
slouken@113
   283
slouken@113
   284
static void _Eff_position_u16msb(int chan, void *stream, int len, void *udata)
slouken@113
   285
{
slouken@113
   286
    /* 16 signed bits (lsb) * 2 channels. */
slouken@113
   287
    volatile position_args *args = (volatile position_args *) udata;
slouken@113
   288
    Uint16 *ptr = (Uint16 *) stream;
slouken@113
   289
    int i;
slouken@113
   290
slouken@113
   291
    for (i = 0; i < len; i += sizeof (Sint16) * 2) {
slouken@218
   292
        Sint16 sampl = (Sint16) (SDL_SwapBE16(*(ptr+0)) - 32768);
slouken@218
   293
        Sint16 sampr = (Sint16) (SDL_SwapBE16(*(ptr+1)) - 32768);
icculus@201
   294
        
icculus@201
   295
        Uint16 swapl = (Uint16) ((Sint16) (((float) sampl * args->left_f)
icculus@201
   296
                                    * args->distance_f) + 32768);
icculus@201
   297
        Uint16 swapr = (Uint16) ((Sint16) (((float) sampr * args->right_f)
icculus@201
   298
                                    * args->distance_f) + 32768);
icculus@201
   299
slouken@218
   300
        *(ptr++) = (Uint16) SDL_SwapBE16(swapl);
slouken@218
   301
        *(ptr++) = (Uint16) SDL_SwapBE16(swapr);
slouken@113
   302
    }
slouken@113
   303
}
slouken@113
   304
slouken@113
   305
static void _Eff_position_s16msb(int chan, void *stream, int len, void *udata)
slouken@113
   306
{
slouken@113
   307
    /* 16 signed bits (lsb) * 2 channels. */
slouken@113
   308
    volatile position_args *args = (volatile position_args *) udata;
slouken@113
   309
    Sint16 *ptr = (Sint16 *) stream;
slouken@113
   310
    int i;
slouken@113
   311
slouken@113
   312
    for (i = 0; i < len; i += sizeof (Sint16) * 2) {
icculus@238
   313
        Sint16 swapl = (Sint16) ((((float) (Sint16) SDL_SwapBE16(*(ptr+0))) *
slouken@113
   314
                                    args->left_f) * args->distance_f);
icculus@238
   315
        Sint16 swapr = (Sint16) ((((float) (Sint16) SDL_SwapBE16(*(ptr+1))) *
slouken@113
   316
                                    args->right_f) * args->distance_f);
slouken@218
   317
        *(ptr++) = (Sint16) SDL_SwapBE16(swapl);
slouken@218
   318
        *(ptr++) = (Sint16) SDL_SwapBE16(swapr);
slouken@113
   319
    }
slouken@113
   320
}
slouken@113
   321
slouken@113
   322
slouken@113
   323
static void init_position_args(position_args *args)
slouken@113
   324
{
slouken@113
   325
    memset(args, '\0', sizeof (position_args));
slouken@113
   326
    args->in_use = 0;
slouken@113
   327
    args->left_u8 = args->right_u8 = args->distance_u8 = 255;
slouken@113
   328
    args->left_f  = args->right_f  = args->distance_f  = 1.0f;
slouken@113
   329
    Mix_QuerySpec(NULL, NULL, (int *) &args->channels);
slouken@113
   330
}
slouken@113
   331
slouken@113
   332
slouken@113
   333
static position_args *get_position_arg(int channel)
slouken@113
   334
{
slouken@113
   335
    void *rc;
slouken@113
   336
    int i;
slouken@113
   337
slouken@113
   338
    if (channel < 0) {
slouken@113
   339
        if (pos_args_global == NULL) {
slouken@113
   340
            pos_args_global = malloc(sizeof (position_args));
slouken@113
   341
            if (pos_args_global == NULL) {
slouken@113
   342
                Mix_SetError("Out of memory");
slouken@113
   343
                return(NULL);
slouken@113
   344
            }
slouken@113
   345
            init_position_args(pos_args_global);
slouken@113
   346
        }
slouken@113
   347
slouken@113
   348
        return(pos_args_global);
slouken@113
   349
    }
slouken@113
   350
slouken@113
   351
    if (channel >= position_channels) {
slouken@113
   352
        rc = realloc(pos_args_array, (channel + 1) * sizeof (position_args *));
slouken@113
   353
        if (rc == NULL) {
slouken@113
   354
            Mix_SetError("Out of memory");
slouken@113
   355
            return(NULL);
slouken@113
   356
        }
slouken@113
   357
        pos_args_array = (position_args **) rc;
slouken@113
   358
        for (i = position_channels; i <= channel; i++) {
slouken@113
   359
            pos_args_array[i] = NULL;
slouken@113
   360
        }
slouken@113
   361
        position_channels = channel + 1;
slouken@113
   362
    }
slouken@113
   363
slouken@113
   364
    if (pos_args_array[channel] == NULL) {
slouken@113
   365
        pos_args_array[channel] = (position_args *)malloc(sizeof(position_args));
slouken@113
   366
        if (pos_args_array[channel] == NULL) {
slouken@113
   367
            Mix_SetError("Out of memory");
slouken@113
   368
            return(NULL);
slouken@113
   369
        }
slouken@113
   370
        init_position_args(pos_args_array[channel]);
slouken@113
   371
    }
slouken@113
   372
slouken@113
   373
    return(pos_args_array[channel]);
slouken@113
   374
}
slouken@113
   375
slouken@113
   376
slouken@113
   377
static Mix_EffectFunc_t get_position_effect_func(Uint16 format)
slouken@113
   378
{
slouken@113
   379
    Mix_EffectFunc_t f = NULL;
slouken@113
   380
slouken@113
   381
    switch (format) {
slouken@113
   382
        case AUDIO_U8:
slouken@113
   383
            f = (_Eff_build_volume_table_u8()) ? _Eff_position_table_u8 :
slouken@113
   384
                                                 _Eff_position_u8;
slouken@113
   385
            break;
slouken@113
   386
slouken@113
   387
        case AUDIO_S8:
slouken@113
   388
            f = (_Eff_build_volume_table_s8()) ? _Eff_position_table_s8 :
slouken@113
   389
                                                 _Eff_position_s8;
slouken@113
   390
            break;
slouken@113
   391
slouken@113
   392
        case AUDIO_U16LSB:
slouken@113
   393
            f = _Eff_position_u16lsb;
slouken@113
   394
            break;
slouken@113
   395
slouken@113
   396
        case AUDIO_S16LSB:
slouken@113
   397
            f = _Eff_position_s16lsb;
slouken@113
   398
            break;
slouken@113
   399
slouken@113
   400
        case AUDIO_U16MSB:
slouken@113
   401
            f = _Eff_position_u16msb;
slouken@113
   402
            break;
slouken@113
   403
slouken@113
   404
        case AUDIO_S16MSB:
slouken@113
   405
            f = _Eff_position_s16msb;
slouken@113
   406
            break;
slouken@113
   407
slouken@113
   408
        default:
slouken@113
   409
            Mix_SetError("Unsupported audio format");
slouken@113
   410
    }
slouken@113
   411
slouken@113
   412
    return(f);
slouken@113
   413
}
slouken@113
   414
slouken@113
   415
slouken@113
   416
int Mix_SetPanning(int channel, Uint8 left, Uint8 right)
slouken@113
   417
{
slouken@113
   418
    Mix_EffectFunc_t f = NULL;
slouken@113
   419
    int channels;
slouken@113
   420
    Uint16 format;
slouken@113
   421
    position_args *args = NULL;
slouken@113
   422
slouken@113
   423
    Mix_QuerySpec(NULL, &format, &channels);
slouken@113
   424
slouken@113
   425
    if (channels != 2)    /* it's a no-op; we call that successful. */
slouken@113
   426
        return(1);
slouken@113
   427
slouken@113
   428
    f = get_position_effect_func(format);
slouken@113
   429
    if (f == NULL)
slouken@113
   430
        return(0);
slouken@113
   431
slouken@113
   432
    args = get_position_arg(channel);
slouken@113
   433
    if (!args)
slouken@113
   434
        return(0);
slouken@113
   435
slouken@113
   436
        /* it's a no-op; unregister the effect, if it's registered. */
slouken@113
   437
    if ((args->distance_u8 == 255) && (left == 255) &&
slouken@113
   438
        (right == 255) && (args->in_use))
slouken@113
   439
    {
slouken@113
   440
        return(Mix_UnregisterEffect(channel, f));
slouken@113
   441
    }
slouken@113
   442
slouken@113
   443
    args->left_u8 = left;
slouken@113
   444
    args->right_u8 = right;
slouken@114
   445
    args->left_f = ((float) left) / 255.0f;
slouken@114
   446
    args->right_f = ((float) right) / 255.0f;
slouken@113
   447
    if (!args->in_use) {
slouken@113
   448
        args->in_use = 1;
slouken@113
   449
        return(Mix_RegisterEffect(channel, f, _Eff_PositionDone, (void *) args));
slouken@113
   450
    }
slouken@113
   451
slouken@113
   452
    return(1);
slouken@113
   453
}
slouken@113
   454
slouken@113
   455
slouken@113
   456
int Mix_SetDistance(int channel, Uint8 distance)
slouken@113
   457
{
slouken@113
   458
    Mix_EffectFunc_t f = NULL;
slouken@113
   459
    Uint16 format;
slouken@113
   460
    position_args *args = NULL;
slouken@113
   461
slouken@113
   462
    Mix_QuerySpec(NULL, &format, NULL);
slouken@113
   463
    f = get_position_effect_func(format);
slouken@113
   464
    if (f == NULL)
slouken@113
   465
        return(0);
slouken@113
   466
slouken@113
   467
    args = get_position_arg(channel);
slouken@113
   468
    if (!args)
slouken@113
   469
        return(0);
slouken@113
   470
slouken@113
   471
    distance = 255 - distance;  /* flip it to our scale. */
slouken@113
   472
slouken@113
   473
        /* it's a no-op; unregister the effect, if it's registered. */
slouken@113
   474
    if ((distance == 255) && (args->left_u8 == 255) &&
slouken@113
   475
        (args->right_u8 == 255) && (args->in_use))
slouken@113
   476
    {
slouken@113
   477
        return(Mix_UnregisterEffect(channel, f));
slouken@113
   478
    }
slouken@113
   479
slouken@113
   480
    args->distance_u8 = distance;
slouken@114
   481
    args->distance_f = ((float) distance) / 255.0f;
slouken@113
   482
    if (!args->in_use) {
slouken@113
   483
        args->in_use = 1;
slouken@113
   484
        return(Mix_RegisterEffect(channel, f, _Eff_PositionDone, (void *) args));
slouken@113
   485
    }
slouken@113
   486
slouken@113
   487
    return(1);
slouken@113
   488
}
slouken@113
   489
slouken@113
   490
slouken@113
   491
int Mix_SetPosition(int channel, Sint16 angle, Uint8 distance)
slouken@113
   492
{
slouken@113
   493
    Mix_EffectFunc_t f = NULL;
slouken@113
   494
    Uint16 format;
slouken@113
   495
    int channels;
slouken@113
   496
    position_args *args = NULL;
slouken@113
   497
    Uint8 left = 255, right = 255;
slouken@113
   498
slouken@113
   499
    Mix_QuerySpec(NULL, &format, &channels);
slouken@113
   500
    f = get_position_effect_func(format);
slouken@113
   501
    if (f == NULL)
slouken@113
   502
        return(0);
slouken@113
   503
slouken@113
   504
        /* unwind the angle...it'll be between 0 and 359. */
slouken@113
   505
    while (angle >= 360) angle -= 360;
slouken@113
   506
    while (angle < 0) angle += 360;
slouken@113
   507
slouken@113
   508
    args = get_position_arg(channel);
slouken@113
   509
    if (!args)
slouken@113
   510
        return(0);
slouken@113
   511
slouken@113
   512
        /* it's a no-op; unregister the effect, if it's registered. */
slouken@113
   513
    if ((!distance) && (!angle) && (args->in_use))
slouken@113
   514
        return(Mix_UnregisterEffect(channel, f));
slouken@113
   515
slouken@113
   516
    if (channels == 2)
slouken@113
   517
    {
slouken@113
   518
        /*
slouken@113
   519
         * We only attenuate by position if the angle falls on the far side
slouken@113
   520
         *  of center; That is, an angle that's due north would not attenuate
slouken@113
   521
         *  either channel. Due west attenuates the right channel to 0.0, and
slouken@113
   522
         *  due east attenuates the left channel to 0.0. Slightly east of
slouken@113
   523
         *  center attenuates the left channel a little, and the right channel
slouken@113
   524
         *  not at all. I think of this as occlusion by one's own head.  :)
slouken@113
   525
         *
slouken@113
   526
         *   ...so, we split our angle circle into four quadrants...
slouken@113
   527
         */
slouken@113
   528
        if (angle < 90) {
slouken@113
   529
            left = 255 - ((Uint8) (255.0f * (((float) angle) / 89.0f)));
slouken@113
   530
        } else if (angle < 180) {
slouken@113
   531
            left = (Uint8) (255.0f * (((float) (angle - 90)) / 89.0f));
slouken@113
   532
        } else if (angle < 270) {
slouken@113
   533
            right = 255 - ((Uint8) (255.0f * (((float) (angle - 180)) / 89.0f)));
slouken@113
   534
        } else {
slouken@113
   535
            right = (Uint8) (255.0f * (((float) (angle - 270)) / 89.0f));
slouken@113
   536
        }
slouken@113
   537
    }
slouken@113
   538
slouken@113
   539
    distance = 255 - distance;  /* flip it to scale Mix_SetDistance() uses. */
slouken@113
   540
slouken@113
   541
    args->left_u8 = left;
slouken@114
   542
    args->left_f = ((float) left) / 255.0f;
slouken@113
   543
    args->right_u8 = right;
slouken@114
   544
    args->right_f = ((float) right) / 255.0f;
slouken@113
   545
    args->distance_u8 = distance;
slouken@114
   546
    args->distance_f = ((float) distance) / 255.0f;
slouken@113
   547
    if (!args->in_use) {
slouken@113
   548
        args->in_use = 1;
slouken@113
   549
        return(Mix_RegisterEffect(channel, f, _Eff_PositionDone, (void *) args));
slouken@113
   550
    }
slouken@113
   551
slouken@113
   552
    return(1);
slouken@113
   553
}
slouken@113
   554
slouken@113
   555
slouken@113
   556
/* end of effects_position.c ... */
slouken@113
   557