audio: Some fixes to the audio data type converter code.
authorRyan C. Gordon <icculus@icculus.org>
Sun, 15 Jan 2017 05:01:59 -0500
changeset 10814938218064f67
parent 10813 b93b700393d5
child 10815 71bbe3233508
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
src/audio/SDL_audiotypecvt.c
     1.1 --- a/src/audio/SDL_audiocvt.c	Sat Jan 14 21:36:06 2017 +0100
     1.2 +++ b/src/audio/SDL_audiocvt.c	Sun Jan 15 05:01:59 2017 -0500
     1.3 @@ -196,7 +196,7 @@
     1.4                          float *last_sample, const float *inbuf,
     1.5                          const int inbuflen, float *outbuf, const int outbuflen)
     1.6  {
     1.7 -    const int framelen = chans * sizeof(float);
     1.8 +    const int framelen = chans * sizeof (float);
     1.9      const int total = (inbuflen / framelen);
    1.10      const int finalpos = total - chans;
    1.11      const double src_incr = 1.0 / rate_incr;
    1.12 @@ -220,7 +220,7 @@
    1.13          idx += src_incr;
    1.14      }
    1.15  
    1.16 -    return (int)((dst - outbuf) * sizeof(float));
    1.17 +    return (int) ((dst - outbuf) * sizeof (float));
    1.18  }
    1.19  
    1.20  
     2.1 --- a/src/audio/SDL_audiotypecvt.c	Sat Jan 14 21:36:06 2017 +0100
     2.2 +++ b/src/audio/SDL_audiotypecvt.c	Sun Jan 15 05:01:59 2017 -0500
     2.3 @@ -31,14 +31,14 @@
     2.4  void SDLCALL
     2.5  SDL_Convert_S8_to_F32(SDL_AudioCVT *cvt, SDL_AudioFormat format)
     2.6  {
     2.7 -    const Uint8 *src = ((const Uint8 *) (cvt->buf + cvt->len_cvt)) - 1;
     2.8 +    const Sint8 *src = ((const Sint8 *) (cvt->buf + cvt->len_cvt)) - 1;
     2.9      float *dst = ((float *) (cvt->buf + cvt->len_cvt * 4)) - 1;
    2.10      int i;
    2.11  
    2.12      LOG_DEBUG_CONVERT("AUDIO_S8", "AUDIO_F32");
    2.13  
    2.14 -    for (i = cvt->len_cvt / sizeof (Uint8); i; --i, --src, --dst) {
    2.15 -        *dst = (((float) ((Sint8) *src)) * DIVBY127);
    2.16 +    for (i = cvt->len_cvt; i; --i, --src, --dst) {
    2.17 +        *dst = (((float) *src) * DIVBY127);
    2.18      }
    2.19  
    2.20      cvt->len_cvt *= 4;
    2.21 @@ -56,7 +56,7 @@
    2.22  
    2.23      LOG_DEBUG_CONVERT("AUDIO_U8", "AUDIO_F32");
    2.24  
    2.25 -    for (i = cvt->len_cvt / sizeof (Uint8); i; --i, --src, --dst) {
    2.26 +    for (i = cvt->len_cvt; i; --i, --src, --dst) {
    2.27          *dst = ((((float) *src) * DIVBY127) - 1.0f);
    2.28      }
    2.29  
    2.30 @@ -107,14 +107,14 @@
    2.31  void SDLCALL
    2.32  SDL_Convert_S32_to_F32(SDL_AudioCVT *cvt, SDL_AudioFormat format)
    2.33  {
    2.34 -    const Uint32 *src = (const Uint32 *) cvt->buf;
    2.35 +    const Sint32 *src = (const Sint32 *) cvt->buf;
    2.36      float *dst = (float *) cvt->buf;
    2.37      int i;
    2.38  
    2.39      LOG_DEBUG_CONVERT("AUDIO_S32", "AUDIO_F32");
    2.40  
    2.41      for (i = cvt->len_cvt / sizeof (Sint32); i; --i, ++src, ++dst) {
    2.42 -        *dst = (((float) *src) * DIVBY2147483647);
    2.43 +        *dst = (float) (((double) *src) * DIVBY2147483647);
    2.44      }
    2.45  
    2.46      if (cvt->filters[++cvt->filter_index]) {
    2.47 @@ -208,7 +208,7 @@
    2.48      LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S32");
    2.49  
    2.50      for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
    2.51 -        *dst = (Sint32) (*src * 2147483647.0);
    2.52 +        *dst = (Sint32) (((double) *src) * 2147483647.0);
    2.53      }
    2.54  
    2.55      if (cvt->filters[++cvt->filter_index]) {