From ea81ade9a2abe24df8c7ee61046e8d824198b3bd Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Fri, 27 Dec 2002 15:13:37 +0000 Subject: [PATCH] (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 | 49 +++++++++++++++++++++++++++++++++------------- effects_internal.c | 9 +++++---- 2 files changed, 40 insertions(+), 18 deletions(-) diff --git a/effect_position.c b/effect_position.c index 3a5d17c4..3c649982 100644 --- a/effect_position.c +++ b/effect_position.c @@ -104,8 +104,11 @@ static void _Eff_position_u8(int chan, void *stream, int len, void *udata) } for (i = 0; i < len; i += sizeof (Uint8) * 2) { - *(ptr++) = (Uint8)((((float) *ptr) * args->left_f) * args->distance_f); - *(ptr++) = (Uint8)((((float) *ptr) * args->right_f) * args->distance_f); + /* must adjust the sample so that 0 is the center */ + *(ptr++) = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + * args->left_f) * args->distance_f) + 128); + *(ptr++) = (Uint8) ((Sint8) ((((float) (Sint8) (*ptr - 128)) + * args->right_f) * args->distance_f) + 128); } } @@ -237,15 +240,24 @@ static void _Eff_position_u16lsb(int chan, void *stream, int len, void *udata) for (i = 0; i < len; i += sizeof (Uint16) * 2) { #if (SDL_BYTEORDER == SDL_BIG_ENDIAN) - Uint16 swapl = (Uint16) ((((float) SDL_Swap16(*(ptr))) * - args->left_f) * args->distance_f); - Uint16 swapr = (Uint16) ((((float) SDL_Swap16(*(ptr+1))) * - args->right_f) * args->distance_f); + Sint16 sampl = (Sint16) (SDL_Swap16(*(ptr+0)) - 32768); + Sint16 sampr = (Sint16) (SDL_Swap16(*(ptr+1)) - 32768); + + Uint16 swapl = (Uint16) ((Sint16) (((float) sampl * args->left_f) + * args->distance_f) + 32768); + Uint16 swapr = (Uint16) ((Sint16) (((float) sampr * args->right_f) + * args->distance_f) + 32768); + *(ptr++) = (Uint16) SDL_Swap16(swapl); *(ptr++) = (Uint16) SDL_Swap16(swapr); #else - *(ptr++) = (Uint16) ((((float) *ptr)*args->left_f)*args->distance_f); - *(ptr++) = (Uint16) ((((float) *ptr)*args->right_f)*args->distance_f); + Sint16 sampl = (Sint16) (*(ptr+0) - 32768); + Sint16 sampr = (Sint16) (*(ptr+1) - 32768); + + *(ptr++) = (Uint16) ((Sint16) (((float) sampl * args->left_f) + * args->distance_f) + 32768); + *(ptr++) = (Uint16) ((Sint16) (((float) sampr * args->right_f) + * args->distance_f) + 32768); #endif } } @@ -281,15 +293,24 @@ static void _Eff_position_u16msb(int chan, void *stream, int len, void *udata) for (i = 0; i < len; i += sizeof (Sint16) * 2) { #if (SDL_BYTEORDER == SDL_LIL_ENDIAN) - Uint16 swapl = (Uint16) ((((float) SDL_Swap16(*(ptr))) * - args->left_f) * args->distance_f); - Uint16 swapr = (Uint16) ((((float) SDL_Swap16(*(ptr+1))) * - args->right_f) * args->distance_f); + Sint16 sampl = (Sint16) (SDL_Swap16(*(ptr+0)) - 32768); + Sint16 sampr = (Sint16) (SDL_Swap16(*(ptr+1)) - 32768); + + Uint16 swapl = (Uint16) ((Sint16) (((float) sampl * args->left_f) + * args->distance_f) + 32768); + Uint16 swapr = (Uint16) ((Sint16) (((float) sampr * args->right_f) + * args->distance_f) + 32768); + *(ptr++) = (Uint16) SDL_Swap16(swapl); *(ptr++) = (Uint16) SDL_Swap16(swapr); #else - *(ptr++) = (Uint16) ((((float) *ptr)*args->left_f)*args->distance_f); - *(ptr++) = (Uint16) ((((float) *ptr)*args->right_f)*args->distance_f); + Sint16 sampl = (Sint16) (*(ptr+0) - 32768); + Sint16 sampr = (Sint16) (*(ptr+1) - 32768); + + *(ptr++) = (Uint16) ((Sint16) (((float) sampl * args->left_f) + * args->distance_f) + 32768); + *(ptr++) = (Uint16) ((Sint16) (((float) sampr * args->right_f) + * args->distance_f) + 32768); #endif } } diff --git a/effects_internal.c b/effects_internal.c index ea37961e..33d80887 100644 --- a/effects_internal.c +++ b/effects_internal.c @@ -67,8 +67,9 @@ void *_Eff_build_volume_table_u8(void) if (rc) { _Eff_volume_table = (void *) rc; for (volume = 0; volume < 256; volume++) { - for (sample = 0; sample < 256; sample ++) { - *rc = (Uint8)(((float) sample) * ((float) volume / 255.0)); + for (sample = -128; sample < 128; sample ++) { + *rc = (Uint8)(((float) sample) * ((float) volume / 255.0)) + + 128; rc++; } } @@ -83,7 +84,7 @@ void *_Eff_build_volume_table_u8(void) * * Each column of the table is a possible sample, while each row of the * table is a volume. Volume is a Uint8, where 0 is silence and 255 is full - * volume. So _Eff_volume_table[128][mysample] would be the value of + * volume. So _Eff_volume_table[128][mysample+128] would be the value of * mysample, at half volume. */ void *_Eff_build_volume_table_s8(void) @@ -97,7 +98,7 @@ void *_Eff_build_volume_table_s8(void) if (rc) { _Eff_volume_table = (void *) rc; for (volume = 0; volume < 256; volume++) { - for (sample = 0; sample < 256; sample ++) { + for (sample = -128; sample < 128; sample ++) { *rc = (Sint8)(((float) sample) * ((float) volume / 255.0)); rc++; }