From 2fc254b72ef2948a3deea287ab4a80c45753597e Mon Sep 17 00:00:00 2001 From: reuk Date: Thu, 4 Nov 2021 11:29:39 +0000 Subject: [PATCH] AudioDeviceManager: Avoid overwriting user-specified devices in initialise unless absolutely necessary This fixes an issue where specifying an audio device type before calling initialise could cause a different device to become active, even if the requested device type had usable devices. With this change in place, a new device type will only be selected if the current device type has no devices. --- .../audio_io/juce_AudioDeviceManager.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.cpp b/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.cpp index 8083139f09..3ffbc3ca25 100644 --- a/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.cpp +++ b/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.cpp @@ -144,6 +144,10 @@ void AudioDeviceManager::pickCurrentDeviceTypeWithDevices() || ! ptr->getDeviceNames (false).isEmpty(); }; + if (auto* type = findType (currentDeviceType)) + if (deviceTypeHasDevices (type)) + return; + const auto iter = std::find_if (availableDeviceTypes.begin(), availableDeviceTypes.end(), deviceTypeHasDevices); @@ -1399,6 +1403,32 @@ public: expectEquals (newSetup.inputChannels.countNumberOfSetBits(), 2); } + beginTest ("If a device type has been explicitly set to a type with devices, " + "initialisation should respect this choice"); + { + AudioDeviceManager manager; + initialiseManagerWithEmptyDeviceType (manager); + manager.setCurrentAudioDeviceType (mockBName, true); + + AudioDeviceManager::AudioDeviceSetup setup; + expect (manager.initialise (2, 2, nullptr, true, {}, &setup).isEmpty()); + + expectEquals (manager.getCurrentAudioDeviceType(), mockBName); + } + + beginTest ("If a device type has been explicitly set to a type without devices, " + "initialisation should pick a type with devices instead"); + { + AudioDeviceManager manager; + initialiseManagerWithEmptyDeviceType (manager); + manager.setCurrentAudioDeviceType (emptyName, true); + + AudioDeviceManager::AudioDeviceSetup setup; + expect (manager.initialise (2, 2, nullptr, true, {}, &setup).isEmpty()); + + expectEquals (manager.getCurrentAudioDeviceType(), mockAName); + } + beginTest ("Carry out a long sequence of configuration changes"); { AudioDeviceManager manager;