src/effect_stereoreverse.c
author Ozkan Sezer
Wed, 18 Dec 2019 18:55:56 +0300
changeset 1098 3be892f72aa7
parent 1047 ee23898a09a2
child 1129 888c7be704ce
permissions -rw-r--r--
fix build.
admin@999
     1
/*
admin@999
     2
  SDL_mixer:  An audio mixer library based on the SDL library
admin@999
     3
  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
admin@999
     4
admin@999
     5
  This software is provided 'as-is', without any express or implied
admin@999
     6
  warranty.  In no event will the authors be held liable for any damages
admin@999
     7
  arising from the use of this software.
admin@999
     8
admin@999
     9
  Permission is granted to anyone to use this software for any purpose,
admin@999
    10
  including commercial applications, and to alter it and redistribute it
admin@999
    11
  freely, subject to the following restrictions:
admin@999
    12
admin@999
    13
  1. The origin of this software must not be misrepresented; you must not
admin@999
    14
     claim that you wrote the original software. If you use this software
admin@999
    15
     in a product, an acknowledgment in the product documentation would be
admin@999
    16
     appreciated but is not required.
admin@999
    17
  2. Altered source versions must be plainly marked as such, and must not be
admin@999
    18
     misrepresented as being the original software.
admin@999
    19
  3. This notice may not be removed or altered from any source distribution.
admin@999
    20
admin@999
    21
  This file by Ryan C. Gordon (icculus@icculus.org)
admin@999
    22
admin@999
    23
  These are some internally supported special effects that use SDL_mixer's
admin@999
    24
  effect callback API. They are meant for speed over quality.  :)
admin@999
    25
*/
admin@999
    26
admin@999
    27
#include <stdio.h>
admin@999
    28
#include <stdlib.h>
admin@999
    29
admin@999
    30
#include "SDL.h"
admin@999
    31
#include "SDL_mixer.h"
admin@999
    32
admin@1047
    33
#define MIX_INTERNAL_EFFECT__
admin@999
    34
#include "effects_internal.h"
admin@999
    35
admin@999
    36
/* profile code:
admin@999
    37
    #include <sys/time.h>
admin@999
    38
    #include <unistd.h>
admin@999
    39
    struct timeval tv1;
admin@999
    40
    struct timeval tv2;
admin@999
    41
admin@999
    42
    gettimeofday(&tv1, NULL);
admin@999
    43
admin@999
    44
        ... do your thing here ...
admin@999
    45
admin@999
    46
    gettimeofday(&tv2, NULL);
admin@999
    47
    printf("%ld\n", tv2.tv_usec - tv1.tv_usec);
admin@999
    48
*/
admin@999
    49
admin@999
    50
admin@999
    51
/*
admin@999
    52
 * Stereo reversal effect...this one's pretty straightforward...
admin@999
    53
 */
admin@999
    54
admin@999
    55
static void SDLCALL _Eff_reversestereo32(int chan, void *stream, int len, void *udata)
admin@999
    56
{
admin@999
    57
    /* 16 bits * 2 channels. */
admin@999
    58
    Uint32 *ptr = (Uint32 *) stream;
admin@999
    59
    Uint32 tmp;
admin@999
    60
    int i;
admin@999
    61
admin@999
    62
    (void)chan;
admin@999
    63
    (void)udata;
admin@999
    64
admin@999
    65
    for (i = 0; i < len; i += 2 * sizeof (Uint32), ptr += 2) {
admin@999
    66
        tmp = ptr[0];
admin@999
    67
        ptr[0] = ptr[1];
admin@999
    68
        ptr[1] = tmp;
admin@999
    69
    }
admin@999
    70
}
admin@999
    71
admin@999
    72
static void SDLCALL _Eff_reversestereo16(int chan, void *stream, int len, void *udata)
admin@999
    73
{
admin@999
    74
    /* 16 bits * 2 channels. */
admin@999
    75
    Uint32 *ptr = (Uint32 *) stream;
admin@999
    76
    int i;
admin@999
    77
admin@999
    78
    (void)chan;
admin@999
    79
    (void)udata;
admin@999
    80
admin@999
    81
    for (i = 0; i < len; i += sizeof (Uint32), ptr++) {
admin@999
    82
        *ptr = (((*ptr) & 0xFFFF0000) >> 16) | (((*ptr) & 0x0000FFFF) << 16);
admin@999
    83
    }
admin@999
    84
}
admin@999
    85
admin@999
    86
static void SDLCALL _Eff_reversestereo8(int chan, void *stream, int len, void *udata)
admin@999
    87
{
admin@999
    88
    /* 8 bits * 2 channels. */
admin@999
    89
    Uint32 *ptr = (Uint32 *) stream;
admin@999
    90
    int i;
admin@999
    91
admin@999
    92
    (void)chan;
admin@999
    93
    (void)udata;
admin@999
    94
admin@999
    95
    /* get the last two bytes if len is not divisible by four... */
admin@999
    96
    if (len % (int)sizeof(Uint32) != 0) {
admin@999
    97
        Uint16 *p = (Uint16 *) (((Uint8 *) stream) + (len - 2));
admin@999
    98
        *p = (Uint16)((((*p) & 0xFF00) >> 8) | (((*ptr) & 0x00FF) << 8));
admin@999
    99
        len -= 2;
admin@999
   100
    }
admin@999
   101
admin@999
   102
    for (i = 0; i < len; i += sizeof (Uint32), ptr++) {
admin@999
   103
        *ptr = (((*ptr) & 0x0000FF00) >> 8) | (((*ptr) & 0x000000FF) << 8) |
admin@999
   104
               (((*ptr) & 0xFF000000) >> 8) | (((*ptr) & 0x00FF0000) << 8);
admin@999
   105
    }
admin@999
   106
}
admin@999
   107
admin@999
   108
admin@999
   109
int Mix_SetReverseStereo(int channel, int flip)
admin@999
   110
{
admin@999
   111
    Mix_EffectFunc_t f = NULL;
admin@999
   112
    int channels;
admin@999
   113
    Uint16 format;
admin@999
   114
admin@999
   115
    Mix_QuerySpec(NULL, &format, &channels);
admin@999
   116
admin@999
   117
    if (channels == 2) {
admin@999
   118
        int bits = (format & 0xFF);
admin@999
   119
        switch (bits) {
admin@999
   120
        case 8:
admin@999
   121
            f = _Eff_reversestereo8;
admin@999
   122
            break;
admin@999
   123
        case 16:
admin@999
   124
            f = _Eff_reversestereo16;
admin@999
   125
            break;
admin@999
   126
        case 32:
admin@999
   127
            f = _Eff_reversestereo32;
admin@999
   128
            break;
admin@999
   129
        default:
admin@999
   130
            Mix_SetError("Unsupported audio format");
admin@999
   131
            return(0);
admin@999
   132
        }
admin@999
   133
        if (!flip) return Mix_UnregisterEffect(channel, f);
admin@999
   134
        return(Mix_RegisterEffect(channel, f, NULL, NULL));
admin@999
   135
    }
admin@999
   136
admin@999
   137
    Mix_SetError("Trying to reverse stereo on a non-stereo stream");
admin@999
   138
    return(0);
admin@999
   139
}
admin@999
   140
admin@999
   141
/* end of effect_stereoreverse.c ... */
admin@999
   142
admin@999
   143
/* vi: set ts=4 sw=4 expandtab: */