Fixed bug #292 SDL-1.2
authorSam Lantinga <slouken@libsdl.org>
Sun, 24 Sep 2006 15:45:37 +0000
branchSDL-1.2
changeset 3880 74e828c64315
parent 3879 c82c1870c77a
child 3881 c1b6bb5205f1
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.
src/audio/SDL_audiocvt.c
--- a/src/audio/SDL_audiocvt.c	Sun Sep 24 15:36:37 2006 +0000
+++ b/src/audio/SDL_audiocvt.c	Sun Sep 24 15:45:37 2006 +0000
@@ -44,11 +44,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;
 			}
@@ -62,14 +58,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;
 			}
@@ -85,14 +74,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;
 				}
@@ -100,14 +85,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;
 				}
@@ -124,18 +105,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;
 				}
@@ -143,18 +116,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;
 				}