effect_stereoreverse.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 14 Dec 2001 12:56:55 +0000
changeset 138 4d0dc6b4985d
parent 114 83ab4ef4458b
child 241 503416fca921
permissions -rw-r--r--
Updated the copyright information
     1 /*
     2     SDL_mixer:  An audio mixer library based on the SDL library
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     This file by Ryan C. Gordon (icculus@linuxgames.com)
    20 
    21     These are some internally supported special effects that use SDL_mixer's
    22     effect callback API. They are meant for speed over quality.  :)
    23 */
    24 
    25 /* $Id$ */
    26 
    27 #include <stdio.h>
    28 #include <stdlib.h>
    29 
    30 #include "SDL.h"
    31 #include "SDL_mixer.h"
    32 
    33 #define __MIX_INTERNAL_EFFECT__
    34 #include "effects_internal.h"
    35 
    36 /* profile code:
    37     #include <sys/time.h>
    38     #include <unistd.h>
    39     struct timeval tv1;
    40     struct timeval tv2;
    41     
    42     gettimeofday(&tv1, NULL);
    43 
    44         ... do your thing here ...
    45 
    46     gettimeofday(&tv2, NULL);
    47     printf("%ld\n", tv2.tv_usec - tv1.tv_usec);
    48 */
    49 
    50 
    51 
    52 /*
    53  * Stereo reversal effect...this one's pretty straightforward...
    54  */
    55 
    56 static void _Eff_reversestereo16(int chan, void *stream, int len, void *udata)
    57 {
    58     /* 16 bits * 2 channels. */
    59     Uint32 *ptr = (Uint32 *) stream;
    60     int i;
    61 
    62     for (i = 0; i < len; i += sizeof (Uint32), ptr++) {
    63         *ptr = (((*ptr) & 0xFFFF0000) >> 16) | (((*ptr) & 0x0000FFFF) << 16);
    64     }
    65 }
    66 
    67 
    68 static void _Eff_reversestereo8(int chan, void *stream, int len, void *udata)
    69 {
    70     /* 8 bits * 2 channels. */
    71     Uint32 *ptr = (Uint32 *) stream;
    72     int i;
    73 
    74     /* get the last two bytes if len is not divisible by four... */
    75     if (len % sizeof (Uint32) != 0) {
    76         Uint16 *p = (Uint16 *) (((Uint8 *) stream) + (len - 2));
    77         *p = (((*p) & 0xFF00) >> 8) | (((*ptr) & 0x00FF) << 8);
    78         len -= 2;
    79     }
    80 
    81     for (i = 0; i < len; i += sizeof (Uint32), ptr++) {
    82         *ptr = (((*ptr) & 0x0000FF00) >> 8) | (((*ptr) & 0x000000FF) << 8) |
    83                (((*ptr) & 0xFF000000) >> 8) | (((*ptr) & 0x00FF0000) << 8);
    84     }
    85 }
    86 
    87 
    88 int Mix_SetReverseStereo(int channel, int flip)
    89 {
    90     Mix_EffectFunc_t f = NULL;
    91     int channels;
    92     Uint16 format;
    93 
    94     Mix_QuerySpec(NULL, &format, &channels);
    95 
    96     if (channels == 2) {
    97         if ((format & 0xFF) == 16)
    98             f = _Eff_reversestereo16;
    99         else if ((format & 0xFF) == 8)
   100             f = _Eff_reversestereo8;
   101         else {
   102             Mix_SetError("Unsupported audio format");
   103             return(0);
   104         }
   105 
   106         if (!flip) {
   107             return(Mix_UnregisterEffect(channel, f));
   108         } else {
   109             return(Mix_RegisterEffect(channel, f, NULL, NULL));
   110         }
   111     }
   112 
   113     return(1);
   114 }
   115 
   116 
   117 /* end of effect_stereoreverse.c ... */
   118