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.

344 lines
11KB

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