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.

183 lines
6.5KB

  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. #if JUCE_MAC
  41. auto f = File::getSpecialLocation (File::currentExecutableFile)
  42. .getParentDirectory().getParentDirectory().getChildFile ("Resources");
  43. #else
  44. auto f = findExamplesDirectoryFromExecutable (File::getSpecialLocation (File::currentApplicationFile));
  45. #endif
  46. #if ! (JUCE_ANDROID || JUCE_IOS)
  47. if (f == File())
  48. {
  49. jassertfalse;
  50. return;
  51. }
  52. #endif
  53. getCategory (category).demos.push_back ({ f.getChildFile (filePath), constructorCallback, isHeavyweight });
  54. }
  55. File JUCEDemos::findExamplesDirectoryFromExecutable (File exec)
  56. {
  57. int numTries = 15;
  58. auto exampleDir = exec.getParentDirectory().getChildFile ("examples");
  59. if (exampleDir.exists())
  60. return exampleDir;
  61. while (exec.getFileName() != "examples" && numTries-- > 0)
  62. exec = exec.getParentDirectory();
  63. if (exec.getFileName() == "examples")
  64. return exec;
  65. return {};
  66. }
  67. //==============================================================================
  68. static String getCurrentDefaultAudioDeviceName (AudioDeviceManager& deviceManager, bool isInput)
  69. {
  70. auto* deviceType = deviceManager.getCurrentDeviceTypeObject();
  71. jassert (deviceType != nullptr);
  72. if (deviceType != nullptr)
  73. {
  74. auto deviceNames = deviceType->getDeviceNames();
  75. return deviceNames [deviceType->getDefaultDeviceIndex (isInput)];
  76. }
  77. return {};
  78. }
  79. // (returns a shared AudioDeviceManager object that all the demos can use)
  80. AudioDeviceManager& getSharedAudioDeviceManager (int numInputChannels, int numOutputChannels)
  81. {
  82. if (sharedAudioDeviceManager == nullptr)
  83. sharedAudioDeviceManager.reset (new AudioDeviceManager());
  84. auto* currentDevice = sharedAudioDeviceManager->getCurrentAudioDevice();
  85. if (numInputChannels < 0)
  86. numInputChannels = (currentDevice != nullptr ? currentDevice->getActiveInputChannels().countNumberOfSetBits() : 1);
  87. if (numOutputChannels < 0)
  88. numOutputChannels = (currentDevice != nullptr ? currentDevice->getActiveOutputChannels().countNumberOfSetBits() : 2);
  89. if (numInputChannels > 0 && ! RuntimePermissions::isGranted (RuntimePermissions::recordAudio))
  90. {
  91. RuntimePermissions::request (RuntimePermissions::recordAudio,
  92. [numInputChannels, numOutputChannels] (bool granted)
  93. {
  94. if (granted)
  95. getSharedAudioDeviceManager (numInputChannels, numOutputChannels);
  96. });
  97. numInputChannels = 0;
  98. }
  99. if (sharedAudioDeviceManager->getCurrentAudioDevice() != nullptr)
  100. {
  101. AudioDeviceManager::AudioDeviceSetup setup;
  102. sharedAudioDeviceManager->getAudioDeviceSetup (setup);
  103. auto numInputs = jmax (numInputChannels, setup.inputChannels.countNumberOfSetBits());
  104. auto numOutputs = jmax (numOutputChannels, setup.outputChannels.countNumberOfSetBits());
  105. auto oldInputs = setup.inputChannels.countNumberOfSetBits();
  106. auto oldOutputs = setup.outputChannels.countNumberOfSetBits();
  107. if (oldInputs != numInputs || oldOutputs != numOutputs)
  108. {
  109. if (oldInputs == 0 && oldOutputs == 0)
  110. {
  111. sharedAudioDeviceManager->initialise (numInputChannels, numOutputChannels, nullptr, true, {}, nullptr);
  112. }
  113. else
  114. {
  115. setup.useDefaultInputChannels = setup.useDefaultOutputChannels = false;
  116. setup.inputChannels.clear();
  117. setup.outputChannels.clear();
  118. setup.inputChannels.setRange (0, numInputs, true);
  119. setup.outputChannels.setRange (0, numOutputs, true);
  120. if (oldInputs == 0 && numInputs > 0 && setup.inputDeviceName.isEmpty())
  121. setup.inputDeviceName = getCurrentDefaultAudioDeviceName (*sharedAudioDeviceManager, true);
  122. if (oldOutputs == 0 && numOutputs > 0 && setup.outputDeviceName.isEmpty())
  123. setup.outputDeviceName = getCurrentDefaultAudioDeviceName (*sharedAudioDeviceManager, false);
  124. sharedAudioDeviceManager->setAudioDeviceSetup (setup, false);
  125. }
  126. }
  127. }
  128. else
  129. {
  130. sharedAudioDeviceManager->initialise (numInputChannels, numOutputChannels, nullptr, true, {}, nullptr);
  131. }
  132. return *sharedAudioDeviceManager;
  133. }
  134. //==============================================================================
  135. // need to split this into two files otherwise VS will fall over
  136. void registerDemos_One() noexcept;
  137. void registerDemos_Two() noexcept;
  138. void registerAllDemos() noexcept
  139. {
  140. registerDemos_One();
  141. registerDemos_Two();
  142. }