Fixed bug #292 SDL-1.2
authorSam Lantinga <slouken@libsdl.org>
Sun, 24 Sep 2006 15:45:37 +0000
branchSDL-1.2
changeset 388074e828c64315
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
     1.1 --- a/src/audio/SDL_audiocvt.c	Sun Sep 24 15:36:37 2006 +0000
     1.2 +++ b/src/audio/SDL_audiocvt.c	Sun Sep 24 15:45:37 2006 +0000
     1.3 @@ -44,11 +44,7 @@
     1.4  			dst = cvt->buf;
     1.5  			for ( i=cvt->len_cvt/2; i; --i ) {
     1.6  				sample = src[0] + src[1];
     1.7 -				if ( sample > 255 ) {
     1.8 -					*dst = 255;
     1.9 -				} else {
    1.10 -					*dst = (Uint8)sample;
    1.11 -				}
    1.12 +				*dst = (Uint8)(sample / 2);
    1.13  				src += 2;
    1.14  				dst += 1;
    1.15  			}
    1.16 @@ -62,14 +58,7 @@
    1.17  			dst = (Sint8 *)cvt->buf;
    1.18  			for ( i=cvt->len_cvt/2; i; --i ) {
    1.19  				sample = src[0] + src[1];
    1.20 -				if ( sample > 127 ) {
    1.21 -					*dst = 127;
    1.22 -				} else
    1.23 -				if ( sample < -128 ) {
    1.24 -					*dst = -128;
    1.25 -				} else {
    1.26 -					*dst = (Sint8)sample;
    1.27 -				}
    1.28 +				*dst = (Sint8)(sample / 2);
    1.29  				src += 2;
    1.30  				dst += 1;
    1.31  			}
    1.32 @@ -85,14 +74,10 @@
    1.33  				for ( i=cvt->len_cvt/4; i; --i ) {
    1.34  					sample = (Uint16)((src[0]<<8)|src[1])+
    1.35  					         (Uint16)((src[2]<<8)|src[3]);
    1.36 -					if ( sample > 65535 ) {
    1.37 -						dst[0] = 0xFF;
    1.38 -						dst[1] = 0xFF;
    1.39 -					} else {
    1.40 -						dst[1] = (sample&0xFF);
    1.41 -						sample >>= 8;
    1.42 -						dst[0] = (sample&0xFF);
    1.43 -					}
    1.44 +					sample /= 2;
    1.45 +					dst[1] = (sample&0xFF);
    1.46 +					sample >>= 8;
    1.47 +					dst[0] = (sample&0xFF);
    1.48  					src += 4;
    1.49  					dst += 2;
    1.50  				}
    1.51 @@ -100,14 +85,10 @@
    1.52  				for ( i=cvt->len_cvt/4; i; --i ) {
    1.53  					sample = (Uint16)((src[1]<<8)|src[0])+
    1.54  					         (Uint16)((src[3]<<8)|src[2]);
    1.55 -					if ( sample > 65535 ) {
    1.56 -						dst[0] = 0xFF;
    1.57 -						dst[1] = 0xFF;
    1.58 -					} else {
    1.59 -						dst[0] = (sample&0xFF);
    1.60 -						sample >>= 8;
    1.61 -						dst[1] = (sample&0xFF);
    1.62 -					}
    1.63 +					sample /= 2;
    1.64 +					dst[0] = (sample&0xFF);
    1.65 +					sample >>= 8;
    1.66 +					dst[1] = (sample&0xFF);
    1.67  					src += 4;
    1.68  					dst += 2;
    1.69  				}
    1.70 @@ -124,18 +105,10 @@
    1.71  				for ( i=cvt->len_cvt/4; i; --i ) {
    1.72  					sample = (Sint16)((src[0]<<8)|src[1])+
    1.73  					         (Sint16)((src[2]<<8)|src[3]);
    1.74 -					if ( sample > 32767 ) {
    1.75 -						dst[0] = 0x7F;
    1.76 -						dst[1] = 0xFF;
    1.77 -					} else
    1.78 -					if ( sample < -32768 ) {
    1.79 -						dst[0] = 0x80;
    1.80 -						dst[1] = 0x00;
    1.81 -					} else {
    1.82 -						dst[1] = (sample&0xFF);
    1.83 -						sample >>= 8;
    1.84 -						dst[0] = (sample&0xFF);
    1.85 -					}
    1.86 +					sample /= 2;
    1.87 +					dst[1] = (sample&0xFF);
    1.88 +					sample >>= 8;
    1.89 +					dst[0] = (sample&0xFF);
    1.90  					src += 4;
    1.91  					dst += 2;
    1.92  				}
    1.93 @@ -143,18 +116,10 @@
    1.94  				for ( i=cvt->len_cvt/4; i; --i ) {
    1.95  					sample = (Sint16)((src[1]<<8)|src[0])+
    1.96  					         (Sint16)((src[3]<<8)|src[2]);
    1.97 -					if ( sample > 32767 ) {
    1.98 -						dst[1] = 0x7F;
    1.99 -						dst[0] = 0xFF;
   1.100 -					} else
   1.101 -					if ( sample < -32768 ) {
   1.102 -						dst[1] = 0x80;
   1.103 -						dst[0] = 0x00;
   1.104 -					} else {
   1.105 -						dst[0] = (sample&0xFF);
   1.106 -						sample >>= 8;
   1.107 -						dst[1] = (sample&0xFF);
   1.108 -					}
   1.109 +					sample /= 2;
   1.110 +					dst[0] = (sample&0xFF);
   1.111 +					sample >>= 8;
   1.112 +					dst[1] = (sample&0xFF);
   1.113  					src += 4;
   1.114  					dst += 2;
   1.115  				}