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.
     1 /*
     2   SDL_mixer:  An audio mixer library based on the SDL library
     3   Copyright (C) 1997-2019 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 #include <stdio.h>
    28 #include <stdlib.h>
    29 
    30 #include "SDL.h"
    31 #include "SDL_mixer.h"
    32 
    33 #define MIX_INTERNAL_EFFECT__
    34 #include "effects_internal.h"
    35 
    36 /* profile code:
    37     #include <sys/time.h>
    38     #include <unistd.h>
    39     struct timeval tv1;
    40     struct timeval tv2;
    41 
    42     gettimeofday(&tv1, NULL);
    43 
    44         ... do your thing here ...
    45 
    46     gettimeofday(&tv2, NULL);
    47     printf("%ld\n", tv2.tv_usec - tv1.tv_usec);
    48 */
    49 
    50 
    51 /*
    52  * Stereo reversal effect...this one's pretty straightforward...
    53  */
    54 
    55 static void SDLCALL _Eff_reversestereo32(int chan, void *stream, int len, void *udata)
    56 {
    57     /* 16 bits * 2 channels. */
    58     Uint32 *ptr = (Uint32 *) stream;
    59     Uint32 tmp;
    60     int i;
    61 
    62     (void)chan;
    63     (void)udata;
    64 
    65     for (i = 0; i < len; i += 2 * sizeof (Uint32), ptr += 2) {
    66         tmp = ptr[0];
    67         ptr[0] = ptr[1];
    68         ptr[1] = tmp;
    69     }
    70 }
    71 
    72 static void SDLCALL _Eff_reversestereo16(int chan, void *stream, int len, void *udata)
    73 {
    74     /* 16 bits * 2 channels. */
    75     Uint32 *ptr = (Uint32 *) stream;
    76     int i;
    77 
    78     (void)chan;
    79     (void)udata;
    80 
    81     for (i = 0; i < len; i += sizeof (Uint32), ptr++) {
    82         *ptr = (((*ptr) & 0xFFFF0000) >> 16) | (((*ptr) & 0x0000FFFF) << 16);
    83     }
    84 }
    85 
    86 static void SDLCALL _Eff_reversestereo8(int chan, void *stream, int len, void *udata)
    87 {
    88     /* 8 bits * 2 channels. */
    89     Uint32 *ptr = (Uint32 *) stream;
    90     int i;
    91 
    92     (void)chan;
    93     (void)udata;
    94 
    95     /* get the last two bytes if len is not divisible by four... */
    96     if (len % (int)sizeof(Uint32) != 0) {
    97         Uint16 *p = (Uint16 *) (((Uint8 *) stream) + (len - 2));
    98         *p = (Uint16)((((*p) & 0xFF00) >> 8) | (((*ptr) & 0x00FF) << 8));
    99         len -= 2;
   100     }
   101 
   102     for (i = 0; i < len; i += sizeof (Uint32), ptr++) {
   103         *ptr = (((*ptr) & 0x0000FF00) >> 8) | (((*ptr) & 0x000000FF) << 8) |
   104                (((*ptr) & 0xFF000000) >> 8) | (((*ptr) & 0x00FF0000) << 8);
   105     }
   106 }
   107 
   108 
   109 int Mix_SetReverseStereo(int channel, int flip)
   110 {
   111     Mix_EffectFunc_t f = NULL;
   112     int channels;
   113     Uint16 format;
   114 
   115     Mix_QuerySpec(NULL, &format, &channels);
   116 
   117     if (channels == 2) {
   118         int bits = (format & 0xFF);
   119         switch (bits) {
   120         case 8:
   121             f = _Eff_reversestereo8;
   122             break;
   123         case 16:
   124             f = _Eff_reversestereo16;
   125             break;
   126         case 32:
   127             f = _Eff_reversestereo32;
   128             break;
   129         default:
   130             Mix_SetError("Unsupported audio format");
   131             return(0);
   132         }
   133         if (!flip) return Mix_UnregisterEffect(channel, f);
   134         return(Mix_RegisterEffect(channel, f, NULL, NULL));
   135     }
   136 
   137     Mix_SetError("Trying to reverse stereo on a non-stereo stream");
   138     return(0);
   139 }
   140 
   141 /* end of effect_stereoreverse.c ... */
   142 
   143 /* vi: set ts=4 sw=4 expandtab: */