effect_stereoreverse.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 11 Sep 2001 19:14:36 +0000
changeset 113 c0c3018bd787
child 114 83ab4ef4458b
permissions -rw-r--r--
Ryan C. Gordon - Tue Sep 11 12:05:54 PDT 2001
* Reworked playwave.c to make it more useful as a mixer testbed
* Added a realtime sound effect API to SDL_mixer.h
* Added the following standard sound effects:
panning, distance attenuation, basic positional audio, stereo reversal
* Added API for mixer versioning: Mix_Linked_Version() and MIX_VERSION()
     1 /*
     2     MIXERLIB:  An audio mixer library based on the SDL library
     3     Copyright (C) 1997-1999  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 #include <sys/time.h>
    30 #include <unistd.h>
    31 #include "SDL.h"
    32 #include "SDL_mixer.h"
    33 
    34 #define __MIX_INTERNAL_EFFECT__
    35 #include "effects_internal.h"
    36 
    37 /* profile code:
    38     #include <sys/time.h>
    39     #include <unistd.h>
    40     struct timeval tv1;
    41     struct timeval tv2;
    42     
    43     gettimeofday(&tv1, NULL);
    44 
    45         ... do your thing here ...
    46 
    47     gettimeofday(&tv2, NULL);
    48     printf("%ld\n", tv2.tv_usec - tv1.tv_usec);
    49 */
    50 
    51 
    52 
    53 /*
    54  * Stereo reversal effect...this one's pretty straightforward...
    55  */
    56 
    57 static void _Eff_reversestereo16(int chan, void *stream, int len, void *udata)
    58 {
    59     /* 16 bits * 2 channels. */
    60     Uint32 *ptr = (Uint32 *) stream;
    61     int i;
    62 
    63     for (i = 0; i < len; i += sizeof (Uint32), ptr++) {
    64         *ptr = (((*ptr) & 0xFFFF0000) >> 16) | (((*ptr) & 0x0000FFFF) << 16);
    65     }
    66 }
    67 
    68 
    69 static void _Eff_reversestereo8(int chan, void *stream, int len, void *udata)
    70 {
    71     /* 8 bits * 2 channels. */
    72     Uint32 *ptr = (Uint32 *) stream;
    73     int i;
    74 
    75     /* get the last two bytes if len is not divisible by four... */
    76     if (len % sizeof (Uint32) != 0) {
    77         Uint16 *p = (Uint16 *) (((Uint8 *) stream) + (len - 2));
    78         *p = (((*p) & 0xFF00) >> 8) | (((*ptr) & 0x00FF) << 8);
    79         len -= 2;
    80     }
    81 
    82     for (i = 0; i < len; i += sizeof (Uint32), ptr++) {
    83         *ptr = (((*ptr) & 0x0000FF00) >> 8) | (((*ptr) & 0x000000FF) << 8) |
    84                (((*ptr) & 0xFF000000) >> 8) | (((*ptr) & 0x00FF0000) << 8);
    85     }
    86 }
    87 
    88 
    89 int Mix_SetReverseStereo(int channel, int flip)
    90 {
    91     Mix_EffectFunc_t f = NULL;
    92     int channels;
    93     Uint16 format;
    94 
    95     Mix_QuerySpec(NULL, &format, &channels);
    96 
    97     if (channels == 2) {
    98         if ((format & 0xFF) == 16)
    99             f = _Eff_reversestereo16;
   100         else if ((format & 0xFF) == 8)
   101             f = _Eff_reversestereo8;
   102         else {
   103             Mix_SetError("Unsupported audio format");
   104             return(0);
   105         }
   106 
   107         if (!flip) {
   108             return(Mix_UnregisterEffect(channel, f));
   109         } else {
   110             return(Mix_RegisterEffect(channel, f, NULL, NULL));
   111         }
   112     }
   113 
   114     return(1);
   115 }
   116 
   117 
   118 /* end of effect_stereoreverse.c ... */
   119