Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
ext_port.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: © 2019-2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#ifndef __AUDIO_EXT_PORT_H__
5#define __AUDIO_EXT_PORT_H__
6
12
13#include "zrythm-config.h"
14
15#include "gui/dsp/port.h"
16#include "utils/serialization.h"
17#include "utils/types.h"
18
19#ifdef HAVE_JACK
20# include "weakjack/weak_libjack.h"
21#endif
22
23#if HAVE_RTMIDI
24# include <rtmidi_c.h>
25#endif
26
28enum class AudioBackend;
29enum class MidiBackend;
30
36
40class ExtPort final : public IPortOwner
41{
42public:
46 enum class Type
47 {
48 JACK,
49 ALSA,
50 WindowsMME,
51 RtMidi,
52 RtAudio,
53 };
54
55 using PortType = zrythm::dsp::PortType;
56 using PortFlow = zrythm::dsp::PortFlow;
57
58public:
59 ExtPort () = default;
60#ifdef HAVE_JACK
61 ExtPort (jack_port_t * jport);
62#endif /* defined(HAVE_JACK) */
63
64 bool is_in_active_project () const override;
65
67 const override;
68
70 get_full_designation_for_port (const dsp::PortIdentifier &id) const override;
71
75 [[gnu::cold]] void init_loaded (HardwareProcessor * hw_processor)
76 {
77 hw_processor_ = hw_processor;
78 }
79
83 void print () const;
84
88 bool matches_backend () const;
89
94
95 friend bool operator== (const ExtPort &a, const ExtPort &b);
96
101
105 float * get_buffer (nframes_t nframes) const;
106
110 void clear_buffer (nframes_t nframes);
111
112#if 0
121void
122connect (
123 Port * port,
124 int src);
125#endif
126
133 void disconnect (Port * port, int src);
134
142 bool activate (Port * port, bool activate);
143
151 bool get_enabled () const;
152
161 static void ext_ports_get (
162 PortType type,
163 PortFlow flow,
164 bool hw,
165 std::vector<ExtPort> &ports,
166 AudioEngine &engine);
167
168private:
169 static constexpr std::string_view kTypeKey = "type";
170 static constexpr std::string_view kFullNameKey = "fullName";
171 static constexpr std::string_view kShortNameKey = "shortName";
172 static constexpr std::string_view kAlias1Key = "alias1";
173 static constexpr std::string_view kAlias2Key = "alias2";
174 static constexpr std::string_view kNumAliasesKey = "numAliases";
175 static constexpr std::string_view kRtAudioDeviceNameKey = "rtAudioDeviceName";
176 static constexpr std::string_view kRtAudioChannelIndexKey =
177 "rtAudioChannelIndex";
178 static constexpr std::string_view kIsMidiKey = "isMidi";
179 friend void to_json (nlohmann::json &j, const ExtPort &port)
180 {
181 j = nlohmann::json{
182 { kTypeKey, port.type_ },
183 { kFullNameKey, port.full_name_ },
184 { kShortNameKey, port.short_name_ },
185 { kAlias1Key, port.alias1_ },
186 { kAlias2Key, port.alias2_ },
187 { kNumAliasesKey, port.num_aliases_ },
188 { kRtAudioDeviceNameKey, port.rtaudio_dev_name_ },
189 { kRtAudioChannelIndexKey, port.rtaudio_channel_idx_ },
190 { kIsMidiKey, port.is_midi_ },
191 };
192 }
193 friend void from_json (const nlohmann::json &j, ExtPort &port)
194 {
195 j.at (kTypeKey).get_to (port.type_);
196 j.at (kFullNameKey).get_to (port.full_name_);
197 j.at (kShortNameKey).get_to (port.short_name_);
198 j.at (kAlias1Key).get_to (port.alias1_);
199 j.at (kAlias2Key).get_to (port.alias2_);
200 j.at (kNumAliasesKey).get_to (port.num_aliases_);
201 j.at (kRtAudioDeviceNameKey).get_to (port.rtaudio_dev_name_);
202 j.at (kRtAudioChannelIndexKey).get_to (port.rtaudio_channel_idx_);
203 j.at (kIsMidiKey).get_to (port.is_midi_);
204 }
205
206public:
208#ifdef HAVE_JACK
209 jack_port_t * jport_ = nullptr;
210#else
211 void * jport_ = nullptr;
212#endif
213
216
219
222
225
226 int num_aliases_ = 0;
227
229 unsigned int rtaudio_channel_idx_ = 0;
230
233
235 unsigned int rtaudio_id_ = 0;
236
238 bool rtaudio_is_input_ = false;
239 bool rtaudio_is_duplex_ = false;
240
241 std::shared_ptr<
242#if HAVE_RTAUDIO
243 RtAudioDevice
244#else
245 int
246#endif
247 >
248 rtaudio_dev_;
249
251 unsigned int rtmidi_id_ = 0;
252
253 std::shared_ptr<
254#if HAVE_RTMIDI
255 RtMidiDevice
256#else
257 int
258#endif
259 >
260 rtmidi_dev_;
261
262 Type type_ = (Type) 0;
263
265 bool is_midi_ = false;
266
269
272
275 bool active_ = false;
276
282 bool pending_reconnect_ = false;
283
287 Port * port_ = nullptr;
288};
289
290inline bool
291operator== (const ExtPort &a, const ExtPort &b)
292{
293 return a.type_ == b.type_ && a.full_name_ == b.full_name_;
294}
295
299
300#endif
The audio engine.
Definition engine.h:168
External port.
Definition ext_port.h:41
void disconnect(Port *port, int src)
Disconnects the Port from the ExtPort.
utils::Utf8String alias2_
Alias #2 if any.
Definition ext_port.h:224
unsigned int rtmidi_id_
RtMidi port index.
Definition ext_port.h:251
HardwareProcessor * hw_processor_
Pointer to owner hardware processor, if any.
Definition ext_port.h:271
utils::Utf8String full_name_
Full port name, used also as ID.
Definition ext_port.h:215
bool is_midi_
True if MIDI, false if audio.
Definition ext_port.h:265
float * get_buffer(nframes_t nframes) const
Returns the buffer of the external port.
void print() const
Prints the port info.
utils::Utf8String short_name_
Short port name.
Definition ext_port.h:218
utils::Utf8String alias1_
Alias #1 if any.
Definition ext_port.h:221
void clear_buffer(nframes_t nframes)
Clears the buffer of the external port.
int hw_processor_index_
Index in the HW processor (cache for real-time use)
Definition ext_port.h:268
bool get_enabled() const
Checks in the GSettings whether this port is marked as enabled by the user.
unsigned int rtaudio_id_
RtAudio device ID (NOT index!
Definition ext_port.h:235
static void ext_ports_get(PortType type, PortFlow flow, bool hw, std::vector< ExtPort > &ports, AudioEngine &engine)
Collects external ports of the given type.
utils::Utf8String get_id() const
Returns a unique identifier (full name prefixed with backend type).
bool rtaudio_is_input_
Whether the channel is input.
Definition ext_port.h:238
unsigned int rtaudio_channel_idx_
RtAudio channel index.
Definition ext_port.h:229
bool active_
Whether the port is active and receiving events (for use by hw processor).
Definition ext_port.h:275
void * jport_
JACK port.
Definition ext_port.h:211
utils::Utf8String get_friendly_name() const
Returns a user-friendly display name (eg, to be used in dropdowns).
bool matches_backend() const
Returns if the ext port matches the current backend.
bool activate(Port *port, bool activate)
Activates the port (starts receiving data) or deactivates it.
void init_loaded(HardwareProcessor *hw_processor)
Inits the ExtPort after loading a project.
Definition ext_port.h:75
bool pending_reconnect_
Set to true when a hardware port is disconnected.
Definition ext_port.h:282
void set_port_metadata_from_owner(dsp::PortIdentifier &id, PortRange &range) const override
Function that will be called by the Port to update the identifier's relevant members based on this po...
Port * port_
Temporary port to receive data.
Definition ext_port.h:287
Type
External port type.
Definition ext_port.h:47
utils::Utf8String rtaudio_dev_name_
RtAudio device name.
Definition ext_port.h:232
Hardware processor.
The Port class represents a port in the audio processing graph.
Definition port.h:181
Struct used to identify Ports in the project.
Lightweight UTF-8 string wrapper with safe conversions.
Definition string.h:39
uint32_t nframes_t
Frame count.
Definition types.h:62
Custom types.