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.

408 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "JuceHeader.h"
  18. #include "FilterGraph.h"
  19. #include "InternalFilters.h"
  20. #include "GraphEditorPanel.h"
  21. //==============================================================================
  22. const int FilterGraph::midiChannelNumber = 0x1000;
  23. FilterGraph::FilterGraph (AudioPluginFormatManager& formatManager_)
  24. : FileBasedDocument (filenameSuffix,
  25. filenameWildcard,
  26. "Load a filter graph",
  27. "Save a filter graph"),
  28. formatManager (formatManager_), lastUID (0), appProperties (nullptr), panel (nullptr)
  29. {
  30. setChangedFlag (false);
  31. }
  32. FilterGraph::~FilterGraph()
  33. {
  34. graph.clear();
  35. }
  36. void FilterGraph::ready(ApplicationProperties* ap)
  37. {
  38. appProperties = ap;
  39. InternalPluginFormat internalFormat;
  40. addFilter (internalFormat.getDescriptionFor (InternalPluginFormat::audioInputFilter), 0.5f, 0.1f);
  41. addFilter (internalFormat.getDescriptionFor (InternalPluginFormat::midiInputFilter), 0.25f, 0.1f);
  42. addFilter (internalFormat.getDescriptionFor (InternalPluginFormat::audioOutputFilter), 0.5f, 0.9f);
  43. addFilter (internalFormat.getDescriptionFor (InternalPluginFormat::midiOutputFilter), 0.25f, 0.9f);
  44. }
  45. void FilterGraph::setPanel(GraphEditorPanel* p)
  46. {
  47. panel = p;
  48. }
  49. uint32 FilterGraph::getNextUID() noexcept
  50. {
  51. return ++lastUID;
  52. }
  53. //==============================================================================
  54. int FilterGraph::getNumFilters() const noexcept
  55. {
  56. return graph.getNumNodes();
  57. }
  58. const AudioProcessorGraph::Node::Ptr FilterGraph::getNode (const int index) const noexcept
  59. {
  60. return graph.getNode (index);
  61. }
  62. const AudioProcessorGraph::Node::Ptr FilterGraph::getNodeForId (const uint32 uid) const noexcept
  63. {
  64. return graph.getNodeForId (uid);
  65. }
  66. void FilterGraph::addFilter (const PluginDescription* desc, double x, double y)
  67. {
  68. if (desc != nullptr)
  69. {
  70. AudioProcessorGraph::Node* node = nullptr;
  71. String errorMessage;
  72. if (AudioPluginInstance* instance = formatManager.createPluginInstance (*desc, graph.getSampleRate(), graph.getBlockSize(), errorMessage))
  73. node = graph.addNode (instance);
  74. if (node != nullptr)
  75. {
  76. node->properties.set ("x", x);
  77. node->properties.set ("y", y);
  78. changed();
  79. }
  80. else
  81. {
  82. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  83. TRANS("Couldn't create filter"),
  84. errorMessage);
  85. }
  86. }
  87. }
  88. void FilterGraph::removeFilter (const uint32 id)
  89. {
  90. if (panel != nullptr)
  91. panel->closeCurrentlyOpenWindowsFor (id);
  92. if (graph.removeNode (id))
  93. changed();
  94. }
  95. void FilterGraph::disconnectFilter (const uint32 id)
  96. {
  97. if (graph.disconnectNode (id))
  98. changed();
  99. }
  100. void FilterGraph::removeIllegalConnections()
  101. {
  102. if (graph.removeIllegalConnections())
  103. changed();
  104. }
  105. void FilterGraph::setNodePosition (const int nodeId, double x, double y)
  106. {
  107. const AudioProcessorGraph::Node::Ptr n (graph.getNodeForId (nodeId));
  108. if (n != nullptr)
  109. {
  110. n->properties.set ("x", jlimit (0.0, 1.0, x));
  111. n->properties.set ("y", jlimit (0.0, 1.0, y));
  112. }
  113. }
  114. void FilterGraph::getNodePosition (const int nodeId, double& x, double& y) const
  115. {
  116. x = y = 0;
  117. const AudioProcessorGraph::Node::Ptr n (graph.getNodeForId (nodeId));
  118. if (n != nullptr)
  119. {
  120. x = (double) n->properties ["x"];
  121. y = (double) n->properties ["y"];
  122. }
  123. }
  124. //==============================================================================
  125. int FilterGraph::getNumConnections() const noexcept
  126. {
  127. return graph.getNumConnections();
  128. }
  129. const AudioProcessorGraph::Connection* FilterGraph::getConnection (const int index) const noexcept
  130. {
  131. return graph.getConnection (index);
  132. }
  133. const AudioProcessorGraph::Connection* FilterGraph::getConnectionBetween (uint32 sourceFilterUID, int sourceFilterChannel,
  134. uint32 destFilterUID, int destFilterChannel) const noexcept
  135. {
  136. return graph.getConnectionBetween (sourceFilterUID, sourceFilterChannel,
  137. destFilterUID, destFilterChannel);
  138. }
  139. bool FilterGraph::canConnect (uint32 sourceFilterUID, int sourceFilterChannel,
  140. uint32 destFilterUID, int destFilterChannel) const noexcept
  141. {
  142. return graph.canConnect (sourceFilterUID, sourceFilterChannel,
  143. destFilterUID, destFilterChannel);
  144. }
  145. bool FilterGraph::addConnection (uint32 sourceFilterUID, int sourceFilterChannel,
  146. uint32 destFilterUID, int destFilterChannel)
  147. {
  148. const bool result = graph.addConnection (sourceFilterUID, sourceFilterChannel,
  149. destFilterUID, destFilterChannel);
  150. if (result)
  151. changed();
  152. return result;
  153. }
  154. void FilterGraph::removeConnection (const int index)
  155. {
  156. graph.removeConnection (index);
  157. changed();
  158. }
  159. void FilterGraph::removeConnection (uint32 sourceFilterUID, int sourceFilterChannel,
  160. uint32 destFilterUID, int destFilterChannel)
  161. {
  162. if (graph.removeConnection (sourceFilterUID, sourceFilterChannel,
  163. destFilterUID, destFilterChannel))
  164. changed();
  165. }
  166. void FilterGraph::clear()
  167. {
  168. if (panel != nullptr)
  169. panel->closeAllCurrentlyOpenWindows();
  170. graph.clear();
  171. changed();
  172. }
  173. void FilterGraph::clearKeepingInternals()
  174. {
  175. if (panel != nullptr)
  176. panel->closeAllCurrentlyOpenWindows();
  177. graph.clear();
  178. InternalPluginFormat internalFormat;
  179. addFilter (internalFormat.getDescriptionFor (InternalPluginFormat::audioInputFilter), 0.5f, 0.1f);
  180. addFilter (internalFormat.getDescriptionFor (InternalPluginFormat::midiInputFilter), 0.25f, 0.1f);
  181. addFilter (internalFormat.getDescriptionFor (InternalPluginFormat::audioOutputFilter), 0.5f, 0.9f);
  182. addFilter (internalFormat.getDescriptionFor (InternalPluginFormat::midiOutputFilter), 0.25f, 0.9f);
  183. changed();
  184. }
  185. //==============================================================================
  186. String FilterGraph::getDocumentTitle()
  187. {
  188. if (! getFile().exists())
  189. return "Unnamed";
  190. return getFile().getFileNameWithoutExtension();
  191. }
  192. Result FilterGraph::loadDocument (const File& file)
  193. {
  194. XmlDocument doc (file);
  195. ScopedPointer<XmlElement> xml (doc.getDocumentElement());
  196. if (xml == nullptr || ! xml->hasTagName ("FILTERGRAPH"))
  197. return Result::fail ("Not a valid filter graph file");
  198. restoreFromXml (*xml);
  199. return Result::ok();
  200. }
  201. Result FilterGraph::saveDocument (const File& file)
  202. {
  203. ScopedPointer<XmlElement> xml (createXml());
  204. if (! xml->writeToFile (file, String()))
  205. return Result::fail ("Couldn't write to the file");
  206. return Result::ok();
  207. }
  208. File FilterGraph::getLastDocumentOpened()
  209. {
  210. if (appProperties == nullptr)
  211. return File();
  212. RecentlyOpenedFilesList recentFiles;
  213. recentFiles.restoreFromString (appProperties->getUserSettings()
  214. ->getValue ("recentFilterGraphFiles"));
  215. return recentFiles.getFile (0);
  216. }
  217. void FilterGraph::setLastDocumentOpened (const File& file)
  218. {
  219. if (appProperties == nullptr)
  220. return;
  221. RecentlyOpenedFilesList recentFiles;
  222. recentFiles.restoreFromString (appProperties->getUserSettings()
  223. ->getValue ("recentFilterGraphFiles"));
  224. recentFiles.addFile (file);
  225. appProperties->getUserSettings()
  226. ->setValue ("recentFilterGraphFiles", recentFiles.toString());
  227. }
  228. //==============================================================================
  229. static XmlElement* createNodeXml (AudioProcessorGraph::Node* const node) noexcept
  230. {
  231. AudioPluginInstance* plugin = dynamic_cast <AudioPluginInstance*> (node->getProcessor());
  232. if (plugin == nullptr)
  233. {
  234. jassertfalse;
  235. return nullptr;
  236. }
  237. XmlElement* e = new XmlElement ("FILTER");
  238. e->setAttribute ("uid", (int) node->nodeId);
  239. e->setAttribute ("x", node->properties ["x"].toString());
  240. e->setAttribute ("y", node->properties ["y"].toString());
  241. e->setAttribute ("uiLastX", node->properties ["uiLastX"].toString());
  242. e->setAttribute ("uiLastY", node->properties ["uiLastY"].toString());
  243. PluginDescription pd;
  244. plugin->fillInPluginDescription (pd);
  245. e->addChildElement (pd.createXml());
  246. XmlElement* state = new XmlElement ("STATE");
  247. MemoryBlock m;
  248. node->getProcessor()->getStateInformation (m);
  249. state->addTextElement (m.toBase64Encoding());
  250. e->addChildElement (state);
  251. return e;
  252. }
  253. void FilterGraph::createNodeFromXml (const XmlElement& xml)
  254. {
  255. PluginDescription pd;
  256. forEachXmlChildElement (xml, e)
  257. {
  258. if (pd.loadFromXml (*e))
  259. break;
  260. }
  261. String errorMessage;
  262. AudioPluginInstance* instance = formatManager.createPluginInstance (pd, graph.getSampleRate(), graph.getBlockSize(), errorMessage);
  263. if (instance == nullptr)
  264. {
  265. // xxx handle ins + outs
  266. }
  267. if (instance == nullptr)
  268. return;
  269. AudioProcessorGraph::Node::Ptr node (graph.addNode (instance, xml.getIntAttribute ("uid")));
  270. if (const XmlElement* const state = xml.getChildByName ("STATE"))
  271. {
  272. MemoryBlock m;
  273. m.fromBase64Encoding (state->getAllSubText());
  274. node->getProcessor()->setStateInformation (m.getData(), (int) m.getSize());
  275. }
  276. node->properties.set ("x", xml.getDoubleAttribute ("x"));
  277. node->properties.set ("y", xml.getDoubleAttribute ("y"));
  278. node->properties.set ("uiLastX", xml.getIntAttribute ("uiLastX"));
  279. node->properties.set ("uiLastY", xml.getIntAttribute ("uiLastY"));
  280. }
  281. XmlElement* FilterGraph::createXml() const
  282. {
  283. XmlElement* xml = new XmlElement ("FILTERGRAPH");
  284. for (int i = 0; i < graph.getNumNodes(); ++i)
  285. xml->addChildElement (createNodeXml (graph.getNode (i)));
  286. for (int i = 0; i < graph.getNumConnections(); ++i)
  287. {
  288. const AudioProcessorGraph::Connection* const fc = graph.getConnection(i);
  289. XmlElement* e = new XmlElement ("CONNECTION");
  290. e->setAttribute ("srcFilter", (int) fc->sourceNodeId);
  291. e->setAttribute ("srcChannel", fc->sourceChannelIndex);
  292. e->setAttribute ("dstFilter", (int) fc->destNodeId);
  293. e->setAttribute ("dstChannel", fc->destChannelIndex);
  294. xml->addChildElement (e);
  295. }
  296. return xml;
  297. }
  298. void FilterGraph::restoreFromXml (const XmlElement& xml)
  299. {
  300. clear();
  301. forEachXmlChildElementWithTagName (xml, e, "FILTER")
  302. {
  303. createNodeFromXml (*e);
  304. changed();
  305. }
  306. forEachXmlChildElementWithTagName (xml, e, "CONNECTION")
  307. {
  308. addConnection ((uint32) e->getIntAttribute ("srcFilter"),
  309. e->getIntAttribute ("srcChannel"),
  310. (uint32) e->getIntAttribute ("dstFilter"),
  311. e->getIntAttribute ("dstChannel"));
  312. }
  313. graph.removeIllegalConnections();
  314. }