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