fix merge with master
[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
31 #include "ardour/ardour.h"
32 #include "ardour/data_type.h"
33 #include "ardour/automatable.h"
34 #include "ardour/movable.h"
35 #include "ardour/readable.h"
36 #include "ardour/session_object.h"
37 #include "ardour/trimmable.h"
38
39 class XMLNode;
40
41
42 namespace ARDOUR {
43
44 namespace Properties {
45         extern PBD::PropertyDescriptor<bool>              muted;
46         extern PBD::PropertyDescriptor<bool>              opaque;
47         extern PBD::PropertyDescriptor<bool>              locked;
48         extern PBD::PropertyDescriptor<bool>              video_locked;
49         extern PBD::PropertyDescriptor<bool>              automatic;
50         extern PBD::PropertyDescriptor<bool>              whole_file;
51         extern PBD::PropertyDescriptor<bool>              import;
52         extern PBD::PropertyDescriptor<bool>              external;
53         extern PBD::PropertyDescriptor<bool>              sync_marked;
54         extern PBD::PropertyDescriptor<bool>              left_of_split;
55         extern PBD::PropertyDescriptor<bool>              right_of_split;
56         extern PBD::PropertyDescriptor<bool>              hidden;
57         extern PBD::PropertyDescriptor<bool>              position_locked;
58         extern PBD::PropertyDescriptor<bool>              valid_transients;
59         extern PBD::PropertyDescriptor<framepos_t>        start;
60         extern PBD::PropertyDescriptor<framecnt_t>        length;
61         extern PBD::PropertyDescriptor<framepos_t>        position;
62         extern PBD::PropertyDescriptor<framecnt_t>        sync_position;
63         extern PBD::PropertyDescriptor<layer_t>           layer;
64         extern PBD::PropertyDescriptor<framepos_t>        ancestral_start;
65         extern PBD::PropertyDescriptor<framecnt_t>        ancestral_length;
66         extern PBD::PropertyDescriptor<float>             stretch;
67         extern PBD::PropertyDescriptor<float>             shift;
68         extern PBD::PropertyDescriptor<PositionLockStyle> position_lock_style;
69         extern PBD::PropertyDescriptor<uint64_t>          layering_index;
70 };
71
72 class Playlist;
73 class Filter;
74 class ExportSpecification;
75 class Progress;
76
77 enum RegionEditState {
78         EditChangesNothing = 0,
79         EditChangesName    = 1,
80         EditChangesID      = 2
81 };
82
83
84 class 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         AnalysisFeatureList transients () { return _transients; };
106
107         /** How the region parameters play together:
108          *
109          * POSITION: first frame of the region along the timeline
110          * START:    first frame of the region within its source(s)
111          * LENGTH:   number of frames the region represents
112          */
113         framepos_t position ()  const { return _position; }
114         framepos_t start ()     const { return _start; }
115         framecnt_t length ()    const { return _length; }
116         layer_t    layer ()     const { return _layer; }
117
118         framecnt_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         framepos_t last_position () const { return _last_position; }
124         framecnt_t last_length ()   const { return _last_length; }
125
126         framepos_t ancestral_start ()  const { return _ancestral_start; }
127         framecnt_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 (framepos_t start, framecnt_t length, float stretch, float shift);
133
134         frameoffset_t sync_offset (int& dir) const;
135         framepos_t sync_position () const;
136         framepos_t sync_point () const;
137
138         framepos_t adjust_to_sync (framepos_t) const;
139
140         /* first_frame() is an alias; last_frame() just hides some math */
141
142         framepos_t first_frame () const { return _position; }
143         framepos_t last_frame ()  const { return _position + _length - 1; }
144
145         /** Return the earliest possible value of _position given the
146          *  value of _start within the region's sources
147          */
148         framepos_t earliest_possible_position () const;
149         /** Return the last possible value of _last_frame given the
150          *  value of _startin the regions's sources
151          */
152         framepos_t latest_possible_frame () const;
153
154         Evoral::Range<framepos_t> last_range () const {
155                 return Evoral::Range<framepos_t> (_last_position, _last_position + _last_length - 1);
156         }
157         
158         Evoral::Range<framepos_t> range () const {
159                 return Evoral::Range<framepos_t> (first_frame(), last_frame());
160         }
161
162         bool hidden ()           const { return _hidden; }
163         bool muted ()            const { return _muted; }
164         bool opaque ()           const { return _opaque; }
165         bool locked ()           const { return _locked; }
166         bool position_locked ()  const { return _position_locked; }
167         bool video_locked ()     const { return _video_locked; }
168         bool valid_transients () const { return _valid_transients; }
169         bool automatic ()        const { return _automatic; }
170         bool whole_file ()       const { return _whole_file; }
171         bool captured ()         const { return !(_import || _external); }
172         bool can_move ()         const { return !_position_locked && !_locked; }
173         bool sync_marked ()      const { return _sync_marked; }
174         bool external ()         const { return _external; }
175         bool import ()           const { return _import; }
176
177         Trimmable::CanTrim can_trim () const;
178
179         PositionLockStyle position_lock_style () const { return _position_lock_style; }
180
181         void set_position_lock_style (PositionLockStyle ps);
182         void recompute_position_from_lock_style ();
183
184         void suspend_property_changes ();
185
186         bool covers (framepos_t frame) const {
187                 return first_frame() <= frame && frame <= last_frame();
188         }
189
190         /** @return coverage of this region with the given range;
191          *  OverlapInternal: the range is internal to this region.
192          *  OverlapStart:    the range overlaps the start of this region.
193          *  OverlapEnd:      the range overlaps the end of this region.
194          *  OverlapExternal: the range overlaps all of this region.
195          */
196         Evoral::OverlapType coverage (framepos_t start, framepos_t end) const {
197                 return Evoral::coverage (first_frame(), last_frame(), start, end);
198         }
199
200         bool equivalent (boost::shared_ptr<const Region>) const;
201         bool size_equivalent (boost::shared_ptr<const Region>) const;
202         bool overlap_equivalent (boost::shared_ptr<const Region>) const;
203         bool region_list_equivalent (boost::shared_ptr<const Region>) const;
204         bool source_equivalent (boost::shared_ptr<const Region>) const;
205         bool any_source_equivalent (boost::shared_ptr<const Region>) const;
206         bool uses_source (boost::shared_ptr<const Source>) const;
207
208         std::string source_string () const;
209
210
211         /* EDITING OPERATIONS */
212
213         void set_length (framecnt_t);
214         void set_start (framepos_t);
215         void set_position (framepos_t);
216         void special_set_position (framepos_t);
217         virtual void update_after_tempo_map_change ();
218         void nudge_position (frameoffset_t);
219
220         bool at_natural_position () const;
221         void move_to_natural_position ();
222
223         void move_start (frameoffset_t distance);
224         void trim_front (framepos_t new_position);
225         void trim_end (framepos_t new_position);
226         void trim_to (framepos_t position, framecnt_t length);
227
228         void cut_front (framepos_t new_position);
229         void cut_end (framepos_t new_position);
230
231         void set_layer (layer_t l); /* ONLY Playlist can call this */
232         void raise ();
233         void lower ();
234         void raise_to_top ();
235         void lower_to_bottom ();
236
237         void set_sync_position (framepos_t n);
238         void clear_sync_position ();
239         void set_hidden (bool yn);
240         void set_muted (bool yn);
241         void set_whole_file (bool yn);
242         void set_automatic (bool yn);
243         void set_opaque (bool yn);
244         void set_locked (bool yn);
245         void set_video_locked (bool yn);
246         void set_position_locked (bool yn);
247
248         int apply (Filter &, Progress* progress = 0);
249
250         boost::shared_ptr<ARDOUR::Playlist> playlist () const { return _playlist.lock(); }
251         virtual void set_playlist (boost::weak_ptr<ARDOUR::Playlist>);
252
253         void source_deleted (boost::weak_ptr<Source>);
254
255         bool is_compound () const;
256
257         boost::shared_ptr<Source> source (uint32_t n=0) const { return _sources[ (n < _sources.size()) ? n : 0 ]; }
258         uint32_t n_channels() const { return _sources.size(); }
259
260         const SourceList& sources ()        const { return _sources; }
261         const SourceList& master_sources () const { return _master_sources; }
262
263         std::vector<std::string> master_source_names();
264         void set_master_sources (const SourceList&);
265
266         /* automation */
267
268         virtual boost::shared_ptr<Evoral::Control>
269         control (const Evoral::Parameter& id, bool create=false) = 0;
270
271         virtual boost::shared_ptr<const Evoral::Control>
272         control (const Evoral::Parameter& id) const = 0;
273
274         /* serialization */
275
276         XMLNode&         get_state ();
277         virtual XMLNode& state ();
278         virtual int      set_state (const XMLNode&, int version);
279
280         virtual boost::shared_ptr<Region> get_parent() const;
281
282         uint64_t layering_index () const { return _layering_index; }
283         void set_layering_index (uint64_t when) { _layering_index = when; }
284
285         virtual bool is_dependent() const { return false; }
286         virtual bool depends_on (boost::shared_ptr<Region> /*other*/) const { return false; }
287
288         virtual void add_transient (framepos_t) {
289                 // no transients, but its OK
290         }
291
292         virtual int update_transient (framepos_t /* old_position */, framepos_t /* new_position */) {
293                 // no transients, but its OK
294                 return 0;
295         }
296
297         virtual void remove_transient (framepos_t /* where */) {
298                 // no transients, but its OK
299         }
300
301         virtual int set_transients (AnalysisFeatureList&) {
302                 // no transients, but its OK
303                 return 0;
304         }
305
306         virtual int get_transients (AnalysisFeatureList&, bool force_new = false) {
307                 (void) force_new;
308                 // no transients, but its OK
309                 return 0;
310         }
311
312         virtual int adjust_transients (frameoffset_t /*delta*/) {
313                 // no transients, but its OK
314                 return 0;
315         }
316
317         virtual int separate_by_channel (ARDOUR::Session&,
318                         std::vector< boost::shared_ptr<Region> >&) const {
319                 return 0;
320         }
321
322         void invalidate_transients ();
323
324         void drop_sources ();
325
326   protected:
327         friend class RegionFactory;
328
329         /** Construct a region from multiple sources*/
330         Region (const SourceList& srcs);
331
332         /** Construct a region from another region */
333         Region (boost::shared_ptr<const Region>);
334
335         /** Construct a region from another region, at an offset within that region */
336         Region (boost::shared_ptr<const Region>, frameoffset_t start_offset);
337
338         /** Construct a region as a copy of another region, but with different sources */
339         Region (boost::shared_ptr<const Region>, const SourceList&);
340
341         /** Constructor for derived types only */
342         Region (Session& s, framepos_t start, framecnt_t length, const std::string& name, DataType);
343
344         virtual bool can_trim_start_before_source_start () const {
345                 return false;
346         }
347
348   protected:
349
350         void send_change (const PBD::PropertyChange&);
351         virtual int _set_state (const XMLNode&, int version, PBD::PropertyChange& what_changed, bool send_signal);
352         void post_set (const PBD::PropertyChange&);
353         virtual void set_position_internal (framepos_t pos, bool allow_bbt_recompute);
354         virtual void set_length_internal (framecnt_t);
355         virtual void set_start_internal (framecnt_t);
356         
357         DataType _type;
358
359         PBD::Property<bool>        _sync_marked;
360         PBD::Property<bool>        _left_of_split;
361         PBD::Property<bool>        _right_of_split;
362         PBD::Property<bool>        _valid_transients;
363         PBD::Property<framepos_t>  _start;
364         PBD::Property<framecnt_t>  _length;
365         PBD::Property<framepos_t>  _position;
366         /** Sync position relative to the start of our file */
367         PBD::Property<framepos_t>  _sync_position;
368         
369         SourceList              _sources;
370         /** Used when timefx are applied, so we can always use the original source */
371         SourceList              _master_sources;
372
373         AnalysisFeatureList     _transients;
374         
375         boost::weak_ptr<ARDOUR::Playlist> _playlist;
376         
377   private:
378         void mid_thaw (const PBD::PropertyChange&);
379
380         void trim_to_internal (framepos_t position, framecnt_t length);
381         void modify_front (framepos_t new_position, bool reset_fade);
382         void modify_end (framepos_t new_position, bool reset_fade);
383
384         void maybe_uncopy ();
385         void first_edit ();
386
387         bool verify_start (framepos_t);
388         bool verify_start_and_length (framepos_t, framecnt_t&);
389         bool verify_start_mutable (framepos_t&_start);
390         bool verify_length (framecnt_t);
391
392         virtual void recompute_at_start () = 0;
393         virtual void recompute_at_end () = 0;
394
395         PBD::Property<bool>        _muted;
396         PBD::Property<bool>        _opaque;
397         PBD::Property<bool>        _locked;
398         PBD::Property<bool>        _video_locked;
399         PBD::Property<bool>        _automatic;
400         PBD::Property<bool>        _whole_file;
401         PBD::Property<bool>        _import;
402         PBD::Property<bool>        _external;
403         PBD::Property<bool>        _hidden;
404         PBD::Property<bool>        _position_locked;
405         PBD::Property<framepos_t>  _ancestral_start;
406         PBD::Property<framecnt_t>  _ancestral_length;
407         PBD::Property<float>       _stretch;
408         PBD::Property<float>       _shift;
409         PBD::EnumProperty<PositionLockStyle> _position_lock_style;
410         PBD::Property<uint64_t>    _layering_index;
411
412         framecnt_t              _last_length;
413         framepos_t              _last_position;
414         mutable RegionEditState _first_edit;
415         Timecode::BBT_Time      _bbt_time;
416         layer_t                 _layer;
417
418         void register_properties ();
419
420         void use_sources (SourceList const &);
421 };
422
423 } /* namespace ARDOUR */
424
425 #endif /* __ardour_region_h__ */