4#ifndef ZRYTHM_GUI_DSP_PORT_H
5#define ZRYTHM_GUI_DSP_PORT_H
7#include "zrythm-config.h"
9#include "dsp/graph_node.h"
10#include "dsp/port_identifier.h"
11#include "gui/dsp/port_backend.h"
12#include "gui/dsp/port_connection.h"
13#include "utils/audio.h"
14#include "utils/ring_buffer.h"
16#include "utils/uuid_identifiable_object.h"
29constexpr int TIME_TO_RESET_PEAK = 4800000;
37 PortRange () =
default;
39 PortRange (
float min,
float max,
float zero = 0.f)
44 float clamp_to_range (
float val) {
return std::clamp (val,
minf_, maxf_); }
47 static constexpr std::string_view kMinKey =
"minf";
48 static constexpr std::string_view kMaxKey =
"maxf";
49 static constexpr std::string_view kZeroKey =
"zerof";
50 friend void to_json (nlohmann::json &j,
const PortRange &p)
56 friend void from_json (
const nlohmann::json &j, PortRange &p)
58 j.at (kMinKey).get_to (p.
minf_);
59 j.at (kMaxKey).get_to (p.maxf_);
60 j.at (kZeroKey).get_to (p.
zerof_);
85 using TrackUuid = dsp::PortIdentifier::TrackUuid;
86 using PluginUuid = dsp::PortIdentifier::PluginUuid;
87 using PortUuid = dsp::PortIdentifier::PortUuid;
91 virtual bool is_in_active_project ()
const = 0;
106 return id.get_label ();
117 const PortUuid &port_uuid,
182 Q_DISABLE_COPY_MOVE (Port)
185 using PortType = dsp::PortType;
186 using PortFlow = dsp::PortFlow;
190 static std::unique_ptr<Port> create_unique_from_type (PortType type);
205 return owner_ && owner_->is_in_active_project ();
212 bool is_control ()
const {
return id_->is_control (); }
213 bool is_audio ()
const {
return id_->type_ == PortType::Audio; }
214 bool is_cv ()
const {
return id_->type_ == PortType::CV; }
215 bool is_event ()
const {
return id_->type_ == PortType::Event; }
216 bool is_midi ()
const {
return is_event (); }
217 bool is_input ()
const {
return id_->flow_ == PortFlow::Input; }
218 bool is_output ()
const {
return id_->flow_ == PortFlow::Output; }
256 return (owner_ !=
nullptr)
257 ? owner_->get_full_designation_for_port (*
id_)
261 void print_full_designation ()
const;
302 [[gnu::hot]]
virtual void
327 bool needs_external_buffer_clear_on_early_return ()
const override;
339 bool has_label ()
const {
return !
id_->label_.empty (); }
340 PortType get_type ()
const {
return id_->type_; }
341 PortFlow get_flow ()
const {
return id_->flow_; }
347 utils::Utf8String label,
354 void copy_members_from (
const Port &other,
ObjectCloneType clone_type);
356 int get_num_unlocked (
bool sources)
const;
359 static constexpr std::string_view kIdKey =
"id";
360 static constexpr std::string_view kExposedToBackendKey =
"exposedToBackend";
361 friend void to_json (nlohmann::json &j,
const Port &p)
366 friend void from_json (
const nlohmann::json &j, Port &p)
368 j.at (kIdKey).get_to (p.
id_);
376 std::unique_ptr<PortIdentifier>
id_;
398 std::vector<std::unique_ptr<PortConnection>> dest_connections_;
467using PortVariant = std::variant<MidiPort, AudioPort, CVPort, ControlPort>;
468using PortPtrVariant = to_pointer_variant<PortVariant>;
469using PortRefVariant = to_reference_variant<PortVariant>;
472using PortRegistryRef = std::reference_wrapper<PortRegistry>;
476from_json (
const nlohmann::json &j, PortRegistry ®istry);
virtual void on_midi_activity(const dsp::PortIdentifier &id)
Called during processing if the MIDI port contains new MIDI events.
virtual bool are_events_on_midi_channel_approved(midi_byte_t channel) const
Returns whether MIDI events on this channel on an input port should be processed (not ignored).
virtual void on_control_change_event(const PortUuid &port_uuid, const dsp::PortIdentifier &id, float val)
Will be called when a control port's value changes.
virtual bool should_bounce_to_master(utils::audio::BounceStep step) const
Whether the port should add its data to the master output when bouncing.
virtual bool should_sum_data_from_backend() const
Whether during processing, the port should sum the data from its backend buffers coming in.
virtual void set_port_metadata_from_owner(dsp::PortIdentifier &id, PortRange &range) const =0
Function that will be called by the Port to update the identifier's relevant members based on this po...
A connection between two ports.
float minf_
Minimum, maximum and zero values for this port.
float zerof_
The zero position of the port.
nframes_t get_single_playback_latency() const override
Returns the latency of only the given processable, without adding the previous/next latencies.
utils::Utf8String get_node_name() const override
Returns a human friendly name of the node.
void disconnect_all()
Disconnects all srcs and dests from port.
size_t get_hash() const
Generates a hash for a given port.
bool is_exposed_to_backend() const
Returns if the port is exposed to the backend.
void set_expose_to_backend(AudioEngine &engine, bool expose)
Sets whether to expose the port to the backend and exposes it or removes it.
bool deleting_
Port undergoing deletion.
bool is_in_active_project() const
Returns whether the port is in the active project.
std::vector< float > buf_
Buffer to be reallocated every time the buffer size changes.
long capture_latency_
Capture latency.
virtual void restore_from_non_project(const Port &non_project)
Reverts the data on the corresponding project port for the given non-project port.
virtual void process(EngineProcessTimeInfo time_nfo, bool noroll)=0
Process the port for the given time range.
bool write_ring_buffers_
Flag to indicate if the ring buffers below should be filled or not.
bool exposed_to_backend_
Flag to indicate that this port is exposed to the backend.
std::unique_ptr< RingBuffer< float > > audio_ring_
Ring buffer for saving the contents of the audio buffer to be used in the UI instead of directly acce...
void clear_external_buffer() override
Clears the backend's port buffer.
virtual void copy_metadata_from_project(const Port &project_port)
Copies the metadata from a project port to the given port.
std::unique_ptr< PortIdentifier > id_
Owned pointer.
virtual bool has_sound() const
If MIDI port, returns if there are any events, if audio port, returns if there is sound in the buffer...
virtual void allocate_bufs()=0
Allocates buffers used during DSP.
std::vector< std::unique_ptr< PortConnection > > src_connections_
Caches filled when recalculating the graph.
void init_loaded(IPortOwner &owner)
This function finds the Ports corresponding to the PortIdentifiers for srcs and dests.
void change_track(IPortOwner::TrackUuid new_track_id)
Updates the owner track identifier.
utils::Utf8String get_full_designation() const
Gets a full designation of the port in the format "Track/Port" or "Track/Plugin/Port".
std::vector< Port * > srcs_
Caches filled when recalculating the graph.
void rename_backend(AudioEngine &engine)
Renames the port on the backend side.
int get_num_unlocked_dests() const
Returns the number of unlocked (user-editable) destinations.
virtual void clear_buffer(AudioEngine &engine)=0
Clears the port buffer.
std::unique_ptr< PortBackend > backend_
Backend functionality.
long playback_latency_
Playback latency.
int get_num_unlocked_srcs() const
Returns the number of unlocked (user-editable) sources.
size_t last_buf_sz_
Last allocated buffer size (used for audio ports).
std::vector< Port * > dests_
Caches filled when recalculating the graph.
Represents a track in the project.
Interface for objects that can be processed in the DSP graph.
Struct used to identify Ports in the project.
A registry that owns and manages objects identified by a UUID.
Lightweight UTF-8 string wrapper with safe conversions.
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.
uint8_t midi_byte_t
MIDI byte.
Common struct to pass around during processing to avoid repeating the data in function arguments.