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.

177 lines
6.3KB

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