1 /*
2 SDL_mixer: An audio mixer library based on the SDL library
3 Copyright (C) 1997-2018 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
_Eff_reversestereo32(int chan,void * stream,int len,void * udata)58 static void SDLCALL _Eff_reversestereo32(int chan, void *stream, int len, void *udata)
59 {
60 /* 16 bits * 2 channels. */
61 Uint32 *ptr = (Uint32 *) stream;
62 Uint32 tmp;
63 int i;
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
_Eff_reversestereo16(int chan,void * stream,int len,void * udata)73 static void SDLCALL _Eff_reversestereo16(int chan, void *stream, int len, void *udata)
74 {
75 /* 16 bits * 2 channels. */
76 Uint32 *ptr = (Uint32 *) stream;
77 int i;
78
79 for (i = 0; i < len; i += sizeof (Uint32), ptr++) {
80 *ptr = (((*ptr) & 0xFFFF0000) >> 16) | (((*ptr) & 0x0000FFFF) << 16);
81 }
82 }
83
84
_Eff_reversestereo8(int chan,void * stream,int len,void * udata)85 static void SDLCALL _Eff_reversestereo8(int chan, void *stream, int len, void *udata)
86 {
87 /* 8 bits * 2 channels. */
88 Uint32 *ptr = (Uint32 *) stream;
89 int i;
90
91 /* get the last two bytes if len is not divisible by four... */
92 if (len % sizeof (Uint32) != 0) {
93 Uint16 *p = (Uint16 *) (((Uint8 *) stream) + (len - 2));
94 *p = (Uint16)((((*p) & 0xFF00) >> 8) | (((*ptr) & 0x00FF) << 8));
95 len -= 2;
96 }
97
98 for (i = 0; i < len; i += sizeof (Uint32), ptr++) {
99 *ptr = (((*ptr) & 0x0000FF00) >> 8) | (((*ptr) & 0x000000FF) << 8) |
100 (((*ptr) & 0xFF000000) >> 8) | (((*ptr) & 0x00FF0000) << 8);
101 }
102 }
103
104
Mix_SetReverseStereo(int channel,int flip)105 int Mix_SetReverseStereo(int channel, int flip)
106 {
107 Mix_EffectFunc_t f = NULL;
108 int channels;
109 Uint16 format;
110
111 Mix_QuerySpec(NULL, &format, &channels);
112
113 if (channels == 2) {
114 int bits = (format & 0xFF);
115 switch (bits) {
116 case 8:
117 f = _Eff_reversestereo8;
118 break;
119 case 16:
120 f = _Eff_reversestereo16;
121 break;
122 case 32:
123 f = _Eff_reversestereo32;
124 break;
125 default:
126 Mix_SetError("Unsupported audio format");
127 return(0);
128 }
129
130 if (!flip) {
131 return(Mix_UnregisterEffect(channel, f));
132 } else {
133 return(Mix_RegisterEffect(channel, f, NULL, NULL));
134 }
135 } else {
136 Mix_SetError("Trying to reverse stereo on a non-stereo stream");
137 return(0);
138 }
139
140 return(1);
141 }
142
143
144 /* end of effect_stereoreverse.c ... */
145
146 /* vi: set ts=4 sw=4 expandtab: */
147