Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
processor_base.h
1// SPDX-FileCopyrightText: © 2025-2026 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "dsp/graph.h"
7#include "dsp/graph_node.h"
8#include "dsp/parameter.h"
9#include "dsp/port_all.h"
10
11#include <QObject>
12
13namespace zrythm::dsp
14{
15
22class ProcessorBase : public dsp::graph::IProcessable
23{
24public:
35 {
36 struct Change
37 {
38 size_t index{};
39 float base_value{};
40 float automated_value{};
41 float modulated_value{};
43 };
44
46 const auto &changes () const { return changes_; }
47
48 private:
49 friend class ProcessorBase;
50
52 std::vector<Change> changes_;
53
55 std::vector<float> prev_values_;
56
59 void prepare (size_t count);
60
63 void record_if_changed (size_t i, dsp::ProcessorParameter * param)
64 {
65 float modulated = param->currentValue ();
66 if (!utils::math::floats_equal (prev_values_[i], modulated))
67 {
68 changes_.push_back (
69 { i, param->baseValue (), param->valueAfterAutomationApplied (),
70 modulated, param });
71 prev_values_[i] = modulated;
72 }
73 }
74
76 void clear () { changes_.clear (); }
77 };
78
79private:
80 struct BaseProcessingCache
81 {
82 units::sample_rate_t sample_rate_;
83 units::sample_u32_t max_block_length_{};
84
85 std::vector<dsp::ProcessorParameter *> live_params_;
86 std::vector<dsp::PortPtrVariant> live_input_ports_;
87 std::vector<dsp::PortPtrVariant> live_output_ports_;
88
89 ParameterChangeTracker change_tracker_;
90
98 bool is_processing_ = false;
99 };
100
101public:
103 {
104 dsp::PortRegistry &port_registry_;
105 dsp::ProcessorParameterRegistry &param_registry_;
106 };
107
108 ProcessorBase (
109 ProcessorBaseDependencies dependencies,
110 utils::Utf8String name = { u8"ProcessorBase" });
111
112 ~ProcessorBase () override;
113
117 void set_name (const utils::Utf8String &name);
118
119 void add_input_port (const dsp::PortUuidReference &uuid);
120 void add_output_port (const dsp::PortUuidReference &uuid);
121 void add_parameter (const dsp::ProcessorParameterUuidReference &uuid);
122
123 auto &get_input_ports () const { return input_ports_; }
124 auto &get_output_ports () const { return output_ports_; }
125 auto &get_parameters () const { return params_; }
126
133 const ParameterChangeTracker &change_tracker () const noexcept
134 {
135 assert (processing_caches_ && processing_caches_->is_processing_);
136 return processing_caches_->change_tracker_;
137 }
138
139 // ============================================================================
140 // IProcessable Interface
141 // ============================================================================
142
143 utils::Utf8String get_node_name () const final { return name_; }
144
155 const dsp::ITransport &transport,
156 const dsp::TempoMap &tempo_map) noexcept final;
158 const graph::GraphNode * node,
159 units::sample_rate_t sample_rate,
160 units::sample_u32_t max_block_length) final;
161 void release_resources () final;
162
163 // ============================================================================
164
165protected:
171 virtual void custom_process_block (
172 dsp::graph::ProcessBlockInfo time_nfo,
173 const dsp::ITransport &transport,
174 const dsp::TempoMap &tempo_map) noexcept [[clang::nonblocking]];
175
176 virtual void custom_prepare_for_processing (
177 const graph::GraphNode * node,
178 units::sample_rate_t sample_rate,
179 units::sample_u32_t max_block_length)
180 {
181 }
182
183 virtual void custom_release_resources () { }
184
185 auto dependencies () const { return dependencies_; }
186
187private:
188 static constexpr auto kProcessorNameKey = "processorName"sv;
189 static constexpr auto kInputPortsKey = "inputPorts"sv;
190 static constexpr auto kOutputPortsKey = "outputPorts"sv;
191 static constexpr auto kParametersKey = "parameters"sv;
192 friend void to_json (nlohmann::json &j, const ProcessorBase &p);
193 friend void from_json (const nlohmann::json &j, ProcessorBase &p);
194
195private:
196 ProcessorBaseDependencies dependencies_;
197 utils::Utf8String name_;
198 std::vector<dsp::PortUuidReference> input_ports_;
199 std::vector<dsp::PortUuidReference> output_ports_;
200 std::vector<dsp::ProcessorParameterUuidReference> params_;
201
202 // Caches
203 std::unique_ptr<BaseProcessingCache> processing_caches_;
204
205 BOOST_DESCRIBE_CLASS (ProcessorBase, (), (), (), (name_))
206};
207
217{
218public:
219 static void add_nodes (dsp::graph::Graph &graph, ProcessorBase &processor);
220 static void
221 add_connections (dsp::graph::Graph &graph, ProcessorBase &processor);
222};
223} // namespace zrythm::dsp
Interface for transport.
Definition itransport.h:16
A base class for processors in the DSP graph.
virtual void custom_process_block(dsp::graph::ProcessBlockInfo time_nfo, const dsp::ITransport &transport, const dsp::TempoMap &tempo_map) noexcept
Custom processor logic after processing all owned parameters.
const ParameterChangeTracker & change_tracker() const noexcept
Returns the change tracker.
void release_resources() final
Called to release resources allocated by prepare_for_processing().
void prepare_for_processing(const graph::GraphNode *node, units::sample_rate_t sample_rate, units::sample_u32_t max_block_length) final
Called to allocate resources required for processing.
void process_block(dsp::graph::ProcessBlockInfo time_nfo, const dsp::ITransport &transport, const dsp::TempoMap &tempo_map) noexcept final
Calls custom_process_block() internally after processing all the parameters.
void set_name(const utils::Utf8String &name)
Set a custom name to be used in the DSP graph.
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:542
Processor parameter that accepts automation and modulation sources and integrates with QML and the DS...
Definition parameter.h:252
Q_INVOKABLE float currentValue() const
Returns the current (normalized) value after any automation and modulation has been applied.
Definition parameter.h:333
Q_INVOKABLE float valueAfterAutomationApplied() const
Returns the value after automation, but before modulation has been applied.
Definition parameter.h:341
Represents a node in a DSP graph.
Definition graph_node.h:173
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:99
Lightweight UTF-8 string wrapper with safe conversions.
Definition utf8_string.h:37
constexpr bool floats_equal(T a, T b)
Checks if 2 floating point numbers are equal.
Definition math_utils.h:74
Tracks parameter value changes across processing cycles.
const auto & changes() const
Returns the changes accumulated during the current cycle.
Common struct to pass around during processing to avoid repeating the data in function arguments.
Definition graph_node.h:51