Fixed crash and garbled output when converting from F32 to S16 audio.
authorSam Lantinga <slouken@libsdl.org>
Thu, 25 Jul 2013 18:11:09 -0700
changeset 7518febc83bc2a43
parent 7517 965d57022c01
child 7519 b9ef4591d6b5
Fixed crash and garbled output when converting from F32 to S16 audio.
src/audio/SDL_audio.c
     1.1 --- a/src/audio/SDL_audio.c	Thu Jul 25 09:51:21 2013 -0700
     1.2 +++ b/src/audio/SDL_audio.c	Thu Jul 25 18:11:09 2013 -0700
     1.3 @@ -360,9 +360,7 @@
     1.4                  device->convert.len_mult;
     1.5          }
     1.6  #endif
     1.7 -
     1.8 -        /* stream_len = device->convert.len; */
     1.9 -        stream_len = device->spec.size;
    1.10 +        stream_len = device->convert.len;
    1.11      } else {
    1.12          stream_len = device->spec.size;
    1.13      }
    1.14 @@ -958,12 +956,6 @@
    1.15          return 0;
    1.16      }
    1.17  
    1.18 -    /* If the audio driver changes the buffer size, accept it */
    1.19 -    if (device->spec.samples != obtained->samples) {
    1.20 -        obtained->samples = device->spec.samples;
    1.21 -        SDL_CalculateAudioSpec(obtained);
    1.22 -    }
    1.23 -
    1.24      /* See if we need to do any conversion */
    1.25      build_cvt = SDL_FALSE;
    1.26      if (obtained->freq != device->spec.freq) {
    1.27 @@ -987,6 +979,16 @@
    1.28              build_cvt = SDL_TRUE;
    1.29          }
    1.30      }
    1.31 +
    1.32 +    /* If the audio driver changes the buffer size, accept it.
    1.33 +       This needs to be done after the format is modified above,
    1.34 +       otherwise it might not have the correct buffer size.
    1.35 +     */
    1.36 +    if (device->spec.samples != obtained->samples) {
    1.37 +        obtained->samples = device->spec.samples;
    1.38 +        SDL_CalculateAudioSpec(obtained);
    1.39 +    }
    1.40 +
    1.41      if (build_cvt) {
    1.42          /* Build an audio conversion block */
    1.43          if (SDL_BuildAudioCVT(&device->convert,
    1.44 @@ -998,7 +1000,7 @@
    1.45              return 0;
    1.46          }
    1.47          if (device->convert.needed) {
    1.48 -            device->convert.len = (int) (((double) obtained->size) /
    1.49 +            device->convert.len = (int) (((double) device->spec.size) /
    1.50                                           device->convert.len_ratio);
    1.51  
    1.52              device->convert.buf =