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