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 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;
79 void
80 prepare_for_processing (sample_rate_t sample_rate, nframes_t max_block_length)
81 final;
82 void release_resources () final;
83
84 // ============================================================================
85
86protected:
92 virtual void custom_process_block (
93 EngineProcessTimeInfo time_nfo,
94 const dsp::ITransport &transport) noexcept [[clang::nonblocking]];
95
96 virtual void custom_prepare_for_processing (
97 sample_rate_t sample_rate,
98 nframes_t max_block_length)
99 {
100 }
101
102 virtual void custom_release_resources () { }
103
104 auto dependencies () const { return dependencies_; }
105
106private:
107 static constexpr auto kProcessorNameKey = "processorName"sv;
108 static constexpr auto kInputPortsKey = "inputPorts"sv;
109 static constexpr auto kOutputPortsKey = "outputPorts"sv;
110 static constexpr auto kParametersKey = "parameters"sv;
111 friend void to_json (nlohmann::json &j, const ProcessorBase &p);
112 friend void from_json (const nlohmann::json &j, ProcessorBase &p);
113
114private:
115 ProcessorBaseDependencies dependencies_;
116 utils::Utf8String name_;
117 std::vector<dsp::PortUuidReference> input_ports_;
118 std::vector<dsp::PortUuidReference> output_ports_;
119 std::vector<dsp::ProcessorParameterUuidReference> params_;
120
121 // Caches
122 std::unique_ptr<BaseProcessingCache> processing_caches_;
123
124 BOOST_DESCRIBE_CLASS (ProcessorBase, (), (), (), (name_))
125};
126
136{
137public:
138 static void add_nodes (dsp::graph::Graph &graph, ProcessorBase &processor);
139 static void
140 add_connections (dsp::graph::Graph &graph, ProcessorBase &processor);
141};
142} // 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 release_resources() final
Called to release resources allocated by prepare_for_processing().
void prepare_for_processing(sample_rate_t sample_rate, nframes_t max_block_length) final
Called to allocate resources required 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:522
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:52
Lightweight UTF-8 string wrapper with safe conversions.
Definition utf8_string.h:38
uint32_t sample_rate_t
Sample rate.
Definition types.h:61
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:136