effect_position.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 15 Jul 2007 15:43:02 +0000
changeset 359 83eaf245b7e7
parent 339 30996c5fd448
child 373 6271eafc6e70
permissions -rw-r--r--
On shutdown, deallocate global memory we might have allocated for effects.

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