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