Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
port.h
1// SPDX-FileCopyrightText: © 2018-2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "dsp/graph_node.h"
7#include "dsp/port_connection.h"
8#include "dsp/port_fwd.h"
9#include "utils/uuid_identifiable_object.h"
10
11namespace zrythm::dsp
12{
13
26class Port
29{
30 Q_DISABLE_COPY_MOVE (Port)
31public:
32 using FullDesignationProvider =
33 std::function<utils::Utf8String (const Port &port)>;
34
35 ~Port () override;
36
37 void set_full_designation_provider (FullDesignationProvider provider)
38 {
39 full_designation_provider_ = std::move (provider);
40 }
41
46 void set_full_designation_provider (const auto * owner)
47 {
48 full_designation_provider_ = [owner] (const Port &port) {
49 return owner->get_full_designation_for_port (port);
50 };
51 }
52
53 bool is_input () const { return flow_ == PortFlow::Input; }
54 bool is_output () const { return flow_ == PortFlow::Output; }
55
56 bool is_midi () const { return type_ == PortType::Midi; }
57 bool is_cv () const { return type_ == PortType::CV; }
58 bool is_audio () const { return type_ == PortType::Audio; }
59
60 utils::Utf8String get_label () const { return label_; }
61
62 auto get_symbol () const { return sym_; }
63 void set_symbol (const utils::Utf8String &sym) { sym_ = sym; }
64
65 // ========================================================================
66 // IProcessable Interface
67 // ========================================================================
68
70 {
71 return get_full_designation ();
72 }
73
74 // ========================================================================
75
79 virtual void clear_buffer (std::size_t offset, std::size_t nframes) = 0;
80
86 {
87 return full_designation_provider_ (*this);
88 }
89
90 bool has_label () const { return !label_.empty (); }
91 PortType type () const { return type_; }
92 PortFlow flow () const { return flow_; }
93
94protected:
95 Port (utils::Utf8String label, PortType type = {}, PortFlow flow = {});
96
97 friend void
98 init_from (Port &obj, const Port &other, utils::ObjectCloneType clone_type);
99
100private:
101 static constexpr auto kFlowId = "flow"sv;
102 static constexpr auto kLabelId = "label"sv;
103 static constexpr auto kSymbolId = "symbol"sv;
104 static constexpr auto kPortGroupId = "portGroup"sv;
105 friend void to_json (nlohmann::json &j, const Port &p);
106 friend void from_json (const nlohmann::json &j, Port &p);
107
108private:
109 FullDesignationProvider full_designation_provider_ =
110 [this] (const Port &port) { return get_label (); };
111
113 PortType type_{};
115 PortFlow flow_{};
116
118 utils::Utf8String label_;
119
125 utils::Utf8String sym_;
126
128 std::optional<utils::Utf8String> port_group_;
129
130 BOOST_DESCRIBE_CLASS (
131 Port,
132 (utils::UuidIdentifiableObject<Port>),
133 (),
134 (),
135 (label_, sym_, port_group_))
136};
137
139{
140public:
142
153 std::atomic<int> num_ring_buffer_readers_{ 0 };
154
160 class RingBufferReader
161 {
162 public:
163 explicit RingBufferReader (RingBufferOwningPortMixin &owner)
164 : owner_ (owner)
165 {
166 owner_.num_ring_buffer_readers_++;
167 }
168
169 ~RingBufferReader () { owner_.num_ring_buffer_readers_--; }
170
171 // Delete copy and move operations
172 Q_DISABLE_COPY_MOVE (RingBufferReader)
173
174 private:
176 };
177};
178
179template <typename PortT> class PortConnectionsCacheMixin
180{
181 using ElementType =
182 std::pair<const PortT *, std::unique_ptr<dsp::PortConnection>>;
183
184public:
185 virtual ~PortConnectionsCacheMixin () = default;
186
187 auto &port_sources () const { return port_sources_; }
188
189 void set_port_sources (this auto &self, RangeOf<PortT *> auto source_ports)
190 [[clang::blocking]]
191 {
192 self.port_sources_.clear ();
193 for (const auto &source_port : source_ports)
194 {
195 self.port_sources_.push_back (
196 std::make_pair (
197 source_port,
198 std::make_unique<dsp::PortConnection> (
199 source_port->get_uuid (), self.get_uuid (), 1.f, true, true)));
200 }
201 }
202
203private:
209 std::vector<ElementType> port_sources_;
210 // std::vector<ElementType> port_destinations_;
211};
212
214using PortRegistryRef = std::reference_wrapper<PortRegistry>;
215using PortUuidReference = utils::UuidReference<PortRegistry>;
216
217} // namespace zrythm::dsp
218
219void
220from_json (const nlohmann::json &j, zrythm::dsp::PortRegistry &registry);
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:85
void set_full_designation_provider(const auto *owner)
Convenience helper for providers that contain a get_full_designation_for_port() method.
Definition port.h:46
utils::Utf8String get_node_name() const override
Returns a human friendly name of the node.
Definition port.h:69
std::atomic< int > num_ring_buffer_readers_
Number of entities that want ring buffers to be written.
Definition port.h:153
Interface for objects that can be processed in the DSP graph.
Definition graph_node.h:86
A registry that owns and manages objects identified by a UUID.
Lightweight UTF-8 string wrapper with safe conversions.
Definition utf8_string.h:37
Base class for objects that need to be uniquely identified by UUID.
A reference-counted RAII wrapper for a UUID in a registry.