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 #include <inttypes.h>
39 #include <stdlib.h>
40
41 #include "audio/engine.h"
42 #include "audio/fader.h"
43 #include "audio/graph.h"
44 #include "audio/graph_node.h"
45 #include "audio/master_track.h"
46 #include "audio/midi_event.h"
47 #include "audio/port.h"
48 #include "audio/router.h"
49 #include "audio/sample_processor.h"
50 #include "audio/tempo_track.h"
51 #include "audio/track.h"
52 #include "audio/track_processor.h"
53 #include "audio/tracklist.h"
54 #include "audio/transport.h"
55 #include "plugins/plugin.h"
56 #include "project.h"
57 #include "utils/arrays.h"
58 #include "utils/mpmc_queue.h"
59 #include "utils/objects.h"
60
61 #include <gtk/gtk.h>
62
63 /**
64 * Returns a human friendly name of the node.
65 *
66 * Must be free'd.
67 */
68 char *
graph_node_get_name(GraphNode * node)69 graph_node_get_name (
70 GraphNode * node)
71 {
72 char str[600];
73 switch (node->type)
74 {
75 case ROUTE_NODE_TYPE_PLUGIN:
76 {
77 Track * track = plugin_get_track (node->pl);
78 return
79 g_strdup_printf (
80 "%s/%s (Plugin)",
81 track->name,
82 node->pl->setting->descr->name);
83 }
84 case ROUTE_NODE_TYPE_PORT:
85 port_get_full_designation (node->port, str);
86 return g_strdup (str);
87 case ROUTE_NODE_TYPE_FADER:
88 {
89 Track * track =
90 fader_get_track (node->fader);
91 return
92 g_strdup_printf (
93 "%s Fader", track->name);
94 }
95 case ROUTE_NODE_TYPE_MODULATOR_MACRO_PROCESOR:
96 {
97 ModulatorMacroProcessor * mmp =
98 node->modulator_macro_processor;
99 Track * track =
100 port_get_track (mmp->cv_in, true);
101 return
102 g_strdup_printf (
103 "%s Modulator Macro Processor",
104 track->name);
105 }
106 case ROUTE_NODE_TYPE_TRACK:
107 return
108 g_strdup (
109 node->track->name);
110 case ROUTE_NODE_TYPE_PREFADER:
111 {
112 Track * track =
113 fader_get_track (node->prefader);
114 return
115 g_strdup_printf (
116 "%s Pre-Fader", track->name);
117 }
118 case ROUTE_NODE_TYPE_MONITOR_FADER:
119 return
120 g_strdup ("Monitor Fader");
121 case ROUTE_NODE_TYPE_SAMPLE_PROCESSOR:
122 return
123 g_strdup ("Sample Processor");
124 case ROUTE_NODE_TYPE_INITIAL_PROCESSOR:
125 return
126 g_strdup ("Initial Processor");
127 case ROUTE_NODE_TYPE_HW_PROCESSOR:
128 return
129 g_strdup ("HW Processor");
130 case ROUTE_NODE_TYPE_CHANNEL_SEND:
131 {
132 Track * track =
133 channel_send_get_track (node->send);
134 return
135 g_strdup_printf (
136 "%s/Channel Send %d",
137 track->name, node->send->slot + 1);
138 }
139 }
140 g_return_val_if_reached (NULL);
141 }
142
143 void *
graph_node_get_pointer(GraphNode * node)144 graph_node_get_pointer (
145 GraphNode * node)
146 {
147 switch (node->type)
148 {
149 case ROUTE_NODE_TYPE_PORT:
150 return node->port;
151 break;
152 case ROUTE_NODE_TYPE_PLUGIN:
153 return node->pl;
154 break;
155 case ROUTE_NODE_TYPE_TRACK:
156 return node->track;
157 break;
158 case ROUTE_NODE_TYPE_FADER:
159 case ROUTE_NODE_TYPE_MONITOR_FADER:
160 return node->fader;
161 break;
162 case ROUTE_NODE_TYPE_PREFADER:
163 return node->prefader;
164 break;
165 case ROUTE_NODE_TYPE_SAMPLE_PROCESSOR:
166 return node->sample_processor;
167 break;
168 case ROUTE_NODE_TYPE_INITIAL_PROCESSOR:
169 return NULL;
170 case ROUTE_NODE_TYPE_HW_PROCESSOR:
171 return node->hw_processor;
172 case ROUTE_NODE_TYPE_MODULATOR_MACRO_PROCESOR:
173 return node->modulator_macro_processor;
174 case ROUTE_NODE_TYPE_CHANNEL_SEND:
175 return node->send;
176 }
177 g_return_val_if_reached (NULL);
178 }
179
180 void
graph_node_print_to_str(GraphNode * node,char * buf,size_t buf_sz)181 graph_node_print_to_str (
182 GraphNode * node,
183 char * buf,
184 size_t buf_sz)
185 {
186 GraphNode * dest;
187 if (!node)
188 {
189 g_message ("(null) node");
190 return;
191 }
192
193 char * name = graph_node_get_name (node);
194 char * str1 =
195 g_strdup_printf (
196 "node [(%d) %s] refcount: %d | terminal: %s | initial: %s | playback latency: %d",
197 node->id,
198 name,
199 node->refcount,
200 node->terminal ? "yes" : "no",
201 node->initial ? "yes" : "no",
202 node->playback_latency);
203 g_free (name);
204 char * str2;
205 for (int j = 0; j < node->n_childnodes; j++)
206 {
207 dest = node->childnodes[j];
208 name = graph_node_get_name (dest);
209 str2 =
210 g_strdup_printf ("%s (dest [(%d) %s])",
211 str1, dest->id, name);
212 g_free (str1);
213 g_free (name);
214 str1 = str2;
215 }
216 strncpy (buf, str1, buf_sz);
217 g_free (str1);
218 }
219
220 void
graph_node_print(GraphNode * node)221 graph_node_print (
222 GraphNode * node)
223 {
224 size_t sz = 2000;
225 char str[sz];
226 graph_node_print_to_str (node, str, sz);
227 g_message ("%s", str);
228 }
229
230 static void
on_node_finish(GraphNode * self)231 on_node_finish (
232 GraphNode * self)
233 {
234 int feeds = 0;
235
236 /* notify downstream nodes that depend on this
237 * node */
238 for (int i = 0; i < self->n_childnodes; ++i)
239 {
240 #if 0
241 /* set the largest playback latency of this
242 * route to the child as well */
243 self->childnodes[i]->route_playback_latency =
244 self->playback_latency;
245 /*MAX (*/
246 /*self->playback_latency,*/
247 /*self->childnodes[i]->*/
248 /*route_playback_latency);*/
249 #endif
250 graph_node_trigger (self->childnodes[i]);
251 feeds = 1;
252 }
253
254 /* if there are no outgoing edges, this is a
255 * terminal node */
256 if (!feeds)
257 {
258 /* notify parent graph */
259 graph_on_reached_terminal_node (self->graph);
260 }
261 }
262
263 HOT
264 static void
process_node(const GraphNode * node,const EngineProcessTimeInfo * const time_nfo)265 process_node (
266 const GraphNode * node,
267 const EngineProcessTimeInfo * const time_nfo)
268 {
269 #define g_start_frames (time_nfo->g_start_frames)
270 #define local_offset (time_nfo->local_offset)
271 #define nframes (time_nfo->nframes)
272
273 switch (node->type)
274 {
275 case ROUTE_NODE_TYPE_PLUGIN:
276 plugin_process (
277 node->pl, g_start_frames, local_offset,
278 nframes);
279 break;
280 case ROUTE_NODE_TYPE_FADER:
281 fader_process (
282 node->fader, g_start_frames,
283 local_offset, nframes);
284 break;
285 case ROUTE_NODE_TYPE_MODULATOR_MACRO_PROCESOR:
286 modulator_macro_processor_process (
287 node->modulator_macro_processor,
288 g_start_frames, local_offset, nframes);
289 break;
290 case ROUTE_NODE_TYPE_MONITOR_FADER:
291 fader_process (
292 node->fader, g_start_frames,
293 local_offset, nframes);
294 break;
295 case ROUTE_NODE_TYPE_PREFADER:
296 fader_process (
297 node->prefader, g_start_frames,
298 local_offset, nframes);
299 break;
300 case ROUTE_NODE_TYPE_SAMPLE_PROCESSOR:
301 sample_processor_process (
302 node->sample_processor, local_offset,
303 nframes);
304 break;
305 case ROUTE_NODE_TYPE_CHANNEL_SEND:
306 channel_send_process (
307 node->send, local_offset, nframes);
308 break;
309 case ROUTE_NODE_TYPE_TRACK:
310 {
311 Track * track = node->track;
312 if (!IS_TRACK (track))
313 {
314 g_return_if_reached ();
315 }
316 if (track->type != TRACK_TYPE_TEMPO &&
317 track->type != TRACK_TYPE_MARKER)
318 {
319 track_processor_process (
320 track->processor, time_nfo);
321 }
322 }
323 break;
324 case ROUTE_NODE_TYPE_PORT:
325 {
326 /* decide what to do based on what port it
327 * is */
328 Port * port = node->port;
329 /*PortIdentifier * id = &port->id;*/
330
331 /* if midi editor manual press */
332 if (port ==
333 AUDIO_ENGINE->
334 midi_editor_manual_press)
335 {
336 midi_events_dequeue (
337 AUDIO_ENGINE->
338 midi_editor_manual_press->
339 midi_events);
340 }
341
342 /* if exporting and the port is not a
343 * project port, ignore it */
344 else if (
345 engine_is_port_own (AUDIO_ENGINE, port)
346 && AUDIO_ENGINE->exporting)
347 {
348 }
349
350 else
351 {
352 port_process (port, time_nfo, false);
353 }
354 }
355 break;
356 default:
357 break;
358 }
359
360 #undef g_start_frames
361 #undef local_offset
362 #undef nframes
363 }
364
365 /**
366 * Processes the GraphNode.
367 */
368 void
graph_node_process(GraphNode * node,EngineProcessTimeInfo time_nfo)369 graph_node_process (
370 GraphNode * node,
371 EngineProcessTimeInfo time_nfo)
372 {
373 g_return_if_fail (
374 node && node->graph && node->graph->router);
375
376 /*g_message (*/
377 /*"processing %s", graph_node_get_name (node));*/
378
379 /* skip BPM during cycle (already processed in
380 * router_start_cycle()) */
381 if (G_UNLIKELY (
382 node->graph->router->callback_in_progress &&
383 node->port &&
384 node->port == P_TEMPO_TRACK->bpm_port))
385 {
386 goto node_process_finish;
387 }
388
389 /* figure out if we are doing a no-roll */
390 if (node->route_playback_latency <
391 AUDIO_ENGINE->remaining_latency_preroll)
392 {
393 /* no roll */
394 if (node->type == ROUTE_NODE_TYPE_PLUGIN)
395 {
396 /*g_message (*/
397 /*"-- not processing: %s "*/
398 /*"route latency %ld",*/
399 /*node->type == ROUTE_NODE_TYPE_PLUGIN ?*/
400 /*node->pl->descr->name :*/
401 /*node->port->identifier.label,*/
402 /*node->route_playback_latency);*/
403 }
404
405 /* if no-roll, only process terminal nodes
406 * to set their buffers to 0 */
407 goto node_process_finish;
408 /*if (!node->terminal)*/
409 /*{*/
410 /*}*/
411 }
412
413 /* only compensate latency when rolling */
414 if (TRANSPORT->play_state == PLAYSTATE_ROLLING)
415 {
416 /* if the playhead is before the loop-end
417 * point and the latency-compensated position
418 * is after the loop-end point it means that
419 * the loop was crossed, so compensate for
420 * that.
421 *
422 * if the position is before loop-end and
423 * position + frames is after loop end (there
424 * is a loop inside the range), that should be
425 * handled by the ports/processors instead */
426 Position playhead_copy = *PLAYHEAD;
427 g_warn_if_fail (
428 node->route_playback_latency >=
429 AUDIO_ENGINE->remaining_latency_preroll);
430 transport_position_add_frames (
431 TRANSPORT, &playhead_copy,
432 node->route_playback_latency -
433 AUDIO_ENGINE->remaining_latency_preroll);
434 time_nfo.g_start_frames =
435 playhead_copy.frames;
436 }
437
438 /* split at loop points */
439 for (nframes_t num_processable_frames = 0;
440 (num_processable_frames =
441 MIN (
442 transport_is_loop_point_met (
443 TRANSPORT, time_nfo.g_start_frames,
444 time_nfo.nframes),
445 time_nfo.nframes)) != 0;)
446 {
447 #if 0
448 g_message (
449 "splitting from %ld "
450 "(num processable frames %"
451 PRIu32 ")",
452 g_start_frames, num_processable_frames);
453 #endif
454
455 /* temporarily change the nframes to avoid
456 * having to declare a separate
457 * EngineProcessTimeInfo */
458 nframes_t orig_nframes = time_nfo.nframes;
459 time_nfo.nframes =
460 num_processable_frames;
461 process_node (node, &time_nfo);
462
463 /* calculate the remaining frames */
464 time_nfo.nframes =
465 orig_nframes - num_processable_frames;
466
467 /* loop back to loop start */
468 time_nfo.g_start_frames =
469 (time_nfo.g_start_frames
470 + num_processable_frames
471 + TRANSPORT->loop_start_pos.frames)
472 - TRANSPORT->loop_end_pos.frames;
473 time_nfo.local_offset +=
474 num_processable_frames;
475 }
476
477 if (time_nfo.nframes > 0)
478 {
479 process_node (node, &time_nfo);
480 }
481
482 node_process_finish:
483 if (node->graph->router->callback_in_progress)
484 {
485 on_node_finish (node);
486 }
487 }
488
489 /**
490 * Called by an upstream node when it has completed
491 * processing.
492 */
493 void
graph_node_trigger(GraphNode * self)494 graph_node_trigger (
495 GraphNode * self)
496 {
497 /* check if we can run */
498 if (g_atomic_int_dec_and_test (&self->refcount))
499 {
500 /* reset reference count for next cycle */
501 g_atomic_int_set (
502 &self->refcount,
503 (unsigned int) self->init_refcount);
504
505 /* all nodes that feed this node have
506 * completed, so this node be processed
507 * now. */
508 g_atomic_int_inc (
509 &self->graph->trigger_queue_size);
510 /*g_message ("triggering node, pushing back");*/
511 mpmc_queue_push_back_node (
512 self->graph->trigger_queue, self);
513 }
514 }
515
516 static void
add_feeds(GraphNode * self,GraphNode * dest)517 add_feeds (
518 GraphNode * self,
519 GraphNode * dest)
520 {
521 /* return if already added */
522 for (int i = 0; i < self->n_childnodes; i++)
523 {
524 if (self->childnodes[i] == dest)
525 {
526 return;
527 }
528 }
529
530 self->childnodes =
531 (GraphNode **) g_realloc (
532 self->childnodes,
533 (size_t) (1 + self->n_childnodes) *
534 sizeof (GraphNode *));
535 self->childnodes[self->n_childnodes++] = dest;
536
537 self->terminal = false;
538 }
539
540 static void
add_depends(GraphNode * self,GraphNode * src)541 add_depends (
542 GraphNode * self,
543 GraphNode * src)
544 {
545 ++self->init_refcount;
546 self->refcount = self->init_refcount;
547
548 /* add parent nodes */
549 self->parentnodes =
550 (GraphNode **) g_realloc (
551 self->parentnodes,
552 (size_t) (self->init_refcount) *
553 sizeof (GraphNode *));
554
555 self->parentnodes[self->init_refcount - 1] =
556 src;
557
558 self->initial = false;
559 }
560
561 /**
562 * Returns the latency of only the given port,
563 * without adding the previous/next latencies.
564 *
565 * It returns the plugin's latency if plugin,
566 * otherwise 0.
567 */
568 nframes_t
graph_node_get_single_playback_latency(GraphNode * node)569 graph_node_get_single_playback_latency (
570 GraphNode * node)
571 {
572 switch (node->type)
573 {
574 case ROUTE_NODE_TYPE_PLUGIN:
575 /* latency is already set at this point */
576 return node->pl->latency;
577 case ROUTE_NODE_TYPE_TRACK:
578 return 0;
579 default:
580 break;
581 }
582
583 return 0;
584 }
585
586 /**
587 * Sets the playback latency of the given node
588 * recursively.
589 *
590 * Used only when (re)creating the graph.
591 *
592 * @param dest_latency The total destination
593 * latency so far.
594 */
595 void
graph_node_set_route_playback_latency(GraphNode * node,nframes_t dest_latency)596 graph_node_set_route_playback_latency (
597 GraphNode * node,
598 nframes_t dest_latency)
599 {
600 /*long max_latency = 0, parent_latency;*/
601
602 /* set route playback latency */
603 if (dest_latency > node->route_playback_latency)
604 {
605 node->route_playback_latency = dest_latency;
606 }
607
608 GraphNode * parent;
609 for (int i = 0; i < node->init_refcount; i++)
610 {
611 parent = node->parentnodes[i];
612 graph_node_set_route_playback_latency (
613 parent, node->route_playback_latency);
614 #if 0
615 g_message (
616 "added %d route playback latency from node %s to "
617 "parent %s. Total route latency on parent: %d",
618 dest_latency,
619 graph_node_get_name (node),
620 graph_node_get_name (parent),
621 parent->route_playback_latency);
622 #endif
623 }
624 }
625
626 void
graph_node_connect(GraphNode * from,GraphNode * to)627 graph_node_connect (
628 GraphNode * from,
629 GraphNode * to)
630 {
631 g_return_if_fail (from && to);
632 if (array_contains (
633 from->childnodes,
634 from->n_childnodes,
635 to))
636 return;
637
638 add_feeds (from, to);
639 add_depends (to, from);
640
641 g_warn_if_fail (!from->terminal && !to->initial);
642 }
643
644 GraphNode *
graph_node_new(Graph * graph,GraphNodeType type,void * data)645 graph_node_new (
646 Graph * graph,
647 GraphNodeType type,
648 void * data)
649 {
650 GraphNode * node = object_new (GraphNode);
651 node->id =
652 (int)
653 g_hash_table_size (graph->setup_graph_nodes);
654 node->graph = graph;
655 node->type = type;
656 switch (type)
657 {
658 case ROUTE_NODE_TYPE_PLUGIN:
659 node->pl = (Plugin *) data;
660 break;
661 case ROUTE_NODE_TYPE_PORT:
662 node->port = (Port *) data;
663 break;
664 case ROUTE_NODE_TYPE_FADER:
665 node->fader = (Fader *) data;
666 break;
667 case ROUTE_NODE_TYPE_MONITOR_FADER:
668 node->fader = (Fader *) data;
669 break;
670 case ROUTE_NODE_TYPE_PREFADER:
671 node->prefader = (Fader *) data;
672 break;
673 case ROUTE_NODE_TYPE_SAMPLE_PROCESSOR:
674 node->sample_processor =
675 (SampleProcessor *) data;
676 break;
677 case ROUTE_NODE_TYPE_TRACK:
678 node->track = (Track *) data;
679 /* set cache */
680 node->track->name_hash =
681 track_get_name_hash (node->track);
682 break;
683 case ROUTE_NODE_TYPE_INITIAL_PROCESSOR:
684 break;
685 case ROUTE_NODE_TYPE_HW_PROCESSOR:
686 node->hw_processor =
687 (HardwareProcessor *) data;
688 break;
689 case ROUTE_NODE_TYPE_MODULATOR_MACRO_PROCESOR:
690 node->modulator_macro_processor =
691 (ModulatorMacroProcessor *) data;
692 break;
693 case ROUTE_NODE_TYPE_CHANNEL_SEND:
694 node->send = (ChannelSend *) data;
695 break;
696 default:
697 g_return_val_if_reached (node);
698 }
699
700 return node;
701 }
702
703 void
graph_node_free(GraphNode * self)704 graph_node_free (
705 GraphNode * self)
706 {
707 free (self->childnodes);
708 free (self->parentnodes);
709
710 object_zero_and_free (self);
711 }
712