Browse Source

Limit number of channels to avoid issues on multichannel audio device (5.1, 7.1, etc) (#2769)

gleblebedev 4 years ago
parent
commit
f551d2eab8
1 changed files with 16 additions and 1 deletions
  1. 16 1
      Source/Urho3D/Audio/Audio.cpp

+ 16 - 1
Source/Urho3D/Audio/Audio.cpp

@@ -97,13 +97,28 @@ bool Audio::SetMode(int bufferLengthMSec, int mixRate, bool stereo, bool interpo
 
     // Intentionally disallow format change so that the obtained format will always be the desired format, even though that format
     // is not matching the device format, however in doing it will enable the SDL's internal audio stream with audio conversion
-    deviceID_ = SDL_OpenAudioDevice(nullptr, SDL_FALSE, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE&~SDL_AUDIO_ALLOW_FORMAT_CHANGE);
+    deviceID_ = SDL_OpenAudioDevice(nullptr, SDL_FALSE, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE & ~SDL_AUDIO_ALLOW_FORMAT_CHANGE);
     if (!deviceID_)
     {
         URHO3D_LOGERROR("Could not initialize audio output");
         return false;
     }
 
+    // If device was created with greater number of channels then requested (for instance 5.1 channels when only 2 stereo channels were requested) then limit the number to requested one.
+    if (obtained.channels > desired.channels)
+    {
+        // Close previously created device.
+        SDL_CloseAudioDevice(deviceID_);
+
+        // Create new device with limited number of channels.
+        deviceID_ = SDL_OpenAudioDevice(nullptr, SDL_FALSE, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE & ~SDL_AUDIO_ALLOW_FORMAT_CHANGE & ~SDL_AUDIO_ALLOW_CHANNELS_CHANGE);
+        if (!deviceID_)
+        {
+            URHO3D_LOGERROR("Could not initialize audio output");
+            return false;
+        }
+    }
+
     if (obtained.format != AUDIO_S16)
     {
         URHO3D_LOGERROR("Could not initialize audio output, 16-bit buffer format not supported");