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.

386 lines
16KB

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