2 ==============================================================================
4 This file is part of the Water library.
5 Copyright (c) 2015 ROLI Ltd.
6 Copyright (C) 2017-2022 Filipe Coelho <falktx@falktx.com>
8 Permission is granted to use this software under the terms of the GNU
9 General Public License as published by the Free Software Foundation;
10 either version 2 of the License, or any later version.
12 This program is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 ==============================================================================
21 #ifndef WATER_AUDIOPROCESSORGRAPH_H_INCLUDED
22 #define WATER_AUDIOPROCESSORGRAPH_H_INCLUDED
24 #include "AudioProcessor.h"
25 #include "../containers/OwnedArray.h"
26 #include "../containers/ReferenceCountedArray.h"
27 #include "../midi/MidiBuffer.h"
31 //==============================================================================
33 A type of AudioProcessor which plays back a graph of other AudioProcessors.
35 Use one of these objects if you want to wire-up a set of AudioProcessors
36 and play back the result.
38 Processors can be added to the graph as "nodes" using addNode(), and once
39 added, you can connect any of their input or output channels to other
40 nodes using addConnection().
42 To play back a graph through an audio device, you might want to use an
43 AudioProcessorPlayer object.
45 class AudioProcessorGraph
: public AudioProcessor
48 //==============================================================================
49 /** Creates an empty graph. */
50 AudioProcessorGraph();
53 Any processor objects that have been added to the graph will also be deleted.
55 ~AudioProcessorGraph();
57 //==============================================================================
58 /** Represents one of the nodes, or processors, in an AudioProcessorGraph.
60 To create a node, call AudioProcessorGraph::addNode().
62 class Node
: public ReferenceCountedObject
65 //==============================================================================
66 /** The ID number assigned to this node.
67 This is assigned by the graph that owns it, and can't be changed.
71 /** The actual processor object that this node represents. */
72 AudioProcessor
* getProcessor() const noexcept
{ return processor
; }
74 /** Custom properties for Carla usage. */
107 //==============================================================================
108 /** A convenient typedef for referring to a pointer to a node object. */
109 typedef ReferenceCountedObjectPtr
<Node
> Ptr
;
112 //==============================================================================
113 friend class AudioProcessorGraph
;
115 const CarlaScopedPointer
<AudioProcessor
> processor
;
118 Node (uint32 nodeId
, AudioProcessor
*) noexcept
;
120 void setParentGraph (AudioProcessorGraph
*) const;
121 void prepare (double newSampleRate
, int newBlockSize
, AudioProcessorGraph
*);
124 CARLA_DECLARE_NON_COPYABLE (Node
)
127 //==============================================================================
128 /** Represents a connection between two channels of two nodes in an AudioProcessorGraph.
130 To create a connection, use AudioProcessorGraph::addConnection().
135 //==============================================================================
136 Connection (ChannelType channelType
,
137 uint32 sourceNodeId
, uint sourceChannelIndex
,
138 uint32 destNodeId
, uint destChannelIndex
) noexcept
;
140 //==============================================================================
141 /** Defines the connection type. */
142 ChannelType channelType
;
144 /** The ID number of the node which is the input source for this connection.
145 @see AudioProcessorGraph::getNodeForId
149 /** The index of the output channel of the source node from which this
150 connection takes its data.
152 If this value is the special number AudioProcessorGraph::midiChannelIndex, then
153 it is referring to the source node's midi output. Otherwise, it is the zero-based
154 index of an audio output channel in the source node.
156 uint sourceChannelIndex
;
158 /** The ID number of the node which is the destination for this connection.
159 @see AudioProcessorGraph::getNodeForId
163 /** The index of the input channel of the destination node to which this
164 connection delivers its data.
166 If this value is the special number AudioProcessorGraph::midiChannelIndex, then
167 it is referring to the destination node's midi input. Otherwise, it is the zero-based
168 index of an audio input channel in the destination node.
170 uint destChannelIndex
;
173 //==============================================================================
174 /** Deletes all nodes and connections from this graph.
175 Any processor objects in the graph will be deleted.
179 /** Returns the number of nodes in the graph. */
180 int getNumNodes() const noexcept
{ return nodes
.size(); }
182 /** Returns a pointer to one of the nodes in the graph.
183 This will return nullptr if the index is out of range.
186 Node
* getNode (const int index
) const noexcept
{ return nodes
[index
]; }
188 /** Searches the graph for a node with the given ID number and returns it.
189 If no such node was found, this returns nullptr.
192 Node
* getNodeForId (const uint32 nodeId
) const;
194 /** Adds a node to the graph.
196 This creates a new node in the graph, for the specified processor. Once you have
197 added a processor to the graph, the graph owns it and will delete it later when
198 it is no longer needed.
200 The optional nodeId parameter lets you specify an ID to use for the node, but
201 if the value is already in use, this new node will overwrite the old one.
203 If this succeeds, it returns a pointer to the newly-created node.
205 Node
* addNode (AudioProcessor
* newProcessor
, uint32 nodeId
= 0);
207 /** Deletes a node within the graph which has the specified ID.
209 This will also delete any connections that are attached to this node.
211 bool removeNode (uint32 nodeId
);
213 /** Deletes a node within the graph which has the specified ID.
215 This will also delete any connections that are attached to this node.
217 bool removeNode (Node
* node
);
219 //==============================================================================
220 /** Returns the number of connections in the graph. */
221 size_t getNumConnections() const { return connections
.size(); }
223 /** Returns a pointer to one of the connections in the graph. */
224 const Connection
* getConnection (size_t index
) const { return connections
[index
]; }
226 /** Searches for a connection between some specified channels.
227 If no such connection is found, this returns nullptr.
229 const Connection
* getConnectionBetween (ChannelType channelType
,
231 uint sourceChannelIndex
,
233 uint destChannelIndex
) const;
235 /** Returns true if there is a connection between any of the channels of
238 bool isConnected (uint32 possibleSourceNodeId
,
239 uint32 possibleDestNodeId
) const;
241 /** Returns true if it would be legal to connect the specified points. */
242 bool canConnect (ChannelType channelType
,
243 uint32 sourceNodeId
, uint sourceChannelIndex
,
244 uint32 destNodeId
, uint destChannelIndex
) const;
246 /** Attempts to connect two specified channels of two nodes.
248 If this isn't allowed (e.g. because you're trying to connect a midi channel
249 to an audio one or other such nonsense), then it'll return false.
251 bool addConnection (ChannelType channelType
,
252 uint32 sourceNodeId
, uint sourceChannelIndex
,
253 uint32 destNodeId
, uint destChannelIndex
);
255 /** Deletes the connection with the specified index. */
256 void removeConnection (int index
);
258 /** Deletes any connection between two specified points.
259 Returns true if a connection was actually deleted.
261 bool removeConnection (ChannelType channelType
,
262 uint32 sourceNodeId
, uint sourceChannelIndex
,
263 uint32 destNodeId
, uint destChannelIndex
);
265 /** Removes all connections from the specified node. */
266 bool disconnectNode (uint32 nodeId
);
268 /** Returns true if the given connection's channel numbers map on to valid
269 channels at each end.
270 Even if a connection is valid when created, its status could change if
271 a node changes its channel config.
273 bool isConnectionLegal (const Connection
* connection
) const;
275 /** Performs a sanity checks of all the connections.
277 This might be useful if some of the processors are doing things like changing
278 their channel counts, which could render some connections obsolete.
280 bool removeIllegalConnections();
282 //==============================================================================
283 /** A special number that represents the midi channel of a node.
285 This is used as a channel index value if you want to refer to the midi input
286 or output instead of an audio channel.
288 static const uint midiChannelIndex
;
291 //==============================================================================
292 /** A special type of AudioProcessor that can live inside an AudioProcessorGraph
293 in order to use the audio that comes into and out of the graph itself.
295 If you create an AudioGraphIOProcessor in "input" mode, it will act as a
296 node in the graph which delivers the audio that is coming into the parent
297 graph. This allows you to stream the data to other nodes and process the
300 Likewise, one of these in "output" mode can be sent data which it will add to
301 the sum of data being sent to the graph's output.
303 @see AudioProcessorGraph
305 class AudioGraphIOProcessor
: public AudioProcessor
308 /** Specifies the mode in which this processor will operate.
312 audioInputNode
, /**< In this mode, the processor has output channels
313 representing all the audio input channels that are
314 coming into its parent audio graph. */
315 audioOutputNode
, /**< In this mode, the processor has input channels
316 representing all the audio output channels that are
317 going out of its parent audio graph. */
318 midiInputNode
, /**< In this mode, the processor has a midi output which
319 delivers the same midi data that is arriving at its
321 midiOutputNode
, /**< In this mode, the processor has a midi input and
322 any data sent to it will be passed out of the parent
324 cvInputNode
, cvOutputNode
,
327 //==============================================================================
328 /** Returns the mode of this processor. */
329 IODeviceType
getType() const noexcept
{ return type
; }
331 /** Returns the parent graph to which this processor belongs, or nullptr if it
332 hasn't yet been added to one. */
333 AudioProcessorGraph
* getParentGraph() const noexcept
{ return graph
; }
335 /** True if this is an audio or midi input. */
336 bool isInput() const noexcept
;
337 /** True if this is an audio or midi output. */
338 bool isOutput() const noexcept
;
340 //==============================================================================
341 AudioGraphIOProcessor (const IODeviceType type
);
342 ~AudioGraphIOProcessor();
344 const String
getName() const override
;
345 void prepareToPlay (double newSampleRate
, int estimatedSamplesPerBlock
) override
;
346 void releaseResources() override
;
347 void processBlockWithCV (AudioSampleBuffer
& audioBuffer
,
348 const AudioSampleBuffer
& cvInBuffer
,
349 AudioSampleBuffer
& cvOutBuffer
,
350 MidiBuffer
& midiMessages
) override
;
352 bool acceptsMidi() const override
;
353 bool producesMidi() const override
;
356 void setParentGraph (AudioProcessorGraph
*);
359 const IODeviceType type
;
360 AudioProcessorGraph
* graph
;
362 //==============================================================================
363 //void processAudio (AudioSampleBuffer& buffer, MidiBuffer& midiMessages);
364 void processAudioAndCV (AudioSampleBuffer
& audioBuffer
,
365 const AudioSampleBuffer
& cvInBuffer
,
366 AudioSampleBuffer
& cvOutBuffer
,
367 MidiBuffer
& midiMessages
);
369 CARLA_DECLARE_NON_COPYABLE (AudioGraphIOProcessor
)
372 //==============================================================================
373 const String
getName() const override
;
374 void prepareToPlay (double, int) override
;
375 void releaseResources() override
;
376 //void processBlock (AudioSampleBuffer&, MidiBuffer&) override;
377 void processBlockWithCV (AudioSampleBuffer
& audioBuffer
,
378 const AudioSampleBuffer
& cvInBuffer
,
379 AudioSampleBuffer
& cvOutBuffer
,
380 MidiBuffer
& midiMessages
) override
;
382 void reset() override
;
383 void setNonRealtime (bool) noexcept override
;
385 bool acceptsMidi() const override
;
386 bool producesMidi() const override
;
388 void reorderNowIfNeeded();
389 const CarlaRecursiveMutex
& getReorderMutex() const;
392 //==============================================================================
393 // void processAudio (AudioSampleBuffer& audioBuffer, MidiBuffer& midiMessages);
394 void processAudioAndCV (AudioSampleBuffer
& audioBuffer
,
395 const AudioSampleBuffer
& cvInBuffer
,
396 AudioSampleBuffer
& cvOutBuffer
,
397 MidiBuffer
& midiMessages
);
399 //==============================================================================
400 ReferenceCountedArray
<Node
> nodes
;
401 OwnedArray
<Connection
> connections
;
403 OwnedArray
<MidiBuffer
> midiBuffers
;
404 Array
<void*> renderingOps
;
406 friend class AudioGraphIOProcessor
;
407 struct AudioProcessorGraphBufferHelpers
;
408 CarlaScopedPointer
<AudioProcessorGraphBufferHelpers
> audioAndCVBuffers
;
410 MidiBuffer
* currentMidiInputBuffer
;
411 MidiBuffer currentMidiOutputBuffer
;
413 bool isPrepared
, needsReorder
;
414 CarlaRecursiveMutex reorderMutex
;
417 void clearRenderingSequence();
418 void buildRenderingSequence();
419 bool isAnInputTo (uint32 possibleInputId
, uint32 possibleDestinationId
, int recursionCheck
) const;
421 CARLA_DECLARE_NON_COPYABLE (AudioProcessorGraph
)
426 #endif // WATER_AUDIOPROCESSORGRAPH_H_INCLUDED