From 725ceb128ed4cb48a039d2f32ae73f8c8dccabef Mon Sep 17 00:00:00 2001
From: Chris Robinson <chris.kcat@gmail.com>
Date: Sat, 20 Apr 2019 19:29:33 -0700
Subject: Open the playback device earlier

So we actually have a device name to get the initial settings for. Be aware
that some backends set a format when opening instead of on reset, so such
devices will only set the default format (it can't get a desired format without
the device name, but the format will already be set once that's known). The
affected backends are WinMM, SDL2, and PortAudio (none of which are generally
used). This could be fixed by reopening the device during reset, but it would
need to be done carefully.
---
 Alc/alc.cpp | 38 ++++++++++++++++++++------------------
 1 file changed, 20 insertions(+), 18 deletions(-)

(limited to 'Alc')

diff --git a/Alc/alc.cpp b/Alc/alc.cpp
index f5da7657..27e926da 100644
--- a/Alc/alc.cpp
+++ b/Alc/alc.cpp
@@ -3665,7 +3665,7 @@ START_API_FUNC
 
     DeviceRef device{new ALCdevice{Playback}};
 
-    //Set output format
+    /* Set output format */
     device->FmtChans = DevFmtChannelsDefault;
     device->FmtType = DevFmtTypeDefault;
     device->Frequency = DEFAULT_OUTPUT_RATE;
@@ -3677,6 +3677,24 @@ START_API_FUNC
     device->AuxiliaryEffectSlotMax = 64;
     device->NumAuxSends = DEFAULT_SENDS;
 
+    /* Create the device backend. */
+    device->Backend = PlaybackBackend.getFactory().createBackend(device.get(),
+        BackendType::Playback);
+    if(!device->Backend)
+    {
+        alcSetError(nullptr, ALC_OUT_OF_MEMORY);
+        return nullptr;
+    }
+
+    /* Find a playback device to open */
+    ALCenum err{device->Backend->open(deviceName)};
+    if(err != ALC_NO_ERROR)
+    {
+        alcSetError(nullptr, err);
+        return nullptr;
+    }
+
+    deviceName = device->DeviceName.c_str();
     const ALCchar *fmt{};
     if(ConfigValueStr(deviceName, nullptr, "channels", &fmt))
     {
@@ -3774,23 +3792,7 @@ START_API_FUNC
     device->NumStereoSources = 1;
     device->NumMonoSources = device->SourcesMax - device->NumStereoSources;
 
-    device->Backend = PlaybackBackend.getFactory().createBackend(device.get(),
-        BackendType::Playback);
-    if(!device->Backend)
-    {
-        alcSetError(nullptr, ALC_OUT_OF_MEMORY);
-        return nullptr;
-    }
-
-    // Find a playback device to open
-    ALCenum err{device->Backend->open(deviceName)};
-    if(err != ALC_NO_ERROR)
-    {
-        alcSetError(nullptr, err);
-        return nullptr;
-    }
-
-    if(ConfigValueStr(device->DeviceName.c_str(), nullptr, "ambi-format", &fmt))
+    if(ConfigValueStr(deviceName, nullptr, "ambi-format", &fmt))
     {
         if(strcasecmp(fmt, "fuma") == 0)
         {
-- 
cgit v1.2.3