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