changeset 2011:b2b7154ce016

Fixed broken audio conversions between float and unsigned datatypes.
author Ryan C. Gordon <icculus@icculus.org>
date Fri, 01 Sep 2006 18:07:41 +0000
parents 39897da56f63
children 575d5c9d4db8
files src/audio/SDL_audiotypecvt.c src/audio/sdlgenaudiocvt.pl
diffstat 2 files changed, 32 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/src/audio/SDL_audiotypecvt.c	Fri Sep 01 18:04:01 2006 +0000
+++ b/src/audio/SDL_audiotypecvt.c	Fri Sep 01 18:07:41 2006 +0000
@@ -28,9 +28,7 @@
 /* *INDENT-OFF* */
 
 #define DIVBY127 0.0078740157480315f
-#define DIVBY255 0.00392156862745098f
 #define DIVBY32767 3.05185094759972e-05f
-#define DIVBY65535 1.52590218966964e-05f
 #define DIVBY2147483647 4.6566128752458e-10f
 
 static void SDLCALL
@@ -221,7 +219,7 @@
     src = (const Uint8 *) (cvt->buf + cvt->len_cvt);
     dst = (float *) (cvt->buf + cvt->len_cvt * 4);
     for (i = cvt->len_cvt / sizeof (Uint8); i; --i, --src, --dst) {
-        const float val = (((float) *src) * DIVBY255);
+        const float val = ((((float) *src) * DIVBY127) - 1.0f);
         *dst = SDL_SwapFloatLE(val);
     }
 
@@ -246,7 +244,7 @@
     src = (const Uint8 *) (cvt->buf + cvt->len_cvt);
     dst = (float *) (cvt->buf + cvt->len_cvt * 4);
     for (i = cvt->len_cvt / sizeof (Uint8); i; --i, --src, --dst) {
-        const float val = (((float) *src) * DIVBY255);
+        const float val = ((((float) *src) * DIVBY127) - 1.0f);
         *dst = SDL_SwapFloatBE(val);
     }
 
@@ -667,7 +665,7 @@
     src = (const Uint16 *) (cvt->buf + cvt->len_cvt);
     dst = (float *) (cvt->buf + cvt->len_cvt * 2);
     for (i = cvt->len_cvt / sizeof (Uint16); i; --i, --src, --dst) {
-        const float val = (((float) SDL_SwapLE16(*src)) * DIVBY65535);
+        const float val = ((((float) SDL_SwapLE16(*src)) * DIVBY32767) - 1.0f);
         *dst = SDL_SwapFloatLE(val);
     }
 
@@ -692,7 +690,7 @@
     src = (const Uint16 *) (cvt->buf + cvt->len_cvt);
     dst = (float *) (cvt->buf + cvt->len_cvt * 2);
     for (i = cvt->len_cvt / sizeof (Uint16); i; --i, --src, --dst) {
-        const float val = (((float) SDL_SwapLE16(*src)) * DIVBY65535);
+        const float val = ((((float) SDL_SwapLE16(*src)) * DIVBY32767) - 1.0f);
         *dst = SDL_SwapFloatBE(val);
     }
 
@@ -1111,7 +1109,7 @@
     src = (const Uint16 *) (cvt->buf + cvt->len_cvt);
     dst = (float *) (cvt->buf + cvt->len_cvt * 2);
     for (i = cvt->len_cvt / sizeof (Uint16); i; --i, --src, --dst) {
-        const float val = (((float) SDL_SwapBE16(*src)) * DIVBY65535);
+        const float val = ((((float) SDL_SwapBE16(*src)) * DIVBY32767) - 1.0f);
         *dst = SDL_SwapFloatLE(val);
     }
 
@@ -1136,7 +1134,7 @@
     src = (const Uint16 *) (cvt->buf + cvt->len_cvt);
     dst = (float *) (cvt->buf + cvt->len_cvt * 2);
     for (i = cvt->len_cvt / sizeof (Uint16); i; --i, --src, --dst) {
-        const float val = (((float) SDL_SwapBE16(*src)) * DIVBY65535);
+        const float val = ((((float) SDL_SwapBE16(*src)) * DIVBY32767) - 1.0f);
         *dst = SDL_SwapFloatBE(val);
     }
 
@@ -1827,7 +1825,7 @@
     src = (const float *) cvt->buf;
     dst = (Uint8 *) cvt->buf;
     for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
-        const Uint8 val = ((Uint8) (SDL_SwapFloatLE(*src) * 255.0f));
+        const Uint8 val = ((Uint8) ((SDL_SwapFloatLE(*src) + 1.0f) * 127.0f));
         *dst = val;
     }
 
@@ -1877,7 +1875,7 @@
     src = (const float *) cvt->buf;
     dst = (Uint16 *) cvt->buf;
     for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
-        const Uint16 val = ((Uint16) (SDL_SwapFloatLE(*src) * 65535.0f));
+        const Uint16 val = ((Uint16) ((SDL_SwapFloatLE(*src) + 1.0f) * 32767.0f));
         *dst = SDL_SwapLE16(val);
     }
 
@@ -1927,7 +1925,7 @@
     src = (const float *) cvt->buf;
     dst = (Uint16 *) cvt->buf;
     for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
-        const Uint16 val = ((Uint16) (SDL_SwapFloatLE(*src) * 65535.0f));
+        const Uint16 val = ((Uint16) ((SDL_SwapFloatLE(*src) + 1.0f) * 32767.0f));
         *dst = SDL_SwapBE16(val);
     }
 
@@ -2049,7 +2047,7 @@
     src = (const float *) cvt->buf;
     dst = (Uint8 *) cvt->buf;
     for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
-        const Uint8 val = ((Uint8) (SDL_SwapFloatBE(*src) * 255.0f));
+        const Uint8 val = ((Uint8) ((SDL_SwapFloatBE(*src) + 1.0f) * 127.0f));
         *dst = val;
     }
 
@@ -2099,7 +2097,7 @@
     src = (const float *) cvt->buf;
     dst = (Uint16 *) cvt->buf;
     for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
-        const Uint16 val = ((Uint16) (SDL_SwapFloatBE(*src) * 65535.0f));
+        const Uint16 val = ((Uint16) ((SDL_SwapFloatBE(*src) + 1.0f) * 32767.0f));
         *dst = SDL_SwapLE16(val);
     }
 
@@ -2149,7 +2147,7 @@
     src = (const float *) cvt->buf;
     dst = (Uint16 *) cvt->buf;
     for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
-        const Uint16 val = ((Uint16) (SDL_SwapFloatBE(*src) * 65535.0f));
+        const Uint16 val = ((Uint16) ((SDL_SwapFloatBE(*src) + 1.0f) * 32767.0f));
         *dst = SDL_SwapBE16(val);
     }
 
--- a/src/audio/sdlgenaudiocvt.pl	Fri Sep 01 18:04:01 2006 +0000
+++ b/src/audio/sdlgenaudiocvt.pl	Fri Sep 01 18:07:41 2006 +0000
@@ -54,7 +54,7 @@
 
 EOF
 
-    my @vals = ( 127, 255, 32767, 65535, 2147483647 );
+    my @vals = ( 127, 32767, 2147483647 );
     foreach (@vals) {
         my $val = $_;
         my $fval = 1.0 / $val;
@@ -113,38 +113,28 @@
 
 
 sub maxIntVal {
-    my ($signed, $size) = @_;
-    if ($signed) {
-        if ($size == 8) {
-            return 0x7F;
-        } elsif ($size == 16) {
-            return 0x7FFF;
-        } elsif ($size == 32) {
-            return 0x7FFFFFFF;
-        }
-    } else {
-        if ($size == 8) {
-            return 0xFF;
-        } elsif ($size == 16) {
-            return 0xFFFF;
-        } elsif ($size == 32) {
-            return 0xFFFFFFFF;
-        }
+    my $size = shift;
+    if ($size == 8) {
+        return 0x7F;
+    } elsif ($size == 16) {
+        return 0x7FFF;
+    } elsif ($size == 32) {
+        return 0x7FFFFFFF;
     }
 
     die("bug in script.\n");
 }
 
 sub getFloatToIntMult {
-    my ($signed, $size) = @_;
-    my $val = maxIntVal($signed, $size) . '.0';
+    my $size = shift;
+    my $val = maxIntVal($size) . '.0';
     $val .= 'f' if ($size < 32);
     return $val;
 }
 
 sub getIntToFloatDivBy {
-    my ($signed, $size) = @_;
-    return 'DIVBY' . maxIntVal($signed, $size);
+    my $size = shift;
+    return 'DIVBY' . maxIntVal($size);
 }
 
 sub getSignFlipVal {
@@ -215,13 +205,19 @@
         my $code = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, '*src');
         if ($ffloat != $tfloat) {
             if ($ffloat) {
-                my $mult = getFloatToIntMult($tsigned, $tsize);
+                my $mult = getFloatToIntMult($tsize);
+                if (!$tsigned) {   # bump from -1.0f/1.0f to 0.0f/2.0f
+                    $code = "($code + 1.0f)";
+                }
                 $code = "(($tctype) ($code * $mult))";
             } else {
                 # $divby will be the reciprocal, to avoid pipeline stalls
                 #  from floating point division...so multiply it.
-                my $divby = getIntToFloatDivBy($fsigned, $fsize);
+                my $divby = getIntToFloatDivBy($fsize);
                 $code = "(((float) $code) * $divby)";
+                if (!$fsigned) {   # bump from 0.0f/2.0f to -1.0f/1.0f.
+                    $code = "($code - 1.0f)";
+                }
             }
         } else {
             # All integer conversions here.