effect_stereoreverse.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 08 Dec 2008 00:27:32 +0000
changeset 386 695494546b3c
parent 339 30996c5fd448
child 518 8bc9b5fd2aae
permissions -rw-r--r--
Updated copyright date
slouken@113
     1
/*
slouken@138
     2
    SDL_mixer:  An audio mixer library based on the SDL library
slouken@386
     3
    Copyright (C) 1997-2009 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
slouken@113
    30
#include "SDL.h"
slouken@113
    31
#include "SDL_mixer.h"
slouken@113
    32
slouken@113
    33
#define __MIX_INTERNAL_EFFECT__
slouken@113
    34
#include "effects_internal.h"
slouken@113
    35
slouken@113
    36
/* profile code:
slouken@113
    37
    #include <sys/time.h>
slouken@113
    38
    #include <unistd.h>
slouken@113
    39
    struct timeval tv1;
slouken@113
    40
    struct timeval tv2;
slouken@113
    41
    
slouken@113
    42
    gettimeofday(&tv1, NULL);
slouken@113
    43
slouken@113
    44
        ... do your thing here ...
slouken@113
    45
slouken@113
    46
    gettimeofday(&tv2, NULL);
slouken@113
    47
    printf("%ld\n", tv2.tv_usec - tv1.tv_usec);
slouken@113
    48
*/
slouken@113
    49
slouken@113
    50
slouken@113
    51
slouken@113
    52
/*
slouken@113
    53
 * Stereo reversal effect...this one's pretty straightforward...
slouken@113
    54
 */
slouken@113
    55
slouken@113
    56
static void _Eff_reversestereo16(int chan, void *stream, int len, void *udata)
slouken@113
    57
{
slouken@113
    58
    /* 16 bits * 2 channels. */
slouken@113
    59
    Uint32 *ptr = (Uint32 *) stream;
slouken@113
    60
    int i;
slouken@113
    61
slouken@113
    62
    for (i = 0; i < len; i += sizeof (Uint32), ptr++) {
slouken@113
    63
        *ptr = (((*ptr) & 0xFFFF0000) >> 16) | (((*ptr) & 0x0000FFFF) << 16);
slouken@113
    64
    }
slouken@113
    65
}
slouken@113
    66
slouken@113
    67
slouken@113
    68
static void _Eff_reversestereo8(int chan, void *stream, int len, void *udata)
slouken@113
    69
{
slouken@113
    70
    /* 8 bits * 2 channels. */
slouken@113
    71
    Uint32 *ptr = (Uint32 *) stream;
slouken@113
    72
    int i;
slouken@113
    73
slouken@113
    74
    /* get the last two bytes if len is not divisible by four... */
slouken@113
    75
    if (len % sizeof (Uint32) != 0) {
slouken@113
    76
        Uint16 *p = (Uint16 *) (((Uint8 *) stream) + (len - 2));
slouken@310
    77
        *p = (Uint16)((((*p) & 0xFF00) >> 8) | (((*ptr) & 0x00FF) << 8));
slouken@113
    78
        len -= 2;
slouken@113
    79
    }
slouken@113
    80
slouken@113
    81
    for (i = 0; i < len; i += sizeof (Uint32), ptr++) {
slouken@113
    82
        *ptr = (((*ptr) & 0x0000FF00) >> 8) | (((*ptr) & 0x000000FF) << 8) |
slouken@113
    83
               (((*ptr) & 0xFF000000) >> 8) | (((*ptr) & 0x00FF0000) << 8);
slouken@113
    84
    }
slouken@113
    85
}
slouken@113
    86
slouken@113
    87
slouken@113
    88
int Mix_SetReverseStereo(int channel, int flip)
slouken@113
    89
{
slouken@113
    90
    Mix_EffectFunc_t f = NULL;
slouken@113
    91
    int channels;
slouken@113
    92
    Uint16 format;
slouken@113
    93
slouken@113
    94
    Mix_QuerySpec(NULL, &format, &channels);
slouken@113
    95
slouken@113
    96
    if (channels == 2) {
slouken@113
    97
        if ((format & 0xFF) == 16)
slouken@113
    98
            f = _Eff_reversestereo16;
slouken@113
    99
        else if ((format & 0xFF) == 8)
slouken@113
   100
            f = _Eff_reversestereo8;
slouken@113
   101
        else {
slouken@113
   102
            Mix_SetError("Unsupported audio format");
slouken@113
   103
            return(0);
slouken@113
   104
        }
slouken@113
   105
slouken@113
   106
        if (!flip) {
slouken@113
   107
            return(Mix_UnregisterEffect(channel, f));
slouken@113
   108
        } else {
slouken@113
   109
            return(Mix_RegisterEffect(channel, f, NULL, NULL));
slouken@113
   110
        }
slouken@113
   111
    }
slouken@113
   112
slouken@113
   113
    return(1);
slouken@113
   114
}
slouken@113
   115
slouken@113
   116
slouken@113
   117
/* end of effect_stereoreverse.c ... */
slouken@113
   118