The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
6.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "../JuceLibraryCode/JuceHeader.h"
  20. #include "../../../Assets/DemoUtilities.h"
  21. #include "JUCEDemos.h"
  22. //==============================================================================
  23. std::vector<JUCEDemos::DemoCategory>& JUCEDemos::getCategories()
  24. {
  25. static std::vector<DemoCategory> categories;
  26. return categories;
  27. }
  28. JUCEDemos::DemoCategory& JUCEDemos::getCategory (const String& name)
  29. {
  30. auto& categories = getCategories();
  31. for (auto& c : categories)
  32. if (c.name == name)
  33. return c;
  34. std::vector<FileAndCallback> fc;
  35. categories.push_back ({ name, fc });
  36. return categories.back();
  37. }
  38. void JUCEDemos::registerDemo (std::function<Component*()> constructorCallback, const String& filePath, const String& category, bool isHeavyweight)
  39. {
  40. auto f = findExamplesDirectoryFromExecutable (File::getSpecialLocation (File::SpecialLocationType::currentExecutableFile));
  41. #if ! (JUCE_ANDROID || JUCE_IOS)
  42. if (f == File())
  43. {
  44. jassertfalse;
  45. return;
  46. }
  47. #endif
  48. getCategory (category).demos.push_back ({ f.getChildFile (filePath), constructorCallback, isHeavyweight });
  49. }
  50. File JUCEDemos::findExamplesDirectoryFromExecutable (File exec)
  51. {
  52. int numTries = 15;
  53. while (exec.getFileName() != "examples" && numTries-- > 0)
  54. exec = exec.getParentDirectory();
  55. if (exec.getFileName() == "examples")
  56. return exec;
  57. return {};
  58. }
  59. //==============================================================================
  60. ScopedPointer<AudioDeviceManager> sharedAudioDeviceManager;
  61. static String getCurrentDefaultAudioDeviceName (AudioDeviceManager& deviceManager, bool isInput)
  62. {
  63. auto* deviceType = deviceManager.getCurrentDeviceTypeObject();
  64. jassert (deviceType != nullptr);
  65. if (deviceType != nullptr)
  66. {
  67. auto deviceNames = deviceType->getDeviceNames();
  68. return deviceNames [deviceType->getDefaultDeviceIndex (isInput)];
  69. }
  70. return {};
  71. }
  72. // (returns a shared AudioDeviceManager object that all the demos can use)
  73. AudioDeviceManager& getSharedAudioDeviceManager (int numInputChannels, int numOutputChannels)
  74. {
  75. if (sharedAudioDeviceManager == nullptr)
  76. sharedAudioDeviceManager = new AudioDeviceManager();
  77. auto* currentDevice = sharedAudioDeviceManager->getCurrentAudioDevice();
  78. if (numInputChannels < 0)
  79. numInputChannels = (currentDevice != nullptr ? currentDevice->getActiveInputChannels().countNumberOfSetBits() : 1);
  80. if (numOutputChannels < 0)
  81. numOutputChannels = (currentDevice != nullptr ? currentDevice->getActiveOutputChannels().countNumberOfSetBits() : 2);
  82. if (numInputChannels > 0 && ! RuntimePermissions::isGranted (RuntimePermissions::recordAudio))
  83. {
  84. RuntimePermissions::request (RuntimePermissions::recordAudio,
  85. [numInputChannels, numOutputChannels] (bool granted)
  86. {
  87. if (granted)
  88. getSharedAudioDeviceManager (numInputChannels, numOutputChannels);
  89. });
  90. numInputChannels = 0;
  91. }
  92. if (sharedAudioDeviceManager->getCurrentAudioDevice() != nullptr)
  93. {
  94. AudioDeviceManager::AudioDeviceSetup setup;
  95. sharedAudioDeviceManager->getAudioDeviceSetup (setup);
  96. auto numInputs = jmax (numInputChannels, setup.inputChannels.countNumberOfSetBits());
  97. auto numOutputs = jmax (numOutputChannels, setup.outputChannels.countNumberOfSetBits());
  98. auto oldInputs = setup.inputChannels.countNumberOfSetBits();
  99. auto oldOutputs = setup.outputChannels.countNumberOfSetBits();
  100. if (oldInputs != numInputs || oldOutputs != numOutputs)
  101. {
  102. if (oldInputs == 0 && oldOutputs == 0)
  103. {
  104. sharedAudioDeviceManager->initialise (numInputChannels, numOutputChannels, nullptr, true, {}, nullptr);
  105. }
  106. else
  107. {
  108. setup.useDefaultInputChannels = setup.useDefaultOutputChannels = false;
  109. setup.inputChannels.clear();
  110. setup.outputChannels.clear();
  111. setup.inputChannels.setRange (0, numInputs, true);
  112. setup.outputChannels.setRange (0, numOutputs, true);
  113. if (oldInputs == 0 && numInputs > 0 && setup.inputDeviceName.isEmpty())
  114. setup.inputDeviceName = getCurrentDefaultAudioDeviceName (*sharedAudioDeviceManager, true);
  115. if (oldOutputs == 0 && numOutputs > 0 && setup.outputDeviceName.isEmpty())
  116. setup.outputDeviceName = getCurrentDefaultAudioDeviceName (*sharedAudioDeviceManager, false);
  117. sharedAudioDeviceManager->setAudioDeviceSetup (setup, false);
  118. }
  119. }
  120. }
  121. else
  122. {
  123. sharedAudioDeviceManager->initialise (numInputChannels, numOutputChannels, nullptr, true, {}, nullptr);
  124. }
  125. return *sharedAudioDeviceManager;
  126. }
  127. //==============================================================================
  128. // need to split this into two files otherwise VS will fall over
  129. void registerDemos_One() noexcept;
  130. void registerDemos_Two() noexcept;
  131. void registerAllDemos() noexcept
  132. {
  133. registerDemos_One();
  134. registerDemos_Two();
  135. }