Skip to content
Permalink
Browse files

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).
  • Loading branch information
icculus committed May 15, 2018
1 parent ed4fe4c commit 7832cb652e852bb40547d810b511e5eb05ae05a1
Showing with 10 additions and 10 deletions.
  1. +10 −10 src/audio/SDL_audiotypecvt.c
@@ -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) {
const float sample = *src;
if (sample > 1.0f) {
if (sample >= 1.0f) {
*dst = 127;
} else if (sample < -1.0f) {
} else if (sample <= -1.0f) {
*dst = -127;
} else {
*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) {
const float sample = *src;
if (sample > 1.0f) {
if (sample >= 1.0f) {
*dst = 255;
} else if (sample < -1.0f) {
} else if (sample <= -1.0f) {
*dst = 0;
} else {
*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) {
const float sample = *src;
if (sample > 1.0f) {
if (sample >= 1.0f) {
*dst = 32767;
} else if (sample < -1.0f) {
} else if (sample <= -1.0f) {
*dst = -32767;
} else {
*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) {
const float sample = *src;
if (sample > 1.0f) {
if (sample >= 1.0f) {
*dst = 65534;
} else if (sample < -1.0f) {
} else if (sample <= -1.0f) {
*dst = 0;
} else {
*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) {
const float sample = *src;
if (sample > 1.0f) {
if (sample >= 1.0f) {
*dst = 2147483647;
} else if (sample < -1.0f) {
} else if (sample <= -1.0f) {
*dst = -2147483647;
} else {
*dst = ((Sint32)(sample * 8388607.0f)) << 8;

0 comments on commit 7832cb6

Please sign in to comment.