Fixed broken audio conversions between float and unsigned datatypes.
authorRyan C. Gordon <icculus@icculus.org>
Fri, 01 Sep 2006 18:07:41 +0000
changeset 2011b2b7154ce016
parent 2010 39897da56f63
child 2012 575d5c9d4db8
Fixed broken audio conversions between float and unsigned datatypes.
src/audio/SDL_audiotypecvt.c
src/audio/sdlgenaudiocvt.pl
     1.1 --- a/src/audio/SDL_audiotypecvt.c	Fri Sep 01 18:04:01 2006 +0000
     1.2 +++ b/src/audio/SDL_audiotypecvt.c	Fri Sep 01 18:07:41 2006 +0000
     1.3 @@ -28,9 +28,7 @@
     1.4  /* *INDENT-OFF* */
     1.5  
     1.6  #define DIVBY127 0.0078740157480315f
     1.7 -#define DIVBY255 0.00392156862745098f
     1.8  #define DIVBY32767 3.05185094759972e-05f
     1.9 -#define DIVBY65535 1.52590218966964e-05f
    1.10  #define DIVBY2147483647 4.6566128752458e-10f
    1.11  
    1.12  static void SDLCALL
    1.13 @@ -221,7 +219,7 @@
    1.14      src = (const Uint8 *) (cvt->buf + cvt->len_cvt);
    1.15      dst = (float *) (cvt->buf + cvt->len_cvt * 4);
    1.16      for (i = cvt->len_cvt / sizeof (Uint8); i; --i, --src, --dst) {
    1.17 -        const float val = (((float) *src) * DIVBY255);
    1.18 +        const float val = ((((float) *src) * DIVBY127) - 1.0f);
    1.19          *dst = SDL_SwapFloatLE(val);
    1.20      }
    1.21  
    1.22 @@ -246,7 +244,7 @@
    1.23      src = (const Uint8 *) (cvt->buf + cvt->len_cvt);
    1.24      dst = (float *) (cvt->buf + cvt->len_cvt * 4);
    1.25      for (i = cvt->len_cvt / sizeof (Uint8); i; --i, --src, --dst) {
    1.26 -        const float val = (((float) *src) * DIVBY255);
    1.27 +        const float val = ((((float) *src) * DIVBY127) - 1.0f);
    1.28          *dst = SDL_SwapFloatBE(val);
    1.29      }
    1.30  
    1.31 @@ -667,7 +665,7 @@
    1.32      src = (const Uint16 *) (cvt->buf + cvt->len_cvt);
    1.33      dst = (float *) (cvt->buf + cvt->len_cvt * 2);
    1.34      for (i = cvt->len_cvt / sizeof (Uint16); i; --i, --src, --dst) {
    1.35 -        const float val = (((float) SDL_SwapLE16(*src)) * DIVBY65535);
    1.36 +        const float val = ((((float) SDL_SwapLE16(*src)) * DIVBY32767) - 1.0f);
    1.37          *dst = SDL_SwapFloatLE(val);
    1.38      }
    1.39  
    1.40 @@ -692,7 +690,7 @@
    1.41      src = (const Uint16 *) (cvt->buf + cvt->len_cvt);
    1.42      dst = (float *) (cvt->buf + cvt->len_cvt * 2);
    1.43      for (i = cvt->len_cvt / sizeof (Uint16); i; --i, --src, --dst) {
    1.44 -        const float val = (((float) SDL_SwapLE16(*src)) * DIVBY65535);
    1.45 +        const float val = ((((float) SDL_SwapLE16(*src)) * DIVBY32767) - 1.0f);
    1.46          *dst = SDL_SwapFloatBE(val);
    1.47      }
    1.48  
    1.49 @@ -1111,7 +1109,7 @@
    1.50      src = (const Uint16 *) (cvt->buf + cvt->len_cvt);
    1.51      dst = (float *) (cvt->buf + cvt->len_cvt * 2);
    1.52      for (i = cvt->len_cvt / sizeof (Uint16); i; --i, --src, --dst) {
    1.53 -        const float val = (((float) SDL_SwapBE16(*src)) * DIVBY65535);
    1.54 +        const float val = ((((float) SDL_SwapBE16(*src)) * DIVBY32767) - 1.0f);
    1.55          *dst = SDL_SwapFloatLE(val);
    1.56      }
    1.57  
    1.58 @@ -1136,7 +1134,7 @@
    1.59      src = (const Uint16 *) (cvt->buf + cvt->len_cvt);
    1.60      dst = (float *) (cvt->buf + cvt->len_cvt * 2);
    1.61      for (i = cvt->len_cvt / sizeof (Uint16); i; --i, --src, --dst) {
    1.62 -        const float val = (((float) SDL_SwapBE16(*src)) * DIVBY65535);
    1.63 +        const float val = ((((float) SDL_SwapBE16(*src)) * DIVBY32767) - 1.0f);
    1.64          *dst = SDL_SwapFloatBE(val);
    1.65      }
    1.66  
    1.67 @@ -1827,7 +1825,7 @@
    1.68      src = (const float *) cvt->buf;
    1.69      dst = (Uint8 *) cvt->buf;
    1.70      for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
    1.71 -        const Uint8 val = ((Uint8) (SDL_SwapFloatLE(*src) * 255.0f));
    1.72 +        const Uint8 val = ((Uint8) ((SDL_SwapFloatLE(*src) + 1.0f) * 127.0f));
    1.73          *dst = val;
    1.74      }
    1.75  
    1.76 @@ -1877,7 +1875,7 @@
    1.77      src = (const float *) cvt->buf;
    1.78      dst = (Uint16 *) cvt->buf;
    1.79      for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
    1.80 -        const Uint16 val = ((Uint16) (SDL_SwapFloatLE(*src) * 65535.0f));
    1.81 +        const Uint16 val = ((Uint16) ((SDL_SwapFloatLE(*src) + 1.0f) * 32767.0f));
    1.82          *dst = SDL_SwapLE16(val);
    1.83      }
    1.84  
    1.85 @@ -1927,7 +1925,7 @@
    1.86      src = (const float *) cvt->buf;
    1.87      dst = (Uint16 *) cvt->buf;
    1.88      for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
    1.89 -        const Uint16 val = ((Uint16) (SDL_SwapFloatLE(*src) * 65535.0f));
    1.90 +        const Uint16 val = ((Uint16) ((SDL_SwapFloatLE(*src) + 1.0f) * 32767.0f));
    1.91          *dst = SDL_SwapBE16(val);
    1.92      }
    1.93  
    1.94 @@ -2049,7 +2047,7 @@
    1.95      src = (const float *) cvt->buf;
    1.96      dst = (Uint8 *) cvt->buf;
    1.97      for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
    1.98 -        const Uint8 val = ((Uint8) (SDL_SwapFloatBE(*src) * 255.0f));
    1.99 +        const Uint8 val = ((Uint8) ((SDL_SwapFloatBE(*src) + 1.0f) * 127.0f));
   1.100          *dst = val;
   1.101      }
   1.102  
   1.103 @@ -2099,7 +2097,7 @@
   1.104      src = (const float *) cvt->buf;
   1.105      dst = (Uint16 *) cvt->buf;
   1.106      for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
   1.107 -        const Uint16 val = ((Uint16) (SDL_SwapFloatBE(*src) * 65535.0f));
   1.108 +        const Uint16 val = ((Uint16) ((SDL_SwapFloatBE(*src) + 1.0f) * 32767.0f));
   1.109          *dst = SDL_SwapLE16(val);
   1.110      }
   1.111  
   1.112 @@ -2149,7 +2147,7 @@
   1.113      src = (const float *) cvt->buf;
   1.114      dst = (Uint16 *) cvt->buf;
   1.115      for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
   1.116 -        const Uint16 val = ((Uint16) (SDL_SwapFloatBE(*src) * 65535.0f));
   1.117 +        const Uint16 val = ((Uint16) ((SDL_SwapFloatBE(*src) + 1.0f) * 32767.0f));
   1.118          *dst = SDL_SwapBE16(val);
   1.119      }
   1.120  
     2.1 --- a/src/audio/sdlgenaudiocvt.pl	Fri Sep 01 18:04:01 2006 +0000
     2.2 +++ b/src/audio/sdlgenaudiocvt.pl	Fri Sep 01 18:07:41 2006 +0000
     2.3 @@ -54,7 +54,7 @@
     2.4  
     2.5  EOF
     2.6  
     2.7 -    my @vals = ( 127, 255, 32767, 65535, 2147483647 );
     2.8 +    my @vals = ( 127, 32767, 2147483647 );
     2.9      foreach (@vals) {
    2.10          my $val = $_;
    2.11          my $fval = 1.0 / $val;
    2.12 @@ -113,38 +113,28 @@
    2.13  
    2.14  
    2.15  sub maxIntVal {
    2.16 -    my ($signed, $size) = @_;
    2.17 -    if ($signed) {
    2.18 -        if ($size == 8) {
    2.19 -            return 0x7F;
    2.20 -        } elsif ($size == 16) {
    2.21 -            return 0x7FFF;
    2.22 -        } elsif ($size == 32) {
    2.23 -            return 0x7FFFFFFF;
    2.24 -        }
    2.25 -    } else {
    2.26 -        if ($size == 8) {
    2.27 -            return 0xFF;
    2.28 -        } elsif ($size == 16) {
    2.29 -            return 0xFFFF;
    2.30 -        } elsif ($size == 32) {
    2.31 -            return 0xFFFFFFFF;
    2.32 -        }
    2.33 +    my $size = shift;
    2.34 +    if ($size == 8) {
    2.35 +        return 0x7F;
    2.36 +    } elsif ($size == 16) {
    2.37 +        return 0x7FFF;
    2.38 +    } elsif ($size == 32) {
    2.39 +        return 0x7FFFFFFF;
    2.40      }
    2.41  
    2.42      die("bug in script.\n");
    2.43  }
    2.44  
    2.45  sub getFloatToIntMult {
    2.46 -    my ($signed, $size) = @_;
    2.47 -    my $val = maxIntVal($signed, $size) . '.0';
    2.48 +    my $size = shift;
    2.49 +    my $val = maxIntVal($size) . '.0';
    2.50      $val .= 'f' if ($size < 32);
    2.51      return $val;
    2.52  }
    2.53  
    2.54  sub getIntToFloatDivBy {
    2.55 -    my ($signed, $size) = @_;
    2.56 -    return 'DIVBY' . maxIntVal($signed, $size);
    2.57 +    my $size = shift;
    2.58 +    return 'DIVBY' . maxIntVal($size);
    2.59  }
    2.60  
    2.61  sub getSignFlipVal {
    2.62 @@ -215,13 +205,19 @@
    2.63          my $code = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, '*src');
    2.64          if ($ffloat != $tfloat) {
    2.65              if ($ffloat) {
    2.66 -                my $mult = getFloatToIntMult($tsigned, $tsize);
    2.67 +                my $mult = getFloatToIntMult($tsize);
    2.68 +                if (!$tsigned) {   # bump from -1.0f/1.0f to 0.0f/2.0f
    2.69 +                    $code = "($code + 1.0f)";
    2.70 +                }
    2.71                  $code = "(($tctype) ($code * $mult))";
    2.72              } else {
    2.73                  # $divby will be the reciprocal, to avoid pipeline stalls
    2.74                  #  from floating point division...so multiply it.
    2.75 -                my $divby = getIntToFloatDivBy($fsigned, $fsize);
    2.76 +                my $divby = getIntToFloatDivBy($fsize);
    2.77                  $code = "(((float) $code) * $divby)";
    2.78 +                if (!$fsigned) {   # bump from 0.0f/2.0f to -1.0f/1.0f.
    2.79 +                    $code = "($code - 1.0f)";
    2.80 +                }
    2.81              }
    2.82          } else {
    2.83              # All integer conversions here.