Skip to content

Commit

Permalink
(From Steven Fuller. --ryan.)
Browse files Browse the repository at this point in the history
"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."
  • Loading branch information
icculus committed Dec 27, 2002
1 parent 2105242 commit ea81ade
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
49 changes: 35 additions & 14 deletions effect_position.c
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
}
Expand Down
9 changes: 5 additions & 4 deletions effects_internal.c
Expand Up @@ -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++;
}
}
Expand All @@ -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)
Expand All @@ -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++;
}
Expand Down

0 comments on commit ea81ade

Please sign in to comment.