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/types.h"
10#include "utils/uuid_identifiable_object.h"
11
12namespace zrythm::dsp
13{
14
27class Port
30{
31 Z_DISABLE_COPY_MOVE (Port)
32public:
33 using FullDesignationProvider =
34 std::function<utils::Utf8String (const Port &port)>;
35
36 ~Port () override;
37
38 void set_full_designation_provider (FullDesignationProvider provider)
39 {
40 full_designation_provider_ = std::move (provider);
41 }
42
47 void set_full_designation_provider (const auto * owner)
48 {
49 full_designation_provider_ = [owner] (const Port &port) {
50 return owner->get_full_designation_for_port (port);
51 };
52 }
53
54 bool is_input () const { return flow_ == PortFlow::Input; }
55 bool is_output () const { return flow_ == PortFlow::Output; }
56
57 bool is_midi () const { return type_ == PortType::Midi; }
58 bool is_cv () const { return type_ == PortType::CV; }
59 bool is_audio () const { return type_ == PortType::Audio; }
60
61 utils::Utf8String get_label () const { return label_; }
62
63 auto get_symbol () const { return sym_; }
64 void set_symbol (const utils::Utf8String &sym) { sym_ = sym; }
65
66 // ========================================================================
67 // IProcessable Interface
68 // ========================================================================
69
71 {
72 return get_full_designation ();
73 }
74
78 nframes_t get_single_playback_latency () const override { return 0; }
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 (utils::Utf8String label, PortType type = {}, PortFlow flow = {});
102
103 friend void
104 init_from (Port &obj, const Port &other, utils::ObjectCloneType clone_type);
105
106private:
107 static constexpr auto kTypeId = "type"sv;
108 static constexpr auto kFlowId = "flow"sv;
109 static constexpr auto kLabelId = "label"sv;
110 static constexpr auto kSymbolId = "symbol"sv;
111 static constexpr auto kPortGroupId = "portGroup"sv;
112 friend void to_json (nlohmann::json &j, const Port &p)
113 {
114 j[kTypeId] = p.type_;
115 j[kFlowId] = p.flow_;
116 j[kLabelId] = p.label_;
117 j[kSymbolId] = p.sym_;
118 j[kPortGroupId] = p.port_group_;
119 }
120 friend void from_json (const nlohmann::json &j, Port &p)
121 {
122 j.at (kTypeId).get_to (p.type_);
123 j.at (kFlowId).get_to (p.flow_);
124 j.at (kLabelId).get_to (p.label_);
125 j.at (kSymbolId).get_to (p.sym_);
126 j.at (kPortGroupId).get_to (p.port_group_);
127 }
128
129private:
130 FullDesignationProvider full_designation_provider_ =
131 [this] (const Port &port) { return get_label (); };
132
134 PortType type_{ PortType::Unknown };
136 PortFlow flow_{ PortFlow::Unknown };
137
139 utils::Utf8String label_;
140
146 utils::Utf8String sym_;
147
149 std::optional<utils::Utf8String> port_group_;
150
151 BOOST_DESCRIBE_CLASS (
152 Port,
153 (utils::UuidIdentifiableObject<Port>),
154 (),
155 (),
156 (label_, sym_, port_group_))
157};
158
160{
161public:
163
174 std::atomic<int> num_ring_buffer_readers_{ 0 };
175
181 class RingBufferReader
182 {
183 public:
184 explicit RingBufferReader (RingBufferOwningPortMixin &owner)
185 : owner_ (owner)
186 {
187 owner_.num_ring_buffer_readers_++;
188 }
189
190 ~RingBufferReader () { owner_.num_ring_buffer_readers_--; }
191
192 // Delete copy and move operations
193 Z_DISABLE_COPY_MOVE (RingBufferReader)
194
195 private:
197 };
198};
199
200template <typename PortT> class PortConnectionsCacheMixin
201{
202 using ElementType =
203 std::pair<const PortT *, std::unique_ptr<dsp::PortConnection>>;
204
205public:
206 virtual ~PortConnectionsCacheMixin () = default;
207
208 auto &port_sources () const { return port_sources_; }
209
210 void set_port_sources (this auto &self, RangeOf<PortT *> auto source_ports)
211 [[clang::blocking]]
212 {
213 self.port_sources_.clear ();
214 for (const auto &source_port : source_ports)
215 {
216 self.port_sources_.push_back (
217 std::make_pair (
218 source_port,
219 std::make_unique<dsp::PortConnection> (
220 source_port->get_uuid (), self.get_uuid (), 1.f, true, true)));
221 }
222 }
223
224private:
230 std::vector<ElementType> port_sources_;
231 // std::vector<ElementType> port_destinations_;
232};
233
235using PortRegistryRef = std::reference_wrapper<PortRegistry>;
236using PortUuidReference = utils::UuidReference<PortRegistry>;
237
238} // namespace zrythm::dsp
239
240void
241from_json (const nlohmann::json &j, dsp::PortRegistry &registry);
nframes_t get_single_playback_latency() const override
Ports have no latency.
Definition port.h:78
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:47
utils::Utf8String get_node_name() const override
Returns a human friendly name of the node.
Definition port.h:70
std::atomic< int > num_ring_buffer_readers_
Number of entities that want ring buffers to be written.
Definition port.h:174
Interface for objects that can be processed in the DSP graph.
Definition graph_node.h:53
A registry that owns and manages objects identified by a UUID.
Lightweight UTF-8 string wrapper with safe conversions.
Definition utf8_string.h:38
Base class for objects that need to be uniquely identified by UUID.
A reference-counted RAII wrapper for a UUID in a registry.
uint32_t nframes_t
Frame count.
Definition types.h:58