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