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.

185 lines
6.6KB

  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").getChildFile (filePath);
  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. std::unique_ptr<AudioDeviceManager> sharedAudioDeviceManager;
  69. static String getCurrentDefaultAudioDeviceName (AudioDeviceManager& deviceManager, bool isInput)
  70. {
  71. auto* deviceType = deviceManager.getCurrentDeviceTypeObject();
  72. jassert (deviceType != nullptr);
  73. if (deviceType != nullptr)
  74. {
  75. auto deviceNames = deviceType->getDeviceNames();
  76. return deviceNames [deviceType->getDefaultDeviceIndex (isInput)];
  77. }
  78. return {};
  79. }
  80. // (returns a shared AudioDeviceManager object that all the demos can use)
  81. AudioDeviceManager& getSharedAudioDeviceManager (int numInputChannels, int numOutputChannels)
  82. {
  83. if (sharedAudioDeviceManager == nullptr)
  84. sharedAudioDeviceManager.reset (new AudioDeviceManager());
  85. auto* currentDevice = sharedAudioDeviceManager->getCurrentAudioDevice();
  86. if (numInputChannels < 0)
  87. numInputChannels = (currentDevice != nullptr ? currentDevice->getActiveInputChannels().countNumberOfSetBits() : 1);
  88. if (numOutputChannels < 0)
  89. numOutputChannels = (currentDevice != nullptr ? currentDevice->getActiveOutputChannels().countNumberOfSetBits() : 2);
  90. if (numInputChannels > 0 && ! RuntimePermissions::isGranted (RuntimePermissions::recordAudio))
  91. {
  92. RuntimePermissions::request (RuntimePermissions::recordAudio,
  93. [numInputChannels, numOutputChannels] (bool granted)
  94. {
  95. if (granted)
  96. getSharedAudioDeviceManager (numInputChannels, numOutputChannels);
  97. });
  98. numInputChannels = 0;
  99. }
  100. if (sharedAudioDeviceManager->getCurrentAudioDevice() != nullptr)
  101. {
  102. AudioDeviceManager::AudioDeviceSetup setup;
  103. sharedAudioDeviceManager->getAudioDeviceSetup (setup);
  104. auto numInputs = jmax (numInputChannels, setup.inputChannels.countNumberOfSetBits());
  105. auto numOutputs = jmax (numOutputChannels, setup.outputChannels.countNumberOfSetBits());
  106. auto oldInputs = setup.inputChannels.countNumberOfSetBits();
  107. auto oldOutputs = setup.outputChannels.countNumberOfSetBits();
  108. if (oldInputs != numInputs || oldOutputs != numOutputs)
  109. {
  110. if (oldInputs == 0 && oldOutputs == 0)
  111. {
  112. sharedAudioDeviceManager->initialise (numInputChannels, numOutputChannels, nullptr, true, {}, nullptr);
  113. }
  114. else
  115. {
  116. setup.useDefaultInputChannels = setup.useDefaultOutputChannels = false;
  117. setup.inputChannels.clear();
  118. setup.outputChannels.clear();
  119. setup.inputChannels.setRange (0, numInputs, true);
  120. setup.outputChannels.setRange (0, numOutputs, true);
  121. if (oldInputs == 0 && numInputs > 0 && setup.inputDeviceName.isEmpty())
  122. setup.inputDeviceName = getCurrentDefaultAudioDeviceName (*sharedAudioDeviceManager, true);
  123. if (oldOutputs == 0 && numOutputs > 0 && setup.outputDeviceName.isEmpty())
  124. setup.outputDeviceName = getCurrentDefaultAudioDeviceName (*sharedAudioDeviceManager, false);
  125. sharedAudioDeviceManager->setAudioDeviceSetup (setup, false);
  126. }
  127. }
  128. }
  129. else
  130. {
  131. sharedAudioDeviceManager->initialise (numInputChannels, numOutputChannels, nullptr, true, {}, nullptr);
  132. }
  133. return *sharedAudioDeviceManager;
  134. }
  135. //==============================================================================
  136. // need to split this into two files otherwise VS will fall over
  137. void registerDemos_One() noexcept;
  138. void registerDemos_Two() noexcept;
  139. void registerAllDemos() noexcept
  140. {
  141. registerDemos_One();
  142. registerDemos_Two();
  143. }