audio: float to int converters should clamp inclusively.
authorRyan C. Gordon <icculus@icculus.org>
Tue, 15 May 2018 01:35:53 -0400
changeset 11989d194f4f41437
parent 11988 8fa59be85dfb
child 11990 ce0099c8d037
audio: float to int converters should clamp inclusively.

If we have to test if a sample is > 1.0f anyhow, we might as well use this
to avoid the unnecessary multiplication when it's == 1.0f, too. (etc).
src/audio/SDL_audiotypecvt.c
     1.1 --- a/src/audio/SDL_audiotypecvt.c	Tue May 15 00:04:02 2018 -0400
     1.2 +++ b/src/audio/SDL_audiotypecvt.c	Tue May 15 01:35:53 2018 -0400
     1.3 @@ -171,9 +171,9 @@
     1.4  
     1.5      for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
     1.6          const float sample = *src;
     1.7 -        if (sample > 1.0f) {
     1.8 +        if (sample >= 1.0f) {
     1.9              *dst = 127;
    1.10 -        } else if (sample < -1.0f) {
    1.11 +        } else if (sample <= -1.0f) {
    1.12              *dst = -127;
    1.13          } else {
    1.14              *dst = (Sint8)(sample * 127.0f);
    1.15 @@ -197,9 +197,9 @@
    1.16  
    1.17      for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
    1.18          const float sample = *src;
    1.19 -        if (sample > 1.0f) {
    1.20 +        if (sample >= 1.0f) {
    1.21              *dst = 255;
    1.22 -        } else if (sample < -1.0f) {
    1.23 +        } else if (sample <= -1.0f) {
    1.24              *dst = 0;
    1.25          } else {
    1.26              *dst = (Uint8)((sample + 1.0f) * 127.0f);
    1.27 @@ -223,9 +223,9 @@
    1.28  
    1.29      for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
    1.30          const float sample = *src;
    1.31 -        if (sample > 1.0f) {
    1.32 +        if (sample >= 1.0f) {
    1.33              *dst = 32767;
    1.34 -        } else if (sample < -1.0f) {
    1.35 +        } else if (sample <= -1.0f) {
    1.36              *dst = -32767;
    1.37          } else {
    1.38              *dst = (Sint16)(sample * 32767.0f);
    1.39 @@ -249,9 +249,9 @@
    1.40  
    1.41      for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
    1.42          const float sample = *src;
    1.43 -        if (sample > 1.0f) {
    1.44 +        if (sample >= 1.0f) {
    1.45              *dst = 65534;
    1.46 -        } else if (sample < -1.0f) {
    1.47 +        } else if (sample <= -1.0f) {
    1.48              *dst = 0;
    1.49          } else {
    1.50              *dst = (Uint16)((sample + 1.0f) * 32767.0f);
    1.51 @@ -275,9 +275,9 @@
    1.52  
    1.53      for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
    1.54          const float sample = *src;
    1.55 -        if (sample > 1.0f) {
    1.56 +        if (sample >= 1.0f) {
    1.57              *dst = 2147483647;
    1.58 -        } else if (sample < -1.0f) {
    1.59 +        } else if (sample <= -1.0f) {
    1.60              *dst = -2147483647;
    1.61          } else {
    1.62              *dst = ((Sint32)(sample * 8388607.0f)) << 8;