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.

484 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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 "../UI/MainHostWindow.h"
  20. #include "PluginGraph.h"
  21. #include "InternalPlugins.h"
  22. #include "../UI/GraphEditorPanel.h"
  23. //==============================================================================
  24. PluginGraph::PluginGraph (AudioPluginFormatManager& fm)
  25. : FileBasedDocument (getFilenameSuffix(),
  26. getFilenameWildcard(),
  27. "Load a graph",
  28. "Save a graph"),
  29. formatManager (fm)
  30. {
  31. newDocument();
  32. graph.addListener (this);
  33. }
  34. PluginGraph::~PluginGraph()
  35. {
  36. graph.removeListener (this);
  37. graph.removeChangeListener (this);
  38. graph.clear();
  39. }
  40. PluginGraph::NodeID PluginGraph::getNextUID() noexcept
  41. {
  42. return PluginGraph::NodeID (++(lastUID.uid));
  43. }
  44. //==============================================================================
  45. void PluginGraph::changeListenerCallback (ChangeBroadcaster*)
  46. {
  47. changed();
  48. for (int i = activePluginWindows.size(); --i >= 0;)
  49. if (! graph.getNodes().contains (activePluginWindows.getUnchecked(i)->node))
  50. activePluginWindows.remove (i);
  51. }
  52. AudioProcessorGraph::Node::Ptr PluginGraph::getNodeForName (const String& name) const
  53. {
  54. for (auto* node : graph.getNodes())
  55. if (auto p = node->getProcessor())
  56. if (p->getName().equalsIgnoreCase (name))
  57. return node;
  58. return nullptr;
  59. }
  60. void PluginGraph::addPlugin (const PluginDescription& desc, Point<double> pos)
  61. {
  62. formatManager.createPluginInstanceAsync (desc,
  63. graph.getSampleRate(),
  64. graph.getBlockSize(),
  65. [this, pos] (std::unique_ptr<AudioPluginInstance> instance, const String& error)
  66. {
  67. addPluginCallback (std::move (instance), error, pos);
  68. });
  69. }
  70. void PluginGraph::addPluginCallback (std::unique_ptr<AudioPluginInstance> instance,
  71. const String& error, Point<double> pos)
  72. {
  73. if (instance == nullptr)
  74. {
  75. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  76. TRANS("Couldn't create plugin"),
  77. error);
  78. }
  79. else
  80. {
  81. instance->enableAllBuses();
  82. if (auto node = graph.addNode (std::move (instance)))
  83. {
  84. node->properties.set ("x", pos.x);
  85. node->properties.set ("y", pos.y);
  86. changed();
  87. }
  88. }
  89. }
  90. void PluginGraph::setNodePosition (NodeID nodeID, Point<double> pos)
  91. {
  92. if (auto* n = graph.getNodeForId (nodeID))
  93. {
  94. n->properties.set ("x", jlimit (0.0, 1.0, pos.x));
  95. n->properties.set ("y", jlimit (0.0, 1.0, pos.y));
  96. }
  97. }
  98. Point<double> PluginGraph::getNodePosition (NodeID nodeID) const
  99. {
  100. if (auto* n = graph.getNodeForId (nodeID))
  101. return { static_cast<double> (n->properties ["x"]),
  102. static_cast<double> (n->properties ["y"]) };
  103. return {};
  104. }
  105. //==============================================================================
  106. void PluginGraph::clear()
  107. {
  108. closeAnyOpenPluginWindows();
  109. graph.clear();
  110. changed();
  111. }
  112. PluginWindow* PluginGraph::getOrCreateWindowFor (AudioProcessorGraph::Node* node, PluginWindow::Type type)
  113. {
  114. jassert (node != nullptr);
  115. #if JUCE_IOS || JUCE_ANDROID
  116. closeAnyOpenPluginWindows();
  117. #else
  118. for (auto* w : activePluginWindows)
  119. if (w->node.get() == node && w->type == type)
  120. return w;
  121. #endif
  122. if (auto* processor = node->getProcessor())
  123. {
  124. if (auto* plugin = dynamic_cast<AudioPluginInstance*> (processor))
  125. {
  126. auto description = plugin->getPluginDescription();
  127. if (! plugin->hasEditor() && description.pluginFormatName == "Internal")
  128. {
  129. getCommandManager().invokeDirectly (CommandIDs::showAudioSettings, false);
  130. return nullptr;
  131. }
  132. }
  133. #if JUCE_WINDOWS && JUCE_WIN_PER_MONITOR_DPI_AWARE
  134. if (! node->properties["DPIAware"]
  135. && ! node->getProcessor()->getName().contains ("Kontakt")) // Kontakt doesn't behave correctly in DPI unaware mode...
  136. {
  137. ScopedDPIAwarenessDisabler disableDPIAwareness;
  138. return activePluginWindows.add (new PluginWindow (node, type, activePluginWindows));
  139. }
  140. #endif
  141. return activePluginWindows.add (new PluginWindow (node, type, activePluginWindows));
  142. }
  143. return nullptr;
  144. }
  145. bool PluginGraph::closeAnyOpenPluginWindows()
  146. {
  147. bool wasEmpty = activePluginWindows.isEmpty();
  148. activePluginWindows.clear();
  149. return ! wasEmpty;
  150. }
  151. //==============================================================================
  152. String PluginGraph::getDocumentTitle()
  153. {
  154. if (! getFile().exists())
  155. return "Unnamed";
  156. return getFile().getFileNameWithoutExtension();
  157. }
  158. void PluginGraph::newDocument()
  159. {
  160. clear();
  161. setFile ({});
  162. graph.removeChangeListener (this);
  163. InternalPluginFormat internalFormat;
  164. jassert (internalFormat.getAllTypes().size() > 3);
  165. addPlugin (internalFormat.getAllTypes()[0], { 0.5, 0.1 });
  166. addPlugin (internalFormat.getAllTypes()[1], { 0.25, 0.1 });
  167. addPlugin (internalFormat.getAllTypes()[2], { 0.5, 0.9 });
  168. addPlugin (internalFormat.getAllTypes()[3], { 0.25, 0.9 });
  169. MessageManager::callAsync ([this]
  170. {
  171. setChangedFlag (false);
  172. graph.addChangeListener (this);
  173. });
  174. }
  175. Result PluginGraph::loadDocument (const File& file)
  176. {
  177. if (auto xml = parseXMLIfTagMatches (file, "FILTERGRAPH"))
  178. {
  179. graph.removeChangeListener (this);
  180. restoreFromXml (*xml);
  181. MessageManager::callAsync ([this]
  182. {
  183. setChangedFlag (false);
  184. graph.addChangeListener (this);
  185. });
  186. return Result::ok();
  187. }
  188. return Result::fail ("Not a valid graph file");
  189. }
  190. Result PluginGraph::saveDocument (const File& file)
  191. {
  192. auto xml = createXml();
  193. if (! xml->writeTo (file, {}))
  194. return Result::fail ("Couldn't write to the file");
  195. return Result::ok();
  196. }
  197. File PluginGraph::getLastDocumentOpened()
  198. {
  199. RecentlyOpenedFilesList recentFiles;
  200. recentFiles.restoreFromString (getAppProperties().getUserSettings()
  201. ->getValue ("recentFilterGraphFiles"));
  202. return recentFiles.getFile (0);
  203. }
  204. void PluginGraph::setLastDocumentOpened (const File& file)
  205. {
  206. RecentlyOpenedFilesList recentFiles;
  207. recentFiles.restoreFromString (getAppProperties().getUserSettings()
  208. ->getValue ("recentFilterGraphFiles"));
  209. recentFiles.addFile (file);
  210. getAppProperties().getUserSettings()
  211. ->setValue ("recentFilterGraphFiles", recentFiles.toString());
  212. }
  213. //==============================================================================
  214. static void readBusLayoutFromXml (AudioProcessor::BusesLayout& busesLayout, AudioProcessor& plugin,
  215. const XmlElement& xml, bool isInput)
  216. {
  217. auto& targetBuses = (isInput ? busesLayout.inputBuses
  218. : busesLayout.outputBuses);
  219. int maxNumBuses = 0;
  220. if (auto* buses = xml.getChildByName (isInput ? "INPUTS" : "OUTPUTS"))
  221. {
  222. forEachXmlChildElementWithTagName (*buses, e, "BUS")
  223. {
  224. const int busIdx = e->getIntAttribute ("index");
  225. maxNumBuses = jmax (maxNumBuses, busIdx + 1);
  226. // the number of buses on busesLayout may not be in sync with the plugin after adding buses
  227. // because adding an input bus could also add an output bus
  228. for (int actualIdx = plugin.getBusCount (isInput) - 1; actualIdx < busIdx; ++actualIdx)
  229. if (! plugin.addBus (isInput))
  230. return;
  231. for (int actualIdx = targetBuses.size() - 1; actualIdx < busIdx; ++actualIdx)
  232. targetBuses.add (plugin.getChannelLayoutOfBus (isInput, busIdx));
  233. auto layout = e->getStringAttribute ("layout");
  234. if (layout.isNotEmpty())
  235. targetBuses.getReference (busIdx) = AudioChannelSet::fromAbbreviatedString (layout);
  236. }
  237. }
  238. // if the plugin has more buses than specified in the xml, then try to remove them!
  239. while (maxNumBuses < targetBuses.size())
  240. {
  241. if (! plugin.removeBus (isInput))
  242. return;
  243. targetBuses.removeLast();
  244. }
  245. }
  246. //==============================================================================
  247. static XmlElement* createBusLayoutXml (const AudioProcessor::BusesLayout& layout, const bool isInput)
  248. {
  249. auto& buses = isInput ? layout.inputBuses
  250. : layout.outputBuses;
  251. auto* xml = new XmlElement (isInput ? "INPUTS" : "OUTPUTS");
  252. for (int busIdx = 0; busIdx < buses.size(); ++busIdx)
  253. {
  254. auto& set = buses.getReference (busIdx);
  255. auto* bus = xml->createNewChildElement ("BUS");
  256. bus->setAttribute ("index", busIdx);
  257. bus->setAttribute ("layout", set.isDisabled() ? "disabled" : set.getSpeakerArrangementAsString());
  258. }
  259. return xml;
  260. }
  261. static XmlElement* createNodeXml (AudioProcessorGraph::Node* const node) noexcept
  262. {
  263. if (auto* plugin = dynamic_cast<AudioPluginInstance*> (node->getProcessor()))
  264. {
  265. auto e = new XmlElement ("FILTER");
  266. e->setAttribute ("uid", (int) node->nodeID.uid);
  267. e->setAttribute ("x", node->properties ["x"].toString());
  268. e->setAttribute ("y", node->properties ["y"].toString());
  269. #if JUCE_WINDOWS && JUCE_WIN_PER_MONITOR_DPI_AWARE
  270. e->setAttribute ("DPIAware", node->properties["DPIAware"].toString());
  271. #endif
  272. for (int i = 0; i < (int) PluginWindow::Type::numTypes; ++i)
  273. {
  274. auto type = (PluginWindow::Type) i;
  275. if (node->properties.contains (PluginWindow::getOpenProp (type)))
  276. {
  277. e->setAttribute (PluginWindow::getLastXProp (type), node->properties[PluginWindow::getLastXProp (type)].toString());
  278. e->setAttribute (PluginWindow::getLastYProp (type), node->properties[PluginWindow::getLastYProp (type)].toString());
  279. e->setAttribute (PluginWindow::getOpenProp (type), node->properties[PluginWindow::getOpenProp (type)].toString());
  280. }
  281. }
  282. {
  283. PluginDescription pd;
  284. plugin->fillInPluginDescription (pd);
  285. e->addChildElement (pd.createXml().release());
  286. }
  287. {
  288. MemoryBlock m;
  289. node->getProcessor()->getStateInformation (m);
  290. e->createNewChildElement ("STATE")->addTextElement (m.toBase64Encoding());
  291. }
  292. auto layout = plugin->getBusesLayout();
  293. auto layouts = e->createNewChildElement ("LAYOUT");
  294. layouts->addChildElement (createBusLayoutXml (layout, true));
  295. layouts->addChildElement (createBusLayoutXml (layout, false));
  296. return e;
  297. }
  298. jassertfalse;
  299. return nullptr;
  300. }
  301. void PluginGraph::createNodeFromXml (const XmlElement& xml)
  302. {
  303. PluginDescription pd;
  304. forEachXmlChildElement (xml, e)
  305. {
  306. if (pd.loadFromXml (*e))
  307. break;
  308. }
  309. String errorMessage;
  310. if (auto instance = formatManager.createPluginInstance (pd, graph.getSampleRate(),
  311. graph.getBlockSize(), errorMessage))
  312. {
  313. if (auto* layoutEntity = xml.getChildByName ("LAYOUT"))
  314. {
  315. auto layout = instance->getBusesLayout();
  316. readBusLayoutFromXml (layout, *instance, *layoutEntity, true);
  317. readBusLayoutFromXml (layout, *instance, *layoutEntity, false);
  318. instance->setBusesLayout (layout);
  319. }
  320. if (auto node = graph.addNode (std::move (instance), NodeID ((uint32) xml.getIntAttribute ("uid"))))
  321. {
  322. if (auto* state = xml.getChildByName ("STATE"))
  323. {
  324. MemoryBlock m;
  325. m.fromBase64Encoding (state->getAllSubText());
  326. node->getProcessor()->setStateInformation (m.getData(), (int) m.getSize());
  327. }
  328. node->properties.set ("x", xml.getDoubleAttribute ("x"));
  329. node->properties.set ("y", xml.getDoubleAttribute ("y"));
  330. #if JUCE_WINDOWS && JUCE_WIN_PER_MONITOR_DPI_AWARE
  331. node->properties.set ("DPIAware", xml.getDoubleAttribute ("DPIAware"));
  332. #endif
  333. for (int i = 0; i < (int) PluginWindow::Type::numTypes; ++i)
  334. {
  335. auto type = (PluginWindow::Type) i;
  336. if (xml.hasAttribute (PluginWindow::getOpenProp (type)))
  337. {
  338. node->properties.set (PluginWindow::getLastXProp (type), xml.getIntAttribute (PluginWindow::getLastXProp (type)));
  339. node->properties.set (PluginWindow::getLastYProp (type), xml.getIntAttribute (PluginWindow::getLastYProp (type)));
  340. node->properties.set (PluginWindow::getOpenProp (type), xml.getIntAttribute (PluginWindow::getOpenProp (type)));
  341. if (node->properties[PluginWindow::getOpenProp (type)])
  342. {
  343. jassert (node->getProcessor() != nullptr);
  344. if (auto w = getOrCreateWindowFor (node, type))
  345. w->toFront (true);
  346. }
  347. }
  348. }
  349. }
  350. }
  351. }
  352. std::unique_ptr<XmlElement> PluginGraph::createXml() const
  353. {
  354. auto xml = std::make_unique<XmlElement> ("FILTERGRAPH");
  355. for (auto* node : graph.getNodes())
  356. xml->addChildElement (createNodeXml (node));
  357. for (auto& connection : graph.getConnections())
  358. {
  359. auto e = xml->createNewChildElement ("CONNECTION");
  360. e->setAttribute ("srcFilter", (int) connection.source.nodeID.uid);
  361. e->setAttribute ("srcChannel", connection.source.channelIndex);
  362. e->setAttribute ("dstFilter", (int) connection.destination.nodeID.uid);
  363. e->setAttribute ("dstChannel", connection.destination.channelIndex);
  364. }
  365. return xml;
  366. }
  367. void PluginGraph::restoreFromXml (const XmlElement& xml)
  368. {
  369. clear();
  370. forEachXmlChildElementWithTagName (xml, e, "FILTER")
  371. {
  372. createNodeFromXml (*e);
  373. changed();
  374. }
  375. forEachXmlChildElementWithTagName (xml, e, "CONNECTION")
  376. {
  377. graph.addConnection ({ { NodeID ((uint32) e->getIntAttribute ("srcFilter")), e->getIntAttribute ("srcChannel") },
  378. { NodeID ((uint32) e->getIntAttribute ("dstFilter")), e->getIntAttribute ("dstChannel") } });
  379. }
  380. graph.removeIllegalConnections();
  381. }
  382. File PluginGraph::getDefaultGraphDocumentOnMobile()
  383. {
  384. auto persistantStorageLocation = File::getSpecialLocation (File::userApplicationDataDirectory);
  385. return persistantStorageLocation.getChildFile ("state.filtergraph");
  386. }