1 /*
2 * Copyright (C) 2018-2021 Alexandros Theodotou <alex at zrythm dot org>
3 *
4 * This file is part of Zrythm
5 *
6 * Zrythm is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Zrythm is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with Zrythm. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 /**
21 * \file
22 *
23 * Ports that transfer audio/midi/other signals to
24 * one another.
25 */
26
27 #ifndef __AUDIO_PORTS_H__
28 #define __AUDIO_PORTS_H__
29
30 #include "zrythm-config.h"
31
32 #include <stdbool.h>
33
34 #include "audio/port_identifier.h"
35 #include "plugins/lv2/lv2_evbuf.h"
36 #include "utils/types.h"
37 #include "zix/sem.h"
38
39 #include <lilv/lilv.h>
40
41 #ifdef HAVE_JACK
42 #include "weak_libjack.h"
43 #endif
44
45 #ifdef HAVE_RTMIDI
46 #include <rtmidi/rtmidi_c.h>
47 #endif
48
49 #ifdef HAVE_RTAUDIO
50 #include <rtaudio/rtaudio_c.h>
51 #endif
52
53 typedef struct Plugin Plugin;
54 typedef struct MidiEvents MidiEvents;
55 typedef struct Fader Fader;
56 typedef struct ZixRingImpl ZixRing;
57 typedef struct WindowsMmeDevice WindowsMmeDevice;
58 typedef struct Channel Channel;
59 typedef struct AudioEngine AudioEngine;
60 typedef struct Track Track;
61 typedef struct PortConnection PortConnection;
62 typedef struct TrackProcessor TrackProcessor;
63 typedef struct ModulatorMacroProcessor
64 ModulatorMacroProcessor;
65 typedef struct RtMidiDevice RtMidiDevice;
66 typedef struct RtAudioDevice RtAudioDevice;
67 typedef struct AutomationTrack AutomationTrack;
68 typedef struct TruePeakDsp TruePeakDsp;
69 typedef struct ExtPort ExtPort;
70 typedef struct AudioClip AudioClip;
71 typedef struct ChannelSend ChannelSend;
72 typedef struct Transport Transport;
73 typedef struct PluginGtkController
74 PluginGtkController;
75 typedef struct EngineProcessTimeInfo
76 EngineProcessTimeInfo;
77 typedef enum PanAlgorithm PanAlgorithm;
78 typedef enum PanLaw PanLaw;
79
80 /**
81 * @addtogroup audio
82 *
83 * @{
84 */
85
86 #define PORT_SCHEMA_VERSION 1
87 #define STEREO_PORTS_SCHEMA_VERSION 1
88
89 #define PORT_MAGIC 456861194
90 #define IS_PORT(_p) \
91 (((Port *) (_p))->magic == PORT_MAGIC)
92 #define IS_PORT_AND_NONNULL(x) \
93 ((x) && IS_PORT (x))
94
95 #define TIME_TO_RESET_PEAK 4800000
96
97 /**
98 * Special ID for owner_pl, owner_ch, etc. to indicate that
99 * the port is not owned.
100 */
101 #define PORT_NOT_OWNED -1
102
103 #define port_is_owner_active( \
104 self,_owner_type,owner) \
105 ((self->id.owner_type == _owner_type) \
106 && (self->owner != NULL) \
107 && \
108 owner##_is_in_active_project ( \
109 self->owner))
110
111 #define port_is_in_active_project(self) \
112 (port_is_owner_active ( \
113 self, PORT_OWNER_TYPE_AUDIO_ENGINE, \
114 engine) \
115 || \
116 port_is_owner_active ( \
117 self, PORT_OWNER_TYPE_PLUGIN, \
118 plugin) \
119 || \
120 port_is_owner_active ( \
121 self, PORT_OWNER_TYPE_TRACK, track) \
122 || \
123 port_is_owner_active ( \
124 self, PORT_OWNER_TYPE_CHANNEL, track) \
125 || \
126 port_is_owner_active ( \
127 self, PORT_OWNER_TYPE_FADER, fader) \
128 || \
129 port_is_owner_active ( \
130 self, PORT_OWNER_TYPE_CHANNEL_SEND, \
131 channel_send) \
132 || \
133 port_is_owner_active ( \
134 self, PORT_OWNER_TYPE_TRACK_PROCESSOR, \
135 track) \
136 || \
137 port_is_owner_active ( \
138 self, \
139 PORT_OWNER_TYPE_MODULATOR_MACRO_PROCESSOR, \
140 modulator_macro_processor) \
141 || \
142 port_is_owner_active ( \
143 self, PORT_OWNER_TYPE_HW, ext_port))
144
145 /**
146 * What the internal data is.
147 */
148 typedef enum PortInternalType
149 {
150 INTERNAL_NONE,
151
152 /** Pointer to Lv2Port. */
153 INTERNAL_LV2_PORT,
154
155 /** Pointer to jack_port_t. */
156 INTERNAL_JACK_PORT,
157
158 /** TODO */
159 INTERNAL_PA_PORT,
160
161 /** Pointer to snd_seq_port_info_t. */
162 INTERNAL_ALSA_SEQ_PORT,
163 } PortInternalType;
164
165 /**
166 * Scale point.
167 */
168 typedef struct PortScalePoint
169 {
170 float val;
171 char * label;
172 } PortScalePoint;
173
174 int
175 port_scale_point_cmp (
176 const void * _a,
177 const void * _b);
178
179 NONNULL
180 PortScalePoint *
181 port_scale_point_new (
182 const float val,
183 const char * label);
184
185 NONNULL
186 void
187 port_scale_point_free (
188 PortScalePoint * self);
189
190 /**
191 * Must ONLY be created via port_new()
192 */
193 typedef struct Port
194 {
195 int schema_version;
196
197 PortIdentifier id;
198
199 /**
200 * Flag to indicate that this port is exposed
201 * to the backend.
202 */
203 int exposed_to_backend;
204
205 /**
206 * Buffer to be reallocated every time the buffer
207 * size changes.
208 *
209 * The buffer size is AUDIO_ENGINE->block_length.
210 */
211 float * buf;
212
213 /**
214 * Contains raw MIDI data (MIDI ports only)
215 */
216 MidiEvents * midi_events;
217
218 /** Caches filled when recalculating the
219 * graph. */
220 struct Port ** srcs;
221 int num_srcs;
222 size_t srcs_size;
223
224 /** Caches filled when recalculating the
225 * graph. */
226 struct Port ** dests;
227 int num_dests;
228 size_t dests_size;
229
230 /** Caches filled when recalculating the
231 * graph. */
232 const PortConnection ** src_connections;
233 int num_src_connections;
234 size_t src_connections_size;
235
236 /** Caches filled when recalculating the
237 * graph. */
238 const PortConnection ** dest_connections;
239 int num_dest_connections;
240 size_t dest_connections_size;
241
242 /**
243 * Indicates whether data or lv2_port should be
244 * used.
245 */
246 PortInternalType internal_type;
247
248 /**
249 * Minimum, maximum and zero values for this
250 * port.
251 *
252 * Note that for audio, this is the amp (0 - 2)
253 * and not the actual values.
254 */
255 float minf;
256 float maxf;
257
258 /**
259 * The zero position of the port.
260 *
261 * For example, in balance controls, this will
262 * be the middle. In audio ports, this will be
263 * 0 amp (silence), etc.
264 */
265 float zerof;
266
267 /** Default value, only used for controls. */
268 float deff;
269
270 /** Index of the control parameter (for Carla
271 * plugin ports). */
272 int carla_param_id;
273
274 /**
275 * Pointer to arbitrary data.
276 *
277 * Use internal_type to check what data it is.
278 *
279 * FIXME just add the various data structs here
280 * and remove this ambiguity.
281 */
282 void * data;
283
284 #ifdef _WOE32
285 /**
286 * Connections to WindowsMmeDevices.
287 *
288 * These must be pointers to \ref
289 * AudioEngine.mme_in_devs or \ref
290 * AudioEngine.mme_out_devs and must not be
291 * allocated or free'd.
292 */
293 WindowsMmeDevice * mme_connections[40];
294 int num_mme_connections;
295 #else
296 void * mme_connections[40];
297 int num_mme_connections;
298 #endif
299
300 /** Semaphore for changing the connections
301 * atomically. */
302 ZixSem mme_connections_sem;
303
304 /**
305 * Last time the port finished dequeueing
306 * MIDI events.
307 *
308 * Used for some backends only.
309 */
310 gint64 last_midi_dequeue;
311
312 #ifdef HAVE_RTMIDI
313 /**
314 * RtMidi pointers for input ports.
315 *
316 * Each RtMidi port represents a device, and this
317 * Port can be connected to multiple devices.
318 */
319 RtMidiDevice * rtmidi_ins[128];
320 int num_rtmidi_ins;
321
322 /** RtMidi pointers for output ports. */
323 RtMidiDevice * rtmidi_outs[128];
324 int num_rtmidi_outs;
325 #else
326 void * rtmidi_ins[128];
327 int num_rtmidi_ins;
328 void * rtmidi_outs[128];
329 int num_rtmidi_outs;
330 #endif
331
332 #ifdef HAVE_RTAUDIO
333 /**
334 * RtAudio pointers for input ports.
335 *
336 * Each port can have multiple RtAudio devices.
337 */
338 RtAudioDevice * rtaudio_ins[128];
339 int num_rtaudio_ins;
340 #else
341 void * rtaudio_ins[128];
342 int num_rtaudio_ins;
343 #endif
344
345 /** Scale points. */
346 PortScalePoint ** scale_points;
347 int num_scale_points;
348
349 /**
350 * The control value if control port, otherwise
351 * 0.0f.
352 *
353 * FIXME for fader, this should be the
354 * fader_val (0.0 to 1.0) and not the
355 * amplitude.
356 *
357 * This value will be snapped (eg, if integer or
358 * toggle).
359 */
360 float control;
361
362 /** Unsnapped value, used by widgets. */
363 float unsnapped_control;
364
365 /** Flag that the value of the port changed from
366 * reading automation. */
367 bool value_changed_from_reading;
368
369 /**
370 * Last timestamp the control changed.
371 *
372 * This is used when recording automation in
373 * "touch" mode.
374 */
375 gint64 last_change;
376
377 /** Pointer to owner plugin, if any. */
378 Plugin * plugin;
379
380 /** Pointer to owner transport, if any. */
381 Transport * transport;
382
383 /** Pointer to owner channel send, if any. */
384 ChannelSend * channel_send;
385
386 /** Pointer to owner engine, if any. */
387 AudioEngine * engine;
388
389 /** Pointer to owner fader, if any. */
390 Fader * fader;
391
392 /**
393 * Pointer to owner track, if any.
394 *
395 * Also used for channel and track processor
396 * ports.
397 */
398 Track * track;
399
400 /** Pointer to owner modulator macro processor,
401 * if any. */
402 ModulatorMacroProcessor * modulator_macro_processor;
403
404 /* ====== flags to indicate port owner ====== */
405
406 /**
407 * Temporary plugin pointer (used when the
408 * plugin doesn't exist yet in its supposed slot).
409 * FIXME delete
410 */
411 Plugin * tmp_plugin;
412
413 /** used when loading projects FIXME needed? */
414 int initialized;
415
416 /**
417 * For control ports, when a modulator is
418 * attached to the port the previous value will
419 * be saved here.
420 *
421 * Automation in AutomationTrack's will overwrite
422 * this value.
423 */
424 float base_value;
425
426 /**
427 * Capture latency.
428 *
429 * See page 116 of "The Ardour DAW - Latency
430 * Compensation and Anywhere-to-Anywhere Signal
431 * Routing Systems".
432 */
433 long capture_latency;
434
435 /**
436 * Playback latency.
437 *
438 * See page 116 of "The Ardour DAW - Latency
439 * Compensation and Anywhere-to-Anywhere Signal
440 * Routing Systems".
441 */
442 long playback_latency;
443
444 /** Port undergoing deletion. */
445 int deleting;
446
447 /**
448 * Flag to indicate if the ring buffers below
449 * should be filled or not.
450 *
451 * If a UI element that needs them becomes
452 * mapped (visible), this should be set to
453 * 1, and when unmapped (invisible) it should
454 * be set to 0.
455 */
456 bool write_ring_buffers;
457
458 /** Whether the port has midi events not yet
459 * processed by the UI. */
460 volatile int has_midi_events;
461
462 /** Used by the UI to detect when unprocessed
463 * MIDI events exist. */
464 gint64 last_midi_event_time;
465
466 /**
467 * Ring buffer for saving the contents of the
468 * audio buffer to be used in the UI instead of
469 * directly accessing the buffer.
470 *
471 * This should contain blocks of block_length
472 * samples and should maintain at least 10
473 * cycles' worth of buffers.
474 *
475 * This is also used for CV.
476 */
477 ZixRing * audio_ring;
478
479 /**
480 * Ring buffer for saving MIDI events to be
481 * used in the UI instead of directly accessing
482 * the events.
483 *
484 * This should keep pushing MidiEvent's whenever
485 * they occur and the reader should empty it
486 * after cheking if there are any events.
487 *
488 * Currently there is only 1 reader for each port
489 * so this wont be a problem for now, but we
490 * should have one ring for each reader.
491 */
492 ZixRing * midi_ring;
493
494 /** Max amplitude during processing, if audio
495 * (fabsf). */
496 float peak;
497
498 /** Last time \ref Port.max_amp was set. */
499 gint64 peak_timestamp;
500
501 /**
502 * Last known MIDI status byte received.
503 *
504 * Used for running status (see
505 * http://midi.teragonaudio.com/tech/midispec/run.htm).
506 *
507 * Not needed for JACK.
508 */
509 midi_byte_t last_midi_status;
510
511 /* --- ported from Lv2Port --- */
512
513 /** LV2 port. */
514 const LilvPort* lilv_port;
515
516 /** Float for LV2 control ports, declared type for
517 * LV2 parameters. */
518 LV2_URID value_type;
519
520 /** For MIDI ports, otherwise NULL. */
521 LV2_Evbuf * evbuf;
522
523 /**
524 * Control widget, if applicable.
525 *
526 * Only used for generic UIs.
527 */
528 PluginGtkController * widget;
529
530 /**
531 * Minimum buffer size that the port wants, or
532 * 0.
533 *
534 * Used by LV2 plugin ports.
535 */
536 size_t min_buf_size;
537
538 /** Port index in the lilv plugin, if
539 * non-parameter. */
540 int lilv_port_index;
541
542 /**
543 * Whether the port received a UI event from
544 * the plugin UI in this cycle.
545 *
546 * This is used to avoid re-sending that event
547 * to the plugin.
548 *
549 * @note for control ports only.
550 */
551 bool received_ui_event;
552
553 /**
554 * The last known control value sent to the UI
555 * (if control).
556 *
557 * Also used by track processor for MIDI
558 * controls.
559 *
560 * @seealso lv2_ui_send_control_val_event_from_plugin_to_ui().
561 */
562 float last_sent_control;
563
564 /** Whether this value was set via automation. */
565 bool automating;
566
567 /** True for event, false for atom. */
568 bool old_api;
569
570 /* --- end Lv2Port --- */
571
572 /**
573 * Automation track this port is attached to.
574 *
575 * To be set at runtime only (not serialized).
576 */
577 AutomationTrack * at;
578
579 /** Pointer to ExtPort, if hw. */
580 ExtPort * ext_port;
581
582 /** Magic number to identify that this is a
583 * Port. */
584 int magic;
585
586 /** Last allocated buffer size (used for audio
587 * ports). */
588 size_t last_buf_sz;
589 } Port;
590
591 static const cyaml_schema_field_t
592 port_fields_schema[] =
593 {
594 YAML_FIELD_INT (
595 Port, schema_version),
596 YAML_FIELD_MAPPING_EMBEDDED (
597 Port, id,
598 port_identifier_fields_schema),
599 YAML_FIELD_INT (
600 Port, exposed_to_backend),
601 YAML_FIELD_FLOAT (Port, control),
602 YAML_FIELD_FLOAT (Port, minf),
603 YAML_FIELD_FLOAT (Port, maxf),
604 YAML_FIELD_FLOAT (Port, zerof),
605 YAML_FIELD_FLOAT (Port, deff),
606 YAML_FIELD_INT (Port, carla_param_id),
607
608 CYAML_FIELD_END
609 };
610
611 static const cyaml_schema_value_t
612 port_schema =
613 {
614 YAML_VALUE_PTR_NULLABLE (
615 Port, port_fields_schema),
616 };
617
618 /**
619 * L & R port, for convenience.
620 *
621 * Must ONLY be created via stereo_ports_new()
622 */
623 typedef struct StereoPorts
624 {
625 int schema_version;
626 Port * l;
627 Port * r;
628 } StereoPorts;
629
630 static const cyaml_schema_field_t
631 stereo_ports_fields_schema[] =
632 {
633 YAML_FIELD_INT (
634 StereoPorts, schema_version),
635 YAML_FIELD_MAPPING_PTR (
636 StereoPorts, l,
637 port_fields_schema),
638 YAML_FIELD_MAPPING_PTR (
639 StereoPorts, r,
640 port_fields_schema),
641
642 CYAML_FIELD_END
643 };
644
645 static const cyaml_schema_value_t
646 stereo_ports_schema =
647 {
648 YAML_VALUE_PTR (
649 StereoPorts, stereo_ports_fields_schema),
650 };
651
652 /**
653 * This function finds the Ports corresponding to
654 * the PortIdentifiers for srcs and dests.
655 *
656 * Should be called after the ports are loaded from
657 * yml.
658 */
659 NONNULL
660 void
661 port_init_loaded (
662 Port * self,
663 void * owner);
664
665 void
666 port_set_owner (
667 Port * self,
668 PortOwnerType owner_type,
669 void * owner);
670
671 NONNULL
672 Port *
673 port_find_from_identifier (
674 const PortIdentifier * const id);
675
676 NONNULL
677 static inline void
stereo_ports_init_loaded(StereoPorts * sp,void * owner)678 stereo_ports_init_loaded (
679 StereoPorts * sp,
680 void * owner)
681 {
682 port_init_loaded (sp->l, owner);
683 port_init_loaded (sp->r, owner);
684 }
685
686 NONNULL_ARGS (1)
687 static inline void
stereo_ports_set_owner(StereoPorts * sp,PortOwnerType owner_type,void * owner)688 stereo_ports_set_owner (
689 StereoPorts * sp,
690 PortOwnerType owner_type,
691 void * owner)
692 {
693 port_set_owner (sp->l, owner_type, owner);
694 port_set_owner (sp->r, owner_type, owner);
695 }
696
697 /**
698 * Creates port.
699 */
700 WARN_UNUSED_RESULT
701 NONNULL
702 Port *
703 port_new_with_type (
704 PortType type,
705 PortFlow flow,
706 const char * label);
707
708 WARN_UNUSED_RESULT
709 NONNULL
710 Port *
711 port_new_with_type_and_owner (
712 PortType type,
713 PortFlow flow,
714 const char * label,
715 PortOwnerType owner_type,
716 void * owner);
717
718 /**
719 * Allocates buffers used during DSP.
720 *
721 * To be called only where necessary to save
722 * RAM.
723 */
724 NONNULL
725 void
726 port_allocate_bufs (
727 Port * self);
728
729 /**
730 * Frees buffers.
731 *
732 * To be used when removing ports from the
733 * project/graph.
734 */
735 NONNULL
736 void
737 port_free_bufs (
738 Port * self);
739
740 /**
741 * Creates blank stereo ports.
742 */
743 NONNULL
744 StereoPorts *
745 stereo_ports_new_from_existing (
746 Port * l, Port * r);
747
748 /**
749 * Creates stereo ports for generic use.
750 *
751 * @param in 1 for in, 0 for out.
752 * @param owner Pointer to the owner. The type is
753 * determined by owner_type.
754 */
755 StereoPorts *
756 stereo_ports_new_generic (
757 int in,
758 const char * name,
759 PortOwnerType owner_type,
760 void * owner);
761
762 /**
763 * Connects the internal ports using
764 * port_connect().
765 *
766 * @param locked Lock the connection.
767 *
768 * @return Non-zero if error.
769 */
770 NONNULL
771 void
772 stereo_ports_connect (
773 StereoPorts * src,
774 StereoPorts * dest,
775 int locked);
776
777 NONNULL
778 void
779 stereo_ports_disconnect (
780 StereoPorts * self);
781
782 NONNULL
783 void
784 stereo_ports_fill_from_clip (
785 StereoPorts * self,
786 AudioClip * clip,
787 long g_start_frames,
788 nframes_t start_frame,
789 nframes_t nframes);
790
791 StereoPorts *
792 stereo_ports_clone (
793 const StereoPorts * src);
794
795 NONNULL
796 void
797 stereo_ports_free (
798 StereoPorts * self);
799
800 /**
801 * Function to get a port's value from its string
802 * symbol.
803 *
804 * Used when saving the LV2 state.
805 * This function MUST set size and type
806 * appropriately.
807 */
808 const void *
809 port_get_value_from_symbol (
810 const char * port_sym,
811 void * user_data,
812 uint32_t * size,
813 uint32_t * type);
814
815 #ifdef HAVE_JACK
816 /**
817 * Receives MIDI data from the port's exposed
818 * JACK port (if any) into the port.
819 */
820 NONNULL
821 void
822 port_receive_midi_events_from_jack (
823 Port * self,
824 const nframes_t start_frames,
825 const nframes_t nframes);
826
827 /**
828 * Receives audio data from the port's exposed
829 * JACK port (if any) into the port.
830 */
831 NONNULL
832 void
833 port_receive_audio_data_from_jack (
834 Port * self,
835 const nframes_t start_frames,
836 const nframes_t nframes);
837 #endif
838
839 /**
840 * If MIDI port, returns if there are any events,
841 * if audio port, returns if there is sound in the
842 * buffer.
843 */
844 NONNULL
845 bool
846 port_has_sound (
847 Port * self);
848
849 /**
850 * Copies a full designation of \p self in the
851 * format "Track/Port" or "Track/Plugin/Port" in
852 * \p buf.
853 */
854 NONNULL
855 void
856 port_get_full_designation (
857 Port * const self,
858 char * buf);
859
860 NONNULL
861 void
862 port_print_full_designation (
863 Port * const self);
864
865 /**
866 * Gathers all ports in the project and appends them
867 * in the given array.
868 */
869 void
870 port_get_all (
871 GPtrArray * ports);
872
873 NONNULL
874 Track *
875 port_get_track (
876 const Port * const self,
877 int warn_if_fail);
878
879 NONNULL
880 Plugin *
881 port_get_plugin (
882 Port * const self,
883 const bool warn_if_fail);
884
885 /**
886 * To be called when the port's identifier changes
887 * to update corresponding identifiers.
888 *
889 * @param prev_id Previous identifier to be used
890 * for searching.
891 * @param track The track that owns this port.
892 * @param update_automation_track Whether to update
893 * the identifier in the corresponding automation
894 * track as well. This should be false when
895 * moving a plugin.
896 */
897 void
898 port_update_identifier (
899 Port * self,
900 const PortIdentifier * prev_id,
901 Track * track,
902 bool update_automation_track);
903
904 #ifdef HAVE_RTMIDI
905 /**
906 * Dequeue the midi events from the ring
907 * buffers into \ref RtMidiDevice.events.
908 */
909 NONNULL
910 void
911 port_prepare_rtmidi_events (
912 Port * self);
913
914 /**
915 * Sums the inputs coming in from RtMidi
916 * before the port is processed.
917 */
918 NONNULL
919 void
920 port_sum_data_from_rtmidi (
921 Port * self,
922 const nframes_t start_frame,
923 const nframes_t nframes);
924 #endif
925
926 #ifdef HAVE_RTAUDIO
927 /**
928 * Dequeue the audio data from the ring
929 * buffers into \ref RtAudioDevice.buf.
930 */
931 NONNULL
932 void
933 port_prepare_rtaudio_data (
934 Port * self);
935
936 /**
937 * Sums the inputs coming in from RtAudio
938 * before the port is processed.
939 */
940 NONNULL
941 void
942 port_sum_data_from_rtaudio (
943 Port * self,
944 const nframes_t start_frame,
945 const nframes_t nframes);
946 #endif
947
948 /**
949 * Disconnects all hardware inputs from the port.
950 */
951 NONNULL
952 void
953 port_disconnect_hw_inputs (
954 Port * self);
955
956 /**
957 * Sets whether to expose the port to the backend
958 * and exposes it or removes it.
959 *
960 * It checks what the backend is using the engine's
961 * audio backend or midi backend settings.
962 */
963 NONNULL
964 void
965 port_set_expose_to_backend (
966 Port * self,
967 int expose);
968
969 /**
970 * Returns if the port is exposed to the backend.
971 */
972 NONNULL
973 int
974 port_is_exposed_to_backend (
975 const Port * self);
976
977 /**
978 * Renames the port on the backend side.
979 */
980 NONNULL
981 void
982 port_rename_backend (
983 Port * self);
984
985 #ifdef HAVE_ALSA
986
987 /**
988 * Returns the Port matching the given ALSA
989 * sequencer port ID.
990 */
991 Port *
992 port_find_by_alsa_seq_id (
993 const int id);
994 #endif
995
996 /**
997 * Sets the given control value to the
998 * corresponding underlying structure in the Port.
999 *
1000 * Note: this is only for setting the base values
1001 * (eg when automating via an automation lane). For
1002 * CV automations this should not be used.
1003 *
1004 * @param is_normalized Whether the given value is
1005 * normalized between 0 and 1.
1006 * @param forward_event Whether to forward a port
1007 * control change event to the plugin UI. Only
1008 * applicable for plugin control ports.
1009 * If the control is being changed manually or
1010 * from within Zrythm, this should be true to
1011 * notify the plugin of the change.
1012 */
1013 NONNULL
1014 void
1015 port_set_control_value (
1016 Port * self,
1017 const float val,
1018 const bool is_normalized,
1019 const bool forward_event);
1020
1021 /**
1022 * Gets the given control value from the
1023 * corresponding underlying structure in the Port.
1024 *
1025 * @param normalize Whether to get the value
1026 * normalized or not.
1027 */
1028 NONNULL
1029 HOT
1030 float
1031 port_get_control_value (
1032 Port * self,
1033 const bool normalize);
1034
1035 /**
1036 * Connects @ref a and @ref b with default
1037 * settings:
1038 * - enabled
1039 * - multiplier 1.0
1040 */
1041 #define port_connect(a,b,locked) \
1042 port_connections_manager_ensure_connect ( \
1043 PORT_CONNECTIONS_MGR, &((a)->id), &((b)->id), \
1044 1.f, locked, true)
1045
1046 /**
1047 * Removes the connection between the given ports.
1048 */
1049 #define port_disconnect(a,b) \
1050 port_connections_manager_ensure_disconnect ( \
1051 PORT_CONNECTIONS_MGR, &((a)->id), &((b)->id))
1052
1053 /**
1054 * Returns the number of unlocked (user-editable)
1055 * sources.
1056 */
1057 NONNULL
1058 int
1059 port_get_num_unlocked_srcs (
1060 const Port * self);
1061
1062 /**
1063 * Returns the number of unlocked (user-editable)
1064 * destinations.
1065 */
1066 NONNULL
1067 int
1068 port_get_num_unlocked_dests (
1069 const Port * self);
1070
1071 /**
1072 * Updates the track name hash on a track port and
1073 * all its source/destination identifiers.
1074 *
1075 * @param track Owner track.
1076 * @param hash The new hash.
1077 */
1078 void
1079 port_update_track_name_hash (
1080 Port * self,
1081 Track * track,
1082 unsigned int new_hash);
1083
1084 /**
1085 * Apply given fader value to port.
1086 *
1087 * @param start_frame The start frame offset from
1088 * 0 in this cycle.
1089 * @param nframes The number of frames to process.
1090 */
1091 NONNULL
1092 static inline void
port_apply_fader(Port * port,float amp,nframes_t start_frame,const nframes_t nframes)1093 port_apply_fader (
1094 Port * port,
1095 float amp,
1096 nframes_t start_frame,
1097 const nframes_t nframes)
1098 {
1099 nframes_t end = start_frame + nframes;
1100 while (start_frame < end)
1101 {
1102 port->buf[start_frame++] *= amp;
1103 }
1104 }
1105
1106 /**
1107 * First sets port buf to 0, then sums the given
1108 * port signal from its inputs.
1109 *
1110 * @param noroll Clear the port buffer in this
1111 * range.
1112 */
1113 HOT
1114 NONNULL
1115 void
1116 port_process (
1117 Port * port,
1118 const EngineProcessTimeInfo * const time_nfo,
1119 const bool noroll);
1120
1121 #define ports_connected(a,b) \
1122 (port_connections_manager_find_connection ( \
1123 PORT_CONNECTIONS_MGR, &(a)->id, &(b)->id) \
1124 != NULL)
1125
1126 /**
1127 * Returns whether the Port's can be connected (if
1128 * the connection will be valid and won't break the
1129 * acyclicity of the graph).
1130 */
1131 NONNULL
1132 bool
1133 ports_can_be_connected (
1134 const Port * src,
1135 const Port *dest);
1136
1137 /**
1138 * Disconnects all the given ports.
1139 */
1140 NONNULL
1141 void
1142 ports_disconnect (
1143 Port ** ports,
1144 int num_ports,
1145 int deleting);
1146
1147 /**
1148 * Copies the metadata from a project port to
1149 * the given port.
1150 *
1151 * Used when doing delete actions so that ports
1152 * can be restored on undo.
1153 */
1154 NONNULL
1155 void
1156 port_copy_metadata_from_project (
1157 Port * self,
1158 Port * project_port);
1159
1160 /**
1161 * Copies the port values from @ref other to @ref
1162 * self.
1163 *
1164 * @param self
1165 * @param other
1166 */
1167 NONNULL
1168 void
1169 port_copy_values (
1170 Port * self,
1171 const Port * other);
1172
1173 /**
1174 * Reverts the data on the corresponding project
1175 * port for the given non-project port.
1176 *
1177 * This restores src/dest connections and the
1178 * control value.
1179 *
1180 * @param self Project port.
1181 * @param non_project Non-project port.
1182 */
1183 NONNULL
1184 void
1185 port_restore_from_non_project (
1186 Port * self,
1187 Port * non_project);
1188
1189 /**
1190 * Clears the port buffer.
1191 */
1192 HOT
1193 NONNULL
1194 void
1195 port_clear_buffer (Port * port);
1196
1197 /**
1198 * Disconnects all srcs and dests from port.
1199 */
1200 NONNULL
1201 int
1202 port_disconnect_all (Port * port);
1203
1204 /**
1205 * Applies the pan to the given L/R ports.
1206 */
1207 NONNULL
1208 void
1209 port_apply_pan_stereo (Port * l,
1210 Port * r,
1211 float pan,
1212 PanLaw pan_law,
1213 PanAlgorithm pan_algo);
1214
1215 /**
1216 * Applies the pan to the given port.
1217 *
1218 * @param start_frame The start frame offset from
1219 * 0 in this cycle.
1220 * @param nframes The number of frames to process.
1221 */
1222 NONNULL
1223 void
1224 port_apply_pan (
1225 Port * port,
1226 float pan,
1227 PanLaw pan_law,
1228 PanAlgorithm pan_algo,
1229 nframes_t start_frame,
1230 const nframes_t nframes);
1231
1232 /**
1233 * Generates a hash for a given port.
1234 */
1235 NONNULL
1236 unsigned int
1237 port_get_hash (
1238 const void * ptr);
1239
1240 /**
1241 * To be used during serialization.
1242 */
1243 Port *
1244 port_clone (
1245 const Port * src);
1246
1247 /**
1248 * Deletes port, doing required cleanup and updating counters.
1249 */
1250 NONNULL
1251 void
1252 port_free (Port * port);
1253
1254 /**
1255 * @}
1256 */
1257
1258 #endif
1259