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.

438 lines
17KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_AUDIOPROCESSORGRAPH_JUCEHEADER__
  19. #define __JUCE_AUDIOPROCESSORGRAPH_JUCEHEADER__
  20. #include "juce_AudioProcessor.h"
  21. #include "../plugins/juce_AudioPluginFormatManager.h"
  22. #include "../plugins/juce_KnownPluginList.h"
  23. #include "../../containers/juce_NamedValueSet.h"
  24. #include "../../containers/juce_ReferenceCountedArray.h"
  25. //==============================================================================
  26. /**
  27. A type of AudioProcessor which plays back a graph of other AudioProcessors.
  28. Use one of these objects if you want to wire-up a set of AudioProcessors
  29. and play back the result.
  30. Processors can be added to the graph as "nodes" using addNode(), and once
  31. added, you can connect any of their input or output channels to other
  32. nodes using addConnection().
  33. To play back a graph through an audio device, you might want to use an
  34. AudioProcessorPlayer object.
  35. */
  36. class JUCE_API AudioProcessorGraph : public AudioProcessor,
  37. public AsyncUpdater
  38. {
  39. public:
  40. //==============================================================================
  41. /** Creates an empty graph.
  42. */
  43. AudioProcessorGraph();
  44. /** Destructor.
  45. Any processor objects that have been added to the graph will also be deleted.
  46. */
  47. ~AudioProcessorGraph();
  48. //==============================================================================
  49. /** Represents one of the nodes, or processors, in an AudioProcessorGraph.
  50. To create a node, call AudioProcessorGraph::addNode().
  51. */
  52. class JUCE_API Node : public ReferenceCountedObject
  53. {
  54. public:
  55. /** Destructor.
  56. */
  57. ~Node();
  58. //==============================================================================
  59. /** The ID number assigned to this node.
  60. This is assigned by the graph that owns it, and can't be changed.
  61. */
  62. const uint32 id;
  63. /** The actual processor object that this node represents.
  64. */
  65. AudioProcessor* const processor;
  66. /** A set of user-definable properties that are associated with this node.
  67. This can be used to attach values to the node for whatever purpose seems
  68. useful. For example, you might store an x and y position if your application
  69. is displaying the nodes on-screen.
  70. */
  71. NamedValueSet properties;
  72. //==============================================================================
  73. /** A convenient typedef for referring to a pointer to a node object.
  74. */
  75. typedef ReferenceCountedObjectPtr <Node> Ptr;
  76. //==============================================================================
  77. juce_UseDebuggingNewOperator
  78. private:
  79. friend class AudioProcessorGraph;
  80. bool isPrepared;
  81. Node (const uint32 id, AudioProcessor* const processor);
  82. void prepare (const double sampleRate, const int blockSize, AudioProcessorGraph* const graph);
  83. void unprepare();
  84. Node (const Node&);
  85. Node& operator= (const Node&);
  86. };
  87. //==============================================================================
  88. /** Represents a connection between two channels of two nodes in an AudioProcessorGraph.
  89. To create a connection, use AudioProcessorGraph::addConnection().
  90. */
  91. struct JUCE_API Connection
  92. {
  93. public:
  94. //==============================================================================
  95. /** The ID number of the node which is the input source for this connection.
  96. @see AudioProcessorGraph::getNodeForId
  97. */
  98. uint32 sourceNodeId;
  99. /** The index of the output channel of the source node from which this
  100. connection takes its data.
  101. If this value is the special number AudioProcessorGraph::midiChannelIndex, then
  102. it is referring to the source node's midi output. Otherwise, it is the zero-based
  103. index of an audio output channel in the source node.
  104. */
  105. int sourceChannelIndex;
  106. /** The ID number of the node which is the destination for this connection.
  107. @see AudioProcessorGraph::getNodeForId
  108. */
  109. uint32 destNodeId;
  110. /** The index of the input channel of the destination node to which this
  111. connection delivers its data.
  112. If this value is the special number AudioProcessorGraph::midiChannelIndex, then
  113. it is referring to the destination node's midi input. Otherwise, it is the zero-based
  114. index of an audio input channel in the destination node.
  115. */
  116. int destChannelIndex;
  117. //==============================================================================
  118. juce_UseDebuggingNewOperator
  119. private:
  120. };
  121. //==============================================================================
  122. /** Deletes all nodes and connections from this graph.
  123. Any processor objects in the graph will be deleted.
  124. */
  125. void clear();
  126. /** Returns the number of nodes in the graph. */
  127. int getNumNodes() const { return nodes.size(); }
  128. /** Returns a pointer to one of the nodes in the graph.
  129. This will return 0 if the index is out of range.
  130. @see getNodeForId
  131. */
  132. Node* getNode (const int index) const { return nodes [index]; }
  133. /** Searches the graph for a node with the given ID number and returns it.
  134. If no such node was found, this returns 0.
  135. @see getNode
  136. */
  137. Node* getNodeForId (const uint32 nodeId) const;
  138. /** Adds a node to the graph.
  139. This creates a new node in the graph, for the specified processor. Once you have
  140. added a processor to the graph, the graph owns it and will delete it later when
  141. it is no longer needed.
  142. The optional nodeId parameter lets you specify an ID to use for the node, but
  143. if the value is already in use, this new node will overwrite the old one.
  144. If this succeeds, it returns a pointer to the newly-created node.
  145. */
  146. Node* addNode (AudioProcessor* const newProcessor,
  147. uint32 nodeId = 0);
  148. /** Deletes a node within the graph which has the specified ID.
  149. This will also delete any connections that are attached to this node.
  150. */
  151. bool removeNode (const uint32 nodeId);
  152. //==============================================================================
  153. /** Returns the number of connections in the graph. */
  154. int getNumConnections() const { return connections.size(); }
  155. /** Returns a pointer to one of the connections in the graph. */
  156. const Connection* getConnection (const int index) const { return connections [index]; }
  157. /** Searches for a connection between some specified channels.
  158. If no such connection is found, this returns 0.
  159. */
  160. const Connection* getConnectionBetween (const uint32 sourceNodeId,
  161. const int sourceChannelIndex,
  162. const uint32 destNodeId,
  163. const int destChannelIndex) const;
  164. /** Returns true if there is a connection between any of the channels of
  165. two specified nodes.
  166. */
  167. bool isConnected (const uint32 possibleSourceNodeId,
  168. const uint32 possibleDestNodeId) const;
  169. /** Returns true if it would be legal to connect the specified points.
  170. */
  171. bool canConnect (const uint32 sourceNodeId, const int sourceChannelIndex,
  172. const uint32 destNodeId, const int destChannelIndex) const;
  173. /** Attempts to connect two specified channels of two nodes.
  174. If this isn't allowed (e.g. because you're trying to connect a midi channel
  175. to an audio one or other such nonsense), then it'll return false.
  176. */
  177. bool addConnection (const uint32 sourceNodeId, const int sourceChannelIndex,
  178. const uint32 destNodeId, const int destChannelIndex);
  179. /** Deletes the connection with the specified index.
  180. Returns true if a connection was actually deleted.
  181. */
  182. void removeConnection (const int index);
  183. /** Deletes any connection between two specified points.
  184. Returns true if a connection was actually deleted.
  185. */
  186. bool removeConnection (const uint32 sourceNodeId, const int sourceChannelIndex,
  187. const uint32 destNodeId, const int destChannelIndex);
  188. /** Removes all connections from the specified node.
  189. */
  190. bool disconnectNode (const uint32 nodeId);
  191. /** Performs a sanity checks of all the connections.
  192. This might be useful if some of the processors are doing things like changing
  193. their channel counts, which could render some connections obsolete.
  194. */
  195. bool removeIllegalConnections();
  196. //==============================================================================
  197. /** A special number that represents the midi channel of a node.
  198. This is used as a channel index value if you want to refer to the midi input
  199. or output instead of an audio channel.
  200. */
  201. static const int midiChannelIndex;
  202. //==============================================================================
  203. /** A special type of AudioProcessor that can live inside an AudioProcessorGraph
  204. in order to use the audio that comes into and out of the graph itself.
  205. If you create an AudioGraphIOProcessor in "input" mode, it will act as a
  206. node in the graph which delivers the audio that is coming into the parent
  207. graph. This allows you to stream the data to other nodes and process the
  208. incoming audio.
  209. Likewise, one of these in "output" mode can be sent data which it will add to
  210. the sum of data being sent to the graph's output.
  211. @see AudioProcessorGraph
  212. */
  213. class JUCE_API AudioGraphIOProcessor : public AudioPluginInstance
  214. {
  215. public:
  216. /** Specifies the mode in which this processor will operate.
  217. */
  218. enum IODeviceType
  219. {
  220. audioInputNode, /**< In this mode, the processor has output channels
  221. representing all the audio input channels that are
  222. coming into its parent audio graph. */
  223. audioOutputNode, /**< In this mode, the processor has input channels
  224. representing all the audio output channels that are
  225. going out of its parent audio graph. */
  226. midiInputNode, /**< In this mode, the processor has a midi output which
  227. delivers the same midi data that is arriving at its
  228. parent graph. */
  229. midiOutputNode /**< In this mode, the processor has a midi input and
  230. any data sent to it will be passed out of the parent
  231. graph. */
  232. };
  233. //==============================================================================
  234. /** Returns the mode of this processor. */
  235. IODeviceType getType() const { return type; }
  236. /** Returns the parent graph to which this processor belongs, or 0 if it
  237. hasn't yet been added to one. */
  238. AudioProcessorGraph* getParentGraph() const { return graph; }
  239. /** True if this is an audio or midi input. */
  240. bool isInput() const;
  241. /** True if this is an audio or midi output. */
  242. bool isOutput() const;
  243. //==============================================================================
  244. AudioGraphIOProcessor (const IODeviceType type);
  245. ~AudioGraphIOProcessor();
  246. const String getName() const;
  247. void fillInPluginDescription (PluginDescription& d) const;
  248. void prepareToPlay (double sampleRate, int estimatedSamplesPerBlock);
  249. void releaseResources();
  250. void processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages);
  251. const String getInputChannelName (const int channelIndex) const;
  252. const String getOutputChannelName (const int channelIndex) const;
  253. bool isInputChannelStereoPair (int index) const;
  254. bool isOutputChannelStereoPair (int index) const;
  255. bool acceptsMidi() const;
  256. bool producesMidi() const;
  257. AudioProcessorEditor* createEditor();
  258. int getNumParameters();
  259. const String getParameterName (int);
  260. float getParameter (int);
  261. const String getParameterText (int);
  262. void setParameter (int, float);
  263. int getNumPrograms();
  264. int getCurrentProgram();
  265. void setCurrentProgram (int);
  266. const String getProgramName (int);
  267. void changeProgramName (int, const String&);
  268. void getStateInformation (JUCE_NAMESPACE::MemoryBlock& destData);
  269. void setStateInformation (const void* data, int sizeInBytes);
  270. /** @internal */
  271. void setParentGraph (AudioProcessorGraph* const graph);
  272. juce_UseDebuggingNewOperator
  273. private:
  274. const IODeviceType type;
  275. AudioProcessorGraph* graph;
  276. AudioGraphIOProcessor (const AudioGraphIOProcessor&);
  277. AudioGraphIOProcessor& operator= (const AudioGraphIOProcessor&);
  278. };
  279. //==============================================================================
  280. // AudioProcessor methods:
  281. const String getName() const;
  282. void prepareToPlay (double sampleRate, int estimatedSamplesPerBlock);
  283. void releaseResources();
  284. void processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages);
  285. const String getInputChannelName (const int channelIndex) const;
  286. const String getOutputChannelName (const int channelIndex) const;
  287. bool isInputChannelStereoPair (int index) const;
  288. bool isOutputChannelStereoPair (int index) const;
  289. bool acceptsMidi() const;
  290. bool producesMidi() const;
  291. AudioProcessorEditor* createEditor() { return 0; }
  292. int getNumParameters() { return 0; }
  293. const String getParameterName (int) { return String::empty; }
  294. float getParameter (int) { return 0; }
  295. const String getParameterText (int) { return String::empty; }
  296. void setParameter (int, float) { }
  297. int getNumPrograms() { return 0; }
  298. int getCurrentProgram() { return 0; }
  299. void setCurrentProgram (int) { }
  300. const String getProgramName (int) { return String::empty; }
  301. void changeProgramName (int, const String&) { }
  302. void getStateInformation (JUCE_NAMESPACE::MemoryBlock& destData);
  303. void setStateInformation (const void* data, int sizeInBytes);
  304. /** @internal */
  305. void handleAsyncUpdate();
  306. //==============================================================================
  307. juce_UseDebuggingNewOperator
  308. private:
  309. ReferenceCountedArray <Node> nodes;
  310. OwnedArray <Connection> connections;
  311. int lastNodeId;
  312. AudioSampleBuffer renderingBuffers;
  313. OwnedArray <MidiBuffer> midiBuffers;
  314. CriticalSection renderLock;
  315. VoidArray renderingOps;
  316. friend class AudioGraphIOProcessor;
  317. AudioSampleBuffer* currentAudioInputBuffer;
  318. AudioSampleBuffer currentAudioOutputBuffer;
  319. MidiBuffer* currentMidiInputBuffer;
  320. MidiBuffer currentMidiOutputBuffer;
  321. void clearRenderingSequence();
  322. void buildRenderingSequence();
  323. bool isAnInputTo (const uint32 possibleInputId,
  324. const uint32 possibleDestinationId,
  325. const int recursionCheck) const;
  326. AudioProcessorGraph (const AudioProcessorGraph&);
  327. AudioProcessorGraph& operator= (const AudioProcessorGraph&);
  328. };
  329. #endif // __JUCE_AUDIOPROCESSORGRAPH_JUCEHEADER__