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  * The backend for a timeline track.
24  */
25 
26 #ifndef __AUDIO_TRACK_H__
27 #define __AUDIO_TRACK_H__
28 
29 #include "audio/automation_tracklist.h"
30 #include "audio/channel.h"
31 #include "audio/chord_object.h"
32 #include "audio/modulator_macro_processor.h"
33 #include "audio/marker.h"
34 #include "audio/region.h"
35 #include "audio/scale.h"
36 #include "audio/scale_object.h"
37 #include "audio/track_lane.h"
38 #include "audio/track_processor.h"
39 #include "plugins/plugin.h"
40 #include "utils/yaml.h"
41 
42 #include <gtk/gtk.h>
43 
44 #define MAX_REGIONS 300
45 
46 typedef struct AutomationTracklist
47   AutomationTracklist;
48 typedef struct ZRegion ZRegion;
49 typedef struct Position Position;
50 typedef struct _TrackWidget TrackWidget;
51 typedef struct _FolderChannelWidget
52   FolderChannelWidget;
53 typedef struct Channel Channel;
54 typedef struct MidiEvents MidiEvents;
55 typedef struct AutomationTrack AutomationTrack;
56 typedef struct Automatable Automatable;
57 typedef struct AutomationPoint AutomationPoint;
58 typedef struct ChordObject ChordObject;
59 typedef struct MusicalScale MusicalScale;
60 typedef struct Modulator Modulator;
61 typedef struct Marker Marker;
62 typedef struct PluginDescriptor PluginDescriptor;
63 typedef struct Tracklist Tracklist;
64 typedef struct SupportedFile SupportedFile;
65 typedef struct TracklistSelections
66   TracklistSelections;
67 typedef enum PassthroughProcessorType
68   PassthroughProcessorType;
69 typedef enum FaderType FaderType;
70 typedef void MIDI_FILE;
71 
72 /**
73  * @addtogroup audio
74  *
75  * @{
76  */
77 
78 #define TRACK_SCHEMA_VERSION 1
79 
80 #define TRACK_MIN_HEIGHT 24
81 #define TRACK_DEF_HEIGHT 48
82 
83 #define TRACK_MAGIC 21890135
84 #define IS_TRACK(x) \
85   (((Track *) x)->magic == TRACK_MAGIC)
86 #define IS_TRACK_AND_NONNULL(x) \
87   (x && IS_TRACK (x))
88 
89 #define track_is_in_active_project(self) \
90   (self->tracklist \
91    && \
92    tracklist_is_in_active_project ( \
93      self->tracklist))
94 
95 /** Whether this track is part of the
96  * SampleProcessor auditioner tracklist. */
97 #define track_is_auditioner(self) \
98   (self->tracklist \
99    && tracklist_is_auditioner (self->tracklist))
100 
101 /**
102  * The Track's type.
103  */
104 typedef enum TrackType
105 {
106   /**
107    * Instrument tracks must have an Instrument
108    * plugin at the first slot and they produce
109    * audio output.
110    */
111   TRACK_TYPE_INSTRUMENT,
112 
113   /**
114    * Audio tracks can record and contain audio
115    * clips. Other than that their channel strips
116    * are similar to buses.
117    */
118   TRACK_TYPE_AUDIO,
119 
120   /**
121    * The master track is a special type of group
122    * track.
123    */
124   TRACK_TYPE_MASTER,
125 
126   /**
127    * The chord track contains chords that can be
128    * used to modify midi in real time or to color
129    * the piano roll.
130    */
131   TRACK_TYPE_CHORD,
132 
133   /**
134    * Marker Track's contain named markers at
135    * specific Position's in the song.
136    */
137   TRACK_TYPE_MARKER,
138 
139   /**
140    * Special track for BPM (tempo) and time
141    * signature events.
142    */
143   TRACK_TYPE_TEMPO,
144 
145   /**
146    * Special track to contain global Modulator's.
147    */
148   TRACK_TYPE_MODULATOR,
149 
150   /**
151    * Buses are channels that receive audio input
152    * and have effects on their channel strip. They
153    * are similar to Group Tracks, except that they
154    * cannot be routed to directly. Buses are used
155    * for send effects.
156    */
157   TRACK_TYPE_AUDIO_BUS,
158 
159   /**
160    * Group Tracks are used for grouping audio
161    * signals, for example routing multiple drum
162    * tracks to a "Drums" group track. Like buses,
163    * they only contain effects but unlike buses
164    * they can be routed to.
165    */
166   TRACK_TYPE_AUDIO_GROUP,
167 
168   /**
169    * Midi tracks can only have MIDI effects in the
170    * strip and produce MIDI output that can be
171    * routed to instrument channels or hardware.
172    */
173   TRACK_TYPE_MIDI,
174 
175 
176   /** Same with audio bus but for MIDI signals. */
177   TRACK_TYPE_MIDI_BUS,
178 
179   /** Same with audio group but for MIDI signals. */
180   TRACK_TYPE_MIDI_GROUP,
181 
182   /** Foldable track used for visual grouping. */
183   TRACK_TYPE_FOLDER,
184 } TrackType;
185 
186 static const cyaml_strval_t
187 track_type_strings[] =
188 {
189   { __("Instrument"),   TRACK_TYPE_INSTRUMENT    },
190   { __("Audio"),        TRACK_TYPE_AUDIO   },
191   { __("Master"),       TRACK_TYPE_MASTER   },
192   { __("Chord"),        TRACK_TYPE_CHORD   },
193   { __("Marker"),       TRACK_TYPE_MARKER   },
194   { __("Tempo"),        TRACK_TYPE_TEMPO   },
195   { __("Modulator"),    TRACK_TYPE_MODULATOR   },
196   { __("Audio FX"),     TRACK_TYPE_AUDIO_BUS   },
197   { __("Audio Group"),  TRACK_TYPE_AUDIO_GROUP   },
198   { __("MIDI"),         TRACK_TYPE_MIDI   },
199   { __("MIDI FX"),      TRACK_TYPE_MIDI_BUS   },
200   { __("MIDI Group"),   TRACK_TYPE_MIDI_GROUP   },
201   { __("Folder"),       TRACK_TYPE_FOLDER   },
202 };
203 
204 /**
205  * Track to be inserted into the Project's
206  * Tracklist.
207  *
208  * Each Track contains a Channel with Plugins.
209  *
210  * Tracks shall be identified by ther position
211  * (index) in the Tracklist.
212  */
213 typedef struct Track
214 {
215   int                 schema_version;
216 
217   /**
218    * Position in the Tracklist.
219    *
220    * This is also used in the Mixer for the Channels.
221    * If a track doesn't have a Channel, the Mixer
222    * can just skip.
223    */
224   int                 pos;
225 
226   /**
227    * Used to remember the position before pinned,
228    * so it can be moved back there when unpinned.
229    */
230   //int                 pos_before_pinned;
231 
232   /** The type of track this is. */
233   TrackType           type;
234 
235   /** Track name, used in channel too. */
236   char *              name;
237 
238   /** Cache calculated when adding to graph. */
239   unsigned int        name_hash;
240 
241   /** Icon name of the track. */
242   char *              icon_name;
243 
244   /**
245    * Track Widget created dynamically.
246    * 1 track has 1 widget.
247    */
248   TrackWidget *       widget;
249 
250   /**
251    * Widget used for foldable tracks in the mixer.
252    */
253   FolderChannelWidget * folder_ch_widget;
254 
255   /** Flag to set automations visible or not. */
256   bool                automation_visible;
257 
258   /** Flag to set track lanes visible or not. */
259   bool                lanes_visible;
260 
261   /** Whole Track is visible or not. */
262   bool                visible;
263 
264   /** Height of the main part (without lanes). */
265   double              main_height;
266 
267   /** Recording or not. */
268   Port *              recording;
269 
270   /**
271    * Whether record was set automatically when
272    * the channel was selected.
273    *
274    * This is so that it can be unset when selecting
275    * another track. If we don't do this all the
276    * tracks end up staying on record mode.
277    */
278   bool                record_set_automatically;
279 
280   /**
281    * Active (enabled) or not.
282    *
283    * Disabled tracks should be ignored in routing.
284    * Similar to Plugin.enabled (bypass).
285    */
286   bool                enabled;
287 
288   /**
289    * Track color.
290    *
291    * This is used in the channels as well.
292    */
293   GdkRGBA             color;
294 
295   /* ==== INSTRUMENT/MIDI/AUDIO TRACK ==== */
296 
297   /** Lanes in this track containing Regions. */
298   TrackLane **        lanes;
299   int                 num_lanes;
300   size_t              lanes_size;
301 
302   /** MIDI channel (MIDI/Instrument track only). */
303   uint8_t             midi_ch;
304 
305   /**
306    * Whether drum mode in the piano roll is
307    * enabled for this track.
308    *
309    * Only used for tracks that have a piano roll.
310    */
311   bool                drum_mode;
312 
313   /**
314    * If set to 1, the input received will not be
315    * changed to the selected MIDI channel.
316    *
317    * If this is 0, all input received will have its
318    * channel changed to the selected MIDI channel.
319    */
320   int                 passthrough_midi_input;
321 
322   /**
323    * ZRegion currently recording on.
324    *
325    * This must only be set by the RecordingManager
326    * when processing an event and should not
327    * be touched by anything else.
328    */
329   ZRegion *           recording_region;
330 
331   /**
332    * This is a flag to let the recording manager
333    * know that a START signal was already sent for
334    * recording.
335    *
336    * This is because \ref Track.recording_region
337    * takes a cycle or 2 to become non-NULL.
338    */
339   bool                recording_start_sent;
340 
341   /**
342    * This is a flag to let the recording manager
343    * know that a STOP signal was already sent for
344    * recording.
345    *
346    * This is because \ref Track.recording_region
347    * takes a cycle or 2 to become NULL.
348    */
349   bool                recording_stop_sent;
350 
351   /**
352    * This must only be set by the RecordingManager
353    * when temporarily pausing recording, eg when
354    * looping or leaving the punch range.
355    *
356    * See \ref
357    * RECORDING_EVENT_TYPE_PAUSE_TRACK_RECORDING.
358    */
359   bool                recording_paused;
360 
361   /** Lane index of region before recording
362    * paused. */
363   int                 last_lane_idx;
364 
365   /* ==== INSTRUMENT/MIDI/AUDIO TRACK END ==== */
366 
367   /* ==== AUDIO TRACK ==== */
368 
369   /** Real-time time stretcher. */
370   Stretcher *         rt_stretcher;
371 
372   /* ==== AUDIO TRACK END ==== */
373 
374   /* ==== CHORD TRACK ==== */
375 
376   /**
377    * ChordObject's.
378    *
379    * Note: these must always be sorted by Position.
380    */
381   ZRegion **          chord_regions;
382   int                 num_chord_regions;
383   size_t              chord_regions_size;
384 
385   /**
386    * ScaleObject's.
387    *
388    * Note: these must always be sorted by Position.
389    */
390   ScaleObject **      scales;
391   int                 num_scales;
392   size_t              scales_size;
393 
394   /* ==== CHORD TRACK END ==== */
395 
396   /* ==== MARKER TRACK ==== */
397 
398   Marker **           markers;
399   int                 num_markers;
400   size_t              markers_size;
401 
402   /* ==== MARKER TRACK END ==== */
403 
404   /* ==== TEMPO TRACK ==== */
405 
406   /** Automatable BPM control. */
407   Port *              bpm_port;
408 
409   /** Automatable beats per bar port. */
410   Port *              beats_per_bar_port;
411 
412   /** Automatable beat unit port. */
413   Port *              beat_unit_port;
414 
415   /* ==== TEMPO TRACK END ==== */
416 
417   /* ==== FOLDABLE TRACK ==== */
418 
419   /**
420    * Number of tracks inside this track.
421    *
422    * Should be 1 unless foldable.
423    */
424   int                 size;
425 
426   /** Whether currently folded. */
427   bool                folded;
428 
429   /* ==== FOLDABLE TRACK END ==== */
430 
431   /* ==== MODULATOR TRACK ==== */
432 
433   /** Modulators. */
434   Plugin **           modulators;
435   int                 num_modulators;
436   size_t              modulators_size;
437 
438   /** Modulator macros. */
439   ModulatorMacroProcessor * modulator_macros[128];
440   int                 num_modulator_macros;
441   int                 num_visible_modulator_macros;
442 
443   /* ==== MODULATOR TRACK END ==== */
444 
445   /* ==== CHANNEL TRACK ==== */
446 
447   /** 1 Track has 0 or 1 Channel. */
448   Channel *           channel;
449 
450   /* ==== CHANNEL TRACK END ==== */
451 
452   /**
453    * The TrackProcessor, used for processing.
454    *
455    * This is the starting point when processing
456    * a Track.
457    */
458   TrackProcessor *    processor;
459 
460   AutomationTracklist automation_tracklist;
461 
462   /**
463    * Flag to tell the UI that this channel had
464    * MIDI activity.
465    *
466    * When processing this and setting it to 0,
467    * the UI should create a separate event using
468    * EVENTS_PUSH.
469    */
470   bool                trigger_midi_activity;
471 
472   /**
473    * The input signal type (eg audio bus tracks have
474    * audio input signals).
475    */
476   PortType            in_signal_type;
477 
478   /**
479    * The output signal type (eg midi tracks have
480    * MIDI output singals).
481    */
482   PortType            out_signal_type;
483 
484   /** User comments. */
485   char *              comment;
486 
487   /**
488    * Set to ON during bouncing if this
489    * track should be included.
490    *
491    * Only relevant for tracks that output audio.
492    */
493   bool                bounce;
494 
495   /**
496    * Whether to temporarily route the output to
497    * master (e.g., when bouncing the track on its
498    * own without its parents).
499    */
500   bool                bounce_to_master;
501 
502   /**
503    * Name hashes of tracks that are routed to this
504    * track, if group track.
505    *
506    * This is used when undoing track deletion.
507    */
508   unsigned int *      children;
509   int                 num_children;
510   size_t              children_size;
511 
512   /** Whether the track is currently frozen. */
513   bool                frozen;
514 
515   /** Pool ID of the clip if track is frozen. */
516   int                 pool_id;
517 
518   int                 magic;
519 
520   /** Whether currently disconnecting. */
521   bool                disconnecting;
522 
523   /** Pointer to owner tracklist, if any. */
524   Tracklist *         tracklist;
525 
526   /** Pointer to owner tracklist selections, if
527    * any. */
528   TracklistSelections * ts;
529 } Track;
530 
531 static const cyaml_schema_field_t
532 track_fields_schema[] =
533 {
534   YAML_FIELD_INT (Track, schema_version),
535   YAML_FIELD_STRING_PTR (Track, name),
536   YAML_FIELD_STRING_PTR (Track, icon_name),
537   YAML_FIELD_ENUM (
538     Track, type, track_type_strings),
539   YAML_FIELD_INT (Track, pos),
540   YAML_FIELD_INT (Track, lanes_visible),
541   YAML_FIELD_INT (Track, automation_visible),
542   YAML_FIELD_INT (Track, visible),
543   YAML_FIELD_FLOAT (Track, main_height),
544   YAML_FIELD_INT (Track, passthrough_midi_input),
545   YAML_FIELD_MAPPING_PTR_OPTIONAL (
546     Track, recording, port_fields_schema),
547   YAML_FIELD_INT (Track, enabled),
548   YAML_FIELD_MAPPING_EMBEDDED (
549     Track, color, gdk_rgba_fields_schema),
550   YAML_FIELD_DYN_PTR_ARRAY_VAR_COUNT (
551     Track, lanes, track_lane_schema),
552   YAML_FIELD_DYN_PTR_ARRAY_VAR_COUNT_OPT (
553     Track, chord_regions, region_schema),
554   YAML_FIELD_DYN_PTR_ARRAY_VAR_COUNT_OPT (
555     Track, scales, scale_object_schema),
556   YAML_FIELD_DYN_PTR_ARRAY_VAR_COUNT_OPT (
557     Track, markers, marker_schema),
558   YAML_FIELD_MAPPING_PTR_OPTIONAL (
559     Track, channel, channel_fields_schema),
560   YAML_FIELD_MAPPING_PTR_OPTIONAL (
561     Track, bpm_port, port_fields_schema),
562   YAML_FIELD_MAPPING_PTR_OPTIONAL (
563     Track, beats_per_bar_port, port_fields_schema),
564   YAML_FIELD_MAPPING_PTR_OPTIONAL (
565     Track, beat_unit_port, port_fields_schema),
566   YAML_FIELD_DYN_ARRAY_VAR_COUNT (
567     Track, modulators, plugin_schema),
568   YAML_FIELD_FIXED_SIZE_PTR_ARRAY_VAR_COUNT (
569     Track, modulator_macros,
570     modulator_macro_processor_schema),
571   YAML_FIELD_INT (
572     Track, num_visible_modulator_macros),
573   YAML_FIELD_MAPPING_PTR (
574     Track, processor,
575     track_processor_fields_schema),
576   YAML_FIELD_MAPPING_EMBEDDED (
577     Track, automation_tracklist,
578     automation_tracklist_fields_schema),
579   YAML_FIELD_ENUM (
580     Track, in_signal_type, port_type_strings),
581   YAML_FIELD_ENUM (
582     Track, out_signal_type, port_type_strings),
583   YAML_FIELD_UINT (
584     Track, midi_ch),
585   YAML_FIELD_STRING_PTR (
586     Track, comment),
587   YAML_FIELD_DYN_ARRAY_VAR_COUNT_PRIMITIVES (
588     Track, children, unsigned_int_schema),
589   YAML_FIELD_INT (Track, frozen),
590   YAML_FIELD_INT (Track, pool_id),
591   YAML_FIELD_INT (Track, size),
592   YAML_FIELD_INT (Track, folded),
593   YAML_FIELD_INT (
594     Track, record_set_automatically),
595   YAML_FIELD_INT (Track, drum_mode),
596 
597   CYAML_FIELD_END
598 };
599 
600 static const cyaml_schema_value_t
601 track_schema = {
602   YAML_VALUE_PTR (
603     Track, track_fields_schema),
604 };
605 
606 COLD
607 NONNULL_ARGS (1)
608 void
609 track_init_loaded (
610   Track *               self,
611   Tracklist *           tracklist,
612   TracklistSelections * ts);
613 
614 /**
615  * Inits the Track, optionally adding a single
616  * lane.
617  *
618  * @param add_lane Add a lane. This should be used
619  *   for new Tracks. When cloning, the lanes should
620  *   be cloned so this should be 0.
621  */
622 void
623 track_init (
624   Track *   self,
625   const int add_lane);
626 
627 /**
628  * Creates a track with the given label and returns
629  * it.
630  *
631  * If the TrackType is one that needs a Channel,
632  * then a Channel is also created for the track.
633  *
634  * @param pos Position in the Tracklist.
635  * @param with_lane Init the Track with a lane.
636  */
637 Track *
638 track_new (
639   TrackType    type,
640   int          pos,
641   const char * label,
642   const int    with_lane);
643 
644 /**
645  * Clones the track and returns the clone.
646  *
647  * @param error To be filled if an error occurred.
648  */
649 NONNULL_ARGS (1)
650 Track *
651 track_clone (
652   Track *   track,
653   GError ** error);
654 
655 /**
656  * Returns if the given TrackType is a type of
657  * Track that has a Channel.
658  */
659 bool
660 track_type_has_channel (
661   TrackType type);
662 
663 static inline bool
track_type_can_have_region_type(TrackType type,RegionType region_type)664 track_type_can_have_region_type (
665   TrackType  type,
666   RegionType region_type)
667 {
668   switch (region_type)
669     {
670     case REGION_TYPE_AUDIO:
671       return type == TRACK_TYPE_AUDIO;
672     case REGION_TYPE_MIDI:
673       return
674         type == TRACK_TYPE_MIDI ||
675         type == TRACK_TYPE_INSTRUMENT;
676     case REGION_TYPE_CHORD:
677       return
678         type == TRACK_TYPE_CHORD;
679     case REGION_TYPE_AUTOMATION:
680       return true;
681     }
682 
683   g_return_val_if_reached (false);
684 }
685 
686 static inline bool
track_type_is_foldable(TrackType type)687 track_type_is_foldable (
688   TrackType type)
689 {
690   return
691     type == TRACK_TYPE_FOLDER ||
692     type == TRACK_TYPE_MIDI_GROUP ||
693     type == TRACK_TYPE_AUDIO_GROUP;
694 }
695 
696 /**
697  * Sets magic on objects recursively.
698  */
699 NONNULL
700 void
701 track_set_magic (
702   Track * self);
703 
704 NONNULL
705 unsigned int
706 track_get_name_hash (
707   Track * self);
708 
709 /**
710  * Sets track muted and optionally adds the action
711  * to the undo stack.
712  */
713 NONNULL
714 void
715 track_set_muted (
716   Track * track,
717   bool    mute,
718   bool    trigger_undo,
719   bool    auto_select,
720   bool    fire_events);
721 
722 /**
723  * Sets track folded and optionally adds the action
724  * to the undo stack.
725  */
726 NONNULL
727 void
728 track_set_folded (
729   Track * self,
730   bool    folded,
731   bool    trigger_undo,
732   bool    auto_select,
733   bool    fire_events);
734 
735 NONNULL
736 TrackType
737 track_get_type_from_plugin_descriptor (
738   PluginDescriptor * descr);
739 
740 /**
741  * Returns whether the track type is deletable
742  * by the user.
743  */
744 NONNULL
745 bool
746 track_type_is_deletable (
747   TrackType type);
748 
749 NONNULL
750 Tracklist *
751 track_get_tracklist (
752   Track * self);
753 
754 /**
755  * Returns whether the track should be visible.
756  *
757  * Takes into account Track.visible and whether
758  * any of the track's foldable parents are folded.
759  */
760 NONNULL
761 bool
762 track_get_should_be_visible (
763   Track * self);
764 
765 /**
766  * Returns the full visible height (main height +
767  * height of all visible automation tracks + height
768  * of all visible lanes).
769  */
770 NONNULL
771 double
772 track_get_full_visible_height (
773   Track * const self);
774 
775 bool
776 track_multiply_heights (
777   Track * self,
778   double  multiplier,
779   bool    visible_only,
780   bool    check_only);
781 
782 /**
783  * Returns if the track is soloed.
784  */
785 HOT
786 NONNULL
787 bool
788 track_get_soloed (
789   Track * self);
790 
791 /**
792  * Returns whether the track is not soloed on its
793  * own but its direct out (or its direct out's direct
794  * out, etc.) is soloed.
795  */
796 NONNULL
797 bool
798 track_get_implied_soloed (
799   Track * self);
800 
801 /**
802  * Returns if the track is muted.
803  */
804 NONNULL
805 bool
806 track_get_muted (
807   Track * self);
808 
809 /**
810  * Returns if the track is listened.
811  */
812 NONNULL
813 bool
814 track_get_listened (
815   Track * self);
816 
817 /**
818  * Returns whether monitor audio is on.
819  */
820 NONNULL
821 bool
822 track_get_monitor_audio (
823   Track * self);
824 
825 /**
826  * Sets whether monitor audio is on.
827  */
828 NONNULL
829 void
830 track_set_monitor_audio (
831   Track * self,
832   bool    monitor,
833   bool    auto_select,
834   bool    fire_events);
835 
836 /**
837  * Sets track soloed, updates UI and optionally
838  * adds the action to the undo stack.
839  *
840  * @param auto_select Makes this track the only
841  *   selection in the tracklist. Useful when
842  *   listening to a single track.
843  * @param trigger_undo Create and perform an
844  *   undoable action.
845  * @param fire_events Fire UI events.
846  */
847 NONNULL
848 void
849 track_set_listened (
850   Track * self,
851   bool    listen,
852   bool    trigger_undo,
853   bool    auto_select,
854   bool    fire_events);
855 
856 HOT
857 NONNULL
858 bool
859 track_get_recording (
860   const Track * const track);
861 
862 /**
863  * Sets recording and connects/disconnects the
864  * JACK ports.
865  */
866 NONNULL
867 void
868 track_set_recording (
869   Track *   track,
870   bool      recording,
871   bool      fire_events);
872 
873 /**
874  * Sets track soloed, updates UI and optionally
875  * adds the action to the undo stack.
876  *
877  * @param auto_select Makes this track the only
878  *   selection in the tracklist. Useful when soloing
879  *   a single track.
880  * @param trigger_undo Create and perform an
881  *   undoable action.
882  * @param fire_events Fire UI events.
883  */
884 NONNULL
885 void
886 track_set_soloed (
887   Track * self,
888   bool    solo,
889   bool    trigger_undo,
890   bool    auto_select,
891   bool    fire_events);
892 
893 /**
894  * Returns if Track is in TracklistSelections.
895  */
896 NONNULL
897 int
898 track_is_selected (Track * self);
899 
900 /**
901  * Returns whether the track is pinned.
902  */
903 #define track_is_pinned(x) \
904   (x->pos < TRACKLIST->pinned_tracks_cutoff)
905 
906 /**
907  * Adds a ZRegion to the given lane or
908  * AutomationTrack of the track.
909  *
910  * The ZRegion must be the main region (see
911  * ArrangerObjectInfo).
912  *
913  * @param at The AutomationTrack of this ZRegion, if
914  *   automation region.
915  * @param lane_pos The position of the lane to add
916  *   to, if applicable.
917  * @param gen_name Generate a unique region name or
918  *   not. This will be 0 if the caller already
919  *   generated a unique name.
920  */
921 void
922 track_add_region (
923   Track *           track,
924   ZRegion *         region,
925   AutomationTrack * at,
926   int               lane_pos,
927   int               gen_name,
928   int               fire_events);
929 
930 /**
931  * Inserts a ZRegion to the given lane or
932  * AutomationTrack of the track, at the given
933  * index.
934  *
935  * The ZRegion must be the main region (see
936  * ArrangerObjectInfo).
937  *
938  * @param at The AutomationTrack of this ZRegion, if
939  *   automation region.
940  * @param lane_pos The position of the lane to add
941  *   to, if applicable.
942  * @param idx The index to insert the region at
943  *   inside its parent, or -1 to append.
944  * @param gen_name Generate a unique region name or
945  *   not. This will be 0 if the caller already
946  *   generated a unique name.
947  */
948 void
949 track_insert_region (
950   Track *           track,
951   ZRegion *         region,
952   AutomationTrack * at,
953   int               lane_pos,
954   int               idx,
955   int               gen_name,
956   int               fire_events);
957 
958 /**
959  * Writes the track to the given MIDI file.
960  */
961 NONNULL
962 void
963 track_write_to_midi_file (
964   Track *     self,
965   MIDI_FILE * mf);
966 
967 /**
968  * Appends the Track to the selections.
969  *
970  * @param exclusive Select only this track.
971  * @param fire_events Fire events to update the
972  *   UI.
973  */
974 NONNULL
975 void
976 track_select (
977   Track * self,
978   bool    select,
979   bool    exclusive,
980   bool    fire_events);
981 
982 /**
983  * Unselects all arranger objects in the track.
984  */
985 NONNULL
986 void
987 track_unselect_all (
988   Track * self);
989 
990 /**
991  * Removes all objects recursively from the track.
992  */
993 NONNULL
994 void
995 track_clear (
996   Track * self);
997 
998 /**
999  * Only removes the region from the track.
1000  *
1001  * @pararm free Also free the Region.
1002  */
1003 NONNULL
1004 void
1005 track_remove_region (
1006   Track *   self,
1007   ZRegion * region,
1008   bool      fire_events,
1009   bool      free);
1010 
1011 /**
1012  * Wrapper for audio and MIDI/instrument tracks
1013  * to fill in MidiEvents or StereoPorts from the
1014  * timeline data.
1015  *
1016  * @note The engine splits the cycle so transport
1017  *   loop related logic is not needed.
1018  *
1019  * @param stereo_ports StereoPorts to fill.
1020  * @param midi_events MidiEvents to fill (from
1021  *   Piano Roll Port for example).
1022  */
1023 void
1024 track_fill_events (
1025   const Track *                       self,
1026   const EngineProcessTimeInfo * const time_nfo,
1027   MidiEvents *                        midi_events,
1028   StereoPorts *                       stereo_ports);
1029 
1030 /**
1031  * Verifies the identifiers on a live Track
1032  * (in the project, not a clone).
1033  *
1034  * @return True if pass.
1035  */
1036 bool
1037 track_validate (
1038   Track * self);
1039 
1040 /**
1041  * Adds the track's folder parents to the given
1042  * array.
1043  *
1044  * @param prepend Whether to prepend instead of
1045  *   append.
1046  */
1047 void
1048 track_add_folder_parents (
1049   Track *     self,
1050   GPtrArray * parents,
1051   bool        prepend);
1052 
1053 /**
1054  * Returns the closest foldable parent or NULL.
1055  */
1056 Track *
1057 track_get_direct_folder_parent (
1058   Track * track);
1059 
1060 /**
1061  * Remove the track from all folders.
1062  *
1063  * Used when deleting tracks.
1064  */
1065 void
1066 track_remove_from_folder_parents (
1067   Track * self);
1068 
1069 /**
1070  * Returns the region at the given position, or
1071  * NULL.
1072  *
1073  * @param include_region_end Whether to include the
1074  *   region's end in the calculation.
1075  */
1076 ZRegion *
1077 track_get_region_at_pos (
1078   const Track *    track,
1079   const Position * pos,
1080   bool             include_region_end);
1081 
1082 /**
1083  * Returns the last ZRegion in the
1084  * track, or NULL.
1085  */
1086 ZRegion *
1087 track_get_last_region (
1088   Track *    track);
1089 
1090 /**
1091  * Set track lanes visible and fire events.
1092  */
1093 void
1094 track_set_lanes_visible (
1095   Track *   track,
1096   const int visible);
1097 
1098 /**
1099  * Set automation visible and fire events.
1100  */
1101 void
1102 track_set_automation_visible (
1103   Track *    track,
1104   const bool visible);
1105 
1106 /**
1107  * Returns if the given TrackType can host the
1108  * given RegionType.
1109  */
1110 int
1111 track_type_can_host_region_type (
1112   const TrackType  tt,
1113   const RegionType rt);
1114 
1115 static inline bool
track_type_has_mono_compat_switch(const TrackType tt)1116 track_type_has_mono_compat_switch (
1117   const TrackType tt)
1118 {
1119   return
1120     tt == TRACK_TYPE_AUDIO_GROUP ||
1121     tt == TRACK_TYPE_MASTER;
1122 }
1123 
1124 #define track_type_is_audio_group \
1125   track_type_has_mono_compat_switch
1126 
1127 static inline bool
track_type_is_fx(const TrackType type)1128 track_type_is_fx (
1129   const TrackType type)
1130 {
1131   return
1132     type == TRACK_TYPE_AUDIO_BUS ||
1133     type == TRACK_TYPE_MIDI_BUS;
1134 }
1135 
1136 /**
1137  * Returns if the Track can record.
1138  */
1139 static inline int
track_type_can_record(const TrackType type)1140 track_type_can_record (
1141   const TrackType type)
1142 {
1143   return
1144     type == TRACK_TYPE_AUDIO ||
1145     type == TRACK_TYPE_MIDI ||
1146     type == TRACK_TYPE_INSTRUMENT;
1147   /* TODO add chord when implemented */
1148 }
1149 
1150 /**
1151  * Generates automatables for the track.
1152  *
1153  * Should be called as soon as the track is
1154  * created.
1155  */
1156 void
1157 track_generate_automation_tracks (
1158   Track * track);
1159 
1160 /**
1161  * Wrapper.
1162  */
1163 void
1164 track_setup (Track * track);
1165 
1166 /**
1167  * Returns the automation tracklist if the track
1168  * type has one,
1169  * or NULL if it doesn't (like chord tracks).
1170  */
1171 AutomationTracklist *
1172 track_get_automation_tracklist (
1173   Track * const track);
1174 
1175 /**
1176  * Returns the channel of the track, if the track
1177  * type has a channel, or NULL if it doesn't.
1178  */
1179 NONNULL
1180 static inline Channel *
track_get_channel(const Track * const track)1181 track_get_channel (
1182   const Track * const track)
1183 {
1184   switch (track->type)
1185     {
1186     case TRACK_TYPE_MASTER:
1187     case TRACK_TYPE_INSTRUMENT:
1188     case TRACK_TYPE_AUDIO:
1189     case TRACK_TYPE_AUDIO_BUS:
1190     case TRACK_TYPE_AUDIO_GROUP:
1191     case TRACK_TYPE_MIDI_BUS:
1192     case TRACK_TYPE_MIDI_GROUP:
1193     case TRACK_TYPE_MIDI:
1194     case TRACK_TYPE_CHORD:
1195       return track->channel;
1196     default:
1197       return NULL;
1198     }
1199 }
1200 
1201 /**
1202  * Updates the track's children.
1203  *
1204  * Used when changing track positions.
1205  */
1206 void
1207 track_update_children (
1208   Track * self);
1209 
1210 /**
1211  * Returns if the Track should have a piano roll.
1212  */
1213 bool
1214 track_type_has_piano_roll (
1215   const TrackType type);
1216 
1217 /**
1218  * Returns if the Track should have an inputs
1219  * selector.
1220  */
1221 static inline int
track_has_inputs(const Track * track)1222 track_has_inputs (
1223   const Track * track)
1224 {
1225   return
1226     track->type == TRACK_TYPE_MIDI ||
1227     track->type == TRACK_TYPE_INSTRUMENT ||
1228     track->type == TRACK_TYPE_AUDIO;
1229 }
1230 
1231 Track *
1232 track_find_by_name (
1233   const char * name);
1234 
1235 /**
1236  * Fills in the array with all the velocities in
1237  * the project that are within or outside the
1238  * range given.
1239  *
1240  * @param inside Whether to find velocities inside
1241  *   the range (1) or outside (0).
1242  */
1243 void
1244 track_get_velocities_in_range (
1245   const Track *    track,
1246   const Position * start_pos,
1247   const Position * end_pos,
1248   Velocity ***     velocities,
1249   int *            num_velocities,
1250   size_t *         velocities_size,
1251   int              inside);
1252 
1253 /**
1254  * Getter for the track name.
1255  */
1256 const char *
1257 track_get_name (Track * track);
1258 
1259 /**
1260  * Internally called by
1261  * track_set_name_with_action().
1262  */
1263 NONNULL
1264 bool
1265 track_set_name_with_action_full (
1266   Track *      track,
1267   const char * name);
1268 
1269 /**
1270  * Setter to be used by the UI to create an
1271  * undoable action.
1272  */
1273 void
1274 track_set_name_with_action (
1275   Track *      track,
1276   const char * name);
1277 
1278 /**
1279  * Setter for the track name.
1280  *
1281  * If a track with that name already exists, it
1282  * adds a number at the end.
1283  *
1284  * Must only be called from the GTK thread.
1285  */
1286 void
1287 track_set_name (
1288   Track *      self,
1289   const char * name,
1290   bool         pub_events);
1291 
1292 /**
1293  * Returns a unique name for a new track based on
1294  * the given name.
1295  */
1296 char *
1297 track_get_unique_name (
1298   Track *      track_to_skip,
1299   const char * name);
1300 
1301 /**
1302  * Returns the Track from the Project matching
1303  * \p name.
1304  *
1305  * @param name Name to search for.
1306  */
1307 Track *
1308 track_get_from_name (
1309   const char * name);
1310 
1311 const char *
1312 track_stringize_type (
1313   TrackType type);
1314 
1315 /**
1316  * Returns if regions in tracks from type1 can
1317  * be moved to type2.
1318  */
1319 static inline int
track_type_is_compatible_for_moving(const TrackType type1,const TrackType type2)1320 track_type_is_compatible_for_moving (
1321   const TrackType type1,
1322   const TrackType type2)
1323 {
1324   return
1325     type1 == type2 ||
1326     (type1 == TRACK_TYPE_MIDI &&
1327      type2 == TRACK_TYPE_INSTRUMENT) ||
1328     (type1 == TRACK_TYPE_INSTRUMENT &&
1329      type2 == TRACK_TYPE_MIDI);
1330 }
1331 
1332 /**
1333  * Updates the frames/ticks of each position in
1334  * each child of the track recursively.
1335  *
1336  * @param from_ticks Whether to update the
1337  *   positions based on ticks (true) or frames
1338  *   (false).
1339  */
1340 void
1341 track_update_positions (
1342   Track * self,
1343   bool    from_ticks);
1344 
1345 /**
1346  * Returns the Fader (if applicable).
1347  *
1348  * @param post_fader True to get post fader,
1349  *   false to get pre fader.
1350  */
1351 Fader *
1352 track_get_fader (
1353   Track * track,
1354   bool    post_fader);
1355 
1356 /**
1357  * Returns the FaderType corresponding to the given
1358  * Track.
1359  */
1360 FaderType
1361 track_get_fader_type (
1362   const Track * track);
1363 
1364 /**
1365  * Returns the prefader type
1366  * corresponding to the given Track.
1367  */
1368 FaderType
1369 track_type_get_prefader_type (
1370   TrackType type);
1371 
1372 /**
1373  * Creates missing TrackLane's until pos.
1374  */
1375 void
1376 track_create_missing_lanes (
1377   Track *   track,
1378   const int pos);
1379 
1380 /**
1381  * Removes the empty last lanes of the Track
1382  * (except the last one).
1383  */
1384 void
1385 track_remove_empty_last_lanes (
1386   Track * track);
1387 
1388 /**
1389  * Returns all the regions inside the given range,
1390  * or all the regions if both @ref p1 and @ref p2
1391  * are NULL.
1392  *
1393  * @return The number of regions returned.
1394  */
1395 int
1396 track_get_regions_in_range (
1397   Track *    self,
1398   Position * p1,
1399   Position * p2,
1400   ZRegion ** regions);
1401 
1402 void
1403 track_activate_all_plugins (
1404   Track * track,
1405   bool    activate);
1406 
1407 /**
1408  * Comment setter.
1409  *
1410  * @note This creates an undoable action.
1411  */
1412 void
1413 track_comment_setter (
1414   void *        track,
1415   const char *  comment);
1416 
1417 /**
1418  * @param undoable Create an undable action.
1419  */
1420 void
1421 track_set_comment (
1422   Track *       self,
1423   const char *  comment,
1424   bool          undoable);
1425 
1426 /**
1427  * Comment getter.
1428  */
1429 const char *
1430 track_get_comment (
1431   void *  track);
1432 
1433 /**
1434  * Sets the track color.
1435  */
1436 void
1437 track_set_color (
1438   Track *         self,
1439   const GdkRGBA * color,
1440   bool            undoable,
1441   bool            fire_events);
1442 
1443 /**
1444  * Sets the track icon.
1445  */
1446 void
1447 track_set_icon (
1448   Track *      self,
1449   const char * icon_name,
1450   bool         undoable,
1451   bool         fire_events);
1452 
1453 /**
1454  * Returns the plugin at the given slot, if any.
1455  *
1456  * @param slot The slot (ignored if instrument is
1457  *   selected.
1458  */
1459 Plugin *
1460 track_get_plugin_at_slot (
1461   Track *           track,
1462   PluginSlotType    slot_type,
1463   int               slot);
1464 
1465 /**
1466  * Marks the track for bouncing.
1467  *
1468  * @param mark_children Whether to mark all
1469  *   children tracks as well. Used when exporting
1470  *   stems on the specific track stem only.
1471  * @param mark_parents Whether to mark all parent
1472  *   tracks as well.
1473  */
1474 void
1475 track_mark_for_bounce (
1476   Track * self,
1477   bool    bounce,
1478   bool    mark_regions,
1479   bool    mark_children,
1480   bool    mark_parents);
1481 
1482 /**
1483  * Appends all channel ports and optionally
1484  * plugin ports to the array.
1485  */
1486 void
1487 track_append_ports (
1488   Track *     self,
1489   GPtrArray * ports,
1490   bool        include_plugins);
1491 
1492 /**
1493  * Freezes or unfreezes the track.
1494  *
1495  * When a track is frozen, it is bounced with
1496  * effects to a temporary file in the pool, which
1497  * is played back directly from disk.
1498  *
1499  * When the track is unfrozen, this file will be
1500  * removed from the pool and the track will be
1501  * played normally again.
1502  */
1503 void
1504 track_freeze (
1505   Track * self,
1506   bool    freeze);
1507 
1508 /**
1509  * Wrapper over channel_add_plugin() and
1510  * modulator_track_insert_modulator().
1511  *
1512  * @param instantiate_plugin Whether to attempt to
1513  *   instantiate the plugin.
1514  */
1515 void
1516 track_insert_plugin (
1517   Track *        self,
1518   Plugin *       pl,
1519   PluginSlotType slot_type,
1520   int            slot,
1521   bool           instantiate_plugin,
1522   bool           replacing_plugin,
1523   bool           moving_plugin,
1524   bool           confirm,
1525   bool           gen_automatables,
1526   bool           recalc_graph,
1527   bool           fire_events);
1528 
1529 /**
1530  * Wrapper over channel_remove_plugin() and
1531  * modulator_track_remove_modulator().
1532  */
1533 void
1534 track_remove_plugin (
1535   Track *        self,
1536   PluginSlotType slot_type,
1537   int            slot,
1538   bool           replacing_plugin,
1539   bool           moving_plugin,
1540   bool           deleting_plugin,
1541   bool           deleting_track,
1542   bool           recalc_graph);
1543 
1544 /**
1545  * Disconnects the track from the processing
1546  * chain.
1547  *
1548  * This should be called immediately when the
1549  * track is getting deleted, and track_free
1550  * should be designed to be called later after
1551  * an arbitrary delay.
1552  *
1553  * @param remove_pl Remove the Plugin from the
1554  *   Channel. Useful when deleting the channel.
1555  * @param recalc_graph Recalculate mixer graph.
1556  */
1557 void
1558 track_disconnect (
1559   Track * self,
1560   bool    remove_pl,
1561   bool    recalc_graph);
1562 
1563 NONNULL
1564 bool
1565 track_is_enabled (
1566   Track * self);
1567 
1568 NONNULL
1569 void
1570 track_set_enabled (
1571   Track * self,
1572   bool    enabled,
1573   bool    trigger_undo,
1574   bool    auto_select,
1575   bool    fire_events);
1576 
1577 static inline const char *
track_type_to_string(TrackType type)1578 track_type_to_string (
1579   TrackType type)
1580 {
1581   return track_type_strings[type].str;
1582 }
1583 
1584 TrackType
1585 track_type_get_from_string (
1586   const char * str);
1587 
1588 void
1589 track_get_total_bars (
1590   Track * self,
1591   int *   total_bars);
1592 
1593 void
1594 track_set_caches (
1595   Track * self);
1596 
1597 Track *
1598 track_create_with_action (
1599   TrackType       type,
1600   PluginSetting * pl_setting,
1601   SupportedFile * file_descr,
1602   Position *      pos,
1603   int             index,
1604   int             num_tracks,
1605   GError **       error);
1606 
1607 #define track_create_empty_at_idx_with_action( \
1608   type,idx,error) \
1609   track_create_with_action ( \
1610     type, NULL, NULL, NULL, idx, 1, error)
1611 
1612 /**
1613  * Create track at index for plugin with action.
1614  */
1615 #define track_create_for_plugin_at_idx_w_action( \
1616   type,pl_setting,idx,error) \
1617   track_create_with_action ( \
1618     type, pl_setting, NULL, NULL, idx, 1, error)
1619 
1620 /**
1621  * Creates a new empty track at the end of the
1622  * tracklist.
1623  */
1624 #define track_create_empty_with_action(type,error) \
1625   track_create_empty_at_idx_with_action ( \
1626     type, TRACKLIST->num_tracks, error)
1627 
1628 /**
1629  * Wrapper for each track type.
1630  */
1631 void
1632 track_free (Track * track);
1633 
1634 /**
1635  * @}
1636  */
1637 
1638 #endif // __AUDIO_TRACK_H__
1639