effect_stereoreverse.c
changeset 113 c0c3018bd787
child 114 83ab4ef4458b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/effect_stereoreverse.c	Tue Sep 11 19:14:36 2001 +0000
     1.3 @@ -0,0 +1,119 @@
     1.4 +/*
     1.5 +    MIXERLIB:  An audio mixer library based on the SDL library
     1.6 +    Copyright (C) 1997-1999  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    This file by Ryan C. Gordon (icculus@linuxgames.com)
    1.23 +
    1.24 +    These are some internally supported special effects that use SDL_mixer's
    1.25 +    effect callback API. They are meant for speed over quality.  :)
    1.26 +*/
    1.27 +
    1.28 +/* $Id$ */
    1.29 +
    1.30 +#include <stdio.h>
    1.31 +#include <stdlib.h>
    1.32 +#include <sys/time.h>
    1.33 +#include <unistd.h>
    1.34 +#include "SDL.h"
    1.35 +#include "SDL_mixer.h"
    1.36 +
    1.37 +#define __MIX_INTERNAL_EFFECT__
    1.38 +#include "effects_internal.h"
    1.39 +
    1.40 +/* profile code:
    1.41 +    #include <sys/time.h>
    1.42 +    #include <unistd.h>
    1.43 +    struct timeval tv1;
    1.44 +    struct timeval tv2;
    1.45 +    
    1.46 +    gettimeofday(&tv1, NULL);
    1.47 +
    1.48 +        ... do your thing here ...
    1.49 +
    1.50 +    gettimeofday(&tv2, NULL);
    1.51 +    printf("%ld\n", tv2.tv_usec - tv1.tv_usec);
    1.52 +*/
    1.53 +
    1.54 +
    1.55 +
    1.56 +/*
    1.57 + * Stereo reversal effect...this one's pretty straightforward...
    1.58 + */
    1.59 +
    1.60 +static void _Eff_reversestereo16(int chan, void *stream, int len, void *udata)
    1.61 +{
    1.62 +    /* 16 bits * 2 channels. */
    1.63 +    Uint32 *ptr = (Uint32 *) stream;
    1.64 +    int i;
    1.65 +
    1.66 +    for (i = 0; i < len; i += sizeof (Uint32), ptr++) {
    1.67 +        *ptr = (((*ptr) & 0xFFFF0000) >> 16) | (((*ptr) & 0x0000FFFF) << 16);
    1.68 +    }
    1.69 +}
    1.70 +
    1.71 +
    1.72 +static void _Eff_reversestereo8(int chan, void *stream, int len, void *udata)
    1.73 +{
    1.74 +    /* 8 bits * 2 channels. */
    1.75 +    Uint32 *ptr = (Uint32 *) stream;
    1.76 +    int i;
    1.77 +
    1.78 +    /* get the last two bytes if len is not divisible by four... */
    1.79 +    if (len % sizeof (Uint32) != 0) {
    1.80 +        Uint16 *p = (Uint16 *) (((Uint8 *) stream) + (len - 2));
    1.81 +        *p = (((*p) & 0xFF00) >> 8) | (((*ptr) & 0x00FF) << 8);
    1.82 +        len -= 2;
    1.83 +    }
    1.84 +
    1.85 +    for (i = 0; i < len; i += sizeof (Uint32), ptr++) {
    1.86 +        *ptr = (((*ptr) & 0x0000FF00) >> 8) | (((*ptr) & 0x000000FF) << 8) |
    1.87 +               (((*ptr) & 0xFF000000) >> 8) | (((*ptr) & 0x00FF0000) << 8);
    1.88 +    }
    1.89 +}
    1.90 +
    1.91 +
    1.92 +int Mix_SetReverseStereo(int channel, int flip)
    1.93 +{
    1.94 +    Mix_EffectFunc_t f = NULL;
    1.95 +    int channels;
    1.96 +    Uint16 format;
    1.97 +
    1.98 +    Mix_QuerySpec(NULL, &format, &channels);
    1.99 +
   1.100 +    if (channels == 2) {
   1.101 +        if ((format & 0xFF) == 16)
   1.102 +            f = _Eff_reversestereo16;
   1.103 +        else if ((format & 0xFF) == 8)
   1.104 +            f = _Eff_reversestereo8;
   1.105 +        else {
   1.106 +            Mix_SetError("Unsupported audio format");
   1.107 +            return(0);
   1.108 +        }
   1.109 +
   1.110 +        if (!flip) {
   1.111 +            return(Mix_UnregisterEffect(channel, f));
   1.112 +        } else {
   1.113 +            return(Mix_RegisterEffect(channel, f, NULL, NULL));
   1.114 +        }
   1.115 +    }
   1.116 +
   1.117 +    return(1);
   1.118 +}
   1.119 +
   1.120 +
   1.121 +/* end of effect_stereoreverse.c ... */
   1.122 +