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.

174 lines
6.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. #include <JuceHeader.h>
  14. #include "../../../Assets/DemoUtilities.h"
  15. #include "JUCEDemos.h"
  16. //==============================================================================
  17. std::vector<JUCEDemos::DemoCategory>& JUCEDemos::getCategories()
  18. {
  19. static std::vector<DemoCategory> categories;
  20. return categories;
  21. }
  22. JUCEDemos::DemoCategory& JUCEDemos::getCategory (const String& name)
  23. {
  24. auto& categories = getCategories();
  25. for (auto& c : categories)
  26. if (c.name == name)
  27. return c;
  28. std::vector<FileAndCallback> fc;
  29. categories.push_back ({ name, fc });
  30. return categories.back();
  31. }
  32. void JUCEDemos::registerDemo (std::function<Component*()> constructorCallback, const String& filePath, const String& category, bool isHeavyweight)
  33. {
  34. #if JUCE_MAC
  35. auto f = File::getSpecialLocation (File::currentExecutableFile)
  36. .getParentDirectory().getParentDirectory().getChildFile ("Resources");
  37. #else
  38. auto f = findExamplesDirectoryFromExecutable (File::getSpecialLocation (File::currentApplicationFile));
  39. #endif
  40. #if ! (JUCE_ANDROID || JUCE_IOS)
  41. if (f == File())
  42. {
  43. jassertfalse;
  44. return;
  45. }
  46. #endif
  47. getCategory (category).demos.push_back ({ f.getChildFile (filePath), constructorCallback, isHeavyweight });
  48. }
  49. File JUCEDemos::findExamplesDirectoryFromExecutable (File exec)
  50. {
  51. int numTries = 15;
  52. auto exampleDir = exec.getParentDirectory().getChildFile ("examples");
  53. if (exampleDir.exists())
  54. return exampleDir;
  55. while (exec.getFileName() != "examples" && numTries-- > 0)
  56. exec = exec.getParentDirectory();
  57. if (exec.getFileName() == "examples")
  58. return exec;
  59. return {};
  60. }
  61. //==============================================================================
  62. static String getCurrentDefaultAudioDeviceName (AudioDeviceManager& deviceManager, bool isInput)
  63. {
  64. auto* deviceType = deviceManager.getCurrentDeviceTypeObject();
  65. jassert (deviceType != nullptr);
  66. if (deviceType != nullptr)
  67. {
  68. auto deviceNames = deviceType->getDeviceNames();
  69. return deviceNames [deviceType->getDefaultDeviceIndex (isInput)];
  70. }
  71. return {};
  72. }
  73. // (returns a shared AudioDeviceManager object that all the demos can use)
  74. AudioDeviceManager& getSharedAudioDeviceManager (int numInputChannels, int numOutputChannels)
  75. {
  76. if (sharedAudioDeviceManager == nullptr)
  77. sharedAudioDeviceManager.reset (new AudioDeviceManager());
  78. auto* currentDevice = sharedAudioDeviceManager->getCurrentAudioDevice();
  79. if (numInputChannels < 0)
  80. numInputChannels = (currentDevice != nullptr ? currentDevice->getActiveInputChannels().countNumberOfSetBits() : 1);
  81. if (numOutputChannels < 0)
  82. numOutputChannels = (currentDevice != nullptr ? currentDevice->getActiveOutputChannels().countNumberOfSetBits() : 2);
  83. if (numInputChannels > 0 && ! RuntimePermissions::isGranted (RuntimePermissions::recordAudio))
  84. {
  85. RuntimePermissions::request (RuntimePermissions::recordAudio,
  86. [numInputChannels, numOutputChannels] (bool granted)
  87. {
  88. if (granted)
  89. getSharedAudioDeviceManager (numInputChannels, numOutputChannels);
  90. });
  91. numInputChannels = 0;
  92. }
  93. if (sharedAudioDeviceManager->getCurrentAudioDevice() != nullptr)
  94. {
  95. auto setup = sharedAudioDeviceManager->getAudioDeviceSetup();
  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. }