comparison src/audio/SDL_audiocvt.c @ 2042:3908e1f808e1

Fixed bug #292 I might be on crack here. It looks like SDL_ConvertMono() in src/audio/SDL_audiocvt.c adds the left and right channels of a stereo stream together, and clamps the new mono channel if it would overflow. Shouldn't it be dividing by 2 to average the two sample points instead of clamping? Otherwise the mono sample point's volume doubles in the conversion. This would also make the conversion faster, as it replaces two branches per sample frame with a bitwise shift. --ryan.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 24 Sep 2006 15:56:36 +0000
parents 7abe37467fa5
children 5f6550e5184f
comparison
equal deleted inserted replaced
2041:4a32d186f35b 2042:3908e1f808e1
43 43
44 src = cvt->buf; 44 src = cvt->buf;
45 dst = cvt->buf; 45 dst = cvt->buf;
46 for (i = cvt->len_cvt / 2; i; --i) { 46 for (i = cvt->len_cvt / 2; i; --i) {
47 sample = src[0] + src[1]; 47 sample = src[0] + src[1];
48 if (sample > 255) { 48 *dst = (Uint8) (sample / 2);
49 *dst = 255;
50 } else {
51 *dst = (Uint8) sample;
52 }
53 src += 2; 49 src += 2;
54 dst += 1; 50 dst += 1;
55 } 51 }
56 } 52 }
57 break; 53 break;
62 58
63 src = (Sint8 *) cvt->buf; 59 src = (Sint8 *) cvt->buf;
64 dst = (Sint8 *) cvt->buf; 60 dst = (Sint8 *) cvt->buf;
65 for (i = cvt->len_cvt / 2; i; --i) { 61 for (i = cvt->len_cvt / 2; i; --i) {
66 sample = src[0] + src[1]; 62 sample = src[0] + src[1];
67 if (sample > 127) { 63 *dst = (Sint8) (sample / 2);
68 *dst = 127;
69 } else if (sample < -128) {
70 *dst = -128;
71 } else {
72 *dst = (Sint8) sample;
73 }
74 src += 2; 64 src += 2;
75 dst += 1; 65 dst += 1;
76 } 66 }
77 } 67 }
78 break; 68 break;
85 dst = cvt->buf; 75 dst = cvt->buf;
86 if (SDL_AUDIO_ISBIGENDIAN(format)) { 76 if (SDL_AUDIO_ISBIGENDIAN(format)) {
87 for (i = cvt->len_cvt / 4; i; --i) { 77 for (i = cvt->len_cvt / 4; i; --i) {
88 sample = (Uint16) ((src[0] << 8) | src[1]) + 78 sample = (Uint16) ((src[0] << 8) | src[1]) +
89 (Uint16) ((src[2] << 8) | src[3]); 79 (Uint16) ((src[2] << 8) | src[3]);
90 if (sample > 65535) { 80 sample /= 2;
91 dst[0] = 0xFF; 81 dst[1] = (sample & 0xFF);
92 dst[1] = 0xFF; 82 sample >>= 8;
93 } else { 83 dst[0] = (sample & 0xFF);
94 dst[1] = (sample & 0xFF);
95 sample >>= 8;
96 dst[0] = (sample & 0xFF);
97 }
98 src += 4; 84 src += 4;
99 dst += 2; 85 dst += 2;
100 } 86 }
101 } else { 87 } else {
102 for (i = cvt->len_cvt / 4; i; --i) { 88 for (i = cvt->len_cvt / 4; i; --i) {
103 sample = (Uint16) ((src[1] << 8) | src[0]) + 89 sample = (Uint16) ((src[1] << 8) | src[0]) +
104 (Uint16) ((src[3] << 8) | src[2]); 90 (Uint16) ((src[3] << 8) | src[2]);
105 if (sample > 65535) { 91 sample /= 2;
106 dst[0] = 0xFF; 92 dst[0] = (sample & 0xFF);
107 dst[1] = 0xFF; 93 sample >>= 8;
108 } else { 94 dst[1] = (sample & 0xFF);
109 dst[0] = (sample & 0xFF);
110 sample >>= 8;
111 dst[1] = (sample & 0xFF);
112 }
113 src += 4; 95 src += 4;
114 dst += 2; 96 dst += 2;
115 } 97 }
116 } 98 }
117 } 99 }
125 dst = cvt->buf; 107 dst = cvt->buf;
126 if (SDL_AUDIO_ISBIGENDIAN(format)) { 108 if (SDL_AUDIO_ISBIGENDIAN(format)) {
127 for (i = cvt->len_cvt / 4; i; --i) { 109 for (i = cvt->len_cvt / 4; i; --i) {
128 sample = (Sint16) ((src[0] << 8) | src[1]) + 110 sample = (Sint16) ((src[0] << 8) | src[1]) +
129 (Sint16) ((src[2] << 8) | src[3]); 111 (Sint16) ((src[2] << 8) | src[3]);
130 if (sample > 32767) { 112 sample /= 2;
131 dst[0] = 0x7F; 113 dst[1] = (sample & 0xFF);
132 dst[1] = 0xFF; 114 sample >>= 8;
133 } else if (sample < -32768) { 115 dst[0] = (sample & 0xFF);
134 dst[0] = 0x80;
135 dst[1] = 0x00;
136 } else {
137 dst[1] = (sample & 0xFF);
138 sample >>= 8;
139 dst[0] = (sample & 0xFF);
140 }
141 src += 4; 116 src += 4;
142 dst += 2; 117 dst += 2;
143 } 118 }
144 } else { 119 } else {
145 for (i = cvt->len_cvt / 4; i; --i) { 120 for (i = cvt->len_cvt / 4; i; --i) {
146 sample = (Sint16) ((src[1] << 8) | src[0]) + 121 sample = (Sint16) ((src[1] << 8) | src[0]) +
147 (Sint16) ((src[3] << 8) | src[2]); 122 (Sint16) ((src[3] << 8) | src[2]);
148 if (sample > 32767) { 123 sample /= 2;
149 dst[1] = 0x7F; 124 dst[0] = (sample & 0xFF);
150 dst[0] = 0xFF; 125 sample >>= 8;
151 } else if (sample < -32768) { 126 dst[1] = (sample & 0xFF);
152 dst[1] = 0x80;
153 dst[0] = 0x00;
154 } else {
155 dst[0] = (sample & 0xFF);
156 sample >>= 8;
157 dst[1] = (sample & 0xFF);
158 }
159 src += 4; 127 src += 4;
160 dst += 2; 128 dst += 2;
161 } 129 }
162 } 130 }
163 } 131 }