Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
graph_node.h
1// SPDX-FileCopyrightText: © 2019-2021, 2024-2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3/*
4 * This file incorporates work covered by the following copyright and
5 * permission notice:
6 *
7 * ---
8 *
9 * Copyright (C) 2017, 2019 Robin Gareus <robin@gareus.org>
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 *
24 * SPDX-License-Identifier: GPL-2.0-or-later
25 *
26 * ---
27 */
28
29#pragma once
30
31#include "dsp/itransport.h"
32#include "dsp/tempo_map.h"
33#include "utils/units.h"
34
35#include <QObject>
36
37namespace zrythm::utils
38{
39class Utf8String;
40}
41
42namespace zrythm::dsp::graph
43{
44class GraphNode;
45
51{
52public:
53 void print () const;
54
55public:
57 units::sample_u64_t g_start_frame_;
58
61 units::sample_u64_t g_start_frame_w_offset_;
62
65 units::sample_u32_t local_offset_;
66
70 units::sample_u32_t nframes_;
71};
72
86{
87public:
88 virtual ~IProcessable () { release_resources (); }
89
93 virtual utils::Utf8String get_node_name () const = 0;
94
99 [[gnu::hot]] virtual units::sample_u32_t get_single_playback_latency () const
100 {
101 return units::samples (0);
102 }
103
113 const GraphNode * node,
114 units::sample_rate_t sample_rate,
115 units::sample_u32_t max_block_length)
116 {
117 }
118
119 [[gnu::hot]] virtual void process_block (
121 const dsp::ITransport &transport,
122 const dsp::TempoMap &tempo_map) noexcept [[clang::nonblocking]] { };
123
130 virtual void release_resources () { }
131};
132
133class InitialProcessor final : public QObject, public IProcessable
134{
135 Q_OBJECT
136
137public:
139};
140
159class GraphNode
160{
161public:
162 using NodeId = int;
163
164 GraphNode (NodeId id, IProcessable &processable);
165 GraphNode (const GraphNode &) = delete;
166 GraphNode &operator= (const GraphNode &) = delete;
167 GraphNode (GraphNode &&) = delete;
168 GraphNode &operator= (GraphNode &&) = delete;
169 ~GraphNode () noexcept = default;
170
172 std::string print_node_to_str () const;
173
174 // For debugging purposes.
175 NodeId get_id () const { return node_id_; }
176
187 [[gnu::hot]] void process (
189 units::sample_u64_t remaining_preroll_frames,
190 const dsp::ITransport &transport,
191 const dsp::TempoMap &tempo_map) const;
192
193 units::sample_u32_t get_single_playback_latency () const
194 {
195 return processable_.get_single_playback_latency ();
196 }
197
205 void set_route_playback_latency (units::sample_u32_t dest_latency);
206
207 void connect_to (GraphNode &target);
208
217 void set_skip_processing (bool skip) { bypass_ = skip; }
218
219 IProcessable &get_processable () { return processable_; }
220
224 auto &feeds () const { return childnodes_; }
225
229 auto &depends () const { return parentnodes_; }
230
231 bool remove_feed (const GraphNode &feed);
232 bool remove_depend (const GraphNode &depend);
233
234private:
235 void add_feeds (GraphNode &dest);
236 void add_depends (GraphNode &src);
237
248 [[gnu::hot]] void compensate_latency (
250 units::sample_u32_t remaining_preroll_frames,
251 const dsp::ITransport &transport,
252 const dsp::TempoMap &tempo_map) const;
253
263 [[gnu::hot]] void process_chunks_after_splitting_at_loop_points (
265 const dsp::ITransport &transport,
266 const dsp::TempoMap &tempo_map) const;
267
268public:
270 std::atomic<int> refcount_ = 0;
271
278 units::sample_u32_t playback_latency_;
279
280 // TODO
287 units::sample_u32_t capture_latency_;
288
291
293 units::sample_u32_t route_playback_latency_;
294
295 /* For debugging. These are set by
296 * GraphNodeCollection.set_initial_and_terminal_nodes(). */
297 bool terminal_{ false };
298 bool initial_{ false };
299
300private:
301 NodeId node_id_ = 0;
302
309 std::vector<std::reference_wrapper<GraphNode>> parentnodes_;
310
316 std::vector<std::reference_wrapper<GraphNode>> childnodes_;
317
318 IProcessable &processable_;
319
323 bool bypass_ = false;
324};
325
333{
334public:
340 units::sample_u32_t get_max_route_playback_latency () const;
341
346
351
358
359 GraphNode * find_node_for_processable (const IProcessable &processable) const;
360
361public:
365 std::vector<std::unique_ptr<GraphNode>> graph_nodes_;
366
373 std::vector<std::reference_wrapper<GraphNode>> trigger_nodes_;
374
380 std::vector<std::reference_wrapper<GraphNode>> terminal_nodes_;
381
382 std::unique_ptr<InitialProcessor> initial_processor_;
383};
384
385} // namespace zrythm::dsp::graph
Interface for transport.
Definition itransport.h:16
Manages the collection of graph nodes.
Definition graph_node.h:333
void update_latencies()
Updates the latencies of all nodes.
void set_initial_and_terminal_nodes()
Updates the initial and terminal nodes based on graph_nodes_.
void finalize_nodes()
Sets the initial/terminal nodes.
Definition graph_node.h:357
std::vector< std::reference_wrapper< GraphNode > > trigger_nodes_
A subset of graph_nodes_ that are trigger nodes.
Definition graph_node.h:373
std::vector< std::reference_wrapper< GraphNode > > terminal_nodes_
A subset of graph_nodes_ that are terminal nodes.
Definition graph_node.h:380
units::sample_u32_t get_max_route_playback_latency() const
Returns the max playback latency of the trigger nodes.
std::vector< std::unique_ptr< GraphNode > > graph_nodes_
All nodes in the graph.
Definition graph_node.h:365
Represents a node in a DSP graph.
Definition graph_node.h:160
int init_refcount_
Initial incoming node count.
Definition graph_node.h:290
std::string print_node_to_str() const
For general debugging.
units::sample_u32_t capture_latency_
The capture latency of the node, in samples.
Definition graph_node.h:287
std::atomic< int > refcount_
Incoming node count.
Definition graph_node.h:270
auto & feeds() const
Read-only access to child nodes (outgoing connections).
Definition graph_node.h:224
units::sample_u32_t playback_latency_
The playback latency of the node, in samples.
Definition graph_node.h:278
void set_route_playback_latency(units::sample_u32_t dest_latency)
Sets the playback latency of the given node recursively.
units::sample_u32_t route_playback_latency_
The route's playback latency so far.
Definition graph_node.h:293
auto & depends() const
Read-only access to parent nodes (incoming connections).
Definition graph_node.h:229
void process(dsp::graph::EngineProcessTimeInfo time_nfo, units::sample_u64_t remaining_preroll_frames, const dsp::ITransport &transport, const dsp::TempoMap &tempo_map) const
Processes the GraphNode.
void set_skip_processing(bool skip)
Sets whether processing should be skipped for this node.
Definition graph_node.h:217
Interface for objects that can be processed in the DSP graph.
Definition graph_node.h:86
virtual void prepare_for_processing(const GraphNode *node, units::sample_rate_t sample_rate, units::sample_u32_t max_block_length)
Called to allocate resources required for processing.
Definition graph_node.h:112
virtual utils::Utf8String get_node_name() const =0
Returns a human friendly name of the node.
virtual void release_resources()
Called to release resources allocated by prepare_for_processing().
Definition graph_node.h:130
virtual units::sample_u32_t get_single_playback_latency() const
Returns the latency of only the given processable, without adding the previous/next latencies (zero l...
Definition graph_node.h:99
utils::Utf8String get_node_name() const override
Returns a human friendly name of the node.
Lightweight UTF-8 string wrapper with safe conversions.
Definition utf8_string.h:37
String utilities.
Common struct to pass around during processing to avoid repeating the data in function arguments.
Definition graph_node.h:51
units::sample_u64_t g_start_frame_
Global position at the start of the processing cycle (no offset added).
Definition graph_node.h:57
units::sample_u32_t nframes_
Number of frames to process in this call, starting from the offset.
Definition graph_node.h:70
units::sample_u32_t local_offset_
Offset in the current processing cycle, between 0 and the number of frames in AudioEngine....
Definition graph_node.h:65
units::sample_u64_t g_start_frame_w_offset_
Global position with dsp::graph::EngineProcessTimeInfo.local_offset added, for convenience.
Definition graph_node.h:61