Fixed bug #292
authorSam Lantinga <slouken@libsdl.org>
Sun, 24 Sep 2006 15:56:36 +0000
changeset 20423908e1f808e1
parent 2041 4a32d186f35b
child 2043 adf732f1f016
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 00:55:19 2006 +0000
     1.2 +++ b/src/audio/SDL_audiocvt.c	Sun Sep 24 15:56:36 2006 +0000
     1.3 @@ -45,11 +45,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 @@ -64,13 +60,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 if (sample < -128) {
    1.23 -                    *dst = -128;
    1.24 -                } else {
    1.25 -                    *dst = (Sint8) sample;
    1.26 -                }
    1.27 +                *dst = (Sint8) (sample / 2);
    1.28                  src += 2;
    1.29                  dst += 1;
    1.30              }
    1.31 @@ -87,14 +77,10 @@
    1.32                  for (i = cvt->len_cvt / 4; i; --i) {
    1.33                      sample = (Uint16) ((src[0] << 8) | src[1]) +
    1.34                          (Uint16) ((src[2] << 8) | src[3]);
    1.35 -                    if (sample > 65535) {
    1.36 -                        dst[0] = 0xFF;
    1.37 -                        dst[1] = 0xFF;
    1.38 -                    } else {
    1.39 -                        dst[1] = (sample & 0xFF);
    1.40 -                        sample >>= 8;
    1.41 -                        dst[0] = (sample & 0xFF);
    1.42 -                    }
    1.43 +                    sample /= 2;
    1.44 +                    dst[1] = (sample & 0xFF);
    1.45 +                    sample >>= 8;
    1.46 +                    dst[0] = (sample & 0xFF);
    1.47                      src += 4;
    1.48                      dst += 2;
    1.49                  }
    1.50 @@ -102,14 +88,10 @@
    1.51                  for (i = cvt->len_cvt / 4; i; --i) {
    1.52                      sample = (Uint16) ((src[1] << 8) | src[0]) +
    1.53                          (Uint16) ((src[3] << 8) | src[2]);
    1.54 -                    if (sample > 65535) {
    1.55 -                        dst[0] = 0xFF;
    1.56 -                        dst[1] = 0xFF;
    1.57 -                    } else {
    1.58 -                        dst[0] = (sample & 0xFF);
    1.59 -                        sample >>= 8;
    1.60 -                        dst[1] = (sample & 0xFF);
    1.61 -                    }
    1.62 +                    sample /= 2;
    1.63 +                    dst[0] = (sample & 0xFF);
    1.64 +                    sample >>= 8;
    1.65 +                    dst[1] = (sample & 0xFF);
    1.66                      src += 4;
    1.67                      dst += 2;
    1.68                  }
    1.69 @@ -127,17 +109,10 @@
    1.70                  for (i = cvt->len_cvt / 4; i; --i) {
    1.71                      sample = (Sint16) ((src[0] << 8) | src[1]) +
    1.72                          (Sint16) ((src[2] << 8) | src[3]);
    1.73 -                    if (sample > 32767) {
    1.74 -                        dst[0] = 0x7F;
    1.75 -                        dst[1] = 0xFF;
    1.76 -                    } else if (sample < -32768) {
    1.77 -                        dst[0] = 0x80;
    1.78 -                        dst[1] = 0x00;
    1.79 -                    } else {
    1.80 -                        dst[1] = (sample & 0xFF);
    1.81 -                        sample >>= 8;
    1.82 -                        dst[0] = (sample & 0xFF);
    1.83 -                    }
    1.84 +                    sample /= 2;
    1.85 +                    dst[1] = (sample & 0xFF);
    1.86 +                    sample >>= 8;
    1.87 +                    dst[0] = (sample & 0xFF);
    1.88                      src += 4;
    1.89                      dst += 2;
    1.90                  }
    1.91 @@ -145,17 +120,10 @@
    1.92                  for (i = cvt->len_cvt / 4; i; --i) {
    1.93                      sample = (Sint16) ((src[1] << 8) | src[0]) +
    1.94                          (Sint16) ((src[3] << 8) | src[2]);
    1.95 -                    if (sample > 32767) {
    1.96 -                        dst[1] = 0x7F;
    1.97 -                        dst[0] = 0xFF;
    1.98 -                    } else if (sample < -32768) {
    1.99 -                        dst[1] = 0x80;
   1.100 -                        dst[0] = 0x00;
   1.101 -                    } else {
   1.102 -                        dst[0] = (sample & 0xFF);
   1.103 -                        sample >>= 8;
   1.104 -                        dst[1] = (sample & 0xFF);
   1.105 -                    }
   1.106 +                    sample /= 2;
   1.107 +                    dst[0] = (sample & 0xFF);
   1.108 +                    sample >>= 8;
   1.109 +                    dst[1] = (sample & 0xFF);
   1.110                      src += 4;
   1.111                      dst += 2;
   1.112                  }