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.

180 lines
6.4KB

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