effect_stereoreverse.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 26 Oct 2018 11:04:13 -0700
changeset 915 2e274cefe1db
parent 848 3907db698eb5
child 926 d6c9518fb5ee
permissions -rw-r--r--
Updated to version 2.0.4
slouken@113
     1
/*
slouken@518
     2
  SDL_mixer:  An audio mixer library based on the SDL library
slouken@848
     3
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
slouken@113
     4
slouken@518
     5
  This software is provided 'as-is', without any express or implied
slouken@518
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@518
     7
  arising from the use of this software.
slouken@113
     8
slouken@518
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@518
    10
  including commercial applications, and to alter it and redistribute it
slouken@518
    11
  freely, subject to the following restrictions:
slouken@113
    12
slouken@518
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@518
    14
     claim that you wrote the original software. If you use this software
slouken@518
    15
     in a product, an acknowledgment in the product documentation would be
slouken@518
    16
     appreciated but is not required.
slouken@518
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@518
    18
     misrepresented as being the original software.
slouken@518
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@113
    20
slouken@518
    21
  This file by Ryan C. Gordon (icculus@icculus.org)
slouken@113
    22
slouken@518
    23
  These are some internally supported special effects that use SDL_mixer's
slouken@518
    24
  effect callback API. They are meant for speed over quality.  :)
slouken@113
    25
*/
slouken@113
    26
slouken@113
    27
/* $Id$ */
slouken@113
    28
slouken@113
    29
#include <stdio.h>
slouken@113
    30
#include <stdlib.h>
slouken@114
    31
slouken@113
    32
#include "SDL.h"
slouken@113
    33
#include "SDL_mixer.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@617
    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
/*
slouken@113
    55
 * Stereo reversal effect...this one's pretty straightforward...
slouken@113
    56
 */
slouken@113
    57
sezero@817
    58
static void SDLCALL _Eff_reversestereo32(int chan, void *stream, int len, void *udata)
slouken@734
    59
{
slouken@734
    60
    /* 16 bits * 2 channels. */
slouken@734
    61
    Uint32 *ptr = (Uint32 *) stream;
slouken@800
    62
    Uint32 tmp;
slouken@734
    63
    int i;
slouken@734
    64
slouken@734
    65
    for (i = 0; i < len; i += 2 * sizeof (Uint32), ptr += 2) {
slouken@800
    66
        tmp = ptr[0];
slouken@800
    67
        ptr[0] = ptr[1];
slouken@800
    68
        ptr[1] = tmp;
slouken@734
    69
    }
slouken@734
    70
}
slouken@734
    71
slouken@734
    72
sezero@817
    73
static void SDLCALL _Eff_reversestereo16(int chan, void *stream, int len, void *udata)
slouken@113
    74
{
slouken@113
    75
    /* 16 bits * 2 channels. */
slouken@113
    76
    Uint32 *ptr = (Uint32 *) stream;
slouken@113
    77
    int i;
slouken@113
    78
slouken@113
    79
    for (i = 0; i < len; i += sizeof (Uint32), ptr++) {
slouken@113
    80
        *ptr = (((*ptr) & 0xFFFF0000) >> 16) | (((*ptr) & 0x0000FFFF) << 16);
slouken@113
    81
    }
slouken@113
    82
}
slouken@113
    83
slouken@113
    84
sezero@817
    85
static void SDLCALL _Eff_reversestereo8(int chan, void *stream, int len, void *udata)
slouken@113
    86
{
slouken@113
    87
    /* 8 bits * 2 channels. */
slouken@113
    88
    Uint32 *ptr = (Uint32 *) stream;
slouken@113
    89
    int i;
slouken@113
    90
slouken@113
    91
    /* get the last two bytes if len is not divisible by four... */
slouken@113
    92
    if (len % sizeof (Uint32) != 0) {
slouken@113
    93
        Uint16 *p = (Uint16 *) (((Uint8 *) stream) + (len - 2));
slouken@310
    94
        *p = (Uint16)((((*p) & 0xFF00) >> 8) | (((*ptr) & 0x00FF) << 8));
slouken@113
    95
        len -= 2;
slouken@113
    96
    }
slouken@113
    97
slouken@113
    98
    for (i = 0; i < len; i += sizeof (Uint32), ptr++) {
slouken@113
    99
        *ptr = (((*ptr) & 0x0000FF00) >> 8) | (((*ptr) & 0x000000FF) << 8) |
slouken@113
   100
               (((*ptr) & 0xFF000000) >> 8) | (((*ptr) & 0x00FF0000) << 8);
slouken@113
   101
    }
slouken@113
   102
}
slouken@113
   103
slouken@113
   104
slouken@113
   105
int Mix_SetReverseStereo(int channel, int flip)
slouken@113
   106
{
slouken@113
   107
    Mix_EffectFunc_t f = NULL;
slouken@113
   108
    int channels;
slouken@113
   109
    Uint16 format;
slouken@113
   110
slouken@113
   111
    Mix_QuerySpec(NULL, &format, &channels);
slouken@113
   112
slouken@113
   113
    if (channels == 2) {
slouken@800
   114
        int bits = (format & 0xFF);
slouken@800
   115
        switch (bits) {
slouken@800
   116
        case 8:
slouken@734
   117
            f = _Eff_reversestereo8;
slouken@800
   118
            break;
slouken@800
   119
        case 16:
slouken@113
   120
            f = _Eff_reversestereo16;
slouken@800
   121
            break;
slouken@800
   122
        case 32:
slouken@734
   123
            f = _Eff_reversestereo32;
slouken@800
   124
            break;
slouken@800
   125
        default:
slouken@113
   126
            Mix_SetError("Unsupported audio format");
slouken@113
   127
            return(0);
slouken@113
   128
        }
slouken@113
   129
slouken@113
   130
        if (!flip) {
slouken@113
   131
            return(Mix_UnregisterEffect(channel, f));
slouken@113
   132
        } else {
slouken@113
   133
            return(Mix_RegisterEffect(channel, f, NULL, NULL));
slouken@113
   134
        }
slouken@734
   135
    } else {
slouken@800
   136
        Mix_SetError("Trying to reverse stereo on a non-stereo stream");
slouken@800
   137
        return(0);
slouken@800
   138
    }
slouken@113
   139
slouken@113
   140
    return(1);
slouken@113
   141
}
slouken@113
   142
slouken@113
   143
slouken@113
   144
/* end of effect_stereoreverse.c ... */
slouken@113
   145
slouken@777
   146
/* vi: set ts=4 sw=4 expandtab: */