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.

351 lines
11KB

  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. #pragma once
  19. #include "../Plugins/IOConfigurationWindow.h"
  20. #include "../Plugins/ARAPlugin.h"
  21. inline String getFormatSuffix (const AudioProcessor* plugin)
  22. {
  23. const auto format = [plugin]()
  24. {
  25. if (auto* instance = dynamic_cast<const AudioPluginInstance*> (plugin))
  26. return instance->getPluginDescription().pluginFormatName;
  27. return String();
  28. }();
  29. return format.isNotEmpty() ? (" (" + format + ")") : format;
  30. }
  31. class PluginGraph;
  32. /**
  33. A window that shows a log of parameter change messages sent by the plugin.
  34. */
  35. class PluginDebugWindow : public AudioProcessorEditor,
  36. public AudioProcessorParameter::Listener,
  37. public ListBoxModel,
  38. public AsyncUpdater
  39. {
  40. public:
  41. PluginDebugWindow (AudioProcessor& proc)
  42. : AudioProcessorEditor (proc), audioProc (proc)
  43. {
  44. setSize (500, 200);
  45. addAndMakeVisible (list);
  46. for (auto* p : audioProc.getParameters())
  47. p->addListener (this);
  48. log.add ("Parameter debug log started");
  49. }
  50. ~PluginDebugWindow() override
  51. {
  52. for (auto* p : audioProc.getParameters())
  53. p->removeListener (this);
  54. }
  55. void parameterValueChanged (int parameterIndex, float newValue) override
  56. {
  57. auto* param = audioProc.getParameters()[parameterIndex];
  58. auto value = param->getCurrentValueAsText().quoted() + " (" + String (newValue, 4) + ")";
  59. appendToLog ("parameter change", *param, value);
  60. }
  61. void parameterGestureChanged (int parameterIndex, bool gestureIsStarting) override
  62. {
  63. auto* param = audioProc.getParameters()[parameterIndex];
  64. appendToLog ("gesture", *param, gestureIsStarting ? "start" : "end");
  65. }
  66. private:
  67. void appendToLog (StringRef action, AudioProcessorParameter& param, StringRef value)
  68. {
  69. String entry (action + " " + param.getName (30).quoted() + " [" + String (param.getParameterIndex()) + "]: " + value);
  70. {
  71. ScopedLock lock (pendingLogLock);
  72. pendingLogEntries.add (entry);
  73. }
  74. triggerAsyncUpdate();
  75. }
  76. void resized() override
  77. {
  78. list.setBounds(getLocalBounds());
  79. }
  80. int getNumRows() override
  81. {
  82. return log.size();
  83. }
  84. void paintListBoxItem (int rowNumber, Graphics& g, int width, int height, bool) override
  85. {
  86. g.setColour (getLookAndFeel().findColour (TextEditor::textColourId));
  87. if (isPositiveAndBelow (rowNumber, log.size()))
  88. g.drawText (log[rowNumber], Rectangle<int> { 0, 0, width, height }, Justification::left, true);
  89. }
  90. void handleAsyncUpdate() override
  91. {
  92. if (log.size() > logSizeTrimThreshold)
  93. log.removeRange (0, log.size() - maxLogSize);
  94. {
  95. ScopedLock lock (pendingLogLock);
  96. log.addArray (pendingLogEntries);
  97. pendingLogEntries.clear();
  98. }
  99. list.updateContent();
  100. list.scrollToEnsureRowIsOnscreen (log.size() - 1);
  101. }
  102. constexpr static const int maxLogSize = 300;
  103. constexpr static const int logSizeTrimThreshold = 400;
  104. ListBox list { "Log", this };
  105. StringArray log;
  106. StringArray pendingLogEntries;
  107. CriticalSection pendingLogLock;
  108. AudioProcessor& audioProc;
  109. };
  110. //==============================================================================
  111. /**
  112. A desktop window containing a plugin's GUI.
  113. */
  114. class PluginWindow : public DocumentWindow
  115. {
  116. public:
  117. enum class Type
  118. {
  119. normal = 0,
  120. generic,
  121. programs,
  122. audioIO,
  123. debug,
  124. araHost,
  125. numTypes
  126. };
  127. PluginWindow (AudioProcessorGraph::Node* n, Type t, OwnedArray<PluginWindow>& windowList)
  128. : DocumentWindow (n->getProcessor()->getName() + getFormatSuffix (n->getProcessor()),
  129. LookAndFeel::getDefaultLookAndFeel().findColour (ResizableWindow::backgroundColourId),
  130. DocumentWindow::minimiseButton | DocumentWindow::closeButton),
  131. activeWindowList (windowList),
  132. node (n), type (t)
  133. {
  134. setSize (400, 300);
  135. if (auto* ui = createProcessorEditor (*node->getProcessor(), type))
  136. {
  137. setContentOwned (ui, true);
  138. setResizable (ui->isResizable(), false);
  139. }
  140. #if JUCE_IOS || JUCE_ANDROID
  141. auto screenBounds = Desktop::getInstance().getDisplays().getTotalBounds (true).toFloat();
  142. auto scaleFactor = jmin ((screenBounds.getWidth() - 50) / getWidth(), (screenBounds.getHeight() - 50) / getHeight());
  143. if (scaleFactor < 1.0f)
  144. setSize ((int) (getWidth() * scaleFactor), (int) (getHeight() * scaleFactor));
  145. setTopLeftPosition (20, 20);
  146. #else
  147. setTopLeftPosition (node->properties.getWithDefault (getLastXProp (type), Random::getSystemRandom().nextInt (500)),
  148. node->properties.getWithDefault (getLastYProp (type), Random::getSystemRandom().nextInt (500)));
  149. #endif
  150. node->properties.set (getOpenProp (type), true);
  151. setVisible (true);
  152. }
  153. ~PluginWindow() override
  154. {
  155. clearContentComponent();
  156. }
  157. void moved() override
  158. {
  159. node->properties.set (getLastXProp (type), getX());
  160. node->properties.set (getLastYProp (type), getY());
  161. }
  162. void closeButtonPressed() override
  163. {
  164. node->properties.set (getOpenProp (type), false);
  165. activeWindowList.removeObject (this);
  166. }
  167. static String getLastXProp (Type type) { return "uiLastX_" + getTypeName (type); }
  168. static String getLastYProp (Type type) { return "uiLastY_" + getTypeName (type); }
  169. static String getOpenProp (Type type) { return "uiopen_" + getTypeName (type); }
  170. OwnedArray<PluginWindow>& activeWindowList;
  171. const AudioProcessorGraph::Node::Ptr node;
  172. const Type type;
  173. BorderSize<int> getBorderThickness() override
  174. {
  175. #if JUCE_IOS || JUCE_ANDROID
  176. const int border = 10;
  177. return { border, border, border, border };
  178. #else
  179. return DocumentWindow::getBorderThickness();
  180. #endif
  181. }
  182. private:
  183. float getDesktopScaleFactor() const override { return 1.0f; }
  184. static AudioProcessorEditor* createProcessorEditor (AudioProcessor& processor,
  185. PluginWindow::Type type)
  186. {
  187. if (type == PluginWindow::Type::normal)
  188. {
  189. if (processor.hasEditor())
  190. if (auto* ui = processor.createEditorIfNeeded())
  191. return ui;
  192. type = PluginWindow::Type::generic;
  193. }
  194. if (type == PluginWindow::Type::araHost)
  195. {
  196. #if JUCE_PLUGINHOST_ARA && (JUCE_MAC || JUCE_WINDOWS)
  197. if (auto* araPluginInstanceWrapper = dynamic_cast<ARAPluginInstanceWrapper*> (&processor))
  198. if (auto* ui = araPluginInstanceWrapper->createARAHostEditor())
  199. return ui;
  200. #endif
  201. return {};
  202. }
  203. if (type == PluginWindow::Type::generic) return new GenericAudioProcessorEditor (processor);
  204. if (type == PluginWindow::Type::programs) return new ProgramAudioProcessorEditor (processor);
  205. if (type == PluginWindow::Type::audioIO) return new IOConfigurationWindow (processor);
  206. if (type == PluginWindow::Type::debug) return new PluginDebugWindow (processor);
  207. jassertfalse;
  208. return {};
  209. }
  210. static String getTypeName (Type type)
  211. {
  212. switch (type)
  213. {
  214. case Type::normal: return "Normal";
  215. case Type::generic: return "Generic";
  216. case Type::programs: return "Programs";
  217. case Type::audioIO: return "IO";
  218. case Type::debug: return "Debug";
  219. case Type::araHost: return "ARAHost";
  220. case Type::numTypes:
  221. default: return {};
  222. }
  223. }
  224. //==============================================================================
  225. struct ProgramAudioProcessorEditor : public AudioProcessorEditor
  226. {
  227. ProgramAudioProcessorEditor (AudioProcessor& p) : AudioProcessorEditor (p)
  228. {
  229. setOpaque (true);
  230. addAndMakeVisible (panel);
  231. Array<PropertyComponent*> programs;
  232. auto numPrograms = p.getNumPrograms();
  233. int totalHeight = 0;
  234. for (int i = 0; i < numPrograms; ++i)
  235. {
  236. auto name = p.getProgramName (i).trim();
  237. if (name.isEmpty())
  238. name = "Unnamed";
  239. auto pc = new PropertyComp (name, p);
  240. programs.add (pc);
  241. totalHeight += pc->getPreferredHeight();
  242. }
  243. panel.addProperties (programs);
  244. setSize (400, jlimit (25, 400, totalHeight));
  245. }
  246. void paint (Graphics& g) override
  247. {
  248. g.fillAll (Colours::grey);
  249. }
  250. void resized() override
  251. {
  252. panel.setBounds (getLocalBounds());
  253. }
  254. private:
  255. struct PropertyComp : public PropertyComponent,
  256. private AudioProcessorListener
  257. {
  258. PropertyComp (const String& name, AudioProcessor& p) : PropertyComponent (name), owner (p)
  259. {
  260. owner.addListener (this);
  261. }
  262. ~PropertyComp() override
  263. {
  264. owner.removeListener (this);
  265. }
  266. void refresh() override {}
  267. void audioProcessorChanged (AudioProcessor*, const ChangeDetails&) override {}
  268. void audioProcessorParameterChanged (AudioProcessor*, int, float) override {}
  269. AudioProcessor& owner;
  270. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PropertyComp)
  271. };
  272. PropertyPanel panel;
  273. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProgramAudioProcessorEditor)
  274. };
  275. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PluginWindow)
  276. };