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).
This commit is contained in:
Ryan C. Gordon 2018-05-15 01:35:53 -04:00
parent ed4fe4c9a0
commit 7832cb652e

View File

@ -171,9 +171,9 @@ SDL_Convert_F32_to_S8_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) { for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
const float sample = *src; const float sample = *src;
if (sample > 1.0f) { if (sample >= 1.0f) {
*dst = 127; *dst = 127;
} else if (sample < -1.0f) { } else if (sample <= -1.0f) {
*dst = -127; *dst = -127;
} else { } else {
*dst = (Sint8)(sample * 127.0f); *dst = (Sint8)(sample * 127.0f);
@ -197,9 +197,9 @@ SDL_Convert_F32_to_U8_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) { for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
const float sample = *src; const float sample = *src;
if (sample > 1.0f) { if (sample >= 1.0f) {
*dst = 255; *dst = 255;
} else if (sample < -1.0f) { } else if (sample <= -1.0f) {
*dst = 0; *dst = 0;
} else { } else {
*dst = (Uint8)((sample + 1.0f) * 127.0f); *dst = (Uint8)((sample + 1.0f) * 127.0f);
@ -223,9 +223,9 @@ SDL_Convert_F32_to_S16_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) { for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
const float sample = *src; const float sample = *src;
if (sample > 1.0f) { if (sample >= 1.0f) {
*dst = 32767; *dst = 32767;
} else if (sample < -1.0f) { } else if (sample <= -1.0f) {
*dst = -32767; *dst = -32767;
} else { } else {
*dst = (Sint16)(sample * 32767.0f); *dst = (Sint16)(sample * 32767.0f);
@ -249,9 +249,9 @@ SDL_Convert_F32_to_U16_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) { for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
const float sample = *src; const float sample = *src;
if (sample > 1.0f) { if (sample >= 1.0f) {
*dst = 65534; *dst = 65534;
} else if (sample < -1.0f) { } else if (sample <= -1.0f) {
*dst = 0; *dst = 0;
} else { } else {
*dst = (Uint16)((sample + 1.0f) * 32767.0f); *dst = (Uint16)((sample + 1.0f) * 32767.0f);
@ -275,9 +275,9 @@ SDL_Convert_F32_to_S32_Scalar(SDL_AudioCVT *cvt, SDL_AudioFormat format)
for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) { for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
const float sample = *src; const float sample = *src;
if (sample > 1.0f) { if (sample >= 1.0f) {
*dst = 2147483647; *dst = 2147483647;
} else if (sample < -1.0f) { } else if (sample <= -1.0f) {
*dst = -2147483647; *dst = -2147483647;
} else { } else {
*dst = ((Sint32)(sample * 8388607.0f)) << 8; *dst = ((Sint32)(sample * 8388607.0f)) << 8;