1 /*
2  * Copyright (C) 2019-2021 Alexandros Theodotou <alex at zrythm dot org>
3  *
4  * This file is part of Zrythm
5  *
6  * Zrythm is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Zrythm is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with Zrythm.  If not, see <https://www.gnu.org/licenses/>.
18  *
19  * This file incorporates work covered by the following copyright and
20  * permission notice:
21  *
22  * Copyright (C) 2017, 2019 Robin Gareus <robin@gareus.org>
23  *
24  * This program is free software: you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation, either version 2 of the License, or
27  * (at your option) any later version.
28  *
29  * This program is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  * GNU General Public License for more details.
33  *
34  * You should have received a copy of the GNU General Public License
35  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
36  */
37 
38 /**
39  * \file
40  *
41  * Routing graph node.
42  */
43 
44 #ifndef __AUDIO_GRAPH_NODE_H__
45 #define __AUDIO_GRAPH_NODE_H__
46 
47 #include <stdbool.h>
48 
49 #include "utils/types.h"
50 
51 #include <gtk/gtk.h>
52 
53 typedef struct GraphNode GraphNode;
54 typedef struct Graph Graph;
55 typedef struct PassthroughProcessor
56   PassthroughProcessor;
57 typedef struct Port Port;
58 typedef struct Fader Fader;
59 typedef struct Track Track;
60 typedef struct SampleProcessor SampleProcessor;
61 typedef struct Plugin Plugin;
62 typedef struct HardwareProcessor HardwareProcessor;
63 typedef struct ModulatorMacroProcessor
64   ModulatorMacroProcessor;
65 typedef struct EngineProcessTimeInfo
66   EngineProcessTimeInfo;
67 
68 /**
69  * @addtogroup audio
70  *
71  * @{
72  */
73 
74 /**
75  * Graph nodes can be either ports or processors.
76  *
77  * Processors can be plugins, faders, etc.
78  */
79 typedef enum GraphNodeType
80 {
81   /** Port. */
82   ROUTE_NODE_TYPE_PORT,
83   /** Plugin processor. */
84   ROUTE_NODE_TYPE_PLUGIN,
85   /** Track processor. */
86   ROUTE_NODE_TYPE_TRACK,
87   /** Fader/pan processor. */
88   ROUTE_NODE_TYPE_FADER,
89   /** Fader/pan processor for monitor. */
90   ROUTE_NODE_TYPE_MONITOR_FADER,
91   /** Pre-Fader passthrough processor. */
92   ROUTE_NODE_TYPE_PREFADER,
93   /** Sample processor. */
94   ROUTE_NODE_TYPE_SAMPLE_PROCESSOR,
95 
96   /**
97    * Initial processor.
98    *
99    * The initial processor is a dummy processor
100    * in the chain processed before anything else.
101    */
102   ROUTE_NODE_TYPE_INITIAL_PROCESSOR,
103 
104   /** Hardware processor. */
105   ROUTE_NODE_TYPE_HW_PROCESSOR,
106 
107   ROUTE_NODE_TYPE_MODULATOR_MACRO_PROCESOR,
108 
109   /** Channel send. */
110   ROUTE_NODE_TYPE_CHANNEL_SEND,
111 } GraphNodeType;
112 
113 /**
114  * A node in the processing graph.
115  */
116 typedef struct GraphNode
117 {
118   int           id;
119   /** Ref back to the graph so we don't have to
120    * pass it around. */
121   Graph *      graph;
122 
123   /** outgoing edges
124    * downstream nodes to activate when this node
125    * has completed processed
126    */
127   GraphNode **  childnodes;
128   int           n_childnodes;
129 
130   /** Incoming node count. */
131   volatile gint refcount;
132 
133   /** Initial incoming node count. */
134   gint          init_refcount;
135 
136   /** Used when creating the graph so we can
137    * traverse it backwards to set the latencies. */
138   GraphNode **  parentnodes;
139 
140   /** Port, if not a plugin or fader. */
141   Port *        port;
142 
143   /** Plugin, if plugin. */
144   Plugin *      pl;
145 
146   /** Fader, if fader. */
147   Fader *       fader;
148 
149   Track *       track;
150 
151   /** Pre-Fader, if prefader node. */
152   Fader *       prefader;
153 
154   /** Sample processor, if sample processor. */
155   SampleProcessor * sample_processor;
156 
157   /** Hardware processor, if hardware processor. */
158   HardwareProcessor * hw_processor;
159 
160   ModulatorMacroProcessor * modulator_macro_processor;
161 
162   ChannelSend * send;
163 
164   /** For debugging. */
165   bool          terminal;
166   bool          initial;
167 
168   /** The playback latency of the node, in
169    * samples. */
170   nframes_t     playback_latency;
171 
172   /** The route's playback latency so far. */
173   nframes_t     route_playback_latency;
174 
175   GraphNodeType type;
176 } GraphNode;
177 
178 /**
179  * Returns a human friendly name of the node.
180  *
181  * Must be free'd.
182  */
183 char *
184 graph_node_get_name (
185   GraphNode * node);
186 
187 void *
188 graph_node_get_pointer (
189   GraphNode * self);
190 
191 void
192 graph_node_print_to_str (
193   GraphNode * node,
194   char *      buf,
195   size_t      buf_sz);
196 
197 void
198 graph_node_print (
199   GraphNode * node);
200 
201 /**
202  * Processes the GraphNode.
203  */
204 HOT
205 void
206 graph_node_process (
207   GraphNode *           node,
208   EngineProcessTimeInfo time_nfo);
209 
210 /**
211  * Returns the latency of only the given port,
212  * without adding the previous/next latencies.
213  *
214  * It returns the plugin's latency if plugin,
215  * otherwise 0.
216  */
217 nframes_t
218 graph_node_get_single_playback_latency (
219   GraphNode * node);
220 
221 /**
222  * Sets the playback latency of the given node
223  * recursively.
224  *
225  * Used only when (re)creating the graph.
226  *
227  * @param dest_latency The total destination
228  * latency so far.
229  */
230 void
231 graph_node_set_route_playback_latency (
232   GraphNode * node,
233   nframes_t   dest_latency);
234 
235 /**
236  * Called by an upstream node when it has completed
237  * processing.
238  */
239 HOT
240 void
241 graph_node_trigger (
242   GraphNode * self);
243 
244 //void
245 //graph_node_add_feeds (
246   //GraphNode * self,
247   //GraphNode * dest);
248 
249 //void
250 //graph_node_add_depends (
251   //GraphNode * self,
252   //GraphNode * src);
253 
254 void
255 graph_node_connect (
256   GraphNode * from,
257   GraphNode * to);
258 
259 GraphNode *
260 graph_node_new (
261   Graph * graph,
262   GraphNodeType type,
263   void *   data);
264 
265 void
266 graph_node_free (
267   GraphNode * node);
268 
269 /**
270  * @}
271  */
272 
273 #endif
274