effect_position.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 07 Feb 2002 17:40:36 +0000
changeset 159 01490534f9fe
parent 138 4d0dc6b4985d
child 201 e55fb6b45559
permissions -rw-r--r--
*** empty log message ***
slouken@113
     1
/*
slouken@138
     2
    SDL_mixer:  An audio mixer library based on the SDL library
slouken@138
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  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@113
   102
        *(ptr++) = (Uint8) (((float) *ptr) * args->distance_f);
slouken@113
   103
        len--;
slouken@113
   104
    }
slouken@113
   105
slouken@113
   106
    for (i = 0; i < len; i += sizeof (Uint8) * 2) {
slouken@113
   107
        *(ptr++) = (Uint8)((((float) *ptr) * args->left_f) * args->distance_f);
slouken@113
   108
        *(ptr++) = (Uint8)((((float) *ptr) * args->right_f) * args->distance_f);
slouken@113
   109
    }
slouken@113
   110
}
slouken@113
   111
slouken@113
   112
slouken@113
   113
/*
slouken@113
   114
 * This one runs about 10.1 times faster than the non-table version, with
slouken@113
   115
 *  no loss in quality. It does, however, require 64k of memory for the
slouken@113
   116
 *  lookup table. Also, this will only update position information once per
slouken@113
   117
 *  call; the non-table version always checks the arguments for each sample,
slouken@113
   118
 *  in case the user has called Mix_SetPanning() or whatnot again while this
slouken@113
   119
 *  callback is running.
slouken@113
   120
 */
slouken@113
   121
static void _Eff_position_table_u8(int chan, void *stream, int len, void *udata)
slouken@113
   122
{
slouken@113
   123
    volatile position_args *args = (volatile position_args *) udata;
slouken@113
   124
    Uint8 *ptr = (Uint8 *) stream;
slouken@113
   125
    Uint32 *p;
slouken@113
   126
    int i;
slouken@113
   127
    Uint8 *l = ((Uint8 *) _Eff_volume_table) + (256 * args->left_u8);
slouken@113
   128
    Uint8 *r = ((Uint8 *) _Eff_volume_table) + (256 * args->right_u8);
slouken@113
   129
    Uint8 *d = ((Uint8 *) _Eff_volume_table) + (256 * args->distance_u8);
slouken@113
   130
slouken@113
   131
        /*
slouken@113
   132
         * if there's only a mono channnel, then l[] and r[] are always
slouken@113
   133
         *  volume 255, and are therefore throwaways. Still, we have to
slouken@113
   134
         *  be sure not to overrun the audio buffer...
slouken@113
   135
         */
slouken@113
   136
    while (len % sizeof (Uint32) != 0) {
slouken@113
   137
        *(ptr++) = d[l[*ptr]];
slouken@113
   138
        if (args->channels == 2)
slouken@113
   139
            *(ptr++) = d[r[*ptr]];
slouken@113
   140
        len -= args->channels;
slouken@113
   141
    }
slouken@113
   142
slouken@113
   143
    p = (Uint32 *) ptr;
slouken@113
   144
slouken@113
   145
    for (i = 0; i < len; i += sizeof (Uint32)) {
slouken@159
   146
#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
slouken@113
   147
        *(p++) = (d[l[(*p & 0xFF000000) >> 24]] << 24) |
slouken@113
   148
                 (d[r[(*p & 0x00FF0000) >> 16]] << 16) |
slouken@113
   149
                 (d[l[(*p & 0x0000FF00) >>  8]] <<  8) |
slouken@113
   150
                 (d[r[(*p & 0x000000FF)      ]]      ) ;
slouken@113
   151
#else
slouken@113
   152
        *(p++) = (d[r[(*p & 0xFF000000) >> 24]] << 24) |
slouken@113
   153
                 (d[l[(*p & 0x00FF0000) >> 16]] << 16) |
slouken@113
   154
                 (d[r[(*p & 0x0000FF00) >>  8]] <<  8) |
slouken@113
   155
                 (d[l[(*p & 0x000000FF)      ]]      ) ;
slouken@113
   156
#endif
slouken@113
   157
    }
slouken@113
   158
}
slouken@113
   159
slouken@113
   160
slouken@113
   161
static void _Eff_position_s8(int chan, void *stream, int len, void *udata)
slouken@113
   162
{
slouken@113
   163
    volatile position_args *args = (volatile position_args *) udata;
slouken@113
   164
    Sint8 *ptr = (Sint8 *) stream;
slouken@113
   165
    int i;
slouken@113
   166
slouken@113
   167
        /*
slouken@113
   168
         * if there's only a mono channnel (the only way we wouldn't have
slouken@113
   169
         *  a len divisible by 2 here), then left_f and right_f are always
slouken@113
   170
         *  1.0, and are therefore throwaways.
slouken@113
   171
         */
slouken@113
   172
    if (len % sizeof (Sint16) != 0) {
slouken@113
   173
        *(ptr++) = (Sint8) (((float) *ptr) * args->distance_f);
slouken@113
   174
        len--;
slouken@113
   175
    }
slouken@113
   176
slouken@113
   177
    for (i = 0; i < len; i += sizeof (Sint8) * 2) {
slouken@113
   178
        *(ptr++) = (Sint8)((((float) *ptr) * args->left_f) * args->distance_f);
slouken@113
   179
        *(ptr++) = (Sint8)((((float) *ptr) * args->right_f) * args->distance_f);
slouken@113
   180
    }
slouken@113
   181
}
slouken@113
   182
slouken@113
   183
slouken@113
   184
/*
slouken@113
   185
 * This one runs about 10.1 times faster than the non-table version, with
slouken@113
   186
 *  no loss in quality. It does, however, require 64k of memory for the
slouken@113
   187
 *  lookup table. Also, this will only update position information once per
slouken@113
   188
 *  call; the non-table version always checks the arguments for each sample,
slouken@113
   189
 *  in case the user has called Mix_SetPanning() or whatnot again while this
slouken@113
   190
 *  callback is running.
slouken@113
   191
 */
slouken@113
   192
static void _Eff_position_table_s8(int chan, void *stream, int len, void *udata)
slouken@113
   193
{
slouken@113
   194
    volatile position_args *args = (volatile position_args *) udata;
slouken@113
   195
    Sint8 *ptr = (Sint8 *) stream;
slouken@113
   196
    Uint32 *p;
slouken@113
   197
    int i;
slouken@132
   198
    Sint8 *l = ((Sint8 *) _Eff_volume_table) + (256 * args->left_u8);
slouken@132
   199
    Sint8 *r = ((Sint8 *) _Eff_volume_table) + (256 * args->right_u8);
slouken@113
   200
    Sint8 *d = ((Sint8 *) _Eff_volume_table) + (256 * args->distance_u8);
slouken@113
   201
slouken@113
   202
slouken@113
   203
    while (len % sizeof (Uint32) != 0) {
slouken@113
   204
        *(ptr++) = d[l[*ptr]];
slouken@113
   205
        if (args->channels > 1)
slouken@113
   206
            *(ptr++) = d[r[*ptr]];
slouken@113
   207
        len -= args->channels;
slouken@113
   208
    }
slouken@113
   209
slouken@113
   210
    p = (Uint32 *) ptr;
slouken@113
   211
slouken@113
   212
    for (i = 0; i < len; i += sizeof (Uint32)) {
slouken@159
   213
#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
slouken@113
   214
        *(p++) = (d[l[((Sint16)(Sint8)((*p & 0xFF000000) >> 24))+128]] << 24) |
slouken@113
   215
                 (d[r[((Sint16)(Sint8)((*p & 0x00FF0000) >> 16))+128]] << 16) |
slouken@113
   216
                 (d[l[((Sint16)(Sint8)((*p & 0x0000FF00) >>  8))+128]] <<  8) |
slouken@113
   217
                 (d[r[((Sint16)(Sint8)((*p & 0x000000FF)      ))+128]]      ) ;
slouken@113
   218
#else
slouken@113
   219
        *(p++) = (d[r[((Sint16)(Sint8)((*p & 0xFF000000) >> 24))+128]] << 24) |
slouken@113
   220
                 (d[l[((Sint16)(Sint8)((*p & 0x00FF0000) >> 16))+128]] << 16) |
slouken@113
   221
                 (d[r[((Sint16)(Sint8)((*p & 0x0000FF00) >>  8))+128]] <<  8) |
slouken@113
   222
                 (d[l[((Sint16)(Sint8)((*p & 0x000000FF)      ))+128]]      ) ;
slouken@113
   223
#endif
slouken@113
   224
    }
slouken@113
   225
slouken@113
   226
slouken@113
   227
}
slouken@113
   228
slouken@113
   229
slouken@113
   230
/* !!! FIXME : Optimize the code for 16-bit samples? */
slouken@113
   231
slouken@113
   232
static void _Eff_position_u16lsb(int chan, void *stream, int len, void *udata)
slouken@113
   233
{
slouken@113
   234
    volatile position_args *args = (volatile position_args *) udata;
slouken@113
   235
    Uint16 *ptr = (Uint16 *) stream;
slouken@113
   236
    int i;
slouken@113
   237
slouken@113
   238
    for (i = 0; i < len; i += sizeof (Uint16) * 2) {
slouken@159
   239
#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
slouken@113
   240
        Uint16 swapl = (Uint16) ((((float) SDL_Swap16(*(ptr))) *
slouken@113
   241
                                    args->left_f) * args->distance_f);
slouken@159
   242
        Uint16 swapr = (Uint16) ((((float) SDL_Swap16(*(ptr+1))) *
slouken@113
   243
                                    args->right_f) * args->distance_f);
slouken@113
   244
        *(ptr++) = (Uint16) SDL_Swap16(swapl);
slouken@113
   245
        *(ptr++) = (Uint16) SDL_Swap16(swapr);
slouken@113
   246
#else
slouken@113
   247
        *(ptr++) = (Uint16) ((((float) *ptr)*args->left_f)*args->distance_f);
slouken@113
   248
        *(ptr++) = (Uint16) ((((float) *ptr)*args->right_f)*args->distance_f);
slouken@113
   249
#endif
slouken@113
   250
    }
slouken@113
   251
}
slouken@113
   252
slouken@113
   253
static void _Eff_position_s16lsb(int chan, void *stream, int len, void *udata)
slouken@113
   254
{
slouken@113
   255
    /* 16 signed bits (lsb) * 2 channels. */
slouken@113
   256
    volatile position_args *args = (volatile position_args *) udata;
slouken@113
   257
    Sint16 *ptr = (Sint16 *) stream;
slouken@113
   258
    int i;
slouken@113
   259
slouken@113
   260
    for (i = 0; i < len; i += sizeof (Sint16) * 2) {
slouken@159
   261
#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
slouken@113
   262
        Sint16 swapl = (Sint16) ((((float) SDL_Swap16(*(ptr))) *
slouken@113
   263
                                    args->left_f) * args->distance_f);
slouken@159
   264
        Sint16 swapr = (Sint16) ((((float) SDL_Swap16(*(ptr+1))) *
slouken@113
   265
                                    args->right_f) * args->distance_f);
slouken@113
   266
        *(ptr++) = (Sint16) SDL_Swap16(swapl);
slouken@113
   267
        *(ptr++) = (Sint16) SDL_Swap16(swapr);
slouken@113
   268
#else
slouken@113
   269
        *(ptr++) = (Sint16) ((((float) *ptr)*args->left_f)*args->distance_f);
slouken@113
   270
        *(ptr++) = (Sint16) ((((float) *ptr)*args->right_f)*args->distance_f);
slouken@113
   271
#endif
slouken@113
   272
    }
slouken@113
   273
}
slouken@113
   274
slouken@113
   275
static void _Eff_position_u16msb(int chan, void *stream, int len, void *udata)
slouken@113
   276
{
slouken@113
   277
    /* 16 signed bits (lsb) * 2 channels. */
slouken@113
   278
    volatile position_args *args = (volatile position_args *) udata;
slouken@113
   279
    Uint16 *ptr = (Uint16 *) stream;
slouken@113
   280
    int i;
slouken@113
   281
slouken@113
   282
    for (i = 0; i < len; i += sizeof (Sint16) * 2) {
slouken@159
   283
#if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
slouken@113
   284
        Uint16 swapl = (Uint16) ((((float) SDL_Swap16(*(ptr))) *
slouken@113
   285
                                    args->left_f) * args->distance_f);
slouken@159
   286
        Uint16 swapr = (Uint16) ((((float) SDL_Swap16(*(ptr+1))) *
slouken@113
   287
                                    args->right_f) * args->distance_f);
slouken@113
   288
        *(ptr++) = (Uint16) SDL_Swap16(swapl);
slouken@113
   289
        *(ptr++) = (Uint16) SDL_Swap16(swapr);
slouken@113
   290
#else
slouken@113
   291
        *(ptr++) = (Uint16) ((((float) *ptr)*args->left_f)*args->distance_f);
slouken@113
   292
        *(ptr++) = (Uint16) ((((float) *ptr)*args->right_f)*args->distance_f);
slouken@113
   293
#endif
slouken@113
   294
    }
slouken@113
   295
}
slouken@113
   296
slouken@113
   297
static void _Eff_position_s16msb(int chan, void *stream, int len, void *udata)
slouken@113
   298
{
slouken@113
   299
    /* 16 signed bits (lsb) * 2 channels. */
slouken@113
   300
    volatile position_args *args = (volatile position_args *) udata;
slouken@113
   301
    Sint16 *ptr = (Sint16 *) stream;
slouken@113
   302
    int i;
slouken@113
   303
slouken@113
   304
    for (i = 0; i < len; i += sizeof (Sint16) * 2) {
slouken@159
   305
#if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
slouken@113
   306
        Sint16 swapl = (Sint16) ((((float) SDL_Swap16(*(ptr))) *
slouken@113
   307
                                    args->left_f) * args->distance_f);
slouken@159
   308
        Sint16 swapr = (Sint16) ((((float) SDL_Swap16(*(ptr+1))) *
slouken@113
   309
                                    args->right_f) * args->distance_f);
slouken@113
   310
        *(ptr++) = (Sint16) SDL_Swap16(swapl);
slouken@113
   311
        *(ptr++) = (Sint16) SDL_Swap16(swapr);
slouken@113
   312
#else
slouken@113
   313
        *(ptr++) = (Sint16) ((((float) *ptr)*args->left_f)*args->distance_f);
slouken@113
   314
        *(ptr++) = (Sint16) ((((float) *ptr)*args->right_f)*args->distance_f);
slouken@113
   315
#endif
slouken@113
   316
    }
slouken@113
   317
}
slouken@113
   318
slouken@113
   319
slouken@113
   320
static void init_position_args(position_args *args)
slouken@113
   321
{
slouken@113
   322
    memset(args, '\0', sizeof (position_args));
slouken@113
   323
    args->in_use = 0;
slouken@113
   324
    args->left_u8 = args->right_u8 = args->distance_u8 = 255;
slouken@113
   325
    args->left_f  = args->right_f  = args->distance_f  = 1.0f;
slouken@113
   326
    Mix_QuerySpec(NULL, NULL, (int *) &args->channels);
slouken@113
   327
}
slouken@113
   328
slouken@113
   329
slouken@113
   330
static position_args *get_position_arg(int channel)
slouken@113
   331
{
slouken@113
   332
    void *rc;
slouken@113
   333
    int i;
slouken@113
   334
slouken@113
   335
    if (channel < 0) {
slouken@113
   336
        if (pos_args_global == NULL) {
slouken@113
   337
            pos_args_global = malloc(sizeof (position_args));
slouken@113
   338
            if (pos_args_global == NULL) {
slouken@113
   339
                Mix_SetError("Out of memory");
slouken@113
   340
                return(NULL);
slouken@113
   341
            }
slouken@113
   342
            init_position_args(pos_args_global);
slouken@113
   343
        }
slouken@113
   344
slouken@113
   345
        return(pos_args_global);
slouken@113
   346
    }
slouken@113
   347
slouken@113
   348
    if (channel >= position_channels) {
slouken@113
   349
        rc = realloc(pos_args_array, (channel + 1) * sizeof (position_args *));
slouken@113
   350
        if (rc == NULL) {
slouken@113
   351
            Mix_SetError("Out of memory");
slouken@113
   352
            return(NULL);
slouken@113
   353
        }
slouken@113
   354
        pos_args_array = (position_args **) rc;
slouken@113
   355
        for (i = position_channels; i <= channel; i++) {
slouken@113
   356
            pos_args_array[i] = NULL;
slouken@113
   357
        }
slouken@113
   358
        position_channels = channel + 1;
slouken@113
   359
    }
slouken@113
   360
slouken@113
   361
    if (pos_args_array[channel] == NULL) {
slouken@113
   362
        pos_args_array[channel] = (position_args *)malloc(sizeof(position_args));
slouken@113
   363
        if (pos_args_array[channel] == NULL) {
slouken@113
   364
            Mix_SetError("Out of memory");
slouken@113
   365
            return(NULL);
slouken@113
   366
        }
slouken@113
   367
        init_position_args(pos_args_array[channel]);
slouken@113
   368
    }
slouken@113
   369
slouken@113
   370
    return(pos_args_array[channel]);
slouken@113
   371
}
slouken@113
   372
slouken@113
   373
slouken@113
   374
static Mix_EffectFunc_t get_position_effect_func(Uint16 format)
slouken@113
   375
{
slouken@113
   376
    Mix_EffectFunc_t f = NULL;
slouken@113
   377
slouken@113
   378
    switch (format) {
slouken@113
   379
        case AUDIO_U8:
slouken@113
   380
            f = (_Eff_build_volume_table_u8()) ? _Eff_position_table_u8 :
slouken@113
   381
                                                 _Eff_position_u8;
slouken@113
   382
            break;
slouken@113
   383
slouken@113
   384
        case AUDIO_S8:
slouken@113
   385
            f = (_Eff_build_volume_table_s8()) ? _Eff_position_table_s8 :
slouken@113
   386
                                                 _Eff_position_s8;
slouken@113
   387
            break;
slouken@113
   388
slouken@113
   389
        case AUDIO_U16LSB:
slouken@113
   390
            f = _Eff_position_u16lsb;
slouken@113
   391
            break;
slouken@113
   392
slouken@113
   393
        case AUDIO_S16LSB:
slouken@113
   394
            f = _Eff_position_s16lsb;
slouken@113
   395
            break;
slouken@113
   396
slouken@113
   397
        case AUDIO_U16MSB:
slouken@113
   398
            f = _Eff_position_u16msb;
slouken@113
   399
            break;
slouken@113
   400
slouken@113
   401
        case AUDIO_S16MSB:
slouken@113
   402
            f = _Eff_position_s16msb;
slouken@113
   403
            break;
slouken@113
   404
slouken@113
   405
        default:
slouken@113
   406
            Mix_SetError("Unsupported audio format");
slouken@113
   407
    }
slouken@113
   408
slouken@113
   409
    return(f);
slouken@113
   410
}
slouken@113
   411
slouken@113
   412
slouken@113
   413
int Mix_SetPanning(int channel, Uint8 left, Uint8 right)
slouken@113
   414
{
slouken@113
   415
    Mix_EffectFunc_t f = NULL;
slouken@113
   416
    int channels;
slouken@113
   417
    Uint16 format;
slouken@113
   418
    position_args *args = NULL;
slouken@113
   419
slouken@113
   420
    Mix_QuerySpec(NULL, &format, &channels);
slouken@113
   421
slouken@113
   422
    if (channels != 2)    /* it's a no-op; we call that successful. */
slouken@113
   423
        return(1);
slouken@113
   424
slouken@113
   425
    f = get_position_effect_func(format);
slouken@113
   426
    if (f == NULL)
slouken@113
   427
        return(0);
slouken@113
   428
slouken@113
   429
    args = get_position_arg(channel);
slouken@113
   430
    if (!args)
slouken@113
   431
        return(0);
slouken@113
   432
slouken@113
   433
        /* it's a no-op; unregister the effect, if it's registered. */
slouken@113
   434
    if ((args->distance_u8 == 255) && (left == 255) &&
slouken@113
   435
        (right == 255) && (args->in_use))
slouken@113
   436
    {
slouken@113
   437
        return(Mix_UnregisterEffect(channel, f));
slouken@113
   438
    }
slouken@113
   439
slouken@113
   440
    args->left_u8 = left;
slouken@113
   441
    args->right_u8 = right;
slouken@114
   442
    args->left_f = ((float) left) / 255.0f;
slouken@114
   443
    args->right_f = ((float) right) / 255.0f;
slouken@113
   444
    if (!args->in_use) {
slouken@113
   445
        args->in_use = 1;
slouken@113
   446
        return(Mix_RegisterEffect(channel, f, _Eff_PositionDone, (void *) args));
slouken@113
   447
    }
slouken@113
   448
slouken@113
   449
    return(1);
slouken@113
   450
}
slouken@113
   451
slouken@113
   452
slouken@113
   453
int Mix_SetDistance(int channel, Uint8 distance)
slouken@113
   454
{
slouken@113
   455
    Mix_EffectFunc_t f = NULL;
slouken@113
   456
    Uint16 format;
slouken@113
   457
    position_args *args = NULL;
slouken@113
   458
slouken@113
   459
    Mix_QuerySpec(NULL, &format, NULL);
slouken@113
   460
    f = get_position_effect_func(format);
slouken@113
   461
    if (f == NULL)
slouken@113
   462
        return(0);
slouken@113
   463
slouken@113
   464
    args = get_position_arg(channel);
slouken@113
   465
    if (!args)
slouken@113
   466
        return(0);
slouken@113
   467
slouken@113
   468
    distance = 255 - distance;  /* flip it to our scale. */
slouken@113
   469
slouken@113
   470
        /* it's a no-op; unregister the effect, if it's registered. */
slouken@113
   471
    if ((distance == 255) && (args->left_u8 == 255) &&
slouken@113
   472
        (args->right_u8 == 255) && (args->in_use))
slouken@113
   473
    {
slouken@113
   474
        return(Mix_UnregisterEffect(channel, f));
slouken@113
   475
    }
slouken@113
   476
slouken@113
   477
    args->distance_u8 = distance;
slouken@114
   478
    args->distance_f = ((float) distance) / 255.0f;
slouken@113
   479
    if (!args->in_use) {
slouken@113
   480
        args->in_use = 1;
slouken@113
   481
        return(Mix_RegisterEffect(channel, f, _Eff_PositionDone, (void *) args));
slouken@113
   482
    }
slouken@113
   483
slouken@113
   484
    return(1);
slouken@113
   485
}
slouken@113
   486
slouken@113
   487
slouken@113
   488
int Mix_SetPosition(int channel, Sint16 angle, Uint8 distance)
slouken@113
   489
{
slouken@113
   490
    Mix_EffectFunc_t f = NULL;
slouken@113
   491
    Uint16 format;
slouken@113
   492
    int channels;
slouken@113
   493
    position_args *args = NULL;
slouken@113
   494
    Uint8 left = 255, right = 255;
slouken@113
   495
slouken@113
   496
    Mix_QuerySpec(NULL, &format, &channels);
slouken@113
   497
    f = get_position_effect_func(format);
slouken@113
   498
    if (f == NULL)
slouken@113
   499
        return(0);
slouken@113
   500
slouken@113
   501
        /* unwind the angle...it'll be between 0 and 359. */
slouken@113
   502
    while (angle >= 360) angle -= 360;
slouken@113
   503
    while (angle < 0) angle += 360;
slouken@113
   504
slouken@113
   505
    args = get_position_arg(channel);
slouken@113
   506
    if (!args)
slouken@113
   507
        return(0);
slouken@113
   508
slouken@113
   509
        /* it's a no-op; unregister the effect, if it's registered. */
slouken@113
   510
    if ((!distance) && (!angle) && (args->in_use))
slouken@113
   511
        return(Mix_UnregisterEffect(channel, f));
slouken@113
   512
slouken@113
   513
    if (channels == 2)
slouken@113
   514
    {
slouken@113
   515
        /*
slouken@113
   516
         * We only attenuate by position if the angle falls on the far side
slouken@113
   517
         *  of center; That is, an angle that's due north would not attenuate
slouken@113
   518
         *  either channel. Due west attenuates the right channel to 0.0, and
slouken@113
   519
         *  due east attenuates the left channel to 0.0. Slightly east of
slouken@113
   520
         *  center attenuates the left channel a little, and the right channel
slouken@113
   521
         *  not at all. I think of this as occlusion by one's own head.  :)
slouken@113
   522
         *
slouken@113
   523
         *   ...so, we split our angle circle into four quadrants...
slouken@113
   524
         */
slouken@113
   525
        if (angle < 90) {
slouken@113
   526
            left = 255 - ((Uint8) (255.0f * (((float) angle) / 89.0f)));
slouken@113
   527
        } else if (angle < 180) {
slouken@113
   528
            left = (Uint8) (255.0f * (((float) (angle - 90)) / 89.0f));
slouken@113
   529
        } else if (angle < 270) {
slouken@113
   530
            right = 255 - ((Uint8) (255.0f * (((float) (angle - 180)) / 89.0f)));
slouken@113
   531
        } else {
slouken@113
   532
            right = (Uint8) (255.0f * (((float) (angle - 270)) / 89.0f));
slouken@113
   533
        }
slouken@113
   534
    }
slouken@113
   535
slouken@113
   536
    distance = 255 - distance;  /* flip it to scale Mix_SetDistance() uses. */
slouken@113
   537
slouken@113
   538
    args->left_u8 = left;
slouken@114
   539
    args->left_f = ((float) left) / 255.0f;
slouken@113
   540
    args->right_u8 = right;
slouken@114
   541
    args->right_f = ((float) right) / 255.0f;
slouken@113
   542
    args->distance_u8 = distance;
slouken@114
   543
    args->distance_f = ((float) distance) / 255.0f;
slouken@113
   544
    if (!args->in_use) {
slouken@113
   545
        args->in_use = 1;
slouken@113
   546
        return(Mix_RegisterEffect(channel, f, _Eff_PositionDone, (void *) args));
slouken@113
   547
    }
slouken@113
   548
slouken@113
   549
    return(1);
slouken@113
   550
}
slouken@113
   551
slouken@113
   552
slouken@113
   553
/* end of effects_position.c ... */
slouken@113
   554