Zrythm v2.0.0-alpha.1
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 <string_view>
8
9#include "dsp/graph_node.h"
10#include "dsp/port_connection.h"
11#include "dsp/port_fwd.h"
12#include "utils/typed_uuid_reference.h"
13#include "utils/utf8_string.h"
14#include "utils/uuid_identifiable_object.h"
15
16using namespace std::string_view_literals;
17
18namespace zrythm::dsp
19{
20
34{
35 Q_OBJECT
36 Q_DISABLE_COPY_MOVE (Port)
37public:
38 using FullDesignationProvider =
39 std::function<utils::Utf8String (const Port &port)>;
40
41 ~Port () override;
42
43 void set_full_designation_provider (FullDesignationProvider provider)
44 {
45 full_designation_provider_ = std::move (provider);
46 }
47
52 void set_full_designation_provider (const auto * owner)
53 {
54 full_designation_provider_ = [owner] (const Port &port) {
55 return owner->get_full_designation_for_port (port);
56 };
57 }
58
59 bool is_input () const { return flow_ == PortFlow::Input; }
60 bool is_output () const { return flow_ == PortFlow::Output; }
61
62 bool is_midi () const { return type_ == PortType::Midi; }
63 bool is_cv () const { return type_ == PortType::CV; }
64 bool is_audio () const { return type_ == PortType::Audio; }
65
66 utils::Utf8String get_label () const { return label_; }
67
68 auto get_symbol () const { return sym_; }
69 void set_symbol (const utils::Utf8String &sym) { sym_ = sym; }
70
71 // ========================================================================
72 // IProcessable Interface
73 // ========================================================================
74
76 {
77 return get_full_designation ();
78 }
79
80 // ========================================================================
81
85 virtual void clear_buffer (std::size_t offset, std::size_t nframes) = 0;
86
92 {
93 return full_designation_provider_ (*this);
94 }
95
96 bool has_label () const { return !label_.empty (); }
97 PortType type () const { return type_; }
98 PortFlow flow () const { return flow_; }
99
100protected:
101 Port (
102 utils::Utf8String label,
103 PortType type = {},
104 PortFlow flow = {},
105 QObject * parent = nullptr);
106
107 friend void
108 init_from (Port &obj, const Port &other, utils::ObjectCloneType clone_type);
109
110private:
111 static constexpr auto kFlowId = "flow"sv;
112 static constexpr auto kLabelId = "label"sv;
113 static constexpr auto kSymbolId = "symbol"sv;
114 static constexpr auto kPortGroupId = "portGroup"sv;
115 friend void to_json (nlohmann::json &j, const Port &p);
116 friend void from_json (const nlohmann::json &j, Port &p);
117
118private:
119 FullDesignationProvider full_designation_provider_ =
120 [this] (const Port &port) { return get_label (); };
121
123 PortType type_{};
125 PortFlow flow_{};
126
128 utils::Utf8String label_;
129
135 utils::Utf8String sym_;
136
138 std::optional<utils::Utf8String> port_group_;
139
140 BOOST_DESCRIBE_CLASS (
141 Port,
142 (utils::UuidIdentifiableObject<Port>),
143 (),
144 (),
145 (label_, sym_, port_group_))
146};
147
149{
150public:
152
163 std::atomic<int> num_ring_buffer_readers_{ 0 };
164
170 class RingBufferReader
171 {
172 public:
173 explicit RingBufferReader (RingBufferOwningPortMixin &owner)
174 : owner_ (owner)
175 {
176 owner_.num_ring_buffer_readers_++;
177 }
178
179 ~RingBufferReader () { owner_.num_ring_buffer_readers_--; }
180
181 // Delete copy and move operations
182 Q_DISABLE_COPY_MOVE (RingBufferReader)
183
184 private:
186 };
187};
188
189template <typename PortT> class PortConnectionsCacheMixin
190{
191 using ElementType =
192 std::pair<const PortT *, std::unique_ptr<dsp::PortConnection>>;
193
194public:
195 virtual ~PortConnectionsCacheMixin () = default;
196
197 auto &port_sources () const { return port_sources_; }
198
199 void
200 set_port_sources (this auto &self, utils::RangeOf<PortT *> auto source_ports)
201 [[clang::blocking]]
202 {
203 self.port_sources_.clear ();
204 for (const auto &source_port : source_ports)
205 {
206 self.port_sources_.push_back (
207 std::make_pair (
208 source_port,
209 std::make_unique<dsp::PortConnection> (
210 source_port->get_uuid (), self.get_uuid (), 1.f, true, true)));
211 }
212 }
213
214private:
220 std::vector<ElementType> port_sources_;
221 // std::vector<ElementType> port_destinations_;
222};
223
224using PortUuidReference = utils::TypedUuidReference<Port>;
225
226} // 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:91
void set_full_designation_provider(const auto *owner)
Convenience helper for providers that contain a get_full_designation_for_port() method.
Definition port.h:52
utils::Utf8String get_node_name() const override
Returns a human friendly name of the node.
Definition port.h:75
std::atomic< int > num_ring_buffer_readers_
Number of entities that want ring buffers to be written.
Definition port.h:163
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.