From e14a183886fafdfe9c2831d9295305448f8e7085 Mon Sep 17 00:00:00 2001 From: ed Date: Wed, 6 Mar 2019 17:13:54 +0000 Subject: [PATCH] Update requested input/output channels whenever AudioDeviceManager::setAudioDeviceSetup() is called --- .../audio_io/juce_AudioDeviceManager.cpp | 77 +++++++++---------- .../audio_io/juce_AudioDeviceManager.h | 1 - 2 files changed, 37 insertions(+), 41 deletions(-) diff --git a/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.cpp b/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.cpp index f81d3690ce..29eb223d6a 100644 --- a/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.cpp +++ b/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.cpp @@ -439,6 +439,25 @@ AudioIODeviceType* AudioDeviceManager::getCurrentDeviceTypeObject() const return availableDeviceTypes.getFirst(); } +static void updateSetupChannels (AudioDeviceManager::AudioDeviceSetup& setup, int defaultNumIns, int defaultNumOuts) +{ + auto updateChannels = [](const String& deviceName, BigInteger& channels, int defaultNumChannels) + { + if (deviceName.isEmpty()) + { + channels.clear(); + } + else if (defaultNumChannels != -1) + { + channels.clear(); + channels.setRange (0, defaultNumChannels, true); + } + }; + + updateChannels (setup.inputDeviceName, setup.inputChannels, setup.useDefaultInputChannels ? defaultNumIns : -1); + updateChannels (setup.outputDeviceName, setup.outputChannels, setup.useDefaultOutputChannels ? defaultNumOuts : -1); +} + String AudioDeviceManager::setAudioDeviceSetup (const AudioDeviceSetup& newSetup, bool treatAsChosenDevice) { @@ -447,20 +466,7 @@ String AudioDeviceManager::setAudioDeviceSetup (const AudioDeviceSetup& newSetup if (newSetup == currentSetup && currentAudioDevice != nullptr) return {}; - if (! (newSetup == currentSetup)) - sendChangeMessage(); - - stopDevice(); - - if (! newSetup.useDefaultInputChannels) - numInputChansNeeded = newSetup.inputChannels.countNumberOfSetBits(); - - if (! newSetup.useDefaultOutputChannels) - numOutputChansNeeded = newSetup.outputChannels.countNumberOfSetBits(); - - auto* type = getCurrentDeviceTypeObject(); - - if (type == nullptr) + if (getCurrentDeviceTypeObject() == nullptr) { deleteCurrentDevice(); @@ -470,6 +476,11 @@ String AudioDeviceManager::setAudioDeviceSetup (const AudioDeviceSetup& newSetup return {}; } + stopDevice(); + + if (newSetup != currentSetup) + sendChangeMessage(); + String error; if (currentSetup.inputDeviceName != newSetup.inputDeviceName @@ -479,6 +490,8 @@ String AudioDeviceManager::setAudioDeviceSetup (const AudioDeviceSetup& newSetup deleteCurrentDevice(); scanDevicesIfNeeded(); + auto* type = getCurrentDeviceTypeObject(); + if (newSetup.outputDeviceName.isNotEmpty() && ! deviceListContains (type, false, newSetup.outputDeviceName)) return "No such device: " + newSetup.outputDeviceName; @@ -499,32 +512,16 @@ String AudioDeviceManager::setAudioDeviceSetup (const AudioDeviceSetup& newSetup deleteCurrentDevice(); return error; } - - if (newSetup.useDefaultInputChannels) - { - inputChannels.clear(); - inputChannels.setRange (0, numInputChansNeeded, true); - } - - if (newSetup.useDefaultOutputChannels) - { - outputChannels.clear(); - outputChannels.setRange (0, numOutputChansNeeded, true); - } - - if (newSetup.inputDeviceName.isEmpty()) inputChannels.clear(); - if (newSetup.outputDeviceName.isEmpty()) outputChannels.clear(); } - if (! newSetup.useDefaultInputChannels) - inputChannels = newSetup.inputChannels; - - if (! newSetup.useDefaultOutputChannels) - outputChannels = newSetup.outputChannels; - currentSetup = newSetup; - if (inputChannels.isZero() && outputChannels.isZero()) + if (! currentSetup.useDefaultInputChannels) numInputChansNeeded = currentSetup.inputChannels.countNumberOfSetBits(); + if (! currentSetup.useDefaultOutputChannels) numOutputChansNeeded = currentSetup.outputChannels.countNumberOfSetBits(); + + updateSetupChannels (currentSetup, numInputChansNeeded, numOutputChansNeeded); + + if (currentSetup.inputChannels.isZero() && currentSetup.outputChannels.isZero()) { if (treatAsChosenDevice) updateXml(); @@ -532,11 +529,11 @@ String AudioDeviceManager::setAudioDeviceSetup (const AudioDeviceSetup& newSetup return {}; } - currentSetup.sampleRate = chooseBestSampleRate (newSetup.sampleRate); - currentSetup.bufferSize = chooseBestBufferSize (newSetup.bufferSize); + currentSetup.sampleRate = chooseBestSampleRate (currentSetup.sampleRate); + currentSetup.bufferSize = chooseBestBufferSize (currentSetup.bufferSize); - error = currentAudioDevice->open (inputChannels, - outputChannels, + error = currentAudioDevice->open (currentSetup.inputChannels, + currentSetup.outputChannels, currentSetup.sampleRate, currentSetup.bufferSize); diff --git a/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.h b/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.h index 8914dbcc0d..2f33f9c3d6 100644 --- a/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.h +++ b/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.h @@ -471,7 +471,6 @@ private: Array callbacks; int numInputChansNeeded = 0, numOutputChansNeeded = 2; String preferredDeviceName, currentDeviceType; - BigInteger inputChannels, outputChannels; std::unique_ptr lastExplicitSettings; mutable bool listNeedsScanning = true; AudioBuffer tempBuffer;