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 7518 febc83bc2a43
parent 7517 965d57022c01
child 7519 b9ef4591d6b5
Fixed crash and garbled output when converting from F32 to S16 audio.
src/audio/SDL_audio.c
--- a/src/audio/SDL_audio.c	Thu Jul 25 09:51:21 2013 -0700
+++ b/src/audio/SDL_audio.c	Thu Jul 25 18:11:09 2013 -0700
@@ -360,9 +360,7 @@
                 device->convert.len_mult;
         }
 #endif
-
-        /* stream_len = device->convert.len; */
-        stream_len = device->spec.size;
+        stream_len = device->convert.len;
     } else {
         stream_len = device->spec.size;
     }
@@ -958,12 +956,6 @@
         return 0;
     }
 
-    /* If the audio driver changes the buffer size, accept it */
-    if (device->spec.samples != obtained->samples) {
-        obtained->samples = device->spec.samples;
-        SDL_CalculateAudioSpec(obtained);
-    }
-
     /* See if we need to do any conversion */
     build_cvt = SDL_FALSE;
     if (obtained->freq != device->spec.freq) {
@@ -987,6 +979,16 @@
             build_cvt = SDL_TRUE;
         }
     }
+
+    /* If the audio driver changes the buffer size, accept it.
+       This needs to be done after the format is modified above,
+       otherwise it might not have the correct buffer size.
+     */
+    if (device->spec.samples != obtained->samples) {
+        obtained->samples = device->spec.samples;
+        SDL_CalculateAudioSpec(obtained);
+    }
+
     if (build_cvt) {
         /* Build an audio conversion block */
         if (SDL_BuildAudioCVT(&device->convert,
@@ -998,7 +1000,7 @@
             return 0;
         }
         if (device->convert.needed) {
-            device->convert.len = (int) (((double) obtained->size) /
+            device->convert.len = (int) (((double) device->spec.size) /
                                          device->convert.len_ratio);
 
             device->convert.buf =