Zrythm v2.0.0-alpha.1+31.4967fd053471
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
port.h
1// SPDX-FileCopyrightText: © 2018-2026 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include <functional>
7#include <stdexcept>
8#include <string_view>
9
10#include "dsp/graph_node.h"
11#include "dsp/port_connection.h"
12#include "dsp/port_fwd.h"
13#include "utils/typed_uuid_reference.h"
14#include "utils/utf8_string.h"
15#include "utils/uuid_identifiable_object.h"
16
17#include <fmt/format.h>
18
19using namespace std::string_view_literals;
20
21namespace zrythm::dsp
22{
23
37{
38 Q_OBJECT
39 QML_ELEMENT
40 QML_UNCREATABLE ("")
41
42 Q_DISABLE_COPY_MOVE (Port)
43public:
44 using FullDesignationProvider =
45 std::function<utils::Utf8String (const Port &port)>;
46
47 ~Port () override;
48
49 void set_full_designation_provider (FullDesignationProvider provider)
50 {
51 full_designation_provider_ = std::move (provider);
52 }
53
58 void set_full_designation_provider (const auto * owner)
59 {
60 full_designation_provider_ = [owner] (const Port &port) {
61 return owner->get_full_designation_for_port (port);
62 };
63 }
64
65 bool is_input () const { return flow_ == PortFlow::Input; }
66 bool is_output () const { return flow_ == PortFlow::Output; }
67
68 bool is_midi () const { return type_ == PortType::Midi; }
69 bool is_cv () const { return type_ == PortType::CV; }
70 bool is_audio () const { return type_ == PortType::Audio; }
71
72 utils::Utf8String get_label () const { return label_; }
73
74 auto get_symbol () const { return sym_; }
75 void set_symbol (const utils::Utf8String &sym) { sym_ = sym; }
76
77 // ========================================================================
78 // IProcessable Interface
79 // ========================================================================
80
82 {
83 return get_full_designation ();
84 }
85
86 // ========================================================================
87
91 virtual void clear_buffer (std::size_t offset, std::size_t nframes) = 0;
92
98 {
99 return full_designation_provider_ (*this);
100 }
101
102 bool has_label () const { return !label_.empty (); }
103 PortType type () const { return type_; }
104 PortFlow flow () const { return flow_; }
105
106protected:
107 Port (
108 utils::Utf8String label,
109 PortType type = {},
110 PortFlow flow = {},
111 QObject * parent = nullptr);
112
113 friend void
114 init_from (Port &obj, const Port &other, utils::ObjectCloneType clone_type);
115
116private:
117 static constexpr auto kFlowId = "flow"sv;
118 static constexpr auto kLabelId = "label"sv;
119 static constexpr auto kSymbolId = "symbol"sv;
120 static constexpr auto kPortGroupId = "portGroup"sv;
121 friend void to_json (nlohmann::json &j, const Port &p);
122 friend void from_json (const nlohmann::json &j, Port &p);
123
124private:
125 FullDesignationProvider full_designation_provider_ =
126 [this] (const Port &port) { return get_label (); };
127
129 PortType type_{};
131 PortFlow flow_{};
132
134 utils::Utf8String label_;
135
141 utils::Utf8String sym_;
142
144 std::optional<utils::Utf8String> port_group_;
145
146 BOOST_DESCRIBE_CLASS (
147 Port,
148 (utils::UuidIdentifiableObject<Port>),
149 (),
150 (),
151 (label_, sym_, port_group_))
152};
153
154template <typename PortT> class PortConnectionsCacheMixin
155{
156 using ElementType =
157 std::pair<const PortT *, std::unique_ptr<dsp::PortConnection>>;
158
159public:
160 virtual ~PortConnectionsCacheMixin () = default;
161
162 auto &port_sources () const { return port_sources_; }
163
164 void
165 set_port_sources (this auto &self, utils::RangeOf<PortT *> auto source_ports)
166 [[clang::blocking]]
167 {
168 self.port_sources_.clear ();
169 if (self.flow () != PortFlow::Input)
170 {
171 throw std::runtime_error (
172 fmt::format (
173 "Destination port '{}' must be an input port (is {})",
174 self.get_full_designation (),
175 self.flow () == PortFlow::Output ? "Output" : "Unknown"));
176 }
177 for (const auto &source_port : source_ports)
178 {
179 if (source_port->flow () != PortFlow::Output)
180 {
181 throw std::runtime_error (
182 fmt::format (
183 "Source port '{}' must be an output port (is {}), destination '{}'",
184 source_port->get_full_designation (),
185 source_port->flow () == PortFlow::Input ? "Input" : "Unknown",
186 self.get_full_designation ()));
187 }
188 self.port_sources_.push_back (
189 std::make_pair (
190 source_port,
191 std::make_unique<dsp::PortConnection> (
192 source_port->get_uuid (), self.get_uuid (), 1.f, true, true)));
193 }
194 }
195
196private:
202 std::vector<ElementType> port_sources_;
203 // std::vector<ElementType> port_destinations_;
204};
205
206using PortUuidReference = utils::TypedUuidReference<Port>;
207
208} // namespace zrythm::dsp
virtual void clear_buffer(std::size_t offset, std::size_t nframes)=0
Clears the port buffer.
utils::Utf8String get_full_designation() const
Gets a full designation of the port in the format "Track/Port" or "Track/Plugin/Port".
Definition port.h:97
void set_full_designation_provider(const auto *owner)
Convenience helper for providers that contain a get_full_designation_for_port() method.
Definition port.h:58
utils::Utf8String get_node_name() const override
Returns a human friendly name of the node.
Definition port.h:81
Interface for objects that can be processed in the DSP graph.
Definition graph_node.h:99
Typed, reference-counted UUID reference into an IObjectRegistry.
Lightweight UTF-8 string wrapper with safe conversions.
Definition utf8_string.h:37
CRTP base that adds a typed UUID strong-typedef to a class hierarchy.