NOOP: whitespace change
[ardour.git] / libs / ardour / ardour / region.h
1 /*
2  * Copyright (C) 2000-2017 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2006-2014 David Robillard <d@drobilla.net>
4  * Copyright (C) 2007-2012 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
6  * Copyright (C) 2015-2017 Nick Mainsbridge <mainsbridge@gmail.com>
7  * Copyright (C) 2018 Ben Loftis <ben@harrisonconsoles.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #ifndef __ardour_region_h__
25 #define __ardour_region_h__
26
27 #include <vector>
28 #include <boost/shared_ptr.hpp>
29 #include <boost/enable_shared_from_this.hpp>
30 #include <boost/utility.hpp>
31
32 #include "pbd/undo.h"
33 #include "pbd/signals.h"
34 #include "ardour/ardour.h"
35 #include "ardour/data_type.h"
36 #include "ardour/automatable.h"
37 #include "ardour/movable.h"
38 #include "ardour/readable.h"
39 #include "ardour/session_object.h"
40 #include "ardour/trimmable.h"
41 #include "ardour/types_convert.h"
42
43 class XMLNode;
44
45 namespace ARDOUR {
46
47 namespace Properties {
48         LIBARDOUR_API extern PBD::PropertyDescriptor<bool>              muted;
49         LIBARDOUR_API extern PBD::PropertyDescriptor<bool>              opaque;
50         LIBARDOUR_API extern PBD::PropertyDescriptor<bool>              locked;
51         LIBARDOUR_API extern PBD::PropertyDescriptor<bool>              video_locked;
52         LIBARDOUR_API extern PBD::PropertyDescriptor<bool>              automatic;
53         LIBARDOUR_API extern PBD::PropertyDescriptor<bool>              whole_file;
54         LIBARDOUR_API extern PBD::PropertyDescriptor<bool>              import;
55         LIBARDOUR_API extern PBD::PropertyDescriptor<bool>              external;
56         LIBARDOUR_API extern PBD::PropertyDescriptor<bool>              sync_marked;
57         LIBARDOUR_API extern PBD::PropertyDescriptor<bool>              left_of_split;
58         LIBARDOUR_API extern PBD::PropertyDescriptor<bool>              right_of_split;
59         LIBARDOUR_API extern PBD::PropertyDescriptor<bool>              hidden;
60         LIBARDOUR_API extern PBD::PropertyDescriptor<bool>              position_locked;
61         LIBARDOUR_API extern PBD::PropertyDescriptor<bool>              valid_transients;
62         LIBARDOUR_API extern PBD::PropertyDescriptor<samplepos_t>       start;
63         LIBARDOUR_API extern PBD::PropertyDescriptor<samplecnt_t>       length;
64         LIBARDOUR_API extern PBD::PropertyDescriptor<samplepos_t>       position;
65         LIBARDOUR_API extern PBD::PropertyDescriptor<double>            beat;
66         LIBARDOUR_API extern PBD::PropertyDescriptor<samplecnt_t>       sync_position;
67         LIBARDOUR_API extern PBD::PropertyDescriptor<layer_t>           layer;
68         LIBARDOUR_API extern PBD::PropertyDescriptor<samplepos_t>       ancestral_start;
69         LIBARDOUR_API extern PBD::PropertyDescriptor<samplecnt_t>       ancestral_length;
70         LIBARDOUR_API extern PBD::PropertyDescriptor<float>             stretch;
71         LIBARDOUR_API extern PBD::PropertyDescriptor<float>             shift;
72         LIBARDOUR_API extern PBD::PropertyDescriptor<PositionLockStyle> position_lock_style;
73         LIBARDOUR_API extern PBD::PropertyDescriptor<uint64_t>          layering_index;
74         LIBARDOUR_API extern PBD::PropertyDescriptor<std::string>       tags;
75 };
76
77 class Playlist;
78 class Filter;
79 class ExportSpecification;
80 class Progress;
81
82 enum LIBARDOUR_API RegionEditState {
83         EditChangesNothing = 0,
84         EditChangesName    = 1,
85         EditChangesID      = 2
86 };
87
88
89 class LIBARDOUR_API Region
90         : public SessionObject
91         , public boost::enable_shared_from_this<Region>
92         , public Readable
93         , public Trimmable
94         , public Movable
95 {
96 public:
97         typedef std::vector<boost::shared_ptr<Source> > SourceList;
98
99         static void make_property_quarks ();
100
101         static PBD::Signal2<void,boost::shared_ptr<ARDOUR::Region>, const PBD::PropertyChange&> RegionPropertyChanged;
102
103         virtual ~Region();
104
105         /** Note: changing the name of a Region does not constitute an edit */
106         bool set_name (const std::string& str);
107
108         const DataType& data_type () const { return _type; }
109
110         /** How the region parameters play together:
111          *
112          * POSITION: first sample of the region along the timeline
113          * START:    first sample of the region within its source(s)
114          * LENGTH:   number of samples the region represents
115          */
116         samplepos_t position () const { return _position; }
117         samplepos_t start ()    const { return _start; }
118         samplecnt_t length ()   const { return _length; }
119         layer_t    layer ()     const { return _layer; }
120
121         void set_selected_for_solo(bool yn);
122
123         samplecnt_t source_length (uint32_t n) const;
124         uint32_t   max_source_level () const;
125
126         /* these two are valid ONLY during a StateChanged signal handler */
127
128         samplepos_t last_position () const { return _last_position; }
129         samplecnt_t last_length ()   const { return _last_length; }
130
131         samplepos_t ancestral_start ()  const { return _ancestral_start; }
132         samplecnt_t ancestral_length () const { return _ancestral_length; }
133
134         float stretch () const { return _stretch; }
135         float shift ()   const { return _shift; }
136
137         void set_ancestral_data (samplepos_t start, samplecnt_t length, float stretch, float shift);
138
139         sampleoffset_t sync_offset (int& dir) const;
140         samplepos_t sync_position () const;
141
142         samplepos_t adjust_to_sync (samplepos_t) const;
143
144         /* first_sample() is an alias; last_sample() just hides some math */
145
146         samplepos_t first_sample () const { return _position; }
147         samplepos_t last_sample ()  const { return _position + _length - 1; }
148
149         /** Return the earliest possible value of _position given the
150          *  value of _start within the region's sources
151          */
152         samplepos_t earliest_possible_position () const;
153         /** Return the last possible value of _last_sample given the
154          *  value of _startin the regions's sources
155          */
156         samplepos_t latest_possible_sample () const;
157
158         Evoral::Range<samplepos_t> last_range () const {
159                 return Evoral::Range<samplepos_t> (_last_position, _last_position + _last_length - 1);
160         }
161
162         Evoral::Range<samplepos_t> range () const {
163                 return Evoral::Range<samplepos_t> (first_sample(), last_sample());
164         }
165
166         bool hidden ()           const { return _hidden; }
167         bool muted ()            const { return _muted; }
168         bool opaque ()           const { return _opaque; }
169         bool locked ()           const { return _locked; }
170         bool position_locked ()  const { return _position_locked; }
171         bool video_locked ()     const { return _video_locked; }
172         bool automatic ()        const { return _automatic; }
173         bool whole_file ()       const { return _whole_file; }
174         bool captured ()         const { return !(_import || _external); }
175         bool can_move ()         const { return !_position_locked && !_locked; }
176         bool sync_marked ()      const { return _sync_marked; }
177         bool external ()         const { return _external; }
178         bool import ()           const { return _import; }
179
180         Trimmable::CanTrim can_trim () const;
181
182         PositionLockStyle position_lock_style () const { return _position_lock_style; }
183         void set_position_lock_style (PositionLockStyle ps);
184         void recompute_position_from_lock_style (const int32_t sub_num);
185
186         /* meter-based beat at the region position */
187         double beat () const { return _beat; }
188         void set_beat (double beat) { _beat = beat; }
189         /* quarter-note at the region position */
190         double quarter_note () const { return _quarter_note; }
191         void set_quarter_note (double qn) { _quarter_note = qn; }
192
193         void suspend_property_changes ();
194
195         bool covers (samplepos_t sample) const {
196                 return first_sample() <= sample && sample <= last_sample();
197         }
198
199         /** @return coverage of this region with the given range;
200          *  OverlapInternal: the range is internal to this region.
201          *  OverlapStart:    the range overlaps the start of this region.
202          *  OverlapEnd:      the range overlaps the end of this region.
203          *  OverlapExternal: the range overlaps all of this region.
204          */
205         Evoral::OverlapType coverage (samplepos_t start, samplepos_t end) const {
206                 return Evoral::coverage (first_sample(), last_sample(), start, end);
207         }
208
209         bool exact_equivalent (boost::shared_ptr<const Region>) const;
210         bool size_equivalent (boost::shared_ptr<const Region>) const;
211         bool overlap_equivalent (boost::shared_ptr<const Region>) const;
212         bool enclosed_equivalent (boost::shared_ptr<const Region>) const;
213         bool layer_and_time_equivalent (boost::shared_ptr<const Region>) const;
214         bool region_list_equivalent (boost::shared_ptr<const Region>) const;
215         bool source_equivalent (boost::shared_ptr<const Region>) const;
216         bool any_source_equivalent (boost::shared_ptr<const Region>) const;
217         bool uses_source (boost::shared_ptr<const Source>, bool shallow = false) const;
218         void deep_sources (std::set<boost::shared_ptr<Source> >&) const;
219
220         std::string source_string () const;
221
222
223         /* EDITING OPERATIONS */
224
225         void set_length (samplecnt_t, const int32_t sub_num);
226         void set_start (samplepos_t);
227         void set_position (samplepos_t, int32_t sub_num = 0);
228         void set_position_music (double qn);
229         void set_initial_position (samplepos_t);
230         void special_set_position (samplepos_t);
231         virtual void update_after_tempo_map_change (bool send_change = true);
232         void nudge_position (sampleoffset_t);
233
234         bool at_natural_position () const;
235         void move_to_natural_position ();
236
237         void move_start (sampleoffset_t distance, const int32_t sub_num = 0);
238         void trim_front (samplepos_t new_position, const int32_t sub_num = 0);
239         void trim_end (samplepos_t new_position, const int32_t sub_num = 0);
240         void trim_to (samplepos_t position, samplecnt_t length, const int32_t sub_num = 0);
241
242         virtual void fade_range (samplepos_t, samplepos_t) {}
243
244         void cut_front (samplepos_t new_position, const int32_t sub_num = 0);
245         void cut_end (samplepos_t new_position, const int32_t sub_num = 0);
246
247         void set_layer (layer_t l); /* ONLY Playlist can call this */
248         void raise ();
249         void lower ();
250         void raise_to_top ();
251         void lower_to_bottom ();
252
253         void set_sync_position (samplepos_t n);
254         void clear_sync_position ();
255         void set_hidden (bool yn);
256         void set_muted (bool yn);
257         void set_whole_file (bool yn);
258         void set_automatic (bool yn);
259         void set_opaque (bool yn);
260         void set_locked (bool yn);
261         void set_video_locked (bool yn);
262         void set_position_locked (bool yn);
263
264         int apply (Filter &, Progress* progress = 0);
265
266         boost::shared_ptr<ARDOUR::Playlist> playlist () const { return _playlist.lock(); }
267         virtual void set_playlist (boost::weak_ptr<ARDOUR::Playlist>);
268
269         void source_deleted (boost::weak_ptr<Source>);
270
271         bool is_compound () const;
272
273         boost::shared_ptr<Source> source (uint32_t n=0) const { return _sources[ (n < _sources.size()) ? n : 0 ]; }
274         uint32_t n_channels() const { return _sources.size(); }
275
276         const SourceList& sources ()        const { return _sources; }
277         const SourceList& master_sources () const { return _master_sources; }
278
279         std::vector<std::string> master_source_names();
280         void set_master_sources (const SourceList&);
281
282         /* automation */
283
284         virtual boost::shared_ptr<Evoral::Control>
285         control (const Evoral::Parameter& id, bool create=false) = 0;
286
287         virtual boost::shared_ptr<const Evoral::Control>
288         control (const Evoral::Parameter& id) const = 0;
289
290         /* tags */
291
292         std::string tags()    const { return _tags; }
293         virtual bool set_tags (const std::string& str) {
294                 if (_tags != str) {
295                         _tags = str;
296                         PropertyChanged (PBD::PropertyChange (Properties::tags));
297                 }
298                 return true;
299         }
300
301         /* serialization */
302
303         XMLNode&         get_state ();
304         virtual int      set_state (const XMLNode&, int version);
305
306         virtual boost::shared_ptr<Region> get_parent() const;
307
308         uint64_t layering_index () const { return _layering_index; }
309         void set_layering_index (uint64_t when) { _layering_index = when; }
310
311         virtual bool is_dependent() const { return false; }
312         virtual bool depends_on (boost::shared_ptr<Region> /*other*/) const { return false; }
313
314         virtual void add_transient (samplepos_t) {
315                 // no transients, but its OK
316         }
317
318         virtual void clear_transients () {
319                 // no transients, but its OK
320         }
321
322         virtual void update_transient (samplepos_t /* old_position */, samplepos_t /* new_position */) {
323                 // no transients, but its OK
324         }
325
326         virtual void remove_transient (samplepos_t /* where */) {
327                 // no transients, but its OK
328         }
329
330         virtual void set_onsets (AnalysisFeatureList&) {
331                 // no transients, but its OK
332         }
333
334         /** merges _onsets and _user_transients into given list
335          * and removed exact duplicates.
336          */
337         void transients (AnalysisFeatureList&);
338
339         /** merges _onsets OR _transients with _user_transients into given list
340          * if _onsets and _transients are unset, run analysis.
341          * list is not thinned, duplicates remain in place.
342          *
343          * intended for: Playlist::find_next_transient ()
344          */
345         virtual void get_transients (AnalysisFeatureList&) {
346                 // no transients, but its OK
347         }
348
349         /* wrapper to the above for easy access throug Lua */
350         AnalysisFeatureList transients () {
351                 AnalysisFeatureList rv;
352                 get_transients (rv);
353                 return rv;
354         }
355
356         bool has_transients () const;
357
358         virtual int separate_by_channel (std::vector< boost::shared_ptr<Region> >&) const {
359                 return -1;
360         }
361
362         void maybe_invalidate_transients ();
363
364         void drop_sources ();
365
366 protected:
367         virtual XMLNode& state ();
368
369         friend class RegionFactory;
370
371         /** Construct a region from multiple sources*/
372         Region (const SourceList& srcs);
373
374         /** Construct a region from another region */
375         Region (boost::shared_ptr<const Region>);
376
377         /** Construct a region from another region, at an offset within that region */
378         Region (boost::shared_ptr<const Region>, ARDOUR::MusicSample start_offset);
379
380         /** Construct a region as a copy of another region, but with different sources */
381         Region (boost::shared_ptr<const Region>, const SourceList&);
382
383         /** Constructor for derived types only */
384         Region (Session& s, samplepos_t start, samplecnt_t length, const std::string& name, DataType);
385
386         virtual bool can_trim_start_before_source_start () const {
387                 return false;
388         }
389
390 protected:
391
392         void send_change (const PBD::PropertyChange&);
393         virtual int _set_state (const XMLNode&, int version, PBD::PropertyChange& what_changed, bool send_signal);
394         void post_set (const PBD::PropertyChange&);
395         virtual void set_position_internal (samplepos_t pos, bool allow_bbt_recompute, const int32_t sub_num);
396         virtual void set_position_music_internal (double qn);
397         virtual void set_length_internal (samplecnt_t, const int32_t sub_num);
398         virtual void set_start_internal (samplecnt_t, const int32_t sub_num = 0);
399         bool verify_start_and_length (samplepos_t, samplecnt_t&);
400         void first_edit ();
401
402         DataType _type;
403
404         PBD::Property<bool>        _sync_marked;
405         PBD::Property<bool>        _left_of_split;
406         PBD::Property<bool>        _right_of_split;
407         PBD::Property<bool>        _valid_transients;
408         PBD::Property<samplepos_t> _start;
409         PBD::Property<samplecnt_t> _length;
410         PBD::Property<samplepos_t> _position;
411         PBD::Property<double>      _beat;
412         /** Sync position relative to the start of our file */
413         PBD::Property<samplepos_t> _sync_position;
414
415         double                  _quarter_note;
416
417         SourceList              _sources;
418         /** Used when timefx are applied, so we can always use the original source */
419         SourceList              _master_sources;
420
421         boost::weak_ptr<ARDOUR::Playlist> _playlist;
422
423         void merge_features (AnalysisFeatureList&, const AnalysisFeatureList&, const sampleoffset_t) const;
424
425         AnalysisFeatureList     _onsets; // used by the Ferret (Aubio OnsetDetector)
426
427         // _transient_user_start is covered by  _valid_transients
428         AnalysisFeatureList     _user_transients; // user added
429         samplepos_t             _transient_user_start; // region's _start relative to user_transients
430
431         // these are used by Playlist::find_next_transient() in absence of onsets
432         AnalysisFeatureList     _transients; // Source Analysis (QM Transient), user read-only
433         samplepos_t             _transient_analysis_start;
434         samplepos_t             _transient_analysis_end;
435
436         bool                    _soloSelected;
437
438 private:
439         void mid_thaw (const PBD::PropertyChange&);
440
441         virtual void trim_to_internal (samplepos_t position, samplecnt_t length, const int32_t sub_num);
442         void modify_front (samplepos_t new_position, bool reset_fade, const int32_t sub_num);
443         void modify_end (samplepos_t new_position, bool reset_fade, const int32_t sub_num);
444
445         void maybe_uncopy ();
446
447         bool verify_start (samplepos_t);
448         bool verify_start_mutable (samplepos_t&_start);
449         bool verify_length (samplecnt_t&);
450
451         virtual void recompute_at_start () = 0;
452         virtual void recompute_at_end () = 0;
453
454         PBD::Property<bool>        _muted;
455         PBD::Property<bool>        _opaque;
456         PBD::Property<bool>        _locked;
457         PBD::Property<bool>        _video_locked;
458         PBD::Property<bool>        _automatic;
459         PBD::Property<bool>        _whole_file;
460         PBD::Property<bool>        _import;
461         PBD::Property<bool>        _external;
462         PBD::Property<bool>        _hidden;
463         PBD::Property<bool>        _position_locked;
464         PBD::Property<samplepos_t> _ancestral_start;
465         PBD::Property<samplecnt_t> _ancestral_length;
466         PBD::Property<float>       _stretch;
467         PBD::Property<float>       _shift;
468         PBD::EnumProperty<PositionLockStyle> _position_lock_style;
469         PBD::Property<uint64_t>    _layering_index;
470         PBD::Property<std::string> _tags;
471
472         samplecnt_t             _last_length;
473         samplepos_t             _last_position;
474         mutable RegionEditState _first_edit;
475         layer_t                 _layer;
476
477         void register_properties ();
478
479         void use_sources (SourceList const &);
480 };
481
482 } /* namespace ARDOUR */
483
484 #endif /* __ardour_region_h__ */