diff 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
line wrap: on
line diff
--- a/src/audio/SDL_audiocvt.c	Sun Sep 24 00:55:19 2006 +0000
+++ b/src/audio/SDL_audiocvt.c	Sun Sep 24 15:56:36 2006 +0000
@@ -45,11 +45,7 @@
             dst = cvt->buf;
             for (i = cvt->len_cvt / 2; i; --i) {
                 sample = src[0] + src[1];
-                if (sample > 255) {
-                    *dst = 255;
-                } else {
-                    *dst = (Uint8) sample;
-                }
+                *dst = (Uint8) (sample / 2);
                 src += 2;
                 dst += 1;
             }
@@ -64,13 +60,7 @@
             dst = (Sint8 *) cvt->buf;
             for (i = cvt->len_cvt / 2; i; --i) {
                 sample = src[0] + src[1];
-                if (sample > 127) {
-                    *dst = 127;
-                } else if (sample < -128) {
-                    *dst = -128;
-                } else {
-                    *dst = (Sint8) sample;
-                }
+                *dst = (Sint8) (sample / 2);
                 src += 2;
                 dst += 1;
             }
@@ -87,14 +77,10 @@
                 for (i = cvt->len_cvt / 4; i; --i) {
                     sample = (Uint16) ((src[0] << 8) | src[1]) +
                         (Uint16) ((src[2] << 8) | src[3]);
-                    if (sample > 65535) {
-                        dst[0] = 0xFF;
-                        dst[1] = 0xFF;
-                    } else {
-                        dst[1] = (sample & 0xFF);
-                        sample >>= 8;
-                        dst[0] = (sample & 0xFF);
-                    }
+                    sample /= 2;
+                    dst[1] = (sample & 0xFF);
+                    sample >>= 8;
+                    dst[0] = (sample & 0xFF);
                     src += 4;
                     dst += 2;
                 }
@@ -102,14 +88,10 @@
                 for (i = cvt->len_cvt / 4; i; --i) {
                     sample = (Uint16) ((src[1] << 8) | src[0]) +
                         (Uint16) ((src[3] << 8) | src[2]);
-                    if (sample > 65535) {
-                        dst[0] = 0xFF;
-                        dst[1] = 0xFF;
-                    } else {
-                        dst[0] = (sample & 0xFF);
-                        sample >>= 8;
-                        dst[1] = (sample & 0xFF);
-                    }
+                    sample /= 2;
+                    dst[0] = (sample & 0xFF);
+                    sample >>= 8;
+                    dst[1] = (sample & 0xFF);
                     src += 4;
                     dst += 2;
                 }
@@ -127,17 +109,10 @@
                 for (i = cvt->len_cvt / 4; i; --i) {
                     sample = (Sint16) ((src[0] << 8) | src[1]) +
                         (Sint16) ((src[2] << 8) | src[3]);
-                    if (sample > 32767) {
-                        dst[0] = 0x7F;
-                        dst[1] = 0xFF;
-                    } else if (sample < -32768) {
-                        dst[0] = 0x80;
-                        dst[1] = 0x00;
-                    } else {
-                        dst[1] = (sample & 0xFF);
-                        sample >>= 8;
-                        dst[0] = (sample & 0xFF);
-                    }
+                    sample /= 2;
+                    dst[1] = (sample & 0xFF);
+                    sample >>= 8;
+                    dst[0] = (sample & 0xFF);
                     src += 4;
                     dst += 2;
                 }
@@ -145,17 +120,10 @@
                 for (i = cvt->len_cvt / 4; i; --i) {
                     sample = (Sint16) ((src[1] << 8) | src[0]) +
                         (Sint16) ((src[3] << 8) | src[2]);
-                    if (sample > 32767) {
-                        dst[1] = 0x7F;
-                        dst[0] = 0xFF;
-                    } else if (sample < -32768) {
-                        dst[1] = 0x80;
-                        dst[0] = 0x00;
-                    } else {
-                        dst[0] = (sample & 0xFF);
-                        sample >>= 8;
-                        dst[1] = (sample & 0xFF);
-                    }
+                    sample /= 2;
+                    dst[0] = (sample & 0xFF);
+                    sample >>= 8;
+                    dst[1] = (sample & 0xFF);
                     src += 4;
                     dst += 2;
                 }