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 17KB

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