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 kFlowId = "flow"sv;
108 static constexpr auto kLabelId = "label"sv;
109 static constexpr auto kSymbolId = "symbol"sv;
110 static constexpr auto kPortGroupId = "portGroup"sv;
111 friend void to_json (nlohmann::json &j, const Port &p);
112 friend void from_json (const nlohmann::json &j, Port &p);
113
114private:
115 FullDesignationProvider full_designation_provider_ =
116 [this] (const Port &port) { return get_label (); };
117
119 PortType type_{};
121 PortFlow flow_{};
122
124 utils::Utf8String label_;
125
131 utils::Utf8String sym_;
132
134 std::optional<utils::Utf8String> port_group_;
135
136 BOOST_DESCRIBE_CLASS (
137 Port,
138 (utils::UuidIdentifiableObject<Port>),
139 (),
140 (),
141 (label_, sym_, port_group_))
142};
143
145{
146public:
148
159 std::atomic<int> num_ring_buffer_readers_{ 0 };
160
166 class RingBufferReader
167 {
168 public:
169 explicit RingBufferReader (RingBufferOwningPortMixin &owner)
170 : owner_ (owner)
171 {
172 owner_.num_ring_buffer_readers_++;
173 }
174
175 ~RingBufferReader () { owner_.num_ring_buffer_readers_--; }
176
177 // Delete copy and move operations
178 Z_DISABLE_COPY_MOVE (RingBufferReader)
179
180 private:
182 };
183};
184
185template <typename PortT> class PortConnectionsCacheMixin
186{
187 using ElementType =
188 std::pair<const PortT *, std::unique_ptr<dsp::PortConnection>>;
189
190public:
191 virtual ~PortConnectionsCacheMixin () = default;
192
193 auto &port_sources () const { return port_sources_; }
194
195 void set_port_sources (this auto &self, RangeOf<PortT *> auto source_ports)
196 [[clang::blocking]]
197 {
198 self.port_sources_.clear ();
199 for (const auto &source_port : source_ports)
200 {
201 self.port_sources_.push_back (
202 std::make_pair (
203 source_port,
204 std::make_unique<dsp::PortConnection> (
205 source_port->get_uuid (), self.get_uuid (), 1.f, true, true)));
206 }
207 }
208
209private:
215 std::vector<ElementType> port_sources_;
216 // std::vector<ElementType> port_destinations_;
217};
218
220using PortRegistryRef = std::reference_wrapper<PortRegistry>;
221using PortUuidReference = utils::UuidReference<PortRegistry>;
222
223} // namespace zrythm::dsp
224
225void
226from_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:159
Interface for objects that can be processed in the DSP graph.
Definition graph_node.h:54
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