Commit 93ff12ce8344cfdd81c5cb6fcc1e52432b6dd2d6

Sam Lantinga 2016-10-07T17:23:20

Fixed bug 2952 - SDL_MixAudioFormat does not support audio format AUDIO_U16LSB/AUDIO_U16MSB Simon Sandstr?m As stated in Summary. The switch statement will execute the default case and set a SDL error message: "SDL_MixAudio(): unknown audio format". There are atleast two more problems here: 1. SDL_MixAudioFormat does not notify the user that an error has occured and that a SDL error message was set. It took me awhile to understand why I couldn't mix down the volume on my AUDIO_U16LSB formatted audio stream.. until I started digging in the SDL source code. 2. The error message is incorrect, it should read: "SDL_MixAudioFormat(): unknown audio format".

diff --git a/src/audio/SDL_mixer.c b/src/audio/SDL_mixer.c
index b49c73d..e021939 100644
--- a/src/audio/SDL_mixer.c
+++ b/src/audio/SDL_mixer.c
@@ -202,6 +202,54 @@ SDL_MixAudioFormat(Uint8 * dst, const Uint8 * src, SDL_AudioFormat format,
         }
         break;
 
+    case AUDIO_U16LSB:
+        {
+            Uint16 src1, src2;
+            int dst_sample;
+            const int max_audioval = 0xFFFF;
+
+            len /= 2;
+            while (len--) {
+                src1 = ((src[1]) << 8 | src[0]);
+                ADJUST_VOLUME(src1, volume);
+                src2 = ((dst[1]) << 8 | dst[0]);
+                src += 2;
+                dst_sample = src1 + src2;
+                if (dst_sample > max_audioval) {
+                    dst_sample = max_audioval;
+                }
+                dst[0] = dst_sample & 0xFF;
+                dst_sample >>= 8;
+                dst[1] = dst_sample & 0xFF;
+                dst += 2;
+            }
+        }
+        break;
+
+    case AUDIO_U16MSB:
+        {
+            Uint16 src1, src2;
+            int dst_sample;
+            const int max_audioval = 0xFFFF;
+
+            len /= 2;
+            while (len--) {
+                src1 = ((src[0]) << 8 | src[1]);
+                ADJUST_VOLUME(src1, volume);
+                src2 = ((dst[0]) << 8 | dst[1]);
+                src += 2;
+                dst_sample = src1 + src2;
+                if (dst_sample > max_audioval) {
+                    dst_sample = max_audioval;
+                }
+                dst[1] = dst_sample & 0xFF;
+                dst_sample >>= 8;
+                dst[0] = dst_sample & 0xFF;
+                dst += 2;
+            }
+        }
+        break;
+
     case AUDIO_S32LSB:
         {
             const Uint32 *src32 = (Uint32 *) src;
@@ -313,7 +361,7 @@ SDL_MixAudioFormat(Uint8 * dst, const Uint8 * src, SDL_AudioFormat format,
         break;
 
     default:                   /* If this happens... FIXME! */
-        SDL_SetError("SDL_MixAudio(): unknown audio format");
+        SDL_SetError("SDL_MixAudioFormat(): unknown audio format");
         return;
     }
 }