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