effect_position.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 21 Jul 2007 06:37:58 +0000
changeset 373 6271eafc6e70
parent 359 83eaf245b7e7
child 379 c300eee1d1e4
permissions -rw-r--r--
Fixed compiler warnings
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
icculus@339
    19
    This file by Ryan C. Gordon (icculus@icculus.org)
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@245
    63
    volatile float left_rear_f;
slouken@245
    64
    volatile float right_rear_f;
slouken@245
    65
    volatile float center_f;
slouken@245
    66
    volatile float lfe_f;
slouken@245
    67
    volatile Uint8 left_rear_u8;
slouken@245
    68
    volatile Uint8 right_rear_u8;
slouken@245
    69
    volatile Uint8 center_u8;
slouken@245
    70
    volatile Uint8 lfe_u8;
slouken@113
    71
    volatile float distance_f;
slouken@113
    72
    volatile Uint8 distance_u8;
slouken@245
    73
    volatile Sint16 room_angle;
slouken@113
    74
    volatile int in_use;
slouken@113
    75
    volatile int channels;
slouken@113
    76
} position_args;
slouken@113
    77
slouken@113
    78
static position_args **pos_args_array = NULL;
slouken@113
    79
static position_args *pos_args_global = NULL;
slouken@113
    80
static int position_channels = 0;
slouken@113
    81
icculus@359
    82
void _Eff_PositionDeinit(void)
icculus@359
    83
{
icculus@359
    84
    int i;
icculus@359
    85
    for (i = 0; i < position_channels; i++) {
icculus@359
    86
        free(pos_args_array[i]);
icculus@359
    87
    }
icculus@359
    88
icculus@359
    89
    free(pos_args_global);
icculus@359
    90
    pos_args_global = NULL;
icculus@359
    91
    free(pos_args_array);
icculus@359
    92
    pos_args_array = NULL;
icculus@359
    93
}
icculus@359
    94
icculus@359
    95
slouken@113
    96
/* This just frees up the callback-specific data. */
slouken@113
    97
static void _Eff_PositionDone(int channel, void *udata)
slouken@113
    98
{
slouken@113
    99
    if (channel < 0) {
slouken@113
   100
        if (pos_args_global != NULL) {
slouken@113
   101
            free(pos_args_global);
slouken@113
   102
            pos_args_global = NULL;
slouken@113
   103
        }
slouken@113
   104
    }
slouken@113
   105
slouken@113
   106
    else if (pos_args_array[channel] != NULL) {
slouken@113
   107
        free(pos_args_array[channel]);
slouken@113
   108
        pos_args_array[channel] = NULL;
slouken@113
   109
    }
slouken@113
   110
}
slouken@113
   111
slouken@113
   112
slouken@113
   113
static void _Eff_position_u8(int chan, void *stream, int len, void *udata)
slouken@113
   114
{
slouken@113
   115
    volatile position_args *args = (volatile position_args *) udata;
slouken@113
   116
    Uint8 *ptr = (Uint8 *) stream;
slouken@113
   117
    int i;
slouken@113
   118
slouken@113
   119
        /*
slouken@113
   120
         * if there's only a mono channnel (the only way we wouldn't have
slouken@113
   121
         *  a len divisible by 2 here), then left_f and right_f are always
slouken@113
   122
         *  1.0, and are therefore throwaways.
slouken@113
   123
         */
slouken@113
   124
    if (len % sizeof (Uint16) != 0) {
slouken@218
   125
        *ptr = (Uint8) (((float) *ptr) * args->distance_f);
slouken@218
   126
        ptr++;
slouken@113
   127
        len--;
slouken@113
   128
    }
slouken@113
   129
slouken@245
   130
    if (args->room_angle == 0)
slouken@113
   131
    for (i = 0; i < len; i += sizeof (Uint8) * 2) {
icculus@201
   132
        /* must adjust the sample so that 0 is the center */
slouken@218
   133
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   134
            * args->right_f) * args->distance_f) + 128);
slouken@245
   135
        ptr++;
slouken@245
   136
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   137
            * args->left_f) * args->distance_f) + 128);
slouken@245
   138
        ptr++;
slouken@245
   139
    }
slouken@245
   140
    else for (i = 0; i < len; i += sizeof (Uint8) * 2) {
slouken@245
   141
        /* must adjust the sample so that 0 is the center */
slouken@245
   142
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
icculus@201
   143
            * args->left_f) * args->distance_f) + 128);
slouken@218
   144
        ptr++;
slouken@218
   145
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
icculus@201
   146
            * args->right_f) * args->distance_f) + 128);
slouken@218
   147
        ptr++;
slouken@113
   148
    }
slouken@113
   149
}
slouken@245
   150
static void _Eff_position_u8_c4(int chan, void *stream, int len, void *udata)
slouken@245
   151
{
slouken@245
   152
    volatile position_args *args = (volatile position_args *) udata;
slouken@245
   153
    Uint8 *ptr = (Uint8 *) stream;
slouken@245
   154
    int i;
slouken@245
   155
slouken@245
   156
        /*
slouken@245
   157
         * if there's only a mono channnel (the only way we wouldn't have
slouken@245
   158
         *  a len divisible by 2 here), then left_f and right_f are always
slouken@245
   159
         *  1.0, and are therefore throwaways.
slouken@245
   160
         */
slouken@245
   161
    if (len % sizeof (Uint16) != 0) {
slouken@245
   162
        *ptr = (Uint8) (((float) *ptr) * args->distance_f);
slouken@245
   163
        ptr++;
slouken@245
   164
        len--;
slouken@245
   165
    }
slouken@245
   166
slouken@245
   167
    if (args->room_angle == 0)
slouken@245
   168
    for (i = 0; i < len; i += sizeof (Uint8) * 6) {
slouken@245
   169
        /* must adjust the sample so that 0 is the center */
slouken@245
   170
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   171
            * args->left_f) * args->distance_f) + 128);
slouken@245
   172
        ptr++;
slouken@245
   173
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   174
            * args->right_f) * args->distance_f) + 128);
slouken@245
   175
        ptr++;
slouken@245
   176
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   177
            * args->left_rear_f) * args->distance_f) + 128);
slouken@245
   178
        ptr++;
slouken@245
   179
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   180
            * args->right_rear_f) * args->distance_f) + 128);
slouken@245
   181
        ptr++;
slouken@245
   182
    }
slouken@245
   183
    else if (args->room_angle == 90)
slouken@245
   184
    for (i = 0; i < len; i += sizeof (Uint8) * 6) {
slouken@245
   185
        /* must adjust the sample so that 0 is the center */
slouken@245
   186
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   187
            * args->right_f) * args->distance_f) + 128);
slouken@245
   188
        ptr++;
slouken@245
   189
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   190
            * args->right_rear_f) * args->distance_f) + 128);
slouken@245
   191
        ptr++;
slouken@245
   192
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   193
            * args->left_f) * args->distance_f) + 128);
slouken@245
   194
        ptr++;
slouken@245
   195
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   196
            * args->left_rear_f) * args->distance_f) + 128);
slouken@245
   197
        ptr++;
slouken@245
   198
    }
slouken@245
   199
    else if (args->room_angle == 180)
slouken@245
   200
    for (i = 0; i < len; i += sizeof (Uint8) * 6) {
slouken@245
   201
        /* must adjust the sample so that 0 is the center */
slouken@245
   202
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   203
            * args->right_rear_f) * args->distance_f) + 128);
slouken@245
   204
        ptr++;
slouken@245
   205
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   206
            * args->left_rear_f) * args->distance_f) + 128);
slouken@245
   207
        ptr++;
slouken@245
   208
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   209
            * args->right_f) * args->distance_f) + 128);
slouken@245
   210
        ptr++;
slouken@245
   211
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   212
            * args->left_f) * args->distance_f) + 128);
slouken@245
   213
        ptr++;
slouken@245
   214
    }
slouken@245
   215
    else if (args->room_angle == 270)
slouken@245
   216
    for (i = 0; i < len; i += sizeof (Uint8) * 6) {
slouken@245
   217
        /* must adjust the sample so that 0 is the center */
slouken@245
   218
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   219
            * args->left_rear_f) * args->distance_f) + 128);
slouken@245
   220
        ptr++;
slouken@245
   221
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   222
            * args->left_f) * args->distance_f) + 128);
slouken@245
   223
        ptr++;
slouken@245
   224
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   225
            * args->right_rear_f) * args->distance_f) + 128);
slouken@245
   226
        ptr++;
slouken@245
   227
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   228
            * args->right_f) * args->distance_f) + 128);
slouken@245
   229
        ptr++;
slouken@245
   230
    }
slouken@245
   231
}
slouken@245
   232
slouken@245
   233
slouken@245
   234
static void _Eff_position_u8_c6(int chan, void *stream, int len, void *udata)
slouken@245
   235
{
slouken@245
   236
    volatile position_args *args = (volatile position_args *) udata;
slouken@245
   237
    Uint8 *ptr = (Uint8 *) stream;
slouken@245
   238
    int i;
slouken@245
   239
slouken@245
   240
        /*
slouken@245
   241
         * if there's only a mono channnel (the only way we wouldn't have
slouken@245
   242
         *  a len divisible by 2 here), then left_f and right_f are always
slouken@245
   243
         *  1.0, and are therefore throwaways.
slouken@245
   244
         */
slouken@245
   245
    if (len % sizeof (Uint16) != 0) {
slouken@245
   246
        *ptr = (Uint8) (((float) *ptr) * args->distance_f);
slouken@245
   247
        ptr++;
slouken@245
   248
        len--;
slouken@245
   249
    }
slouken@245
   250
slouken@245
   251
    if (args->room_angle == 0)
slouken@245
   252
    for (i = 0; i < len; i += sizeof (Uint8) * 6) {
slouken@245
   253
        /* must adjust the sample so that 0 is the center */
slouken@245
   254
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   255
            * args->left_f) * args->distance_f) + 128);
slouken@245
   256
        ptr++;
slouken@245
   257
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   258
            * args->right_f) * args->distance_f) + 128);
slouken@245
   259
        ptr++;
slouken@245
   260
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   261
            * args->left_rear_f) * args->distance_f) + 128);
slouken@245
   262
        ptr++;
slouken@245
   263
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   264
            * args->right_rear_f) * args->distance_f) + 128);
slouken@245
   265
        ptr++;
slouken@245
   266
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   267
            * args->center_f) * args->distance_f) + 128);
slouken@245
   268
        ptr++;
slouken@245
   269
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   270
            * args->lfe_f) * args->distance_f) + 128);
slouken@245
   271
        ptr++;
slouken@245
   272
    }
slouken@245
   273
    else if (args->room_angle == 90)
slouken@245
   274
    for (i = 0; i < len; i += sizeof (Uint8) * 6) {
slouken@245
   275
        /* must adjust the sample so that 0 is the center */
slouken@245
   276
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   277
            * args->right_f) * args->distance_f) + 128);
slouken@245
   278
        ptr++;
slouken@245
   279
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   280
            * args->right_rear_f) * args->distance_f) + 128);
slouken@245
   281
        ptr++;
slouken@245
   282
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   283
            * args->left_f) * args->distance_f) + 128);
slouken@245
   284
        ptr++;
slouken@245
   285
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   286
            * args->left_rear_f) * args->distance_f) + 128);
slouken@245
   287
        ptr++;
slouken@245
   288
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   289
            * args->right_rear_f) * args->distance_f/2) + 128)
slouken@245
   290
            + (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   291
            * args->right_f) * args->distance_f/2) + 128);
slouken@245
   292
        ptr++;
slouken@245
   293
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   294
            * args->lfe_f) * args->distance_f) + 128);
slouken@245
   295
        ptr++;
slouken@245
   296
    }
slouken@245
   297
    else if (args->room_angle == 180)
slouken@245
   298
    for (i = 0; i < len; i += sizeof (Uint8) * 6) {
slouken@245
   299
        /* must adjust the sample so that 0 is the center */
slouken@245
   300
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   301
            * args->right_rear_f) * args->distance_f) + 128);
slouken@245
   302
        ptr++;
slouken@245
   303
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   304
            * args->left_rear_f) * args->distance_f) + 128);
slouken@245
   305
        ptr++;
slouken@245
   306
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   307
            * args->right_f) * args->distance_f) + 128);
slouken@245
   308
        ptr++;
slouken@245
   309
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   310
            * args->left_f) * args->distance_f) + 128);
slouken@245
   311
        ptr++;
slouken@245
   312
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   313
            * args->right_rear_f) * args->distance_f/2) + 128)
slouken@245
   314
            + (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   315
            * args->left_rear_f) * args->distance_f/2) + 128);
slouken@245
   316
        ptr++;
slouken@245
   317
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   318
            * args->lfe_f) * args->distance_f) + 128);
slouken@245
   319
        ptr++;
slouken@245
   320
    }
slouken@245
   321
    else if (args->room_angle == 270)
slouken@245
   322
    for (i = 0; i < len; i += sizeof (Uint8) * 6) {
slouken@245
   323
        /* must adjust the sample so that 0 is the center */
slouken@245
   324
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   325
            * args->left_rear_f) * args->distance_f) + 128);
slouken@245
   326
        ptr++;
slouken@245
   327
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   328
            * args->left_f) * args->distance_f) + 128);
slouken@245
   329
        ptr++;
slouken@245
   330
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   331
            * args->right_rear_f) * args->distance_f) + 128);
slouken@245
   332
        ptr++;
slouken@245
   333
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   334
            * args->right_f) * args->distance_f) + 128);
slouken@245
   335
        ptr++;
slouken@245
   336
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   337
            * args->left_f) * args->distance_f/2) + 128)
slouken@245
   338
            + (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   339
            * args->left_rear_f) * args->distance_f/2) + 128);
slouken@245
   340
        ptr++;
slouken@245
   341
        *ptr = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
slouken@245
   342
            * args->lfe_f) * args->distance_f) + 128);
slouken@245
   343
        ptr++;
slouken@245
   344
    }
slouken@245
   345
}
slouken@113
   346
slouken@113
   347
slouken@113
   348
/*
slouken@113
   349
 * This one runs about 10.1 times faster than the non-table version, with
slouken@113
   350
 *  no loss in quality. It does, however, require 64k of memory for the
slouken@113
   351
 *  lookup table. Also, this will only update position information once per
slouken@113
   352
 *  call; the non-table version always checks the arguments for each sample,
slouken@113
   353
 *  in case the user has called Mix_SetPanning() or whatnot again while this
slouken@113
   354
 *  callback is running.
slouken@113
   355
 */
slouken@113
   356
static void _Eff_position_table_u8(int chan, void *stream, int len, void *udata)
slouken@113
   357
{
slouken@113
   358
    volatile position_args *args = (volatile position_args *) udata;
slouken@113
   359
    Uint8 *ptr = (Uint8 *) stream;
slouken@113
   360
    Uint32 *p;
slouken@113
   361
    int i;
slouken@113
   362
    Uint8 *l = ((Uint8 *) _Eff_volume_table) + (256 * args->left_u8);
slouken@113
   363
    Uint8 *r = ((Uint8 *) _Eff_volume_table) + (256 * args->right_u8);
slouken@113
   364
    Uint8 *d = ((Uint8 *) _Eff_volume_table) + (256 * args->distance_u8);
slouken@113
   365
slouken@245
   366
    if (args->room_angle == 180) {
slouken@245
   367
	    Uint8 *temp = l;
slouken@245
   368
	    l = r;
slouken@245
   369
	    r = temp;
slouken@245
   370
    }
slouken@113
   371
        /*
slouken@113
   372
         * if there's only a mono channnel, then l[] and r[] are always
slouken@113
   373
         *  volume 255, and are therefore throwaways. Still, we have to
slouken@113
   374
         *  be sure not to overrun the audio buffer...
slouken@113
   375
         */
slouken@113
   376
    while (len % sizeof (Uint32) != 0) {
slouken@218
   377
        *ptr = d[l[*ptr]];
slouken@218
   378
        ptr++;
slouken@218
   379
        if (args->channels > 1) {
slouken@218
   380
            *ptr = d[r[*ptr]];
slouken@218
   381
            ptr++;
slouken@218
   382
        }
slouken@113
   383
        len -= args->channels;
slouken@113
   384
    }
slouken@113
   385
slouken@113
   386
    p = (Uint32 *) ptr;
slouken@113
   387
slouken@113
   388
    for (i = 0; i < len; i += sizeof (Uint32)) {
slouken@159
   389
#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
slouken@373
   390
        *p = (d[l[(*p & 0xFF000000) >> 24]] << 24) |
slouken@373
   391
             (d[r[(*p & 0x00FF0000) >> 16]] << 16) |
slouken@373
   392
             (d[l[(*p & 0x0000FF00) >>  8]] <<  8) |
slouken@373
   393
             (d[r[(*p & 0x000000FF)      ]]      ) ;
slouken@113
   394
#else
slouken@373
   395
        *p = (d[r[(*p & 0xFF000000) >> 24]] << 24) |
slouken@373
   396
             (d[l[(*p & 0x00FF0000) >> 16]] << 16) |
slouken@373
   397
             (d[r[(*p & 0x0000FF00) >>  8]] <<  8) |
slouken@373
   398
             (d[l[(*p & 0x000000FF)      ]]      ) ;
slouken@113
   399
#endif
slouken@373
   400
        ++p;
slouken@113
   401
    }
slouken@113
   402
}
slouken@113
   403
slouken@113
   404
slouken@113
   405
static void _Eff_position_s8(int chan, void *stream, int len, void *udata)
slouken@113
   406
{
slouken@113
   407
    volatile position_args *args = (volatile position_args *) udata;
slouken@113
   408
    Sint8 *ptr = (Sint8 *) stream;
slouken@113
   409
    int i;
slouken@113
   410
slouken@113
   411
        /*
slouken@113
   412
         * if there's only a mono channnel (the only way we wouldn't have
slouken@113
   413
         *  a len divisible by 2 here), then left_f and right_f are always
slouken@113
   414
         *  1.0, and are therefore throwaways.
slouken@113
   415
         */
slouken@113
   416
    if (len % sizeof (Sint16) != 0) {
slouken@218
   417
        *ptr = (Sint8) (((float) *ptr) * args->distance_f);
slouken@218
   418
        ptr++;
slouken@113
   419
        len--;
slouken@113
   420
    }
slouken@113
   421
slouken@245
   422
    if (args->room_angle == 180)
slouken@245
   423
    for (i = 0; i < len; i += sizeof (Sint8) * 2) {
slouken@245
   424
        *ptr = (Sint8)((((float) *ptr) * args->right_f) * args->distance_f);
slouken@245
   425
        ptr++;
slouken@245
   426
        *ptr = (Sint8)((((float) *ptr) * args->left_f) * args->distance_f);
slouken@245
   427
        ptr++;
slouken@245
   428
    }
slouken@245
   429
    else
slouken@113
   430
    for (i = 0; i < len; i += sizeof (Sint8) * 2) {
slouken@218
   431
        *ptr = (Sint8)((((float) *ptr) * args->left_f) * args->distance_f);
slouken@218
   432
        ptr++;
slouken@218
   433
        *ptr = (Sint8)((((float) *ptr) * args->right_f) * args->distance_f);
slouken@218
   434
        ptr++;
slouken@113
   435
    }
slouken@113
   436
}
slouken@245
   437
static void _Eff_position_s8_c4(int chan, void *stream, int len, void *udata)
slouken@245
   438
{
slouken@245
   439
    volatile position_args *args = (volatile position_args *) udata;
slouken@245
   440
    Sint8 *ptr = (Sint8 *) stream;
slouken@245
   441
    int i;
slouken@245
   442
slouken@245
   443
        /*
slouken@245
   444
         * if there's only a mono channnel (the only way we wouldn't have
slouken@245
   445
         *  a len divisible by 2 here), then left_f and right_f are always
slouken@245
   446
         *  1.0, and are therefore throwaways.
slouken@245
   447
         */
slouken@245
   448
    if (len % sizeof (Sint16) != 0) {
slouken@245
   449
        *ptr = (Sint8) (((float) *ptr) * args->distance_f);
slouken@245
   450
        ptr++;
slouken@245
   451
        len--;
slouken@245
   452
    }
slouken@245
   453
slouken@245
   454
    for (i = 0; i < len; i += sizeof (Sint8) * 4) {
slouken@245
   455
      switch (args->room_angle) {
slouken@245
   456
       case 0:
slouken@245
   457
        *ptr = (Sint8)((((float) *ptr) * args->left_f) * args->distance_f); ptr++;
slouken@245
   458
        *ptr = (Sint8)((((float) *ptr) * args->right_f) * args->distance_f); ptr++;
slouken@245
   459
        *ptr = (Sint8)((((float) *ptr) * args->left_rear_f) * args->distance_f); ptr++;
slouken@245
   460
        *ptr = (Sint8)((((float) *ptr) * args->right_rear_f) * args->distance_f); ptr++;
slouken@245
   461
	break;
slouken@245
   462
       case 90:
slouken@245
   463
        *ptr = (Sint8)((((float) *ptr) * args->right_f) * args->distance_f); ptr++;
slouken@245
   464
        *ptr = (Sint8)((((float) *ptr) * args->right_rear_f) * args->distance_f); ptr++;
slouken@245
   465
        *ptr = (Sint8)((((float) *ptr) * args->left_f) * args->distance_f); ptr++;
slouken@245
   466
        *ptr = (Sint8)((((float) *ptr) * args->left_rear_f) * args->distance_f); ptr++;
slouken@245
   467
	break;
slouken@245
   468
       case 180:
slouken@245
   469
        *ptr = (Sint8)((((float) *ptr) * args->right_rear_f) * args->distance_f); ptr++;
slouken@245
   470
        *ptr = (Sint8)((((float) *ptr) * args->left_rear_f) * args->distance_f); ptr++;
slouken@245
   471
        *ptr = (Sint8)((((float) *ptr) * args->right_f) * args->distance_f); ptr++;
slouken@245
   472
        *ptr = (Sint8)((((float) *ptr) * args->left_f) * args->distance_f); ptr++;
slouken@245
   473
	break;
slouken@245
   474
       case 270:
slouken@245
   475
        *ptr = (Sint8)((((float) *ptr) * args->left_rear_f) * args->distance_f); ptr++;
slouken@245
   476
        *ptr = (Sint8)((((float) *ptr) * args->left_f) * args->distance_f); ptr++;
slouken@245
   477
        *ptr = (Sint8)((((float) *ptr) * args->right_rear_f) * args->distance_f); ptr++;
slouken@245
   478
        *ptr = (Sint8)((((float) *ptr) * args->right_f) * args->distance_f); ptr++;
slouken@245
   479
	break;
slouken@245
   480
      }
slouken@245
   481
    }
slouken@245
   482
}
slouken@245
   483
static void _Eff_position_s8_c6(int chan, void *stream, int len, void *udata)
slouken@245
   484
{
slouken@245
   485
    volatile position_args *args = (volatile position_args *) udata;
slouken@245
   486
    Sint8 *ptr = (Sint8 *) stream;
slouken@245
   487
    int i;
slouken@245
   488
slouken@245
   489
        /*
slouken@245
   490
         * if there's only a mono channnel (the only way we wouldn't have
slouken@245
   491
         *  a len divisible by 2 here), then left_f and right_f are always
slouken@245
   492
         *  1.0, and are therefore throwaways.
slouken@245
   493
         */
slouken@245
   494
    if (len % sizeof (Sint16) != 0) {
slouken@245
   495
        *ptr = (Sint8) (((float) *ptr) * args->distance_f);
slouken@245
   496
        ptr++;
slouken@245
   497
        len--;
slouken@245
   498
    }
slouken@245
   499
slouken@245
   500
    for (i = 0; i < len; i += sizeof (Sint8) * 6) {
slouken@245
   501
      switch (args->room_angle) {
slouken@245
   502
       case 0:
slouken@245
   503
        *ptr = (Sint8)((((float) *ptr) * args->left_f) * args->distance_f); ptr++;
slouken@245
   504
        *ptr = (Sint8)((((float) *ptr) * args->right_f) * args->distance_f); ptr++;
slouken@245
   505
        *ptr = (Sint8)((((float) *ptr) * args->left_rear_f) * args->distance_f); ptr++;
slouken@245
   506
        *ptr = (Sint8)((((float) *ptr) * args->right_rear_f) * args->distance_f); ptr++;
slouken@245
   507
        *ptr = (Sint8)((((float) *ptr) * args->center_f) * args->distance_f); ptr++;
slouken@245
   508
        *ptr = (Sint8)((((float) *ptr) * args->lfe_f) * args->distance_f); ptr++;
slouken@245
   509
	break;
slouken@245
   510
       case 90:
slouken@245
   511
        *ptr = (Sint8)((((float) *ptr) * args->right_f) * args->distance_f); ptr++;
slouken@245
   512
        *ptr = (Sint8)((((float) *ptr) * args->right_rear_f) * args->distance_f); ptr++;
slouken@245
   513
        *ptr = (Sint8)((((float) *ptr) * args->left_f) * args->distance_f); ptr++;
slouken@245
   514
        *ptr = (Sint8)((((float) *ptr) * args->left_rear_f) * args->distance_f); ptr++;
slouken@245
   515
        *ptr = (Sint8)((((float) *ptr) * args->right_rear_f) * args->distance_f / 2)
slouken@245
   516
           + (Sint8)((((float) *ptr) * args->right_f) * args->distance_f / 2); ptr++;
slouken@245
   517
        *ptr = (Sint8)((((float) *ptr) * args->lfe_f) * args->distance_f); ptr++;
slouken@245
   518
	break;
slouken@245
   519
       case 180:
slouken@245
   520
        *ptr = (Sint8)((((float) *ptr) * args->right_rear_f) * args->distance_f); ptr++;
slouken@245
   521
        *ptr = (Sint8)((((float) *ptr) * args->left_rear_f) * args->distance_f); ptr++;
slouken@245
   522
        *ptr = (Sint8)((((float) *ptr) * args->right_f) * args->distance_f); ptr++;
slouken@245
   523
        *ptr = (Sint8)((((float) *ptr) * args->left_f) * args->distance_f); ptr++;
slouken@245
   524
        *ptr = (Sint8)((((float) *ptr) * args->right_rear_f) * args->distance_f / 2)
slouken@245
   525
           + (Sint8)((((float) *ptr) * args->left_rear_f) * args->distance_f / 2); ptr++;
slouken@245
   526
        *ptr = (Sint8)((((float) *ptr) * args->lfe_f) * args->distance_f); ptr++;
slouken@245
   527
	break;
slouken@245
   528
       case 270:
slouken@245
   529
        *ptr = (Sint8)((((float) *ptr) * args->left_rear_f) * args->distance_f); ptr++;
slouken@245
   530
        *ptr = (Sint8)((((float) *ptr) * args->left_f) * args->distance_f); ptr++;
slouken@245
   531
        *ptr = (Sint8)((((float) *ptr) * args->right_rear_f) * args->distance_f); ptr++;
slouken@245
   532
        *ptr = (Sint8)((((float) *ptr) * args->right_f) * args->distance_f); ptr++;
slouken@245
   533
        *ptr = (Sint8)((((float) *ptr) * args->left_f) * args->distance_f / 2)
slouken@245
   534
           + (Sint8)((((float) *ptr) * args->left_rear_f) * args->distance_f / 2); ptr++;
slouken@245
   535
        *ptr = (Sint8)((((float) *ptr) * args->lfe_f) * args->distance_f); ptr++;
slouken@245
   536
	break;
slouken@245
   537
      }
slouken@245
   538
    }
slouken@245
   539
}
slouken@113
   540
slouken@113
   541
slouken@113
   542
/*
slouken@113
   543
 * This one runs about 10.1 times faster than the non-table version, with
slouken@113
   544
 *  no loss in quality. It does, however, require 64k of memory for the
slouken@113
   545
 *  lookup table. Also, this will only update position information once per
slouken@113
   546
 *  call; the non-table version always checks the arguments for each sample,
slouken@113
   547
 *  in case the user has called Mix_SetPanning() or whatnot again while this
slouken@113
   548
 *  callback is running.
slouken@113
   549
 */
slouken@113
   550
static void _Eff_position_table_s8(int chan, void *stream, int len, void *udata)
slouken@113
   551
{
slouken@113
   552
    volatile position_args *args = (volatile position_args *) udata;
slouken@113
   553
    Sint8 *ptr = (Sint8 *) stream;
slouken@113
   554
    Uint32 *p;
slouken@113
   555
    int i;
slouken@132
   556
    Sint8 *l = ((Sint8 *) _Eff_volume_table) + (256 * args->left_u8);
slouken@132
   557
    Sint8 *r = ((Sint8 *) _Eff_volume_table) + (256 * args->right_u8);
slouken@113
   558
    Sint8 *d = ((Sint8 *) _Eff_volume_table) + (256 * args->distance_u8);
slouken@113
   559
slouken@245
   560
    if (args->room_angle == 180) {
slouken@245
   561
	    Sint8 *temp = l;
slouken@245
   562
	    l = r;
slouken@245
   563
	    r = temp;
slouken@245
   564
    }
slouken@245
   565
slouken@113
   566
slouken@113
   567
    while (len % sizeof (Uint32) != 0) {
slouken@218
   568
        *ptr = d[l[*ptr]];
slouken@218
   569
        ptr++;
slouken@218
   570
        if (args->channels > 1) {
slouken@218
   571
            *ptr = d[r[*ptr]];
slouken@218
   572
            ptr++;
slouken@218
   573
        }
slouken@113
   574
        len -= args->channels;
slouken@113
   575
    }
slouken@113
   576
slouken@113
   577
    p = (Uint32 *) ptr;
slouken@113
   578
slouken@113
   579
    for (i = 0; i < len; i += sizeof (Uint32)) {
slouken@159
   580
#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
slouken@373
   581
        *p = (d[l[((Sint16)(Sint8)((*p & 0xFF000000) >> 24))+128]] << 24) |
slouken@373
   582
             (d[r[((Sint16)(Sint8)((*p & 0x00FF0000) >> 16))+128]] << 16) |
slouken@373
   583
             (d[l[((Sint16)(Sint8)((*p & 0x0000FF00) >>  8))+128]] <<  8) |
slouken@373
   584
             (d[r[((Sint16)(Sint8)((*p & 0x000000FF)      ))+128]]      ) ;
slouken@113
   585
#else
slouken@373
   586
        *p = (d[r[((Sint16)(Sint8)((*p & 0xFF000000) >> 24))+128]] << 24) |
slouken@373
   587
             (d[l[((Sint16)(Sint8)((*p & 0x00FF0000) >> 16))+128]] << 16) |
slouken@373
   588
             (d[r[((Sint16)(Sint8)((*p & 0x0000FF00) >>  8))+128]] <<  8) |
slouken@373
   589
             (d[l[((Sint16)(Sint8)((*p & 0x000000FF)      ))+128]]      ) ;
slouken@113
   590
#endif
slouken@373
   591
        ++p;
slouken@113
   592
    }
slouken@113
   593
slouken@113
   594
slouken@113
   595
}
slouken@113
   596
slouken@113
   597
slouken@113
   598
/* !!! FIXME : Optimize the code for 16-bit samples? */
slouken@113
   599
slouken@113
   600
static void _Eff_position_u16lsb(int chan, void *stream, int len, void *udata)
slouken@113
   601
{
slouken@113
   602
    volatile position_args *args = (volatile position_args *) udata;
slouken@113
   603
    Uint16 *ptr = (Uint16 *) stream;
slouken@113
   604
    int i;
slouken@113
   605
slouken@113
   606
    for (i = 0; i < len; i += sizeof (Uint16) * 2) {
slouken@218
   607
        Sint16 sampl = (Sint16) (SDL_SwapLE16(*(ptr+0)) - 32768);
slouken@218
   608
        Sint16 sampr = (Sint16) (SDL_SwapLE16(*(ptr+1)) - 32768);
icculus@201
   609
        
icculus@201
   610
        Uint16 swapl = (Uint16) ((Sint16) (((float) sampl * args->left_f)
icculus@201
   611
                                    * args->distance_f) + 32768);
icculus@201
   612
        Uint16 swapr = (Uint16) ((Sint16) (((float) sampr * args->right_f)
icculus@201
   613
                                    * args->distance_f) + 32768);
icculus@201
   614
slouken@245
   615
	if (args->room_angle == 180) {
slouken@245
   616
        	*(ptr++) = (Uint16) SDL_SwapLE16(swapr);
slouken@245
   617
        	*(ptr++) = (Uint16) SDL_SwapLE16(swapl);
slouken@245
   618
	}
slouken@245
   619
	else {
slouken@245
   620
        	*(ptr++) = (Uint16) SDL_SwapLE16(swapl);
slouken@245
   621
        	*(ptr++) = (Uint16) SDL_SwapLE16(swapr);
slouken@245
   622
	}
slouken@245
   623
    }
slouken@245
   624
}
slouken@245
   625
static void _Eff_position_u16lsb_c4(int chan, void *stream, int len, void *udata)
slouken@245
   626
{
slouken@245
   627
    volatile position_args *args = (volatile position_args *) udata;
slouken@245
   628
    Uint16 *ptr = (Uint16 *) stream;
slouken@245
   629
    int i;
slouken@245
   630
slouken@245
   631
    for (i = 0; i < len; i += sizeof (Uint16) * 4) {
slouken@245
   632
        Sint16 sampl = (Sint16) (SDL_SwapLE16(*(ptr+0)) - 32768);
slouken@245
   633
        Sint16 sampr = (Sint16) (SDL_SwapLE16(*(ptr+1)) - 32768);
slouken@245
   634
        Sint16 samplr = (Sint16) (SDL_SwapLE16(*(ptr+2)) - 32768);
slouken@245
   635
        Sint16 samprr = (Sint16) (SDL_SwapLE16(*(ptr+3)) - 32768);
slouken@245
   636
        
slouken@245
   637
        Uint16 swapl = (Uint16) ((Sint16) (((float) sampl * args->left_f)
slouken@245
   638
                                    * args->distance_f) + 32768);
slouken@245
   639
        Uint16 swapr = (Uint16) ((Sint16) (((float) sampr * args->right_f)
slouken@245
   640
                                    * args->distance_f) + 32768);
icculus@330
   641
        Uint16 swaplr = (Uint16) ((Sint16) (((float) samplr * args->left_rear_f)
slouken@245
   642
                                    * args->distance_f) + 32768);
icculus@330
   643
        Uint16 swaprr = (Uint16) ((Sint16) (((float) samprr * args->right_rear_f)
slouken@245
   644
                                    * args->distance_f) + 32768);
slouken@245
   645
slouken@245
   646
	switch (args->room_angle) {
slouken@245
   647
		case 0:
slouken@245
   648
        		*(ptr++) = (Uint16) SDL_SwapLE16(swapl);
slouken@245
   649
        		*(ptr++) = (Uint16) SDL_SwapLE16(swapr);
slouken@245
   650
        		*(ptr++) = (Uint16) SDL_SwapLE16(swaplr);
slouken@245
   651
        		*(ptr++) = (Uint16) SDL_SwapLE16(swaprr);
slouken@245
   652
			break;
slouken@245
   653
		case 90:
slouken@245
   654
        		*(ptr++) = (Uint16) SDL_SwapLE16(swapr);
slouken@245
   655
        		*(ptr++) = (Uint16) SDL_SwapLE16(swaprr);
slouken@245
   656
        		*(ptr++) = (Uint16) SDL_SwapLE16(swapl);
slouken@245
   657
        		*(ptr++) = (Uint16) SDL_SwapLE16(swaplr);
slouken@245
   658
			break;
slouken@245
   659
		case 180:
slouken@245
   660
        		*(ptr++) = (Uint16) SDL_SwapLE16(swaprr);
slouken@245
   661
        		*(ptr++) = (Uint16) SDL_SwapLE16(swaplr);
slouken@245
   662
        		*(ptr++) = (Uint16) SDL_SwapLE16(swapr);
slouken@245
   663
        		*(ptr++) = (Uint16) SDL_SwapLE16(swapl);
slouken@245
   664
			break;
slouken@245
   665
		case 270:
slouken@245
   666
        		*(ptr++) = (Uint16) SDL_SwapLE16(swaplr);
slouken@245
   667
        		*(ptr++) = (Uint16) SDL_SwapLE16(swapl);
slouken@245
   668
        		*(ptr++) = (Uint16) SDL_SwapLE16(swaprr);
slouken@245
   669
        		*(ptr++) = (Uint16) SDL_SwapLE16(swapr);
slouken@245
   670
			break;
slouken@245
   671
	}
slouken@245
   672
    }
slouken@245
   673
}
slouken@245
   674
static void _Eff_position_u16lsb_c6(int chan, void *stream, int len, void *udata)
slouken@245
   675
{
slouken@245
   676
    volatile position_args *args = (volatile position_args *) udata;
slouken@245
   677
    Uint16 *ptr = (Uint16 *) stream;
slouken@245
   678
    int i;
slouken@245
   679
slouken@245
   680
    for (i = 0; i < len; i += sizeof (Uint16) * 6) {
slouken@245
   681
        Sint16 sampl = (Sint16) (SDL_SwapLE16(*(ptr+0)) - 32768);
slouken@245
   682
        Sint16 sampr = (Sint16) (SDL_SwapLE16(*(ptr+1)) - 32768);
slouken@245
   683
        Sint16 samplr = (Sint16) (SDL_SwapLE16(*(ptr+2)) - 32768);
slouken@245
   684
        Sint16 samprr = (Sint16) (SDL_SwapLE16(*(ptr+3)) - 32768);
slouken@245
   685
        Sint16 sampce = (Sint16) (SDL_SwapLE16(*(ptr+4)) - 32768);
slouken@245
   686
        Sint16 sampwf = (Sint16) (SDL_SwapLE16(*(ptr+5)) - 32768);
icculus@330
   687
slouken@245
   688
        Uint16 swapl = (Uint16) ((Sint16) (((float) sampl * args->left_f)
slouken@245
   689
                                    * args->distance_f) + 32768);
slouken@245
   690
        Uint16 swapr = (Uint16) ((Sint16) (((float) sampr * args->right_f)
slouken@245
   691
                                    * args->distance_f) + 32768);
icculus@330
   692
        Uint16 swaplr = (Uint16) ((Sint16) (((float) samplr * args->left_rear_f)
slouken@245
   693
                                    * args->distance_f) + 32768);
icculus@330
   694
        Uint16 swaprr = (Uint16) ((Sint16) (((float) samprr * args->right_rear_f)
slouken@245
   695
                                    * args->distance_f) + 32768);
icculus@330
   696
        Uint16 swapce = (Uint16) ((Sint16) (((float) sampce * args->center_f)
slouken@245
   697
                                    * args->distance_f) + 32768);
icculus@330
   698
        Uint16 swapwf = (Uint16) ((Sint16) (((float) sampwf * args->lfe_f)
slouken@245
   699
                                    * args->distance_f) + 32768);
slouken@245
   700
slouken@245
   701
	switch (args->room_angle) {
slouken@245
   702
		case 0:
slouken@245
   703
        		*(ptr++) = (Uint16) SDL_SwapLE16(swapl);
slouken@245
   704
        		*(ptr++) = (Uint16) SDL_SwapLE16(swapr);
slouken@245
   705
        		*(ptr++) = (Uint16) SDL_SwapLE16(swaplr);
slouken@245
   706
        		*(ptr++) = (Uint16) SDL_SwapLE16(swaprr);
slouken@245
   707
        		*(ptr++) = (Uint16) SDL_SwapLE16(swapce);
slouken@245
   708
        		*(ptr++) = (Uint16) SDL_SwapLE16(swapwf);
slouken@245
   709
			break;
slouken@245
   710
		case 90:
slouken@245
   711
        		*(ptr++) = (Uint16) SDL_SwapLE16(swapr);
slouken@245
   712
        		*(ptr++) = (Uint16) SDL_SwapLE16(swaprr);
slouken@245
   713
        		*(ptr++) = (Uint16) SDL_SwapLE16(swapl);
slouken@245
   714
        		*(ptr++) = (Uint16) SDL_SwapLE16(swaplr);
slouken@245
   715
        		*(ptr++) = (Uint16) SDL_SwapLE16(swapr)/2 + (Uint16) SDL_SwapLE16(swaprr)/2;
slouken@245
   716
        		*(ptr++) = (Uint16) SDL_SwapLE16(swapwf);
slouken@245
   717
			break;
slouken@245
   718
		case 180:
slouken@245
   719
        		*(ptr++) = (Uint16) SDL_SwapLE16(swaprr);
slouken@245
   720
        		*(ptr++) = (Uint16) SDL_SwapLE16(swaplr);
slouken@245
   721
        		*(ptr++) = (Uint16) SDL_SwapLE16(swapr);
slouken@245
   722
        		*(ptr++) = (Uint16) SDL_SwapLE16(swapl);
slouken@245
   723
        		*(ptr++) = (Uint16) SDL_SwapLE16(swaprr)/2 + (Uint16) SDL_SwapLE16(swaplr)/2;
slouken@245
   724
        		*(ptr++) = (Uint16) SDL_SwapLE16(swapwf);
slouken@245
   725
			break;
slouken@245
   726
		case 270:
slouken@245
   727
        		*(ptr++) = (Uint16) SDL_SwapLE16(swaplr);
slouken@245
   728
        		*(ptr++) = (Uint16) SDL_SwapLE16(swapl);
slouken@245
   729
        		*(ptr++) = (Uint16) SDL_SwapLE16(swaprr);
slouken@245
   730
        		*(ptr++) = (Uint16) SDL_SwapLE16(swapr);
slouken@245
   731
        		*(ptr++) = (Uint16) SDL_SwapLE16(swapl)/2 + (Uint16) SDL_SwapLE16(swaplr)/2;
slouken@245
   732
        		*(ptr++) = (Uint16) SDL_SwapLE16(swapwf);
slouken@245
   733
			break;
slouken@245
   734
	}
slouken@113
   735
    }
slouken@113
   736
}
slouken@113
   737
slouken@113
   738
static void _Eff_position_s16lsb(int chan, void *stream, int len, void *udata)
slouken@113
   739
{
slouken@113
   740
    /* 16 signed bits (lsb) * 2 channels. */
slouken@113
   741
    volatile position_args *args = (volatile position_args *) udata;
slouken@113
   742
    Sint16 *ptr = (Sint16 *) stream;
slouken@113
   743
    int i;
slouken@113
   744
slouken@245
   745
#if 0
slouken@245
   746
    if (len % (sizeof(Sint16) * 2)) {
slouken@245
   747
	    fprintf(stderr,"Not an even number of frames! len=%d\n", len);
slouken@245
   748
	    return;
slouken@245
   749
    }
slouken@245
   750
#endif
slouken@245
   751
slouken@113
   752
    for (i = 0; i < len; i += sizeof (Sint16) * 2) {
icculus@238
   753
        Sint16 swapl = (Sint16) ((((float) (Sint16) SDL_SwapLE16(*(ptr+0))) *
slouken@113
   754
                                    args->left_f) * args->distance_f);
icculus@238
   755
        Sint16 swapr = (Sint16) ((((float) (Sint16) SDL_SwapLE16(*(ptr+1))) *
slouken@113
   756
                                    args->right_f) * args->distance_f);
slouken@245
   757
	if (args->room_angle == 180) {
slouken@245
   758
        	*(ptr++) = (Sint16) SDL_SwapLE16(swapr);
slouken@245
   759
        	*(ptr++) = (Sint16) SDL_SwapLE16(swapl);
slouken@245
   760
	}
slouken@245
   761
	else {
slouken@245
   762
        	*(ptr++) = (Sint16) SDL_SwapLE16(swapl);
slouken@245
   763
        	*(ptr++) = (Sint16) SDL_SwapLE16(swapr);
slouken@245
   764
	}
slouken@245
   765
    }
slouken@245
   766
}
slouken@245
   767
static void _Eff_position_s16lsb_c4(int chan, void *stream, int len, void *udata)
slouken@245
   768
{
slouken@245
   769
    /* 16 signed bits (lsb) * 4 channels. */
slouken@245
   770
    volatile position_args *args = (volatile position_args *) udata;
slouken@245
   771
    Sint16 *ptr = (Sint16 *) stream;
slouken@245
   772
    int i;
slouken@245
   773
slouken@245
   774
    for (i = 0; i < len; i += sizeof (Sint16) * 4) {
slouken@245
   775
        Sint16 swapl = (Sint16) ((((float) (Sint16) SDL_SwapLE16(*(ptr+0))) *
slouken@245
   776
                                    args->left_f) * args->distance_f);
slouken@245
   777
        Sint16 swapr = (Sint16) ((((float) (Sint16) SDL_SwapLE16(*(ptr+1))) *
slouken@245
   778
                                    args->right_f) * args->distance_f);
slouken@245
   779
        Sint16 swaplr = (Sint16) ((((float) (Sint16) SDL_SwapLE16(*(ptr+1))) *
slouken@245
   780
                                    args->left_rear_f) * args->distance_f);
slouken@245
   781
        Sint16 swaprr = (Sint16) ((((float) (Sint16) SDL_SwapLE16(*(ptr+2))) *
slouken@245
   782
                                    args->right_rear_f) * args->distance_f);
slouken@245
   783
	switch (args->room_angle) {
slouken@245
   784
		case 0:
slouken@245
   785
        		*(ptr++) = (Sint16) SDL_SwapLE16(swapl);
slouken@245
   786
        		*(ptr++) = (Sint16) SDL_SwapLE16(swapr);
slouken@245
   787
        		*(ptr++) = (Sint16) SDL_SwapLE16(swaplr);
slouken@245
   788
        		*(ptr++) = (Sint16) SDL_SwapLE16(swaprr);
slouken@245
   789
			break;
slouken@245
   790
		case 90:
slouken@245
   791
        		*(ptr++) = (Sint16) SDL_SwapLE16(swapr);
slouken@245
   792
        		*(ptr++) = (Sint16) SDL_SwapLE16(swaprr);
slouken@245
   793
        		*(ptr++) = (Sint16) SDL_SwapLE16(swapl);
slouken@245
   794
        		*(ptr++) = (Sint16) SDL_SwapLE16(swaplr);
slouken@245
   795
			break;
slouken@245
   796
		case 180:
slouken@245
   797
        		*(ptr++) = (Sint16) SDL_SwapLE16(swaprr);
slouken@245
   798
        		*(ptr++) = (Sint16) SDL_SwapLE16(swaplr);
slouken@245
   799
        		*(ptr++) = (Sint16) SDL_SwapLE16(swapr);
slouken@245
   800
        		*(ptr++) = (Sint16) SDL_SwapLE16(swapl);
slouken@245
   801
			break;
slouken@245
   802
		case 270:
slouken@245
   803
        		*(ptr++) = (Sint16) SDL_SwapLE16(swaplr);
slouken@245
   804
        		*(ptr++) = (Sint16) SDL_SwapLE16(swapl);
slouken@245
   805
        		*(ptr++) = (Sint16) SDL_SwapLE16(swaprr);
slouken@245
   806
        		*(ptr++) = (Sint16) SDL_SwapLE16(swapr);
slouken@245
   807
			break;
slouken@245
   808
	}
slouken@245
   809
    }
slouken@245
   810
}
slouken@245
   811
slouken@245
   812
static void _Eff_position_s16lsb_c6(int chan, void *stream, int len, void *udata)
slouken@245
   813
{
slouken@245
   814
    /* 16 signed bits (lsb) * 6 channels. */
slouken@245
   815
    volatile position_args *args = (volatile position_args *) udata;
slouken@245
   816
    Sint16 *ptr = (Sint16 *) stream;
slouken@245
   817
    int i;
slouken@245
   818
slouken@245
   819
    for (i = 0; i < len; i += sizeof (Sint16) * 6) {
slouken@245
   820
        Sint16 swapl = (Sint16) ((((float) (Sint16) SDL_SwapLE16(*(ptr+0))) *
slouken@245
   821
                                    args->left_f) * args->distance_f);
slouken@245
   822
        Sint16 swapr = (Sint16) ((((float) (Sint16) SDL_SwapLE16(*(ptr+1))) *
slouken@245
   823
                                    args->right_f) * args->distance_f);
slouken@245
   824
        Sint16 swaplr = (Sint16) ((((float) (Sint16) SDL_SwapLE16(*(ptr+2))) *
slouken@245
   825
                                    args->left_rear_f) * args->distance_f);
slouken@245
   826
        Sint16 swaprr = (Sint16) ((((float) (Sint16) SDL_SwapLE16(*(ptr+3))) *
slouken@245
   827
                                    args->right_rear_f) * args->distance_f);
slouken@245
   828
        Sint16 swapce = (Sint16) ((((float) (Sint16) SDL_SwapLE16(*(ptr+4))) *
slouken@245
   829
                                    args->center_f) * args->distance_f);
slouken@245
   830
        Sint16 swapwf = (Sint16) ((((float) (Sint16) SDL_SwapLE16(*(ptr+5))) *
slouken@245
   831
                                    args->lfe_f) * args->distance_f);
slouken@245
   832
	switch (args->room_angle) {
slouken@245
   833
		case 0:
slouken@245
   834
        		*(ptr++) = (Sint16) SDL_SwapLE16(swapl);
slouken@245
   835
        		*(ptr++) = (Sint16) SDL_SwapLE16(swapr);
slouken@245
   836
        		*(ptr++) = (Sint16) SDL_SwapLE16(swaplr);
slouken@245
   837
        		*(ptr++) = (Sint16) SDL_SwapLE16(swaprr);
slouken@245
   838
        		*(ptr++) = (Sint16) SDL_SwapLE16(swapce);
slouken@245
   839
        		*(ptr++) = (Sint16) SDL_SwapLE16(swapwf);
slouken@245
   840
			break;
slouken@245
   841
		case 90:
slouken@245
   842
        		*(ptr++) = (Sint16) SDL_SwapLE16(swapr);
slouken@245
   843
        		*(ptr++) = (Sint16) SDL_SwapLE16(swaprr);
slouken@245
   844
        		*(ptr++) = (Sint16) SDL_SwapLE16(swapl);
slouken@245
   845
        		*(ptr++) = (Sint16) SDL_SwapLE16(swaplr);
slouken@245
   846
        		*(ptr++) = (Sint16) SDL_SwapLE16(swapr)/2 + (Sint16) SDL_SwapLE16(swaprr)/2;
slouken@245
   847
        		*(ptr++) = (Sint16) SDL_SwapLE16(swapwf);
slouken@245
   848
			break;
slouken@245
   849
		case 180:
slouken@245
   850
        		*(ptr++) = (Sint16) SDL_SwapLE16(swaprr);
slouken@245
   851
        		*(ptr++) = (Sint16) SDL_SwapLE16(swaplr);
slouken@245
   852
        		*(ptr++) = (Sint16) SDL_SwapLE16(swapr);
slouken@245
   853
        		*(ptr++) = (Sint16) SDL_SwapLE16(swapl);
slouken@245
   854
        		*(ptr++) = (Sint16) SDL_SwapLE16(swaprr)/2 + (Sint16) SDL_SwapLE16(swaplr)/2;
slouken@245
   855
        		*(ptr++) = (Sint16) SDL_SwapLE16(swapwf);
slouken@245
   856
			break;
slouken@245
   857
		case 270:
slouken@245
   858
        		*(ptr++) = (Sint16) SDL_SwapLE16(swaplr);
slouken@245
   859
        		*(ptr++) = (Sint16) SDL_SwapLE16(swapl);
slouken@245
   860
        		*(ptr++) = (Sint16) SDL_SwapLE16(swaprr);
slouken@245
   861
        		*(ptr++) = (Sint16) SDL_SwapLE16(swapr);
slouken@245
   862
        		*(ptr++) = (Sint16) SDL_SwapLE16(swapl)/2 + (Sint16) SDL_SwapLE16(swaplr)/2;
slouken@245
   863
        		*(ptr++) = (Sint16) SDL_SwapLE16(swapwf);
slouken@245
   864
			break;
slouken@245
   865
	}
slouken@113
   866
    }
slouken@113
   867
}
slouken@113
   868
slouken@113
   869
static void _Eff_position_u16msb(int chan, void *stream, int len, void *udata)
slouken@113
   870
{
slouken@113
   871
    /* 16 signed bits (lsb) * 2 channels. */
slouken@113
   872
    volatile position_args *args = (volatile position_args *) udata;
slouken@113
   873
    Uint16 *ptr = (Uint16 *) stream;
slouken@113
   874
    int i;
slouken@113
   875
slouken@113
   876
    for (i = 0; i < len; i += sizeof (Sint16) * 2) {
slouken@218
   877
        Sint16 sampl = (Sint16) (SDL_SwapBE16(*(ptr+0)) - 32768);
slouken@218
   878
        Sint16 sampr = (Sint16) (SDL_SwapBE16(*(ptr+1)) - 32768);
icculus@201
   879
        
icculus@201
   880
        Uint16 swapl = (Uint16) ((Sint16) (((float) sampl * args->left_f)
icculus@201
   881
                                    * args->distance_f) + 32768);
icculus@201
   882
        Uint16 swapr = (Uint16) ((Sint16) (((float) sampr * args->right_f)
icculus@201
   883
                                    * args->distance_f) + 32768);
icculus@201
   884
slouken@245
   885
	if (args->room_angle == 180) {
slouken@245
   886
        	*(ptr++) = (Uint16) SDL_SwapBE16(swapr);
slouken@245
   887
        	*(ptr++) = (Uint16) SDL_SwapBE16(swapl);
slouken@245
   888
	}
slouken@245
   889
	else {
slouken@245
   890
        	*(ptr++) = (Uint16) SDL_SwapBE16(swapl);
slouken@245
   891
        	*(ptr++) = (Uint16) SDL_SwapBE16(swapr);
slouken@245
   892
	}
slouken@245
   893
    }
slouken@245
   894
}
slouken@245
   895
static void _Eff_position_u16msb_c4(int chan, void *stream, int len, void *udata)
slouken@245
   896
{
slouken@245
   897
    /* 16 signed bits (lsb) * 4 channels. */
slouken@245
   898
    volatile position_args *args = (volatile position_args *) udata;
slouken@245
   899
    Uint16 *ptr = (Uint16 *) stream;
slouken@245
   900
    int i;
slouken@245
   901
slouken@245
   902
    for (i = 0; i < len; i += sizeof (Sint16) * 4) {
slouken@245
   903
        Sint16 sampl = (Sint16) (SDL_SwapBE16(*(ptr+0)) - 32768);
slouken@245
   904
        Sint16 sampr = (Sint16) (SDL_SwapBE16(*(ptr+1)) - 32768);
slouken@245
   905
        Sint16 samplr = (Sint16) (SDL_SwapBE16(*(ptr+2)) - 32768);
slouken@245
   906
        Sint16 samprr = (Sint16) (SDL_SwapBE16(*(ptr+3)) - 32768);
slouken@245
   907
        
slouken@245
   908
        Uint16 swapl = (Uint16) ((Sint16) (((float) sampl * args->left_f)
slouken@245
   909
                                    * args->distance_f) + 32768);
slouken@245
   910
        Uint16 swapr = (Uint16) ((Sint16) (((float) sampr * args->right_f)
slouken@245
   911
                                    * args->distance_f) + 32768);
slouken@245
   912
        Uint16 swaplr = (Uint16) ((Sint16) (((float) samplr * args->left_rear_f)
slouken@245
   913
                                    * args->distance_f) + 32768);
slouken@245
   914
        Uint16 swaprr = (Uint16) ((Sint16) (((float) samprr * args->right_rear_f)
slouken@245
   915
                                    * args->distance_f) + 32768);
slouken@245
   916
slouken@245
   917
	switch (args->room_angle) {
slouken@245
   918
		case 0:
slouken@245
   919
        		*(ptr++) = (Uint16) SDL_SwapBE16(swapl);
slouken@245
   920
        		*(ptr++) = (Uint16) SDL_SwapBE16(swapr);
slouken@245
   921
        		*(ptr++) = (Uint16) SDL_SwapBE16(swaplr);
slouken@245
   922
        		*(ptr++) = (Uint16) SDL_SwapBE16(swaprr);
slouken@245
   923
			break;
slouken@245
   924
		case 90:
slouken@245
   925
        		*(ptr++) = (Uint16) SDL_SwapBE16(swapr);
slouken@245
   926
        		*(ptr++) = (Uint16) SDL_SwapBE16(swaprr);
slouken@245
   927
        		*(ptr++) = (Uint16) SDL_SwapBE16(swapl);
slouken@245
   928
        		*(ptr++) = (Uint16) SDL_SwapBE16(swaplr);
slouken@245
   929
			break;
slouken@245
   930
		case 180:
slouken@245
   931
        		*(ptr++) = (Uint16) SDL_SwapBE16(swaprr);
slouken@245
   932
        		*(ptr++) = (Uint16) SDL_SwapBE16(swaplr);
slouken@245
   933
        		*(ptr++) = (Uint16) SDL_SwapBE16(swapr);
slouken@245
   934
        		*(ptr++) = (Uint16) SDL_SwapBE16(swapl);
slouken@245
   935
			break;
slouken@245
   936
		case 270:
slouken@245
   937
        		*(ptr++) = (Uint16) SDL_SwapBE16(swaplr);
slouken@245
   938
        		*(ptr++) = (Uint16) SDL_SwapBE16(swapl);
slouken@245
   939
        		*(ptr++) = (Uint16) SDL_SwapBE16(swaprr);
slouken@245
   940
        		*(ptr++) = (Uint16) SDL_SwapBE16(swapr);
slouken@245
   941
			break;
slouken@245
   942
	}
slouken@245
   943
    }
slouken@245
   944
}
slouken@245
   945
static void _Eff_position_u16msb_c6(int chan, void *stream, int len, void *udata)
slouken@245
   946
{
slouken@245
   947
    /* 16 signed bits (lsb) * 6 channels. */
slouken@245
   948
    volatile position_args *args = (volatile position_args *) udata;
slouken@245
   949
    Uint16 *ptr = (Uint16 *) stream;
slouken@245
   950
    int i;
slouken@245
   951
slouken@245
   952
    for (i = 0; i < len; i += sizeof (Sint16) * 6) {
slouken@245
   953
        Sint16 sampl = (Sint16) (SDL_SwapBE16(*(ptr+0)) - 32768);
slouken@245
   954
        Sint16 sampr = (Sint16) (SDL_SwapBE16(*(ptr+1)) - 32768);
slouken@245
   955
        Sint16 samplr = (Sint16) (SDL_SwapBE16(*(ptr+2)) - 32768);
slouken@245
   956
        Sint16 samprr = (Sint16) (SDL_SwapBE16(*(ptr+3)) - 32768);
slouken@245
   957
        Sint16 sampce = (Sint16) (SDL_SwapBE16(*(ptr+4)) - 32768);
slouken@245
   958
        Sint16 sampwf = (Sint16) (SDL_SwapBE16(*(ptr+5)) - 32768);
slouken@245
   959
        
slouken@245
   960
        Uint16 swapl = (Uint16) ((Sint16) (((float) sampl * args->left_f)
slouken@245
   961
                                    * args->distance_f) + 32768);
slouken@245
   962
        Uint16 swapr = (Uint16) ((Sint16) (((float) sampr * args->right_f)
slouken@245
   963
                                    * args->distance_f) + 32768);
slouken@245
   964
        Uint16 swaplr = (Uint16) ((Sint16) (((float) samplr * args->left_rear_f)
slouken@245
   965
                                    * args->distance_f) + 32768);
slouken@245
   966
        Uint16 swaprr = (Uint16) ((Sint16) (((float) samprr * args->right_rear_f)
slouken@245
   967
                                    * args->distance_f) + 32768);
slouken@245
   968
        Uint16 swapce = (Uint16) ((Sint16) (((float) sampce * args->center_f)
slouken@245
   969
                                    * args->distance_f) + 32768);
slouken@245
   970
        Uint16 swapwf = (Uint16) ((Sint16) (((float) sampwf * args->lfe_f)
slouken@245
   971
                                    * args->distance_f) + 32768);
slouken@245
   972
slouken@245
   973
	switch (args->room_angle) {
slouken@245
   974
		case 0:
slouken@245
   975
        		*(ptr++) = (Uint16) SDL_SwapBE16(swapl);
slouken@245
   976
        		*(ptr++) = (Uint16) SDL_SwapBE16(swapr);
slouken@245
   977
        		*(ptr++) = (Uint16) SDL_SwapBE16(swaplr);
slouken@245
   978
        		*(ptr++) = (Uint16) SDL_SwapBE16(swaprr);
slouken@245
   979
        		*(ptr++) = (Uint16) SDL_SwapBE16(swapce);
slouken@245
   980
        		*(ptr++) = (Uint16) SDL_SwapBE16(swapwf);
slouken@245
   981
			break;
slouken@245
   982
		case 90:
slouken@245
   983
        		*(ptr++) = (Uint16) SDL_SwapBE16(swapr);
slouken@245
   984
        		*(ptr++) = (Uint16) SDL_SwapBE16(swaprr);
slouken@245
   985
        		*(ptr++) = (Uint16) SDL_SwapBE16(swapl);
slouken@245
   986
        		*(ptr++) = (Uint16) SDL_SwapBE16(swaplr);
slouken@245
   987
        		*(ptr++) = (Uint16) SDL_SwapBE16(swapr)/2 + (Uint16) SDL_SwapBE16(swaprr)/2;
slouken@245
   988
        		*(ptr++) = (Uint16) SDL_SwapBE16(swapwf);
slouken@245
   989
			break;
slouken@245
   990
		case 180:
slouken@245
   991
        		*(ptr++) = (Uint16) SDL_SwapBE16(swaprr);
slouken@245
   992
        		*(ptr++) = (Uint16) SDL_SwapBE16(swaplr);
slouken@245
   993
        		*(ptr++) = (Uint16) SDL_SwapBE16(swapr);
slouken@245
   994
        		*(ptr++) = (Uint16) SDL_SwapBE16(swapl);
slouken@245
   995
        		*(ptr++) = (Uint16) SDL_SwapBE16(swaprr)/2 + (Uint16) SDL_SwapBE16(swaplr)/2;
slouken@245
   996
        		*(ptr++) = (Uint16) SDL_SwapBE16(swapwf);
slouken@245
   997
			break;
slouken@245
   998
		case 270:
slouken@245
   999
        		*(ptr++) = (Uint16) SDL_SwapBE16(swaplr);
slouken@245
  1000
        		*(ptr++) = (Uint16) SDL_SwapBE16(swapl);
slouken@245
  1001
        		*(ptr++) = (Uint16) SDL_SwapBE16(swaprr);
slouken@245
  1002
        		*(ptr++) = (Uint16) SDL_SwapBE16(swapr);
slouken@245
  1003
        		*(ptr++) = (Uint16) SDL_SwapBE16(swapl)/2 + (Uint16) SDL_SwapBE16(swaplr)/2;
slouken@245
  1004
        		*(ptr++) = (Uint16) SDL_SwapBE16(swapwf);
slouken@245
  1005
			break;
slouken@245
  1006
	}
slouken@113
  1007
    }
slouken@113
  1008
}
slouken@113
  1009
slouken@113
  1010
static void _Eff_position_s16msb(int chan, void *stream, int len, void *udata)
slouken@113
  1011
{
slouken@113
  1012
    /* 16 signed bits (lsb) * 2 channels. */
slouken@113
  1013
    volatile position_args *args = (volatile position_args *) udata;
slouken@113
  1014
    Sint16 *ptr = (Sint16 *) stream;
slouken@113
  1015
    int i;
slouken@113
  1016
slouken@113
  1017
    for (i = 0; i < len; i += sizeof (Sint16) * 2) {
icculus@238
  1018
        Sint16 swapl = (Sint16) ((((float) (Sint16) SDL_SwapBE16(*(ptr+0))) *
slouken@113
  1019
                                    args->left_f) * args->distance_f);
icculus@238
  1020
        Sint16 swapr = (Sint16) ((((float) (Sint16) SDL_SwapBE16(*(ptr+1))) *
slouken@113
  1021
                                    args->right_f) * args->distance_f);
slouken@218
  1022
        *(ptr++) = (Sint16) SDL_SwapBE16(swapl);
slouken@218
  1023
        *(ptr++) = (Sint16) SDL_SwapBE16(swapr);
slouken@113
  1024
    }
slouken@113
  1025
}
slouken@245
  1026
static void _Eff_position_s16msb_c4(int chan, void *stream, int len, void *udata)
slouken@245
  1027
{
slouken@245
  1028
    /* 16 signed bits (lsb) * 4 channels. */
slouken@245
  1029
    volatile position_args *args = (volatile position_args *) udata;
slouken@245
  1030
    Sint16 *ptr = (Sint16 *) stream;
slouken@245
  1031
    int i;
slouken@113
  1032
slouken@245
  1033
    for (i = 0; i < len; i += sizeof (Sint16) * 4) {
slouken@245
  1034
        Sint16 swapl = (Sint16) ((((float) (Sint16) SDL_SwapBE16(*(ptr+0))) *
slouken@245
  1035
                                    args->left_f) * args->distance_f);
slouken@245
  1036
        Sint16 swapr = (Sint16) ((((float) (Sint16) SDL_SwapBE16(*(ptr+1))) *
slouken@245
  1037
                                    args->right_f) * args->distance_f);
slouken@245
  1038
        Sint16 swaplr = (Sint16) ((((float) (Sint16) SDL_SwapBE16(*(ptr+2))) *
slouken@245
  1039
                                    args->left_rear_f) * args->distance_f);
slouken@245
  1040
        Sint16 swaprr = (Sint16) ((((float) (Sint16) SDL_SwapBE16(*(ptr+3))) *
slouken@245
  1041
                                    args->right_rear_f) * args->distance_f);
slouken@245
  1042
	switch (args->room_angle) {
slouken@245
  1043
		case 0:
slouken@245
  1044
        		*(ptr++) = (Sint16) SDL_SwapBE16(swapl);
slouken@245
  1045
        		*(ptr++) = (Sint16) SDL_SwapBE16(swapr);
slouken@245
  1046
        		*(ptr++) = (Sint16) SDL_SwapBE16(swaplr);
slouken@245
  1047
        		*(ptr++) = (Sint16) SDL_SwapBE16(swaprr);
slouken@245
  1048
			break;
slouken@245
  1049
		case 90:
slouken@245
  1050
        		*(ptr++) = (Sint16) SDL_SwapBE16(swapr);
slouken@245
  1051
        		*(ptr++) = (Sint16) SDL_SwapBE16(swaprr);
slouken@245
  1052
        		*(ptr++) = (Sint16) SDL_SwapBE16(swapl);
slouken@245
  1053
        		*(ptr++) = (Sint16) SDL_SwapBE16(swaplr);
slouken@245
  1054
			break;
slouken@245
  1055
		case 180:
slouken@245
  1056
        		*(ptr++) = (Sint16) SDL_SwapBE16(swaprr);
slouken@245
  1057
        		*(ptr++) = (Sint16) SDL_SwapBE16(swaplr);
slouken@245
  1058
        		*(ptr++) = (Sint16) SDL_SwapBE16(swapr);
slouken@245
  1059
        		*(ptr++) = (Sint16) SDL_SwapBE16(swapl);
slouken@245
  1060
			break;
slouken@245
  1061
		case 270:
slouken@245
  1062
        		*(ptr++) = (Sint16) SDL_SwapBE16(swaplr);
slouken@245
  1063
        		*(ptr++) = (Sint16) SDL_SwapBE16(swapl);
slouken@245
  1064
        		*(ptr++) = (Sint16) SDL_SwapBE16(swaprr);
slouken@245
  1065
        		*(ptr++) = (Sint16) SDL_SwapBE16(swapr);
slouken@245
  1066
			break;
slouken@245
  1067
	}
slouken@245
  1068
    }
slouken@245
  1069
}
slouken@245
  1070
static void _Eff_position_s16msb_c6(int chan, void *stream, int len, void *udata)
slouken@245
  1071
{
slouken@245
  1072
    /* 16 signed bits (lsb) * 6 channels. */
slouken@245
  1073
    volatile position_args *args = (volatile position_args *) udata;
slouken@245
  1074
    Sint16 *ptr = (Sint16 *) stream;
slouken@245
  1075
    int i;
slouken@245
  1076
slouken@245
  1077
    for (i = 0; i < len; i += sizeof (Sint16) * 6) {
slouken@245
  1078
        Sint16 swapl = (Sint16) ((((float) (Sint16) SDL_SwapBE16(*(ptr+0))) *
slouken@245
  1079
                                    args->left_f) * args->distance_f);
slouken@245
  1080
        Sint16 swapr = (Sint16) ((((float) (Sint16) SDL_SwapBE16(*(ptr+1))) *
slouken@245
  1081
                                    args->right_f) * args->distance_f);
slouken@245
  1082
        Sint16 swaplr = (Sint16) ((((float) (Sint16) SDL_SwapBE16(*(ptr+2))) *
slouken@245
  1083
                                    args->left_rear_f) * args->distance_f);
slouken@245
  1084
        Sint16 swaprr = (Sint16) ((((float) (Sint16) SDL_SwapBE16(*(ptr+3))) *
slouken@245
  1085
                                    args->right_rear_f) * args->distance_f);
slouken@245
  1086
        Sint16 swapce = (Sint16) ((((float) (Sint16) SDL_SwapBE16(*(ptr+4))) *
slouken@245
  1087
                                    args->center_f) * args->distance_f);
slouken@245
  1088
        Sint16 swapwf = (Sint16) ((((float) (Sint16) SDL_SwapBE16(*(ptr+5))) *
slouken@245
  1089
                                    args->lfe_f) * args->distance_f);
slouken@245
  1090
slouken@245
  1091
	switch (args->room_angle) {
slouken@245
  1092
		case 0:
slouken@245
  1093
        		*(ptr++) = (Sint16) SDL_SwapBE16(swapl);
slouken@245
  1094
        		*(ptr++) = (Sint16) SDL_SwapBE16(swapr);
slouken@245
  1095
        		*(ptr++) = (Sint16) SDL_SwapBE16(swaplr);
slouken@245
  1096
        		*(ptr++) = (Sint16) SDL_SwapBE16(swaprr);
slouken@245
  1097
        		*(ptr++) = (Sint16) SDL_SwapBE16(swapce);
slouken@245
  1098
        		*(ptr++) = (Sint16) SDL_SwapBE16(swapwf);
slouken@245
  1099
			break;
slouken@245
  1100
		case 90:
slouken@245
  1101
        		*(ptr++) = (Sint16) SDL_SwapBE16(swapr);
slouken@245
  1102
        		*(ptr++) = (Sint16) SDL_SwapBE16(swaprr);
slouken@245
  1103
        		*(ptr++) = (Sint16) SDL_SwapBE16(swapl);
slouken@245
  1104
        		*(ptr++) = (Sint16) SDL_SwapBE16(swaplr);
slouken@245
  1105
        		*(ptr++) = (Sint16) SDL_SwapBE16(swapr)/2 + (Sint16) SDL_SwapBE16(swaprr)/2;
slouken@245
  1106
        		*(ptr++) = (Sint16) SDL_SwapBE16(swapwf);
slouken@245
  1107
			break;
slouken@245
  1108
		case 180:
slouken@245
  1109
        		*(ptr++) = (Sint16) SDL_SwapBE16(swaprr);
slouken@245
  1110
        		*(ptr++) = (Sint16) SDL_SwapBE16(swaplr);
slouken@245
  1111
        		*(ptr++) = (Sint16) SDL_SwapBE16(swapr);
slouken@245
  1112
        		*(ptr++) = (Sint16) SDL_SwapBE16(swapl);
slouken@245
  1113
        		*(ptr++) = (Sint16) SDL_SwapBE16(swaprr)/2 + (Sint16) SDL_SwapBE16(swaplr)/2;
slouken@245
  1114
        		*(ptr++) = (Sint16) SDL_SwapBE16(swapwf);
slouken@245
  1115
			break;
slouken@245
  1116
		case 270:
slouken@245
  1117
        		*(ptr++) = (Sint16) SDL_SwapBE16(swaplr);
slouken@245
  1118
        		*(ptr++) = (Sint16) SDL_SwapBE16(swapl);
slouken@245
  1119
        		*(ptr++) = (Sint16) SDL_SwapBE16(swaprr);
slouken@245
  1120
        		*(ptr++) = (Sint16) SDL_SwapBE16(swapr);
slouken@245
  1121
        		*(ptr++) = (Sint16) SDL_SwapBE16(swapl)/2 + (Sint16) SDL_SwapBE16(swaplr)/2;
slouken@245
  1122
        		*(ptr++) = (Sint16) SDL_SwapBE16(swapwf);
slouken@245
  1123
			break;
slouken@245
  1124
	}
slouken@245
  1125
    }
slouken@245
  1126
}
slouken@113
  1127
slouken@113
  1128
static void init_position_args(position_args *args)
slouken@113
  1129
{
slouken@113
  1130
    memset(args, '\0', sizeof (position_args));
slouken@113
  1131
    args->in_use = 0;
slouken@245
  1132
    args->room_angle = 0;
slouken@113
  1133
    args->left_u8 = args->right_u8 = args->distance_u8 = 255;
slouken@113
  1134
    args->left_f  = args->right_f  = args->distance_f  = 1.0f;
slouken@245
  1135
    args->left_rear_u8 = args->right_rear_u8 = args->center_u8 = args->lfe_u8 = 255;
slouken@245
  1136
    args->left_rear_f = args->right_rear_f = args->center_f = args->lfe_f = 1.0f;
slouken@113
  1137
    Mix_QuerySpec(NULL, NULL, (int *) &args->channels);
slouken@113
  1138
}
slouken@113
  1139
slouken@113
  1140
slouken@113
  1141
static position_args *get_position_arg(int channel)
slouken@113
  1142
{
slouken@113
  1143
    void *rc;
slouken@113
  1144
    int i;
slouken@113
  1145
slouken@113
  1146
    if (channel < 0) {
slouken@113
  1147
        if (pos_args_global == NULL) {
slouken@113
  1148
            pos_args_global = malloc(sizeof (position_args));
slouken@113
  1149
            if (pos_args_global == NULL) {
slouken@113
  1150
                Mix_SetError("Out of memory");
slouken@113
  1151
                return(NULL);
slouken@113
  1152
            }
slouken@113
  1153
            init_position_args(pos_args_global);
slouken@113
  1154
        }
slouken@113
  1155
slouken@113
  1156
        return(pos_args_global);
slouken@113
  1157
    }
slouken@113
  1158
slouken@113
  1159
    if (channel >= position_channels) {
slouken@113
  1160
        rc = realloc(pos_args_array, (channel + 1) * sizeof (position_args *));
slouken@113
  1161
        if (rc == NULL) {
slouken@113
  1162
            Mix_SetError("Out of memory");
slouken@113
  1163
            return(NULL);
slouken@113
  1164
        }
slouken@113
  1165
        pos_args_array = (position_args **) rc;
slouken@113
  1166
        for (i = position_channels; i <= channel; i++) {
slouken@113
  1167
            pos_args_array[i] = NULL;
slouken@113
  1168
        }
slouken@113
  1169
        position_channels = channel + 1;
slouken@113
  1170
    }
slouken@113
  1171
slouken@113
  1172
    if (pos_args_array[channel] == NULL) {
slouken@113
  1173
        pos_args_array[channel] = (position_args *)malloc(sizeof(position_args));
slouken@113
  1174
        if (pos_args_array[channel] == NULL) {
slouken@113
  1175
            Mix_SetError("Out of memory");
slouken@113
  1176
            return(NULL);
slouken@113
  1177
        }
slouken@113
  1178
        init_position_args(pos_args_array[channel]);
slouken@113
  1179
    }
slouken@113
  1180
slouken@113
  1181
    return(pos_args_array[channel]);
slouken@113
  1182
}
slouken@113
  1183
slouken@113
  1184
slouken@245
  1185
static Mix_EffectFunc_t get_position_effect_func(Uint16 format, int channels)
slouken@113
  1186
{
slouken@113
  1187
    Mix_EffectFunc_t f = NULL;
slouken@113
  1188
slouken@113
  1189
    switch (format) {
slouken@113
  1190
        case AUDIO_U8:
slouken@245
  1191
	    switch (channels) {
slouken@245
  1192
		    case 1:
slouken@245
  1193
		    case 2:
slouken@245
  1194
            		f = (_Eff_build_volume_table_u8()) ? _Eff_position_table_u8 :
slouken@245
  1195
                                                 		_Eff_position_u8;
slouken@245
  1196
	    		break;
slouken@245
  1197
	    	    case 4:
slouken@250
  1198
                        f = _Eff_position_u8_c4;
slouken@245
  1199
	    		break;
slouken@245
  1200
	    	    case 6:
slouken@250
  1201
                        f = _Eff_position_u8_c6;
slouken@245
  1202
	    		break;
slouken@245
  1203
	    }
slouken@113
  1204
            break;
slouken@113
  1205
slouken@113
  1206
        case AUDIO_S8:
slouken@245
  1207
	    switch (channels) {
slouken@245
  1208
		    case 1:
slouken@245
  1209
		    case 2:
slouken@245
  1210
            		f = (_Eff_build_volume_table_s8()) ? _Eff_position_table_s8 :
slouken@245
  1211
                                                 		_Eff_position_s8;
slouken@245
  1212
	    		break;
slouken@245
  1213
	    	    case 4:
slouken@251
  1214
                        f = _Eff_position_s8_c4;
slouken@245
  1215
	    		break;
slouken@245
  1216
	    	    case 6:
slouken@251
  1217
                        f = _Eff_position_s8_c6;
slouken@245
  1218
	    		break;
slouken@245
  1219
	    }
slouken@113
  1220
            break;
slouken@113
  1221
slouken@113
  1222
        case AUDIO_U16LSB:
slouken@245
  1223
	    switch (channels) {
slouken@245
  1224
		    case 1:
slouken@245
  1225
		    case 2:
slouken@245
  1226
            		f = _Eff_position_u16lsb;
slouken@245
  1227
	    		break;
slouken@245
  1228
	    	    case 4:
slouken@245
  1229
            		f = _Eff_position_u16lsb_c4;
slouken@245
  1230
	    		break;
slouken@245
  1231
	    	    case 6:
slouken@245
  1232
            		f = _Eff_position_u16lsb_c6;
slouken@245
  1233
	    		break;
slouken@245
  1234
	    }
slouken@113
  1235
            break;
slouken@113
  1236
slouken@113
  1237
        case AUDIO_S16LSB:
slouken@245
  1238
	    switch (channels) {
slouken@245
  1239
		    case 1:
slouken@245
  1240
		    case 2:
slouken@245
  1241
            		f = _Eff_position_s16lsb;
slouken@245
  1242
	    		break;
slouken@245
  1243
	    	    case 4:
slouken@245
  1244
            		f = _Eff_position_s16lsb_c4;
slouken@245
  1245
	    		break;
slouken@245
  1246
	    	    case 6:
slouken@245
  1247
            		f = _Eff_position_s16lsb_c6;
slouken@245
  1248
	    		break;
slouken@245
  1249
	    }
slouken@113
  1250
            break;
slouken@113
  1251
slouken@113
  1252
        case AUDIO_U16MSB:
slouken@245
  1253
	    switch (channels) {
slouken@245
  1254
		    case 1:
slouken@245
  1255
		    case 2:
slouken@245
  1256
            		f = _Eff_position_u16msb;
slouken@245
  1257
	    		break;
slouken@245
  1258
	    	    case 4:
slouken@245
  1259
            		f = _Eff_position_u16msb_c4;
slouken@245
  1260
	    		break;
slouken@245
  1261
	    	    case 6:
slouken@245
  1262
            		f = _Eff_position_u16msb_c6;
slouken@245
  1263
	    		break;
slouken@245
  1264
	    }
slouken@113
  1265
            break;
slouken@113
  1266
slouken@113
  1267
        case AUDIO_S16MSB:
slouken@245
  1268
	    switch (channels) {
slouken@245
  1269
		    case 1:
slouken@245
  1270
		    case 2:
slouken@245
  1271
            		f = _Eff_position_s16msb;
slouken@245
  1272
	    		break;
slouken@245
  1273
	    	    case 4:
slouken@245
  1274
            		f = _Eff_position_s16msb_c4;
slouken@245
  1275
	    		break;
slouken@245
  1276
	    	    case 6:
slouken@245
  1277
            		f = _Eff_position_s16msb_c6;
slouken@245
  1278
	    		break;
slouken@245
  1279
	    }
slouken@113
  1280
            break;
slouken@113
  1281
slouken@113
  1282
        default:
slouken@113
  1283
            Mix_SetError("Unsupported audio format");
slouken@113
  1284
    }
slouken@113
  1285
slouken@113
  1286
    return(f);
slouken@113
  1287
}
slouken@113
  1288
slouken@245
  1289
static Uint8 speaker_amplitude[6];
slouken@245
  1290
slouken@245
  1291
static void set_amplitudes(int channels, int angle, int room_angle)
slouken@245
  1292
{
slouken@245
  1293
    int left = 255, right = 255;
slouken@245
  1294
    int left_rear = 255, right_rear = 255, center = 255;
slouken@245
  1295
slouken@245
  1296
        /* unwind the angle...it'll be between 0 and 359. */
slouken@245
  1297
    while (angle >= 360) angle -= 360;
slouken@245
  1298
    while (angle < 0) angle += 360;
slouken@245
  1299
slouken@245
  1300
    if (channels == 2)
slouken@245
  1301
    {
slouken@245
  1302
        /*
slouken@245
  1303
         * We only attenuate by position if the angle falls on the far side
slouken@245
  1304
         *  of center; That is, an angle that's due north would not attenuate
slouken@245
  1305
         *  either channel. Due west attenuates the right channel to 0.0, and
slouken@245
  1306
         *  due east attenuates the left channel to 0.0. Slightly east of
slouken@245
  1307
         *  center attenuates the left channel a little, and the right channel
slouken@245
  1308
         *  not at all. I think of this as occlusion by one's own head.  :)
slouken@245
  1309
         *
slouken@245
  1310
         *   ...so, we split our angle circle into four quadrants...
slouken@245
  1311
         */
slouken@245
  1312
        if (angle < 90) {
slouken@245
  1313
            left = 255 - ((int) (255.0f * (((float) angle) / 89.0f)));
slouken@245
  1314
        } else if (angle < 180) {
slouken@245
  1315
            left = (int) (255.0f * (((float) (angle - 90)) / 89.0f));
slouken@245
  1316
        } else if (angle < 270) {
slouken@245
  1317
            right = 255 - ((int) (255.0f * (((float) (angle - 180)) / 89.0f)));
slouken@245
  1318
        } else {
slouken@245
  1319
            right = (int) (255.0f * (((float) (angle - 270)) / 89.0f));
slouken@245
  1320
        }
slouken@245
  1321
    }
slouken@245
  1322
slouken@245
  1323
    if (channels == 4 || channels == 6)
slouken@245
  1324
    {
slouken@245
  1325
        /*
slouken@245
  1326
         *  An angle that's due north does not attenuate the center channel.
slouken@245
  1327
         *  An angle in the first quadrant, 0-90, does not attenuate the RF.
slouken@245
  1328
         *
slouken@245
  1329
         *   ...so, we split our angle circle into 8 ...
slouken@245
  1330
	 *
slouken@245
  1331
	 *             CE
slouken@245
  1332
	 *             0
slouken@245
  1333
	 *     LF      |         RF
slouken@245
  1334
	 *             |
slouken@245
  1335
	 *  270<-------|----------->90
slouken@245
  1336
	 *             |
slouken@245
  1337
	 *     LR      |         RR
slouken@245
  1338
	 *            180
slouken@245
  1339
	 *   
slouken@245
  1340
         */
slouken@245
  1341
        if (angle < 45) {
slouken@245
  1342
            left = ((int) (255.0f * (((float) (180 - angle)) / 179.0f)));
slouken@245
  1343
            left_rear = 255 - ((int) (255.0f * (((float) (angle + 45)) / 89.0f)));
slouken@245
  1344
            right_rear = 255 - ((int) (255.0f * (((float) (90 - angle)) / 179.0f)));
slouken@245
  1345
        } else if (angle < 90) {
slouken@245
  1346
            center = ((int) (255.0f * (((float) (225 - angle)) / 179.0f)));
slouken@245
  1347
            left = ((int) (255.0f * (((float) (180 - angle)) / 179.0f)));
slouken@245
  1348
            left_rear = 255 - ((int) (255.0f * (((float) (135 - angle)) / 89.0f)));
slouken@245
  1349
            right_rear = ((int) (255.0f * (((float) (90 + angle)) / 179.0f)));
slouken@245
  1350
        } else if (angle < 135) {
slouken@245
  1351
            center = ((int) (255.0f * (((float) (225 - angle)) / 179.0f)));
slouken@245
  1352
            left = 255 - ((int) (255.0f * (((float) (angle - 45)) / 89.0f)));
slouken@245
  1353
            right = ((int) (255.0f * (((float) (270 - angle)) / 179.0f)));
slouken@245
  1354
            left_rear = ((int) (255.0f * (((float) (angle)) / 179.0f)));
slouken@245
  1355
        } else if (angle < 180) {
slouken@245
  1356
            center = 255 - ((int) (255.0f * (((float) (angle - 90)) / 89.0f)));
slouken@245
  1357
            left = 255 - ((int) (255.0f * (((float) (225 - angle)) / 89.0f)));
slouken@245
  1358
            right = ((int) (255.0f * (((float) (270 - angle)) / 179.0f)));
slouken@245
  1359
            left_rear = ((int) (255.0f * (((float) (angle)) / 179.0f)));
slouken@245
  1360
        } else if (angle < 225) {
slouken@245
  1361
            center = 255 - ((int) (255.0f * (((float) (270 - angle)) / 89.0f)));
slouken@245
  1362
            left = ((int) (255.0f * (((float) (angle - 90)) / 179.0f)));
slouken@245
  1363
            right = 255 - ((int) (255.0f * (((float) (angle - 135)) / 89.0f)));
slouken@245
  1364
            right_rear = ((int) (255.0f * (((float) (360 - angle)) / 179.0f)));
slouken@245
  1365
        } else if (angle < 270) {
slouken@245
  1366
            center = ((int) (255.0f * (((float) (angle - 135)) / 179.0f)));
slouken@245
  1367
            left = ((int) (255.0f * (((float) (angle - 90)) / 179.0f)));
slouken@245
  1368
            right = 255 - ((int) (255.0f * (((float) (315 - angle)) / 89.0f)));
slouken@245
  1369
            right_rear = ((int) (255.0f * (((float) (360 - angle)) / 179.0f)));
slouken@245
  1370
        } else if (angle < 315) {
slouken@245
  1371
            center = ((int) (255.0f * (((float) (angle - 135)) / 179.0f)));
slouken@245
  1372
            right = ((int) (255.0f * (((float) (angle - 180)) / 179.0f)));
slouken@245
  1373
            left_rear = ((int) (255.0f * (((float) (450 - angle)) / 179.0f)));
slouken@245
  1374
            right_rear = 255 - ((int) (255.0f * (((float) (angle - 225)) / 89.0f)));
slouken@245
  1375
        } else {
slouken@245
  1376
            right = ((int) (255.0f * (((float) (angle - 180)) / 179.0f)));
slouken@245
  1377
            left_rear = ((int) (255.0f * (((float) (450 - angle)) / 179.0f)));
slouken@245
  1378
            right_rear = 255 - ((int) (255.0f * (((float) (405 - angle)) / 89.0f)));
slouken@245
  1379
        }
slouken@245
  1380
    }
slouken@245
  1381
slouken@245
  1382
    if (left < 0) left = 0; if (left > 255) left = 255;
slouken@245
  1383
    if (right < 0) right = 0; if (right > 255) right = 255;
slouken@245
  1384
    if (left_rear < 0) left_rear = 0; if (left_rear > 255) left_rear = 255;
slouken@245
  1385
    if (right_rear < 0) right_rear = 0; if (right_rear > 255) right_rear = 255;
slouken@245
  1386
    if (center < 0) center = 0; if (center > 255) center = 255;
slouken@245
  1387
slouken@245
  1388
    if (room_angle == 90) {
slouken@245
  1389
    	speaker_amplitude[0] = (Uint8)left_rear;
slouken@245
  1390
    	speaker_amplitude[1] = (Uint8)left;
slouken@245
  1391
    	speaker_amplitude[2] = (Uint8)right_rear;
slouken@245
  1392
    	speaker_amplitude[3] = (Uint8)right;
slouken@245
  1393
    }
slouken@245
  1394
    else if (room_angle == 180) {
slouken@245
  1395
	if (channels == 2) {
slouken@245
  1396
    	    speaker_amplitude[0] = (Uint8)right;
slouken@245
  1397
    	    speaker_amplitude[1] = (Uint8)left;
slouken@245
  1398
	}
slouken@245
  1399
	else {
slouken@245
  1400
    	    speaker_amplitude[0] = (Uint8)right_rear;
slouken@245
  1401
    	    speaker_amplitude[1] = (Uint8)left_rear;
slouken@245
  1402
    	    speaker_amplitude[2] = (Uint8)right;
slouken@245
  1403
    	    speaker_amplitude[3] = (Uint8)left;
slouken@245
  1404
	}
slouken@245
  1405
    }
slouken@245
  1406
    else if (room_angle == 270) {
slouken@245
  1407
    	speaker_amplitude[0] = (Uint8)right;
slouken@245
  1408
    	speaker_amplitude[1] = (Uint8)right_rear;
slouken@245
  1409
    	speaker_amplitude[2] = (Uint8)left;
slouken@245
  1410
    	speaker_amplitude[3] = (Uint8)left_rear;
slouken@245
  1411
    }
slouken@245
  1412
    else {
slouken@245
  1413
    	speaker_amplitude[0] = (Uint8)left;
slouken@245
  1414
    	speaker_amplitude[1] = (Uint8)right;
slouken@245
  1415
    	speaker_amplitude[2] = (Uint8)left_rear;
slouken@245
  1416
    	speaker_amplitude[3] = (Uint8)right_rear;
slouken@245
  1417
    }
slouken@245
  1418
    speaker_amplitude[4] = (Uint8)center;
slouken@245
  1419
    speaker_amplitude[5] = 255;
slouken@245
  1420
}
slouken@245
  1421
slouken@245
  1422
int Mix_SetPosition(int channel, Sint16 angle, Uint8 distance);
slouken@113
  1423
slouken@113
  1424
int Mix_SetPanning(int channel, Uint8 left, Uint8 right)
slouken@113
  1425
{
slouken@113
  1426
    Mix_EffectFunc_t f = NULL;
slouken@113
  1427
    int channels;
slouken@113
  1428
    Uint16 format;
slouken@113
  1429
    position_args *args = NULL;
slouken@113
  1430
    Mix_QuerySpec(NULL, &format, &channels);
slouken@113
  1431
slouken@245
  1432
    if (channels != 2 && channels != 4 && channels != 6)    /* it's a no-op; we call that successful. */
slouken@113
  1433
        return(1);
slouken@113
  1434
slouken@245
  1435
    if (channels > 2) {
icculus@330
  1436
        /* left = right = 255 => angle = 0, to unregister effect as when channels = 2 */
slouken@245
  1437
    	/* left = 255 =>  angle = -90;  left = 0 => angle = +89 */
icculus@330
  1438
        int angle = 0;
icculus@330
  1439
        if ((left != 255) || (right != 255)) {
icculus@330
  1440
	    angle = (int)left;
icculus@330
  1441
    	    angle = 127 - angle;
icculus@330
  1442
	    angle = -angle;
icculus@330
  1443
    	    angle = angle * 90 / 128; /* Make it larger for more effect? */
icculus@330
  1444
        }
icculus@330
  1445
        return( Mix_SetPosition(channel, angle, 0) );
slouken@245
  1446
    }
slouken@245
  1447
slouken@245
  1448
    f = get_position_effect_func(format, channels);
slouken@113
  1449
    if (f == NULL)
slouken@113
  1450
        return(0);
slouken@113
  1451
slouken@113
  1452
    args = get_position_arg(channel);
slouken@113
  1453
    if (!args)
slouken@113
  1454
        return(0);
slouken@113
  1455
slouken@113
  1456
        /* it's a no-op; unregister the effect, if it's registered. */
icculus@330
  1457
    if ((args->distance_u8 == 255) && (left == 255) && (right == 255)) {
icculus@330
  1458
        if (args->in_use) {
icculus@330
  1459
            return(Mix_UnregisterEffect(channel, f));
icculus@330
  1460
        } else {
icculus@330
  1461
	  return(1);
icculus@330
  1462
        }
slouken@113
  1463
    }
slouken@113
  1464
slouken@113
  1465
    args->left_u8 = left;
slouken@245
  1466
    args->left_f = ((float) left) / 255.0f;
slouken@113
  1467
    args->right_u8 = right;
slouken@114
  1468
    args->right_f = ((float) right) / 255.0f;
slouken@245
  1469
    args->room_angle = 0;
slouken@245
  1470
slouken@113
  1471
    if (!args->in_use) {
slouken@113
  1472
        args->in_use = 1;
slouken@113
  1473
        return(Mix_RegisterEffect(channel, f, _Eff_PositionDone, (void *) args));
slouken@113
  1474
    }
slouken@113
  1475
slouken@113
  1476
    return(1);
slouken@113
  1477
}
slouken@113
  1478
slouken@113
  1479
slouken@113
  1480
int Mix_SetDistance(int channel, Uint8 distance)
slouken@113
  1481
{
slouken@113
  1482
    Mix_EffectFunc_t f = NULL;
slouken@113
  1483
    Uint16 format;
slouken@113
  1484
    position_args *args = NULL;
slouken@245
  1485
    int channels;
slouken@113
  1486
slouken@245
  1487
    Mix_QuerySpec(NULL, &format, &channels);
slouken@245
  1488
    f = get_position_effect_func(format, channels);
slouken@113
  1489
    if (f == NULL)
slouken@113
  1490
        return(0);
slouken@113
  1491
slouken@113
  1492
    args = get_position_arg(channel);
slouken@113
  1493
    if (!args)
slouken@113
  1494
        return(0);
slouken@113
  1495
slouken@113
  1496
    distance = 255 - distance;  /* flip it to our scale. */
slouken@113
  1497
slouken@113
  1498
        /* it's a no-op; unregister the effect, if it's registered. */
icculus@330
  1499
    if ((distance == 255) && (args->left_u8 == 255) && (args->right_u8 == 255)) {
icculus@330
  1500
        if (args->in_use) {
icculus@330
  1501
            return(Mix_UnregisterEffect(channel, f));
icculus@330
  1502
        } else {
icculus@330
  1503
            return(1);
icculus@330
  1504
        }
slouken@113
  1505
    }
slouken@113
  1506
slouken@113
  1507
    args->distance_u8 = distance;
slouken@114
  1508
    args->distance_f = ((float) distance) / 255.0f;
slouken@113
  1509
    if (!args->in_use) {
slouken@113
  1510
        args->in_use = 1;
slouken@113
  1511
        return(Mix_RegisterEffect(channel, f, _Eff_PositionDone, (void *) args));
slouken@113
  1512
    }
slouken@113
  1513
slouken@113
  1514
    return(1);
slouken@113
  1515
}
slouken@113
  1516
slouken@113
  1517
slouken@113
  1518
int Mix_SetPosition(int channel, Sint16 angle, Uint8 distance)
slouken@113
  1519
{
slouken@113
  1520
    Mix_EffectFunc_t f = NULL;
slouken@113
  1521
    Uint16 format;
slouken@113
  1522
    int channels;
slouken@113
  1523
    position_args *args = NULL;
slouken@373
  1524
    Sint16 room_angle = 0;
slouken@113
  1525
slouken@113
  1526
    Mix_QuerySpec(NULL, &format, &channels);
slouken@245
  1527
    f = get_position_effect_func(format, channels);
slouken@113
  1528
    if (f == NULL)
slouken@113
  1529
        return(0);
slouken@113
  1530
slouken@113
  1531
        /* unwind the angle...it'll be between 0 and 359. */
slouken@113
  1532
    while (angle >= 360) angle -= 360;
slouken@113
  1533
    while (angle < 0) angle += 360;
slouken@113
  1534
slouken@113
  1535
    args = get_position_arg(channel);
slouken@113
  1536
    if (!args)
slouken@113
  1537
        return(0);
slouken@113
  1538
slouken@113
  1539
        /* it's a no-op; unregister the effect, if it's registered. */
icculus@330
  1540
    if ((!distance) && (!angle)) {
icculus@330
  1541
        if (args->in_use) {
icculus@330
  1542
            return(Mix_UnregisterEffect(channel, f));
icculus@330
  1543
        } else {
icculus@330
  1544
	  return(1);
icculus@330
  1545
	}
icculus@330
  1546
    }
slouken@113
  1547
slouken@113
  1548
    if (channels == 2)
slouken@113
  1549
    {
slouken@245
  1550
	if (angle > 180)
slouken@245
  1551
		room_angle = 180; /* exchange left and right channels */
slouken@245
  1552
	else room_angle = 0;
slouken@113
  1553
    }
slouken@113
  1554
slouken@245
  1555
    if (channels == 4 || channels == 6)
slouken@245
  1556
    {
slouken@245
  1557
	if (angle > 315) room_angle = 0;
slouken@245
  1558
	else if (angle > 225) room_angle = 270;
slouken@245
  1559
	else if (angle > 135) room_angle = 180;
slouken@245
  1560
	else if (angle > 45) room_angle = 90;
slouken@245
  1561
	else room_angle = 0;
slouken@245
  1562
    }
slouken@245
  1563
slouken@245
  1564
slouken@113
  1565
    distance = 255 - distance;  /* flip it to scale Mix_SetDistance() uses. */
slouken@113
  1566
slouken@245
  1567
    set_amplitudes(channels, angle, room_angle);
slouken@245
  1568
slouken@245
  1569
    args->left_u8 = speaker_amplitude[0];
slouken@245
  1570
    args->left_f = ((float) speaker_amplitude[0]) / 255.0f;
slouken@245
  1571
    args->right_u8 = speaker_amplitude[1];
slouken@245
  1572
    args->right_f = ((float) speaker_amplitude[1]) / 255.0f;
slouken@245
  1573
    args->left_rear_u8 = speaker_amplitude[2];
slouken@245
  1574
    args->left_rear_f = ((float) speaker_amplitude[2]) / 255.0f;
slouken@245
  1575
    args->right_rear_u8 = speaker_amplitude[3];
slouken@245
  1576
    args->right_rear_f = ((float) speaker_amplitude[3]) / 255.0f;
slouken@245
  1577
    args->center_u8 = speaker_amplitude[4];
slouken@245
  1578
    args->center_f = ((float) speaker_amplitude[4]) / 255.0f;
icculus@330
  1579
    args->lfe_u8 = speaker_amplitude[5];
icculus@330
  1580
    args->lfe_f = ((float) speaker_amplitude[5]) / 255.0f;
slouken@113
  1581
    args->distance_u8 = distance;
slouken@114
  1582
    args->distance_f = ((float) distance) / 255.0f;
slouken@245
  1583
    args->room_angle = room_angle;
slouken@113
  1584
    if (!args->in_use) {
slouken@113
  1585
        args->in_use = 1;
slouken@113
  1586
        return(Mix_RegisterEffect(channel, f, _Eff_PositionDone, (void *) args));
slouken@113
  1587
    }
slouken@113
  1588
slouken@113
  1589
    return(1);
slouken@113
  1590
}
slouken@113
  1591
slouken@113
  1592
slouken@113
  1593
/* end of effects_position.c ... */
slouken@113
  1594