effect_stereoreverse.c
author Ozkan Sezer <sezeroz@gmail.com>
Sun, 07 Oct 2018 03:07:33 +0300
branchSDL-1.2
changeset 885 dd251a0ec666
parent 518 8bc9b5fd2aae
child 601 05123263dab3
child 887 e49459791ae8
permissions -rw-r--r--
backport fix for bug #2795. (from 2.0 branch commit b28b41b93ba7).
slouken@113
     1
/*
slouken@518
     2
  SDL_mixer:  An audio mixer library based on the SDL library
slouken@518
     3
  Copyright (C) 1997-2012 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@113
    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
slouken@113
    58
static void _Eff_reversestereo16(int chan, void *stream, int len, void *udata)
slouken@113
    59
{
slouken@113
    60
    /* 16 bits * 2 channels. */
slouken@113
    61
    Uint32 *ptr = (Uint32 *) stream;
slouken@113
    62
    int i;
slouken@113
    63
slouken@113
    64
    for (i = 0; i < len; i += sizeof (Uint32), ptr++) {
slouken@113
    65
        *ptr = (((*ptr) & 0xFFFF0000) >> 16) | (((*ptr) & 0x0000FFFF) << 16);
slouken@113
    66
    }
slouken@113
    67
}
slouken@113
    68
slouken@113
    69
slouken@113
    70
static void _Eff_reversestereo8(int chan, void *stream, int len, void *udata)
slouken@113
    71
{
slouken@113
    72
    /* 8 bits * 2 channels. */
slouken@113
    73
    Uint32 *ptr = (Uint32 *) stream;
slouken@113
    74
    int i;
slouken@113
    75
slouken@113
    76
    /* get the last two bytes if len is not divisible by four... */
slouken@113
    77
    if (len % sizeof (Uint32) != 0) {
slouken@113
    78
        Uint16 *p = (Uint16 *) (((Uint8 *) stream) + (len - 2));
slouken@310
    79
        *p = (Uint16)((((*p) & 0xFF00) >> 8) | (((*ptr) & 0x00FF) << 8));
slouken@113
    80
        len -= 2;
slouken@113
    81
    }
slouken@113
    82
slouken@113
    83
    for (i = 0; i < len; i += sizeof (Uint32), ptr++) {
slouken@113
    84
        *ptr = (((*ptr) & 0x0000FF00) >> 8) | (((*ptr) & 0x000000FF) << 8) |
slouken@113
    85
               (((*ptr) & 0xFF000000) >> 8) | (((*ptr) & 0x00FF0000) << 8);
slouken@113
    86
    }
slouken@113
    87
}
slouken@113
    88
slouken@113
    89
slouken@113
    90
int Mix_SetReverseStereo(int channel, int flip)
slouken@113
    91
{
slouken@113
    92
    Mix_EffectFunc_t f = NULL;
slouken@113
    93
    int channels;
slouken@113
    94
    Uint16 format;
slouken@113
    95
slouken@113
    96
    Mix_QuerySpec(NULL, &format, &channels);
slouken@113
    97
slouken@113
    98
    if (channels == 2) {
slouken@113
    99
        if ((format & 0xFF) == 16)
slouken@113
   100
            f = _Eff_reversestereo16;
slouken@113
   101
        else if ((format & 0xFF) == 8)
slouken@113
   102
            f = _Eff_reversestereo8;
slouken@113
   103
        else {
slouken@113
   104
            Mix_SetError("Unsupported audio format");
slouken@113
   105
            return(0);
slouken@113
   106
        }
slouken@113
   107
slouken@113
   108
        if (!flip) {
slouken@113
   109
            return(Mix_UnregisterEffect(channel, f));
slouken@113
   110
        } else {
slouken@113
   111
            return(Mix_RegisterEffect(channel, f, NULL, NULL));
slouken@113
   112
        }
slouken@113
   113
    }
slouken@113
   114
slouken@113
   115
    return(1);
slouken@113
   116
}
slouken@113
   117
slouken@113
   118
slouken@113
   119
/* end of effect_stereoreverse.c ... */
slouken@113
   120