Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
processor_base.h
1// SPDX-FileCopyrightText: © 2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include <utility>
7
8#include "dsp/graph.h"
9#include "dsp/graph_node.h"
10#include "dsp/parameter.h"
11#include "dsp/port_all.h"
12
13#include <QObject>
14
15namespace zrythm::dsp
16{
17
24class ProcessorBase : public dsp::graph::IProcessable
25{
26 struct BaseProcessingCache
27 {
28 units::sample_rate_t sample_rate_;
29 nframes_t max_block_length_{};
30
31 std::vector<dsp::ProcessorParameter *> live_params_;
32 std::vector<dsp::PortPtrVariant> live_input_ports_;
33 std::vector<dsp::PortPtrVariant> live_output_ports_;
34 };
35
36public:
38 {
39 dsp::PortRegistry &port_registry_;
40 dsp::ProcessorParameterRegistry &param_registry_;
41 };
42
43 ProcessorBase (
44 ProcessorBaseDependencies dependencies,
45 utils::Utf8String name = { u8"ProcessorBase" });
46
47 ~ProcessorBase () override;
48
52 void set_name (const utils::Utf8String &name);
53
54 void add_input_port (const dsp::PortUuidReference &uuid);
55 void add_output_port (const dsp::PortUuidReference &uuid);
56 void add_parameter (const dsp::ProcessorParameterUuidReference &uuid);
57
58 auto &get_input_ports () const { return input_ports_; }
59 auto &get_output_ports () const { return output_ports_; }
60 auto &get_parameters () const { return params_; }
61
62 // ============================================================================
63 // IProcessable Interface
64 // ============================================================================
65
66 utils::Utf8String get_node_name () const final { return name_; }
67
77 EngineProcessTimeInfo time_nfo,
78 const dsp::ITransport &transport) noexcept final;
80 const graph::GraphNode * node,
81 units::sample_rate_t sample_rate,
82 nframes_t max_block_length) final;
83 void release_resources () final;
84
85 // ============================================================================
86
87protected:
93 virtual void custom_process_block (
94 EngineProcessTimeInfo time_nfo,
95 const dsp::ITransport &transport) noexcept [[clang::nonblocking]];
96
97 virtual void custom_prepare_for_processing (
98 const graph::GraphNode * node,
99 units::sample_rate_t sample_rate,
100 nframes_t max_block_length)
101 {
102 }
103
104 virtual void custom_release_resources () { }
105
106 auto dependencies () const { return dependencies_; }
107
108private:
109 static constexpr auto kProcessorNameKey = "processorName"sv;
110 static constexpr auto kInputPortsKey = "inputPorts"sv;
111 static constexpr auto kOutputPortsKey = "outputPorts"sv;
112 static constexpr auto kParametersKey = "parameters"sv;
113 friend void to_json (nlohmann::json &j, const ProcessorBase &p);
114 friend void from_json (const nlohmann::json &j, ProcessorBase &p);
115
116private:
117 ProcessorBaseDependencies dependencies_;
118 utils::Utf8String name_;
119 std::vector<dsp::PortUuidReference> input_ports_;
120 std::vector<dsp::PortUuidReference> output_ports_;
121 std::vector<dsp::ProcessorParameterUuidReference> params_;
122
123 // Caches
124 std::unique_ptr<BaseProcessingCache> processing_caches_;
125
126 BOOST_DESCRIBE_CLASS (ProcessorBase, (), (), (), (name_))
127};
128
138{
139public:
140 static void add_nodes (dsp::graph::Graph &graph, ProcessorBase &processor);
141 static void
142 add_connections (dsp::graph::Graph &graph, ProcessorBase &processor);
143};
144} // namespace zrythm::dsp
Interface for transport.
Definition itransport.h:17
A base class for processors in the DSP graph.
void process_block(EngineProcessTimeInfo time_nfo, const dsp::ITransport &transport) noexcept final
Calls custom_process_block() internally after processing all the parameters.
void prepare_for_processing(const graph::GraphNode *node, units::sample_rate_t sample_rate, nframes_t max_block_length) final
Called to allocate resources required for processing.
void release_resources() final
Called to release resources allocated by prepare_for_processing().
void set_name(const utils::Utf8String &name)
Set a custom name to be used in the DSP graph.
virtual void custom_process_block(EngineProcessTimeInfo time_nfo, const dsp::ITransport &transport) noexcept
Custom processor logic after processing all owned parameters.
utils::Utf8String get_node_name() const final
Returns a human friendly name of the node.
Helper class to insert nodes and connections pertaining to a ProcessorBase instance to a graph.
Wrapper over a Uuid registry that provides (slow) lookup by unique ID.
Definition parameter.h:523
Represents a node in a DSP graph.
Definition graph_node.h:129
The Graph class represents a graph of DSP nodes.
Definition graph.h:20
Interface for objects that can be processed in the DSP graph.
Definition graph_node.h:53
Lightweight UTF-8 string wrapper with safe conversions.
Definition utf8_string.h:38
uint32_t nframes_t
Frame count.
Definition types.h:58
Common struct to pass around during processing to avoid repeating the data in function arguments.
Definition types.h:133