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
55 static constexpr ProcessBlockInfo from_position_and_nframes (
56 units::sample_u64_t transport_position,
57 units::sample_u32_t nframes)
58 {
59 return {
60 .transport_position_ = transport_position,
61 .buffer_offset_ = units::samples (0),
62 .nframes_ = nframes
63 };
64 }
65
66 [[gnu::hot]] constexpr units::sample_u64_t end_position () const noexcept
67 {
69 }
70
71public:
74 units::sample_u64_t transport_position_;
75
78 units::sample_u32_t buffer_offset_;
79
83 units::sample_u32_t nframes_;
84};
85
99{
100public:
101 virtual ~IProcessable () { release_resources (); }
102
106 virtual utils::Utf8String get_node_name () const = 0;
107
112 [[gnu::hot]] virtual units::sample_u32_t get_single_playback_latency () const
113 {
114 return units::samples (0);
115 }
116
126 const GraphNode * node,
127 units::sample_rate_t sample_rate,
128 units::sample_u32_t max_block_length)
129 {
130 }
131
132 [[gnu::hot]] virtual void process_block (
134 const dsp::ITransport &transport,
135 const dsp::TempoMap &tempo_map) noexcept [[clang::nonblocking]] { };
136
143 virtual void release_resources () { }
144};
145
146class InitialProcessor final : public QObject, public IProcessable
147{
148 Q_OBJECT
149
150public:
152};
153
172class GraphNode
173{
174public:
175 using NodeId = int;
176
177 GraphNode (NodeId id, IProcessable &processable);
178 GraphNode (const GraphNode &) = delete;
179 GraphNode &operator= (const GraphNode &) = delete;
180 GraphNode (GraphNode &&) = delete;
181 GraphNode &operator= (GraphNode &&) = delete;
182 ~GraphNode () noexcept = default;
183
185 std::string print_node_to_str () const;
186
187 // For debugging purposes.
188 NodeId get_id () const { return node_id_; }
189
200 [[gnu::hot]] void process (
202 units::sample_u64_t remaining_preroll_frames,
203 const dsp::ITransport &transport,
204 const dsp::TempoMap &tempo_map) const;
205
206 units::sample_u32_t get_single_playback_latency () const
207 {
208 return processable_.get_single_playback_latency ();
209 }
210
218 void set_route_playback_latency (units::sample_u32_t dest_latency);
219
220 void connect_to (GraphNode &target);
221
230 void set_skip_processing (bool skip) { bypass_ = skip; }
231
232 IProcessable &get_processable () { return processable_; }
233
237 auto &feeds () const { return childnodes_; }
238
242 auto &depends () const { return parentnodes_; }
243
244 bool remove_feed (const GraphNode &feed);
245 bool remove_depend (const GraphNode &depend);
246
247private:
248 void add_feeds (GraphNode &dest);
249 void add_depends (GraphNode &src);
250
261 [[gnu::hot]] void compensate_latency (
263 units::sample_u32_t remaining_preroll_frames,
264 const dsp::ITransport &transport,
265 const dsp::TempoMap &tempo_map) const;
266
276 [[gnu::hot]] void process_chunks_after_splitting_at_loop_points (
278 const dsp::ITransport &transport,
279 const dsp::TempoMap &tempo_map) const;
280
281public:
283 std::atomic<int> refcount_ = 0;
284
291 units::sample_u32_t playback_latency_;
292
293 // TODO
300 units::sample_u32_t capture_latency_;
301
304
306 units::sample_u32_t route_playback_latency_;
307
308 /* For debugging. These are set by
309 * GraphNodeCollection.set_initial_and_terminal_nodes(). */
310 bool terminal_{ false };
311 bool initial_{ false };
312
313private:
314 NodeId node_id_ = 0;
315
322 std::vector<std::reference_wrapper<GraphNode>> parentnodes_;
323
329 std::vector<std::reference_wrapper<GraphNode>> childnodes_;
330
331 IProcessable &processable_;
332
336 bool bypass_ = false;
337};
338
346{
347public:
353 units::sample_u32_t get_max_route_playback_latency () const;
354
359
364
371
372 GraphNode * find_node_for_processable (const IProcessable &processable) const;
373
374public:
378 std::vector<std::unique_ptr<GraphNode>> graph_nodes_;
379
386 std::vector<std::reference_wrapper<GraphNode>> trigger_nodes_;
387
393 std::vector<std::reference_wrapper<GraphNode>> terminal_nodes_;
394
395 std::unique_ptr<InitialProcessor> initial_processor_;
396};
397
398} // namespace zrythm::dsp::graph
Interface for transport.
Definition itransport.h:16
Manages the collection of graph nodes.
Definition graph_node.h:346
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:370
std::vector< std::reference_wrapper< GraphNode > > trigger_nodes_
A subset of graph_nodes_ that are trigger nodes.
Definition graph_node.h:386
std::vector< std::reference_wrapper< GraphNode > > terminal_nodes_
A subset of graph_nodes_ that are terminal nodes.
Definition graph_node.h:393
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:378
Represents a node in a DSP graph.
Definition graph_node.h:173
int init_refcount_
Initial incoming node count.
Definition graph_node.h:303
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:300
std::atomic< int > refcount_
Incoming node count.
Definition graph_node.h:283
auto & feeds() const
Read-only access to child nodes (outgoing connections).
Definition graph_node.h:237
units::sample_u32_t playback_latency_
The playback latency of the node, in samples.
Definition graph_node.h:291
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:306
auto & depends() const
Read-only access to parent nodes (incoming connections).
Definition graph_node.h:242
void process(dsp::graph::ProcessBlockInfo 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:230
Interface for objects that can be processed in the DSP graph.
Definition graph_node.h:99
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:125
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:143
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:112
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_u32_t buffer_offset_
Offset in the current processing cycle's audio buffer, between 0 and the number of frames in AudioEng...
Definition graph_node.h:78
units::sample_u32_t nframes_
Number of frames to process in this call, starting from the offset.
Definition graph_node.h:83
units::sample_u64_t transport_position_
Transport (timeline) position at the start of this chunk (already taking into account the offset).
Definition graph_node.h:74