From 1e66d457d76202f58fb489c757ef0ee033c6787a Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sun, 15 Jan 2017 05:01:59 -0500 Subject: [PATCH] audio: Some fixes to the audio data type converter code. Removed some needless things ("len / sizeof (Uint8)"), and made sure the int32 -> float code uses doubles to avoid working with large integer values in a 32-bit float. --- src/audio/SDL_audiocvt.c | 4 ++-- src/audio/SDL_audiotypecvt.c | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/audio/SDL_audiocvt.c b/src/audio/SDL_audiocvt.c index 88fb1aa92c4d7..3b7faff50a96e 100644 --- a/src/audio/SDL_audiocvt.c +++ b/src/audio/SDL_audiocvt.c @@ -196,7 +196,7 @@ SDL_ResampleAudioSimple(const int chans, const double rate_incr, float *last_sample, const float *inbuf, const int inbuflen, float *outbuf, const int outbuflen) { - const int framelen = chans * sizeof(float); + const int framelen = chans * sizeof (float); const int total = (inbuflen / framelen); const int finalpos = total - chans; const double src_incr = 1.0 / rate_incr; @@ -220,7 +220,7 @@ SDL_ResampleAudioSimple(const int chans, const double rate_incr, idx += src_incr; } - return (int)((dst - outbuf) * sizeof(float)); + return (int) ((dst - outbuf) * sizeof (float)); } diff --git a/src/audio/SDL_audiotypecvt.c b/src/audio/SDL_audiotypecvt.c index 12b34fe995aff..cebfca6e5cdbb 100644 --- a/src/audio/SDL_audiotypecvt.c +++ b/src/audio/SDL_audiotypecvt.c @@ -31,14 +31,14 @@ void SDLCALL SDL_Convert_S8_to_F32(SDL_AudioCVT *cvt, SDL_AudioFormat format) { - const Uint8 *src = ((const Uint8 *) (cvt->buf + cvt->len_cvt)) - 1; + const Sint8 *src = ((const Sint8 *) (cvt->buf + cvt->len_cvt)) - 1; float *dst = ((float *) (cvt->buf + cvt->len_cvt * 4)) - 1; int i; LOG_DEBUG_CONVERT("AUDIO_S8", "AUDIO_F32"); - for (i = cvt->len_cvt / sizeof (Uint8); i; --i, --src, --dst) { - *dst = (((float) ((Sint8) *src)) * DIVBY127); + for (i = cvt->len_cvt; i; --i, --src, --dst) { + *dst = (((float) *src) * DIVBY127); } cvt->len_cvt *= 4; @@ -56,7 +56,7 @@ SDL_Convert_U8_to_F32(SDL_AudioCVT *cvt, SDL_AudioFormat format) LOG_DEBUG_CONVERT("AUDIO_U8", "AUDIO_F32"); - for (i = cvt->len_cvt / sizeof (Uint8); i; --i, --src, --dst) { + for (i = cvt->len_cvt; i; --i, --src, --dst) { *dst = ((((float) *src) * DIVBY127) - 1.0f); } @@ -107,14 +107,14 @@ SDL_Convert_U16_to_F32(SDL_AudioCVT *cvt, SDL_AudioFormat format) void SDLCALL SDL_Convert_S32_to_F32(SDL_AudioCVT *cvt, SDL_AudioFormat format) { - const Uint32 *src = (const Uint32 *) cvt->buf; + const Sint32 *src = (const Sint32 *) cvt->buf; float *dst = (float *) cvt->buf; int i; LOG_DEBUG_CONVERT("AUDIO_S32", "AUDIO_F32"); for (i = cvt->len_cvt / sizeof (Sint32); i; --i, ++src, ++dst) { - *dst = (((float) *src) * DIVBY2147483647); + *dst = (float) (((double) *src) * DIVBY2147483647); } if (cvt->filters[++cvt->filter_index]) { @@ -208,7 +208,7 @@ SDL_Convert_F32_to_S32(SDL_AudioCVT *cvt, SDL_AudioFormat format) LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S32"); for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) { - *dst = (Sint32) (*src * 2147483647.0); + *dst = (Sint32) (((double) *src) * 2147483647.0); } if (cvt->filters[++cvt->filter_index]) {