(From Steven Fuller. --ryan.)
authorRyan C. Gordon <icculus@icculus.org>
Fri, 27 Dec 2002 15:13:37 +0000
changeset 201e55fb6b45559
parent 200 15d79d938aae
child 202 eda0ff130252
(From Steven Fuller. --ryan.)

"Hi,

Here's a patch that fixes problems (attenuation on unsigned samples
would cause the samples to fall towards 0 when it should instead fall
towards 128/32768) when doing panning on 8-bit sounds and 16-bit unsigned
samples."
effect_position.c
effects_internal.c
     1.1 --- a/effect_position.c	Mon Dec 16 04:50:16 2002 +0000
     1.2 +++ b/effect_position.c	Fri Dec 27 15:13:37 2002 +0000
     1.3 @@ -104,8 +104,11 @@
     1.4      }
     1.5  
     1.6      for (i = 0; i < len; i += sizeof (Uint8) * 2) {
     1.7 -        *(ptr++) = (Uint8)((((float) *ptr) * args->left_f) * args->distance_f);
     1.8 -        *(ptr++) = (Uint8)((((float) *ptr) * args->right_f) * args->distance_f);
     1.9 +        /* must adjust the sample so that 0 is the center */
    1.10 +        *(ptr++) = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
    1.11 +            * args->left_f) * args->distance_f) + 128);
    1.12 +        *(ptr++) = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) 
    1.13 +            * args->right_f) * args->distance_f) + 128);
    1.14      }
    1.15  }
    1.16  
    1.17 @@ -237,15 +240,24 @@
    1.18  
    1.19      for (i = 0; i < len; i += sizeof (Uint16) * 2) {
    1.20  #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
    1.21 -        Uint16 swapl = (Uint16) ((((float) SDL_Swap16(*(ptr))) *
    1.22 -                                    args->left_f) * args->distance_f);
    1.23 -        Uint16 swapr = (Uint16) ((((float) SDL_Swap16(*(ptr+1))) *
    1.24 -                                    args->right_f) * args->distance_f);
    1.25 +        Sint16 sampl = (Sint16) (SDL_Swap16(*(ptr+0)) - 32768);
    1.26 +        Sint16 sampr = (Sint16) (SDL_Swap16(*(ptr+1)) - 32768);
    1.27 +        
    1.28 +        Uint16 swapl = (Uint16) ((Sint16) (((float) sampl * args->left_f)
    1.29 +                                    * args->distance_f) + 32768);
    1.30 +        Uint16 swapr = (Uint16) ((Sint16) (((float) sampr * args->right_f)
    1.31 +                                    * args->distance_f) + 32768);
    1.32 +
    1.33          *(ptr++) = (Uint16) SDL_Swap16(swapl);
    1.34          *(ptr++) = (Uint16) SDL_Swap16(swapr);
    1.35  #else
    1.36 -        *(ptr++) = (Uint16) ((((float) *ptr)*args->left_f)*args->distance_f);
    1.37 -        *(ptr++) = (Uint16) ((((float) *ptr)*args->right_f)*args->distance_f);
    1.38 +        Sint16 sampl = (Sint16) (*(ptr+0) - 32768);
    1.39 +        Sint16 sampr = (Sint16) (*(ptr+1) - 32768);
    1.40 +
    1.41 +        *(ptr++) = (Uint16) ((Sint16) (((float) sampl * args->left_f) 
    1.42 +                                    * args->distance_f) + 32768);
    1.43 +        *(ptr++) = (Uint16) ((Sint16) (((float) sampr * args->right_f) 
    1.44 +                                    * args->distance_f) + 32768);
    1.45  #endif
    1.46      }
    1.47  }
    1.48 @@ -281,15 +293,24 @@
    1.49  
    1.50      for (i = 0; i < len; i += sizeof (Sint16) * 2) {
    1.51  #if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
    1.52 -        Uint16 swapl = (Uint16) ((((float) SDL_Swap16(*(ptr))) *
    1.53 -                                    args->left_f) * args->distance_f);
    1.54 -        Uint16 swapr = (Uint16) ((((float) SDL_Swap16(*(ptr+1))) *
    1.55 -                                    args->right_f) * args->distance_f);
    1.56 +        Sint16 sampl = (Sint16) (SDL_Swap16(*(ptr+0)) - 32768);
    1.57 +        Sint16 sampr = (Sint16) (SDL_Swap16(*(ptr+1)) - 32768);
    1.58 +        
    1.59 +        Uint16 swapl = (Uint16) ((Sint16) (((float) sampl * args->left_f)
    1.60 +                                    * args->distance_f) + 32768);
    1.61 +        Uint16 swapr = (Uint16) ((Sint16) (((float) sampr * args->right_f)
    1.62 +                                    * args->distance_f) + 32768);
    1.63 +
    1.64          *(ptr++) = (Uint16) SDL_Swap16(swapl);
    1.65          *(ptr++) = (Uint16) SDL_Swap16(swapr);
    1.66  #else
    1.67 -        *(ptr++) = (Uint16) ((((float) *ptr)*args->left_f)*args->distance_f);
    1.68 -        *(ptr++) = (Uint16) ((((float) *ptr)*args->right_f)*args->distance_f);
    1.69 +        Sint16 sampl = (Sint16) (*(ptr+0) - 32768);
    1.70 +        Sint16 sampr = (Sint16) (*(ptr+1) - 32768);
    1.71 +
    1.72 +        *(ptr++) = (Uint16) ((Sint16) (((float) sampl * args->left_f) 
    1.73 +                                    * args->distance_f) + 32768);
    1.74 +        *(ptr++) = (Uint16) ((Sint16) (((float) sampr * args->right_f) 
    1.75 +                                    * args->distance_f) + 32768);
    1.76  #endif
    1.77      }
    1.78  }
     2.1 --- a/effects_internal.c	Mon Dec 16 04:50:16 2002 +0000
     2.2 +++ b/effects_internal.c	Fri Dec 27 15:13:37 2002 +0000
     2.3 @@ -67,8 +67,9 @@
     2.4          if (rc) {
     2.5              _Eff_volume_table = (void *) rc;
     2.6              for (volume = 0; volume < 256; volume++) {
     2.7 -                for (sample = 0; sample < 256; sample ++) {
     2.8 -                    *rc = (Uint8)(((float) sample) * ((float) volume / 255.0));
     2.9 +                for (sample = -128; sample < 128; sample ++) {
    2.10 +                    *rc = (Uint8)(((float) sample) * ((float) volume / 255.0)) 
    2.11 +                        + 128;
    2.12                      rc++;
    2.13                  }
    2.14              }
    2.15 @@ -83,7 +84,7 @@
    2.16   *
    2.17   * Each column of the table is a possible sample, while each row of the
    2.18   *  table is a volume. Volume is a Uint8, where 0 is silence and 255 is full
    2.19 - *  volume. So _Eff_volume_table[128][mysample] would be the value of
    2.20 + *  volume. So _Eff_volume_table[128][mysample+128] would be the value of
    2.21   *  mysample, at half volume.
    2.22   */
    2.23  void *_Eff_build_volume_table_s8(void)
    2.24 @@ -97,7 +98,7 @@
    2.25          if (rc) {
    2.26              _Eff_volume_table = (void *) rc;
    2.27              for (volume = 0; volume < 256; volume++) {
    2.28 -                for (sample = 0; sample < 256; sample ++) {
    2.29 +                for (sample = -128; sample < 128; sample ++) {
    2.30                      *rc = (Sint8)(((float) sample) * ((float) volume / 255.0));
    2.31                      rc++;
    2.32                  }