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.

AudioProcessorGraph.h 16KB

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