Audio plugin host https://kx.studio/carla
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.

383 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. #ifndef JUCE_AUDIOPROCESSORGRAPH_H_INCLUDED
  18. #define JUCE_AUDIOPROCESSORGRAPH_H_INCLUDED
  19. #include "juce_AudioProcessor.h"
  20. //==============================================================================
  21. /**
  22. A type of AudioProcessor which plays back a graph of other AudioProcessors.
  23. Use one of these objects if you want to wire-up a set of AudioProcessors
  24. and play back the result.
  25. Processors can be added to the graph as "nodes" using addNode(), and once
  26. added, you can connect any of their input or output channels to other
  27. nodes using addConnection().
  28. To play back a graph through an audio device, you might want to use an
  29. AudioProcessorPlayer object.
  30. */
  31. class JUCE_API AudioProcessorGraph : public AudioProcessor
  32. /* private AsyncUpdater*/
  33. {
  34. public:
  35. //==============================================================================
  36. /** Creates an empty graph. */
  37. AudioProcessorGraph();
  38. /** Destructor.
  39. Any processor objects that have been added to the graph will also be deleted.
  40. */
  41. ~AudioProcessorGraph();
  42. //==============================================================================
  43. /** Represents one of the nodes, or processors, in an AudioProcessorGraph.
  44. To create a node, call AudioProcessorGraph::addNode().
  45. */
  46. class JUCE_API Node : public ReferenceCountedObject
  47. {
  48. public:
  49. //==============================================================================
  50. /** The ID number assigned to this node.
  51. This is assigned by the graph that owns it, and can't be changed.
  52. */
  53. const uint32 nodeId;
  54. /** The actual processor object that this node represents. */
  55. AudioProcessor* getProcessor() const noexcept { return processor; }
  56. /** A set of user-definable properties that are associated with this node.
  57. This can be used to attach values to the node for whatever purpose seems
  58. useful. For example, you might store an x and y position if your application
  59. is displaying the nodes on-screen.
  60. */
  61. NamedValueSet properties;
  62. //==============================================================================
  63. /** A convenient typedef for referring to a pointer to a node object. */
  64. typedef ReferenceCountedObjectPtr<Node> Ptr;
  65. private:
  66. //==============================================================================
  67. friend class AudioProcessorGraph;
  68. const ScopedPointer<AudioProcessor> processor;
  69. bool isPrepared;
  70. Node (uint32 nodeId, AudioProcessor*) noexcept;
  71. void setParentGraph (AudioProcessorGraph*) const;
  72. void prepare (double newSampleRate, int newBlockSize, AudioProcessorGraph*);
  73. void unprepare();
  74. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Node)
  75. };
  76. //==============================================================================
  77. /** Represents a connection between two channels of two nodes in an AudioProcessorGraph.
  78. To create a connection, use AudioProcessorGraph::addConnection().
  79. */
  80. struct JUCE_API Connection
  81. {
  82. public:
  83. //==============================================================================
  84. Connection (uint32 sourceNodeId, int sourceChannelIndex,
  85. uint32 destNodeId, int destChannelIndex) noexcept;
  86. //==============================================================================
  87. /** The ID number of the node which is the input source for this connection.
  88. @see AudioProcessorGraph::getNodeForId
  89. */
  90. uint32 sourceNodeId;
  91. /** The index of the output channel of the source node from which this
  92. connection takes its data.
  93. If this value is the special number AudioProcessorGraph::midiChannelIndex, then
  94. it is referring to the source node's midi output. Otherwise, it is the zero-based
  95. index of an audio output channel in the source node.
  96. */
  97. int sourceChannelIndex;
  98. /** The ID number of the node which is the destination for this connection.
  99. @see AudioProcessorGraph::getNodeForId
  100. */
  101. uint32 destNodeId;
  102. /** The index of the input channel of the destination node to which this
  103. connection delivers its data.
  104. If this value is the special number AudioProcessorGraph::midiChannelIndex, then
  105. it is referring to the destination node's midi input. Otherwise, it is the zero-based
  106. index of an audio input channel in the destination node.
  107. */
  108. int destChannelIndex;
  109. private:
  110. //==============================================================================
  111. JUCE_LEAK_DETECTOR (Connection)
  112. };
  113. //==============================================================================
  114. /** Deletes all nodes and connections from this graph.
  115. Any processor objects in the graph will be deleted.
  116. */
  117. void clear();
  118. /** Returns the number of nodes in the graph. */
  119. int getNumNodes() const noexcept { return nodes.size(); }
  120. /** Returns a pointer to one of the nodes in the graph.
  121. This will return nullptr if the index is out of range.
  122. @see getNodeForId
  123. */
  124. Node* getNode (const int index) const noexcept { return nodes [index]; }
  125. /** Searches the graph for a node with the given ID number and returns it.
  126. If no such node was found, this returns nullptr.
  127. @see getNode
  128. */
  129. Node* getNodeForId (const uint32 nodeId) const;
  130. /** Adds a node to the graph.
  131. This creates a new node in the graph, for the specified processor. Once you have
  132. added a processor to the graph, the graph owns it and will delete it later when
  133. it is no longer needed.
  134. The optional nodeId parameter lets you specify an ID to use for the node, but
  135. if the value is already in use, this new node will overwrite the old one.
  136. If this succeeds, it returns a pointer to the newly-created node.
  137. */
  138. Node* addNode (AudioProcessor* newProcessor, uint32 nodeId = 0);
  139. /** Deletes a node within the graph which has the specified ID.
  140. This will also delete any connections that are attached to this node.
  141. */
  142. bool removeNode (uint32 nodeId);
  143. /** Deletes a node within the graph which has the specified ID.
  144. This will also delete any connections that are attached to this node.
  145. */
  146. bool removeNode (Node* node);
  147. //==============================================================================
  148. /** Returns the number of connections in the graph. */
  149. int getNumConnections() const { return connections.size(); }
  150. /** Returns a pointer to one of the connections in the graph. */
  151. const Connection* getConnection (int index) const { return connections [index]; }
  152. /** Searches for a connection between some specified channels.
  153. If no such connection is found, this returns nullptr.
  154. */
  155. const Connection* getConnectionBetween (uint32 sourceNodeId,
  156. int sourceChannelIndex,
  157. uint32 destNodeId,
  158. int destChannelIndex) const;
  159. /** Returns true if there is a connection between any of the channels of
  160. two specified nodes.
  161. */
  162. bool isConnected (uint32 possibleSourceNodeId,
  163. uint32 possibleDestNodeId) const;
  164. /** Returns true if it would be legal to connect the specified points. */
  165. bool canConnect (uint32 sourceNodeId, int sourceChannelIndex,
  166. uint32 destNodeId, int destChannelIndex) const;
  167. /** Attempts to connect two specified channels of two nodes.
  168. If this isn't allowed (e.g. because you're trying to connect a midi channel
  169. to an audio one or other such nonsense), then it'll return false.
  170. */
  171. bool addConnection (uint32 sourceNodeId, int sourceChannelIndex,
  172. uint32 destNodeId, int destChannelIndex);
  173. /** Deletes the connection with the specified index. */
  174. void removeConnection (int index);
  175. /** Deletes any connection between two specified points.
  176. Returns true if a connection was actually deleted.
  177. */
  178. bool removeConnection (uint32 sourceNodeId, int sourceChannelIndex,
  179. uint32 destNodeId, int destChannelIndex);
  180. /** Removes all connections from the specified node. */
  181. bool disconnectNode (uint32 nodeId);
  182. /** Returns true if the given connection's channel numbers map on to valid
  183. channels at each end.
  184. Even if a connection is valid when created, its status could change if
  185. a node changes its channel config.
  186. */
  187. bool isConnectionLegal (const Connection* connection) const;
  188. /** Performs a sanity checks of all the connections.
  189. This might be useful if some of the processors are doing things like changing
  190. their channel counts, which could render some connections obsolete.
  191. */
  192. bool removeIllegalConnections();
  193. //==============================================================================
  194. /** A special number that represents the midi channel of a node.
  195. This is used as a channel index value if you want to refer to the midi input
  196. or output instead of an audio channel.
  197. */
  198. static const int midiChannelIndex;
  199. //==============================================================================
  200. /** A special type of AudioProcessor that can live inside an AudioProcessorGraph
  201. in order to use the audio that comes into and out of the graph itself.
  202. If you create an AudioGraphIOProcessor in "input" mode, it will act as a
  203. node in the graph which delivers the audio that is coming into the parent
  204. graph. This allows you to stream the data to other nodes and process the
  205. incoming audio.
  206. Likewise, one of these in "output" mode can be sent data which it will add to
  207. the sum of data being sent to the graph's output.
  208. @see AudioProcessorGraph
  209. */
  210. class JUCE_API AudioGraphIOProcessor : public AudioProcessor
  211. {
  212. public:
  213. /** Specifies the mode in which this processor will operate.
  214. */
  215. enum IODeviceType
  216. {
  217. audioInputNode, /**< In this mode, the processor has output channels
  218. representing all the audio input channels that are
  219. coming into its parent audio graph. */
  220. audioOutputNode, /**< In this mode, the processor has input channels
  221. representing all the audio output channels that are
  222. going out of its parent audio graph. */
  223. midiInputNode, /**< In this mode, the processor has a midi output which
  224. delivers the same midi data that is arriving at its
  225. parent graph. */
  226. midiOutputNode /**< In this mode, the processor has a midi input and
  227. any data sent to it will be passed out of the parent
  228. graph. */
  229. };
  230. //==============================================================================
  231. /** Returns the mode of this processor. */
  232. IODeviceType getType() const noexcept { return type; }
  233. /** Returns the parent graph to which this processor belongs, or nullptr if it
  234. hasn't yet been added to one. */
  235. AudioProcessorGraph* getParentGraph() const noexcept { return graph; }
  236. /** True if this is an audio or midi input. */
  237. bool isInput() const noexcept;
  238. /** True if this is an audio or midi output. */
  239. bool isOutput() const noexcept;
  240. //==============================================================================
  241. AudioGraphIOProcessor (const IODeviceType type);
  242. ~AudioGraphIOProcessor();
  243. const String getName() const override;
  244. #if 0
  245. void fillInPluginDescription (PluginDescription&) const override;
  246. #endif
  247. void prepareToPlay (double newSampleRate, int estimatedSamplesPerBlock) override;
  248. void releaseResources() override;
  249. void processBlock (AudioSampleBuffer&, MidiBuffer&) override;
  250. bool acceptsMidi() const override;
  251. bool producesMidi() const override;
  252. /** @internal */
  253. void setParentGraph (AudioProcessorGraph*);
  254. private:
  255. const IODeviceType type;
  256. AudioProcessorGraph* graph;
  257. //==============================================================================
  258. void processAudio (AudioSampleBuffer& buffer, MidiBuffer& midiMessages);
  259. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioGraphIOProcessor)
  260. };
  261. //==============================================================================
  262. const String getName() const override;
  263. void prepareToPlay (double, int) override;
  264. void releaseResources() override;
  265. void processBlock (AudioSampleBuffer&, MidiBuffer&) override;
  266. void reset() override;
  267. void setNonRealtime (bool) noexcept override;
  268. void setPlayHead (AudioPlayHead*) override;
  269. bool acceptsMidi() const override;
  270. bool producesMidi() const override;
  271. void reorderNowIfNeeded();
  272. private:
  273. //==============================================================================
  274. void processAudio (AudioSampleBuffer& buffer, MidiBuffer& midiMessages);
  275. //==============================================================================
  276. ReferenceCountedArray<Node> nodes;
  277. OwnedArray<Connection> connections;
  278. uint32 lastNodeId;
  279. OwnedArray<MidiBuffer> midiBuffers;
  280. Array<void*> renderingOps;
  281. friend class AudioGraphIOProcessor;
  282. struct AudioProcessorGraphBufferHelpers;
  283. ScopedPointer<AudioProcessorGraphBufferHelpers> audioBuffers;
  284. MidiBuffer* currentMidiInputBuffer;
  285. MidiBuffer currentMidiOutputBuffer;
  286. bool isPrepared, needsReorder;
  287. CarlaRecursiveMutex reorderMutex;
  288. void clearRenderingSequence();
  289. void buildRenderingSequence();
  290. bool isAnInputTo (uint32 possibleInputId, uint32 possibleDestinationId, int recursionCheck) const;
  291. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioProcessorGraph)
  292. };
  293. #endif // JUCE_AUDIOPROCESSORGRAPH_H_INCLUDED